proc.h revision 1.89
1/*	$NetBSD: proc.h,v 1.89 2000/03/28 06:01:05 simonb 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.15 (Berkeley) 5/19/95
41 */
42
43#ifndef _SYS_PROC_H_
44#define	_SYS_PROC_H_
45
46#if defined(_KERNEL) && !defined(_LKM)
47#include "opt_multiprocessor.h"
48#endif
49
50#if defined(MULTIPROCESSOR)
51#include <machine/cpu.h>		/* curcpu() and cpu_info */
52#endif
53#include <machine/proc.h>		/* Machine-dependent proc substruct. */
54#include <sys/lock.h>
55#include <sys/queue.h>
56#include <sys/callout.h>
57
58/*
59 * One structure allocated per session.
60 */
61struct	session {
62	int	s_count;		/* Ref cnt; pgrps in session. */
63	struct	proc *s_leader;		/* Session leader. */
64	struct	vnode *s_ttyvp;		/* Vnode of controlling terminal. */
65	struct	tty *s_ttyp;		/* Controlling terminal. */
66	char	s_login[MAXLOGNAME];	/* Setlogin() name. */
67	pid_t	s_sid;			/* session ID (pid of leader) */
68};
69
70/*
71 * One structure allocated per process group.
72 */
73struct	pgrp {
74	LIST_ENTRY(pgrp) pg_hash;	/* Hash chain. */
75	LIST_HEAD(, proc) pg_members;	/* Pointer to pgrp members. */
76	struct	session *pg_session;	/* Pointer to session. */
77	pid_t	pg_id;			/* Pgrp id. */
78	int	pg_jobc;	/* # procs qualifying pgrp for job control */
79};
80
81/*
82 * One structure allocated per emulation.
83 */
84struct exec_package;
85struct ps_strings;
86
87struct	emul {
88	char	e_name[8];		/* Symbolic name */
89	int	*e_errno;		/* Errno array */
90					/* Signal sending function */
91	void	(*e_sendsig) __P((sig_t, int, sigset_t *, u_long));
92	int	e_nosys;		/* Offset of the nosys() syscall */
93	int	e_nsysent;		/* Number of system call entries */
94	struct sysent *e_sysent;	/* System call array */
95	char	**e_syscallnames;	/* System call name array */
96	int	e_arglen;		/* Extra argument size in words */
97					/* Copy arguments on the new stack */
98	void	*(*e_copyargs) __P((struct exec_package *, struct ps_strings *,
99				    void *, void *));
100					/* Set registers before execution */
101	void	(*e_setregs) __P((struct proc *, struct exec_package *,
102				  u_long));
103
104	char	*e_sigcode;		/* Start of sigcode */
105	char	*e_esigcode;		/* End of sigcode */
106};
107
108/*
109 * Description of a process.
110 *
111 * This structure contains the information needed to manage a thread of
112 * control, known in UN*X as a process; it has references to substructures
113 * containing descriptions of things that the process uses, but may share
114 * with related processes.  The process structure and the substructures
115 * are always addressible except for those marked "(PROC ONLY)" below,
116 * which might be addressible only on a processor on which the process
117 * is running.
118 */
119struct	proc {
120	struct	proc *p_forw;		/* Doubly-linked run/sleep queue. */
121	struct	proc *p_back;
122	LIST_ENTRY(proc) p_list;	/* List of all processes. */
123
124	/* substructures: */
125	struct	pcred *p_cred;		/* Process owner's identity. */
126	struct	filedesc *p_fd;		/* Ptr to open files structure. */
127	struct	cwdinfo *p_cwdi;	/* cdir/rdir/cmask info */
128	struct	pstats *p_stats;	/* Accounting/statistics (PROC ONLY). */
129	struct	plimit *p_limit;	/* Process limits. */
130	struct	vmspace *p_vmspace;	/* Address space. */
131	struct	sigacts *p_sigacts;	/* Signal actions, state (PROC ONLY). */
132
133#define	p_ucred		p_cred->pc_ucred
134#define	p_rlimit	p_limit->pl_rlimit
135
136	int	p_exitsig;		/* signal to sent to parent on exit */
137	int	p_flag;			/* P_* flags. */
138	u_char	p_unused;		/* XXX: used to be emulation flag */
139	char	p_stat;			/* S* process status. */
140	char	p_pad1[2];
141
142	pid_t	p_pid;			/* Process identifier. */
143	LIST_ENTRY(proc) p_hash;	/* Hash chain. */
144	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp. */
145	struct	proc *p_pptr;	 	/* Pointer to parent process. */
146	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes. */
147	LIST_HEAD(, proc) p_children;	/* Pointer to list of children. */
148
149/* The following fields are all zeroed upon creation in fork. */
150#define	p_startzero	p_oppid
151
152	pid_t	p_oppid;	 /* Save parent pid during ptrace. XXX */
153	int	p_dupfd;	 /* Sideways return value from filedescopen. XXX */
154
155	/* scheduling */
156	u_int	p_estcpu;	 /* Time averaged value of p_cpticks. XXX belongs in p_startcopy section */
157	int	p_cpticks;	 /* Ticks of cpu time. */
158	fixpt_t	p_pctcpu;	 /* %cpu for this process during p_swtime */
159	void	*p_wchan;	 /* Sleep address. */
160	struct callout p_tsleep_ch;/* callout for tsleep */
161	const char *p_wmesg;	 /* Reason for sleep. */
162	u_int	p_swtime;	 /* Time swapped in or out. */
163	u_int	p_slptime;	 /* Time since last blocked. */
164	int	p_schedflags;	 /* PSCHED_* flags */
165
166	struct	callout p_realit_ch;	/* real time callout */
167	struct	itimerval p_realtimer;	/* Alarm timer. */
168	struct	timeval p_rtime;	/* Real time. */
169	u_quad_t p_uticks;		/* Statclock hits in user mode. */
170	u_quad_t p_sticks;		/* Statclock hits in system mode. */
171	u_quad_t p_iticks;		/* Statclock hits processing intr. */
172
173	int	p_traceflag;		/* Kernel trace points. */
174	void	*p_tracep;		/* Trace to vnode or file */
175
176	sigset_t p_siglist;		/* Signals arrived but not delivered. */
177	char	p_sigcheck;		/* May have deliverable signals. */
178
179	struct	vnode *p_textvp;	/* Vnode of executable. */
180
181	short   p_locks;		/* DEBUG: lockmgr count of held locks */
182	short   p_simple_locks;		/* DEBUG: count of held simple locks */
183
184	int	p_holdcnt;		/* If non-zero, don't swap. */
185	struct	emul *p_emul;		/* Emulation information */
186
187/* End area that is zeroed on creation. */
188#define	p_endzero	p_startcopy
189
190/* The following fields are all copied upon creation in fork. */
191#define	p_startcopy	p_sigmask
192
193	sigset_t p_sigmask;	/* Current signal mask. */
194	sigset_t p_sigignore;	/* Signals being ignored. */
195	sigset_t p_sigcatch;	/* Signals being caught by user. */
196
197	u_char	p_priority;	/* Process priority. */
198	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
199	u_char	p_nice;		/* Process "nice" value. */
200	char	p_comm[MAXCOMLEN+1];
201
202	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
203	void	*p_ctxlink;	/* uc_link {get,set}context */
204
205/* End area that is copied on creation. */
206#define	p_endcopy	p_thread
207
208	void	*p_thread;	/* Id for this "thread"; Mach glue. XXX */
209	struct	user *p_addr;	/* Kernel virtual addr of u-area (PROC ONLY). */
210	struct	mdproc p_md;	/* Any machine-dependent fields. */
211
212	u_short	p_xstat;	/* Exit status for wait; also stop signal. */
213	u_short	p_acflag;	/* Accounting flags. */
214	struct	rusage *p_ru;	/* Exit information. XXX */
215};
216
217#define	p_session	p_pgrp->pg_session
218#define	p_pgid		p_pgrp->pg_id
219
220/* Status values. */
221#define	SIDL	1		/* Process being created by fork. */
222#define	SRUN	2		/* Currently runnable. */
223#define	SSLEEP	3		/* Sleeping on an address. */
224#define	SSTOP	4		/* Process debugging or suspension. */
225#define	SZOMB	5		/* Awaiting collection by parent. */
226#define	SDEAD	6		/* Process is almost a zombie. */
227
228#define	P_ZOMBIE(p)	((p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
229
230/* These flags are kept in p_flag. */
231#define	P_ADVLOCK	0x00001	/* Process may hold a POSIX advisory lock. */
232#define	P_CONTROLT	0x00002	/* Has a controlling terminal. */
233#define	P_INMEM		0x00004	/* Loaded into memory. */
234#define	P_NOCLDSTOP	0x00008	/* No SIGCHLD when children stop. */
235#define	P_PPWAIT	0x00010	/* Parent is waiting for child to exec/exit. */
236#define	P_PROFIL	0x00020	/* Has started profiling. */
237#define	P_SELECT	0x00040	/* Selecting; wakeup/waiting danger. */
238#define	P_SINTR		0x00080	/* Sleep is interruptible. */
239#define	P_SUGID		0x00100	/* Had set id privileges since last exec. */
240#define	P_SYSTEM	0x00200	/* System proc: no sigs, stats or swapping. */
241#define	P_TIMEOUT	0x00400	/* Timing out during sleep. */
242#define	P_TRACED	0x00800	/* Debugged process being traced. */
243#define	P_WAITED	0x01000	/* Debugging process has waited for child. */
244#define	P_WEXIT		0x02000	/* Working on exiting. */
245#define	P_EXEC		0x04000	/* Process called exec. */
246#define	P_OWEUPC	0x08000	/* Owe process an addupc() call at next ast. */
247#define	P_FSTRACE	0x10000	/* Debugger process being traced by procfs */
248#define	P_NOCLDWAIT	0x20000	/* No zombies if child dies */
249#define	P_32		0x40000	/* 32-bit process -- only used on 64-bit kernels */
250
251/*
252 * These flags are kept in p_schedflags.  p_schedflags may be modified
253 * only at splstatclock().
254 */
255#define	PSCHED_SEENRR		0x0001	/* process has been in roundrobin() */
256#define	PSCHED_SHOULDYIELD	0x0002	/* process should yield */
257
258#define	PSCHED_SWITCHCLEAR	(PSCHED_SEENRR|PSCHED_SHOULDYIELD)
259
260/*
261 * Macro to compute the exit signal to be delivered.
262 */
263#define	P_EXITSIG(p)	(((p)->p_flag & (P_TRACED|P_FSTRACE)) ? SIGCHLD : \
264			 p->p_exitsig)
265
266/*
267 * MOVE TO ucred.h?
268 *
269 * Shareable process credentials (always resident).  This includes a reference
270 * to the current user credentials as well as real and saved ids that may be
271 * used to change ids.
272 */
273struct	pcred {
274	struct	ucred *pc_ucred;	/* Current credentials. */
275	uid_t	p_ruid;			/* Real user id. */
276	uid_t	p_svuid;		/* Saved effective user id. */
277	gid_t	p_rgid;			/* Real group id. */
278	gid_t	p_svgid;		/* Saved effective group id. */
279	int	p_refcnt;		/* Number of references. */
280};
281
282LIST_HEAD(proclist, proc);		/* a list of processes */
283
284/*
285 * This structure associates a proclist with its lock.
286 */
287struct proclist_desc {
288	struct proclist *pd_list;	/* the list */
289	/*
290	 * XXX Add a pointer to the proclist's lock eventually.
291	 */
292};
293
294#ifdef _KERNEL
295/*
296 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
297 * as it is used to represent "no process group".
298 */
299#define	PID_MAX		30000
300#define	NO_PID		30001
301
302#define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
303#define	SESSHOLD(s)	((s)->s_count++)
304#define	SESSRELE(s) {							\
305	if (--(s)->s_count == 0)					\
306		FREE(s, M_SESSION);					\
307}
308
309#define	PHOLD(p) {							\
310	if ((p)->p_holdcnt++ == 0 && ((p)->p_flag & P_INMEM) == 0)	\
311		uvm_swapin(p);						\
312}
313#define	PRELE(p)	(--(p)->p_holdcnt)
314
315/*
316 * Flags passed to fork1().
317 */
318#define	FORK_PPWAIT	0x01		/* block parent until child exit */
319#define	FORK_SHAREVM	0x02		/* share vmspace with parent */
320#define	FORK_SHARECWD	0x04		/* share cdir/rdir/cmask */
321#define	FORK_SHAREFILES	0x08		/* share file descriptors */
322#define	FORK_SHARESIGS	0x10		/* share signal actions */
323
324#define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
325extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
326extern u_long pidhash;
327
328#define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
329extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
330extern u_long pgrphash;
331
332/*
333 * Allow machine-dependent code to override curproc in <machine/proc.h> for
334 * its own convenience.  Otherwise, we declare it as appropriate.
335 */
336#if !defined(curproc)
337#if defined(MULTIPROCESSOR)
338#define	curproc	curcpu()->ci_curproc	/* Current running proc. */
339#else
340extern struct proc *curproc;		/* Current running proc. */
341#endif /* MULTIPROCESSOR */
342#endif /* ! curproc */
343
344extern struct proc proc0;		/* Process slot for swapper. */
345extern int nprocs, maxproc;		/* Current and max number of procs. */
346
347/* Process list lock; see kern_proc.c for locking protocol details. */
348extern struct lock proclist_lock;
349
350extern struct proclist allproc;		/* List of all processes. */
351extern struct proclist zombproc;	/* List of zombie processes. */
352
353extern struct proclist deadproc;	/* List of dead processes. */
354extern struct simplelock deadproc_slock;
355
356extern struct proc *initproc;		/* Process slots for init, pager. */
357
358extern const struct proclist_desc proclists[];
359
360extern struct pool proc_pool;		/* memory pool for procs */
361extern struct pool pcred_pool;		/* memory pool for pcreds */
362extern struct pool plimit_pool;		/* memory pool for plimits */
363extern struct pool rusage_pool;		/* memory pool for rusages */
364
365#define	NQS	32			/* 32 run queues. */
366int	whichqs;			/* Bit mask summary of non-empty Q's. */
367struct	prochd {
368	struct	proc *ph_link;		/* Linked list of running processes. */
369	struct	proc *ph_rlink;
370} qs[NQS];
371
372struct proc *pfind __P((pid_t));	/* Find process by id. */
373struct pgrp *pgfind __P((pid_t));	/* Find process group by id. */
374
375int	chgproccnt __P((uid_t uid, int diff));
376int	enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
377void	fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
378int	inferior __P((struct proc *p));
379int	leavepgrp __P((struct proc *p));
380void	yield __P((void));
381void	preempt __P((struct proc *));
382void	mi_switch __P((void));
383void	pgdelete __P((struct pgrp *pgrp));
384void	procinit __P((void));
385void	remrunqueue __P((struct proc *));
386void	resetpriority __P((struct proc *));
387void	setrunnable __P((struct proc *));
388void	setrunqueue __P((struct proc *));
389void	sleep __P((void *chan, int pri));
390int	tsleep __P((void *chan, int pri, const char *wmesg, int timo));
391void	unsleep __P((struct proc *));
392void	wakeup __P((void *chan));
393void	wakeup_one __P((void *chan));
394void	reaper __P((void));
395void	exit1 __P((struct proc *, int));
396void	exit2 __P((struct proc *));
397int	fork1 __P((struct proc *, int, int, void *, size_t, register_t *,
398	    struct proc **));
399void	rqinit __P((void));
400int	groupmember __P((gid_t, struct ucred *));
401void	cpu_switch __P((struct proc *));
402void	cpu_wait __P((struct proc *));
403void	cpu_exit __P((struct proc *));
404int	proc_isunder __P((struct proc *, struct proc*));
405
406void	proclist_lock_read __P((void));
407void	proclist_unlock_read __P((void));
408int	proclist_lock_write __P((void));
409void	proclist_unlock_write __P((int));
410void	p_sugid __P((struct proc*));
411#endif	/* _KERNEL */
412#endif	/* !_SYS_PROC_H_ */
413