proc.h revision 1.110
1/*	$NetBSD: proc.h,v 1.110 2000/11/19 00:56:39 sommerfeld 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(_KERNEL)
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	const char * const *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	/* Per-process hooks */
108	void	(*e_proc_exec) __P((struct proc *, struct exec_package *));
109	void	(*e_proc_fork) __P((struct proc *p, struct proc *parent));
110	void	(*e_proc_exit) __P((struct proc *));
111};
112
113/*
114 * Description of a process.
115 *
116 * This structure contains the information needed to manage a thread of
117 * control, known in UN*X as a process; it has references to substructures
118 * containing descriptions of things that the process uses, but may share
119 * with related processes.  The process structure and the substructures
120 * are always addressible except for those marked "(PROC ONLY)" below,
121 * which might be addressible only on a processor on which the process
122 * is running.
123 */
124struct	proc {
125	struct	proc *p_forw;		/* Doubly-linked run/sleep queue. */
126	struct	proc *p_back;
127	LIST_ENTRY(proc) p_list;	/* List of all processes. */
128
129	/* substructures: */
130	struct	pcred *p_cred;		/* Process owner's identity. */
131	struct	filedesc *p_fd;		/* Ptr to open files structure. */
132	struct	cwdinfo *p_cwdi;	/* cdir/rdir/cmask info */
133	struct	pstats *p_stats;	/* Accounting/statistics (PROC ONLY). */
134	struct	plimit *p_limit;	/* Process limits. */
135	struct	vmspace *p_vmspace;	/* Address space. */
136	struct	sigacts *p_sigacts;	/* Signal actions, state (PROC ONLY). */
137
138#define	p_ucred		p_cred->pc_ucred
139#define	p_rlimit	p_limit->pl_rlimit
140
141	int	p_exitsig;		/* signal to sent to parent on exit */
142	int	p_flag;			/* P_* flags. */
143	struct cpu_info * __volatile p_cpu; /* CPU we're running on if
144					       SONPROC */
145	u_char	p_unused;		/* XXX: used to be emulation flag */
146	char	p_stat;			/* S* process status. */
147	char	p_pad1[2];
148
149	pid_t	p_pid;			/* Process identifier. */
150	LIST_ENTRY(proc) p_hash;	/* Hash chain. */
151	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp. */
152	struct	proc *p_pptr;	 	/* Pointer to parent process. */
153	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes. */
154	LIST_HEAD(, proc) p_children;	/* Pointer to list of children. */
155
156/* The following fields are all zeroed upon creation in fork. */
157#define	p_startzero	p_oppid
158
159	pid_t	p_oppid;	 /* Save parent pid during ptrace. XXX */
160	int	p_dupfd;	 /* Sideways return value from filedescopen. XXX */
161
162	/* scheduling */
163	u_int	p_estcpu;	 /* Time averaged value of p_cpticks. XXX belongs in p_startcopy section */
164	int	p_cpticks;	 /* Ticks of cpu time. */
165	fixpt_t	p_pctcpu;	 /* %cpu for this process during p_swtime */
166	void	*p_wchan;	 /* Sleep address. */
167	struct callout p_tsleep_ch;/* callout for tsleep */
168	const char *p_wmesg;	 /* Reason for sleep. */
169	u_int	p_swtime;	 /* Time swapped in or out. */
170	u_int	p_slptime;	 /* Time since last blocked. */
171
172	struct	callout p_realit_ch;	/* real time callout */
173	struct	itimerval p_realtimer;	/* Alarm timer. */
174	struct	timeval p_rtime;	/* Real time. */
175	u_quad_t p_uticks;		/* Statclock hits in user mode. */
176	u_quad_t p_sticks;		/* Statclock hits in system mode. */
177	u_quad_t p_iticks;		/* Statclock hits processing intr. */
178
179	int	p_traceflag;		/* Kernel trace points. */
180	struct 	file *p_tracep;		/* Trace to file */
181
182	sigset_t p_siglist;		/* Signals arrived but not delivered. */
183	char	p_sigcheck;		/* May have deliverable signals. */
184
185	struct	vnode *p_textvp;	/* Vnode of executable. */
186
187	int	p_locks;		/* DEBUG: lockmgr count of held locks */
188
189	int	p_holdcnt;		/* If non-zero, don't swap. */
190	struct	emul *p_emul;		/* Emulation information */
191	void	*p_emuldata;		/* Per-process emulation data, or NULL.
192					 * Malloc type M_EMULDATA */
193
194/* End area that is zeroed on creation. */
195#define	p_endzero	p_startcopy
196
197/* The following fields are all copied upon creation in fork. */
198#define	p_startcopy	p_sigmask
199
200	sigset_t p_sigmask;	/* Current signal mask. */
201	sigset_t p_sigignore;	/* Signals being ignored. */
202	sigset_t p_sigcatch;	/* Signals being caught by user. */
203
204	u_char	p_priority;	/* Process priority. */
205	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
206	u_char	p_nice;		/* Process "nice" value. */
207	char	p_comm[MAXCOMLEN+1];
208
209	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
210	void	*p_ctxlink;	/* uc_link {get,set}context */
211
212	struct	ps_strings *p_psstr;	/* address of process's ps_strings */
213	size_t	p_psargv;		/* offset of ps_argvstr in above */
214	size_t	p_psnargv;		/* offset of ps_nargvstr in above */
215	size_t	p_psenv;		/* offset of ps_envstr in above */
216	size_t	p_psnenv;		/* offset of ps_nenvstr in above */
217
218/* End area that is copied on creation. */
219#define	p_endcopy	p_thread
220
221	void	*p_thread;	/* Id for this "thread"; Mach glue. XXX */
222	struct	user *p_addr;	/* Kernel virtual addr of u-area (PROC ONLY). */
223	struct	mdproc p_md;	/* Any machine-dependent fields. */
224
225	u_short	p_xstat;	/* Exit status for wait; also stop signal. */
226	u_short	p_acflag;	/* Accounting flags. */
227	struct	rusage *p_ru;	/* Exit information. XXX */
228};
229
230#define	p_session	p_pgrp->pg_session
231#define	p_pgid		p_pgrp->pg_id
232
233/*
234 * Status values.
235 *
236 * A note about SRUN and SONPROC: SRUN indicates that a process is
237 * runnable but *not* yet running, i.e. is on a run queue.  SONPROC
238 * indicates that the process is actually executing on a CPU, i.e.
239 * it is no longer on a run queue.
240 */
241#define	SIDL	1		/* Process being created by fork. */
242#define	SRUN	2		/* Currently runnable. */
243#define	SSLEEP	3		/* Sleeping on an address. */
244#define	SSTOP	4		/* Process debugging or suspension. */
245#define	SZOMB	5		/* Awaiting collection by parent. */
246#define	SDEAD	6		/* Process is almost a zombie. */
247#define	SONPROC	7		/* Process is currently on a CPU */
248
249#define	P_ZOMBIE(p)	((p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
250
251/* These flags are kept in p_flag. */
252#define	P_ADVLOCK	0x00001	/* Process may hold a POSIX advisory lock. */
253#define	P_CONTROLT	0x00002	/* Has a controlling terminal. */
254#define	P_INMEM		0x00004	/* Loaded into memory. */
255#define	P_NOCLDSTOP	0x00008	/* No SIGCHLD when children stop. */
256#define	P_PPWAIT	0x00010	/* Parent is waiting for child to exec/exit. */
257#define	P_PROFIL	0x00020	/* Has started profiling. */
258#define	P_SELECT	0x00040	/* Selecting; wakeup/waiting danger. */
259#define	P_SINTR		0x00080	/* Sleep is interruptible. */
260#define	P_SUGID		0x00100	/* Had set id privileges since last exec. */
261#define	P_SYSTEM	0x00200	/* System proc: no sigs, stats or swapping. */
262#define	P_TIMEOUT	0x00400	/* Timing out during sleep. */
263#define	P_TRACED	0x00800	/* Debugged process being traced. */
264#define	P_WAITED	0x01000	/* Debugging process has waited for child. */
265#define	P_WEXIT		0x02000	/* Working on exiting. */
266#define	P_EXEC		0x04000	/* Process called exec. */
267#define	P_OWEUPC	0x08000	/* Owe process an addupc() call at next ast. */
268#define	P_FSTRACE	0x10000	/* Debugger process being traced by procfs */
269#define	P_NOCLDWAIT	0x20000	/* No zombies if child dies */
270#define	P_32		0x40000	/* 32-bit process (used on 64-bit kernels) */
271#define P_BIGLOCK	0x80000 /* Process needs kernel "big lock" to run */
272
273
274/*
275 * Macro to compute the exit signal to be delivered.
276 */
277#define	P_EXITSIG(p)	(((p)->p_flag & (P_TRACED|P_FSTRACE)) ? SIGCHLD : \
278			 p->p_exitsig)
279
280/*
281 * MOVE TO ucred.h?
282 *
283 * Shareable process credentials (always resident).  This includes a reference
284 * to the current user credentials as well as real and saved ids that may be
285 * used to change ids.
286 */
287struct	pcred {
288	struct	ucred *pc_ucred;	/* Current credentials. */
289	uid_t	p_ruid;			/* Real user id. */
290	uid_t	p_svuid;		/* Saved effective user id. */
291	gid_t	p_rgid;			/* Real group id. */
292	gid_t	p_svgid;		/* Saved effective group id. */
293	int	p_refcnt;		/* Number of references. */
294};
295
296LIST_HEAD(proclist, proc);		/* a list of processes */
297
298/*
299 * This structure associates a proclist with its lock.
300 */
301struct proclist_desc {
302	struct proclist *pd_list;	/* the list */
303	/*
304	 * XXX Add a pointer to the proclist's lock eventually.
305	 */
306};
307
308#ifdef _KERNEL
309/*
310 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
311 * as it is used to represent "no process group".
312 */
313#define	PID_MAX		30000
314#define	NO_PID		30001
315
316#define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
317#define	SESSHOLD(s)	((s)->s_count++)
318#define	SESSRELE(s) {							\
319	if (--(s)->s_count == 0)					\
320		FREE(s, M_SESSION);					\
321}
322
323#define	PHOLD(p) {							\
324	if ((p)->p_holdcnt++ == 0 && ((p)->p_flag & P_INMEM) == 0)	\
325		uvm_swapin(p);						\
326}
327#define	PRELE(p)	(--(p)->p_holdcnt)
328
329/*
330 * Flags passed to fork1().
331 */
332#define	FORK_PPWAIT	0x01		/* block parent until child exit */
333#define	FORK_SHAREVM	0x02		/* share vmspace with parent */
334#define	FORK_SHARECWD	0x04		/* share cdir/rdir/cmask */
335#define	FORK_SHAREFILES	0x08		/* share file descriptors */
336#define	FORK_SHARESIGS	0x10		/* share signal actions */
337
338#define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
339extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
340extern u_long pidhash;
341
342#define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
343extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
344extern u_long pgrphash;
345
346/*
347 * Allow machine-dependent code to override curproc in <machine/cpu.h> for
348 * its own convenience.  Otherwise, we declare it as appropriate.
349 */
350#if !defined(curproc)
351#if defined(MULTIPROCESSOR)
352#define	curproc	curcpu()->ci_curproc	/* Current running proc. */
353#else
354extern struct proc *curproc;		/* Current running proc. */
355#endif /* MULTIPROCESSOR */
356#endif /* ! curproc */
357
358extern struct proc proc0;		/* Process slot for swapper. */
359extern int nprocs, maxproc;		/* Current and max number of procs. */
360
361/* Process list lock; see kern_proc.c for locking protocol details. */
362extern struct lock proclist_lock;
363
364extern struct proclist allproc;		/* List of all processes. */
365extern struct proclist zombproc;	/* List of zombie processes. */
366
367extern struct proclist deadproc;	/* List of dead processes. */
368extern struct simplelock deadproc_slock;
369
370extern struct proc *initproc;		/* Process slots for init, pager. */
371
372extern const struct proclist_desc proclists[];
373
374extern struct pool proc_pool;		/* memory pool for procs */
375extern struct pool pcred_pool;		/* memory pool for pcreds */
376extern struct pool plimit_pool;		/* memory pool for plimits */
377extern struct pool rusage_pool;		/* memory pool for rusages */
378
379struct proc *pfind __P((pid_t));	/* Find process by id. */
380struct pgrp *pgfind __P((pid_t));	/* Find process group by id. */
381
382struct simplelock;
383
384int	chgproccnt __P((uid_t uid, int diff));
385int	enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
386void	fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
387int	inferior __P((struct proc *p, struct proc *q));
388int	leavepgrp __P((struct proc *p));
389void	yield __P((void));
390void	preempt __P((struct proc *));
391void	mi_switch __P((struct proc *));
392void	pgdelete __P((struct pgrp *pgrp));
393void	procinit __P((void));
394void	remrunqueue __P((struct proc *));
395void	resetpriority __P((struct proc *));
396void	setrunnable __P((struct proc *));
397void	setrunqueue __P((struct proc *));
398void	suspendsched __P((void));
399int	ltsleep __P((void *chan, int pri, const char *wmesg, int timo,
400	    __volatile struct simplelock *));
401void	unsleep __P((struct proc *));
402void	wakeup __P((void *chan));
403void	wakeup_one __P((void *chan));
404void	reaper __P((void *));
405void	exit1 __P((struct proc *, int));
406void	exit2 __P((struct proc *));
407int	fork1 __P((struct proc *, int, int, void *, size_t,
408	    void (*)(void *), void *, register_t *, struct proc **));
409void	rqinit __P((void));
410int	groupmember __P((gid_t, struct ucred *));
411void	cpu_switch __P((struct proc *));
412void	cpu_wait __P((struct proc *));
413void	cpu_exit __P((struct proc *));
414void	cpu_fork __P((struct proc *, struct proc *, void *, size_t,
415	    void (*)(void *), void *));
416
417void	child_return __P((void *));
418
419int	proc_isunder __P((struct proc *, struct proc*));
420
421void	proclist_lock_read __P((void));
422void	proclist_unlock_read __P((void));
423int	proclist_lock_write __P((void));
424void	proclist_unlock_write __P((int));
425void	p_sugid __P((struct proc*));
426
427/* Compatbility with old, non-interlocked tsleep call. */
428#define	tsleep(chan, pri, wmesg, timo)					\
429	ltsleep(chan, pri, wmesg, timo, NULL)
430
431#if defined(MULTIPROCESSOR)
432void	proc_trampoline_mp(void);	/* XXX */
433#endif
434
435#endif	/* _KERNEL */
436#endif	/* !_SYS_PROC_H_ */
437