]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/proc_internal.h
xnu-3789.21.4.tar.gz
[apple/xnu.git] / bsd / sys / proc_internal.h
CommitLineData
91447636 1/*
6d2010ae 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
27 */
28/* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29/*-
30 * Copyright (c) 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)proc_internal.h 8.15 (Berkeley) 5/19/95
67 */
2d21ac55
A
68/*
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
72 * Version 2.0.
73 */
91447636
A
74
75#ifndef _SYS_PROC_INTERNAL_H_
76#define _SYS_PROC_INTERNAL_H_
77
2d21ac55 78#include <libkern/OSAtomic.h>
91447636 79#include <sys/proc.h>
39037602
A
80#include <mach/resource_monitors.h> // command/proc_name_t
81
82
91447636
A
83__BEGIN_DECLS
84#include <kern/locks.h>
b0d623f7
A
85#if PSYNCH
86#include <kern/thread_call.h>
87#endif /* PSYNCH */
91447636
A
88__END_DECLS
89
593a1d5f 90#if DEBUG
2d21ac55 91#define __PROC_INTERNAL_DEBUG 1
593a1d5f 92#endif
2d21ac55
A
93
94/*
95 * The short form for various locks that protect fields in the data structures.
96 * PL = Process Lock
97 * PGL = Process Group Lock
98 * PFDL = Process File Desc Lock
4bd07ac2 99 * PUCL = Process User Credentials Lock
2d21ac55
A
100 * PSL = Process Spin Lock
101 * PPL = Parent Process Lock (planed for later usage)
102 * LL = List Lock
103 * SL = Session Lock
104*/
105struct label;
106
91447636
A
107/*
108 * One structure allocated per session.
109 */
110struct session {
2d21ac55
A
111 int s_count; /* Ref cnt; pgrps in session. (LL) */
112 struct proc * s_leader; /* Session leader.(static) */
113 struct vnode * s_ttyvp; /* Vnode of controlling terminal.(SL) */
114 int s_ttyvid; /* Vnode id of the controlling terminal (SL) */
b0d623f7 115 struct tty * s_ttyp; /* Controlling terminal. (SL + ttyvp != NULL) */
2d21ac55
A
116 pid_t s_ttypgrpid; /* tty's pgrp id */
117 pid_t s_sid; /* Session ID (static) */
118 char s_login[MAXLOGNAME]; /* Setlogin() name.(SL) */
119 int s_flags; /* Session flags (s_mlock) */
120 LIST_ENTRY(session) s_hash; /* Hash chain.(LL) */
121 lck_mtx_t s_mlock; /* mutex lock to protect session */
122 int s_listflags;
91447636
A
123};
124
2d21ac55
A
125#define SESSION_NULL (struct session *)0
126
b0d623f7
A
127/*
128 * accessor for s_ttyp which treats it as invalid if s_ttyvp is not valid;
129 * note that s_ttyp is not a reference in the session structre, so it can
130 * become invalid out from under the session if the device is closed, without
131 * this protection. We can't safely make it into a reference without reflexive
132 * close notification of tty devices through cdevsw[].
133 *
134 * NB: <sys/tty.h> is not in scope and there is not typedef type enforcement,
135 * or '0' below would be 'TTY_NULL'.
136 */
137#define SESSION_TP(sp) (((sp)->s_ttyvp != 0) ? (sp)->s_ttyp : 0)
138
2d21ac55
A
139/*
140 * Session flags; used to tunnel information to lower layers and line
141 * disciplines, etc.
142 */
143#define S_DEFAULT 0x00000000 /* No flags set */
144#define S_NOCTTY 0x00000001 /* Do not associate controlling tty */
39236c6e 145#define S_CTTYREF 0x00000010 /* vnode ref taken by cttyopen */
2d21ac55
A
146
147
148#define S_LIST_TERM 1 /* marked for termination */
149#define S_LIST_DEAD 2 /* already dead */
91447636
A
150/*
151 * One structure allocated per process group.
152 */
153struct pgrp {
2d21ac55
A
154 LIST_ENTRY(pgrp) pg_hash; /* Hash chain. (LL) */
155 LIST_HEAD(, proc) pg_members; /* Pointer to pgrp members. (PGL) */
156 struct session * pg_session; /* Pointer to session. (LL ) */
157 pid_t pg_id; /* Pgrp id. (static) */
158 int pg_jobc; /* # procs qualifying pgrp for job control (PGL) */
159 int pg_membercnt; /* Number of processes in the pgrocess group (PGL) */
160 int pg_refcount; /* number of current iterators (LL) */
161 unsigned int pg_listflags; /* (LL) */
162 lck_mtx_t pg_mlock; /* mutex lock to protect pgrp */
91447636
A
163};
164
2d21ac55
A
165#define PGRP_FLAG_TERMINATE 1
166#define PGRP_FLAG_WAITTERMINATE 2
167#define PGRP_FLAG_DEAD 4
168#define PGRP_FLAG_ITERABEGIN 8
169#define PGRP_FLAG_ITERWAIT 0x10
170
171#define PGRP_NULL (struct pgrp *)0
91447636
A
172struct proc;
173
ff6e181a 174#define PROC_NULL (struct proc *)0
91447636 175
6d2010ae
A
176#define PROC_UPDATE_CREDS_ONPROC(p) { \
177 p->p_uid = kauth_cred_getuid(p->p_ucred); \
178 p->p_gid = kauth_cred_getgid(p->p_ucred); \
179 p->p_ruid = kauth_cred_getruid(p->p_ucred); \
180 p->p_rgid = kauth_cred_getrgid(p->p_ucred); \
181 p->p_svuid = kauth_cred_getsvuid(p->p_ucred); \
182 p->p_svgid = kauth_cred_getsvgid(p->p_ucred); \
183 }
91447636
A
184/*
185 * Description of a process.
186 *
187 * This structure contains the information needed to manage a thread of
188 * control, known in UN*X as a process; it has references to substructures
189 * containing descriptions of things that the process uses, but may share
190 * with related processes. The process structure and the substructures
191 * are always addressible except for those marked "(PROC ONLY)" below,
192 * which might be addressible only on a processor on which the process
193 * is running.
194 */
195struct proc {
2d21ac55 196 LIST_ENTRY(proc) p_list; /* List of all processes. */
91447636 197
2d21ac55
A
198 pid_t p_pid; /* Process identifier. (static)*/
199 void * task; /* corresponding task (static)*/
200 struct proc * p_pptr; /* Pointer to parent process.(LL) */
201 pid_t p_ppid; /* process's parent pid number */
202 pid_t p_pgrpid; /* process group id of the process (LL)*/
6d2010ae
A
203 uid_t p_uid;
204 gid_t p_gid;
205 uid_t p_ruid;
206 gid_t p_rgid;
207 uid_t p_svuid;
208 gid_t p_svgid;
39236c6e
A
209 uint64_t p_uniqueid; /* process unique ID - incremented on fork/spawn/vfork, remains same across exec. */
210 uint64_t p_puniqueid; /* parent's unique ID - set on fork/spawn/vfork, doesn't change if reparented. */
91447636 211
2d21ac55 212 lck_mtx_t p_mlock; /* mutex lock for proc */
91447636 213
2d21ac55
A
214 char p_stat; /* S* process status. (PL)*/
215 char p_shutdownstate;
216 char p_kdebug; /* P_KDEBUG eq (CC)*/
217 char p_btrace; /* P_BTRACE eq (CC)*/
91447636 218
2d21ac55
A
219 LIST_ENTRY(proc) p_pglist; /* List of processes in pgrp.(PGL) */
220 LIST_ENTRY(proc) p_sibling; /* List of sibling processes. (LL)*/
221 LIST_HEAD(, proc) p_children; /* Pointer to list of children. (LL)*/
222 TAILQ_HEAD( , uthread) p_uthlist; /* List of uthreads (PL) */
91447636 223
2d21ac55
A
224 LIST_ENTRY(proc) p_hash; /* Hash chain. (LL)*/
225 TAILQ_HEAD( ,eventqelt) p_evlist; /* (PL) */
91447636 226
490019cf
A
227#if CONFIG_PERSONAS
228 struct persona *p_persona;
229 LIST_ENTRY(proc) p_persona_list;
230#endif
231
2d21ac55 232 lck_mtx_t p_fdmlock; /* proc lock to protect fdesc */
4bd07ac2 233 lck_mtx_t p_ucred_mlock; /* mutex lock to protect p_ucred */
91447636 234
2d21ac55 235 /* substructures: */
4bd07ac2 236 kauth_cred_t p_ucred; /* Process owner's identity. (PUCL) */
2d21ac55
A
237 struct filedesc *p_fd; /* Ptr to open files structure. (PFDL) */
238 struct pstats *p_stats; /* Accounting/statistics (PL). */
239 struct plimit *p_limit; /* Process limits.(PL) */
91447636 240
2d21ac55 241 struct sigacts *p_sigacts; /* Signal actions, state (PL) */
b0d623f7 242 int p_siglist; /* signals captured back from threads */
2d21ac55 243 lck_spin_t p_slock; /* spin lock for itimer/profil protection */
91447636 244
2d21ac55 245#define p_rlimit p_limit->pl_rlimit
91447636 246
2d21ac55
A
247 struct plimit *p_olimit; /* old process limits - not inherited by child (PL) */
248 unsigned int p_flag; /* P_* flags. (atomic bit ops) */
249 unsigned int p_lflag; /* local flags (PL) */
250 unsigned int p_listflag; /* list flags (LL) */
251 unsigned int p_ladvflag; /* local adv flags (atomic) */
252 int p_refcount; /* number of outstanding users(LL) */
253 int p_childrencnt; /* children holding ref on parent (LL) */
254 int p_parentref; /* children lookup ref on parent (LL) */
255
256 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
257 u_int p_xstat; /* Exit status for wait; also stop signal. */
258
259#ifdef _PROC_HAS_SCHEDINFO_
260 /* may need cleanup, not used */
261 u_int p_estcpu; /* Time averaged value of p_cpticks.(used by aio and proc_comapre) */
262 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime (used by aio)*/
263 u_int p_slptime; /* used by proc_compare */
264#endif /* _PROC_HAS_SCHEDINFO_ */
265
266 struct itimerval p_realtimer; /* Alarm timer. (PSL) */
267 struct timeval p_rtime; /* Real time.(PSL) */
268 struct itimerval p_vtimer_user; /* Virtual timers.(PSL) */
269 struct itimerval p_vtimer_prof; /* (PSL) */
270
271 struct timeval p_rlim_cpu; /* Remaining rlim cpu value.(PSL) */
272 int p_debugger; /* NU 1: can exec set-bit programs if suser */
273 boolean_t sigwait; /* indication to suspend (PL) */
274 void *sigwait_thread; /* 'thread' holding sigwait(PL) */
275 void *exit_thread; /* Which thread is exiting(PL) */
276 int p_vforkcnt; /* number of outstanding vforks(PL) */
277 void * p_vforkact; /* activation running this vfork proc)(static) */
278 int p_fpdrainwait; /* (PFDL) */
279 pid_t p_contproc; /* last PID to send us a SIGCONT (PL) */
280
281 /* Following fields are info from SIGCHLD (PL) */
282 pid_t si_pid; /* (PL) */
283 u_int si_status; /* (PL) */
284 u_int si_code; /* (PL) */
285 uid_t si_uid; /* (PL) */
286
287 void * vm_shm; /* (SYSV SHM Lock) for sysV shared memory */
288
289#if CONFIG_DTRACE
290 user_addr_t p_dtrace_argv; /* (write once, read only after that) */
291 user_addr_t p_dtrace_envp; /* (write once, read only after that) */
292 lck_mtx_t p_dtrace_sprlock; /* sun proc lock emulation */
293 int p_dtrace_probes; /* (PL) are there probes for this proc? */
294 u_int p_dtrace_count; /* (sprlock) number of DTrace tracepoints */
6d2010ae 295 uint8_t p_dtrace_stop; /* indicates a DTrace-desired stop */
2d21ac55
A
296 struct dtrace_ptss_page* p_dtrace_ptss_pages; /* (sprlock) list of user ptss pages */
297 struct dtrace_ptss_page_entry* p_dtrace_ptss_free_list; /* (atomic) list of individual ptss entries */
298 struct dtrace_helpers* p_dtrace_helpers; /* (dtrace_lock) DTrace per-proc private */
299 struct dof_ioctl_data* p_dtrace_lazy_dofs; /* (sprlock) unloaded dof_helper_t's */
300#endif /* CONFIG_DTRACE */
301
302/* XXXXXXXXXXXXX BCOPY'ed on fork XXXXXXXXXXXXXXXX */
303/* The following fields are all copied upon creation in fork. */
304#define p_startcopy p_argslen
91447636 305
2d21ac55
A
306 u_int p_argslen; /* Length of process arguments. */
307 int p_argc; /* saved argc for sysctl_procargs() */
308 user_addr_t user_stack; /* where user stack was allocated */
91447636 309 struct vnode *p_textvp; /* Vnode of executable. */
2d21ac55 310 off_t p_textoff; /* offset in executable vnode */
91447636 311
2d21ac55
A
312 sigset_t p_sigmask; /* DEPRECATED */
313 sigset_t p_sigignore; /* Signals being ignored. (PL) */
314 sigset_t p_sigcatch; /* Signals being caught by user.(PL) */
91447636 315
2d21ac55
A
316 u_char p_priority; /* (NU) Process priority. */
317 u_char p_resv0; /* (NU) User-priority based on p_cpu and p_nice. */
318 char p_nice; /* Process "nice" value.(PL) */
319 u_char p_resv1; /* (NU) User-priority based on p_cpu and p_nice. */
91447636 320
2d21ac55
A
321#if CONFIG_MACF
322 int p_mac_enforce; /* MAC policy enforcement control */
323#endif
91447636 324
39037602
A
325 // types currently in sys/param.h
326 command_t p_comm;
327 proc_name_t p_name; /* can be changed by the process */
91447636 328
39037602
A
329
330 struct pgrp *p_pgrp; /* Pointer to process group. (LL) */
2d21ac55 331 uint32_t p_csflags; /* flags for codesign (PL) */
b0d623f7
A
332 uint32_t p_pcaction; /* action for process control on starvation */
333 uint8_t p_uuid[16]; /* from LC_UUID load command */
91447636 334
fe8ab488
A
335 /*
336 * CPU type and subtype of binary slice executed in
337 * this process. Protected by proc lock.
338 */
339 cpu_type_t p_cputype;
340 cpu_subtype_t p_cpusubtype;
341
91447636 342/* End area that is copied on creation. */
2d21ac55 343/* XXXXXXXXXXXXX End of BCOPY'ed on fork (AIOLOCK)XXXXXXXXXXXXXXXX */
b0d623f7
A
344#define p_endcopy p_aio_total_count
345 int p_aio_total_count; /* all allocated AIO requests for this proc */
346 int p_aio_active_count; /* all unfinished AIO requests for this proc */
347 TAILQ_HEAD( , aio_workq_entry ) p_aio_activeq; /* active async IO requests */
348 TAILQ_HEAD( , aio_workq_entry ) p_aio_doneq; /* completed async IO requests */
2d21ac55
A
349
350 struct klist p_klist; /* knote list (PL ?)*/
91447636 351
39236c6e 352 struct rusage_superset *p_ru; /* Exit information. (PL) */
6d2010ae 353 int p_sigwaitcnt;
2d21ac55
A
354 thread_t p_signalholder;
355 thread_t p_transholder;
356
357 /* DEPRECATE following field */
358 u_short p_acflag; /* Accounting flags. */
fe8ab488 359 volatile u_short p_vfs_iopolicy; /* VFS iopolicy flags. (atomic bit ops) */
2d21ac55 360
2d21ac55
A
361 user_addr_t p_threadstart; /* pthread start fn */
362 user_addr_t p_wqthread; /* pthread workqueue fn */
363 int p_pthsize; /* pthread size */
fe8ab488 364 uint32_t p_pth_tsd_offset; /* offset from pthread_t to TSD for new threads */
fe8ab488 365 user_addr_t p_stack_addr_hint; /* stack allocation hint for wq threads */
2d21ac55 366 void * p_wqptr; /* workq ptr */
3e170ce0
A
367 struct kqueue * p_wqkqueue; /* private workq kqueue */
368
2d21ac55
A
369 struct timeval p_start; /* starting time */
370 void * p_rcall;
371 int p_ractive;
593a1d5f 372 int p_idversion; /* version of process identity */
b0d623f7 373 void * p_pthhash; /* pthread waitqueue hash */
39236c6e
A
374 volatile uint64_t was_throttled __attribute__((aligned(8))); /* Counter for number of throttled I/Os */
375 volatile uint64_t did_throttle __attribute__((aligned(8))); /* Counter for number of I/Os this proc throttled */
376
2d21ac55 377#if DIAGNOSTIC
91447636
A
378 unsigned int p_fdlock_pc[4];
379 unsigned int p_fdunlock_pc[4];
91447636
A
380#if SIGNAL_DEBUG
381 unsigned int lockpc[8];
382 unsigned int unlockpc[8];
383#endif /* SIGNAL_DEBUG */
384#endif /* DIAGNOSTIC */
b0d623f7 385 uint64_t p_dispatchqueue_offset;
39236c6e 386 uint64_t p_dispatchqueue_serialno_offset;
316670eb
A
387#if VM_PRESSURE_EVENTS
388 struct timeval vm_pressure_last_notify_tstamp;
389#endif
39236c6e
A
390
391#if CONFIG_MEMORYSTATUS
392 /* Fields protected by proc list lock */
393 TAILQ_ENTRY(proc) p_memstat_list; /* priority bucket link */
394 uint32_t p_memstat_state; /* state */
395 int32_t p_memstat_effectivepriority; /* priority after transaction state accounted for */
396 int32_t p_memstat_requestedpriority; /* active priority */
39236c6e 397 uint32_t p_memstat_dirty; /* dirty state */
fe8ab488 398 uint64_t p_memstat_userdata; /* user state */
39236c6e 399 uint64_t p_memstat_idledeadline; /* time at which process became clean */
39037602
A
400 uint64_t p_memstat_idle_start; /* abstime process transitions into the idle band */
401 uint64_t p_memstat_idle_delta; /* abstime delta spent in idle band */
3e170ce0
A
402 int32_t p_memstat_memlimit; /* cached memory limit, toggles between active and inactive limits */
403 int32_t p_memstat_memlimit_active; /* memory limit enforced when process is in active jetsam state */
404 int32_t p_memstat_memlimit_inactive; /* memory limit enforced when process is in inactive jetsam state */
39236c6e
A
405#if CONFIG_FREEZE
406 uint32_t p_memstat_suspendedfootprint; /* footprint at time of suspensions */
407#endif /* CONFIG_FREEZE */
408#endif /* CONFIG_MEMORYSTATUS */
3e170ce0
A
409
410 /* cached proc-specific data required for corpse inspection */
411 pid_t p_responsible_pid; /* pid resonsible for this process */
39037602
A
412
413 struct os_reason *p_exit_reason;
91447636
A
414};
415
2d21ac55
A
416#define PGRPID_DEAD 0xdeaddead
417
418/* p_listflag */
419#define P_LIST_DRAIN 0x00000001
420#define P_LIST_DRAINWAIT 0x00000002
421#define P_LIST_DRAINED 0x00000004
422#define P_LIST_DEAD 0x00000008
423#define P_LIST_WAITING 0x00000010
424#define P_LIST_EXITED 0x00000040
425#define P_LIST_CHILDDRSTART 0x00000080
426#define P_LIST_CHILDDRAINED 0x00000100
427#define P_LIST_CHILDDRWAIT 0x00000200
428#define P_LIST_CHILDLKWAIT 0x00000400
429#define P_LIST_DEADPARENT 0x00000800
430#define P_LIST_PARENTREFWAIT 0x00001000
431#define P_LIST_INCREATE 0x00002000
432/* 0x4000 & 0x8000 Not used */
433#define P_LIST_INHASH 0x00010000 /* process is in hash */
434#define P_LIST_INPGRP 0x00020000 /* process is in pgrp */
435#define P_LIST_PGRPTRANS 0x00040000 /* pgrp is getting replaced */
436#define P_LIST_PGRPTRWAIT 0x00080000 /* wait for pgrp replacement */
743345f9
A
437#define P_LIST_EXITCOUNT 0x00100000 /* counted for process exit */
438#define P_LIST_REFWAIT 0x00200000 /* wait to take a ref */
2d21ac55 439
91447636 440
b36670ce 441/* local flags */
2d21ac55
A
442#define P_LDELAYTERM 0x00000001 /* */
443#define P_LNOZOMB 0x00000002 /* */
444#define P_LTERM 0x00000004 /* */
445#define P_LEXIT 0x00000008 /* */
446#define P_LPEXIT 0x00000010
b0d623f7 447#define P_LTRANSCOMMIT 0x00000020 /* process is committed to trans */
2d21ac55
A
448#define P_LINTRANSIT 0x00000040 /* process in exec or in creation */
449#define P_LTRANSWAIT 0x00000080 /* waiting for trans to complete */
39236c6e
A
450#define P_LVFORK 0x00000100 /* parent proc of a vfork */
451#define P_LINVFORK 0x00000200 /* child proc of a vfork */
2d21ac55
A
452#define P_LTRACED 0x00000400 /* */
453#define P_LSIGEXC 0x00000800 /* */
454#define P_LNOATTACH 0x00001000 /* */
455#define P_LPPWAIT 0x00002000 /* */
456#define P_LKQWDRAIN 0x00004000
457#define P_LKQWDRAINWAIT 0x00008000
458#define P_LKQWDEAD 0x00010000
459#define P_LLIMCHANGE 0x00020000
460#define P_LLIMWAIT 0x00040000
461#define P_LWAITED 0x00080000
462#define P_LINSIGNAL 0x00100000
2d21ac55 463#define P_LRAGE_VNODES 0x00400000
b0d623f7 464#define P_LREGISTER 0x00800000 /* thread start fns registered */
316670eb 465#define P_LVMRSRCOWNER 0x01000000 /* can handle the resource ownership of */
39236c6e
A
466#define P_LTERM_DECRYPTFAIL 0x04000000 /* process terminating due to key failure to decrypt */
467#define P_LTERM_JETSAM 0x08000000 /* process is being jetsam'd */
468#define P_JETSAM_VMPAGESHORTAGE 0x00000000 /* jetsam: lowest jetsam priority proc, killed due to vm page shortage */
469#define P_JETSAM_VMTHRASHING 0x10000000 /* jetsam: lowest jetsam priority proc, killed due to vm thrashing */
470#define P_JETSAM_HIWAT 0x20000000 /* jetsam: high water mark */
471#define P_JETSAM_PID 0x30000000 /* jetsam: pid */
472#define P_JETSAM_IDLEEXIT 0x40000000 /* jetsam: idle exit */
473#define P_JETSAM_VNODE 0x50000000 /* jetsam: vnode kill */
fe8ab488 474#define P_JETSAM_FCTHRASHING 0x60000000 /* jetsam: lowest jetsam priority proc, killed due to filecache thrashing */
39236c6e 475#define P_JETSAM_MASK 0x70000000 /* jetsam type mask */
b0d623f7
A
476
477/* Process control state for resource starvation */
478#define P_PCTHROTTLE 1
479#define P_PCSUSP 2
480#define P_PCKILL 3
481#define P_PCMAX 3
482
483/* Process control action state on resrouce starvation */
484#define PROC_ACTION_MASK 0xffff0000;
485#define PROC_CONTROL_STATE(p) (p->p_pcaction & P_PCMAX)
486#define PROC_ACTION_STATE(p) ((p->p_pcaction >> 16) & P_PCMAX)
487#define PROC_SETACTION_STATE(p) (p->p_pcaction = (PROC_CONTROL_STATE(p) | (PROC_CONTROL_STATE(p) << 16)))
488#define PROC_RESETACTION_STATE(p) (p->p_pcaction = PROC_CONTROL_STATE(p))
b36670ce 489
6d2010ae 490/* additional process flags */
b36670ce 491#define P_LADVLOCK 0x01
3e170ce0 492#define P_LXBKIDLEINPROG 0x02
91447636 493
39236c6e
A
494/* p_vfs_iopolicy flags */
495#define P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY 0x0001
496
b0d623f7
A
497/* process creation arguments */
498#define PROC_CREATE_FORK 0 /* independent child (running) */
499#define PROC_CREATE_SPAWN 1 /* independent child (suspended) */
500#define PROC_CREATE_VFORK 2 /* child borrows context */
501
91447636
A
502/* LP64 version of extern_proc. all pointers
503 * grow when we're dealing with a 64-bit process.
504 * WARNING - keep in sync with extern_proc
505 * but use native alignment of 64-bit process.
506 */
507
508#ifdef KERNEL
509#include <sys/time.h> /* user_timeval, user_itimerval */
510
3e170ce0
A
511/* This packing breaks symmetry with userspace side (struct extern_proc
512 * of proc.h) for the ARMV7K ABI where 64-bit types are 64-bit aligned
513 */
b0d623f7
A
514#pragma pack(4)
515struct user32_extern_proc {
516 union {
517 struct {
518 uint32_t __p_forw; /* Doubly-linked run/sleep queue. */
519 uint32_t __p_back;
520 } p_st1;
521 struct user32_timeval __p_starttime; /* process start time */
522 } p_un;
523 uint32_t p_vmspace; /* Address space. */
524 uint32_t p_sigacts; /* Signal actions, state (PROC ONLY). */
525 int p_flag; /* P_* flags. */
526 char p_stat; /* S* process status. */
527 pid_t p_pid; /* Process identifier. */
528 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
529 int p_dupfd; /* Sideways return value from fdopen. XXX */
530 /* Mach related */
531 uint32_t user_stack; /* where user stack was allocated */
532 uint32_t exit_thread; /* XXX Which thread is exiting? */
533 int p_debugger; /* allow to debug */
534 boolean_t sigwait; /* indication to suspend */
535 /* scheduling */
536 u_int p_estcpu; /* Time averaged value of p_cpticks. */
537 int p_cpticks; /* Ticks of cpu time. */
538 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
539 uint32_t p_wchan; /* Sleep address. */
540 uint32_t p_wmesg; /* Reason for sleep. */
541 u_int p_swtime; /* Time swapped in or out. */
542 u_int p_slptime; /* Time since last blocked. */
543 struct user32_itimerval p_realtimer; /* Alarm timer. */
544 struct user32_timeval p_rtime; /* Real time. */
545 u_quad_t p_uticks; /* Statclock hits in user mode. */
546 u_quad_t p_sticks; /* Statclock hits in system mode. */
547 u_quad_t p_iticks; /* Statclock hits processing intr. */
548 int p_traceflag; /* Kernel trace points. */
549 uint32_t p_tracep; /* Trace to vnode. */
550 int p_siglist; /* DEPRECATED */
551 uint32_t p_textvp; /* Vnode of executable. */
552 int p_holdcnt; /* If non-zero, don't swap. */
553 sigset_t p_sigmask; /* DEPRECATED. */
554 sigset_t p_sigignore; /* Signals being ignored. */
555 sigset_t p_sigcatch; /* Signals being caught by user. */
556 u_char p_priority; /* Process priority. */
557 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
558 char p_nice; /* Process "nice" value. */
559 char p_comm[MAXCOMLEN+1];
560 uint32_t p_pgrp; /* Pointer to process group. */
561 uint32_t p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */
562 u_short p_xstat; /* Exit status for wait; also stop signal. */
563 u_short p_acflag; /* Accounting flags. */
564 uint32_t p_ru; /* Exit information. XXX */
565};
566#pragma pack()
567struct user64_extern_proc {
91447636
A
568 union {
569 struct {
570 user_addr_t __p_forw; /* Doubly-linked run/sleep queue. */
571 user_addr_t __p_back;
572 } p_st1;
b0d623f7 573 struct user64_timeval __p_starttime; /* process start time */
91447636
A
574 } p_un;
575 user_addr_t p_vmspace; /* Address space. */
576 user_addr_t p_sigacts; /* Signal actions, state (PROC ONLY). */
577 int p_flag; /* P_* flags. */
578 char p_stat; /* S* process status. */
579 pid_t p_pid; /* Process identifier. */
580 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
581 int p_dupfd; /* Sideways return value from fdopen. XXX */
582 /* Mach related */
0c530ab8 583 user_addr_t user_stack __attribute((aligned(8))); /* where user stack was allocated */
91447636
A
584 user_addr_t exit_thread; /* XXX Which thread is exiting? */
585 int p_debugger; /* allow to debug */
586 boolean_t sigwait; /* indication to suspend */
587 /* scheduling */
588 u_int p_estcpu; /* Time averaged value of p_cpticks. */
589 int p_cpticks; /* Ticks of cpu time. */
590 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
0c530ab8 591 user_addr_t p_wchan __attribute((aligned(8))); /* Sleep address. */
91447636
A
592 user_addr_t p_wmesg; /* Reason for sleep. */
593 u_int p_swtime; /* Time swapped in or out. */
594 u_int p_slptime; /* Time since last blocked. */
b0d623f7
A
595 struct user64_itimerval p_realtimer; /* Alarm timer. */
596 struct user64_timeval p_rtime; /* Real time. */
91447636
A
597 u_quad_t p_uticks; /* Statclock hits in user mode. */
598 u_quad_t p_sticks; /* Statclock hits in system mode. */
599 u_quad_t p_iticks; /* Statclock hits processing intr. */
600 int p_traceflag; /* Kernel trace points. */
0c530ab8 601 user_addr_t p_tracep __attribute((aligned(8))); /* Trace to vnode. */
91447636 602 int p_siglist; /* DEPRECATED */
0c530ab8 603 user_addr_t p_textvp __attribute((aligned(8))); /* Vnode of executable. */
91447636
A
604 int p_holdcnt; /* If non-zero, don't swap. */
605 sigset_t p_sigmask; /* DEPRECATED. */
606 sigset_t p_sigignore; /* Signals being ignored. */
607 sigset_t p_sigcatch; /* Signals being caught by user. */
608 u_char p_priority; /* Process priority. */
609 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
610 char p_nice; /* Process "nice" value. */
611 char p_comm[MAXCOMLEN+1];
0c530ab8 612 user_addr_t p_pgrp __attribute((aligned(8))); /* Pointer to process group. */
91447636
A
613 user_addr_t p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */
614 u_short p_xstat; /* Exit status for wait; also stop signal. */
615 u_short p_acflag; /* Accounting flags. */
0c530ab8 616 user_addr_t p_ru __attribute((aligned(8))); /* Exit information. XXX */
91447636 617};
91447636
A
618#endif /* KERNEL */
619
620/*
621 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
622 * as it is used to represent "no process group".
623 */
624extern int nprocs, maxproc; /* Current and max number of procs. */
0c530ab8 625extern int maxprocperuid; /* Current number of procs per uid */
39236c6e 626extern int hard_maxproc; /* hard limit */
b0d623f7 627extern unsigned int proc_shutdown_exitcount;
91447636 628
2d21ac55
A
629#define PID_MAX 99999
630#define NO_PID 100000
631extern lck_mtx_t * proc_list_mlock;
632extern lck_mtx_t * proc_klist_mlock;
633
b0d623f7 634#define BSD_SIMUL_EXECS 33 /* 32 , allow for rounding */
6d2010ae
A
635#define BSD_PAGEABLE_SIZE_PER_EXEC (NCARGS + PAGE_SIZE + PAGE_SIZE) /* page for apple vars, page for executable header */
636extern int execargs_cache_size;
637extern int execargs_free_count;
638extern vm_offset_t * execargs_cache;
b0d623f7 639
2d21ac55
A
640#define SESS_LEADER(p, sessp) ((sessp)->s_leader == (p))
641
91447636
A
642#define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
643extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
644extern u_long pidhash;
645
646#define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
647extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
648extern u_long pgrphash;
2d21ac55
A
649#define SESSHASH(sessid) (&sesshashtbl[(sessid) & sesshash])
650extern LIST_HEAD(sesshashhead, session) *sesshashtbl;
651extern u_long sesshash;
652
91447636 653extern lck_grp_t * proc_lck_grp;
6d2010ae 654#if CONFIG_FINE_LOCK_GROUPS
b0d623f7
A
655extern lck_grp_t * proc_mlock_grp;
656extern lck_grp_t * proc_fdmlock_grp;
4bd07ac2 657extern lck_grp_t * proc_ucred_mlock_grp;
b0d623f7 658extern lck_grp_t * proc_slock_grp;
6d2010ae 659#endif
91447636
A
660extern lck_grp_attr_t * proc_lck_grp_attr;
661extern lck_attr_t * proc_lck_attr;
662
663LIST_HEAD(proclist, proc);
664extern struct proclist allproc; /* List of all processes. */
665extern struct proclist zombproc; /* List of zombie processes. */
39037602 666
91447636 667extern struct proc *initproc;
39236c6e 668extern void procinit(void);
91447636
A
669extern void proc_lock(struct proc *);
670extern void proc_unlock(struct proc *);
2d21ac55
A
671extern void proc_spinlock(struct proc *);
672extern void proc_spinunlock(struct proc *);
673extern void proc_list_lock(void);
674extern void proc_list_unlock(void);
675extern void proc_klist_lock(void);
676extern void proc_klist_unlock(void);
91447636 677extern void proc_fdlock(struct proc *);
2d21ac55 678extern void proc_fdlock_spin(struct proc *);
91447636 679extern void proc_fdunlock(struct proc *);
2d21ac55 680extern void proc_fdlock_assert(proc_t p, int assertflags);
4bd07ac2
A
681extern void proc_ucred_lock(struct proc *);
682extern void proc_ucred_unlock(struct proc *);
2d21ac55
A
683__private_extern__ int proc_core_name(const char *name, uid_t uid, pid_t pid,
684 char *cr_name, size_t cr_name_len);
91447636 685extern int isinferior(struct proc *, struct proc *);
91447636 686__private_extern__ struct proc *pzfind(pid_t); /* Find zombie by id. */
6d2010ae
A
687__private_extern__ struct proc *proc_find_zombref(pid_t); /* Find zombie by id. */
688__private_extern__ void proc_drop_zombref(struct proc * p); /* Find zombie by id. */
689
2d21ac55 690
91447636 691extern int chgproccnt(uid_t uid, int diff);
2d21ac55 692extern void pinsertchild(struct proc *parent, struct proc *child);
91447636
A
693extern int enterpgrp(struct proc *p, pid_t pgid, int mksess);
694extern void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
695extern int inferior(struct proc *p);
696extern int leavepgrp(struct proc *p);
697extern void resetpriority(struct proc *);
698extern void setrunnable(struct proc *);
699extern void setrunqueue(struct proc *);
700extern int sleep(void *chan, int pri);
701extern int tsleep0(void *chan, int pri, const char *wmesg, int timo, int (*continuation)(int));
702extern int tsleep1(void *chan, int pri, const char *wmesg, u_int64_t abstime, int (*continuation)(int));
703extern int msleep0(void *chan, lck_mtx_t *mtx, int pri, const char *wmesg, int timo, int (*continuation)(int));
b0d623f7 704extern void vfork_return(struct proc *child, int32_t *retval, int rval);
2d21ac55 705extern int exit1(struct proc *, int, int *);
39236c6e 706extern int exit1_internal(struct proc *, int, int *, boolean_t, boolean_t, int);
39037602 707extern int exit_with_reason(struct proc *, int, int *, boolean_t, boolean_t, int, struct os_reason *);
3e170ce0 708extern int fork1(proc_t, thread_t *, int, coalition_t *);
2d21ac55
A
709extern void vfork_exit_internal(struct proc *p, int rv, int forced);
710extern void proc_reparentlocked(struct proc *child, struct proc * newparent, int cansignal, int locked);
39037602 711
fe8ab488 712extern proc_t proc_findinternal(int pid, int locked);
316670eb 713extern proc_t proc_findthread(thread_t thread);
2d21ac55 714extern void proc_refdrain(proc_t);
743345f9
A
715extern proc_t proc_refdrain_with_refwait(proc_t p, boolean_t get_ref_and_allow_wait);
716extern void proc_refwake(proc_t p);
2d21ac55
A
717extern void proc_childdrainlocked(proc_t);
718extern void proc_childdrainstart(proc_t);
719extern void proc_childdrainend(proc_t);
720extern void proc_checkdeadrefs(proc_t);
721struct proc *pfind_locked(pid_t);
722extern struct pgrp *pgfind(pid_t);
723extern void pg_rele(struct pgrp * pgrp);
724extern struct session * session_find_internal(pid_t sessid);
725extern struct pgrp * proc_pgrp(proc_t);
726extern struct pgrp * tty_pgrp(struct tty * tp);
727extern struct pgrp * pgfind_internal(pid_t);
728extern struct session * proc_session(proc_t);
729extern void pgrp_lock(struct pgrp * pgrp);
730extern void pgrp_unlock(struct pgrp * pgrp);
731extern void session_lock(struct session * sess);
732extern void session_unlock(struct session * sess);
733extern struct session * pgrp_session(struct pgrp * pgrp);
734extern void session_rele(struct session *sess);
735extern int isbackground(proc_t p, struct tty *tp);
736extern proc_t proc_parent(proc_t);
737extern proc_t proc_parentholdref(proc_t);
738extern int proc_parentdropref(proc_t, int);
739int itimerfix(struct timeval *tv);
740int itimerdecr(struct proc * p, struct itimerval *itp, int usec);
39037602 741void proc_free_realitimer(proc_t proc);
4bd07ac2 742int timespec_is_valid(const struct timespec *);
2d21ac55
A
743void proc_signalstart(struct proc *, int locked);
744void proc_signalend(struct proc *, int locked);
fe8ab488 745int proc_transstart(struct proc *, int locked, int non_blocking);
b0d623f7 746void proc_transcommit(struct proc *, int locked);
2d21ac55 747void proc_transend(struct proc *, int locked);
b0d623f7 748int proc_transwait(struct proc *, int locked);
2d21ac55 749void proc_rele_locked(struct proc * p);
6d2010ae 750struct proc *proc_ref_locked(struct proc * p);
2d21ac55 751void proc_knote(struct proc * p, long hint);
b0d623f7 752void proc_knote_drain(struct proc *p);
b0d623f7
A
753void proc_setregister(proc_t p);
754void proc_resetregister(proc_t p);
755/* returns the first thread_t in the process, or NULL XXX for NFS, DO NOT USE */
756thread_t proc_thread(proc_t);
757extern int proc_pendingsignals(proc_t, sigset_t);
758int proc_getpcontrol(int pid, int * pcontrolp);
fe8ab488 759int proc_dopcontrol(proc_t p);
b0d623f7
A
760int proc_resetpcontrol(int pid);
761#if PSYNCH
762void pth_proc_hashinit(proc_t);
763void pth_proc_hashdelete(proc_t);
764void pth_global_hashinit(void);
765extern thread_call_t psynch_thcall;
766void psynch_wq_cleanup(__unused void * param, __unused void * param1);
767extern lck_mtx_t * pthread_list_mlock;
768#endif /* PSYNCH */
769struct uthread * current_uthread(void);
39236c6e 770
39037602
A
771/* process iteration */
772
773#define ALLPROC_FOREACH(var) \
774 LIST_FOREACH((var), &allproc, p_list)
775
776#define ZOMBPROC_FOREACH(var) \
777 LIST_FOREACH((var), &zombproc, p_list)
778
779#define PGMEMBERS_FOREACH(group, var) \
780 LIST_FOREACH((var), &((struct pgrp *)(group))->pg_members, p_pglist)
781
782#define PCHILDREN_FOREACH(parent, var) \
783 LIST_FOREACH((var), &(((struct proc *)(parent))->p_children), p_sibling)
784
785typedef int (*proc_iterate_fn_t)(proc_t, void *);
786
787/*
788 * These are the only valid return values of `callout` functions provided to
789 * process iterators.
790 *
791 * CLAIMED returns expect the caller to call proc_rele on the proc. DONE
792 * returns stop iterating processes early.
793 */
794#define PROC_RETURNED (0)
795#define PROC_RETURNED_DONE (1)
796#define PROC_CLAIMED (2)
797#define PROC_CLAIMED_DONE (3)
798
799/*
800 * pgrp_iterate walks the provided process group, calling `filterfn` with
801 * `filterarg` for each process. For processes where `filterfn` returned
802 * non-zero, `callout` is called with `arg`. If `PGRP_DROPREF` is supplied in
803 * `flags`, a reference will be dropped from the process group after obtaining
804 * the list of processes to call `callout` on.
805 *
806 * `PGMEMBERS_FOREACH` might also be used under the pgrp_lock to achieve a
807 * similar effect.
808 */
809#define PGRP_DROPREF (1)
810
811extern int pgrp_iterate(struct pgrp *pgrp, unsigned int flags, proc_iterate_fn_t callout, void *arg, proc_iterate_fn_t filterfn, void *filterarg);
812
813/*
814 * proc_iterate walks the `allproc` and/or `zombproc` lists, calling `filterfn`
815 * with `filterarg` for each process. For processes where `filterfn` returned
816 * non-zero, `callout` is called with `arg`. If the `PROC_NOWAITTRANS` flag is
817 * set, this function waits for transitions.
818 *
819 * `ALLPROC_FOREACH` or `ZOMBPROC_FOREACH` might also be used under the
820 * `proc_list_lock` to achieve a similar effect.
821 */
822#define PROC_ALLPROCLIST (1U << 0) /* walk the allproc list (processes not yet exited) */
823#define PROC_ZOMBPROCLIST (1U << 1) /* walk the zombie list */
824#define PROC_NOWAITTRANS (1U << 2) /* do not wait for transitions (checkdirs only) */
825
826extern int proc_iterate(unsigned int flags, proc_iterate_fn_t callout, void *arg, proc_iterate_fn_t filterfn, void *filterarg);
827
828/*
829 * proc_childrenwalk walks the children of process `p`, calling `callout` for
830 * each one.
831 *
832 * `PCHILDREN_FOREACH` might also be used under the `proc_list_lock` to achieve
833 * a similar effect.
834 */
835extern int proc_childrenwalk(proc_t p, proc_iterate_fn_t callout, void *arg);
836
837/*
838 * proc_rebootscan should only be used by kern_shutdown.c
839 */
840extern void proc_rebootscan(proc_iterate_fn_t callout, void *arg, proc_iterate_fn_t filterfn, void *filterarg);
39236c6e
A
841
842pid_t dtrace_proc_selfpid(void);
843pid_t dtrace_proc_selfppid(void);
844uid_t dtrace_proc_selfruid(void);
39037602 845
91447636 846#endif /* !_SYS_PROC_INTERNAL_H_ */