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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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 <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         /*
  411          * Call machine-dependent code to release any
  412          * machine-dependent resources other than the address space.
  413          * The address space is released by "vmspace_exitfree(p)" in
  414          * vm_waitproc().
  415          */
  416         cpu_exit(td);
  417 
  418         WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
  419 
  420         sx_xlock(&proctree_lock);
  421         q = LIST_FIRST(&p->p_children);
  422         if (q != NULL)          /* only need this if any child is S_ZOMB */
  423                 wakeup(initproc);
  424         for (; q != NULL; q = nq) {
  425                 nq = LIST_NEXT(q, p_sibling);
  426                 PROC_LOCK(q);
  427                 proc_reparent(q, initproc);
  428                 q->p_sigparent = SIGCHLD;
  429                 /*
  430                  * Traced processes are killed
  431                  * since their existence means someone is screwing up.
  432                  */
  433                 if (q->p_flag & P_TRACED) {
  434                         q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
  435                         psignal(q, SIGKILL);
  436                 }
  437                 PROC_UNLOCK(q);
  438         }
  439 
  440         /*
  441          * Save exit status and finalize rusage info except for times,
  442          * adding in child rusage info.
  443          */
  444         PROC_LOCK(p);
  445         p->p_xstat = rv;
  446         p->p_xthread = td;
  447         p->p_stats->p_ru.ru_nvcsw++;
  448         *p->p_ru = p->p_stats->p_ru;
  449 
  450         /*
  451          * Notify interested parties of our demise.
  452          */
  453         KNOTE_LOCKED(&p->p_klist, NOTE_EXIT);
  454 
  455         /*
  456          * Just delete all entries in the p_klist. At this point we won't
  457          * report any more events, and there are nasty race conditions that
  458          * can beat us if we don't.
  459          */
  460         knlist_clear(&p->p_klist, 1);
  461 
  462         /*
  463          * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
  464          * flag set, or if the handler is set to SIG_IGN, notify process
  465          * 1 instead (and hope it will handle this situation).
  466          */
  467         PROC_LOCK(p->p_pptr);
  468         mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
  469         if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
  470                 struct proc *pp;
  471 
  472                 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
  473                 pp = p->p_pptr;
  474                 PROC_UNLOCK(pp);
  475                 proc_reparent(p, initproc);
  476                 p->p_sigparent = SIGCHLD;
  477                 PROC_LOCK(p->p_pptr);
  478                 /*
  479                  * If this was the last child of our parent, notify
  480                  * parent, so in case he was wait(2)ing, he will
  481                  * continue.
  482                  */
  483                 if (LIST_EMPTY(&pp->p_children))
  484                         wakeup(pp);
  485         } else
  486                 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
  487 
  488         if (p->p_pptr == initproc)
  489                 psignal(p->p_pptr, SIGCHLD);
  490         else if (p->p_sigparent != 0)
  491                 psignal(p->p_pptr, p->p_sigparent);
  492         sx_xunlock(&proctree_lock);
  493 
  494         /*
  495          * If this is a kthread, then wakeup anyone waiting for it to exit.
  496          */
  497         if (p->p_flag & P_KTHREAD)
  498                 wakeup(p);
  499 
  500         /*
  501          * We have to wait until after acquiring all locks before
  502          * changing p_state.  We need to avoid all possible context
  503          * switches (including ones from blocking on a mutex) while
  504          * marked as a zombie.  We also have to set the zombie state
  505          * before we release the parent process' proc lock to avoid
  506          * a lost wakeup.  So, we first call wakeup, then we grab the
  507          * sched lock, update the state, and release the parent process'
  508          * proc lock.
  509          */
  510         wakeup(p->p_pptr);
  511         mtx_lock_spin(&sched_lock);
  512         p->p_state = PRS_ZOMBIE;
  513         PROC_UNLOCK(p->p_pptr);
  514 
  515         sched_exit(p->p_pptr, td);
  516 
  517         /*
  518          * Hopefully no one will try to deliver a signal to the process this
  519          * late in the game.
  520          */
  521         knlist_destroy(&p->p_klist);
  522 
  523         /*
  524          * Make sure the scheduler takes this thread out of its tables etc.
  525          * This will also release this thread's reference to the ucred.
  526          * Other thread parts to release include pcb bits and such.
  527          */
  528         thread_exit();
  529 }
  530 
  531 #ifdef COMPAT_43
  532 /*
  533  * The dirty work is handled by kern_wait().
  534  *
  535  * MPSAFE.
  536  */
  537 int
  538 owait(struct thread *td, struct owait_args *uap __unused)
  539 {
  540         int error, status;
  541 
  542         error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
  543         if (error == 0)
  544                 td->td_retval[1] = status;
  545         return (error);
  546 }
  547 #endif /* COMPAT_43 */
  548 
  549 /*
  550  * The dirty work is handled by kern_wait().
  551  *
  552  * MPSAFE.
  553  */
  554 int
  555 wait4(struct thread *td, struct wait_args *uap)
  556 {
  557         struct rusage ru, *rup;
  558         int error, status;
  559 
  560         if (uap->rusage != NULL)
  561                 rup = &ru;
  562         else
  563                 rup = NULL;
  564         error = kern_wait(td, uap->pid, &status, uap->options, rup);
  565         if (uap->status != NULL && error == 0)
  566                 error = copyout(&status, uap->status, sizeof(status));
  567         if (uap->rusage != NULL && error == 0)
  568                 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
  569         return (error);
  570 }
  571 
  572 int
  573 kern_wait(struct thread *td, pid_t pid, int *status, int options,
  574     struct rusage *rusage)
  575 {
  576         struct proc *p, *q, *t;
  577         int error, nfound;
  578 
  579         AUDIT_ARG(pid, pid);
  580 
  581         q = td->td_proc;
  582         if (pid == 0) {
  583                 PROC_LOCK(q);
  584                 pid = -q->p_pgid;
  585                 PROC_UNLOCK(q);
  586         }
  587         if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE))
  588                 return (EINVAL);
  589 loop:
  590         if (q->p_flag & P_STATCHILD) {
  591                 PROC_LOCK(q);
  592                 q->p_flag &= ~P_STATCHILD;
  593                 PROC_UNLOCK(q);
  594         }
  595         nfound = 0;
  596         sx_xlock(&proctree_lock);
  597         LIST_FOREACH(p, &q->p_children, p_sibling) {
  598                 PROC_LOCK(p);
  599                 if (pid != WAIT_ANY &&
  600                     p->p_pid != pid && p->p_pgid != -pid) {
  601                         PROC_UNLOCK(p);
  602                         continue;
  603                 }
  604                 if (p_canwait(td, p)) {
  605                         PROC_UNLOCK(p);
  606                         continue;
  607                 }
  608 
  609                 /*
  610                  * This special case handles a kthread spawned by linux_clone
  611                  * (see linux_misc.c).  The linux_wait4 and linux_waitpid
  612                  * functions need to be able to distinguish between waiting
  613                  * on a process and waiting on a thread.  It is a thread if
  614                  * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
  615                  * signifies we want to wait for threads and not processes.
  616                  */
  617                 if ((p->p_sigparent != SIGCHLD) ^
  618                     ((options & WLINUXCLONE) != 0)) {
  619                         PROC_UNLOCK(p);
  620                         continue;
  621                 }
  622 
  623                 nfound++;
  624                 if (p->p_state == PRS_ZOMBIE) {
  625 
  626                         /*
  627                          * It is possible that the last thread of this
  628                          * process is still running on another CPU
  629                          * in thread_exit() after having dropped the process
  630                          * lock via PROC_UNLOCK() but before it has completed
  631                          * cpu_throw().  In that case, the other thread must
  632                          * still hold sched_lock, so simply by acquiring
  633                          * sched_lock once we will wait long enough for the
  634                          * thread to exit in that case.
  635                          */
  636                         mtx_lock_spin(&sched_lock);
  637                         mtx_unlock_spin(&sched_lock);
  638                         
  639                         td->td_retval[0] = p->p_pid;
  640                         if (status)
  641                                 *status = p->p_xstat;   /* convert to int */
  642                         if (rusage) {
  643                                 *rusage = *p->p_ru;
  644                                 calcru(p, &rusage->ru_utime, &rusage->ru_stime);
  645                         }
  646 
  647                         /*
  648                          * If we got the child via a ptrace 'attach',
  649                          * we need to give it back to the old parent.
  650                          */
  651                         PROC_UNLOCK(p);
  652                         if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) {
  653                                 PROC_LOCK(p);
  654                                 p->p_oppid = 0;
  655                                 proc_reparent(p, t);
  656                                 PROC_UNLOCK(p);
  657                                 psignal(t, SIGCHLD);
  658                                 wakeup(t);
  659                                 PROC_UNLOCK(t);
  660                                 sx_xunlock(&proctree_lock);
  661                                 return (0);
  662                         }
  663 
  664                         /*
  665                          * Remove other references to this process to ensure
  666                          * we have an exclusive reference.
  667                          */
  668                         sx_xlock(&allproc_lock);
  669                         LIST_REMOVE(p, p_list); /* off zombproc */
  670                         sx_xunlock(&allproc_lock);
  671                         LIST_REMOVE(p, p_sibling);
  672                         leavepgrp(p);
  673                         sx_xunlock(&proctree_lock);
  674 
  675                         /*
  676                          * As a side effect of this lock, we know that
  677                          * all other writes to this proc are visible now, so
  678                          * no more locking is needed for p.
  679                          */
  680                         PROC_LOCK(p);
  681                         p->p_xstat = 0;         /* XXX: why? */
  682                         PROC_UNLOCK(p);
  683                         PROC_LOCK(q);
  684                         ruadd(&q->p_stats->p_cru, &q->p_crux, p->p_ru,
  685                             &p->p_rux);
  686                         PROC_UNLOCK(q);
  687                         FREE(p->p_ru, M_ZOMBIE);
  688                         p->p_ru = NULL;
  689 
  690                         /*
  691                          * Decrement the count of procs running with this uid.
  692                          */
  693                         (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
  694 
  695                         /*
  696                          * Free credentials, arguments, and sigacts.
  697                          */
  698                         crfree(p->p_ucred);
  699                         p->p_ucred = NULL;
  700                         pargs_drop(p->p_args);
  701                         p->p_args = NULL;
  702                         sigacts_free(p->p_sigacts);
  703                         p->p_sigacts = NULL;
  704 
  705                         /*
  706                          * Do any thread-system specific cleanups.
  707                          */
  708                         thread_wait(p);
  709 
  710                         /*
  711                          * Give vm and machine-dependent layer a chance
  712                          * to free anything that cpu_exit couldn't
  713                          * release while still running in process context.
  714                          */
  715                         vm_waitproc(p);
  716 #ifdef MAC
  717                         mac_destroy_proc(p);
  718 #endif
  719 #ifdef AUDIT
  720                         audit_proc_free(p);
  721 #endif
  722                         KASSERT(FIRST_THREAD_IN_PROC(p),
  723                             ("kern_wait: no residual thread!"));
  724                         uma_zfree(proc_zone, p);
  725                         sx_xlock(&allproc_lock);
  726                         nprocs--;
  727                         sx_xunlock(&allproc_lock);
  728                         return (0);
  729                 }
  730                 mtx_lock_spin(&sched_lock);
  731                 if ((p->p_flag & P_STOPPED_SIG) &&
  732                     (p->p_suspcount == p->p_numthreads) &&
  733                     (p->p_flag & P_WAITED) == 0 &&
  734                     (p->p_flag & P_TRACED || options & WUNTRACED)) {
  735                         mtx_unlock_spin(&sched_lock);
  736                         p->p_flag |= P_WAITED;
  737                         sx_xunlock(&proctree_lock);
  738                         td->td_retval[0] = p->p_pid;
  739                         if (status)
  740                                 *status = W_STOPCODE(p->p_xstat);
  741                         PROC_UNLOCK(p);
  742                         return (0);
  743                 }
  744                 mtx_unlock_spin(&sched_lock);
  745                 if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) {
  746                         sx_xunlock(&proctree_lock);
  747                         td->td_retval[0] = p->p_pid;
  748                         p->p_flag &= ~P_CONTINUED;
  749                         PROC_UNLOCK(p);
  750 
  751                         if (status)
  752                                 *status = SIGCONT;
  753                         return (0);
  754                 }
  755                 PROC_UNLOCK(p);
  756         }
  757         if (nfound == 0) {
  758                 sx_xunlock(&proctree_lock);
  759                 return (ECHILD);
  760         }
  761         if (options & WNOHANG) {
  762                 sx_xunlock(&proctree_lock);
  763                 td->td_retval[0] = 0;
  764                 return (0);
  765         }
  766         PROC_LOCK(q);
  767         sx_xunlock(&proctree_lock);
  768         if (q->p_flag & P_STATCHILD) {
  769                 q->p_flag &= ~P_STATCHILD;
  770                 error = 0;
  771         } else
  772                 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0);
  773         PROC_UNLOCK(q);
  774         if (error)
  775                 return (error); 
  776         goto loop;
  777 }
  778 
  779 /*
  780  * Make process 'parent' the new parent of process 'child'.
  781  * Must be called with an exclusive hold of proctree lock.
  782  */
  783 void
  784 proc_reparent(struct proc *child, struct proc *parent)
  785 {
  786 
  787         sx_assert(&proctree_lock, SX_XLOCKED);
  788         PROC_LOCK_ASSERT(child, MA_OWNED);
  789         if (child->p_pptr == parent)
  790                 return;
  791 
  792         LIST_REMOVE(child, p_sibling);
  793         LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
  794         child->p_pptr = parent;
  795 }

Cache object: 382f14058f56f9e3c7000ef794235cdb


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