proc.h revision 1.161
1/*	$NetBSD: proc.h,v 1.161 2003/03/12 22:16:31 dsl 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_OPT)
47#include "opt_multiprocessor.h"
48#include "opt_kstack.h"
49#endif
50
51#include <machine/proc.h>		/* Machine-dependent proc substruct */
52#include <sys/lock.h>
53#include <sys/lwp.h>
54#include <sys/queue.h>
55#include <sys/callout.h>
56#include <sys/signalvar.h>
57#include <sys/event.h>
58
59/*
60 * One structure allocated per session.
61 */
62struct session {
63	int		s_count;	/* Ref cnt; pgrps in session */
64	u_int		s_flags;
65#define	S_LOGIN_SET	1		/* s_login set in this session */
66	struct proc	*s_leader;	/* Session leader */
67	struct vnode	*s_ttyvp;	/* Vnode of controlling terminal */
68	struct tty	*s_ttyp;	/* Controlling terminal */
69	char		s_login[MAXLOGNAME]; /* Setlogin() name */
70	pid_t		s_sid;		/* Session ID (pid of leader) */
71};
72
73/*
74 * One structure allocated per process group.
75 */
76struct pgrp {
77	LIST_ENTRY(pgrp) pg_hash;	/* Hash chain */
78	LIST_HEAD(, proc) pg_members;	/* Pointer to pgrp members */
79	struct session	*pg_session;	/* Pointer to session */
80	pid_t		pg_id;		/* Pgrp id */
81	int		pg_jobc;	/*
82					 * Number of processes qualifying
83					 * pgrp for job control
84					 */
85};
86
87/*
88 * One structure allocated per emulation.
89 */
90struct exec_package;
91struct ps_strings;
92struct ras;
93
94struct emul {
95	const char	*e_name;	/* Symbolic name */
96	const char	*e_path;	/* Extra emulation path (NULL if none)*/
97#ifndef __HAVE_MINIMAL_EMUL
98	int		e_flags;	/* Miscellaneous flags, see above */
99					/* Syscall handling function */
100	const int	*e_errno;	/* Errno array */
101	int		e_nosys;	/* Offset of the nosys() syscall */
102	int		e_nsysent;	/* Number of system call entries */
103#endif
104	const struct sysent *e_sysent;	/* System call array */
105	const char * const *e_syscallnames; /* System call name array */
106					/* Signal sending function */
107	void		(*e_sendsig) __P((int, sigset_t *, u_long));
108	void		(*e_trapsignal) __P((struct lwp *, int, u_long));
109	char		*e_sigcode;	/* Start of sigcode */
110	char		*e_esigcode;	/* End of sigcode */
111					/* Set registers before execution */
112	void		(*e_setregs) __P((struct lwp *, struct exec_package *,
113				  u_long));
114
115					/* Per-process hooks */
116	void		(*e_proc_exec) __P((struct proc *,
117					    struct exec_package *));
118	void		(*e_proc_fork) __P((struct proc *p,
119					    struct proc *parent));
120	void		(*e_proc_exit) __P((struct proc *));
121
122#ifdef __HAVE_SYSCALL_INTERN
123	void		(*e_syscall_intern) __P((struct proc *));
124#else
125	void		(*e_syscall) __P((void));
126#endif
127					/* Emulation specific sysctl */
128	int		(*e_sysctl) __P((int *, u_int , void *, size_t *,
129				void *, size_t, struct proc *p));
130					/* Specific VM fault handling */
131	int		(*e_fault) __P((struct proc *, vaddr_t, int, int));
132};
133
134/*
135 * Emulation miscelaneous flags
136 */
137#define	EMUL_HAS_SYS___syscall	0x001	/* Has SYS___syscall */
138
139/*
140 * Description of a process.
141 *
142 * This structure contains the information needed to manage a thread of
143 * control, known in UN*X as a process; it has references to substructures
144 * containing descriptions of things that the process uses, but may share
145 * with related processes.  The process structure and the substructures
146 * are always addressible except for those marked "(PROC ONLY)" below,
147 * which might be addressible only on a processor on which the process
148 * is running.
149 */
150struct proc {
151	LIST_ENTRY(proc) p_list;	/* List of all processes */
152
153	/* Substructures: */
154	struct pcred	*p_cred;	/* Process owner's identity */
155	struct filedesc	*p_fd;		/* Ptr to open files structure */
156	struct cwdinfo	*p_cwdi;	/* cdir/rdir/cmask info */
157	struct pstats	*p_stats;	/* Accounting/statistics (PROC ONLY) */
158	struct plimit	*p_limit;	/* Process limits */
159	struct vmspace	*p_vmspace;	/* Address space */
160	struct sigacts	*p_sigacts;	/* Process sigactions (state is below)*/
161
162	void		*p_ksems;	/* p1003.1b semaphores */
163
164#define	p_ucred		p_cred->pc_ucred
165#define	p_rlimit	p_limit->pl_rlimit
166
167	int		p_exitsig;	/* signal to sent to parent on exit */
168	int		p_flag;		/* P_* flags. */
169	char		p_stat;		/* S* process status. */
170	char		p_pad1[3];
171
172	pid_t		p_pid;		/* Process identifier. */
173	LIST_ENTRY(proc) p_hash;	/* Hash chain. */
174	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp. */
175	struct proc 	*p_pptr;	/* Pointer to parent process. */
176	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes. */
177	LIST_HEAD(, proc) p_children;	/* Pointer to list of children. */
178
179	struct simplelock p_lwplock;	/* Lock on LWP-related state. */
180
181	LIST_HEAD(, lwp) p_lwps;	/* Pointer to list of LWPs. */
182
183	LIST_HEAD(, ras) p_raslist;	/* Pointer to RAS queue */
184	u_int p_nras;			/* number of RASs */
185	struct simplelock p_raslock;	/* Lock for RAS queue */
186
187/* The following fields are all zeroed upon creation in fork. */
188#define	p_startzero	p_nlwps
189
190	int 		p_nlwps;	/* Number of LWPs */
191	int 		p_nrlwps;	/* Number of running LWPs */
192	int 		p_nzlwps;	/* Number of zombie LWPs */
193	int 		p_nlwpid;	/* Next LWP ID */
194
195	struct sadata 	*p_sa;		/* Scheduler activation information */
196
197	/* scheduling */
198	u_int		p_estcpu;	/* Time averaged value of p_cpticks XXX belongs in p_startcopy section */
199	int		p_cpticks;	/* Ticks of cpu time */
200	fixpt_t		p_pctcpu;	/* %cpu for this process during p_swtime */
201
202	struct proc	*p_opptr;	/* Save parent during ptrace. */
203	int		p_dupfd;	/* Sideways return value from filedescopen XXX */
204	struct ptimers	*p_timers;	/* Timers: real, virtual, profiling */
205	struct timeval 	p_rtime;	/* Real time */
206	u_quad_t 	p_uticks;	/* Statclock hits in user mode */
207	u_quad_t 	p_sticks;	/* Statclock hits in system mode */
208	u_quad_t 	p_iticks;	/* Statclock hits processing intr */
209
210	int		p_traceflag;	/* Kernel trace points */
211	struct file	*p_tracep;	/* Trace to file */
212	void		*p_systrace;	/* Back pointer to systrace */
213
214	struct vnode 	*p_textvp;	/* Vnode of executable */
215
216	const struct emul *p_emul;	/* Emulation information */
217	void		*p_emuldata;	/* Per-process emulation data, or NULL.
218					 * Malloc type M_EMULDATA
219					 */
220
221	void 		(*p_userret)(struct lwp *l, void *arg);
222					/* Function to call at userret(). */
223	void		*p_userret_arg;
224
225	const struct execsw *p_execsw;	/* Exec package information */
226	struct klist	p_klist;	/* Knotes attached to this process */
227
228/*
229 * End area that is zeroed on creation
230 */
231#define	p_endzero	p_startcopy
232
233/*
234 * The following fields are all copied upon creation in fork.
235 */
236#define	p_startcopy	p_sigctx.ps_startcopy
237
238	struct sigctx 	p_sigctx;	/* Signal state */
239
240	u_char		p_nice;		/* Process "nice" value */
241	char		p_comm[MAXCOMLEN+1];	/* basename of last exec file */
242
243	struct pgrp 	*p_pgrp;	/* Pointer to process group */
244
245	struct ps_strings *p_psstr;	/* address of process's ps_strings */
246	size_t 		p_psargv;	/* offset of ps_argvstr in above */
247	size_t 		p_psnargv;	/* offset of ps_nargvstr in above */
248	size_t 		p_psenv;	/* offset of ps_envstr in above */
249	size_t 		p_psnenv;	/* offset of ps_nenvstr in above */
250
251/*
252 * End area that is copied on creation
253 */
254#define	p_endcopy	p_xstat
255
256	u_short		p_xstat;	/* Exit status for wait; also stop signal */
257	u_short		p_acflag;	/* Accounting flags */
258	struct rusage 	*p_ru;		/* Exit information. XXX */
259
260	struct mdproc	p_md;		/* Any machine-dependent fields */
261};
262
263#define	p_session	p_pgrp->pg_session
264#define	p_pgid		p_pgrp->pg_id
265
266/*
267 * Status values.
268 *
269 */
270#define	SIDL		1		/* Process being created by fork */
271#define	SACTIVE		2		/* Process is not stopped */
272#define	SSTOP		4		/* Process debugging or suspension */
273#define	SZOMB		5		/* Awaiting collection by parent */
274#define	SDEAD		6		/* Process is almost a zombie */
275
276#define	P_ZOMBIE(p)	((p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
277
278/* These flags are kept in p_flag. */
279#define	P_ADVLOCK	0x000001 /* Process may hold a POSIX advisory lock */
280#define	P_CONTROLT	0x000002 /* Has a controlling terminal */
281#define	P_NOCLDSTOP	0x000008 /* No SIGCHLD when children stop */
282#define	P_PPWAIT	0x000010 /* Parent is waiting for child to exec/exit */
283#define	P_PROFIL	0x000020 /* Has started profiling */
284#define	P_SUGID		0x000100 /* Had set id privileges since last exec */
285#define	P_SYSTEM	0x000200 /* System proc: no sigs, stats or swapping */
286#define	P_SA		0x000400 /* Using scheduler activations */
287#define	P_TRACED	0x000800 /* Debugged process being traced */
288#define	P_WAITED	0x001000 /* Debugging process has waited for child */
289#define	P_WEXIT		0x002000 /* Working on exiting */
290#define	P_EXEC		0x004000 /* Process called exec */
291#define	P_OWEUPC	0x008000 /* Owe process an addupc() call at next ast */
292#define	P_FSTRACE	0x010000 /* Debugger process being traced by procfs */
293#define	P_NOCLDWAIT	0x020000 /* No zombies if child dies */
294#define	P_32		0x040000 /* 32-bit process (used on 64-bit kernels) */
295#define	P_INEXEC	0x100000 /* Process is exec'ing and cannot be traced */
296#define	P_SYSTRACE	0x200000 /* Process system call tracing active */
297#define	P_CHTRACED	0x400000 /* Child has been traced & reparented */
298#define	P_STOPFORK	0x800000 /* Child will be stopped on fork(2) */
299#define	P_STOPEXEC	0x1000000 /* Will be stopped on exec(2) */
300
301/*
302 * Macro to compute the exit signal to be delivered.
303 */
304#define	P_EXITSIG(p)	(((p)->p_flag & (P_TRACED|P_FSTRACE)) ? SIGCHLD : \
305			 p->p_exitsig)
306
307/*
308 * MOVE TO ucred.h?
309 *
310 * Shareable process credentials (always resident).  This includes a reference
311 * to the current user credentials as well as real and saved ids that may be
312 * used to change ids.
313 */
314struct pcred {
315	struct ucred	*pc_ucred;	/* Current credentials */
316	uid_t		p_ruid;		/* Real user id */
317	uid_t		p_svuid;	/* Saved effective user id */
318	gid_t		p_rgid;		/* Real group id */
319	gid_t		p_svgid;	/* Saved effective group id */
320	int		p_refcnt;	/* Number of references */
321};
322
323LIST_HEAD(proclist, proc);		/* A list of processes */
324
325/*
326 * This structure associates a proclist with its lock.
327 */
328struct proclist_desc {
329	struct proclist	*pd_list;	/* The list */
330	/*
331	 * XXX Add a pointer to the proclist's lock eventually.
332	 */
333};
334
335#ifdef _KERNEL
336#include <sys/mallocvar.h>
337MALLOC_DECLARE(M_EMULDATA);
338MALLOC_DECLARE(M_PROC);
339MALLOC_DECLARE(M_SESSION);
340MALLOC_DECLARE(M_SUBPROC);
341
342/*
343 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
344 * as it is used to represent "no process group".
345 */
346#define	PID_MAX		30000
347#define	NO_PID		30001
348
349/*
350 * Process IDs <0,PID_SKIP-1> are not considered for new processes
351 * once the prototype wraps around.
352 */
353#define PID_SKIP	500
354
355#define	SESS_LEADER(p)	((p)->p_session->s_leader == (p))
356#define	SESSHOLD(s)	((s)->s_count++)
357#define	SESSRELE(s)							\
358do {									\
359	if (--(s)->s_count == 0)					\
360		FREE(s, M_SESSION);					\
361} while (/* CONSTCOND */ 0)
362
363
364/*
365 * Flags passed to fork1().
366 */
367#define	FORK_PPWAIT	0x01		/* Block parent until child exit */
368#define	FORK_SHAREVM	0x02		/* Share vmspace with parent */
369#define	FORK_SHARECWD	0x04		/* Share cdir/rdir/cmask */
370#define	FORK_SHAREFILES	0x08		/* Share file descriptors */
371#define	FORK_SHARESIGS	0x10		/* Share signal actions */
372#define	FORK_NOWAIT	0x20		/* Make init the parent of the child */
373#define	FORK_CLEANFILES	0x40		/* Start with a clean descriptor set */
374
375#define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
376extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
377extern u_long		pidhash;
378
379#define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
380extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
381extern u_long		pgrphash;
382
383/*
384 * Allow machine-dependent code to override curproc in <machine/cpu.h> for
385 * its own convenience.  Otherwise, we declare it as appropriate.
386 */
387#if !defined(curlwp)
388#if defined(MULTIPROCESSOR)
389#define	curlwp		curcpu()->ci_curlwp	/* Current running LWP */
390#else
391extern struct lwp	*curlwp;		/* Current running LWP */
392#endif /* MULTIPROCESSOR */
393#endif /* ! curproc */
394
395#define curproc ((curlwp) ? (curlwp)->l_proc : NULL)
396
397extern struct proc	proc0;		/* Process slot for swapper */
398extern int		nprocs, maxproc; /* Current and max number of procs */
399
400/* Process list lock; see kern_proc.c for locking protocol details */
401extern struct lock	proclist_lock;
402
403extern struct proclist	allproc;	/* List of all processes */
404extern struct proclist	zombproc;	/* List of zombie processes */
405
406extern struct proclist deadproc;	/* List of dead processes */
407extern struct simplelock deadproc_slock;
408
409extern struct proc	*initproc;	/* Process slots for init, pager */
410
411extern const struct proclist_desc proclists[];
412
413extern struct pool	proc_pool;	/* Memory pool for procs */
414extern struct pool	pcred_pool;	/* Memory pool for pcreds */
415extern struct pool	plimit_pool;	/* Memory pool for plimits */
416extern struct pool 	pstats_pool;	/* memory pool for pstats */
417extern struct pool	rusage_pool;	/* Memory pool for rusages */
418extern struct pool	ptimer_pool;	/* Memory pool for ptimers */
419
420struct proc *pfind(pid_t);		/* Find process by id */
421struct pgrp *pgfind(pid_t);		/* Find process group by id */
422
423struct simplelock;
424int	chgproccnt(uid_t uid, int diff);
425int	enterpgrp(struct proc *p, pid_t pgid, int mksess);
426void	fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
427int	inferior(struct proc *p, struct proc *q);
428int	leavepgrp(struct proc *p);
429void	yield(void);
430struct lwp *chooselwp(void);
431void	pgdelete(struct pgrp *pgrp);
432void	procinit(void);
433void	resetprocpriority(struct proc *);
434void	suspendsched(void);
435int	ltsleep(const void *chan, int pri, const char *wmesg, int timo,
436	    __volatile struct simplelock *);
437void	wakeup(const void *chan);
438void	wakeup_one(const void *chan);
439void	reaper(void *);
440void	exit1(struct lwp *, int);
441void	exit2(struct lwp *);
442int	find_stopped_child(struct proc *, pid_t, int, struct proc **);
443void	proc_free(struct proc *);
444void	exit_lwps(struct lwp *l);
445int	fork1(struct lwp *, int, int, void *, size_t,
446	    void (*)(void *), void *, register_t *, struct proc **);
447void	rqinit(void);
448int	groupmember(gid_t, const struct ucred *);
449int	pgid_in_session(struct proc *, pid_t);
450#ifndef cpu_idle
451void	cpu_idle(void);
452#endif
453void	cpu_exit(struct lwp *, int);
454void	cpu_lwp_fork(struct lwp *, struct lwp *, void *, size_t,
455	    void (*)(void *), void *);
456
457		/*
458		 * XXX: use __P() to allow ports to have as a #define.
459		 * XXX: we need a better way to solve this.
460		 */
461void	cpu_wait __P((struct lwp *));
462
463void	child_return(void *);
464
465int	proc_isunder(struct proc *, struct proc*);
466
467void	proclist_lock_read(void);
468void	proclist_unlock_read(void);
469int	proclist_lock_write(void);
470void	proclist_unlock_write(int);
471void	p_sugid(struct proc*);
472
473/* Compatibility with old, non-interlocked tsleep call */
474#define	tsleep(chan, pri, wmesg, timo)					\
475	ltsleep(chan, pri, wmesg, timo, NULL)
476
477#if defined(MULTIPROCESSOR)
478void	proc_trampoline_mp(void);	/* XXX */
479#endif
480
481#ifdef KSTACK_CHECK_MAGIC
482void kstack_setup_magic(const struct lwp *);
483void kstack_check_magic(const struct lwp *);
484#endif
485
486/*
487 * kernel stack paramaters
488 * XXX require sizeof(struct user)
489 */
490/* the lowest address of kernel stack */
491#ifndef KSTACK_LOWEST_ADDR
492#define	KSTACK_LOWEST_ADDR(l)	((caddr_t)ALIGN((l)->l_addr + 1))
493#endif
494/* size of kernel stack */
495#ifndef KSTACK_SIZE
496#define	KSTACK_SIZE	(USPACE - ALIGN(sizeof(struct user)))
497#endif
498
499#endif	/* _KERNEL */
500#endif	/* !_SYS_PROC_H_ */
501