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.0/sys/kern/kern_exit.c 108169 2002-12-22 03:30:34Z dillon $
   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/kernel.h>
   50 #include <sys/malloc.h>
   51 #include <sys/lock.h>
   52 #include <sys/mutex.h>
   53 #include <sys/proc.h>
   54 #include <sys/pioctl.h>
   55 #include <sys/tty.h>
   56 #include <sys/wait.h>
   57 #include <sys/vmmeter.h>
   58 #include <sys/vnode.h>
   59 #include <sys/resourcevar.h>
   60 #include <sys/signalvar.h>
   61 #include <sys/sched.h>
   62 #include <sys/sx.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 #include <sys/jail.h>
   70 #ifdef KTRACE
   71 #include <sys/ktrace.h>
   72 #endif
   73 
   74 #include <vm/vm.h>
   75 #include <vm/vm_extern.h>
   76 #include <vm/vm_param.h>
   77 #include <vm/pmap.h>
   78 #include <vm/vm_map.h>
   79 #include <vm/vm_page.h>
   80 #include <vm/uma.h>
   81 #include <sys/user.h>
   82 
   83 /* Required to be non-static for SysVR4 emulator */
   84 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
   85 
   86 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
   87 
   88 static int wait1(struct thread *, struct wait_args *, int);
   89 
   90 /*
   91  * callout list for things to do at exit time
   92  */
   93 struct exitlist {
   94         exitlist_fn function;
   95         TAILQ_ENTRY(exitlist) next;
   96 };
   97 
   98 TAILQ_HEAD(exit_list_head, exitlist);
   99 static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
  100 
  101 /*
  102  * exit --
  103  *      Death of process.
  104  *
  105  * MPSAFE
  106  */
  107 void
  108 sys_exit(td, uap)
  109         struct thread *td;
  110         struct sys_exit_args /* {
  111                 int     rval;
  112         } */ *uap;
  113 {
  114 
  115         mtx_lock(&Giant);
  116         exit1(td, W_EXITCODE(uap->rval, 0));
  117         /* NOTREACHED */
  118 }
  119 
  120 /*
  121  * Exit: deallocate address space and other resources, change proc state
  122  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
  123  * status and rusage for wait().  Check for child processes and orphan them.
  124  */
  125 void
  126 exit1(td, rv)
  127         register struct thread *td;
  128         int rv;
  129 {
  130         struct exitlist *ep;
  131         struct proc *p, *nq, *q;
  132         struct tty *tp;
  133         struct vnode *ttyvp;
  134         register struct vmspace *vm;
  135         struct vnode *vtmp;
  136 #ifdef KTRACE
  137         struct vnode *tracevp;
  138 #endif
  139 
  140         GIANT_REQUIRED;
  141 
  142         p = td->td_proc;
  143         if (p == initproc) {
  144                 printf("init died (signal %d, exit %d)\n",
  145                     WTERMSIG(rv), WEXITSTATUS(rv));
  146                 panic("Going nowhere without my init!");
  147         }
  148 
  149         /*
  150          * XXXXKSE: MUST abort all other threads before proceeding past here.
  151          */
  152         PROC_LOCK(p);
  153         if (p->p_flag & P_KSES) {
  154                 /*
  155                  * First check if some other thread got here before us..
  156                  * if so, act apropriatly, (exit or suspend);
  157                  */
  158                 thread_suspend_check(0);
  159                 /*
  160                  * Here is a trick..
  161                  * We need to free up our KSE to process other threads
  162                  * so that we can safely set the UNBOUND flag
  163                  * (whether or not we have a mailbox) as we are NEVER
  164                  * going to return to the user.
  165                  * The flag will not be set yet if we are exiting
  166                  * because of a signal, pagefault, or similar
  167                  * (or even an exit(2) from the UTS).
  168                  */
  169                 td->td_flags |= TDF_UNBOUND;
  170 
  171                 /*
  172                  * Kill off the other threads. This requires
  173                  * Some co-operation from other parts of the kernel
  174                  * so it may not be instant.
  175                  * With this state set:
  176                  * Any thread entering the kernel from userspace will
  177                  * thread_exit() in trap().  Any thread attempting to
  178                  * sleep will return immediatly
  179                  * with EINTR or EWOULDBLOCK, which will hopefully force them
  180                  * to back out to userland, freeing resources as they go, and
  181                  * anything attempting to return to userland will thread_exit()
  182                  * from userret().  thread_exit() will unsuspend us
  183                  * when the last other thread exits.
  184                  */
  185                 if (thread_single(SINGLE_EXIT)) {
  186                         panic ("Exit: Single threading fouled up");
  187                 }
  188                 /*
  189                  * All other activity in this process is now stopped.
  190                  * Remove excess KSEs and KSEGRPS. XXXKSE (when we have them)
  191                  * ... 
  192                  * Turn off threading support.
  193                  */
  194                 p->p_flag &= ~P_KSES;
  195                 td->td_flags &= ~TDF_UNBOUND;
  196                 thread_single_end();    /* Don't need this any more. */
  197         }
  198         /*
  199          * With this state set:
  200          * Any thread entering the kernel from userspace will thread_exit()
  201          * in trap().  Any thread attempting to sleep will return immediatly
  202          * with EINTR or EWOULDBLOCK, which will hopefully force them
  203          * to back out to userland, freeing resources as they go, and
  204          * anything attempting to return to userland will thread_exit()
  205          * from userret().  thread_exit() will do a wakeup on p->p_numthreads
  206          * if it transitions to 1.
  207          */
  208 
  209         p->p_flag |= P_WEXIT;
  210         PROC_UNLOCK(p);
  211 
  212         /* Are we a task leader? */
  213         if (p == p->p_leader) {
  214                 mtx_lock(&ppeers_lock);
  215                 q = p->p_peers;
  216                 while (q != NULL) {
  217                         PROC_LOCK(q);
  218                         psignal(q, SIGKILL);
  219                         PROC_UNLOCK(q);
  220                         q = q->p_peers;
  221                 }
  222                 while (p->p_peers != NULL) 
  223                         msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
  224                 mtx_unlock(&ppeers_lock);
  225         }
  226 
  227 #ifdef PGINPROF
  228         vmsizmon();
  229 #endif
  230         STOPEVENT(p, S_EXIT, rv);
  231         wakeup(&p->p_stype);    /* Wakeup anyone in procfs' PIOCWAIT */
  232 
  233         /* 
  234          * Check if any loadable modules need anything done at process exit.
  235          * e.g. SYSV IPC stuff
  236          * XXX what if one of these generates an error?
  237          */
  238         TAILQ_FOREACH(ep, &exit_list, next) 
  239                 (*ep->function)(p);
  240 
  241         stopprofclock(p);
  242 
  243         MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
  244                 M_ZOMBIE, M_WAITOK);
  245         /*
  246          * If parent is waiting for us to exit or exec,
  247          * P_PPWAIT is set; we will wakeup the parent below.
  248          */
  249         PROC_LOCK(p);
  250         p->p_flag &= ~(P_TRACED | P_PPWAIT);
  251         SIGEMPTYSET(p->p_siglist);
  252         PROC_UNLOCK(p);
  253         if (timevalisset(&p->p_realtimer.it_value))
  254                 callout_stop(&p->p_itcallout);
  255 
  256         /*
  257          * Reset any sigio structures pointing to us as a result of
  258          * F_SETOWN with our pid.
  259          */
  260         funsetownlst(&p->p_sigiolst);
  261 
  262         /*
  263          * Close open files and release open-file table.
  264          * This may block!
  265          */
  266         fdfree(td); /* XXXKSE *//* may not be the one in proc */
  267 
  268         /*
  269          * Remove ourself from our leader's peer list and wake our leader.
  270          */
  271         mtx_lock(&ppeers_lock);
  272         if (p->p_leader->p_peers) {
  273                 q = p->p_leader;
  274                 while (q->p_peers != p)
  275                         q = q->p_peers;
  276                 q->p_peers = p->p_peers;
  277                 wakeup(p->p_leader);
  278         }
  279         mtx_unlock(&ppeers_lock);
  280 
  281         /* The next two chunks should probably be moved to vmspace_exit. */
  282         vm = p->p_vmspace;
  283         /*
  284          * Release user portion of address space.
  285          * This releases references to vnodes,
  286          * which could cause I/O if the file has been unlinked.
  287          * Need to do this early enough that we can still sleep.
  288          * Can't free the entire vmspace as the kernel stack
  289          * may be mapped within that space also.
  290          *
  291          * Processes sharing the same vmspace may exit in one order, and
  292          * get cleaned up by vmspace_exit() in a different order.  The
  293          * last exiting process to reach this point releases as much of
  294          * the environment as it can, and the last process cleaned up
  295          * by vmspace_exit() (which decrements exitingcnt) cleans up the
  296          * remainder.
  297          */
  298         ++vm->vm_exitingcnt;
  299         if (--vm->vm_refcnt == 0) {
  300                 if (vm->vm_shm)
  301                         shmexit(p);
  302                 vm_page_lock_queues();
  303                 pmap_remove_pages(vmspace_pmap(vm), vm_map_min(&vm->vm_map),
  304                     vm_map_max(&vm->vm_map));
  305                 vm_page_unlock_queues();
  306                 (void) vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
  307                     vm_map_max(&vm->vm_map));
  308         }
  309 
  310         sx_xlock(&proctree_lock);
  311         if (SESS_LEADER(p)) {
  312                 register struct session *sp;
  313 
  314                 sp = p->p_session;
  315                 if (sp->s_ttyvp) {
  316                         /*
  317                          * Controlling process.
  318                          * Signal foreground pgrp,
  319                          * drain controlling terminal
  320                          * and revoke access to controlling terminal.
  321                          */
  322                         if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
  323                                 tp = sp->s_ttyp;
  324                                 if (sp->s_ttyp->t_pgrp) {
  325                                         PGRP_LOCK(sp->s_ttyp->t_pgrp);
  326                                         pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
  327                                         PGRP_UNLOCK(sp->s_ttyp->t_pgrp);
  328                                 }
  329                                 /* XXX tp should be locked. */
  330                                 sx_xunlock(&proctree_lock);
  331                                 (void) ttywait(tp);
  332                                 sx_xlock(&proctree_lock);
  333                                 /*
  334                                  * The tty could have been revoked
  335                                  * if we blocked.
  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                                         sx_xunlock(&proctree_lock);
  343                                         VOP_REVOKE(ttyvp, REVOKEALL);
  344                                         vrele(ttyvp);
  345                                         sx_xlock(&proctree_lock);
  346                                 }
  347                         }
  348                         if (sp->s_ttyvp) {
  349                                 ttyvp = sp->s_ttyvp;
  350                                 SESS_LOCK(p->p_session);
  351                                 sp->s_ttyvp = NULL;
  352                                 SESS_UNLOCK(p->p_session);
  353                                 vrele(ttyvp);
  354                         }
  355                         /*
  356                          * s_ttyp is not zero'd; we use this to indicate
  357                          * that the session once had a controlling terminal.
  358                          * (for logging and informational purposes)
  359                          */
  360                 }
  361                 SESS_LOCK(p->p_session);
  362                 sp->s_leader = NULL;
  363                 SESS_UNLOCK(p->p_session);
  364         }
  365         fixjobc(p, p->p_pgrp, 0);
  366         sx_xunlock(&proctree_lock);
  367         (void)acct_process(td);
  368 #ifdef KTRACE
  369         /*
  370          * release trace file
  371          */
  372         PROC_LOCK(p);
  373         mtx_lock(&ktrace_mtx);
  374         p->p_traceflag = 0;     /* don't trace the vrele() */
  375         tracevp = p->p_tracep;
  376         p->p_tracep = NULL;
  377         mtx_unlock(&ktrace_mtx);
  378         PROC_UNLOCK(p);
  379         if (tracevp != NULL)
  380                 vrele(tracevp);
  381 #endif
  382         /*
  383          * Release reference to text vnode
  384          */
  385         if ((vtmp = p->p_textvp) != NULL) {
  386                 p->p_textvp = NULL;
  387                 vrele(vtmp);
  388         }
  389 
  390         /*
  391          * Release our limits structure.
  392          */
  393         mtx_assert(&Giant, MA_OWNED);
  394         if (--p->p_limit->p_refcnt == 0) {
  395                 FREE(p->p_limit, M_SUBPROC);
  396                 p->p_limit = NULL;
  397         }
  398 
  399         /*
  400          * Release this thread's reference to the ucred.  The actual proc
  401          * reference will stay around until the proc is harvested by
  402          * wait().  At this point the ucred is immutable (no other threads
  403          * from this proc are around that can change it) so we leave the
  404          * per-thread ucred pointer intact in case it is needed although
  405          * in theory nothing should be using it at this point.
  406          */
  407         crfree(td->td_ucred);
  408 
  409         /*
  410          * Remove proc from allproc queue and pidhash chain.
  411          * Place onto zombproc.  Unlink from parent's child list.
  412          */
  413         sx_xlock(&allproc_lock);
  414         LIST_REMOVE(p, p_list);
  415         LIST_INSERT_HEAD(&zombproc, p, p_list);
  416         LIST_REMOVE(p, p_hash);
  417         sx_xunlock(&allproc_lock);
  418 
  419         sx_xlock(&proctree_lock);
  420         q = LIST_FIRST(&p->p_children);
  421         if (q != NULL)          /* only need this if any child is S_ZOMB */
  422                 wakeup(initproc);
  423         for (; q != NULL; q = nq) {
  424                 nq = LIST_NEXT(q, p_sibling);
  425                 PROC_LOCK(q);
  426                 proc_reparent(q, initproc);
  427                 q->p_sigparent = SIGCHLD;
  428                 /*
  429                  * Traced processes are killed
  430                  * since their existence means someone is screwing up.
  431                  */
  432                 if (q->p_flag & P_TRACED) {
  433                         q->p_flag &= ~P_TRACED;
  434                         psignal(q, SIGKILL);
  435                 }
  436                 PROC_UNLOCK(q);
  437         }
  438 
  439         /*
  440          * Save exit status and final rusage info, adding in child rusage
  441          * info and self times.
  442          */
  443         PROC_LOCK(p);
  444         p->p_xstat = rv;
  445         *p->p_ru = p->p_stats->p_ru;
  446         mtx_lock_spin(&sched_lock);
  447         calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
  448         mtx_unlock_spin(&sched_lock);
  449         ruadd(p->p_ru, &p->p_stats->p_cru);
  450 
  451         /*
  452          * Notify interested parties of our demise.
  453          */
  454         KNOTE(&p->p_klist, NOTE_EXIT);
  455 
  456         /*
  457          * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
  458          * flag set, or if the handler is set to SIG_IGN, notify process
  459          * 1 instead (and hope it will handle this situation).
  460          */
  461         PROC_LOCK(p->p_pptr);
  462         if (p->p_pptr->p_procsig->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
  463                 struct proc *pp;
  464 
  465                 pp = p->p_pptr;
  466                 PROC_UNLOCK(pp);
  467                 proc_reparent(p, initproc);
  468                 PROC_LOCK(p->p_pptr);
  469                 /*
  470                  * If this was the last child of our parent, notify
  471                  * parent, so in case he was wait(2)ing, he will
  472                  * continue.
  473                  */
  474                 if (LIST_EMPTY(&pp->p_children))
  475                         wakeup(pp);
  476         }
  477 
  478         if (p->p_sigparent && p->p_pptr != initproc)
  479                 psignal(p->p_pptr, p->p_sigparent);
  480         else
  481                 psignal(p->p_pptr, SIGCHLD);
  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         PROC_LOCK(p);
  500         PROC_LOCK(p->p_pptr);
  501         sx_xunlock(&proctree_lock);
  502         mtx_lock_spin(&sched_lock);
  503 
  504         while (mtx_owned(&Giant))
  505                 mtx_unlock(&Giant);
  506 
  507         /*
  508          * We have to wait until after releasing all locks before
  509          * changing p_state.  If we block on a mutex then we will be
  510          * back at SRUN when we resume and our parent will never
  511          * harvest us.
  512          */
  513         p->p_state = PRS_ZOMBIE;
  514 
  515         wakeup(p->p_pptr);
  516         PROC_UNLOCK(p->p_pptr);
  517         cnt.v_swtch++;
  518         binuptime(PCPU_PTR(switchtime));
  519         PCPU_SET(switchticks, ticks);
  520 
  521         cpu_sched_exit(td); /* XXXKSE check if this should be in thread_exit */
  522         /*
  523          * Make sure the scheduler takes this thread out of its tables etc.
  524          * This will also release this thread's reference to the ucred.
  525          * Other thread parts to release include pcb bits and such.
  526          */
  527         thread_exit();
  528 }
  529 
  530 #ifdef COMPAT_43
  531 /*
  532  * MPSAFE.  The dirty work is handled by wait1().
  533  */
  534 int
  535 owait(td, uap)
  536         struct thread *td;
  537         register struct owait_args /* {
  538                 int     dummy;
  539         } */ *uap;
  540 {
  541         struct wait_args w;
  542 
  543         w.options = 0;
  544         w.rusage = NULL;
  545         w.pid = WAIT_ANY;
  546         w.status = NULL;
  547         return (wait1(td, &w, 1));
  548 }
  549 #endif /* COMPAT_43 */
  550 
  551 /*
  552  * MPSAFE.  The dirty work is handled by wait1().
  553  */
  554 int
  555 wait4(td, uap)
  556         struct thread *td;
  557         struct wait_args *uap;
  558 {
  559 
  560         return (wait1(td, uap, 0));
  561 }
  562 
  563 /*
  564  * MPSAFE
  565  */
  566 static int
  567 wait1(td, uap, compat)
  568         register struct thread *td;
  569         register struct wait_args /* {
  570                 int pid;
  571                 int *status;
  572                 int options;
  573                 struct rusage *rusage;
  574         } */ *uap;
  575         int compat;
  576 {
  577         struct rusage ru;
  578         int nfound;
  579         struct proc *p, *q, *t;
  580         int status, error;
  581 
  582         q = td->td_proc;
  583         if (uap->pid == 0) {
  584                 PROC_LOCK(q);
  585                 uap->pid = -q->p_pgid;
  586                 PROC_UNLOCK(q);
  587         }
  588         if (uap->options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE))
  589                 return (EINVAL);
  590         mtx_lock(&Giant);
  591 loop:
  592         nfound = 0;
  593         sx_xlock(&proctree_lock);
  594         LIST_FOREACH(p, &q->p_children, p_sibling) {
  595                 PROC_LOCK(p);
  596                 if (uap->pid != WAIT_ANY &&
  597                     p->p_pid != uap->pid && p->p_pgid != -uap->pid) {
  598                         PROC_UNLOCK(p);
  599                         continue;
  600                 }
  601 
  602                 /*
  603                  * This special case handles a kthread spawned by linux_clone 
  604                  * (see linux_misc.c).  The linux_wait4 and linux_waitpid
  605                  * functions need to be able to distinguish between waiting
  606                  * on a process and waiting on a thread.  It is a thread if
  607                  * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
  608                  * signifies we want to wait for threads and not processes.
  609                  */
  610                 if ((p->p_sigparent != SIGCHLD) ^
  611                     ((uap->options & WLINUXCLONE) != 0)) {
  612                         PROC_UNLOCK(p);
  613                         continue;
  614                 }
  615 
  616                 nfound++;
  617                 if (p->p_state == PRS_ZOMBIE) {
  618                         /*
  619                          * Allow the scheduler to adjust the priority of the
  620                          * parent when a kseg is exiting.
  621                          */
  622                         if (curthread->td_proc->p_pid != 1) {
  623                                 mtx_lock_spin(&sched_lock);
  624                                 sched_exit(curthread->td_ksegrp,
  625                                     FIRST_KSEGRP_IN_PROC(p));
  626                                 mtx_unlock_spin(&sched_lock);
  627                         }
  628 
  629                         td->td_retval[0] = p->p_pid;
  630 #ifdef COMPAT_43
  631                         if (compat)
  632                                 td->td_retval[1] = p->p_xstat;
  633                         else
  634 #endif
  635                         if (uap->status) {
  636                                 status = p->p_xstat;    /* convert to int */
  637                                 PROC_UNLOCK(p);
  638                                 if ((error = copyout(&status,
  639                                     uap->status, sizeof(status)))) {
  640                                         sx_xunlock(&proctree_lock);
  641                                         mtx_unlock(&Giant);
  642                                         return (error);
  643                                 }
  644                                 PROC_LOCK(p);
  645                         }
  646                         if (uap->rusage) {
  647                                 bcopy(p->p_ru, &ru, sizeof(ru));
  648                                 PROC_UNLOCK(p);
  649                                 if ((error = copyout(&ru,
  650                                     uap->rusage, sizeof (struct rusage)))) {
  651                                         sx_xunlock(&proctree_lock);
  652                                         mtx_unlock(&Giant);
  653                                         return (error);
  654                                 }
  655                         } else
  656                                 PROC_UNLOCK(p);
  657                         /*
  658                          * If we got the child via a ptrace 'attach',
  659                          * we need to give it back to the old parent.
  660                          */
  661                         if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) {
  662                                 PROC_LOCK(p);
  663                                 p->p_oppid = 0;
  664                                 proc_reparent(p, t);
  665                                 PROC_UNLOCK(p);
  666                                 psignal(t, SIGCHLD);
  667                                 wakeup(t);
  668                                 PROC_UNLOCK(t);
  669                                 sx_xunlock(&proctree_lock);
  670                                 mtx_unlock(&Giant);
  671                                 return (0);
  672                         }
  673                         /*
  674                          * Remove other references to this process to ensure
  675                          * we have an exclusive reference.
  676                          */
  677                         leavepgrp(p);
  678 
  679                         sx_xlock(&allproc_lock);
  680                         LIST_REMOVE(p, p_list); /* off zombproc */
  681                         sx_xunlock(&allproc_lock);
  682 
  683                         LIST_REMOVE(p, p_sibling);
  684                         sx_xunlock(&proctree_lock);
  685 
  686                         /*
  687                          * As a side effect of this lock, we know that
  688                          * all other writes to this proc are visible now, so
  689                          * no more locking is needed for p.
  690                          */
  691                         PROC_LOCK(p);
  692                         p->p_xstat = 0;         /* XXX: why? */
  693                         PROC_UNLOCK(p);
  694                         PROC_LOCK(q);
  695                         ruadd(&q->p_stats->p_cru, p->p_ru);
  696                         PROC_UNLOCK(q);
  697                         FREE(p->p_ru, M_ZOMBIE);
  698                         p->p_ru = NULL;
  699 
  700                         /*
  701                          * Decrement the count of procs running with this uid.
  702                          */
  703                         (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
  704 
  705                         /*
  706                          * Free up credentials.
  707                          */
  708                         crfree(p->p_ucred);
  709                         p->p_ucred = NULL;      /* XXX: why? */
  710 
  711                         /*
  712                          * Remove unused arguments
  713                          */
  714                         pargs_drop(p->p_args);
  715                         p->p_args = NULL;
  716 
  717                         if (--p->p_procsig->ps_refcnt == 0) {
  718                                 if (p->p_sigacts != &p->p_uarea->u_sigacts)
  719                                         FREE(p->p_sigacts, M_SUBPROC);
  720                                 FREE(p->p_procsig, M_SUBPROC);
  721                                 p->p_procsig = NULL;
  722                         }
  723 
  724                         /*
  725                          * do any thread-system specific cleanups
  726                          */
  727                         thread_wait(p);
  728 
  729                         /*
  730                          * Give vm and machine-dependent layer a chance
  731                          * to free anything that cpu_exit couldn't
  732                          * release while still running in process context.
  733                          */
  734                         vm_waitproc(p);
  735                         mtx_destroy(&p->p_mtx);
  736 #ifdef MAC
  737                         mac_destroy_proc(p);
  738 #endif
  739                         KASSERT(FIRST_THREAD_IN_PROC(p),
  740                             ("wait1: no residual thread!"));
  741                         uma_zfree(proc_zone, p);
  742                         sx_xlock(&allproc_lock);
  743                         nprocs--;
  744                         sx_xunlock(&allproc_lock);
  745                         mtx_unlock(&Giant);
  746                         return (0);
  747                 }
  748                 if (P_SHOULDSTOP(p) && ((p->p_flag & P_WAITED) == 0) &&
  749                     (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
  750                         p->p_flag |= P_WAITED;
  751                         sx_xunlock(&proctree_lock);
  752                         td->td_retval[0] = p->p_pid;
  753 #ifdef COMPAT_43
  754                         if (compat) {
  755                                 td->td_retval[1] = W_STOPCODE(p->p_xstat);
  756                                 PROC_UNLOCK(p);
  757                                 error = 0;
  758                         } else
  759 #endif
  760                         if (uap->status) {
  761                                 status = W_STOPCODE(p->p_xstat);
  762                                 PROC_UNLOCK(p);
  763                                 error = copyout(&status,
  764                                         uap->status, sizeof(status));
  765                         } else {
  766                                 PROC_UNLOCK(p);
  767                                 error = 0;
  768                         }
  769                         mtx_unlock(&Giant);
  770                         return (error);
  771                 }
  772                 if (uap->options & WCONTINUED && (p->p_flag & P_CONTINUED)) {
  773                         sx_xunlock(&proctree_lock);
  774                         td->td_retval[0] = p->p_pid;
  775                         p->p_flag &= ~P_CONTINUED;
  776                         PROC_UNLOCK(p);
  777 
  778                         if (uap->status) {
  779                                 status = SIGCONT;
  780                                 error = copyout(&status,
  781                                     uap->status, sizeof(status));
  782                         } else
  783                                 error = 0;
  784 
  785                         mtx_unlock(&Giant);
  786                         return (error);
  787                 }
  788                 PROC_UNLOCK(p);
  789         }
  790         if (nfound == 0) {
  791                 sx_xunlock(&proctree_lock);
  792                 mtx_unlock(&Giant);
  793                 return (ECHILD);
  794         }
  795         if (uap->options & WNOHANG) {
  796                 sx_xunlock(&proctree_lock);
  797                 td->td_retval[0] = 0;
  798                 mtx_unlock(&Giant);
  799                 return (0);
  800         }
  801         PROC_LOCK(q);
  802         sx_xunlock(&proctree_lock);
  803         error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0);
  804         PROC_UNLOCK(q);
  805         if (error) {
  806                 mtx_unlock(&Giant);
  807                 return (error);
  808         }
  809         goto loop;
  810 }
  811 
  812 /*
  813  * Make process 'parent' the new parent of process 'child'.
  814  * Must be called with an exclusive hold of proctree lock.
  815  */
  816 void
  817 proc_reparent(child, parent)
  818         register struct proc *child;
  819         register struct proc *parent;
  820 {
  821 
  822         sx_assert(&proctree_lock, SX_XLOCKED);
  823         PROC_LOCK_ASSERT(child, MA_OWNED);
  824         if (child->p_pptr == parent)
  825                 return;
  826 
  827         LIST_REMOVE(child, p_sibling);
  828         LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
  829         child->p_pptr = parent;
  830 }
  831 
  832 /*
  833  * The next two functions are to handle adding/deleting items on the
  834  * exit callout list
  835  * 
  836  * at_exit():
  837  * Take the arguments given and put them onto the exit callout list,
  838  * However first make sure that it's not already there.
  839  * returns 0 on success.
  840  */
  841 
  842 int
  843 at_exit(function)
  844         exitlist_fn function;
  845 {
  846         struct exitlist *ep;
  847 
  848 #ifdef INVARIANTS
  849         /* Be noisy if the programmer has lost track of things */
  850         if (rm_at_exit(function)) 
  851                 printf("WARNING: exit callout entry (%p) already present\n",
  852                     function);
  853 #endif
  854         ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
  855         if (ep == NULL)
  856                 return (ENOMEM);
  857         ep->function = function;
  858         TAILQ_INSERT_TAIL(&exit_list, ep, next);
  859         return (0);
  860 }
  861 
  862 /*
  863  * Scan the exit callout list for the given item and remove it.
  864  * Returns the number of items removed (0 or 1)
  865  */
  866 int
  867 rm_at_exit(function)
  868         exitlist_fn function;
  869 {
  870         struct exitlist *ep;
  871 
  872         TAILQ_FOREACH(ep, &exit_list, next) {
  873                 if (ep->function == function) {
  874                         TAILQ_REMOVE(&exit_list, ep, next);
  875                         free(ep, M_ATEXIT);
  876                         return (1);
  877                 }
  878         }
  879         return (0);
  880 }

Cache object: b987350877673ee7b756f6a4724ec958


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