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