Home | History | Annotate | Line # | Download | only in kern
kern_exit.c revision 1.220
      1 /*	$NetBSD: kern_exit.c,v 1.220 2009/04/25 15:06:31 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  * (c) UNIX System Laboratories, Inc.
     37  * All or some portions of this file are derived from material licensed
     38  * to the University of California by American Telephone and Telegraph
     39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  * the permission of UNIX System Laboratories, Inc.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. 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.10 (Berkeley) 2/23/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.220 2009/04/25 15:06:31 rmind Exp $");
     71 
     72 #include "opt_ktrace.h"
     73 #include "opt_perfctrs.h"
     74 #include "opt_sa.h"
     75 #include "opt_sysv.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/ioctl.h>
     80 #include <sys/tty.h>
     81 #include <sys/time.h>
     82 #include <sys/resource.h>
     83 #include <sys/kernel.h>
     84 #include <sys/proc.h>
     85 #include <sys/buf.h>
     86 #include <sys/wait.h>
     87 #include <sys/file.h>
     88 #include <sys/vnode.h>
     89 #include <sys/syslog.h>
     90 #include <sys/pool.h>
     91 #include <sys/uidinfo.h>
     92 #if defined(PERFCTRS)
     93 #include <sys/pmc.h>
     94 #endif
     95 #include <sys/ptrace.h>
     96 #include <sys/acct.h>
     97 #include <sys/filedesc.h>
     98 #include <sys/ras.h>
     99 #include <sys/signalvar.h>
    100 #include <sys/sched.h>
    101 #include <sys/sa.h>
    102 #include <sys/savar.h>
    103 #include <sys/mount.h>
    104 #include <sys/syscallargs.h>
    105 #include <sys/kauth.h>
    106 #include <sys/sleepq.h>
    107 #include <sys/lockdebug.h>
    108 #include <sys/ktrace.h>
    109 #include <sys/cpu.h>
    110 #include <sys/lwpctl.h>
    111 #include <sys/atomic.h>
    112 
    113 #include <uvm/uvm_extern.h>
    114 
    115 #ifdef DEBUG_EXIT
    116 int debug_exit = 0;
    117 #define DPRINTF(x) if (debug_exit) printf x
    118 #else
    119 #define DPRINTF(x)
    120 #endif
    121 
    122 static int find_stopped_child(struct proc *, pid_t, int, struct proc **, int *);
    123 static void proc_free(struct proc *, struct rusage *);
    124 
    125 /*
    126  * Fill in the appropriate signal information, and signal the parent.
    127  */
    128 static void
    129 exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
    130 {
    131 
    132 	KSI_INIT(ksi);
    133 	if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
    134 		if (WIFSIGNALED(p->p_xstat)) {
    135 			if (WCOREDUMP(p->p_xstat))
    136 				ksi->ksi_code = CLD_DUMPED;
    137 			else
    138 				ksi->ksi_code = CLD_KILLED;
    139 		} else {
    140 			ksi->ksi_code = CLD_EXITED;
    141 		}
    142 	}
    143 	/*
    144 	 * We fill those in, even for non-SIGCHLD.
    145 	 * It's safe to access p->p_cred unlocked here.
    146 	 */
    147 	ksi->ksi_pid = p->p_pid;
    148 	ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
    149 	ksi->ksi_status = p->p_xstat;
    150 	/* XXX: is this still valid? */
    151 	ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
    152 	ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
    153 }
    154 
    155 /*
    156  * exit --
    157  *	Death of process.
    158  */
    159 int
    160 sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval)
    161 {
    162 	/* {
    163 		syscallarg(int)	rval;
    164 	} */
    165 	struct proc *p = l->l_proc;
    166 
    167 	/* Don't call exit1() multiple times in the same process. */
    168 	mutex_enter(p->p_lock);
    169 	if (p->p_sflag & PS_WEXIT) {
    170 		mutex_exit(p->p_lock);
    171 		lwp_exit(l);
    172 	}
    173 
    174 	/* exit1() will release the mutex. */
    175 	exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
    176 	/* NOTREACHED */
    177 	return (0);
    178 }
    179 
    180 /*
    181  * Exit: deallocate address space and other resources, change proc state
    182  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
    183  * status and rusage for wait().  Check for child processes and orphan them.
    184  *
    185  * Must be called with p->p_lock held.  Does not return.
    186  */
    187 void
    188 exit1(struct lwp *l, int rv)
    189 {
    190 	struct proc	*p, *q, *nq;
    191 	struct pgrp	*pgrp;
    192 	ksiginfo_t	ksi;
    193 	ksiginfoq_t	kq;
    194 	int		wakeinit, sa;
    195 
    196 	p = l->l_proc;
    197 
    198 	KASSERT(mutex_owned(p->p_lock));
    199 
    200 	if (__predict_false(p == initproc))
    201 		panic("init died (signal %d, exit %d)",
    202 		    WTERMSIG(rv), WEXITSTATUS(rv));
    203 
    204 	/*
    205 	 * Disable scheduler activation upcalls.  We're trying to get out of
    206 	 * here.
    207 	 */
    208 	sa = 0;
    209 #ifdef KERN_SA
    210 	if ((p->p_sa != NULL)) {
    211 		l->l_pflag |= LP_SA_NOBLOCK;
    212 		sa = 1;
    213 	}
    214 #endif
    215 
    216 	p->p_sflag |= PS_WEXIT;
    217 
    218 	/*
    219 	 * Force all other LWPs to exit before we do.  Only then can we
    220 	 * begin to tear down the rest of the process state.
    221 	 */
    222 	if (sa || p->p_nlwps > 1)
    223 		exit_lwps(l);
    224 
    225 	ksiginfo_queue_init(&kq);
    226 
    227 	/*
    228 	 * If we have been asked to stop on exit, do so now.
    229 	 */
    230 	if (__predict_false(p->p_sflag & PS_STOPEXIT)) {
    231 		KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
    232 		sigclearall(p, &contsigmask, &kq);
    233 		p->p_waited = 0;
    234 		membar_producer();
    235 		p->p_stat = SSTOP;
    236 		lwp_lock(l);
    237 		p->p_nrlwps--;
    238 		l->l_stat = LSSTOP;
    239 		mutex_exit(p->p_lock);
    240 		mi_switch(l);
    241 		KERNEL_LOCK(l->l_biglocks, l);
    242 		mutex_enter(p->p_lock);
    243 	}
    244 
    245 	/*
    246 	 * Bin any remaining signals and mark the process as dying so it will
    247 	 * not be found for, e.g. signals.
    248 	 */
    249 	sigfillset(&p->p_sigctx.ps_sigignore);
    250 	sigclearall(p, NULL, &kq);
    251 	p->p_stat = SDYING;
    252 	mutex_exit(p->p_lock);
    253 	ksiginfo_queue_drain(&kq);
    254 
    255 	/* Destroy any lwpctl info. */
    256 	if (p->p_lwpctl != NULL)
    257 		lwp_ctl_exit();
    258 
    259 	/*
    260 	 * Drain all remaining references that procfs, ptrace and others may
    261 	 * have on the process.
    262 	 */
    263 	rw_enter(&p->p_reflock, RW_WRITER);
    264 
    265 	DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
    266 
    267 	timers_free(p, TIMERS_ALL);
    268 #if defined(__HAVE_RAS)
    269 	ras_purgeall();
    270 #endif
    271 
    272 	/*
    273 	 * Close open files, release open-file table and free signal
    274 	 * actions.  This may block!
    275 	 */
    276 	fd_free();
    277 	cwdfree(p->p_cwdi);
    278 	p->p_cwdi = NULL;
    279 	doexithooks(p);
    280 	sigactsfree(p->p_sigacts);
    281 
    282 	/*
    283 	 * Write out accounting data.
    284 	 */
    285 	(void)acct_process(l);
    286 
    287 #ifdef KTRACE
    288 	/*
    289 	 * Release trace file.
    290 	 */
    291 	if (p->p_tracep != NULL) {
    292 		mutex_enter(&ktrace_lock);
    293 		ktrderef(p);
    294 		mutex_exit(&ktrace_lock);
    295 	}
    296 #endif
    297 
    298 	/*
    299 	 * If emulation has process exit hook, call it now.
    300 	 * Set the exit status now so that the exit hook has
    301 	 * an opportunity to tweak it (COMPAT_LINUX requires
    302 	 * this for thread group emulation)
    303 	 */
    304 	p->p_xstat = rv;
    305 	if (p->p_emul->e_proc_exit)
    306 		(*p->p_emul->e_proc_exit)(p);
    307 
    308 	/*
    309 	 * Free the VM resources we're still holding on to.
    310 	 * We must do this from a valid thread because doing
    311 	 * so may block. This frees vmspace, which we don't
    312 	 * need anymore. The only remaining lwp is the one
    313 	 * we run at this moment, nothing runs in userland
    314 	 * anymore.
    315 	 */
    316 	uvm_proc_exit(p);
    317 
    318 	/*
    319 	 * Stop profiling.
    320 	 */
    321 	if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) {
    322 		mutex_spin_enter(&p->p_stmutex);
    323 		stopprofclock(p);
    324 		mutex_spin_exit(&p->p_stmutex);
    325 	}
    326 
    327 	/*
    328 	 * If parent is waiting for us to exit or exec, PL_PPWAIT is set; we
    329 	 * wake up the parent early to avoid deadlock.  We can do this once
    330 	 * the VM resources are released.
    331 	 */
    332 	mutex_enter(proc_lock);
    333 	if (p->p_lflag & PL_PPWAIT) {
    334 		p->p_lflag &= ~PL_PPWAIT;
    335 		cv_broadcast(&p->p_pptr->p_waitcv);
    336 	}
    337 
    338 	if (SESS_LEADER(p)) {
    339 		struct vnode *vprele = NULL, *vprevoke = NULL;
    340 		struct session *sp = p->p_session;
    341 		struct tty *tp;
    342 
    343 		if (sp->s_ttyvp) {
    344 			/*
    345 			 * Controlling process.
    346 			 * Signal foreground pgrp,
    347 			 * drain controlling terminal
    348 			 * and revoke access to controlling terminal.
    349 			 */
    350 			tp = sp->s_ttyp;
    351 			mutex_spin_enter(&tty_lock);
    352 			if (tp->t_session == sp) {
    353 				/* we can't guarantee the revoke will do this */
    354 				pgrp = tp->t_pgrp;
    355 				tp->t_pgrp = NULL;
    356 				tp->t_session = NULL;
    357 				mutex_spin_exit(&tty_lock);
    358 				if (pgrp != NULL) {
    359 					pgsignal(pgrp, SIGHUP, 1);
    360 				}
    361 				mutex_exit(proc_lock);
    362 				(void) ttywait(tp);
    363 				mutex_enter(proc_lock);
    364 
    365 				/* The tty could have been revoked. */
    366 				vprevoke = sp->s_ttyvp;
    367 			} else
    368 				mutex_spin_exit(&tty_lock);
    369 			vprele = sp->s_ttyvp;
    370 			sp->s_ttyvp = NULL;
    371 			/*
    372 			 * s_ttyp is not zero'd; we use this to indicate
    373 			 * that the session once had a controlling terminal.
    374 			 * (for logging and informational purposes)
    375 			 */
    376 		}
    377 		sp->s_leader = NULL;
    378 
    379 		if (vprevoke != NULL || vprele != NULL) {
    380 			if (vprevoke != NULL) {
    381 				/* Releases proc_lock. */
    382 				proc_sessrele(sp);
    383 				VOP_REVOKE(vprevoke, REVOKEALL);
    384 			} else
    385 				mutex_exit(proc_lock);
    386 			if (vprele != NULL)
    387 				vrele(vprele);
    388 			mutex_enter(proc_lock);
    389 		}
    390 	}
    391 	fixjobc(p, p->p_pgrp, 0);
    392 
    393 	/*
    394 	 * Finalize the last LWP's specificdata, as well as the
    395 	 * specificdata for the proc itself.
    396 	 */
    397 	lwp_finispecific(l);
    398 	proc_finispecific(p);
    399 
    400 	/*
    401 	 * Notify interested parties of our demise.
    402 	 */
    403 	KNOTE(&p->p_klist, NOTE_EXIT);
    404 
    405 #if PERFCTRS
    406 	/*
    407 	 * Save final PMC information in parent process & clean up.
    408 	 */
    409 	if (PMC_ENABLED(p)) {
    410 		pmc_save_context(p);
    411 		pmc_accumulate(p->p_pptr, p);
    412 		pmc_process_exit(p);
    413 	}
    414 #endif
    415 
    416 	/*
    417 	 * Reset p_opptr pointer of all former children which got
    418 	 * traced by another process and were reparented. We reset
    419 	 * it to NULL here; the trace detach code then reparents
    420 	 * the child to initproc. We only check allproc list, since
    421 	 * eventual former children on zombproc list won't reference
    422 	 * p_opptr anymore.
    423 	 */
    424 	if (__predict_false(p->p_slflag & PSL_CHTRACED)) {
    425 		PROCLIST_FOREACH(q, &allproc) {
    426 			if ((q->p_flag & PK_MARKER) != 0)
    427 				continue;
    428 			if (q->p_opptr == p)
    429 				q->p_opptr = NULL;
    430 		}
    431 	}
    432 
    433 	/*
    434 	 * Give orphaned children to init(8).
    435 	 */
    436 	q = LIST_FIRST(&p->p_children);
    437 	wakeinit = (q != NULL);
    438 	for (; q != NULL; q = nq) {
    439 		nq = LIST_NEXT(q, p_sibling);
    440 
    441 		/*
    442 		 * Traced processes are killed since their existence
    443 		 * means someone is screwing up. Since we reset the
    444 		 * trace flags, the logic in sys_wait4() would not be
    445 		 * triggered to reparent the process to its
    446 		 * original parent, so we must do this here.
    447 		 */
    448 		if (__predict_false(q->p_slflag & PSL_TRACED)) {
    449 			mutex_enter(p->p_lock);
    450 			q->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
    451 			mutex_exit(p->p_lock);
    452 			if (q->p_opptr != q->p_pptr) {
    453 				struct proc *t = q->p_opptr;
    454 				proc_reparent(q, t ? t : initproc);
    455 				q->p_opptr = NULL;
    456 			} else
    457 				proc_reparent(q, initproc);
    458 			killproc(q, "orphaned traced process");
    459 		} else
    460 			proc_reparent(q, initproc);
    461 	}
    462 
    463 	/*
    464 	 * Move proc from allproc to zombproc, it's now nearly ready to be
    465 	 * collected by parent.
    466 	 */
    467 	LIST_REMOVE(l, l_list);
    468 	LIST_REMOVE(p, p_list);
    469 	LIST_INSERT_HEAD(&zombproc, p, p_list);
    470 
    471 	/*
    472 	 * Mark the process as dead.  We must do this before we signal
    473 	 * the parent.
    474 	 */
    475 	p->p_stat = SDEAD;
    476 
    477 	/* Put in front of parent's sibling list for parent to collect it */
    478 	q = p->p_pptr;
    479 	q->p_nstopchild++;
    480 	if (LIST_FIRST(&q->p_children) != p) {
    481 		/* Put child where it can be found quickly */
    482 		LIST_REMOVE(p, p_sibling);
    483 		LIST_INSERT_HEAD(&q->p_children, p, p_sibling);
    484 	}
    485 
    486 	/*
    487 	 * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
    488 	 * flag set, notify init instead (and hope it will handle
    489 	 * this situation).
    490 	 */
    491 	if (q->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
    492 		proc_reparent(p, initproc);
    493 		wakeinit = 1;
    494 
    495 		/*
    496 		 * If this was the last child of our parent, notify
    497 		 * parent, so in case he was wait(2)ing, he will
    498 		 * continue.
    499 		 */
    500 		if (LIST_FIRST(&q->p_children) == NULL)
    501 			cv_broadcast(&q->p_waitcv);
    502 	}
    503 
    504 	/* Reload parent pointer, since p may have been reparented above */
    505 	q = p->p_pptr;
    506 
    507 	if (__predict_false((p->p_slflag & PSL_FSTRACE) == 0 && p->p_exitsig != 0)) {
    508 		exit_psignal(p, q, &ksi);
    509 		kpsignal(q, &ksi, NULL);
    510 	}
    511 
    512 	/* Calculate the final rusage info.  */
    513 	calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime,
    514 	    NULL, NULL);
    515 
    516 	if (wakeinit)
    517 		cv_broadcast(&initproc->p_waitcv);
    518 
    519 	callout_destroy(&l->l_timeout_ch);
    520 
    521 	/*
    522 	 * Remaining lwp resources will be freed in lwp_exit2() once we've
    523 	 * switch to idle context; at that point, we will be marked as a
    524 	 * full blown zombie.
    525 	 */
    526 	mutex_enter(p->p_lock);
    527 	lwp_drainrefs(l);
    528 	lwp_lock(l);
    529 	l->l_prflag &= ~LPR_DETACHED;
    530 	l->l_stat = LSZOMB;
    531 	lwp_unlock(l);
    532 	KASSERT(curlwp == l);
    533 	KASSERT(p->p_nrlwps == 1);
    534 	KASSERT(p->p_nlwps == 1);
    535 	p->p_stat = SZOMB;
    536 	p->p_nrlwps--;
    537 	p->p_nzlwps++;
    538 	p->p_ndlwps = 0;
    539 	mutex_exit(p->p_lock);
    540 
    541 	/*
    542 	 * Signal the parent to collect us, and drop the proclist lock.
    543 	 * Drop debugger/procfs lock; no new references can be gained.
    544 	 */
    545 	cv_broadcast(&p->p_pptr->p_waitcv);
    546 	mutex_exit(proc_lock);
    547 	rw_exit(&p->p_reflock);
    548 
    549 	/* Verify that we hold no locks other than the kernel lock. */
    550 	LOCKDEBUG_BARRIER(&kernel_lock, 0);
    551 
    552 	/*
    553 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
    554 	 */
    555 
    556 	/*
    557 	 * Give machine-dependent code a chance to free any MD LWP
    558 	 * resources.  This must be done before uvm_lwp_exit(), in
    559 	 * case these resources are in the PCB.
    560 	 */
    561 #ifndef __NO_CPU_LWP_FREE
    562 	cpu_lwp_free(l, 1);
    563 #endif
    564 	pmap_deactivate(l);
    565 
    566 	/* This process no longer needs to hold the kernel lock. */
    567 #ifdef notyet
    568 	/* XXXSMP hold in lwp_userret() */
    569 	KERNEL_UNLOCK_LAST(l);
    570 #else
    571 	KERNEL_UNLOCK_ALL(l, NULL);
    572 #endif
    573 
    574 	lwp_exit_switchaway(l);
    575 }
    576 
    577 void
    578 exit_lwps(struct lwp *l)
    579 {
    580 	struct proc *p;
    581 	struct lwp *l2;
    582 	int error;
    583 	lwpid_t waited;
    584 	int nlocks;
    585 
    586 	KERNEL_UNLOCK_ALL(l, &nlocks);
    587 
    588 	p = l->l_proc;
    589 	KASSERT(mutex_owned(p->p_lock));
    590 
    591 #ifdef KERN_SA
    592 	if (p->p_sa != NULL) {
    593 		struct sadata_vp *vp;
    594 		SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
    595 			/*
    596 			 * Make SA-cached LWPs normal process interruptable
    597 			 * so that the exit code can wake them. Locking
    598 			 * savp_mutex locks all the lwps on this vp that
    599 			 * we need to adjust.
    600 			 */
    601 			mutex_enter(&vp->savp_mutex);
    602 			DPRINTF(("exit_lwps: Making cached LWPs of %d on "
    603 			    "VP %d interruptable: ", p->p_pid, vp->savp_id));
    604 			TAILQ_FOREACH(l2, &vp->savp_lwpcache, l_sleepchain) {
    605 				l2->l_flag |= LW_SINTR;
    606 				DPRINTF(("%d ", l2->l_lid));
    607 			}
    608 			DPRINTF(("\n"));
    609 
    610 			DPRINTF(("exit_lwps: Making unblocking LWPs of %d on "
    611 			    "VP %d interruptable: ", p->p_pid, vp->savp_id));
    612 			TAILQ_FOREACH(l2, &vp->savp_woken, l_sleepchain) {
    613 				vp->savp_woken_count--;
    614 				l2->l_flag |= LW_SINTR;
    615 				DPRINTF(("%d ", l2->l_lid));
    616 			}
    617 			DPRINTF(("\n"));
    618 			mutex_exit(&vp->savp_mutex);
    619 		}
    620 	}
    621 #endif
    622 
    623  retry:
    624 	/*
    625 	 * Interrupt LWPs in interruptable sleep, unsuspend suspended
    626 	 * LWPs and then wait for everyone else to finish.
    627 	 */
    628 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    629 		if (l2 == l)
    630 			continue;
    631 		lwp_lock(l2);
    632 		l2->l_flag &= ~LW_SA;
    633 		l2->l_flag |= LW_WEXIT;
    634 		if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
    635 		    l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
    636 		    	/* setrunnable() will release the lock. */
    637 			setrunnable(l2);
    638 			DPRINTF(("exit_lwps: Made %d.%d runnable\n",
    639 			    p->p_pid, l2->l_lid));
    640 			continue;
    641 		}
    642 		lwp_unlock(l2);
    643 	}
    644 	while (p->p_nlwps > 1) {
    645 		DPRINTF(("exit_lwps: waiting for %d LWPs (%d zombies)\n",
    646 		    p->p_nlwps, p->p_nzlwps));
    647 		error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
    648 		if (p->p_nlwps == 1)
    649 			break;
    650 		if (error == EDEADLK) {
    651 			/*
    652 			 * LWPs can get suspended/slept behind us.
    653 			 * (eg. sa_setwoken)
    654 			 * kick them again and retry.
    655 			 */
    656 			goto retry;
    657 		}
    658 		if (error)
    659 			panic("exit_lwps: lwp_wait1 failed with error %d",
    660 			    error);
    661 		DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
    662 	}
    663 
    664 	KERNEL_LOCK(nlocks, l);
    665 	KASSERT(p->p_nlwps == 1);
    666 }
    667 
    668 int
    669 do_sys_wait(struct lwp *l, int *pid, int *status, int options,
    670     struct rusage *ru, int *was_zombie)
    671 {
    672 	struct proc	*child;
    673 	int		error;
    674 
    675 	mutex_enter(proc_lock);
    676 	error = find_stopped_child(l->l_proc, *pid, options, &child, status);
    677 
    678 	if (child == NULL) {
    679 		mutex_exit(proc_lock);
    680 		*pid = 0;
    681 		return error;
    682 	}
    683 
    684 	*pid = child->p_pid;
    685 
    686 	if (child->p_stat == SZOMB) {
    687 		/* proc_free() will release the proc_lock. */
    688 		*was_zombie = 1;
    689 		if (options & WNOWAIT)
    690 			mutex_exit(proc_lock);
    691 		else {
    692 			proc_free(child, ru);
    693 		}
    694 	} else {
    695 		/* Child state must have been SSTOP. */
    696 		*was_zombie = 0;
    697 		mutex_exit(proc_lock);
    698 		*status = W_STOPCODE(*status);
    699 	}
    700 
    701 	return 0;
    702 }
    703 
    704 int
    705 sys___wait450(struct lwp *l, const struct sys___wait450_args *uap, register_t *retval)
    706 {
    707 	/* {
    708 		syscallarg(int)			pid;
    709 		syscallarg(int *)		status;
    710 		syscallarg(int)			options;
    711 		syscallarg(struct rusage *)	rusage;
    712 	} */
    713 	int		status, error;
    714 	int		was_zombie;
    715 	struct rusage	ru;
    716 	int pid = SCARG(uap, pid);
    717 
    718 	error = do_sys_wait(l, &pid, &status, SCARG(uap, options),
    719 	    SCARG(uap, rusage) != NULL ? &ru : NULL, &was_zombie);
    720 
    721 	retval[0] = pid;
    722 	if (pid == 0)
    723 		return error;
    724 
    725 	if (SCARG(uap, rusage))
    726 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
    727 
    728 	if (error == 0 && SCARG(uap, status))
    729 		error = copyout(&status, SCARG(uap, status), sizeof(status));
    730 
    731 	return error;
    732 }
    733 
    734 /*
    735  * Scan list of child processes for a child process that has stopped or
    736  * exited.  Used by sys_wait4 and 'compat' equivalents.
    737  *
    738  * Must be called with the proc_lock held, and may release while waiting.
    739  */
    740 static int
    741 find_stopped_child(struct proc *parent, pid_t pid, int options,
    742 		   struct proc **child_p, int *status_p)
    743 {
    744 	struct proc *child, *dead;
    745 	int error;
    746 
    747 	KASSERT(mutex_owned(proc_lock));
    748 
    749 	if (options & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG)
    750 	    && !(options & WOPTSCHECKED)) {
    751 		*child_p = NULL;
    752 		return EINVAL;
    753 	}
    754 
    755 	if (pid == 0 && !(options & WOPTSCHECKED))
    756 		pid = -parent->p_pgid;
    757 
    758 	for (;;) {
    759 		error = ECHILD;
    760 		dead = NULL;
    761 
    762 		LIST_FOREACH(child, &parent->p_children, p_sibling) {
    763 			if (pid >= 0) {
    764 				if (child->p_pid != pid) {
    765 					child = p_find(pid, PFIND_ZOMBIE |
    766 					    PFIND_LOCKED);
    767 					if (child == NULL ||
    768 					    child->p_pptr != parent) {
    769 						child = NULL;
    770 						break;
    771 					}
    772 				}
    773 			} else if (pid != WAIT_ANY && child->p_pgid != -pid) {
    774 				/* Child not in correct pgrp */
    775 				continue;
    776 			}
    777 
    778 			/*
    779 			 * Wait for processes with p_exitsig != SIGCHLD
    780 			 * processes only if WALTSIG is set; wait for
    781 			 * processes with p_exitsig == SIGCHLD only
    782 			 * if WALTSIG is clear.
    783 			 */
    784 			if (((options & WALLSIG) == 0) &&
    785 			    (options & WALTSIG ? child->p_exitsig == SIGCHLD
    786 						: P_EXITSIG(child) != SIGCHLD)){
    787 				if (child->p_pid == pid) {
    788 					child = NULL;
    789 					break;
    790 				}
    791 				continue;
    792 			}
    793 
    794 			error = 0;
    795 			if ((options & WNOZOMBIE) == 0) {
    796 				if (child->p_stat == SZOMB)
    797 					break;
    798 				if (child->p_stat == SDEAD) {
    799 					/*
    800 					 * We may occasionally arrive here
    801 					 * after receiving a signal, but
    802 					 * immediatley before the child
    803 					 * process is zombified.  The wait
    804 					 * will be short, so avoid returning
    805 					 * to userspace.
    806 					 */
    807 					dead = child;
    808 				}
    809 			}
    810 
    811 			if (child->p_stat == SSTOP &&
    812 			    child->p_waited == 0 &&
    813 			    (child->p_slflag & PSL_TRACED ||
    814 			    options & WUNTRACED)) {
    815 				if ((options & WNOWAIT) == 0) {
    816 					child->p_waited = 1;
    817 					parent->p_nstopchild--;
    818 				}
    819 				break;
    820 			}
    821 			if (parent->p_nstopchild == 0 || child->p_pid == pid) {
    822 				child = NULL;
    823 				break;
    824 			}
    825 		}
    826 
    827 		if (child != NULL || error != 0 ||
    828 		    ((options & WNOHANG) != 0 && dead == NULL)) {
    829 		    	if (child != NULL) {
    830 			    	*status_p = child->p_xstat;
    831 			}
    832 			*child_p = child;
    833 			return error;
    834 		}
    835 
    836 		/*
    837 		 * Wait for another child process to stop.
    838 		 */
    839 		error = cv_wait_sig(&parent->p_waitcv, proc_lock);
    840 
    841 		if (error != 0) {
    842 			*child_p = NULL;
    843 			return error;
    844 		}
    845 	}
    846 }
    847 
    848 /*
    849  * Free a process after parent has taken all the state info.  Must be called
    850  * with the proclist lock held, and will release before returning.
    851  *
    852  * *ru is returned to the caller, and must be freed by the caller.
    853  */
    854 static void
    855 proc_free(struct proc *p, struct rusage *ru)
    856 {
    857 	struct proc *parent = p->p_pptr;
    858 	struct lwp *l;
    859 	ksiginfo_t ksi;
    860 	kauth_cred_t cred1, cred2;
    861 	uid_t uid;
    862 
    863 	KASSERT(mutex_owned(proc_lock));
    864 	KASSERT(p->p_nlwps == 1);
    865 	KASSERT(p->p_nzlwps == 1);
    866 	KASSERT(p->p_nrlwps == 0);
    867 	KASSERT(p->p_stat == SZOMB);
    868 
    869 	/*
    870 	 * If we got the child via ptrace(2) or procfs, and
    871 	 * the parent is different (meaning the process was
    872 	 * attached, rather than run as a child), then we need
    873 	 * to give it back to the old parent, and send the
    874 	 * parent the exit signal.  The rest of the cleanup
    875 	 * will be done when the old parent waits on the child.
    876 	 */
    877 	if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) {
    878 		mutex_enter(p->p_lock);
    879 		p->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
    880 		mutex_exit(p->p_lock);
    881 		parent = (p->p_opptr == NULL) ? initproc : p->p_opptr;
    882 		proc_reparent(p, parent);
    883 		p->p_opptr = NULL;
    884 		if (p->p_exitsig != 0) {
    885 			exit_psignal(p, parent, &ksi);
    886 			kpsignal(parent, &ksi, NULL);
    887 		}
    888 		cv_broadcast(&parent->p_waitcv);
    889 		mutex_exit(proc_lock);
    890 		return;
    891 	}
    892 
    893 	sched_proc_exit(parent, p);
    894 
    895 	/*
    896 	 * Add child times of exiting process onto its own times.
    897 	 * This cannot be done any earlier else it might get done twice.
    898 	 */
    899 	l = LIST_FIRST(&p->p_lwps);
    900 	p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
    901 	p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw;
    902 	ruadd(&p->p_stats->p_ru, &l->l_ru);
    903 	ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru);
    904 	ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
    905 	if (ru != NULL)
    906 		*ru = p->p_stats->p_ru;
    907 	p->p_xstat = 0;
    908 
    909 	/* Release any SA state. */
    910 #ifdef KERN_SA
    911 	if (p->p_sa)
    912 		sa_release(p);
    913 #endif
    914 
    915 	/*
    916 	 * At this point we are going to start freeing the final resources.
    917 	 * If anyone tries to access the proc structure after here they will
    918 	 * get a shock - bits are missing.  Attempt to make it hard!  We
    919 	 * don't bother with any further locking past this point.
    920 	 */
    921 	p->p_stat = SIDL;		/* not even a zombie any more */
    922 	LIST_REMOVE(p, p_list);	/* off zombproc */
    923 	parent->p_nstopchild--;
    924 	LIST_REMOVE(p, p_sibling);
    925 
    926 	/*
    927 	 * Let pid be reallocated.
    928 	 */
    929 	proc_free_pid(p);
    930 
    931 	/*
    932 	 * Unlink process from its process group.
    933 	 * Releases the proc_lock.
    934 	 */
    935 	proc_leavepgrp(p);
    936 
    937 	/*
    938 	 * Delay release until after lwp_free.
    939 	 */
    940 	cred2 = l->l_cred;
    941 
    942 	/*
    943 	 * Free the last LWP's resources.
    944 	 *
    945 	 * lwp_free ensures the LWP is no longer running on another CPU.
    946 	 */
    947 	lwp_free(l, false, true);
    948 
    949 	/*
    950 	 * Now no one except us can reach the process p.
    951 	 */
    952 
    953 	/*
    954 	 * Decrement the count of procs running with this uid.
    955 	 */
    956 	cred1 = p->p_cred;
    957 	uid = kauth_cred_getuid(cred1);
    958 	(void)chgproccnt(uid, -1);
    959 
    960 	/*
    961 	 * Release substructures.
    962 	 */
    963 
    964 	limfree(p->p_limit);
    965 	pstatsfree(p->p_stats);
    966 	kauth_cred_free(cred1);
    967 	kauth_cred_free(cred2);
    968 
    969 	/*
    970 	 * Release reference to text vnode
    971 	 */
    972 	if (p->p_textvp)
    973 		vrele(p->p_textvp);
    974 
    975 	mutex_destroy(&p->p_auxlock);
    976 	mutex_obj_free(p->p_lock);
    977 	mutex_destroy(&p->p_stmutex);
    978 	cv_destroy(&p->p_waitcv);
    979 	cv_destroy(&p->p_lwpcv);
    980 	rw_destroy(&p->p_reflock);
    981 
    982 	proc_free_mem(p);
    983 }
    984 
    985 /*
    986  * make process 'parent' the new parent of process 'child'.
    987  *
    988  * Must be called with proc_lock held.
    989  */
    990 void
    991 proc_reparent(struct proc *child, struct proc *parent)
    992 {
    993 
    994 	KASSERT(mutex_owned(proc_lock));
    995 
    996 	if (child->p_pptr == parent)
    997 		return;
    998 
    999 	if (child->p_stat == SZOMB ||
   1000 	    (child->p_stat == SSTOP && !child->p_waited)) {
   1001 		child->p_pptr->p_nstopchild--;
   1002 		parent->p_nstopchild++;
   1003 	}
   1004 	if (parent == initproc)
   1005 		child->p_exitsig = SIGCHLD;
   1006 
   1007 	LIST_REMOVE(child, p_sibling);
   1008 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
   1009 	child->p_pptr = parent;
   1010 	child->p_ppid = parent->p_pid;
   1011 }
   1012