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