proc.h revision 1.171
1/*	$NetBSD: proc.h,v 1.171 2003/09/13 15:55:29 jdolecek 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. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)proc.h	8.15 (Berkeley) 5/19/95
37 */
38
39#ifndef _SYS_PROC_H_
40#define	_SYS_PROC_H_
41
42#if defined(_KERNEL_OPT)
43#include "opt_multiprocessor.h"
44#include "opt_kstack.h"
45#endif
46
47#include <machine/proc.h>		/* Machine-dependent proc substruct */
48#include <sys/lock.h>
49#include <sys/lwp.h>
50#include <sys/queue.h>
51#include <sys/callout.h>
52#include <sys/signalvar.h>
53#include <sys/siginfo.h>
54#include <sys/event.h>
55
56/*
57 * One structure allocated per session.
58 */
59struct session {
60	int		s_count;	/* Ref cnt; pgrps in session */
61	u_int		s_flags;
62#define	S_LOGIN_SET	1		/* s_login set in this 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_HEAD(, proc) pg_members;	/* Pointer to pgrp members */
75	struct session	*pg_session;	/* Pointer to session */
76	pid_t		pg_id;		/* Pgrp id */
77	int		pg_jobc;	/*
78					 * Number of processes qualifying
79					 * pgrp for job control
80					 */
81};
82
83/*
84 * One structure allocated per emulation.
85 */
86struct exec_package;
87struct ps_strings;
88struct ras;
89
90struct emul {
91	const char	*e_name;	/* Symbolic name */
92	const char	*e_path;	/* Extra emulation path (NULL if none)*/
93#ifndef __HAVE_MINIMAL_EMUL
94	int		e_flags;	/* Miscellaneous flags, see above */
95					/* Syscall handling function */
96	const int	*e_errno;	/* Errno array */
97	int		e_nosys;	/* Offset of the nosys() syscall */
98	int		e_nsysent;	/* Number of system call entries */
99#endif
100	const struct sysent *e_sysent;	/* System call array */
101	const char * const *e_syscallnames; /* System call name array */
102					/* Signal sending function */
103#ifdef __HAVE_SIGINFO
104	void		(*e_sendsig) __P((struct ksiginfo *, sigset_t *));
105	void		(*e_trapsignal) __P((struct lwp *, struct ksiginfo *));
106#else
107	void		(*e_sendsig) __P((int, sigset_t *, u_long));
108	void		(*e_trapsignal) __P((struct lwp *, int, u_long));
109#endif
110	char		*e_sigcode;	/* Start of sigcode */
111	char		*e_esigcode;	/* End of sigcode */
112					/* Set registers before execution */
113	struct uvm_object **e_sigobject;/* shared sigcode object */
114	void		(*e_setregs) __P((struct lwp *, struct exec_package *,
115				  u_long));
116
117					/* Per-process hooks */
118	void		(*e_proc_exec) __P((struct proc *,
119					    struct exec_package *));
120	void		(*e_proc_fork) __P((struct proc *, struct proc *));
121	void		(*e_proc_exit) __P((struct proc *));
122
123#ifdef __HAVE_SYSCALL_INTERN
124	void		(*e_syscall_intern) __P((struct proc *));
125#else
126	void		(*e_syscall) __P((void));
127#endif
128					/* Emulation specific sysctl */
129	int		(*e_sysctl) __P((int *, u_int , void *, size_t *,
130				void *, size_t, struct proc *));
131					/* Specific VM fault handling */
132	int		(*e_fault) __P((struct proc *, vaddr_t, int, int));
133};
134
135/*
136 * Emulation miscelaneous flags
137 */
138#define	EMUL_HAS_SYS___syscall	0x001	/* Has SYS___syscall */
139
140/*
141 * Description of a process.
142 *
143 * This structure contains the information needed to manage a thread of
144 * control, known in UN*X as a process; it has references to substructures
145 * containing descriptions of things that the process uses, but may share
146 * with related processes.  The process structure and the substructures
147 * are always addressible except for those marked "(PROC ONLY)" below,
148 * which might be addressible only on a processor on which the process
149 * is running.
150 */
151struct proc {
152	LIST_ENTRY(proc) p_list;	/* List of all processes */
153
154	/* Substructures: */
155	struct pcred	*p_cred;	/* Process owner's identity */
156	struct filedesc	*p_fd;		/* Ptr to open files structure */
157	struct cwdinfo	*p_cwdi;	/* cdir/rdir/cmask info */
158	struct pstats	*p_stats;	/* Accounting/statistics (PROC ONLY) */
159	struct plimit	*p_limit;	/* Process limits */
160	struct vmspace	*p_vmspace;	/* Address space */
161	struct sigacts	*p_sigacts;	/* Process sigactions (state is below)*/
162
163	void		*p_ksems;	/* p1003.1b semaphores */
164
165#define	p_ucred		p_cred->pc_ucred
166#define	p_rlimit	p_limit->pl_rlimit
167
168	int		p_exitsig;	/* signal to sent to parent on exit */
169	int		p_flag;		/* P_* flags. */
170	char		p_stat;		/* S* process status. */
171	char		p_pad1[3];
172
173	pid_t		p_pid;		/* Process identifier. */
174	SLIST_ENTRY(proc) p_dead;	/* Processes waiting for reaper */
175	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp. */
176	struct proc 	*p_pptr;	/* Pointer to parent process. */
177	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes. */
178	LIST_HEAD(, proc) p_children;	/* Pointer to list of children. */
179
180	struct simplelock p_lwplock;	/* Lock on LWP-related state. */
181
182	LIST_HEAD(, lwp) p_lwps;	/* Pointer to list of LWPs. */
183
184	LIST_HEAD(, ras) p_raslist;	/* Pointer to RAS queue */
185	u_int p_nras;			/* number of RASs */
186	struct simplelock p_raslock;	/* Lock for RAS queue */
187
188/* The following fields are all zeroed upon creation in fork. */
189#define	p_startzero	p_nlwps
190
191	int 		p_nlwps;	/* Number of LWPs */
192	int 		p_nrlwps;	/* Number of running LWPs */
193	int 		p_nzlwps;	/* Number of zombie LWPs */
194	int 		p_nlwpid;	/* Next LWP ID */
195
196	struct sadata 	*p_sa;		/* Scheduler activation information */
197
198	/* scheduling */
199	u_int		p_estcpu;	/* Time averaged value of p_cpticks XXX belongs in p_startcopy section */
200	int		p_cpticks;	/* Ticks of cpu time */
201	fixpt_t		p_pctcpu;	/* %cpu for this process during p_swtime */
202
203	struct proc	*p_opptr;	/* Save parent during ptrace. */
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 *, void *);
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	0x00000001 /* Process may hold a POSIX advisory lock */
280#define	P_CONTROLT	0x00000002 /* Has a controlling terminal */
281#define	P_NOCLDSTOP	0x00000008 /* No SIGCHLD when children stop */
282#define	P_PPWAIT	0x00000010 /* Parent is waiting for child exec/exit */
283#define	P_PROFIL	0x00000020 /* Has started profiling */
284#define	P_SUGID		0x00000100 /* Had set id privileges since last exec */
285#define	P_SYSTEM	0x00000200 /* System proc: no sigs, stats or swapping */
286#define	P_SA		0x00000400 /* Using scheduler activations */
287#define	P_TRACED	0x00000800 /* Debugged process being traced */
288#define	P_WAITED	0x00001000 /* Debugging process has waited for child */
289#define	P_WEXIT		0x00002000 /* Working on exiting */
290#define	P_EXEC		0x00004000 /* Process called exec */
291#define	P_OWEUPC	0x00008000 /* Owe process an addupc() at next ast */
292#define	P_FSTRACE	0x00010000 /* Debugger process being traced by procfs */
293#define	P_NOCLDWAIT	0x00020000 /* No zombies if child dies */
294#define	P_32		0x00040000 /* 32-bit process (used on 64-bit kernels) */
295#define	P_INEXEC	0x00100000 /* Process is exec'ing and can't be traced */
296#define	P_SYSTRACE	0x00200000 /* Process system call tracing active */
297#define	P_CHTRACED	0x00400000 /* Child has been traced & reparented */
298#define	P_STOPFORK	0x00800000 /* Child will be stopped on fork(2) */
299#define	P_STOPEXEC	0x01000000 /* 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 until there are > 16k processes.
344 * NO_PGID is used to represent "no process group" for a tty.
345 */
346#define	PID_MAX		30000
347#define	NO_PGID		((pid_t)-1)
348
349#define	SESS_LEADER(p)	((p)->p_session->s_leader == (p))
350#define	SESSHOLD(s)	((s)->s_count++)
351#define	SESSRELE(s)							\
352do {									\
353	if (--(s)->s_count == 0)					\
354		sessdelete(s);						\
355} while (/* CONSTCOND */ 0)
356
357
358/*
359 * Flags passed to fork1().
360 */
361#define	FORK_PPWAIT	0x01		/* Block parent until child exit */
362#define	FORK_SHAREVM	0x02		/* Share vmspace with parent */
363#define	FORK_SHARECWD	0x04		/* Share cdir/rdir/cmask */
364#define	FORK_SHAREFILES	0x08		/* Share file descriptors */
365#define	FORK_SHARESIGS	0x10		/* Share signal actions */
366#define	FORK_NOWAIT	0x20		/* Make init the parent of the child */
367#define	FORK_CLEANFILES	0x40		/* Start with a clean descriptor set */
368
369/*
370 * Allow machine-dependent code to override curproc in <machine/cpu.h> for
371 * its own convenience.  Otherwise, we declare it as appropriate.
372 */
373#if !defined(curlwp)
374#if defined(MULTIPROCESSOR)
375#define	curlwp		curcpu()->ci_curlwp	/* Current running LWP */
376#else
377extern struct lwp	*curlwp;		/* Current running LWP */
378#endif /* MULTIPROCESSOR */
379#endif /* ! curproc */
380
381#define curproc ((curlwp) ? (curlwp)->l_proc : NULL)
382
383extern struct proc	proc0;		/* Process slot for swapper */
384extern int		nprocs, maxproc; /* Current and max number of procs */
385
386/* Process list lock; see kern_proc.c for locking protocol details */
387extern struct lock	proclist_lock;
388
389extern struct proclist	allproc;	/* List of all processes */
390extern struct proclist	zombproc;	/* List of zombie processes */
391
392extern SLIST_HEAD(deadprocs, proc) deadprocs;	/* List of dead processes */
393extern struct simplelock deadproc_slock;
394
395extern struct proc	*initproc;	/* Process slots for init, pager */
396
397extern const struct proclist_desc proclists[];
398
399extern struct pool	pcred_pool;	/* Memory pool for pcreds */
400extern struct pool	plimit_pool;	/* Memory pool for plimits */
401extern struct pool 	pstats_pool;	/* memory pool for pstats */
402extern struct pool	rusage_pool;	/* Memory pool for rusages */
403extern struct pool	ptimer_pool;	/* Memory pool for ptimers */
404
405struct proc *pfind(pid_t);		/* Find process by id */
406struct pgrp *pgfind(pid_t);		/* Find process group by id */
407
408struct simplelock;
409int	chgproccnt(uid_t, int);
410int	enterpgrp(struct proc *, pid_t, int);
411void	fixjobc(struct proc *, struct pgrp *, int);
412int	inferior(struct proc *, struct proc *);
413int	leavepgrp(struct proc *);
414void	sessdelete(struct session *);
415void	yield(void);
416struct lwp *chooselwp(void);
417void	pgdelete(struct pgrp *);
418void	procinit(void);
419void	resetprocpriority(struct proc *);
420void	suspendsched(void);
421int	ltsleep(const void *, int, const char *, int,
422	    __volatile struct simplelock *);
423void	wakeup(const void *);
424void	wakeup_one(const void *);
425void	reaper(void *);
426void	exit1(struct lwp *, int);
427void	exit2(struct lwp *);
428int	find_stopped_child(struct proc *, pid_t, int, struct proc **);
429struct proc *proc_alloc(void);
430void	proc0_insert(struct proc *, struct lwp *, struct pgrp *, struct session *);
431void	proc_free(struct proc *);
432void	proc_free_mem(struct proc *);
433void	exit_lwps(struct lwp *l);
434int	fork1(struct lwp *, int, int, void *, size_t,
435	    void (*)(void *), void *, register_t *, struct proc **);
436void	rqinit(void);
437int	groupmember(gid_t, const struct ucred *);
438int	pgid_in_session(struct proc *, pid_t);
439#ifndef cpu_idle
440void	cpu_idle(void);
441#endif
442void	cpu_exit(struct lwp *, int);
443void	cpu_lwp_fork(struct lwp *, struct lwp *, void *, size_t,
444	    void (*)(void *), void *);
445
446		/*
447		 * XXX: use __P() to allow ports to have as a #define.
448		 * XXX: we need a better way to solve this.
449		 */
450void	cpu_wait __P((struct lwp *));
451
452void	child_return(void *);
453
454int	proc_isunder(struct proc *, struct proc *);
455
456void	proclist_lock_read(void);
457void	proclist_unlock_read(void);
458int	proclist_lock_write(void);
459void	proclist_unlock_write(int);
460void	p_sugid(struct proc *);
461
462/* Compatibility with old, non-interlocked tsleep call */
463#define	tsleep(chan, pri, wmesg, timo)					\
464	ltsleep(chan, pri, wmesg, timo, NULL)
465
466#if defined(MULTIPROCESSOR)
467void	proc_trampoline_mp(void);	/* XXX */
468#endif
469
470#ifdef KSTACK_CHECK_MAGIC
471void kstack_setup_magic(const struct lwp *);
472void kstack_check_magic(const struct lwp *);
473#endif
474
475/*
476 * kernel stack paramaters
477 * XXX require sizeof(struct user)
478 */
479/* the lowest address of kernel stack */
480#ifndef KSTACK_LOWEST_ADDR
481#define	KSTACK_LOWEST_ADDR(l)	((caddr_t)ALIGN((l)->l_addr + 1))
482#endif
483/* size of kernel stack */
484#ifndef KSTACK_SIZE
485#define	KSTACK_SIZE	(USPACE - ALIGN(sizeof(struct user)))
486#endif
487
488#endif	/* _KERNEL */
489#endif	/* !_SYS_PROC_H_ */
490