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