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