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


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

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

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

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

Cache object: 96d760d9a33d51a55c92afadeca0485b


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