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
);
183 * Things which should have prototypes in headers, but don't
185 void proc_exit(proc_t p
);
186 int wait1continue(int result
);
187 int waitidcontinue(int result
);
188 kern_return_t
sys_perf_notify(thread_t thread
, int pid
);
189 kern_return_t
task_exception_notify(exception_type_t exception
,
190 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
);
191 kern_return_t
task_violated_guard(mach_exception_code_t
, mach_exception_subcode_t
, void *);
193 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
196 * NOTE: Source and target may *NOT* overlap!
197 * XXX Should share code with bsd/dev/ppc/unix_signal.c
200 siginfo_user_to_user32(user_siginfo_t
*in
, user32_siginfo_t
*out
)
202 out
->si_signo
= in
->si_signo
;
203 out
->si_errno
= in
->si_errno
;
204 out
->si_code
= in
->si_code
;
205 out
->si_pid
= in
->si_pid
;
206 out
->si_uid
= in
->si_uid
;
207 out
->si_status
= in
->si_status
;
208 out
->si_addr
= CAST_DOWN_EXPLICIT(user32_addr_t
, in
->si_addr
);
209 /* following cast works for sival_int because of padding */
210 out
->si_value
.sival_ptr
= CAST_DOWN_EXPLICIT(user32_addr_t
, in
->si_value
.sival_ptr
);
211 out
->si_band
= in
->si_band
; /* range reduction */
215 siginfo_user_to_user64(user_siginfo_t
*in
, user64_siginfo_t
*out
)
217 out
->si_signo
= in
->si_signo
;
218 out
->si_errno
= in
->si_errno
;
219 out
->si_code
= in
->si_code
;
220 out
->si_pid
= in
->si_pid
;
221 out
->si_uid
= in
->si_uid
;
222 out
->si_status
= in
->si_status
;
223 out
->si_addr
= in
->si_addr
;
224 /* following cast works for sival_int because of padding */
225 out
->si_value
.sival_ptr
= in
->si_value
.sival_ptr
;
226 out
->si_band
= in
->si_band
; /* range reduction */
230 copyoutsiginfo(user_siginfo_t
*native
, boolean_t is64
, user_addr_t uaddr
)
233 user64_siginfo_t sinfo64
;
235 bzero(&sinfo64
, sizeof(sinfo64
));
236 siginfo_user_to_user64(native
, &sinfo64
);
237 return copyout(&sinfo64
, uaddr
, sizeof(sinfo64
));
239 user32_siginfo_t sinfo32
;
241 bzero(&sinfo32
, sizeof(sinfo32
));
242 siginfo_user_to_user32(native
, &sinfo32
);
243 return copyout(&sinfo32
, uaddr
, sizeof(sinfo32
));
248 gather_populate_corpse_crashinfo(proc_t p
, task_t corpse_task
,
249 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
,
250 uint64_t *udata_buffer
, int num_udata
, void *reason
)
252 struct rusage_superset rup
;
254 gather_rusage_info(p
, &rup
.ri
, RUSAGE_INFO_CURRENT
);
255 rup
.ri
.ri_phys_footprint
= 0;
256 populate_corpse_crashinfo(p
, corpse_task
, &rup
, code
, subcode
,
257 udata_buffer
, num_udata
, reason
);
261 proc_update_corpse_exception_codes(proc_t p
, mach_exception_data_type_t
*code
, mach_exception_data_type_t
*subcode
)
263 mach_exception_data_type_t code_update
= *code
;
264 mach_exception_data_type_t subcode_update
= *subcode
;
265 if (p
->p_exit_reason
== OS_REASON_NULL
) {
269 switch (p
->p_exit_reason
->osr_namespace
) {
270 case OS_REASON_JETSAM
:
271 if (p
->p_exit_reason
->osr_code
== JETSAM_REASON_MEMORY_PERPROCESSLIMIT
) {
272 /* Update the code with EXC_RESOURCE code for high memory watermark */
273 EXC_RESOURCE_ENCODE_TYPE(code_update
, RESOURCE_TYPE_MEMORY
);
274 EXC_RESOURCE_ENCODE_FLAVOR(code_update
, FLAVOR_HIGH_WATERMARK
);
275 EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update
, ((get_task_phys_footprint_limit(p
->task
)) >> 20));
286 *subcode
= subcode_update
;
290 mach_exception_data_type_t
291 proc_encode_exit_exception_code(proc_t p
)
293 uint64_t subcode
= 0;
295 if (p
->p_exit_reason
== OS_REASON_NULL
) {
299 /* Embed first 32 bits of osr_namespace and osr_code in exception code */
300 ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode
, p
->p_exit_reason
->osr_namespace
);
301 ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode
, p
->p_exit_reason
->osr_code
);
302 return (mach_exception_data_type_t
)subcode
;
306 populate_corpse_crashinfo(proc_t p
, task_t corpse_task
, struct rusage_superset
*rup
,
307 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
,
308 uint64_t *udata_buffer
, int num_udata
, os_reason_t reason
)
310 mach_vm_address_t uaddr
= 0;
311 mach_exception_data_type_t exc_codes
[EXCEPTION_CODE_MAX
];
313 exc_codes
[1] = subcode
;
315 struct proc_uniqidentifierinfo p_uniqidinfo
;
316 struct proc_workqueueinfo pwqinfo
;
318 uint64_t crashed_threadid
= task_corpse_get_crashed_thread_id(corpse_task
);
319 unsigned int pflags
= 0;
320 uint64_t max_footprint_mb
;
321 uint64_t max_footprint
;
323 uint64_t ledger_internal
;
324 uint64_t ledger_internal_compressed
;
325 uint64_t ledger_iokit_mapped
;
326 uint64_t ledger_alternate_accounting
;
327 uint64_t ledger_alternate_accounting_compressed
;
328 uint64_t ledger_purgeable_nonvolatile
;
329 uint64_t ledger_purgeable_nonvolatile_compressed
;
330 uint64_t ledger_page_table
;
331 uint64_t ledger_phys_footprint
;
332 uint64_t ledger_phys_footprint_lifetime_max
;
333 uint64_t ledger_network_nonvolatile
;
334 uint64_t ledger_network_nonvolatile_compressed
;
335 uint64_t ledger_wired_mem
;
337 void *crash_info_ptr
= task_get_corpseinfo(corpse_task
);
339 #if CONFIG_MEMORYSTATUS
340 int memstat_dirty_flags
= 0;
343 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_EXCEPTION_CODES
, sizeof(exc_codes
), &uaddr
)) {
344 kcdata_memcpy(crash_info_ptr
, uaddr
, exc_codes
, sizeof(exc_codes
));
347 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PID
, sizeof(p
->p_pid
), &uaddr
)) {
348 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_pid
, sizeof(p
->p_pid
));
351 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PPID
, sizeof(p
->p_ppid
), &uaddr
)) {
352 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_ppid
, sizeof(p
->p_ppid
));
355 /* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
356 if ((p
->p_exit_reason
== OS_REASON_NULL
) || !(p
->p_exit_reason
->osr_flags
& OS_REASON_FLAG_NO_CRASHED_TID
)) {
357 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CRASHED_THREADID
, sizeof(uint64_t), &uaddr
)) {
358 kcdata_memcpy(crash_info_ptr
, uaddr
, &crashed_threadid
, sizeof(uint64_t));
362 static_assert(sizeof(struct proc_uniqidentifierinfo
) == sizeof(struct crashinfo_proc_uniqidentifierinfo
));
364 kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_BSDINFOWITHUNIQID
, sizeof(struct proc_uniqidentifierinfo
), &uaddr
)) {
365 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
366 kcdata_memcpy(crash_info_ptr
, uaddr
, &p_uniqidinfo
, sizeof(struct proc_uniqidentifierinfo
));
369 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RUSAGE_INFO
, sizeof(rusage_info_current
), &uaddr
)) {
370 kcdata_memcpy(crash_info_ptr
, uaddr
, &rup
->ri
, sizeof(rusage_info_current
));
373 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_CSFLAGS
, sizeof(p
->p_csflags
), &uaddr
)) {
374 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_csflags
, sizeof(p
->p_csflags
));
377 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_NAME
, sizeof(p
->p_comm
), &uaddr
)) {
378 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_comm
, sizeof(p
->p_comm
));
381 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_STARTTIME
, sizeof(p
->p_start
), &uaddr
)) {
382 struct timeval64 t64
;
383 t64
.tv_sec
= (int64_t)p
->p_start
.tv_sec
;
384 t64
.tv_usec
= (int64_t)p
->p_start
.tv_usec
;
385 kcdata_memcpy(crash_info_ptr
, uaddr
, &t64
, sizeof(t64
));
388 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_USERSTACK
, sizeof(p
->user_stack
), &uaddr
)) {
389 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->user_stack
, sizeof(p
->user_stack
));
392 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_ARGSLEN
, sizeof(p
->p_argslen
), &uaddr
)) {
393 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_argslen
, sizeof(p
->p_argslen
));
396 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_ARGC
, sizeof(p
->p_argc
), &uaddr
)) {
397 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_argc
, sizeof(p
->p_argc
));
400 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_PATH
, MAXPATHLEN
, &uaddr
)) {
401 char *buf
= (char *) kalloc(MAXPATHLEN
);
403 bzero(buf
, MAXPATHLEN
);
404 proc_pidpathinfo_internal(p
, 0, buf
, MAXPATHLEN
, &retval
);
405 kcdata_memcpy(crash_info_ptr
, uaddr
, buf
, MAXPATHLEN
);
406 kfree(buf
, MAXPATHLEN
);
410 pflags
= p
->p_flag
& (P_LP64
| P_SUGID
);
411 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_FLAGS
, sizeof(pflags
), &uaddr
)) {
412 kcdata_memcpy(crash_info_ptr
, uaddr
, &pflags
, sizeof(pflags
));
415 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_UID
, sizeof(p
->p_uid
), &uaddr
)) {
416 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_uid
, sizeof(p
->p_uid
));
419 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_GID
, sizeof(p
->p_gid
), &uaddr
)) {
420 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_gid
, sizeof(p
->p_gid
));
423 cputype
= cpu_type() & ~CPU_ARCH_MASK
;
424 if (IS_64BIT_PROCESS(p
)) {
425 cputype
|= CPU_ARCH_ABI64
;
428 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CPUTYPE
, sizeof(cpu_type_t
), &uaddr
)) {
429 kcdata_memcpy(crash_info_ptr
, uaddr
, &cputype
, sizeof(cpu_type_t
));
432 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_MEMORY_LIMIT
, sizeof(max_footprint_mb
), &uaddr
)) {
433 max_footprint
= get_task_phys_footprint_limit(p
->task
);
434 max_footprint_mb
= max_footprint
>> 20;
435 kcdata_memcpy(crash_info_ptr
, uaddr
, &max_footprint_mb
, sizeof(max_footprint_mb
));
438 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT_LIFETIME_MAX
, sizeof(ledger_phys_footprint_lifetime_max
), &uaddr
)) {
439 ledger_phys_footprint_lifetime_max
= get_task_phys_footprint_lifetime_max(p
->task
);
440 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_phys_footprint_lifetime_max
, sizeof(ledger_phys_footprint_lifetime_max
));
443 // In the forking case, the current ledger info is copied into the corpse while the original task is suspended for consistency
444 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_INTERNAL
, sizeof(ledger_internal
), &uaddr
)) {
445 ledger_internal
= get_task_internal(corpse_task
);
446 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_internal
, sizeof(ledger_internal
));
449 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_INTERNAL_COMPRESSED
, sizeof(ledger_internal_compressed
), &uaddr
)) {
450 ledger_internal_compressed
= get_task_internal_compressed(corpse_task
);
451 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_internal_compressed
, sizeof(ledger_internal_compressed
));
454 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_IOKIT_MAPPED
, sizeof(ledger_iokit_mapped
), &uaddr
)) {
455 ledger_iokit_mapped
= get_task_iokit_mapped(corpse_task
);
456 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_iokit_mapped
, sizeof(ledger_iokit_mapped
));
459 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING
, sizeof(ledger_alternate_accounting
), &uaddr
)) {
460 ledger_alternate_accounting
= get_task_alternate_accounting(corpse_task
);
461 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_alternate_accounting
, sizeof(ledger_alternate_accounting
));
464 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING_COMPRESSED
, sizeof(ledger_alternate_accounting_compressed
), &uaddr
)) {
465 ledger_alternate_accounting_compressed
= get_task_alternate_accounting_compressed(corpse_task
);
466 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_alternate_accounting_compressed
, sizeof(ledger_alternate_accounting_compressed
));
469 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE
, sizeof(ledger_purgeable_nonvolatile
), &uaddr
)) {
470 ledger_purgeable_nonvolatile
= get_task_purgeable_nonvolatile(corpse_task
);
471 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_purgeable_nonvolatile
, sizeof(ledger_purgeable_nonvolatile
));
474 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE_COMPRESSED
, sizeof(ledger_purgeable_nonvolatile_compressed
), &uaddr
)) {
475 ledger_purgeable_nonvolatile_compressed
= get_task_purgeable_nonvolatile_compressed(corpse_task
);
476 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_purgeable_nonvolatile_compressed
, sizeof(ledger_purgeable_nonvolatile_compressed
));
479 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PAGE_TABLE
, sizeof(ledger_page_table
), &uaddr
)) {
480 ledger_page_table
= get_task_page_table(corpse_task
);
481 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_page_table
, sizeof(ledger_page_table
));
484 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT
, sizeof(ledger_phys_footprint
), &uaddr
)) {
485 ledger_phys_footprint
= get_task_phys_footprint(corpse_task
);
486 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_phys_footprint
, sizeof(ledger_phys_footprint
));
489 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE
, sizeof(ledger_network_nonvolatile
), &uaddr
)) {
490 ledger_network_nonvolatile
= get_task_network_nonvolatile(corpse_task
);
491 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_network_nonvolatile
, sizeof(ledger_network_nonvolatile
));
494 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE_COMPRESSED
, sizeof(ledger_network_nonvolatile_compressed
), &uaddr
)) {
495 ledger_network_nonvolatile_compressed
= get_task_network_nonvolatile_compressed(corpse_task
);
496 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_network_nonvolatile_compressed
, sizeof(ledger_network_nonvolatile_compressed
));
499 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_LEDGER_WIRED_MEM
, sizeof(ledger_wired_mem
), &uaddr
)) {
500 ledger_wired_mem
= get_task_wired_mem(corpse_task
);
501 kcdata_memcpy(crash_info_ptr
, uaddr
, &ledger_wired_mem
, sizeof(ledger_wired_mem
));
504 bzero(&pwqinfo
, sizeof(struct proc_workqueueinfo
));
505 retval
= fill_procworkqueue(p
, &pwqinfo
);
507 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_WORKQUEUEINFO
, sizeof(struct proc_workqueueinfo
), &uaddr
)) {
508 kcdata_memcpy(crash_info_ptr
, uaddr
, &pwqinfo
, sizeof(struct proc_workqueueinfo
));
512 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RESPONSIBLE_PID
, sizeof(p
->p_responsible_pid
), &uaddr
)) {
513 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_responsible_pid
, sizeof(p
->p_responsible_pid
));
516 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_PERSONA_ID
, sizeof(uid_t
), &uaddr
)) {
517 uid_t persona_id
= proc_persona_id(p
);
518 kcdata_memcpy(crash_info_ptr
, uaddr
, &persona_id
, sizeof(persona_id
));
521 #if CONFIG_COALITIONS
522 if (KERN_SUCCESS
== kcdata_get_memory_addr_for_array(crash_info_ptr
, TASK_CRASHINFO_COALITION_ID
, sizeof(uint64_t), COALITION_NUM_TYPES
, &uaddr
)) {
523 uint64_t coalition_ids
[COALITION_NUM_TYPES
];
524 task_coalition_ids(p
->task
, coalition_ids
);
525 kcdata_memcpy(crash_info_ptr
, uaddr
, coalition_ids
, sizeof(coalition_ids
));
527 #endif /* CONFIG_COALITIONS */
529 #if CONFIG_MEMORYSTATUS
530 memstat_dirty_flags
= memorystatus_dirty_get(p
, FALSE
);
531 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_DIRTY_FLAGS
, sizeof(memstat_dirty_flags
), &uaddr
)) {
532 kcdata_memcpy(crash_info_ptr
, uaddr
, &memstat_dirty_flags
, sizeof(memstat_dirty_flags
));
536 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_MEMORY_LIMIT_INCREASE
, sizeof(p
->p_memlimit_increase
), &uaddr
)) {
537 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_memlimit_increase
, sizeof(p
->p_memlimit_increase
));
540 if (p
->p_exit_reason
!= OS_REASON_NULL
&& reason
== OS_REASON_NULL
) {
541 reason
= p
->p_exit_reason
;
543 if (reason
!= OS_REASON_NULL
) {
544 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, EXIT_REASON_SNAPSHOT
, sizeof(struct exit_reason_snapshot
), &uaddr
)) {
545 struct exit_reason_snapshot ers
= {
546 .ers_namespace
= reason
->osr_namespace
,
547 .ers_code
= reason
->osr_code
,
548 .ers_flags
= reason
->osr_flags
551 kcdata_memcpy(crash_info_ptr
, uaddr
, &ers
, sizeof(ers
));
554 if (reason
->osr_kcd_buf
!= 0) {
555 uint32_t reason_buf_size
= kcdata_memory_get_used_bytes(&reason
->osr_kcd_descriptor
);
556 assert(reason_buf_size
!= 0);
558 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, KCDATA_TYPE_NESTED_KCDATA
, reason_buf_size
, &uaddr
)) {
559 kcdata_memcpy(crash_info_ptr
, uaddr
, reason
->osr_kcd_buf
, reason_buf_size
);
565 if (KERN_SUCCESS
== kcdata_get_memory_addr_for_array(crash_info_ptr
, TASK_CRASHINFO_UDATA_PTRS
,
566 sizeof(uint64_t), num_udata
, &uaddr
)) {
567 kcdata_memcpy(crash_info_ptr
, uaddr
, udata_buffer
, sizeof(uint64_t) * num_udata
);
573 * We only parse exit reason kcdata blobs for launchd when it dies
574 * and we're going to panic.
576 * Meant to be called immediately before panicking.
579 launchd_exit_reason_get_string_desc(os_reason_t exit_reason
)
583 if (exit_reason
== OS_REASON_NULL
|| exit_reason
->osr_kcd_buf
== NULL
||
584 exit_reason
->osr_bufsize
== 0) {
588 iter
= kcdata_iter(exit_reason
->osr_kcd_buf
, exit_reason
->osr_bufsize
);
589 if (!kcdata_iter_valid(iter
)) {
590 #if DEBUG || DEVELOPMENT
591 printf("launchd exit reason has invalid exit reason buffer\n");
596 if (kcdata_iter_type(iter
) != KCDATA_BUFFER_BEGIN_OS_REASON
) {
597 #if DEBUG || DEVELOPMENT
598 printf("launchd exit reason buffer type mismatch, expected %d got %d\n",
599 KCDATA_BUFFER_BEGIN_OS_REASON
, kcdata_iter_type(iter
));
604 iter
= kcdata_iter_find_type(iter
, EXIT_REASON_USER_DESC
);
605 if (!kcdata_iter_valid(iter
)) {
609 return (char *)kcdata_iter_payload(iter
);
614 launchd_crashed_panic(proc_t p
, int rv
)
616 char *launchd_exit_reason_desc
= launchd_exit_reason_get_string_desc(p
->p_exit_reason
);
618 if (p
->p_exit_reason
== OS_REASON_NULL
) {
619 printf("pid 1 exited -- no exit reason available -- (signal %d, exit %d)\n",
620 WTERMSIG(rv
), WEXITSTATUS(rv
));
622 printf("pid 1 exited -- exit reason namespace %d subcode 0x%llx, description %s\n",
623 p
->p_exit_reason
->osr_namespace
, p
->p_exit_reason
->osr_code
, launchd_exit_reason_desc
?
624 launchd_exit_reason_desc
: "none");
627 const char *launchd_crashed_prefix_str
;
629 if (strnstr(p
->p_name
, "preinit", sizeof(p
->p_name
))) {
630 launchd_crashed_prefix_str
= "LTE preinit process exited";
632 launchd_crashed_prefix_str
= "initproc exited";
635 #if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
637 * For debugging purposes, generate a core file of initproc before
638 * panicking. Leave at least 300 MB free on the root volume, and ignore
639 * the process's corefile ulimit. fsync() the file to ensure it lands on disk
640 * before the panic hits.
644 uint64_t coredump_start
= mach_absolute_time();
645 uint64_t coredump_end
;
647 clock_usec_t tv_usec
;
651 err
= coredump(p
, 300, COREDUMP_IGNORE_ULIMIT
| COREDUMP_FULLFSYNC
);
653 coredump_end
= mach_absolute_time();
655 absolutetime_to_microtime(coredump_end
- coredump_start
, &tv_sec
, &tv_usec
);
657 tv_msec
= tv_usec
/ 1000;
660 printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
661 err
, (uint32_t)tv_sec
, tv_msec
);
663 printf("Generated initproc core file in %d.%03d seconds\n",
664 (uint32_t)tv_sec
, tv_msec
);
666 #endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
668 sync(p
, (void *)NULL
, (int *)NULL
);
670 if (p
->p_exit_reason
== OS_REASON_NULL
) {
671 panic_with_options(0, NULL
, DEBUGGER_OPTION_INITPROC_PANIC
, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
672 launchd_crashed_prefix_str
, WTERMSIG(rv
), WEXITSTATUS(rv
), ((p
->p_csflags
& CS_KILLED
) ? "CS_KILLED" : ""));
674 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",
675 ((p
->p_csflags
& CS_KILLED
) ? "CS_KILLED" : ""),
676 launchd_crashed_prefix_str
, p
->p_exit_reason
->osr_namespace
, p
->p_exit_reason
->osr_code
,
677 launchd_exit_reason_desc
? launchd_exit_reason_desc
: "none");
681 #define OS_REASON_IFLAG_USER_FAULT 0x1
683 #define OS_REASON_TOTAL_USER_FAULTS_PER_PROC 5
686 abort_with_payload_internal(proc_t p
,
687 uint32_t reason_namespace
, uint64_t reason_code
,
688 user_addr_t payload
, uint32_t payload_size
,
689 user_addr_t reason_string
, uint64_t reason_flags
,
690 uint32_t internal_flags
)
692 os_reason_t exit_reason
= OS_REASON_NULL
;
693 kern_return_t kr
= KERN_SUCCESS
;
695 if (internal_flags
& OS_REASON_IFLAG_USER_FAULT
) {
696 uint32_t old_value
= atomic_load_explicit(&p
->p_user_faults
,
697 memory_order_relaxed
);
699 if (old_value
>= OS_REASON_TOTAL_USER_FAULTS_PER_PROC
) {
702 // this reloads the value in old_value
703 if (atomic_compare_exchange_strong_explicit(&p
->p_user_faults
,
704 &old_value
, old_value
+ 1, memory_order_relaxed
,
705 memory_order_relaxed
)) {
711 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
712 p
->p_pid
, reason_namespace
,
715 exit_reason
= build_userspace_exit_reason(reason_namespace
, reason_code
,
716 payload
, payload_size
, reason_string
, reason_flags
| OS_REASON_FLAG_ABORT
);
718 if (internal_flags
& OS_REASON_IFLAG_USER_FAULT
) {
719 mach_exception_code_t code
= 0;
721 EXC_GUARD_ENCODE_TYPE(code
, GUARD_TYPE_USER
); /* simulated EXC_GUARD */
722 EXC_GUARD_ENCODE_FLAVOR(code
, 0);
723 EXC_GUARD_ENCODE_TARGET(code
, reason_namespace
);
725 if (exit_reason
== OS_REASON_NULL
) {
726 kr
= KERN_RESOURCE_SHORTAGE
;
728 kr
= task_violated_guard(code
, reason_code
, exit_reason
);
730 os_reason_free(exit_reason
);
733 * We use SIGABRT (rather than calling exit directly from here) so that
734 * the debugger can catch abort_with_{reason,payload} calls.
736 psignal_try_thread_with_reason(p
, current_thread(), SIGABRT
, exit_reason
);
742 case KERN_NOT_SUPPORTED
:
744 case KERN_INVALID_ARGUMENT
:
746 case KERN_RESOURCE_SHORTAGE
:
753 abort_with_payload(struct proc
*cur_proc
, struct abort_with_payload_args
*args
,
754 __unused
void *retval
)
756 abort_with_payload_internal(cur_proc
, args
->reason_namespace
,
757 args
->reason_code
, args
->payload
, args
->payload_size
,
758 args
->reason_string
, args
->reason_flags
, 0);
764 os_fault_with_payload(struct proc
*cur_proc
,
765 struct os_fault_with_payload_args
*args
, __unused
int *retval
)
767 return abort_with_payload_internal(cur_proc
, args
->reason_namespace
,
768 args
->reason_code
, args
->payload
, args
->payload_size
,
769 args
->reason_string
, args
->reason_flags
, OS_REASON_IFLAG_USER_FAULT
);
777 __attribute__((noreturn
))
779 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
781 p
->p_xhighbits
= ((uint32_t)(uap
->rval
) & 0xFF000000) >> 24;
782 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
784 thread_exception_return();
787 thread_block(THREAD_CONTINUE_NULL
);
793 * Exit: deallocate address space and other resources, change proc state
794 * to zombie, and unlink proc from allproc and parent's lists. Save exit
795 * status and rusage for wait(). Check for child processes and orphan them.
798 exit1(proc_t p
, int rv
, int *retval
)
800 return exit1_internal(p
, rv
, retval
, TRUE
, TRUE
, 0);
804 exit1_internal(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
807 return exit_with_reason(p
, rv
, retval
, thread_can_terminate
, perf_notify
, jetsam_flags
, OS_REASON_NULL
);
811 * NOTE: exit_with_reason drops a reference on the passed exit_reason
814 exit_with_reason(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
815 int jetsam_flags
, struct os_reason
*exit_reason
)
817 thread_t self
= current_thread();
818 struct task
*task
= p
->task
;
823 * If a thread in this task has already
824 * called exit(), then halt any others
828 ut
= get_bsdthread_info(self
);
829 if ((p
== current_proc()) &&
830 (ut
->uu_flag
& UT_VFORK
)) {
831 os_reason_free(exit_reason
);
832 if (!thread_can_terminate
) {
837 vfork_return(p
, retval
, p
->p_pid
);
838 unix_syscall_return(0);
843 * The parameter list of audit_syscall_exit() was augmented to
844 * take the Darwin syscall number as the first parameter,
845 * which is currently required by mac_audit_postselect().
849 * The BSM token contains two components: an exit status as passed
850 * to exit(), and a return value to indicate what sort of exit it
851 * was. The exit status is WEXITSTATUS(rv), but it's not clear
852 * what the return value is.
854 AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0);
856 * TODO: what to audit here when jetsam calls exit and the uthread,
857 * 'ut' does not belong to the proc, 'p'.
859 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
861 DTRACE_PROC1(exit
, int, CLD_EXITED
);
863 /* mark process is going to exit and pull out of DBG/disk throttle */
864 /* TODO: This should be done after becoming exit thread */
865 proc_set_task_policy(p
->task
, TASK_POLICY_ATTRIBUTE
,
866 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
869 error
= proc_transstart(p
, 1, (jetsam_flags
? 1 : 0));
870 if (error
== EDEADLK
) {
872 * If proc_transstart() returns EDEADLK, then another thread
873 * is either exec'ing or exiting. Return an error and allow
874 * the other thread to continue.
877 os_reason_free(exit_reason
);
878 if (current_proc() == p
) {
879 if (p
->exit_thread
== self
) {
880 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
881 p
->p_comm
, p
->p_pid
);
884 if (thread_can_terminate
) {
885 thread_exception_return();
892 while (p
->exit_thread
!= self
) {
893 if (sig_try_locked(p
) <= 0) {
895 os_reason_free(exit_reason
);
897 if (get_threadtask(self
) != task
) {
903 thread_terminate(self
);
904 if (!thread_can_terminate
) {
908 thread_exception_return();
914 if (exit_reason
!= OS_REASON_NULL
) {
915 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_COMMIT
) | DBG_FUNC_NONE
,
916 p
->p_pid
, exit_reason
->osr_namespace
,
917 exit_reason
->osr_code
, 0, 0);
920 assert(p
->p_exit_reason
== OS_REASON_NULL
);
921 p
->p_exit_reason
= exit_reason
;
923 p
->p_lflag
|= P_LEXIT
;
925 p
->p_lflag
|= jetsam_flags
;
930 proc_prepareexit(p
, rv
, perf_notify
);
932 /* Last thread to terminate will call proc_exit() */
933 task_terminate_internal(task
);
938 #if CONFIG_MEMORYSTATUS
940 * Remove this process from jetsam bands for freezing or exiting. Note this will block, if the process
941 * is currently being frozen.
942 * The proc_list_lock is held by the caller.
943 * NB: If the process should be ineligible for future freezing or jetsaming the caller should first set
944 * the p_listflag P_LIST_EXITED bit.
947 proc_memorystatus_remove(proc_t p
)
949 LCK_MTX_ASSERT(proc_list_mlock
, LCK_MTX_ASSERT_OWNED
);
950 while (memorystatus_remove(p
) == EAGAIN
) {
951 os_log(OS_LOG_DEFAULT
, "memorystatus_remove: Process[%d] tried to exit while being frozen. Blocking exit until freeze completes.", p
->p_pid
);
952 msleep(&p
->p_memstat_state
, proc_list_mlock
, PWAIT
, "proc_memorystatus_remove", NULL
);
958 proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
)
960 mach_exception_data_type_t code
= 0, subcode
= 0;
963 thread_t self
= current_thread();
964 ut
= get_bsdthread_info(self
);
965 struct rusage_superset
*rup
;
967 int create_corpse
= FALSE
;
970 launchd_crashed_panic(p
, rv
);
975 * Generate a corefile/crashlog if:
976 * The process doesn't have an exit reason that indicates no crash report should be created
977 * AND any of the following are true:
978 * - The process was terminated due to a fatal signal that generates a core
979 * - The process was killed due to a code signing violation
980 * - The process has an exit reason that indicates we should generate a crash report
982 * The first condition is necessary because abort_with_reason()/payload() use SIGABRT
983 * (which normally triggers a core) but may indicate that no crash report should be created.
985 if (!(PROC_HAS_EXITREASON(p
) && (PROC_EXITREASON_FLAGS(p
) & OS_REASON_FLAG_NO_CRASH_REPORT
)) &&
986 (hassigprop(WTERMSIG(rv
), SA_CORE
) || ((p
->p_csflags
& CS_KILLED
) != 0) ||
987 (PROC_HAS_EXITREASON(p
) && (PROC_EXITREASON_FLAGS(p
) &
988 OS_REASON_FLAG_GENERATE_CRASH_REPORT
)))) {
990 * Workaround for processes checking up on PT_DENY_ATTACH:
991 * should be backed out post-Leopard (details in 5431025).
993 if ((SIGSEGV
== WTERMSIG(rv
)) &&
994 (p
->p_pptr
->p_lflag
& P_LNOATTACH
)) {
999 * Crash Reporter looks for the signal value, original exception
1000 * type, and low 20 bits of the original code in code[0]
1001 * (8, 4, and 20 bits respectively). code[1] is unmodified.
1003 code
= ((WTERMSIG(rv
) & 0xff) << 24) |
1004 ((ut
->uu_exception
& 0x0f) << 20) |
1005 ((int)ut
->uu_code
& 0xfffff);
1006 subcode
= ut
->uu_subcode
;
1008 kr
= task_exception_notify(EXC_CRASH
, code
, subcode
);
1010 /* Nobody handled EXC_CRASH?? remember to make corpse */
1012 create_corpse
= TRUE
;
1017 /* Notify the perf server? */
1019 (void)sys_perf_notify(self
, p
->p_pid
);
1023 /* stash the usage into corpse data if making_corpse == true */
1024 if (create_corpse
== TRUE
) {
1025 kr
= task_mark_corpse(p
->task
);
1026 if (kr
!= KERN_SUCCESS
) {
1027 if (kr
== KERN_NO_SPACE
) {
1028 printf("Process[%d] has no vm space for corpse info.\n", p
->p_pid
);
1029 } else if (kr
== KERN_NOT_SUPPORTED
) {
1030 printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", p
->p_pid
);
1032 printf("Process[%d] crashed: %s. Too many corpses being created.\n", p
->p_pid
, p
->p_comm
);
1034 create_corpse
= FALSE
;
1039 * Before this process becomes a zombie, stash resource usage
1040 * stats in the proc for external observers to query
1041 * via proc_pid_rusage().
1043 * If the zombie allocation fails, just punt the stats.
1045 MALLOC_ZONE(rup
, struct rusage_superset
*,
1046 sizeof(*rup
), M_ZOMBIE
, M_WAITOK
);
1048 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
1049 rup
->ri
.ri_phys_footprint
= 0;
1050 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
1053 * Make the rusage_info visible to external observers
1054 * only after it has been completely filled in.
1058 if (create_corpse
) {
1059 int est_knotes
= 0, num_knotes
= 0;
1060 uint64_t *buffer
= NULL
;
1063 /* Get all the udata pointers from kqueue */
1064 est_knotes
= kevent_proc_copy_uptrs(p
, NULL
, 0);
1065 if (est_knotes
> 0) {
1066 buf_size
= (est_knotes
+ 32) * sizeof(uint64_t);
1067 buffer
= (uint64_t *) kalloc(buf_size
);
1068 num_knotes
= kevent_proc_copy_uptrs(p
, buffer
, buf_size
);
1069 if (num_knotes
> est_knotes
+ 32) {
1070 num_knotes
= est_knotes
+ 32;
1074 /* Update the code, subcode based on exit reason */
1075 proc_update_corpse_exception_codes(p
, &code
, &subcode
);
1076 populate_corpse_crashinfo(p
, p
->task
, rup
,
1077 code
, subcode
, buffer
, num_knotes
, NULL
);
1078 if (buffer
!= NULL
) {
1079 kfree(buffer
, buf_size
);
1083 * Remove proc from allproc queue and from pidhash chain.
1084 * Need to do this before we do anything that can block.
1085 * Not doing causes things like mount() find this on allproc
1086 * in partially cleaned state.
1091 #if CONFIG_MEMORYSTATUS
1092 proc_memorystatus_remove(p
);
1095 LIST_REMOVE(p
, p_list
);
1096 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1097 /* will not be visible via proc_find */
1098 p
->p_listflag
|= P_LIST_EXITED
;
1106 * If parent is waiting for us to exit or exec,
1107 * P_LPPWAIT is set; we will wakeup the parent below.
1110 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1111 p
->p_sigignore
= ~(sigcantmask
);
1121 struct task
*task
= p
->task
;
1122 vnode_t tvp
= NULLVP
;
1124 struct session
*sessp
;
1125 struct uthread
* uth
;
1130 uth
= current_uthread();
1133 proc_transstart(p
, 1, 0);
1134 if (!(p
->p_lflag
& P_LEXIT
)) {
1136 * This can happen if a thread_terminate() occurs
1137 * in a single-threaded process.
1139 p
->p_lflag
|= P_LEXIT
;
1140 proc_transend(p
, 1);
1142 proc_prepareexit(p
, 0, TRUE
);
1143 (void) task_terminate_internal(task
);
1146 proc_transend(p
, 1);
1149 p
->p_lflag
|= P_LPEXIT
;
1152 * Other kernel threads may be in the middle of signalling this process.
1153 * Wait for those threads to wrap it up before making the process
1154 * disappear on them.
1156 if ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 0)) {
1158 while ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 1)) {
1159 msleep(&p
->p_sigmask
, &p
->p_mlock
, PWAIT
, "proc_sigdrain", NULL
);
1166 exitval
= p
->p_xstat
;
1167 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1168 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_START
,
1169 pid
, exitval
, 0, 0, 0);
1172 dtrace_proc_exit(p
);
1176 * need to cancel async IO requests that can be cancelled and wait for those
1177 * already active. MAY BLOCK!
1182 /* if any pending cpu limits action, clear it */
1183 task_clear_cpuusage(p
->task
, TRUE
);
1185 workq_mark_exiting(p
);
1190 * Close open files and release open-file table.
1196 * Once all the knotes, kqueues & workloops are destroyed, get rid of the
1201 if (uth
->uu_lowpri_window
) {
1203 * task is marked as a low priority I/O type
1204 * and the I/O we issued while in flushing files on close
1205 * collided with normal I/O operations...
1206 * no need to throttle this thread since its going away
1207 * but we do need to update our bookeeping w/r to throttled threads
1209 throttle_lowpri_io(0);
1212 if (p
->p_lflag
& P_LNSPACE_RESOLVER
) {
1214 * The namespace resolver is exiting; there may be
1215 * outstanding materialization requests to clean up.
1217 nspace_resolver_exited(p
);
1221 /* Close ref SYSV Shared memory*/
1227 /* Release SYSV semaphores */
1232 pth_proc_hashdelete(p
);
1235 sessp
= proc_session(p
);
1236 if (SESS_LEADER(p
, sessp
)) {
1237 if (sessp
->s_ttyvp
!= NULLVP
) {
1238 struct vnode
*ttyvp
;
1241 struct vfs_context context
;
1245 * Controlling process.
1246 * Signal foreground pgrp,
1247 * drain controlling terminal
1248 * and revoke access to controlling terminal.
1250 session_lock(sessp
);
1251 tp
= SESSION_TP(sessp
);
1252 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1253 session_unlock(sessp
);
1256 * We're going to SIGHUP the foreground process
1257 * group. It can't change from this point on
1258 * until the revoke is complete.
1259 * The process group changes under both the tty
1260 * lock and proc_list_lock but we need only one
1266 tty_pgsignal(tp
, SIGHUP
, 1);
1268 session_lock(sessp
);
1269 tp
= SESSION_TP(sessp
);
1271 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
1272 sessp
->s_flags
&= ~S_CTTYREF
;
1273 ttyvp
= sessp
->s_ttyvp
;
1274 ttyvid
= sessp
->s_ttyvid
;
1275 sessp
->s_ttyvp
= NULLVP
;
1276 sessp
->s_ttyvid
= 0;
1277 sessp
->s_ttyp
= TTY_NULL
;
1278 sessp
->s_ttypgrpid
= NO_PID
;
1279 session_unlock(sessp
);
1281 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1282 if (tp
!= TTY_NULL
) {
1287 context
.vc_thread
= proc_thread(p
); /* XXX */
1288 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1289 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1292 * Release the extra usecount taken in cttyopen.
1293 * usecount should be released after VNOP_REVOKE is called.
1294 * This usecount was taken to ensure that
1295 * the VNOP_REVOKE results in a close to
1296 * the tty since cttyclose is a no-op.
1301 kauth_cred_unref(&context
.vc_ucred
);
1306 * This is cleared even if not set. This is also done in
1307 * spec_close to ensure that the flag is cleared.
1316 session_lock(sessp
);
1317 sessp
->s_leader
= NULL
;
1318 session_unlock(sessp
);
1320 session_rele(sessp
);
1326 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1327 (void)acct_process(p
);
1331 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) {
1332 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
1333 proc_shutdown_exitcount
--;
1334 if (proc_shutdown_exitcount
== 0) {
1335 wakeup(&proc_shutdown_exitcount
);
1339 /* wait till parentrefs are dropped and grant no more */
1340 proc_childdrainstart(p
);
1341 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1342 int reparentedtoinit
= (q
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
1343 if (q
->p_stat
== SZOMB
) {
1344 if (p
!= q
->p_pptr
) {
1345 panic("parent child linkage broken");
1347 /* check for sysctl zomb lookup */
1348 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1349 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1351 q
->p_listflag
|= P_LIST_WAITING
;
1353 * This is a named reference and it is not granted
1354 * if the reap is already in progress. So we get
1355 * the reference here exclusively and their can be
1356 * no waiters. So there is no need for a wakeup
1357 * after we are done. Also the reap frees the structure
1358 * and the proc struct cannot be used for wakeups as well.
1359 * It is safe to use q here as this is system reap
1361 (void)reap_child_locked(p
, q
, 1, reparentedtoinit
, 1, 0);
1364 * Traced processes are killed
1365 * since their existence means someone is messing up.
1367 if (q
->p_lflag
& P_LTRACED
) {
1371 * Take a reference on the child process to
1372 * ensure it doesn't exit and disappear between
1373 * the time we drop the list_lock and attempt
1374 * to acquire its proc_lock.
1376 if (proc_ref_locked(q
) != q
) {
1382 opp
= proc_find(q
->p_oppid
);
1383 if (opp
!= PROC_NULL
) {
1387 proc_reparentlocked(q
, opp
, 0, 0);
1390 /* original parent exited while traced */
1392 q
->p_listflag
|= P_LIST_DEADPARENT
;
1395 proc_reparentlocked(q
, initproc
, 0, 0);
1399 q
->p_lflag
&= ~P_LTRACED
;
1401 if (q
->sigwait_thread
) {
1402 thread_t thread
= q
->sigwait_thread
;
1406 * The sigwait_thread could be stopped at a
1407 * breakpoint. Wake it up to kill.
1408 * Need to do this as it could be a thread which is not
1409 * the first thread in the task. So any attempts to kill
1410 * the process would result into a deadlock on q->sigwait.
1412 thread_resume(thread
);
1413 clear_wait(thread
, THREAD_INTERRUPTED
);
1414 threadsignal(thread
, SIGKILL
, 0, TRUE
);
1419 psignal(q
, SIGKILL
);
1421 proc_rele_locked(q
);
1423 q
->p_listflag
|= P_LIST_DEADPARENT
;
1424 proc_reparentlocked(q
, initproc
, 0, 1);
1429 proc_childdrainend(p
);
1434 * Notify MAC policies that proc is dead.
1435 * This should be replaced with proper label management
1436 * (rdar://problem/32126399).
1438 mac_proc_notify_exit(p
);
1442 * Release reference to text vnode
1446 if (tvp
!= NULLVP
) {
1451 * Save exit status and final rusage info, adding in child rusage
1452 * info and self times. If we were unable to allocate a zombie
1453 * structure, this information is lost.
1455 if (p
->p_ru
!= NULL
) {
1456 calcru(p
, &p
->p_stats
->p_ru
.ru_utime
, &p
->p_stats
->p_ru
.ru_stime
, NULL
);
1457 p
->p_ru
->ru
= p
->p_stats
->p_ru
;
1459 ruadd(&(p
->p_ru
->ru
), &p
->p_stats
->p_cru
);
1463 * Free up profiling buffers.
1466 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1472 for (; p1
!= NULL
; p1
= pn
) {
1474 kfree(p1
, sizeof *p1
);
1478 proc_free_realitimer(p
);
1481 * Other substructures are freed from wait().
1483 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
1486 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
1487 p
->p_sigacts
= NULL
;
1489 proc_limitdrop(p
, 1);
1493 * Finish up by terminating the task
1494 * and halt this thread (only if a
1495 * member of the task exiting).
1497 p
->task
= TASK_NULL
;
1498 set_bsdtask_info(task
, NULL
);
1500 knote_hint
= NOTE_EXIT
| (p
->p_xstat
& 0xffff);
1501 proc_knote(p
, knote_hint
);
1503 /* mark the thread as the one that is doing proc_exit
1504 * no need to hold proc lock in uthread_free
1506 uth
->uu_flag
|= UT_PROCEXIT
;
1508 * Notify parent that we're gone.
1510 pp
= proc_parent(p
);
1511 if (pp
->p_flag
& P_NOCLDWAIT
) {
1512 if (p
->p_ru
!= NULL
) {
1516 * If the parent is ignoring SIGCHLD, then POSIX requires
1517 * us to not add the resource usage to the parent process -
1518 * we are only going to hand it off to init to get reaped.
1519 * We should contest the standard in this case on the basis
1522 #else /* !3839178 */
1524 * Add child resource usage to parent before giving
1525 * zombie to init. If we were unable to allocate a
1526 * zombie structure, this information is lost.
1528 ruadd(&pp
->p_stats
->p_cru
, &p
->p_ru
->ru
);
1529 #endif /* !3839178 */
1530 update_rusage_info_child(&pp
->p_stats
->ri_child
, &p
->p_ru
->ri
);
1534 /* kernel can reap this one, no need to move it to launchd */
1536 p
->p_listflag
|= P_LIST_DEADPARENT
;
1539 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0 || p
->p_oppid
) {
1540 if (pp
!= initproc
) {
1542 pp
->si_pid
= p
->p_pid
;
1543 pp
->p_xhighbits
= p
->p_xhighbits
;
1545 pp
->si_status
= p
->p_xstat
;
1546 pp
->si_code
= CLD_EXITED
;
1548 * p_ucred usage is safe as it is an exiting process
1549 * and reference is dropped in reap
1551 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
1554 /* mark as a zombie */
1555 /* No need to take proc lock as all refs are drained and
1556 * no one except parent (reaping ) can look at this.
1557 * The write is to an int and is coherent. Also parent is
1558 * keyed off of list lock for reaping
1560 DTRACE_PROC2(exited
, proc_t
, p
, int, exitval
);
1561 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1562 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
1563 pid
, exitval
, 0, 0, 0);
1566 * The current process can be reaped so, no one
1567 * can depend on this
1570 psignal(pp
, SIGCHLD
);
1572 /* and now wakeup the parent */
1574 wakeup((caddr_t
)pp
);
1577 /* should be fine as parent proc would be initproc */
1578 /* mark as a zombie */
1579 /* No need to take proc lock as all refs are drained and
1580 * no one except parent (reaping ) can look at this.
1581 * The write is to an int and is coherent. Also parent is
1582 * keyed off of list lock for reaping
1584 DTRACE_PROC2(exited
, proc_t
, p
, int, exitval
);
1586 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1587 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
1588 pid
, exitval
, 0, 0, 0);
1589 /* check for sysctl zomb lookup */
1590 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1591 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1593 /* safe to use p as this is a system reap */
1595 p
->p_listflag
|= P_LIST_WAITING
;
1598 * This is a named reference and it is not granted
1599 * if the reap is already in progress. So we get
1600 * the reference here exclusively and their can be
1601 * no waiters. So there is no need for a wakeup
1602 * after we are done. AlsO the reap frees the structure
1603 * and the proc struct cannot be used for wakeups as well.
1604 * It is safe to use p here as this is system reap
1606 (void)reap_child_locked(pp
, p
, 1, 0, 1, 1);
1607 /* list lock dropped by reap_child_locked */
1609 if (uth
->uu_lowpri_window
) {
1611 * task is marked as a low priority I/O type and we've
1612 * somehow picked up another throttle during exit processing...
1613 * no need to throttle this thread since its going away
1614 * but we do need to update our bookeeping w/r to throttled threads
1616 throttle_lowpri_io(0);
1626 * Description: Given a process from which all status information needed
1627 * has already been extracted, if the process is a ptrace
1628 * attach process, detach it and give it back to its real
1629 * parent, else recover all resources remaining associated
1632 * Parameters: proc_t parent Parent of process being reaped
1633 * proc_t child Process to reap
1635 * Returns: 0 Process was not reaped because it
1636 * came from an attach
1637 * 1 Process was reaped
1640 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
)
1642 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
1649 * If we got the child via a ptrace 'attach',
1650 * we need to give it back to the old parent.
1652 * Exception: someone who has been reparented to launchd before being
1653 * ptraced can simply be reaped, refer to radar 5677288
1654 * p_oppid -> ptraced
1655 * trace_parent == initproc -> away from launchd
1656 * reparentedtoinit -> came to launchd by reparenting
1658 if (child
->p_oppid
) {
1663 oppid
= child
->p_oppid
;
1665 knote_hint
= NOTE_EXIT
| (child
->p_xstat
& 0xffff);
1668 if ((trace_parent
= proc_find(oppid
))
1669 && !((trace_parent
== initproc
) && reparentedtoinit
)) {
1670 if (trace_parent
!= initproc
) {
1672 * proc internal fileds and p_ucred usage safe
1673 * here as child is dead and is not reaped or
1676 proc_lock(trace_parent
);
1677 trace_parent
->si_pid
= child
->p_pid
;
1678 trace_parent
->si_status
= child
->p_xstat
;
1679 trace_parent
->si_code
= CLD_CONTINUED
;
1680 trace_parent
->si_uid
= kauth_cred_getruid(child
->p_ucred
);
1681 proc_unlock(trace_parent
);
1683 proc_reparentlocked(child
, trace_parent
, 1, 0);
1685 /* resend knote to original parent (and others) after reparenting */
1686 proc_knote(child
, knote_hint
);
1688 psignal(trace_parent
, SIGCHLD
);
1690 wakeup((caddr_t
)trace_parent
);
1691 child
->p_listflag
&= ~P_LIST_WAITING
;
1692 wakeup(&child
->p_stat
);
1694 proc_rele(trace_parent
);
1695 if ((locked
== 1) && (droplock
== 0)) {
1702 * If we can't reparent (e.g. the original parent exited while child was being debugged, or
1703 * original parent is the same as the debugger currently exiting), we still need to satisfy
1704 * the knote lifecycle for other observers on the system. While the debugger was attached,
1705 * the NOTE_EXIT would not have been broadcast during initial child termination.
1707 proc_knote(child
, knote_hint
);
1709 if (trace_parent
!= PROC_NULL
) {
1710 proc_rele(trace_parent
);
1714 #pragma clang diagnostic push
1715 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1716 proc_knote(child
, NOTE_REAP
);
1717 #pragma clang diagnostic pop
1719 proc_knote_drain(child
);
1726 * If the parent is ignoring SIGCHLD, then POSIX requires
1727 * us to not add the resource usage to the parent process -
1728 * we are only going to hand it off to init to get reaped.
1729 * We should contest the standard in this case on the basis
1732 if (!(parent
->p_flag
& P_NOCLDWAIT
))
1733 #endif /* 3839178 */
1734 ruadd(&parent
->p_stats
->p_cru
, &child
->p_ru
->ru
);
1735 update_rusage_info_child(&parent
->p_stats
->ri_child
, &child
->p_ru
->ri
);
1736 proc_unlock(parent
);
1737 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
1740 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
1743 AUDIT_SESSION_PROCEXIT(child
);
1746 * Decrement the count of procs running with this uid.
1747 * p_ucred usage is safe here as it is an exited process.
1748 * and refernce is dropped after these calls down below
1749 * (locking protection is provided by list lock held in chgproccnt)
1753 * persona_proc_drop calls chgproccnt(-1) on the persona uid,
1754 * and (+1) on the child->p_ucred uid
1756 persona_proc_drop(child
);
1758 (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1);
1760 os_reason_free(child
->p_exit_reason
);
1763 * Free up credentials.
1765 if (IS_VALID_CRED(child
->p_ucred
)) {
1766 kauth_cred_unref(&child
->p_ucred
);
1769 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
1772 * Finally finished with old proc entry.
1773 * Unlink it from its process group and free it.
1778 LIST_REMOVE(child
, p_list
); /* off zombproc */
1779 parent
->p_childrencnt
--;
1780 LIST_REMOVE(child
, p_sibling
);
1781 /* If there are no more children wakeup parent */
1782 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
))) {
1783 wakeup((caddr_t
)parent
); /* with list lock held */
1785 child
->p_listflag
&= ~P_LIST_WAITING
;
1786 wakeup(&child
->p_stat
);
1788 /* Take it out of process hash */
1789 LIST_REMOVE(child
, p_hash
);
1790 child
->p_listflag
&= ~P_LIST_INHASH
;
1791 proc_checkdeadrefs(child
);
1796 * If a child zombie is being reaped because its parent
1797 * is exiting, make sure we update the list flag
1799 child
->p_listflag
|= P_LIST_DEADPARENT
;
1805 lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
);
1806 lck_mtx_destroy(&child
->p_ucred_mlock
, proc_ucred_mlock_grp
);
1807 lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
);
1809 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1811 lck_spin_destroy(&child
->p_slock
, proc_slock_grp
);
1813 FREE_ZONE(child
, sizeof *child
, M_PROC
);
1814 if ((locked
== 1) && (droplock
== 0)) {
1823 wait1continue(int result
)
1828 struct _wait4_data
*wait4_data
;
1829 struct wait4_nocancel_args
*uap
;
1837 thread
= current_thread();
1838 uth
= (struct uthread
*)get_bsdthread_info(thread
);
1840 wait4_data
= &uth
->uu_save
.uus_wait4_data
;
1841 uap
= wait4_data
->args
;
1842 retval
= wait4_data
->retval
;
1843 return wait4_nocancel(p
, uap
, retval
);
1847 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
1849 __pthread_testcancel(1);
1850 return wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
);
1854 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1861 struct _wait4_data
*wait4_data
;
1863 AUDIT_ARG(pid
, uap
->pid
);
1865 if (uap
->pid
== 0) {
1866 uap
->pid
= -q
->p_pgrpid
;
1875 PCHILDREN_FOREACH(q
, p
) {
1876 if (p
->p_sibling
.le_next
!= 0) {
1879 if (uap
->pid
!= WAIT_ANY
&&
1880 p
->p_pid
!= uap
->pid
&&
1881 p
->p_pgrpid
!= -(uap
->pid
)) {
1887 /* XXX This is racy because we don't get the lock!!!! */
1889 if (p
->p_listflag
& P_LIST_WAITING
) {
1890 /* we're not using a continuation here but we still need to stash
1891 * the args for stackshot. */
1892 uth
= current_uthread();
1893 wait4_data
= &uth
->uu_save
.uus_wait4_data
;
1894 wait4_data
->args
= uap
;
1895 thread_set_pending_block_hint(current_thread(), kThreadWaitOnProcess
);
1897 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1900 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1903 if (p
->p_stat
== SZOMB
) {
1904 int reparentedtoinit
= (p
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
1908 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
1912 retval
[0] = p
->p_pid
;
1914 /* Legacy apps expect only 8 bits of status */
1915 status
= 0xffff & p
->p_xstat
; /* convert to int */
1916 error
= copyout((caddr_t
)&status
,
1924 if (p
->p_ru
== NULL
) {
1927 if (IS_64BIT_PROCESS(q
)) {
1928 struct user64_rusage my_rusage
= {};
1929 munge_user64_rusage(&p
->p_ru
->ru
, &my_rusage
);
1930 error
= copyout((caddr_t
)&my_rusage
,
1934 struct user32_rusage my_rusage
= {};
1935 munge_user32_rusage(&p
->p_ru
->ru
, &my_rusage
);
1936 error
= copyout((caddr_t
)&my_rusage
,
1941 /* information unavailable? */
1947 /* Conformance change for 6577252.
1948 * When SIGCHLD is blocked and wait() returns because the status
1949 * of a child process is available and there are no other
1950 * children processes, then any pending SIGCHLD signal is cleared.
1952 if (sibling_count
== 0) {
1953 int mask
= sigmask(SIGCHLD
);
1954 uth
= current_uthread();
1956 if ((uth
->uu_sigmask
& mask
) != 0) {
1957 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1958 * This locking looks funny but it is protecting access to the
1959 * thread via p_uthlist.
1962 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
1968 (void)reap_child_locked(q
, p
, 0, reparentedtoinit
, 0, 0);
1972 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1973 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1976 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
1981 p
->p_lflag
|= P_LWAITED
;
1983 retval
[0] = p
->p_pid
;
1985 status
= W_STOPCODE(p
->p_xstat
);
1986 error
= copyout((caddr_t
)&status
,
1995 * If we are waiting for continued processses, and this
1996 * process was continued
1998 if ((uap
->options
& WCONTINUED
) &&
1999 (p
->p_flag
& P_CONTINUED
)) {
2002 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2007 /* Prevent other process for waiting for this event */
2008 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
2009 retval
[0] = p
->p_pid
;
2011 status
= W_STOPCODE(SIGCONT
);
2012 error
= copyout((caddr_t
)&status
,
2020 p
->p_listflag
&= ~P_LIST_WAITING
;
2023 /* list lock is held when we get here any which way */
2029 if (uap
->options
& WNOHANG
) {
2035 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2036 uth
= current_uthread();
2037 wait4_data
= &uth
->uu_save
.uus_wait4_data
;
2038 wait4_data
->args
= uap
;
2039 wait4_data
->retval
= retval
;
2041 thread_set_pending_block_hint(current_thread(), kThreadWaitOnProcess
);
2042 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
))) {
2049 p
->p_listflag
&= ~P_LIST_WAITING
;
2056 #define ASSERT_LCK_MTX_OWNED(lock) \
2057 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
2059 #define ASSERT_LCK_MTX_OWNED(lock) /* nothing */
2063 waitidcontinue(int result
)
2068 struct _waitid_data
*waitid_data
;
2069 struct waitid_nocancel_args
*uap
;
2077 thread
= current_thread();
2078 uth
= (struct uthread
*)get_bsdthread_info(thread
);
2080 waitid_data
= &uth
->uu_save
.uus_waitid_data
;
2081 uap
= waitid_data
->args
;
2082 retval
= waitid_data
->retval
;
2083 return waitid_nocancel(p
, uap
, retval
);
2087 * Description: Suspend the calling thread until one child of the process
2088 * containing the calling thread changes state.
2090 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
2091 * uap->id pid_t or gid_t or ignored
2092 * uap->infop Address of siginfo_t struct in
2093 * user space into which to return status
2094 * uap->options flag values
2096 * Returns: 0 Success
2097 * !0 Error returning status to user space
2100 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
2102 __pthread_testcancel(1);
2103 return waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
);
2107 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
,
2108 __unused
int32_t *retval
)
2110 user_siginfo_t siginfo
; /* siginfo data to return to caller */
2111 boolean_t caller64
= IS_64BIT_PROCESS(q
);
2116 struct _waitid_data
*waitid_data
;
2118 if (uap
->options
== 0 ||
2119 (uap
->options
& ~(WNOHANG
| WNOWAIT
| WCONTINUED
| WSTOPPED
| WEXITED
))) {
2120 return EINVAL
; /* bits set that aren't recognized */
2122 switch (uap
->idtype
) {
2123 case P_PID
: /* child with process ID equal to... */
2124 case P_PGID
: /* child with process group ID equal to... */
2125 if (((int)uap
->id
) < 0) {
2129 case P_ALL
: /* any child */
2138 PCHILDREN_FOREACH(q
, p
) {
2139 switch (uap
->idtype
) {
2140 case P_PID
: /* child with process ID equal to... */
2141 if (p
->p_pid
!= (pid_t
)uap
->id
) {
2145 case P_PGID
: /* child with process group ID equal to... */
2146 if (p
->p_pgrpid
!= (pid_t
)uap
->id
) {
2150 case P_ALL
: /* any child */
2154 /* XXX This is racy because we don't get the lock!!!! */
2157 * Wait collision; go to sleep and restart; used to maintain
2158 * the single return for waited process guarantee.
2160 if (p
->p_listflag
& P_LIST_WAITING
) {
2161 (void) msleep(&p
->p_stat
, proc_list_mlock
,
2162 PWAIT
, "waitidcoll", 0);
2165 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
2169 bzero(&siginfo
, sizeof(siginfo
));
2171 switch (p
->p_stat
) {
2172 case SZOMB
: /* Exited */
2173 if (!(uap
->options
& WEXITED
)) {
2178 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2182 siginfo
.si_signo
= SIGCHLD
;
2183 siginfo
.si_pid
= p
->p_pid
;
2184 siginfo
.si_status
= (WEXITSTATUS(p
->p_xstat
) & 0x00FFFFFF) | (((uint32_t)(p
->p_xhighbits
) << 24) & 0xFF000000);
2186 if (WIFSIGNALED(p
->p_xstat
)) {
2187 siginfo
.si_code
= WCOREDUMP(p
->p_xstat
) ?
2188 CLD_DUMPED
: CLD_KILLED
;
2190 siginfo
.si_code
= CLD_EXITED
;
2193 if ((error
= copyoutsiginfo(&siginfo
,
2194 caller64
, uap
->infop
)) != 0) {
2198 /* Prevent other process for waiting for this event? */
2199 if (!(uap
->options
& WNOWAIT
)) {
2200 (void) reap_child_locked(q
, p
, 0, 0, 0, 0);
2205 case SSTOP
: /* Stopped */
2207 * If we are not interested in stopped processes, then
2210 if (!(uap
->options
& WSTOPPED
)) {
2215 * If someone has already waited it, we lost a race
2216 * to be the one to return status.
2218 if ((p
->p_lflag
& P_LWAITED
) != 0) {
2223 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2227 siginfo
.si_signo
= SIGCHLD
;
2228 siginfo
.si_pid
= p
->p_pid
;
2229 siginfo
.si_status
= p
->p_xstat
; /* signal number */
2230 siginfo
.si_code
= CLD_STOPPED
;
2232 if ((error
= copyoutsiginfo(&siginfo
,
2233 caller64
, uap
->infop
)) != 0) {
2237 /* Prevent other process for waiting for this event? */
2238 if (!(uap
->options
& WNOWAIT
)) {
2240 p
->p_lflag
|= P_LWAITED
;
2245 default: /* All other states => Continued */
2246 if (!(uap
->options
& WCONTINUED
)) {
2251 * If the flag isn't set, then this process has not
2252 * been stopped and continued, or the status has
2253 * already been reaped by another caller of waitid().
2255 if ((p
->p_flag
& P_CONTINUED
) == 0) {
2260 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2264 siginfo
.si_signo
= SIGCHLD
;
2265 siginfo
.si_code
= CLD_CONTINUED
;
2267 siginfo
.si_pid
= p
->p_contproc
;
2268 siginfo
.si_status
= p
->p_xstat
;
2271 if ((error
= copyoutsiginfo(&siginfo
,
2272 caller64
, uap
->infop
)) != 0) {
2276 /* Prevent other process for waiting for this event? */
2277 if (!(uap
->options
& WNOWAIT
)) {
2278 OSBitAndAtomic(~((uint32_t)P_CONTINUED
),
2283 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
2285 /* Not a process we are interested in; go on to next child */
2287 p
->p_listflag
&= ~P_LIST_WAITING
;
2290 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
2292 /* No child processes that could possibly satisfy the request? */
2299 if (uap
->options
& WNOHANG
) {
2302 if ((error
= mac_proc_check_wait(q
, p
)) != 0) {
2307 * The state of the siginfo structure in this case
2308 * is undefined. Some implementations bzero it, some
2309 * (like here) leave it untouched for efficiency.
2311 * Thus the most portable check for "no matching pid with
2312 * WNOHANG" is to store a zero into si_pid before
2313 * invocation, then check for a non-zero value afterwards.
2318 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2319 uth
= current_uthread();
2320 waitid_data
= &uth
->uu_save
.uus_waitid_data
;
2321 waitid_data
->args
= uap
;
2322 waitid_data
->retval
= retval
;
2324 if ((error
= msleep0(q
, proc_list_mlock
,
2325 PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)) != 0) {
2332 p
->p_listflag
&= ~P_LIST_WAITING
;
2339 * make process 'parent' the new parent of process 'child'.
2342 proc_reparentlocked(proc_t child
, proc_t parent
, int signallable
, int locked
)
2344 proc_t oldparent
= PROC_NULL
;
2346 if (child
->p_pptr
== parent
) {
2354 oldparent
= child
->p_pptr
;
2355 #if __PROC_INTERNAL_DEBUG
2356 if (oldparent
== PROC_NULL
) {
2357 panic("proc_reparent: process %p does not have a parent\n", child
);
2361 LIST_REMOVE(child
, p_sibling
);
2362 #if __PROC_INTERNAL_DEBUG
2363 if (oldparent
->p_childrencnt
== 0) {
2364 panic("process children count already 0\n");
2367 oldparent
->p_childrencnt
--;
2368 #if __PROC_INTERNAL_DEBUG
2369 if (oldparent
->p_childrencnt
< 0) {
2370 panic("process children count -ve\n");
2373 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
2374 parent
->p_childrencnt
++;
2375 child
->p_pptr
= parent
;
2376 child
->p_ppid
= parent
->p_pid
;
2380 if ((signallable
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
)) {
2381 psignal(initproc
, SIGCHLD
);
2389 * Exit: deallocate address space and other resources, change proc state
2390 * to zombie, and unlink proc from allproc and parent's lists. Save exit
2391 * status and rusage for wait(). Check for child processes and orphan them.
2395 vfork_exit(proc_t p
, int rv
)
2397 vfork_exit_internal(p
, rv
, 0);
2401 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
2403 thread_t self
= current_thread();
2405 struct task
*task
= p
->task
;
2410 * If a thread in this task has already
2411 * called exit(), then halt any others
2415 ut
= get_bsdthread_info(self
);
2419 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
2421 * This happens when a parent exits/killed and vfork is in progress
2422 * other threads. But shutdown code for ex has already called exit1()
2427 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
2430 if (forceexit
== 0) {
2432 * parent of a vfork child has already called exit() and the
2433 * thread that has vfork in proress terminates. So there is no
2434 * separate address space here and it has already been marked for
2435 * termination. This was never covered before and could cause problems
2436 * if we block here for outside code.
2438 /* Notify the perf server */
2439 (void)sys_perf_notify(self
, p
->p_pid
);
2443 * Remove proc from allproc queue and from pidhash chain.
2444 * Need to do this before we do anything that can block.
2445 * Not doing causes things like mount() find this on allproc
2446 * in partially cleaned state.
2451 #if CONFIG_MEMORYSTATUS
2452 proc_memorystatus_remove(p
);
2455 LIST_REMOVE(p
, p_list
);
2456 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
2457 /* will not be visible via proc_find */
2458 p
->p_listflag
|= P_LIST_EXITED
;
2464 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
2465 p
->p_sigignore
= ~0;
2470 /* begin vproc_exit */
2478 struct session
*sessp
;
2479 struct rusage_superset
*rup
;
2481 /* XXX Zombie allocation may fail, in which case stats get lost */
2482 MALLOC_ZONE(rup
, struct rusage_superset
*,
2483 sizeof(*rup
), M_ZOMBIE
, M_WAITOK
);
2488 * Close open files and release open-file table.
2493 sessp
= proc_session(p
);
2494 if (SESS_LEADER(p
, sessp
)) {
2495 if (sessp
->s_ttyvp
!= NULLVP
) {
2496 struct vnode
*ttyvp
;
2499 struct vfs_context context
;
2503 * Controlling process.
2504 * Signal foreground pgrp,
2505 * drain controlling terminal
2506 * and revoke access to controlling terminal.
2508 session_lock(sessp
);
2509 tp
= SESSION_TP(sessp
);
2510 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
2511 session_unlock(sessp
);
2514 * We're going to SIGHUP the foreground process
2515 * group. It can't change from this point on
2516 * until the revoke is complete.
2517 * The process group changes under both the tty
2518 * lock and proc_list_lock but we need only one
2524 tty_pgsignal(tp
, SIGHUP
, 1);
2526 session_lock(sessp
);
2527 tp
= SESSION_TP(sessp
);
2529 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
2530 sessp
->s_flags
&= ~S_CTTYREF
;
2531 ttyvp
= sessp
->s_ttyvp
;
2532 ttyvid
= sessp
->s_ttyvid
;
2533 sessp
->s_ttyvp
= NULL
;
2534 sessp
->s_ttyvid
= 0;
2535 sessp
->s_ttyp
= TTY_NULL
;
2536 sessp
->s_ttypgrpid
= NO_PID
;
2537 session_unlock(sessp
);
2539 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
2540 if (tp
!= TTY_NULL
) {
2545 context
.vc_thread
= proc_thread(p
); /* XXX */
2546 context
.vc_ucred
= kauth_cred_proc_ref(p
);
2547 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
2550 * Release the extra usecount taken in cttyopen.
2551 * usecount should be released after VNOP_REVOKE is called.
2552 * This usecount was taken to ensure that
2553 * the VNOP_REVOKE results in a close to
2554 * the tty since cttyclose is a no-op.
2559 kauth_cred_unref(&context
.vc_ucred
);
2564 * This is cleared even if not set. This is also done in
2565 * spec_close to ensure that the flag is cleared.
2574 session_lock(sessp
);
2575 sessp
->s_leader
= NULL
;
2576 session_unlock(sessp
);
2578 session_rele(sessp
);
2584 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
2587 proc_childdrainstart(p
);
2588 while ((q
= p
->p_children
.lh_first
) != NULL
) {
2589 if (q
->p_stat
== SZOMB
) {
2590 if (p
!= q
->p_pptr
) {
2591 panic("parent child linkage broken");
2593 /* check for lookups by zomb sysctl */
2594 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2595 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2597 q
->p_listflag
|= P_LIST_WAITING
;
2599 * This is a named reference and it is not granted
2600 * if the reap is already in progress. So we get
2601 * the reference here exclusively and their can be
2602 * no waiters. So there is no need for a wakeup
2603 * after we are done. AlsO the reap frees the structure
2604 * and the proc struct cannot be used for wakeups as well.
2605 * It is safe to use q here as this is system reap
2607 (void)reap_child_locked(p
, q
, 1, 0, 1, 0);
2610 * Traced processes are killed
2611 * since their existence means someone is messing up.
2613 if (q
->p_lflag
& P_LTRACED
) {
2618 opp
= proc_find(q
->p_oppid
);
2619 if (opp
!= PROC_NULL
) {
2623 proc_reparentlocked(q
, opp
, 0, 0);
2626 /* original parent exited while traced */
2628 q
->p_listflag
|= P_LIST_DEADPARENT
;
2631 proc_reparentlocked(q
, initproc
, 0, 0);
2635 q
->p_lflag
&= ~P_LTRACED
;
2637 if (q
->sigwait_thread
) {
2638 thread_t thread
= q
->sigwait_thread
;
2642 * The sigwait_thread could be stopped at a
2643 * breakpoint. Wake it up to kill.
2644 * Need to do this as it could be a thread which is not
2645 * the first thread in the task. So any attempts to kill
2646 * the process would result into a deadlock on q->sigwait.
2648 thread_resume(thread
);
2649 clear_wait(thread
, THREAD_INTERRUPTED
);
2650 threadsignal(thread
, SIGKILL
, 0, TRUE
);
2655 psignal(q
, SIGKILL
);
2658 q
->p_listflag
|= P_LIST_DEADPARENT
;
2659 proc_reparentlocked(q
, initproc
, 0, 1);
2664 proc_childdrainend(p
);
2668 * Release reference to text vnode
2672 if (tvp
!= NULLVP
) {
2677 * Save exit status and final rusage info, adding in child rusage
2678 * info and self times. If we were unable to allocate a zombie
2679 * structure, this information is lost.
2682 rup
->ru
= p
->p_stats
->p_ru
;
2683 timerclear(&rup
->ru
.ru_utime
);
2684 timerclear(&rup
->ru
.ru_stime
);
2688 mach_task_basic_info_data_t tinfo
;
2689 task_thread_times_info_data_t ttimesinfo
;
2690 int task_info_stuff
, task_ttimes_stuff
;
2691 struct timeval ut
, st
;
2693 task_info_stuff
= MACH_TASK_BASIC_INFO_COUNT
;
2694 task_info(task
, MACH_TASK_BASIC_INFO
,
2695 &tinfo
, &task_info_stuff
);
2696 p
->p_ru
->ru
.ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
2697 p
->p_ru
->ru
.ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
2698 p
->p_ru
->ru
.ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
2699 p
->p_ru
->ru
.ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
2701 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
2702 task_info(task
, TASK_THREAD_TIMES_INFO
,
2703 &ttimesinfo
, &task_ttimes_stuff
);
2705 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
2706 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
2707 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
2708 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
2709 timeradd(&ut
, &p
->p_ru
->ru
.ru_utime
, &p
->p_ru
->ru
.ru_utime
);
2710 timeradd(&st
, &p
->p_ru
->ru
.ru_stime
, &p
->p_ru
->ru
.ru_stime
);
2714 ruadd(&rup
->ru
, &p
->p_stats
->p_cru
);
2716 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
2717 rup
->ri
.ri_phys_footprint
= 0;
2718 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
2721 * Now that we have filled in the rusage info, make it
2722 * visible to an external observer via proc_pid_rusage().
2728 * Free up profiling buffers.
2731 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
2737 for (; p1
!= NULL
; p1
= pn
) {
2739 kfree(p1
, sizeof *p1
);
2744 pth_proc_hashdelete(p
);
2747 proc_free_realitimer(p
);
2750 * Other substructures are freed from wait().
2752 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
2755 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
2756 p
->p_sigacts
= NULL
;
2758 proc_limitdrop(p
, 1);
2762 * Finish up by terminating the task
2763 * and halt this thread (only if a
2764 * member of the task exiting).
2766 p
->task
= TASK_NULL
;
2769 * Notify parent that we're gone.
2771 pp
= proc_parent(p
);
2772 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
2773 if (pp
!= initproc
) {
2775 pp
->si_pid
= p
->p_pid
;
2776 pp
->p_xhighbits
= p
->p_xhighbits
;
2778 pp
->si_status
= p
->p_xstat
;
2779 pp
->si_code
= CLD_EXITED
;
2781 * p_ucred usage is safe as it is an exiting process
2782 * and reference is dropped in reap
2784 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
2787 /* mark as a zombie */
2788 /* mark as a zombie */
2789 /* No need to take proc lock as all refs are drained and
2790 * no one except parent (reaping ) can look at this.
2791 * The write is to an int and is coherent. Also parent is
2792 * keyed off of list lock for reaping
2796 psignal(pp
, SIGCHLD
);
2798 /* and now wakeup the parent */
2800 wakeup((caddr_t
)pp
);
2804 /* check for lookups by zomb sysctl */
2805 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2806 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2809 p
->p_listflag
|= P_LIST_WAITING
;
2812 * This is a named reference and it is not granted
2813 * if the reap is already in progress. So we get
2814 * the reference here exclusively and their can be
2815 * no waiters. So there is no need for a wakeup
2816 * after we are done. AlsO the reap frees the structure
2817 * and the proc struct cannot be used for wakeups as well.
2818 * It is safe to use p here as this is system reap
2820 (void)reap_child_locked(pp
, p
, 0, 0, 1, 1);
2821 /* list lock dropped by reap_child_locked */
2829 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
2830 * process. We munge the kernel version of rusage into the
2833 __private_extern__
void
2834 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
2836 /* Zero-out struct so that padding is cleared */
2837 bzero(a_user_rusage_p
, sizeof(struct user64_rusage
));
2839 /* timeval changes size, so utime and stime need special handling */
2840 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2841 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2842 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2843 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2845 * everything else can be a direct assign, since there is no loss
2846 * of precision implied boing 32->64.
2848 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2849 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2850 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2851 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2852 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2853 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2854 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2855 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2856 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2857 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2858 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2859 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2860 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2861 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
2864 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2865 __private_extern__
void
2866 munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
)
2868 bzero(a_user_rusage_p
, sizeof(struct user32_rusage
));
2870 /* timeval changes size, so utime and stime need special handling */
2871 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2872 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2873 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2874 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2876 * everything else can be a direct assign. We currently ignore
2877 * the loss of precision
2879 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2880 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2881 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2882 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2883 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2884 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2885 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2886 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2887 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2888 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2889 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2890 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2891 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2892 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
2896 kdp_wait4_find_process(thread_t thread
, __unused event64_t wait_event
, thread_waitinfo_t
*waitinfo
)
2898 assert(thread
!= NULL
);
2899 assert(waitinfo
!= NULL
);
2901 struct uthread
*ut
= get_bsdthread_info(thread
);
2902 waitinfo
->context
= 0;
2903 // ensure wmesg is consistent with a thread waiting in wait4
2904 assert(!strcmp(ut
->uu_wmesg
, "waitcoll") || !strcmp(ut
->uu_wmesg
, "wait"));
2905 struct wait4_nocancel_args
*args
= ut
->uu_save
.uus_wait4_data
.args
;
2906 // May not actually contain a pid; this is just the argument to wait4.
2907 // See man wait4 for other valid wait4 arguments.
2908 waitinfo
->owner
= args
->pid
;