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