Home | History | Annotate | Line # | Download | only in kern
kern_exit.c revision 1.24
      1  1.24  cgd /*	$NetBSD: kern_exit.c,v 1.24 1994/06/29 06:32:26 cgd Exp $	*/
      2  1.24  cgd 
      3  1.24  cgd /*
      4  1.24  cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  1.24  cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.24  cgd  * (c) UNIX System Laboratories, Inc.
      7  1.24  cgd  * All or some portions of this file are derived from material licensed
      8  1.24  cgd  * to the University of California by American Telephone and Telegraph
      9  1.24  cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.24  cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.24  cgd  *
     12  1.24  cgd  * Redistribution and use in source and binary forms, with or without
     13  1.24  cgd  * modification, are permitted provided that the following conditions
     14  1.24  cgd  * are met:
     15  1.24  cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.24  cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.24  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.24  cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.24  cgd  *    documentation and/or other materials provided with the distribution.
     20  1.24  cgd  * 3. All advertising materials mentioning features or use of this software
     21  1.24  cgd  *    must display the following acknowledgement:
     22  1.24  cgd  *	This product includes software developed by the University of
     23  1.24  cgd  *	California, Berkeley and its contributors.
     24  1.24  cgd  * 4. Neither the name of the University nor the names of its contributors
     25  1.24  cgd  *    may be used to endorse or promote products derived from this software
     26  1.24  cgd  *    without specific prior written permission.
     27  1.24  cgd  *
     28  1.24  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.24  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.24  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.24  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.24  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.24  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.24  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.24  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.24  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.24  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.24  cgd  * SUCH DAMAGE.
     39  1.24  cgd  *
     40  1.24  cgd  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
     41  1.24  cgd  */
     42  1.24  cgd 
     43  1.24  cgd #include <sys/param.h>
     44  1.24  cgd #include <sys/systm.h>
     45  1.24  cgd #include <sys/map.h>
     46  1.24  cgd #include <sys/ioctl.h>
     47  1.24  cgd #include <sys/proc.h>
     48  1.24  cgd #include <sys/tty.h>
     49  1.24  cgd #include <sys/time.h>
     50  1.24  cgd #include <sys/resource.h>
     51  1.24  cgd #include <sys/kernel.h>
     52  1.24  cgd #include <sys/proc.h>
     53  1.24  cgd #include <sys/buf.h>
     54  1.24  cgd #include <sys/wait.h>
     55  1.24  cgd #include <sys/file.h>
     56  1.24  cgd #include <sys/vnode.h>
     57  1.24  cgd #include <sys/syslog.h>
     58  1.24  cgd #include <sys/malloc.h>
     59  1.24  cgd #include <sys/resourcevar.h>
     60  1.24  cgd #include <sys/ptrace.h>
     61  1.24  cgd 
     62  1.24  cgd #include <machine/cpu.h>
     63  1.24  cgd #ifdef COMPAT_43
     64  1.24  cgd #include <machine/reg.h>
     65  1.24  cgd #include <machine/psl.h>
     66  1.24  cgd #endif
     67  1.24  cgd 
     68  1.24  cgd #include <vm/vm.h>
     69  1.24  cgd #include <vm/vm_kern.h>
     70  1.24  cgd 
     71  1.24  cgd __dead void cpu_exit __P((struct proc *));
     72  1.24  cgd __dead void exit1 __P((struct proc *, int));
     73  1.24  cgd 
     74  1.24  cgd /*
     75  1.24  cgd  * exit --
     76  1.24  cgd  *	Death of process.
     77  1.24  cgd  */
     78  1.24  cgd struct exit_args {
     79  1.24  cgd 	int	rval;
     80  1.24  cgd };
     81  1.24  cgd __dead void
     82  1.24  cgd exit(p, uap, retval)
     83  1.24  cgd 	struct proc *p;
     84  1.24  cgd 	struct exit_args *uap;
     85  1.24  cgd 	int *retval;
     86  1.24  cgd {
     87  1.24  cgd 
     88  1.24  cgd 	exit1(p, W_EXITCODE(uap->rval, 0));
     89  1.24  cgd 	/* NOTREACHED */
     90  1.24  cgd }
     91  1.24  cgd 
     92  1.24  cgd /*
     93  1.24  cgd  * Exit: deallocate address space and other resources, change proc state
     94  1.24  cgd  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
     95  1.24  cgd  * status and rusage for wait().  Check for child processes and orphan them.
     96  1.24  cgd  */
     97  1.24  cgd __dead void
     98  1.24  cgd exit1(p, rv)
     99  1.24  cgd 	register struct proc *p;
    100  1.24  cgd 	int rv;
    101  1.24  cgd {
    102  1.24  cgd 	register struct proc *q, *nq;
    103  1.24  cgd 	register struct proc **pp;
    104  1.24  cgd 	register struct vmspace *vm;
    105  1.24  cgd 
    106  1.24  cgd 	if (p->p_pid == 1)
    107  1.24  cgd 		panic("init died (signal %d, exit %d)",
    108  1.24  cgd 		    WTERMSIG(rv), WEXITSTATUS(rv));
    109  1.24  cgd #ifdef PGINPROF
    110  1.24  cgd 	vmsizmon();
    111  1.24  cgd #endif
    112  1.24  cgd 	if (p->p_flag & P_PROFIL)
    113  1.24  cgd 		stopprofclock(p);
    114  1.24  cgd 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
    115  1.24  cgd 		M_ZOMBIE, M_WAITOK);
    116  1.24  cgd 	/*
    117  1.24  cgd 	 * If parent is waiting for us to exit or exec,
    118  1.24  cgd 	 * P_PPWAIT is set; we will wakeup the parent below.
    119  1.24  cgd 	 */
    120  1.24  cgd 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
    121  1.24  cgd 	p->p_flag |= P_WEXIT;
    122  1.24  cgd 	p->p_sigignore = ~0;
    123  1.24  cgd 	p->p_siglist = 0;
    124  1.24  cgd 	untimeout(realitexpire, (caddr_t)p);
    125  1.24  cgd 
    126  1.24  cgd 	/*
    127  1.24  cgd 	 * Close open files and release open-file table.
    128  1.24  cgd 	 * This may block!
    129  1.24  cgd 	 */
    130  1.24  cgd 	fdfree(p);
    131  1.24  cgd 
    132  1.24  cgd 	/* The next three chunks should probably be moved to vmspace_exit. */
    133  1.24  cgd 	vm = p->p_vmspace;
    134  1.24  cgd #ifdef SYSVSHM
    135  1.24  cgd 	if (vm->vm_shm)
    136  1.24  cgd 		shmexit(p);
    137  1.24  cgd #endif
    138  1.24  cgd #ifdef SYSVSEM
    139  1.24  cgd 	semexit(p);
    140  1.24  cgd #endif
    141  1.24  cgd 	/*
    142  1.24  cgd 	 * Release user portion of address space.
    143  1.24  cgd 	 * This releases references to vnodes,
    144  1.24  cgd 	 * which could cause I/O if the file has been unlinked.
    145  1.24  cgd 	 * Need to do this early enough that we can still sleep.
    146  1.24  cgd 	 * Can't free the entire vmspace as the kernel stack
    147  1.24  cgd 	 * may be mapped within that space also.
    148  1.24  cgd 	 */
    149  1.24  cgd 	if (vm->vm_refcnt == 1)
    150  1.24  cgd 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
    151  1.24  cgd 		    VM_MAXUSER_ADDRESS);
    152  1.24  cgd 
    153  1.24  cgd 	if (SESS_LEADER(p)) {
    154  1.24  cgd 		register struct session *sp = p->p_session;
    155  1.24  cgd 
    156  1.24  cgd 		if (sp->s_ttyvp) {
    157  1.24  cgd 			/*
    158  1.24  cgd 			 * Controlling process.
    159  1.24  cgd 			 * Signal foreground pgrp,
    160  1.24  cgd 			 * drain controlling terminal
    161  1.24  cgd 			 * and revoke access to controlling terminal.
    162  1.24  cgd 			 */
    163  1.24  cgd 			if (sp->s_ttyp->t_session == sp) {
    164  1.24  cgd 				if (sp->s_ttyp->t_pgrp)
    165  1.24  cgd 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    166  1.24  cgd 				(void) ttywait(sp->s_ttyp);
    167  1.24  cgd 				/*
    168  1.24  cgd 				 * The tty could have been revoked
    169  1.24  cgd 				 * if we blocked.
    170  1.24  cgd 				 */
    171  1.24  cgd 				if (sp->s_ttyvp)
    172  1.24  cgd 					vgoneall(sp->s_ttyvp);
    173  1.24  cgd 			}
    174  1.24  cgd 			if (sp->s_ttyvp)
    175  1.24  cgd 				vrele(sp->s_ttyvp);
    176  1.24  cgd 			sp->s_ttyvp = NULL;
    177  1.24  cgd 			/*
    178  1.24  cgd 			 * s_ttyp is not zero'd; we use this to indicate
    179  1.24  cgd 			 * that the session once had a controlling terminal.
    180  1.24  cgd 			 * (for logging and informational purposes)
    181  1.24  cgd 			 */
    182  1.24  cgd 		}
    183  1.24  cgd 		sp->s_leader = NULL;
    184  1.24  cgd 	}
    185  1.24  cgd 	fixjobc(p, p->p_pgrp, 0);
    186  1.24  cgd 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    187  1.24  cgd 	(void)acct_process(p);
    188  1.24  cgd #ifdef KTRACE
    189  1.24  cgd 	/*
    190  1.24  cgd 	 * release trace file
    191  1.24  cgd 	 */
    192  1.24  cgd 	p->p_traceflag = 0;	/* don't trace the vrele() */
    193  1.24  cgd 	if (p->p_tracep)
    194  1.24  cgd 		vrele(p->p_tracep);
    195  1.24  cgd #endif
    196  1.24  cgd 	/*
    197  1.24  cgd 	 * Remove proc from allproc queue and pidhash chain.
    198  1.24  cgd 	 * Place onto zombproc.  Unlink from parent's child list.
    199  1.24  cgd 	 */
    200  1.24  cgd 	if (*p->p_prev = p->p_next)
    201  1.24  cgd 		p->p_next->p_prev = p->p_prev;
    202  1.24  cgd 	if (p->p_next = zombproc)
    203  1.24  cgd 		p->p_next->p_prev = &p->p_next;
    204  1.24  cgd 	p->p_prev = &zombproc;
    205  1.24  cgd 	zombproc = p;
    206  1.24  cgd 	p->p_stat = SZOMB;
    207  1.24  cgd 
    208  1.24  cgd 	for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
    209  1.24  cgd 		if (*pp == p) {
    210  1.24  cgd 			*pp = p->p_hash;
    211  1.24  cgd 			goto done;
    212  1.24  cgd 		}
    213  1.24  cgd 	panic("exit");
    214  1.24  cgd done:
    215  1.24  cgd 
    216  1.24  cgd 	if (p->p_cptr)		/* only need this if any child is S_ZOMB */
    217  1.24  cgd 		wakeup((caddr_t) initproc);
    218  1.24  cgd 	for (q = p->p_cptr; q != NULL; q = nq) {
    219  1.24  cgd 		nq = q->p_osptr;
    220  1.24  cgd 		if (nq != NULL)
    221  1.24  cgd 			nq->p_ysptr = NULL;
    222  1.24  cgd 		if (initproc->p_cptr)
    223  1.24  cgd 			initproc->p_cptr->p_ysptr = q;
    224  1.24  cgd 		q->p_osptr = initproc->p_cptr;
    225  1.24  cgd 		q->p_ysptr = NULL;
    226  1.24  cgd 		initproc->p_cptr = q;
    227  1.24  cgd 
    228  1.24  cgd 		q->p_pptr = initproc;
    229  1.24  cgd 		/*
    230  1.24  cgd 		 * Traced processes are killed
    231  1.24  cgd 		 * since their existence means someone is screwing up.
    232  1.24  cgd 		 */
    233  1.24  cgd 		if (q->p_flag & P_TRACED) {
    234  1.24  cgd 			q->p_flag &= ~P_TRACED;
    235  1.24  cgd 			psignal(q, SIGKILL);
    236  1.24  cgd 		}
    237  1.24  cgd 	}
    238  1.24  cgd 	p->p_cptr = NULL;
    239  1.24  cgd 
    240  1.24  cgd 	/*
    241  1.24  cgd 	 * Save exit status and final rusage info, adding in child rusage
    242  1.24  cgd 	 * info and self times.
    243  1.24  cgd 	 */
    244  1.24  cgd 	p->p_xstat = rv;
    245  1.24  cgd 	*p->p_ru = p->p_stats->p_ru;
    246  1.24  cgd 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
    247  1.24  cgd 	ruadd(p->p_ru, &p->p_stats->p_cru);
    248  1.24  cgd 
    249  1.24  cgd 	/*
    250  1.24  cgd 	 * Notify parent that we're gone.
    251  1.24  cgd 	 */
    252  1.24  cgd 	psignal(p->p_pptr, SIGCHLD);
    253  1.24  cgd 	wakeup((caddr_t)p->p_pptr);
    254  1.24  cgd 	/*
    255  1.24  cgd 	 * Notify procfs debugger
    256  1.24  cgd 	 */
    257  1.24  cgd 	if (p->p_flag & P_FSTRACE)
    258  1.24  cgd 		wakeup((caddr_t)p);
    259  1.24  cgd #if defined(tahoe)
    260  1.24  cgd 	/* move this to cpu_exit */
    261  1.24  cgd 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
    262  1.24  cgd #endif
    263  1.24  cgd 	/*
    264  1.24  cgd 	 * Clear curproc after we've done all operations
    265  1.24  cgd 	 * that could block, and before tearing down the rest
    266  1.24  cgd 	 * of the process state that might be used from clock, etc.
    267  1.24  cgd 	 * Also, can't clear curproc while we're still runnable,
    268  1.24  cgd 	 * as we're not on a run queue (we are current, just not
    269  1.24  cgd 	 * a proper proc any longer!).
    270  1.24  cgd 	 *
    271  1.24  cgd 	 * Other substructures are freed from wait().
    272  1.24  cgd 	 */
    273  1.24  cgd 	curproc = NULL;
    274  1.24  cgd 	if (--p->p_limit->p_refcnt == 0)
    275  1.24  cgd 		FREE(p->p_limit, M_SUBPROC);
    276  1.24  cgd 
    277  1.24  cgd 	/*
    278  1.24  cgd 	 * Finally, call machine-dependent code to release the remaining
    279  1.24  cgd 	 * resources including address space, the kernel stack and pcb.
    280  1.24  cgd 	 * The address space is released by "vmspace_free(p->p_vmspace)";
    281  1.24  cgd 	 * This is machine-dependent, as we may have to change stacks
    282  1.24  cgd 	 * or ensure that the current one isn't reallocated before we
    283  1.24  cgd 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
    284  1.24  cgd 	 * our execution (pun intended).
    285  1.24  cgd 	 */
    286  1.24  cgd 	cpu_exit(p);
    287  1.24  cgd }
    288  1.24  cgd 
    289  1.24  cgd struct wait_args {
    290  1.24  cgd 	int	pid;
    291  1.24  cgd 	int	*status;
    292  1.24  cgd 	int	options;
    293  1.24  cgd 	struct	rusage *rusage;
    294  1.24  cgd #ifdef COMPAT_43
    295  1.24  cgd 	int	compat;		/* pseudo */
    296  1.24  cgd #endif
    297  1.24  cgd };
    298  1.24  cgd 
    299  1.24  cgd #ifdef COMPAT_43
    300  1.24  cgd #ifdef m68k
    301  1.24  cgd #include <machine/frame.h>
    302  1.24  cgd #define GETPS(rp)	((struct frame *)(rp))->f_sr
    303  1.24  cgd #else
    304  1.24  cgd #define GETPS(rp)	(rp)[PS]
    305  1.24  cgd #endif
    306  1.24  cgd 
    307  1.24  cgd owait(p, uap, retval)
    308  1.24  cgd 	struct proc *p;
    309  1.24  cgd 	register struct wait_args *uap;
    310  1.24  cgd 	int *retval;
    311  1.24  cgd {
    312  1.24  cgd 
    313  1.24  cgd #ifdef PSL_ALLCC
    314  1.24  cgd 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
    315  1.24  cgd 		uap->options = 0;
    316  1.24  cgd 		uap->rusage = NULL;
    317  1.24  cgd 	} else {
    318  1.24  cgd 		uap->options = p->p_md.md_regs[R0];
    319  1.24  cgd 		uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
    320  1.24  cgd 	}
    321  1.24  cgd #else
    322  1.24  cgd 	uap->options = 0;
    323  1.24  cgd 	uap->rusage = NULL;
    324  1.24  cgd #endif
    325  1.24  cgd 	uap->pid = WAIT_ANY;
    326  1.24  cgd 	uap->status = NULL;
    327  1.24  cgd 	uap->compat = 1;
    328  1.24  cgd 	return (wait1(p, uap, retval));
    329  1.24  cgd }
    330  1.24  cgd 
    331  1.24  cgd wait4(p, uap, retval)
    332  1.24  cgd 	struct proc *p;
    333  1.24  cgd 	struct wait_args *uap;
    334  1.24  cgd 	int *retval;
    335  1.24  cgd {
    336  1.24  cgd 
    337  1.24  cgd 	uap->compat = 0;
    338  1.24  cgd 	return (wait1(p, uap, retval));
    339  1.24  cgd }
    340  1.24  cgd #else
    341  1.24  cgd #define	wait1	wait4
    342  1.24  cgd #endif
    343  1.24  cgd 
    344  1.24  cgd int
    345  1.24  cgd wait1(q, uap, retval)
    346  1.24  cgd 	register struct proc *q;
    347  1.24  cgd 	register struct wait_args *uap;
    348  1.24  cgd 	int retval[];
    349  1.24  cgd {
    350  1.24  cgd 	register int nfound;
    351  1.24  cgd 	register struct proc *p, *t;
    352  1.24  cgd 	int status, error;
    353  1.24  cgd 
    354  1.24  cgd #ifdef COMPAT_09
    355  1.24  cgd 	uap->pid = (short) uap->pid;
    356  1.24  cgd #endif
    357  1.24  cgd 
    358  1.24  cgd 	if (uap->pid == 0)
    359  1.24  cgd 		uap->pid = -q->p_pgid;
    360  1.24  cgd #ifdef notyet
    361  1.24  cgd 	if (uap->options &~ (WUNTRACED|WNOHANG))
    362  1.24  cgd 		return (EINVAL);
    363  1.24  cgd #endif
    364  1.24  cgd loop:
    365  1.24  cgd 	nfound = 0;
    366  1.24  cgd 	for (p = q->p_cptr; p; p = p->p_osptr) {
    367  1.24  cgd 		if (uap->pid != WAIT_ANY &&
    368  1.24  cgd 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
    369  1.24  cgd 			continue;
    370  1.24  cgd 		nfound++;
    371  1.24  cgd 		if (p->p_stat == SZOMB) {
    372  1.24  cgd 			retval[0] = p->p_pid;
    373  1.24  cgd #ifdef COMPAT_43
    374  1.24  cgd 			if (uap->compat)
    375  1.24  cgd 				retval[1] = p->p_xstat;
    376  1.24  cgd 			else
    377  1.24  cgd #endif
    378  1.24  cgd 			if (uap->status) {
    379  1.24  cgd 				status = p->p_xstat;	/* convert to int */
    380  1.24  cgd 				if (error = copyout((caddr_t)&status,
    381  1.24  cgd 				    (caddr_t)uap->status, sizeof(status)))
    382  1.24  cgd 					return (error);
    383  1.24  cgd 			}
    384  1.24  cgd 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
    385  1.24  cgd 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
    386  1.24  cgd 				return (error);
    387  1.24  cgd 			/*
    388  1.24  cgd 			 * If we got the child via a ptrace 'attach',
    389  1.24  cgd 			 * we need to give it back to the old parent.
    390  1.24  cgd 			 */
    391  1.24  cgd 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
    392  1.24  cgd 				p->p_oppid = 0;
    393  1.24  cgd 				proc_reparent(p, t);
    394  1.24  cgd 				psignal(t, SIGCHLD);
    395  1.24  cgd 				wakeup((caddr_t)t);
    396  1.24  cgd 				return (0);
    397  1.24  cgd 			}
    398  1.24  cgd 			p->p_xstat = 0;
    399  1.24  cgd 			ruadd(&q->p_stats->p_cru, p->p_ru);
    400  1.24  cgd 			FREE(p->p_ru, M_ZOMBIE);
    401  1.24  cgd 
    402  1.24  cgd 			/*
    403  1.24  cgd 			 * Decrement the count of procs running with this uid.
    404  1.24  cgd 			 */
    405  1.24  cgd 			(void)chgproccnt(p->p_cred->p_ruid, -1);
    406  1.24  cgd 
    407  1.24  cgd 			/*
    408  1.24  cgd 			 * Free up credentials.
    409  1.24  cgd 			 */
    410  1.24  cgd 			if (--p->p_cred->p_refcnt == 0) {
    411  1.24  cgd 				crfree(p->p_cred->pc_ucred);
    412  1.24  cgd 				FREE(p->p_cred, M_SUBPROC);
    413  1.24  cgd 			}
    414  1.24  cgd 
    415  1.24  cgd 			/*
    416  1.24  cgd 			 * Release reference to text vnode
    417  1.24  cgd 			 */
    418  1.24  cgd 			if (p->p_textvp)
    419  1.24  cgd 				vrele(p->p_textvp);
    420  1.24  cgd 
    421  1.24  cgd 			/*
    422  1.24  cgd 			 * Finally finished with old proc entry.
    423  1.24  cgd 			 * Unlink it from its process group and free it.
    424  1.24  cgd 			 */
    425  1.24  cgd 			leavepgrp(p);
    426  1.24  cgd 			if (*p->p_prev = p->p_next)	/* off zombproc */
    427  1.24  cgd 				p->p_next->p_prev = p->p_prev;
    428  1.24  cgd 			if (q = p->p_ysptr)
    429  1.24  cgd 				q->p_osptr = p->p_osptr;
    430  1.24  cgd 			if (q = p->p_osptr)
    431  1.24  cgd 				q->p_ysptr = p->p_ysptr;
    432  1.24  cgd 			if ((q = p->p_pptr)->p_cptr == p)
    433  1.24  cgd 				q->p_cptr = p->p_osptr;
    434  1.24  cgd 
    435  1.24  cgd 			/*
    436  1.24  cgd 			 * Give machine-dependent layer a chance
    437  1.24  cgd 			 * to free anything that cpu_exit couldn't
    438  1.24  cgd 			 * release while still running in process context.
    439  1.24  cgd 			 */
    440  1.24  cgd 			cpu_wait(p);
    441  1.24  cgd 			FREE(p, M_PROC);
    442  1.24  cgd 			nprocs--;
    443  1.24  cgd 			return (0);
    444  1.24  cgd 		}
    445  1.24  cgd 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
    446  1.24  cgd 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
    447  1.24  cgd 			p->p_flag |= P_WAITED;
    448  1.24  cgd 			retval[0] = p->p_pid;
    449  1.24  cgd #ifdef COMPAT_43
    450  1.24  cgd 			if (uap->compat) {
    451  1.24  cgd 				retval[1] = W_STOPCODE(p->p_xstat);
    452  1.24  cgd 				error = 0;
    453  1.24  cgd 			} else
    454  1.24  cgd #endif
    455  1.24  cgd 			if (uap->status) {
    456  1.24  cgd 				status = W_STOPCODE(p->p_xstat);
    457  1.24  cgd 				error = copyout((caddr_t)&status,
    458  1.24  cgd 					(caddr_t)uap->status, sizeof(status));
    459  1.24  cgd 			} else
    460  1.24  cgd 				error = 0;
    461  1.24  cgd 			return (error);
    462  1.24  cgd 		}
    463  1.24  cgd 	}
    464  1.24  cgd 	if (nfound == 0)
    465  1.24  cgd 		return (ECHILD);
    466  1.24  cgd 	if (uap->options & WNOHANG) {
    467  1.24  cgd 		retval[0] = 0;
    468  1.24  cgd 		return (0);
    469  1.24  cgd 	}
    470  1.24  cgd 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
    471  1.24  cgd 		return (error);
    472  1.24  cgd 	goto loop;
    473  1.24  cgd }
    474  1.24  cgd 
    475  1.24  cgd /*
    476  1.24  cgd  * make process 'parent' the new parent of process 'child'.
    477  1.24  cgd  */
    478  1.24  cgd void
    479  1.24  cgd proc_reparent(child, parent)
    480  1.24  cgd 	register struct proc *child;
    481  1.24  cgd 	register struct proc *parent;
    482  1.24  cgd {
    483  1.24  cgd 	register struct proc *o;
    484  1.24  cgd 	register struct proc *y;
    485  1.24  cgd 
    486  1.24  cgd 	if (child->p_pptr == parent)
    487  1.24  cgd 		return;
    488  1.24  cgd 
    489  1.24  cgd 	/* fix up the child linkage for the old parent */
    490  1.24  cgd 	o = child->p_osptr;
    491  1.24  cgd 	y = child->p_ysptr;
    492  1.24  cgd 	if (y)
    493  1.24  cgd 		y->p_osptr = o;
    494  1.24  cgd 	if (o)
    495  1.24  cgd 		o->p_ysptr = y;
    496  1.24  cgd 	if (child->p_pptr->p_cptr == child)
    497  1.24  cgd 		child->p_pptr->p_cptr = o;
    498  1.24  cgd 
    499  1.24  cgd 	/* fix up child linkage for new parent */
    500  1.24  cgd 	o = parent->p_cptr;
    501  1.24  cgd 	if (o)
    502  1.24  cgd 		o->p_ysptr = child;
    503  1.24  cgd 	child->p_osptr = o;
    504  1.24  cgd 	child->p_ysptr = NULL;
    505  1.24  cgd 	parent->p_cptr = child;
    506  1.24  cgd 	child->p_pptr = parent;
    507  1.24  cgd }
    508