Home | History | Annotate | Line # | Download | only in kern
kern_exit.c revision 1.279
      1  1.279        ad /*	$NetBSD: kern_exit.c,v 1.279 2020/01/08 17:38:42 ad Exp $	*/
      2   1.56   thorpej 
      3   1.56   thorpej /*-
      4  1.204        ad  * Copyright (c) 1998, 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5   1.56   thorpej  * All rights reserved.
      6   1.58  christos  *
      7   1.56   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.56   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.165        ad  * NASA Ames Research Center, and by Andrew Doran.
     10   1.56   thorpej  *
     11   1.56   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.56   thorpej  * modification, are permitted provided that the following conditions
     13   1.56   thorpej  * are met:
     14   1.56   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.56   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.58  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.56   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.56   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.58  christos  *
     20   1.56   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.56   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.56   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.56   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.56   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.56   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.56   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.56   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.56   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.56   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.56   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.56   thorpej  */
     32   1.24       cgd 
     33   1.24       cgd /*
     34   1.24       cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     35   1.24       cgd  *	The Regents of the University of California.  All rights reserved.
     36   1.24       cgd  * (c) UNIX System Laboratories, Inc.
     37   1.24       cgd  * All or some portions of this file are derived from material licensed
     38   1.24       cgd  * to the University of California by American Telephone and Telegraph
     39   1.24       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40   1.24       cgd  * the permission of UNIX System Laboratories, Inc.
     41   1.24       cgd  *
     42   1.24       cgd  * Redistribution and use in source and binary forms, with or without
     43   1.24       cgd  * modification, are permitted provided that the following conditions
     44   1.24       cgd  * are met:
     45   1.24       cgd  * 1. Redistributions of source code must retain the above copyright
     46   1.24       cgd  *    notice, this list of conditions and the following disclaimer.
     47   1.24       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.24       cgd  *    notice, this list of conditions and the following disclaimer in the
     49   1.24       cgd  *    documentation and/or other materials provided with the distribution.
     50  1.119       agc  * 3. Neither the name of the University nor the names of its contributors
     51   1.24       cgd  *    may be used to endorse or promote products derived from this software
     52   1.24       cgd  *    without specific prior written permission.
     53   1.24       cgd  *
     54   1.24       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.24       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.24       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.24       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.24       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.24       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.24       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.24       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.24       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.24       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.24       cgd  * SUCH DAMAGE.
     65   1.24       cgd  *
     66   1.49      fvdl  *	@(#)kern_exit.c	8.10 (Berkeley) 2/23/95
     67   1.24       cgd  */
     68   1.92     lukem 
     69   1.92     lukem #include <sys/cdefs.h>
     70  1.279        ad __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.279 2020/01/08 17:38:42 ad Exp $");
     71   1.48       mrg 
     72   1.51   thorpej #include "opt_ktrace.h"
     73  1.245  christos #include "opt_dtrace.h"
     74   1.60      tron #include "opt_sysv.h"
     75   1.24       cgd 
     76   1.24       cgd #include <sys/param.h>
     77   1.24       cgd #include <sys/systm.h>
     78   1.24       cgd #include <sys/ioctl.h>
     79   1.24       cgd #include <sys/tty.h>
     80   1.24       cgd #include <sys/time.h>
     81   1.24       cgd #include <sys/resource.h>
     82   1.24       cgd #include <sys/kernel.h>
     83   1.24       cgd #include <sys/proc.h>
     84   1.24       cgd #include <sys/buf.h>
     85   1.24       cgd #include <sys/wait.h>
     86   1.24       cgd #include <sys/file.h>
     87  1.274   hannken #include <sys/fstrans.h>
     88   1.24       cgd #include <sys/vnode.h>
     89   1.24       cgd #include <sys/syslog.h>
     90   1.53   thorpej #include <sys/pool.h>
     91  1.212     pooka #include <sys/uidinfo.h>
     92   1.24       cgd #include <sys/ptrace.h>
     93   1.29       cgd #include <sys/acct.h>
     94   1.36  christos #include <sys/filedesc.h>
     95  1.100  gmcgarry #include <sys/ras.h>
     96   1.36  christos #include <sys/signalvar.h>
     97   1.64      ross #include <sys/sched.h>
     98   1.26       cgd #include <sys/mount.h>
     99   1.26       cgd #include <sys/syscallargs.h>
    100  1.156      elad #include <sys/kauth.h>
    101  1.165        ad #include <sys/sleepq.h>
    102  1.165        ad #include <sys/lockdebug.h>
    103  1.165        ad #include <sys/ktrace.h>
    104  1.189        ad #include <sys/cpu.h>
    105  1.193        ad #include <sys/lwpctl.h>
    106  1.194        ad #include <sys/atomic.h>
    107  1.226    darran #include <sys/sdt.h>
    108  1.275     ozaki #include <sys/psref.h>
    109   1.24       cgd 
    110   1.47       mrg #include <uvm/uvm_extern.h>
    111   1.47       mrg 
    112  1.107   thorpej #ifdef DEBUG_EXIT
    113  1.107   thorpej int debug_exit = 0;
    114  1.107   thorpej #define DPRINTF(x) if (debug_exit) printf x
    115  1.107   thorpej #else
    116  1.107   thorpej #define DPRINTF(x)
    117  1.107   thorpej #endif
    118  1.107   thorpej 
    119  1.249  christos static int find_stopped_child(struct proc *, idtype_t, id_t, int,
    120  1.255  christos     struct proc **, struct wrusage *, siginfo_t *);
    121  1.249  christos static void proc_free(struct proc *, struct wrusage *);
    122  1.177       dsl 
    123  1.123  christos /*
    124  1.226    darran  * DTrace SDT provider definitions
    125  1.226    darran  */
    126  1.245  christos SDT_PROVIDER_DECLARE(proc);
    127  1.245  christos SDT_PROBE_DEFINE1(proc, kernel, , exit, "int");
    128  1.245  christos 
    129  1.226    darran /*
    130  1.132  jdolecek  * Fill in the appropriate signal information, and signal the parent.
    131  1.123  christos  */
    132  1.235       chs /* XXX noclone works around a gcc 4.5 bug on arm */
    133  1.235       chs static void __noclone
    134  1.162      yamt exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
    135  1.123  christos {
    136  1.123  christos 
    137  1.163      yamt 	KSI_INIT(ksi);
    138  1.140        pk 	if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
    139  1.252  christos 		if (p->p_xsig) {
    140  1.252  christos 			if (p->p_sflag & PS_COREDUMP)
    141  1.140        pk 				ksi->ksi_code = CLD_DUMPED;
    142  1.123  christos 			else
    143  1.140        pk 				ksi->ksi_code = CLD_KILLED;
    144  1.252  christos 			ksi->ksi_status = p->p_xsig;
    145  1.123  christos 		} else {
    146  1.140        pk 			ksi->ksi_code = CLD_EXITED;
    147  1.252  christos 			ksi->ksi_status = p->p_xexit;
    148  1.123  christos 		}
    149  1.252  christos 	} else {
    150  1.252  christos 		ksi->ksi_code = SI_USER;
    151  1.252  christos 		ksi->ksi_status = p->p_xsig;
    152  1.123  christos 	}
    153  1.123  christos 	/*
    154  1.165        ad 	 * We fill those in, even for non-SIGCHLD.
    155  1.165        ad 	 * It's safe to access p->p_cred unlocked here.
    156  1.123  christos 	 */
    157  1.140        pk 	ksi->ksi_pid = p->p_pid;
    158  1.156      elad 	ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
    159  1.123  christos 	/* XXX: is this still valid? */
    160  1.175       dsl 	ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
    161  1.175       dsl 	ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
    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.195       dsl sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval)
    170   1.31   thorpej {
    171  1.195       dsl 	/* {
    172   1.89     lukem 		syscallarg(int)	rval;
    173  1.195       dsl 	} */
    174  1.165        ad 	struct proc *p = l->l_proc;
    175   1.24       cgd 
    176  1.165        ad 	/* Don't call exit1() multiple times in the same process. */
    177  1.204        ad 	mutex_enter(p->p_lock);
    178  1.165        ad 	if (p->p_sflag & PS_WEXIT) {
    179  1.204        ad 		mutex_exit(p->p_lock);
    180  1.107   thorpej 		lwp_exit(l);
    181  1.165        ad 	}
    182  1.107   thorpej 
    183  1.165        ad 	/* exit1() will release the mutex. */
    184  1.253  christos 	exit1(l, SCARG(uap, rval), 0);
    185   1.24       cgd 	/* NOTREACHED */
    186   1.31   thorpej 	return (0);
    187   1.24       cgd }
    188   1.24       cgd 
    189   1.24       cgd /*
    190   1.24       cgd  * Exit: deallocate address space and other resources, change proc state
    191   1.24       cgd  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
    192   1.24       cgd  * status and rusage for wait().  Check for child processes and orphan them.
    193  1.165        ad  *
    194  1.204        ad  * Must be called with p->p_lock held.  Does not return.
    195   1.24       cgd  */
    196   1.31   thorpej void
    197  1.253  christos exit1(struct lwp *l, int exitcode, int signo)
    198   1.24       cgd {
    199  1.241  riastrad 	struct proc	*p, *child, *next_child, *old_parent, *new_parent;
    200  1.201        ad 	struct pgrp	*pgrp;
    201  1.140        pk 	ksiginfo_t	ksi;
    202  1.165        ad 	ksiginfoq_t	kq;
    203  1.237     rmind 	int		wakeinit;
    204   1.24       cgd 
    205  1.107   thorpej 	p = l->l_proc;
    206  1.107   thorpej 
    207  1.279        ad 	/* Verify that we hold no locks other than p->p_lock. */
    208  1.279        ad 	LOCKDEBUG_BARRIER(p->p_lock, 0);
    209  1.204        ad 	KASSERT(mutex_owned(p->p_lock));
    210  1.238    martin 	KASSERT(p->p_vmspace != NULL);
    211  1.165        ad 
    212  1.237     rmind 	if (__predict_false(p == initproc)) {
    213  1.252  christos 		panic("init died (signal %d, exit %d)", signo, exitcode);
    214  1.213  wrstuden 	}
    215  1.213  wrstuden 
    216  1.165        ad 	p->p_sflag |= PS_WEXIT;
    217  1.165        ad 
    218  1.165        ad 	/*
    219  1.165        ad 	 * Force all other LWPs to exit before we do.  Only then can we
    220  1.165        ad 	 * begin to tear down the rest of the process state.
    221  1.165        ad 	 */
    222  1.237     rmind 	if (p->p_nlwps > 1) {
    223  1.165        ad 		exit_lwps(l);
    224  1.237     rmind 	}
    225  1.165        ad 
    226  1.165        ad 	ksiginfo_queue_init(&kq);
    227  1.165        ad 
    228  1.165        ad 	/*
    229  1.165        ad 	 * If we have been asked to stop on exit, do so now.
    230  1.165        ad 	 */
    231  1.211        ad 	if (__predict_false(p->p_sflag & PS_STOPEXIT)) {
    232  1.165        ad 		KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
    233  1.165        ad 		sigclearall(p, &contsigmask, &kq);
    234  1.247  pgoyette 
    235  1.247  pgoyette 		if (!mutex_tryenter(proc_lock)) {
    236  1.247  pgoyette 			mutex_exit(p->p_lock);
    237  1.247  pgoyette 			mutex_enter(proc_lock);
    238  1.247  pgoyette 			mutex_enter(p->p_lock);
    239  1.247  pgoyette 		}
    240  1.165        ad 		p->p_waited = 0;
    241  1.247  pgoyette 		p->p_pptr->p_nstopchild++;
    242  1.248  pgoyette 		p->p_stat = SSTOP;
    243  1.247  pgoyette 		mutex_exit(proc_lock);
    244  1.165        ad 		lwp_lock(l);
    245  1.165        ad 		p->p_nrlwps--;
    246  1.130    atatat 		l->l_stat = LSSTOP;
    247  1.231     rmind 		lwp_unlock(l);
    248  1.204        ad 		mutex_exit(p->p_lock);
    249  1.231     rmind 		lwp_lock(l);
    250  1.278        ad 		spc_lock(l->l_cpu);
    251  1.179      yamt 		mi_switch(l);
    252  1.211        ad 		mutex_enter(p->p_lock);
    253  1.211        ad 	}
    254  1.211        ad 
    255  1.211        ad 	/*
    256  1.211        ad 	 * Bin any remaining signals and mark the process as dying so it will
    257  1.241  riastrad 	 * not be found for, e.g. signals.
    258  1.211        ad 	 */
    259  1.211        ad 	sigfillset(&p->p_sigctx.ps_sigignore);
    260  1.211        ad 	sigclearall(p, NULL, &kq);
    261  1.211        ad 	p->p_stat = SDYING;
    262  1.211        ad 	mutex_exit(p->p_lock);
    263  1.211        ad 	ksiginfo_queue_drain(&kq);
    264  1.165        ad 
    265  1.193        ad 	/* Destroy any lwpctl info. */
    266  1.193        ad 	if (p->p_lwpctl != NULL)
    267  1.193        ad 		lwp_ctl_exit();
    268  1.193        ad 
    269  1.165        ad 	/*
    270  1.165        ad 	 * Drain all remaining references that procfs, ptrace and others may
    271  1.165        ad 	 * have on the process.
    272  1.165        ad 	 */
    273  1.191        ad 	rw_enter(&p->p_reflock, RW_WRITER);
    274  1.112   nathanw 
    275  1.252  christos 	DPRINTF(("%s: %d.%d exiting.\n", __func__, p->p_pid, l->l_lid));
    276  1.107   thorpej 
    277  1.107   thorpej 	timers_free(p, TIMERS_ALL);
    278  1.100  gmcgarry #if defined(__HAVE_RAS)
    279  1.190        ad 	ras_purgeall();
    280  1.100  gmcgarry #endif
    281   1.24       cgd 
    282   1.24       cgd 	/*
    283  1.165        ad 	 * Close open files, release open-file table and free signal
    284  1.165        ad 	 * actions.  This may block!
    285   1.24       cgd 	 */
    286  1.200        ad 	fd_free();
    287  1.141        pk 	cwdfree(p->p_cwdi);
    288  1.165        ad 	p->p_cwdi = NULL;
    289   1.93  christos 	doexithooks(p);
    290  1.165        ad 	sigactsfree(p->p_sigacts);
    291  1.157        ad 
    292  1.157        ad 	/*
    293  1.164        ad 	 * Write out accounting data.
    294  1.157        ad 	 */
    295  1.154  christos 	(void)acct_process(l);
    296  1.157        ad 
    297   1.24       cgd #ifdef KTRACE
    298  1.145     perry 	/*
    299  1.157        ad 	 * Release trace file.
    300   1.24       cgd 	 */
    301  1.165        ad 	if (p->p_tracep != NULL) {
    302  1.185        ad 		mutex_enter(&ktrace_lock);
    303  1.165        ad 		ktrderef(p);
    304  1.185        ad 		mutex_exit(&ktrace_lock);
    305  1.165        ad 	}
    306   1.94  christos #endif
    307  1.157        ad 
    308  1.252  christos 	p->p_xexit = exitcode;
    309  1.252  christos 	p->p_xsig = signo;
    310  1.252  christos 
    311   1.24       cgd 	/*
    312   1.99      manu 	 * If emulation has process exit hook, call it now.
    313  1.158      manu 	 * Set the exit status now so that the exit hook has
    314  1.158      manu 	 * an opportunity to tweak it (COMPAT_LINUX requires
    315  1.158      manu 	 * this for thread group emulation)
    316   1.99      manu 	 */
    317   1.99      manu 	if (p->p_emul->e_proc_exit)
    318   1.99      manu 		(*p->p_emul->e_proc_exit)(p);
    319   1.99      manu 
    320  1.160   thorpej 	/*
    321  1.140        pk 	 * Free the VM resources we're still holding on to.
    322  1.140        pk 	 * We must do this from a valid thread because doing
    323  1.140        pk 	 * so may block. This frees vmspace, which we don't
    324  1.140        pk 	 * need anymore. The only remaining lwp is the one
    325  1.140        pk 	 * we run at this moment, nothing runs in userland
    326  1.140        pk 	 * anymore.
    327  1.140        pk 	 */
    328  1.271  christos 	ruspace(p);	/* Update our vm resource use */
    329  1.140        pk 	uvm_proc_exit(p);
    330  1.140        pk 
    331  1.140        pk 	/*
    332  1.165        ad 	 * Stop profiling.
    333  1.140        pk 	 */
    334  1.211        ad 	if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) {
    335  1.165        ad 		mutex_spin_enter(&p->p_stmutex);
    336  1.165        ad 		stopprofclock(p);
    337  1.165        ad 		mutex_spin_exit(&p->p_stmutex);
    338  1.165        ad 	}
    339  1.140        pk 
    340  1.140        pk 	/*
    341  1.214      yamt 	 * If parent is waiting for us to exit or exec, PL_PPWAIT is set; we
    342  1.165        ad 	 * wake up the parent early to avoid deadlock.  We can do this once
    343  1.165        ad 	 * the VM resources are released.
    344  1.140        pk 	 */
    345  1.203        ad 	mutex_enter(proc_lock);
    346  1.211        ad 	if (p->p_lflag & PL_PPWAIT) {
    347  1.276     kamil 		lwp_t *lp;
    348  1.276     kamil 
    349  1.240  christos 		l->l_lwpctl = NULL; /* was on loan from blocked parent */
    350  1.240  christos 		p->p_lflag &= ~PL_PPWAIT;
    351  1.276     kamil 
    352  1.276     kamil 		lp = p->p_vforklwp;
    353  1.276     kamil 		p->p_vforklwp = NULL;
    354  1.276     kamil 		lp->l_vforkwaiting = false;
    355  1.276     kamil 		cv_broadcast(&lp->l_waitcv);
    356  1.165        ad 	}
    357  1.165        ad 
    358  1.165        ad 	if (SESS_LEADER(p)) {
    359  1.165        ad 		struct vnode *vprele = NULL, *vprevoke = NULL;
    360  1.165        ad 		struct session *sp = p->p_session;
    361  1.165        ad 		struct tty *tp;
    362  1.165        ad 
    363  1.165        ad 		if (sp->s_ttyvp) {
    364  1.165        ad 			/*
    365  1.165        ad 			 * Controlling process.
    366  1.165        ad 			 * Signal foreground pgrp,
    367  1.165        ad 			 * drain controlling terminal
    368  1.165        ad 			 * and revoke access to controlling terminal.
    369  1.165        ad 			 */
    370  1.165        ad 			tp = sp->s_ttyp;
    371  1.192        ad 			mutex_spin_enter(&tty_lock);
    372  1.165        ad 			if (tp->t_session == sp) {
    373  1.165        ad 				/* we can't guarantee the revoke will do this */
    374  1.201        ad 				pgrp = tp->t_pgrp;
    375  1.165        ad 				tp->t_pgrp = NULL;
    376  1.165        ad 				tp->t_session = NULL;
    377  1.192        ad 				mutex_spin_exit(&tty_lock);
    378  1.201        ad 				if (pgrp != NULL) {
    379  1.201        ad 					pgsignal(pgrp, SIGHUP, 1);
    380  1.201        ad 				}
    381  1.203        ad 				mutex_exit(proc_lock);
    382  1.165        ad 				(void) ttywait(tp);
    383  1.203        ad 				mutex_enter(proc_lock);
    384  1.165        ad 
    385  1.198        ad 				/* The tty could have been revoked. */
    386  1.165        ad 				vprevoke = sp->s_ttyvp;
    387  1.192        ad 			} else
    388  1.192        ad 				mutex_spin_exit(&tty_lock);
    389  1.165        ad 			vprele = sp->s_ttyvp;
    390  1.165        ad 			sp->s_ttyvp = NULL;
    391  1.165        ad 			/*
    392  1.165        ad 			 * s_ttyp is not zero'd; we use this to indicate
    393  1.165        ad 			 * that the session once had a controlling terminal.
    394  1.165        ad 			 * (for logging and informational purposes)
    395  1.165        ad 			 */
    396  1.165        ad 		}
    397  1.165        ad 		sp->s_leader = NULL;
    398  1.140        pk 
    399  1.165        ad 		if (vprevoke != NULL || vprele != NULL) {
    400  1.198        ad 			if (vprevoke != NULL) {
    401  1.220     rmind 				/* Releases proc_lock. */
    402  1.220     rmind 				proc_sessrele(sp);
    403  1.165        ad 				VOP_REVOKE(vprevoke, REVOKEALL);
    404  1.198        ad 			} else
    405  1.203        ad 				mutex_exit(proc_lock);
    406  1.165        ad 			if (vprele != NULL)
    407  1.165        ad 				vrele(vprele);
    408  1.203        ad 			mutex_enter(proc_lock);
    409  1.165        ad 		}
    410  1.165        ad 	}
    411  1.165        ad 	fixjobc(p, p->p_pgrp, 0);
    412  1.140        pk 
    413  1.274   hannken 	/* Release fstrans private data. */
    414  1.274   hannken 	fstrans_lwp_dtor(l);
    415  1.274   hannken 
    416  1.140        pk 	/*
    417  1.171        ad 	 * Finalize the last LWP's specificdata, as well as the
    418  1.171        ad 	 * specificdata for the proc itself.
    419  1.171        ad 	 */
    420  1.171        ad 	lwp_finispecific(l);
    421  1.171        ad 	proc_finispecific(p);
    422  1.171        ad 
    423  1.171        ad 	/*
    424  1.140        pk 	 * Notify interested parties of our demise.
    425  1.140        pk 	 */
    426  1.140        pk 	KNOTE(&p->p_klist, NOTE_EXIT);
    427  1.140        pk 
    428  1.245  christos 	SDT_PROBE(proc, kernel, , exit,
    429  1.252  christos 		((p->p_sflag & PS_COREDUMP) ? CLD_DUMPED :
    430  1.252  christos 		 (p->p_xsig ? CLD_KILLED : CLD_EXITED)),
    431  1.226    darran 		0,0,0,0);
    432  1.226    darran 
    433  1.140        pk 	/*
    434  1.128       dsl 	 * Reset p_opptr pointer of all former children which got
    435  1.128       dsl 	 * traced by another process and were reparented. We reset
    436  1.128       dsl 	 * it to NULL here; the trace detach code then reparents
    437  1.128       dsl 	 * the child to initproc. We only check allproc list, since
    438  1.128       dsl 	 * eventual former children on zombproc list won't reference
    439  1.128       dsl 	 * p_opptr anymore.
    440  1.128       dsl 	 */
    441  1.208        ad 	if (__predict_false(p->p_slflag & PSL_CHTRACED)) {
    442  1.241  riastrad 		struct proc *q;
    443  1.143      yamt 		PROCLIST_FOREACH(q, &allproc) {
    444  1.128       dsl 			if (q->p_opptr == p)
    445  1.128       dsl 				q->p_opptr = NULL;
    446  1.128       dsl 		}
    447  1.262  christos 		PROCLIST_FOREACH(q, &zombproc) {
    448  1.262  christos 			if (q->p_opptr == p)
    449  1.262  christos 				q->p_opptr = NULL;
    450  1.262  christos 		}
    451  1.128       dsl 	}
    452  1.128       dsl 
    453  1.128       dsl 	/*
    454   1.72   thorpej 	 * Give orphaned children to init(8).
    455   1.72   thorpej 	 */
    456  1.241  riastrad 	child = LIST_FIRST(&p->p_children);
    457  1.241  riastrad 	wakeinit = (child != NULL);
    458  1.241  riastrad 	for (; child != NULL; child = next_child) {
    459  1.241  riastrad 		next_child = LIST_NEXT(child, p_sibling);
    460  1.104  jdolecek 
    461   1.24       cgd 		/*
    462  1.104  jdolecek 		 * Traced processes are killed since their existence
    463  1.104  jdolecek 		 * means someone is screwing up. Since we reset the
    464  1.104  jdolecek 		 * trace flags, the logic in sys_wait4() would not be
    465  1.104  jdolecek 		 * triggered to reparent the process to its
    466  1.106  jdolecek 		 * original parent, so we must do this here.
    467   1.24       cgd 		 */
    468  1.241  riastrad 		if (__predict_false(child->p_slflag & PSL_TRACED)) {
    469  1.204        ad 			mutex_enter(p->p_lock);
    470  1.241  riastrad 			child->p_slflag &=
    471  1.269     kamil 			    ~(PSL_TRACED|PSL_SYSCALL);
    472  1.204        ad 			mutex_exit(p->p_lock);
    473  1.241  riastrad 			if (child->p_opptr != child->p_pptr) {
    474  1.241  riastrad 				struct proc *t = child->p_opptr;
    475  1.241  riastrad 				proc_reparent(child, t ? t : initproc);
    476  1.241  riastrad 				child->p_opptr = NULL;
    477  1.105  jdolecek 			} else
    478  1.241  riastrad 				proc_reparent(child, initproc);
    479  1.241  riastrad 			killproc(child, "orphaned traced process");
    480  1.165        ad 		} else
    481  1.241  riastrad 			proc_reparent(child, initproc);
    482   1.24       cgd 	}
    483  1.115       dsl 
    484  1.115       dsl 	/*
    485  1.165        ad 	 * Move proc from allproc to zombproc, it's now nearly ready to be
    486  1.165        ad 	 * collected by parent.
    487  1.115       dsl 	 */
    488  1.165        ad 	LIST_REMOVE(l, l_list);
    489  1.115       dsl 	LIST_REMOVE(p, p_list);
    490  1.115       dsl 	LIST_INSERT_HEAD(&zombproc, p, p_list);
    491  1.133  jdolecek 
    492  1.165        ad 	/*
    493  1.165        ad 	 * Mark the process as dead.  We must do this before we signal
    494  1.165        ad 	 * the parent.
    495  1.165        ad 	 */
    496  1.165        ad 	p->p_stat = SDEAD;
    497  1.133  jdolecek 
    498  1.133  jdolecek 	/* Put in front of parent's sibling list for parent to collect it */
    499  1.241  riastrad 	old_parent = p->p_pptr;
    500  1.241  riastrad 	old_parent->p_nstopchild++;
    501  1.241  riastrad 	if (LIST_FIRST(&old_parent->p_children) != p) {
    502  1.133  jdolecek 		/* Put child where it can be found quickly */
    503  1.133  jdolecek 		LIST_REMOVE(p, p_sibling);
    504  1.241  riastrad 		LIST_INSERT_HEAD(&old_parent->p_children, p, p_sibling);
    505  1.133  jdolecek 	}
    506  1.133  jdolecek 
    507   1.59  christos 	/*
    508   1.59  christos 	 * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
    509   1.59  christos 	 * flag set, notify init instead (and hope it will handle
    510   1.59  christos 	 * this situation).
    511   1.59  christos 	 */
    512  1.241  riastrad 	if (old_parent->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
    513   1.59  christos 		proc_reparent(p, initproc);
    514  1.165        ad 		wakeinit = 1;
    515  1.133  jdolecek 
    516   1.59  christos 		/*
    517   1.59  christos 		 * If this was the last child of our parent, notify
    518   1.59  christos 		 * parent, so in case he was wait(2)ing, he will
    519   1.59  christos 		 * continue.
    520   1.59  christos 		 */
    521  1.241  riastrad 		if (LIST_FIRST(&old_parent->p_children) == NULL)
    522  1.241  riastrad 			cv_broadcast(&old_parent->p_waitcv);
    523   1.59  christos 	}
    524   1.24       cgd 
    525  1.140        pk 	/* Reload parent pointer, since p may have been reparented above */
    526  1.241  riastrad 	new_parent = p->p_pptr;
    527  1.140        pk 
    528  1.269     kamil 	if (__predict_false(p->p_exitsig != 0)) {
    529  1.241  riastrad 		exit_psignal(p, new_parent, &ksi);
    530  1.241  riastrad 		kpsignal(new_parent, &ksi, NULL);
    531  1.140        pk 	}
    532  1.140        pk 
    533  1.175       dsl 	/* Calculate the final rusage info.  */
    534  1.175       dsl 	calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime,
    535  1.175       dsl 	    NULL, NULL);
    536  1.140        pk 
    537  1.165        ad 	if (wakeinit)
    538  1.184        ad 		cv_broadcast(&initproc->p_waitcv);
    539  1.165        ad 
    540  1.187        ad 	callout_destroy(&l->l_timeout_ch);
    541  1.183        ad 
    542  1.165        ad 	/*
    543  1.234      matt 	 * Release any PCU resources before becoming a zombie.
    544  1.234      matt 	 */
    545  1.234      matt 	pcu_discard_all(l);
    546  1.234      matt 
    547  1.204        ad 	mutex_enter(p->p_lock);
    548  1.244  christos 	/* Free the linux lwp id */
    549  1.244  christos 	if ((l->l_pflag & LP_PIDLID) != 0 && l->l_lid != p->p_pid)
    550  1.244  christos 		proc_free_pid(l->l_lid);
    551  1.165        ad 	lwp_drainrefs(l);
    552  1.165        ad 	lwp_lock(l);
    553  1.165        ad 	l->l_prflag &= ~LPR_DETACHED;
    554  1.165        ad 	l->l_stat = LSZOMB;
    555  1.165        ad 	lwp_unlock(l);
    556  1.165        ad 	KASSERT(curlwp == l);
    557  1.165        ad 	KASSERT(p->p_nrlwps == 1);
    558  1.165        ad 	KASSERT(p->p_nlwps == 1);
    559  1.165        ad 	p->p_stat = SZOMB;
    560  1.165        ad 	p->p_nrlwps--;
    561  1.165        ad 	p->p_nzlwps++;
    562  1.165        ad 	p->p_ndlwps = 0;
    563  1.204        ad 	mutex_exit(p->p_lock);
    564  1.140        pk 
    565  1.165        ad 	/*
    566  1.165        ad 	 * Signal the parent to collect us, and drop the proclist lock.
    567  1.191        ad 	 * Drop debugger/procfs lock; no new references can be gained.
    568  1.165        ad 	 */
    569  1.184        ad 	cv_broadcast(&p->p_pptr->p_waitcv);
    570  1.221      yamt 	rw_exit(&p->p_reflock);
    571  1.203        ad 	mutex_exit(proc_lock);
    572  1.140        pk 
    573  1.165        ad 	/*
    574  1.165        ad 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
    575  1.165        ad 	 */
    576  1.157        ad 
    577  1.165        ad 	/*
    578  1.165        ad 	 * Give machine-dependent code a chance to free any MD LWP
    579  1.165        ad 	 * resources.  This must be done before uvm_lwp_exit(), in
    580  1.165        ad 	 * case these resources are in the PCB.
    581  1.165        ad 	 */
    582  1.165        ad 	cpu_lwp_free(l, 1);
    583  1.238    martin 
    584  1.279        ad 	/* For the LW_RUNNING check in lwp_free(). */
    585  1.279        ad 	membar_exit();
    586  1.133  jdolecek 
    587  1.279        ad 	/* Switch away into oblivion. */
    588  1.279        ad 	lwp_lock(l);
    589  1.279        ad 	spc_lock(l->l_cpu);
    590  1.279        ad 	mi_switch(l);
    591  1.279        ad 	panic("exit1");
    592  1.107   thorpej }
    593  1.107   thorpej 
    594  1.107   thorpej void
    595  1.107   thorpej exit_lwps(struct lwp *l)
    596  1.107   thorpej {
    597  1.242     rmind 	proc_t *p = l->l_proc;
    598  1.242     rmind 	lwp_t *l2;
    599  1.165        ad 
    600  1.242     rmind retry:
    601  1.242     rmind 	KASSERT(mutex_owned(p->p_lock));
    602  1.107   thorpej 
    603  1.107   thorpej 	/*
    604  1.107   thorpej 	 * Interrupt LWPs in interruptable sleep, unsuspend suspended
    605  1.165        ad 	 * LWPs and then wait for everyone else to finish.
    606  1.107   thorpej 	 */
    607  1.107   thorpej 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    608  1.165        ad 		if (l2 == l)
    609  1.165        ad 			continue;
    610  1.165        ad 		lwp_lock(l2);
    611  1.167     pavel 		l2->l_flag |= LW_WEXIT;
    612  1.167     pavel 		if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
    613  1.118      fvdl 		    l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
    614  1.277     kamil 			l2->l_flag &= ~LW_DBGSUSPEND;
    615  1.165        ad 		    	/* setrunnable() will release the lock. */
    616  1.107   thorpej 			setrunnable(l2);
    617  1.165        ad 			continue;
    618  1.107   thorpej 		}
    619  1.165        ad 		lwp_unlock(l2);
    620  1.107   thorpej 	}
    621  1.242     rmind 
    622  1.242     rmind 	/*
    623  1.242     rmind 	 * Wait for every LWP to exit.  Note: LWPs can get suspended/slept
    624  1.242     rmind 	 * behind us or there may even be new LWPs created.  Therefore, a
    625  1.242     rmind 	 * full retry is required on error.
    626  1.242     rmind 	 */
    627  1.107   thorpej 	while (p->p_nlwps > 1) {
    628  1.242     rmind 		if (lwp_wait(l, 0, NULL, true)) {
    629  1.150      yamt 			goto retry;
    630  1.150      yamt 		}
    631  1.145     perry 	}
    632  1.107   thorpej 
    633  1.181      yamt 	KASSERT(p->p_nlwps == 1);
    634   1.24       cgd }
    635   1.24       cgd 
    636  1.259     skrll int
    637  1.249  christos do_sys_waitid(idtype_t idtype, id_t id, int *pid, int *status, int options,
    638  1.249  christos     struct wrusage *wru, siginfo_t *si)
    639  1.177       dsl {
    640  1.225     rmind 	proc_t *child;
    641  1.225     rmind 	int error;
    642  1.177       dsl 
    643  1.260     skrll 
    644  1.249  christos 	if (wru != NULL)
    645  1.249  christos 		memset(wru, 0, sizeof(*wru));
    646  1.249  christos 	if (si != NULL)
    647  1.249  christos 		memset(si, 0, sizeof(*si));
    648  1.249  christos 
    649  1.203        ad 	mutex_enter(proc_lock);
    650  1.255  christos 	error = find_stopped_child(curproc, idtype, id, options, &child,
    651  1.249  christos 	    wru, si);
    652  1.177       dsl 	if (child == NULL) {
    653  1.203        ad 		mutex_exit(proc_lock);
    654  1.177       dsl 		*pid = 0;
    655  1.273      maxv 		*status = 0;
    656  1.177       dsl 		return error;
    657  1.177       dsl 	}
    658  1.177       dsl 	*pid = child->p_pid;
    659  1.177       dsl 
    660  1.177       dsl 	if (child->p_stat == SZOMB) {
    661  1.255  christos 		/* Child is exiting */
    662  1.255  christos 		*status = P_WAITSTATUS(child);
    663  1.203        ad 		/* proc_free() will release the proc_lock. */
    664  1.224     rmind 		if (options & WNOWAIT) {
    665  1.203        ad 			mutex_exit(proc_lock);
    666  1.224     rmind 		} else {
    667  1.249  christos 			proc_free(child, wru);
    668  1.177       dsl 		}
    669  1.177       dsl 	} else {
    670  1.261  christos 		/* Don't mark SIGCONT if we are being stopped */
    671  1.261  christos 		*status = (child->p_xsig == SIGCONT && child->p_stat != SSTOP) ?
    672  1.261  christos 		    W_CONTCODE() : W_STOPCODE(child->p_xsig);
    673  1.224     rmind 		mutex_exit(proc_lock);
    674  1.177       dsl 	}
    675  1.177       dsl 	return 0;
    676  1.177       dsl }
    677  1.177       dsl 
    678  1.177       dsl int
    679  1.249  christos do_sys_wait(int *pid, int *status, int options, struct rusage *ru)
    680  1.249  christos {
    681  1.249  christos 	idtype_t idtype;
    682  1.249  christos 	id_t id;
    683  1.249  christos 	int ret;
    684  1.249  christos 	struct wrusage wru;
    685  1.249  christos 
    686  1.249  christos 	/*
    687  1.249  christos 	 * Translate the special pid values into the (idtype, pid)
    688  1.249  christos 	 * pair for wait6. The WAIT_MYPGRP case is handled by
    689  1.249  christos 	 * find_stopped_child() on its own.
    690  1.249  christos 	 */
    691  1.249  christos 	if (*pid == WAIT_ANY) {
    692  1.249  christos 		idtype = P_ALL;
    693  1.249  christos 		id = 0;
    694  1.249  christos 	} else if (*pid < 0) {
    695  1.249  christos 		idtype = P_PGID;
    696  1.249  christos 		id = (id_t)-*pid;
    697  1.249  christos 	} else {
    698  1.249  christos 		idtype = P_PID;
    699  1.249  christos 		id = (id_t)*pid;
    700  1.249  christos 	}
    701  1.249  christos 	options |= WEXITED | WTRAPPED;
    702  1.249  christos 	ret = do_sys_waitid(idtype, id, pid, status, options, ru ? &wru : NULL,
    703  1.249  christos 	    NULL);
    704  1.249  christos 	if (ru)
    705  1.249  christos 		*ru = wru.wru_self;
    706  1.249  christos 	return ret;
    707  1.249  christos }
    708  1.249  christos 
    709  1.249  christos int
    710  1.225     rmind sys___wait450(struct lwp *l, const struct sys___wait450_args *uap,
    711  1.225     rmind     register_t *retval)
    712   1.31   thorpej {
    713  1.195       dsl 	/* {
    714   1.89     lukem 		syscallarg(int)			pid;
    715   1.89     lukem 		syscallarg(int *)		status;
    716   1.89     lukem 		syscallarg(int)			options;
    717   1.89     lukem 		syscallarg(struct rusage *)	rusage;
    718  1.195       dsl 	} */
    719  1.225     rmind 	int error, status, pid = SCARG(uap, pid);
    720  1.225     rmind 	struct rusage ru;
    721   1.24       cgd 
    722  1.225     rmind 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
    723  1.225     rmind 	    SCARG(uap, rusage) != NULL ? &ru : NULL);
    724  1.107   thorpej 
    725  1.195       dsl 	retval[0] = pid;
    726  1.225     rmind 	if (pid == 0) {
    727  1.109       dsl 		return error;
    728  1.225     rmind 	}
    729  1.225     rmind 	if (SCARG(uap, status)) {
    730  1.225     rmind 		error = copyout(&status, SCARG(uap, status), sizeof(status));
    731  1.225     rmind 	}
    732  1.225     rmind 	if (SCARG(uap, rusage) && error == 0) {
    733  1.177       dsl 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
    734  1.225     rmind 	}
    735  1.177       dsl 	return error;
    736  1.109       dsl }
    737  1.109       dsl 
    738  1.249  christos int
    739  1.249  christos sys_wait6(struct lwp *l, const struct sys_wait6_args *uap, register_t *retval)
    740  1.249  christos {
    741  1.249  christos 	/* {
    742  1.249  christos 		syscallarg(idtype_t)		idtype;
    743  1.249  christos 		syscallarg(id_t)		id;
    744  1.249  christos 		syscallarg(int *)		status;
    745  1.249  christos 		syscallarg(int)			options;
    746  1.249  christos 		syscallarg(struct wrusage *)	wru;
    747  1.249  christos 		syscallarg(siginfo_t *)		si;
    748  1.249  christos 	} */
    749  1.249  christos 	struct wrusage wru, *wrup;
    750  1.249  christos 	siginfo_t si, *sip;
    751  1.249  christos 	idtype_t idtype;
    752  1.249  christos 	int pid;
    753  1.249  christos 	id_t id;
    754  1.249  christos 	int error, status;
    755  1.249  christos 
    756  1.249  christos 	idtype = SCARG(uap, idtype);
    757  1.249  christos 	id = SCARG(uap, id);
    758  1.249  christos 
    759  1.249  christos 	if (SCARG(uap, wru) != NULL)
    760  1.249  christos 		wrup = &wru;
    761  1.249  christos 	else
    762  1.249  christos 		wrup = NULL;
    763  1.249  christos 
    764  1.249  christos 	if (SCARG(uap, info) != NULL)
    765  1.249  christos 		sip = &si;
    766  1.249  christos 	else
    767  1.249  christos 		sip = NULL;
    768  1.249  christos 
    769  1.249  christos 	/*
    770  1.249  christos 	 *  We expect all callers of wait6() to know about WEXITED and
    771  1.249  christos 	 *  WTRAPPED.
    772  1.249  christos 	 */
    773  1.249  christos 	error = do_sys_waitid(idtype, id, &pid, &status, SCARG(uap, options),
    774  1.249  christos 	    wrup, sip);
    775  1.249  christos 
    776  1.257  christos 	retval[0] = pid; 	/* tell userland who it was */
    777  1.257  christos 
    778  1.257  christos #if 0
    779  1.260     skrll 	/*
    780  1.257  christos 	 * should we copyout if there was no process, hence no useful data?
    781  1.257  christos 	 * We don't for an old sytle wait4() (etc) but I believe
    782  1.257  christos 	 * FreeBSD does for wait6(), so a tossup...  Go with FreeBSD for now.
    783  1.257  christos 	 */
    784  1.257  christos 	if (pid == 0)
    785  1.257  christos 		return error;
    786  1.257  christos #endif
    787  1.257  christos 
    788  1.249  christos 	if (SCARG(uap, status) != NULL && error == 0)
    789  1.249  christos 		error = copyout(&status, SCARG(uap, status), sizeof(status));
    790  1.249  christos 	if (SCARG(uap, wru) != NULL && error == 0)
    791  1.249  christos 		error = copyout(&wru, SCARG(uap, wru), sizeof(wru));
    792  1.249  christos 	if (SCARG(uap, info) != NULL && error == 0)
    793  1.249  christos 		error = copyout(&si, SCARG(uap, info), sizeof(si));
    794  1.249  christos 	return error;
    795  1.249  christos }
    796  1.249  christos 
    797  1.249  christos 
    798  1.251  christos /*
    799  1.251  christos  * Find a process that matches the provided criteria, and fill siginfo
    800  1.251  christos  * and resources if found.
    801  1.251  christos  * Returns:
    802  1.251  christos  *	-1: 	Not found, abort early
    803  1.251  christos  *	 0:	Not matched
    804  1.251  christos  *	 1:	Matched, there might be more matches
    805  1.251  christos  *	 2:	This is the only match
    806  1.251  christos  */
    807  1.249  christos static int
    808  1.265       kre match_process(const struct proc *pp, struct proc **q, idtype_t idtype, id_t id,
    809  1.249  christos     int options, struct wrusage *wrusage, siginfo_t *siginfo)
    810  1.249  christos {
    811  1.249  christos 	struct rusage *rup;
    812  1.249  christos 	struct proc *p = *q;
    813  1.251  christos 	int rv = 1;
    814  1.249  christos 
    815  1.249  christos 	mutex_enter(p->p_lock);
    816  1.249  christos 	switch (idtype) {
    817  1.249  christos 	case P_ALL:
    818  1.249  christos 		break;
    819  1.249  christos 	case P_PID:
    820  1.249  christos 		if (p->p_pid != (pid_t)id) {
    821  1.249  christos 			mutex_exit(p->p_lock);
    822  1.249  christos 			p = *q = proc_find_raw((pid_t)id);
    823  1.249  christos 			if (p == NULL || p->p_stat == SIDL || p->p_pptr != pp) {
    824  1.249  christos 				*q = NULL;
    825  1.249  christos 				return -1;
    826  1.249  christos 			}
    827  1.249  christos 			mutex_enter(p->p_lock);
    828  1.249  christos 		}
    829  1.251  christos 		rv++;
    830  1.249  christos 		break;
    831  1.249  christos 	case P_PGID:
    832  1.249  christos 		if (p->p_pgid != (pid_t)id)
    833  1.249  christos 			goto out;
    834  1.249  christos 		break;
    835  1.249  christos 	case P_SID:
    836  1.249  christos 		if (p->p_session->s_sid != (pid_t)id)
    837  1.249  christos 			goto out;
    838  1.249  christos 		break;
    839  1.249  christos 	case P_UID:
    840  1.250  christos 		if (kauth_cred_geteuid(p->p_cred) != (uid_t)id)
    841  1.249  christos 			goto out;
    842  1.249  christos 		break;
    843  1.249  christos 	case P_GID:
    844  1.250  christos 		if (kauth_cred_getegid(p->p_cred) != (gid_t)id)
    845  1.249  christos 			goto out;
    846  1.249  christos 		break;
    847  1.249  christos 	case P_CID:
    848  1.249  christos 	case P_PSETID:
    849  1.249  christos 	case P_CPUID:
    850  1.249  christos 		/* XXX: Implement me */
    851  1.249  christos 	default:
    852  1.249  christos 	out:
    853  1.249  christos 		mutex_exit(p->p_lock);
    854  1.249  christos 		return 0;
    855  1.249  christos 	}
    856  1.249  christos 
    857  1.249  christos 	if ((options & WEXITED) == 0 && p->p_stat == SZOMB)
    858  1.249  christos 		goto out;
    859  1.249  christos 
    860  1.249  christos 	if (siginfo != NULL) {
    861  1.249  christos 		siginfo->si_errno = 0;
    862  1.249  christos 
    863  1.249  christos 		/*
    864  1.249  christos 		 * SUSv4 requires that the si_signo value is always
    865  1.249  christos 		 * SIGCHLD. Obey it despite the rfork(2) interface
    866  1.249  christos 		 * allows to request other signal for child exit
    867  1.249  christos 		 * notification.
    868  1.249  christos 		 */
    869  1.249  christos 		siginfo->si_signo = SIGCHLD;
    870  1.249  christos 
    871  1.249  christos 		/*
    872  1.249  christos 		 *  This is still a rough estimate.  We will fix the
    873  1.249  christos 		 *  cases TRAPPED, STOPPED, and CONTINUED later.
    874  1.249  christos 		 */
    875  1.252  christos 		if (p->p_sflag & PS_COREDUMP) {
    876  1.249  christos 			siginfo->si_code = CLD_DUMPED;
    877  1.252  christos 			siginfo->si_status = p->p_xsig;
    878  1.252  christos 		} else if (p->p_xsig) {
    879  1.249  christos 			siginfo->si_code = CLD_KILLED;
    880  1.252  christos 			siginfo->si_status = p->p_xsig;
    881  1.249  christos 		} else {
    882  1.249  christos 			siginfo->si_code = CLD_EXITED;
    883  1.252  christos 			siginfo->si_status = p->p_xexit;
    884  1.249  christos 		}
    885  1.249  christos 
    886  1.249  christos 		siginfo->si_pid = p->p_pid;
    887  1.250  christos 		siginfo->si_uid = kauth_cred_geteuid(p->p_cred);
    888  1.250  christos 		siginfo->si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
    889  1.250  christos 		siginfo->si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
    890  1.249  christos 	}
    891  1.249  christos 
    892  1.249  christos 	/*
    893  1.249  christos 	 * There should be no reason to limit resources usage info to
    894  1.249  christos 	 * exited processes only.  A snapshot about any resources used
    895  1.249  christos 	 * by a stopped process may be exactly what is needed.
    896  1.249  christos 	 */
    897  1.249  christos 	if (wrusage != NULL) {
    898  1.249  christos 		rup = &wrusage->wru_self;
    899  1.249  christos 		*rup = p->p_stats->p_ru;
    900  1.249  christos 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
    901  1.249  christos 
    902  1.249  christos 		rup = &wrusage->wru_children;
    903  1.249  christos 		*rup = p->p_stats->p_cru;
    904  1.249  christos 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
    905  1.249  christos 	}
    906  1.249  christos 
    907  1.249  christos 	mutex_exit(p->p_lock);
    908  1.251  christos 	return rv;
    909  1.249  christos }
    910  1.249  christos 
    911  1.109       dsl /*
    912  1.265       kre  * Determine if there are existing processes being debugged
    913  1.265       kre  * that used to be (and sometime later will be again) children
    914  1.265       kre  * of a specific parent (while matching wait criteria)
    915  1.265       kre  */
    916  1.265       kre static bool
    917  1.265       kre debugged_child_exists(idtype_t idtype, id_t id, int options, siginfo_t *si,
    918  1.265       kre     const struct proc *parent)
    919  1.265       kre {
    920  1.265       kre 	struct proc *pp;
    921  1.265       kre 
    922  1.265       kre 	/*
    923  1.265       kre 	 * If we are searching for a specific pid, we can optimise a little
    924  1.265       kre 	 */
    925  1.265       kre 	if (idtype == P_PID) {
    926  1.265       kre 		/*
    927  1.265       kre 		 * Check the specific process to see if its real parent is us
    928  1.265       kre 		 */
    929  1.265       kre 		pp = proc_find_raw((pid_t)id);
    930  1.265       kre 		if (pp != NULL && pp->p_stat != SIDL && pp->p_opptr == parent) {
    931  1.265       kre 			/*
    932  1.265       kre 			 * using P_ALL here avoids match_process() doing the
    933  1.265       kre 			 * same work that we just did, but incorrectly for
    934  1.265       kre 			 * this scenario.
    935  1.265       kre 			 */
    936  1.265       kre 			if (match_process(parent, &pp, P_ALL, id, options,
    937  1.265       kre 			    NULL, si))
    938  1.265       kre 				return true;
    939  1.265       kre 		}
    940  1.265       kre 		return false;
    941  1.265       kre 	}
    942  1.265       kre 
    943  1.265       kre 	/*
    944  1.265       kre 	 * For the hard cases, just look everywhere to see if some
    945  1.265       kre 	 * stolen (reparented) process is really our lost child.
    946  1.265       kre 	 * Then check if that process could satisfy the wait conditions.
    947  1.265       kre 	 */
    948  1.265       kre 
    949  1.265       kre 	/*
    950  1.265       kre 	 * XXX inefficient, but hopefully fairly rare.
    951  1.265       kre 	 * XXX should really use a list of reparented processes.
    952  1.265       kre 	 */
    953  1.265       kre 	PROCLIST_FOREACH(pp, &allproc) {
    954  1.265       kre 		if (pp->p_stat == SIDL)		/* XXX impossible ?? */
    955  1.265       kre 			continue;
    956  1.265       kre 		if (pp->p_opptr == parent &&
    957  1.265       kre 		    match_process(parent, &pp, idtype, id, options, NULL, si))
    958  1.265       kre 			return true;
    959  1.265       kre 	}
    960  1.265       kre 	PROCLIST_FOREACH(pp, &zombproc) {
    961  1.265       kre 		if (pp->p_stat == SIDL)		/* XXX impossible ?? */
    962  1.265       kre 			continue;
    963  1.265       kre 		if (pp->p_opptr == parent &&
    964  1.265       kre 		    match_process(parent, &pp, idtype, id, options, NULL, si))
    965  1.265       kre 			return true;
    966  1.265       kre 	}
    967  1.265       kre 
    968  1.265       kre 	return false;
    969  1.265       kre }
    970  1.265       kre 
    971  1.265       kre /*
    972  1.109       dsl  * Scan list of child processes for a child process that has stopped or
    973  1.109       dsl  * exited.  Used by sys_wait4 and 'compat' equivalents.
    974  1.165        ad  *
    975  1.203        ad  * Must be called with the proc_lock held, and may release while waiting.
    976  1.109       dsl  */
    977  1.177       dsl static int
    978  1.249  christos find_stopped_child(struct proc *parent, idtype_t idtype, id_t id, int options,
    979  1.255  christos     struct proc **child_p, struct wrusage *wru, siginfo_t *si)
    980  1.109       dsl {
    981  1.165        ad 	struct proc *child, *dead;
    982  1.265       kre 	int error;
    983   1.68   thorpej 
    984  1.203        ad 	KASSERT(mutex_owned(proc_lock));
    985  1.165        ad 
    986  1.266  christos 	if (options & ~WALLOPTS) {
    987  1.177       dsl 		*child_p = NULL;
    988  1.177       dsl 		return EINVAL;
    989  1.177       dsl 	}
    990  1.177       dsl 
    991  1.266  christos 	if ((options & WSELECTOPTS) == 0) {
    992  1.249  christos 		/*
    993  1.249  christos 		 * We will be unable to find any matching processes,
    994  1.249  christos 		 * because there are no known events to look for.
    995  1.249  christos 		 * Prefer to return error instead of blocking
    996  1.249  christos 		 * indefinitely.
    997  1.249  christos 		 */
    998  1.249  christos 		*child_p = NULL;
    999  1.249  christos 		return EINVAL;
   1000  1.249  christos 	}
   1001  1.249  christos 
   1002  1.249  christos 	if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
   1003  1.249  christos 		mutex_enter(parent->p_lock);
   1004  1.249  christos 		id = (id_t)parent->p_pgid;
   1005  1.249  christos 		mutex_exit(parent->p_lock);
   1006  1.249  christos 		idtype = P_PGID;
   1007  1.249  christos 	}
   1008  1.177       dsl 
   1009  1.120      yamt 	for (;;) {
   1010  1.128       dsl 		error = ECHILD;
   1011  1.165        ad 		dead = NULL;
   1012  1.165        ad 
   1013  1.109       dsl 		LIST_FOREACH(child, &parent->p_children, p_sibling) {
   1014  1.249  christos 			int rv = match_process(parent, &child, idtype, id,
   1015  1.249  christos 			    options, wru, si);
   1016  1.249  christos 			if (rv == -1)
   1017  1.249  christos 				break;
   1018  1.249  christos 			if (rv == 0)
   1019  1.165        ad 				continue;
   1020  1.165        ad 
   1021  1.109       dsl 			/*
   1022  1.109       dsl 			 * Wait for processes with p_exitsig != SIGCHLD
   1023  1.109       dsl 			 * processes only if WALTSIG is set; wait for
   1024  1.109       dsl 			 * processes with p_exitsig == SIGCHLD only
   1025  1.109       dsl 			 * if WALTSIG is clear.
   1026  1.109       dsl 			 */
   1027  1.109       dsl 			if (((options & WALLSIG) == 0) &&
   1028  1.109       dsl 			    (options & WALTSIG ? child->p_exitsig == SIGCHLD
   1029  1.128       dsl 						: P_EXITSIG(child) != SIGCHLD)){
   1030  1.251  christos 				if (rv == 2) {
   1031  1.251  christos 					child = NULL;
   1032  1.251  christos 					break;
   1033  1.251  christos 				}
   1034  1.109       dsl 				continue;
   1035  1.128       dsl 			}
   1036  1.109       dsl 
   1037  1.128       dsl 			error = 0;
   1038  1.165        ad 			if ((options & WNOZOMBIE) == 0) {
   1039  1.165        ad 				if (child->p_stat == SZOMB)
   1040  1.165        ad 					break;
   1041  1.165        ad 				if (child->p_stat == SDEAD) {
   1042  1.165        ad 					/*
   1043  1.165        ad 					 * We may occasionally arrive here
   1044  1.165        ad 					 * after receiving a signal, but
   1045  1.228     pooka 					 * immediately before the child
   1046  1.165        ad 					 * process is zombified.  The wait
   1047  1.165        ad 					 * will be short, so avoid returning
   1048  1.165        ad 					 * to userspace.
   1049  1.165        ad 					 */
   1050  1.165        ad 					dead = child;
   1051  1.165        ad 				}
   1052  1.165        ad 			}
   1053  1.109       dsl 
   1054  1.250  christos 			if ((options & WCONTINUED) != 0 &&
   1055  1.258  christos 			    child->p_xsig == SIGCONT &&
   1056  1.258  christos 			    (child->p_sflag & PS_CONTINUED)) {
   1057  1.250  christos 				if ((options & WNOWAIT) == 0) {
   1058  1.258  christos 					child->p_sflag &= ~PS_CONTINUED;
   1059  1.250  christos 					child->p_waited = 1;
   1060  1.250  christos 					parent->p_nstopchild--;
   1061  1.250  christos 				}
   1062  1.250  christos 				if (si) {
   1063  1.252  christos 					si->si_status = child->p_xsig;
   1064  1.250  christos 					si->si_code = CLD_CONTINUED;
   1065  1.250  christos 				}
   1066  1.250  christos 				break;
   1067  1.250  christos 			}
   1068  1.250  christos 
   1069  1.250  christos 			if ((options & (WTRAPPED|WSTOPPED)) != 0 &&
   1070  1.249  christos 			    child->p_stat == SSTOP &&
   1071  1.165        ad 			    child->p_waited == 0 &&
   1072  1.250  christos 			    ((child->p_slflag & PSL_TRACED) ||
   1073  1.250  christos 			    options & (WUNTRACED|WSTOPPED))) {
   1074  1.128       dsl 				if ((options & WNOWAIT) == 0) {
   1075  1.165        ad 					child->p_waited = 1;
   1076  1.128       dsl 					parent->p_nstopchild--;
   1077  1.128       dsl 				}
   1078  1.249  christos 				if (si) {
   1079  1.252  christos 					si->si_status = child->p_xsig;
   1080  1.260     skrll 					si->si_code =
   1081  1.250  christos 					    (child->p_slflag & PSL_TRACED) ?
   1082  1.250  christos 					    CLD_TRAPPED : CLD_STOPPED;
   1083  1.249  christos 				}
   1084  1.128       dsl 				break;
   1085  1.128       dsl 			}
   1086  1.251  christos 			if (parent->p_nstopchild == 0 || rv == 2) {
   1087  1.128       dsl 				child = NULL;
   1088  1.128       dsl 				break;
   1089   1.24       cgd 			}
   1090  1.109       dsl 		}
   1091  1.165        ad 
   1092  1.265       kre 		/*
   1093  1.265       kre 		 * If we found nothing, but we are the bereaved parent
   1094  1.265       kre 		 * of a stolen child, look and see if that child (or
   1095  1.265       kre 		 * one of them) meets our search criteria.   If so, then
   1096  1.265       kre 		 * we cannot succeed, but we can hang (wait...),
   1097  1.265       kre 		 * or if WNOHANG, return 0 instead of ECHILD
   1098  1.265       kre 		 */
   1099  1.265       kre 		if (child == NULL && error == ECHILD &&
   1100  1.265       kre 		    (parent->p_slflag & PSL_CHTRACED) &&
   1101  1.265       kre 		    debugged_child_exists(idtype, id, options, si, parent))
   1102  1.265       kre 			error = 0;
   1103  1.265       kre 
   1104  1.265       kre 		if (child != NULL || error != 0 ||
   1105  1.265       kre 		    ((options & WNOHANG) != 0 && dead == NULL)) {
   1106  1.128       dsl 			*child_p = child;
   1107  1.265       kre 			return error;
   1108  1.109       dsl 		}
   1109  1.165        ad 
   1110  1.165        ad 		/*
   1111  1.165        ad 		 * Wait for another child process to stop.
   1112  1.165        ad 		 */
   1113  1.203        ad 		error = cv_wait_sig(&parent->p_waitcv, proc_lock);
   1114  1.165        ad 
   1115  1.177       dsl 		if (error != 0) {
   1116  1.177       dsl 			*child_p = NULL;
   1117  1.109       dsl 			return error;
   1118  1.177       dsl 		}
   1119  1.109       dsl 	}
   1120  1.109       dsl }
   1121  1.109       dsl 
   1122  1.109       dsl /*
   1123  1.165        ad  * Free a process after parent has taken all the state info.  Must be called
   1124  1.169        ad  * with the proclist lock held, and will release before returning.
   1125  1.165        ad  *
   1126  1.165        ad  * *ru is returned to the caller, and must be freed by the caller.
   1127  1.109       dsl  */
   1128  1.178       dsl static void
   1129  1.249  christos proc_free(struct proc *p, struct wrusage *wru)
   1130  1.109       dsl {
   1131  1.219     rmind 	struct proc *parent = p->p_pptr;
   1132  1.165        ad 	struct lwp *l;
   1133  1.140        pk 	ksiginfo_t ksi;
   1134  1.182        ad 	kauth_cred_t cred1, cred2;
   1135  1.165        ad 	uid_t uid;
   1136   1.24       cgd 
   1137  1.203        ad 	KASSERT(mutex_owned(proc_lock));
   1138  1.165        ad 	KASSERT(p->p_nlwps == 1);
   1139  1.165        ad 	KASSERT(p->p_nzlwps == 1);
   1140  1.137      yamt 	KASSERT(p->p_nrlwps == 0);
   1141  1.165        ad 	KASSERT(p->p_stat == SZOMB);
   1142  1.137      yamt 
   1143  1.109       dsl 	/*
   1144  1.109       dsl 	 * If we got the child via ptrace(2) or procfs, and
   1145  1.109       dsl 	 * the parent is different (meaning the process was
   1146  1.109       dsl 	 * attached, rather than run as a child), then we need
   1147  1.109       dsl 	 * to give it back to the old parent, and send the
   1148  1.109       dsl 	 * parent the exit signal.  The rest of the cleanup
   1149  1.109       dsl 	 * will be done when the old parent waits on the child.
   1150  1.109       dsl 	 */
   1151  1.219     rmind 	if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) {
   1152  1.219     rmind 		mutex_enter(p->p_lock);
   1153  1.269     kamil 		p->p_slflag &= ~(PSL_TRACED|PSL_SYSCALL);
   1154  1.219     rmind 		mutex_exit(p->p_lock);
   1155  1.219     rmind 		parent = (p->p_opptr == NULL) ? initproc : p->p_opptr;
   1156  1.219     rmind 		proc_reparent(p, parent);
   1157  1.219     rmind 		p->p_opptr = NULL;
   1158  1.219     rmind 		if (p->p_exitsig != 0) {
   1159  1.219     rmind 			exit_psignal(p, parent, &ksi);
   1160  1.219     rmind 			kpsignal(parent, &ksi, NULL);
   1161  1.140        pk 		}
   1162  1.219     rmind 		cv_broadcast(&parent->p_waitcv);
   1163  1.219     rmind 		mutex_exit(proc_lock);
   1164  1.219     rmind 		return;
   1165  1.109       dsl 	}
   1166  1.109       dsl 
   1167  1.179      yamt 	sched_proc_exit(parent, p);
   1168  1.202        ad 
   1169  1.178       dsl 	/*
   1170  1.178       dsl 	 * Add child times of exiting process onto its own times.
   1171  1.178       dsl 	 * This cannot be done any earlier else it might get done twice.
   1172  1.178       dsl 	 */
   1173  1.202        ad 	l = LIST_FIRST(&p->p_lwps);
   1174  1.202        ad 	p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
   1175  1.202        ad 	p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw;
   1176  1.202        ad 	ruadd(&p->p_stats->p_ru, &l->l_ru);
   1177  1.178       dsl 	ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru);
   1178  1.175       dsl 	ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
   1179  1.249  christos 	if (wru != NULL) {
   1180  1.249  christos 		wru->wru_self = p->p_stats->p_ru;
   1181  1.249  christos 		wru->wru_children = p->p_stats->p_cru;
   1182  1.249  christos 	}
   1183  1.252  christos 	p->p_xsig = 0;
   1184  1.252  christos 	p->p_xexit = 0;
   1185  1.115       dsl 
   1186  1.115       dsl 	/*
   1187  1.260     skrll 	 * At this point we are going to start freeing the final resources.
   1188  1.165        ad 	 * If anyone tries to access the proc structure after here they will
   1189  1.165        ad 	 * get a shock - bits are missing.  Attempt to make it hard!  We
   1190  1.165        ad 	 * don't bother with any further locking past this point.
   1191  1.115       dsl 	 */
   1192  1.165        ad 	p->p_stat = SIDL;		/* not even a zombie any more */
   1193  1.165        ad 	LIST_REMOVE(p, p_list);	/* off zombproc */
   1194  1.219     rmind 	parent->p_nstopchild--;
   1195  1.165        ad 	LIST_REMOVE(p, p_sibling);
   1196  1.115       dsl 
   1197  1.188        ad 	/*
   1198  1.188        ad 	 * Let pid be reallocated.
   1199  1.188        ad 	 */
   1200  1.230       chs 	proc_free_pid(p->p_pid);
   1201  1.220     rmind 
   1202  1.220     rmind 	/*
   1203  1.220     rmind 	 * Unlink process from its process group.
   1204  1.220     rmind 	 * Releases the proc_lock.
   1205  1.220     rmind 	 */
   1206  1.220     rmind 	proc_leavepgrp(p);
   1207  1.182        ad 
   1208  1.109       dsl 	/*
   1209  1.188        ad 	 * Delay release until after lwp_free.
   1210  1.109       dsl 	 */
   1211  1.182        ad 	cred2 = l->l_cred;
   1212  1.182        ad 
   1213  1.182        ad 	/*
   1214  1.188        ad 	 * Free the last LWP's resources.
   1215  1.188        ad 	 *
   1216  1.188        ad 	 * lwp_free ensures the LWP is no longer running on another CPU.
   1217  1.182        ad 	 */
   1218  1.182        ad 	lwp_free(l, false, true);
   1219   1.56   thorpej 
   1220  1.165        ad 	/*
   1221  1.188        ad 	 * Now no one except us can reach the process p.
   1222  1.165        ad 	 */
   1223   1.35   mycroft 
   1224  1.109       dsl 	/*
   1225  1.109       dsl 	 * Decrement the count of procs running with this uid.
   1226  1.109       dsl 	 */
   1227  1.188        ad 	cred1 = p->p_cred;
   1228  1.188        ad 	uid = kauth_cred_getuid(cred1);
   1229  1.165        ad 	(void)chgproccnt(uid, -1);
   1230   1.24       cgd 
   1231  1.109       dsl 	/*
   1232  1.165        ad 	 * Release substructures.
   1233  1.109       dsl 	 */
   1234  1.188        ad 
   1235  1.233     rmind 	lim_free(p->p_limit);
   1236  1.188        ad 	pstatsfree(p->p_stats);
   1237  1.182        ad 	kauth_cred_free(cred1);
   1238  1.182        ad 	kauth_cred_free(cred2);
   1239  1.107   thorpej 
   1240  1.109       dsl 	/*
   1241  1.109       dsl 	 * Release reference to text vnode
   1242  1.109       dsl 	 */
   1243  1.188        ad 	if (p->p_textvp)
   1244  1.188        ad 		vrele(p->p_textvp);
   1245  1.270  christos 	kmem_strfree(p->p_path);
   1246  1.188        ad 
   1247  1.198        ad 	mutex_destroy(&p->p_auxlock);
   1248  1.204        ad 	mutex_obj_free(p->p_lock);
   1249  1.188        ad 	mutex_destroy(&p->p_stmutex);
   1250  1.188        ad 	cv_destroy(&p->p_waitcv);
   1251  1.188        ad 	cv_destroy(&p->p_lwpcv);
   1252  1.191        ad 	rw_destroy(&p->p_reflock);
   1253  1.188        ad 
   1254  1.196        ad 	proc_free_mem(p);
   1255   1.24       cgd }
   1256   1.24       cgd 
   1257   1.24       cgd /*
   1258  1.263  christos  * Change the parent of a process for tracing purposes.
   1259  1.263  christos  */
   1260  1.263  christos void
   1261  1.263  christos proc_changeparent(struct proc *t, struct proc *p)
   1262  1.263  christos {
   1263  1.263  christos 	SET(t->p_slflag, PSL_TRACED);
   1264  1.263  christos 	t->p_opptr = t->p_pptr;
   1265  1.263  christos 	if (t->p_pptr == p)
   1266  1.263  christos 		return;
   1267  1.263  christos 	struct proc *parent = t->p_pptr;
   1268  1.263  christos 
   1269  1.263  christos 	if (parent->p_lock < t->p_lock) {
   1270  1.263  christos 		if (!mutex_tryenter(parent->p_lock)) {
   1271  1.263  christos 			mutex_exit(t->p_lock);
   1272  1.263  christos 			mutex_enter(parent->p_lock);
   1273  1.263  christos 			mutex_enter(t->p_lock);
   1274  1.263  christos 		}
   1275  1.263  christos 	} else if (parent->p_lock > t->p_lock) {
   1276  1.263  christos 		mutex_enter(parent->p_lock);
   1277  1.263  christos 	}
   1278  1.263  christos 	parent->p_slflag |= PSL_CHTRACED;
   1279  1.263  christos 	proc_reparent(t, p);
   1280  1.263  christos 	if (parent->p_lock != t->p_lock)
   1281  1.263  christos 		mutex_exit(parent->p_lock);
   1282  1.263  christos }
   1283  1.263  christos 
   1284  1.263  christos /*
   1285   1.24       cgd  * make process 'parent' the new parent of process 'child'.
   1286  1.128       dsl  *
   1287  1.203        ad  * Must be called with proc_lock held.
   1288   1.24       cgd  */
   1289   1.24       cgd void
   1290   1.82   thorpej proc_reparent(struct proc *child, struct proc *parent)
   1291   1.24       cgd {
   1292   1.24       cgd 
   1293  1.203        ad 	KASSERT(mutex_owned(proc_lock));
   1294  1.165        ad 
   1295   1.24       cgd 	if (child->p_pptr == parent)
   1296   1.24       cgd 		return;
   1297   1.70   thorpej 
   1298  1.246  pgoyette 	if (child->p_stat == SZOMB || child->p_stat == SDEAD ||
   1299  1.165        ad 	    (child->p_stat == SSTOP && !child->p_waited)) {
   1300  1.128       dsl 		child->p_pptr->p_nstopchild--;
   1301  1.128       dsl 		parent->p_nstopchild++;
   1302  1.128       dsl 	}
   1303  1.267  christos 	if (parent == initproc) {
   1304   1.70   thorpej 		child->p_exitsig = SIGCHLD;
   1305  1.267  christos 		child->p_ppid = parent->p_pid;
   1306  1.267  christos 	}
   1307   1.24       cgd 
   1308   1.25   mycroft 	LIST_REMOVE(child, p_sibling);
   1309   1.25   mycroft 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
   1310   1.24       cgd 	child->p_pptr = parent;
   1311   1.24       cgd }
   1312