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