Home | History | Annotate | Line # | Download | only in kern
kern_exit.c revision 1.127
      1  1.127       dsl /*	$NetBSD: kern_exit.c,v 1.127 2003/11/06 09:30:13 dsl Exp $	*/
      2   1.56   thorpej 
      3   1.56   thorpej /*-
      4   1.72   thorpej  * Copyright (c) 1998, 1999 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.58  christos  * NASA Ames Research Center.
     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.56   thorpej  * 3. All advertising materials mentioning features or use of this software
     20   1.56   thorpej  *    must display the following acknowledgement:
     21   1.58  christos  *	This product includes software developed by the NetBSD
     22   1.58  christos  *	Foundation, Inc. and its contributors.
     23   1.56   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.56   thorpej  *    contributors may be used to endorse or promote products derived
     25   1.56   thorpej  *    from this software without specific prior written permission.
     26   1.58  christos  *
     27   1.56   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.56   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.56   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.56   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.56   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.56   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.56   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.56   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.56   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.56   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.56   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38   1.56   thorpej  */
     39   1.24       cgd 
     40   1.24       cgd /*
     41   1.24       cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     42   1.24       cgd  *	The Regents of the University of California.  All rights reserved.
     43   1.24       cgd  * (c) UNIX System Laboratories, Inc.
     44   1.24       cgd  * All or some portions of this file are derived from material licensed
     45   1.24       cgd  * to the University of California by American Telephone and Telegraph
     46   1.24       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     47   1.24       cgd  * the permission of UNIX System Laboratories, Inc.
     48   1.24       cgd  *
     49   1.24       cgd  * Redistribution and use in source and binary forms, with or without
     50   1.24       cgd  * modification, are permitted provided that the following conditions
     51   1.24       cgd  * are met:
     52   1.24       cgd  * 1. Redistributions of source code must retain the above copyright
     53   1.24       cgd  *    notice, this list of conditions and the following disclaimer.
     54   1.24       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     55   1.24       cgd  *    notice, this list of conditions and the following disclaimer in the
     56   1.24       cgd  *    documentation and/or other materials provided with the distribution.
     57  1.119       agc  * 3. Neither the name of the University nor the names of its contributors
     58   1.24       cgd  *    may be used to endorse or promote products derived from this software
     59   1.24       cgd  *    without specific prior written permission.
     60   1.24       cgd  *
     61   1.24       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62   1.24       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63   1.24       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64   1.24       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65   1.24       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66   1.24       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67   1.24       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68   1.24       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69   1.24       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70   1.24       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71   1.24       cgd  * SUCH DAMAGE.
     72   1.24       cgd  *
     73   1.49      fvdl  *	@(#)kern_exit.c	8.10 (Berkeley) 2/23/95
     74   1.24       cgd  */
     75   1.92     lukem 
     76   1.92     lukem #include <sys/cdefs.h>
     77  1.127       dsl __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.127 2003/11/06 09:30:13 dsl Exp $");
     78   1.48       mrg 
     79   1.51   thorpej #include "opt_ktrace.h"
     80   1.97    briggs #include "opt_perfctrs.h"
     81   1.94  christos #include "opt_systrace.h"
     82   1.60      tron #include "opt_sysv.h"
     83   1.24       cgd 
     84   1.24       cgd #include <sys/param.h>
     85   1.24       cgd #include <sys/systm.h>
     86   1.24       cgd #include <sys/ioctl.h>
     87   1.24       cgd #include <sys/proc.h>
     88   1.24       cgd #include <sys/tty.h>
     89   1.24       cgd #include <sys/time.h>
     90   1.24       cgd #include <sys/resource.h>
     91   1.24       cgd #include <sys/kernel.h>
     92   1.50  christos #include <sys/ktrace.h>
     93   1.24       cgd #include <sys/proc.h>
     94   1.24       cgd #include <sys/buf.h>
     95   1.24       cgd #include <sys/wait.h>
     96   1.24       cgd #include <sys/file.h>
     97   1.24       cgd #include <sys/vnode.h>
     98   1.24       cgd #include <sys/syslog.h>
     99   1.24       cgd #include <sys/malloc.h>
    100   1.53   thorpej #include <sys/pool.h>
    101   1.24       cgd #include <sys/resourcevar.h>
    102   1.98    briggs #if defined(PERFCTRS)
    103   1.97    briggs #include <sys/pmc.h>
    104   1.98    briggs #endif
    105   1.24       cgd #include <sys/ptrace.h>
    106   1.29       cgd #include <sys/acct.h>
    107   1.36  christos #include <sys/filedesc.h>
    108  1.100  gmcgarry #include <sys/ras.h>
    109   1.36  christos #include <sys/signalvar.h>
    110   1.64      ross #include <sys/sched.h>
    111  1.107   thorpej #include <sys/sa.h>
    112  1.107   thorpej #include <sys/savar.h>
    113   1.26       cgd #include <sys/mount.h>
    114   1.26       cgd #include <sys/syscallargs.h>
    115   1.94  christos #include <sys/systrace.h>
    116   1.26       cgd 
    117   1.24       cgd #include <machine/cpu.h>
    118   1.24       cgd 
    119   1.47       mrg #include <uvm/uvm_extern.h>
    120   1.47       mrg 
    121  1.107   thorpej #define DEBUG_EXIT
    122  1.107   thorpej 
    123  1.107   thorpej #ifdef DEBUG_EXIT
    124  1.107   thorpej int debug_exit = 0;
    125  1.107   thorpej #define DPRINTF(x) if (debug_exit) printf x
    126  1.107   thorpej #else
    127  1.107   thorpej #define DPRINTF(x)
    128  1.107   thorpej #endif
    129  1.107   thorpej 
    130  1.107   thorpej static void lwp_exit_hook(struct lwp *, void *);
    131  1.123  christos static void exit_psignal(struct proc *, struct proc *);
    132  1.123  christos 
    133  1.123  christos /*
    134  1.123  christos  * Fill in the appropriate signal information, and kill the parent.
    135  1.123  christos  */
    136  1.123  christos static void
    137  1.123  christos exit_psignal(struct proc *p, struct proc *pp)
    138  1.123  christos {
    139  1.123  christos 	ksiginfo_t ksi;
    140  1.123  christos 
    141  1.123  christos 	(void)memset(&ksi, 0, sizeof(ksi));
    142  1.123  christos 	if ((ksi.ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
    143  1.123  christos 		if (WIFSIGNALED(p->p_xstat)) {
    144  1.123  christos 			if (WCOREDUMP(p->p_xstat))
    145  1.123  christos 				ksi.ksi_code = CLD_DUMPED;
    146  1.123  christos 			else
    147  1.123  christos 				ksi.ksi_code = CLD_KILLED;
    148  1.123  christos 		} else {
    149  1.123  christos 			ksi.ksi_code = CLD_EXITED;
    150  1.123  christos 		}
    151  1.123  christos 	}
    152  1.123  christos 	/*
    153  1.123  christos 	 * we fill those in, even for non-SIGCHLD.
    154  1.123  christos 	 */
    155  1.123  christos 	ksi.ksi_pid = p->p_pid;
    156  1.123  christos 	ksi.ksi_uid = p->p_ucred->cr_uid;
    157  1.123  christos 	ksi.ksi_status = p->p_xstat;
    158  1.123  christos 	/* XXX: is this still valid? */
    159  1.123  christos 	ksi.ksi_utime = p->p_ru->ru_utime.tv_sec;
    160  1.123  christos 	ksi.ksi_stime = p->p_ru->ru_stime.tv_sec;
    161  1.123  christos 	kpsignal(pp, &ksi, NULL);
    162  1.123  christos }
    163   1.94  christos 
    164   1.24       cgd /*
    165   1.24       cgd  * exit --
    166   1.24       cgd  *	Death of process.
    167   1.24       cgd  */
    168   1.31   thorpej int
    169  1.107   thorpej sys_exit(struct lwp *l, void *v, register_t *retval)
    170   1.31   thorpej {
    171   1.33   mycroft 	struct sys_exit_args /* {
    172   1.89     lukem 		syscallarg(int)	rval;
    173   1.31   thorpej 	} */ *uap = v;
    174   1.24       cgd 
    175  1.107   thorpej 	/* Don't call exit1() multiple times in the same process.*/
    176  1.107   thorpej 	if (l->l_proc->p_flag & P_WEXIT)
    177  1.107   thorpej 		lwp_exit(l);
    178  1.107   thorpej 
    179  1.107   thorpej 	exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
    180   1.24       cgd 	/* NOTREACHED */
    181   1.31   thorpej 	return (0);
    182   1.24       cgd }
    183   1.24       cgd 
    184   1.24       cgd /*
    185   1.24       cgd  * Exit: deallocate address space and other resources, change proc state
    186   1.24       cgd  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
    187   1.24       cgd  * status and rusage for wait().  Check for child processes and orphan them.
    188   1.24       cgd  */
    189   1.31   thorpej void
    190  1.107   thorpej exit1(struct lwp *l, int rv)
    191   1.24       cgd {
    192  1.107   thorpej 	struct proc	*p, *q, *nq;
    193  1.108   nathanw 	int		s, sa;
    194   1.24       cgd 
    195  1.107   thorpej 	p = l->l_proc;
    196  1.107   thorpej 
    197   1.78   thorpej 	if (__predict_false(p == initproc))
    198   1.24       cgd 		panic("init died (signal %d, exit %d)",
    199   1.24       cgd 		    WTERMSIG(rv), WEXITSTATUS(rv));
    200   1.73   thorpej 
    201  1.112   nathanw 	p->p_flag |= P_WEXIT;
    202  1.112   nathanw 
    203  1.107   thorpej 	DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
    204  1.107   thorpej 	/*
    205  1.107   thorpej 	 * Disable scheduler activation upcalls.
    206  1.107   thorpej 	 * We're trying to get out of here.
    207  1.107   thorpej 	 */
    208  1.108   nathanw 	sa = 0;
    209  1.110   nathanw 	if (p->p_sa != NULL) {
    210  1.107   thorpej 		l->l_flag &= ~L_SA;
    211  1.118      fvdl #if 0
    212  1.107   thorpej 		p->p_flag &= ~P_SA;
    213  1.118      fvdl #endif
    214  1.108   nathanw 		sa = 1;
    215  1.107   thorpej 	}
    216  1.107   thorpej 
    217   1.24       cgd #ifdef PGINPROF
    218   1.24       cgd 	vmsizmon();
    219   1.24       cgd #endif
    220   1.24       cgd 	if (p->p_flag & P_PROFIL)
    221   1.24       cgd 		stopprofclock(p);
    222   1.55   thorpej 	p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
    223   1.24       cgd 	/*
    224   1.37   mycroft 	 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
    225   1.37   mycroft 	 * wake up the parent early to avoid deadlock.
    226   1.24       cgd 	 */
    227   1.37   mycroft 	if (p->p_flag & P_PPWAIT) {
    228   1.37   mycroft 		p->p_flag &= ~P_PPWAIT;
    229  1.126       dsl 		wakeup(p->p_pptr);
    230   1.37   mycroft 	}
    231   1.87  jdolecek 	sigfillset(&p->p_sigctx.ps_sigignore);
    232   1.87  jdolecek 	sigemptyset(&p->p_sigctx.ps_siglist);
    233   1.87  jdolecek 	p->p_sigctx.ps_sigcheck = 0;
    234  1.107   thorpej 	timers_free(p, TIMERS_ALL);
    235  1.107   thorpej 
    236  1.108   nathanw 	if (sa || (p->p_nlwps > 1))
    237  1.107   thorpej 		exit_lwps(l);
    238  1.100  gmcgarry 
    239  1.100  gmcgarry #if defined(__HAVE_RAS)
    240  1.100  gmcgarry 	ras_purgeall(p);
    241  1.100  gmcgarry #endif
    242   1.24       cgd 
    243   1.24       cgd 	/*
    244   1.24       cgd 	 * Close open files and release open-file table.
    245   1.24       cgd 	 * This may block!
    246   1.24       cgd 	 */
    247  1.117      fvdl 	fdfree(p);
    248   1.66   thorpej 	cwdfree(p);
    249   1.24       cgd 
    250   1.93  christos 	doexithooks(p);
    251   1.93  christos 
    252   1.24       cgd 	if (SESS_LEADER(p)) {
    253   1.76  augustss 		struct session *sp = p->p_session;
    254  1.127       dsl 		struct tty *tp;
    255   1.24       cgd 
    256   1.24       cgd 		if (sp->s_ttyvp) {
    257   1.24       cgd 			/*
    258   1.24       cgd 			 * Controlling process.
    259   1.24       cgd 			 * Signal foreground pgrp,
    260   1.24       cgd 			 * drain controlling terminal
    261   1.24       cgd 			 * and revoke access to controlling terminal.
    262   1.24       cgd 			 */
    263  1.127       dsl 			tp = sp->s_ttyp;
    264  1.127       dsl 			TTY_LOCK(tp);
    265  1.127       dsl 			if (tp->t_session == sp) {
    266  1.127       dsl 				if (tp->t_pgrp)
    267  1.127       dsl 					pgsignal(tp->t_pgrp, SIGHUP, 1);
    268  1.127       dsl 				/* we can't guarantee the revoke will do this */
    269  1.127       dsl 				tp->t_pgrp = NULL;
    270  1.127       dsl 				tp->t_session = NULL;
    271  1.127       dsl 				TTY_UNLOCK(tp);
    272  1.127       dsl 				SESSRELE(sp);
    273  1.127       dsl 				(void) ttywait(tp);
    274   1.24       cgd 				/*
    275   1.24       cgd 				 * The tty could have been revoked
    276   1.24       cgd 				 * if we blocked.
    277   1.24       cgd 				 */
    278   1.24       cgd 				if (sp->s_ttyvp)
    279   1.49      fvdl 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
    280  1.127       dsl 			} else
    281  1.127       dsl 				TTY_UNLOCK(tp);
    282   1.24       cgd 			if (sp->s_ttyvp)
    283   1.24       cgd 				vrele(sp->s_ttyvp);
    284   1.24       cgd 			sp->s_ttyvp = NULL;
    285   1.24       cgd 			/*
    286   1.24       cgd 			 * s_ttyp is not zero'd; we use this to indicate
    287   1.24       cgd 			 * that the session once had a controlling terminal.
    288   1.24       cgd 			 * (for logging and informational purposes)
    289   1.24       cgd 			 */
    290   1.24       cgd 		}
    291   1.24       cgd 		sp->s_leader = NULL;
    292   1.24       cgd 	}
    293   1.24       cgd 	fixjobc(p, p->p_pgrp, 0);
    294  1.117      fvdl 	(void)acct_process(p);
    295   1.24       cgd #ifdef KTRACE
    296   1.24       cgd 	/*
    297   1.24       cgd 	 * release trace file
    298   1.24       cgd 	 */
    299   1.50  christos 	ktrderef(p);
    300   1.94  christos #endif
    301   1.94  christos #ifdef SYSTRACE
    302   1.94  christos 	systrace_sys_exit(p);
    303   1.24       cgd #endif
    304   1.24       cgd 	/*
    305   1.99      manu 	 * If emulation has process exit hook, call it now.
    306   1.99      manu 	 */
    307   1.99      manu 	if (p->p_emul->e_proc_exit)
    308   1.99      manu 		(*p->p_emul->e_proc_exit)(p);
    309   1.99      manu 
    310   1.99      manu 	/*
    311   1.72   thorpej 	 * Give orphaned children to init(8).
    312   1.72   thorpej 	 */
    313  1.101      matt 	q = LIST_FIRST(&p->p_children);
    314   1.25   mycroft 	if (q)		/* only need this if any child is S_ZOMB */
    315  1.126       dsl 		wakeup(initproc);
    316   1.25   mycroft 	for (; q != 0; q = nq) {
    317  1.101      matt 		nq = LIST_NEXT(q, p_sibling);
    318  1.104  jdolecek 
    319   1.24       cgd 		/*
    320  1.104  jdolecek 		 * Traced processes are killed since their existence
    321  1.104  jdolecek 		 * means someone is screwing up. Since we reset the
    322  1.104  jdolecek 		 * trace flags, the logic in sys_wait4() would not be
    323  1.104  jdolecek 		 * triggered to reparent the process to its
    324  1.106  jdolecek 		 * original parent, so we must do this here.
    325   1.24       cgd 		 */
    326   1.24       cgd 		if (q->p_flag & P_TRACED) {
    327  1.104  jdolecek 			if (q->p_opptr != q->p_pptr) {
    328  1.104  jdolecek 				struct proc *t = q->p_opptr;
    329  1.104  jdolecek 				proc_reparent(q, t ? t : initproc);
    330  1.104  jdolecek 				q->p_opptr = NULL;
    331  1.105  jdolecek 			} else
    332  1.105  jdolecek 				proc_reparent(q, initproc);
    333   1.45   mycroft 			q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    334   1.24       cgd 			psignal(q, SIGKILL);
    335  1.104  jdolecek 		} else {
    336  1.104  jdolecek 			proc_reparent(q, initproc);
    337   1.24       cgd 		}
    338   1.24       cgd 	}
    339   1.24       cgd 
    340   1.24       cgd 	/*
    341   1.95  jdolecek 	 * Reset p_opptr pointer of all former children which got
    342   1.95  jdolecek 	 * traced by another process and were reparented. We reset
    343   1.95  jdolecek 	 * it to NULL here; the trace detach code then reparents
    344   1.95  jdolecek 	 * the child to initproc. We only check allproc list, since
    345   1.95  jdolecek 	 * eventual former children on zombproc list won't reference
    346   1.95  jdolecek 	 * p_opptr anymore.
    347   1.95  jdolecek 	 */
    348   1.95  jdolecek 	if (p->p_flag & P_CHTRACED) {
    349   1.95  jdolecek 		struct proc *t;
    350   1.95  jdolecek 
    351   1.95  jdolecek 		proclist_lock_read();
    352   1.95  jdolecek 
    353   1.95  jdolecek 		LIST_FOREACH(t, &allproc, p_list) {
    354   1.95  jdolecek 			if (t->p_opptr == p)
    355   1.95  jdolecek 				t->p_opptr = NULL;
    356   1.95  jdolecek 		}
    357   1.95  jdolecek 
    358   1.95  jdolecek 		proclist_unlock_read();
    359   1.95  jdolecek 	}
    360  1.103  jdolecek 
    361  1.103  jdolecek 	/*
    362  1.115       dsl 	 * Save exit status and final rusage info, adding in child rusage
    363  1.115       dsl 	 * info and self times.
    364  1.115       dsl 	 * In order to pick up the time for the current execution, we must
    365  1.115       dsl 	 * do this before unlinking the lwp from l_list.
    366  1.115       dsl 	 */
    367  1.115       dsl 	p->p_xstat = rv;
    368  1.115       dsl 	*p->p_ru = p->p_stats->p_ru;
    369  1.115       dsl 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
    370  1.115       dsl 	ruadd(p->p_ru, &p->p_stats->p_cru);
    371  1.115       dsl 
    372  1.115       dsl 	/*
    373  1.115       dsl 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
    374  1.115       dsl 	 */
    375  1.115       dsl 
    376  1.115       dsl 	/*
    377  1.115       dsl 	 * Move proc from allproc to zombproc, but do not yet
    378  1.115       dsl 	 * wake up the reaper.  We will put the proc on the
    379  1.115       dsl 	 * deadproc list later (using the p_dead member), and
    380  1.115       dsl 	 * wake up the reaper when we do.
    381  1.115       dsl 	 * Changing the state to SDEAD stops it being found by pfind().
    382  1.115       dsl 	 */
    383  1.115       dsl 	s = proclist_lock_write();
    384  1.115       dsl 	p->p_stat = SDEAD;
    385  1.115       dsl 	p->p_nrlwps--;
    386  1.121      yamt 	l->l_stat = LSDEAD;
    387  1.115       dsl 	LIST_REMOVE(p, p_list);
    388  1.115       dsl 	LIST_INSERT_HEAD(&zombproc, p, p_list);
    389  1.115       dsl 	LIST_REMOVE(l, l_list);
    390  1.115       dsl 	l->l_flag |= L_DETACHED;
    391  1.115       dsl 	proclist_unlock_write(s);
    392  1.115       dsl 
    393  1.115       dsl 	/*
    394  1.103  jdolecek 	 * Notify interested parties of our demise.
    395  1.103  jdolecek 	 */
    396  1.103  jdolecek 	KNOTE(&p->p_klist, NOTE_EXIT);
    397   1.97    briggs 
    398   1.97    briggs #if PERFCTRS
    399   1.97    briggs 	/*
    400   1.97    briggs 	 * Save final PMC information in parent process & clean up.
    401   1.97    briggs 	 */
    402   1.97    briggs 	if (PMC_ENABLED(p)) {
    403   1.97    briggs 		pmc_save_context(p);
    404   1.97    briggs 		pmc_accumulate(p->p_pptr, p);
    405   1.97    briggs 		pmc_process_exit(p);
    406   1.97    briggs 	}
    407   1.97    briggs #endif
    408   1.59  christos 
    409   1.59  christos 	/*
    410   1.59  christos 	 * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
    411   1.59  christos 	 * flag set, notify init instead (and hope it will handle
    412   1.59  christos 	 * this situation).
    413   1.59  christos 	 */
    414   1.59  christos 	if (p->p_pptr->p_flag & P_NOCLDWAIT) {
    415   1.59  christos 		struct proc *pp = p->p_pptr;
    416   1.59  christos 		proc_reparent(p, initproc);
    417   1.59  christos 		/*
    418   1.59  christos 		 * If this was the last child of our parent, notify
    419   1.59  christos 		 * parent, so in case he was wait(2)ing, he will
    420   1.59  christos 		 * continue.
    421   1.59  christos 		 */
    422  1.101      matt 		if (LIST_FIRST(&pp->p_children) == NULL)
    423  1.126       dsl 			wakeup(pp);
    424   1.59  christos 	}
    425   1.67   thorpej 
    426   1.67   thorpej 	/*
    427   1.67   thorpej 	 * Release the process's signal state.
    428   1.67   thorpej 	 */
    429   1.67   thorpej 	sigactsfree(p);
    430   1.96      manu 
    431   1.96      manu 	/*
    432  1.107   thorpej 	 * Clear curlwp after we've done all operations
    433   1.24       cgd 	 * that could block, and before tearing down the rest
    434   1.24       cgd 	 * of the process state that might be used from clock, etc.
    435  1.107   thorpej 	 * Also, can't clear curlwp while we're still runnable,
    436   1.24       cgd 	 * as we're not on a run queue (we are current, just not
    437   1.24       cgd 	 * a proper proc any longer!).
    438   1.24       cgd 	 *
    439   1.24       cgd 	 * Other substructures are freed from wait().
    440   1.24       cgd 	 */
    441  1.107   thorpej 	curlwp = NULL;
    442   1.74    bouyer 	limfree(p->p_limit);
    443  1.107   thorpej 	pstatsfree(p->p_stats);
    444   1.77   thorpej 	p->p_limit = NULL;
    445   1.24       cgd 
    446   1.85   thorpej 	/* This process no longer needs to hold the kernel lock. */
    447  1.107   thorpej 	KERNEL_PROC_UNLOCK(l);
    448   1.85   thorpej 
    449   1.24       cgd 	/*
    450   1.56   thorpej 	 * Finally, call machine-dependent code to switch to a new
    451   1.56   thorpej 	 * context (possibly the idle context).  Once we are no longer
    452   1.56   thorpej 	 * using the dead process's vmspace and stack, exit2() will be
    453   1.56   thorpej 	 * called to schedule those resources to be released by the
    454   1.56   thorpej 	 * reaper thread.
    455   1.56   thorpej 	 *
    456   1.56   thorpej 	 * Note that cpu_exit() will end with a call equivalent to
    457   1.56   thorpej 	 * cpu_switch(), finishing our execution (pun intended).
    458   1.24       cgd 	 */
    459  1.107   thorpej 
    460  1.107   thorpej 	cpu_exit(l, 1);
    461  1.107   thorpej }
    462  1.107   thorpej 
    463  1.107   thorpej void
    464  1.107   thorpej exit_lwps(struct lwp *l)
    465  1.107   thorpej {
    466  1.107   thorpej 	struct proc *p;
    467  1.107   thorpej 	struct lwp *l2;
    468  1.107   thorpej 	int s, error;
    469  1.107   thorpej 	lwpid_t		waited;
    470  1.107   thorpej 
    471  1.107   thorpej 	p = l->l_proc;
    472  1.107   thorpej 
    473  1.107   thorpej 	/* XXX SMP
    474  1.107   thorpej 	 * This would be the right place to IPI any LWPs running on
    475  1.107   thorpej 	 * other processors so that they can notice the userret exit hook.
    476  1.107   thorpej 	 */
    477  1.107   thorpej 	p->p_userret = lwp_exit_hook;
    478  1.107   thorpej 	p->p_userret_arg = NULL;
    479  1.107   thorpej 
    480  1.107   thorpej 	/*
    481  1.107   thorpej 	 * Make SA-cached LWPs normal process runnable LWPs so that
    482  1.107   thorpej 	 * they'll also self-destruct.
    483  1.107   thorpej 	 */
    484  1.107   thorpej 	if (p->p_sa && p->p_sa->sa_ncached > 0) {
    485  1.107   thorpej 		DPRINTF(("exit_lwps: Making cached LWPs of %d runnable: ",
    486  1.107   thorpej 		    p->p_pid));
    487  1.107   thorpej 		SCHED_LOCK(s);
    488  1.107   thorpej 		while ((l2 = sa_getcachelwp(p)) != 0) {
    489  1.107   thorpej 			l2->l_priority = l2->l_usrpri;
    490  1.107   thorpej 			setrunnable(l2);
    491  1.107   thorpej 			DPRINTF(("%d ", l2->l_lid));
    492  1.107   thorpej 		}
    493  1.107   thorpej 		DPRINTF(("\n"));
    494  1.107   thorpej 		SCHED_UNLOCK(s);
    495  1.107   thorpej 	}
    496  1.107   thorpej 
    497  1.107   thorpej 	/*
    498  1.107   thorpej 	 * Interrupt LWPs in interruptable sleep, unsuspend suspended
    499  1.111     skrll 	 * LWPs, make detached LWPs undetached (so we can wait for
    500  1.107   thorpej 	 * them) and then wait for everyone else to finish.
    501  1.107   thorpej 	 */
    502  1.107   thorpej 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    503  1.107   thorpej 		l2->l_flag &= ~(L_DETACHED|L_SA);
    504  1.118      fvdl 
    505  1.124        cl 		if (l2->l_wchan == &l2->l_upcallstack)
    506  1.124        cl 			wakeup(&l2->l_upcallstack);
    507  1.118      fvdl 
    508  1.107   thorpej 		if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) ||
    509  1.118      fvdl 		    l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
    510  1.107   thorpej 			SCHED_LOCK(s);
    511  1.107   thorpej 			setrunnable(l2);
    512  1.107   thorpej 			SCHED_UNLOCK(s);
    513  1.107   thorpej 			DPRINTF(("exit_lwps: Made %d.%d runnable\n",
    514  1.107   thorpej 			    p->p_pid, l2->l_lid));
    515  1.107   thorpej 		}
    516  1.107   thorpej 	}
    517  1.107   thorpej 
    518  1.107   thorpej 
    519  1.107   thorpej 	while (p->p_nlwps > 1) {
    520  1.107   thorpej 		DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n",
    521  1.107   thorpej 		    p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
    522  1.107   thorpej 		error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
    523  1.107   thorpej 		if (error)
    524  1.107   thorpej 			panic("exit_lwps: lwp_wait1 failed with error %d\n",
    525  1.107   thorpej 			    error);
    526  1.107   thorpej 		DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
    527  1.107   thorpej 	}
    528  1.107   thorpej 
    529  1.107   thorpej 	p->p_userret = NULL;
    530  1.107   thorpej }
    531  1.107   thorpej 
    532  1.107   thorpej /* Wrapper function for use in p_userret */
    533  1.107   thorpej static void
    534  1.107   thorpej lwp_exit_hook(struct lwp *l, void *arg)
    535  1.107   thorpej {
    536  1.107   thorpej 	KERNEL_PROC_LOCK(l);
    537  1.107   thorpej 	lwp_exit(l);
    538   1.24       cgd }
    539   1.24       cgd 
    540   1.56   thorpej /*
    541   1.56   thorpej  * We are called from cpu_exit() once it is safe to schedule the
    542   1.85   thorpej  * dead process's resources to be freed (i.e., once we've switched to
    543   1.85   thorpej  * the idle PCB for the current CPU).
    544   1.72   thorpej  *
    545   1.72   thorpej  * NOTE: One must be careful with locking in this routine.  It's
    546   1.72   thorpej  * called from a critical section in machine-dependent code, so
    547   1.72   thorpej  * we should refrain from changing any interrupt state.
    548   1.72   thorpej  *
    549   1.72   thorpej  * We lock the deadproc list (a spin lock), place the proc on that
    550  1.115       dsl  * list (using the p_dead member), and wake up the reaper.
    551   1.56   thorpej  */
    552   1.56   thorpej void
    553  1.107   thorpej exit2(struct lwp *l)
    554   1.56   thorpej {
    555  1.107   thorpej 	struct proc *p = l->l_proc;
    556   1.56   thorpej 
    557   1.72   thorpej 	simple_lock(&deadproc_slock);
    558  1.115       dsl 	SLIST_INSERT_HEAD(&deadprocs, p, p_dead);
    559   1.72   thorpej 	simple_unlock(&deadproc_slock);
    560   1.72   thorpej 
    561  1.107   thorpej 	/* lwp_exit2() will wake up deadproc for us. */
    562  1.107   thorpej 	lwp_exit2(l);
    563   1.56   thorpej }
    564   1.56   thorpej 
    565   1.56   thorpej /*
    566   1.56   thorpej  * Process reaper.  This is run by a kernel thread to free the resources
    567   1.56   thorpej  * of a dead process.  Once the resources are free, the process becomes
    568   1.56   thorpej  * a zombie, and the parent is allowed to read the undead's status.
    569   1.56   thorpej  */
    570   1.56   thorpej void
    571   1.84   thorpej reaper(void *arg)
    572   1.56   thorpej {
    573   1.56   thorpej 	struct proc *p;
    574  1.107   thorpej 	struct lwp *l;
    575   1.56   thorpej 
    576  1.107   thorpej 	KERNEL_PROC_UNLOCK(curlwp);
    577   1.90      fvdl 
    578   1.56   thorpej 	for (;;) {
    579   1.72   thorpej 		simple_lock(&deadproc_slock);
    580  1.115       dsl 		p = SLIST_FIRST(&deadprocs);
    581  1.107   thorpej 		l = LIST_FIRST(&deadlwp);
    582  1.107   thorpej 		if (p == NULL && l == NULL) {
    583   1.56   thorpej 			/* No work for us; go to sleep until someone exits. */
    584  1.115       dsl 			(void) ltsleep(&deadprocs, PVM|PNORELOCK,
    585   1.83   thorpej 			    "reaper", 0, &deadproc_slock);
    586   1.56   thorpej 			continue;
    587   1.56   thorpej 		}
    588   1.56   thorpej 
    589  1.107   thorpej 		if (l != NULL ) {
    590  1.107   thorpej 			p = l->l_proc;
    591   1.56   thorpej 
    592  1.115       dsl 			/* Remove lwp from the deadlwp list. */
    593  1.107   thorpej 			LIST_REMOVE(l, l_list);
    594  1.107   thorpej 			simple_unlock(&deadproc_slock);
    595  1.107   thorpej 			KERNEL_PROC_LOCK(curlwp);
    596  1.107   thorpej 
    597  1.107   thorpej 			/*
    598  1.107   thorpej 			 * Give machine-dependent code a chance to free any
    599  1.107   thorpej 			 * resources it couldn't free while still running on
    600  1.107   thorpej 			 * that process's context.  This must be done before
    601  1.107   thorpej 			 * uvm_lwp_exit(), in case these resources are in the
    602  1.107   thorpej 			 * PCB.
    603  1.107   thorpej 			 */
    604  1.107   thorpej 			cpu_wait(l);
    605   1.71   thorpej 
    606  1.107   thorpej 			/*
    607  1.107   thorpej 			 * Free the VM resources we're still holding on to.
    608  1.107   thorpej 			 */
    609  1.107   thorpej 			uvm_lwp_exit(l);
    610   1.56   thorpej 
    611  1.107   thorpej 			l->l_stat = LSZOMB;
    612  1.107   thorpej 			if (l->l_flag & L_DETACHED) {
    613  1.107   thorpej 				/* Nobody waits for detached LWPs. */
    614  1.107   thorpej 				LIST_REMOVE(l, l_sibling);
    615  1.107   thorpej 				p->p_nlwps--;
    616  1.107   thorpej 				pool_put(&lwp_pool, l);
    617  1.107   thorpej 			} else {
    618  1.107   thorpej 				p->p_nzlwps++;
    619  1.126       dsl 				wakeup(&p->p_nlwps);
    620  1.107   thorpej 			}
    621  1.107   thorpej 			/* XXXNJW where should this be with respect to
    622  1.107   thorpej 			 * the wakeup() above? */
    623  1.107   thorpej 			KERNEL_PROC_UNLOCK(curlwp);
    624  1.107   thorpej 		} else {
    625  1.115       dsl 			/* Remove proc from the deadproc list. */
    626  1.115       dsl 			SLIST_REMOVE_HEAD(&deadprocs, p_dead);
    627  1.107   thorpej 			simple_unlock(&deadproc_slock);
    628  1.107   thorpej 			KERNEL_PROC_LOCK(curlwp);
    629   1.56   thorpej 
    630  1.107   thorpej 			/*
    631  1.107   thorpej 			 * Free the VM resources we're still holding on to.
    632  1.107   thorpej 			 * We must do this from a valid thread because doing
    633  1.107   thorpej 			 * so may block.
    634  1.107   thorpej 			 */
    635  1.107   thorpej 			uvm_proc_exit(p);
    636  1.107   thorpej 
    637  1.107   thorpej 			/* Process is now a true zombie. */
    638  1.107   thorpej 			p->p_stat = SZOMB;
    639  1.107   thorpej 
    640  1.107   thorpej 			/* Wake up the parent so it can get exit status. */
    641  1.107   thorpej 			if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0)
    642  1.123  christos 				exit_psignal(p, p->p_pptr);
    643  1.107   thorpej 			KERNEL_PROC_UNLOCK(curlwp);
    644  1.126       dsl 			wakeup(p->p_pptr);
    645  1.107   thorpej 		}
    646   1.56   thorpej 	}
    647   1.56   thorpej }
    648   1.56   thorpej 
    649   1.24       cgd int
    650  1.107   thorpej sys_wait4(struct lwp *l, void *v, register_t *retval)
    651   1.31   thorpej {
    652   1.76  augustss 	struct sys_wait4_args /* {
    653   1.89     lukem 		syscallarg(int)			pid;
    654   1.89     lukem 		syscallarg(int *)		status;
    655   1.89     lukem 		syscallarg(int)			options;
    656   1.89     lukem 		syscallarg(struct rusage *)	rusage;
    657   1.31   thorpej 	} */ *uap = v;
    658  1.109       dsl 	struct proc	*child, *parent;
    659  1.109       dsl 	int		status, error;
    660   1.24       cgd 
    661  1.109       dsl 	parent = l->l_proc;
    662  1.107   thorpej 
    663   1.26       cgd 	if (SCARG(uap, pid) == 0)
    664  1.109       dsl 		SCARG(uap, pid) = -parent->p_pgid;
    665  1.109       dsl 	if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
    666   1.24       cgd 		return (EINVAL);
    667   1.41     mikel 
    668  1.109       dsl 	error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
    669  1.109       dsl 				&child);
    670  1.109       dsl 	if (error != 0)
    671  1.109       dsl 		return error;
    672  1.109       dsl 	if (child == NULL) {
    673  1.109       dsl 		*retval = 0;
    674  1.109       dsl 		return 0;
    675  1.109       dsl 	}
    676  1.109       dsl 
    677  1.109       dsl 	retval[0] = child->p_pid;
    678  1.109       dsl 
    679  1.109       dsl 	if (child->p_stat == SZOMB) {
    680  1.109       dsl 		if (SCARG(uap, status)) {
    681  1.109       dsl 			status = child->p_xstat;	/* convert to int */
    682  1.126       dsl 			error = copyout(&status, SCARG(uap, status),
    683  1.109       dsl 					sizeof(status));
    684  1.109       dsl 			if (error)
    685  1.109       dsl 				return (error);
    686  1.109       dsl 		}
    687  1.109       dsl 		if (SCARG(uap, rusage)) {
    688  1.126       dsl 			error = copyout(child->p_ru, SCARG(uap, rusage),
    689  1.109       dsl 				    sizeof(struct rusage));
    690  1.109       dsl 			if (error)
    691  1.109       dsl 				return (error);
    692  1.109       dsl 		}
    693  1.109       dsl 
    694  1.109       dsl 		proc_free(child);
    695  1.109       dsl 		return 0;
    696  1.109       dsl 	}
    697  1.109       dsl 
    698  1.109       dsl 	/* child state must be SSTOP */
    699  1.109       dsl 	if (SCARG(uap, status)) {
    700  1.109       dsl 		status = W_STOPCODE(child->p_xstat);
    701  1.126       dsl 		return copyout(&status, SCARG(uap, status), sizeof (status));
    702  1.109       dsl 	}
    703  1.109       dsl 	return 0;
    704  1.109       dsl }
    705  1.109       dsl 
    706  1.109       dsl /*
    707  1.109       dsl  * Scan list of child processes for a child process that has stopped or
    708  1.109       dsl  * exited.  Used by sys_wait4 and 'compat' equivalents.
    709  1.109       dsl  */
    710  1.109       dsl int
    711  1.109       dsl find_stopped_child(struct proc *parent, pid_t pid, int options,
    712  1.109       dsl 	struct proc **child_p)
    713  1.109       dsl {
    714  1.109       dsl 	struct proc *child;
    715  1.109       dsl 	int c_found, error;
    716   1.68   thorpej 
    717  1.120      yamt 	for (;;) {
    718  1.109       dsl 		c_found = 0;
    719  1.109       dsl 		LIST_FOREACH(child, &parent->p_children, p_sibling) {
    720  1.109       dsl 			if (pid != WAIT_ANY &&
    721  1.109       dsl 			    child->p_pid != pid &&
    722  1.109       dsl 			    child->p_pgid != -pid)
    723  1.109       dsl 				continue;
    724  1.109       dsl 			/*
    725  1.109       dsl 			 * Wait for processes with p_exitsig != SIGCHLD
    726  1.109       dsl 			 * processes only if WALTSIG is set; wait for
    727  1.109       dsl 			 * processes with p_exitsig == SIGCHLD only
    728  1.109       dsl 			 * if WALTSIG is clear.
    729  1.109       dsl 			 */
    730  1.109       dsl 			if (((options & WALLSIG) == 0) &&
    731  1.109       dsl 			    (options & WALTSIG ? child->p_exitsig == SIGCHLD
    732  1.109       dsl 						: P_EXITSIG(child) != SIGCHLD))
    733  1.109       dsl 				continue;
    734  1.109       dsl 
    735  1.109       dsl 			c_found = 1;
    736  1.109       dsl 			if (child->p_stat == SZOMB &&
    737  1.109       dsl 			    (options & WNOZOMBIE) == 0) {
    738  1.109       dsl 				*child_p = child;
    739  1.109       dsl 				return 0;
    740   1.24       cgd 			}
    741  1.109       dsl 
    742  1.109       dsl 			if (child->p_stat == SSTOP &&
    743  1.109       dsl 			    (child->p_flag & P_WAITED) == 0 &&
    744  1.109       dsl 			    (child->p_flag & P_TRACED || options & WUNTRACED)) {
    745  1.109       dsl 				if ((options & WNOWAIT) == 0)
    746  1.109       dsl 					child->p_flag |= P_WAITED;
    747  1.109       dsl 				*child_p = child;
    748  1.109       dsl 				return 0;
    749   1.24       cgd 			}
    750  1.109       dsl 		}
    751  1.109       dsl 		if (c_found == 0)
    752  1.109       dsl 			return ECHILD;
    753  1.109       dsl 		if (options & WNOHANG) {
    754  1.109       dsl 			*child_p = NULL;
    755  1.109       dsl 			return 0;
    756  1.109       dsl 		}
    757  1.126       dsl 		error = tsleep(parent, PWAIT | PCATCH, "wait", 0);
    758  1.109       dsl 		if (error != 0)
    759  1.109       dsl 			return error;
    760  1.109       dsl 	}
    761  1.109       dsl }
    762  1.109       dsl 
    763  1.109       dsl /*
    764  1.109       dsl  * Free a process after parent has taken all the state info.
    765  1.109       dsl  */
    766  1.109       dsl void
    767  1.109       dsl proc_free(struct proc *p)
    768  1.109       dsl {
    769  1.109       dsl 	struct proc *parent = p->p_pptr;
    770  1.109       dsl 	int s;
    771   1.24       cgd 
    772  1.109       dsl 	/*
    773  1.109       dsl 	 * If we got the child via ptrace(2) or procfs, and
    774  1.109       dsl 	 * the parent is different (meaning the process was
    775  1.109       dsl 	 * attached, rather than run as a child), then we need
    776  1.109       dsl 	 * to give it back to the old parent, and send the
    777  1.109       dsl 	 * parent the exit signal.  The rest of the cleanup
    778  1.109       dsl 	 * will be done when the old parent waits on the child.
    779  1.109       dsl 	 */
    780  1.109       dsl 	if ((p->p_flag & P_TRACED) && p->p_opptr != parent){
    781  1.109       dsl 		parent = p->p_opptr;
    782  1.109       dsl 		if (parent == NULL)
    783  1.109       dsl 			parent = initproc;
    784  1.109       dsl 		proc_reparent(p, parent);
    785  1.109       dsl 		p->p_opptr = NULL;
    786  1.109       dsl 		p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    787  1.109       dsl 		if (p->p_exitsig != 0)
    788  1.123  christos 			exit_psignal(p, parent);
    789  1.126       dsl 		wakeup(parent);
    790  1.109       dsl 		return;
    791  1.109       dsl 	}
    792  1.109       dsl 
    793  1.109       dsl 	scheduler_wait_hook(parent, p);
    794  1.109       dsl 	p->p_xstat = 0;
    795  1.115       dsl 
    796  1.109       dsl 	ruadd(&parent->p_stats->p_cru, p->p_ru);
    797  1.115       dsl 
    798  1.115       dsl 	/*
    799  1.115       dsl 	 * At this point we are going to start freeing the final resources.
    800  1.115       dsl 	 * If anyone tries to access the proc structure after here they
    801  1.115       dsl 	 * will get a shock - bits are missing.
    802  1.115       dsl 	 * Attempt to make it hard!
    803  1.115       dsl 	 */
    804  1.115       dsl 
    805  1.115       dsl 	p->p_stat = SIDL;		/* not even a zombie any more */
    806  1.115       dsl 
    807  1.109       dsl 	pool_put(&rusage_pool, p->p_ru);
    808   1.56   thorpej 
    809  1.109       dsl 	/*
    810  1.109       dsl 	 * Finally finished with old proc entry.
    811  1.109       dsl 	 * Unlink it from its process group and free it.
    812  1.109       dsl 	 */
    813  1.109       dsl 	leavepgrp(p);
    814   1.56   thorpej 
    815  1.109       dsl 	s = proclist_lock_write();
    816  1.109       dsl 	LIST_REMOVE(p, p_list);	/* off zombproc */
    817  1.115       dsl 	LIST_REMOVE(p, p_sibling);
    818  1.109       dsl 	proclist_unlock_write(s);
    819   1.35   mycroft 
    820  1.109       dsl 	/*
    821  1.109       dsl 	 * Decrement the count of procs running with this uid.
    822  1.109       dsl 	 */
    823  1.109       dsl 	(void)chgproccnt(p->p_cred->p_ruid, -1);
    824   1.24       cgd 
    825  1.109       dsl 	/*
    826  1.109       dsl 	 * Free up credentials.
    827  1.109       dsl 	 */
    828  1.109       dsl 	if (--p->p_cred->p_refcnt == 0) {
    829  1.109       dsl 		crfree(p->p_cred->pc_ucred);
    830  1.109       dsl 		pool_put(&pcred_pool, p->p_cred);
    831  1.109       dsl 	}
    832  1.107   thorpej 
    833  1.109       dsl 	/*
    834  1.109       dsl 	 * Release reference to text vnode
    835  1.109       dsl 	 */
    836  1.109       dsl 	if (p->p_textvp)
    837  1.109       dsl 		vrele(p->p_textvp);
    838   1.24       cgd 
    839  1.109       dsl 	/*
    840  1.109       dsl 	 * Release any SA state
    841  1.109       dsl 	 */
    842  1.109       dsl 	if (p->p_sa) {
    843  1.109       dsl 		free(p->p_sa->sa_stacks, M_SA);
    844  1.109       dsl 		pool_put(&sadata_pool, p->p_sa);
    845   1.24       cgd 	}
    846  1.109       dsl 
    847  1.115       dsl 	/* Free proc structure and let pid be reallocated */
    848  1.115       dsl 	proc_free_mem(p);
    849   1.24       cgd }
    850   1.24       cgd 
    851   1.24       cgd /*
    852   1.24       cgd  * make process 'parent' the new parent of process 'child'.
    853   1.24       cgd  */
    854   1.24       cgd void
    855   1.82   thorpej proc_reparent(struct proc *child, struct proc *parent)
    856   1.24       cgd {
    857   1.24       cgd 
    858   1.24       cgd 	if (child->p_pptr == parent)
    859   1.24       cgd 		return;
    860   1.70   thorpej 
    861   1.70   thorpej 	if (parent == initproc)
    862   1.70   thorpej 		child->p_exitsig = SIGCHLD;
    863   1.24       cgd 
    864   1.25   mycroft 	LIST_REMOVE(child, p_sibling);
    865   1.25   mycroft 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
    866   1.24       cgd 	child->p_pptr = parent;
    867   1.24       cgd }
    868