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>
133 #include <pexpert/pexpert.h>
136 #include <sys/shm_internal.h> /* shmexit */
137 #endif /* SYSV_SHM */
139 #include <sys/persona.h>
140 #endif /* CONFIG_PERSONAS */
141 #if CONFIG_MEMORYSTATUS
142 #include <sys/kern_memorystatus.h>
143 #endif /* CONFIG_MEMORYSTATUS */
145 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
146 void dtrace_proc_exit(proc_t p
);
147 #include <sys/dtrace_ptss.h>
148 #endif /* CONFIG_DTRACE */
150 #include <security/mac_framework.h>
151 #include <security/mac_mach_internal.h>
152 #include <sys/syscall.h>
153 #endif /* CONFIG_MACF */
155 void proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
);
156 void gather_populate_corpse_crashinfo(proc_t p
, task_t corpse_task
,
157 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
,
158 uint64_t *udata_buffer
, int num_udata
, void *reason
);
159 mach_exception_data_type_t
proc_encode_exit_exception_code(proc_t p
);
160 void vfork_exit(proc_t p
, int rv
);
161 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
162 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
163 static int reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
);
164 static void populate_corpse_crashinfo(proc_t p
, task_t corpse_task
,
165 struct rusage_superset
*rup
, mach_exception_data_type_t code
,
166 mach_exception_data_type_t subcode
, uint64_t *udata_buffer
,
167 int num_udata
, os_reason_t reason
);
168 static void proc_update_corpse_exception_codes(proc_t p
, mach_exception_data_type_t
*code
, mach_exception_data_type_t
*subcode
);
169 extern int proc_pidpathinfo_internal(proc_t p
, uint64_t arg
, char *buffer
, uint32_t buffersize
, int32_t *retval
);
170 static __attribute__((noinline
)) void launchd_crashed_panic(proc_t p
, int rv
);
171 extern void proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
172 extern void task_coalition_ids(task_t task
, uint64_t ids
[COALITION_NUM_TYPES
]);
173 extern uint64_t get_task_phys_footprint_limit(task_t
);
174 int proc_list_uptrs(void *p
, uint64_t *udata_buffer
, int size
);
175 extern uint64_t task_corpse_get_crashed_thread_id(task_t corpse_task
);
179 * Things which should have prototypes in headers, but don't
181 void proc_exit(proc_t p
);
182 int wait1continue(int result
);
183 int waitidcontinue(int result
);
184 kern_return_t
sys_perf_notify(thread_t thread
, int pid
);
185 kern_return_t
task_exception_notify(exception_type_t exception
,
186 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
);
187 kern_return_t
task_violated_guard(mach_exception_code_t
, mach_exception_subcode_t
, void *);
189 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
192 * NOTE: Source and target may *NOT* overlap!
193 * XXX Should share code with bsd/dev/ppc/unix_signal.c
196 siginfo_user_to_user32(user_siginfo_t
*in
, user32_siginfo_t
*out
)
198 out
->si_signo
= in
->si_signo
;
199 out
->si_errno
= in
->si_errno
;
200 out
->si_code
= in
->si_code
;
201 out
->si_pid
= in
->si_pid
;
202 out
->si_uid
= in
->si_uid
;
203 out
->si_status
= in
->si_status
;
204 out
->si_addr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_addr
);
205 /* following cast works for sival_int because of padding */
206 out
->si_value
.sival_ptr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_value
.sival_ptr
);
207 out
->si_band
= in
->si_band
; /* range reduction */
211 siginfo_user_to_user64(user_siginfo_t
*in
, user64_siginfo_t
*out
)
213 out
->si_signo
= in
->si_signo
;
214 out
->si_errno
= in
->si_errno
;
215 out
->si_code
= in
->si_code
;
216 out
->si_pid
= in
->si_pid
;
217 out
->si_uid
= in
->si_uid
;
218 out
->si_status
= in
->si_status
;
219 out
->si_addr
= in
->si_addr
;
220 /* following cast works for sival_int because of padding */
221 out
->si_value
.sival_ptr
= in
->si_value
.sival_ptr
;
222 out
->si_band
= in
->si_band
; /* range reduction */
226 copyoutsiginfo(user_siginfo_t
*native
, boolean_t is64
, user_addr_t uaddr
)
229 user64_siginfo_t sinfo64
;
231 bzero(&sinfo64
, sizeof (sinfo64
));
232 siginfo_user_to_user64(native
, &sinfo64
);
233 return (copyout(&sinfo64
, uaddr
, sizeof (sinfo64
)));
235 user32_siginfo_t sinfo32
;
237 bzero(&sinfo32
, sizeof (sinfo32
));
238 siginfo_user_to_user32(native
, &sinfo32
);
239 return (copyout(&sinfo32
, uaddr
, sizeof (sinfo32
)));
243 void gather_populate_corpse_crashinfo(proc_t p
, task_t corpse_task
,
244 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
,
245 uint64_t *udata_buffer
, int num_udata
, void *reason
)
247 struct rusage_superset rup
;
249 gather_rusage_info(p
, &rup
.ri
, RUSAGE_INFO_CURRENT
);
250 rup
.ri
.ri_phys_footprint
= 0;
251 populate_corpse_crashinfo(p
, corpse_task
, &rup
, code
, subcode
,
252 udata_buffer
, num_udata
, reason
);
255 static void proc_update_corpse_exception_codes(proc_t p
, mach_exception_data_type_t
*code
, mach_exception_data_type_t
*subcode
)
257 mach_exception_data_type_t code_update
= *code
;
258 mach_exception_data_type_t subcode_update
= *subcode
;
259 if (p
->p_exit_reason
== OS_REASON_NULL
) {
263 switch (p
->p_exit_reason
->osr_namespace
) {
264 case OS_REASON_JETSAM
:
265 if (p
->p_exit_reason
->osr_code
== JETSAM_REASON_MEMORY_PERPROCESSLIMIT
) {
266 /* Update the code with EXC_RESOURCE code for high memory watermark */
267 EXC_RESOURCE_ENCODE_TYPE(code_update
, RESOURCE_TYPE_MEMORY
);
268 EXC_RESOURCE_ENCODE_FLAVOR(code_update
, FLAVOR_HIGH_WATERMARK
);
269 EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update
, ((get_task_phys_footprint_limit(p
->task
)) >> 20));
280 *subcode
= subcode_update
;
284 mach_exception_data_type_t
proc_encode_exit_exception_code(proc_t p
)
286 uint64_t subcode
= 0;
288 if (p
->p_exit_reason
== OS_REASON_NULL
) {
292 /* Embed first 32 bits of osr_namespace and osr_code in exception code */
293 ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode
, p
->p_exit_reason
->osr_namespace
);
294 ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode
, p
->p_exit_reason
->osr_code
);
295 return (mach_exception_data_type_t
)subcode
;
299 populate_corpse_crashinfo(proc_t p
, task_t corpse_task
, struct rusage_superset
*rup
,
300 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
,
301 uint64_t *udata_buffer
, int num_udata
, os_reason_t reason
)
303 mach_vm_address_t uaddr
= 0;
304 mach_exception_data_type_t exc_codes
[EXCEPTION_CODE_MAX
];
306 exc_codes
[1] = subcode
;
308 struct proc_uniqidentifierinfo p_uniqidinfo
;
309 struct proc_workqueueinfo pwqinfo
;
311 uint64_t crashed_threadid
= task_corpse_get_crashed_thread_id(corpse_task
);
312 unsigned int pflags
= 0;
313 uint64_t max_footprint_mb
;
314 uint64_t max_footprint
;
315 void *crash_info_ptr
= task_get_corpseinfo(corpse_task
);
317 #if CONFIG_MEMORYSTATUS
318 int memstat_dirty_flags
= 0;
321 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_EXCEPTION_CODES
, sizeof(exc_codes
), &uaddr
)) {
322 kcdata_memcpy(crash_info_ptr
, uaddr
, exc_codes
, sizeof(exc_codes
));
325 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PID
, sizeof(p
->p_pid
), &uaddr
)) {
326 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_pid
, sizeof(p
->p_pid
));
329 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PPID
, sizeof(p
->p_ppid
), &uaddr
)) {
330 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_ppid
, sizeof(p
->p_ppid
));
333 /* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
334 if ((p
->p_exit_reason
== OS_REASON_NULL
) || !(p
->p_exit_reason
->osr_flags
& OS_REASON_FLAG_NO_CRASHED_TID
)) {
335 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CRASHED_THREADID
, sizeof(uint64_t), &uaddr
)) {
336 kcdata_memcpy(crash_info_ptr
, uaddr
, &crashed_threadid
, sizeof(uint64_t));
341 kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_BSDINFOWITHUNIQID
, sizeof(struct proc_uniqidentifierinfo
), &uaddr
)) {
342 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
343 kcdata_memcpy(crash_info_ptr
, uaddr
, &p_uniqidinfo
, sizeof(struct proc_uniqidentifierinfo
));
346 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RUSAGE_INFO
, sizeof(rusage_info_current
), &uaddr
)) {
347 kcdata_memcpy(crash_info_ptr
, uaddr
, &rup
->ri
, sizeof(rusage_info_current
));
350 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_CSFLAGS
, sizeof(p
->p_csflags
), &uaddr
)) {
351 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_csflags
, sizeof(p
->p_csflags
));
354 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_NAME
, sizeof(p
->p_comm
), &uaddr
)) {
355 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_comm
, sizeof(p
->p_comm
));
358 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_STARTTIME
, sizeof(p
->p_start
), &uaddr
)) {
359 struct timeval64 t64
;
360 t64
.tv_sec
= (int64_t)p
->p_start
.tv_sec
;
361 t64
.tv_usec
= (int64_t)p
->p_start
.tv_usec
;
362 kcdata_memcpy(crash_info_ptr
, uaddr
, &t64
, sizeof(t64
));
365 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_USERSTACK
, sizeof(p
->user_stack
), &uaddr
)) {
366 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->user_stack
, sizeof(p
->user_stack
));
369 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_ARGSLEN
, sizeof(p
->p_argslen
), &uaddr
)) {
370 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_argslen
, sizeof(p
->p_argslen
));
373 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_ARGC
, sizeof(p
->p_argc
), &uaddr
)) {
374 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_argc
, sizeof(p
->p_argc
));
377 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_PATH
, MAXPATHLEN
, &uaddr
)) {
378 char *buf
= (char *) kalloc(MAXPATHLEN
);
380 bzero(buf
, MAXPATHLEN
);
381 proc_pidpathinfo_internal(p
, 0, buf
, MAXPATHLEN
, &retval
);
382 kcdata_memcpy(crash_info_ptr
, uaddr
, buf
, MAXPATHLEN
);
383 kfree(buf
, MAXPATHLEN
);
387 pflags
= p
->p_flag
& (P_LP64
| P_SUGID
);
388 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_FLAGS
, sizeof(pflags
), &uaddr
)) {
389 kcdata_memcpy(crash_info_ptr
, uaddr
, &pflags
, sizeof(pflags
));
392 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_UID
, sizeof(p
->p_uid
), &uaddr
)) {
393 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_uid
, sizeof(p
->p_uid
));
396 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_GID
, sizeof(p
->p_gid
), &uaddr
)) {
397 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_gid
, sizeof(p
->p_gid
));
400 cputype
= cpu_type() & ~CPU_ARCH_MASK
;
401 if (IS_64BIT_PROCESS(p
))
402 cputype
|= CPU_ARCH_ABI64
;
404 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CPUTYPE
, sizeof(cpu_type_t
), &uaddr
)) {
405 kcdata_memcpy(crash_info_ptr
, uaddr
, &cputype
, sizeof(cpu_type_t
));
408 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_MEMORY_LIMIT
, sizeof(max_footprint_mb
), &uaddr
)) {
409 max_footprint
= get_task_phys_footprint_limit(p
->task
);
410 max_footprint_mb
= max_footprint
>> 20;
411 kcdata_memcpy(crash_info_ptr
, uaddr
, &max_footprint_mb
, sizeof(max_footprint_mb
));
414 bzero(&pwqinfo
, sizeof(struct proc_workqueueinfo
));
415 retval
= fill_procworkqueue(p
, &pwqinfo
);
417 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_WORKQUEUEINFO
, sizeof(struct proc_workqueueinfo
), &uaddr
)) {
418 kcdata_memcpy(crash_info_ptr
, uaddr
, &pwqinfo
, sizeof(struct proc_workqueueinfo
));
422 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RESPONSIBLE_PID
, sizeof(p
->p_responsible_pid
), &uaddr
)) {
423 kcdata_memcpy(crash_info_ptr
, uaddr
, &p
->p_responsible_pid
, sizeof(p
->p_responsible_pid
));
426 #if CONFIG_COALITIONS
427 if (KERN_SUCCESS
== kcdata_get_memory_addr_for_array(crash_info_ptr
, TASK_CRASHINFO_COALITION_ID
, sizeof(uint64_t), COALITION_NUM_TYPES
, &uaddr
)) {
428 uint64_t coalition_ids
[COALITION_NUM_TYPES
];
429 task_coalition_ids(p
->task
, coalition_ids
);
430 kcdata_memcpy(crash_info_ptr
, uaddr
, coalition_ids
, sizeof(coalition_ids
));
432 #endif /* CONFIG_COALITIONS */
434 #if CONFIG_MEMORYSTATUS
435 memstat_dirty_flags
= memorystatus_dirty_get(p
);
436 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_DIRTY_FLAGS
, sizeof(memstat_dirty_flags
), &uaddr
)) {
437 kcdata_memcpy(crash_info_ptr
, uaddr
, &memstat_dirty_flags
, sizeof(memstat_dirty_flags
));
441 if (p
->p_exit_reason
!= OS_REASON_NULL
&& reason
== OS_REASON_NULL
) {
442 reason
= p
->p_exit_reason
;
444 if (reason
!= OS_REASON_NULL
) {
445 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, EXIT_REASON_SNAPSHOT
, sizeof(struct exit_reason_snapshot
), &uaddr
)) {
446 struct exit_reason_snapshot ers
= {
447 .ers_namespace
= reason
->osr_namespace
,
448 .ers_code
= reason
->osr_code
,
449 .ers_flags
= reason
->osr_flags
452 kcdata_memcpy(crash_info_ptr
, uaddr
, &ers
, sizeof(ers
));
455 if (reason
->osr_kcd_buf
!= 0) {
456 uint32_t reason_buf_size
= kcdata_memory_get_used_bytes(&reason
->osr_kcd_descriptor
);
457 assert(reason_buf_size
!= 0);
459 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, KCDATA_TYPE_NESTED_KCDATA
, reason_buf_size
, &uaddr
)) {
460 kcdata_memcpy(crash_info_ptr
, uaddr
, reason
->osr_kcd_buf
, reason_buf_size
);
466 if (KERN_SUCCESS
== kcdata_get_memory_addr_for_array(crash_info_ptr
, TASK_CRASHINFO_UDATA_PTRS
,
467 sizeof(uint64_t), num_udata
, &uaddr
)) {
468 kcdata_memcpy(crash_info_ptr
, uaddr
, udata_buffer
, sizeof(uint64_t) * num_udata
);
474 * We only parse exit reason kcdata blobs for launchd when it dies
475 * and we're going to panic.
477 * Meant to be called immediately before panicking.
480 launchd_exit_reason_get_string_desc(os_reason_t exit_reason
)
484 if (exit_reason
== OS_REASON_NULL
|| exit_reason
->osr_kcd_buf
== NULL
||
485 exit_reason
->osr_bufsize
== 0) {
489 iter
= kcdata_iter(exit_reason
->osr_kcd_buf
, exit_reason
->osr_bufsize
);
490 if (!kcdata_iter_valid(iter
)) {
491 #if DEBUG || DEVELOPMENT
492 printf("launchd exit reason has invalid exit reason buffer\n");
497 if (kcdata_iter_type(iter
) != KCDATA_BUFFER_BEGIN_OS_REASON
) {
498 #if DEBUG || DEVELOPMENT
499 printf("launchd exit reason buffer type mismatch, expected %d got %d\n",
500 KCDATA_BUFFER_BEGIN_OS_REASON
, kcdata_iter_type(iter
));
505 iter
= kcdata_iter_find_type(iter
, EXIT_REASON_USER_DESC
);
506 if (!kcdata_iter_valid(iter
)) {
510 return (char *)kcdata_iter_payload(iter
);
513 static __attribute__((noinline
)) void
514 launchd_crashed_panic(proc_t p
, int rv
)
516 char *launchd_exit_reason_desc
= launchd_exit_reason_get_string_desc(p
->p_exit_reason
);
518 if (p
->p_exit_reason
== OS_REASON_NULL
) {
519 printf("pid 1 exited -- no exit reason available -- (signal %d, exit %d)\n",
520 WTERMSIG(rv
), WEXITSTATUS(rv
));
522 printf("pid 1 exited -- exit reason namespace %d subcode 0x%llx, description %s\n",
523 p
->p_exit_reason
->osr_namespace
, p
->p_exit_reason
->osr_code
, launchd_exit_reason_desc
?
524 launchd_exit_reason_desc
: "none");
527 const char *launchd_crashed_prefix_str
;
529 if (strnstr(p
->p_name
, "preinit", sizeof(p
->p_name
))) {
530 launchd_crashed_prefix_str
= "LTE preinit process exited";
532 launchd_crashed_prefix_str
= "initproc exited";
535 #if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
537 * For debugging purposes, generate a core file of initproc before
538 * panicking. Leave at least 300 MB free on the root volume, and ignore
539 * the process's corefile ulimit. fsync() the file to ensure it lands on disk
540 * before the panic hits.
544 uint64_t coredump_start
= mach_absolute_time();
545 uint64_t coredump_end
;
547 clock_usec_t tv_usec
;
551 err
= coredump(p
, 300, COREDUMP_IGNORE_ULIMIT
| COREDUMP_FULLFSYNC
);
553 coredump_end
= mach_absolute_time();
555 absolutetime_to_microtime(coredump_end
- coredump_start
, &tv_sec
, &tv_usec
);
557 tv_msec
= tv_usec
/ 1000;
560 printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
561 err
, (uint32_t)tv_sec
, tv_msec
);
563 printf("Generated initproc core file in %d.%03d seconds\n",
564 (uint32_t)tv_sec
, tv_msec
);
566 #endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
568 sync(p
, (void *)NULL
, (int *)NULL
);
570 if (p
->p_exit_reason
== OS_REASON_NULL
) {
571 panic_with_options(0, NULL
, DEBUGGER_OPTION_INITPROC_PANIC
, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
572 launchd_crashed_prefix_str
, WTERMSIG(rv
), WEXITSTATUS(rv
), ((p
->p_csflags
& CS_KILLED
) ? "CS_KILLED" : ""));
574 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",
575 ((p
->p_csflags
& CS_KILLED
) ? "CS_KILLED" : ""),
576 launchd_crashed_prefix_str
, p
->p_exit_reason
->osr_namespace
, p
->p_exit_reason
->osr_code
,
577 launchd_exit_reason_desc
? launchd_exit_reason_desc
: "none");
581 #define OS_REASON_IFLAG_USER_FAULT 0x1
583 #define OS_REASON_TOTAL_USER_FAULTS_PER_PROC 5
586 abort_with_payload_internal(proc_t p
,
587 uint32_t reason_namespace
, uint64_t reason_code
,
588 user_addr_t payload
, uint32_t payload_size
,
589 user_addr_t reason_string
, uint64_t reason_flags
,
590 uint32_t internal_flags
)
592 os_reason_t exit_reason
= OS_REASON_NULL
;
593 kern_return_t kr
= KERN_SUCCESS
;
595 if (internal_flags
& OS_REASON_IFLAG_USER_FAULT
) {
596 uint32_t old_value
= atomic_load_explicit(&p
->p_user_faults
,
597 memory_order_relaxed
);
599 if (old_value
>= OS_REASON_TOTAL_USER_FAULTS_PER_PROC
) {
602 // this reloads the value in old_value
603 if (atomic_compare_exchange_strong_explicit(&p
->p_user_faults
,
604 &old_value
, old_value
+ 1, memory_order_relaxed
,
605 memory_order_relaxed
)) {
611 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
612 p
->p_pid
, reason_namespace
,
615 exit_reason
= build_userspace_exit_reason(reason_namespace
, reason_code
,
616 payload
, payload_size
, reason_string
, reason_flags
);
618 if (internal_flags
& OS_REASON_IFLAG_USER_FAULT
) {
619 mach_exception_code_t code
= 0;
621 EXC_GUARD_ENCODE_TYPE(code
, GUARD_TYPE_USER
); /* simulated EXC_GUARD */
622 EXC_GUARD_ENCODE_FLAVOR(code
, 0);
623 EXC_GUARD_ENCODE_TARGET(code
, reason_namespace
);
625 if (exit_reason
== OS_REASON_NULL
) {
626 kr
= KERN_RESOURCE_SHORTAGE
;
628 kr
= task_violated_guard(code
, reason_code
, exit_reason
);
630 os_reason_free(exit_reason
);
633 * We use SIGABRT (rather than calling exit directly from here) so that
634 * the debugger can catch abort_with_{reason,payload} calls.
636 psignal_try_thread_with_reason(p
, current_thread(), SIGABRT
, exit_reason
);
642 case KERN_NOT_SUPPORTED
:
644 case KERN_INVALID_ARGUMENT
:
646 case KERN_RESOURCE_SHORTAGE
:
653 abort_with_payload(struct proc
*cur_proc
, struct abort_with_payload_args
*args
,
654 __unused
void *retval
)
656 abort_with_payload_internal(cur_proc
, args
->reason_namespace
,
657 args
->reason_code
, args
->payload
, args
->payload_size
,
658 args
->reason_string
, args
->reason_flags
, 0);
664 os_fault_with_payload(struct proc
*cur_proc
,
665 struct os_fault_with_payload_args
*args
, __unused
int *retval
)
667 return abort_with_payload_internal(cur_proc
, args
->reason_namespace
,
668 args
->reason_code
, args
->payload
, args
->payload_size
,
669 args
->reason_string
, args
->reason_flags
, OS_REASON_IFLAG_USER_FAULT
);
677 __attribute__((noreturn
))
679 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
681 p
->p_xhighbits
= ((uint32_t)(uap
->rval
) & 0xFF000000) >> 24;
682 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
684 thread_exception_return();
687 thread_block(THREAD_CONTINUE_NULL
);
692 * Exit: deallocate address space and other resources, change proc state
693 * to zombie, and unlink proc from allproc and parent's lists. Save exit
694 * status and rusage for wait(). Check for child processes and orphan them.
697 exit1(proc_t p
, int rv
, int *retval
)
699 return exit1_internal(p
, rv
, retval
, TRUE
, TRUE
, 0);
703 exit1_internal(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
706 return exit_with_reason(p
, rv
, retval
, thread_can_terminate
, perf_notify
, jetsam_flags
, OS_REASON_NULL
);
710 * NOTE: exit_with_reason drops a reference on the passed exit_reason
713 exit_with_reason(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
714 int jetsam_flags
, struct os_reason
*exit_reason
)
716 thread_t self
= current_thread();
717 struct task
*task
= p
->task
;
722 * If a thread in this task has already
723 * called exit(), then halt any others
727 ut
= get_bsdthread_info(self
);
728 if ((p
== current_proc()) &&
729 (ut
->uu_flag
& UT_VFORK
)) {
730 os_reason_free(exit_reason
);
731 if (!thread_can_terminate
) {
736 vfork_return(p
, retval
, p
->p_pid
);
737 unix_syscall_return(0);
742 * The parameter list of audit_syscall_exit() was augmented to
743 * take the Darwin syscall number as the first parameter,
744 * which is currently required by mac_audit_postselect().
748 * The BSM token contains two components: an exit status as passed
749 * to exit(), and a return value to indicate what sort of exit it
750 * was. The exit status is WEXITSTATUS(rv), but it's not clear
751 * what the return value is.
753 AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0);
755 * TODO: what to audit here when jetsam calls exit and the uthread,
756 * 'ut' does not belong to the proc, 'p'.
758 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
760 DTRACE_PROC1(exit
, int, CLD_EXITED
);
762 /* mark process is going to exit and pull out of DBG/disk throttle */
763 /* TODO: This should be done after becoming exit thread */
764 proc_set_task_policy(p
->task
, TASK_POLICY_ATTRIBUTE
,
765 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
768 error
= proc_transstart(p
, 1, (jetsam_flags
? 1 : 0));
769 if (error
== EDEADLK
) {
771 * If proc_transstart() returns EDEADLK, then another thread
772 * is either exec'ing or exiting. Return an error and allow
773 * the other thread to continue.
776 os_reason_free(exit_reason
);
777 if (current_proc() == p
){
778 if (p
->exit_thread
== self
) {
779 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
780 p
->p_comm
, p
->p_pid
);
783 if (thread_can_terminate
) {
784 thread_exception_return();
791 while (p
->exit_thread
!= self
) {
792 if (sig_try_locked(p
) <= 0) {
794 os_reason_free(exit_reason
);
796 if (get_threadtask(self
) != task
) {
802 thread_terminate(self
);
803 if (!thread_can_terminate
) {
807 thread_exception_return();
813 if (exit_reason
!= OS_REASON_NULL
) {
814 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_COMMIT
) | DBG_FUNC_NONE
,
815 p
->p_pid
, exit_reason
->osr_namespace
,
816 exit_reason
->osr_code
, 0, 0);
819 assert(p
->p_exit_reason
== OS_REASON_NULL
);
820 p
->p_exit_reason
= exit_reason
;
822 p
->p_lflag
|= P_LEXIT
;
824 p
->p_lflag
|= jetsam_flags
;
829 proc_prepareexit(p
, rv
, perf_notify
);
831 /* Last thread to terminate will call proc_exit() */
832 task_terminate_internal(task
);
838 proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
)
840 mach_exception_data_type_t code
= 0, subcode
= 0;
843 thread_t self
= current_thread();
844 ut
= get_bsdthread_info(self
);
845 struct rusage_superset
*rup
;
847 int create_corpse
= FALSE
;
850 launchd_crashed_panic(p
, rv
);
855 * Generate a corefile/crashlog if:
856 * The process doesn't have an exit reason that indicates no crash report should be created
857 * AND any of the following are true:
858 * - The process was terminated due to a fatal signal that generates a core
859 * - The process was killed due to a code signing violation
860 * - The process has an exit reason that indicates we should generate a crash report
862 * The first condition is necessary because abort_with_reason()/payload() use SIGABRT
863 * (which normally triggers a core) but may indicate that no crash report should be created.
865 if (!(PROC_HAS_EXITREASON(p
) && (PROC_EXITREASON_FLAGS(p
) & OS_REASON_FLAG_NO_CRASH_REPORT
)) &&
866 (hassigprop(WTERMSIG(rv
), SA_CORE
) || ((p
->p_csflags
& CS_KILLED
) != 0) ||
867 (PROC_HAS_EXITREASON(p
) && (PROC_EXITREASON_FLAGS(p
) &
868 OS_REASON_FLAG_GENERATE_CRASH_REPORT
)))) {
870 * Workaround for processes checking up on PT_DENY_ATTACH:
871 * should be backed out post-Leopard (details in 5431025).
873 if ((SIGSEGV
== WTERMSIG(rv
)) &&
874 (p
->p_pptr
->p_lflag
& P_LNOATTACH
)) {
879 * Crash Reporter looks for the signal value, original exception
880 * type, and low 20 bits of the original code in code[0]
881 * (8, 4, and 20 bits respectively). code[1] is unmodified.
883 code
= ((WTERMSIG(rv
) & 0xff) << 24) |
884 ((ut
->uu_exception
& 0x0f) << 20) |
885 ((int)ut
->uu_code
& 0xfffff);
886 subcode
= ut
->uu_subcode
;
888 kr
= task_exception_notify(EXC_CRASH
, code
, subcode
);
890 /* Nobody handled EXC_CRASH?? remember to make corpse */
892 create_corpse
= TRUE
;
897 /* Notify the perf server? */
899 (void)sys_perf_notify(self
, p
->p_pid
);
903 /* stash the usage into corpse data if making_corpse == true */
904 if (create_corpse
== TRUE
) {
905 kr
= task_mark_corpse(p
->task
);
906 if (kr
!= KERN_SUCCESS
) {
907 if (kr
== KERN_NO_SPACE
) {
908 printf("Process[%d] has no vm space for corpse info.\n", p
->p_pid
);
909 } else if (kr
== KERN_NOT_SUPPORTED
) {
910 printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", p
->p_pid
);
912 printf("Process[%d] crashed: %s. Too many corpses being created.\n", p
->p_pid
, p
->p_comm
);
914 create_corpse
= FALSE
;
919 * Before this process becomes a zombie, stash resource usage
920 * stats in the proc for external observers to query
921 * via proc_pid_rusage().
923 * If the zombie allocation fails, just punt the stats.
925 MALLOC_ZONE(rup
, struct rusage_superset
*,
926 sizeof (*rup
), M_ZOMBIE
, M_WAITOK
);
928 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
929 rup
->ri
.ri_phys_footprint
= 0;
930 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
933 * Make the rusage_info visible to external observers
934 * only after it has been completely filled in.
939 int est_knotes
= 0, num_knotes
= 0;
940 uint64_t *buffer
= NULL
;
943 /* Get all the udata pointers from kqueue */
944 est_knotes
= kevent_proc_copy_uptrs(p
, NULL
, 0);
945 if (est_knotes
> 0) {
946 buf_size
= (est_knotes
+ 32) * sizeof(uint64_t);
947 buffer
= (uint64_t *) kalloc(buf_size
);
948 num_knotes
= kevent_proc_copy_uptrs(p
, buffer
, buf_size
);
949 if (num_knotes
> est_knotes
+ 32) {
950 num_knotes
= est_knotes
+ 32;
954 /* Update the code, subcode based on exit reason */
955 proc_update_corpse_exception_codes(p
, &code
, &subcode
);
956 populate_corpse_crashinfo(p
, p
->task
, rup
,
957 code
, subcode
, buffer
, num_knotes
, NULL
);
958 if (buffer
!= NULL
) {
959 kfree(buffer
, buf_size
);
963 * Remove proc from allproc queue and from pidhash chain.
964 * Need to do this before we do anything that can block.
965 * Not doing causes things like mount() find this on allproc
966 * in partially cleaned state.
971 #if CONFIG_MEMORYSTATUS
972 memorystatus_remove(p
, TRUE
);
975 LIST_REMOVE(p
, p_list
);
976 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
977 /* will not be visible via proc_find */
978 p
->p_listflag
|= P_LIST_EXITED
;
987 * If parent is waiting for us to exit or exec,
988 * P_LPPWAIT is set; we will wakeup the parent below.
991 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
992 p
->p_sigignore
= ~(sigcantmask
);
1002 struct task
*task
= p
->task
;
1003 vnode_t tvp
= NULLVP
;
1005 struct session
*sessp
;
1006 struct uthread
* uth
;
1011 uth
= current_uthread();
1014 proc_transstart(p
, 1, 0);
1015 if( !(p
->p_lflag
& P_LEXIT
)) {
1017 * This can happen if a thread_terminate() occurs
1018 * in a single-threaded process.
1020 p
->p_lflag
|= P_LEXIT
;
1021 proc_transend(p
, 1);
1023 proc_prepareexit(p
, 0, TRUE
);
1024 (void) task_terminate_internal(task
);
1027 proc_transend(p
, 1);
1030 p
->p_lflag
|= P_LPEXIT
;
1033 * Other kernel threads may be in the middle of signalling this process.
1034 * Wait for those threads to wrap it up before making the process
1035 * disappear on them.
1037 if ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 0)) {
1039 while ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 1))
1040 msleep(&p
->p_sigmask
, &p
->p_mlock
, PWAIT
, "proc_sigdrain", NULL
);
1046 exitval
= p
->p_xstat
;
1047 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1048 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_START
,
1049 pid
, exitval
, 0, 0, 0);
1052 dtrace_proc_exit(p
);
1055 nspace_proc_exit(p
);
1058 * need to cancel async IO requests that can be cancelled and wait for those
1059 * already active. MAY BLOCK!
1064 /* if any pending cpu limits action, clear it */
1065 task_clear_cpuusage(p
->task
, TRUE
);
1067 workqueue_mark_exiting(p
);
1072 * Close open files and release open-file table.
1078 * Once all the knotes, kqueues & workloops are destroyed, get rid of the
1083 if (uth
->uu_lowpri_window
) {
1085 * task is marked as a low priority I/O type
1086 * and the I/O we issued while in flushing files on close
1087 * collided with normal I/O operations...
1088 * no need to throttle this thread since its going away
1089 * but we do need to update our bookeeping w/r to throttled threads
1091 throttle_lowpri_io(0);
1095 /* Close ref SYSV Shared memory*/
1100 /* Release SYSV semaphores */
1105 pth_proc_hashdelete(p
);
1108 sessp
= proc_session(p
);
1109 if (SESS_LEADER(p
, sessp
)) {
1111 if (sessp
->s_ttyvp
!= NULLVP
) {
1112 struct vnode
*ttyvp
;
1115 struct vfs_context context
;
1119 * Controlling process.
1120 * Signal foreground pgrp,
1121 * drain controlling terminal
1122 * and revoke access to controlling terminal.
1124 session_lock(sessp
);
1125 tp
= SESSION_TP(sessp
);
1126 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1127 session_unlock(sessp
);
1130 * We're going to SIGHUP the foreground process
1131 * group. It can't change from this point on
1132 * until the revoke is complete.
1133 * The process group changes under both the tty
1134 * lock and proc_list_lock but we need only one
1140 tty_pgsignal(tp
, SIGHUP
, 1);
1142 session_lock(sessp
);
1143 tp
= SESSION_TP(sessp
);
1145 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
1146 sessp
->s_flags
&= ~S_CTTYREF
;
1147 ttyvp
= sessp
->s_ttyvp
;
1148 ttyvid
= sessp
->s_ttyvid
;
1149 sessp
->s_ttyvp
= NULLVP
;
1150 sessp
->s_ttyvid
= 0;
1151 sessp
->s_ttyp
= TTY_NULL
;
1152 sessp
->s_ttypgrpid
= NO_PID
;
1153 session_unlock(sessp
);
1155 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1156 if (tp
!= TTY_NULL
) {
1161 context
.vc_thread
= proc_thread(p
); /* XXX */
1162 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1163 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1166 * Release the extra usecount taken in cttyopen.
1167 * usecount should be released after VNOP_REVOKE is called.
1168 * This usecount was taken to ensure that
1169 * the VNOP_REVOKE results in a close to
1170 * the tty since cttyclose is a no-op.
1175 kauth_cred_unref(&context
.vc_ucred
);
1180 * This is cleared even if not set. This is also done in
1181 * spec_close to ensure that the flag is cleared.
1190 session_lock(sessp
);
1191 sessp
->s_leader
= NULL
;
1192 session_unlock(sessp
);
1194 session_rele(sessp
);
1200 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1201 (void)acct_process(p
);
1205 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) {
1206 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
1207 proc_shutdown_exitcount
--;
1208 if (proc_shutdown_exitcount
== 0)
1209 wakeup(&proc_shutdown_exitcount
);
1212 /* wait till parentrefs are dropped and grant no more */
1213 proc_childdrainstart(p
);
1214 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1215 int reparentedtoinit
= (q
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
1216 if (q
->p_stat
== SZOMB
) {
1218 panic("parent child linkage broken");
1219 /* check for sysctl zomb lookup */
1220 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1221 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1223 q
->p_listflag
|= P_LIST_WAITING
;
1225 * This is a named reference and it is not granted
1226 * if the reap is already in progress. So we get
1227 * the reference here exclusively and their can be
1228 * no waiters. So there is no need for a wakeup
1229 * after we are done. Also the reap frees the structure
1230 * and the proc struct cannot be used for wakeups as well.
1231 * It is safe to use q here as this is system reap
1233 (void)reap_child_locked(p
, q
, 1, reparentedtoinit
, 1, 0);
1236 * Traced processes are killed
1237 * since their existence means someone is messing up.
1239 if (q
->p_lflag
& P_LTRACED
) {
1243 * Take a reference on the child process to
1244 * ensure it doesn't exit and disappear between
1245 * the time we drop the list_lock and attempt
1246 * to acquire its proc_lock.
1248 if (proc_ref_locked(q
) != q
)
1253 opp
= proc_find(q
->p_oppid
);
1254 if (opp
!= PROC_NULL
) {
1258 proc_reparentlocked(q
, opp
, 0, 0);
1261 /* original parent exited while traced */
1263 q
->p_listflag
|= P_LIST_DEADPARENT
;
1266 proc_reparentlocked(q
, initproc
, 0, 0);
1270 q
->p_lflag
&= ~P_LTRACED
;
1272 if (q
->sigwait_thread
) {
1273 thread_t thread
= q
->sigwait_thread
;
1277 * The sigwait_thread could be stopped at a
1278 * breakpoint. Wake it up to kill.
1279 * Need to do this as it could be a thread which is not
1280 * the first thread in the task. So any attempts to kill
1281 * the process would result into a deadlock on q->sigwait.
1283 thread_resume(thread
);
1284 clear_wait(thread
, THREAD_INTERRUPTED
);
1285 threadsignal(thread
, SIGKILL
, 0, TRUE
);
1290 psignal(q
, SIGKILL
);
1292 proc_rele_locked(q
);
1294 q
->p_listflag
|= P_LIST_DEADPARENT
;
1295 proc_reparentlocked(q
, initproc
, 0, 1);
1300 proc_childdrainend(p
);
1305 * Notify MAC policies that proc is dead.
1306 * This should be replaced with proper label management
1307 * (rdar://problem/32126399).
1309 mac_proc_notify_exit(p
);
1313 * Release reference to text vnode
1317 if (tvp
!= NULLVP
) {
1322 * Save exit status and final rusage info, adding in child rusage
1323 * info and self times. If we were unable to allocate a zombie
1324 * structure, this information is lost.
1326 if (p
->p_ru
!= NULL
) {
1327 calcru(p
, &p
->p_stats
->p_ru
.ru_utime
, &p
->p_stats
->p_ru
.ru_stime
, NULL
);
1328 p
->p_ru
->ru
= p
->p_stats
->p_ru
;
1330 ruadd(&(p
->p_ru
->ru
), &p
->p_stats
->p_cru
);
1334 * Free up profiling buffers.
1337 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1343 for (; p1
!= NULL
; p1
= pn
) {
1345 kfree(p1
, sizeof *p1
);
1349 proc_free_realitimer(p
);
1352 * Other substructures are freed from wait().
1354 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
1357 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
1358 p
->p_sigacts
= NULL
;
1360 proc_limitdrop(p
, 1);
1363 vm_purgeable_disown(p
->task
);
1366 * Finish up by terminating the task
1367 * and halt this thread (only if a
1368 * member of the task exiting).
1370 p
->task
= TASK_NULL
;
1371 set_bsdtask_info(task
, NULL
);
1373 knote_hint
= NOTE_EXIT
| (p
->p_xstat
& 0xffff);
1374 proc_knote(p
, knote_hint
);
1376 /* mark the thread as the one that is doing proc_exit
1377 * no need to hold proc lock in uthread_free
1379 uth
->uu_flag
|= UT_PROCEXIT
;
1381 * Notify parent that we're gone.
1383 pp
= proc_parent(p
);
1384 if (pp
->p_flag
& P_NOCLDWAIT
) {
1386 if (p
->p_ru
!= NULL
) {
1390 * If the parent is ignoring SIGCHLD, then POSIX requires
1391 * us to not add the resource usage to the parent process -
1392 * we are only going to hand it off to init to get reaped.
1393 * We should contest the standard in this case on the basis
1396 #else /* !3839178 */
1398 * Add child resource usage to parent before giving
1399 * zombie to init. If we were unable to allocate a
1400 * zombie structure, this information is lost.
1402 ruadd(&pp
->p_stats
->p_cru
, &p
->p_ru
->ru
);
1403 #endif /* !3839178 */
1404 update_rusage_info_child(&pp
->p_stats
->ri_child
, &p
->p_ru
->ri
);
1408 /* kernel can reap this one, no need to move it to launchd */
1410 p
->p_listflag
|= P_LIST_DEADPARENT
;
1413 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0 || p
->p_oppid
) {
1414 if (pp
!= initproc
) {
1416 pp
->si_pid
= p
->p_pid
;
1417 pp
->p_xhighbits
= p
->p_xhighbits
;
1419 pp
->si_status
= p
->p_xstat
;
1420 pp
->si_code
= CLD_EXITED
;
1422 * p_ucred usage is safe as it is an exiting process
1423 * and reference is dropped in reap
1425 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
1428 /* mark as a zombie */
1429 /* No need to take proc lock as all refs are drained and
1430 * no one except parent (reaping ) can look at this.
1431 * The write is to an int and is coherent. Also parent is
1432 * keyed off of list lock for reaping
1434 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1435 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
1436 pid
, exitval
, 0, 0, 0);
1439 * The current process can be reaped so, no one
1440 * can depend on this
1443 psignal(pp
, SIGCHLD
);
1445 /* and now wakeup the parent */
1447 wakeup((caddr_t
)pp
);
1450 /* should be fine as parent proc would be initproc */
1451 /* mark as a zombie */
1452 /* No need to take proc lock as all refs are drained and
1453 * no one except parent (reaping ) can look at this.
1454 * The write is to an int and is coherent. Also parent is
1455 * keyed off of list lock for reaping
1458 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1459 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
1460 pid
, exitval
, 0, 0, 0);
1461 /* check for sysctl zomb lookup */
1462 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1463 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1465 /* safe to use p as this is a system reap */
1467 p
->p_listflag
|= P_LIST_WAITING
;
1470 * This is a named reference and it is not granted
1471 * if the reap is already in progress. So we get
1472 * the reference here exclusively and their can be
1473 * no waiters. So there is no need for a wakeup
1474 * after we are done. AlsO the reap frees the structure
1475 * and the proc struct cannot be used for wakeups as well.
1476 * It is safe to use p here as this is system reap
1478 (void)reap_child_locked(pp
, p
, 1, 0, 1, 1);
1479 /* list lock dropped by reap_child_locked */
1481 if (uth
->uu_lowpri_window
) {
1483 * task is marked as a low priority I/O type and we've
1484 * somehow picked up another throttle during exit processing...
1485 * no need to throttle this thread since its going away
1486 * but we do need to update our bookeeping w/r to throttled threads
1488 throttle_lowpri_io(0);
1499 * Description: Given a process from which all status information needed
1500 * has already been extracted, if the process is a ptrace
1501 * attach process, detach it and give it back to its real
1502 * parent, else recover all resources remaining associated
1505 * Parameters: proc_t parent Parent of process being reaped
1506 * proc_t child Process to reap
1508 * Returns: 0 Process was not reaped because it
1509 * came from an attach
1510 * 1 Process was reaped
1513 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
)
1515 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
1521 * If we got the child via a ptrace 'attach',
1522 * we need to give it back to the old parent.
1524 * Exception: someone who has been reparented to launchd before being
1525 * ptraced can simply be reaped, refer to radar 5677288
1526 * p_oppid -> ptraced
1527 * trace_parent == initproc -> away from launchd
1528 * reparentedtoinit -> came to launchd by reparenting
1530 if (child
->p_oppid
) {
1535 oppid
= child
->p_oppid
;
1537 knote_hint
= NOTE_EXIT
| (child
->p_xstat
& 0xffff);
1540 if ((trace_parent
= proc_find(oppid
))
1541 && !((trace_parent
== initproc
) && reparentedtoinit
)) {
1543 if (trace_parent
!= initproc
) {
1545 * proc internal fileds and p_ucred usage safe
1546 * here as child is dead and is not reaped or
1549 proc_lock(trace_parent
);
1550 trace_parent
->si_pid
= child
->p_pid
;
1551 trace_parent
->si_status
= child
->p_xstat
;
1552 trace_parent
->si_code
= CLD_CONTINUED
;
1553 trace_parent
->si_uid
= kauth_cred_getruid(child
->p_ucred
);
1554 proc_unlock(trace_parent
);
1556 proc_reparentlocked(child
, trace_parent
, 1, 0);
1558 /* resend knote to original parent (and others) after reparenting */
1559 proc_knote(child
, knote_hint
);
1561 psignal(trace_parent
, SIGCHLD
);
1563 wakeup((caddr_t
)trace_parent
);
1564 child
->p_listflag
&= ~P_LIST_WAITING
;
1565 wakeup(&child
->p_stat
);
1567 proc_rele(trace_parent
);
1568 if ((locked
== 1) && (droplock
== 0))
1574 * If we can't reparent (e.g. the original parent exited while child was being debugged, or
1575 * original parent is the same as the debugger currently exiting), we still need to satisfy
1576 * the knote lifecycle for other observers on the system. While the debugger was attached,
1577 * the NOTE_EXIT would not have been broadcast during initial child termination.
1579 proc_knote(child
, knote_hint
);
1581 if (trace_parent
!= PROC_NULL
) {
1582 proc_rele(trace_parent
);
1586 #pragma clang diagnostic push
1587 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1588 proc_knote(child
, NOTE_REAP
);
1589 #pragma clang diagnostic pop
1591 proc_knote_drain(child
);
1598 * If the parent is ignoring SIGCHLD, then POSIX requires
1599 * us to not add the resource usage to the parent process -
1600 * we are only going to hand it off to init to get reaped.
1601 * We should contest the standard in this case on the basis
1604 if (!(parent
->p_flag
& P_NOCLDWAIT
))
1605 #endif /* 3839178 */
1606 ruadd(&parent
->p_stats
->p_cru
, &child
->p_ru
->ru
);
1607 update_rusage_info_child(&parent
->p_stats
->ri_child
, &child
->p_ru
->ri
);
1608 proc_unlock(parent
);
1609 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
1612 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
1615 AUDIT_SESSION_PROCEXIT(child
);
1618 * Decrement the count of procs running with this uid.
1619 * p_ucred usage is safe here as it is an exited process.
1620 * and refernce is dropped after these calls down below
1621 * (locking protection is provided by list lock held in chgproccnt)
1625 * persona_proc_drop calls chgproccnt(-1) on the persona uid,
1626 * and (+1) on the child->p_ucred uid
1628 persona_proc_drop(child
);
1630 (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1);
1632 os_reason_free(child
->p_exit_reason
);
1635 * Free up credentials.
1637 if (IS_VALID_CRED(child
->p_ucred
)) {
1638 kauth_cred_unref(&child
->p_ucred
);
1641 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
1644 * Finally finished with old proc entry.
1645 * Unlink it from its process group and free it.
1650 LIST_REMOVE(child
, p_list
); /* off zombproc */
1651 parent
->p_childrencnt
--;
1652 LIST_REMOVE(child
, p_sibling
);
1653 /* If there are no more children wakeup parent */
1654 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
)))
1655 wakeup((caddr_t
)parent
); /* with list lock held */
1656 child
->p_listflag
&= ~P_LIST_WAITING
;
1657 wakeup(&child
->p_stat
);
1659 /* Take it out of process hash */
1660 LIST_REMOVE(child
, p_hash
);
1661 child
->p_listflag
&= ~P_LIST_INHASH
;
1662 proc_checkdeadrefs(child
);
1667 * If a child zombie is being reaped because its parent
1668 * is exiting, make sure we update the list flag
1670 child
->p_listflag
|= P_LIST_DEADPARENT
;
1675 #if CONFIG_FINE_LOCK_GROUPS
1676 lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
);
1677 lck_mtx_destroy(&child
->p_ucred_mlock
, proc_ucred_mlock_grp
);
1678 lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
);
1680 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1682 lck_spin_destroy(&child
->p_slock
, proc_slock_grp
);
1683 #else /* CONFIG_FINE_LOCK_GROUPS */
1684 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
1685 lck_mtx_destroy(&child
->p_ucred_mlock
, proc_lck_grp
);
1686 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
1688 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1690 lck_spin_destroy(&child
->p_slock
, proc_lck_grp
);
1691 #endif /* CONFIG_FINE_LOCK_GROUPS */
1693 FREE_ZONE(child
, sizeof *child
, M_PROC
);
1694 if ((locked
== 1) && (droplock
== 0))
1702 wait1continue(int result
)
1707 struct _wait4_data
*wait4_data
;
1708 struct wait4_nocancel_args
*uap
;
1715 thread
= current_thread();
1716 uth
= (struct uthread
*)get_bsdthread_info(thread
);
1718 wait4_data
= &uth
->uu_kevent
.uu_wait4_data
;
1719 uap
= wait4_data
->args
;
1720 retval
= wait4_data
->retval
;
1721 return(wait4_nocancel(p
, uap
, retval
));
1725 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
1727 __pthread_testcancel(1);
1728 return(wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
));
1732 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1739 struct _wait4_data
*wait4_data
;
1741 AUDIT_ARG(pid
, uap
->pid
);
1744 uap
->pid
= -q
->p_pgrpid
;
1752 PCHILDREN_FOREACH(q
, p
) {
1753 if ( p
->p_sibling
.le_next
!= 0 )
1755 if (uap
->pid
!= WAIT_ANY
&&
1756 p
->p_pid
!= uap
->pid
&&
1757 p
->p_pgrpid
!= -(uap
->pid
))
1762 /* XXX This is racy because we don't get the lock!!!! */
1764 if (p
->p_listflag
& P_LIST_WAITING
) {
1765 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1768 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1771 if (p
->p_stat
== SZOMB
) {
1772 int reparentedtoinit
= (p
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
1776 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1779 retval
[0] = p
->p_pid
;
1781 /* Legacy apps expect only 8 bits of status */
1782 status
= 0xffff & p
->p_xstat
; /* convert to int */
1783 error
= copyout((caddr_t
)&status
,
1790 if (p
->p_ru
== NULL
) {
1793 if (IS_64BIT_PROCESS(q
)) {
1794 struct user64_rusage my_rusage
= {};
1795 munge_user64_rusage(&p
->p_ru
->ru
, &my_rusage
);
1796 error
= copyout((caddr_t
)&my_rusage
,
1798 sizeof (my_rusage
));
1801 struct user32_rusage my_rusage
= {};
1802 munge_user32_rusage(&p
->p_ru
->ru
, &my_rusage
);
1803 error
= copyout((caddr_t
)&my_rusage
,
1805 sizeof (my_rusage
));
1808 /* information unavailable? */
1813 /* Conformance change for 6577252.
1814 * When SIGCHLD is blocked and wait() returns because the status
1815 * of a child process is available and there are no other
1816 * children processes, then any pending SIGCHLD signal is cleared.
1818 if ( sibling_count
== 0 ) {
1819 int mask
= sigmask(SIGCHLD
);
1820 uth
= current_uthread();
1822 if ( (uth
->uu_sigmask
& mask
) != 0 ) {
1823 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1824 * This locking looks funny but it is protecting access to the
1825 * thread via p_uthlist.
1828 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
1834 (void)reap_child_locked(q
, p
, 0, reparentedtoinit
, 0, 0);
1838 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1839 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1842 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1846 p
->p_lflag
|= P_LWAITED
;
1848 retval
[0] = p
->p_pid
;
1850 status
= W_STOPCODE(p
->p_xstat
);
1851 error
= copyout((caddr_t
)&status
,
1859 * If we are waiting for continued processses, and this
1860 * process was continued
1862 if ((uap
->options
& WCONTINUED
) &&
1863 (p
->p_flag
& P_CONTINUED
)) {
1866 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1870 /* Prevent other process for waiting for this event */
1871 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1872 retval
[0] = p
->p_pid
;
1874 status
= W_STOPCODE(SIGCONT
);
1875 error
= copyout((caddr_t
)&status
,
1882 p
->p_listflag
&= ~P_LIST_WAITING
;
1885 /* list lock is held when we get here any which way */
1891 if (uap
->options
& WNOHANG
) {
1897 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
1898 uth
= current_uthread();
1899 wait4_data
= &uth
->uu_kevent
.uu_wait4_data
;
1900 wait4_data
->args
= uap
;
1901 wait4_data
->retval
= retval
;
1903 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
)))
1909 p
->p_listflag
&= ~P_LIST_WAITING
;
1916 #define ASSERT_LCK_MTX_OWNED(lock) \
1917 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
1919 #define ASSERT_LCK_MTX_OWNED(lock) /* nothing */
1923 waitidcontinue(int result
)
1928 struct _waitid_data
*waitid_data
;
1929 struct waitid_nocancel_args
*uap
;
1936 thread
= current_thread();
1937 uth
= (struct uthread
*)get_bsdthread_info(thread
);
1939 waitid_data
= &uth
->uu_kevent
.uu_waitid_data
;
1940 uap
= waitid_data
->args
;
1941 retval
= waitid_data
->retval
;
1942 return(waitid_nocancel(p
, uap
, retval
));
1946 * Description: Suspend the calling thread until one child of the process
1947 * containing the calling thread changes state.
1949 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1950 * uap->id pid_t or gid_t or ignored
1951 * uap->infop Address of siginfo_t struct in
1952 * user space into which to return status
1953 * uap->options flag values
1955 * Returns: 0 Success
1956 * !0 Error returning status to user space
1959 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
1961 __pthread_testcancel(1);
1962 return (waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
));
1966 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
,
1967 __unused
int32_t *retval
)
1969 user_siginfo_t siginfo
; /* siginfo data to return to caller */
1970 boolean_t caller64
= IS_64BIT_PROCESS(q
);
1975 struct _waitid_data
*waitid_data
;
1977 if (uap
->options
== 0 ||
1978 (uap
->options
& ~(WNOHANG
|WNOWAIT
|WCONTINUED
|WSTOPPED
|WEXITED
)))
1979 return (EINVAL
); /* bits set that aren't recognized */
1981 switch (uap
->idtype
) {
1982 case P_PID
: /* child with process ID equal to... */
1983 case P_PGID
: /* child with process group ID equal to... */
1984 if (((int)uap
->id
) < 0)
1987 case P_ALL
: /* any child */
1996 PCHILDREN_FOREACH(q
, p
) {
1997 switch (uap
->idtype
) {
1998 case P_PID
: /* child with process ID equal to... */
1999 if (p
->p_pid
!= (pid_t
)uap
->id
)
2002 case P_PGID
: /* child with process group ID equal to... */
2003 if (p
->p_pgrpid
!= (pid_t
)uap
->id
)
2006 case P_ALL
: /* any child */
2010 /* XXX This is racy because we don't get the lock!!!! */
2013 * Wait collision; go to sleep and restart; used to maintain
2014 * the single return for waited process guarantee.
2016 if (p
->p_listflag
& P_LIST_WAITING
) {
2017 (void) msleep(&p
->p_stat
, proc_list_mlock
,
2018 PWAIT
, "waitidcoll", 0);
2021 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
2025 bzero(&siginfo
, sizeof (siginfo
));
2027 switch (p
->p_stat
) {
2028 case SZOMB
: /* Exited */
2029 if (!(uap
->options
& WEXITED
))
2033 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
2036 siginfo
.si_signo
= SIGCHLD
;
2037 siginfo
.si_pid
= p
->p_pid
;
2038 siginfo
.si_status
= (WEXITSTATUS(p
->p_xstat
) & 0x00FFFFFF) | (((uint32_t)(p
->p_xhighbits
) << 24) & 0xFF000000);
2040 if (WIFSIGNALED(p
->p_xstat
)) {
2041 siginfo
.si_code
= WCOREDUMP(p
->p_xstat
) ?
2042 CLD_DUMPED
: CLD_KILLED
;
2044 siginfo
.si_code
= CLD_EXITED
;
2046 if ((error
= copyoutsiginfo(&siginfo
,
2047 caller64
, uap
->infop
)) != 0)
2050 /* Prevent other process for waiting for this event? */
2051 if (!(uap
->options
& WNOWAIT
)) {
2052 (void) reap_child_locked(q
, p
, 0, 0, 0, 0);
2057 case SSTOP
: /* Stopped */
2059 * If we are not interested in stopped processes, then
2062 if (!(uap
->options
& WSTOPPED
))
2066 * If someone has already waited it, we lost a race
2067 * to be the one to return status.
2069 if ((p
->p_lflag
& P_LWAITED
) != 0)
2073 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
2076 siginfo
.si_signo
= SIGCHLD
;
2077 siginfo
.si_pid
= p
->p_pid
;
2078 siginfo
.si_status
= p
->p_xstat
; /* signal number */
2079 siginfo
.si_code
= CLD_STOPPED
;
2081 if ((error
= copyoutsiginfo(&siginfo
,
2082 caller64
, uap
->infop
)) != 0)
2085 /* Prevent other process for waiting for this event? */
2086 if (!(uap
->options
& WNOWAIT
)) {
2088 p
->p_lflag
|= P_LWAITED
;
2093 default: /* All other states => Continued */
2094 if (!(uap
->options
& WCONTINUED
))
2098 * If the flag isn't set, then this process has not
2099 * been stopped and continued, or the status has
2100 * already been reaped by another caller of waitid().
2102 if ((p
->p_flag
& P_CONTINUED
) == 0)
2106 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
2109 siginfo
.si_signo
= SIGCHLD
;
2110 siginfo
.si_code
= CLD_CONTINUED
;
2112 siginfo
.si_pid
= p
->p_contproc
;
2113 siginfo
.si_status
= p
->p_xstat
;
2116 if ((error
= copyoutsiginfo(&siginfo
,
2117 caller64
, uap
->infop
)) != 0)
2120 /* Prevent other process for waiting for this event? */
2121 if (!(uap
->options
& WNOWAIT
)) {
2122 OSBitAndAtomic(~((uint32_t)P_CONTINUED
),
2127 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
2129 /* Not a process we are interested in; go on to next child */
2131 p
->p_listflag
&= ~P_LIST_WAITING
;
2134 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
2136 /* No child processes that could possibly satisfy the request? */
2143 if (uap
->options
& WNOHANG
) {
2146 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
2150 * The state of the siginfo structure in this case
2151 * is undefined. Some implementations bzero it, some
2152 * (like here) leave it untouched for efficiency.
2154 * Thus the most portable check for "no matching pid with
2155 * WNOHANG" is to store a zero into si_pid before
2156 * invocation, then check for a non-zero value afterwards.
2161 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2162 uth
= current_uthread();
2163 waitid_data
= &uth
->uu_kevent
.uu_waitid_data
;
2164 waitid_data
->args
= uap
;
2165 waitid_data
->retval
= retval
;
2167 if ((error
= msleep0(q
, proc_list_mlock
,
2168 PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)) != 0)
2174 p
->p_listflag
&= ~P_LIST_WAITING
;
2181 * make process 'parent' the new parent of process 'child'.
2184 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
)
2186 proc_t oldparent
= PROC_NULL
;
2188 if (child
->p_pptr
== parent
)
2194 oldparent
= child
->p_pptr
;
2195 #if __PROC_INTERNAL_DEBUG
2196 if (oldparent
== PROC_NULL
)
2197 panic("proc_reparent: process %p does not have a parent\n", child
);
2200 LIST_REMOVE(child
, p_sibling
);
2201 #if __PROC_INTERNAL_DEBUG
2202 if (oldparent
->p_childrencnt
== 0)
2203 panic("process children count already 0\n");
2205 oldparent
->p_childrencnt
--;
2206 #if __PROC_INTERNAL_DEBUG1
2207 if (oldparent
->p_childrencnt
< 0)
2208 panic("process children count -ve\n");
2210 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
2211 parent
->p_childrencnt
++;
2212 child
->p_pptr
= parent
;
2213 child
->p_ppid
= parent
->p_pid
;
2217 if ((cansignal
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
))
2218 psignal(initproc
, SIGCHLD
);
2224 * Exit: deallocate address space and other resources, change proc state
2225 * to zombie, and unlink proc from allproc and parent's lists. Save exit
2226 * status and rusage for wait(). Check for child processes and orphan them.
2230 vfork_exit(proc_t p
, int rv
)
2232 vfork_exit_internal(p
, rv
, 0);
2236 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
2238 thread_t self
= current_thread();
2240 struct task
*task
= p
->task
;
2245 * If a thread in this task has already
2246 * called exit(), then halt any others
2250 ut
= get_bsdthread_info(self
);
2254 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
2256 * This happens when a parent exits/killed and vfork is in progress
2257 * other threads. But shutdown code for ex has already called exit1()
2262 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
2265 if (forceexit
== 0) {
2267 * parent of a vfork child has already called exit() and the
2268 * thread that has vfork in proress terminates. So there is no
2269 * separate address space here and it has already been marked for
2270 * termination. This was never covered before and could cause problems
2271 * if we block here for outside code.
2273 /* Notify the perf server */
2274 (void)sys_perf_notify(self
, p
->p_pid
);
2278 * Remove proc from allproc queue and from pidhash chain.
2279 * Need to do this before we do anything that can block.
2280 * Not doing causes things like mount() find this on allproc
2281 * in partially cleaned state.
2286 #if CONFIG_MEMORYSTATUS
2287 memorystatus_remove(p
, TRUE
);
2290 LIST_REMOVE(p
, p_list
);
2291 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
2292 /* will not be visible via proc_find */
2293 p
->p_listflag
|= P_LIST_EXITED
;
2299 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
2300 p
->p_sigignore
= ~0;
2305 /* begin vproc_exit */
2313 struct session
*sessp
;
2314 struct rusage_superset
*rup
;
2316 /* XXX Zombie allocation may fail, in which case stats get lost */
2317 MALLOC_ZONE(rup
, struct rusage_superset
*,
2318 sizeof (*rup
), M_ZOMBIE
, M_WAITOK
);
2323 * Close open files and release open-file table.
2328 sessp
= proc_session(p
);
2329 if (SESS_LEADER(p
, sessp
)) {
2331 if (sessp
->s_ttyvp
!= NULLVP
) {
2332 struct vnode
*ttyvp
;
2335 struct vfs_context context
;
2339 * Controlling process.
2340 * Signal foreground pgrp,
2341 * drain controlling terminal
2342 * and revoke access to controlling terminal.
2344 session_lock(sessp
);
2345 tp
= SESSION_TP(sessp
);
2346 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
2347 session_unlock(sessp
);
2350 * We're going to SIGHUP the foreground process
2351 * group. It can't change from this point on
2352 * until the revoke is complete.
2353 * The process group changes under both the tty
2354 * lock and proc_list_lock but we need only one
2360 tty_pgsignal(tp
, SIGHUP
, 1);
2362 session_lock(sessp
);
2363 tp
= SESSION_TP(sessp
);
2365 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
2366 sessp
->s_flags
&= ~S_CTTYREF
;
2367 ttyvp
= sessp
->s_ttyvp
;
2368 ttyvid
= sessp
->s_ttyvid
;
2369 sessp
->s_ttyvp
= NULL
;
2370 sessp
->s_ttyvid
= 0;
2371 sessp
->s_ttyp
= TTY_NULL
;
2372 sessp
->s_ttypgrpid
= NO_PID
;
2373 session_unlock(sessp
);
2375 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
2376 if (tp
!= TTY_NULL
) {
2381 context
.vc_thread
= proc_thread(p
); /* XXX */
2382 context
.vc_ucred
= kauth_cred_proc_ref(p
);
2383 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
2386 * Release the extra usecount taken in cttyopen.
2387 * usecount should be released after VNOP_REVOKE is called.
2388 * This usecount was taken to ensure that
2389 * the VNOP_REVOKE results in a close to
2390 * the tty since cttyclose is a no-op.
2395 kauth_cred_unref(&context
.vc_ucred
);
2400 * This is cleared even if not set. This is also done in
2401 * spec_close to ensure that the flag is cleared.
2410 session_lock(sessp
);
2411 sessp
->s_leader
= NULL
;
2412 session_unlock(sessp
);
2414 session_rele(sessp
);
2420 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
2423 proc_childdrainstart(p
);
2424 while ((q
= p
->p_children
.lh_first
) != NULL
) {
2425 if (q
->p_stat
== SZOMB
) {
2427 panic("parent child linkage broken");
2428 /* check for lookups by zomb sysctl */
2429 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2430 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2432 q
->p_listflag
|= P_LIST_WAITING
;
2434 * This is a named reference and it is not granted
2435 * if the reap is already in progress. So we get
2436 * the reference here exclusively and their can be
2437 * no waiters. So there is no need for a wakeup
2438 * after we are done. AlsO the reap frees the structure
2439 * and the proc struct cannot be used for wakeups as well.
2440 * It is safe to use q here as this is system reap
2442 (void)reap_child_locked(p
, q
, 1, 0, 1, 0);
2445 * Traced processes are killed
2446 * since their existence means someone is messing up.
2448 if (q
->p_lflag
& P_LTRACED
) {
2453 opp
= proc_find(q
->p_oppid
);
2454 if (opp
!= PROC_NULL
) {
2458 proc_reparentlocked(q
, opp
, 0, 0);
2461 /* original parent exited while traced */
2463 q
->p_listflag
|= P_LIST_DEADPARENT
;
2466 proc_reparentlocked(q
, initproc
, 0, 0);
2470 q
->p_lflag
&= ~P_LTRACED
;
2472 if (q
->sigwait_thread
) {
2473 thread_t thread
= q
->sigwait_thread
;
2477 * The sigwait_thread could be stopped at a
2478 * breakpoint. Wake it up to kill.
2479 * Need to do this as it could be a thread which is not
2480 * the first thread in the task. So any attempts to kill
2481 * the process would result into a deadlock on q->sigwait.
2483 thread_resume(thread
);
2484 clear_wait(thread
, THREAD_INTERRUPTED
);
2485 threadsignal(thread
, SIGKILL
, 0, TRUE
);
2490 psignal(q
, SIGKILL
);
2493 q
->p_listflag
|= P_LIST_DEADPARENT
;
2494 proc_reparentlocked(q
, initproc
, 0, 1);
2499 proc_childdrainend(p
);
2503 * Release reference to text vnode
2507 if (tvp
!= NULLVP
) {
2512 * Save exit status and final rusage info, adding in child rusage
2513 * info and self times. If we were unable to allocate a zombie
2514 * structure, this information is lost.
2517 rup
->ru
= p
->p_stats
->p_ru
;
2518 timerclear(&rup
->ru
.ru_utime
);
2519 timerclear(&rup
->ru
.ru_stime
);
2523 mach_task_basic_info_data_t tinfo
;
2524 task_thread_times_info_data_t ttimesinfo
;
2525 int task_info_stuff
, task_ttimes_stuff
;
2526 struct timeval ut
,st
;
2528 task_info_stuff
= MACH_TASK_BASIC_INFO_COUNT
;
2529 task_info(task
, MACH_TASK_BASIC_INFO
,
2530 &tinfo
, &task_info_stuff
);
2531 p
->p_ru
->ru
.ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
2532 p
->p_ru
->ru
.ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
2533 p
->p_ru
->ru
.ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
2534 p
->p_ru
->ru
.ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
2536 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
2537 task_info(task
, TASK_THREAD_TIMES_INFO
,
2538 &ttimesinfo
, &task_ttimes_stuff
);
2540 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
2541 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
2542 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
2543 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
2544 timeradd(&ut
,&p
->p_ru
->ru
.ru_utime
,&p
->p_ru
->ru
.ru_utime
);
2545 timeradd(&st
,&p
->p_ru
->ru
.ru_stime
,&p
->p_ru
->ru
.ru_stime
);
2549 ruadd(&rup
->ru
, &p
->p_stats
->p_cru
);
2551 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
2552 rup
->ri
.ri_phys_footprint
= 0;
2553 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
2556 * Now that we have filled in the rusage info, make it
2557 * visible to an external observer via proc_pid_rusage().
2563 * Free up profiling buffers.
2566 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
2572 for (; p1
!= NULL
; p1
= pn
) {
2574 kfree(p1
, sizeof *p1
);
2579 pth_proc_hashdelete(p
);
2582 proc_free_realitimer(p
);
2585 * Other substructures are freed from wait().
2587 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
2590 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
2591 p
->p_sigacts
= NULL
;
2593 proc_limitdrop(p
, 1);
2597 * Finish up by terminating the task
2598 * and halt this thread (only if a
2599 * member of the task exiting).
2601 p
->task
= TASK_NULL
;
2604 * Notify parent that we're gone.
2606 pp
= proc_parent(p
);
2607 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
2608 if (pp
!= initproc
) {
2610 pp
->si_pid
= p
->p_pid
;
2611 pp
->p_xhighbits
= p
->p_xhighbits
;
2613 pp
->si_status
= p
->p_xstat
;
2614 pp
->si_code
= CLD_EXITED
;
2616 * p_ucred usage is safe as it is an exiting process
2617 * and reference is dropped in reap
2619 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
2622 /* mark as a zombie */
2623 /* mark as a zombie */
2624 /* No need to take proc lock as all refs are drained and
2625 * no one except parent (reaping ) can look at this.
2626 * The write is to an int and is coherent. Also parent is
2627 * keyed off of list lock for reaping
2631 psignal(pp
, SIGCHLD
);
2633 /* and now wakeup the parent */
2635 wakeup((caddr_t
)pp
);
2639 /* check for lookups by zomb sysctl */
2640 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2641 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2644 p
->p_listflag
|= P_LIST_WAITING
;
2647 * This is a named reference and it is not granted
2648 * if the reap is already in progress. So we get
2649 * the reference here exclusively and their can be
2650 * no waiters. So there is no need for a wakeup
2651 * after we are done. AlsO the reap frees the structure
2652 * and the proc struct cannot be used for wakeups as well.
2653 * It is safe to use p here as this is system reap
2655 (void)reap_child_locked(pp
, p
, 0, 0, 1, 1);
2656 /* list lock dropped by reap_child_locked */
2664 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
2665 * process. We munge the kernel version of rusage into the
2668 __private_extern__
void
2669 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
2671 /* Zero-out struct so that padding is cleared */
2672 bzero(a_user_rusage_p
, sizeof(struct user64_rusage
));
2674 /* timeval changes size, so utime and stime need special handling */
2675 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2676 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2677 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2678 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2680 * everything else can be a direct assign, since there is no loss
2681 * of precision implied boing 32->64.
2683 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2684 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2685 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2686 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2687 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2688 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2689 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2690 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2691 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2692 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2693 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2694 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2695 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2696 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
2699 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2700 __private_extern__
void
2701 munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
)
2703 /* timeval changes size, so utime and stime need special handling */
2704 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2705 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2706 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2707 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2709 * everything else can be a direct assign. We currently ignore
2710 * the loss of precision
2712 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2713 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2714 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2715 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2716 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2717 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2718 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2719 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2720 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2721 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2722 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2723 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2724 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2725 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;