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


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

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

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

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

Cache object: 28764f763d9f2400ece3f982b8d28170


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