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