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

Cache object: 42a1349f92c8a360d05addcc3a626333


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