2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
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.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
66 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
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,
75 #include <machine/reg.h>
76 #include <machine/psl.h>
77 #include <stdatomic.h>
79 #include "compat_43.h"
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/ioctl.h>
84 #include <sys/proc_internal.h>
86 #include <sys/kauth.h>
89 #include <sys/resource.h>
90 #include <sys/kernel.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 */
112 #include <security/audit/audit.h>
113 #include <bsm/audit_kevents.h>
115 #include <mach/mach_types.h>
116 #include <mach/task.h>
117 #include <mach/thread_act.h>
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>
132 #include <vm/vm_protos.h>
135 #include <pexpert/pexpert.h>
138 #include <sys/shm_internal.h> /* shmexit */
139 #endif /* SYSV_SHM */
141 #include <sys/persona.h>
142 #endif /* CONFIG_PERSONAS */
143 #if CONFIG_MEMORYSTATUS
144 #include <sys/kern_memorystatus.h>
145 #endif /* CONFIG_MEMORYSTATUS */
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 */
152 #include <security/mac_framework.h>
153 #include <security/mac_mach_internal.h>
154 #include <sys/syscall.h>
155 #endif /* CONFIG_MACF */
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
);
182 ZONE_DECLARE(zombie_zone
, "zombie",
183 sizeof(struct rusage_superset
), ZC_NOENCRYPT
);
187 * Things which should have prototypes in headers, but don't
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 *);
197 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
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) */
206 * NOTE: Source and target may *NOT* overlap!
207 * XXX Should share code with bsd/dev/ppc/unix_signal.c
210 siginfo_user_to_user32(user_siginfo_t
*in
, user32_siginfo_t
*out
)
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 */
225 siginfo_user_to_user64(user_siginfo_t
*in
, user64_siginfo_t
*out
)
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 */
240 copyoutsiginfo(user_siginfo_t
*native
, boolean_t is64
, user_addr_t uaddr
)
243 user64_siginfo_t sinfo64
;
245 bzero(&sinfo64
, sizeof(sinfo64
));
246 siginfo_user_to_user64(native
, &sinfo64
);
247 return copyout(&sinfo64
, uaddr
, sizeof(sinfo64
));
249 user32_siginfo_t sinfo32
;
251 bzero(&sinfo32
, sizeof(sinfo32
));
252 siginfo_user_to_user32(native
, &sinfo32
);
253 return copyout(&sinfo32
, uaddr
, sizeof(sinfo32
));
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
)
262 struct rusage_superset rup
;
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
);
271 proc_update_corpse_exception_codes(proc_t p
, mach_exception_data_type_t
*code
, mach_exception_data_type_t
*subcode
)
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
) {
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));
296 *subcode
= subcode_update
;
300 mach_exception_data_type_t
301 proc_encode_exit_exception_code(proc_t p
)
303 uint64_t subcode
= 0;
305 if (p
->p_exit_reason
== OS_REASON_NULL
) {
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
;
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
)
320 mach_vm_address_t uaddr
= 0;
321 mach_exception_data_type_t exc_codes
[EXCEPTION_CODE_MAX
];
323 exc_codes
[1] = subcode
;
325 struct proc_uniqidentifierinfo p_uniqidinfo
;
326 struct proc_workqueueinfo pwqinfo
;
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
;
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
;
355 void *crash_info_ptr
= task_get_corpseinfo(corpse_task
);
357 #if CONFIG_MEMORYSTATUS
358 int memstat_dirty_flags
= 0;
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
));
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
));
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
));
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));
380 static_assert(sizeof(struct proc_uniqidentifierinfo
) == sizeof(struct crashinfo_proc_uniqidentifierinfo
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
);
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
));
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
));
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
));
438 cputype
= cpu_type() & ~CPU_ARCH_MASK
;
439 if (IS_64BIT_PROCESS(p
)) {
440 cputype
|= CPU_ARCH_ABI64
;
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
519 bzero(&pwqinfo
, sizeof(struct proc_workqueueinfo
));
520 retval
= fill_procworkqueue(p
, &pwqinfo
);
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
));
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
));
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
));
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
));
542 #endif /* CONFIG_COALITIONS */
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
599 if (p
->p_exit_reason
!= OS_REASON_NULL
&& reason
== OS_REASON_NULL
) {
600 reason
= p
->p_exit_reason
;
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
610 kcdata_memcpy(crash_info_ptr
, uaddr
, &ers
, sizeof(ers
));
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);
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
);
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
);
632 * We only parse exit reason kcdata blobs for launchd when it dies
633 * and we're going to panic.
635 * Meant to be called immediately before panicking.
638 launchd_exit_reason_get_string_desc(os_reason_t exit_reason
)
642 if (exit_reason
== OS_REASON_NULL
|| exit_reason
->osr_kcd_buf
== NULL
||
643 exit_reason
->osr_bufsize
== 0) {
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");
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
));
663 iter
= kcdata_iter_find_type(iter
, EXIT_REASON_USER_DESC
);
664 if (!kcdata_iter_valid(iter
)) {
668 return (char *)kcdata_iter_payload(iter
);
673 launchd_crashed_panic(proc_t p
, int rv
)
675 char *launchd_exit_reason_desc
= launchd_exit_reason_get_string_desc(p
->p_exit_reason
);
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
));
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");
686 const char *launchd_crashed_prefix_str
;
688 if (strnstr(p
->p_name
, "preinit", sizeof(p
->p_name
))) {
689 launchd_crashed_prefix_str
= "LTE preinit process exited";
691 launchd_crashed_prefix_str
= "initproc exited";
694 #if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
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.
703 uint64_t coredump_start
= mach_absolute_time();
704 uint64_t coredump_end
;
706 clock_usec_t tv_usec
;
710 err
= coredump(p
, 300, COREDUMP_IGNORE_ULIMIT
| COREDUMP_FULLFSYNC
);
712 coredump_end
= mach_absolute_time();
714 absolutetime_to_microtime(coredump_end
- coredump_start
, &tv_sec
, &tv_usec
);
716 tv_msec
= tv_usec
/ 1000;
719 printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
720 err
, (uint32_t)tv_sec
, tv_msec
);
722 printf("Generated initproc core file in %d.%03d seconds\n",
723 (uint32_t)tv_sec
, tv_msec
);
725 #endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
727 sync(p
, (void *)NULL
, (int *)NULL
);
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" : ""));
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");
740 #define OS_REASON_IFLAG_USER_FAULT 0x1
742 #define OS_REASON_TOTAL_USER_FAULTS_PER_PROC 5
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
)
751 os_reason_t exit_reason
= OS_REASON_NULL
;
752 kern_return_t kr
= KERN_SUCCESS
;
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
);
758 if (old_value
>= OS_REASON_TOTAL_USER_FAULTS_PER_PROC
) {
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
)) {
770 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
771 p
->p_pid
, reason_namespace
,
774 exit_reason
= build_userspace_exit_reason(reason_namespace
, reason_code
,
775 payload
, payload_size
, reason_string
, reason_flags
| OS_REASON_FLAG_ABORT
);
777 if (internal_flags
& OS_REASON_IFLAG_USER_FAULT
) {
778 mach_exception_code_t code
= 0;
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
);
784 if (exit_reason
== OS_REASON_NULL
) {
785 kr
= KERN_RESOURCE_SHORTAGE
;
787 kr
= task_violated_guard(code
, reason_code
, exit_reason
);
789 os_reason_free(exit_reason
);
792 * We use SIGABRT (rather than calling exit directly from here) so that
793 * the debugger can catch abort_with_{reason,payload} calls.
795 psignal_try_thread_with_reason(p
, current_thread(), SIGABRT
, exit_reason
);
801 case KERN_NOT_SUPPORTED
:
803 case KERN_INVALID_ARGUMENT
:
805 case KERN_RESOURCE_SHORTAGE
:
812 abort_with_payload(struct proc
*cur_proc
, struct abort_with_payload_args
*args
,
813 __unused
void *retval
)
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);
823 os_fault_with_payload(struct proc
*cur_proc
,
824 struct os_fault_with_payload_args
*args
, __unused
int *retval
)
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
);
836 __attribute__((noreturn
))
838 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
840 p
->p_xhighbits
= ((uint32_t)(uap
->rval
) & 0xFF000000) >> 24;
841 exit1(p
, W_EXITCODE((uint32_t)uap
->rval
, 0), retval
);
843 thread_exception_return();
846 thread_block(THREAD_CONTINUE_NULL
);
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.
857 exit1(proc_t p
, int rv
, int *retval
)
859 return exit1_internal(p
, rv
, retval
, TRUE
, TRUE
, 0);
863 exit1_internal(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
866 return exit_with_reason(p
, rv
, retval
, thread_can_terminate
, perf_notify
, jetsam_flags
, OS_REASON_NULL
);
870 * NOTE: exit_with_reason drops a reference on the passed exit_reason
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
)
876 thread_t self
= current_thread();
877 struct task
*task
= p
->task
;
882 * If a thread in this task has already
883 * called exit(), then halt any others
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
) {
896 vfork_return(p
, retval
, p
->p_pid
);
897 unix_syscall_return(0);
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().
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.
913 AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0);
915 * TODO: what to audit here when jetsam calls exit and the uthread,
916 * 'ut' does not belong to the proc, 'p'.
918 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
920 DTRACE_PROC1(exit
, int, CLD_EXITED
);
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
);
928 error
= proc_transstart(p
, 1, (jetsam_flags
? 1 : 0));
929 if (error
== EDEADLK
) {
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.
936 os_reason_free(exit_reason
);
937 if (current_proc() == p
) {
938 if (p
->exit_thread
== self
) {
939 panic("exit_thread failed to exit");
942 if (thread_can_terminate
) {
943 thread_exception_return();
950 while (p
->exit_thread
!= self
) {
951 if (sig_try_locked(p
) <= 0) {
953 os_reason_free(exit_reason
);
955 if (get_threadtask(self
) != task
) {
961 thread_terminate(self
);
962 if (!thread_can_terminate
) {
966 thread_exception_return();
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);
978 assert(p
->p_exit_reason
== OS_REASON_NULL
);
979 p
->p_exit_reason
= exit_reason
;
981 p
->p_lflag
|= P_LEXIT
;
983 p
->p_lflag
|= jetsam_flags
;
988 proc_prepareexit(p
, rv
, perf_notify
);
990 /* Last thread to terminate will call proc_exit() */
991 task_terminate_internal(task
);
996 #if CONFIG_MEMORYSTATUS
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.
1005 proc_memorystatus_remove(proc_t p
)
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
);
1016 proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
)
1018 mach_exception_data_type_t code
= 0, subcode
= 0;
1021 thread_t self
= current_thread();
1022 ut
= get_bsdthread_info(self
);
1023 struct rusage_superset
*rup
;
1025 int create_corpse
= FALSE
;
1027 if (p
== initproc
) {
1028 launchd_crashed_panic(p
, rv
);
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
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.
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
)))) {
1048 * Workaround for processes checking up on PT_DENY_ATTACH:
1049 * should be backed out post-Leopard (details in 5431025).
1051 if ((SIGSEGV
== WTERMSIG(rv
)) &&
1052 (p
->p_pptr
->p_lflag
& P_LNOATTACH
)) {
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.
1061 code
= ((WTERMSIG(rv
) & 0xff) << 24) |
1062 ((ut
->uu_exception
& 0x0f) << 20) |
1063 ((int)ut
->uu_code
& 0xfffff);
1064 subcode
= ut
->uu_subcode
;
1066 kr
= task_exception_notify(EXC_CRASH
, code
, subcode
);
1068 /* Nobody handled EXC_CRASH?? remember to make corpse */
1070 create_corpse
= TRUE
;
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.
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.
1084 task_t task
= proc_task(p
);
1089 unsigned int frame_count
= backtrace_user(bt
, 2, &bt_err
, &user64
, &was_truncated
);
1091 if (bt_err
== 0 && frame_count
>= 1) {
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.
1098 kr
= revalidate_text_page(task
, bt
[0]);
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
));
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]);
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.
1119 if (kr
!= KERN_SUCCESS
) {
1120 os_log(OS_LOG_DEFAULT
,
1121 "Text page corruption detected in dying process %d\n", p
->p_pid
);
1127 /* Notify the perf server? */
1129 (void)sys_perf_notify(self
, p
->p_pid
);
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
);
1142 printf("Process[%d] crashed: %s. Too many corpses being created.\n", p
->p_pid
, p
->p_comm
);
1144 create_corpse
= FALSE
;
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().
1153 * If the zombie allocation fails, just punt the stats.
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();
1160 * Make the rusage_info visible to external observers
1161 * only after it has been completely filled in.
1165 if (create_corpse
) {
1166 int est_knotes
= 0, num_knotes
= 0;
1167 uint64_t *buffer
= NULL
;
1168 uint32_t buf_size
= 0;
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;
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
);
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.
1198 #if CONFIG_MEMORYSTATUS
1199 proc_memorystatus_remove(p
);
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
;
1213 * If parent is waiting for us to exit or exec,
1214 * P_LPPWAIT is set; we will wakeup the parent below.
1217 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1218 p
->p_sigignore
= ~(sigcantmask
);
1228 struct task
*task
= p
->task
;
1229 vnode_t tvp
= NULLVP
;
1231 struct session
*sessp
;
1232 struct uthread
* uth
;
1237 uth
= current_uthread();
1240 proc_transstart(p
, 1, 0);
1241 if (!(p
->p_lflag
& P_LEXIT
)) {
1243 * This can happen if a thread_terminate() occurs
1244 * in a single-threaded process.
1246 p
->p_lflag
|= P_LEXIT
;
1247 proc_transend(p
, 1);
1249 proc_prepareexit(p
, 0, TRUE
);
1250 (void) task_terminate_internal(task
);
1253 proc_transend(p
, 1);
1256 p
->p_lflag
|= P_LPEXIT
;
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.
1263 if ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 0)) {
1265 while ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 1)) {
1266 msleep(&p
->p_sigmask
, &p
->p_mlock
, PWAIT
, "proc_sigdrain", NULL
);
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);
1279 dtrace_proc_exit(p
);
1283 * need to cancel async IO requests that can be cancelled and wait for those
1284 * already active. MAY BLOCK!
1289 /* if any pending cpu limits action, clear it */
1290 task_clear_cpuusage(p
->task
, TRUE
);
1292 workq_mark_exiting(p
);
1297 * Close open files and release open-file table.
1303 * Once all the knotes, kqueues & workloops are destroyed, get rid of the
1308 if (uth
->uu_lowpri_window
) {
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
1316 throttle_lowpri_io(0);
1319 if (p
->p_lflag
& P_LNSPACE_RESOLVER
) {
1321 * The namespace resolver is exiting; there may be
1322 * outstanding materialization requests to clean up.
1324 nspace_resolver_exited(p
);
1328 /* Close ref SYSV Shared memory*/
1334 /* Release SYSV semaphores */
1339 pth_proc_hashdelete(p
);
1342 sessp
= proc_session(p
);
1343 if (SESS_LEADER(p
, sessp
)) {
1344 if (sessp
->s_ttyvp
!= NULLVP
) {
1345 struct vnode
*ttyvp
;
1348 struct vfs_context context
;
1352 * Controlling process.
1353 * Signal foreground pgrp,
1354 * drain controlling terminal
1355 * and revoke access to controlling terminal.
1357 session_lock(sessp
);
1358 tp
= SESSION_TP(sessp
);
1359 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1360 session_unlock(sessp
);
1362 tty_pgsignal(tp
, SIGHUP
, 1);
1364 session_lock(sessp
);
1365 tp
= SESSION_TP(sessp
);
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
);
1377 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1378 if (tp
!= TTY_NULL
) {
1384 context
.vc_thread
= NULL
;
1385 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1386 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
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.
1398 kauth_cred_unref(&context
.vc_ucred
);
1405 session_lock(sessp
);
1406 sessp
->s_leader
= NULL
;
1407 session_unlock(sessp
);
1409 session_rele(sessp
);
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().
1420 proc_limitsetcur_internal(p
, RLIMIT_FSIZE
, RLIM_INFINITY
);
1422 (void)acct_process(p
);
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
);
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");
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);
1446 q
->p_listflag
|= P_LIST_WAITING
;
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
1456 (void)reap_child_locked(p
, q
, 1, reparentedtoinit
, 1, 0);
1459 * Traced processes are killed
1460 * since their existence means someone is messing up.
1462 if (q
->p_lflag
& P_LTRACED
) {
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.
1471 if (proc_ref_locked(q
) != q
) {
1477 opp
= proc_find(q
->p_oppid
);
1478 if (opp
!= PROC_NULL
) {
1482 proc_reparentlocked(q
, opp
, 0, 0);
1485 /* original parent exited while traced */
1487 q
->p_listflag
|= P_LIST_DEADPARENT
;
1490 proc_reparentlocked(q
, initproc
, 0, 0);
1494 q
->p_lflag
&= ~P_LTRACED
;
1496 if (q
->sigwait_thread
) {
1497 thread_t thread
= q
->sigwait_thread
;
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.
1507 thread_resume(thread
);
1508 clear_wait(thread
, THREAD_INTERRUPTED
);
1509 threadsignal(thread
, SIGKILL
, 0, TRUE
);
1514 psignal(q
, SIGKILL
);
1516 proc_rele_locked(q
);
1518 q
->p_listflag
|= P_LIST_DEADPARENT
;
1519 proc_reparentlocked(q
, initproc
, 0, 1);
1524 proc_childdrainend(p
);
1529 * Notify MAC policies that proc is dead.
1530 * This should be replaced with proper label management
1531 * (rdar://problem/32126399).
1533 mac_proc_notify_exit(p
);
1537 * Release reference to text vnode
1541 if (tvp
!= NULLVP
) {
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.
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
;
1554 ruadd(&(p
->p_ru
->ru
), &p
->p_stats
->p_cru
);
1558 * Free up profiling buffers.
1561 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1567 for (; p1
!= NULL
; p1
= pn
) {
1569 kfree(p1
, sizeof *p1
);
1573 proc_free_realitimer(p
);
1576 * Other substructures are freed from wait().
1578 zfree(proc_stats_zone
, p
->p_stats
);
1581 zfree(proc_sigacts_zone
, p
->p_sigacts
);
1582 p
->p_sigacts
= NULL
;
1587 * Finish up by terminating the task
1588 * and halt this thread (only if a
1589 * member of the task exiting).
1591 p
->task
= TASK_NULL
;
1592 set_bsdtask_info(task
, NULL
);
1594 knote_hint
= NOTE_EXIT
| (p
->p_xstat
& 0xffff);
1595 proc_knote(p
, knote_hint
);
1597 /* mark the thread as the one that is doing proc_exit
1598 * no need to hold proc lock in uthread_free
1600 uth
->uu_flag
|= UT_PROCEXIT
;
1602 * Notify parent that we're gone.
1604 pp
= proc_parent(p
);
1605 if (pp
->p_flag
& P_NOCLDWAIT
) {
1606 if (p
->p_ru
!= NULL
) {
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
1616 #else /* !3839178 */
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.
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
);
1628 /* kernel can reap this one, no need to move it to launchd */
1630 p
->p_listflag
|= P_LIST_DEADPARENT
;
1633 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0 || p
->p_oppid
) {
1634 if (pp
!= initproc
) {
1636 pp
->si_pid
= p
->p_pid
;
1637 pp
->p_xhighbits
= p
->p_xhighbits
;
1639 pp
->si_status
= p
->p_xstat
;
1640 pp
->si_code
= CLD_EXITED
;
1642 * p_ucred usage is safe as it is an exiting process
1643 * and reference is dropped in reap
1645 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
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
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);
1660 * The current process can be reaped so, no one
1661 * can depend on this
1664 psignal(pp
, SIGCHLD
);
1666 /* and now wakeup the parent */
1668 wakeup((caddr_t
)pp
);
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
1678 DTRACE_PROC2(exited
, proc_t
, p
, int, exitval
);
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);
1687 /* safe to use p as this is a system reap */
1689 p
->p_listflag
|= P_LIST_WAITING
;
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
1700 (void)reap_child_locked(pp
, p
, 1, 0, 1, 1);
1701 /* list lock dropped by reap_child_locked */
1703 if (uth
->uu_lowpri_window
) {
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
1710 throttle_lowpri_io(0);
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
1726 * Parameters: proc_t parent Parent of process being reaped
1727 * proc_t child Process to reap
1729 * Returns: 0 Process was not reaped because it
1730 * came from an attach
1731 * 1 Process was reaped
1734 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
)
1736 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
1743 * If we got the child via a ptrace 'attach',
1744 * we need to give it back to the old parent.
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
1752 if (child
->p_oppid
) {
1757 oppid
= child
->p_oppid
;
1759 knote_hint
= NOTE_EXIT
| (child
->p_xstat
& 0xffff);
1762 if ((trace_parent
= proc_find(oppid
))
1763 && !((trace_parent
== initproc
) && reparentedtoinit
)) {
1764 if (trace_parent
!= initproc
) {
1766 * proc internal fileds and p_ucred usage safe
1767 * here as child is dead and is not reaped or
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
);
1777 proc_reparentlocked(child
, trace_parent
, 1, 0);
1779 /* resend knote to original parent (and others) after reparenting */
1780 proc_knote(child
, knote_hint
);
1782 psignal(trace_parent
, SIGCHLD
);
1784 wakeup((caddr_t
)trace_parent
);
1785 child
->p_listflag
&= ~P_LIST_WAITING
;
1786 wakeup(&child
->p_stat
);
1788 proc_rele(trace_parent
);
1789 if ((locked
== 1) && (droplock
== 0)) {
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.
1801 proc_knote(child
, knote_hint
);
1803 if (trace_parent
!= PROC_NULL
) {
1804 proc_rele(trace_parent
);
1808 #pragma clang diagnostic push
1809 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1810 proc_knote(child
, NOTE_REAP
);
1811 #pragma clang diagnostic pop
1813 proc_knote_drain(child
);
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
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
);
1834 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
1837 AUDIT_SESSION_PROCEXIT(child
);
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)
1847 * persona_proc_drop calls chgproccnt(-1) on the persona uid,
1848 * and (+1) on the child->p_ucred uid
1850 persona_proc_drop(child
);
1852 (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1);
1854 os_reason_free(child
->p_exit_reason
);
1857 * Finally finished with old proc entry.
1858 * Unlink it from its process group and free it.
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 */
1870 child
->p_listflag
&= ~P_LIST_WAITING
;
1871 wakeup(&child
->p_stat
);
1873 /* Take it out of process hash */
1874 LIST_REMOVE(child
, p_hash
);
1875 child
->p_listflag
&= ~P_LIST_INHASH
;
1876 proc_checkdeadrefs(child
);
1881 * If a child zombie is being reaped because its parent
1882 * is exiting, make sure we update the list flag
1884 child
->p_listflag
|= P_LIST_DEADPARENT
;
1890 * Free up credentials.
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
;
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
);
1902 lck_mtx_destroy(&child
->p_dtrace_sprlock
, &proc_lck_grp
);
1904 lck_spin_destroy(&child
->p_slock
, &proc_slock_grp
);
1905 lck_rw_destroy(&child
->p_dirs_lock
, &proc_dirslock_grp
);
1907 zfree(proc_zone
, child
);
1908 if ((locked
== 1) && (droplock
== 0)) {
1917 wait1continue(int result
)
1922 struct _wait4_data
*wait4_data
;
1923 struct wait4_nocancel_args
*uap
;
1931 thread
= current_thread();
1932 uth
= (struct uthread
*)get_bsdthread_info(thread
);
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
);
1941 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
1943 __pthread_testcancel(1);
1944 return wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
);
1948 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1955 struct _wait4_data
*wait4_data
;
1957 AUDIT_ARG(pid
, uap
->pid
);
1959 if (uap
->pid
== 0) {
1960 uap
->pid
= -q
->p_pgrpid
;
1969 PCHILDREN_FOREACH(q
, p
) {
1970 if (p
->p_sibling
.le_next
!= 0) {
1973 if (uap
->pid
!= WAIT_ANY
&&
1974 p
->p_pid
!= uap
->pid
&&
1975 p
->p_pgrpid
!= -(uap
->pid
)) {
1981 /* XXX This is racy because we don't get the lock!!!! */
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
);
1991 (void)msleep(&p
->p_stat
, &proc_list_mlock
, PWAIT
, "waitcoll", 0);
1994 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1997 if (p
->p_stat
== SZOMB
) {
1998 int reparentedtoinit
= (p
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
2002 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2006 retval
[0] = p
->p_pid
;
2008 /* Legacy apps expect only 8 bits of status */
2009 status
= 0xffff & p
->p_xstat
; /* convert to int */
2010 error
= copyout((caddr_t
)&status
,
2018 if (p
->p_ru
== NULL
) {
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
,
2028 struct user32_rusage my_rusage
= {};
2029 munge_user32_rusage(&p
->p_ru
->ru
, &my_rusage
);
2030 error
= copyout((caddr_t
)&my_rusage
,
2035 /* information unavailable? */
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.
2046 if (sibling_count
== 0) {
2047 int mask
= sigmask(SIGCHLD
);
2048 uth
= current_uthread();
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.
2056 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
2062 (void)reap_child_locked(q
, p
, 0, reparentedtoinit
, 0, 0);
2066 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
2067 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
2070 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2075 p
->p_lflag
|= P_LWAITED
;
2077 retval
[0] = p
->p_pid
;
2079 status
= W_STOPCODE(p
->p_xstat
);
2080 error
= copyout((caddr_t
)&status
,
2089 * If we are waiting for continued processses, and this
2090 * process was continued
2092 if ((uap
->options
& WCONTINUED
) &&
2093 (p
->p_flag
& P_CONTINUED
)) {
2096 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2101 /* Prevent other process for waiting for this event */
2102 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2103 retval
[0] = p
->p_pid
;
2105 status
= W_STOPCODE(SIGCONT
);
2106 error
= copyout((caddr_t
)&status
,
2114 p
->p_listflag
&= ~P_LIST_WAITING
;
2117 /* list lock is held when we get here any which way */
2123 if (uap
->options
& WNOHANG
) {
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
;
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
))) {
2143 p
->p_listflag
&= ~P_LIST_WAITING
;
2150 #define ASSERT_LCK_MTX_OWNED(lock) \
2151 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
2153 #define ASSERT_LCK_MTX_OWNED(lock) /* nothing */
2157 waitidcontinue(int result
)
2162 struct _waitid_data
*waitid_data
;
2163 struct waitid_nocancel_args
*uap
;
2171 thread
= current_thread();
2172 uth
= (struct uthread
*)get_bsdthread_info(thread
);
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
);
2181 * Description: Suspend the calling thread until one child of the process
2182 * containing the calling thread changes state.
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
2190 * Returns: 0 Success
2191 * !0 Error returning status to user space
2194 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
2196 __pthread_testcancel(1);
2197 return waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
);
2201 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
,
2202 __unused
int32_t *retval
)
2204 user_siginfo_t siginfo
; /* siginfo data to return to caller */
2205 boolean_t caller64
= IS_64BIT_PROCESS(q
);
2210 struct _waitid_data
*waitid_data
;
2212 if (uap
->options
== 0 ||
2213 (uap
->options
& ~(WNOHANG
| WNOWAIT
| WCONTINUED
| WSTOPPED
| WEXITED
))) {
2214 return EINVAL
; /* bits set that aren't recognized */
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) {
2223 case P_ALL
: /* any child */
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
) {
2239 case P_PGID
: /* child with process group ID equal to... */
2240 if (p
->p_pgrpid
!= (pid_t
)uap
->id
) {
2244 case P_ALL
: /* any child */
2248 /* XXX This is racy because we don't get the lock!!!! */
2251 * Wait collision; go to sleep and restart; used to maintain
2252 * the single return for waited process guarantee.
2254 if (p
->p_listflag
& P_LIST_WAITING
) {
2255 (void) msleep(&p
->p_stat
, &proc_list_mlock
,
2256 PWAIT
, "waitidcoll", 0);
2259 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
2263 bzero(&siginfo
, sizeof(siginfo
));
2265 switch (p
->p_stat
) {
2266 case SZOMB
: /* Exited */
2267 if (!(uap
->options
& WEXITED
)) {
2272 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2276 siginfo
.si_signo
= SIGCHLD
;
2277 siginfo
.si_pid
= p
->p_pid
;
2279 /* If the child terminated abnormally due to a signal, the signum
2280 * needs to be preserved in the exit status.
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
);
2287 siginfo
.si_code
= CLD_EXITED
;
2288 siginfo
.si_status
= WEXITSTATUS(p
->p_xstat
) & 0x00FFFFFF;
2290 siginfo
.si_status
|= (((uint32_t)(p
->p_xhighbits
) << 24) & 0xFF000000);
2293 if ((error
= copyoutsiginfo(&siginfo
,
2294 caller64
, uap
->infop
)) != 0) {
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);
2305 case SSTOP
: /* Stopped */
2307 * If we are not interested in stopped processes, then
2310 if (!(uap
->options
& WSTOPPED
)) {
2315 * If someone has already waited it, we lost a race
2316 * to be the one to return status.
2318 if ((p
->p_lflag
& P_LWAITED
) != 0) {
2323 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
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
;
2332 if ((error
= copyoutsiginfo(&siginfo
,
2333 caller64
, uap
->infop
)) != 0) {
2337 /* Prevent other process for waiting for this event? */
2338 if (!(uap
->options
& WNOWAIT
)) {
2340 p
->p_lflag
|= P_LWAITED
;
2345 default: /* All other states => Continued */
2346 if (!(uap
->options
& WCONTINUED
)) {
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().
2355 if ((p
->p_flag
& P_CONTINUED
) == 0) {
2360 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2364 siginfo
.si_signo
= SIGCHLD
;
2365 siginfo
.si_code
= CLD_CONTINUED
;
2367 siginfo
.si_pid
= p
->p_contproc
;
2368 siginfo
.si_status
= p
->p_xstat
;
2371 if ((error
= copyoutsiginfo(&siginfo
,
2372 caller64
, uap
->infop
)) != 0) {
2376 /* Prevent other process for waiting for this event? */
2377 if (!(uap
->options
& WNOWAIT
)) {
2378 OSBitAndAtomic(~((uint32_t)P_CONTINUED
),
2383 ASSERT_LCK_MTX_OWNED(&proc_list_mlock
);
2385 /* Not a process we are interested in; go on to next child */
2387 p
->p_listflag
&= ~P_LIST_WAITING
;
2390 ASSERT_LCK_MTX_OWNED(&proc_list_mlock
);
2392 /* No child processes that could possibly satisfy the request? */
2399 if (uap
->options
& WNOHANG
) {
2402 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
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.
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.
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
;
2424 if ((error
= msleep0(q
, &proc_list_mlock
,
2425 PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)) != 0) {
2432 p
->p_listflag
&= ~P_LIST_WAITING
;
2439 * make process 'parent' the new parent of process 'child'.
2442 proc_reparentlocked(proc_t child
, proc_t parent
, int signallable
, int locked
)
2444 proc_t oldparent
= PROC_NULL
;
2446 if (child
->p_pptr
== parent
) {
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
);
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");
2467 oldparent
->p_childrencnt
--;
2468 #if __PROC_INTERNAL_DEBUG
2469 if (oldparent
->p_childrencnt
< 0) {
2470 panic("process children count -ve\n");
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
;
2480 if ((signallable
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
)) {
2481 psignal(initproc
, SIGCHLD
);
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.
2495 vfork_exit(proc_t p
, int rv
)
2497 vfork_exit_internal(p
, rv
, 0);
2501 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
2503 thread_t self
= current_thread();
2505 struct task
*task
= p
->task
;
2510 * If a thread in this task has already
2511 * called exit(), then halt any others
2515 ut
= get_bsdthread_info(self
);
2519 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
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()
2527 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
2530 if (forceexit
== 0) {
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.
2538 /* Notify the perf server */
2539 (void)sys_perf_notify(self
, p
->p_pid
);
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.
2551 #if CONFIG_MEMORYSTATUS
2552 proc_memorystatus_remove(p
);
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
;
2564 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
2565 p
->p_sigignore
= ~0;
2570 /* begin vproc_exit */
2578 struct session
*sessp
;
2579 struct rusage_superset
*rup
;
2581 rup
= zalloc(zombie_zone
);
2586 * Close open files and release open-file table.
2591 sessp
= proc_session(p
);
2592 if (SESS_LEADER(p
, sessp
)) {
2593 panic("vfork child is session leader");
2595 session_rele(sessp
);
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().
2606 proc_limitsetcur_internal(p
, RLIMIT_FSIZE
, RLIM_INFINITY
);
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");
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);
2620 q
->p_listflag
|= P_LIST_WAITING
;
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
2630 (void)reap_child_locked(p
, q
, 1, 0, 1, 0);
2633 * Traced processes are killed
2634 * since their existence means someone is messing up.
2636 if (q
->p_lflag
& P_LTRACED
) {
2641 opp
= proc_find(q
->p_oppid
);
2642 if (opp
!= PROC_NULL
) {
2646 proc_reparentlocked(q
, opp
, 0, 0);
2649 /* original parent exited while traced */
2651 q
->p_listflag
|= P_LIST_DEADPARENT
;
2654 proc_reparentlocked(q
, initproc
, 0, 0);
2658 q
->p_lflag
&= ~P_LTRACED
;
2660 if (q
->sigwait_thread
) {
2661 thread_t thread
= q
->sigwait_thread
;
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.
2671 thread_resume(thread
);
2672 clear_wait(thread
, THREAD_INTERRUPTED
);
2673 threadsignal(thread
, SIGKILL
, 0, TRUE
);
2678 psignal(q
, SIGKILL
);
2681 q
->p_listflag
|= P_LIST_DEADPARENT
;
2682 proc_reparentlocked(q
, initproc
, 0, 1);
2687 proc_childdrainend(p
);
2691 * Release reference to text vnode
2695 if (tvp
!= NULLVP
) {
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.
2705 rup
->ru
= p
->p_stats
->p_ru
;
2706 timerclear(&rup
->ru
.ru_utime
);
2707 timerclear(&rup
->ru
.ru_stime
);
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
;
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
;
2724 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
2725 task_info(task
, TASK_THREAD_TIMES_INFO
,
2726 &ttimesinfo
, &task_ttimes_stuff
);
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
);
2737 ruadd(&rup
->ru
, &p
->p_stats
->p_cru
);
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();
2744 * Now that we have filled in the rusage info, make it
2745 * visible to an external observer via proc_pid_rusage().
2751 * Free up profiling buffers.
2754 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
2760 for (; p1
!= NULL
; p1
= pn
) {
2762 kfree(p1
, sizeof *p1
);
2767 pth_proc_hashdelete(p
);
2770 proc_free_realitimer(p
);
2773 * Other substructures are freed from wait().
2775 zfree(proc_stats_zone
, p
->p_stats
);
2778 zfree(proc_sigacts_zone
, p
->p_sigacts
);
2779 p
->p_sigacts
= NULL
;
2781 if (p
->p_subsystem_root_path
) {
2782 zfree(ZV_NAMEI
, p
->p_subsystem_root_path
);
2788 * Finish up by terminating the task
2789 * and halt this thread (only if a
2790 * member of the task exiting).
2792 p
->task
= TASK_NULL
;
2795 * Notify parent that we're gone.
2797 pp
= proc_parent(p
);
2798 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
2799 if (pp
!= initproc
) {
2801 pp
->si_pid
= p
->p_pid
;
2802 pp
->p_xhighbits
= p
->p_xhighbits
;
2804 pp
->si_status
= p
->p_xstat
;
2805 pp
->si_code
= CLD_EXITED
;
2807 * p_ucred usage is safe as it is an exiting process
2808 * and reference is dropped in reap
2810 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
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
2822 psignal(pp
, SIGCHLD
);
2824 /* and now wakeup the parent */
2826 wakeup((caddr_t
)pp
);
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);
2835 p
->p_listflag
|= P_LIST_WAITING
;
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
2846 (void)reap_child_locked(pp
, p
, 0, 0, 1, 1);
2847 /* list lock dropped by reap_child_locked */
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
2859 __private_extern__
void
2860 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
2862 /* Zero-out struct so that padding is cleared */
2863 bzero(a_user_rusage_p
, sizeof(struct user64_rusage
));
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
;
2871 * everything else can be a direct assign, since there is no loss
2872 * of precision implied boing 32->64.
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
;
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
)
2894 bzero(a_user_rusage_p
, sizeof(struct user32_rusage
));
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
;
2902 * everything else can be a direct assign. We currently ignore
2903 * the loss of precision
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
;
2922 kdp_wait4_find_process(thread_t thread
, __unused event64_t wait_event
, thread_waitinfo_t
*waitinfo
)
2924 assert(thread
!= NULL
);
2925 assert(waitinfo
!= NULL
);
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
;
2937 #if __has_feature(ptrauth_calls)
2939 exit_with_pac_exception(proc_t p
, exception_type_t exception
, mach_exception_code_t code
,
2940 mach_exception_subcode_t subcode
)
2942 thread_t self
= current_thread();
2943 struct uthread
*ut
= get_bsdthread_info(self
);
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
;
2950 ut
->uu_subcode
= subcode
;
2952 return exit_with_reason(p
, W_EXITCODE(0, SIGKILL
), (int *)NULL
, TRUE
, FALSE
,
2953 0, exception_reason
);
2955 #endif /* __has_feature(ptrauth_calls) */