Home | History | Annotate | Line # | Download | only in kern
kern_exit.c revision 1.60
      1  1.60      tron /*	$NetBSD: kern_exit.c,v 1.60 1998/10/19 22:19:26 tron Exp $	*/
      2  1.56   thorpej 
      3  1.56   thorpej /*-
      4  1.56   thorpej  * Copyright (c) 1998 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.48       mrg 
     80  1.51   thorpej #include "opt_ktrace.h"
     81  1.48       mrg #include "opt_uvm.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/map.h>
     87  1.24       cgd #include <sys/ioctl.h>
     88  1.24       cgd #include <sys/proc.h>
     89  1.24       cgd #include <sys/tty.h>
     90  1.24       cgd #include <sys/time.h>
     91  1.24       cgd #include <sys/resource.h>
     92  1.24       cgd #include <sys/kernel.h>
     93  1.50  christos #include <sys/ktrace.h>
     94  1.24       cgd #include <sys/proc.h>
     95  1.24       cgd #include <sys/buf.h>
     96  1.24       cgd #include <sys/wait.h>
     97  1.24       cgd #include <sys/file.h>
     98  1.24       cgd #include <sys/vnode.h>
     99  1.24       cgd #include <sys/syslog.h>
    100  1.24       cgd #include <sys/malloc.h>
    101  1.53   thorpej #include <sys/pool.h>
    102  1.24       cgd #include <sys/resourcevar.h>
    103  1.24       cgd #include <sys/ptrace.h>
    104  1.29       cgd #include <sys/acct.h>
    105  1.36  christos #include <sys/filedesc.h>
    106  1.36  christos #include <sys/signalvar.h>
    107  1.38  christos #ifdef SYSVSHM
    108  1.38  christos #include <sys/shm.h>
    109  1.38  christos #endif
    110  1.38  christos #ifdef SYSVSEM
    111  1.38  christos #include <sys/sem.h>
    112  1.38  christos #endif
    113  1.24       cgd 
    114  1.26       cgd #include <sys/mount.h>
    115  1.26       cgd #include <sys/syscallargs.h>
    116  1.26       cgd 
    117  1.24       cgd #include <machine/cpu.h>
    118  1.24       cgd 
    119  1.24       cgd #include <vm/vm.h>
    120  1.24       cgd #include <vm/vm_kern.h>
    121  1.24       cgd 
    122  1.47       mrg #if defined(UVM)
    123  1.47       mrg #include <uvm/uvm_extern.h>
    124  1.47       mrg #endif
    125  1.47       mrg 
    126  1.24       cgd /*
    127  1.24       cgd  * exit --
    128  1.24       cgd  *	Death of process.
    129  1.24       cgd  */
    130  1.31   thorpej int
    131  1.33   mycroft sys_exit(p, v, retval)
    132  1.24       cgd 	struct proc *p;
    133  1.31   thorpej 	void *v;
    134  1.32   thorpej 	register_t *retval;
    135  1.31   thorpej {
    136  1.33   mycroft 	struct sys_exit_args /* {
    137  1.26       cgd 		syscallarg(int) rval;
    138  1.31   thorpej 	} */ *uap = v;
    139  1.24       cgd 
    140  1.26       cgd 	exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
    141  1.24       cgd 	/* NOTREACHED */
    142  1.31   thorpej 	return (0);
    143  1.24       cgd }
    144  1.24       cgd 
    145  1.24       cgd /*
    146  1.24       cgd  * Exit: deallocate address space and other resources, change proc state
    147  1.24       cgd  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
    148  1.24       cgd  * status and rusage for wait().  Check for child processes and orphan them.
    149  1.24       cgd  */
    150  1.31   thorpej void
    151  1.24       cgd exit1(p, rv)
    152  1.24       cgd 	register struct proc *p;
    153  1.24       cgd 	int rv;
    154  1.24       cgd {
    155  1.24       cgd 	register struct proc *q, *nq;
    156  1.24       cgd 	register struct vmspace *vm;
    157  1.24       cgd 
    158  1.24       cgd 	if (p->p_pid == 1)
    159  1.24       cgd 		panic("init died (signal %d, exit %d)",
    160  1.24       cgd 		    WTERMSIG(rv), WEXITSTATUS(rv));
    161  1.24       cgd #ifdef PGINPROF
    162  1.24       cgd 	vmsizmon();
    163  1.24       cgd #endif
    164  1.24       cgd 	if (p->p_flag & P_PROFIL)
    165  1.24       cgd 		stopprofclock(p);
    166  1.55   thorpej 	p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
    167  1.24       cgd 	/*
    168  1.37   mycroft 	 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
    169  1.37   mycroft 	 * wake up the parent early to avoid deadlock.
    170  1.24       cgd 	 */
    171  1.24       cgd 	p->p_flag |= P_WEXIT;
    172  1.37   mycroft 	if (p->p_flag & P_PPWAIT) {
    173  1.37   mycroft 		p->p_flag &= ~P_PPWAIT;
    174  1.37   mycroft 		wakeup((caddr_t)p->p_pptr);
    175  1.37   mycroft 	}
    176  1.57   mycroft 	sigfillset(&p->p_sigignore);
    177  1.57   mycroft 	sigemptyset(&p->p_siglist);
    178  1.57   mycroft 	p->p_sigcheck = 0;
    179  1.24       cgd 	untimeout(realitexpire, (caddr_t)p);
    180  1.24       cgd 
    181  1.24       cgd 	/*
    182  1.24       cgd 	 * Close open files and release open-file table.
    183  1.24       cgd 	 * This may block!
    184  1.24       cgd 	 */
    185  1.24       cgd 	fdfree(p);
    186  1.24       cgd 
    187  1.24       cgd 	/* The next three chunks should probably be moved to vmspace_exit. */
    188  1.24       cgd 	vm = p->p_vmspace;
    189  1.24       cgd #ifdef SYSVSHM
    190  1.46   thorpej 	if (vm->vm_shm && vm->vm_refcnt == 1)
    191  1.46   thorpej 		shmexit(vm);
    192  1.24       cgd #endif
    193  1.24       cgd #ifdef SYSVSEM
    194  1.24       cgd 	semexit(p);
    195  1.24       cgd #endif
    196  1.24       cgd 	/*
    197  1.24       cgd 	 * Release user portion of address space.
    198  1.24       cgd 	 * This releases references to vnodes,
    199  1.24       cgd 	 * which could cause I/O if the file has been unlinked.
    200  1.24       cgd 	 * Need to do this early enough that we can still sleep.
    201  1.24       cgd 	 * Can't free the entire vmspace as the kernel stack
    202  1.24       cgd 	 * may be mapped within that space also.
    203  1.24       cgd 	 */
    204  1.47       mrg #if defined(UVM)
    205  1.47       mrg 	if (vm->vm_refcnt == 1)
    206  1.47       mrg 		(void) uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    207  1.47       mrg 		    VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    208  1.47       mrg #else
    209  1.24       cgd 	if (vm->vm_refcnt == 1)
    210  1.24       cgd 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
    211  1.24       cgd 		    VM_MAXUSER_ADDRESS);
    212  1.47       mrg #endif
    213  1.24       cgd 
    214  1.24       cgd 	if (SESS_LEADER(p)) {
    215  1.24       cgd 		register struct session *sp = p->p_session;
    216  1.24       cgd 
    217  1.24       cgd 		if (sp->s_ttyvp) {
    218  1.24       cgd 			/*
    219  1.24       cgd 			 * Controlling process.
    220  1.24       cgd 			 * Signal foreground pgrp,
    221  1.24       cgd 			 * drain controlling terminal
    222  1.24       cgd 			 * and revoke access to controlling terminal.
    223  1.24       cgd 			 */
    224  1.24       cgd 			if (sp->s_ttyp->t_session == sp) {
    225  1.24       cgd 				if (sp->s_ttyp->t_pgrp)
    226  1.24       cgd 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    227  1.24       cgd 				(void) ttywait(sp->s_ttyp);
    228  1.24       cgd 				/*
    229  1.24       cgd 				 * The tty could have been revoked
    230  1.24       cgd 				 * if we blocked.
    231  1.24       cgd 				 */
    232  1.24       cgd 				if (sp->s_ttyvp)
    233  1.49      fvdl 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
    234  1.24       cgd 			}
    235  1.24       cgd 			if (sp->s_ttyvp)
    236  1.24       cgd 				vrele(sp->s_ttyvp);
    237  1.24       cgd 			sp->s_ttyvp = NULL;
    238  1.24       cgd 			/*
    239  1.24       cgd 			 * s_ttyp is not zero'd; we use this to indicate
    240  1.24       cgd 			 * that the session once had a controlling terminal.
    241  1.24       cgd 			 * (for logging and informational purposes)
    242  1.24       cgd 			 */
    243  1.24       cgd 		}
    244  1.24       cgd 		sp->s_leader = NULL;
    245  1.24       cgd 	}
    246  1.24       cgd 	fixjobc(p, p->p_pgrp, 0);
    247  1.24       cgd 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    248  1.24       cgd 	(void)acct_process(p);
    249  1.24       cgd #ifdef KTRACE
    250  1.24       cgd 	/*
    251  1.24       cgd 	 * release trace file
    252  1.24       cgd 	 */
    253  1.50  christos 	ktrderef(p);
    254  1.24       cgd #endif
    255  1.24       cgd 	/*
    256  1.24       cgd 	 * Remove proc from allproc queue and pidhash chain.
    257  1.56   thorpej 	 * Unlink from parent's child list.
    258  1.24       cgd 	 */
    259  1.25   mycroft 	LIST_REMOVE(p, p_list);
    260  1.24       cgd 	p->p_stat = SZOMB;
    261  1.24       cgd 
    262  1.56   thorpej 	/*
    263  1.56   thorpej 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
    264  1.56   thorpej 	 */
    265  1.56   thorpej 
    266  1.25   mycroft 	LIST_REMOVE(p, p_hash);
    267  1.24       cgd 
    268  1.25   mycroft 	q = p->p_children.lh_first;
    269  1.25   mycroft 	if (q)		/* only need this if any child is S_ZOMB */
    270  1.37   mycroft 		wakeup((caddr_t)initproc);
    271  1.25   mycroft 	for (; q != 0; q = nq) {
    272  1.25   mycroft 		nq = q->p_sibling.le_next;
    273  1.27   mycroft 		proc_reparent(q, initproc);
    274  1.24       cgd 		/*
    275  1.24       cgd 		 * Traced processes are killed
    276  1.24       cgd 		 * since their existence means someone is screwing up.
    277  1.24       cgd 		 */
    278  1.24       cgd 		if (q->p_flag & P_TRACED) {
    279  1.45   mycroft 			q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    280  1.24       cgd 			psignal(q, SIGKILL);
    281  1.24       cgd 		}
    282  1.24       cgd 	}
    283  1.24       cgd 
    284  1.24       cgd 	/*
    285  1.24       cgd 	 * Save exit status and final rusage info, adding in child rusage
    286  1.24       cgd 	 * info and self times.
    287  1.24       cgd 	 */
    288  1.24       cgd 	p->p_xstat = rv;
    289  1.24       cgd 	*p->p_ru = p->p_stats->p_ru;
    290  1.24       cgd 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
    291  1.24       cgd 	ruadd(p->p_ru, &p->p_stats->p_cru);
    292  1.59  christos 
    293  1.59  christos 	/*
    294  1.59  christos 	 * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
    295  1.59  christos 	 * flag set, notify init instead (and hope it will handle
    296  1.59  christos 	 * this situation).
    297  1.59  christos 	 */
    298  1.59  christos 	if (p->p_pptr->p_flag & P_NOCLDWAIT) {
    299  1.59  christos 		struct proc *pp = p->p_pptr;
    300  1.59  christos 		proc_reparent(p, initproc);
    301  1.59  christos 		/*
    302  1.59  christos 		 * If this was the last child of our parent, notify
    303  1.59  christos 		 * parent, so in case he was wait(2)ing, he will
    304  1.59  christos 		 * continue.
    305  1.59  christos 		 */
    306  1.59  christos 		if (pp->p_children.lh_first == NULL)
    307  1.59  christos 			wakeup((caddr_t)pp);
    308  1.59  christos 	}
    309  1.59  christos 	/*
    310  1.59  christos 	 * Notify parent that we're gone.
    311  1.59  christos 	 */
    312  1.59  christos 	if ((p->p_flag & P_FSTRACE) == 0)
    313  1.59  christos 		psignal(p->p_pptr, SIGCHLD);
    314  1.59  christos 	wakeup((caddr_t)p->p_pptr);
    315  1.24       cgd 
    316  1.24       cgd 	/*
    317  1.24       cgd 	 * Clear curproc after we've done all operations
    318  1.24       cgd 	 * that could block, and before tearing down the rest
    319  1.24       cgd 	 * of the process state that might be used from clock, etc.
    320  1.24       cgd 	 * Also, can't clear curproc while we're still runnable,
    321  1.24       cgd 	 * as we're not on a run queue (we are current, just not
    322  1.24       cgd 	 * a proper proc any longer!).
    323  1.24       cgd 	 *
    324  1.24       cgd 	 * Other substructures are freed from wait().
    325  1.24       cgd 	 */
    326  1.24       cgd 	curproc = NULL;
    327  1.36  christos 	if (--p->p_limit->p_refcnt == 0)
    328  1.54   thorpej 		pool_put(&plimit_pool, p->p_limit);
    329  1.24       cgd 
    330  1.24       cgd 	/*
    331  1.56   thorpej 	 * Finally, call machine-dependent code to switch to a new
    332  1.56   thorpej 	 * context (possibly the idle context).  Once we are no longer
    333  1.56   thorpej 	 * using the dead process's vmspace and stack, exit2() will be
    334  1.56   thorpej 	 * called to schedule those resources to be released by the
    335  1.56   thorpej 	 * reaper thread.
    336  1.56   thorpej 	 *
    337  1.56   thorpej 	 * Note that cpu_exit() will end with a call equivalent to
    338  1.56   thorpej 	 * cpu_switch(), finishing our execution (pun intended).
    339  1.24       cgd 	 */
    340  1.24       cgd 	cpu_exit(p);
    341  1.24       cgd }
    342  1.24       cgd 
    343  1.56   thorpej /*
    344  1.56   thorpej  * We are called from cpu_exit() once it is safe to schedule the
    345  1.56   thorpej  * dead process's resources to be freed.
    346  1.56   thorpej  */
    347  1.56   thorpej void
    348  1.56   thorpej exit2(p)
    349  1.56   thorpej 	struct proc *p;
    350  1.56   thorpej {
    351  1.56   thorpej 
    352  1.56   thorpej 	LIST_INSERT_HEAD(&deadproc, p, p_list);
    353  1.56   thorpej 	wakeup(&deadproc);
    354  1.56   thorpej }
    355  1.56   thorpej 
    356  1.56   thorpej /*
    357  1.56   thorpej  * Process reaper.  This is run by a kernel thread to free the resources
    358  1.56   thorpej  * of a dead process.  Once the resources are free, the process becomes
    359  1.56   thorpej  * a zombie, and the parent is allowed to read the undead's status.
    360  1.56   thorpej  */
    361  1.56   thorpej void
    362  1.56   thorpej reaper()
    363  1.56   thorpej {
    364  1.56   thorpej 	struct proc *p;
    365  1.56   thorpej 
    366  1.56   thorpej 	for (;;) {
    367  1.56   thorpej 		p = LIST_FIRST(&deadproc);
    368  1.56   thorpej 		if (p == NULL) {
    369  1.56   thorpej 			/* No work for us; go to sleep until someone exits. */
    370  1.56   thorpej 			(void) tsleep(&deadproc, PVM, "reaper", 0);
    371  1.56   thorpej 			continue;
    372  1.56   thorpej 		}
    373  1.56   thorpej 
    374  1.56   thorpej 		/* Remove us from the deadproc list. */
    375  1.56   thorpej 		LIST_REMOVE(p, p_list);
    376  1.56   thorpej 
    377  1.56   thorpej 		/*
    378  1.56   thorpej 		 * Free the VM resources we're still holding on to.
    379  1.56   thorpej 		 * We must do this from a valid thread because doing
    380  1.56   thorpej 		 * so may block.
    381  1.56   thorpej 		 */
    382  1.56   thorpej #if defined(UVM)
    383  1.56   thorpej 		uvm_exit(p);
    384  1.56   thorpej #else
    385  1.56   thorpej 		vm_exit(p);
    386  1.56   thorpej #endif
    387  1.56   thorpej 
    388  1.56   thorpej 		/* Process is now a true zombie. */
    389  1.56   thorpej 		LIST_INSERT_HEAD(&zombproc, p, p_list);
    390  1.56   thorpej 
    391  1.56   thorpej 		/* Wake up the parent so it can get exit satus. */
    392  1.56   thorpej 		if ((p->p_flag & P_FSTRACE) == 0)
    393  1.56   thorpej 			psignal(p->p_pptr, SIGCHLD);
    394  1.56   thorpej 		wakeup((caddr_t)p->p_pptr);
    395  1.56   thorpej 	}
    396  1.56   thorpej }
    397  1.56   thorpej 
    398  1.24       cgd int
    399  1.33   mycroft sys_wait4(q, v, retval)
    400  1.24       cgd 	register struct proc *q;
    401  1.31   thorpej 	void *v;
    402  1.31   thorpej 	register_t *retval;
    403  1.31   thorpej {
    404  1.33   mycroft 	register struct sys_wait4_args /* {
    405  1.26       cgd 		syscallarg(int) pid;
    406  1.26       cgd 		syscallarg(int *) status;
    407  1.26       cgd 		syscallarg(int) options;
    408  1.26       cgd 		syscallarg(struct rusage *) rusage;
    409  1.31   thorpej 	} */ *uap = v;
    410  1.24       cgd 	register int nfound;
    411  1.24       cgd 	register struct proc *p, *t;
    412  1.24       cgd 	int status, error;
    413  1.24       cgd 
    414  1.26       cgd 	if (SCARG(uap, pid) == 0)
    415  1.26       cgd 		SCARG(uap, pid) = -q->p_pgid;
    416  1.26       cgd 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
    417  1.24       cgd 		return (EINVAL);
    418  1.41     mikel 
    419  1.24       cgd loop:
    420  1.24       cgd 	nfound = 0;
    421  1.25   mycroft 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    422  1.26       cgd 		if (SCARG(uap, pid) != WAIT_ANY &&
    423  1.26       cgd 		    p->p_pid != SCARG(uap, pid) &&
    424  1.26       cgd 		    p->p_pgid != -SCARG(uap, pid))
    425  1.24       cgd 			continue;
    426  1.24       cgd 		nfound++;
    427  1.24       cgd 		if (p->p_stat == SZOMB) {
    428  1.24       cgd 			retval[0] = p->p_pid;
    429  1.30  christos 
    430  1.26       cgd 			if (SCARG(uap, status)) {
    431  1.24       cgd 				status = p->p_xstat;	/* convert to int */
    432  1.36  christos 				error = copyout((caddr_t)&status,
    433  1.36  christos 						(caddr_t)SCARG(uap, status),
    434  1.36  christos 						sizeof(status));
    435  1.36  christos 				if (error)
    436  1.24       cgd 					return (error);
    437  1.24       cgd 			}
    438  1.26       cgd 			if (SCARG(uap, rusage) &&
    439  1.26       cgd 			    (error = copyout((caddr_t)p->p_ru,
    440  1.26       cgd 			    (caddr_t)SCARG(uap, rusage),
    441  1.52     perry 			    sizeof(struct rusage))))
    442  1.24       cgd 				return (error);
    443  1.24       cgd 			/*
    444  1.45   mycroft 			 * If we got the child via ptrace(2) or procfs, and
    445  1.45   mycroft 			 * the parent is different (meaning the process was
    446  1.45   mycroft 			 * attached, rather than run as a child), then we need
    447  1.45   mycroft 			 * to give it back to the old parent, and send the
    448  1.45   mycroft 			 * parent a SIGCHLD.  The rest of the cleanup will be
    449  1.45   mycroft 			 * done when the old parent waits on the child.
    450  1.24       cgd 			 */
    451  1.45   mycroft 			if ((p->p_flag & P_TRACED) &&
    452  1.45   mycroft 			    p->p_oppid != p->p_pptr->p_pid) {
    453  1.45   mycroft 				t = pfind(p->p_oppid);
    454  1.45   mycroft 				proc_reparent(p, t ? t : initproc);
    455  1.24       cgd 				p->p_oppid = 0;
    456  1.45   mycroft 				p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    457  1.45   mycroft 				psignal(p->p_pptr, SIGCHLD);
    458  1.45   mycroft 				wakeup((caddr_t)p->p_pptr);
    459  1.24       cgd 				return (0);
    460  1.24       cgd 			}
    461  1.24       cgd 			p->p_xstat = 0;
    462  1.24       cgd 			ruadd(&q->p_stats->p_cru, p->p_ru);
    463  1.55   thorpej 			pool_put(&rusage_pool, p->p_ru);
    464  1.24       cgd 
    465  1.24       cgd 			/*
    466  1.35   mycroft 			 * Finally finished with old proc entry.
    467  1.35   mycroft 			 * Unlink it from its process group and free it.
    468  1.35   mycroft 			 */
    469  1.35   mycroft 			leavepgrp(p);
    470  1.56   thorpej 
    471  1.35   mycroft 			LIST_REMOVE(p, p_list);	/* off zombproc */
    472  1.56   thorpej 
    473  1.35   mycroft 			LIST_REMOVE(p, p_sibling);
    474  1.35   mycroft 
    475  1.35   mycroft 			/*
    476  1.24       cgd 			 * Decrement the count of procs running with this uid.
    477  1.24       cgd 			 */
    478  1.24       cgd 			(void)chgproccnt(p->p_cred->p_ruid, -1);
    479  1.24       cgd 
    480  1.24       cgd 			/*
    481  1.24       cgd 			 * Free up credentials.
    482  1.24       cgd 			 */
    483  1.24       cgd 			if (--p->p_cred->p_refcnt == 0) {
    484  1.24       cgd 				crfree(p->p_cred->pc_ucred);
    485  1.54   thorpej 				pool_put(&pcred_pool, p->p_cred);
    486  1.24       cgd 			}
    487  1.24       cgd 
    488  1.24       cgd 			/*
    489  1.24       cgd 			 * Release reference to text vnode
    490  1.24       cgd 			 */
    491  1.24       cgd 			if (p->p_textvp)
    492  1.24       cgd 				vrele(p->p_textvp);
    493  1.24       cgd 
    494  1.24       cgd 			/*
    495  1.24       cgd 			 * Give machine-dependent layer a chance
    496  1.24       cgd 			 * to free anything that cpu_exit couldn't
    497  1.24       cgd 			 * release while still running in process context.
    498  1.24       cgd 			 */
    499  1.24       cgd 			cpu_wait(p);
    500  1.53   thorpej 			pool_put(&proc_pool, p);
    501  1.24       cgd 			nprocs--;
    502  1.24       cgd 			return (0);
    503  1.24       cgd 		}
    504  1.24       cgd 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
    505  1.26       cgd 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
    506  1.24       cgd 			p->p_flag |= P_WAITED;
    507  1.24       cgd 			retval[0] = p->p_pid;
    508  1.30  christos 
    509  1.26       cgd 			if (SCARG(uap, status)) {
    510  1.24       cgd 				status = W_STOPCODE(p->p_xstat);
    511  1.24       cgd 				error = copyout((caddr_t)&status,
    512  1.26       cgd 				    (caddr_t)SCARG(uap, status),
    513  1.26       cgd 				    sizeof(status));
    514  1.24       cgd 			} else
    515  1.24       cgd 				error = 0;
    516  1.24       cgd 			return (error);
    517  1.24       cgd 		}
    518  1.24       cgd 	}
    519  1.24       cgd 	if (nfound == 0)
    520  1.24       cgd 		return (ECHILD);
    521  1.26       cgd 	if (SCARG(uap, options) & WNOHANG) {
    522  1.24       cgd 		retval[0] = 0;
    523  1.24       cgd 		return (0);
    524  1.24       cgd 	}
    525  1.36  christos 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
    526  1.24       cgd 		return (error);
    527  1.24       cgd 	goto loop;
    528  1.24       cgd }
    529  1.24       cgd 
    530  1.24       cgd /*
    531  1.24       cgd  * make process 'parent' the new parent of process 'child'.
    532  1.24       cgd  */
    533  1.24       cgd void
    534  1.24       cgd proc_reparent(child, parent)
    535  1.24       cgd 	register struct proc *child;
    536  1.24       cgd 	register struct proc *parent;
    537  1.24       cgd {
    538  1.24       cgd 
    539  1.24       cgd 	if (child->p_pptr == parent)
    540  1.24       cgd 		return;
    541  1.24       cgd 
    542  1.25   mycroft 	LIST_REMOVE(child, p_sibling);
    543  1.25   mycroft 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
    544  1.24       cgd 	child->p_pptr = parent;
    545  1.24       cgd }
    546