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