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