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