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