]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_exit.c
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / bsd / kern / kern_exit.c
1 /*
2 * Copyright (c) 2000-2016 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) 1982, 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 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
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 #include <machine/reg.h>
76 #include <machine/psl.h>
77 #include <stdatomic.h>
78
79 #include "compat_43.h"
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/ioctl.h>
84 #include <sys/proc_internal.h>
85 #include <sys/proc.h>
86 #include <sys/kauth.h>
87 #include <sys/tty.h>
88 #include <sys/time.h>
89 #include <sys/resource.h>
90 #include <sys/kernel.h>
91 #include <sys/wait.h>
92 #include <sys/file_internal.h>
93 #include <sys/vnode_internal.h>
94 #include <sys/syslog.h>
95 #include <sys/malloc.h>
96 #include <sys/resourcevar.h>
97 #include <sys/ptrace.h>
98 #include <sys/proc_info.h>
99 #include <sys/reason.h>
100 #include <sys/_types/_timeval64.h>
101 #include <sys/user.h>
102 #include <sys/aio_kern.h>
103 #include <sys/sysproto.h>
104 #include <sys/signalvar.h>
105 #include <sys/kdebug.h>
106 #include <sys/filedesc.h> /* fdfree */
107 #include <sys/acct.h> /* acct_process */
108 #include <sys/codesign.h>
109 #include <sys/event.h> /* kevent_proc_copy_uptrs */
110 #include <sys/sdt.h>
111
112 #include <security/audit/audit.h>
113 #include <bsm/audit_kevents.h>
114
115 #include <mach/mach_types.h>
116 #include <mach/task.h>
117 #include <mach/thread_act.h>
118
119 #include <kern/exc_resource.h>
120 #include <kern/kern_types.h>
121 #include <kern/kalloc.h>
122 #include <kern/task.h>
123 #include <corpses/task_corpse.h>
124 #include <kern/thread.h>
125 #include <kern/thread_call.h>
126 #include <kern/sched_prim.h>
127 #include <kern/assert.h>
128 #include <kern/policy_internal.h>
129 #include <kern/exc_guard.h>
130
131 #include <vm/vm_protos.h>
132 #include <os/log.h>
133
134 #include <pexpert/pexpert.h>
135
136 #if SYSV_SHM
137 #include <sys/shm_internal.h> /* shmexit */
138 #endif /* SYSV_SHM */
139 #if CONFIG_PERSONAS
140 #include <sys/persona.h>
141 #endif /* CONFIG_PERSONAS */
142 #if CONFIG_MEMORYSTATUS
143 #include <sys/kern_memorystatus.h>
144 #endif /* CONFIG_MEMORYSTATUS */
145 #if CONFIG_DTRACE
146 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
147 void dtrace_proc_exit(proc_t p);
148 #include <sys/dtrace_ptss.h>
149 #endif /* CONFIG_DTRACE */
150 #if CONFIG_MACF
151 #include <security/mac_framework.h>
152 #include <security/mac_mach_internal.h>
153 #include <sys/syscall.h>
154 #endif /* CONFIG_MACF */
155
156 #if CONFIG_MEMORYSTATUS
157 static void proc_memorystatus_remove(proc_t p);
158 #endif /* CONFIG_MEMORYSTATUS */
159 void proc_prepareexit(proc_t p, int rv, boolean_t perf_notify);
160 void gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
161 mach_exception_data_type_t code, mach_exception_data_type_t subcode,
162 uint64_t *udata_buffer, int num_udata, void *reason);
163 mach_exception_data_type_t proc_encode_exit_exception_code(proc_t p);
164 void vfork_exit(proc_t p, int rv);
165 __private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
166 __private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
167 static int reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock);
168 static void populate_corpse_crashinfo(proc_t p, task_t corpse_task,
169 struct rusage_superset *rup, mach_exception_data_type_t code,
170 mach_exception_data_type_t subcode, uint64_t *udata_buffer,
171 int num_udata, os_reason_t reason);
172 static void proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode);
173 extern int proc_pidpathinfo_internal(proc_t p, uint64_t arg, char *buffer, uint32_t buffersize, int32_t *retval);
174 static __attribute__((noinline)) void launchd_crashed_panic(proc_t p, int rv);
175 extern void proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo);
176 extern void task_coalition_ids(task_t task, uint64_t ids[COALITION_NUM_TYPES]);
177 extern uint64_t get_task_phys_footprint_limit(task_t);
178 int proc_list_uptrs(void *p, uint64_t *udata_buffer, int size);
179 extern uint64_t task_corpse_get_crashed_thread_id(task_t corpse_task);
180
181 ZONE_DECLARE(zombie_zone, "zombie",
182 sizeof(struct rusage_superset), ZC_NOENCRYPT);
183
184
185 /*
186 * Things which should have prototypes in headers, but don't
187 */
188 void proc_exit(proc_t p);
189 int wait1continue(int result);
190 int waitidcontinue(int result);
191 kern_return_t sys_perf_notify(thread_t thread, int pid);
192 kern_return_t task_exception_notify(exception_type_t exception,
193 mach_exception_data_type_t code, mach_exception_data_type_t subcode);
194 kern_return_t task_violated_guard(mach_exception_code_t, mach_exception_subcode_t, void *);
195 void delay(int);
196 void gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor);
197
198 #if __has_feature(ptrauth_calls)
199 int exit_with_pac_exception(proc_t p, exception_type_t exception, mach_exception_code_t code,
200 mach_exception_subcode_t subcode);
201 #endif /* __has_feature(ptrauth_calls) */
202
203
204 /*
205 * NOTE: Source and target may *NOT* overlap!
206 * XXX Should share code with bsd/dev/ppc/unix_signal.c
207 */
208 void
209 siginfo_user_to_user32(user_siginfo_t *in, user32_siginfo_t *out)
210 {
211 out->si_signo = in->si_signo;
212 out->si_errno = in->si_errno;
213 out->si_code = in->si_code;
214 out->si_pid = in->si_pid;
215 out->si_uid = in->si_uid;
216 out->si_status = in->si_status;
217 out->si_addr = CAST_DOWN_EXPLICIT(user32_addr_t, in->si_addr);
218 /* following cast works for sival_int because of padding */
219 out->si_value.sival_ptr = CAST_DOWN_EXPLICIT(user32_addr_t, in->si_value.sival_ptr);
220 out->si_band = (user32_long_t)in->si_band; /* range reduction */
221 }
222
223 void
224 siginfo_user_to_user64(user_siginfo_t *in, user64_siginfo_t *out)
225 {
226 out->si_signo = in->si_signo;
227 out->si_errno = in->si_errno;
228 out->si_code = in->si_code;
229 out->si_pid = in->si_pid;
230 out->si_uid = in->si_uid;
231 out->si_status = in->si_status;
232 out->si_addr = in->si_addr;
233 /* following cast works for sival_int because of padding */
234 out->si_value.sival_ptr = in->si_value.sival_ptr;
235 out->si_band = in->si_band; /* range reduction */
236 }
237
238 static int
239 copyoutsiginfo(user_siginfo_t *native, boolean_t is64, user_addr_t uaddr)
240 {
241 if (is64) {
242 user64_siginfo_t sinfo64;
243
244 bzero(&sinfo64, sizeof(sinfo64));
245 siginfo_user_to_user64(native, &sinfo64);
246 return copyout(&sinfo64, uaddr, sizeof(sinfo64));
247 } else {
248 user32_siginfo_t sinfo32;
249
250 bzero(&sinfo32, sizeof(sinfo32));
251 siginfo_user_to_user32(native, &sinfo32);
252 return copyout(&sinfo32, uaddr, sizeof(sinfo32));
253 }
254 }
255
256 void
257 gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
258 mach_exception_data_type_t code, mach_exception_data_type_t subcode,
259 uint64_t *udata_buffer, int num_udata, void *reason)
260 {
261 struct rusage_superset rup;
262
263 gather_rusage_info(p, &rup.ri, RUSAGE_INFO_CURRENT);
264 rup.ri.ri_phys_footprint = 0;
265 populate_corpse_crashinfo(p, corpse_task, &rup, code, subcode,
266 udata_buffer, num_udata, reason);
267 }
268
269 static void
270 proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode)
271 {
272 mach_exception_data_type_t code_update = *code;
273 mach_exception_data_type_t subcode_update = *subcode;
274 if (p->p_exit_reason == OS_REASON_NULL) {
275 return;
276 }
277
278 switch (p->p_exit_reason->osr_namespace) {
279 case OS_REASON_JETSAM:
280 if (p->p_exit_reason->osr_code == JETSAM_REASON_MEMORY_PERPROCESSLIMIT) {
281 /* Update the code with EXC_RESOURCE code for high memory watermark */
282 EXC_RESOURCE_ENCODE_TYPE(code_update, RESOURCE_TYPE_MEMORY);
283 EXC_RESOURCE_ENCODE_FLAVOR(code_update, FLAVOR_HIGH_WATERMARK);
284 EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update, ((get_task_phys_footprint_limit(p->task)) >> 20));
285 subcode_update = 0;
286 break;
287 }
288
289 break;
290 default:
291 break;
292 }
293
294 *code = code_update;
295 *subcode = subcode_update;
296 return;
297 }
298
299 mach_exception_data_type_t
300 proc_encode_exit_exception_code(proc_t p)
301 {
302 uint64_t subcode = 0;
303
304 if (p->p_exit_reason == OS_REASON_NULL) {
305 return 0;
306 }
307
308 /* Embed first 32 bits of osr_namespace and osr_code in exception code */
309 ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_namespace);
310 ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_code);
311 return (mach_exception_data_type_t)subcode;
312 }
313
314 static void
315 populate_corpse_crashinfo(proc_t p, task_t corpse_task, struct rusage_superset *rup,
316 mach_exception_data_type_t code, mach_exception_data_type_t subcode,
317 uint64_t *udata_buffer, int num_udata, os_reason_t reason)
318 {
319 mach_vm_address_t uaddr = 0;
320 mach_exception_data_type_t exc_codes[EXCEPTION_CODE_MAX];
321 exc_codes[0] = code;
322 exc_codes[1] = subcode;
323 cpu_type_t cputype;
324 struct proc_uniqidentifierinfo p_uniqidinfo;
325 struct proc_workqueueinfo pwqinfo;
326 int retval = 0;
327 uint64_t crashed_threadid = task_corpse_get_crashed_thread_id(corpse_task);
328 unsigned int pflags = 0;
329 uint64_t max_footprint_mb;
330 uint64_t max_footprint;
331
332 uint64_t ledger_internal;
333 uint64_t ledger_internal_compressed;
334 uint64_t ledger_iokit_mapped;
335 uint64_t ledger_alternate_accounting;
336 uint64_t ledger_alternate_accounting_compressed;
337 uint64_t ledger_purgeable_nonvolatile;
338 uint64_t ledger_purgeable_nonvolatile_compressed;
339 uint64_t ledger_page_table;
340 uint64_t ledger_phys_footprint;
341 uint64_t ledger_phys_footprint_lifetime_max;
342 uint64_t ledger_network_nonvolatile;
343 uint64_t ledger_network_nonvolatile_compressed;
344 uint64_t ledger_wired_mem;
345 uint64_t ledger_tagged_footprint;
346 uint64_t ledger_tagged_footprint_compressed;
347 uint64_t ledger_media_footprint;
348 uint64_t ledger_media_footprint_compressed;
349 uint64_t ledger_graphics_footprint;
350 uint64_t ledger_graphics_footprint_compressed;
351 uint64_t ledger_neural_footprint;
352 uint64_t ledger_neural_footprint_compressed;
353
354 void *crash_info_ptr = task_get_corpseinfo(corpse_task);
355
356 #if CONFIG_MEMORYSTATUS
357 int memstat_dirty_flags = 0;
358 #endif
359
360 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_EXCEPTION_CODES, sizeof(exc_codes), &uaddr)) {
361 kcdata_memcpy(crash_info_ptr, uaddr, exc_codes, sizeof(exc_codes));
362 }
363
364 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PID, sizeof(p->p_pid), &uaddr)) {
365 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_pid, sizeof(p->p_pid));
366 }
367
368 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PPID, sizeof(p->p_ppid), &uaddr)) {
369 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_ppid, sizeof(p->p_ppid));
370 }
371
372 /* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
373 if ((p->p_exit_reason == OS_REASON_NULL) || !(p->p_exit_reason->osr_flags & OS_REASON_FLAG_NO_CRASHED_TID)) {
374 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CRASHED_THREADID, sizeof(uint64_t), &uaddr)) {
375 kcdata_memcpy(crash_info_ptr, uaddr, &crashed_threadid, sizeof(uint64_t));
376 }
377 }
378
379 static_assert(sizeof(struct proc_uniqidentifierinfo) == sizeof(struct crashinfo_proc_uniqidentifierinfo));
380 if (KERN_SUCCESS ==
381 kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_BSDINFOWITHUNIQID, sizeof(struct proc_uniqidentifierinfo), &uaddr)) {
382 proc_piduniqidentifierinfo(p, &p_uniqidinfo);
383 kcdata_memcpy(crash_info_ptr, uaddr, &p_uniqidinfo, sizeof(struct proc_uniqidentifierinfo));
384 }
385
386 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RUSAGE_INFO, sizeof(rusage_info_current), &uaddr)) {
387 kcdata_memcpy(crash_info_ptr, uaddr, &rup->ri, sizeof(rusage_info_current));
388 }
389
390 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_CSFLAGS, sizeof(p->p_csflags), &uaddr)) {
391 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_csflags, sizeof(p->p_csflags));
392 }
393
394 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_NAME, sizeof(p->p_comm), &uaddr)) {
395 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_comm, sizeof(p->p_comm));
396 }
397
398 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_STARTTIME, sizeof(p->p_start), &uaddr)) {
399 struct timeval64 t64;
400 t64.tv_sec = (int64_t)p->p_start.tv_sec;
401 t64.tv_usec = (int64_t)p->p_start.tv_usec;
402 kcdata_memcpy(crash_info_ptr, uaddr, &t64, sizeof(t64));
403 }
404
405 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_USERSTACK, sizeof(p->user_stack), &uaddr)) {
406 kcdata_memcpy(crash_info_ptr, uaddr, &p->user_stack, sizeof(p->user_stack));
407 }
408
409 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_ARGSLEN, sizeof(p->p_argslen), &uaddr)) {
410 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argslen, sizeof(p->p_argslen));
411 }
412
413 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_ARGC, sizeof(p->p_argc), &uaddr)) {
414 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argc, sizeof(p->p_argc));
415 }
416
417 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PATH, MAXPATHLEN, &uaddr)) {
418 char *buf = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO);
419 proc_pidpathinfo_internal(p, 0, buf, MAXPATHLEN, &retval);
420 kcdata_memcpy(crash_info_ptr, uaddr, buf, MAXPATHLEN);
421 zfree(ZV_NAMEI, buf);
422 }
423
424 pflags = p->p_flag & (P_LP64 | P_SUGID | P_TRANSLATED);
425 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_FLAGS, sizeof(pflags), &uaddr)) {
426 kcdata_memcpy(crash_info_ptr, uaddr, &pflags, sizeof(pflags));
427 }
428
429 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_UID, sizeof(p->p_uid), &uaddr)) {
430 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_uid, sizeof(p->p_uid));
431 }
432
433 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_GID, sizeof(p->p_gid), &uaddr)) {
434 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_gid, sizeof(p->p_gid));
435 }
436
437 cputype = cpu_type() & ~CPU_ARCH_MASK;
438 if (IS_64BIT_PROCESS(p)) {
439 cputype |= CPU_ARCH_ABI64;
440 }
441
442 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CPUTYPE, sizeof(cpu_type_t), &uaddr)) {
443 kcdata_memcpy(crash_info_ptr, uaddr, &cputype, sizeof(cpu_type_t));
444 }
445
446 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT, sizeof(max_footprint_mb), &uaddr)) {
447 max_footprint = get_task_phys_footprint_limit(p->task);
448 max_footprint_mb = max_footprint >> 20;
449 kcdata_memcpy(crash_info_ptr, uaddr, &max_footprint_mb, sizeof(max_footprint_mb));
450 }
451
452 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT_LIFETIME_MAX, sizeof(ledger_phys_footprint_lifetime_max), &uaddr)) {
453 ledger_phys_footprint_lifetime_max = get_task_phys_footprint_lifetime_max(p->task);
454 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_phys_footprint_lifetime_max, sizeof(ledger_phys_footprint_lifetime_max));
455 }
456
457 // In the forking case, the current ledger info is copied into the corpse while the original task is suspended for consistency
458 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_INTERNAL, sizeof(ledger_internal), &uaddr)) {
459 ledger_internal = get_task_internal(corpse_task);
460 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_internal, sizeof(ledger_internal));
461 }
462
463 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_INTERNAL_COMPRESSED, sizeof(ledger_internal_compressed), &uaddr)) {
464 ledger_internal_compressed = get_task_internal_compressed(corpse_task);
465 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_internal_compressed, sizeof(ledger_internal_compressed));
466 }
467
468 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_IOKIT_MAPPED, sizeof(ledger_iokit_mapped), &uaddr)) {
469 ledger_iokit_mapped = get_task_iokit_mapped(corpse_task);
470 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_iokit_mapped, sizeof(ledger_iokit_mapped));
471 }
472
473 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING, sizeof(ledger_alternate_accounting), &uaddr)) {
474 ledger_alternate_accounting = get_task_alternate_accounting(corpse_task);
475 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_alternate_accounting, sizeof(ledger_alternate_accounting));
476 }
477
478 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING_COMPRESSED, sizeof(ledger_alternate_accounting_compressed), &uaddr)) {
479 ledger_alternate_accounting_compressed = get_task_alternate_accounting_compressed(corpse_task);
480 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_alternate_accounting_compressed, sizeof(ledger_alternate_accounting_compressed));
481 }
482
483 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE, sizeof(ledger_purgeable_nonvolatile), &uaddr)) {
484 ledger_purgeable_nonvolatile = get_task_purgeable_nonvolatile(corpse_task);
485 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_purgeable_nonvolatile, sizeof(ledger_purgeable_nonvolatile));
486 }
487
488 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE_COMPRESSED, sizeof(ledger_purgeable_nonvolatile_compressed), &uaddr)) {
489 ledger_purgeable_nonvolatile_compressed = get_task_purgeable_nonvolatile_compressed(corpse_task);
490 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_purgeable_nonvolatile_compressed, sizeof(ledger_purgeable_nonvolatile_compressed));
491 }
492
493 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PAGE_TABLE, sizeof(ledger_page_table), &uaddr)) {
494 ledger_page_table = get_task_page_table(corpse_task);
495 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_page_table, sizeof(ledger_page_table));
496 }
497
498 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT, sizeof(ledger_phys_footprint), &uaddr)) {
499 ledger_phys_footprint = get_task_phys_footprint(corpse_task);
500 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_phys_footprint, sizeof(ledger_phys_footprint));
501 }
502
503 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE, sizeof(ledger_network_nonvolatile), &uaddr)) {
504 ledger_network_nonvolatile = get_task_network_nonvolatile(corpse_task);
505 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_network_nonvolatile, sizeof(ledger_network_nonvolatile));
506 }
507
508 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE_COMPRESSED, sizeof(ledger_network_nonvolatile_compressed), &uaddr)) {
509 ledger_network_nonvolatile_compressed = get_task_network_nonvolatile_compressed(corpse_task);
510 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_network_nonvolatile_compressed, sizeof(ledger_network_nonvolatile_compressed));
511 }
512
513 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_WIRED_MEM, sizeof(ledger_wired_mem), &uaddr)) {
514 ledger_wired_mem = get_task_wired_mem(corpse_task);
515 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_wired_mem, sizeof(ledger_wired_mem));
516 }
517
518 bzero(&pwqinfo, sizeof(struct proc_workqueueinfo));
519 retval = fill_procworkqueue(p, &pwqinfo);
520 if (retval == 0) {
521 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_WORKQUEUEINFO, sizeof(struct proc_workqueueinfo), &uaddr)) {
522 kcdata_memcpy(crash_info_ptr, uaddr, &pwqinfo, sizeof(struct proc_workqueueinfo));
523 }
524 }
525
526 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RESPONSIBLE_PID, sizeof(p->p_responsible_pid), &uaddr)) {
527 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_responsible_pid, sizeof(p->p_responsible_pid));
528 }
529
530 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PERSONA_ID, sizeof(uid_t), &uaddr)) {
531 uid_t persona_id = proc_persona_id(p);
532 kcdata_memcpy(crash_info_ptr, uaddr, &persona_id, sizeof(persona_id));
533 }
534
535 #if CONFIG_COALITIONS
536 if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_COALITION_ID, sizeof(uint64_t), COALITION_NUM_TYPES, &uaddr)) {
537 uint64_t coalition_ids[COALITION_NUM_TYPES];
538 task_coalition_ids(p->task, coalition_ids);
539 kcdata_memcpy(crash_info_ptr, uaddr, coalition_ids, sizeof(coalition_ids));
540 }
541 #endif /* CONFIG_COALITIONS */
542
543 #if CONFIG_MEMORYSTATUS
544 memstat_dirty_flags = memorystatus_dirty_get(p, FALSE);
545 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_DIRTY_FLAGS, sizeof(memstat_dirty_flags), &uaddr)) {
546 kcdata_memcpy(crash_info_ptr, uaddr, &memstat_dirty_flags, sizeof(memstat_dirty_flags));
547 }
548 #endif
549
550 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT_INCREASE, sizeof(p->p_memlimit_increase), &uaddr)) {
551 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_memlimit_increase, sizeof(p->p_memlimit_increase));
552 }
553
554 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT, sizeof(ledger_tagged_footprint), &uaddr)) {
555 ledger_tagged_footprint = get_task_tagged_footprint(corpse_task);
556 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_tagged_footprint, sizeof(ledger_tagged_footprint));
557 }
558
559 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT_COMPRESSED, sizeof(ledger_tagged_footprint_compressed), &uaddr)) {
560 ledger_tagged_footprint_compressed = get_task_tagged_footprint_compressed(corpse_task);
561 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_tagged_footprint_compressed, sizeof(ledger_tagged_footprint_compressed));
562 }
563
564 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT, sizeof(ledger_media_footprint), &uaddr)) {
565 ledger_media_footprint = get_task_media_footprint(corpse_task);
566 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_media_footprint, sizeof(ledger_media_footprint));
567 }
568
569 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT_COMPRESSED, sizeof(ledger_media_footprint_compressed), &uaddr)) {
570 ledger_media_footprint_compressed = get_task_media_footprint_compressed(corpse_task);
571 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_media_footprint_compressed, sizeof(ledger_media_footprint_compressed));
572 }
573
574 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT, sizeof(ledger_graphics_footprint), &uaddr)) {
575 ledger_graphics_footprint = get_task_graphics_footprint(corpse_task);
576 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_graphics_footprint, sizeof(ledger_graphics_footprint));
577 }
578
579 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT_COMPRESSED, sizeof(ledger_graphics_footprint_compressed), &uaddr)) {
580 ledger_graphics_footprint_compressed = get_task_graphics_footprint_compressed(corpse_task);
581 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_graphics_footprint_compressed, sizeof(ledger_graphics_footprint_compressed));
582 }
583
584 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT, sizeof(ledger_neural_footprint), &uaddr)) {
585 ledger_neural_footprint = get_task_neural_footprint(corpse_task);
586 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_neural_footprint, sizeof(ledger_neural_footprint));
587 }
588
589 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT_COMPRESSED, sizeof(ledger_neural_footprint_compressed), &uaddr)) {
590 ledger_neural_footprint_compressed = get_task_neural_footprint_compressed(corpse_task);
591 kcdata_memcpy(crash_info_ptr, uaddr, &ledger_neural_footprint_compressed, sizeof(ledger_neural_footprint_compressed));
592 }
593
594 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORYSTATUS_EFFECTIVE_PRIORITY, sizeof(p->p_memstat_effectivepriority), &uaddr)) {
595 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_memstat_effectivepriority, sizeof(p->p_memstat_effectivepriority));
596 }
597
598 if (p->p_exit_reason != OS_REASON_NULL && reason == OS_REASON_NULL) {
599 reason = p->p_exit_reason;
600 }
601 if (reason != OS_REASON_NULL) {
602 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, EXIT_REASON_SNAPSHOT, sizeof(struct exit_reason_snapshot), &uaddr)) {
603 struct exit_reason_snapshot ers = {
604 .ers_namespace = reason->osr_namespace,
605 .ers_code = reason->osr_code,
606 .ers_flags = reason->osr_flags
607 };
608
609 kcdata_memcpy(crash_info_ptr, uaddr, &ers, sizeof(ers));
610 }
611
612 if (reason->osr_kcd_buf != 0) {
613 uint32_t reason_buf_size = (uint32_t)kcdata_memory_get_used_bytes(&reason->osr_kcd_descriptor);
614 assert(reason_buf_size != 0);
615
616 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, KCDATA_TYPE_NESTED_KCDATA, reason_buf_size, &uaddr)) {
617 kcdata_memcpy(crash_info_ptr, uaddr, reason->osr_kcd_buf, reason_buf_size);
618 }
619 }
620 }
621
622 if (num_udata > 0) {
623 if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_UDATA_PTRS,
624 sizeof(uint64_t), num_udata, &uaddr)) {
625 kcdata_memcpy(crash_info_ptr, uaddr, udata_buffer, sizeof(uint64_t) * num_udata);
626 }
627 }
628 }
629
630 /*
631 * We only parse exit reason kcdata blobs for launchd when it dies
632 * and we're going to panic.
633 *
634 * Meant to be called immediately before panicking.
635 */
636 char *
637 launchd_exit_reason_get_string_desc(os_reason_t exit_reason)
638 {
639 kcdata_iter_t iter;
640
641 if (exit_reason == OS_REASON_NULL || exit_reason->osr_kcd_buf == NULL ||
642 exit_reason->osr_bufsize == 0) {
643 return NULL;
644 }
645
646 iter = kcdata_iter(exit_reason->osr_kcd_buf, exit_reason->osr_bufsize);
647 if (!kcdata_iter_valid(iter)) {
648 #if DEBUG || DEVELOPMENT
649 printf("launchd exit reason has invalid exit reason buffer\n");
650 #endif
651 return NULL;
652 }
653
654 if (kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_OS_REASON) {
655 #if DEBUG || DEVELOPMENT
656 printf("launchd exit reason buffer type mismatch, expected %d got %d\n",
657 KCDATA_BUFFER_BEGIN_OS_REASON, kcdata_iter_type(iter));
658 #endif
659 return NULL;
660 }
661
662 iter = kcdata_iter_find_type(iter, EXIT_REASON_USER_DESC);
663 if (!kcdata_iter_valid(iter)) {
664 return NULL;
665 }
666
667 return (char *)kcdata_iter_payload(iter);
668 }
669
670 __abortlike
671 static void
672 launchd_crashed_panic(proc_t p, int rv)
673 {
674 char *launchd_exit_reason_desc = launchd_exit_reason_get_string_desc(p->p_exit_reason);
675
676 if (p->p_exit_reason == OS_REASON_NULL) {
677 printf("pid 1 exited -- no exit reason available -- (signal %d, exit %d)\n",
678 WTERMSIG(rv), WEXITSTATUS(rv));
679 } else {
680 printf("pid 1 exited -- exit reason namespace %d subcode 0x%llx, description %s\n",
681 p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code, launchd_exit_reason_desc ?
682 launchd_exit_reason_desc : "none");
683 }
684
685 const char *launchd_crashed_prefix_str;
686
687 if (strnstr(p->p_name, "preinit", sizeof(p->p_name))) {
688 launchd_crashed_prefix_str = "LTE preinit process exited";
689 } else {
690 launchd_crashed_prefix_str = "initproc exited";
691 }
692
693 #if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
694 /*
695 * For debugging purposes, generate a core file of initproc before
696 * panicking. Leave at least 300 MB free on the root volume, and ignore
697 * the process's corefile ulimit. fsync() the file to ensure it lands on disk
698 * before the panic hits.
699 */
700
701 int err;
702 uint64_t coredump_start = mach_absolute_time();
703 uint64_t coredump_end;
704 clock_sec_t tv_sec;
705 clock_usec_t tv_usec;
706 uint32_t tv_msec;
707
708
709 err = coredump(p, 300, COREDUMP_IGNORE_ULIMIT | COREDUMP_FULLFSYNC);
710
711 coredump_end = mach_absolute_time();
712
713 absolutetime_to_microtime(coredump_end - coredump_start, &tv_sec, &tv_usec);
714
715 tv_msec = tv_usec / 1000;
716
717 if (err != 0) {
718 printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
719 err, (uint32_t)tv_sec, tv_msec);
720 } else {
721 printf("Generated initproc core file in %d.%03d seconds\n",
722 (uint32_t)tv_sec, tv_msec);
723 }
724 #endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
725
726 sync(p, (void *)NULL, (int *)NULL);
727
728 if (p->p_exit_reason == OS_REASON_NULL) {
729 panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
730 launchd_crashed_prefix_str, WTERMSIG(rv), WEXITSTATUS(rv), ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""));
731 } else {
732 panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s %s -- exit reason namespace %d subcode 0x%llx description: %." LAUNCHD_PANIC_REASON_STRING_MAXLEN "s",
733 ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""),
734 launchd_crashed_prefix_str, p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code,
735 launchd_exit_reason_desc ? launchd_exit_reason_desc : "none");
736 }
737 }
738
739 #define OS_REASON_IFLAG_USER_FAULT 0x1
740
741 #define OS_REASON_TOTAL_USER_FAULTS_PER_PROC 5
742
743 static int
744 abort_with_payload_internal(proc_t p,
745 uint32_t reason_namespace, uint64_t reason_code,
746 user_addr_t payload, uint32_t payload_size,
747 user_addr_t reason_string, uint64_t reason_flags,
748 uint32_t internal_flags)
749 {
750 os_reason_t exit_reason = OS_REASON_NULL;
751 kern_return_t kr = KERN_SUCCESS;
752
753 if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
754 uint32_t old_value = atomic_load_explicit(&p->p_user_faults,
755 memory_order_relaxed);
756 for (;;) {
757 if (old_value >= OS_REASON_TOTAL_USER_FAULTS_PER_PROC) {
758 return EQFULL;
759 }
760 // this reloads the value in old_value
761 if (atomic_compare_exchange_strong_explicit(&p->p_user_faults,
762 &old_value, old_value + 1, memory_order_relaxed,
763 memory_order_relaxed)) {
764 break;
765 }
766 }
767 }
768
769 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
770 p->p_pid, reason_namespace,
771 reason_code, 0, 0);
772
773 exit_reason = build_userspace_exit_reason(reason_namespace, reason_code,
774 payload, payload_size, reason_string, reason_flags | OS_REASON_FLAG_ABORT);
775
776 if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
777 mach_exception_code_t code = 0;
778
779 EXC_GUARD_ENCODE_TYPE(code, GUARD_TYPE_USER); /* simulated EXC_GUARD */
780 EXC_GUARD_ENCODE_FLAVOR(code, 0);
781 EXC_GUARD_ENCODE_TARGET(code, reason_namespace);
782
783 if (exit_reason == OS_REASON_NULL) {
784 kr = KERN_RESOURCE_SHORTAGE;
785 } else {
786 kr = task_violated_guard(code, reason_code, exit_reason);
787 }
788 os_reason_free(exit_reason);
789 } else {
790 /*
791 * We use SIGABRT (rather than calling exit directly from here) so that
792 * the debugger can catch abort_with_{reason,payload} calls.
793 */
794 psignal_try_thread_with_reason(p, current_thread(), SIGABRT, exit_reason);
795 }
796
797 switch (kr) {
798 case KERN_SUCCESS:
799 return 0;
800 case KERN_NOT_SUPPORTED:
801 return ENOTSUP;
802 case KERN_INVALID_ARGUMENT:
803 return EINVAL;
804 case KERN_RESOURCE_SHORTAGE:
805 default:
806 return EBUSY;
807 }
808 }
809
810 int
811 abort_with_payload(struct proc *cur_proc, struct abort_with_payload_args *args,
812 __unused void *retval)
813 {
814 abort_with_payload_internal(cur_proc, args->reason_namespace,
815 args->reason_code, args->payload, args->payload_size,
816 args->reason_string, args->reason_flags, 0);
817
818 return 0;
819 }
820
821 int
822 os_fault_with_payload(struct proc *cur_proc,
823 struct os_fault_with_payload_args *args, __unused int *retval)
824 {
825 return abort_with_payload_internal(cur_proc, args->reason_namespace,
826 args->reason_code, args->payload, args->payload_size,
827 args->reason_string, args->reason_flags, OS_REASON_IFLAG_USER_FAULT);
828 }
829
830
831 /*
832 * exit --
833 * Death of process.
834 */
835 __attribute__((noreturn))
836 void
837 exit(proc_t p, struct exit_args *uap, int *retval)
838 {
839 p->p_xhighbits = ((uint32_t)(uap->rval) & 0xFF000000) >> 24;
840 exit1(p, W_EXITCODE((uint32_t)uap->rval, 0), retval);
841
842 thread_exception_return();
843 /* NOTREACHED */
844 while (TRUE) {
845 thread_block(THREAD_CONTINUE_NULL);
846 }
847 /* NOTREACHED */
848 }
849
850 /*
851 * Exit: deallocate address space and other resources, change proc state
852 * to zombie, and unlink proc from allproc and parent's lists. Save exit
853 * status and rusage for wait(). Check for child processes and orphan them.
854 */
855 int
856 exit1(proc_t p, int rv, int *retval)
857 {
858 return exit1_internal(p, rv, retval, TRUE, TRUE, 0);
859 }
860
861 int
862 exit1_internal(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
863 int jetsam_flags)
864 {
865 return exit_with_reason(p, rv, retval, thread_can_terminate, perf_notify, jetsam_flags, OS_REASON_NULL);
866 }
867
868 /*
869 * NOTE: exit_with_reason drops a reference on the passed exit_reason
870 */
871 int
872 exit_with_reason(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
873 int jetsam_flags, struct os_reason *exit_reason)
874 {
875 thread_t self = current_thread();
876 struct task *task = p->task;
877 struct uthread *ut;
878 int error = 0;
879
880 /*
881 * If a thread in this task has already
882 * called exit(), then halt any others
883 * right here.
884 */
885
886 ut = get_bsdthread_info(self);
887 if ((p == current_proc()) &&
888 (ut->uu_flag & UT_VFORK)) {
889 os_reason_free(exit_reason);
890 if (!thread_can_terminate) {
891 return EINVAL;
892 }
893
894 vfork_exit(p, rv);
895 vfork_return(p, retval, p->p_pid);
896 unix_syscall_return(0);
897 /* NOT REACHED */
898 }
899
900 /*
901 * The parameter list of audit_syscall_exit() was augmented to
902 * take the Darwin syscall number as the first parameter,
903 * which is currently required by mac_audit_postselect().
904 */
905
906 /*
907 * The BSM token contains two components: an exit status as passed
908 * to exit(), and a return value to indicate what sort of exit it
909 * was. The exit status is WEXITSTATUS(rv), but it's not clear
910 * what the return value is.
911 */
912 AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
913 /*
914 * TODO: what to audit here when jetsam calls exit and the uthread,
915 * 'ut' does not belong to the proc, 'p'.
916 */
917 AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */
918
919 DTRACE_PROC1(exit, int, CLD_EXITED);
920
921 /* mark process is going to exit and pull out of DBG/disk throttle */
922 /* TODO: This should be done after becoming exit thread */
923 proc_set_task_policy(p->task, TASK_POLICY_ATTRIBUTE,
924 TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
925
926 proc_lock(p);
927 error = proc_transstart(p, 1, (jetsam_flags ? 1 : 0));
928 if (error == EDEADLK) {
929 /*
930 * If proc_transstart() returns EDEADLK, then another thread
931 * is either exec'ing or exiting. Return an error and allow
932 * the other thread to continue.
933 */
934 proc_unlock(p);
935 os_reason_free(exit_reason);
936 if (current_proc() == p) {
937 if (p->exit_thread == self) {
938 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
939 p->p_comm, p->p_pid);
940 }
941
942 if (thread_can_terminate) {
943 thread_exception_return();
944 }
945 }
946
947 return error;
948 }
949
950 while (p->exit_thread != self) {
951 if (sig_try_locked(p) <= 0) {
952 proc_transend(p, 1);
953 os_reason_free(exit_reason);
954
955 if (get_threadtask(self) != task) {
956 proc_unlock(p);
957 return 0;
958 }
959 proc_unlock(p);
960
961 thread_terminate(self);
962 if (!thread_can_terminate) {
963 return 0;
964 }
965
966 thread_exception_return();
967 /* NOTREACHED */
968 }
969 sig_lock_to_exit(p);
970 }
971
972 if (exit_reason != OS_REASON_NULL) {
973 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_COMMIT) | DBG_FUNC_NONE,
974 p->p_pid, exit_reason->osr_namespace,
975 exit_reason->osr_code, 0, 0);
976 }
977
978 assert(p->p_exit_reason == OS_REASON_NULL);
979 p->p_exit_reason = exit_reason;
980
981 p->p_lflag |= P_LEXIT;
982 p->p_xstat = rv;
983 p->p_lflag |= jetsam_flags;
984
985 proc_transend(p, 1);
986 proc_unlock(p);
987
988 proc_prepareexit(p, rv, perf_notify);
989
990 /* Last thread to terminate will call proc_exit() */
991 task_terminate_internal(task);
992
993 return 0;
994 }
995
996 #if CONFIG_MEMORYSTATUS
997 /*
998 * Remove this process from jetsam bands for freezing or exiting. Note this will block, if the process
999 * is currently being frozen.
1000 * The proc_list_lock is held by the caller.
1001 * NB: If the process should be ineligible for future freezing or jetsaming the caller should first set
1002 * the p_listflag P_LIST_EXITED bit.
1003 */
1004 static void
1005 proc_memorystatus_remove(proc_t p)
1006 {
1007 LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED);
1008 while (memorystatus_remove(p) == EAGAIN) {
1009 os_log(OS_LOG_DEFAULT, "memorystatus_remove: Process[%d] tried to exit while being frozen. Blocking exit until freeze completes.", p->p_pid);
1010 msleep(&p->p_memstat_state, proc_list_mlock, PWAIT, "proc_memorystatus_remove", NULL);
1011 }
1012 }
1013 #endif
1014
1015 void
1016 proc_prepareexit(proc_t p, int rv, boolean_t perf_notify)
1017 {
1018 mach_exception_data_type_t code = 0, subcode = 0;
1019
1020 struct uthread *ut;
1021 thread_t self = current_thread();
1022 ut = get_bsdthread_info(self);
1023 struct rusage_superset *rup;
1024 int kr = 0;
1025 int create_corpse = FALSE;
1026
1027 if (p == initproc) {
1028 launchd_crashed_panic(p, rv);
1029 /* NOTREACHED */
1030 }
1031
1032 /*
1033 * Generate a corefile/crashlog if:
1034 * The process doesn't have an exit reason that indicates no crash report should be created
1035 * AND any of the following are true:
1036 * - The process was terminated due to a fatal signal that generates a core
1037 * - The process was killed due to a code signing violation
1038 * - The process has an exit reason that indicates we should generate a crash report
1039 *
1040 * The first condition is necessary because abort_with_reason()/payload() use SIGABRT
1041 * (which normally triggers a core) but may indicate that no crash report should be created.
1042 */
1043 if (!(PROC_HAS_EXITREASON(p) && (PROC_EXITREASON_FLAGS(p) & OS_REASON_FLAG_NO_CRASH_REPORT)) &&
1044 (hassigprop(WTERMSIG(rv), SA_CORE) || ((p->p_csflags & CS_KILLED) != 0) ||
1045 (PROC_HAS_EXITREASON(p) && (PROC_EXITREASON_FLAGS(p) &
1046 OS_REASON_FLAG_GENERATE_CRASH_REPORT)))) {
1047 /*
1048 * Workaround for processes checking up on PT_DENY_ATTACH:
1049 * should be backed out post-Leopard (details in 5431025).
1050 */
1051 if ((SIGSEGV == WTERMSIG(rv)) &&
1052 (p->p_pptr->p_lflag & P_LNOATTACH)) {
1053 goto skipcheck;
1054 }
1055
1056 /*
1057 * Crash Reporter looks for the signal value, original exception
1058 * type, and low 20 bits of the original code in code[0]
1059 * (8, 4, and 20 bits respectively). code[1] is unmodified.
1060 */
1061 code = ((WTERMSIG(rv) & 0xff) << 24) |
1062 ((ut->uu_exception & 0x0f) << 20) |
1063 ((int)ut->uu_code & 0xfffff);
1064 subcode = ut->uu_subcode;
1065
1066 kr = task_exception_notify(EXC_CRASH, code, subcode);
1067
1068 /* Nobody handled EXC_CRASH?? remember to make corpse */
1069 if (kr != 0) {
1070 create_corpse = TRUE;
1071 }
1072 }
1073
1074 skipcheck:
1075 /* Notify the perf server? */
1076 if (perf_notify) {
1077 (void)sys_perf_notify(self, p->p_pid);
1078 }
1079
1080
1081 /* stash the usage into corpse data if making_corpse == true */
1082 if (create_corpse == TRUE) {
1083 kr = task_mark_corpse(p->task);
1084 if (kr != KERN_SUCCESS) {
1085 if (kr == KERN_NO_SPACE) {
1086 printf("Process[%d] has no vm space for corpse info.\n", p->p_pid);
1087 } else if (kr == KERN_NOT_SUPPORTED) {
1088 printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", p->p_pid);
1089 } else {
1090 printf("Process[%d] crashed: %s. Too many corpses being created.\n", p->p_pid, p->p_comm);
1091 }
1092 create_corpse = FALSE;
1093 }
1094 }
1095
1096 /*
1097 * Before this process becomes a zombie, stash resource usage
1098 * stats in the proc for external observers to query
1099 * via proc_pid_rusage().
1100 *
1101 * If the zombie allocation fails, just punt the stats.
1102 */
1103 rup = zalloc(zombie_zone);
1104 gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT);
1105 rup->ri.ri_phys_footprint = 0;
1106 rup->ri.ri_proc_exit_abstime = mach_absolute_time();
1107 /*
1108 * Make the rusage_info visible to external observers
1109 * only after it has been completely filled in.
1110 */
1111 p->p_ru = rup;
1112
1113 if (create_corpse) {
1114 int est_knotes = 0, num_knotes = 0;
1115 uint64_t *buffer = NULL;
1116 uint32_t buf_size = 0;
1117
1118 /* Get all the udata pointers from kqueue */
1119 est_knotes = kevent_proc_copy_uptrs(p, NULL, 0);
1120 if (est_knotes > 0) {
1121 buf_size = (uint32_t)((est_knotes + 32) * sizeof(uint64_t));
1122 buffer = kheap_alloc(KHEAP_TEMP, buf_size, Z_WAITOK);
1123 num_knotes = kevent_proc_copy_uptrs(p, buffer, buf_size);
1124 if (num_knotes > est_knotes + 32) {
1125 num_knotes = est_knotes + 32;
1126 }
1127 }
1128
1129 /* Update the code, subcode based on exit reason */
1130 proc_update_corpse_exception_codes(p, &code, &subcode);
1131 populate_corpse_crashinfo(p, p->task, rup,
1132 code, subcode, buffer, num_knotes, NULL);
1133 if (buffer != NULL) {
1134 kheap_free(KHEAP_TEMP, buffer, buf_size);
1135 }
1136 }
1137 /*
1138 * Remove proc from allproc queue and from pidhash chain.
1139 * Need to do this before we do anything that can block.
1140 * Not doing causes things like mount() find this on allproc
1141 * in partially cleaned state.
1142 */
1143
1144 proc_list_lock();
1145
1146 #if CONFIG_MEMORYSTATUS
1147 proc_memorystatus_remove(p);
1148 #endif
1149
1150 LIST_REMOVE(p, p_list);
1151 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
1152 /* will not be visible via proc_find */
1153 p->p_listflag |= P_LIST_EXITED;
1154
1155 proc_list_unlock();
1156
1157 #ifdef PGINPROF
1158 vmsizmon();
1159 #endif
1160 /*
1161 * If parent is waiting for us to exit or exec,
1162 * P_LPPWAIT is set; we will wakeup the parent below.
1163 */
1164 proc_lock(p);
1165 p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
1166 p->p_sigignore = ~(sigcantmask);
1167 ut->uu_siglist = 0;
1168 proc_unlock(p);
1169 }
1170
1171 void
1172 proc_exit(proc_t p)
1173 {
1174 proc_t q;
1175 proc_t pp;
1176 struct task *task = p->task;
1177 vnode_t tvp = NULLVP;
1178 struct pgrp * pg;
1179 struct session *sessp;
1180 struct uthread * uth;
1181 pid_t pid;
1182 int exitval;
1183 int knote_hint;
1184
1185 uth = current_uthread();
1186
1187 proc_lock(p);
1188 proc_transstart(p, 1, 0);
1189 if (!(p->p_lflag & P_LEXIT)) {
1190 /*
1191 * This can happen if a thread_terminate() occurs
1192 * in a single-threaded process.
1193 */
1194 p->p_lflag |= P_LEXIT;
1195 proc_transend(p, 1);
1196 proc_unlock(p);
1197 proc_prepareexit(p, 0, TRUE);
1198 (void) task_terminate_internal(task);
1199 proc_lock(p);
1200 } else {
1201 proc_transend(p, 1);
1202 }
1203
1204 p->p_lflag |= P_LPEXIT;
1205
1206 /*
1207 * Other kernel threads may be in the middle of signalling this process.
1208 * Wait for those threads to wrap it up before making the process
1209 * disappear on them.
1210 */
1211 if ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 0)) {
1212 p->p_sigwaitcnt++;
1213 while ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 1)) {
1214 msleep(&p->p_sigmask, &p->p_mlock, PWAIT, "proc_sigdrain", NULL);
1215 }
1216 p->p_sigwaitcnt--;
1217 }
1218
1219 proc_unlock(p);
1220 pid = p->p_pid;
1221 exitval = p->p_xstat;
1222 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
1223 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_START,
1224 pid, exitval, 0, 0, 0);
1225
1226 #if CONFIG_DTRACE
1227 dtrace_proc_exit(p);
1228 #endif
1229
1230 /*
1231 * need to cancel async IO requests that can be cancelled and wait for those
1232 * already active. MAY BLOCK!
1233 */
1234
1235 proc_refdrain(p);
1236
1237 /* if any pending cpu limits action, clear it */
1238 task_clear_cpuusage(p->task, TRUE);
1239
1240 workq_mark_exiting(p);
1241
1242 _aio_exit( p );
1243
1244 /*
1245 * Close open files and release open-file table.
1246 * This may block!
1247 */
1248 fdfree(p);
1249
1250 /*
1251 * Once all the knotes, kqueues & workloops are destroyed, get rid of the
1252 * workqueue.
1253 */
1254 workq_exit(p);
1255
1256 if (uth->uu_lowpri_window) {
1257 /*
1258 * task is marked as a low priority I/O type
1259 * and the I/O we issued while in flushing files on close
1260 * collided with normal I/O operations...
1261 * no need to throttle this thread since its going away
1262 * but we do need to update our bookeeping w/r to throttled threads
1263 */
1264 throttle_lowpri_io(0);
1265 }
1266
1267 if (p->p_lflag & P_LNSPACE_RESOLVER) {
1268 /*
1269 * The namespace resolver is exiting; there may be
1270 * outstanding materialization requests to clean up.
1271 */
1272 nspace_resolver_exited(p);
1273 }
1274
1275 #if SYSV_SHM
1276 /* Close ref SYSV Shared memory*/
1277 if (p->vm_shm) {
1278 shmexit(p);
1279 }
1280 #endif
1281 #if SYSV_SEM
1282 /* Release SYSV semaphores */
1283 semexit(p);
1284 #endif
1285
1286 #if PSYNCH
1287 pth_proc_hashdelete(p);
1288 #endif /* PSYNCH */
1289
1290 sessp = proc_session(p);
1291 if (SESS_LEADER(p, sessp)) {
1292 if (sessp->s_ttyvp != NULLVP) {
1293 struct vnode *ttyvp;
1294 int ttyvid;
1295 int cttyflag = 0;
1296 struct vfs_context context;
1297 struct tty *tp;
1298
1299 /*
1300 * Controlling process.
1301 * Signal foreground pgrp,
1302 * drain controlling terminal
1303 * and revoke access to controlling terminal.
1304 */
1305 session_lock(sessp);
1306 tp = SESSION_TP(sessp);
1307 if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
1308 session_unlock(sessp);
1309
1310 tty_pgsignal(tp, SIGHUP, 1);
1311
1312 session_lock(sessp);
1313 tp = SESSION_TP(sessp);
1314 }
1315 cttyflag = sessp->s_flags & S_CTTYREF;
1316 sessp->s_flags &= ~S_CTTYREF;
1317 ttyvp = sessp->s_ttyvp;
1318 ttyvid = sessp->s_ttyvid;
1319 sessp->s_ttyvp = NULLVP;
1320 sessp->s_ttyvid = 0;
1321 sessp->s_ttyp = TTY_NULL;
1322 sessp->s_ttypgrpid = NO_PID;
1323 session_unlock(sessp);
1324
1325 if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
1326 if (tp != TTY_NULL) {
1327 tty_lock(tp);
1328 (void) ttywait(tp);
1329 tty_unlock(tp);
1330 }
1331
1332 context.vc_thread = NULL;
1333 context.vc_ucred = kauth_cred_proc_ref(p);
1334 VNOP_REVOKE(ttyvp, REVOKEALL, &context);
1335 if (cttyflag) {
1336 /*
1337 * Release the extra usecount taken in cttyopen.
1338 * usecount should be released after VNOP_REVOKE is called.
1339 * This usecount was taken to ensure that
1340 * the VNOP_REVOKE results in a close to
1341 * the tty since cttyclose is a no-op.
1342 */
1343 vnode_rele(ttyvp);
1344 }
1345 vnode_put(ttyvp);
1346 kauth_cred_unref(&context.vc_ucred);
1347 ttyvp = NULLVP;
1348 }
1349 if (tp) {
1350 ttyfree(tp);
1351 }
1352 }
1353 session_lock(sessp);
1354 sessp->s_leader = NULL;
1355 session_unlock(sessp);
1356 }
1357 session_rele(sessp);
1358
1359 pg = proc_pgrp(p);
1360 fixjobc(p, pg, 0);
1361 pg_rele(pg);
1362
1363 /*
1364 * Change RLIMIT_FSIZE for accounting/debugging. proc_limitsetcur_internal() will COW the current plimit
1365 * before making changes if the current plimit is shared. The COW'ed plimit will be freed
1366 * below by calling proc_limitdrop().
1367 */
1368 proc_limitsetcur_internal(p, RLIMIT_FSIZE, RLIM_INFINITY);
1369
1370 (void)acct_process(p);
1371
1372 proc_list_lock();
1373
1374 if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
1375 p->p_listflag &= ~P_LIST_EXITCOUNT;
1376 proc_shutdown_exitcount--;
1377 if (proc_shutdown_exitcount == 0) {
1378 wakeup(&proc_shutdown_exitcount);
1379 }
1380 }
1381
1382 /* wait till parentrefs are dropped and grant no more */
1383 proc_childdrainstart(p);
1384 while ((q = p->p_children.lh_first) != NULL) {
1385 int reparentedtoinit = (q->p_listflag & P_LIST_DEADPARENT) ? 1 : 0;
1386 if (q->p_stat == SZOMB) {
1387 if (p != q->p_pptr) {
1388 panic("parent child linkage broken");
1389 }
1390 /* check for sysctl zomb lookup */
1391 while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
1392 msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1393 }
1394 q->p_listflag |= P_LIST_WAITING;
1395 /*
1396 * This is a named reference and it is not granted
1397 * if the reap is already in progress. So we get
1398 * the reference here exclusively and their can be
1399 * no waiters. So there is no need for a wakeup
1400 * after we are done. Also the reap frees the structure
1401 * and the proc struct cannot be used for wakeups as well.
1402 * It is safe to use q here as this is system reap
1403 */
1404 (void)reap_child_locked(p, q, 1, reparentedtoinit, 1, 0);
1405 } else {
1406 /*
1407 * Traced processes are killed
1408 * since their existence means someone is messing up.
1409 */
1410 if (q->p_lflag & P_LTRACED) {
1411 struct proc *opp;
1412
1413 /*
1414 * Take a reference on the child process to
1415 * ensure it doesn't exit and disappear between
1416 * the time we drop the list_lock and attempt
1417 * to acquire its proc_lock.
1418 */
1419 if (proc_ref_locked(q) != q) {
1420 continue;
1421 }
1422
1423 proc_list_unlock();
1424
1425 opp = proc_find(q->p_oppid);
1426 if (opp != PROC_NULL) {
1427 proc_list_lock();
1428 q->p_oppid = 0;
1429 proc_list_unlock();
1430 proc_reparentlocked(q, opp, 0, 0);
1431 proc_rele(opp);
1432 } else {
1433 /* original parent exited while traced */
1434 proc_list_lock();
1435 q->p_listflag |= P_LIST_DEADPARENT;
1436 q->p_oppid = 0;
1437 proc_list_unlock();
1438 proc_reparentlocked(q, initproc, 0, 0);
1439 }
1440
1441 proc_lock(q);
1442 q->p_lflag &= ~P_LTRACED;
1443
1444 if (q->sigwait_thread) {
1445 thread_t thread = q->sigwait_thread;
1446
1447 proc_unlock(q);
1448 /*
1449 * The sigwait_thread could be stopped at a
1450 * breakpoint. Wake it up to kill.
1451 * Need to do this as it could be a thread which is not
1452 * the first thread in the task. So any attempts to kill
1453 * the process would result into a deadlock on q->sigwait.
1454 */
1455 thread_resume(thread);
1456 clear_wait(thread, THREAD_INTERRUPTED);
1457 threadsignal(thread, SIGKILL, 0, TRUE);
1458 } else {
1459 proc_unlock(q);
1460 }
1461
1462 psignal(q, SIGKILL);
1463 proc_list_lock();
1464 proc_rele_locked(q);
1465 } else {
1466 q->p_listflag |= P_LIST_DEADPARENT;
1467 proc_reparentlocked(q, initproc, 0, 1);
1468 }
1469 }
1470 }
1471
1472 proc_childdrainend(p);
1473 proc_list_unlock();
1474
1475 #if CONFIG_MACF
1476 /*
1477 * Notify MAC policies that proc is dead.
1478 * This should be replaced with proper label management
1479 * (rdar://problem/32126399).
1480 */
1481 mac_proc_notify_exit(p);
1482 #endif
1483
1484 /*
1485 * Release reference to text vnode
1486 */
1487 tvp = p->p_textvp;
1488 p->p_textvp = NULL;
1489 if (tvp != NULLVP) {
1490 vnode_rele(tvp);
1491 }
1492
1493 /*
1494 * Save exit status and final rusage info, adding in child rusage
1495 * info and self times. If we were unable to allocate a zombie
1496 * structure, this information is lost.
1497 */
1498 if (p->p_ru != NULL) {
1499 calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, NULL);
1500 p->p_ru->ru = p->p_stats->p_ru;
1501
1502 ruadd(&(p->p_ru->ru), &p->p_stats->p_cru);
1503 }
1504
1505 /*
1506 * Free up profiling buffers.
1507 */
1508 {
1509 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
1510
1511 p1 = p0->pr_next;
1512 p0->pr_next = NULL;
1513 p0->pr_scale = 0;
1514
1515 for (; p1 != NULL; p1 = pn) {
1516 pn = p1->pr_next;
1517 kfree(p1, sizeof *p1);
1518 }
1519 }
1520
1521 proc_free_realitimer(p);
1522
1523 /*
1524 * Other substructures are freed from wait().
1525 */
1526 zfree(proc_stats_zone, p->p_stats);
1527 p->p_stats = NULL;
1528
1529 zfree(proc_sigacts_zone, p->p_sigacts);
1530 p->p_sigacts = NULL;
1531
1532 proc_limitdrop(p);
1533
1534 /*
1535 * Finish up by terminating the task
1536 * and halt this thread (only if a
1537 * member of the task exiting).
1538 */
1539 p->task = TASK_NULL;
1540 set_bsdtask_info(task, NULL);
1541
1542 knote_hint = NOTE_EXIT | (p->p_xstat & 0xffff);
1543 proc_knote(p, knote_hint);
1544
1545 /* mark the thread as the one that is doing proc_exit
1546 * no need to hold proc lock in uthread_free
1547 */
1548 uth->uu_flag |= UT_PROCEXIT;
1549 /*
1550 * Notify parent that we're gone.
1551 */
1552 pp = proc_parent(p);
1553 if (pp->p_flag & P_NOCLDWAIT) {
1554 if (p->p_ru != NULL) {
1555 proc_lock(pp);
1556 #if 3839178
1557 /*
1558 * If the parent is ignoring SIGCHLD, then POSIX requires
1559 * us to not add the resource usage to the parent process -
1560 * we are only going to hand it off to init to get reaped.
1561 * We should contest the standard in this case on the basis
1562 * of RLIMIT_CPU.
1563 */
1564 #else /* !3839178 */
1565 /*
1566 * Add child resource usage to parent before giving
1567 * zombie to init. If we were unable to allocate a
1568 * zombie structure, this information is lost.
1569 */
1570 ruadd(&pp->p_stats->p_cru, &p->p_ru->ru);
1571 #endif /* !3839178 */
1572 update_rusage_info_child(&pp->p_stats->ri_child, &p->p_ru->ri);
1573 proc_unlock(pp);
1574 }
1575
1576 /* kernel can reap this one, no need to move it to launchd */
1577 proc_list_lock();
1578 p->p_listflag |= P_LIST_DEADPARENT;
1579 proc_list_unlock();
1580 }
1581 if ((p->p_listflag & P_LIST_DEADPARENT) == 0 || p->p_oppid) {
1582 if (pp != initproc) {
1583 proc_lock(pp);
1584 pp->si_pid = p->p_pid;
1585 pp->p_xhighbits = p->p_xhighbits;
1586 p->p_xhighbits = 0;
1587 pp->si_status = p->p_xstat;
1588 pp->si_code = CLD_EXITED;
1589 /*
1590 * p_ucred usage is safe as it is an exiting process
1591 * and reference is dropped in reap
1592 */
1593 pp->si_uid = kauth_cred_getruid(p->p_ucred);
1594 proc_unlock(pp);
1595 }
1596 /* mark as a zombie */
1597 /* No need to take proc lock as all refs are drained and
1598 * no one except parent (reaping ) can look at this.
1599 * The write is to an int and is coherent. Also parent is
1600 * keyed off of list lock for reaping
1601 */
1602 DTRACE_PROC2(exited, proc_t, p, int, exitval);
1603 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
1604 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
1605 pid, exitval, 0, 0, 0);
1606 p->p_stat = SZOMB;
1607 /*
1608 * The current process can be reaped so, no one
1609 * can depend on this
1610 */
1611
1612 psignal(pp, SIGCHLD);
1613
1614 /* and now wakeup the parent */
1615 proc_list_lock();
1616 wakeup((caddr_t)pp);
1617 proc_list_unlock();
1618 } else {
1619 /* should be fine as parent proc would be initproc */
1620 /* mark as a zombie */
1621 /* No need to take proc lock as all refs are drained and
1622 * no one except parent (reaping ) can look at this.
1623 * The write is to an int and is coherent. Also parent is
1624 * keyed off of list lock for reaping
1625 */
1626 DTRACE_PROC2(exited, proc_t, p, int, exitval);
1627 proc_list_lock();
1628 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
1629 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
1630 pid, exitval, 0, 0, 0);
1631 /* check for sysctl zomb lookup */
1632 while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
1633 msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1634 }
1635 /* safe to use p as this is a system reap */
1636 p->p_stat = SZOMB;
1637 p->p_listflag |= P_LIST_WAITING;
1638
1639 /*
1640 * This is a named reference and it is not granted
1641 * if the reap is already in progress. So we get
1642 * the reference here exclusively and their can be
1643 * no waiters. So there is no need for a wakeup
1644 * after we are done. AlsO the reap frees the structure
1645 * and the proc struct cannot be used for wakeups as well.
1646 * It is safe to use p here as this is system reap
1647 */
1648 (void)reap_child_locked(pp, p, 1, 0, 1, 1);
1649 /* list lock dropped by reap_child_locked */
1650 }
1651 if (uth->uu_lowpri_window) {
1652 /*
1653 * task is marked as a low priority I/O type and we've
1654 * somehow picked up another throttle during exit processing...
1655 * no need to throttle this thread since its going away
1656 * but we do need to update our bookeeping w/r to throttled threads
1657 */
1658 throttle_lowpri_io(0);
1659 }
1660
1661 proc_rele(pp);
1662 }
1663
1664
1665 /*
1666 * reap_child_locked
1667 *
1668 * Description: Given a process from which all status information needed
1669 * has already been extracted, if the process is a ptrace
1670 * attach process, detach it and give it back to its real
1671 * parent, else recover all resources remaining associated
1672 * with it.
1673 *
1674 * Parameters: proc_t parent Parent of process being reaped
1675 * proc_t child Process to reap
1676 *
1677 * Returns: 0 Process was not reaped because it
1678 * came from an attach
1679 * 1 Process was reaped
1680 */
1681 static int
1682 reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock)
1683 {
1684 proc_t trace_parent = PROC_NULL; /* Traced parent process, if tracing */
1685
1686 if (locked == 1) {
1687 proc_list_unlock();
1688 }
1689
1690 /*
1691 * If we got the child via a ptrace 'attach',
1692 * we need to give it back to the old parent.
1693 *
1694 * Exception: someone who has been reparented to launchd before being
1695 * ptraced can simply be reaped, refer to radar 5677288
1696 * p_oppid -> ptraced
1697 * trace_parent == initproc -> away from launchd
1698 * reparentedtoinit -> came to launchd by reparenting
1699 */
1700 if (child->p_oppid) {
1701 int knote_hint;
1702 pid_t oppid;
1703
1704 proc_lock(child);
1705 oppid = child->p_oppid;
1706 child->p_oppid = 0;
1707 knote_hint = NOTE_EXIT | (child->p_xstat & 0xffff);
1708 proc_unlock(child);
1709
1710 if ((trace_parent = proc_find(oppid))
1711 && !((trace_parent == initproc) && reparentedtoinit)) {
1712 if (trace_parent != initproc) {
1713 /*
1714 * proc internal fileds and p_ucred usage safe
1715 * here as child is dead and is not reaped or
1716 * reparented yet
1717 */
1718 proc_lock(trace_parent);
1719 trace_parent->si_pid = child->p_pid;
1720 trace_parent->si_status = child->p_xstat;
1721 trace_parent->si_code = CLD_CONTINUED;
1722 trace_parent->si_uid = kauth_cred_getruid(child->p_ucred);
1723 proc_unlock(trace_parent);
1724 }
1725 proc_reparentlocked(child, trace_parent, 1, 0);
1726
1727 /* resend knote to original parent (and others) after reparenting */
1728 proc_knote(child, knote_hint);
1729
1730 psignal(trace_parent, SIGCHLD);
1731 proc_list_lock();
1732 wakeup((caddr_t)trace_parent);
1733 child->p_listflag &= ~P_LIST_WAITING;
1734 wakeup(&child->p_stat);
1735 proc_list_unlock();
1736 proc_rele(trace_parent);
1737 if ((locked == 1) && (droplock == 0)) {
1738 proc_list_lock();
1739 }
1740 return 0;
1741 }
1742
1743 /*
1744 * If we can't reparent (e.g. the original parent exited while child was being debugged, or
1745 * original parent is the same as the debugger currently exiting), we still need to satisfy
1746 * the knote lifecycle for other observers on the system. While the debugger was attached,
1747 * the NOTE_EXIT would not have been broadcast during initial child termination.
1748 */
1749 proc_knote(child, knote_hint);
1750
1751 if (trace_parent != PROC_NULL) {
1752 proc_rele(trace_parent);
1753 }
1754 }
1755
1756 #pragma clang diagnostic push
1757 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1758 proc_knote(child, NOTE_REAP);
1759 #pragma clang diagnostic pop
1760
1761 proc_knote_drain(child);
1762
1763 child->p_xstat = 0;
1764 if (child->p_ru) {
1765 proc_lock(parent);
1766 #if 3839178
1767 /*
1768 * If the parent is ignoring SIGCHLD, then POSIX requires
1769 * us to not add the resource usage to the parent process -
1770 * we are only going to hand it off to init to get reaped.
1771 * We should contest the standard in this case on the basis
1772 * of RLIMIT_CPU.
1773 */
1774 if (!(parent->p_flag & P_NOCLDWAIT))
1775 #endif /* 3839178 */
1776 ruadd(&parent->p_stats->p_cru, &child->p_ru->ru);
1777 update_rusage_info_child(&parent->p_stats->ri_child, &child->p_ru->ri);
1778 proc_unlock(parent);
1779 zfree(zombie_zone, child->p_ru);
1780 child->p_ru = NULL;
1781 } else {
1782 printf("Warning : lost p_ru for %s\n", child->p_comm);
1783 }
1784
1785 AUDIT_SESSION_PROCEXIT(child);
1786
1787 /*
1788 * Decrement the count of procs running with this uid.
1789 * p_ucred usage is safe here as it is an exited process.
1790 * and refernce is dropped after these calls down below
1791 * (locking protection is provided by list lock held in chgproccnt)
1792 */
1793 #if CONFIG_PERSONAS
1794 /*
1795 * persona_proc_drop calls chgproccnt(-1) on the persona uid,
1796 * and (+1) on the child->p_ucred uid
1797 */
1798 persona_proc_drop(child);
1799 #endif
1800 (void)chgproccnt(kauth_cred_getruid(child->p_ucred), -1);
1801
1802 os_reason_free(child->p_exit_reason);
1803
1804 /*
1805 * Finally finished with old proc entry.
1806 * Unlink it from its process group and free it.
1807 */
1808 leavepgrp(child);
1809
1810 proc_list_lock();
1811 LIST_REMOVE(child, p_list); /* off zombproc */
1812 parent->p_childrencnt--;
1813 LIST_REMOVE(child, p_sibling);
1814 /* If there are no more children wakeup parent */
1815 if ((deadparent != 0) && (LIST_EMPTY(&parent->p_children))) {
1816 wakeup((caddr_t)parent); /* with list lock held */
1817 }
1818 child->p_listflag &= ~P_LIST_WAITING;
1819 wakeup(&child->p_stat);
1820
1821 /* Take it out of process hash */
1822 LIST_REMOVE(child, p_hash);
1823 child->p_listflag &= ~P_LIST_INHASH;
1824 proc_checkdeadrefs(child);
1825 nprocs--;
1826
1827 if (deadparent) {
1828 /*
1829 * If a child zombie is being reaped because its parent
1830 * is exiting, make sure we update the list flag
1831 */
1832 child->p_listflag |= P_LIST_DEADPARENT;
1833 }
1834
1835 proc_list_unlock();
1836
1837 /*
1838 * Free up credentials.
1839 */
1840 if (IS_VALID_CRED(child->p_ucred)) {
1841 kauth_cred_t tmp_ucred = child->p_ucred;
1842 kauth_cred_unref(&tmp_ucred);
1843 child->p_ucred = NOCRED;
1844 }
1845
1846 lck_mtx_destroy(&child->p_mlock, proc_mlock_grp);
1847 lck_mtx_destroy(&child->p_ucred_mlock, proc_ucred_mlock_grp);
1848 lck_mtx_destroy(&child->p_fdmlock, proc_fdmlock_grp);
1849 #if CONFIG_DTRACE
1850 lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
1851 #endif
1852 lck_spin_destroy(&child->p_slock, proc_slock_grp);
1853
1854 zfree(proc_zone, child);
1855 if ((locked == 1) && (droplock == 0)) {
1856 proc_list_lock();
1857 }
1858
1859 return 1;
1860 }
1861
1862
1863 int
1864 wait1continue(int result)
1865 {
1866 proc_t p;
1867 thread_t thread;
1868 uthread_t uth;
1869 struct _wait4_data *wait4_data;
1870 struct wait4_nocancel_args *uap;
1871 int *retval;
1872
1873 if (result) {
1874 return result;
1875 }
1876
1877 p = current_proc();
1878 thread = current_thread();
1879 uth = (struct uthread *)get_bsdthread_info(thread);
1880
1881 wait4_data = &uth->uu_save.uus_wait4_data;
1882 uap = wait4_data->args;
1883 retval = wait4_data->retval;
1884 return wait4_nocancel(p, uap, retval);
1885 }
1886
1887 int
1888 wait4(proc_t q, struct wait4_args *uap, int32_t *retval)
1889 {
1890 __pthread_testcancel(1);
1891 return wait4_nocancel(q, (struct wait4_nocancel_args *)uap, retval);
1892 }
1893
1894 int
1895 wait4_nocancel(proc_t q, struct wait4_nocancel_args *uap, int32_t *retval)
1896 {
1897 int nfound;
1898 int sibling_count;
1899 proc_t p;
1900 int status, error;
1901 uthread_t uth;
1902 struct _wait4_data *wait4_data;
1903
1904 AUDIT_ARG(pid, uap->pid);
1905
1906 if (uap->pid == 0) {
1907 uap->pid = -q->p_pgrpid;
1908 }
1909
1910 loop:
1911 proc_list_lock();
1912 loop1:
1913 nfound = 0;
1914 sibling_count = 0;
1915
1916 PCHILDREN_FOREACH(q, p) {
1917 if (p->p_sibling.le_next != 0) {
1918 sibling_count++;
1919 }
1920 if (uap->pid != WAIT_ANY &&
1921 p->p_pid != uap->pid &&
1922 p->p_pgrpid != -(uap->pid)) {
1923 continue;
1924 }
1925
1926 nfound++;
1927
1928 /* XXX This is racy because we don't get the lock!!!! */
1929
1930 if (p->p_listflag & P_LIST_WAITING) {
1931 /* we're not using a continuation here but we still need to stash
1932 * the args for stackshot. */
1933 uth = current_uthread();
1934 wait4_data = &uth->uu_save.uus_wait4_data;
1935 wait4_data->args = uap;
1936 thread_set_pending_block_hint(current_thread(), kThreadWaitOnProcess);
1937
1938 (void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1939 goto loop1;
1940 }
1941 p->p_listflag |= P_LIST_WAITING; /* only allow single thread to wait() */
1942
1943
1944 if (p->p_stat == SZOMB) {
1945 int reparentedtoinit = (p->p_listflag & P_LIST_DEADPARENT) ? 1 : 0;
1946
1947 proc_list_unlock();
1948 #if CONFIG_MACF
1949 if ((error = mac_proc_check_wait(q, p)) != 0) {
1950 goto out;
1951 }
1952 #endif
1953 retval[0] = p->p_pid;
1954 if (uap->status) {
1955 /* Legacy apps expect only 8 bits of status */
1956 status = 0xffff & p->p_xstat; /* convert to int */
1957 error = copyout((caddr_t)&status,
1958 uap->status,
1959 sizeof(status));
1960 if (error) {
1961 goto out;
1962 }
1963 }
1964 if (uap->rusage) {
1965 if (p->p_ru == NULL) {
1966 error = ENOMEM;
1967 } else {
1968 if (IS_64BIT_PROCESS(q)) {
1969 struct user64_rusage my_rusage = {};
1970 munge_user64_rusage(&p->p_ru->ru, &my_rusage);
1971 error = copyout((caddr_t)&my_rusage,
1972 uap->rusage,
1973 sizeof(my_rusage));
1974 } else {
1975 struct user32_rusage my_rusage = {};
1976 munge_user32_rusage(&p->p_ru->ru, &my_rusage);
1977 error = copyout((caddr_t)&my_rusage,
1978 uap->rusage,
1979 sizeof(my_rusage));
1980 }
1981 }
1982 /* information unavailable? */
1983 if (error) {
1984 goto out;
1985 }
1986 }
1987
1988 /* Conformance change for 6577252.
1989 * When SIGCHLD is blocked and wait() returns because the status
1990 * of a child process is available and there are no other
1991 * children processes, then any pending SIGCHLD signal is cleared.
1992 */
1993 if (sibling_count == 0) {
1994 int mask = sigmask(SIGCHLD);
1995 uth = current_uthread();
1996
1997 if ((uth->uu_sigmask & mask) != 0) {
1998 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1999 * This locking looks funny but it is protecting access to the
2000 * thread via p_uthlist.
2001 */
2002 proc_lock(q);
2003 uth->uu_siglist &= ~mask; /* clear pending signal */
2004 proc_unlock(q);
2005 }
2006 }
2007
2008 /* Clean up */
2009 (void)reap_child_locked(q, p, 0, reparentedtoinit, 0, 0);
2010
2011 return 0;
2012 }
2013 if (p->p_stat == SSTOP && (p->p_lflag & P_LWAITED) == 0 &&
2014 (p->p_lflag & P_LTRACED || uap->options & WUNTRACED)) {
2015 proc_list_unlock();
2016 #if CONFIG_MACF
2017 if ((error = mac_proc_check_wait(q, p)) != 0) {
2018 goto out;
2019 }
2020 #endif
2021 proc_lock(p);
2022 p->p_lflag |= P_LWAITED;
2023 proc_unlock(p);
2024 retval[0] = p->p_pid;
2025 if (uap->status) {
2026 status = W_STOPCODE(p->p_xstat);
2027 error = copyout((caddr_t)&status,
2028 uap->status,
2029 sizeof(status));
2030 } else {
2031 error = 0;
2032 }
2033 goto out;
2034 }
2035 /*
2036 * If we are waiting for continued processses, and this
2037 * process was continued
2038 */
2039 if ((uap->options & WCONTINUED) &&
2040 (p->p_flag & P_CONTINUED)) {
2041 proc_list_unlock();
2042 #if CONFIG_MACF
2043 if ((error = mac_proc_check_wait(q, p)) != 0) {
2044 goto out;
2045 }
2046 #endif
2047
2048 /* Prevent other process for waiting for this event */
2049 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
2050 retval[0] = p->p_pid;
2051 if (uap->status) {
2052 status = W_STOPCODE(SIGCONT);
2053 error = copyout((caddr_t)&status,
2054 uap->status,
2055 sizeof(status));
2056 } else {
2057 error = 0;
2058 }
2059 goto out;
2060 }
2061 p->p_listflag &= ~P_LIST_WAITING;
2062 wakeup(&p->p_stat);
2063 }
2064 /* list lock is held when we get here any which way */
2065 if (nfound == 0) {
2066 proc_list_unlock();
2067 return ECHILD;
2068 }
2069
2070 if (uap->options & WNOHANG) {
2071 retval[0] = 0;
2072 proc_list_unlock();
2073 return 0;
2074 }
2075
2076 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2077 uth = current_uthread();
2078 wait4_data = &uth->uu_save.uus_wait4_data;
2079 wait4_data->args = uap;
2080 wait4_data->retval = retval;
2081
2082 thread_set_pending_block_hint(current_thread(), kThreadWaitOnProcess);
2083 if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "wait", 0, wait1continue))) {
2084 return error;
2085 }
2086
2087 goto loop;
2088 out:
2089 proc_list_lock();
2090 p->p_listflag &= ~P_LIST_WAITING;
2091 wakeup(&p->p_stat);
2092 proc_list_unlock();
2093 return error;
2094 }
2095
2096 #if DEBUG
2097 #define ASSERT_LCK_MTX_OWNED(lock) \
2098 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
2099 #else
2100 #define ASSERT_LCK_MTX_OWNED(lock) /* nothing */
2101 #endif
2102
2103 int
2104 waitidcontinue(int result)
2105 {
2106 proc_t p;
2107 thread_t thread;
2108 uthread_t uth;
2109 struct _waitid_data *waitid_data;
2110 struct waitid_nocancel_args *uap;
2111 int *retval;
2112
2113 if (result) {
2114 return result;
2115 }
2116
2117 p = current_proc();
2118 thread = current_thread();
2119 uth = (struct uthread *)get_bsdthread_info(thread);
2120
2121 waitid_data = &uth->uu_save.uus_waitid_data;
2122 uap = waitid_data->args;
2123 retval = waitid_data->retval;
2124 return waitid_nocancel(p, uap, retval);
2125 }
2126
2127 /*
2128 * Description: Suspend the calling thread until one child of the process
2129 * containing the calling thread changes state.
2130 *
2131 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
2132 * uap->id pid_t or gid_t or ignored
2133 * uap->infop Address of siginfo_t struct in
2134 * user space into which to return status
2135 * uap->options flag values
2136 *
2137 * Returns: 0 Success
2138 * !0 Error returning status to user space
2139 */
2140 int
2141 waitid(proc_t q, struct waitid_args *uap, int32_t *retval)
2142 {
2143 __pthread_testcancel(1);
2144 return waitid_nocancel(q, (struct waitid_nocancel_args *)uap, retval);
2145 }
2146
2147 int
2148 waitid_nocancel(proc_t q, struct waitid_nocancel_args *uap,
2149 __unused int32_t *retval)
2150 {
2151 user_siginfo_t siginfo; /* siginfo data to return to caller */
2152 boolean_t caller64 = IS_64BIT_PROCESS(q);
2153 int nfound;
2154 proc_t p;
2155 int error;
2156 uthread_t uth;
2157 struct _waitid_data *waitid_data;
2158
2159 if (uap->options == 0 ||
2160 (uap->options & ~(WNOHANG | WNOWAIT | WCONTINUED | WSTOPPED | WEXITED))) {
2161 return EINVAL; /* bits set that aren't recognized */
2162 }
2163 switch (uap->idtype) {
2164 case P_PID: /* child with process ID equal to... */
2165 case P_PGID: /* child with process group ID equal to... */
2166 if (((int)uap->id) < 0) {
2167 return EINVAL;
2168 }
2169 break;
2170 case P_ALL: /* any child */
2171 break;
2172 }
2173
2174 loop:
2175 proc_list_lock();
2176 loop1:
2177 nfound = 0;
2178
2179 PCHILDREN_FOREACH(q, p) {
2180 switch (uap->idtype) {
2181 case P_PID: /* child with process ID equal to... */
2182 if (p->p_pid != (pid_t)uap->id) {
2183 continue;
2184 }
2185 break;
2186 case P_PGID: /* child with process group ID equal to... */
2187 if (p->p_pgrpid != (pid_t)uap->id) {
2188 continue;
2189 }
2190 break;
2191 case P_ALL: /* any child */
2192 break;
2193 }
2194
2195 /* XXX This is racy because we don't get the lock!!!! */
2196
2197 /*
2198 * Wait collision; go to sleep and restart; used to maintain
2199 * the single return for waited process guarantee.
2200 */
2201 if (p->p_listflag & P_LIST_WAITING) {
2202 (void) msleep(&p->p_stat, proc_list_mlock,
2203 PWAIT, "waitidcoll", 0);
2204 goto loop1;
2205 }
2206 p->p_listflag |= P_LIST_WAITING; /* mark busy */
2207
2208 nfound++;
2209
2210 bzero(&siginfo, sizeof(siginfo));
2211
2212 switch (p->p_stat) {
2213 case SZOMB: /* Exited */
2214 if (!(uap->options & WEXITED)) {
2215 break;
2216 }
2217 proc_list_unlock();
2218 #if CONFIG_MACF
2219 if ((error = mac_proc_check_wait(q, p)) != 0) {
2220 goto out;
2221 }
2222 #endif
2223 siginfo.si_signo = SIGCHLD;
2224 siginfo.si_pid = p->p_pid;
2225
2226 /* If the child terminated abnormally due to a signal, the signum
2227 * needs to be preserved in the exit status.
2228 */
2229 if (WIFSIGNALED(p->p_xstat)) {
2230 siginfo.si_code = WCOREDUMP(p->p_xstat) ?
2231 CLD_DUMPED : CLD_KILLED;
2232 siginfo.si_status = WTERMSIG(p->p_xstat);
2233 } else {
2234 siginfo.si_code = CLD_EXITED;
2235 siginfo.si_status = WEXITSTATUS(p->p_xstat) & 0x00FFFFFF;
2236 }
2237 siginfo.si_status |= (((uint32_t)(p->p_xhighbits) << 24) & 0xFF000000);
2238 p->p_xhighbits = 0;
2239
2240 if ((error = copyoutsiginfo(&siginfo,
2241 caller64, uap->infop)) != 0) {
2242 goto out;
2243 }
2244
2245 /* Prevent other process for waiting for this event? */
2246 if (!(uap->options & WNOWAIT)) {
2247 (void) reap_child_locked(q, p, 0, 0, 0, 0);
2248 return 0;
2249 }
2250 goto out;
2251
2252 case SSTOP: /* Stopped */
2253 /*
2254 * If we are not interested in stopped processes, then
2255 * ignore this one.
2256 */
2257 if (!(uap->options & WSTOPPED)) {
2258 break;
2259 }
2260
2261 /*
2262 * If someone has already waited it, we lost a race
2263 * to be the one to return status.
2264 */
2265 if ((p->p_lflag & P_LWAITED) != 0) {
2266 break;
2267 }
2268 proc_list_unlock();
2269 #if CONFIG_MACF
2270 if ((error = mac_proc_check_wait(q, p)) != 0) {
2271 goto out;
2272 }
2273 #endif
2274 siginfo.si_signo = SIGCHLD;
2275 siginfo.si_pid = p->p_pid;
2276 siginfo.si_status = p->p_xstat; /* signal number */
2277 siginfo.si_code = CLD_STOPPED;
2278
2279 if ((error = copyoutsiginfo(&siginfo,
2280 caller64, uap->infop)) != 0) {
2281 goto out;
2282 }
2283
2284 /* Prevent other process for waiting for this event? */
2285 if (!(uap->options & WNOWAIT)) {
2286 proc_lock(p);
2287 p->p_lflag |= P_LWAITED;
2288 proc_unlock(p);
2289 }
2290 goto out;
2291
2292 default: /* All other states => Continued */
2293 if (!(uap->options & WCONTINUED)) {
2294 break;
2295 }
2296
2297 /*
2298 * If the flag isn't set, then this process has not
2299 * been stopped and continued, or the status has
2300 * already been reaped by another caller of waitid().
2301 */
2302 if ((p->p_flag & P_CONTINUED) == 0) {
2303 break;
2304 }
2305 proc_list_unlock();
2306 #if CONFIG_MACF
2307 if ((error = mac_proc_check_wait(q, p)) != 0) {
2308 goto out;
2309 }
2310 #endif
2311 siginfo.si_signo = SIGCHLD;
2312 siginfo.si_code = CLD_CONTINUED;
2313 proc_lock(p);
2314 siginfo.si_pid = p->p_contproc;
2315 siginfo.si_status = p->p_xstat;
2316 proc_unlock(p);
2317
2318 if ((error = copyoutsiginfo(&siginfo,
2319 caller64, uap->infop)) != 0) {
2320 goto out;
2321 }
2322
2323 /* Prevent other process for waiting for this event? */
2324 if (!(uap->options & WNOWAIT)) {
2325 OSBitAndAtomic(~((uint32_t)P_CONTINUED),
2326 &p->p_flag);
2327 }
2328 goto out;
2329 }
2330 ASSERT_LCK_MTX_OWNED(proc_list_mlock);
2331
2332 /* Not a process we are interested in; go on to next child */
2333
2334 p->p_listflag &= ~P_LIST_WAITING;
2335 wakeup(&p->p_stat);
2336 }
2337 ASSERT_LCK_MTX_OWNED(proc_list_mlock);
2338
2339 /* No child processes that could possibly satisfy the request? */
2340
2341 if (nfound == 0) {
2342 proc_list_unlock();
2343 return ECHILD;
2344 }
2345
2346 if (uap->options & WNOHANG) {
2347 proc_list_unlock();
2348 #if CONFIG_MACF
2349 if ((error = mac_proc_check_wait(q, p)) != 0) {
2350 return error;
2351 }
2352 #endif
2353 /*
2354 * The state of the siginfo structure in this case
2355 * is undefined. Some implementations bzero it, some
2356 * (like here) leave it untouched for efficiency.
2357 *
2358 * Thus the most portable check for "no matching pid with
2359 * WNOHANG" is to store a zero into si_pid before
2360 * invocation, then check for a non-zero value afterwards.
2361 */
2362 return 0;
2363 }
2364
2365 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2366 uth = current_uthread();
2367 waitid_data = &uth->uu_save.uus_waitid_data;
2368 waitid_data->args = uap;
2369 waitid_data->retval = retval;
2370
2371 if ((error = msleep0(q, proc_list_mlock,
2372 PWAIT | PCATCH | PDROP, "waitid", 0, waitidcontinue)) != 0) {
2373 return error;
2374 }
2375
2376 goto loop;
2377 out:
2378 proc_list_lock();
2379 p->p_listflag &= ~P_LIST_WAITING;
2380 wakeup(&p->p_stat);
2381 proc_list_unlock();
2382 return error;
2383 }
2384
2385 /*
2386 * make process 'parent' the new parent of process 'child'.
2387 */
2388 void
2389 proc_reparentlocked(proc_t child, proc_t parent, int signallable, int locked)
2390 {
2391 proc_t oldparent = PROC_NULL;
2392
2393 if (child->p_pptr == parent) {
2394 return;
2395 }
2396
2397 if (locked == 0) {
2398 proc_list_lock();
2399 }
2400
2401 oldparent = child->p_pptr;
2402 #if __PROC_INTERNAL_DEBUG
2403 if (oldparent == PROC_NULL) {
2404 panic("proc_reparent: process %p does not have a parent\n", child);
2405 }
2406 #endif
2407
2408 LIST_REMOVE(child, p_sibling);
2409 #if __PROC_INTERNAL_DEBUG
2410 if (oldparent->p_childrencnt == 0) {
2411 panic("process children count already 0\n");
2412 }
2413 #endif
2414 oldparent->p_childrencnt--;
2415 #if __PROC_INTERNAL_DEBUG
2416 if (oldparent->p_childrencnt < 0) {
2417 panic("process children count -ve\n");
2418 }
2419 #endif
2420 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
2421 parent->p_childrencnt++;
2422 child->p_pptr = parent;
2423 child->p_ppid = parent->p_pid;
2424
2425 proc_list_unlock();
2426
2427 if ((signallable != 0) && (initproc == parent) && (child->p_stat == SZOMB)) {
2428 psignal(initproc, SIGCHLD);
2429 }
2430 if (locked == 1) {
2431 proc_list_lock();
2432 }
2433 }
2434
2435 /*
2436 * Exit: deallocate address space and other resources, change proc state
2437 * to zombie, and unlink proc from allproc and parent's lists. Save exit
2438 * status and rusage for wait(). Check for child processes and orphan them.
2439 */
2440
2441 void
2442 vfork_exit(proc_t p, int rv)
2443 {
2444 vfork_exit_internal(p, rv, 0);
2445 }
2446
2447 void
2448 vfork_exit_internal(proc_t p, int rv, int forceexit)
2449 {
2450 thread_t self = current_thread();
2451 #ifdef FIXME
2452 struct task *task = p->task;
2453 #endif
2454 struct uthread *ut;
2455
2456 /*
2457 * If a thread in this task has already
2458 * called exit(), then halt any others
2459 * right here.
2460 */
2461
2462 ut = get_bsdthread_info(self);
2463
2464
2465 proc_lock(p);
2466 if ((p->p_lflag & P_LPEXIT) == P_LPEXIT) {
2467 /*
2468 * This happens when a parent exits/killed and vfork is in progress
2469 * other threads. But shutdown code for ex has already called exit1()
2470 */
2471 proc_unlock(p);
2472 return;
2473 }
2474 p->p_lflag |= (P_LEXIT | P_LPEXIT);
2475 proc_unlock(p);
2476
2477 if (forceexit == 0) {
2478 /*
2479 * parent of a vfork child has already called exit() and the
2480 * thread that has vfork in proress terminates. So there is no
2481 * separate address space here and it has already been marked for
2482 * termination. This was never covered before and could cause problems
2483 * if we block here for outside code.
2484 */
2485 /* Notify the perf server */
2486 (void)sys_perf_notify(self, p->p_pid);
2487 }
2488
2489 /*
2490 * Remove proc from allproc queue and from pidhash chain.
2491 * Need to do this before we do anything that can block.
2492 * Not doing causes things like mount() find this on allproc
2493 * in partially cleaned state.
2494 */
2495
2496 proc_list_lock();
2497
2498 #if CONFIG_MEMORYSTATUS
2499 proc_memorystatus_remove(p);
2500 #endif
2501
2502 LIST_REMOVE(p, p_list);
2503 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
2504 /* will not be visible via proc_find */
2505 p->p_listflag |= P_LIST_EXITED;
2506
2507 proc_list_unlock();
2508
2509 proc_lock(p);
2510 p->p_xstat = rv;
2511 p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
2512 p->p_sigignore = ~0;
2513 proc_unlock(p);
2514
2515 ut->uu_siglist = 0;
2516
2517 /* begin vproc_exit */
2518
2519 proc_t q;
2520 proc_t pp;
2521
2522 vnode_t tvp;
2523
2524 struct pgrp * pg;
2525 struct session *sessp;
2526 struct rusage_superset *rup;
2527
2528 rup = zalloc(zombie_zone);
2529
2530 proc_refdrain(p);
2531
2532 /*
2533 * Close open files and release open-file table.
2534 * This may block!
2535 */
2536 fdfree(p);
2537
2538 sessp = proc_session(p);
2539 if (SESS_LEADER(p, sessp)) {
2540 panic("vfork child is session leader");
2541 }
2542 session_rele(sessp);
2543
2544 pg = proc_pgrp(p);
2545 fixjobc(p, pg, 0);
2546 pg_rele(pg);
2547
2548 /*
2549 * Change RLIMIT_FSIZE for accounting/debugging. proc_limitsetcur_internal() will COW the current plimit
2550 * before making changes if the current plimit is shared. The COW'ed plimit will be freed
2551 * below by calling proc_limitdrop().
2552 */
2553 proc_limitsetcur_internal(p, RLIMIT_FSIZE, RLIM_INFINITY);
2554
2555 proc_list_lock();
2556
2557 proc_childdrainstart(p);
2558 while ((q = p->p_children.lh_first) != NULL) {
2559 if (q->p_stat == SZOMB) {
2560 if (p != q->p_pptr) {
2561 panic("parent child linkage broken");
2562 }
2563 /* check for lookups by zomb sysctl */
2564 while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
2565 msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
2566 }
2567 q->p_listflag |= P_LIST_WAITING;
2568 /*
2569 * This is a named reference and it is not granted
2570 * if the reap is already in progress. So we get
2571 * the reference here exclusively and their can be
2572 * no waiters. So there is no need for a wakeup
2573 * after we are done. AlsO the reap frees the structure
2574 * and the proc struct cannot be used for wakeups as well.
2575 * It is safe to use q here as this is system reap
2576 */
2577 (void)reap_child_locked(p, q, 1, 0, 1, 0);
2578 } else {
2579 /*
2580 * Traced processes are killed
2581 * since their existence means someone is messing up.
2582 */
2583 if (q->p_lflag & P_LTRACED) {
2584 struct proc *opp;
2585
2586 proc_list_unlock();
2587
2588 opp = proc_find(q->p_oppid);
2589 if (opp != PROC_NULL) {
2590 proc_list_lock();
2591 q->p_oppid = 0;
2592 proc_list_unlock();
2593 proc_reparentlocked(q, opp, 0, 0);
2594 proc_rele(opp);
2595 } else {
2596 /* original parent exited while traced */
2597 proc_list_lock();
2598 q->p_listflag |= P_LIST_DEADPARENT;
2599 q->p_oppid = 0;
2600 proc_list_unlock();
2601 proc_reparentlocked(q, initproc, 0, 0);
2602 }
2603
2604 proc_lock(q);
2605 q->p_lflag &= ~P_LTRACED;
2606
2607 if (q->sigwait_thread) {
2608 thread_t thread = q->sigwait_thread;
2609
2610 proc_unlock(q);
2611 /*
2612 * The sigwait_thread could be stopped at a
2613 * breakpoint. Wake it up to kill.
2614 * Need to do this as it could be a thread which is not
2615 * the first thread in the task. So any attempts to kill
2616 * the process would result into a deadlock on q->sigwait.
2617 */
2618 thread_resume(thread);
2619 clear_wait(thread, THREAD_INTERRUPTED);
2620 threadsignal(thread, SIGKILL, 0, TRUE);
2621 } else {
2622 proc_unlock(q);
2623 }
2624
2625 psignal(q, SIGKILL);
2626 proc_list_lock();
2627 } else {
2628 q->p_listflag |= P_LIST_DEADPARENT;
2629 proc_reparentlocked(q, initproc, 0, 1);
2630 }
2631 }
2632 }
2633
2634 proc_childdrainend(p);
2635 proc_list_unlock();
2636
2637 /*
2638 * Release reference to text vnode
2639 */
2640 tvp = p->p_textvp;
2641 p->p_textvp = NULL;
2642 if (tvp != NULLVP) {
2643 vnode_rele(tvp);
2644 }
2645
2646 /*
2647 * Save exit status and final rusage info, adding in child rusage
2648 * info and self times. If we were unable to allocate a zombie
2649 * structure, this information is lost.
2650 */
2651 if (rup != NULL) {
2652 rup->ru = p->p_stats->p_ru;
2653 timerclear(&rup->ru.ru_utime);
2654 timerclear(&rup->ru.ru_stime);
2655
2656 #ifdef FIXME
2657 if (task) {
2658 mach_task_basic_info_data_t tinfo;
2659 task_thread_times_info_data_t ttimesinfo;
2660 int task_info_stuff, task_ttimes_stuff;
2661 struct timeval ut, st;
2662
2663 task_info_stuff = MACH_TASK_BASIC_INFO_COUNT;
2664 task_info(task, MACH_TASK_BASIC_INFO,
2665 &tinfo, &task_info_stuff);
2666 p->p_ru->ru.ru_utime.tv_sec = tinfo.user_time.seconds;
2667 p->p_ru->ru.ru_utime.tv_usec = tinfo.user_time.microseconds;
2668 p->p_ru->ru.ru_stime.tv_sec = tinfo.system_time.seconds;
2669 p->p_ru->ru.ru_stime.tv_usec = tinfo.system_time.microseconds;
2670
2671 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
2672 task_info(task, TASK_THREAD_TIMES_INFO,
2673 &ttimesinfo, &task_ttimes_stuff);
2674
2675 ut.tv_sec = ttimesinfo.user_time.seconds;
2676 ut.tv_usec = ttimesinfo.user_time.microseconds;
2677 st.tv_sec = ttimesinfo.system_time.seconds;
2678 st.tv_usec = ttimesinfo.system_time.microseconds;
2679 timeradd(&ut, &p->p_ru->ru.ru_utime, &p->p_ru->ru.ru_utime);
2680 timeradd(&st, &p->p_ru->ru.ru_stime, &p->p_ru->ru.ru_stime);
2681 }
2682 #endif /* FIXME */
2683
2684 ruadd(&rup->ru, &p->p_stats->p_cru);
2685
2686 gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT);
2687 rup->ri.ri_phys_footprint = 0;
2688 rup->ri.ri_proc_exit_abstime = mach_absolute_time();
2689
2690 /*
2691 * Now that we have filled in the rusage info, make it
2692 * visible to an external observer via proc_pid_rusage().
2693 */
2694 p->p_ru = rup;
2695 }
2696
2697 /*
2698 * Free up profiling buffers.
2699 */
2700 {
2701 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
2702
2703 p1 = p0->pr_next;
2704 p0->pr_next = NULL;
2705 p0->pr_scale = 0;
2706
2707 for (; p1 != NULL; p1 = pn) {
2708 pn = p1->pr_next;
2709 kfree(p1, sizeof *p1);
2710 }
2711 }
2712
2713 #if PSYNCH
2714 pth_proc_hashdelete(p);
2715 #endif /* PSYNCH */
2716
2717 proc_free_realitimer(p);
2718
2719 /*
2720 * Other substructures are freed from wait().
2721 */
2722 zfree(proc_stats_zone, p->p_stats);
2723 p->p_stats = NULL;
2724
2725 zfree(proc_sigacts_zone, p->p_sigacts);
2726 p->p_sigacts = NULL;
2727
2728 FREE(p->p_subsystem_root_path, M_SBUF);
2729 p->p_subsystem_root_path = NULL;
2730
2731 proc_limitdrop(p);
2732
2733 /*
2734 * Finish up by terminating the task
2735 * and halt this thread (only if a
2736 * member of the task exiting).
2737 */
2738 p->task = TASK_NULL;
2739
2740 /*
2741 * Notify parent that we're gone.
2742 */
2743 pp = proc_parent(p);
2744 if ((p->p_listflag & P_LIST_DEADPARENT) == 0) {
2745 if (pp != initproc) {
2746 proc_lock(pp);
2747 pp->si_pid = p->p_pid;
2748 pp->p_xhighbits = p->p_xhighbits;
2749 p->p_xhighbits = 0;
2750 pp->si_status = p->p_xstat;
2751 pp->si_code = CLD_EXITED;
2752 /*
2753 * p_ucred usage is safe as it is an exiting process
2754 * and reference is dropped in reap
2755 */
2756 pp->si_uid = kauth_cred_getruid(p->p_ucred);
2757 proc_unlock(pp);
2758 }
2759 /* mark as a zombie */
2760 /* mark as a zombie */
2761 /* No need to take proc lock as all refs are drained and
2762 * no one except parent (reaping ) can look at this.
2763 * The write is to an int and is coherent. Also parent is
2764 * keyed off of list lock for reaping
2765 */
2766 p->p_stat = SZOMB;
2767
2768 psignal(pp, SIGCHLD);
2769
2770 /* and now wakeup the parent */
2771 proc_list_lock();
2772 wakeup((caddr_t)pp);
2773 proc_list_unlock();
2774 } else {
2775 proc_list_lock();
2776 /* check for lookups by zomb sysctl */
2777 while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
2778 msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
2779 }
2780 p->p_stat = SZOMB;
2781 p->p_listflag |= P_LIST_WAITING;
2782
2783 /*
2784 * This is a named reference and it is not granted
2785 * if the reap is already in progress. So we get
2786 * the reference here exclusively and their can be
2787 * no waiters. So there is no need for a wakeup
2788 * after we are done. AlsO the reap frees the structure
2789 * and the proc struct cannot be used for wakeups as well.
2790 * It is safe to use p here as this is system reap
2791 */
2792 (void)reap_child_locked(pp, p, 0, 0, 1, 1);
2793 /* list lock dropped by reap_child_locked */
2794 }
2795 proc_rele(pp);
2796 }
2797
2798
2799 /*
2800 * munge_rusage
2801 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
2802 * process. We munge the kernel version of rusage into the
2803 * 64 bit version.
2804 */
2805 __private_extern__ void
2806 munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p)
2807 {
2808 /* Zero-out struct so that padding is cleared */
2809 bzero(a_user_rusage_p, sizeof(struct user64_rusage));
2810
2811 /* timeval changes size, so utime and stime need special handling */
2812 a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
2813 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
2814 a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
2815 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
2816 /*
2817 * everything else can be a direct assign, since there is no loss
2818 * of precision implied boing 32->64.
2819 */
2820 a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
2821 a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
2822 a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
2823 a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
2824 a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
2825 a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
2826 a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
2827 a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
2828 a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
2829 a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
2830 a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
2831 a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
2832 a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
2833 a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
2834 }
2835
2836 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2837 __private_extern__ void
2838 munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p)
2839 {
2840 bzero(a_user_rusage_p, sizeof(struct user32_rusage));
2841
2842 /* timeval changes size, so utime and stime need special handling */
2843 a_user_rusage_p->ru_utime.tv_sec = (user32_time_t)a_rusage_p->ru_utime.tv_sec;
2844 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
2845 a_user_rusage_p->ru_stime.tv_sec = (user32_time_t)a_rusage_p->ru_stime.tv_sec;
2846 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
2847 /*
2848 * everything else can be a direct assign. We currently ignore
2849 * the loss of precision
2850 */
2851 a_user_rusage_p->ru_maxrss = (user32_long_t)a_rusage_p->ru_maxrss;
2852 a_user_rusage_p->ru_ixrss = (user32_long_t)a_rusage_p->ru_ixrss;
2853 a_user_rusage_p->ru_idrss = (user32_long_t)a_rusage_p->ru_idrss;
2854 a_user_rusage_p->ru_isrss = (user32_long_t)a_rusage_p->ru_isrss;
2855 a_user_rusage_p->ru_minflt = (user32_long_t)a_rusage_p->ru_minflt;
2856 a_user_rusage_p->ru_majflt = (user32_long_t)a_rusage_p->ru_majflt;
2857 a_user_rusage_p->ru_nswap = (user32_long_t)a_rusage_p->ru_nswap;
2858 a_user_rusage_p->ru_inblock = (user32_long_t)a_rusage_p->ru_inblock;
2859 a_user_rusage_p->ru_oublock = (user32_long_t)a_rusage_p->ru_oublock;
2860 a_user_rusage_p->ru_msgsnd = (user32_long_t)a_rusage_p->ru_msgsnd;
2861 a_user_rusage_p->ru_msgrcv = (user32_long_t)a_rusage_p->ru_msgrcv;
2862 a_user_rusage_p->ru_nsignals = (user32_long_t)a_rusage_p->ru_nsignals;
2863 a_user_rusage_p->ru_nvcsw = (user32_long_t)a_rusage_p->ru_nvcsw;
2864 a_user_rusage_p->ru_nivcsw = (user32_long_t)a_rusage_p->ru_nivcsw;
2865 }
2866
2867 void
2868 kdp_wait4_find_process(thread_t thread, __unused event64_t wait_event, thread_waitinfo_t *waitinfo)
2869 {
2870 assert(thread != NULL);
2871 assert(waitinfo != NULL);
2872
2873 struct uthread *ut = get_bsdthread_info(thread);
2874 waitinfo->context = 0;
2875 // ensure wmesg is consistent with a thread waiting in wait4
2876 assert(!strcmp(ut->uu_wmesg, "waitcoll") || !strcmp(ut->uu_wmesg, "wait"));
2877 struct wait4_nocancel_args *args = ut->uu_save.uus_wait4_data.args;
2878 // May not actually contain a pid; this is just the argument to wait4.
2879 // See man wait4 for other valid wait4 arguments.
2880 waitinfo->owner = args->pid;
2881 }
2882
2883 #if __has_feature(ptrauth_calls)
2884 int
2885 exit_with_pac_exception(proc_t p, exception_type_t exception, mach_exception_code_t code,
2886 mach_exception_subcode_t subcode)
2887 {
2888 thread_t self = current_thread();
2889 struct uthread *ut = get_bsdthread_info(self);
2890
2891 os_reason_t exception_reason = os_reason_create(OS_REASON_PAC_EXCEPTION, (uint64_t)code);
2892 assert(exception_reason != OS_REASON_NULL);
2893 exception_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
2894 ut->uu_exception = exception;
2895 ut->uu_code = code;
2896 ut->uu_subcode = subcode;
2897
2898 return exit_with_reason(p, W_EXITCODE(0, SIGKILL), (int *)NULL, TRUE, FALSE,
2899 0, exception_reason);
2900 }
2901 #endif /* __has_feature(ptrauth_calls) */