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

Cache object: 37da82a3546e23bb0163820b8fb5833b


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