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.162.2.1 2007/04/01 16:16:20 bouyer Exp $       */
    2 
    3 /*-
    4  * Copyright (c) 1998, 1999 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.
   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  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the NetBSD
   22  *      Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 /*
   41  * Copyright (c) 1982, 1986, 1989, 1991, 1993
   42  *      The Regents of the University of California.  All rights reserved.
   43  * (c) UNIX System Laboratories, Inc.
   44  * All or some portions of this file are derived from material licensed
   45  * to the University of California by American Telephone and Telegraph
   46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   47  * the permission of UNIX System Laboratories, Inc.
   48  *
   49  * Redistribution and use in source and binary forms, with or without
   50  * modification, are permitted provided that the following conditions
   51  * are met:
   52  * 1. Redistributions of source code must retain the above copyright
   53  *    notice, this list of conditions and the following disclaimer.
   54  * 2. Redistributions in binary form must reproduce the above copyright
   55  *    notice, this list of conditions and the following disclaimer in the
   56  *    documentation and/or other materials provided with the distribution.
   57  * 3. Neither the name of the University nor the names of its contributors
   58  *    may be used to endorse or promote products derived from this software
   59  *    without specific prior written permission.
   60  *
   61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   71  * SUCH DAMAGE.
   72  *
   73  *      @(#)kern_exit.c 8.10 (Berkeley) 2/23/95
   74  */
   75 
   76 #include <sys/cdefs.h>
   77 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.162.2.1 2007/04/01 16:16:20 bouyer Exp $");
   78 
   79 #include "opt_ktrace.h"
   80 #include "opt_perfctrs.h"
   81 #include "opt_systrace.h"
   82 #include "opt_sysv.h"
   83 
   84 #include <sys/param.h>
   85 #include <sys/systm.h>
   86 #include <sys/ioctl.h>
   87 #include <sys/tty.h>
   88 #include <sys/time.h>
   89 #include <sys/resource.h>
   90 #include <sys/kernel.h>
   91 #include <sys/ktrace.h>
   92 #include <sys/proc.h>
   93 #include <sys/buf.h>
   94 #include <sys/wait.h>
   95 #include <sys/file.h>
   96 #include <sys/vnode.h>
   97 #include <sys/syslog.h>
   98 #include <sys/malloc.h>
   99 #include <sys/pool.h>
  100 #include <sys/resourcevar.h>
  101 #if defined(PERFCTRS)
  102 #include <sys/pmc.h>
  103 #endif
  104 #include <sys/ptrace.h>
  105 #include <sys/acct.h>
  106 #include <sys/filedesc.h>
  107 #include <sys/ras.h>
  108 #include <sys/signalvar.h>
  109 #include <sys/sched.h>
  110 #include <sys/sa.h>
  111 #include <sys/savar.h>
  112 #include <sys/mount.h>
  113 #include <sys/syscallargs.h>
  114 #include <sys/systrace.h>
  115 #include <sys/kauth.h>
  116 
  117 #include <machine/cpu.h>
  118 
  119 #include <uvm/uvm_extern.h>
  120 
  121 #define DEBUG_EXIT
  122 
  123 #ifdef DEBUG_EXIT
  124 int debug_exit = 0;
  125 #define DPRINTF(x) if (debug_exit) printf x
  126 #else
  127 #define DPRINTF(x)
  128 #endif
  129 
  130 static void lwp_exit_hook(struct lwp *, void *);
  131 
  132 /*
  133  * Fill in the appropriate signal information, and signal the parent.
  134  */
  135 static void
  136 exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
  137 {
  138 
  139         (void)memset(ksi, 0, sizeof(ksiginfo_t));
  140         if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
  141                 if (WIFSIGNALED(p->p_xstat)) {
  142                         if (WCOREDUMP(p->p_xstat))
  143                                 ksi->ksi_code = CLD_DUMPED;
  144                         else
  145                                 ksi->ksi_code = CLD_KILLED;
  146                 } else {
  147                         ksi->ksi_code = CLD_EXITED;
  148                 }
  149         }
  150         /*
  151          * we fill those in, even for non-SIGCHLD.
  152          */
  153         ksi->ksi_pid = p->p_pid;
  154         ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
  155         ksi->ksi_status = p->p_xstat;
  156         /* XXX: is this still valid? */
  157         ksi->ksi_utime = p->p_ru->ru_utime.tv_sec;
  158         ksi->ksi_stime = p->p_ru->ru_stime.tv_sec;
  159 }
  160 
  161 /*
  162  * exit --
  163  *      Death of process.
  164  */
  165 int
  166 sys_exit(struct lwp *l, void *v, register_t *retval)
  167 {
  168         struct sys_exit_args /* {
  169                 syscallarg(int) rval;
  170         } */ *uap = v;
  171 
  172         /* Don't call exit1() multiple times in the same process.*/
  173         if (l->l_proc->p_flag & P_WEXIT)
  174                 lwp_exit(l);
  175 
  176         exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
  177         /* NOTREACHED */
  178         return (0);
  179 }
  180 
  181 /*
  182  * Exit: deallocate address space and other resources, change proc state
  183  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
  184  * status and rusage for wait().  Check for child processes and orphan them.
  185  */
  186 void
  187 exit1(struct lwp *l, int rv)
  188 {
  189         struct proc     *p, *q, *nq;
  190         int             s, sa;
  191         ksiginfo_t      ksi;
  192         int             do_psignal = 0;
  193 
  194         p = l->l_proc;
  195 
  196         if (__predict_false(p == initproc))
  197                 panic("init died (signal %d, exit %d)",
  198                     WTERMSIG(rv), WEXITSTATUS(rv));
  199 
  200         p->p_flag |= P_WEXIT;
  201         if (p->p_flag & P_STOPEXIT) {
  202                 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
  203                 SCHED_LOCK(s);
  204                 p->p_stat = SSTOP;
  205                 l->l_stat = LSSTOP;
  206                 p->p_nrlwps--;
  207                 mi_switch(l, NULL);
  208                 SCHED_ASSERT_UNLOCKED();
  209                 splx(s);
  210         }
  211 
  212         DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
  213         /*
  214          * Disable scheduler activation upcalls.
  215          * We're trying to get out of here.
  216          */
  217         sa = 0;
  218         if (p->p_sa != NULL) {
  219                 l->l_flag &= ~L_SA;
  220 #if 0
  221                 p->p_flag &= ~P_SA;
  222 #endif
  223                 sa = 1;
  224         }
  225 
  226 #ifdef PGINPROF
  227         vmsizmon();
  228 #endif
  229         if (p->p_flag & P_PROFIL)
  230                 stopprofclock(p);
  231         p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
  232         /*
  233          * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
  234          * wake up the parent early to avoid deadlock.
  235          */
  236         if (p->p_flag & P_PPWAIT) {
  237                 p->p_flag &= ~P_PPWAIT;
  238                 wakeup(p->p_pptr);
  239         }
  240         sigfillset(&p->p_sigctx.ps_sigignore);
  241         sigemptyset(&p->p_sigctx.ps_siglist);
  242         p->p_sigctx.ps_sigcheck = 0;
  243         p->p_stat = SDYING;
  244         timers_free(p, TIMERS_ALL);
  245 
  246         if (sa || (p->p_nlwps > 1)) {
  247                 exit_lwps(l);
  248 
  249                 /*
  250                  * Collect thread u-areas.
  251                  */
  252                 uvm_uarea_drain(FALSE);
  253         }
  254 
  255 #if defined(__HAVE_RAS)
  256         ras_purgeall(p);
  257 #endif
  258 
  259         /*
  260          * Close open files and release open-file table.
  261          * This may block!
  262          */
  263         fdfree(l);
  264         cwdfree(p->p_cwdi);
  265         p->p_cwdi = 0;
  266 
  267         doexithooks(p);
  268 
  269         if (SESS_LEADER(p)) {
  270                 struct session *sp = p->p_session;
  271                 struct tty *tp;
  272 
  273                 if (sp->s_ttyvp) {
  274                         /*
  275                          * Controlling process.
  276                          * Signal foreground pgrp,
  277                          * drain controlling terminal
  278                          * and revoke access to controlling terminal.
  279                          */
  280                         tp = sp->s_ttyp;
  281                         s = spltty();
  282                         TTY_LOCK(tp);
  283                         if (tp->t_session == sp) {
  284                                 if (tp->t_pgrp)
  285                                         pgsignal(tp->t_pgrp, SIGHUP, 1);
  286                                 /* we can't guarantee the revoke will do this */
  287                                 tp->t_pgrp = NULL;
  288                                 tp->t_session = NULL;
  289                                 TTY_UNLOCK(tp);
  290                                 splx(s);
  291                                 SESSRELE(sp);
  292                                 (void) ttywait(tp);
  293                                 /*
  294                                  * The tty could have been revoked
  295                                  * if we blocked.
  296                                  */
  297                                 if (sp->s_ttyvp)
  298                                         VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
  299                         } else {
  300                                 TTY_UNLOCK(tp);
  301                                 splx(s);
  302                         }
  303                         if (sp->s_ttyvp)
  304                                 vrele(sp->s_ttyvp);
  305                         sp->s_ttyvp = NULL;
  306                         /*
  307                          * s_ttyp is not zero'd; we use this to indicate
  308                          * that the session once had a controlling terminal.
  309                          * (for logging and informational purposes)
  310                          */
  311                 }
  312                 sp->s_leader = NULL;
  313         }
  314         fixjobc(p, p->p_pgrp, 0);
  315 
  316         /*
  317          * Collect accounting flags from the last remaining LWP (this one),
  318          * and write out accounting data.
  319          */
  320         p->p_acflag |= l->l_acflag;
  321         (void)acct_process(l);
  322 
  323 #ifdef KTRACE
  324         /*
  325          * Release trace file.
  326          */
  327         ktrderef(p);
  328 #endif
  329 #ifdef SYSTRACE
  330         systrace_sys_exit(p);
  331 #endif
  332 
  333         /*
  334          * If emulation has process exit hook, call it now.
  335          * Set the exit status now so that the exit hook has
  336          * an opportunity to tweak it (COMPAT_LINUX requires
  337          * this for thread group emulation)
  338          */
  339         p->p_xstat = rv;
  340         if (p->p_emul->e_proc_exit)
  341                 (*p->p_emul->e_proc_exit)(p);
  342 
  343         /*
  344          * Free the VM resources we're still holding on to.
  345          * We must do this from a valid thread because doing
  346          * so may block. This frees vmspace, which we don't
  347          * need anymore. The only remaining lwp is the one
  348          * we run at this moment, nothing runs in userland
  349          * anymore.
  350          */
  351         uvm_proc_exit(p);
  352 
  353         /* Release substructures.  These might need to sleep. */
  354         kauth_cred_free(l->l_cred);
  355         sigactsfree(p->p_sigacts);
  356         p->p_sigacts = NULL;
  357 
  358         /*
  359          * Finalize the last LWP's specificdata, as well as the
  360          * specificdata for the proc itself.
  361          */
  362         lwp_finispecific(l);
  363         proc_finispecific(p);
  364 
  365         /*
  366          * Give machine-dependent code a chance to free any
  367          * MD LWP resources while we can still block. This must be done
  368          * before uvm_lwp_exit(), in case these resources are in the
  369          * PCB.
  370          * THIS IS LAST BLOCKING OPERATION.
  371          */
  372 #ifndef __NO_CPU_LWP_FREE
  373         cpu_lwp_free(l, 1);
  374 #endif
  375 
  376         pmap_deactivate(l);
  377 
  378         /*
  379          * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
  380          */
  381 
  382         /*
  383          * Save final rusage info, adding in child rusage info and self times.
  384          * In order to pick up the time for the current execution, we must
  385          * do this before unlinking the lwp from l_list.
  386          */
  387         *p->p_ru = p->p_stats->p_ru;
  388         calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
  389         ruadd(p->p_ru, &p->p_stats->p_cru);
  390 
  391         /*
  392          * Notify interested parties of our demise.
  393          */
  394         KNOTE(&p->p_klist, NOTE_EXIT);
  395 
  396 #if PERFCTRS
  397         /*
  398          * Save final PMC information in parent process & clean up.
  399          */
  400         if (PMC_ENABLED(p)) {
  401                 pmc_save_context(p);
  402                 pmc_accumulate(p->p_pptr, p);
  403                 pmc_process_exit(p);
  404         }
  405 #endif
  406 
  407         s = proclist_lock_write();
  408         /*
  409          * Reset p_opptr pointer of all former children which got
  410          * traced by another process and were reparented. We reset
  411          * it to NULL here; the trace detach code then reparents
  412          * the child to initproc. We only check allproc list, since
  413          * eventual former children on zombproc list won't reference
  414          * p_opptr anymore.
  415          */
  416         if (p->p_flag & P_CHTRACED) {
  417                 PROCLIST_FOREACH(q, &allproc) {
  418                         if (q->p_opptr == p)
  419                                 q->p_opptr = NULL;
  420                 }
  421         }
  422 
  423         /*
  424          * Give orphaned children to init(8).
  425          */
  426         q = LIST_FIRST(&p->p_children);
  427         if (q)          /* only need this if any child is SZOMB */
  428                 wakeup(initproc);
  429         for (; q != NULL; q = nq) {
  430                 nq = LIST_NEXT(q, p_sibling);
  431 
  432                 /*
  433                  * Traced processes are killed since their existence
  434                  * means someone is screwing up. Since we reset the
  435                  * trace flags, the logic in sys_wait4() would not be
  436                  * triggered to reparent the process to its
  437                  * original parent, so we must do this here.
  438                  */
  439                 if (q->p_flag & P_TRACED) {
  440                         if (q->p_opptr != q->p_pptr) {
  441                                 struct proc *t = q->p_opptr;
  442                                 proc_reparent(q, t ? t : initproc);
  443                                 q->p_opptr = NULL;
  444                         } else
  445                                 proc_reparent(q, initproc);
  446                         q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE|P_SYSCALL);
  447                         killproc(q, "orphaned traced process");
  448                 } else {
  449                         proc_reparent(q, initproc);
  450                 }
  451         }
  452 
  453         /*
  454          * Move proc from allproc to zombproc, it's now ready
  455          * to be collected by parent. Remaining lwp resources
  456          * will be freed in lwp_exit2() once we've switch to idle
  457          * context.
  458          * Changing the state to SZOMB stops it being found by pfind().
  459          */
  460         LIST_REMOVE(p, p_list);
  461         LIST_INSERT_HEAD(&zombproc, p, p_list);
  462         p->p_stat = SZOMB;
  463 
  464         LIST_REMOVE(l, l_list);
  465         LIST_REMOVE(l, l_sibling);
  466         l->l_flag |= L_DETACHED;        /* detached from proc too */
  467         l->l_stat = LSDEAD;
  468 
  469         KASSERT(p->p_nrlwps == 1);
  470         KASSERT(p->p_nlwps == 1);
  471         p->p_nrlwps--;
  472         p->p_nlwps--;
  473 
  474         /* Put in front of parent's sibling list for parent to collect it */
  475         q = p->p_pptr;
  476         q->p_nstopchild++;
  477         if (LIST_FIRST(&q->p_children) != p) {
  478                 /* Put child where it can be found quickly */
  479                 LIST_REMOVE(p, p_sibling);
  480                 LIST_INSERT_HEAD(&q->p_children, p, p_sibling);
  481         }
  482 
  483         /*
  484          * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
  485          * flag set, notify init instead (and hope it will handle
  486          * this situation).
  487          */
  488         if (q->p_flag & (P_NOCLDWAIT|P_CLDSIGIGN)) {
  489                 proc_reparent(p, initproc);
  490 
  491                 /*
  492                  * If this was the last child of our parent, notify
  493                  * parent, so in case he was wait(2)ing, he will
  494                  * continue.
  495                  */
  496                 if (LIST_FIRST(&q->p_children) == NULL)
  497                         wakeup(q);
  498         }
  499 
  500         /*
  501          * Clear curlwp after we've done all operations
  502          * that could block, and before tearing down the rest
  503          * of the process state that might be used from clock, etc.
  504          * Also, can't clear curlwp while we're still runnable,
  505          * as we're not on a run queue (we are current, just not
  506          * a proper proc any longer!).
  507          *
  508          * Other substructures are freed from wait().
  509          */
  510         curlwp = NULL;
  511 
  512         /* Reload parent pointer, since p may have been reparented above */
  513         q = p->p_pptr;
  514 
  515         if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) {
  516                 exit_psignal(p, q, &ksi);
  517                 do_psignal = 1;
  518         }
  519 
  520         /*
  521          * Once we release the proclist lock, we shouldn't touch the
  522          * process structure anymore, since it's now on the zombie
  523          * list and available for collection by the parent.
  524          */
  525         proclist_unlock_write(s);
  526 
  527         if (do_psignal)
  528                 kpsignal(q, &ksi, NULL);
  529 
  530         /* Wake up the parent so it can get exit status. */
  531         wakeup(q);
  532 
  533 #ifdef DEBUG
  534         /* Nothing should use the process link anymore */
  535         l->l_proc = NULL;
  536 #endif
  537 
  538         /* This process no longer needs to hold the kernel lock. */
  539         KERNEL_PROC_UNLOCK(l);
  540 
  541         /*
  542          * Finally, call machine-dependent code to switch to a new
  543          * context (possibly the idle context).  Once we are no longer
  544          * using the dead lwp's stack, lwp_exit2() will be called
  545          * to arrange for the resources to be released.
  546          *
  547          * Note that cpu_exit() will end with a call equivalent to
  548          * cpu_switch(), finishing our execution (pun intended).
  549          */
  550 
  551         uvmexp.swtch++;
  552         cpu_exit(l);
  553 }
  554 
  555 void
  556 exit_lwps(struct lwp *l)
  557 {
  558         struct proc *p;
  559         struct lwp *l2;
  560         struct sadata_vp *vp;
  561         int s, error;
  562         lwpid_t         waited;
  563 
  564         p = l->l_proc;
  565 
  566         /* XXX SMP
  567          * This would be the right place to IPI any LWPs running on
  568          * other processors so that they can notice the userret exit hook.
  569          */
  570         p->p_userret = lwp_exit_hook;
  571         p->p_userret_arg = NULL;
  572 
  573         if (p->p_sa) {
  574                 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
  575                         /*
  576                          * Make SA-cached LWPs normal process runnable
  577                          * LWPs so that they'll also self-destruct.
  578                          */
  579                         DPRINTF(("exit_lwps: Making cached LWPs of %d on VP %d runnable: ",
  580                                     p->p_pid, vp->savp_id));
  581                         SCHED_LOCK(s);
  582                         while ((l2 = sa_getcachelwp(vp)) != 0) {
  583                                 l2->l_priority = l2->l_usrpri;
  584                                 setrunnable(l2);
  585                                 DPRINTF(("%d ", l2->l_lid));
  586                         }
  587                         SCHED_UNLOCK(s);
  588                         DPRINTF(("\n"));
  589 
  590                         /*
  591                          * Clear wokenq, the LWPs on the queue will
  592                          * run below.
  593                          */
  594                         vp->savp_wokenq_head = NULL;
  595                 }
  596         }
  597 
  598 retry:
  599         /*
  600          * Interrupt LWPs in interruptable sleep, unsuspend suspended
  601          * LWPs, make detached LWPs undetached (so we can wait for
  602          * them) and then wait for everyone else to finish.
  603          */
  604         LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
  605                 l2->l_flag &= ~(L_DETACHED|L_SA);
  606 
  607                 SCHED_LOCK(s);
  608                 if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) ||
  609                     l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
  610                         setrunnable(l2);
  611                         DPRINTF(("exit_lwps: Made %d.%d runnable\n",
  612                             p->p_pid, l2->l_lid));
  613                 }
  614                 SCHED_UNLOCK(s);
  615         }
  616 
  617 
  618         while (p->p_nlwps > 1) {
  619                 DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n",
  620                     p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
  621                 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
  622                 if (error == EDEADLK) {
  623                         /*
  624                          * LWPs can get suspended/slept behind us.
  625                          * (eg. sa_setwoken)
  626                          * kick them again and retry.
  627                          */
  628                         goto retry;
  629                 }
  630                 if (error)
  631                         panic("exit_lwps: lwp_wait1 failed with error %d",
  632                             error);
  633                 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
  634         }
  635 
  636         p->p_userret = NULL;
  637 }
  638 
  639 /* Wrapper function for use in p_userret */
  640 static void
  641 lwp_exit_hook(struct lwp *l, void *arg)
  642 {
  643 
  644         KERNEL_PROC_LOCK(l);
  645         lwp_exit(l);
  646 }
  647 
  648 int
  649 sys_wait4(struct lwp *l, void *v, register_t *retval)
  650 {
  651         struct sys_wait4_args /* {
  652                 syscallarg(int)                 pid;
  653                 syscallarg(int *)               status;
  654                 syscallarg(int)                 options;
  655                 syscallarg(struct rusage *)     rusage;
  656         } */ *uap = v;
  657         struct proc     *child, *parent;
  658         int             status, error;
  659 
  660         parent = l->l_proc;
  661 
  662         if (SCARG(uap, pid) == 0)
  663                 SCARG(uap, pid) = -parent->p_pgid;
  664         if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
  665                 return (EINVAL);
  666 
  667         error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
  668                                 &child);
  669         if (error != 0)
  670                 return error;
  671         if (child == NULL) {
  672                 *retval = 0;
  673                 return 0;
  674         }
  675 
  676         /*
  677          * Collect child u-areas.
  678          */
  679         uvm_uarea_drain(FALSE);
  680 
  681         retval[0] = child->p_pid;
  682 
  683         if (P_ZOMBIE(child)) {
  684                 if (SCARG(uap, status)) {
  685                         status = child->p_xstat;        /* convert to int */
  686                         error = copyout(&status, SCARG(uap, status),
  687                                         sizeof(status));
  688                         if (error)
  689                                 return (error);
  690                 }
  691                 if (SCARG(uap, rusage)) {
  692                         error = copyout(child->p_ru, SCARG(uap, rusage),
  693                                     sizeof(struct rusage));
  694                         if (error)
  695                                 return (error);
  696                 }
  697 
  698                 proc_free(child);
  699                 return 0;
  700         }
  701 
  702         /* child state must be SSTOP */
  703         if (SCARG(uap, status)) {
  704                 status = W_STOPCODE(child->p_xstat);
  705                 return copyout(&status, SCARG(uap, status), sizeof(status));
  706         }
  707         return 0;
  708 }
  709 
  710 /*
  711  * Scan list of child processes for a child process that has stopped or
  712  * exited.  Used by sys_wait4 and 'compat' equivalents.
  713  */
  714 int
  715 find_stopped_child(struct proc *parent, pid_t pid, int options,
  716         struct proc **child_p)
  717 {
  718         struct proc *child;
  719         int error;
  720 
  721         for (;;) {
  722                 proclist_lock_read();
  723                 error = ECHILD;
  724                 LIST_FOREACH(child, &parent->p_children, p_sibling) {
  725                         if (pid >= 0) {
  726                                 if (child->p_pid != pid) {
  727                                         child = p_find(pid, PFIND_ZOMBIE |
  728                                                                 PFIND_LOCKED);
  729                                         if (child == NULL
  730                                             || child->p_pptr != parent) {
  731                                                 child = NULL;
  732                                                 break;
  733                                         }
  734                                 }
  735                         } else
  736                                 if (pid != WAIT_ANY && child->p_pgid != -pid)
  737                                         /* child not in correct pgrp */
  738                                         continue;
  739                         /*
  740                          * Wait for processes with p_exitsig != SIGCHLD
  741                          * processes only if WALTSIG is set; wait for
  742                          * processes with p_exitsig == SIGCHLD only
  743                          * if WALTSIG is clear.
  744                          */
  745                         if (((options & WALLSIG) == 0) &&
  746                             (options & WALTSIG ? child->p_exitsig == SIGCHLD
  747                                                 : P_EXITSIG(child) != SIGCHLD)){
  748                                 if (child->p_pid == pid) {
  749                                         child = NULL;
  750                                         break;
  751                                 }
  752                                 continue;
  753                         }
  754 
  755                         error = 0;
  756                         if (child->p_stat == SZOMB && !(options & WNOZOMBIE))
  757                                 break;
  758 
  759                         if (child->p_stat == SSTOP &&
  760                             (child->p_flag & P_WAITED) == 0 &&
  761                             (child->p_flag & P_TRACED || options & WUNTRACED)) {
  762                                 if ((options & WNOWAIT) == 0) {
  763                                         child->p_flag |= P_WAITED;
  764                                         parent->p_nstopchild--;
  765                                 }
  766                                 break;
  767                         }
  768                         if (parent->p_nstopchild == 0 || child->p_pid == pid) {
  769                                 child = NULL;
  770                                 break;
  771                         }
  772                 }
  773                 proclist_unlock_read();
  774                 if (child != NULL || error != 0 || options & WNOHANG) {
  775                         *child_p = child;
  776                         return error;
  777                 }
  778                 error = tsleep(parent, PWAIT | PCATCH, "wait", 0);
  779                 if (error != 0)
  780                         return error;
  781         }
  782 }
  783 
  784 /*
  785  * Free a process after parent has taken all the state info.
  786  */
  787 void
  788 proc_free(struct proc *p)
  789 {
  790         struct proc *parent = p->p_pptr;
  791         ksiginfo_t ksi;
  792         int s;
  793 
  794         KASSERT(p->p_nlwps == 0);
  795         KASSERT(p->p_nzlwps == 0);
  796         KASSERT(p->p_nrlwps == 0);
  797         KASSERT(LIST_EMPTY(&p->p_lwps));
  798 
  799         /*
  800          * If we got the child via ptrace(2) or procfs, and
  801          * the parent is different (meaning the process was
  802          * attached, rather than run as a child), then we need
  803          * to give it back to the old parent, and send the
  804          * parent the exit signal.  The rest of the cleanup
  805          * will be done when the old parent waits on the child.
  806          */
  807         if ((p->p_flag & P_TRACED) && p->p_opptr != parent){
  808                 parent = p->p_opptr;
  809                 if (parent == NULL)
  810                         parent = initproc;
  811                 proc_reparent(p, parent);
  812                 p->p_opptr = NULL;
  813                 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE|P_SYSCALL);
  814                 if (p->p_exitsig != 0) {
  815                         exit_psignal(p, parent, &ksi);
  816                         kpsignal(parent, &ksi, NULL);
  817                 }
  818                 wakeup(parent);
  819                 return;
  820         }
  821 
  822         scheduler_wait_hook(parent, p);
  823         p->p_xstat = 0;
  824 
  825         ruadd(&parent->p_stats->p_cru, p->p_ru);
  826 
  827         /*
  828          * At this point we are going to start freeing the final resources.
  829          * If anyone tries to access the proc structure after here they
  830          * will get a shock - bits are missing.
  831          * Attempt to make it hard!
  832          */
  833 
  834         p->p_stat = SIDL;               /* not even a zombie any more */
  835 
  836         pool_put(&rusage_pool, p->p_ru);
  837 
  838         /*
  839          * Finally finished with old proc entry.
  840          * Unlink it from its process group and free it.
  841          */
  842         leavepgrp(p);
  843 
  844         s = proclist_lock_write();
  845         LIST_REMOVE(p, p_list); /* off zombproc */
  846         p->p_pptr->p_nstopchild--;
  847         LIST_REMOVE(p, p_sibling);
  848         proclist_unlock_write(s);
  849 
  850         /*
  851          * Decrement the count of procs running with this uid.
  852          */
  853         (void)chgproccnt(kauth_cred_getuid(p->p_cred), -1);
  854 
  855         /*
  856          * Free up credentials.
  857          */
  858         kauth_cred_free(p->p_cred);
  859 
  860         /*
  861          * Release reference to text vnode
  862          */
  863         if (p->p_textvp)
  864                 vrele(p->p_textvp);
  865 
  866         /* Release any SA state. */
  867         if (p->p_sa)
  868                 sa_release(p);
  869 
  870         /* Release remaining substructures. */
  871         limfree(p->p_limit);
  872         pstatsfree(p->p_stats);
  873 
  874         /* Free proc structure and let pid be reallocated */
  875         proc_free_mem(p);
  876 }
  877 
  878 /*
  879  * make process 'parent' the new parent of process 'child'.
  880  *
  881  * Must be called with proclist_lock_write() held.
  882  */
  883 void
  884 proc_reparent(struct proc *child, struct proc *parent)
  885 {
  886 
  887         if (child->p_pptr == parent)
  888                 return;
  889 
  890         if (child->p_stat == SZOMB
  891             || (child->p_stat == SSTOP && !(child->p_flag & P_WAITED))) {
  892                 child->p_pptr->p_nstopchild--;
  893                 parent->p_nstopchild++;
  894         }
  895         if (parent == initproc)
  896                 child->p_exitsig = SIGCHLD;
  897 
  898         LIST_REMOVE(child, p_sibling);
  899         LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
  900         child->p_pptr = parent;
  901 }

Cache object: 1926420b60f94bf8f3749120b407d95a


[ 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.