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