The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_exit.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 583ff9785c301952297326e3d5f1aa11


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