1 /* $OpenBSD: proc.h,v 1.376 2024/10/22 11:54:05 claudio Exp $ */
2 /* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */
3
4 /*-
5 * Copyright (c) 1986, 1989, 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)proc.h 8.8 (Berkeley) 1/21/94
38 */
39
40 #ifndef _SYS_PROC_H_
41 #define _SYS_PROC_H_
42
43 #include <machine/proc.h> /* Machine-dependent proc substruct. */
44 #include <sys/selinfo.h> /* For struct selinfo */
45 #include <sys/syslimits.h> /* For LOGIN_NAME_MAX */
46 #include <sys/queue.h>
47 #include <sys/timeout.h> /* For struct timeout */
48 #include <sys/event.h> /* For struct klist */
49 #include <sys/mutex.h> /* For struct mutex */
50 #include <sys/resource.h> /* For struct rusage */
51 #include <sys/rwlock.h> /* For struct rwlock */
52 #include <sys/sigio.h> /* For struct sigio */
53 #include <sys/refcnt.h> /* For struct refcnt */
54
55 #ifdef _KERNEL
56 #include <sys/atomic.h>
57 #define __need_process
58 #endif
59
60 /*
61 * One structure allocated per session.
62 */
63 struct process;
64 struct session {
65 int s_count; /* Ref cnt; pgrps in session. */
66 struct process *s_leader; /* Session leader. */
67 struct vnode *s_ttyvp; /* Vnode of controlling terminal. */
68 struct tty *s_ttyp; /* Controlling terminal. */
69 char s_login[LOGIN_NAME_MAX]; /* Setlogin() name. */
70 pid_t s_verauthppid;
71 uid_t s_verauthuid;
72 struct timeout s_verauthto;
73 };
74
75 void zapverauth(/* struct session */ void *);
76
77 /*
78 * One structure allocated per process group.
79 */
80 struct pgrp {
81 LIST_ENTRY(pgrp) pg_hash; /* Hash chain. */
82 LIST_HEAD(, process) pg_members;/* Pointer to pgrp members. */
83 struct session *pg_session; /* Pointer to session. */
84 struct sigiolst pg_sigiolst; /* List of sigio structures. */
85 pid_t pg_id; /* Pgrp id. */
86 int pg_jobc; /* # procs qualifying pgrp for job control */
87 };
88
89 /*
90 * time usage: accumulated times in ticks
91 * Each thread is immediately accumulated here. For processes only the
92 * time of exited threads is accumulated and to get the proper process
93 * time usage tuagg_get_process() needs to be called.
94 * Accounting of threads is done lockless by curproc using the tu_gen
95 * generation counter. Code should use tu_enter() and tu_leave() for this.
96 * The process ps_tu structure is locked by the ps_mtx.
97 */
98 struct tusage {
99 uint64_t tu_gen; /* generation counter */
100 uint64_t tu_uticks; /* Statclock hits in user mode. */
101 uint64_t tu_sticks; /* Statclock hits in system mode. */
102 uint64_t tu_iticks; /* Statclock hits processing intr. */
103 struct timespec tu_runtime; /* Realtime. */
104 };
105
106 /*
107 * Description of a process.
108 *
109 * These structures contain the information needed to manage a thread of
110 * control, known in UN*X as a process; it has references to substructures
111 * containing descriptions of things that the process uses, but may share
112 * with related processes.
113 *
114 * struct process is the higher level process containing information
115 * shared by all threads in a process, while struct proc contains the
116 * run-time information needed by threads.
117 */
118 #ifdef __need_process
119 struct futex;
120 LIST_HEAD(futex_list, futex);
121 struct proc;
122 struct tslpentry;
123 TAILQ_HEAD(tslpqueue, tslpentry);
124 struct unveil;
125
126 struct pinsyscall {
127 vaddr_t pn_start;
128 vaddr_t pn_end;
129 u_int *pn_pins; /* array of offsets indexed by syscall# */
130 int pn_npins; /* number of entries in table */
131 };
132
133 /*
134 * Locks used to protect struct members in this file:
135 * I immutable after creation
136 * a atomic operations
137 * K kernel lock
138 * m this process' `ps_mtx'
139 * p this process' `ps_lock'
140 * Q kqueue_ps_list_lock
141 * R rlimit_lock
142 * S scheduler lock
143 * T itimer_mtx
144 */
145 struct process {
146 struct refcnt ps_refcnt;
147
148 /*
149 * ps_mainproc is the original thread in the process.
150 * It's only still special for the handling of
151 * some signal and ptrace behaviors that need to be fixed.
152 */
153 struct proc *ps_mainproc;
154 struct ucred *ps_ucred; /* Process owner's identity. */
155
156 LIST_ENTRY(process) ps_list; /* List of all processes. */
157 TAILQ_HEAD(,proc) ps_threads; /* [K|m] Threads in this process. */
158
159 LIST_ENTRY(process) ps_pglist; /* List of processes in pgrp. */
160 struct process *ps_pptr; /* [K|m] Pointer to parent process. */
161 LIST_ENTRY(process) ps_sibling; /* List of sibling processes. */
162 LIST_HEAD(, process) ps_children;/* Pointer to list of children. */
163 LIST_ENTRY(process) ps_hash; /* Hash chain. */
164
165 /*
166 * An orphan is the child that has been re-parented to the
167 * debugger as a result of attaching to it. Need to keep
168 * track of them for parent to be able to collect the exit
169 * status of what used to be children.
170 */
171 LIST_ENTRY(process) ps_orphan; /* List of orphan processes. */
172 LIST_HEAD(, process) ps_orphans;/* Pointer to list of orphans. */
173
174 struct sigiolst ps_sigiolst; /* List of sigio structures. */
175 struct sigacts *ps_sigacts; /* [I] Signal actions, state */
176 struct vnode *ps_textvp; /* Vnode of executable. */
177 struct filedesc *ps_fd; /* Ptr to open files structure */
178 struct vmspace *ps_vmspace; /* Address space */
179 pid_t ps_pid; /* [I] Process identifier. */
180
181 struct futex_list ps_ftlist; /* futexes attached to this process */
182 struct tslpqueue ps_tslpqueue; /* [p] queue of threads in thrsleep */
183 struct rwlock ps_lock; /* per-process rwlock */
184 struct mutex ps_mtx; /* per-process mutex */
185
186 /* The following fields are all zeroed upon creation in process_new. */
187 #define ps_startzero ps_klist
188 struct klist ps_klist; /* [Q,m] knotes attached to process */
189 u_int ps_flags; /* [a] PS_* flags. */
190 int ps_siglist; /* Signals pending for the process. */
191
192 struct proc *ps_single; /* [m] Thread for single-threading. */
193 u_int ps_singlecnt; /* [m] Number of threads to suspend. */
194 u_int ps_exitcnt; /* [m] Number of threads in exit1. */
195
196 int ps_traceflag; /* Kernel trace points. */
197 struct vnode *ps_tracevp; /* Trace to vnode. */
198 struct ucred *ps_tracecred; /* Creds for writing trace */
199
200 u_int ps_xexit; /* Exit status for wait */
201 int ps_xsig; /* Stopping or killing signal */
202
203 pid_t ps_ppid; /* [K|m] Cached parent pid */
204 int ps_ptmask; /* Ptrace event mask */
205 struct ptrace_state *ps_ptstat;/* Ptrace state */
206 struct process *ps_opptr; /* [K|m] Old parent during ptrace. */
207
208 struct rusage *ps_ru; /* sum of stats for dead threads. */
209 struct tusage ps_tu; /* [m] accumul times of dead threads. */
210 struct rusage ps_cru; /* sum of stats for reaped children */
211 struct itimerspec ps_timer[3]; /* [m] ITIMER_REAL timer */
212 /* [T] ITIMER_{VIRTUAL,PROF} timers */
213 struct timeout ps_rucheck_to; /* [] resource limit check timer */
214 time_t ps_nextxcpu; /* when to send next SIGXCPU, */
215 /* in seconds of process runtime */
216
217 u_int64_t ps_wxcounter;
218
219 struct unveil *ps_uvpaths; /* unveil vnodes and names */
220 ssize_t ps_uvvcount; /* count of unveil vnodes held */
221 size_t ps_uvncount; /* count of unveil names allocated */
222 int ps_uvdone; /* no more unveil is permitted */
223
224 /* End area that is zeroed on creation. */
225 #define ps_endzero ps_startcopy
226
227 /* The following fields are all copied upon creation in process_new. */
228 #define ps_startcopy ps_limit
229 struct plimit *ps_limit; /* [m,R] Process limits. */
230 struct pgrp *ps_pgrp; /* [K|m] Pointer to process group. */
231
232 char ps_comm[_MAXCOMLEN]; /* command name, incl NUL */
233
234 vaddr_t ps_strings; /* User pointers to argv/env */
235 vaddr_t ps_auxinfo; /* User pointer to auxinfo */
236 vaddr_t ps_timekeep; /* User pointer to timekeep */
237 vaddr_t ps_sigcode; /* [I] User pointer to signal code */
238 vaddr_t ps_sigcoderet; /* [I] User ptr to sigreturn retPC */
239 u_long ps_sigcookie; /* [I] */
240 u_int ps_rtableid; /* [a] Process routing table/domain. */
241 char ps_nice; /* Process "nice" value. */
242
243 struct uprof { /* profile arguments */
244 caddr_t pr_base; /* buffer base */
245 size_t pr_size; /* buffer size */
246 u_long pr_off; /* pc offset */
247 u_int pr_scale; /* pc scaling */
248 } ps_prof;
249
250 u_int32_t ps_acflag; /* Accounting flags. */
251
252 uint64_t ps_pledge; /* [m] pledge promises */
253 uint64_t ps_execpledge; /* [m] execpledge promises */
254
255 int64_t ps_kbind_cookie; /* [m] */
256 u_long ps_kbind_addr; /* [m] */
257 /* an address that can't be in userspace or kernelspace */
258 #define BOGO_PC (u_long)-1
259
260 struct pinsyscall ps_pin; /* static or ld.so */
261 struct pinsyscall ps_libcpin; /* libc.so, from pinsyscalls(2) */
262
263 /* End area that is copied on creation. */
264 #define ps_endcopy ps_threadcnt
265 u_int ps_threadcnt; /* [m] Number of threads. */
266
267 struct timespec ps_start; /* starting uptime. */
268 struct timeout ps_realit_to; /* [m] ITIMER_REAL timeout */
269 };
270
271 #define ps_session ps_pgrp->pg_session
272 #define ps_pgid ps_pgrp->pg_id
273
274 #endif /* __need_process */
275
276 /*
277 * These flags are kept in ps_flags.
278 */
279 #define PS_CONTROLT 0x00000001 /* Has a controlling terminal. */
280 #define PS_EXEC 0x00000002 /* Process called exec. */
281 #define PS_INEXEC 0x00000004 /* Process is doing an exec right now */
282 #define PS_EXITING 0x00000008 /* Process is exiting. */
283 #define PS_SUGID 0x00000010 /* Had set id privs since last exec. */
284 #define PS_SUGIDEXEC 0x00000020 /* last execve() was set[ug]id */
285 #define PS_PPWAIT 0x00000040 /* Parent waits for exec/exit. */
286 #define PS_ISPWAIT 0x00000080 /* Is parent of PPWAIT child. */
287 #define PS_PROFIL 0x00000100 /* Has started profiling. */
288 #define PS_TRACED 0x00000200 /* Being ptraced. */
289 #define PS_WAITED 0x00000400 /* Stopped proc was waited for. */
290 #define PS_COREDUMP 0x00000800 /* Busy coredumping */
291 #define PS_SINGLEEXIT 0x00001000 /* Other threads must die. */
292 #define PS_SINGLEUNWIND 0x00002000 /* Other threads must unwind. */
293 #define PS_NOZOMBIE 0x00004000 /* No signal or zombie at exit. */
294 #define PS_STOPPING 0x00008000 /* Just stopped, need sig to parent. */
295 #define PS_SYSTEM 0x00010000 /* No sigs, stats or swapping. */
296 #define PS_EMBRYO 0x00020000 /* New process, not yet fledged */
297 #define PS_ZOMBIE 0x00040000 /* Dead and ready to be waited for */
298 #define PS_NOBROADCASTKILL 0x00080000 /* Process excluded from kill -1. */
299 #define PS_PLEDGE 0x00100000 /* Has called pledge(2) */
300 #define PS_WXNEEDED 0x00200000 /* Process allowed to violate W^X */
301 #define PS_EXECPLEDGE 0x00400000 /* Has exec pledges */
302 #define PS_ORPHAN 0x00800000 /* Process is on an orphan list */
303 #define PS_CHROOT 0x01000000 /* Process is chrooted */
304 #define PS_NOBTCFI 0x02000000 /* No Branch Target CFI */
305 #define PS_ITIMER 0x04000000 /* Virtual interval timers running */
306 #define PS_CONTINUED 0x20000000 /* Continued proc not yet waited for */
307 #define PS_STOPPED 0x40000000 /* Stopped process */
308
309 #define PS_BITS \
310 ("\20" "\01CONTROLT" "\02EXEC" "\03INEXEC" "\04EXITING" "\05SUGID" \
311 "\06SUGIDEXEC" "\07PPWAIT" "\010ISPWAIT" "\011PROFIL" "\012TRACED" \
312 "\013WAITED" "\014COREDUMP" "\015SINGLEEXIT" "\016SINGLEUNWIND" \
313 "\017NOZOMBIE" "\020STOPPING" "\021SYSTEM" "\022EMBRYO" "\023ZOMBIE" \
314 "\024NOBROADCASTKILL" "\025PLEDGE" "\026WXNEEDED" "\027EXECPLEDGE" \
315 "\030ORPHAN" "\031CHROOT" "\032NOBTCFI" "\033ITIMER" "\036CONTINUED" \
316 "\037STOPPED")
317
318 struct kcov_dev;
319 struct lock_list_entry;
320 struct kqueue;
321
322 struct p_inentry {
323 u_long ie_serial;
324 vaddr_t ie_start;
325 vaddr_t ie_end;
326 };
327
328 /*
329 * Locks used to protect struct members in this file:
330 * I immutable after creation
331 * S scheduler lock
332 * U uidinfolk
333 * l read only reference, see lim_read_enter()
334 * o owned (modified only) by this thread
335 * m this proc's' `p->p_p->ps_mtx'
336 */
337 struct proc {
338 TAILQ_ENTRY(proc) p_runq; /* [S] current run/sleep queue */
339 LIST_ENTRY(proc) p_list; /* List of all threads. */
340
341 struct process *p_p; /* [I] The process of this thread. */
342 TAILQ_ENTRY(proc) p_thr_link; /* [K|m] Threads in a process linkage. */
343
344 TAILQ_ENTRY(proc) p_fut_link; /* Threads in a futex linkage. */
345 struct futex *p_futex; /* Current sleeping futex. */
346
347 /* substructures: */
348 struct filedesc *p_fd; /* copy of p_p->ps_fd */
349 struct vmspace *p_vmspace; /* [I] copy of p_p->ps_vmspace */
350 struct p_inentry p_spinentry; /* [o] cache for SP check */
351
352 int p_flag; /* P_* flags. */
353 u_char p_spare; /* unused */
354 char p_stat; /* [S] S* process status. */
355 u_char p_runpri; /* [S] Runqueue priority */
356 u_char p_descfd; /* if not 255, fdesc permits this fd */
357
358 pid_t p_tid; /* Thread identifier. */
359 LIST_ENTRY(proc) p_hash; /* Hash chain. */
360
361 /* The following fields are all zeroed upon creation in fork. */
362 #define p_startzero p_dupfd
363 int p_dupfd; /* Sideways return value from filedescopen. XXX */
364
365 /* scheduling */
366 int p_cpticks; /* Ticks of cpu time. */
367 const volatile void *p_wchan; /* [S] Sleep address. */
368 struct timeout p_sleep_to;/* timeout for tsleep() */
369 const char *p_wmesg; /* [S] Reason for sleep. */
370 fixpt_t p_pctcpu; /* [S] %cpu for this thread */
371 u_int p_slptime; /* [S] Time since last blocked. */
372 struct cpu_info * volatile p_cpu; /* [S] CPU we're running on. */
373
374 struct rusage p_ru; /* Statistics */
375 struct tusage p_tu; /* [o] accumulated times. */
376
377 struct plimit *p_limit; /* [l] read ref. of p_p->ps_limit */
378 struct kcov_dev *p_kd; /* kcov device handle */
379 struct lock_list_entry *p_sleeplocks; /* WITNESS lock tracking */
380 struct kqueue *p_kq; /* [o] select/poll queue of evts */
381 unsigned long p_kq_serial; /* [o] to check against enqueued evts */
382
383 int p_siglist; /* [a] Signals arrived & not delivered*/
384
385 /* End area that is zeroed on creation. */
386 #define p_endzero p_startcopy
387
388 /* The following fields are all copied upon creation in fork. */
389 #define p_startcopy p_sigmask
390 sigset_t p_sigmask; /* [o] Current signal mask */
391
392 char p_name[_MAXCOMLEN]; /* thread name, incl NUL */
393 u_char p_slppri; /* [S] Sleeping priority */
394 u_char p_usrpri; /* [S] Priority based on p_estcpu & ps_nice */
395 u_int p_estcpu; /* [S] Time averaged val of p_cpticks */
396 int p_pledge_syscall; /* Cache of current syscall */
397
398 struct ucred *p_ucred; /* [o] cached credentials */
399 struct sigaltstack p_sigstk; /* sp & on stack state variable */
400
401 u_long p_prof_addr; /* tmp storage for profiling addr until AST */
402 u_long p_prof_ticks; /* tmp storage for profiling ticks until AST */
403
404 /* End area that is copied on creation. */
405 #define p_endcopy p_addr
406 struct user *p_addr; /* Kernel virtual addr of u-area */
407 struct mdproc p_md; /* Any machine-dependent fields. */
408
409 sigset_t p_oldmask; /* [o] Saved mask from before sigpause */
410 int p_sisig; /* For core dump/debugger XXX */
411 union sigval p_sigval; /* For core dump/debugger XXX */
412 long p_sitrapno; /* For core dump/debugger XXX */
413 int p_sicode; /* For core dump/debugger XXX */
414 };
415
416 /* Status values. */
417 #define SIDL 1 /* Thread being created by fork. */
418 #define SRUN 2 /* Currently runnable. */
419 #define SSLEEP 3 /* Sleeping on an address. */
420 #define SSTOP 4 /* Debugging or suspension. */
421 #define SZOMB 5 /* unused */
422 #define SDEAD 6 /* Thread is almost gone */
423 #define SONPROC 7 /* Thread is currently on a CPU. */
424
425 #define P_ZOMBIE(p) ((p)->p_stat == SDEAD)
426 #define P_HASSIBLING(p) ((p)->p_p->ps_threadcnt > 1)
427
428 /*
429 * These flags are per-thread and kept in p_flag
430 */
431 #define P_INKTR 0x00000001 /* In a ktrace op, don't recurse */
432 #define P_PROFPEND 0x00000002 /* SIGPROF needs to be posted */
433 #define P_ALRMPEND 0x00000004 /* SIGVTALRM needs to be posted */
434 #define P_SIGSUSPEND 0x00000008 /* Need to restore before-suspend mask*/
435 #define P_CANTSLEEP 0x00000010 /* insomniac thread */
436 #define P_WSLEEP 0x00000020 /* Working on going to sleep. */
437 #define P_SINTR 0x00000080 /* Sleep is interruptible. */
438 #define P_SYSTEM 0x00000200 /* No sigs, stats or swapping. */
439 #define P_TIMEOUT 0x00000400 /* Timing out during sleep. */
440 #define P_TRACESINGLE 0x00001000 /* Ptrace: keep single threaded. */
441 #define P_WEXIT 0x00002000 /* Working on exiting. */
442 #define P_OWEUPC 0x00008000 /* Owe proc an addupc() at next ast. */
443 #define P_SUSPSINGLE 0x00080000 /* Need to stop for single threading. */
444 #define P_THREAD 0x04000000 /* Only a thread, not a real process */
445 #define P_SUSPSIG 0x08000000 /* Stopped from signal. */
446 #define P_CPUPEG 0x40000000 /* Do not move to another cpu. */
447
448 #define P_BITS \
449 ("\20" "\01INKTR" "\02PROFPEND" "\03ALRMPEND" "\04SIGSUSPEND" \
450 "\05CANTSLEEP" "\06WSLEEP" "\010SINTR" "\012SYSTEM" "\013TIMEOUT" \
451 "\015TRACESINGLE" "\016WEXIT" "\020OWEUPC" "\024SUSPSINGLE" \
452 "\033THREAD" "\034SUSPSIG" "\037CPUPEG")
453
454 #define THREAD_PID_OFFSET 100000
455
456 #ifdef _KERNEL
457
458 struct uidinfo {
459 LIST_ENTRY(uidinfo) ui_hash; /* [U] */
460 uid_t ui_uid; /* [I] */
461 long ui_proccnt; /* [U] proc structs */
462 long ui_lockcnt; /* [U] lockf structs */
463 };
464
465 struct uidinfo *uid_find(uid_t);
466 void uid_release(struct uidinfo *);
467
468 /*
469 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
470 * as it is used to represent "no process group".
471 * We set PID_MAX to 99999 to keep it in 5 columns in ps
472 * When exposed to userspace, thread IDs have THREAD_PID_OFFSET
473 * added to keep them from overlapping the PID range. For them,
474 * we use a * a (0 .. 2^n] range for cheapness, picking 'n' such
475 * that 2^n + THREAD_PID_OFFSET and THREAD_PID_OFFSET have
476 * the same number of columns when printed.
477 */
478 #define PID_MAX 99999
479 #define TID_MASK 0x7ffff
480
481 #define NO_PID (PID_MAX+1)
482
483 #define SESS_LEADER(pr) ((pr)->ps_session->s_leader == (pr))
484 #define SESSHOLD(s) ((s)->s_count++)
485 #define SESSRELE(s) do { \
486 if (--(s)->s_count == 0) { \
487 timeout_del(&(s)->s_verauthto); \
488 pool_put(&session_pool, (s)); \
489 } \
490 } while (/* CONSTCOND */ 0)
491
492 /*
493 * Flags to fork1().
494 */
495 #define FORK_FORK 0x00000001
496 #define FORK_VFORK 0x00000002
497 #define FORK_IDLE 0x00000004
498 #define FORK_PPWAIT 0x00000008
499 #define FORK_SHAREFILES 0x00000010
500 #define FORK_SYSTEM 0x00000020
501 #define FORK_NOZOMBIE 0x00000040
502 #define FORK_SHAREVM 0x00000080
503 #define FORK_PTRACE 0x00000400
504
505 #define EXIT_NORMAL 0x00000001
506 #define EXIT_THREAD 0x00000002
507 #define EXIT_THREAD_NOCHECK 0x00000003
508
509 #define TIDHASH(tid) (&tidhashtbl[(tid) & tidhash])
510 extern LIST_HEAD(tidhashhead, proc) *tidhashtbl;
511 extern u_long tidhash;
512
513 #define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
514 extern LIST_HEAD(pidhashhead, process) *pidhashtbl;
515 extern u_long pidhash;
516
517 #define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
518 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
519 extern u_long pgrphash;
520
521 extern struct proc proc0; /* Process slot for swapper. */
522 extern struct process process0; /* Process slot for kernel threads. */
523 extern int nprocesses, maxprocess; /* Cur and max number of processes. */
524 extern int nthreads, maxthread; /* Cur and max number of threads. */
525
526 LIST_HEAD(proclist, proc);
527 LIST_HEAD(processlist, process);
528 extern struct processlist allprocess; /* List of all processes. */
529 extern struct processlist zombprocess; /* List of zombie processes. */
530 extern struct proclist allproc; /* List of all threads. */
531
532 extern struct process *initprocess; /* Process slot for init. */
533 extern struct proc *reaperproc; /* Thread slot for reaper. */
534 extern struct proc *syncerproc; /* filesystem syncer daemon */
535
536 extern struct pool process_pool; /* memory pool for processes */
537 extern struct pool proc_pool; /* memory pool for procs */
538 extern struct pool rusage_pool; /* memory pool for zombies */
539 extern struct pool ucred_pool; /* memory pool for ucreds */
540 extern struct pool session_pool; /* memory pool for sessions */
541 extern struct pool pgrp_pool; /* memory pool for pgrps */
542
543 void freepid(pid_t);
544
545 struct process *prfind(pid_t); /* Find process by id. */
546 struct process *zombiefind(pid_t); /* Find zombie process by id. */
547 struct proc *tfind(pid_t); /* Find thread by id. */
548 struct pgrp *pgfind(pid_t); /* Find process group by id. */
549 struct proc *tfind_user(pid_t, struct process *);
550 /* Find thread by userspace id. */
551 void proc_printit(struct proc *p, const char *modif,
552 int (*pr)(const char *, ...));
553
554 int chgproccnt(uid_t uid, int diff);
555 void enternewpgrp(struct process *, struct pgrp *, struct session *);
556 void enterthispgrp(struct process *, struct pgrp *);
557 int inferior(struct process *, struct process *);
558 void leavepgrp(struct process *);
559 void killjobc(struct process *);
560 void preempt(void);
561 void procinit(void);
562 void setpriority(struct proc *, uint32_t, uint8_t);
563 void setrunnable(struct proc *);
564 void endtsleep(void *);
565 int wakeup_proc(struct proc *, int);
566 void unsleep(struct proc *);
567 void reaper(void *);
568 __dead void exit1(struct proc *, int, int, int);
569 void exit2(struct proc *);
570 void cpu_fork(struct proc *_curp, struct proc *_child, void *_stack,
571 void *_tcb, void (*_func)(void *), void *_arg);
572 void cpu_exit(struct proc *);
573 void process_initialize(struct process *, struct proc *);
574 int fork1(struct proc *_curp, int _flags, void (*_func)(void *),
575 void *_arg, register_t *_retval, struct proc **_newprocp);
576 int thread_fork(struct proc *_curp, void *_stack, void *_tcb,
577 pid_t *_tidptr, register_t *_retval);
578 int groupmember(gid_t, struct ucred *);
579 void dorefreshcreds(struct process *, struct proc *);
580 void dosigsuspend(struct proc *, sigset_t);
581
582 static inline void
refreshcreds(struct proc * p)583 refreshcreds(struct proc *p)
584 {
585 struct process *pr = p->p_p;
586
587 /* this is an unlocked access to ps_ucred, but the result is benign */
588 if (pr->ps_ucred != p->p_ucred)
589 dorefreshcreds(pr, p);
590 }
591
592 #define SINGLE_SUSPEND 0x01 /* other threads to stop wherever they are */
593 #define SINGLE_UNWIND 0x02 /* other threads to unwind and stop */
594 #define SINGLE_EXIT 0x03 /* other threads to unwind and then exit */
595 #define SINGLE_MASK 0x0f
596 /* extra flags for single_thread_set */
597 #define SINGLE_DEEP 0x10 /* call is in deep */
598 #define SINGLE_NOWAIT 0x20 /* do not wait for other threads to stop */
599
600 int single_thread_set(struct proc *, int);
601 int single_thread_wait(struct process *, int);
602 void single_thread_clear(struct proc *, int);
603 int single_thread_check(struct proc *, int);
604
605 void child_return(void *);
606
607 int proc_cansugid(struct proc *);
608
609 struct cond {
610 unsigned int c_wait; /* [a] initialized and waiting */
611 };
612
613 #define COND_INITIALIZER() { .c_wait = 1 }
614
615 void proc_trampoline_mi(void);
616
617 /*
618 * functions to handle sets of cpus.
619 *
620 * For now we keep the cpus in ints so that we can use the generic
621 * atomic ops.
622 */
623 #define CPUSET_ASIZE(x) (((x) - 1)/32 + 1)
624 #define CPUSET_SSIZE CPUSET_ASIZE(MAXCPUS)
625 struct cpuset {
626 int cs_set[CPUSET_SSIZE];
627 };
628
629 void cpuset_init_cpu(struct cpu_info *);
630
631 void cpuset_clear(struct cpuset *);
632 void cpuset_add(struct cpuset *, struct cpu_info *);
633 void cpuset_del(struct cpuset *, struct cpu_info *);
634 int cpuset_isset(struct cpuset *, struct cpu_info *);
635 void cpuset_add_all(struct cpuset *);
636 void cpuset_copy(struct cpuset *, struct cpuset *);
637 void cpuset_union(struct cpuset *, struct cpuset *, struct cpuset *);
638 void cpuset_intersection(struct cpuset *t, struct cpuset *, struct cpuset *);
639 void cpuset_complement(struct cpuset *, struct cpuset *, struct cpuset *);
640 int cpuset_cardinality(struct cpuset *);
641 struct cpu_info *cpuset_first(struct cpuset *);
642
643 static inline void
tu_enter(struct tusage * tu)644 tu_enter(struct tusage *tu)
645 {
646 ++tu->tu_gen; /* make the generation number odd */
647 membar_producer();
648 }
649
650 static inline void
tu_leave(struct tusage * tu)651 tu_leave(struct tusage *tu)
652 {
653 membar_producer();
654 ++tu->tu_gen; /* make the generation number even again */
655 }
656
657 #endif /* _KERNEL */
658 #endif /* !_SYS_PROC_H_ */
659
660