proc.h revision 1.53
1104476Ssam/*	$NetBSD: proc.h,v 1.53 1997/10/10 08:19:50 mycroft Exp $	*/
2104476Ssam
3104476Ssam/*-
4104476Ssam * Copyright (c) 1986, 1989, 1991, 1993
5104476Ssam *	The Regents of the University of California.  All rights reserved.
6104476Ssam * (c) UNIX System Laboratories, Inc.
7104476Ssam * All or some portions of this file are derived from material licensed
8104476Ssam * to the University of California by American Telephone and Telegraph
9104476Ssam * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10104476Ssam * the permission of UNIX System Laboratories, Inc.
11104476Ssam *
12104476Ssam * Redistribution and use in source and binary forms, with or without
13104476Ssam * modification, are permitted provided that the following conditions
14104476Ssam * are met:
15104476Ssam * 1. Redistributions of source code must retain the above copyright
16104476Ssam *    notice, this list of conditions and the following disclaimer.
17104476Ssam * 2. Redistributions in binary form must reproduce the above copyright
18104476Ssam *    notice, this list of conditions and the following disclaimer in the
19104476Ssam *    documentation and/or other materials provided with the distribution.
20104476Ssam * 3. All advertising materials mentioning features or use of this software
21104476Ssam *    must display the following acknowledgement:
22104476Ssam *	This product includes software developed by the University of
23104476Ssam *	California, Berkeley and its contributors.
24104476Ssam * 4. Neither the name of the University nor the names of its contributors
25104476Ssam *    may be used to endorse or promote products derived from this software
26104476Ssam *    without specific prior written permission.
27104476Ssam *
28104476Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29104476Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30116191Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31116191Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32116191Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33104476Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34104476Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35104476Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36104476Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37104476Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38104476Ssam * SUCH DAMAGE.
39104476Ssam *
40104476Ssam *	@(#)proc.h	8.8 (Berkeley) 1/21/94
41104476Ssam */
42104476Ssam
43104476Ssam#ifndef _SYS_PROC_H_
44104476Ssam#define	_SYS_PROC_H_
45104476Ssam
46104476Ssam#include <machine/proc.h>		/* Machine-dependent proc substruct. */
47104476Ssam#include <sys/queue.h>
48104476Ssam
49104476Ssam/*
50104476Ssam * One structure allocated per session.
51104476Ssam */
52104476Ssamstruct	session {
53104476Ssam	int	s_count;		/* Ref cnt; pgrps in session. */
54104476Ssam	struct	proc *s_leader;		/* Session leader. */
55104476Ssam	struct	vnode *s_ttyvp;		/* Vnode of controlling terminal. */
56104476Ssam	struct	tty *s_ttyp;		/* Controlling terminal. */
57104476Ssam	char	s_login[MAXLOGNAME];	/* Setlogin() name. */
58104476Ssam};
59104476Ssam
60104476Ssam/*
61104476Ssam * One structure allocated per process group.
62104476Ssam */
63104476Ssamstruct	pgrp {
64104476Ssam	LIST_ENTRY(pgrp) pg_hash;	/* Hash chain. */
65104476Ssam	LIST_HEAD(, proc) pg_members;	/* Pointer to pgrp members. */
66104476Ssam	struct	session *pg_session;	/* Pointer to session. */
67104476Ssam	pid_t	pg_id;			/* Pgrp id. */
68104476Ssam	int	pg_jobc;	/* # procs qualifying pgrp for job control */
69104476Ssam};
70104476Ssam
71104476Ssam/*
72104476Ssam * One structure allocated per emulation.
73104476Ssam */
74104476Ssamstruct exec_package;
75104476Ssamstruct ps_strings;
76104476Ssam
77104476Ssamstruct	emul {
78104476Ssam	char	e_name[8];		/* Symbolic name */
79104476Ssam	int	*e_errno;		/* Errno array */
80104476Ssam					/* Signal sending function */
81104476Ssam	void	(*e_sendsig) __P((sig_t, int, int, u_long));
82104476Ssam	int	e_nosys;		/* Offset of the nosys() syscall */
83104476Ssam	int	e_nsysent;		/* Number of system call entries */
84104476Ssam	struct sysent *e_sysent;	/* System call array */
85104476Ssam	char	**e_syscallnames;	/* System call name array */
86104476Ssam	int	e_arglen;		/* Extra argument size in words */
87104476Ssam					/* Copy arguments on the stack */
88104476Ssam	void	*(*e_copyargs) __P((struct exec_package *, struct ps_strings *,
89104476Ssam				    void *, void *));
90104476Ssam					/* Set registers before execution */
91104476Ssam	void	(*e_setregs) __P((struct proc *, struct exec_package *,
92104476Ssam				  u_long));
93104476Ssam	char	*e_sigcode;		/* Start of sigcode */
94104476Ssam	char	*e_esigcode;		/* End of sigcode */
95104476Ssam};
96104476Ssam
97104476Ssam/*
98104476Ssam * Description of a process.
99104476Ssam *
100104476Ssam * This structure contains the information needed to manage a thread of
101104476Ssam * control, known in UN*X as a process; it has references to substructures
102104476Ssam * containing descriptions of things that the process uses, but may share
103104476Ssam * with related processes.  The process structure and the substructures
104104476Ssam * are always addressible except for those marked "(PROC ONLY)" below,
105104476Ssam * which might be addressible only on a processor on which the process
106104476Ssam * is running.
107104476Ssam */
108104476Ssamstruct	proc {
109104476Ssam	struct	proc *p_forw;		/* Doubly-linked run/sleep queue. */
110104476Ssam	struct	proc *p_back;
111104476Ssam	LIST_ENTRY(proc) p_list;	/* List of all processes. */
112104476Ssam
113104476Ssam	/* substructures: */
114104476Ssam	struct	pcred *p_cred;		/* Process owner's identity. */
115104476Ssam	struct	filedesc *p_fd;		/* Ptr to open files structure. */
116104476Ssam	struct	pstats *p_stats;	/* Accounting/statistics (PROC ONLY). */
117104476Ssam	struct	plimit *p_limit;	/* Process limits. */
118104476Ssam	struct	vmspace *p_vmspace;	/* Address space. */
119104476Ssam	struct	sigacts *p_sigacts;	/* Signal actions, state (PROC ONLY). */
120104476Ssam
121104476Ssam#define	p_ucred		p_cred->pc_ucred
122104476Ssam#define	p_rlimit	p_limit->pl_rlimit
123104476Ssam
124104476Ssam	int	p_flag;			/* P_* flags. */
125104476Ssam	u_char	p_unused;		/* XXX: used to be emulation flag */
126104476Ssam	char	p_stat;			/* S* process status. */
127104476Ssam	char	p_pad1[2];
128104476Ssam
129104476Ssam	pid_t	p_pid;			/* Process identifier. */
130104476Ssam	LIST_ENTRY(proc) p_hash;	/* Hash chain. */
131104476Ssam	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp. */
132104476Ssam	struct	proc *p_pptr;	 	/* Pointer to parent process. */
133104476Ssam	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes. */
134104476Ssam	LIST_HEAD(, proc) p_children;	/* Pointer to list of children. */
135104476Ssam
136104476Ssam/* The following fields are all zeroed upon creation in fork. */
137104476Ssam#define	p_startzero	p_oppid
138104476Ssam
139104476Ssam	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	const 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	short   p_locks;		/* DEBUG: lockmgr count of held locks */
165	short   p_simple_locks;		/* DEBUG: count of held simple locks */
166
167	int	p_holdcnt;		/* If non-zero, don't swap. */
168	struct	emul *p_emul;		/* Emulation information */
169
170	long	p_spare[1];		/* pad to 256, avoid shifting eproc. */
171
172
173/* End area that is zeroed on creation. */
174#define	p_endzero	p_startcopy
175
176/* The following fields are all copied upon creation in fork. */
177#define	p_startcopy	p_sigmask
178
179	sigset_t p_sigmask;	/* Current signal mask. */
180	sigset_t p_sigignore;	/* Signals being ignored. */
181	sigset_t p_sigcatch;	/* Signals being caught by user. */
182
183	u_char	p_priority;	/* Process priority. */
184	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
185	u_char	p_nice;		/* Process "nice" value. */
186	char	p_comm[MAXCOMLEN+1];
187
188	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
189
190/* End area that is copied on creation. */
191#define	p_endcopy	p_thread
192
193	void	*p_thread;	/* Id for this "thread"; Mach glue. XXX */
194	struct	user *p_addr;	/* Kernel virtual addr of u-area (PROC ONLY). */
195	struct	mdproc p_md;	/* Any machine-dependent fields. */
196
197	u_short	p_xstat;	/* Exit status for wait; also stop signal. */
198	u_short	p_acflag;	/* Accounting flags. */
199	struct	rusage *p_ru;	/* Exit information. XXX */
200};
201
202#define	p_session	p_pgrp->pg_session
203#define	p_pgid		p_pgrp->pg_id
204
205/* Status values. */
206#define	SIDL	1		/* Process being created by fork. */
207#define	SRUN	2		/* Currently runnable. */
208#define	SSLEEP	3		/* Sleeping on an address. */
209#define	SSTOP	4		/* Process debugging or suspension. */
210#define	SZOMB	5		/* Awaiting collection by parent. */
211
212/* These flags are kept in p_flag. */
213#define	P_ADVLOCK	0x00001	/* Process may hold a POSIX advisory lock. */
214#define	P_CONTROLT	0x00002	/* Has a controlling terminal. */
215#define	P_INMEM		0x00004	/* Loaded into memory. */
216#define	P_NOCLDSTOP	0x00008	/* No SIGCHLD when children stop. */
217#define	P_PPWAIT	0x00010	/* Parent is waiting for child to exec/exit. */
218#define	P_PROFIL	0x00020	/* Has started profiling. */
219#define	P_SELECT	0x00040	/* Selecting; wakeup/waiting danger. */
220#define	P_SINTR		0x00080	/* Sleep is interruptible. */
221#define	P_SUGID		0x00100	/* Had set id privileges since last exec. */
222#define	P_SYSTEM	0x00200	/* System proc: no sigs, stats or swapping. */
223#define	P_TIMEOUT	0x00400	/* Timing out during sleep. */
224#define	P_TRACED	0x00800	/* Debugged process being traced. */
225#define	P_WAITED	0x01000	/* Debugging process has waited for child. */
226#define	P_WEXIT		0x02000	/* Working on exiting. */
227#define	P_EXEC		0x04000	/* Process called exec. */
228#define	P_FSTRACE	0x10000	/* Don't send SIGCHLD to parent on stop. */
229
230/* Should be moved to machine-dependent areas. */
231#define	P_OWEUPC	0x08000	/* Owe process an addupc() call at next ast. */
232#define	P_SSTEP		0x20000	/* Process needs single-step fixup. */
233
234/*
235 * MOVE TO ucred.h?
236 *
237 * Shareable process credentials (always resident).  This includes a reference
238 * to the current user credentials as well as real and saved ids that may be
239 * used to change ids.
240 */
241struct	pcred {
242	struct	ucred *pc_ucred;	/* Current credentials. */
243	uid_t	p_ruid;			/* Real user id. */
244	uid_t	p_svuid;		/* Saved effective user id. */
245	gid_t	p_rgid;			/* Real group id. */
246	gid_t	p_svgid;		/* Saved effective group id. */
247	int	p_refcnt;		/* Number of references. */
248};
249
250#ifdef _KERNEL
251/*
252 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
253 * as it is used to represent "no process group".
254 */
255#define	PID_MAX		30000
256#define	NO_PID		30001
257
258#define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
259#define	SESSHOLD(s)	((s)->s_count++)
260#define	SESSRELE(s) {							\
261	if (--(s)->s_count == 0)					\
262		FREE(s, M_SESSION);					\
263}
264
265#define	PHOLD(p) {							\
266	if ((p)->p_holdcnt++ == 0 && ((p)->p_flag & P_INMEM) == 0)	\
267		swapin(p);						\
268}
269#define	PRELE(p)	(--(p)->p_holdcnt)
270
271#define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
272extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
273extern u_long pidhash;
274
275#define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
276extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
277extern u_long pgrphash;
278
279extern struct proc *curproc;		/* Current running proc. */
280extern struct proc proc0;		/* Process slot for swapper. */
281extern int nprocs, maxproc;		/* Current and max number of procs. */
282
283LIST_HEAD(proclist, proc);
284extern struct proclist allproc;		/* List of all processes. */
285extern struct proclist zombproc;	/* List of zombie processes. */
286struct proc *initproc;			/* Process slots for init, pager. */
287
288#define	NQS	32			/* 32 run queues. */
289int	whichqs;			/* Bit mask summary of non-empty Q's. */
290struct	prochd {
291	struct	proc *ph_link;		/* Linked list of running processes. */
292	struct	proc *ph_rlink;
293} qs[NQS];
294
295struct proc *pfind __P((pid_t));	/* Find process by id. */
296struct pgrp *pgfind __P((pid_t));	/* Find process group by id. */
297
298int	chgproccnt __P((uid_t uid, int diff));
299int	enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
300void	fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
301int	inferior __P((struct proc *p));
302int	leavepgrp __P((struct proc *p));
303void	mi_switch __P((void));
304void	pgdelete __P((struct pgrp *pgrp));
305void	procinit __P((void));
306void	remrunqueue __P((struct proc *));
307void	resetpriority __P((struct proc *));
308void	setrunnable __P((struct proc *));
309void	setrunqueue __P((struct proc *));
310void	sleep __P((void *chan, int pri));
311void	swapin __P((struct proc *));
312int	tsleep __P((void *chan, int pri, const char *wmesg, int timo));
313void	unsleep __P((struct proc *));
314void	wakeup __P((void *chan));
315void	exit1 __P((struct proc *, int));
316int	fork1 __P((struct proc *, int, register_t *));
317void	kmeminit __P((void));
318void	rqinit __P((void));
319int	groupmember __P((gid_t, struct ucred *));
320void	cpu_switch __P((struct proc *));
321void	cpu_wait __P((struct proc *));
322void	cpu_exit __P((struct proc *));
323#endif	/* _KERNEL */
324#endif	/* !_SYS_PROC_H_ */
325