The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_exit.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 8531e53449771a259ae96e3d82cde1a2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.