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