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>
131 #include <vm/vm_protos.h>
134 #include <pexpert/pexpert.h>
137 #include <sys/shm_internal.h> /* shmexit */
138 #endif /* SYSV_SHM */
140 #include <sys/persona.h>
141 #endif /* CONFIG_PERSONAS */
142 #if CONFIG_MEMORYSTATUS
143 #include <sys/kern_memorystatus.h>
144 #endif /* CONFIG_MEMORYSTATUS */
146 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
147 void dtrace_proc_exit(proc_t p
);
148 #include <sys/dtrace_ptss.h>
149 #endif /* CONFIG_DTRACE */
151 #include <security/mac_framework.h>
152 #include <security/mac_mach_internal.h>
153 #include <sys/syscall.h>
154 #endif /* CONFIG_MACF */
156 #if CONFIG_MEMORYSTATUS
157 static void proc_memorystatus_remove(proc_t p
);
158 #endif /* CONFIG_MEMORYSTATUS */
159 void proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
);
160 void gather_populate_corpse_crashinfo(proc_t p
, task_t corpse_task
,
161 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
,
162 uint64_t *udata_buffer
, int num_udata
, void *reason
);
163 mach_exception_data_type_t
proc_encode_exit_exception_code(proc_t p
);
164 void vfork_exit(proc_t p
, int rv
);
165 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
166 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
167 static int reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
);
168 static void populate_corpse_crashinfo(proc_t p
, task_t corpse_task
,
169 struct rusage_superset
*rup
, mach_exception_data_type_t code
,
170 mach_exception_data_type_t subcode
, uint64_t *udata_buffer
,
171 int num_udata
, os_reason_t reason
);
172 static void proc_update_corpse_exception_codes(proc_t p
, mach_exception_data_type_t
*code
, mach_exception_data_type_t
*subcode
);
173 extern int proc_pidpathinfo_internal(proc_t p
, uint64_t arg
, char *buffer
, uint32_t buffersize
, int32_t *retval
);
174 static __attribute__((noinline
)) void launchd_crashed_panic(proc_t p
, int rv
);
175 extern void proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
176 extern void task_coalition_ids(task_t task
, uint64_t ids
[COALITION_NUM_TYPES
]);
177 extern uint64_t get_task_phys_footprint_limit(task_t
);
178 int proc_list_uptrs(void *p
, uint64_t *udata_buffer
, int size
);
179 extern uint64_t task_corpse_get_crashed_thread_id(task_t corpse_task
);
181 ZONE_DECLARE(zombie_zone
, "zombie",
182 sizeof(struct rusage_superset
), ZC_NOENCRYPT
);
186 * Things which should have prototypes in headers, but don't
188 void proc_exit(proc_t p
);
189 int wait1continue(int result
);
190 int waitidcontinue(int result
);
191 kern_return_t
sys_perf_notify(thread_t thread
, int pid
);
192 kern_return_t
task_exception_notify(exception_type_t exception
,
193 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
);
194 kern_return_t
task_violated_guard(mach_exception_code_t
, mach_exception_subcode_t
, void *);
196 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
198 #if __has_feature(ptrauth_calls)
199 int exit_with_pac_exception(proc_t p
, exception_type_t exception
, mach_exception_code_t code
,
200 mach_exception_subcode_t subcode
);
201 #endif /* __has_feature(ptrauth_calls) */
205 * NOTE: Source and target may *NOT* overlap!
206 * XXX Should share code with bsd/dev/ppc/unix_signal.c
209 siginfo_user_to_user32(user_siginfo_t
*in
, user32_siginfo_t
*out
)
211 out
->si_signo
= in
->si_signo
;
212 out
->si_errno
= in
->si_errno
;
213 out
->si_code
= in
->si_code
;
214 out
->si_pid
= in
->si_pid
;
215 out
->si_uid
= in
->si_uid
;
216 out
->si_status
= in
->si_status
;
217 out
->si_addr
= CAST_DOWN_EXPLICIT(user32_addr_t
, in
->si_addr
);
218 /* following cast works for sival_int because of padding */
219 out
->si_value
.sival_ptr
= CAST_DOWN_EXPLICIT(user32_addr_t
, in
->si_value
.sival_ptr
);
220 out
->si_band
= (user32_long_t
)in
->si_band
; /* range reduction */
224 siginfo_user_to_user64(user_siginfo_t
*in
, user64_siginfo_t
*out
)
226 out
->si_signo
= in
->si_signo
;
227 out
->si_errno
= in
->si_errno
;
228 out
->si_code
= in
->si_code
;
229 out
->si_pid
= in
->si_pid
;
230 out
->si_uid
= in
->si_uid
;
231 out
->si_status
= in
->si_status
;
232 out
->si_addr
= in
->si_addr
;
233 /* following cast works for sival_int because of padding */
234 out
->si_value
.sival_ptr
= in
->si_value
.sival_ptr
;
235 out
->si_band
= in
->si_band
; /* range reduction */
239 copyoutsiginfo(user_siginfo_t
*native
, boolean_t is64
, user_addr_t uaddr
)
242 user64_siginfo_t sinfo64
;
244 bzero(&sinfo64
, sizeof(sinfo64
));
245 siginfo_user_to_user64(native
, &sinfo64
);
246 return copyout(&sinfo64
, uaddr
, sizeof(sinfo64
));
248 user32_siginfo_t sinfo32
;
250 bzero(&sinfo32
, sizeof(sinfo32
));
251 siginfo_user_to_user32(native
, &sinfo32
);
252 return copyout(&sinfo32
, uaddr
, sizeof(sinfo32
));
257 gather_populate_corpse_crashinfo(proc_t p
, task_t corpse_task
,
258 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
,
259 uint64_t *udata_buffer
, int num_udata
, void *reason
)
261 struct rusage_superset rup
;
263 gather_rusage_info(p
, &rup
.ri
, RUSAGE_INFO_CURRENT
);
264 rup
.ri
.ri_phys_footprint
= 0;
265 populate_corpse_crashinfo(p
, corpse_task
, &rup
, code
, subcode
,
266 udata_buffer
, num_udata
, reason
);
270 proc_update_corpse_exception_codes(proc_t p
, mach_exception_data_type_t
*code
, mach_exception_data_type_t
*subcode
)
272 mach_exception_data_type_t code_update
= *code
;
273 mach_exception_data_type_t subcode_update
= *subcode
;
274 if (p
->p_exit_reason
== OS_REASON_NULL
) {
278 switch (p
->p_exit_reason
->osr_namespace
) {
279 case OS_REASON_JETSAM
:
280 if (p
->p_exit_reason
->osr_code
== JETSAM_REASON_MEMORY_PERPROCESSLIMIT
) {
281 /* Update the code with EXC_RESOURCE code for high memory watermark */
282 EXC_RESOURCE_ENCODE_TYPE(code_update
, RESOURCE_TYPE_MEMORY
);
283 EXC_RESOURCE_ENCODE_FLAVOR(code_update
, FLAVOR_HIGH_WATERMARK
);
284 EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update
, ((get_task_phys_footprint_limit(p
->task
)) >> 20));
295 *subcode
= subcode_update
;
299 mach_exception_data_type_t
300 proc_encode_exit_exception_code(proc_t p
)
302 uint64_t subcode
= 0;
304 if (p
->p_exit_reason
== OS_REASON_NULL
) {
308 /* Embed first 32 bits of osr_namespace and osr_code in exception code */
309 ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode
, p
->p_exit_reason
->osr_namespace
);
310 ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode
, p
->p_exit_reason
->osr_code
);
311 return (mach_exception_data_type_t
)subcode
;
315 populate_corpse_crashinfo(proc_t p
, task_t corpse_task
, struct rusage_superset
*rup
,
316 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
,
317 uint64_t *udata_buffer
, int num_udata
, os_reason_t reason
)
319 mach_vm_address_t uaddr
= 0;
320 mach_exception_data_type_t exc_codes
[EXCEPTION_CODE_MAX
];
322 exc_codes
[1] = subcode
;
324 struct proc_uniqidentifierinfo p_uniqidinfo
;
325 struct proc_workqueueinfo pwqinfo
;
327 uint64_t crashed_threadid
= task_corpse_get_crashed_thread_id(corpse_task
);
328 unsigned int pflags
= 0;
329 uint64_t max_footprint_mb
;
330 uint64_t max_footprint
;
332 uint64_t ledger_internal
;
333 uint64_t ledger_internal_compressed
;
334 uint64_t ledger_iokit_mapped
;
335 uint64_t ledger_alternate_accounting
;
336 uint64_t ledger_alternate_accounting_compressed
;
337 uint64_t ledger_purgeable_nonvolatile
;
338 uint64_t ledger_purgeable_nonvolatile_compressed
;
339 uint64_t ledger_page_table
;
340 uint64_t ledger_phys_footprint
;
341 uint64_t ledger_phys_footprint_lifetime_max
;
342 uint64_t ledger_network_nonvolatile
;
343 uint64_t ledger_network_nonvolatile_compressed
;
344 uint64_t ledger_wired_mem
;
345 uint64_t ledger_tagged_footprint
;
346 uint64_t ledger_tagged_footprint_compressed
;
347 uint64_t ledger_media_footprint
;
348 uint64_t ledger_media_footprint_compressed
;
349 uint64_t ledger_graphics_footprint
;
350 uint64_t ledger_graphics_footprint_compressed
;
351 uint64_t ledger_neural_footprint
;
352 uint64_t ledger_neural_footprint_compressed
;
354 void *crash_info_ptr
= task_get_corpseinfo(corpse_task
);
356 #if CONFIG_MEMORYSTATUS
357 int memstat_dirty_flags
= 0;
360 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_EXCEPTION_CODES
, sizeof(exc_codes
), &uaddr
)) {
361 kcdata_memcpy(crash_info_ptr
, uaddr
, exc_codes
, sizeof(exc_codes
));
364 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PID
, sizeof(p
->p_pid
), &uaddr
)) {
365 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_pid
, sizeof(p
->p_pid
));
368 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PPID
, sizeof(p
->p_ppid
), &uaddr
)) {
369 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_ppid
, sizeof(p
->p_ppid
));
372 /* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
373 if ((p
->p_exit_reason
== OS_REASON_NULL
) || !(p
->p_exit_reason
->osr_flags
& OS_REASON_FLAG_NO_CRASHED_TID
)) {
374 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CRASHED_THREADID
, sizeof(uint64_t), &uaddr
)) {
375 kcdata_memcpy(crash_info_ptr
, uaddr
, &crashed_threadid
, sizeof(uint64_t));
379 static_assert(sizeof(struct proc_uniqidentifierinfo
) == sizeof(struct crashinfo_proc_uniqidentifierinfo
));
381 kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_BSDINFOWITHUNIQID
, sizeof(struct proc_uniqidentifierinfo
), &uaddr
)) {
382 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
383 kcdata_memcpy(crash_info_ptr
, uaddr
, &p_uniqidinfo
, sizeof(struct proc_uniqidentifierinfo
));
386 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RUSAGE_INFO
, sizeof(rusage_info_current
), &uaddr
)) {
387 kcdata_memcpy(crash_info_ptr
, uaddr
, &rup
->ri
, sizeof(rusage_info_current
));
390 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_CSFLAGS
, sizeof(p
->p_csflags
), &uaddr
)) {
391 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_csflags
, sizeof(p
->p_csflags
));
394 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_NAME
, sizeof(p
->p_comm
), &uaddr
)) {
395 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_comm
, sizeof(p
->p_comm
));
398 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_STARTTIME
, sizeof(p
->p_start
), &uaddr
)) {
399 struct timeval64 t64
;
400 t64
.tv_sec
= (int64_t)p
->p_start
.tv_sec
;
401 t64
.tv_usec
= (int64_t)p
->p_start
.tv_usec
;
402 kcdata_memcpy(crash_info_ptr
, uaddr
, &t64
, sizeof(t64
));
405 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_USERSTACK
, sizeof(p
->user_stack
), &uaddr
)) {
406 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->user_stack
, sizeof(p
->user_stack
));
409 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_ARGSLEN
, sizeof(p
->p_argslen
), &uaddr
)) {
410 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_argslen
, sizeof(p
->p_argslen
));
413 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_ARGC
, sizeof(p
->p_argc
), &uaddr
)) {
414 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_argc
, sizeof(p
->p_argc
));
417 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_PATH
, MAXPATHLEN
, &uaddr
)) {
418 char *buf
= zalloc_flags(ZV_NAMEI
, Z_WAITOK
| Z_ZERO
);
419 proc_pidpathinfo_internal(p
, 0, buf
, MAXPATHLEN
, &retval
);
420 kcdata_memcpy(crash_info_ptr
, uaddr
, buf
, MAXPATHLEN
);
421 zfree(ZV_NAMEI
, buf
);
424 pflags
= p
->p_flag
& (P_LP64
| P_SUGID
| P_TRANSLATED
);
425 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_FLAGS
, sizeof(pflags
), &uaddr
)) {
426 kcdata_memcpy(crash_info_ptr
, uaddr
, &pflags
, sizeof(pflags
));
429 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_UID
, sizeof(p
->p_uid
), &uaddr
)) {
430 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_uid
, sizeof(p
->p_uid
));
433 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_GID
, sizeof(p
->p_gid
), &uaddr
)) {
434 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_gid
, sizeof(p
->p_gid
));
437 cputype
= cpu_type() & ~CPU_ARCH_MASK
;
438 if (IS_64BIT_PROCESS(p
)) {
439 cputype
|= CPU_ARCH_ABI64
;
442 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CPUTYPE
, sizeof(cpu_type_t
), &uaddr
)) {
443 kcdata_memcpy(crash_info_ptr
, uaddr
, &cputype
, sizeof(cpu_type_t
));
446 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_MEMORY_LIMIT
, sizeof(max_footprint_mb
), &uaddr
)) {
447 max_footprint
= get_task_phys_footprint_limit(p
->task
);
448 max_footprint_mb
= max_footprint
>> 20;
449 kcdata_memcpy(crash_info_ptr
, uaddr
, &max_footprint_mb
, sizeof(max_footprint_mb
));
452 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT_LIFETIME_MAX
, sizeof(ledger_phys_footprint_lifetime_max
), &uaddr
)) {
453 ledger_phys_footprint_lifetime_max
= get_task_phys_footprint_lifetime_max(p
->task
);
454 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_phys_footprint_lifetime_max
, sizeof(ledger_phys_footprint_lifetime_max
));
457 // In the forking case, the current ledger info is copied into the corpse while the original task is suspended for consistency
458 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_INTERNAL
, sizeof(ledger_internal
), &uaddr
)) {
459 ledger_internal
= get_task_internal(corpse_task
);
460 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_internal
, sizeof(ledger_internal
));
463 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_INTERNAL_COMPRESSED
, sizeof(ledger_internal_compressed
), &uaddr
)) {
464 ledger_internal_compressed
= get_task_internal_compressed(corpse_task
);
465 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_internal_compressed
, sizeof(ledger_internal_compressed
));
468 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_IOKIT_MAPPED
, sizeof(ledger_iokit_mapped
), &uaddr
)) {
469 ledger_iokit_mapped
= get_task_iokit_mapped(corpse_task
);
470 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_iokit_mapped
, sizeof(ledger_iokit_mapped
));
473 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING
, sizeof(ledger_alternate_accounting
), &uaddr
)) {
474 ledger_alternate_accounting
= get_task_alternate_accounting(corpse_task
);
475 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_alternate_accounting
, sizeof(ledger_alternate_accounting
));
478 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING_COMPRESSED
, sizeof(ledger_alternate_accounting_compressed
), &uaddr
)) {
479 ledger_alternate_accounting_compressed
= get_task_alternate_accounting_compressed(corpse_task
);
480 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_alternate_accounting_compressed
, sizeof(ledger_alternate_accounting_compressed
));
483 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE
, sizeof(ledger_purgeable_nonvolatile
), &uaddr
)) {
484 ledger_purgeable_nonvolatile
= get_task_purgeable_nonvolatile(corpse_task
);
485 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_purgeable_nonvolatile
, sizeof(ledger_purgeable_nonvolatile
));
488 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE_COMPRESSED
, sizeof(ledger_purgeable_nonvolatile_compressed
), &uaddr
)) {
489 ledger_purgeable_nonvolatile_compressed
= get_task_purgeable_nonvolatile_compressed(corpse_task
);
490 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_purgeable_nonvolatile_compressed
, sizeof(ledger_purgeable_nonvolatile_compressed
));
493 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PAGE_TABLE
, sizeof(ledger_page_table
), &uaddr
)) {
494 ledger_page_table
= get_task_page_table(corpse_task
);
495 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_page_table
, sizeof(ledger_page_table
));
498 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT
, sizeof(ledger_phys_footprint
), &uaddr
)) {
499 ledger_phys_footprint
= get_task_phys_footprint(corpse_task
);
500 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_phys_footprint
, sizeof(ledger_phys_footprint
));
503 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE
, sizeof(ledger_network_nonvolatile
), &uaddr
)) {
504 ledger_network_nonvolatile
= get_task_network_nonvolatile(corpse_task
);
505 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_network_nonvolatile
, sizeof(ledger_network_nonvolatile
));
508 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE_COMPRESSED
, sizeof(ledger_network_nonvolatile_compressed
), &uaddr
)) {
509 ledger_network_nonvolatile_compressed
= get_task_network_nonvolatile_compressed(corpse_task
);
510 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_network_nonvolatile_compressed
, sizeof(ledger_network_nonvolatile_compressed
));
513 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_WIRED_MEM
, sizeof(ledger_wired_mem
), &uaddr
)) {
514 ledger_wired_mem
= get_task_wired_mem(corpse_task
);
515 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_wired_mem
, sizeof(ledger_wired_mem
));
518 bzero(&pwqinfo
, sizeof(struct proc_workqueueinfo
));
519 retval
= fill_procworkqueue(p
, &pwqinfo
);
521 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_WORKQUEUEINFO
, sizeof(struct proc_workqueueinfo
), &uaddr
)) {
522 kcdata_memcpy(crash_info_ptr
, uaddr
, &pwqinfo
, sizeof(struct proc_workqueueinfo
));
526 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RESPONSIBLE_PID
, sizeof(p
->p_responsible_pid
), &uaddr
)) {
527 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_responsible_pid
, sizeof(p
->p_responsible_pid
));
530 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_PERSONA_ID
, sizeof(uid_t
), &uaddr
)) {
531 uid_t persona_id
= proc_persona_id(p
);
532 kcdata_memcpy(crash_info_ptr
, uaddr
, &persona_id
, sizeof(persona_id
));
535 #if CONFIG_COALITIONS
536 if (KERN_SUCCESS
== kcdata_get_memory_addr_for_array(crash_info_ptr
, TASK_CRASHINFO_COALITION_ID
, sizeof(uint64_t), COALITION_NUM_TYPES
, &uaddr
)) {
537 uint64_t coalition_ids
[COALITION_NUM_TYPES
];
538 task_coalition_ids(p
->task
, coalition_ids
);
539 kcdata_memcpy(crash_info_ptr
, uaddr
, coalition_ids
, sizeof(coalition_ids
));
541 #endif /* CONFIG_COALITIONS */
543 #if CONFIG_MEMORYSTATUS
544 memstat_dirty_flags
= memorystatus_dirty_get(p
, FALSE
);
545 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_DIRTY_FLAGS
, sizeof(memstat_dirty_flags
), &uaddr
)) {
546 kcdata_memcpy(crash_info_ptr
, uaddr
, &memstat_dirty_flags
, sizeof(memstat_dirty_flags
));
550 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_MEMORY_LIMIT_INCREASE
, sizeof(p
->p_memlimit_increase
), &uaddr
)) {
551 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_memlimit_increase
, sizeof(p
->p_memlimit_increase
));
554 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT
, sizeof(ledger_tagged_footprint
), &uaddr
)) {
555 ledger_tagged_footprint
= get_task_tagged_footprint(corpse_task
);
556 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_tagged_footprint
, sizeof(ledger_tagged_footprint
));
559 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT_COMPRESSED
, sizeof(ledger_tagged_footprint_compressed
), &uaddr
)) {
560 ledger_tagged_footprint_compressed
= get_task_tagged_footprint_compressed(corpse_task
);
561 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_tagged_footprint_compressed
, sizeof(ledger_tagged_footprint_compressed
));
564 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT
, sizeof(ledger_media_footprint
), &uaddr
)) {
565 ledger_media_footprint
= get_task_media_footprint(corpse_task
);
566 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_media_footprint
, sizeof(ledger_media_footprint
));
569 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT_COMPRESSED
, sizeof(ledger_media_footprint_compressed
), &uaddr
)) {
570 ledger_media_footprint_compressed
= get_task_media_footprint_compressed(corpse_task
);
571 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_media_footprint_compressed
, sizeof(ledger_media_footprint_compressed
));
574 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT
, sizeof(ledger_graphics_footprint
), &uaddr
)) {
575 ledger_graphics_footprint
= get_task_graphics_footprint(corpse_task
);
576 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_graphics_footprint
, sizeof(ledger_graphics_footprint
));
579 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT_COMPRESSED
, sizeof(ledger_graphics_footprint_compressed
), &uaddr
)) {
580 ledger_graphics_footprint_compressed
= get_task_graphics_footprint_compressed(corpse_task
);
581 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_graphics_footprint_compressed
, sizeof(ledger_graphics_footprint_compressed
));
584 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT
, sizeof(ledger_neural_footprint
), &uaddr
)) {
585 ledger_neural_footprint
= get_task_neural_footprint(corpse_task
);
586 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_neural_footprint
, sizeof(ledger_neural_footprint
));
589 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT_COMPRESSED
, sizeof(ledger_neural_footprint_compressed
), &uaddr
)) {
590 ledger_neural_footprint_compressed
= get_task_neural_footprint_compressed(corpse_task
);
591 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_neural_footprint_compressed
, sizeof(ledger_neural_footprint_compressed
));
594 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_MEMORYSTATUS_EFFECTIVE_PRIORITY
, sizeof(p
->p_memstat_effectivepriority
), &uaddr
)) {
595 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_memstat_effectivepriority
, sizeof(p
->p_memstat_effectivepriority
));
598 if (p
->p_exit_reason
!= OS_REASON_NULL
&& reason
== OS_REASON_NULL
) {
599 reason
= p
->p_exit_reason
;
601 if (reason
!= OS_REASON_NULL
) {
602 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, EXIT_REASON_SNAPSHOT
, sizeof(struct exit_reason_snapshot
), &uaddr
)) {
603 struct exit_reason_snapshot ers
= {
604 .ers_namespace
= reason
->osr_namespace
,
605 .ers_code
= reason
->osr_code
,
606 .ers_flags
= reason
->osr_flags
609 kcdata_memcpy(crash_info_ptr
, uaddr
, &ers
, sizeof(ers
));
612 if (reason
->osr_kcd_buf
!= 0) {
613 uint32_t reason_buf_size
= (uint32_t)kcdata_memory_get_used_bytes(&reason
->osr_kcd_descriptor
);
614 assert(reason_buf_size
!= 0);
616 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, KCDATA_TYPE_NESTED_KCDATA
, reason_buf_size
, &uaddr
)) {
617 kcdata_memcpy(crash_info_ptr
, uaddr
, reason
->osr_kcd_buf
, reason_buf_size
);
623 if (KERN_SUCCESS
== kcdata_get_memory_addr_for_array(crash_info_ptr
, TASK_CRASHINFO_UDATA_PTRS
,
624 sizeof(uint64_t), num_udata
, &uaddr
)) {
625 kcdata_memcpy(crash_info_ptr
, uaddr
, udata_buffer
, sizeof(uint64_t) * num_udata
);
631 * We only parse exit reason kcdata blobs for launchd when it dies
632 * and we're going to panic.
634 * Meant to be called immediately before panicking.
637 launchd_exit_reason_get_string_desc(os_reason_t exit_reason
)
641 if (exit_reason
== OS_REASON_NULL
|| exit_reason
->osr_kcd_buf
== NULL
||
642 exit_reason
->osr_bufsize
== 0) {
646 iter
= kcdata_iter(exit_reason
->osr_kcd_buf
, exit_reason
->osr_bufsize
);
647 if (!kcdata_iter_valid(iter
)) {
648 #if DEBUG || DEVELOPMENT
649 printf("launchd exit reason has invalid exit reason buffer\n");
654 if (kcdata_iter_type(iter
) != KCDATA_BUFFER_BEGIN_OS_REASON
) {
655 #if DEBUG || DEVELOPMENT
656 printf("launchd exit reason buffer type mismatch, expected %d got %d\n",
657 KCDATA_BUFFER_BEGIN_OS_REASON
, kcdata_iter_type(iter
));
662 iter
= kcdata_iter_find_type(iter
, EXIT_REASON_USER_DESC
);
663 if (!kcdata_iter_valid(iter
)) {
667 return (char *)kcdata_iter_payload(iter
);
672 launchd_crashed_panic(proc_t p
, int rv
)
674 char *launchd_exit_reason_desc
= launchd_exit_reason_get_string_desc(p
->p_exit_reason
);
676 if (p
->p_exit_reason
== OS_REASON_NULL
) {
677 printf("pid 1 exited -- no exit reason available -- (signal %d, exit %d)\n",
678 WTERMSIG(rv
), WEXITSTATUS(rv
));
680 printf("pid 1 exited -- exit reason namespace %d subcode 0x%llx, description %s\n",
681 p
->p_exit_reason
->osr_namespace
, p
->p_exit_reason
->osr_code
, launchd_exit_reason_desc
?
682 launchd_exit_reason_desc
: "none");
685 const char *launchd_crashed_prefix_str
;
687 if (strnstr(p
->p_name
, "preinit", sizeof(p
->p_name
))) {
688 launchd_crashed_prefix_str
= "LTE preinit process exited";
690 launchd_crashed_prefix_str
= "initproc exited";
693 #if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
695 * For debugging purposes, generate a core file of initproc before
696 * panicking. Leave at least 300 MB free on the root volume, and ignore
697 * the process's corefile ulimit. fsync() the file to ensure it lands on disk
698 * before the panic hits.
702 uint64_t coredump_start
= mach_absolute_time();
703 uint64_t coredump_end
;
705 clock_usec_t tv_usec
;
709 err
= coredump(p
, 300, COREDUMP_IGNORE_ULIMIT
| COREDUMP_FULLFSYNC
);
711 coredump_end
= mach_absolute_time();
713 absolutetime_to_microtime(coredump_end
- coredump_start
, &tv_sec
, &tv_usec
);
715 tv_msec
= tv_usec
/ 1000;
718 printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
719 err
, (uint32_t)tv_sec
, tv_msec
);
721 printf("Generated initproc core file in %d.%03d seconds\n",
722 (uint32_t)tv_sec
, tv_msec
);
724 #endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
726 sync(p
, (void *)NULL
, (int *)NULL
);
728 if (p
->p_exit_reason
== OS_REASON_NULL
) {
729 panic_with_options(0, NULL
, DEBUGGER_OPTION_INITPROC_PANIC
, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
730 launchd_crashed_prefix_str
, WTERMSIG(rv
), WEXITSTATUS(rv
), ((p
->p_csflags
& CS_KILLED
) ? "CS_KILLED" : ""));
732 panic_with_options(0, NULL
, DEBUGGER_OPTION_INITPROC_PANIC
, "%s %s -- exit reason namespace %d subcode 0x%llx description: %." LAUNCHD_PANIC_REASON_STRING_MAXLEN
"s",
733 ((p
->p_csflags
& CS_KILLED
) ? "CS_KILLED" : ""),
734 launchd_crashed_prefix_str
, p
->p_exit_reason
->osr_namespace
, p
->p_exit_reason
->osr_code
,
735 launchd_exit_reason_desc
? launchd_exit_reason_desc
: "none");
739 #define OS_REASON_IFLAG_USER_FAULT 0x1
741 #define OS_REASON_TOTAL_USER_FAULTS_PER_PROC 5
744 abort_with_payload_internal(proc_t p
,
745 uint32_t reason_namespace
, uint64_t reason_code
,
746 user_addr_t payload
, uint32_t payload_size
,
747 user_addr_t reason_string
, uint64_t reason_flags
,
748 uint32_t internal_flags
)
750 os_reason_t exit_reason
= OS_REASON_NULL
;
751 kern_return_t kr
= KERN_SUCCESS
;
753 if (internal_flags
& OS_REASON_IFLAG_USER_FAULT
) {
754 uint32_t old_value
= atomic_load_explicit(&p
->p_user_faults
,
755 memory_order_relaxed
);
757 if (old_value
>= OS_REASON_TOTAL_USER_FAULTS_PER_PROC
) {
760 // this reloads the value in old_value
761 if (atomic_compare_exchange_strong_explicit(&p
->p_user_faults
,
762 &old_value
, old_value
+ 1, memory_order_relaxed
,
763 memory_order_relaxed
)) {
769 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
770 p
->p_pid
, reason_namespace
,
773 exit_reason
= build_userspace_exit_reason(reason_namespace
, reason_code
,
774 payload
, payload_size
, reason_string
, reason_flags
| OS_REASON_FLAG_ABORT
);
776 if (internal_flags
& OS_REASON_IFLAG_USER_FAULT
) {
777 mach_exception_code_t code
= 0;
779 EXC_GUARD_ENCODE_TYPE(code
, GUARD_TYPE_USER
); /* simulated EXC_GUARD */
780 EXC_GUARD_ENCODE_FLAVOR(code
, 0);
781 EXC_GUARD_ENCODE_TARGET(code
, reason_namespace
);
783 if (exit_reason
== OS_REASON_NULL
) {
784 kr
= KERN_RESOURCE_SHORTAGE
;
786 kr
= task_violated_guard(code
, reason_code
, exit_reason
);
788 os_reason_free(exit_reason
);
791 * We use SIGABRT (rather than calling exit directly from here) so that
792 * the debugger can catch abort_with_{reason,payload} calls.
794 psignal_try_thread_with_reason(p
, current_thread(), SIGABRT
, exit_reason
);
800 case KERN_NOT_SUPPORTED
:
802 case KERN_INVALID_ARGUMENT
:
804 case KERN_RESOURCE_SHORTAGE
:
811 abort_with_payload(struct proc
*cur_proc
, struct abort_with_payload_args
*args
,
812 __unused
void *retval
)
814 abort_with_payload_internal(cur_proc
, args
->reason_namespace
,
815 args
->reason_code
, args
->payload
, args
->payload_size
,
816 args
->reason_string
, args
->reason_flags
, 0);
822 os_fault_with_payload(struct proc
*cur_proc
,
823 struct os_fault_with_payload_args
*args
, __unused
int *retval
)
825 return abort_with_payload_internal(cur_proc
, args
->reason_namespace
,
826 args
->reason_code
, args
->payload
, args
->payload_size
,
827 args
->reason_string
, args
->reason_flags
, OS_REASON_IFLAG_USER_FAULT
);
835 __attribute__((noreturn
))
837 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
839 p
->p_xhighbits
= ((uint32_t)(uap
->rval
) & 0xFF000000) >> 24;
840 exit1(p
, W_EXITCODE((uint32_t)uap
->rval
, 0), retval
);
842 thread_exception_return();
845 thread_block(THREAD_CONTINUE_NULL
);
851 * Exit: deallocate address space and other resources, change proc state
852 * to zombie, and unlink proc from allproc and parent's lists. Save exit
853 * status and rusage for wait(). Check for child processes and orphan them.
856 exit1(proc_t p
, int rv
, int *retval
)
858 return exit1_internal(p
, rv
, retval
, TRUE
, TRUE
, 0);
862 exit1_internal(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
865 return exit_with_reason(p
, rv
, retval
, thread_can_terminate
, perf_notify
, jetsam_flags
, OS_REASON_NULL
);
869 * NOTE: exit_with_reason drops a reference on the passed exit_reason
872 exit_with_reason(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
873 int jetsam_flags
, struct os_reason
*exit_reason
)
875 thread_t self
= current_thread();
876 struct task
*task
= p
->task
;
881 * If a thread in this task has already
882 * called exit(), then halt any others
886 ut
= get_bsdthread_info(self
);
887 if ((p
== current_proc()) &&
888 (ut
->uu_flag
& UT_VFORK
)) {
889 os_reason_free(exit_reason
);
890 if (!thread_can_terminate
) {
895 vfork_return(p
, retval
, p
->p_pid
);
896 unix_syscall_return(0);
901 * The parameter list of audit_syscall_exit() was augmented to
902 * take the Darwin syscall number as the first parameter,
903 * which is currently required by mac_audit_postselect().
907 * The BSM token contains two components: an exit status as passed
908 * to exit(), and a return value to indicate what sort of exit it
909 * was. The exit status is WEXITSTATUS(rv), but it's not clear
910 * what the return value is.
912 AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0);
914 * TODO: what to audit here when jetsam calls exit and the uthread,
915 * 'ut' does not belong to the proc, 'p'.
917 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
919 DTRACE_PROC1(exit
, int, CLD_EXITED
);
921 /* mark process is going to exit and pull out of DBG/disk throttle */
922 /* TODO: This should be done after becoming exit thread */
923 proc_set_task_policy(p
->task
, TASK_POLICY_ATTRIBUTE
,
924 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
927 error
= proc_transstart(p
, 1, (jetsam_flags
? 1 : 0));
928 if (error
== EDEADLK
) {
930 * If proc_transstart() returns EDEADLK, then another thread
931 * is either exec'ing or exiting. Return an error and allow
932 * the other thread to continue.
935 os_reason_free(exit_reason
);
936 if (current_proc() == p
) {
937 if (p
->exit_thread
== self
) {
938 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
939 p
->p_comm
, p
->p_pid
);
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
;
1075 /* Notify the perf server? */
1077 (void)sys_perf_notify(self
, p
->p_pid
);
1081 /* stash the usage into corpse data if making_corpse == true */
1082 if (create_corpse
== TRUE
) {
1083 kr
= task_mark_corpse(p
->task
);
1084 if (kr
!= KERN_SUCCESS
) {
1085 if (kr
== KERN_NO_SPACE
) {
1086 printf("Process[%d] has no vm space for corpse info.\n", p
->p_pid
);
1087 } else if (kr
== KERN_NOT_SUPPORTED
) {
1088 printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", p
->p_pid
);
1090 printf("Process[%d] crashed: %s. Too many corpses being created.\n", p
->p_pid
, p
->p_comm
);
1092 create_corpse
= FALSE
;
1097 * Before this process becomes a zombie, stash resource usage
1098 * stats in the proc for external observers to query
1099 * via proc_pid_rusage().
1101 * If the zombie allocation fails, just punt the stats.
1103 rup
= zalloc(zombie_zone
);
1104 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
1105 rup
->ri
.ri_phys_footprint
= 0;
1106 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
1108 * Make the rusage_info visible to external observers
1109 * only after it has been completely filled in.
1113 if (create_corpse
) {
1114 int est_knotes
= 0, num_knotes
= 0;
1115 uint64_t *buffer
= NULL
;
1116 uint32_t buf_size
= 0;
1118 /* Get all the udata pointers from kqueue */
1119 est_knotes
= kevent_proc_copy_uptrs(p
, NULL
, 0);
1120 if (est_knotes
> 0) {
1121 buf_size
= (uint32_t)((est_knotes
+ 32) * sizeof(uint64_t));
1122 buffer
= kheap_alloc(KHEAP_TEMP
, buf_size
, Z_WAITOK
);
1123 num_knotes
= kevent_proc_copy_uptrs(p
, buffer
, buf_size
);
1124 if (num_knotes
> est_knotes
+ 32) {
1125 num_knotes
= est_knotes
+ 32;
1129 /* Update the code, subcode based on exit reason */
1130 proc_update_corpse_exception_codes(p
, &code
, &subcode
);
1131 populate_corpse_crashinfo(p
, p
->task
, rup
,
1132 code
, subcode
, buffer
, num_knotes
, NULL
);
1133 if (buffer
!= NULL
) {
1134 kheap_free(KHEAP_TEMP
, buffer
, buf_size
);
1138 * Remove proc from allproc queue and from pidhash chain.
1139 * Need to do this before we do anything that can block.
1140 * Not doing causes things like mount() find this on allproc
1141 * in partially cleaned state.
1146 #if CONFIG_MEMORYSTATUS
1147 proc_memorystatus_remove(p
);
1150 LIST_REMOVE(p
, p_list
);
1151 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1152 /* will not be visible via proc_find */
1153 p
->p_listflag
|= P_LIST_EXITED
;
1161 * If parent is waiting for us to exit or exec,
1162 * P_LPPWAIT is set; we will wakeup the parent below.
1165 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1166 p
->p_sigignore
= ~(sigcantmask
);
1176 struct task
*task
= p
->task
;
1177 vnode_t tvp
= NULLVP
;
1179 struct session
*sessp
;
1180 struct uthread
* uth
;
1185 uth
= current_uthread();
1188 proc_transstart(p
, 1, 0);
1189 if (!(p
->p_lflag
& P_LEXIT
)) {
1191 * This can happen if a thread_terminate() occurs
1192 * in a single-threaded process.
1194 p
->p_lflag
|= P_LEXIT
;
1195 proc_transend(p
, 1);
1197 proc_prepareexit(p
, 0, TRUE
);
1198 (void) task_terminate_internal(task
);
1201 proc_transend(p
, 1);
1204 p
->p_lflag
|= P_LPEXIT
;
1207 * Other kernel threads may be in the middle of signalling this process.
1208 * Wait for those threads to wrap it up before making the process
1209 * disappear on them.
1211 if ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 0)) {
1213 while ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 1)) {
1214 msleep(&p
->p_sigmask
, &p
->p_mlock
, PWAIT
, "proc_sigdrain", NULL
);
1221 exitval
= p
->p_xstat
;
1222 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1223 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_START
,
1224 pid
, exitval
, 0, 0, 0);
1227 dtrace_proc_exit(p
);
1231 * need to cancel async IO requests that can be cancelled and wait for those
1232 * already active. MAY BLOCK!
1237 /* if any pending cpu limits action, clear it */
1238 task_clear_cpuusage(p
->task
, TRUE
);
1240 workq_mark_exiting(p
);
1245 * Close open files and release open-file table.
1251 * Once all the knotes, kqueues & workloops are destroyed, get rid of the
1256 if (uth
->uu_lowpri_window
) {
1258 * task is marked as a low priority I/O type
1259 * and the I/O we issued while in flushing files on close
1260 * collided with normal I/O operations...
1261 * no need to throttle this thread since its going away
1262 * but we do need to update our bookeeping w/r to throttled threads
1264 throttle_lowpri_io(0);
1267 if (p
->p_lflag
& P_LNSPACE_RESOLVER
) {
1269 * The namespace resolver is exiting; there may be
1270 * outstanding materialization requests to clean up.
1272 nspace_resolver_exited(p
);
1276 /* Close ref SYSV Shared memory*/
1282 /* Release SYSV semaphores */
1287 pth_proc_hashdelete(p
);
1290 sessp
= proc_session(p
);
1291 if (SESS_LEADER(p
, sessp
)) {
1292 if (sessp
->s_ttyvp
!= NULLVP
) {
1293 struct vnode
*ttyvp
;
1296 struct vfs_context context
;
1300 * Controlling process.
1301 * Signal foreground pgrp,
1302 * drain controlling terminal
1303 * and revoke access to controlling terminal.
1305 session_lock(sessp
);
1306 tp
= SESSION_TP(sessp
);
1307 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1308 session_unlock(sessp
);
1310 tty_pgsignal(tp
, SIGHUP
, 1);
1312 session_lock(sessp
);
1313 tp
= SESSION_TP(sessp
);
1315 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
1316 sessp
->s_flags
&= ~S_CTTYREF
;
1317 ttyvp
= sessp
->s_ttyvp
;
1318 ttyvid
= sessp
->s_ttyvid
;
1319 sessp
->s_ttyvp
= NULLVP
;
1320 sessp
->s_ttyvid
= 0;
1321 sessp
->s_ttyp
= TTY_NULL
;
1322 sessp
->s_ttypgrpid
= NO_PID
;
1323 session_unlock(sessp
);
1325 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1326 if (tp
!= TTY_NULL
) {
1332 context
.vc_thread
= NULL
;
1333 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1334 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1337 * Release the extra usecount taken in cttyopen.
1338 * usecount should be released after VNOP_REVOKE is called.
1339 * This usecount was taken to ensure that
1340 * the VNOP_REVOKE results in a close to
1341 * the tty since cttyclose is a no-op.
1346 kauth_cred_unref(&context
.vc_ucred
);
1353 session_lock(sessp
);
1354 sessp
->s_leader
= NULL
;
1355 session_unlock(sessp
);
1357 session_rele(sessp
);
1364 * Change RLIMIT_FSIZE for accounting/debugging. proc_limitsetcur_internal() will COW the current plimit
1365 * before making changes if the current plimit is shared. The COW'ed plimit will be freed
1366 * below by calling proc_limitdrop().
1368 proc_limitsetcur_internal(p
, RLIMIT_FSIZE
, RLIM_INFINITY
);
1370 (void)acct_process(p
);
1374 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) {
1375 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
1376 proc_shutdown_exitcount
--;
1377 if (proc_shutdown_exitcount
== 0) {
1378 wakeup(&proc_shutdown_exitcount
);
1382 /* wait till parentrefs are dropped and grant no more */
1383 proc_childdrainstart(p
);
1384 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1385 int reparentedtoinit
= (q
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
1386 if (q
->p_stat
== SZOMB
) {
1387 if (p
!= q
->p_pptr
) {
1388 panic("parent child linkage broken");
1390 /* check for sysctl zomb lookup */
1391 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1392 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1394 q
->p_listflag
|= P_LIST_WAITING
;
1396 * This is a named reference and it is not granted
1397 * if the reap is already in progress. So we get
1398 * the reference here exclusively and their can be
1399 * no waiters. So there is no need for a wakeup
1400 * after we are done. Also the reap frees the structure
1401 * and the proc struct cannot be used for wakeups as well.
1402 * It is safe to use q here as this is system reap
1404 (void)reap_child_locked(p
, q
, 1, reparentedtoinit
, 1, 0);
1407 * Traced processes are killed
1408 * since their existence means someone is messing up.
1410 if (q
->p_lflag
& P_LTRACED
) {
1414 * Take a reference on the child process to
1415 * ensure it doesn't exit and disappear between
1416 * the time we drop the list_lock and attempt
1417 * to acquire its proc_lock.
1419 if (proc_ref_locked(q
) != q
) {
1425 opp
= proc_find(q
->p_oppid
);
1426 if (opp
!= PROC_NULL
) {
1430 proc_reparentlocked(q
, opp
, 0, 0);
1433 /* original parent exited while traced */
1435 q
->p_listflag
|= P_LIST_DEADPARENT
;
1438 proc_reparentlocked(q
, initproc
, 0, 0);
1442 q
->p_lflag
&= ~P_LTRACED
;
1444 if (q
->sigwait_thread
) {
1445 thread_t thread
= q
->sigwait_thread
;
1449 * The sigwait_thread could be stopped at a
1450 * breakpoint. Wake it up to kill.
1451 * Need to do this as it could be a thread which is not
1452 * the first thread in the task. So any attempts to kill
1453 * the process would result into a deadlock on q->sigwait.
1455 thread_resume(thread
);
1456 clear_wait(thread
, THREAD_INTERRUPTED
);
1457 threadsignal(thread
, SIGKILL
, 0, TRUE
);
1462 psignal(q
, SIGKILL
);
1464 proc_rele_locked(q
);
1466 q
->p_listflag
|= P_LIST_DEADPARENT
;
1467 proc_reparentlocked(q
, initproc
, 0, 1);
1472 proc_childdrainend(p
);
1477 * Notify MAC policies that proc is dead.
1478 * This should be replaced with proper label management
1479 * (rdar://problem/32126399).
1481 mac_proc_notify_exit(p
);
1485 * Release reference to text vnode
1489 if (tvp
!= NULLVP
) {
1494 * Save exit status and final rusage info, adding in child rusage
1495 * info and self times. If we were unable to allocate a zombie
1496 * structure, this information is lost.
1498 if (p
->p_ru
!= NULL
) {
1499 calcru(p
, &p
->p_stats
->p_ru
.ru_utime
, &p
->p_stats
->p_ru
.ru_stime
, NULL
);
1500 p
->p_ru
->ru
= p
->p_stats
->p_ru
;
1502 ruadd(&(p
->p_ru
->ru
), &p
->p_stats
->p_cru
);
1506 * Free up profiling buffers.
1509 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1515 for (; p1
!= NULL
; p1
= pn
) {
1517 kfree(p1
, sizeof *p1
);
1521 proc_free_realitimer(p
);
1524 * Other substructures are freed from wait().
1526 zfree(proc_stats_zone
, p
->p_stats
);
1529 zfree(proc_sigacts_zone
, p
->p_sigacts
);
1530 p
->p_sigacts
= NULL
;
1535 * Finish up by terminating the task
1536 * and halt this thread (only if a
1537 * member of the task exiting).
1539 p
->task
= TASK_NULL
;
1540 set_bsdtask_info(task
, NULL
);
1542 knote_hint
= NOTE_EXIT
| (p
->p_xstat
& 0xffff);
1543 proc_knote(p
, knote_hint
);
1545 /* mark the thread as the one that is doing proc_exit
1546 * no need to hold proc lock in uthread_free
1548 uth
->uu_flag
|= UT_PROCEXIT
;
1550 * Notify parent that we're gone.
1552 pp
= proc_parent(p
);
1553 if (pp
->p_flag
& P_NOCLDWAIT
) {
1554 if (p
->p_ru
!= NULL
) {
1558 * If the parent is ignoring SIGCHLD, then POSIX requires
1559 * us to not add the resource usage to the parent process -
1560 * we are only going to hand it off to init to get reaped.
1561 * We should contest the standard in this case on the basis
1564 #else /* !3839178 */
1566 * Add child resource usage to parent before giving
1567 * zombie to init. If we were unable to allocate a
1568 * zombie structure, this information is lost.
1570 ruadd(&pp
->p_stats
->p_cru
, &p
->p_ru
->ru
);
1571 #endif /* !3839178 */
1572 update_rusage_info_child(&pp
->p_stats
->ri_child
, &p
->p_ru
->ri
);
1576 /* kernel can reap this one, no need to move it to launchd */
1578 p
->p_listflag
|= P_LIST_DEADPARENT
;
1581 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0 || p
->p_oppid
) {
1582 if (pp
!= initproc
) {
1584 pp
->si_pid
= p
->p_pid
;
1585 pp
->p_xhighbits
= p
->p_xhighbits
;
1587 pp
->si_status
= p
->p_xstat
;
1588 pp
->si_code
= CLD_EXITED
;
1590 * p_ucred usage is safe as it is an exiting process
1591 * and reference is dropped in reap
1593 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
1596 /* mark as a zombie */
1597 /* No need to take proc lock as all refs are drained and
1598 * no one except parent (reaping ) can look at this.
1599 * The write is to an int and is coherent. Also parent is
1600 * keyed off of list lock for reaping
1602 DTRACE_PROC2(exited
, proc_t
, p
, int, exitval
);
1603 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1604 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
1605 pid
, exitval
, 0, 0, 0);
1608 * The current process can be reaped so, no one
1609 * can depend on this
1612 psignal(pp
, SIGCHLD
);
1614 /* and now wakeup the parent */
1616 wakeup((caddr_t
)pp
);
1619 /* should be fine as parent proc would be initproc */
1620 /* mark as a zombie */
1621 /* No need to take proc lock as all refs are drained and
1622 * no one except parent (reaping ) can look at this.
1623 * The write is to an int and is coherent. Also parent is
1624 * keyed off of list lock for reaping
1626 DTRACE_PROC2(exited
, proc_t
, p
, int, exitval
);
1628 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1629 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
1630 pid
, exitval
, 0, 0, 0);
1631 /* check for sysctl zomb lookup */
1632 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1633 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1635 /* safe to use p as this is a system reap */
1637 p
->p_listflag
|= P_LIST_WAITING
;
1640 * This is a named reference and it is not granted
1641 * if the reap is already in progress. So we get
1642 * the reference here exclusively and their can be
1643 * no waiters. So there is no need for a wakeup
1644 * after we are done. AlsO the reap frees the structure
1645 * and the proc struct cannot be used for wakeups as well.
1646 * It is safe to use p here as this is system reap
1648 (void)reap_child_locked(pp
, p
, 1, 0, 1, 1);
1649 /* list lock dropped by reap_child_locked */
1651 if (uth
->uu_lowpri_window
) {
1653 * task is marked as a low priority I/O type and we've
1654 * somehow picked up another throttle during exit processing...
1655 * no need to throttle this thread since its going away
1656 * but we do need to update our bookeeping w/r to throttled threads
1658 throttle_lowpri_io(0);
1668 * Description: Given a process from which all status information needed
1669 * has already been extracted, if the process is a ptrace
1670 * attach process, detach it and give it back to its real
1671 * parent, else recover all resources remaining associated
1674 * Parameters: proc_t parent Parent of process being reaped
1675 * proc_t child Process to reap
1677 * Returns: 0 Process was not reaped because it
1678 * came from an attach
1679 * 1 Process was reaped
1682 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
)
1684 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
1691 * If we got the child via a ptrace 'attach',
1692 * we need to give it back to the old parent.
1694 * Exception: someone who has been reparented to launchd before being
1695 * ptraced can simply be reaped, refer to radar 5677288
1696 * p_oppid -> ptraced
1697 * trace_parent == initproc -> away from launchd
1698 * reparentedtoinit -> came to launchd by reparenting
1700 if (child
->p_oppid
) {
1705 oppid
= child
->p_oppid
;
1707 knote_hint
= NOTE_EXIT
| (child
->p_xstat
& 0xffff);
1710 if ((trace_parent
= proc_find(oppid
))
1711 && !((trace_parent
== initproc
) && reparentedtoinit
)) {
1712 if (trace_parent
!= initproc
) {
1714 * proc internal fileds and p_ucred usage safe
1715 * here as child is dead and is not reaped or
1718 proc_lock(trace_parent
);
1719 trace_parent
->si_pid
= child
->p_pid
;
1720 trace_parent
->si_status
= child
->p_xstat
;
1721 trace_parent
->si_code
= CLD_CONTINUED
;
1722 trace_parent
->si_uid
= kauth_cred_getruid(child
->p_ucred
);
1723 proc_unlock(trace_parent
);
1725 proc_reparentlocked(child
, trace_parent
, 1, 0);
1727 /* resend knote to original parent (and others) after reparenting */
1728 proc_knote(child
, knote_hint
);
1730 psignal(trace_parent
, SIGCHLD
);
1732 wakeup((caddr_t
)trace_parent
);
1733 child
->p_listflag
&= ~P_LIST_WAITING
;
1734 wakeup(&child
->p_stat
);
1736 proc_rele(trace_parent
);
1737 if ((locked
== 1) && (droplock
== 0)) {
1744 * If we can't reparent (e.g. the original parent exited while child was being debugged, or
1745 * original parent is the same as the debugger currently exiting), we still need to satisfy
1746 * the knote lifecycle for other observers on the system. While the debugger was attached,
1747 * the NOTE_EXIT would not have been broadcast during initial child termination.
1749 proc_knote(child
, knote_hint
);
1751 if (trace_parent
!= PROC_NULL
) {
1752 proc_rele(trace_parent
);
1756 #pragma clang diagnostic push
1757 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1758 proc_knote(child
, NOTE_REAP
);
1759 #pragma clang diagnostic pop
1761 proc_knote_drain(child
);
1768 * If the parent is ignoring SIGCHLD, then POSIX requires
1769 * us to not add the resource usage to the parent process -
1770 * we are only going to hand it off to init to get reaped.
1771 * We should contest the standard in this case on the basis
1774 if (!(parent
->p_flag
& P_NOCLDWAIT
))
1775 #endif /* 3839178 */
1776 ruadd(&parent
->p_stats
->p_cru
, &child
->p_ru
->ru
);
1777 update_rusage_info_child(&parent
->p_stats
->ri_child
, &child
->p_ru
->ri
);
1778 proc_unlock(parent
);
1779 zfree(zombie_zone
, child
->p_ru
);
1782 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
1785 AUDIT_SESSION_PROCEXIT(child
);
1788 * Decrement the count of procs running with this uid.
1789 * p_ucred usage is safe here as it is an exited process.
1790 * and refernce is dropped after these calls down below
1791 * (locking protection is provided by list lock held in chgproccnt)
1795 * persona_proc_drop calls chgproccnt(-1) on the persona uid,
1796 * and (+1) on the child->p_ucred uid
1798 persona_proc_drop(child
);
1800 (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1);
1802 os_reason_free(child
->p_exit_reason
);
1805 * Finally finished with old proc entry.
1806 * Unlink it from its process group and free it.
1811 LIST_REMOVE(child
, p_list
); /* off zombproc */
1812 parent
->p_childrencnt
--;
1813 LIST_REMOVE(child
, p_sibling
);
1814 /* If there are no more children wakeup parent */
1815 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
))) {
1816 wakeup((caddr_t
)parent
); /* with list lock held */
1818 child
->p_listflag
&= ~P_LIST_WAITING
;
1819 wakeup(&child
->p_stat
);
1821 /* Take it out of process hash */
1822 LIST_REMOVE(child
, p_hash
);
1823 child
->p_listflag
&= ~P_LIST_INHASH
;
1824 proc_checkdeadrefs(child
);
1829 * If a child zombie is being reaped because its parent
1830 * is exiting, make sure we update the list flag
1832 child
->p_listflag
|= P_LIST_DEADPARENT
;
1838 * Free up credentials.
1840 if (IS_VALID_CRED(child
->p_ucred
)) {
1841 kauth_cred_t tmp_ucred
= child
->p_ucred
;
1842 kauth_cred_unref(&tmp_ucred
);
1843 child
->p_ucred
= NOCRED
;
1846 lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
);
1847 lck_mtx_destroy(&child
->p_ucred_mlock
, proc_ucred_mlock_grp
);
1848 lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
);
1850 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1852 lck_spin_destroy(&child
->p_slock
, proc_slock_grp
);
1854 zfree(proc_zone
, child
);
1855 if ((locked
== 1) && (droplock
== 0)) {
1864 wait1continue(int result
)
1869 struct _wait4_data
*wait4_data
;
1870 struct wait4_nocancel_args
*uap
;
1878 thread
= current_thread();
1879 uth
= (struct uthread
*)get_bsdthread_info(thread
);
1881 wait4_data
= &uth
->uu_save
.uus_wait4_data
;
1882 uap
= wait4_data
->args
;
1883 retval
= wait4_data
->retval
;
1884 return wait4_nocancel(p
, uap
, retval
);
1888 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
1890 __pthread_testcancel(1);
1891 return wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
);
1895 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1902 struct _wait4_data
*wait4_data
;
1904 AUDIT_ARG(pid
, uap
->pid
);
1906 if (uap
->pid
== 0) {
1907 uap
->pid
= -q
->p_pgrpid
;
1916 PCHILDREN_FOREACH(q
, p
) {
1917 if (p
->p_sibling
.le_next
!= 0) {
1920 if (uap
->pid
!= WAIT_ANY
&&
1921 p
->p_pid
!= uap
->pid
&&
1922 p
->p_pgrpid
!= -(uap
->pid
)) {
1928 /* XXX This is racy because we don't get the lock!!!! */
1930 if (p
->p_listflag
& P_LIST_WAITING
) {
1931 /* we're not using a continuation here but we still need to stash
1932 * the args for stackshot. */
1933 uth
= current_uthread();
1934 wait4_data
= &uth
->uu_save
.uus_wait4_data
;
1935 wait4_data
->args
= uap
;
1936 thread_set_pending_block_hint(current_thread(), kThreadWaitOnProcess
);
1938 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1941 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1944 if (p
->p_stat
== SZOMB
) {
1945 int reparentedtoinit
= (p
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
1949 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
1953 retval
[0] = p
->p_pid
;
1955 /* Legacy apps expect only 8 bits of status */
1956 status
= 0xffff & p
->p_xstat
; /* convert to int */
1957 error
= copyout((caddr_t
)&status
,
1965 if (p
->p_ru
== NULL
) {
1968 if (IS_64BIT_PROCESS(q
)) {
1969 struct user64_rusage my_rusage
= {};
1970 munge_user64_rusage(&p
->p_ru
->ru
, &my_rusage
);
1971 error
= copyout((caddr_t
)&my_rusage
,
1975 struct user32_rusage my_rusage
= {};
1976 munge_user32_rusage(&p
->p_ru
->ru
, &my_rusage
);
1977 error
= copyout((caddr_t
)&my_rusage
,
1982 /* information unavailable? */
1988 /* Conformance change for 6577252.
1989 * When SIGCHLD is blocked and wait() returns because the status
1990 * of a child process is available and there are no other
1991 * children processes, then any pending SIGCHLD signal is cleared.
1993 if (sibling_count
== 0) {
1994 int mask
= sigmask(SIGCHLD
);
1995 uth
= current_uthread();
1997 if ((uth
->uu_sigmask
& mask
) != 0) {
1998 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1999 * This locking looks funny but it is protecting access to the
2000 * thread via p_uthlist.
2003 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
2009 (void)reap_child_locked(q
, p
, 0, reparentedtoinit
, 0, 0);
2013 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
2014 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
2017 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2022 p
->p_lflag
|= P_LWAITED
;
2024 retval
[0] = p
->p_pid
;
2026 status
= W_STOPCODE(p
->p_xstat
);
2027 error
= copyout((caddr_t
)&status
,
2036 * If we are waiting for continued processses, and this
2037 * process was continued
2039 if ((uap
->options
& WCONTINUED
) &&
2040 (p
->p_flag
& P_CONTINUED
)) {
2043 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2048 /* Prevent other process for waiting for this event */
2049 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2050 retval
[0] = p
->p_pid
;
2052 status
= W_STOPCODE(SIGCONT
);
2053 error
= copyout((caddr_t
)&status
,
2061 p
->p_listflag
&= ~P_LIST_WAITING
;
2064 /* list lock is held when we get here any which way */
2070 if (uap
->options
& WNOHANG
) {
2076 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2077 uth
= current_uthread();
2078 wait4_data
= &uth
->uu_save
.uus_wait4_data
;
2079 wait4_data
->args
= uap
;
2080 wait4_data
->retval
= retval
;
2082 thread_set_pending_block_hint(current_thread(), kThreadWaitOnProcess
);
2083 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
))) {
2090 p
->p_listflag
&= ~P_LIST_WAITING
;
2097 #define ASSERT_LCK_MTX_OWNED(lock) \
2098 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
2100 #define ASSERT_LCK_MTX_OWNED(lock) /* nothing */
2104 waitidcontinue(int result
)
2109 struct _waitid_data
*waitid_data
;
2110 struct waitid_nocancel_args
*uap
;
2118 thread
= current_thread();
2119 uth
= (struct uthread
*)get_bsdthread_info(thread
);
2121 waitid_data
= &uth
->uu_save
.uus_waitid_data
;
2122 uap
= waitid_data
->args
;
2123 retval
= waitid_data
->retval
;
2124 return waitid_nocancel(p
, uap
, retval
);
2128 * Description: Suspend the calling thread until one child of the process
2129 * containing the calling thread changes state.
2131 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
2132 * uap->id pid_t or gid_t or ignored
2133 * uap->infop Address of siginfo_t struct in
2134 * user space into which to return status
2135 * uap->options flag values
2137 * Returns: 0 Success
2138 * !0 Error returning status to user space
2141 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
2143 __pthread_testcancel(1);
2144 return waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
);
2148 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
,
2149 __unused
int32_t *retval
)
2151 user_siginfo_t siginfo
; /* siginfo data to return to caller */
2152 boolean_t caller64
= IS_64BIT_PROCESS(q
);
2157 struct _waitid_data
*waitid_data
;
2159 if (uap
->options
== 0 ||
2160 (uap
->options
& ~(WNOHANG
| WNOWAIT
| WCONTINUED
| WSTOPPED
| WEXITED
))) {
2161 return EINVAL
; /* bits set that aren't recognized */
2163 switch (uap
->idtype
) {
2164 case P_PID
: /* child with process ID equal to... */
2165 case P_PGID
: /* child with process group ID equal to... */
2166 if (((int)uap
->id
) < 0) {
2170 case P_ALL
: /* any child */
2179 PCHILDREN_FOREACH(q
, p
) {
2180 switch (uap
->idtype
) {
2181 case P_PID
: /* child with process ID equal to... */
2182 if (p
->p_pid
!= (pid_t
)uap
->id
) {
2186 case P_PGID
: /* child with process group ID equal to... */
2187 if (p
->p_pgrpid
!= (pid_t
)uap
->id
) {
2191 case P_ALL
: /* any child */
2195 /* XXX This is racy because we don't get the lock!!!! */
2198 * Wait collision; go to sleep and restart; used to maintain
2199 * the single return for waited process guarantee.
2201 if (p
->p_listflag
& P_LIST_WAITING
) {
2202 (void) msleep(&p
->p_stat
, proc_list_mlock
,
2203 PWAIT
, "waitidcoll", 0);
2206 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
2210 bzero(&siginfo
, sizeof(siginfo
));
2212 switch (p
->p_stat
) {
2213 case SZOMB
: /* Exited */
2214 if (!(uap
->options
& WEXITED
)) {
2219 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2223 siginfo
.si_signo
= SIGCHLD
;
2224 siginfo
.si_pid
= p
->p_pid
;
2226 /* If the child terminated abnormally due to a signal, the signum
2227 * needs to be preserved in the exit status.
2229 if (WIFSIGNALED(p
->p_xstat
)) {
2230 siginfo
.si_code
= WCOREDUMP(p
->p_xstat
) ?
2231 CLD_DUMPED
: CLD_KILLED
;
2232 siginfo
.si_status
= WTERMSIG(p
->p_xstat
);
2234 siginfo
.si_code
= CLD_EXITED
;
2235 siginfo
.si_status
= WEXITSTATUS(p
->p_xstat
) & 0x00FFFFFF;
2237 siginfo
.si_status
|= (((uint32_t)(p
->p_xhighbits
) << 24) & 0xFF000000);
2240 if ((error
= copyoutsiginfo(&siginfo
,
2241 caller64
, uap
->infop
)) != 0) {
2245 /* Prevent other process for waiting for this event? */
2246 if (!(uap
->options
& WNOWAIT
)) {
2247 (void) reap_child_locked(q
, p
, 0, 0, 0, 0);
2252 case SSTOP
: /* Stopped */
2254 * If we are not interested in stopped processes, then
2257 if (!(uap
->options
& WSTOPPED
)) {
2262 * If someone has already waited it, we lost a race
2263 * to be the one to return status.
2265 if ((p
->p_lflag
& P_LWAITED
) != 0) {
2270 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2274 siginfo
.si_signo
= SIGCHLD
;
2275 siginfo
.si_pid
= p
->p_pid
;
2276 siginfo
.si_status
= p
->p_xstat
; /* signal number */
2277 siginfo
.si_code
= CLD_STOPPED
;
2279 if ((error
= copyoutsiginfo(&siginfo
,
2280 caller64
, uap
->infop
)) != 0) {
2284 /* Prevent other process for waiting for this event? */
2285 if (!(uap
->options
& WNOWAIT
)) {
2287 p
->p_lflag
|= P_LWAITED
;
2292 default: /* All other states => Continued */
2293 if (!(uap
->options
& WCONTINUED
)) {
2298 * If the flag isn't set, then this process has not
2299 * been stopped and continued, or the status has
2300 * already been reaped by another caller of waitid().
2302 if ((p
->p_flag
& P_CONTINUED
) == 0) {
2307 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2311 siginfo
.si_signo
= SIGCHLD
;
2312 siginfo
.si_code
= CLD_CONTINUED
;
2314 siginfo
.si_pid
= p
->p_contproc
;
2315 siginfo
.si_status
= p
->p_xstat
;
2318 if ((error
= copyoutsiginfo(&siginfo
,
2319 caller64
, uap
->infop
)) != 0) {
2323 /* Prevent other process for waiting for this event? */
2324 if (!(uap
->options
& WNOWAIT
)) {
2325 OSBitAndAtomic(~((uint32_t)P_CONTINUED
),
2330 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
2332 /* Not a process we are interested in; go on to next child */
2334 p
->p_listflag
&= ~P_LIST_WAITING
;
2337 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
2339 /* No child processes that could possibly satisfy the request? */
2346 if (uap
->options
& WNOHANG
) {
2349 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2354 * The state of the siginfo structure in this case
2355 * is undefined. Some implementations bzero it, some
2356 * (like here) leave it untouched for efficiency.
2358 * Thus the most portable check for "no matching pid with
2359 * WNOHANG" is to store a zero into si_pid before
2360 * invocation, then check for a non-zero value afterwards.
2365 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2366 uth
= current_uthread();
2367 waitid_data
= &uth
->uu_save
.uus_waitid_data
;
2368 waitid_data
->args
= uap
;
2369 waitid_data
->retval
= retval
;
2371 if ((error
= msleep0(q
, proc_list_mlock
,
2372 PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)) != 0) {
2379 p
->p_listflag
&= ~P_LIST_WAITING
;
2386 * make process 'parent' the new parent of process 'child'.
2389 proc_reparentlocked(proc_t child
, proc_t parent
, int signallable
, int locked
)
2391 proc_t oldparent
= PROC_NULL
;
2393 if (child
->p_pptr
== parent
) {
2401 oldparent
= child
->p_pptr
;
2402 #if __PROC_INTERNAL_DEBUG
2403 if (oldparent
== PROC_NULL
) {
2404 panic("proc_reparent: process %p does not have a parent\n", child
);
2408 LIST_REMOVE(child
, p_sibling
);
2409 #if __PROC_INTERNAL_DEBUG
2410 if (oldparent
->p_childrencnt
== 0) {
2411 panic("process children count already 0\n");
2414 oldparent
->p_childrencnt
--;
2415 #if __PROC_INTERNAL_DEBUG
2416 if (oldparent
->p_childrencnt
< 0) {
2417 panic("process children count -ve\n");
2420 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
2421 parent
->p_childrencnt
++;
2422 child
->p_pptr
= parent
;
2423 child
->p_ppid
= parent
->p_pid
;
2427 if ((signallable
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
)) {
2428 psignal(initproc
, SIGCHLD
);
2436 * Exit: deallocate address space and other resources, change proc state
2437 * to zombie, and unlink proc from allproc and parent's lists. Save exit
2438 * status and rusage for wait(). Check for child processes and orphan them.
2442 vfork_exit(proc_t p
, int rv
)
2444 vfork_exit_internal(p
, rv
, 0);
2448 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
2450 thread_t self
= current_thread();
2452 struct task
*task
= p
->task
;
2457 * If a thread in this task has already
2458 * called exit(), then halt any others
2462 ut
= get_bsdthread_info(self
);
2466 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
2468 * This happens when a parent exits/killed and vfork is in progress
2469 * other threads. But shutdown code for ex has already called exit1()
2474 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
2477 if (forceexit
== 0) {
2479 * parent of a vfork child has already called exit() and the
2480 * thread that has vfork in proress terminates. So there is no
2481 * separate address space here and it has already been marked for
2482 * termination. This was never covered before and could cause problems
2483 * if we block here for outside code.
2485 /* Notify the perf server */
2486 (void)sys_perf_notify(self
, p
->p_pid
);
2490 * Remove proc from allproc queue and from pidhash chain.
2491 * Need to do this before we do anything that can block.
2492 * Not doing causes things like mount() find this on allproc
2493 * in partially cleaned state.
2498 #if CONFIG_MEMORYSTATUS
2499 proc_memorystatus_remove(p
);
2502 LIST_REMOVE(p
, p_list
);
2503 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
2504 /* will not be visible via proc_find */
2505 p
->p_listflag
|= P_LIST_EXITED
;
2511 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
2512 p
->p_sigignore
= ~0;
2517 /* begin vproc_exit */
2525 struct session
*sessp
;
2526 struct rusage_superset
*rup
;
2528 rup
= zalloc(zombie_zone
);
2533 * Close open files and release open-file table.
2538 sessp
= proc_session(p
);
2539 if (SESS_LEADER(p
, sessp
)) {
2540 panic("vfork child is session leader");
2542 session_rele(sessp
);
2549 * Change RLIMIT_FSIZE for accounting/debugging. proc_limitsetcur_internal() will COW the current plimit
2550 * before making changes if the current plimit is shared. The COW'ed plimit will be freed
2551 * below by calling proc_limitdrop().
2553 proc_limitsetcur_internal(p
, RLIMIT_FSIZE
, RLIM_INFINITY
);
2557 proc_childdrainstart(p
);
2558 while ((q
= p
->p_children
.lh_first
) != NULL
) {
2559 if (q
->p_stat
== SZOMB
) {
2560 if (p
!= q
->p_pptr
) {
2561 panic("parent child linkage broken");
2563 /* check for lookups by zomb sysctl */
2564 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2565 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2567 q
->p_listflag
|= P_LIST_WAITING
;
2569 * This is a named reference and it is not granted
2570 * if the reap is already in progress. So we get
2571 * the reference here exclusively and their can be
2572 * no waiters. So there is no need for a wakeup
2573 * after we are done. AlsO the reap frees the structure
2574 * and the proc struct cannot be used for wakeups as well.
2575 * It is safe to use q here as this is system reap
2577 (void)reap_child_locked(p
, q
, 1, 0, 1, 0);
2580 * Traced processes are killed
2581 * since their existence means someone is messing up.
2583 if (q
->p_lflag
& P_LTRACED
) {
2588 opp
= proc_find(q
->p_oppid
);
2589 if (opp
!= PROC_NULL
) {
2593 proc_reparentlocked(q
, opp
, 0, 0);
2596 /* original parent exited while traced */
2598 q
->p_listflag
|= P_LIST_DEADPARENT
;
2601 proc_reparentlocked(q
, initproc
, 0, 0);
2605 q
->p_lflag
&= ~P_LTRACED
;
2607 if (q
->sigwait_thread
) {
2608 thread_t thread
= q
->sigwait_thread
;
2612 * The sigwait_thread could be stopped at a
2613 * breakpoint. Wake it up to kill.
2614 * Need to do this as it could be a thread which is not
2615 * the first thread in the task. So any attempts to kill
2616 * the process would result into a deadlock on q->sigwait.
2618 thread_resume(thread
);
2619 clear_wait(thread
, THREAD_INTERRUPTED
);
2620 threadsignal(thread
, SIGKILL
, 0, TRUE
);
2625 psignal(q
, SIGKILL
);
2628 q
->p_listflag
|= P_LIST_DEADPARENT
;
2629 proc_reparentlocked(q
, initproc
, 0, 1);
2634 proc_childdrainend(p
);
2638 * Release reference to text vnode
2642 if (tvp
!= NULLVP
) {
2647 * Save exit status and final rusage info, adding in child rusage
2648 * info and self times. If we were unable to allocate a zombie
2649 * structure, this information is lost.
2652 rup
->ru
= p
->p_stats
->p_ru
;
2653 timerclear(&rup
->ru
.ru_utime
);
2654 timerclear(&rup
->ru
.ru_stime
);
2658 mach_task_basic_info_data_t tinfo
;
2659 task_thread_times_info_data_t ttimesinfo
;
2660 int task_info_stuff
, task_ttimes_stuff
;
2661 struct timeval ut
, st
;
2663 task_info_stuff
= MACH_TASK_BASIC_INFO_COUNT
;
2664 task_info(task
, MACH_TASK_BASIC_INFO
,
2665 &tinfo
, &task_info_stuff
);
2666 p
->p_ru
->ru
.ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
2667 p
->p_ru
->ru
.ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
2668 p
->p_ru
->ru
.ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
2669 p
->p_ru
->ru
.ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
2671 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
2672 task_info(task
, TASK_THREAD_TIMES_INFO
,
2673 &ttimesinfo
, &task_ttimes_stuff
);
2675 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
2676 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
2677 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
2678 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
2679 timeradd(&ut
, &p
->p_ru
->ru
.ru_utime
, &p
->p_ru
->ru
.ru_utime
);
2680 timeradd(&st
, &p
->p_ru
->ru
.ru_stime
, &p
->p_ru
->ru
.ru_stime
);
2684 ruadd(&rup
->ru
, &p
->p_stats
->p_cru
);
2686 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
2687 rup
->ri
.ri_phys_footprint
= 0;
2688 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
2691 * Now that we have filled in the rusage info, make it
2692 * visible to an external observer via proc_pid_rusage().
2698 * Free up profiling buffers.
2701 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
2707 for (; p1
!= NULL
; p1
= pn
) {
2709 kfree(p1
, sizeof *p1
);
2714 pth_proc_hashdelete(p
);
2717 proc_free_realitimer(p
);
2720 * Other substructures are freed from wait().
2722 zfree(proc_stats_zone
, p
->p_stats
);
2725 zfree(proc_sigacts_zone
, p
->p_sigacts
);
2726 p
->p_sigacts
= NULL
;
2728 FREE(p
->p_subsystem_root_path
, M_SBUF
);
2729 p
->p_subsystem_root_path
= NULL
;
2734 * Finish up by terminating the task
2735 * and halt this thread (only if a
2736 * member of the task exiting).
2738 p
->task
= TASK_NULL
;
2741 * Notify parent that we're gone.
2743 pp
= proc_parent(p
);
2744 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
2745 if (pp
!= initproc
) {
2747 pp
->si_pid
= p
->p_pid
;
2748 pp
->p_xhighbits
= p
->p_xhighbits
;
2750 pp
->si_status
= p
->p_xstat
;
2751 pp
->si_code
= CLD_EXITED
;
2753 * p_ucred usage is safe as it is an exiting process
2754 * and reference is dropped in reap
2756 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
2759 /* mark as a zombie */
2760 /* mark as a zombie */
2761 /* No need to take proc lock as all refs are drained and
2762 * no one except parent (reaping ) can look at this.
2763 * The write is to an int and is coherent. Also parent is
2764 * keyed off of list lock for reaping
2768 psignal(pp
, SIGCHLD
);
2770 /* and now wakeup the parent */
2772 wakeup((caddr_t
)pp
);
2776 /* check for lookups by zomb sysctl */
2777 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2778 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2781 p
->p_listflag
|= P_LIST_WAITING
;
2784 * This is a named reference and it is not granted
2785 * if the reap is already in progress. So we get
2786 * the reference here exclusively and their can be
2787 * no waiters. So there is no need for a wakeup
2788 * after we are done. AlsO the reap frees the structure
2789 * and the proc struct cannot be used for wakeups as well.
2790 * It is safe to use p here as this is system reap
2792 (void)reap_child_locked(pp
, p
, 0, 0, 1, 1);
2793 /* list lock dropped by reap_child_locked */
2801 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
2802 * process. We munge the kernel version of rusage into the
2805 __private_extern__
void
2806 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
2808 /* Zero-out struct so that padding is cleared */
2809 bzero(a_user_rusage_p
, sizeof(struct user64_rusage
));
2811 /* timeval changes size, so utime and stime need special handling */
2812 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2813 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2814 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2815 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2817 * everything else can be a direct assign, since there is no loss
2818 * of precision implied boing 32->64.
2820 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2821 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2822 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2823 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2824 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2825 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2826 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2827 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2828 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2829 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2830 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2831 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2832 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2833 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
2836 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2837 __private_extern__
void
2838 munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
)
2840 bzero(a_user_rusage_p
, sizeof(struct user32_rusage
));
2842 /* timeval changes size, so utime and stime need special handling */
2843 a_user_rusage_p
->ru_utime
.tv_sec
= (user32_time_t
)a_rusage_p
->ru_utime
.tv_sec
;
2844 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2845 a_user_rusage_p
->ru_stime
.tv_sec
= (user32_time_t
)a_rusage_p
->ru_stime
.tv_sec
;
2846 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2848 * everything else can be a direct assign. We currently ignore
2849 * the loss of precision
2851 a_user_rusage_p
->ru_maxrss
= (user32_long_t
)a_rusage_p
->ru_maxrss
;
2852 a_user_rusage_p
->ru_ixrss
= (user32_long_t
)a_rusage_p
->ru_ixrss
;
2853 a_user_rusage_p
->ru_idrss
= (user32_long_t
)a_rusage_p
->ru_idrss
;
2854 a_user_rusage_p
->ru_isrss
= (user32_long_t
)a_rusage_p
->ru_isrss
;
2855 a_user_rusage_p
->ru_minflt
= (user32_long_t
)a_rusage_p
->ru_minflt
;
2856 a_user_rusage_p
->ru_majflt
= (user32_long_t
)a_rusage_p
->ru_majflt
;
2857 a_user_rusage_p
->ru_nswap
= (user32_long_t
)a_rusage_p
->ru_nswap
;
2858 a_user_rusage_p
->ru_inblock
= (user32_long_t
)a_rusage_p
->ru_inblock
;
2859 a_user_rusage_p
->ru_oublock
= (user32_long_t
)a_rusage_p
->ru_oublock
;
2860 a_user_rusage_p
->ru_msgsnd
= (user32_long_t
)a_rusage_p
->ru_msgsnd
;
2861 a_user_rusage_p
->ru_msgrcv
= (user32_long_t
)a_rusage_p
->ru_msgrcv
;
2862 a_user_rusage_p
->ru_nsignals
= (user32_long_t
)a_rusage_p
->ru_nsignals
;
2863 a_user_rusage_p
->ru_nvcsw
= (user32_long_t
)a_rusage_p
->ru_nvcsw
;
2864 a_user_rusage_p
->ru_nivcsw
= (user32_long_t
)a_rusage_p
->ru_nivcsw
;
2868 kdp_wait4_find_process(thread_t thread
, __unused event64_t wait_event
, thread_waitinfo_t
*waitinfo
)
2870 assert(thread
!= NULL
);
2871 assert(waitinfo
!= NULL
);
2873 struct uthread
*ut
= get_bsdthread_info(thread
);
2874 waitinfo
->context
= 0;
2875 // ensure wmesg is consistent with a thread waiting in wait4
2876 assert(!strcmp(ut
->uu_wmesg
, "waitcoll") || !strcmp(ut
->uu_wmesg
, "wait"));
2877 struct wait4_nocancel_args
*args
= ut
->uu_save
.uus_wait4_data
.args
;
2878 // May not actually contain a pid; this is just the argument to wait4.
2879 // See man wait4 for other valid wait4 arguments.
2880 waitinfo
->owner
= args
->pid
;
2883 #if __has_feature(ptrauth_calls)
2885 exit_with_pac_exception(proc_t p
, exception_type_t exception
, mach_exception_code_t code
,
2886 mach_exception_subcode_t subcode
)
2888 thread_t self
= current_thread();
2889 struct uthread
*ut
= get_bsdthread_info(self
);
2891 os_reason_t exception_reason
= os_reason_create(OS_REASON_PAC_EXCEPTION
, (uint64_t)code
);
2892 assert(exception_reason
!= OS_REASON_NULL
);
2893 exception_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
2894 ut
->uu_exception
= exception
;
2896 ut
->uu_subcode
= subcode
;
2898 return exit_with_reason(p
, W_EXITCODE(0, SIGKILL
), (int *)NULL
, TRUE
, FALSE
,
2899 0, exception_reason
);
2901 #endif /* __has_feature(ptrauth_calls) */