1 /* 2 * Copyright (c) 2000-2018 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 <kern/smr.h> 79 #include <kern/kalloc.h> 80 #include <libkern/OSAtomic.h> 81 #include <sys/cdefs.h> 82 #include <sys/filedesc.h> 83 #include <sys/proc.h> 84 #include <sys/proc_ro.h> 85 #include <sys/signalvar.h> 86 #include <mach/resource_monitors.h> // command/proc_name_t 87 88 __BEGIN_DECLS 89 #include <kern/locks.h> 90 #if PSYNCH 91 #include <kern/thread_call.h> 92 #endif /* PSYNCH */ 93 __END_DECLS 94 95 #if DEBUG 96 #define __PROC_INTERNAL_DEBUG 1 97 #endif 98 99 /* 100 * The short form for various locks that protect fields in the data structures. 101 * PL = Process Lock 102 * PGL = Process Group Lock 103 * PUCL = Process User Credentials Lock 104 * PSL = Process Spin Lock 105 * LL = List Lock 106 * SL = Session Lock 107 * TTYL = TTY Lock 108 * 109 * C = constant/static 110 */ 111 struct label; 112 113 /* 114 * Flags kept in the low bits of `struct session::s_refcount` 115 */ 116 __options_decl(session_ref_bits_t, uint32_t, { 117 S_DEFAULT = 0x00, 118 S_NOCTTY = 0x01, /* Do not associate controlling tty */ 119 S_CTTYREF = 0x02, /* vnode ref taken by cttyopen */ 120 }); 121 #define SESSION_REF_BITS 4 /* 2 is enough, 4 is easier in hex */ 122 #define SESSION_REF_MASK ((1u << PGRP_REF_BITS) - 1) 123 124 #define SESSION_NULL ((struct session *)NULL) 125 126 /*! 127 * @struct session 128 * 129 * @brief 130 * Structure to keep track of process sessions 131 * 132 * @discussion 133 * Sessions hang (with +1's) from: 134 * - process groups (@c pgrp::pg_session) 135 * - ttys (@c tty::t_session) 136 * 137 * Lock ordering: TTYL > LL > SL 138 */ 139 struct session { 140 lck_mtx_t s_mlock; /* session lock */ 141 LIST_ENTRY(session) s_hash; /* (LL) hash linkage */ 142 struct proc *s_leader; /* (C) session leader */ 143 struct vnode *s_ttyvp; /* (SL) Vnode of controlling terminal */ 144 struct tty *s_ttyp; /* (SL) Controlling terminal */ 145 uint32_t s_ttyvid; /* (SL) Vnode id of the controlling terminal */ 146 pid_t s_ttypgrpid; /* (SL) tty's pgrp id */ 147 dev_t _Atomic s_ttydev; /* (SL) tty's device */ 148 pid_t s_sid; /* (C) Session ID */ 149 os_ref_atomic_t s_refcount; 150 char s_login[MAXLOGNAME]; /* (SL) Setlogin() name */ 151 }; 152 153 154 /* 155 * Flags for pg_refcnt 156 */ 157 __options_decl(pggrp_ref_bits_t, uint32_t, { 158 PGRP_REF_NONE = 0x00, 159 PGRP_REF_EMPTY = 0x01, /* the process group has no members */ 160 }); 161 #define PGRP_REF_BITS 1 162 #define PGRP_REF_MASK ((1u << PGRP_REF_BITS) - 1) 163 164 #define PGRP_NULL ((struct pgrp *)NULL) 165 166 /*! 167 * @struct pgrp 168 * 169 * @abstract 170 * Describes a process group membership. 171 * 172 * @discussion 173 * <b>locking rules</b> 174 * 175 * Process groups have a static ID (@c pg_id) and session (@c pg_session), 176 * and groups hold a reference on their session. 177 * 178 * Process group membership is protected by the @c pgrp_lock(). 179 * 180 * Lock ordering: TTYL > LL > PGL 181 * 182 * <b>lifetime</b> 183 * Process groups are refcounted, with a packed bit that tracks whether 184 * the group is orphaned (has no members), which prevents it 185 * from being looked up. 186 * 187 * Process groups are retired through @c smr_proc_task_call(). 188 * 189 * Process groups are hashed in a global hash table that can be consulted 190 * while holding the @c proc_list_lock() with @c pghash_find_locked() 191 * or using hazard pointers with @c pgrp_find(). 192 */ 193 struct pgrp { 194 union { 195 lck_mtx_t pg_mlock; /* process group lock (PGL) */ 196 struct smr_node pg_smr_node; 197 }; 198 struct smrq_slink pg_hash; /* hash chain (PLL) */ 199 LIST_HEAD(, proc) pg_members; /* group members (PGL) */ 200 struct session *pg_session; /* session (static) */ 201 pid_t pg_id; /* group ID (static) */ 202 int pg_jobc; /* # procs qualifying pgrp for job control (PGL) */ 203 os_ref_atomic_t pg_refcount; 204 os_ref_atomic_t pg_hashref; 205 }; 206 207 208 __options_decl(proc_ref_bits_t, uint32_t, { 209 P_REF_NONE = 0x00u, 210 P_REF_NEW = 0x01u, /* the proc is being initialized */ 211 P_REF_DEAD = 0x02u, /* the proc is becoming a zombie */ 212 P_REF_WILL_EXEC = 0x04u, /* see proc_refdrain_will_exec() */ 213 P_REF_IN_EXEC = 0x08u, /* see proc_refdrain_will_exec() */ 214 P_REF_DRAINING = 0x10u, /* someone is in proc_refdrain() */ 215 P_REF_SHADOW = 0x20u, /* the proc is shadow proc in exec */ 216 P_REF_PROC_HOLD = 0x40u, /* the proc has ref on the proc task combined struct */ 217 P_REF_TASK_HOLD = 0x80u, /* the task has ref on the proc task combined struct */ 218 }); 219 #define P_REF_BITS 8 220 #define P_REF_MASK ((1u << P_REF_BITS) - 1) 221 222 /* 223 * Kernel signal definitions and data structures, 224 * not exported to user programs. 225 */ 226 struct sigacts; 227 228 /* 229 * Process signal actions and state, needed only within the process 230 * (not necessarily resident). 231 */ 232 struct sigacts { 233 user_addr_t ps_sigact[NSIG]; /* disposition of signals */ 234 user_addr_t ps_trampact[NSIG]; /* disposition of signals */ 235 sigset_t ps_catchmask[NSIG]; /* signals to be blocked */ 236 sigset_t ps_sigonstack; /* signals to take on sigstack */ 237 sigset_t ps_sigintr; /* signals that interrupt syscalls */ 238 sigset_t ps_sigreset; /* signals that reset when caught */ 239 sigset_t ps_signodefer; /* signals not masked while handled */ 240 sigset_t ps_siginfo; /* signals that want SA_SIGINFO args */ 241 sigset_t ps_oldmask; /* saved mask from before sigpause */ 242 _Atomic uint32_t ps_sigreturn_validation; /* sigreturn argument validation state */ 243 int ps_flags; /* signal flags, below */ 244 int ps_sig; /* for core dump/debugger XXX */ 245 int ps_code; /* for core dump/debugger XXX */ 246 int ps_addr; /* for core dump/debugger XXX */ 247 }; 248 249 #define PROC_NULL ((struct proc *)NULL) 250 251 /*! 252 * @struct proc 253 * 254 * @brief 255 * Description of a process. 256 * 257 * @discussion 258 * This structure contains the information needed to manage a thread of 259 * control, known in UN*X as a process; it has references to substructures 260 * containing descriptions of things that the process uses, but may share 261 * with related processes. The process structure and the substructures 262 * are always addressible except for those marked "(PROC ONLY)" below, 263 * which might be addressible only on a processor on which the process 264 * is running. 265 * 266 * The lifetime of a @c proc struct begins from forkproc() and ends at 267 * proc_free(). Do not modify the @c proc struct or rely on its fields 268 * being properly initialized before forkproc(). For corpses, forkproc() 269 * is not called and the @c proc struct is never initialized. 270 */ 271 struct proc { 272 union { 273 LIST_ENTRY(proc) p_list; /* List of all processes. */ 274 struct smr_node p_smr_node; 275 }; 276 struct proc * XNU_PTRAUTH_SIGNED_PTR("proc.p_pptr") p_pptr; /* Pointer to parent process.(LL) */ 277 proc_ro_t p_proc_ro; 278 pid_t p_ppid; /* process's parent pid number */ 279 pid_t p_pgrpid; /* process group id of the process (LL)*/ 280 uid_t p_uid; 281 gid_t p_gid; 282 uid_t p_ruid; 283 gid_t p_rgid; 284 uid_t p_svuid; 285 gid_t p_svgid; 286 pid_t p_sessionid; 287 uint64_t p_puniqueid; /* parent's unique ID - set on fork/spawn, doesn't change if reparented. */ 288 289 lck_mtx_t p_mlock; /* mutex lock for proc */ 290 pid_t p_pid; /* Process identifier for proc_find. (static)*/ 291 char p_stat; /* S* process status. (PL)*/ 292 char p_shutdownstate; 293 char p_kdebug; /* P_KDEBUG eq (CC)*/ 294 char p_btrace; /* P_BTRACE eq (CC)*/ 295 296 LIST_ENTRY(proc) p_pglist; /* List of processes in pgrp (PGL) */ 297 LIST_ENTRY(proc) p_sibling; /* List of sibling processes (LL)*/ 298 LIST_HEAD(, proc) p_children; /* Pointer to list of children (LL)*/ 299 TAILQ_HEAD(, uthread) p_uthlist; /* List of uthreads (PL) */ 300 301 struct smrq_slink p_hash; /* Hash chain (LL)*/ 302 303 #if CONFIG_PERSONAS 304 struct persona *p_persona; 305 LIST_ENTRY(proc) p_persona_list; 306 #endif 307 308 lck_mtx_t p_ucred_mlock; /* mutex lock to protect p_ucred */ 309 #if CONFIG_AUDIT 310 lck_mtx_t p_audit_mlock; /* mutex lock to protect audit sessions */ 311 #endif /* CONFIG_AUDIT */ 312 313 /* substructures: */ 314 struct filedesc p_fd; /* open files structure */ 315 struct pstats *p_stats; /* Accounting/statistics (PL) */ 316 SMR_POINTER(struct plimit *) p_limit;/* Process limits (PL) */ 317 SMR_POINTER(struct pgrp *XNU_PTRAUTH_SIGNED_PTR("proc.p_pgrp")) p_pgrp; /* Pointer to process group. (LL) */ 318 319 struct sigacts p_sigacts; 320 lck_spin_t p_slock; /* spin lock for itimer/profil protection */ 321 322 int p_siglist; /* signals captured back from threads */ 323 unsigned int p_flag; /* P_* flags. (atomic bit ops) */ 324 unsigned int p_lflag; /* local flags (PL) */ 325 unsigned int p_listflag; /* list flags (LL) */ 326 unsigned int p_ladvflag; /* local adv flags (atomic) */ 327 os_ref_atomic_t p_refcount; /* number of outstanding users */ 328 os_ref_atomic_t p_waitref; /* number of users pending transition */ 329 int p_childrencnt; /* children holding ref on parent (LL) */ 330 int p_parentref; /* children lookup ref on parent (LL) */ 331 pid_t p_oppid; /* Save parent pid during ptrace. XXX */ 332 u_int p_xstat; /* Exit status for wait; also stop signal. */ 333 int p_aio_total_count; /* all allocated AIO requests for this proc */ 334 335 #ifdef _PROC_HAS_SCHEDINFO_ 336 /* may need cleanup, not used */ 337 u_int p_estcpu; /* Time averaged value of p_cpticks.(used by aio and proc_comapre) */ 338 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime (used by aio)*/ 339 u_int p_slptime; /* used by proc_compare */ 340 #endif /* _PROC_HAS_SCHEDINFO_ */ 341 342 struct itimerval p_realtimer; /* Alarm timer. (PSL) */ 343 struct timeval p_rtime; /* Real time.(PSL) */ 344 struct itimerval p_vtimer_user; /* Virtual timers.(PSL) */ 345 struct itimerval p_vtimer_prof; /* (PSL) */ 346 347 struct timeval p_rlim_cpu; /* Remaining rlim cpu value.(PSL) */ 348 int p_debugger; /* NU 1: can exec set-bit programs if suser */ 349 boolean_t sigwait; /* indication to suspend (PL) */ 350 void *sigwait_thread; /* 'thread' holding sigwait(PL) */ 351 void *exit_thread; /* Which thread is exiting(PL) */ 352 /* Following fields are info from SIGCHLD (PL) */ 353 pid_t si_pid; /* (PL) */ 354 u_int si_status; /* (PL) */ 355 u_int si_code; /* (PL) */ 356 uid_t si_uid; /* (PL) */ 357 358 void * vm_shm; /* (SYSV SHM Lock) for sysV shared memory */ 359 int p_ractive; 360 /* cached proc-specific data required for corpse inspection */ 361 pid_t p_responsible_pid; /* pid responsible for this process */ 362 363 #if CONFIG_DTRACE 364 int p_dtrace_probes; /* (PL) are there probes for this proc? */ 365 u_int p_dtrace_count; /* (sprlock) number of DTrace tracepoints */ 366 uint8_t p_dtrace_stop; /* indicates a DTrace-desired stop */ 367 user_addr_t p_dtrace_argv; /* (write once, read only after that) */ 368 user_addr_t p_dtrace_envp; /* (write once, read only after that) */ 369 lck_mtx_t p_dtrace_sprlock; /* sun proc lock emulation */ 370 struct dtrace_ptss_page* p_dtrace_ptss_pages; /* (sprlock) list of user ptss pages */ 371 struct dtrace_ptss_page_entry* p_dtrace_ptss_free_list; /* (atomic) list of individual ptss entries */ 372 struct dtrace_helpers* p_dtrace_helpers; /* (dtrace_lock) DTrace per-proc private */ 373 struct dof_ioctl_data* p_dtrace_lazy_dofs; /* (sprlock) unloaded dof_helper_t's */ 374 #endif /* CONFIG_DTRACE */ 375 376 __xnu_struct_group(proc_forkcopy_data, p_forkcopy, { 377 u_int p_argslen; /* Length of process arguments. */ 378 int p_argc; /* saved argc for sysctl_procargs() */ 379 user_addr_t user_stack; /* where user stack was allocated */ 380 struct vnode * XNU_PTRAUTH_SIGNED_PTR("proc.p_textvp") p_textvp; /* Vnode of executable. */ 381 off_t p_textoff; /* offset in executable vnode */ 382 383 sigset_t p_sigmask; /* DEPRECATED */ 384 sigset_t p_sigignore; /* Signals being ignored. (PL) */ 385 sigset_t p_sigcatch; /* Signals being caught by user.(PL) */ 386 sigset_t p_workq_allow_sigmask; /* Signals allowed for workq threads. Updates protected by proc_lock. */ 387 388 u_char p_priority; /* (NU) Process priority. */ 389 u_char p_resv0; /* (NU) User-priority based on p_cpu and p_nice. */ 390 char p_nice; /* Process "nice" value.(PL) */ 391 u_char p_resv1; /* (NU) User-priority based on p_cpu and p_nice. */ 392 393 // types currently in sys/param.h 394 command_t p_comm; 395 proc_name_t p_name; /* can be changed by the process */ 396 uint8_t p_xhighbits; /* Stores the top byte of exit status to avoid truncation*/ 397 pid_t p_contproc; /* last PID to send us a SIGCONT (PL) */ 398 399 uint32_t p_pcaction; /* action for process control on starvation */ 400 uint8_t p_uuid[16]; /* from LC_UUID load command */ 401 402 uint8_t p_responsible_uuid[16]; /* UUID of pid responsible for this process */ 403 404 /* 405 * CPU type and subtype of binary slice executed in 406 * this process. Protected by proc lock. 407 */ 408 cpu_type_t p_cputype; 409 cpu_subtype_t p_cpusubtype; 410 }); 411 412 TAILQ_HEAD(, aio_workq_entry ) p_aio_activeq; /* active async IO requests */ 413 TAILQ_HEAD(, aio_workq_entry ) p_aio_doneq; /* completed async IO requests */ 414 415 struct klist p_klist; /* knote list (PL ?)*/ 416 417 struct rusage_superset *p_ru; /* Exit information. (PL) */ 418 thread_t p_signalholder; 419 thread_t p_transholder; 420 int p_sigwaitcnt; 421 /* DEPRECATE following field */ 422 u_short p_acflag; /* Accounting flags. */ 423 volatile u_short p_vfs_iopolicy; /* VFS iopolicy flags. (atomic bit ops) */ 424 425 user_addr_t p_threadstart; /* pthread start fn */ 426 user_addr_t p_wqthread; /* pthread workqueue fn */ 427 int p_pthsize; /* pthread size */ 428 uint32_t p_pth_tsd_offset; /* offset from pthread_t to TSD for new threads */ 429 user_addr_t p_stack_addr_hint; /* stack allocation hint for wq threads */ 430 struct workqueue *_Atomic p_wqptr; /* workq ptr */ 431 struct workq_aio_s *_Atomic p_aio_wqptr; /* aio_workq ptr */ 432 433 434 struct timeval p_start; /* starting time */ 435 void * p_rcall; 436 void * p_pthhash; /* pthread waitqueue hash */ 437 volatile uint64_t was_throttled __attribute__((aligned(8))); /* Counter for number of throttled I/Os */ 438 volatile uint64_t did_throttle __attribute__((aligned(8))); /* Counter for number of I/Os this proc throttled */ 439 440 #if DIAGNOSTIC 441 unsigned int p_fdlock_pc[4]; 442 unsigned int p_fdunlock_pc[4]; 443 #if SIGNAL_DEBUG 444 unsigned int lockpc[8]; 445 unsigned int unlockpc[8]; 446 #endif /* SIGNAL_DEBUG */ 447 #endif /* DIAGNOSTIC */ 448 uint64_t p_dispatchqueue_offset; 449 uint64_t p_dispatchqueue_serialno_offset; 450 uint64_t p_dispatchqueue_label_offset; 451 uint64_t p_return_to_kernel_offset; 452 uint64_t p_mach_thread_self_offset; 453 /* The offset is set to 0 if userspace is not requesting for this feature */ 454 uint64_t p_pthread_wq_quantum_offset; 455 #if VM_PRESSURE_EVENTS 456 struct timeval vm_pressure_last_notify_tstamp; 457 #endif 458 uint8_t p_crash_behavior; /* bit fields to control behavior on crash. See spawn.h POSIX_SPAWN_PANIC* */ 459 bool p_posix_spawn_failed; /* indicates that a posix_spawn failed */ 460 bool p_disallow_map_with_linking; /* used to prevent dyld's map_with_linking() usage after startup */ 461 462 #if CONFIG_MEMORYSTATUS 463 #if CONFIG_FREEZE 464 uint8_t p_memstat_freeze_skip_reason; /* memorystaus_freeze_skipped_reason_t. Protected by the freezer mutex. */ 465 #endif /* CONFIG_FREEZE */ 466 /* Fields protected by proc list lock */ 467 uint32_t p_memstat_state; /* state. Also used as a wakeup channel when the memstat's LOCKED bit changes */ 468 int32_t p_memstat_effectivepriority; /* priority after transaction state accounted for */ 469 int32_t p_memstat_requestedpriority; /* active priority */ 470 int32_t p_memstat_assertionpriority; /* assertion driven priority */ 471 uint32_t p_memstat_dirty; /* dirty state */ 472 TAILQ_ENTRY(proc) p_memstat_list; /* priority bucket link */ 473 uint64_t p_memstat_userdata; /* user state */ 474 uint64_t p_memstat_idledeadline; /* time at which process became clean */ 475 uint64_t p_memstat_prio_start; /* abstime process transitioned into the current band */ 476 uint64_t p_memstat_idle_delta; /* abstime delta spent in idle band */ 477 int32_t p_memstat_memlimit; /* cached memory limit, toggles between active and inactive limits */ 478 int32_t p_memstat_memlimit_active; /* memory limit enforced when process is in active jetsam state */ 479 int32_t p_memstat_memlimit_inactive; /* memory limit enforced when process is in inactive jetsam state */ 480 int32_t p_memstat_relaunch_flags; /* flags indicating relaunch behavior for the process */ 481 #if CONFIG_FREEZE 482 uint32_t p_memstat_freeze_sharedanon_pages; /* shared pages left behind after freeze */ 483 uint32_t p_memstat_frozen_count; 484 uint32_t p_memstat_thaw_count; 485 uint32_t p_memstat_last_thaw_interval; /* In which freezer interval was this last thawed? */ 486 #endif /* CONFIG_FREEZE */ 487 #endif /* CONFIG_MEMORYSTATUS */ 488 489 _Atomic uint32_t p_user_faults; /* count the number of user faults generated */ 490 491 uint32_t p_memlimit_increase; /* byte increase for memory limit for dyld SPI rdar://problem/49950264, structure packing 32-bit and 64-bit */ 492 493 uint64_t p_crash_behavior_deadline; /* mach_continuous_time deadline. After this timestamp p_crash_behavior is invalid */ 494 495 uint32_t p_crash_count; /* Consecutive crash count threshold */ 496 uint32_t p_throttle_timeout; /* Exponential backoff throttle */ 497 498 struct os_reason *p_exit_reason; 499 500 #if CONFIG_PROC_UDATA_STORAGE 501 uint64_t p_user_data; /* general-purpose storage for userland-provided data */ 502 #endif /* CONFIG_PROC_UDATA_STORAGE */ 503 504 char * p_subsystem_root_path; 505 }; 506 507 #define PGRPID_DEAD 0xdeaddead 508 509 /* p_listflag */ 510 #define P_LIST_WAITING 0x00000010 511 #define P_LIST_CHILDDRSTART 0x00000080 512 #define P_LIST_CHILDDRAINED 0x00000100 513 #define P_LIST_CHILDDRWAIT 0x00000200 514 #define P_LIST_CHILDLKWAIT 0x00000400 515 #define P_LIST_DEADPARENT 0x00000800 516 #define P_LIST_PARENTREFWAIT 0x00001000 517 #define P_LIST_EXITCOUNT 0x00100000 /* counted for process exit */ 518 519 /* local flags */ 520 #define P_LDELAYTERM 0x00000001 /* */ 521 #define P_LHASTASK 0x00000002 /* process points to a task */ 522 #define P_LTERM 0x00000004 /* */ 523 #define P_LEXIT 0x00000008 /* */ 524 #define P_LPEXIT 0x00000010 525 #define P_LTRANSCOMMIT 0x00000020 /* process is committed to trans */ 526 #define P_LINTRANSIT 0x00000040 /* process in exec or in creation */ 527 #define P_LTRANSWAIT 0x00000080 /* waiting for trans to complete */ 528 #define P_LTRACED 0x00000400 /* */ 529 #define P_LSIGEXC 0x00000800 /* */ 530 #define P_LNOATTACH 0x00001000 /* */ 531 #define P_LPPWAIT 0x00002000 /* */ 532 #define P_LPTHREADJITALLOWLIST 0x00004000 /* process has pthread JIT write function allowlist */ 533 #define P_LPTHREADJITFREEZELATE 0x00008000 /* process JIT function allowlist is frozen late */ 534 #define P_LTRACE_WAIT 0x00010000 /* wait for flag to be cleared before starting ptrace */ 535 #define P_LLIMCHANGE 0x00020000 /* process is changing its plimit (rlim_cur, rlim_max) */ 536 #define P_LLIMWAIT 0x00040000 537 #define P_LWAITED 0x00080000 538 #define P_LINSIGNAL 0x00100000 539 #define P_LCUSTOM_STACK 0x00200000 /* process is using custom stack size */ 540 #define P_LRAGE_VNODES 0x00400000 541 #define P_LREGISTER 0x00800000 /* thread start fns registered */ 542 #define P_LVMRSRCOWNER 0x01000000 /* can handle the resource ownership of */ 543 #define P_LTERM_DECRYPTFAIL 0x04000000 /* process terminating due to key failure to decrypt */ 544 #define P_LTERM_JETSAM 0x08000000 /* process is being jetsam'd */ 545 546 #define P_JETSAM_VMPAGESHORTAGE 0x00000000 /* jetsam: lowest jetsam priority proc, killed due to vm page shortage */ 547 #define P_JETSAM_VMTHRASHING 0x10000000 /* jetsam: lowest jetsam priority proc, killed due to vm thrashing */ 548 #define P_JETSAM_HIWAT 0x20000000 /* jetsam: high water mark */ 549 #define P_JETSAM_PID 0x30000000 /* jetsam: pid */ 550 #define P_JETSAM_IDLEEXIT 0x40000000 /* jetsam: idle exit */ 551 #define P_JETSAM_VNODE 0x50000000 /* jetsam: vnode kill */ 552 #define P_JETSAM_FCTHRASHING 0x60000000 /* jetsam: lowest jetsam priority proc, killed due to filecache thrashing */ 553 #define P_JETSAM_MASK 0x70000000 /* jetsam type mask */ 554 #define P_LNSPACE_RESOLVER 0x80000000 /* process is the namespace resolver */ 555 556 /* Process control state for resource starvation */ 557 #define P_PCTHROTTLE 1 558 #define P_PCSUSP 2 559 #define P_PCKILL 3 560 #define P_PCMAX 3 561 562 /* Process control action state on resrouce starvation */ 563 #define PROC_ACTION_MASK 0xffff0000; 564 #define PROC_CONTROL_STATE(p) (p->p_pcaction & P_PCMAX) 565 #define PROC_ACTION_STATE(p) ((p->p_pcaction >> 16) & P_PCMAX) 566 #define PROC_SETACTION_STATE(p) (p->p_pcaction = (PROC_CONTROL_STATE(p) | (PROC_CONTROL_STATE(p) << 16))) 567 #define PROC_RESETACTION_STATE(p) (p->p_pcaction = PROC_CONTROL_STATE(p)) 568 569 /* Process exit reason macros */ 570 #define PROC_HAS_EXITREASON(p) (p->p_exit_reason != OS_REASON_NULL) 571 #define PROC_EXITREASON_FLAGS(p) p->p_exit_reason->osr_flags 572 573 /* additional process flags */ 574 #define P_LADVLOCK 0x01 575 #define P_LXBKIDLEINPROG 0x02 576 #define P_RSR 0x04 577 578 /* p_vfs_iopolicy flags */ 579 #define P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY 0x0001 580 #define P_VFS_IOPOLICY_ATIME_UPDATES 0x0002 581 #define P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES 0x0004 582 #define P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME 0x0008 583 #define P_VFS_IOPOLICY_TRIGGER_RESOLVE_DISABLE 0x0010 584 #define P_VFS_IOPOLICY_IGNORE_CONTENT_PROTECTION 0x0020 585 #define P_VFS_IOPOLICY_IGNORE_NODE_PERMISSIONS 0x0040 586 #define P_VFS_IOPOLICY_SKIP_MTIME_UPDATE 0x0080 587 #define P_VFS_IOPOLICY_ALLOW_LOW_SPACE_WRITES 0x0100 588 #define P_VFS_IOPOLICY_DISALLOW_RW_FOR_O_EVTONLY 0x0200 589 #define P_VFS_IOPOLICY_ALTLINK 0x0400 590 #define P_VFS_IOPOLICY_NOCACHE_WRITE_FS_BLKSIZE 0x0800 591 #define P_VFS_IOPOLICY_SUPPORT_LONG_PATHS 0x1000 592 #define P_VFS_IOPOLICY_ENTITLED_RESERVE_ACCESS 0x2000 593 #define P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES_ORIG 0x4000 /* preserves original at-launch policy */ 594 595 #define P_VFS_IOPOLICY_INHERITED_MASK \ 596 (P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY | \ 597 P_VFS_IOPOLICY_ATIME_UPDATES | \ 598 P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES | \ 599 P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME | \ 600 P_VFS_IOPOLICY_TRIGGER_RESOLVE_DISABLE | \ 601 P_VFS_IOPOLICY_IGNORE_CONTENT_PROTECTION | \ 602 P_VFS_IOPOLICY_IGNORE_NODE_PERMISSIONS | \ 603 P_VFS_IOPOLICY_SKIP_MTIME_UPDATE | \ 604 P_VFS_IOPOLICY_DISALLOW_RW_FOR_O_EVTONLY | \ 605 P_VFS_IOPOLICY_ALTLINK | \ 606 P_VFS_IOPOLICY_NOCACHE_WRITE_FS_BLKSIZE | \ 607 P_VFS_IOPOLICY_SUPPORT_LONG_PATHS | \ 608 P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES_ORIG) 609 610 #define P_VFS_IOPOLICY_VALID_MASK \ 611 (P_VFS_IOPOLICY_INHERITED_MASK | \ 612 P_VFS_IOPOLICY_ALLOW_LOW_SPACE_WRITES | \ 613 P_VFS_IOPOLICY_ENTITLED_RESERVE_ACCESS) 614 615 /* process creation arguments */ 616 #define PROC_CREATE_FORK 0 /* independent child (running) */ 617 #define PROC_CREATE_SPAWN 1 /* independent child (suspended) */ 618 619 /* LP64 version of extern_proc. all pointers 620 * grow when we're dealing with a 64-bit process. 621 * WARNING - keep in sync with extern_proc 622 * but use native alignment of 64-bit process. 623 */ 624 625 #ifdef KERNEL 626 #include <sys/time.h> /* user_timeval, user_itimerval */ 627 628 /* 629 * This packing is required to ensure symmetry between userspace and kernelspace 630 * when the kernel is 64-bit and the user application is 32-bit. All currently 631 * supported ARM slices (arm64/armv7k/arm64_32) contain the same struct 632 * alignment ABI so this packing isn't needed for ARM. 633 */ 634 #if defined(__x86_64__) 635 #pragma pack(4) 636 #endif 637 struct user32_extern_proc { 638 union { 639 struct { 640 uint32_t __p_forw; /* Doubly-linked run/sleep queue. */ 641 uint32_t __p_back; 642 } p_st1; 643 struct user32_timeval __p_starttime; /* process start time */ 644 } p_un; 645 uint32_t p_vmspace; /* Address space. */ 646 uint32_t p_sigacts; /* Signal actions, state (PROC ONLY). */ 647 int p_flag; /* P_* flags. */ 648 char p_stat; /* S* process status. */ 649 pid_t p_pid; /* Process identifier. */ 650 pid_t p_oppid; /* Save parent pid during ptrace. XXX */ 651 int p_dupfd; /* Sideways return value from fdopen. XXX */ 652 /* Mach related */ 653 uint32_t user_stack; /* where user stack was allocated */ 654 uint32_t exit_thread; /* XXX Which thread is exiting? */ 655 int p_debugger; /* allow to debug */ 656 boolean_t sigwait; /* indication to suspend */ 657 /* scheduling */ 658 u_int p_estcpu; /* Time averaged value of p_cpticks. */ 659 int p_cpticks; /* Ticks of cpu time. */ 660 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */ 661 uint32_t p_wchan; /* Sleep address. */ 662 uint32_t p_wmesg; /* Reason for sleep. */ 663 u_int p_swtime; /* Time swapped in or out. */ 664 u_int p_slptime; /* Time since last blocked. */ 665 struct user32_itimerval p_realtimer; /* Alarm timer. */ 666 struct user32_timeval p_rtime; /* Real time. */ 667 u_quad_t p_uticks; /* Statclock hits in user mode. */ 668 u_quad_t p_sticks; /* Statclock hits in system mode. */ 669 u_quad_t p_iticks; /* Statclock hits processing intr. */ 670 int p_traceflag; /* Kernel trace points. */ 671 uint32_t p_tracep; /* Trace to vnode. */ 672 int p_siglist; /* DEPRECATED */ 673 uint32_t p_textvp; /* Vnode of executable. */ 674 int p_holdcnt; /* If non-zero, don't swap. */ 675 sigset_t p_sigmask; /* DEPRECATED. */ 676 sigset_t p_sigignore; /* Signals being ignored. */ 677 sigset_t p_sigcatch; /* Signals being caught by user. */ 678 u_char p_priority; /* Process priority. */ 679 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */ 680 char p_nice; /* Process "nice" value. */ 681 char p_comm[MAXCOMLEN + 1]; 682 uint32_t p_pgrp; /* Pointer to process group. */ 683 uint32_t p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */ 684 u_short p_xstat; /* Exit status for wait; also stop signal. */ 685 u_short p_acflag; /* Accounting flags. */ 686 uint32_t p_ru; /* Exit information. XXX */ 687 }; 688 #pragma pack() 689 struct user64_extern_proc { 690 union { 691 struct { 692 user_addr_t __p_forw; /* Doubly-linked run/sleep queue. */ 693 user_addr_t __p_back; 694 } p_st1; 695 struct user64_timeval __p_starttime; /* process start time */ 696 } p_un; 697 user_addr_t p_vmspace; /* Address space. */ 698 user_addr_t p_sigacts; /* Signal actions, state (PROC ONLY). */ 699 int p_flag; /* P_* flags. */ 700 char p_stat; /* S* process status. */ 701 pid_t p_pid; /* Process identifier. */ 702 pid_t p_oppid; /* Save parent pid during ptrace. XXX */ 703 int p_dupfd; /* Sideways return value from fdopen. XXX */ 704 /* Mach related */ 705 user_addr_t user_stack __attribute((aligned(8))); /* where user stack was allocated */ 706 user_addr_t exit_thread; /* XXX Which thread is exiting? */ 707 int p_debugger; /* allow to debug */ 708 boolean_t sigwait; /* indication to suspend */ 709 /* scheduling */ 710 u_int p_estcpu; /* Time averaged value of p_cpticks. */ 711 int p_cpticks; /* Ticks of cpu time. */ 712 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */ 713 user_addr_t p_wchan __attribute((aligned(8))); /* Sleep address. */ 714 user_addr_t p_wmesg; /* Reason for sleep. */ 715 u_int p_swtime; /* Time swapped in or out. */ 716 u_int p_slptime; /* Time since last blocked. */ 717 struct user64_itimerval p_realtimer; /* Alarm timer. */ 718 struct user64_timeval p_rtime; /* Real time. */ 719 u_quad_t p_uticks; /* Statclock hits in user mode. */ 720 u_quad_t p_sticks; /* Statclock hits in system mode. */ 721 u_quad_t p_iticks; /* Statclock hits processing intr. */ 722 int p_traceflag; /* Kernel trace points. */ 723 user_addr_t p_tracep __attribute((aligned(8))); /* Trace to vnode. */ 724 int p_siglist; /* DEPRECATED */ 725 user_addr_t p_textvp __attribute((aligned(8))); /* Vnode of executable. */ 726 int p_holdcnt; /* If non-zero, don't swap. */ 727 sigset_t p_sigmask; /* DEPRECATED. */ 728 sigset_t p_sigignore; /* Signals being ignored. */ 729 sigset_t p_sigcatch; /* Signals being caught by user. */ 730 u_char p_priority; /* Process priority. */ 731 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */ 732 char p_nice; /* Process "nice" value. */ 733 char p_comm[MAXCOMLEN + 1]; 734 user_addr_t p_pgrp __attribute((aligned(8))); /* Pointer to process group. */ 735 user_addr_t p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */ 736 u_short p_xstat; /* Exit status for wait; also stop signal. */ 737 u_short p_acflag; /* Accounting flags. */ 738 user_addr_t p_ru __attribute((aligned(8))); /* Exit information. XXX */ 739 }; 740 #endif /* KERNEL */ 741 742 __exported_push_hidden 743 744 extern struct vfs_context vfs_context0; 745 746 /* 747 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t, 748 * as it is used to represent "no process group". 749 */ 750 extern int nprocs, maxproc; /* Current and max number of procs. */ 751 extern int maxprocperuid; /* Current number of procs per uid */ 752 extern int hard_maxproc; /* hard limit */ 753 extern unsigned int proc_shutdown_exitcount; 754 755 #define PID_MAX 99999 756 #define NO_PID 100000 757 extern lck_mtx_t proc_list_mlock; 758 759 #ifdef XNU_KERNEL_PRIVATE 760 /* 761 * Identify a process uniquely. 762 * proc_ident's fields match 1-1 with those in struct proc. 763 */ 764 #define PROC_IDENT_PID_BIT_COUNT 28 765 struct proc_ident { 766 uint64_t p_uniqueid; 767 pid_t 768 may_exit : 1, 769 may_exec : 1, 770 reserved : 2, 771 p_pid : PROC_IDENT_PID_BIT_COUNT; 772 int p_idversion; 773 }; 774 _Static_assert(sizeof(pid_t) == 4, "proc_ident assumes a 32-bit pid_t"); 775 _Static_assert(PID_MAX < (1 << PROC_IDENT_PID_BIT_COUNT), "proc_ident assumes PID_MAX requires less than 28bits"); 776 _Static_assert(NO_PID < (1 << PROC_IDENT_PID_BIT_COUNT), "proc_ident assumes NO_PID requires less than 28bits"); 777 #endif 778 779 #define BSD_SIMUL_EXECS 33 /* 32 , allow for rounding */ 780 #define BSD_PAGEABLE_SIZE_PER_EXEC (NCARGS + PAGE_SIZE + PAGE_SIZE) /* page for apple vars, page for executable header */ 781 extern int execargs_cache_size; 782 extern int execargs_free_count; 783 extern vm_offset_t * execargs_cache; 784 785 #define SESS_LEADER(p, sessp) ((sessp)->s_leader == (p)) 786 787 #define SESSHASH(sessid) (&sesshashtbl[(sessid) & sesshash]) 788 extern LIST_HEAD(sesshashhead, session) * sesshashtbl; 789 extern u_long sesshash; 790 791 extern lck_attr_t proc_lck_attr; 792 extern lck_grp_t proc_fdmlock_grp; 793 extern lck_grp_t proc_lck_grp; 794 extern lck_grp_t proc_kqhashlock_grp; 795 extern lck_grp_t proc_knhashlock_grp; 796 extern lck_grp_t proc_slock_grp; 797 extern lck_grp_t proc_mlock_grp; 798 extern lck_grp_t proc_ucred_mlock_grp; 799 extern lck_grp_t proc_dirslock_grp; 800 801 LIST_HEAD(proclist, proc); 802 extern struct proclist allproc; /* List of all processes. */ 803 extern struct proclist zombproc; /* List of zombie processes. */ 804 805 #if CONFIG_COREDUMP || CONFIG_UCOREDUMP 806 extern const char * defaultcorefiledir; 807 extern const char * defaultdrivercorefiledir; 808 extern char corefilename[MAXPATHLEN + 1]; 809 extern char drivercorefilename[MAXPATHLEN + 1]; 810 extern int do_coredump; 811 extern int sugid_coredump; 812 #if CONFIG_UCOREDUMP 813 extern int do_ucoredump; 814 #endif /* CONFIG_UCOREDUMP */ 815 #endif /* CONFIG_COREDUMP || CONFIG_UCOREDUMP */ 816 817 __options_decl(cloneproc_flags_t, uint32_t, { 818 CLONEPROC_SPAWN = 0, 819 CLONEPROC_FORK = 0x0001, 820 CLONEPROC_INITPROC = 0x0002, 821 CLONEPROC_EXEC = 0x0004, 822 }); 823 824 extern thread_t cloneproc(task_t, coalition_t *, proc_t, cloneproc_flags_t); 825 extern struct proc * XNU_PTRAUTH_SIGNED_PTR("initproc") initproc; 826 extern void proc_lock(struct proc *); 827 extern void proc_unlock(struct proc *); 828 extern void proc_spinlock(struct proc *); 829 extern void proc_spinunlock(struct proc *); 830 extern void proc_list_lock(void); 831 extern void proc_list_unlock(void); 832 extern void proc_list_lock_held(void); 833 extern void proc_klist_lock(void); 834 extern void proc_klist_unlock(void); 835 extern void proc_fdlock(struct proc *); 836 extern void proc_fdlock_spin(struct proc *); 837 extern void proc_fdunlock(struct proc *); 838 extern void proc_fdlock_assert(proc_t p, int assertflags); 839 extern void proc_dirs_lock_shared(struct proc *); 840 extern void proc_dirs_unlock_shared(struct proc *); 841 extern void proc_dirs_lock_exclusive(struct proc *); 842 extern void proc_dirs_unlock_exclusive(struct proc *); 843 extern void proc_ucred_lock(struct proc *); 844 extern void proc_ucred_unlock(struct proc *); 845 extern void proc_update_creds_onproc(struct proc *, kauth_cred_t cred); 846 extern kauth_cred_t proc_ucred_locked(proc_t p); 847 extern kauth_cred_t proc_ucred_smr(proc_t p); 848 extern kauth_cred_t proc_ucred_unsafe(proc_t p) __exported; 849 #if CONFIG_COREDUMP || CONFIG_UCOREDUMP 850 __private_extern__ int proc_core_name(const char *format, const char *name, 851 uid_t uid, pid_t pid, char *cr_name, size_t cr_name_len); 852 #endif 853 /* proc_best_name_for_pid finds a process with a given pid and copies its best name of 854 * the executable (32-byte name if it exists, otherwise the 16-byte name) to 855 * the passed in buffer. The size of the buffer is to be passed in as well. 856 */ 857 extern void proc_best_name_for_pid(int pid, char * buf, int size); 858 extern int isinferior(struct proc *, struct proc *); 859 __private_extern__ bool pzfind(pid_t); /* Check zombie by pid. */ 860 __private_extern__ bool pzfind_unique(pid_t, uint64_t); /* Check zombie by uniqueid. */ 861 __private_extern__ struct proc *proc_find_zombref(pid_t); /* Find zombie by id. */ 862 __private_extern__ struct proc *proc_find_zombref_locked(pid_t); /* Find zombie by id. */ 863 __private_extern__ void proc_drop_zombref(struct proc * p); /* Drop zombie ref. */ 864 865 /* 866 * This function is used to inc/dec proc count per user. 867 * User of the function must obey the contract: 868 * - cannot have spurious calls to this function (e.g. sending -1 decrement to proc without procs on it) 869 * - for every positive diff - there will be sent negative diff when the proc will die (to avoid uid leaks) 870 * - this function uses `proc_list_*lock` functions for synchronizations 871 */ 872 extern size_t chgproccnt(uid_t uid, int diff); 873 874 /* This function is to provide a way to resolve conflict on caller side, instead of 875 * relying on @f chgproccnt doing that. 876 * 877 * `proc_list_lock` is expected to be locked when calling this function. 878 * 879 * This function (same as @f chgproccnt) cannot be called spuriously, callers 880 * are expected to have precise control when this can be called spuriously (e.g. due 881 * to race conditions and missing synchronizations ) and resolve conflicts before calling 882 * this function. 883 * 884 * Main constraints for this function are: 885 * 1) if newuip is provided => `diff > 0` 886 * 2) diff is never == 0 887 * 3) this function return a value to be kfree'd 888 * 4) caller guarantees that element by uid exists or newuip is passed 889 * IMPORTANT INVARIANT: 890 * - this function may return only one value to be freed: 891 * - in one case it may be the `newuip` that we didn't need due to some other 892 * thread added element concurrently 893 * - the only other case is when we remove element from list due to ui_proccnt == 0 894 * this case is possible only with diff < 0, hence `newuip` couldn't be provided 895 * 896 * @p uid is the uid number to change proc count off 897 * @p diff is the delta value to apply 898 * @p newuip - opportunistically allocated element 899 * @p[out] out - value of proc count for given uid is returned via this argument 900 * 901 * Returns value to dellocate (if any) 902 */ 903 extern struct uidinfo *chgproccnt_locked(uid_t uid, int diff, struct uidinfo *newuip, size_t *out); 904 extern void pinsertchild(struct proc *parent, struct proc *child, bool in_exec); 905 extern void p_reparentallchildren(proc_t old_proc, proc_t new_proc); 906 extern int setsid_internal(struct proc *p); 907 #ifndef __cplusplus 908 extern void setlogin_internal(proc_t p, const char login[static MAXLOGNAME]); 909 #endif // __cplusplus 910 extern int setgroups_internal(proc_t p, u_int gidsetsize, gid_t *gidset, uid_t gmuid); 911 extern int enterpgrp(struct proc *p, pid_t pgid, int mksess); 912 extern void fixjobc(struct proc *p, struct pgrp *pgrp, int entering); 913 extern int inferior(struct proc *p); 914 extern void resetpriority(struct proc *); 915 extern void setrunnable(struct proc *); 916 extern void setrunqueue(struct proc *); 917 extern int sleep(void *chan, int pri) __exported; 918 extern int tsleep0(void *chan, int pri, const char *wmesg, int timo, int (*continuation)(int)); 919 extern int tsleep1(void *chan, int pri, const char *wmesg, u_int64_t abstime, int (*continuation)(int)); 920 extern int exit1(struct proc *, int, int *); 921 extern int exit1_internal(struct proc *, int, int *, boolean_t, boolean_t, int); 922 extern int exit_with_reason(struct proc *, int, int *, boolean_t, boolean_t, int, struct os_reason *); 923 extern int fork1(proc_t, thread_t *, int, coalition_t *); 924 extern void proc_reparentlocked(struct proc *child, struct proc * newparent, int cansignal, int locked); 925 926 extern bool proc_list_exited(proc_t p); 927 extern proc_t proc_find_locked(int pid); 928 extern proc_t proc_find_noref_smr(int pid); 929 extern bool proc_is_shadow(proc_t p); 930 extern proc_t proc_findthread(thread_t thread); 931 extern void proc_refdrain(proc_t); 932 extern proc_t proc_refdrain_will_exec(proc_t p); 933 extern void proc_refwake_did_exec(proc_t p); 934 extern void proc_childdrainlocked(proc_t); 935 extern void proc_childdrainstart(proc_t); 936 extern void proc_childdrainend(proc_t); 937 extern void proc_checkdeadrefs(proc_t); 938 struct proc *phash_find_locked(pid_t); 939 extern void phash_insert_locked(struct proc *); 940 extern void phash_remove_locked(struct proc *); 941 extern void phash_replace_locked(struct proc *old_proc, struct proc *new_proc); 942 extern bool pghash_exists_locked(pid_t); 943 extern void pghash_insert_locked(struct pgrp *); 944 extern struct pgrp *pgrp_find(pid_t); 945 extern void pgrp_rele(struct pgrp * pgrp); 946 extern struct session * session_find_internal(pid_t sessid); 947 extern struct pgrp *proc_pgrp(proc_t, struct session **); 948 extern struct pgrp *pgrp_leave_locked(struct proc *p); 949 extern struct pgrp *pgrp_enter_locked(struct proc *parent, struct proc *p); 950 extern struct pgrp *tty_pgrp_locked(struct tty * tp); 951 struct pgrp *pgrp_alloc(pid_t pgid, pggrp_ref_bits_t bits); 952 extern void pgrp_lock(struct pgrp * pgrp); 953 extern void pgrp_unlock(struct pgrp * pgrp); 954 extern struct session *session_find_locked(pid_t sessid); 955 extern void session_replace_leader(struct proc *old_proc, struct proc *new_proc); 956 extern struct session *session_alloc(struct proc *leader); 957 extern void session_lock(struct session * sess); 958 extern void session_unlock(struct session * sess); 959 extern struct session *session_ref(struct session *sess); 960 extern void session_rele(struct session *sess); 961 extern struct tty *session_set_tty_locked(struct session *sessp, struct tty *); 962 extern struct tty *session_clear_tty_locked(struct session *sess); 963 extern struct tty *session_tty(struct session *sess); 964 extern proc_t proc_parentholdref(proc_t); 965 extern int proc_parentdropref(proc_t, int); 966 int itimerfix(struct timeval *tv); 967 int itimerdecr(struct proc * p, struct itimerval *itp, int usec); 968 void proc_free_realitimer(proc_t proc); 969 void proc_inherit_itimers(struct proc *old_proc, struct proc *new_proc); 970 int timespec_is_valid(const struct timespec *); 971 void proc_signalstart(struct proc *, int locked); 972 void proc_signalend(struct proc *, int locked); 973 int proc_transstart(struct proc *, int locked, int non_blocking); 974 void proc_transcommit(struct proc *, int locked); 975 void proc_transend(struct proc *, int locked); 976 int proc_transwait(struct proc *, int locked); 977 struct proc *proc_ref(struct proc *p, int locked); 978 void proc_wait_release(struct proc *p); 979 void proc_knote(struct proc * p, long hint); 980 void proc_transfer_knotes(struct proc *old_proc, struct proc *new_proc); 981 void proc_knote_drain(struct proc *p); 982 void proc_setregister(proc_t p); 983 void proc_resetregister(proc_t p); 984 bool proc_get_pthread_jit_allowlist(proc_t p, bool *late_out); 985 void proc_set_pthread_jit_allowlist(proc_t p, bool late); 986 /* returns the first thread_t in the process, or NULL XXX for NFS, DO NOT USE */ 987 thread_t proc_thread(proc_t); 988 extern int proc_pendingsignals(proc_t, sigset_t); 989 int proc_getpcontrol(int pid, int * pcontrolp); 990 int proc_resetpcontrol(int pid); 991 #if PSYNCH 992 void pth_proc_hashinit(proc_t); 993 void pth_proc_hashdelete(proc_t); 994 void pth_global_hashinit(void); 995 extern thread_call_t psynch_thcall; 996 void psynch_wq_cleanup(__unused void * param, __unused void * param1); 997 extern lck_mtx_t * pthread_list_mlock; 998 #endif /* PSYNCH */ 999 struct uthread *current_uthread(void) __pure2; 1000 1001 extern void proc_set_task(proc_t, task_t); 1002 extern task_t proc_get_task_raw(proc_t proc); 1003 extern proc_t task_get_proc_raw(task_t task); 1004 extern void proc_ref_hold_proc_task_struct(proc_t proc); 1005 extern void proc_release_proc_task_struct(proc_t proc); 1006 extern void task_ref_hold_proc_task_struct(task_t task); 1007 extern void task_release_proc_task_struct(task_t task, proc_ro_t proc_ro); 1008 extern void proc_setpidversion(proc_t, int); 1009 extern uint64_t proc_getcsflags(proc_t); 1010 extern void proc_csflags_update(proc_t, uint64_t); 1011 extern void proc_csflags_set(proc_t, uint64_t); 1012 extern void proc_csflags_clear(proc_t, uint64_t); 1013 extern uint8_t *proc_syscall_filter_mask(proc_t); 1014 extern void proc_syscall_filter_mask_set(proc_t, uint8_t *); 1015 extern pid_t proc_getpid(proc_t); 1016 extern void proc_setplatformdata(proc_t, uint32_t, uint32_t, uint32_t); 1017 extern void proc_set_sigact(proc_t, int, user_addr_t); 1018 extern void proc_set_trampact(proc_t, int, user_addr_t); 1019 extern void proc_set_sigact_trampact(proc_t, int, user_addr_t, user_addr_t); 1020 extern void proc_reset_sigact(proc_t, sigset_t); 1021 extern void proc_setexecutableuuid(proc_t, const uuid_t); 1022 extern const unsigned char *__counted_by(sizeof(uuid_t)) proc_executableuuid_addr(proc_t); 1023 extern void proc_getresponsibleuuid(proc_t target_proc, unsigned char *__counted_by(size)responsible_uuid, unsigned long size); 1024 extern void proc_setresponsibleuuid(proc_t target_proc, unsigned char *__counted_by(size)responsible_uuid, unsigned long size); 1025 1026 #pragma mark - process iteration 1027 1028 /* 1029 * ALLPROC_FOREACH cannot be used to access the task, as the field may be 1030 * swapped out during exec. With `proc_iterate`, find threads by iterating the 1031 * `p_uthlist` field of the proc, under the `proc_lock`. 1032 */ 1033 1034 #define ALLPROC_FOREACH(var) \ 1035 LIST_FOREACH((var), &allproc, p_list) 1036 1037 #define ZOMBPROC_FOREACH(var) \ 1038 LIST_FOREACH((var), &zombproc, p_list) 1039 1040 #define PGMEMBERS_FOREACH(group, var) \ 1041 LIST_FOREACH((var), &((struct pgrp *)(group))->pg_members, p_pglist) 1042 1043 #define PCHILDREN_FOREACH(parent, var) \ 1044 LIST_FOREACH((var), &(((struct proc *)(parent))->p_children), p_sibling) 1045 1046 typedef int (*proc_iterate_fn_t)(proc_t, void *); 1047 1048 /* 1049 * These are the only valid return values of `callout` functions provided to 1050 * process iterators. 1051 * 1052 * CLAIMED returns expect the caller to call proc_rele on the proc. DONE 1053 * returns stop iterating processes early. 1054 */ 1055 #define PROC_RETURNED (0) 1056 #define PROC_RETURNED_DONE (1) 1057 #define PROC_CLAIMED (2) 1058 #define PROC_CLAIMED_DONE (3) 1059 1060 /* 1061 * pgrp_iterate walks the provided process group, calling `filterfn` with 1062 * `filterarg` for each process. For processes where `filterfn` returned 1063 * non-zero, `callout` is called with `arg`. 1064 * 1065 * `PGMEMBERS_FOREACH` might also be used under the pgrp_lock to achieve a 1066 * similar effect. 1067 */ 1068 1069 extern void pgrp_iterate(struct pgrp *pgrp, proc_iterate_fn_t callout, 1070 void *arg, bool (^filterfn)(proc_t)); 1071 1072 /* 1073 * proc_iterate walks the `allproc` and/or `zombproc` lists, calling `filterfn` 1074 * with `filterarg` for each process. For processes where `filterfn` returned 1075 * non-zero, `callout` is called with `arg`. If the `PROC_NOWAITTRANS` flag is 1076 * unset, this function waits for transitions. 1077 * 1078 * `ALLPROC_FOREACH` or `ZOMBPROC_FOREACH` might also be used under the 1079 * `proc_list_lock` to achieve a similar effect. 1080 */ 1081 #define PROC_ALLPROCLIST (1U << 0) /* walk the allproc list (processes not yet exited) */ 1082 #define PROC_ZOMBPROCLIST (1U << 1) /* walk the zombie list */ 1083 #define PROC_NOWAITTRANS (1U << 2) /* do not wait for transitions (checkdirs only) */ 1084 1085 extern void proc_iterate(unsigned int flags, proc_iterate_fn_t callout, 1086 void *arg, proc_iterate_fn_t filterfn, void *filterarg); 1087 1088 /* 1089 * proc_childrenwalk walks the children of process `p`, calling `callout` for 1090 * each one. 1091 * 1092 * `PCHILDREN_FOREACH` might also be used under the `proc_list_lock` to achieve 1093 * a similar effect. 1094 */ 1095 extern void proc_childrenwalk(proc_t p, proc_iterate_fn_t callout, void *arg); 1096 1097 /* 1098 * proc_rebootscan should only be used by kern_shutdown.c 1099 */ 1100 extern void proc_rebootscan(proc_iterate_fn_t callout, void *arg, 1101 proc_iterate_fn_t filterfn, void *filterarg); 1102 1103 /* 1104 * Construct a proc_ident from a proc_t 1105 */ 1106 extern struct proc_ident proc_ident_with_policy(proc_t p, proc_ident_validation_policy_t policy); 1107 1108 /* 1109 * Validate that a particular policy bit is set 1110 */ 1111 extern bool proc_ident_has_policy(const proc_ident_t ident, enum proc_ident_validation_policy policy); 1112 1113 pid_t dtrace_proc_selfpid(void); 1114 pid_t dtrace_proc_selfppid(void); 1115 uid_t dtrace_proc_selfruid(void); 1116 1117 os_refgrp_decl_extern(p_refgrp); 1118 KALLOC_TYPE_DECLARE(proc_stats_zone); 1119 ZONE_DECLARE_ID(ZONE_ID_PROC_TASK, struct proc); 1120 extern zone_t proc_task_zone; 1121 1122 #if CONFIG_PROC_RESOURCE_LIMITS 1123 int proc_set_filedesc_limits(proc_t p, int soft_limit, int hard_limit); 1124 int proc_set_kqworkloop_limits(proc_t p, int soft_limit, int hard_limit); 1125 #endif /* CONFIG_PROC_RESOURCE_LIMITS */ 1126 1127 /* 1128 * True if the process ignores file permissions in case it owns the 1129 * file/directory 1130 */ 1131 bool proc_ignores_node_permissions(proc_t proc); 1132 1133 /* 1134 * @func no_paging_space_action 1135 * 1136 * @brief React to compressor/swap exhaustion 1137 * 1138 * @returns true if the low-swap note should be sent 1139 */ 1140 extern bool no_paging_space_action(uint32_t cause); 1141 1142 __exported_pop 1143 #endif /* !_SYS_PROC_INTERNAL_H_ */ 1144