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