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