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 /*-
    2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * (c) UNIX System Laboratories, Inc.
    5  * All or some portions of this file are derived from material licensed
    6  * to the University of California by American Telephone and Telegraph
    7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8  * the permission of UNIX System Laboratories, Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD: releng/6.0/sys/kern/kern_exit.c 151755 2005-10-27 18:35:19Z glebius $");
   39 
   40 #include "opt_compat.h"
   41 #include "opt_ktrace.h"
   42 #include "opt_mac.h"
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 #include <sys/sysproto.h>
   47 #include <sys/eventhandler.h>
   48 #include <sys/kernel.h>
   49 #include <sys/malloc.h>
   50 #include <sys/lock.h>
   51 #include <sys/mutex.h>
   52 #include <sys/proc.h>
   53 #include <sys/pioctl.h>
   54 #include <sys/tty.h>
   55 #include <sys/wait.h>
   56 #include <sys/vmmeter.h>
   57 #include <sys/vnode.h>
   58 #include <sys/resourcevar.h>
   59 #include <sys/signalvar.h>
   60 #include <sys/sched.h>
   61 #include <sys/sx.h>
   62 #include <sys/syscallsubr.h>
   63 #include <sys/ptrace.h>
   64 #include <sys/acct.h>           /* for acct_process() function prototype */
   65 #include <sys/filedesc.h>
   66 #include <sys/mac.h>
   67 #include <sys/shm.h>
   68 #include <sys/sem.h>
   69 #ifdef KTRACE
   70 #include <sys/ktrace.h>
   71 #endif
   72 
   73 #include <vm/vm.h>
   74 #include <vm/vm_extern.h>
   75 #include <vm/vm_param.h>
   76 #include <vm/pmap.h>
   77 #include <vm/vm_map.h>
   78 #include <vm/vm_page.h>
   79 #include <vm/uma.h>
   80 
   81 /* Required to be non-static for SysVR4 emulator */
   82 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
   83 
   84 /* Hook for NFS teardown procedure. */
   85 void (*nlminfo_release_p)(struct proc *p);
   86 
   87 /*
   88  * exit --
   89  *      Death of process.
   90  *
   91  * MPSAFE
   92  */
   93 void
   94 sys_exit(struct thread *td, struct sys_exit_args *uap)
   95 {
   96 
   97         exit1(td, W_EXITCODE(uap->rval, 0));
   98         /* NOTREACHED */
   99 }
  100 
  101 /*
  102  * Exit: deallocate address space and other resources, change proc state
  103  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
  104  * status and rusage for wait().  Check for child processes and orphan them.
  105  */
  106 void
  107 exit1(struct thread *td, int rv)
  108 {
  109         struct bintime new_switchtime;
  110         struct proc *p, *nq, *q;
  111         struct tty *tp;
  112         struct vnode *ttyvp;
  113         struct vmspace *vm;
  114         struct vnode *vtmp;
  115 #ifdef KTRACE
  116         struct vnode *tracevp;
  117         struct ucred *tracecred;
  118 #endif
  119         struct plimit *plim;
  120         int refcnt;
  121 
  122         /*
  123          * Drop Giant if caller has it.  Eventually we should warn about
  124          * being called with Giant held.
  125          */ 
  126         while (mtx_owned(&Giant))
  127                 mtx_unlock(&Giant);
  128 
  129         p = td->td_proc;
  130         if (p == initproc) {
  131                 printf("init died (signal %d, exit %d)\n",
  132                     WTERMSIG(rv), WEXITSTATUS(rv));
  133                 panic("Going nowhere without my init!");
  134         }
  135 
  136         /*
  137          * MUST abort all other threads before proceeding past here.
  138          */
  139         PROC_LOCK(p);
  140         if (p->p_flag & P_HADTHREADS) {
  141 retry:
  142                 /*
  143                  * First check if some other thread got here before us..
  144                  * if so, act apropriatly, (exit or suspend);
  145                  */
  146                 thread_suspend_check(0);
  147 
  148                 /*
  149                  * Kill off the other threads. This requires
  150                  * some co-operation from other parts of the kernel
  151                  * so it may not be instantaneous.  With this state set
  152                  * any thread entering the kernel from userspace will
  153                  * thread_exit() in trap().  Any thread attempting to
  154                  * sleep will return immediately with EINTR or EWOULDBLOCK
  155                  * which will hopefully force them to back out to userland
  156                  * freeing resources as they go.  Any thread attempting
  157                  * to return to userland will thread_exit() from userret().
  158                  * thread_exit() will unsuspend us when the last of the
  159                  * other threads exits.
  160                  * If there is already a thread singler after resumption,
  161                  * calling thread_single will fail; in that case, we just
  162                  * re-check all suspension request, the thread should
  163                  * either be suspended there or exit.
  164                  */
  165                 if (thread_single(SINGLE_EXIT))
  166                         goto retry;
  167 
  168                 /*
  169                  * All other activity in this process is now stopped.
  170                  * Threading support has been turned off.
  171                  */
  172         }
  173 
  174         p->p_flag |= P_WEXIT;
  175         PROC_UNLOCK(p);
  176 
  177         /* Are we a task leader? */
  178         if (p == p->p_leader) {
  179                 mtx_lock(&ppeers_lock);
  180                 q = p->p_peers;
  181                 while (q != NULL) {
  182                         PROC_LOCK(q);
  183                         psignal(q, SIGKILL);
  184                         PROC_UNLOCK(q);
  185                         q = q->p_peers;
  186                 }
  187                 while (p->p_peers != NULL)
  188                         msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
  189                 mtx_unlock(&ppeers_lock);
  190         }
  191 
  192         PROC_LOCK(p);
  193         _STOPEVENT(p, S_EXIT, rv);
  194         wakeup(&p->p_stype);    /* Wakeup anyone in procfs' PIOCWAIT */
  195         PROC_UNLOCK(p);
  196 
  197         /*
  198          * Check if any loadable modules need anything done at process exit.
  199          * E.g. SYSV IPC stuff
  200          * XXX what if one of these generates an error?
  201          */
  202         EVENTHANDLER_INVOKE(process_exit, p);
  203 
  204         MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
  205                 M_ZOMBIE, M_WAITOK);
  206         /*
  207          * If parent is waiting for us to exit or exec,
  208          * P_PPWAIT is set; we will wakeup the parent below.
  209          */
  210         PROC_LOCK(p);
  211         stopprofclock(p);
  212         p->p_flag &= ~(P_TRACED | P_PPWAIT);
  213         SIGEMPTYSET(p->p_siglist);
  214         SIGEMPTYSET(td->td_siglist);
  215 
  216         /*
  217          * Stop the real interval timer.  If the handler is currently
  218          * executing, prevent it from rearming itself and let it finish.
  219          */
  220         if (timevalisset(&p->p_realtimer.it_value) &&
  221             callout_stop(&p->p_itcallout) == 0) {
  222                 timevalclear(&p->p_realtimer.it_interval);
  223                 msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0);
  224                 KASSERT(!timevalisset(&p->p_realtimer.it_value),
  225                     ("realtime timer is still armed"));
  226         }
  227         PROC_UNLOCK(p);
  228 
  229         /*
  230          * Reset any sigio structures pointing to us as a result of
  231          * F_SETOWN with our pid.
  232          */
  233         mtx_lock(&Giant);       /* XXX: not sure if needed */
  234         funsetownlst(&p->p_sigiolst);
  235 
  236         /*
  237          * If this process has an nlminfo data area (for lockd), release it
  238          */
  239         if (nlminfo_release_p != NULL && p->p_nlminfo != NULL)
  240                 (*nlminfo_release_p)(p);
  241 
  242         /*
  243          * Close open files and release open-file table.
  244          * This may block!
  245          */
  246         fdfree(td);
  247         mtx_unlock(&Giant);     
  248 
  249         /*
  250          * If this thread tickled GEOM, we need to wait for the giggling to
  251          * stop before we return to userland
  252          */
  253         if (td->td_pflags & TDP_GEOM)
  254                 g_waitidle();
  255 
  256         /*
  257          * Remove ourself from our leader's peer list and wake our leader.
  258          */
  259         mtx_lock(&ppeers_lock);
  260         if (p->p_leader->p_peers) {
  261                 q = p->p_leader;
  262                 while (q->p_peers != p)
  263                         q = q->p_peers;
  264                 q->p_peers = p->p_peers;
  265                 wakeup(p->p_leader);
  266         }
  267         mtx_unlock(&ppeers_lock);
  268 
  269         /* The next two chunks should probably be moved to vmspace_exit. */
  270         vm = p->p_vmspace;
  271         /*
  272          * Release user portion of address space.
  273          * This releases references to vnodes,
  274          * which could cause I/O if the file has been unlinked.
  275          * Need to do this early enough that we can still sleep.
  276          * Can't free the entire vmspace as the kernel stack
  277          * may be mapped within that space also.
  278          *
  279          * Processes sharing the same vmspace may exit in one order, and
  280          * get cleaned up by vmspace_exit() in a different order.  The
  281          * last exiting process to reach this point releases as much of
  282          * the environment as it can, and the last process cleaned up
  283          * by vmspace_exit() (which decrements exitingcnt) cleans up the
  284          * remainder.
  285          */
  286         atomic_add_int(&vm->vm_exitingcnt, 1);
  287         do
  288                 refcnt = vm->vm_refcnt;
  289         while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
  290         if (refcnt == 1) {
  291                 shmexit(vm);
  292                 pmap_remove_pages(vmspace_pmap(vm), vm_map_min(&vm->vm_map),
  293                     vm_map_max(&vm->vm_map));
  294                 (void) vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
  295                     vm_map_max(&vm->vm_map));
  296         }
  297 
  298         mtx_lock(&Giant);       
  299         sx_xlock(&proctree_lock);
  300         if (SESS_LEADER(p)) {
  301                 struct session *sp;
  302 
  303                 sp = p->p_session;
  304                 if (sp->s_ttyvp) {
  305                         /*
  306                          * Controlling process.
  307                          * Signal foreground pgrp,
  308                          * drain controlling terminal
  309                          * and revoke access to controlling terminal.
  310                          */
  311                         if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
  312                                 tp = sp->s_ttyp;
  313                                 if (sp->s_ttyp->t_pgrp) {
  314                                         PGRP_LOCK(sp->s_ttyp->t_pgrp);
  315                                         pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
  316                                         PGRP_UNLOCK(sp->s_ttyp->t_pgrp);
  317                                 }
  318                                 /* XXX tp should be locked. */
  319                                 sx_xunlock(&proctree_lock);
  320                                 (void) ttywait(tp);
  321                                 sx_xlock(&proctree_lock);
  322                                 /*
  323                                  * The tty could have been revoked
  324                                  * if we blocked.
  325                                  */
  326                                 if (sp->s_ttyvp) {
  327                                         ttyvp = sp->s_ttyvp;
  328                                         SESS_LOCK(p->p_session);
  329                                         sp->s_ttyvp = NULL;
  330                                         SESS_UNLOCK(p->p_session);
  331                                         sx_xunlock(&proctree_lock);
  332                                         VOP_LOCK(ttyvp, LK_EXCLUSIVE, td);
  333                                         VOP_REVOKE(ttyvp, REVOKEALL);
  334                                         vput(ttyvp);
  335                                         sx_xlock(&proctree_lock);
  336                                 }
  337                         }
  338                         if (sp->s_ttyvp) {
  339                                 ttyvp = sp->s_ttyvp;
  340                                 SESS_LOCK(p->p_session);
  341                                 sp->s_ttyvp = NULL;
  342                                 SESS_UNLOCK(p->p_session);
  343                                 vrele(ttyvp);
  344                         }
  345                         /*
  346                          * s_ttyp is not zero'd; we use this to indicate
  347                          * that the session once had a controlling terminal.
  348                          * (for logging and informational purposes)
  349                          */
  350                 }
  351                 SESS_LOCK(p->p_session);
  352                 sp->s_leader = NULL;
  353                 SESS_UNLOCK(p->p_session);
  354         }
  355         fixjobc(p, p->p_pgrp, 0);
  356         sx_xunlock(&proctree_lock);
  357         (void)acct_process(td);
  358         mtx_unlock(&Giant);     
  359 #ifdef KTRACE
  360         /*
  361          * release trace file
  362          */
  363         PROC_LOCK(p);
  364         mtx_lock(&ktrace_mtx);
  365         p->p_traceflag = 0;     /* don't trace the vrele() */
  366         tracevp = p->p_tracevp;
  367         p->p_tracevp = NULL;
  368         tracecred = p->p_tracecred;
  369         p->p_tracecred = NULL;
  370         mtx_unlock(&ktrace_mtx);
  371         PROC_UNLOCK(p);
  372         if (tracevp != NULL) {
  373                 mtx_lock(&Giant);
  374                 vrele(tracevp);
  375                 mtx_unlock(&Giant);
  376         }
  377         if (tracecred != NULL)
  378                 crfree(tracecred);
  379 #endif
  380         /*
  381          * Release reference to text vnode
  382          */
  383         if ((vtmp = p->p_textvp) != NULL) {
  384                 p->p_textvp = NULL;
  385                 mtx_lock(&Giant);       
  386                 vrele(vtmp);
  387                 mtx_unlock(&Giant);     
  388         }
  389 
  390         /*
  391          * Release our limits structure.
  392          */
  393         PROC_LOCK(p);
  394         plim = p->p_limit;
  395         p->p_limit = NULL;
  396         PROC_UNLOCK(p);
  397         lim_free(plim);
  398 
  399         /*
  400          * Remove proc from allproc queue and pidhash chain.
  401          * Place onto zombproc.  Unlink from parent's child list.
  402          */
  403         sx_xlock(&allproc_lock);
  404         LIST_REMOVE(p, p_list);
  405         LIST_INSERT_HEAD(&zombproc, p, p_list);
  406         LIST_REMOVE(p, p_hash);
  407         sx_xunlock(&allproc_lock);
  408 
  409         sx_xlock(&proctree_lock);
  410         q = LIST_FIRST(&p->p_children);
  411         if (q != NULL)          /* only need this if any child is S_ZOMB */
  412                 wakeup(initproc);
  413         for (; q != NULL; q = nq) {
  414                 nq = LIST_NEXT(q, p_sibling);
  415                 PROC_LOCK(q);
  416                 proc_reparent(q, initproc);
  417                 q->p_sigparent = SIGCHLD;
  418                 /*
  419                  * Traced processes are killed
  420                  * since their existence means someone is screwing up.
  421                  */
  422                 if (q->p_flag & P_TRACED) {
  423                         q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
  424                         psignal(q, SIGKILL);
  425                 }
  426                 PROC_UNLOCK(q);
  427         }
  428 
  429         /*
  430          * Save exit status and finalize rusage info except for times,
  431          * adding in child rusage info.
  432          */
  433         PROC_LOCK(p);
  434         p->p_xstat = rv;
  435         p->p_xthread = td;
  436         p->p_stats->p_ru.ru_nvcsw++;
  437         *p->p_ru = p->p_stats->p_ru;
  438         ruadd(p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
  439 
  440         /*
  441          * Notify interested parties of our demise.
  442          */
  443         KNOTE_LOCKED(&p->p_klist, NOTE_EXIT);
  444 
  445         /*
  446          * Just delete all entries in the p_klist. At this point we won't
  447          * report any more events, and there are nasty race conditions that
  448          * can beat us if we don't.
  449          */
  450         knlist_clear(&p->p_klist, 1);
  451 
  452         /*
  453          * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
  454          * flag set, or if the handler is set to SIG_IGN, notify process
  455          * 1 instead (and hope it will handle this situation).
  456          */
  457         PROC_LOCK(p->p_pptr);
  458         mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
  459         if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
  460                 struct proc *pp;
  461 
  462                 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
  463                 pp = p->p_pptr;
  464                 PROC_UNLOCK(pp);
  465                 proc_reparent(p, initproc);
  466                 p->p_sigparent = SIGCHLD;
  467                 PROC_LOCK(p->p_pptr);
  468                 /*
  469                  * If this was the last child of our parent, notify
  470                  * parent, so in case he was wait(2)ing, he will
  471                  * continue.
  472                  */
  473                 if (LIST_EMPTY(&pp->p_children))
  474                         wakeup(pp);
  475         } else
  476                 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
  477 
  478         if (p->p_pptr == initproc)
  479                 psignal(p->p_pptr, SIGCHLD);
  480         else if (p->p_sigparent != 0)
  481                 psignal(p->p_pptr, p->p_sigparent);
  482         PROC_UNLOCK(p->p_pptr);
  483 
  484         /*
  485          * If this is a kthread, then wakeup anyone waiting for it to exit.
  486          */
  487         if (p->p_flag & P_KTHREAD)
  488                 wakeup(p);
  489         PROC_UNLOCK(p);
  490 
  491         /*
  492          * Finally, call machine-dependent code to release the remaining
  493          * resources including address space.
  494          * The address space is released by "vmspace_exitfree(p)" in
  495          * vm_waitproc().
  496          */
  497         cpu_exit(td);
  498 
  499         WITNESS_WARN(WARN_PANIC, &proctree_lock.sx_object,
  500             "process (pid %d) exiting", p->p_pid);
  501 
  502         PROC_LOCK(p);
  503         PROC_LOCK(p->p_pptr);
  504         sx_xunlock(&proctree_lock);
  505 
  506         /*
  507          * We have to wait until after acquiring all locks before
  508          * changing p_state.  We need to avoid all possible context
  509          * switches (including ones from blocking on a mutex) while
  510          * marked as a zombie.  We also have to set the zombie state
  511          * before we release the parent process' proc lock to avoid
  512          * a lost wakeup.  So, we first call wakeup, then we grab the
  513          * sched lock, update the state, and release the parent process'
  514          * proc lock.
  515          */
  516         wakeup(p->p_pptr);
  517         mtx_lock_spin(&sched_lock);
  518         p->p_state = PRS_ZOMBIE;
  519         PROC_UNLOCK(p->p_pptr);
  520 
  521         /* Do the same timestamp bookkeeping that mi_switch() would do. */
  522         binuptime(&new_switchtime);
  523         bintime_add(&p->p_rux.rux_runtime, &new_switchtime);
  524         bintime_sub(&p->p_rux.rux_runtime, PCPU_PTR(switchtime));
  525         PCPU_SET(switchtime, new_switchtime);
  526         PCPU_SET(switchticks, ticks);
  527         cnt.v_swtch++;
  528 
  529         sched_exit(p->p_pptr, td);
  530 
  531         /*
  532          * Hopefully no one will try to deliver a signal to the process this
  533          * late in the game.
  534          */
  535         knlist_destroy(&p->p_klist);
  536 
  537         /*
  538          * Make sure the scheduler takes this thread out of its tables etc.
  539          * This will also release this thread's reference to the ucred.
  540          * Other thread parts to release include pcb bits and such.
  541          */
  542         thread_exit();
  543 }
  544 
  545 #ifdef COMPAT_43
  546 /*
  547  * The dirty work is handled by kern_wait().
  548  *
  549  * MPSAFE.
  550  */
  551 int
  552 owait(struct thread *td, struct owait_args *uap __unused)
  553 {
  554         int error, status;
  555 
  556         error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
  557         if (error == 0)
  558                 td->td_retval[1] = status;
  559         return (error);
  560 }
  561 #endif /* COMPAT_43 */
  562 
  563 /*
  564  * The dirty work is handled by kern_wait().
  565  *
  566  * MPSAFE.
  567  */
  568 int
  569 wait4(struct thread *td, struct wait_args *uap)
  570 {
  571         struct rusage ru, *rup;
  572         int error, status;
  573 
  574         if (uap->rusage != NULL)
  575                 rup = &ru;
  576         else
  577                 rup = NULL;
  578         error = kern_wait(td, uap->pid, &status, uap->options, rup);
  579         if (uap->status != NULL && error == 0)
  580                 error = copyout(&status, uap->status, sizeof(status));
  581         if (uap->rusage != NULL && error == 0)
  582                 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
  583         return (error);
  584 }
  585 
  586 int
  587 kern_wait(struct thread *td, pid_t pid, int *status, int options,
  588     struct rusage *rusage)
  589 {
  590         struct proc *p, *q, *t;
  591         int error, nfound;
  592 
  593         q = td->td_proc;
  594         if (pid == 0) {
  595                 PROC_LOCK(q);
  596                 pid = -q->p_pgid;
  597                 PROC_UNLOCK(q);
  598         }
  599         if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE))
  600                 return (EINVAL);
  601 loop:
  602         if (q->p_flag & P_STATCHILD) {
  603                 PROC_LOCK(q);
  604                 q->p_flag &= ~P_STATCHILD;
  605                 PROC_UNLOCK(q);
  606         }
  607         nfound = 0;
  608         sx_xlock(&proctree_lock);
  609         LIST_FOREACH(p, &q->p_children, p_sibling) {
  610                 PROC_LOCK(p);
  611                 if (pid != WAIT_ANY &&
  612                     p->p_pid != pid && p->p_pgid != -pid) {
  613                         PROC_UNLOCK(p);
  614                         continue;
  615                 }
  616                 if (p_canwait(td, p)) {
  617                         PROC_UNLOCK(p);
  618                         continue;
  619                 }
  620 
  621                 /*
  622                  * This special case handles a kthread spawned by linux_clone
  623                  * (see linux_misc.c).  The linux_wait4 and linux_waitpid
  624                  * functions need to be able to distinguish between waiting
  625                  * on a process and waiting on a thread.  It is a thread if
  626                  * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
  627                  * signifies we want to wait for threads and not processes.
  628                  */
  629                 if ((p->p_sigparent != SIGCHLD) ^
  630                     ((options & WLINUXCLONE) != 0)) {
  631                         PROC_UNLOCK(p);
  632                         continue;
  633                 }
  634 
  635                 nfound++;
  636                 if (p->p_state == PRS_ZOMBIE) {
  637 
  638                         /*
  639                          * It is possible that the last thread of this
  640                          * process is still running on another CPU
  641                          * in thread_exit() after having dropped the process
  642                          * lock via PROC_UNLOCK() but before it has completed
  643                          * cpu_throw().  In that case, the other thread must
  644                          * still hold sched_lock, so simply by acquiring
  645                          * sched_lock once we will wait long enough for the
  646                          * thread to exit in that case.
  647                          */
  648                         mtx_lock_spin(&sched_lock);
  649                         mtx_unlock_spin(&sched_lock);
  650                         
  651                         td->td_retval[0] = p->p_pid;
  652                         if (status)
  653                                 *status = p->p_xstat;   /* convert to int */
  654                         if (rusage) {
  655                                 *rusage = *p->p_ru;
  656                                 calcru(p, &rusage->ru_utime, &rusage->ru_stime);
  657                         }
  658 
  659                         /*
  660                          * If we got the child via a ptrace 'attach',
  661                          * we need to give it back to the old parent.
  662                          */
  663                         PROC_UNLOCK(p);
  664                         if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) {
  665                                 PROC_LOCK(p);
  666                                 p->p_oppid = 0;
  667                                 proc_reparent(p, t);
  668                                 PROC_UNLOCK(p);
  669                                 psignal(t, SIGCHLD);
  670                                 wakeup(t);
  671                                 PROC_UNLOCK(t);
  672                                 sx_xunlock(&proctree_lock);
  673                                 return (0);
  674                         }
  675 
  676                         /*
  677                          * Remove other references to this process to ensure
  678                          * we have an exclusive reference.
  679                          */
  680                         sx_xlock(&allproc_lock);
  681                         LIST_REMOVE(p, p_list); /* off zombproc */
  682                         sx_xunlock(&allproc_lock);
  683                         LIST_REMOVE(p, p_sibling);
  684                         leavepgrp(p);
  685                         sx_xunlock(&proctree_lock);
  686 
  687                         /*
  688                          * As a side effect of this lock, we know that
  689                          * all other writes to this proc are visible now, so
  690                          * no more locking is needed for p.
  691                          */
  692                         PROC_LOCK(p);
  693                         p->p_xstat = 0;         /* XXX: why? */
  694                         PROC_UNLOCK(p);
  695                         PROC_LOCK(q);
  696                         ruadd(&q->p_stats->p_cru, &q->p_crux, p->p_ru,
  697                             &p->p_rux);
  698                         PROC_UNLOCK(q);
  699                         FREE(p->p_ru, M_ZOMBIE);
  700                         p->p_ru = NULL;
  701 
  702                         /*
  703                          * Decrement the count of procs running with this uid.
  704                          */
  705                         (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
  706 
  707                         /*
  708                          * Free credentials, arguments, and sigacts.
  709                          */
  710                         crfree(p->p_ucred);
  711                         p->p_ucred = NULL;
  712                         pargs_drop(p->p_args);
  713                         p->p_args = NULL;
  714                         sigacts_free(p->p_sigacts);
  715                         p->p_sigacts = NULL;
  716 
  717                         /*
  718                          * Do any thread-system specific cleanups.
  719                          */
  720                         thread_wait(p);
  721 
  722                         /*
  723                          * Give vm and machine-dependent layer a chance
  724                          * to free anything that cpu_exit couldn't
  725                          * release while still running in process context.
  726                          */
  727                         vm_waitproc(p);
  728 #ifdef MAC
  729                         mac_destroy_proc(p);
  730 #endif
  731                         KASSERT(FIRST_THREAD_IN_PROC(p),
  732                             ("kern_wait: no residual thread!"));
  733                         uma_zfree(proc_zone, p);
  734                         sx_xlock(&allproc_lock);
  735                         nprocs--;
  736                         sx_xunlock(&allproc_lock);
  737                         return (0);
  738                 }
  739                 mtx_lock_spin(&sched_lock);
  740                 if ((p->p_flag & P_STOPPED_SIG) &&
  741                     (p->p_suspcount == p->p_numthreads) &&
  742                     (p->p_flag & P_WAITED) == 0 &&
  743                     (p->p_flag & P_TRACED || options & WUNTRACED)) {
  744                         mtx_unlock_spin(&sched_lock);
  745                         p->p_flag |= P_WAITED;
  746                         sx_xunlock(&proctree_lock);
  747                         td->td_retval[0] = p->p_pid;
  748                         if (status)
  749                                 *status = W_STOPCODE(p->p_xstat);
  750                         PROC_UNLOCK(p);
  751                         return (0);
  752                 }
  753                 mtx_unlock_spin(&sched_lock);
  754                 if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) {
  755                         sx_xunlock(&proctree_lock);
  756                         td->td_retval[0] = p->p_pid;
  757                         p->p_flag &= ~P_CONTINUED;
  758                         PROC_UNLOCK(p);
  759 
  760                         if (status)
  761                                 *status = SIGCONT;
  762                         return (0);
  763                 }
  764                 PROC_UNLOCK(p);
  765         }
  766         if (nfound == 0) {
  767                 sx_xunlock(&proctree_lock);
  768                 return (ECHILD);
  769         }
  770         if (options & WNOHANG) {
  771                 sx_xunlock(&proctree_lock);
  772                 td->td_retval[0] = 0;
  773                 return (0);
  774         }
  775         PROC_LOCK(q);
  776         sx_xunlock(&proctree_lock);
  777         if (q->p_flag & P_STATCHILD) {
  778                 q->p_flag &= ~P_STATCHILD;
  779                 error = 0;
  780         } else
  781                 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0);
  782         PROC_UNLOCK(q);
  783         if (error)
  784                 return (error); 
  785         goto loop;
  786 }
  787 
  788 /*
  789  * Make process 'parent' the new parent of process 'child'.
  790  * Must be called with an exclusive hold of proctree lock.
  791  */
  792 void
  793 proc_reparent(struct proc *child, struct proc *parent)
  794 {
  795 
  796         sx_assert(&proctree_lock, SX_XLOCKED);
  797         PROC_LOCK_ASSERT(child, MA_OWNED);
  798         if (child->p_pptr == parent)
  799                 return;
  800 
  801         LIST_REMOVE(child, p_sibling);
  802         LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
  803         child->p_pptr = parent;
  804 }

Cache object: d79be30ad98d622ff23752f65fb346fa


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