proc.h revision 1.46
1/*	$NetBSD: proc.h,v 1.46 1996/10/02 18:05:09 ws Exp $	*/
2
3/*-
4 * Copyright (c) 1986, 1989, 1991, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *	This product includes software developed by the University of
23 *	California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 *    may be used to endorse or promote products derived from this software
26 *    without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 *	@(#)proc.h	8.8 (Berkeley) 1/21/94
41 */
42
43#ifndef _SYS_PROC_H_
44#define	_SYS_PROC_H_
45
46#include <machine/proc.h>		/* Machine-dependent proc substruct. */
47#include <sys/queue.h>
48
49/*
50 * One structure allocated per session.
51 */
52struct	session {
53	int	s_count;		/* Ref cnt; pgrps in session. */
54	struct	proc *s_leader;		/* Session leader. */
55	struct	vnode *s_ttyvp;		/* Vnode of controlling terminal. */
56	struct	tty *s_ttyp;		/* Controlling terminal. */
57	char	s_login[MAXLOGNAME];	/* Setlogin() name. */
58};
59
60/*
61 * One structure allocated per process group.
62 */
63struct	pgrp {
64	LIST_ENTRY(pgrp) pg_hash;	/* Hash chain. */
65	LIST_HEAD(, proc) pg_members;	/* Pointer to pgrp members. */
66	struct	session *pg_session;	/* Pointer to session. */
67	pid_t	pg_id;			/* Pgrp id. */
68	int	pg_jobc;	/* # procs qualifying pgrp for job control */
69};
70
71/*
72 * One structure allocated per emulation.
73 */
74struct exec_package;
75struct ps_strings;
76
77struct	emul {
78	char	e_name[8];		/* Symbolic name */
79	int	*e_errno;		/* Errno array */
80					/* Signal sending function */
81	void	(*e_sendsig) __P((sig_t, int, int, u_long));
82	int	e_nosys;		/* Offset of the nosys() syscall */
83	int	e_nsysent;		/* Number of system call entries */
84	struct sysent *e_sysent;	/* System call array */
85	char	**e_syscallnames;	/* System call name array */
86	int	e_arglen;		/* Extra argument size in words */
87					/* Copy arguments on the stack */
88	void	*(*e_copyargs) __P((struct exec_package *, struct ps_strings *,
89				    void *, void *));
90					/* Set registers before execution */
91	void	(*e_setregs) __P((struct proc *, struct exec_package *,
92				  u_long, register_t *));
93	char	*e_sigcode;		/* Start of sigcode */
94	char	*e_esigcode;		/* End of sigcode */
95};
96
97/*
98 * Description of a process.
99 *
100 * This structure contains the information needed to manage a thread of
101 * control, known in UN*X as a process; it has references to substructures
102 * containing descriptions of things that the process uses, but may share
103 * with related processes.  The process structure and the substructures
104 * are always addressible except for those marked "(PROC ONLY)" below,
105 * which might be addressible only on a processor on which the process
106 * is running.
107 */
108struct	proc {
109	struct	proc *p_forw;		/* Doubly-linked run/sleep queue. */
110	struct	proc *p_back;
111	LIST_ENTRY(proc) p_list;	/* List of all processes. */
112
113	/* substructures: */
114	struct	pcred *p_cred;		/* Process owner's identity. */
115	struct	filedesc *p_fd;		/* Ptr to open files structure. */
116	struct	pstats *p_stats;	/* Accounting/statistics (PROC ONLY). */
117	struct	plimit *p_limit;	/* Process limits. */
118	struct	vmspace *p_vmspace;	/* Address space. */
119	struct	sigacts *p_sigacts;	/* Signal actions, state (PROC ONLY). */
120
121#define	p_ucred		p_cred->pc_ucred
122#define	p_rlimit	p_limit->pl_rlimit
123
124	int	p_flag;			/* P_* flags. */
125	u_char	p_unused;		/* XXX: used to be emulation flag */
126	char	p_stat;			/* S* process status. */
127	char	p_pad1[2];
128
129	pid_t	p_pid;			/* Process identifier. */
130	LIST_ENTRY(proc) p_hash;	/* Hash chain. */
131	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp. */
132	struct	proc *p_pptr;	 	/* Pointer to parent process. */
133	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes. */
134	LIST_HEAD(, proc) p_children;	/* Pointer to list of children. */
135
136/* The following fields are all zeroed upon creation in fork. */
137#define	p_startzero	p_oppid
138
139	pid_t	p_oppid;	 /* Save parent pid during ptrace. XXX */
140	int	p_dupfd;	 /* Sideways return value from filedescopen. XXX */
141
142	/* scheduling */
143	u_int	p_estcpu;	 /* Time averaged value of p_cpticks. */
144	int	p_cpticks;	 /* Ticks of cpu time. */
145	fixpt_t	p_pctcpu;	 /* %cpu for this process during p_swtime */
146	void	*p_wchan;	 /* Sleep address. */
147	char	*p_wmesg;	 /* Reason for sleep. */
148	u_int	p_swtime;	 /* Time swapped in or out. */
149	u_int	p_slptime;	 /* Time since last blocked. */
150
151	struct	itimerval p_realtimer;	/* Alarm timer. */
152	struct	timeval p_rtime;	/* Real time. */
153	u_quad_t p_uticks;		/* Statclock hits in user mode. */
154	u_quad_t p_sticks;		/* Statclock hits in system mode. */
155	u_quad_t p_iticks;		/* Statclock hits processing intr. */
156
157	int	p_traceflag;		/* Kernel trace points. */
158	struct	vnode *p_tracep;	/* Trace to vnode. */
159
160	int	p_siglist;		/* Signals arrived but not delivered. */
161
162	struct	vnode *p_textvp;	/* Vnode of executable. */
163
164	int	p_holdcnt;		/* If non-zero, don't swap. */
165	struct	emul *p_emul;		/* Emulation information */
166
167	long	p_spare[1];		/* pad to 256, avoid shifting eproc. */
168
169
170/* End area that is zeroed on creation. */
171#define	p_endzero	p_startcopy
172
173/* The following fields are all copied upon creation in fork. */
174#define	p_startcopy	p_sigmask
175
176	sigset_t p_sigmask;	/* Current signal mask. */
177	sigset_t p_sigignore;	/* Signals being ignored. */
178	sigset_t p_sigcatch;	/* Signals being caught by user. */
179
180	u_char	p_priority;	/* Process priority. */
181	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
182	u_char	p_nice;		/* Process "nice" value. */
183	char	p_comm[MAXCOMLEN+1];
184
185	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
186
187/* End area that is copied on creation. */
188#define	p_endcopy	p_thread
189
190	void	*p_thread;	/* Id for this "thread"; Mach glue. XXX */
191	struct	user *p_addr;	/* Kernel virtual addr of u-area (PROC ONLY). */
192	struct	mdproc p_md;	/* Any machine-dependent fields. */
193
194	u_short	p_xstat;	/* Exit status for wait; also stop signal. */
195	u_short	p_acflag;	/* Accounting flags. */
196	struct	rusage *p_ru;	/* Exit information. XXX */
197};
198
199#define	p_session	p_pgrp->pg_session
200#define	p_pgid		p_pgrp->pg_id
201
202/* Status values. */
203#define	SIDL	1		/* Process being created by fork. */
204#define	SRUN	2		/* Currently runnable. */
205#define	SSLEEP	3		/* Sleeping on an address. */
206#define	SSTOP	4		/* Process debugging or suspension. */
207#define	SZOMB	5		/* Awaiting collection by parent. */
208
209/* These flags are kept in p_flag. */
210#define	P_ADVLOCK	0x00001	/* Process may hold a POSIX advisory lock. */
211#define	P_CONTROLT	0x00002	/* Has a controlling terminal. */
212#define	P_INMEM		0x00004	/* Loaded into memory. */
213#define	P_NOCLDSTOP	0x00008	/* No SIGCHLD when children stop. */
214#define	P_PPWAIT	0x00010	/* Parent is waiting for child to exec/exit. */
215#define	P_PROFIL	0x00020	/* Has started profiling. */
216#define	P_SELECT	0x00040	/* Selecting; wakeup/waiting danger. */
217#define	P_SINTR		0x00080	/* Sleep is interruptible. */
218#define	P_SUGID		0x00100	/* Had set id privileges since last exec. */
219#define	P_SYSTEM	0x00200	/* System proc: no sigs, stats or swapping. */
220#define	P_TIMEOUT	0x00400	/* Timing out during sleep. */
221#define	P_TRACED	0x00800	/* Debugged process being traced. */
222#define	P_WAITED	0x01000	/* Debugging process has waited for child. */
223#define	P_WEXIT		0x02000	/* Working on exiting. */
224#define	P_EXEC		0x04000	/* Process called exec. */
225
226/* Should be moved to machine-dependent areas. */
227#define	P_OWEUPC	0x08000	/* Owe process an addupc() call at next ast. */
228
229/* XXX Not sure what to do with these, yet. */
230#define	P_FSTRACE	0x10000	/* tracing via file system (elsewhere?) */
231#define	P_SSTEP		0x20000	/* process needs single-step fixup ??? */
232
233/*
234 * MOVE TO ucred.h?
235 *
236 * Shareable process credentials (always resident).  This includes a reference
237 * to the current user credentials as well as real and saved ids that may be
238 * used to change ids.
239 */
240struct	pcred {
241	struct	ucred *pc_ucred;	/* Current credentials. */
242	uid_t	p_ruid;			/* Real user id. */
243	uid_t	p_svuid;		/* Saved effective user id. */
244	gid_t	p_rgid;			/* Real group id. */
245	gid_t	p_svgid;		/* Saved effective group id. */
246	int	p_refcnt;		/* Number of references. */
247};
248
249#ifdef _KERNEL
250/*
251 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
252 * as it is used to represent "no process group".
253 */
254#define	PID_MAX		30000
255#define	NO_PID		30001
256
257#define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
258#define	SESSHOLD(s)	((s)->s_count++)
259#define	SESSRELE(s) {							\
260	if (--(s)->s_count == 0)					\
261		FREE(s, M_SESSION);					\
262}
263
264#define	PHOLD(p) {							\
265	if ((p)->p_holdcnt++ == 0 && ((p)->p_flag & P_INMEM) == 0)	\
266		swapin(p);						\
267}
268#define	PRELE(p)	(--(p)->p_holdcnt)
269
270#define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
271extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
272extern u_long pidhash;
273
274#define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
275extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
276extern u_long pgrphash;
277
278extern struct proc *curproc;		/* Current running proc. */
279extern struct proc proc0;		/* Process slot for swapper. */
280extern int nprocs, maxproc;		/* Current and max number of procs. */
281
282LIST_HEAD(proclist, proc);
283extern struct proclist allproc;		/* List of all processes. */
284extern struct proclist zombproc;	/* List of zombie processes. */
285struct proc *initproc, *pageproc;	/* Process slots for init, pager. */
286
287#define	NQS	32			/* 32 run queues. */
288int	whichqs;			/* Bit mask summary of non-empty Q's. */
289struct	prochd {
290	struct	proc *ph_link;		/* Linked list of running processes. */
291	struct	proc *ph_rlink;
292} qs[NQS];
293
294struct proc *pfind __P((pid_t));	/* Find process by id. */
295struct pgrp *pgfind __P((pid_t));	/* Find process group by id. */
296
297int	chgproccnt __P((uid_t uid, int diff));
298int	enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
299void	fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
300int	inferior __P((struct proc *p));
301int	leavepgrp __P((struct proc *p));
302void	mi_switch __P((void));
303void	pgdelete __P((struct pgrp *pgrp));
304void	procinit __P((void));
305void	resetpriority __P((struct proc *));
306void	setrunnable __P((struct proc *));
307void	setrunqueue __P((struct proc *));
308void	sleep __P((void *chan, int pri));
309void	swapin __P((struct proc *));
310int	tsleep __P((void *chan, int pri, char *wmesg, int timo));
311void	unsleep __P((struct proc *));
312void	wakeup __P((void *chan));
313void	exit1 __P((struct proc *, int));
314int	fork1 __P((struct proc *, int, register_t *));
315void	kmeminit __P((void));
316void	rqinit __P((void));
317int	groupmember __P((gid_t, struct ucred *));
318void	cpu_switch __P((struct proc *));
319void	cpu_wait __P((struct proc *));
320void	cpu_exit __P((struct proc *));
321#endif	/* _KERNEL */
322#endif	/* !_SYS_PROC_H_ */
323