1 /*	$OpenBSD: proc.h,v 1.76 2004/11/23 19:08:55 miod Exp $	*/
2 /* + 1.88 1.119.4.1 */
3 /*	$NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $	*/
4 
5 /*-
6  * Copyright (c) 1986, 1989, 1991, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)proc.h	8.8 (Berkeley) 1/21/94
39  */
40 
41 #ifndef _SYS_PROC_H_
42 #define	_SYS_PROC_H_
43 
44 #include <machine/proc.h>		/* Machine-dependent proc substruct. */
45 #include <sys/select.h>			/* For struct selinfo. */
46 #include <sys/queue.h>
47 #include <sys/timeout.h>		/* For struct timeout. */
48 #include <sys/event.h>			/* For struct klist */
49 
50 /*
51  * One structure allocated per session.
52  */
53 struct	session {
54 	int	s_count;		/* Ref cnt; pgrps in session. */
55 	struct	proc *s_leader;		/* Session leader. */
56 	struct	vnode *s_ttyvp;		/* Vnode of controlling terminal. */
57 	struct	tty *s_ttyp;		/* Controlling terminal. */
58 	char	s_login[MAXLOGNAME];	/* Setlogin() name. */
59 };
60 
61 /*
62  * One structure allocated per process group.
63  */
64 struct	pgrp {
65 	LIST_ENTRY(pgrp) pg_hash;	/* Hash chain. */
66 	LIST_HEAD(, proc) pg_members;	/* Pointer to pgrp members. */
67 	struct	session *pg_session;	/* Pointer to session. */
68 	pid_t	pg_id;			/* Pgrp id. */
69 	int	pg_jobc;	/* # procs qualifying pgrp for job control */
70 };
71 
72 /*
73  * One structure allocated per emulation.
74  */
75 struct exec_package;
76 struct ps_strings;
77 struct uvm_object;
78 union sigval;
79 
80 struct	emul {
81 	char	e_name[8];		/* Symbolic name */
82 	int	*e_errno;		/* Errno array */
83 					/* Signal sending function */
84 	void	(*e_sendsig)(sig_t, int, int, u_long, int, union sigval);
85 	int	e_nosys;		/* Offset of the nosys() syscall */
86 	int	e_nsysent;		/* Number of system call entries */
87 	struct sysent *e_sysent;	/* System call array */
88 	char	**e_syscallnames;	/* System call name array */
89 	int	e_arglen;		/* Extra argument size in words */
90 					/* Copy arguments on the stack */
91 	void	*(*e_copyargs)(struct exec_package *, struct ps_strings *,
92 				    void *, void *);
93 					/* Set registers before execution */
94 	void	(*e_setregs)(struct proc *, struct exec_package *,
95 				  u_long, register_t *);
96 	int	(*e_fixup)(struct proc *, struct exec_package *);
97 	char	*e_sigcode;		/* Start of sigcode */
98 	char	*e_esigcode;		/* End of sigcode */
99 	int	e_flags;		/* Flags, see below */
100 	struct uvm_object *e_sigobject;	/* shared sigcode object */
101 					/* Per-process hooks */
102 	void	(*e_proc_exec)(struct proc *, struct exec_package *);
103 	void	(*e_proc_fork)(struct proc *p, struct proc *parent);
104 	void	(*e_proc_exit)(struct proc *);
105 };
106 /* Flags for e_flags */
107 #define	EMUL_ENABLED	0x0001		/* Allow exec to continue */
108 #define	EMUL_NATIVE	0x0002		/* Always enabled */
109 
110 extern struct emul *emulsw[];		/* All emuls in system */
111 extern int nemuls;			/* Number of emuls */
112 
113 /*
114  * Description of a process.
115  *
116  * This structure contains the information needed to manage a thread of
117  * control, known in UN*X as a process; it has references to substructures
118  * containing descriptions of things that the process uses, but may share
119  * with related processes.  The process structure and the substructures
120  * are always addressable except for those marked "(PROC ONLY)" below,
121  * which might be addressable only on a processor on which the process
122  * is running.
123  */
124 struct	proc {
125 	struct	proc *p_forw;		/* Doubly-linked run/sleep queue. */
126 	struct	proc *p_back;
127 	LIST_ENTRY(proc) p_list;	/* List of all processes. */
128 
129 	/* substructures: */
130 	struct	pcred *p_cred;		/* Process owner's identity. */
131 	struct	filedesc *p_fd;		/* Ptr to open files structure. */
132 	struct	pstats *p_stats;	/* Accounting/statistics (PROC ONLY). */
133 	struct	plimit *p_limit;	/* Process limits. */
134 	struct	vmspace *p_vmspace;	/* Address space. */
135 	struct	sigacts *p_sigacts;	/* Signal actions, state (PROC ONLY). */
136 
137 #define	p_ucred		p_cred->pc_ucred
138 #define	p_rlimit	p_limit->pl_rlimit
139 
140 	int	p_exitsig;		/* Signal to send to parent on exit. */
141 	int	p_flag;			/* P_* flags. */
142 	u_char	p_os;			/* OS tag */
143 	char	p_stat;			/* S* process status. */
144 	char	p_pad1[1];
145 	u_char	p_descfd;		/* if not 255, fdesc permits this fd */
146 
147 	pid_t	p_pid;			/* Process identifier. */
148 	LIST_ENTRY(proc) p_hash;	/* Hash chain. */
149 	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp. */
150 	struct	proc *p_pptr;	 	/* Pointer to parent process. */
151 	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes. */
152 	LIST_HEAD(, proc) p_children;	/* Pointer to list of children. */
153 
154 /* The following fields are all zeroed upon creation in fork. */
155 #define	p_startzero	p_oppid
156 
157 	pid_t	p_oppid;	 /* Save parent pid during ptrace. XXX */
158 	int	p_dupfd;	 /* Sideways return value from filedescopen. XXX */
159 
160 	/* scheduling */
161 	u_int	p_estcpu;	 /* Time averaged value of p_cpticks. */
162 	int	p_cpticks;	 /* Ticks of cpu time. */
163 	fixpt_t	p_pctcpu;	 /* %cpu for this process during p_swtime */
164 	void	*p_wchan;	 /* Sleep address. */
165 	struct	timeout p_sleep_to;/* timeout for tsleep() */
166 	const char *p_wmesg;	 /* Reason for sleep. */
167 	u_int	p_swtime;	 /* Time swapped in or out. */
168 	u_int	p_slptime;	 /* Time since last blocked. */
169 	int	p_schedflags;	 /* PSCHED_* flags */
170 
171 	struct	itimerval p_realtimer;	/* Alarm timer. */
172 	struct	timeout p_realit_to;	/* Alarm timeout. */
173 	struct	timeval p_rtime;	/* Real time. */
174 	u_quad_t p_uticks;		/* Statclock hits in user mode. */
175 	u_quad_t p_sticks;		/* Statclock hits in system mode. */
176 	u_quad_t p_iticks;		/* Statclock hits processing intr. */
177 
178 	int	p_traceflag;		/* Kernel trace points. */
179 	struct	vnode *p_tracep;	/* Trace to vnode. */
180 
181 	void	*p_systrace;		/* Back pointer to systrace */
182 
183 	int	p_siglist;		/* Signals arrived but not delivered. */
184 
185 	struct	vnode *p_textvp;	/* Vnode of executable. */
186 
187 	int	p_holdcnt;		/* If non-zero, don't swap. */
188 	struct	emul *p_emul;		/* Emulation information */
189 	void	*p_emuldata;		/* Per-process emulation data, or */
190 					/* NULL. Malloc type M_EMULDATA */
191 	struct	klist p_klist;		/* knotes attached to this process */
192 					/* pad to 256, avoid shifting eproc. */
193 
194 
195 /* End area that is zeroed on creation. */
196 #define	p_endzero	p_startcopy
197 
198 /* The following fields are all copied upon creation in fork. */
199 #define	p_startcopy	p_sigmask
200 
201 	sigset_t p_sigmask;	/* Current signal mask. */
202 	sigset_t p_sigignore;	/* Signals being ignored. */
203 	sigset_t p_sigcatch;	/* Signals being caught by user. */
204 
205 	u_char	p_priority;	/* Process priority. */
206 	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
207 	char	p_nice;		/* Process "nice" value. */
208 	char	p_comm[MAXCOMLEN+1];
209 
210 	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
211 	vaddr_t	p_sigcode;	/* user pointer to the signal code. */
212 
213 /* End area that is copied on creation. */
214 #define	p_endcopy	p_addr
215 
216 	struct	user *p_addr;	/* Kernel virtual addr of u-area (PROC ONLY). */
217 	struct	mdproc p_md;	/* Any machine-dependent fields. */
218 
219 	u_short	p_xstat;	/* Exit status for wait; also stop signal. */
220 	u_short	p_acflag;	/* Accounting flags. */
221 	struct	rusage *p_ru;	/* Exit information. XXX */
222 };
223 
224 #define	p_session	p_pgrp->pg_session
225 #define	p_pgid		p_pgrp->pg_id
226 
227 /* Status values. */
228 #define	SIDL	1		/* Process being created by fork. */
229 #define	SRUN	2		/* Currently runnable. */
230 #define	SSLEEP	3		/* Sleeping on an address. */
231 #define	SSTOP	4		/* Process debugging or suspension. */
232 #define	SZOMB	5		/* Awaiting collection by parent. */
233 #define SDEAD	6		/* Process is almost a zombie. */
234 
235 #define P_ZOMBIE(p)	((p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
236 
237 /* These flags are kept in p_flag. */
238 #define	P_ADVLOCK	0x000001	/* Proc may hold a POSIX adv. lock. */
239 #define	P_CONTROLT	0x000002	/* Has a controlling terminal. */
240 #define	P_INMEM		0x000004	/* Loaded into memory. */
241 #define	P_NOCLDSTOP	0x000008	/* No SIGCHLD when children stop. */
242 #define	P_PPWAIT	0x000010	/* Parent waits for child exec/exit. */
243 #define	P_PROFIL	0x000020	/* Has started profiling. */
244 #define	P_SELECT	0x000040	/* Selecting; wakeup/waiting danger. */
245 #define	P_SINTR		0x000080	/* Sleep is interruptible. */
246 #define	P_SUGID		0x000100	/* Had set id privs since last exec. */
247 #define	P_SYSTEM	0x000200	/* No sigs, stats or swapping. */
248 #define	P_TIMEOUT	0x000400	/* Timing out during sleep. */
249 #define	P_TRACED	0x000800	/* Debugged process being traced. */
250 #define	P_WAITED	0x001000	/* Debugging proc has waited for child. */
251 /* XXX - Should be merged with INEXEC */
252 #define	P_WEXIT		0x002000	/* Working on exiting. */
253 #define	P_EXEC		0x004000	/* Process called exec. */
254 
255 /* Should be moved to machine-dependent areas. */
256 #define	P_OWEUPC	0x008000	/* Owe proc an addupc() at next ast. */
257 
258 /* XXX Not sure what to do with these, yet. */
259 #define	P_FSTRACE	0x010000	/* tracing via fs (elsewhere?) */
260 #define	P_SSTEP		0x020000	/* proc needs single-step fixup ??? */
261 #define	P_SUGIDEXEC	0x040000	/* last execve() was set[ug]id */
262 
263 #define	P_NOCLDWAIT	0x080000	/* Let pid 1 wait for my children */
264 #define	P_NOZOMBIE	0x100000	/* Pid 1 waits for me instead of dad */
265 #define P_INEXEC	0x200000	/* Process is doing an exec right now */
266 #define P_SYSTRACE	0x400000	/* Process system call tracing active*/
267 #define P_CONTINUED	0x800000	/* Proc has continued from a stopped state. */
268 #define P_SWAPIN	0x1000000	/* Swapping in right now */
269 #define P_SOFTDEP	0x10000000	/* Stuck processing softdep worklist */
270 
271 #define	P_BITS \
272     ("\20\01ADVLOCK\02CTTY\03INMEM\04NOCLDSTOP\05PPWAIT\06PROFIL\07SELECT" \
273      "\010SINTR\011SUGID\012SYSTEM\013TIMEOUT\014TRACED\015WAITED\016WEXIT" \
274      "\017EXEC\020PWEUPC\021FSTRACE\022SSTEP\023SUGIDEXEC\024NOCLDWAIT" \
275      "\025NOZOMBIE\026INEXEC\027SYSTRACE\030CONTINUED\031SWAPIN\035SOFTDEP")
276 
277 /* Macro to compute the exit signal to be delivered. */
278 #define P_EXITSIG(p) \
279     (((p)->p_flag & (P_TRACED | P_FSTRACE)) ? SIGCHLD : (p)->p_exitsig)
280 
281 /*
282  * These flags are kept in p_schedflags.  p_schedflags may be modified
283  * only at splstatclock().
284  */
285 #define PSCHED_SEENRR		0x0001	/* process has been in roundrobin() */
286 #define PSCHED_SHOULDYIELD	0x0002	/* process should yield */
287 
288 #define PSCHED_SWITCHCLEAR	(PSCHED_SEENRR|PSCHED_SHOULDYIELD)
289 
290 /*
291  * MOVE TO ucred.h?
292  *
293  * Shareable process credentials (always resident).  This includes a reference
294  * to the current user credentials as well as real and saved ids that may be
295  * used to change ids.
296  */
297 struct	pcred {
298 	struct	ucred *pc_ucred;	/* Current credentials. */
299 	uid_t	p_ruid;			/* Real user id. */
300 	uid_t	p_svuid;		/* Saved effective user id. */
301 	gid_t	p_rgid;			/* Real group id. */
302 	gid_t	p_svgid;		/* Saved effective group id. */
303 	int	p_refcnt;		/* Number of references. */
304 };
305 
306 #ifdef _KERNEL
307 
308 struct uidinfo {
309 	LIST_ENTRY(uidinfo) ui_hash;
310 	uid_t   ui_uid;
311 	long    ui_proccnt;	/* proc structs */
312 	long	ui_lockcnt;	/* lockf structs */
313 };
314 
315 struct uidinfo *uid_find(uid_t);
316 
317 /*
318  * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
319  * as it is used to represent "no process group".
320  * We set PID_MAX to (SHRT_MAX - 1) so we don't break sys/compat.
321  */
322 #define	PID_MAX		32766
323 #define	NO_PID		(PID_MAX+1)
324 
325 #define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
326 #define	SESSHOLD(s)	((s)->s_count++)
327 #define	SESSRELE(s) {							\
328 	if (--(s)->s_count == 0)					\
329 		pool_put(&session_pool, s);				\
330 }
331 
332 #define	PHOLD(p) {							\
333 	if ((p)->p_holdcnt++ == 0 && ((p)->p_flag & P_INMEM) == 0)	\
334 		uvm_swapin(p);						\
335 }
336 #define	PRELE(p)	(--(p)->p_holdcnt)
337 
338 /*
339  * Flags to fork1().
340  */
341 #define FORK_FORK	0x00000001
342 #define FORK_VFORK	0x00000002
343 #define FORK_RFORK	0x00000004
344 #define FORK_PPWAIT	0x00000008
345 #define FORK_SHAREFILES	0x00000010
346 #define FORK_CLEANFILES	0x00000020
347 #define FORK_NOZOMBIE	0x00000040
348 #define FORK_SHAREVM	0x00000080
349 #define FORK_SIGHAND	0x00000200
350 
351 #define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
352 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
353 extern u_long pidhash;
354 
355 #define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
356 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
357 extern u_long pgrphash;
358 
359 #ifndef curproc
360 extern struct proc *curproc;		/* Current running proc. */
361 #endif
362 extern struct proc proc0;		/* Process slot for swapper. */
363 extern int nprocs, maxproc;		/* Current and max number of procs. */
364 extern int randompid;			/* fork() should create random pid's */
365 
366 LIST_HEAD(proclist, proc);
367 extern struct proclist allproc;		/* List of all processes. */
368 extern struct proclist zombproc;	/* List of zombie processes. */
369 
370 extern struct proclist deadproc;	/* List of dead processes. */
371 extern struct simplelock deadproc_slock;
372 
373 extern struct proc *initproc;		/* Process slots for init, pager. */
374 extern struct proc *syncerproc;		/* filesystem syncer daemon */
375 
376 extern struct pool proc_pool;		/* memory pool for procs */
377 extern struct pool rusage_pool;		/* memory pool for zombies */
378 extern struct pool ucred_pool;		/* memory pool for ucreds */
379 extern struct pool session_pool;	/* memory pool for sessions */
380 extern struct pool pcred_pool;		/* memory pool for pcreds */
381 
382 #define	NQS	32			/* 32 run queues. */
383 extern int whichqs;			/* Bit mask summary of non-empty Q's. */
384 struct	prochd {
385 	struct	proc *ph_link;		/* Linked list of running processes. */
386 	struct	proc *ph_rlink;
387 };
388 extern struct prochd qs[NQS];
389 
390 struct simplelock;
391 
392 struct proc *pfind(pid_t);	/* Find process by id. */
393 struct pgrp *pgfind(pid_t);	/* Find process group by id. */
394 void	proc_printit(struct proc *p, const char *modif,
395     int (*pr)(const char *, ...));
396 
397 int	chgproccnt(uid_t uid, int diff);
398 int	enterpgrp(struct proc *p, pid_t pgid, int mksess);
399 void	fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
400 int	inferior(struct proc *p, struct proc *);
401 int	leavepgrp(struct proc *p);
402 void	yield(void);
403 void	preempt(struct proc *);
404 void	mi_switch(void);
405 void	pgdelete(struct pgrp *pgrp);
406 void	procinit(void);
407 #if !defined(remrunqueue)
408 void	remrunqueue(struct proc *);
409 #endif
410 void	resetpriority(struct proc *);
411 void	setrunnable(struct proc *);
412 #if !defined(setrunqueue)
413 void	setrunqueue(struct proc *);
414 #endif
415 void	sleep(void *chan, int pri);
416 void	uvm_swapin(struct proc *);  /* XXX: uvm_extern.h? */
417 int	ltsleep(void *chan, int pri, const char *wmesg, int timo,
418 	    volatile struct simplelock *);
419 #define tsleep(chan, pri, wmesg, timo) ltsleep(chan, pri, wmesg, timo, NULL)
420 void	unsleep(struct proc *);
421 void    wakeup_n(void *chan, int);
422 void    wakeup(void *chan);
423 #define wakeup_one(c) wakeup_n((c), 1)
424 void	reaper(void);
425 void	exit1(struct proc *, int);
426 void	exit2(struct proc *);
427 int	fork1(struct proc *, int, int, void *, size_t, void (*)(void *),
428 	    void *, register_t *, struct proc **);
429 void	rqinit(void);
430 int	groupmember(gid_t, struct ucred *);
431 #if !defined(cpu_switch)
432 void	cpu_switch(struct proc *);
433 #endif
434 #if !defined(cpu_wait)
435 void	cpu_wait(struct proc *);
436 #endif
437 void	cpu_exit(struct proc *);
438 
439 void	child_return(void *);
440 
441 int	proc_cansugid(struct proc *);
442 void	proc_zap(struct proc *);
443 #endif	/* _KERNEL */
444 #endif	/* !_SYS_PROC_H_ */
445