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

Cache object: 6a40ea6fc88be4ae784dfc45ee660945


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