#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.300 2025/03/16 15:52:18 riastradh Exp $");
#include "opt_ktrace.h"
#include "opt_dtrace.h"
#include "opt_sysv.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/acct.h>
#include <sys/atomic.h>
#include <sys/buf.h>
#include <sys/cpu.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/fstrans.h>
#include <sys/ioctl.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/ktrace.h>
#include <sys/lock.h>
#include <sys/lockdebug.h>
#include <sys/lwpctl.h>
#include <sys/mount.h>
#include <sys/pool.h>
#include <sys/proc.h>
#include <sys/psref.h>
#include <sys/ptrace.h>
#include <sys/ras.h>
#include <sys/resource.h>
#include <sys/sched.h>
#include <sys/sdt.h>
#include <sys/signalvar.h>
#include <sys/sleepq.h>
#include <sys/syscallargs.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/tty.h>
#include <sys/uidinfo.h>
#include <sys/vnode.h>
#include <sys/wait.h>
#include <uvm/uvm_extern.h>
#ifdef DEBUG_EXIT
int debug_exit = 0;
#define DPRINTF(x) if (debug_exit) printf x
#else
#define DPRINTF(x)
#endif
static int find_stopped_child(struct proc *, idtype_t, id_t, int,
struct proc **, struct wrusage *, siginfo_t *);
static void proc_free(struct proc *, struct wrusage *);
SDT_PROVIDER_DECLARE(proc);
SDT_PROBE_DEFINE1(proc, kernel, , exit, "int");
static void __noclone
exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
{
KSI_INIT(ksi);
if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
if (p->p_xsig) {
if (p->p_sflag & PS_COREDUMP)
ksi->ksi_code = CLD_DUMPED;
else
ksi->ksi_code = CLD_KILLED;
ksi->ksi_status = p->p_xsig;
} else {
ksi->ksi_code = CLD_EXITED;
ksi->ksi_status = p->p_xexit;
}
} else {
ksi->ksi_code = SI_USER;
ksi->ksi_status = p->p_xsig;
}
ksi->ksi_pid = p->p_pid;
ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
}
int
sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval)
{
struct proc *p = l->l_proc;
mutex_enter(p->p_lock);
if (p->p_sflag & PS_WEXIT) {
mutex_exit(p->p_lock);
lwp_exit(l);
}
exit1(l, SCARG(uap, rval), 0);
return (0);
}
void
exit1(struct lwp *l, int exitcode, int signo)
{
struct proc *p, *child, *next_child, *old_parent, *new_parent;
struct pgrp *pgrp;
ksiginfo_t ksi;
ksiginfoq_t kq;
int wakeinit;
p = l->l_proc;
LOCKDEBUG_BARRIER(p->p_lock, 0);
KERNEL_UNLOCK_ALL(l, NULL);
KASSERT(mutex_owned(p->p_lock));
KASSERT(p->p_vmspace != NULL);
if (__predict_false(p == initproc)) {
panic("init died (signal %d, exit %d)", signo, exitcode);
}
p->p_sflag |= PS_WEXIT;
if (p->p_nlwps > 1) {
exit_lwps(l);
}
ksiginfo_queue_init(&kq);
if (__predict_false(p->p_sflag & PS_STOPEXIT)) {
KASSERT(l->l_blcnt == 0);
sigclearall(p, &contsigmask, &kq);
if (!mutex_tryenter(&proc_lock)) {
mutex_exit(p->p_lock);
mutex_enter(&proc_lock);
mutex_enter(p->p_lock);
}
p->p_waited = 0;
p->p_pptr->p_nstopchild++;
p->p_stat = SSTOP;
mutex_exit(&proc_lock);
lwp_lock(l);
p->p_nrlwps--;
l->l_stat = LSSTOP;
lwp_unlock(l);
mutex_exit(p->p_lock);
lwp_lock(l);
spc_lock(l->l_cpu);
mi_switch(l);
mutex_enter(p->p_lock);
}
sigfillset(&p->p_sigctx.ps_sigignore);
sigclearall(p, NULL, &kq);
p->p_stat = SDYING;
lwp_thread_cleanup(l);
ksiginfo_queue_drain(&kq);
if (p->p_lwpctl != NULL)
lwp_ctl_exit();
rw_enter(&p->p_reflock, RW_WRITER);
DPRINTF(("%s: %d.%d exiting.\n", __func__, p->p_pid, l->l_lid));
ptimers_free(p, TIMERS_ALL);
#if defined(__HAVE_RAS)
ras_purgeall();
#endif
fd_free();
cwdfree(p->p_cwdi);
p->p_cwdi = NULL;
doexithooks(p);
sigactsfree(p->p_sigacts);
(void)acct_process(l);
#ifdef KTRACE
if (p->p_tracep != NULL) {
mutex_enter(&ktrace_lock);
ktrderef(p);
mutex_exit(&ktrace_lock);
}
#endif
p->p_xexit = exitcode;
p->p_xsig = signo;
if (p->p_emul->e_proc_exit)
(*p->p_emul->e_proc_exit)(p);
ruspace(p);
uvm_proc_exit(p);
if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) {
mutex_spin_enter(&p->p_stmutex);
stopprofclock(p);
mutex_spin_exit(&p->p_stmutex);
}
mutex_enter(&proc_lock);
if (p->p_lflag & PL_PPWAIT) {
lwp_t *lp;
l->l_lwpctl = NULL;
p->p_lflag &= ~PL_PPWAIT;
lp = p->p_vforklwp;
p->p_vforklwp = NULL;
lp->l_vforkwaiting = false;
cv_broadcast(&lp->l_waitcv);
}
if (SESS_LEADER(p)) {
struct vnode *vprele = NULL, *vprevoke = NULL;
struct session *sp = p->p_session;
struct tty *tp;
if (sp->s_ttyvp) {
tp = sp->s_ttyp;
mutex_spin_enter(&tty_lock);
if (tp->t_session == sp) {
pgrp = tp->t_pgrp;
tp->t_pgrp = NULL;
tp->t_session = NULL;
mutex_spin_exit(&tty_lock);
if (pgrp != NULL) {
pgsignal(pgrp, SIGHUP, 1);
}
mutex_exit(&proc_lock);
(void) ttywait(tp);
mutex_enter(&proc_lock);
vprevoke = sp->s_ttyvp;
} else
mutex_spin_exit(&tty_lock);
vprele = sp->s_ttyvp;
sp->s_ttyvp = NULL;
}
sp->s_leader = NULL;
if (vprevoke != NULL || vprele != NULL) {
if (vprevoke != NULL) {
proc_sessrele(sp);
VOP_REVOKE(vprevoke, REVOKEALL);
} else
mutex_exit(&proc_lock);
if (vprele != NULL)
vrele(vprele);
mutex_enter(&proc_lock);
}
}
fixjobc(p, p->p_pgrp, 0);
fstrans_lwp_dtor(l);
lwp_finispecific(l);
proc_finispecific(p);
if (__predict_false(p->p_slflag & PSL_CHTRACED)) {
struct proc *q;
PROCLIST_FOREACH(q, &allproc) {
if (q->p_opptr == p)
q->p_opptr = NULL;
}
PROCLIST_FOREACH(q, &zombproc) {
if (q->p_opptr == p)
q->p_opptr = NULL;
}
}
child = LIST_FIRST(&p->p_children);
wakeinit = (child != NULL);
for (; child != NULL; child = next_child) {
next_child = LIST_NEXT(child, p_sibling);
if (__predict_false(child->p_slflag & PSL_TRACED)) {
mutex_enter(p->p_lock);
child->p_slflag &=
~(PSL_TRACED|PSL_SYSCALL);
mutex_exit(p->p_lock);
if (child->p_opptr != child->p_pptr) {
struct proc *t = child->p_opptr;
proc_reparent(child, t ? t : initproc);
child->p_opptr = NULL;
} else
proc_reparent(child, initproc);
killproc(child, "orphaned traced process");
} else
proc_reparent(child, initproc);
}
LIST_REMOVE(l, l_list);
LIST_REMOVE(p, p_list);
LIST_INSERT_HEAD(&zombproc, p, p_list);
p->p_stat = SDEAD;
SDT_PROBE(proc, kernel, , exit,
((p->p_sflag & PS_COREDUMP) ? CLD_DUMPED :
(p->p_xsig ? CLD_KILLED : CLD_EXITED)),
0,0,0,0);
old_parent = p->p_pptr;
old_parent->p_nstopchild++;
if (LIST_FIRST(&old_parent->p_children) != p) {
LIST_REMOVE(p, p_sibling);
LIST_INSERT_HEAD(&old_parent->p_children, p, p_sibling);
}
if (old_parent->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
proc_reparent(p, initproc);
wakeinit = 1;
if (LIST_FIRST(&old_parent->p_children) == NULL)
cv_broadcast(&old_parent->p_waitcv);
}
new_parent = p->p_pptr;
if (__predict_false(p->p_exitsig != 0)) {
exit_psignal(p, new_parent, &ksi);
kpsignal(new_parent, &ksi, NULL);
}
calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime,
NULL, NULL);
callout_destroy(&l->l_timeout_ch);
pcu_discard_all(l);
mutex_enter(p->p_lock);
if (!SLIST_EMPTY(&p->p_klist)) {
knote_proc_exit(p);
}
proc_free_lwpid(p, l->l_lid);
lwp_drainrefs(l);
lwp_lock(l);
l->l_prflag &= ~LPR_DETACHED;
l->l_stat = LSZOMB;
lwp_unlock(l);
KASSERT(curlwp == l);
KASSERT(p->p_nrlwps == 1);
KASSERT(p->p_nlwps == 1);
p->p_stat = SZOMB;
p->p_nrlwps--;
p->p_nzlwps++;
p->p_ndlwps = 0;
mutex_exit(p->p_lock);
rw_exit(&p->p_reflock);
cv_broadcast(&p->p_pptr->p_waitcv);
mutex_exit(&proc_lock);
if (wakeinit)
cv_broadcast(&initproc->p_waitcv);
cpu_lwp_free(l, 1);
lwp_lock(l);
spc_lock(l->l_cpu);
mi_switch(l);
panic("exit1");
}
void
exit_lwps(struct lwp *l)
{
proc_t *p = l->l_proc;
lwp_t *l2;
retry:
KASSERT(mutex_owned(p->p_lock));
LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
if (l2 == l)
continue;
lwp_lock(l2);
l2->l_flag |= LW_WEXIT;
lwp_need_userret(l2);
if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
l2->l_flag &= ~LW_DBGSUSPEND;
setrunnable(l2);
continue;
}
lwp_unlock(l2);
}
while (p->p_nlwps > 1) {
if (lwp_wait(l, 0, NULL, true)) {
goto retry;
}
}
KASSERT(p->p_nlwps == 1);
}
int
do_sys_waitid(idtype_t idtype, id_t id, int *pid, int *status, int options,
struct wrusage *wru, siginfo_t *si)
{
proc_t *child;
int error;
if (wru != NULL)
memset(wru, 0, sizeof(*wru));
if (si != NULL)
memset(si, 0, sizeof(*si));
mutex_enter(&proc_lock);
error = find_stopped_child(curproc, idtype, id, options, &child,
wru, si);
if (child == NULL) {
mutex_exit(&proc_lock);
*pid = 0;
*status = 0;
return error;
}
*pid = child->p_pid;
if (child->p_stat == SZOMB) {
*status = P_WAITSTATUS(child);
if (options & WNOWAIT) {
mutex_exit(&proc_lock);
} else {
proc_free(child, wru);
}
} else {
*status = (child->p_xsig == SIGCONT && child->p_stat != SSTOP) ?
W_CONTCODE() : W_STOPCODE(child->p_xsig);
mutex_exit(&proc_lock);
}
return 0;
}
int
do_sys_wait(int *pid, int *status, int options, struct rusage *ru)
{
idtype_t idtype;
id_t id;
int ret;
struct wrusage wru;
if (*pid == WAIT_ANY) {
idtype = P_ALL;
id = 0;
} else if (*pid < 0) {
idtype = P_PGID;
id = (id_t)-*pid;
} else {
idtype = P_PID;
id = (id_t)*pid;
}
options |= WEXITED | WTRAPPED;
ret = do_sys_waitid(idtype, id, pid, status, options, ru ? &wru : NULL,
NULL);
if (ru)
*ru = wru.wru_self;
return ret;
}
int
sys___wait450(struct lwp *l, const struct sys___wait450_args *uap,
register_t *retval)
{
int error, status, pid = SCARG(uap, pid);
struct rusage ru;
error = do_sys_wait(&pid, &status, SCARG(uap, options),
SCARG(uap, rusage) != NULL ? &ru : NULL);
retval[0] = pid;
if (pid == 0) {
return error;
}
if (SCARG(uap, status)) {
error = copyout(&status, SCARG(uap, status), sizeof(status));
}
if (SCARG(uap, rusage) && error == 0) {
error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
}
return error;
}
int
sys_wait6(struct lwp *l, const struct sys_wait6_args *uap, register_t *retval)
{
struct wrusage wru, *wrup;
siginfo_t si, *sip;
idtype_t idtype;
int pid;
id_t id;
int error, status;
idtype = SCARG(uap, idtype);
id = SCARG(uap, id);
if (SCARG(uap, wru) != NULL)
wrup = &wru;
else
wrup = NULL;
if (SCARG(uap, info) != NULL)
sip = &si;
else
sip = NULL;
error = do_sys_waitid(idtype, id, &pid, &status, SCARG(uap, options),
wrup, sip);
retval[0] = pid;
#if 0
if (pid == 0)
return error;
#endif
if (SCARG(uap, status) != NULL && error == 0)
error = copyout(&status, SCARG(uap, status), sizeof(status));
if (SCARG(uap, wru) != NULL && error == 0)
error = copyout(&wru, SCARG(uap, wru), sizeof(wru));
if (SCARG(uap, info) != NULL && error == 0)
error = copyout(&si, SCARG(uap, info), sizeof(si));
return error;
}
static int
match_process(const struct proc *pp, struct proc **q, idtype_t idtype, id_t id,
int options, struct wrusage *wrusage, siginfo_t *siginfo)
{
struct rusage *rup;
struct proc *p = *q;
int rv = 1;
switch (idtype) {
case P_ALL:
mutex_enter(p->p_lock);
break;
case P_PID:
if (p->p_pid != (pid_t)id) {
p = *q = proc_find_raw((pid_t)id);
if (p == NULL || p->p_stat == SIDL || p->p_pptr != pp) {
*q = NULL;
return -1;
}
}
mutex_enter(p->p_lock);
rv++;
break;
case P_PGID:
if (p->p_pgid != (pid_t)id)
return 0;
mutex_enter(p->p_lock);
break;
case P_SID:
if (p->p_session->s_sid != (pid_t)id)
return 0;
mutex_enter(p->p_lock);
break;
case P_UID:
mutex_enter(p->p_lock);
if (kauth_cred_geteuid(p->p_cred) != (uid_t)id) {
mutex_exit(p->p_lock);
return 0;
}
break;
case P_GID:
mutex_enter(p->p_lock);
if (kauth_cred_getegid(p->p_cred) != (gid_t)id) {
mutex_exit(p->p_lock);
return 0;
}
break;
case P_CID:
case P_PSETID:
case P_CPUID:
default:
return 0;
}
if ((options & WEXITED) == 0 && p->p_stat == SZOMB) {
mutex_exit(p->p_lock);
return 0;
}
if (siginfo != NULL) {
siginfo->si_errno = 0;
siginfo->si_signo = SIGCHLD;
if (p->p_sflag & PS_COREDUMP) {
siginfo->si_code = CLD_DUMPED;
siginfo->si_status = p->p_xsig;
} else if (p->p_xsig) {
siginfo->si_code = CLD_KILLED;
siginfo->si_status = p->p_xsig;
} else {
siginfo->si_code = CLD_EXITED;
siginfo->si_status = p->p_xexit;
}
siginfo->si_pid = p->p_pid;
siginfo->si_uid = kauth_cred_geteuid(p->p_cred);
siginfo->si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
siginfo->si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
}
if (wrusage != NULL) {
rup = &wrusage->wru_self;
*rup = p->p_stats->p_ru;
calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
rup = &wrusage->wru_children;
*rup = p->p_stats->p_cru;
calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
}
mutex_exit(p->p_lock);
return rv;
}
static bool
debugged_child_exists(idtype_t idtype, id_t id, int options, siginfo_t *si,
const struct proc *parent)
{
struct proc *pp;
if (idtype == P_PID) {
pp = proc_find_raw((pid_t)id);
if (pp != NULL && pp->p_stat != SIDL && pp->p_opptr == parent) {
if (match_process(parent, &pp, P_ALL, id, options,
NULL, si))
return true;
}
return false;
}
PROCLIST_FOREACH(pp, &allproc) {
if (pp->p_stat == SIDL)
continue;
if (pp->p_opptr == parent &&
match_process(parent, &pp, idtype, id, options, NULL, si))
return true;
}
PROCLIST_FOREACH(pp, &zombproc) {
if (pp->p_stat == SIDL)
continue;
if (pp->p_opptr == parent &&
match_process(parent, &pp, idtype, id, options, NULL, si))
return true;
}
return false;
}
static int
find_stopped_child(struct proc *parent, idtype_t idtype, id_t id, int options,
struct proc **child_p, struct wrusage *wru, siginfo_t *si)
{
struct proc *child, *dead;
int error;
KASSERT(mutex_owned(&proc_lock));
if (options & ~WALLOPTS) {
*child_p = NULL;
return SET_ERROR(EINVAL);
}
if ((options & WSELECTOPTS) == 0) {
*child_p = NULL;
return SET_ERROR(EINVAL);
}
if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
id = (id_t)parent->p_pgid;
idtype = P_PGID;
}
for (;;) {
error = ECHILD;
dead = NULL;
LIST_FOREACH(child, &parent->p_children, p_sibling) {
int rv = match_process(parent, &child, idtype, id,
options, wru, si);
if (rv == -1)
break;
if (rv == 0)
continue;
if (((options & WALLSIG) == 0) &&
(options & WALTSIG ? child->p_exitsig == SIGCHLD
: P_EXITSIG(child) != SIGCHLD)){
if (rv == 2) {
child = NULL;
break;
}
continue;
}
error = 0;
if ((options & WNOZOMBIE) == 0) {
if (child->p_stat == SZOMB)
break;
if (child->p_stat == SDEAD) {
dead = child;
}
}
if ((options & WCONTINUED) != 0 &&
child->p_xsig == SIGCONT &&
(child->p_sflag & PS_CONTINUED)) {
if ((options & WNOWAIT) == 0) {
child->p_sflag &= ~PS_CONTINUED;
child->p_waited = 1;
parent->p_nstopchild--;
}
if (si) {
si->si_status = child->p_xsig;
si->si_code = CLD_CONTINUED;
}
break;
}
if ((options & (WTRAPPED|WSTOPPED)) != 0 &&
child->p_stat == SSTOP &&
child->p_waited == 0 &&
((child->p_slflag & PSL_TRACED) ||
options & (WUNTRACED|WSTOPPED))) {
if ((options & WNOWAIT) == 0) {
child->p_waited = 1;
parent->p_nstopchild--;
}
if (si) {
si->si_status = child->p_xsig;
si->si_code =
(child->p_slflag & PSL_TRACED) ?
CLD_TRAPPED : CLD_STOPPED;
}
break;
}
if (parent->p_nstopchild == 0 || rv == 2) {
child = NULL;
break;
}
}
if (child == NULL && error == ECHILD &&
(parent->p_slflag & PSL_CHTRACED) &&
debugged_child_exists(idtype, id, options, si, parent))
error = 0;
if (child != NULL || error != 0 ||
((options & WNOHANG) != 0 && dead == NULL)) {
*child_p = child;
return SET_ERROR(error);
}
error = cv_wait_sig(&parent->p_waitcv, &proc_lock);
if (error != 0) {
*child_p = NULL;
return error;
}
}
}
static void
proc_free(struct proc *p, struct wrusage *wru)
{
struct proc *parent = p->p_pptr;
struct lwp *l;
ksiginfo_t ksi;
kauth_cred_t cred1, cred2;
uid_t uid;
KASSERT(mutex_owned(&proc_lock));
KASSERT(p->p_nlwps == 1);
KASSERT(p->p_nzlwps == 1);
KASSERT(p->p_nrlwps == 0);
KASSERT(p->p_stat == SZOMB);
if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) {
mutex_enter(p->p_lock);
p->p_slflag &= ~(PSL_TRACED|PSL_SYSCALL);
mutex_exit(p->p_lock);
parent = (p->p_opptr == NULL) ? initproc : p->p_opptr;
proc_reparent(p, parent);
p->p_opptr = NULL;
if (p->p_exitsig != 0) {
exit_psignal(p, parent, &ksi);
kpsignal(parent, &ksi, NULL);
}
cv_broadcast(&parent->p_waitcv);
mutex_exit(&proc_lock);
return;
}
sched_proc_exit(parent, p);
l = LIST_FIRST(&p->p_lwps);
ruadd(&p->p_stats->p_ru, &l->l_ru);
ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru);
ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
if (wru != NULL) {
wru->wru_self = p->p_stats->p_ru;
wru->wru_children = p->p_stats->p_cru;
}
p->p_xsig = 0;
p->p_xexit = 0;
p->p_stat = SIDL;
LIST_REMOVE(p, p_list);
parent->p_nstopchild--;
LIST_REMOVE(p, p_sibling);
proc_free_pid(p->p_pid);
atomic_dec_uint(&nprocs);
proc_leavepgrp(p);
cred2 = l->l_cred;
lwp_free(l, false, true);
cred1 = p->p_cred;
uid = kauth_cred_getuid(cred1);
(void)chgproccnt(uid, -1);
lim_free(p->p_limit);
pstatsfree(p->p_stats);
kauth_cred_free(cred1);
kauth_cred_free(cred2);
if (p->p_textvp)
vrele(p->p_textvp);
kmem_strfree(p->p_path);
mutex_destroy(&p->p_auxlock);
mutex_obj_free(p->p_lock);
mutex_destroy(&p->p_stmutex);
cv_destroy(&p->p_waitcv);
cv_destroy(&p->p_lwpcv);
rw_destroy(&p->p_reflock);
proc_free_mem(p);
}
void
proc_changeparent(struct proc *t, struct proc *p)
{
SET(t->p_slflag, PSL_TRACED);
t->p_opptr = t->p_pptr;
if (t->p_pptr == p)
return;
struct proc *parent = t->p_pptr;
if (parent->p_lock < t->p_lock) {
if (!mutex_tryenter(parent->p_lock)) {
mutex_exit(t->p_lock);
mutex_enter(parent->p_lock);
mutex_enter(t->p_lock);
}
} else if (parent->p_lock > t->p_lock) {
mutex_enter(parent->p_lock);
}
parent->p_slflag |= PSL_CHTRACED;
proc_reparent(t, p);
if (parent->p_lock != t->p_lock)
mutex_exit(parent->p_lock);
}
void
proc_reparent(struct proc *child, struct proc *parent)
{
KASSERT(mutex_owned(&proc_lock));
if (child->p_pptr == parent)
return;
if (child->p_stat == SZOMB || child->p_stat == SDEAD ||
(child->p_stat == SSTOP && !child->p_waited)) {
child->p_pptr->p_nstopchild--;
parent->p_nstopchild++;
}
if (parent == initproc) {
child->p_exitsig = SIGCHLD;
child->p_ppid = parent->p_pid;
}
LIST_REMOVE(child, p_sibling);
LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
child->p_pptr = parent;
}