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_sig.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_sig.c  8.7 (Berkeley) 4/18/94
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD: src/sys/kern/kern_sig.c,v 1.289.2.9 2006/02/28 15:02:51 emaste Exp $");
   39 
   40 #include "opt_compat.h"
   41 #include "opt_ktrace.h"
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/signalvar.h>
   46 #include <sys/vnode.h>
   47 #include <sys/acct.h>
   48 #include <sys/condvar.h>
   49 #include <sys/event.h>
   50 #include <sys/fcntl.h>
   51 #include <sys/kernel.h>
   52 #include <sys/kse.h>
   53 #include <sys/ktr.h>
   54 #include <sys/ktrace.h>
   55 #include <sys/lock.h>
   56 #include <sys/malloc.h>
   57 #include <sys/mutex.h>
   58 #include <sys/namei.h>
   59 #include <sys/proc.h>
   60 #include <sys/pioctl.h>
   61 #include <sys/resourcevar.h>
   62 #include <sys/sleepqueue.h>
   63 #include <sys/smp.h>
   64 #include <sys/stat.h>
   65 #include <sys/sx.h>
   66 #include <sys/syscallsubr.h>
   67 #include <sys/sysctl.h>
   68 #include <sys/sysent.h>
   69 #include <sys/syslog.h>
   70 #include <sys/sysproto.h>
   71 #include <sys/unistd.h>
   72 #include <sys/wait.h>
   73 
   74 #include <machine/cpu.h>
   75 
   76 #if defined (__alpha__) && !defined(COMPAT_43)
   77 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
   78 #endif
   79 
   80 #define ONSIG   32              /* NSIG for osig* syscalls.  XXX. */
   81 
   82 static int      coredump(struct thread *);
   83 static char     *expand_name(const char *, uid_t, pid_t);
   84 static int      killpg1(struct thread *td, int sig, int pgid, int all);
   85 static int      issignal(struct thread *p);
   86 static int      sigprop(int sig);
   87 static void     tdsigwakeup(struct thread *td, int sig, sig_t action);
   88 static int      filt_sigattach(struct knote *kn);
   89 static void     filt_sigdetach(struct knote *kn);
   90 static int      filt_signal(struct knote *kn, long hint);
   91 static struct thread *sigtd(struct proc *p, int sig, int prop);
   92 static int      kern_sigtimedwait(struct thread *td, sigset_t set,
   93                                 siginfo_t *info, struct timespec *timeout);
   94 static void     do_tdsignal(struct thread *td, int sig, sigtarget_t target);
   95 
   96 struct filterops sig_filtops =
   97         { 0, filt_sigattach, filt_sigdetach, filt_signal };
   98 
   99 static int      kern_logsigexit = 1;
  100 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 
  101     &kern_logsigexit, 0, 
  102     "Log processes quitting on abnormal signals to syslog(3)");
  103 
  104 /*
  105  * Policy -- Can ucred cr1 send SIGIO to process cr2?
  106  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
  107  * in the right situations.
  108  */
  109 #define CANSIGIO(cr1, cr2) \
  110         ((cr1)->cr_uid == 0 || \
  111             (cr1)->cr_ruid == (cr2)->cr_ruid || \
  112             (cr1)->cr_uid == (cr2)->cr_ruid || \
  113             (cr1)->cr_ruid == (cr2)->cr_uid || \
  114             (cr1)->cr_uid == (cr2)->cr_uid)
  115 
  116 int sugid_coredump;
  117 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 
  118     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
  119 
  120 static int      do_coredump = 1;
  121 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
  122         &do_coredump, 0, "Enable/Disable coredumps");
  123 
  124 static int      set_core_nodump_flag = 0;
  125 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
  126         0, "Enable setting the NODUMP flag on coredump files");
  127 
  128 /*
  129  * Signal properties and actions.
  130  * The array below categorizes the signals and their default actions
  131  * according to the following properties:
  132  */
  133 #define SA_KILL         0x01            /* terminates process by default */
  134 #define SA_CORE         0x02            /* ditto and coredumps */
  135 #define SA_STOP         0x04            /* suspend process */
  136 #define SA_TTYSTOP      0x08            /* ditto, from tty */
  137 #define SA_IGNORE       0x10            /* ignore by default */
  138 #define SA_CONT         0x20            /* continue if suspended */
  139 #define SA_CANTMASK     0x40            /* non-maskable, catchable */
  140 #define SA_PROC         0x80            /* deliverable to any thread */
  141 
  142 static int sigproptbl[NSIG] = {
  143         SA_KILL|SA_PROC,                /* SIGHUP */
  144         SA_KILL|SA_PROC,                /* SIGINT */
  145         SA_KILL|SA_CORE|SA_PROC,        /* SIGQUIT */
  146         SA_KILL|SA_CORE,                /* SIGILL */
  147         SA_KILL|SA_CORE,                /* SIGTRAP */
  148         SA_KILL|SA_CORE,                /* SIGABRT */
  149         SA_KILL|SA_CORE|SA_PROC,        /* SIGEMT */
  150         SA_KILL|SA_CORE,                /* SIGFPE */
  151         SA_KILL|SA_PROC,                /* SIGKILL */
  152         SA_KILL|SA_CORE,                /* SIGBUS */
  153         SA_KILL|SA_CORE,                /* SIGSEGV */
  154         SA_KILL|SA_CORE,                /* SIGSYS */
  155         SA_KILL|SA_PROC,                /* SIGPIPE */
  156         SA_KILL|SA_PROC,                /* SIGALRM */
  157         SA_KILL|SA_PROC,                /* SIGTERM */
  158         SA_IGNORE|SA_PROC,              /* SIGURG */
  159         SA_STOP|SA_PROC,                /* SIGSTOP */
  160         SA_STOP|SA_TTYSTOP|SA_PROC,     /* SIGTSTP */
  161         SA_IGNORE|SA_CONT|SA_PROC,      /* SIGCONT */
  162         SA_IGNORE|SA_PROC,              /* SIGCHLD */
  163         SA_STOP|SA_TTYSTOP|SA_PROC,     /* SIGTTIN */
  164         SA_STOP|SA_TTYSTOP|SA_PROC,     /* SIGTTOU */
  165         SA_IGNORE|SA_PROC,              /* SIGIO */
  166         SA_KILL,                        /* SIGXCPU */
  167         SA_KILL,                        /* SIGXFSZ */
  168         SA_KILL|SA_PROC,                /* SIGVTALRM */
  169         SA_KILL|SA_PROC,                /* SIGPROF */
  170         SA_IGNORE|SA_PROC,              /* SIGWINCH  */
  171         SA_IGNORE|SA_PROC,              /* SIGINFO */
  172         SA_KILL|SA_PROC,                /* SIGUSR1 */
  173         SA_KILL|SA_PROC,                /* SIGUSR2 */
  174 };
  175 
  176 /*
  177  * Determine signal that should be delivered to process p, the current
  178  * process, 0 if none.  If there is a pending stop signal with default
  179  * action, the process stops in issignal().
  180  * XXXKSE   the check for a pending stop is not done under KSE
  181  *
  182  * MP SAFE.
  183  */
  184 int
  185 cursig(struct thread *td)
  186 {
  187         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
  188         mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
  189         mtx_assert(&sched_lock, MA_NOTOWNED);
  190         return (SIGPENDING(td) ? issignal(td) : 0);
  191 }
  192 
  193 /*
  194  * Arrange for ast() to handle unmasked pending signals on return to user
  195  * mode.  This must be called whenever a signal is added to td_siglist or
  196  * unmasked in td_sigmask.
  197  */
  198 void
  199 signotify(struct thread *td)
  200 {
  201         struct proc *p;
  202         sigset_t set, saved;
  203 
  204         p = td->td_proc;
  205 
  206         PROC_LOCK_ASSERT(p, MA_OWNED);
  207 
  208         /*
  209          * If our mask changed we may have to move signal that were
  210          * previously masked by all threads to our siglist.
  211          */
  212         set = p->p_siglist;
  213         if (p->p_flag & P_SA)
  214                 saved = p->p_siglist;
  215         SIGSETNAND(set, td->td_sigmask);
  216         SIGSETNAND(p->p_siglist, set);
  217         SIGSETOR(td->td_siglist, set);
  218 
  219         if (SIGPENDING(td)) {
  220                 mtx_lock_spin(&sched_lock);
  221                 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
  222                 mtx_unlock_spin(&sched_lock);
  223         }
  224         if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
  225                 if (!SIGSETEQ(saved, p->p_siglist)) {
  226                         /* pending set changed */
  227                         p->p_flag |= P_SIGEVENT;
  228                         wakeup(&p->p_siglist);
  229                 }
  230         }
  231 }
  232 
  233 int
  234 sigonstack(size_t sp)
  235 {
  236         struct thread *td = curthread;
  237 
  238         return ((td->td_pflags & TDP_ALTSTACK) ?
  239 #if defined(COMPAT_43)
  240             ((td->td_sigstk.ss_size == 0) ?
  241                 (td->td_sigstk.ss_flags & SS_ONSTACK) :
  242                 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
  243 #else
  244             ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
  245 #endif
  246             : 0);
  247 }
  248 
  249 static __inline int
  250 sigprop(int sig)
  251 {
  252 
  253         if (sig > 0 && sig < NSIG)
  254                 return (sigproptbl[_SIG_IDX(sig)]);
  255         return (0);
  256 }
  257 
  258 int
  259 sig_ffs(sigset_t *set)
  260 {
  261         int i;
  262 
  263         for (i = 0; i < _SIG_WORDS; i++)
  264                 if (set->__bits[i])
  265                         return (ffs(set->__bits[i]) + (i * 32));
  266         return (0);
  267 }
  268 
  269 /*
  270  * kern_sigaction
  271  * sigaction
  272  * freebsd4_sigaction
  273  * osigaction
  274  *
  275  * MPSAFE
  276  */
  277 int
  278 kern_sigaction(td, sig, act, oact, flags)
  279         struct thread *td;
  280         register int sig;
  281         struct sigaction *act, *oact;
  282         int flags;
  283 {
  284         struct sigacts *ps;
  285         struct thread *td0;
  286         struct proc *p = td->td_proc;
  287 
  288         if (!_SIG_VALID(sig))
  289                 return (EINVAL);
  290 
  291         PROC_LOCK(p);
  292         ps = p->p_sigacts;
  293         mtx_lock(&ps->ps_mtx);
  294         if (oact) {
  295                 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
  296                 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
  297                 oact->sa_flags = 0;
  298                 if (SIGISMEMBER(ps->ps_sigonstack, sig))
  299                         oact->sa_flags |= SA_ONSTACK;
  300                 if (!SIGISMEMBER(ps->ps_sigintr, sig))
  301                         oact->sa_flags |= SA_RESTART;
  302                 if (SIGISMEMBER(ps->ps_sigreset, sig))
  303                         oact->sa_flags |= SA_RESETHAND;
  304                 if (SIGISMEMBER(ps->ps_signodefer, sig))
  305                         oact->sa_flags |= SA_NODEFER;
  306                 if (SIGISMEMBER(ps->ps_siginfo, sig))
  307                         oact->sa_flags |= SA_SIGINFO;
  308                 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
  309                         oact->sa_flags |= SA_NOCLDSTOP;
  310                 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
  311                         oact->sa_flags |= SA_NOCLDWAIT;
  312         }
  313         if (act) {
  314                 if ((sig == SIGKILL || sig == SIGSTOP) &&
  315                     act->sa_handler != SIG_DFL) {
  316                         mtx_unlock(&ps->ps_mtx);
  317                         PROC_UNLOCK(p);
  318                         return (EINVAL);
  319                 }
  320 
  321                 /*
  322                  * Change setting atomically.
  323                  */
  324 
  325                 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
  326                 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
  327                 if (act->sa_flags & SA_SIGINFO) {
  328                         ps->ps_sigact[_SIG_IDX(sig)] =
  329                             (__sighandler_t *)act->sa_sigaction;
  330                         SIGADDSET(ps->ps_siginfo, sig);
  331                 } else {
  332                         ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
  333                         SIGDELSET(ps->ps_siginfo, sig);
  334                 }
  335                 if (!(act->sa_flags & SA_RESTART))
  336                         SIGADDSET(ps->ps_sigintr, sig);
  337                 else
  338                         SIGDELSET(ps->ps_sigintr, sig);
  339                 if (act->sa_flags & SA_ONSTACK)
  340                         SIGADDSET(ps->ps_sigonstack, sig);
  341                 else
  342                         SIGDELSET(ps->ps_sigonstack, sig);
  343                 if (act->sa_flags & SA_RESETHAND)
  344                         SIGADDSET(ps->ps_sigreset, sig);
  345                 else
  346                         SIGDELSET(ps->ps_sigreset, sig);
  347                 if (act->sa_flags & SA_NODEFER)
  348                         SIGADDSET(ps->ps_signodefer, sig);
  349                 else
  350                         SIGDELSET(ps->ps_signodefer, sig);
  351                 if (sig == SIGCHLD) {
  352                         if (act->sa_flags & SA_NOCLDSTOP)
  353                                 ps->ps_flag |= PS_NOCLDSTOP;
  354                         else
  355                                 ps->ps_flag &= ~PS_NOCLDSTOP;
  356                         if (act->sa_flags & SA_NOCLDWAIT) {
  357                                 /*
  358                                  * Paranoia: since SA_NOCLDWAIT is implemented
  359                                  * by reparenting the dying child to PID 1 (and
  360                                  * trust it to reap the zombie), PID 1 itself
  361                                  * is forbidden to set SA_NOCLDWAIT.
  362                                  */
  363                                 if (p->p_pid == 1)
  364                                         ps->ps_flag &= ~PS_NOCLDWAIT;
  365                                 else
  366                                         ps->ps_flag |= PS_NOCLDWAIT;
  367                         } else
  368                                 ps->ps_flag &= ~PS_NOCLDWAIT;
  369                         if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
  370                                 ps->ps_flag |= PS_CLDSIGIGN;
  371                         else
  372                                 ps->ps_flag &= ~PS_CLDSIGIGN;
  373                 }
  374                 /*
  375                  * Set bit in ps_sigignore for signals that are set to SIG_IGN,
  376                  * and for signals set to SIG_DFL where the default is to
  377                  * ignore. However, don't put SIGCONT in ps_sigignore, as we
  378                  * have to restart the process.
  379                  */
  380                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
  381                     (sigprop(sig) & SA_IGNORE &&
  382                      ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
  383                         if ((p->p_flag & P_SA) &&
  384                              SIGISMEMBER(p->p_siglist, sig)) {
  385                                 p->p_flag |= P_SIGEVENT;
  386                                 wakeup(&p->p_siglist);
  387                         }
  388                         /* never to be seen again */
  389                         SIGDELSET(p->p_siglist, sig);
  390                         mtx_lock_spin(&sched_lock);
  391                         FOREACH_THREAD_IN_PROC(p, td0)
  392                                 SIGDELSET(td0->td_siglist, sig);
  393                         mtx_unlock_spin(&sched_lock);
  394                         if (sig != SIGCONT)
  395                                 /* easier in psignal */
  396                                 SIGADDSET(ps->ps_sigignore, sig);
  397                         SIGDELSET(ps->ps_sigcatch, sig);
  398                 } else {
  399                         SIGDELSET(ps->ps_sigignore, sig);
  400                         if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
  401                                 SIGDELSET(ps->ps_sigcatch, sig);
  402                         else
  403                                 SIGADDSET(ps->ps_sigcatch, sig);
  404                 }
  405 #ifdef COMPAT_FREEBSD4
  406                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
  407                     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
  408                     (flags & KSA_FREEBSD4) == 0)
  409                         SIGDELSET(ps->ps_freebsd4, sig);
  410                 else
  411                         SIGADDSET(ps->ps_freebsd4, sig);
  412 #endif
  413 #ifdef COMPAT_43
  414                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
  415                     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
  416                     (flags & KSA_OSIGSET) == 0)
  417                         SIGDELSET(ps->ps_osigset, sig);
  418                 else
  419                         SIGADDSET(ps->ps_osigset, sig);
  420 #endif
  421         }
  422         mtx_unlock(&ps->ps_mtx);
  423         PROC_UNLOCK(p);
  424         return (0);
  425 }
  426 
  427 #ifndef _SYS_SYSPROTO_H_
  428 struct sigaction_args {
  429         int     sig;
  430         struct  sigaction *act;
  431         struct  sigaction *oact;
  432 };
  433 #endif
  434 /*
  435  * MPSAFE
  436  */
  437 int
  438 sigaction(td, uap)
  439         struct thread *td;
  440         register struct sigaction_args *uap;
  441 {
  442         struct sigaction act, oact;
  443         register struct sigaction *actp, *oactp;
  444         int error;
  445 
  446         actp = (uap->act != NULL) ? &act : NULL;
  447         oactp = (uap->oact != NULL) ? &oact : NULL;
  448         if (actp) {
  449                 error = copyin(uap->act, actp, sizeof(act));
  450                 if (error)
  451                         return (error);
  452         }
  453         error = kern_sigaction(td, uap->sig, actp, oactp, 0);
  454         if (oactp && !error)
  455                 error = copyout(oactp, uap->oact, sizeof(oact));
  456         return (error);
  457 }
  458 
  459 #ifdef COMPAT_FREEBSD4
  460 #ifndef _SYS_SYSPROTO_H_
  461 struct freebsd4_sigaction_args {
  462         int     sig;
  463         struct  sigaction *act;
  464         struct  sigaction *oact;
  465 };
  466 #endif
  467 /*
  468  * MPSAFE
  469  */
  470 int
  471 freebsd4_sigaction(td, uap)
  472         struct thread *td;
  473         register struct freebsd4_sigaction_args *uap;
  474 {
  475         struct sigaction act, oact;
  476         register struct sigaction *actp, *oactp;
  477         int error;
  478 
  479 
  480         actp = (uap->act != NULL) ? &act : NULL;
  481         oactp = (uap->oact != NULL) ? &oact : NULL;
  482         if (actp) {
  483                 error = copyin(uap->act, actp, sizeof(act));
  484                 if (error)
  485                         return (error);
  486         }
  487         error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
  488         if (oactp && !error)
  489                 error = copyout(oactp, uap->oact, sizeof(oact));
  490         return (error);
  491 }
  492 #endif  /* COMAPT_FREEBSD4 */
  493 
  494 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
  495 #ifndef _SYS_SYSPROTO_H_
  496 struct osigaction_args {
  497         int     signum;
  498         struct  osigaction *nsa;
  499         struct  osigaction *osa;
  500 };
  501 #endif
  502 /*
  503  * MPSAFE
  504  */
  505 int
  506 osigaction(td, uap)
  507         struct thread *td;
  508         register struct osigaction_args *uap;
  509 {
  510         struct osigaction sa;
  511         struct sigaction nsa, osa;
  512         register struct sigaction *nsap, *osap;
  513         int error;
  514 
  515         if (uap->signum <= 0 || uap->signum >= ONSIG)
  516                 return (EINVAL);
  517 
  518         nsap = (uap->nsa != NULL) ? &nsa : NULL;
  519         osap = (uap->osa != NULL) ? &osa : NULL;
  520 
  521         if (nsap) {
  522                 error = copyin(uap->nsa, &sa, sizeof(sa));
  523                 if (error)
  524                         return (error);
  525                 nsap->sa_handler = sa.sa_handler;
  526                 nsap->sa_flags = sa.sa_flags;
  527                 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
  528         }
  529         error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
  530         if (osap && !error) {
  531                 sa.sa_handler = osap->sa_handler;
  532                 sa.sa_flags = osap->sa_flags;
  533                 SIG2OSIG(osap->sa_mask, sa.sa_mask);
  534                 error = copyout(&sa, uap->osa, sizeof(sa));
  535         }
  536         return (error);
  537 }
  538 
  539 #if !defined(__i386__) && !defined(__alpha__)
  540 /* Avoid replicating the same stub everywhere */
  541 int
  542 osigreturn(td, uap)
  543         struct thread *td;
  544         struct osigreturn_args *uap;
  545 {
  546 
  547         return (nosys(td, (struct nosys_args *)uap));
  548 }
  549 #endif
  550 #endif /* COMPAT_43 */
  551 
  552 /*
  553  * Initialize signal state for process 0;
  554  * set to ignore signals that are ignored by default.
  555  */
  556 void
  557 siginit(p)
  558         struct proc *p;
  559 {
  560         register int i;
  561         struct sigacts *ps;
  562 
  563         PROC_LOCK(p);
  564         ps = p->p_sigacts;
  565         mtx_lock(&ps->ps_mtx);
  566         for (i = 1; i <= NSIG; i++)
  567                 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
  568                         SIGADDSET(ps->ps_sigignore, i);
  569         mtx_unlock(&ps->ps_mtx);
  570         PROC_UNLOCK(p);
  571 }
  572 
  573 /*
  574  * Reset signals for an exec of the specified process.
  575  */
  576 void
  577 execsigs(struct proc *p)
  578 {
  579         struct sigacts *ps;
  580         int sig;
  581         struct thread *td;
  582 
  583         /*
  584          * Reset caught signals.  Held signals remain held
  585          * through td_sigmask (unless they were caught,
  586          * and are now ignored by default).
  587          */
  588         PROC_LOCK_ASSERT(p, MA_OWNED);
  589         td = FIRST_THREAD_IN_PROC(p);
  590         ps = p->p_sigacts;
  591         mtx_lock(&ps->ps_mtx);
  592         while (SIGNOTEMPTY(ps->ps_sigcatch)) {
  593                 sig = sig_ffs(&ps->ps_sigcatch);
  594                 SIGDELSET(ps->ps_sigcatch, sig);
  595                 if (sigprop(sig) & SA_IGNORE) {
  596                         if (sig != SIGCONT)
  597                                 SIGADDSET(ps->ps_sigignore, sig);
  598                         SIGDELSET(p->p_siglist, sig);
  599                         /*
  600                          * There is only one thread at this point.
  601                          */
  602                         SIGDELSET(td->td_siglist, sig);
  603                 }
  604                 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
  605         }
  606         /*
  607          * Reset stack state to the user stack.
  608          * Clear set of signals caught on the signal stack.
  609          */
  610         td->td_sigstk.ss_flags = SS_DISABLE;
  611         td->td_sigstk.ss_size = 0;
  612         td->td_sigstk.ss_sp = 0;
  613         td->td_pflags &= ~TDP_ALTSTACK;
  614         /*
  615          * Reset no zombies if child dies flag as Solaris does.
  616          */
  617         ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
  618         if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
  619                 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
  620         mtx_unlock(&ps->ps_mtx);
  621 }
  622 
  623 /*
  624  * kern_sigprocmask()
  625  *
  626  *      Manipulate signal mask.
  627  */
  628 int
  629 kern_sigprocmask(td, how, set, oset, old)
  630         struct thread *td;
  631         int how;
  632         sigset_t *set, *oset;
  633         int old;
  634 {
  635         int error;
  636 
  637         PROC_LOCK(td->td_proc);
  638         if (oset != NULL)
  639                 *oset = td->td_sigmask;
  640 
  641         error = 0;
  642         if (set != NULL) {
  643                 switch (how) {
  644                 case SIG_BLOCK:
  645                         SIG_CANTMASK(*set);
  646                         SIGSETOR(td->td_sigmask, *set);
  647                         break;
  648                 case SIG_UNBLOCK:
  649                         SIGSETNAND(td->td_sigmask, *set);
  650                         signotify(td);
  651                         break;
  652                 case SIG_SETMASK:
  653                         SIG_CANTMASK(*set);
  654                         if (old)
  655                                 SIGSETLO(td->td_sigmask, *set);
  656                         else
  657                                 td->td_sigmask = *set;
  658                         signotify(td);
  659                         break;
  660                 default:
  661                         error = EINVAL;
  662                         break;
  663                 }
  664         }
  665         PROC_UNLOCK(td->td_proc);
  666         return (error);
  667 }
  668 
  669 /*
  670  * sigprocmask() - MP SAFE
  671  */
  672 
  673 #ifndef _SYS_SYSPROTO_H_
  674 struct sigprocmask_args {
  675         int     how;
  676         const sigset_t *set;
  677         sigset_t *oset;
  678 };
  679 #endif
  680 int
  681 sigprocmask(td, uap)
  682         register struct thread *td;
  683         struct sigprocmask_args *uap;
  684 {
  685         sigset_t set, oset;
  686         sigset_t *setp, *osetp;
  687         int error;
  688 
  689         setp = (uap->set != NULL) ? &set : NULL;
  690         osetp = (uap->oset != NULL) ? &oset : NULL;
  691         if (setp) {
  692                 error = copyin(uap->set, setp, sizeof(set));
  693                 if (error)
  694                         return (error);
  695         }
  696         error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
  697         if (osetp && !error) {
  698                 error = copyout(osetp, uap->oset, sizeof(oset));
  699         }
  700         return (error);
  701 }
  702 
  703 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
  704 /*
  705  * osigprocmask() - MP SAFE
  706  */
  707 #ifndef _SYS_SYSPROTO_H_
  708 struct osigprocmask_args {
  709         int     how;
  710         osigset_t mask;
  711 };
  712 #endif
  713 int
  714 osigprocmask(td, uap)
  715         register struct thread *td;
  716         struct osigprocmask_args *uap;
  717 {
  718         sigset_t set, oset;
  719         int error;
  720 
  721         OSIG2SIG(uap->mask, set);
  722         error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
  723         SIG2OSIG(oset, td->td_retval[0]);
  724         return (error);
  725 }
  726 #endif /* COMPAT_43 */
  727 
  728 #ifndef _SYS_SYSPROTO_H_
  729 struct sigpending_args {
  730         sigset_t        *set;
  731 };
  732 #endif
  733 /*
  734  * MPSAFE
  735  */
  736 int
  737 sigwait(struct thread *td, struct sigwait_args *uap)
  738 {
  739         siginfo_t info;
  740         sigset_t set;
  741         int error;
  742 
  743         error = copyin(uap->set, &set, sizeof(set));
  744         if (error) {
  745                 td->td_retval[0] = error;
  746                 return (0);
  747         }
  748 
  749         error = kern_sigtimedwait(td, set, &info, NULL);
  750         if (error) {
  751                 if (error == ERESTART)
  752                         return (error);
  753                 td->td_retval[0] = error;
  754                 return (0);
  755         }
  756 
  757         error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
  758         /* Repost if we got an error. */
  759         if (error && info.si_signo) {
  760                 PROC_LOCK(td->td_proc);
  761                 tdsignal(td, info.si_signo, SIGTARGET_TD);
  762                 PROC_UNLOCK(td->td_proc);
  763         }
  764         td->td_retval[0] = error;
  765         return (0);
  766 }
  767 /*
  768  * MPSAFE
  769  */
  770 int
  771 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
  772 {
  773         struct timespec ts;
  774         struct timespec *timeout;
  775         sigset_t set;
  776         siginfo_t info;
  777         int error;
  778 
  779         if (uap->timeout) {
  780                 error = copyin(uap->timeout, &ts, sizeof(ts));
  781                 if (error)
  782                         return (error);
  783 
  784                 timeout = &ts;
  785         } else
  786                 timeout = NULL;
  787 
  788         error = copyin(uap->set, &set, sizeof(set));
  789         if (error)
  790                 return (error);
  791 
  792         error = kern_sigtimedwait(td, set, &info, timeout);
  793         if (error)
  794                 return (error);
  795 
  796         if (uap->info)
  797                 error = copyout(&info, uap->info, sizeof(info));
  798         /* Repost if we got an error. */
  799         if (error && info.si_signo) {
  800                 PROC_LOCK(td->td_proc);
  801                 tdsignal(td, info.si_signo, SIGTARGET_TD);
  802                 PROC_UNLOCK(td->td_proc);
  803         } else {
  804                 td->td_retval[0] = info.si_signo; 
  805         }
  806         return (error);
  807 }
  808 
  809 /*
  810  * MPSAFE
  811  */
  812 int
  813 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
  814 {
  815         siginfo_t info;
  816         sigset_t set;
  817         int error;
  818 
  819         error = copyin(uap->set, &set, sizeof(set));
  820         if (error)
  821                 return (error);
  822 
  823         error = kern_sigtimedwait(td, set, &info, NULL);
  824         if (error)
  825                 return (error);
  826 
  827         if (uap->info)
  828                 error = copyout(&info, uap->info, sizeof(info));
  829         /* Repost if we got an error. */
  830         if (error && info.si_signo) {
  831                 PROC_LOCK(td->td_proc);
  832                 tdsignal(td, info.si_signo, SIGTARGET_TD);
  833                 PROC_UNLOCK(td->td_proc);
  834         } else {
  835                 td->td_retval[0] = info.si_signo;
  836         }
  837         return (error);
  838 }
  839 
  840 static int
  841 kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
  842     struct timespec *timeout)
  843 {
  844         struct sigacts *ps;
  845         sigset_t savedmask;
  846         struct proc *p;
  847         int error, sig, hz, i, timevalid = 0;
  848         struct timespec rts, ets, ts;
  849         struct timeval tv;
  850 
  851         p = td->td_proc;
  852         error = 0;
  853         sig = 0;
  854         SIG_CANTMASK(waitset);
  855 
  856         PROC_LOCK(p);
  857         ps = p->p_sigacts;
  858         savedmask = td->td_sigmask;
  859         if (timeout) {
  860                 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
  861                         timevalid = 1;
  862                         getnanouptime(&rts);
  863                         ets = rts;
  864                         timespecadd(&ets, timeout);
  865                 }
  866         }
  867 
  868 again:
  869         for (i = 1; i <= _SIG_MAXSIG; ++i) {
  870                 if (!SIGISMEMBER(waitset, i))
  871                         continue;
  872                 if (SIGISMEMBER(td->td_siglist, i)) {
  873                         SIGFILLSET(td->td_sigmask);
  874                         SIG_CANTMASK(td->td_sigmask);
  875                         SIGDELSET(td->td_sigmask, i);
  876                         mtx_lock(&ps->ps_mtx);
  877                         sig = cursig(td);
  878                         i = 0;
  879                         mtx_unlock(&ps->ps_mtx);
  880                 } else if (SIGISMEMBER(p->p_siglist, i)) {
  881                         if (p->p_flag & P_SA) {
  882                                 p->p_flag |= P_SIGEVENT;
  883                                 wakeup(&p->p_siglist);
  884                         }
  885                         SIGDELSET(p->p_siglist, i);
  886                         SIGADDSET(td->td_siglist, i);
  887                         SIGFILLSET(td->td_sigmask);
  888                         SIG_CANTMASK(td->td_sigmask);
  889                         SIGDELSET(td->td_sigmask, i);
  890                         mtx_lock(&ps->ps_mtx);
  891                         sig = cursig(td);
  892                         i = 0;
  893                         mtx_unlock(&ps->ps_mtx);
  894                 }
  895                 if (sig)
  896                         goto out;
  897         }
  898         if (error)
  899                 goto out;
  900 
  901         /*
  902          * POSIX says this must be checked after looking for pending
  903          * signals.
  904          */
  905         if (timeout) {
  906                 if (!timevalid) {
  907                         error = EINVAL;
  908                         goto out;
  909                 }
  910                 getnanouptime(&rts);
  911                 if (timespeccmp(&rts, &ets, >=)) {
  912                         error = EAGAIN;
  913                         goto out;
  914                 }
  915                 ts = ets;
  916                 timespecsub(&ts, &rts);
  917                 TIMESPEC_TO_TIMEVAL(&tv, &ts);
  918                 hz = tvtohz(&tv);
  919         } else
  920                 hz = 0;
  921 
  922         td->td_sigmask = savedmask;
  923         SIGSETNAND(td->td_sigmask, waitset);
  924         signotify(td);
  925         error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
  926         if (timeout) {
  927                 if (error == ERESTART) {
  928                         /* timeout can not be restarted. */
  929                         error = EINTR;
  930                 } else if (error == EAGAIN) {
  931                         /* will calculate timeout by ourself. */
  932                         error = 0;
  933                 }
  934         }
  935         goto again;
  936 
  937 out:
  938         td->td_sigmask = savedmask;
  939         signotify(td);
  940         if (sig) {
  941                 sig_t action;
  942 
  943                 error = 0;
  944                 mtx_lock(&ps->ps_mtx);
  945                 action = ps->ps_sigact[_SIG_IDX(sig)];
  946                 mtx_unlock(&ps->ps_mtx);
  947 #ifdef KTRACE
  948                 if (KTRPOINT(td, KTR_PSIG))
  949                         ktrpsig(sig, action, &td->td_sigmask, 0);
  950 #endif
  951                 _STOPEVENT(p, S_SIG, sig);
  952 
  953                 SIGDELSET(td->td_siglist, sig);
  954                 bzero(info, sizeof(*info));
  955                 info->si_signo = sig;
  956                 info->si_code = 0;
  957         }
  958         PROC_UNLOCK(p);
  959         return (error);
  960 }
  961 
  962 /*
  963  * MPSAFE
  964  */
  965 int
  966 sigpending(td, uap)
  967         struct thread *td;
  968         struct sigpending_args *uap;
  969 {
  970         struct proc *p = td->td_proc;
  971         sigset_t siglist;
  972 
  973         PROC_LOCK(p);
  974         siglist = p->p_siglist;
  975         SIGSETOR(siglist, td->td_siglist);
  976         PROC_UNLOCK(p);
  977         return (copyout(&siglist, uap->set, sizeof(sigset_t)));
  978 }
  979 
  980 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
  981 #ifndef _SYS_SYSPROTO_H_
  982 struct osigpending_args {
  983         int     dummy;
  984 };
  985 #endif
  986 /*
  987  * MPSAFE
  988  */
  989 int
  990 osigpending(td, uap)
  991         struct thread *td;
  992         struct osigpending_args *uap;
  993 {
  994         struct proc *p = td->td_proc;
  995         sigset_t siglist;
  996 
  997         PROC_LOCK(p);
  998         siglist = p->p_siglist;
  999         SIGSETOR(siglist, td->td_siglist);
 1000         PROC_UNLOCK(p);
 1001         SIG2OSIG(siglist, td->td_retval[0]);
 1002         return (0);
 1003 }
 1004 #endif /* COMPAT_43 */
 1005 
 1006 #if defined(COMPAT_43)
 1007 /*
 1008  * Generalized interface signal handler, 4.3-compatible.
 1009  */
 1010 #ifndef _SYS_SYSPROTO_H_
 1011 struct osigvec_args {
 1012         int     signum;
 1013         struct  sigvec *nsv;
 1014         struct  sigvec *osv;
 1015 };
 1016 #endif
 1017 /*
 1018  * MPSAFE
 1019  */
 1020 /* ARGSUSED */
 1021 int
 1022 osigvec(td, uap)
 1023         struct thread *td;
 1024         register struct osigvec_args *uap;
 1025 {
 1026         struct sigvec vec;
 1027         struct sigaction nsa, osa;
 1028         register struct sigaction *nsap, *osap;
 1029         int error;
 1030 
 1031         if (uap->signum <= 0 || uap->signum >= ONSIG)
 1032                 return (EINVAL);
 1033         nsap = (uap->nsv != NULL) ? &nsa : NULL;
 1034         osap = (uap->osv != NULL) ? &osa : NULL;
 1035         if (nsap) {
 1036                 error = copyin(uap->nsv, &vec, sizeof(vec));
 1037                 if (error)
 1038                         return (error);
 1039                 nsap->sa_handler = vec.sv_handler;
 1040                 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
 1041                 nsap->sa_flags = vec.sv_flags;
 1042                 nsap->sa_flags ^= SA_RESTART;   /* opposite of SV_INTERRUPT */
 1043         }
 1044         error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
 1045         if (osap && !error) {
 1046                 vec.sv_handler = osap->sa_handler;
 1047                 SIG2OSIG(osap->sa_mask, vec.sv_mask);
 1048                 vec.sv_flags = osap->sa_flags;
 1049                 vec.sv_flags &= ~SA_NOCLDWAIT;
 1050                 vec.sv_flags ^= SA_RESTART;
 1051                 error = copyout(&vec, uap->osv, sizeof(vec));
 1052         }
 1053         return (error);
 1054 }
 1055 
 1056 #ifndef _SYS_SYSPROTO_H_
 1057 struct osigblock_args {
 1058         int     mask;
 1059 };
 1060 #endif
 1061 /*
 1062  * MPSAFE
 1063  */
 1064 int
 1065 osigblock(td, uap)
 1066         register struct thread *td;
 1067         struct osigblock_args *uap;
 1068 {
 1069         struct proc *p = td->td_proc;
 1070         sigset_t set;
 1071 
 1072         OSIG2SIG(uap->mask, set);
 1073         SIG_CANTMASK(set);
 1074         PROC_LOCK(p);
 1075         SIG2OSIG(td->td_sigmask, td->td_retval[0]);
 1076         SIGSETOR(td->td_sigmask, set);
 1077         PROC_UNLOCK(p);
 1078         return (0);
 1079 }
 1080 
 1081 #ifndef _SYS_SYSPROTO_H_
 1082 struct osigsetmask_args {
 1083         int     mask;
 1084 };
 1085 #endif
 1086 /*
 1087  * MPSAFE
 1088  */
 1089 int
 1090 osigsetmask(td, uap)
 1091         struct thread *td;
 1092         struct osigsetmask_args *uap;
 1093 {
 1094         struct proc *p = td->td_proc;
 1095         sigset_t set;
 1096 
 1097         OSIG2SIG(uap->mask, set);
 1098         SIG_CANTMASK(set);
 1099         PROC_LOCK(p);
 1100         SIG2OSIG(td->td_sigmask, td->td_retval[0]);
 1101         SIGSETLO(td->td_sigmask, set);
 1102         signotify(td);
 1103         PROC_UNLOCK(p);
 1104         return (0);
 1105 }
 1106 #endif /* COMPAT_43 */
 1107 
 1108 /*
 1109  * Suspend process until signal, providing mask to be set
 1110  * in the meantime. 
 1111  ***** XXXKSE this doesn't make sense under KSE.
 1112  ***** Do we suspend the thread or all threads in the process?
 1113  ***** How do we suspend threads running NOW on another processor?
 1114  */
 1115 #ifndef _SYS_SYSPROTO_H_
 1116 struct sigsuspend_args {
 1117         const sigset_t *sigmask;
 1118 };
 1119 #endif
 1120 /*
 1121  * MPSAFE
 1122  */
 1123 /* ARGSUSED */
 1124 int
 1125 sigsuspend(td, uap)
 1126         struct thread *td;
 1127         struct sigsuspend_args *uap;
 1128 {
 1129         sigset_t mask;
 1130         int error;
 1131 
 1132         error = copyin(uap->sigmask, &mask, sizeof(mask));
 1133         if (error)
 1134                 return (error);
 1135         return (kern_sigsuspend(td, mask));
 1136 }
 1137 
 1138 int
 1139 kern_sigsuspend(struct thread *td, sigset_t mask)
 1140 {
 1141         struct proc *p = td->td_proc;
 1142 
 1143         /*
 1144          * When returning from sigsuspend, we want
 1145          * the old mask to be restored after the
 1146          * signal handler has finished.  Thus, we
 1147          * save it here and mark the sigacts structure
 1148          * to indicate this.
 1149          */
 1150         PROC_LOCK(p);
 1151         td->td_oldsigmask = td->td_sigmask;
 1152         td->td_pflags |= TDP_OLDMASK;
 1153         SIG_CANTMASK(mask);
 1154         td->td_sigmask = mask;
 1155         signotify(td);
 1156         while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
 1157                 /* void */;
 1158         PROC_UNLOCK(p);
 1159         /* always return EINTR rather than ERESTART... */
 1160         return (EINTR);
 1161 }
 1162 
 1163 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
 1164 /*
 1165  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
 1166  * convention: libc stub passes mask, not pointer, to save a copyin.
 1167  */
 1168 #ifndef _SYS_SYSPROTO_H_
 1169 struct osigsuspend_args {
 1170         osigset_t mask;
 1171 };
 1172 #endif
 1173 /*
 1174  * MPSAFE
 1175  */
 1176 /* ARGSUSED */
 1177 int
 1178 osigsuspend(td, uap)
 1179         struct thread *td;
 1180         struct osigsuspend_args *uap;
 1181 {
 1182         struct proc *p = td->td_proc;
 1183         sigset_t mask;
 1184 
 1185         PROC_LOCK(p);
 1186         td->td_oldsigmask = td->td_sigmask;
 1187         td->td_pflags |= TDP_OLDMASK;
 1188         OSIG2SIG(uap->mask, mask);
 1189         SIG_CANTMASK(mask);
 1190         SIGSETLO(td->td_sigmask, mask);
 1191         signotify(td);
 1192         while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
 1193                 /* void */;
 1194         PROC_UNLOCK(p);
 1195         /* always return EINTR rather than ERESTART... */
 1196         return (EINTR);
 1197 }
 1198 #endif /* COMPAT_43 */
 1199 
 1200 #if defined(COMPAT_43)
 1201 #ifndef _SYS_SYSPROTO_H_
 1202 struct osigstack_args {
 1203         struct  sigstack *nss;
 1204         struct  sigstack *oss;
 1205 };
 1206 #endif
 1207 /*
 1208  * MPSAFE
 1209  */
 1210 /* ARGSUSED */
 1211 int
 1212 osigstack(td, uap)
 1213         struct thread *td;
 1214         register struct osigstack_args *uap;
 1215 {
 1216         struct sigstack nss, oss;
 1217         int error = 0;
 1218 
 1219         if (uap->nss != NULL) {
 1220                 error = copyin(uap->nss, &nss, sizeof(nss));
 1221                 if (error)
 1222                         return (error);
 1223         }
 1224         oss.ss_sp = td->td_sigstk.ss_sp;
 1225         oss.ss_onstack = sigonstack(cpu_getstack(td));
 1226         if (uap->nss != NULL) {
 1227                 td->td_sigstk.ss_sp = nss.ss_sp;
 1228                 td->td_sigstk.ss_size = 0;
 1229                 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
 1230                 td->td_pflags |= TDP_ALTSTACK;
 1231         }
 1232         if (uap->oss != NULL)
 1233                 error = copyout(&oss, uap->oss, sizeof(oss));
 1234 
 1235         return (error);
 1236 }
 1237 #endif /* COMPAT_43 */
 1238 
 1239 #ifndef _SYS_SYSPROTO_H_
 1240 struct sigaltstack_args {
 1241         stack_t *ss;
 1242         stack_t *oss;
 1243 };
 1244 #endif
 1245 /*
 1246  * MPSAFE
 1247  */
 1248 /* ARGSUSED */
 1249 int
 1250 sigaltstack(td, uap)
 1251         struct thread *td;
 1252         register struct sigaltstack_args *uap;
 1253 {
 1254         stack_t ss, oss;
 1255         int error;
 1256 
 1257         if (uap->ss != NULL) {
 1258                 error = copyin(uap->ss, &ss, sizeof(ss));
 1259                 if (error)
 1260                         return (error);
 1261         }
 1262         error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
 1263             (uap->oss != NULL) ? &oss : NULL);
 1264         if (error)
 1265                 return (error);
 1266         if (uap->oss != NULL)
 1267                 error = copyout(&oss, uap->oss, sizeof(stack_t));
 1268         return (error);
 1269 }
 1270 
 1271 int
 1272 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
 1273 {
 1274         struct proc *p = td->td_proc;
 1275         int oonstack;
 1276 
 1277         oonstack = sigonstack(cpu_getstack(td));
 1278 
 1279         if (oss != NULL) {
 1280                 *oss = td->td_sigstk;
 1281                 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
 1282                     ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
 1283         }
 1284 
 1285         if (ss != NULL) {
 1286                 if (oonstack)
 1287                         return (EPERM);
 1288                 if ((ss->ss_flags & ~SS_DISABLE) != 0)
 1289                         return (EINVAL);
 1290                 if (!(ss->ss_flags & SS_DISABLE)) {
 1291                         if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
 1292                                 return (ENOMEM);
 1293                         }
 1294                         td->td_sigstk = *ss;
 1295                         td->td_pflags |= TDP_ALTSTACK;
 1296                 } else {
 1297                         td->td_pflags &= ~TDP_ALTSTACK;
 1298                 }
 1299         }
 1300         return (0);
 1301 }
 1302 
 1303 /*
 1304  * Common code for kill process group/broadcast kill.
 1305  * cp is calling process.
 1306  */
 1307 static int
 1308 killpg1(td, sig, pgid, all)
 1309         register struct thread *td;
 1310         int sig, pgid, all;
 1311 {
 1312         register struct proc *p;
 1313         struct pgrp *pgrp;
 1314         int nfound = 0;
 1315 
 1316         if (all) {
 1317                 /*
 1318                  * broadcast
 1319                  */
 1320                 sx_slock(&allproc_lock);
 1321                 LIST_FOREACH(p, &allproc, p_list) {
 1322                         PROC_LOCK(p);
 1323                         if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
 1324                             p == td->td_proc) {
 1325                                 PROC_UNLOCK(p);
 1326                                 continue;
 1327                         }
 1328                         if (p_cansignal(td, p, sig) == 0) {
 1329                                 nfound++;
 1330                                 if (sig)
 1331                                         psignal(p, sig);
 1332                         }
 1333                         PROC_UNLOCK(p);
 1334                 }
 1335                 sx_sunlock(&allproc_lock);
 1336         } else {
 1337                 sx_slock(&proctree_lock);
 1338                 if (pgid == 0) {
 1339                         /*
 1340                          * zero pgid means send to my process group.
 1341                          */
 1342                         pgrp = td->td_proc->p_pgrp;
 1343                         PGRP_LOCK(pgrp);
 1344                 } else {
 1345                         pgrp = pgfind(pgid);
 1346                         if (pgrp == NULL) {
 1347                                 sx_sunlock(&proctree_lock);
 1348                                 return (ESRCH);
 1349                         }
 1350                 }
 1351                 sx_sunlock(&proctree_lock);
 1352                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
 1353                         PROC_LOCK(p);         
 1354                         if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
 1355                                 PROC_UNLOCK(p);
 1356                                 continue;
 1357                         }
 1358                         if (p_cansignal(td, p, sig) == 0) {
 1359                                 nfound++;
 1360                                 if (sig)
 1361                                         psignal(p, sig);
 1362                         }
 1363                         PROC_UNLOCK(p);
 1364                 }
 1365                 PGRP_UNLOCK(pgrp);
 1366         }
 1367         return (nfound ? 0 : ESRCH);
 1368 }
 1369 
 1370 #ifndef _SYS_SYSPROTO_H_
 1371 struct kill_args {
 1372         int     pid;
 1373         int     signum;
 1374 };
 1375 #endif
 1376 /*
 1377  * MPSAFE
 1378  */
 1379 /* ARGSUSED */
 1380 int
 1381 kill(td, uap)
 1382         register struct thread *td;
 1383         register struct kill_args *uap;
 1384 {
 1385         register struct proc *p;
 1386         int error;
 1387 
 1388         if ((u_int)uap->signum > _SIG_MAXSIG)
 1389                 return (EINVAL);
 1390 
 1391         if (uap->pid > 0) {
 1392                 /* kill single process */
 1393                 if ((p = pfind(uap->pid)) == NULL) {
 1394                         if ((p = zpfind(uap->pid)) == NULL)
 1395                                 return (ESRCH);
 1396                 }
 1397                 error = p_cansignal(td, p, uap->signum);
 1398                 if (error == 0 && uap->signum)
 1399                         psignal(p, uap->signum);
 1400                 PROC_UNLOCK(p);
 1401                 return (error);
 1402         }
 1403         switch (uap->pid) {
 1404         case -1:                /* broadcast signal */
 1405                 return (killpg1(td, uap->signum, 0, 1));
 1406         case 0:                 /* signal own process group */
 1407                 return (killpg1(td, uap->signum, 0, 0));
 1408         default:                /* negative explicit process group */
 1409                 return (killpg1(td, uap->signum, -uap->pid, 0));
 1410         }
 1411         /* NOTREACHED */
 1412 }
 1413 
 1414 #if defined(COMPAT_43)
 1415 #ifndef _SYS_SYSPROTO_H_
 1416 struct okillpg_args {
 1417         int     pgid;
 1418         int     signum;
 1419 };
 1420 #endif
 1421 /*
 1422  * MPSAFE
 1423  */
 1424 /* ARGSUSED */
 1425 int
 1426 okillpg(td, uap)
 1427         struct thread *td;
 1428         register struct okillpg_args *uap;
 1429 {
 1430 
 1431         if ((u_int)uap->signum > _SIG_MAXSIG)
 1432                 return (EINVAL);
 1433         return (killpg1(td, uap->signum, uap->pgid, 0));
 1434 }
 1435 #endif /* COMPAT_43 */
 1436 
 1437 /*
 1438  * Send a signal to a process group.
 1439  */
 1440 void
 1441 gsignal(pgid, sig)
 1442         int pgid, sig;
 1443 {
 1444         struct pgrp *pgrp;
 1445 
 1446         if (pgid != 0) {
 1447                 sx_slock(&proctree_lock);
 1448                 pgrp = pgfind(pgid);
 1449                 sx_sunlock(&proctree_lock);
 1450                 if (pgrp != NULL) {
 1451                         pgsignal(pgrp, sig, 0);
 1452                         PGRP_UNLOCK(pgrp);
 1453                 }
 1454         }
 1455 }
 1456 
 1457 /*
 1458  * Send a signal to a process group.  If checktty is 1,
 1459  * limit to members which have a controlling terminal.
 1460  */
 1461 void
 1462 pgsignal(pgrp, sig, checkctty)
 1463         struct pgrp *pgrp;
 1464         int sig, checkctty;
 1465 {
 1466         register struct proc *p;
 1467 
 1468         if (pgrp) {
 1469                 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
 1470                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
 1471                         PROC_LOCK(p);
 1472                         if (checkctty == 0 || p->p_flag & P_CONTROLT)
 1473                                 psignal(p, sig);
 1474                         PROC_UNLOCK(p);
 1475                 }
 1476         }
 1477 }
 1478 
 1479 /*
 1480  * Send a signal caused by a trap to the current thread.
 1481  * If it will be caught immediately, deliver it with correct code.
 1482  * Otherwise, post it normally.
 1483  *
 1484  * MPSAFE
 1485  */
 1486 void
 1487 trapsignal(struct thread *td, int sig, u_long code)
 1488 {
 1489         struct sigacts *ps;
 1490         struct proc *p;
 1491         siginfo_t siginfo;
 1492         int error;
 1493 
 1494         p = td->td_proc;
 1495         if (td->td_pflags & TDP_SA) {
 1496                 if (td->td_mailbox == NULL)
 1497                         thread_user_enter(td);
 1498                 PROC_LOCK(p);
 1499                 SIGDELSET(td->td_sigmask, sig);
 1500                 mtx_lock_spin(&sched_lock);
 1501                 /*
 1502                  * Force scheduling an upcall, so UTS has chance to
 1503                  * process the signal before thread runs again in
 1504                  * userland.
 1505                  */
 1506                 if (td->td_upcall)
 1507                         td->td_upcall->ku_flags |= KUF_DOUPCALL;
 1508                 mtx_unlock_spin(&sched_lock);
 1509         } else {
 1510                 PROC_LOCK(p);
 1511         }
 1512         ps = p->p_sigacts;
 1513         mtx_lock(&ps->ps_mtx);
 1514         if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
 1515             !SIGISMEMBER(td->td_sigmask, sig)) {
 1516                 p->p_stats->p_ru.ru_nsignals++;
 1517 #ifdef KTRACE
 1518                 if (KTRPOINT(curthread, KTR_PSIG))
 1519                         ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
 1520                             &td->td_sigmask, code);
 1521 #endif
 1522                 if (!(td->td_pflags & TDP_SA))
 1523                         (*p->p_sysent->sv_sendsig)(
 1524                                 ps->ps_sigact[_SIG_IDX(sig)], sig,
 1525                                 &td->td_sigmask, code);
 1526                 else if (td->td_mailbox == NULL) {
 1527                         mtx_unlock(&ps->ps_mtx);
 1528                         /* UTS caused a sync signal */
 1529                         p->p_code = code;       /* XXX for core dump/debugger */
 1530                         p->p_sig = sig;         /* XXX to verify code */
 1531                         sigexit(td, sig);
 1532                 } else {
 1533                         cpu_thread_siginfo(sig, code, &siginfo);
 1534                         mtx_unlock(&ps->ps_mtx);
 1535                         SIGADDSET(td->td_sigmask, sig);
 1536                         PROC_UNLOCK(p);
 1537                         error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
 1538                             sizeof(siginfo));
 1539                         PROC_LOCK(p);
 1540                         /* UTS memory corrupted */
 1541                         if (error)
 1542                                 sigexit(td, SIGSEGV);
 1543                         mtx_lock(&ps->ps_mtx);
 1544                 }
 1545                 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
 1546                 if (!SIGISMEMBER(ps->ps_signodefer, sig))
 1547                         SIGADDSET(td->td_sigmask, sig);
 1548                 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
 1549                         /*
 1550                          * See kern_sigaction() for origin of this code.
 1551                          */
 1552                         SIGDELSET(ps->ps_sigcatch, sig);
 1553                         if (sig != SIGCONT &&
 1554                             sigprop(sig) & SA_IGNORE)
 1555                                 SIGADDSET(ps->ps_sigignore, sig);
 1556                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
 1557                 }
 1558                 mtx_unlock(&ps->ps_mtx);
 1559         } else {
 1560                 mtx_unlock(&ps->ps_mtx);
 1561                 p->p_code = code;       /* XXX for core dump/debugger */
 1562                 p->p_sig = sig;         /* XXX to verify code */
 1563                 tdsignal(td, sig, SIGTARGET_TD);
 1564         }
 1565         PROC_UNLOCK(p);
 1566 }
 1567 
 1568 static struct thread *
 1569 sigtd(struct proc *p, int sig, int prop)
 1570 {
 1571         struct thread *td, *signal_td;
 1572 
 1573         PROC_LOCK_ASSERT(p, MA_OWNED);
 1574 
 1575         /*
 1576          * Check if current thread can handle the signal without
 1577          * switching conetxt to another thread.
 1578          */
 1579         if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
 1580                 return (curthread);
 1581         signal_td = NULL;
 1582         mtx_lock_spin(&sched_lock);
 1583         FOREACH_THREAD_IN_PROC(p, td) {
 1584                 if (!SIGISMEMBER(td->td_sigmask, sig)) {
 1585                         signal_td = td;
 1586                         break;
 1587                 }
 1588         }
 1589         if (signal_td == NULL)
 1590                 signal_td = FIRST_THREAD_IN_PROC(p);
 1591         mtx_unlock_spin(&sched_lock);
 1592         return (signal_td);
 1593 }
 1594 
 1595 /*
 1596  * Send the signal to the process.  If the signal has an action, the action
 1597  * is usually performed by the target process rather than the caller; we add
 1598  * the signal to the set of pending signals for the process.
 1599  *
 1600  * Exceptions:
 1601  *   o When a stop signal is sent to a sleeping process that takes the
 1602  *     default action, the process is stopped without awakening it.
 1603  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
 1604  *     regardless of the signal action (eg, blocked or ignored).
 1605  *
 1606  * Other ignored signals are discarded immediately.
 1607  *
 1608  * MPSAFE
 1609  */
 1610 void
 1611 psignal(struct proc *p, int sig)
 1612 {
 1613         struct thread *td;
 1614         int prop;
 1615 
 1616         if (!_SIG_VALID(sig))
 1617                 panic("psignal(): invalid signal");
 1618 
 1619         PROC_LOCK_ASSERT(p, MA_OWNED);
 1620         /*
 1621          * IEEE Std 1003.1-2001: return success when killing a zombie.
 1622          */
 1623         if (p->p_state == PRS_ZOMBIE)
 1624                 return;
 1625         prop = sigprop(sig);
 1626 
 1627         /*
 1628          * Find a thread to deliver the signal to.
 1629          */
 1630         td = sigtd(p, sig, prop);
 1631 
 1632         tdsignal(td, sig, SIGTARGET_P);
 1633 }
 1634 
 1635 /*
 1636  * MPSAFE
 1637  */
 1638 void
 1639 tdsignal(struct thread *td, int sig, sigtarget_t target)
 1640 {
 1641         sigset_t saved;
 1642         struct proc *p = td->td_proc;
 1643 
 1644         if (p->p_flag & P_SA)
 1645                 saved = p->p_siglist;
 1646         do_tdsignal(td, sig, target);
 1647         if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
 1648                 if (!SIGSETEQ(saved, p->p_siglist)) {
 1649                         /* pending set changed */
 1650                         p->p_flag |= P_SIGEVENT;
 1651                         wakeup(&p->p_siglist);
 1652                 }
 1653         }
 1654 }
 1655 
 1656 static void
 1657 do_tdsignal(struct thread *td, int sig, sigtarget_t target)
 1658 {
 1659         struct proc *p;
 1660         register sig_t action;
 1661         sigset_t *siglist;
 1662         struct thread *td0;
 1663         register int prop;
 1664         struct sigacts *ps;
 1665 
 1666         if (!_SIG_VALID(sig))
 1667                 panic("do_tdsignal(): invalid signal");
 1668 
 1669         p = td->td_proc;
 1670         ps = p->p_sigacts;
 1671 
 1672         PROC_LOCK_ASSERT(p, MA_OWNED);
 1673         KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
 1674 
 1675         prop = sigprop(sig);
 1676 
 1677         /*
 1678          * If the signal is blocked and not destined for this thread, then
 1679          * assign it to the process so that we can find it later in the first
 1680          * thread that unblocks it.  Otherwise, assign it to this thread now.
 1681          */
 1682         if (target == SIGTARGET_TD) {
 1683                 siglist = &td->td_siglist;
 1684         } else {
 1685                 if (!SIGISMEMBER(td->td_sigmask, sig))
 1686                         siglist = &td->td_siglist;
 1687                 else
 1688                         siglist = &p->p_siglist;
 1689         }
 1690 
 1691         /*
 1692          * If the signal is being ignored,
 1693          * then we forget about it immediately.
 1694          * (Note: we don't set SIGCONT in ps_sigignore,
 1695          * and if it is set to SIG_IGN,
 1696          * action will be SIG_DFL here.)
 1697          */
 1698         mtx_lock(&ps->ps_mtx);
 1699         if (SIGISMEMBER(ps->ps_sigignore, sig) ||
 1700             (p->p_flag & P_WEXIT)) {
 1701                 mtx_unlock(&ps->ps_mtx);
 1702                 return;
 1703         }
 1704         if (SIGISMEMBER(td->td_sigmask, sig))
 1705                 action = SIG_HOLD;
 1706         else if (SIGISMEMBER(ps->ps_sigcatch, sig))
 1707                 action = SIG_CATCH;
 1708         else
 1709                 action = SIG_DFL;
 1710         mtx_unlock(&ps->ps_mtx);
 1711 
 1712         if (prop & SA_CONT) {
 1713                 SIG_STOPSIGMASK(p->p_siglist);
 1714                 /*
 1715                  * XXX Should investigate leaving STOP and CONT sigs only in
 1716                  * the proc's siglist.
 1717                  */
 1718                 mtx_lock_spin(&sched_lock);
 1719                 FOREACH_THREAD_IN_PROC(p, td0)
 1720                         SIG_STOPSIGMASK(td0->td_siglist);
 1721                 mtx_unlock_spin(&sched_lock);
 1722         }
 1723 
 1724         if (prop & SA_STOP) {
 1725                 /*
 1726                  * If sending a tty stop signal to a member of an orphaned
 1727                  * process group, discard the signal here if the action
 1728                  * is default; don't stop the process below if sleeping,
 1729                  * and don't clear any pending SIGCONT.
 1730                  */
 1731                 if ((prop & SA_TTYSTOP) &&
 1732                     (p->p_pgrp->pg_jobc == 0) &&
 1733                     (action == SIG_DFL))
 1734                         return;
 1735                 SIG_CONTSIGMASK(p->p_siglist);
 1736                 mtx_lock_spin(&sched_lock);
 1737                 FOREACH_THREAD_IN_PROC(p, td0)
 1738                         SIG_CONTSIGMASK(td0->td_siglist);
 1739                 mtx_unlock_spin(&sched_lock);
 1740                 p->p_flag &= ~P_CONTINUED;
 1741         }
 1742 
 1743         SIGADDSET(*siglist, sig);
 1744         signotify(td);                  /* uses schedlock */
 1745         /*
 1746          * Defer further processing for signals which are held,
 1747          * except that stopped processes must be continued by SIGCONT.
 1748          */
 1749         if (action == SIG_HOLD &&
 1750             !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
 1751                 return;
 1752         /*
 1753          * SIGKILL: Remove procfs STOPEVENTs.
 1754          */
 1755         if (sig == SIGKILL) {
 1756                 /* from procfs_ioctl.c: PIOCBIC */
 1757                 p->p_stops = 0;
 1758                 /* from procfs_ioctl.c: PIOCCONT */
 1759                 p->p_step = 0;
 1760                 wakeup(&p->p_step);
 1761         }
 1762         /*
 1763          * Some signals have a process-wide effect and a per-thread
 1764          * component.  Most processing occurs when the process next
 1765          * tries to cross the user boundary, however there are some
 1766          * times when processing needs to be done immediatly, such as
 1767          * waking up threads so that they can cross the user boundary.
 1768          * We try do the per-process part here.
 1769          */
 1770         if (P_SHOULDSTOP(p)) {
 1771                 /*
 1772                  * The process is in stopped mode. All the threads should be
 1773                  * either winding down or already on the suspended queue.
 1774                  */
 1775                 if (p->p_flag & P_TRACED) {
 1776                         /*
 1777                          * The traced process is already stopped,
 1778                          * so no further action is necessary.
 1779                          * No signal can restart us.
 1780                          */
 1781                         goto out;
 1782                 }
 1783 
 1784                 if (sig == SIGKILL) {
 1785                         /*
 1786                          * SIGKILL sets process running.
 1787                          * It will die elsewhere.
 1788                          * All threads must be restarted.
 1789                          */
 1790                         p->p_flag &= ~P_STOPPED_SIG;
 1791                         goto runfast;
 1792                 }
 1793 
 1794                 if (prop & SA_CONT) {
 1795                         /*
 1796                          * If SIGCONT is default (or ignored), we continue the
 1797                          * process but don't leave the signal in siglist as
 1798                          * it has no further action.  If SIGCONT is held, we
 1799                          * continue the process and leave the signal in
 1800                          * siglist.  If the process catches SIGCONT, let it
 1801                          * handle the signal itself.  If it isn't waiting on
 1802                          * an event, it goes back to run state.
 1803                          * Otherwise, process goes back to sleep state.
 1804                          */
 1805                         p->p_flag &= ~P_STOPPED_SIG;
 1806                         p->p_flag |= P_CONTINUED;
 1807                         if (action == SIG_DFL) {
 1808                                 SIGDELSET(*siglist, sig);
 1809                         } else if (action == SIG_CATCH) {
 1810                                 /*
 1811                                  * The process wants to catch it so it needs
 1812                                  * to run at least one thread, but which one?
 1813                                  * It would seem that the answer would be to
 1814                                  * run an upcall in the next KSE to run, and
 1815                                  * deliver the signal that way. In a NON KSE
 1816                                  * process, we need to make sure that the
 1817                                  * single thread is runnable asap.
 1818                                  * XXXKSE for now however, make them all run.
 1819                                  */
 1820                                 goto runfast;
 1821                         }
 1822                         /*
 1823                          * The signal is not ignored or caught.
 1824                          */
 1825                         mtx_lock_spin(&sched_lock);
 1826                         thread_unsuspend(p);
 1827                         mtx_unlock_spin(&sched_lock);
 1828                         goto out;
 1829                 }
 1830 
 1831                 if (prop & SA_STOP) {
 1832                         /*
 1833                          * Already stopped, don't need to stop again
 1834                          * (If we did the shell could get confused).
 1835                          * Just make sure the signal STOP bit set.
 1836                          */
 1837                         p->p_flag |= P_STOPPED_SIG;
 1838                         SIGDELSET(*siglist, sig);
 1839                         goto out;
 1840                 }
 1841 
 1842                 /*
 1843                  * All other kinds of signals:
 1844                  * If a thread is sleeping interruptibly, simulate a
 1845                  * wakeup so that when it is continued it will be made
 1846                  * runnable and can look at the signal.  However, don't make
 1847                  * the PROCESS runnable, leave it stopped.
 1848                  * It may run a bit until it hits a thread_suspend_check().
 1849                  */
 1850                 mtx_lock_spin(&sched_lock);
 1851                 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
 1852                         sleepq_abort(td);
 1853                 mtx_unlock_spin(&sched_lock);
 1854                 goto out;
 1855                 /*
 1856                  * Mutexes are short lived. Threads waiting on them will
 1857                  * hit thread_suspend_check() soon.
 1858                  */
 1859         } else if (p->p_state == PRS_NORMAL) {
 1860                 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
 1861                         mtx_lock_spin(&sched_lock);
 1862                         tdsigwakeup(td, sig, action);
 1863                         mtx_unlock_spin(&sched_lock);
 1864                         goto out;
 1865                 }
 1866 
 1867                 MPASS(action == SIG_DFL);
 1868 
 1869                 if (prop & SA_STOP) {
 1870                         if (p->p_flag & P_PPWAIT)
 1871                                 goto out;
 1872                         p->p_flag |= P_STOPPED_SIG;
 1873                         p->p_xstat = sig;
 1874                         mtx_lock_spin(&sched_lock);
 1875                         FOREACH_THREAD_IN_PROC(p, td0) {
 1876                                 if (TD_IS_SLEEPING(td0) &&
 1877                                     (td0->td_flags & TDF_SINTR) &&
 1878                                     !TD_IS_SUSPENDED(td0)) {
 1879                                         thread_suspend_one(td0);
 1880                                 } else if (td != td0) {
 1881                                         td0->td_flags |= TDF_ASTPENDING;
 1882                                 }
 1883                         }
 1884                         thread_stopped(p);
 1885                         if (p->p_numthreads == p->p_suspcount) {
 1886                                 SIGDELSET(p->p_siglist, p->p_xstat);
 1887                                 FOREACH_THREAD_IN_PROC(p, td0)
 1888                                         SIGDELSET(td0->td_siglist, p->p_xstat);
 1889                         }
 1890                         mtx_unlock_spin(&sched_lock);
 1891                         goto out;
 1892                 } 
 1893                 else
 1894                         goto runfast;
 1895                 /* NOTREACHED */
 1896         } else {
 1897                 /* Not in "NORMAL" state. discard the signal. */
 1898                 SIGDELSET(*siglist, sig);
 1899                 goto out;
 1900         }
 1901 
 1902         /*
 1903          * The process is not stopped so we need to apply the signal to all the
 1904          * running threads.
 1905          */
 1906 
 1907 runfast:
 1908         mtx_lock_spin(&sched_lock);
 1909         tdsigwakeup(td, sig, action);
 1910         thread_unsuspend(p);
 1911         mtx_unlock_spin(&sched_lock);
 1912 out:
 1913         /* If we jump here, sched_lock should not be owned. */
 1914         mtx_assert(&sched_lock, MA_NOTOWNED);
 1915 }
 1916 
 1917 /*
 1918  * The force of a signal has been directed against a single
 1919  * thread.  We need to see what we can do about knocking it
 1920  * out of any sleep it may be in etc.
 1921  */
 1922 static void
 1923 tdsigwakeup(struct thread *td, int sig, sig_t action)
 1924 {
 1925         struct proc *p = td->td_proc;
 1926         register int prop;
 1927 
 1928         PROC_LOCK_ASSERT(p, MA_OWNED);
 1929         mtx_assert(&sched_lock, MA_OWNED);
 1930         prop = sigprop(sig);
 1931 
 1932         /*
 1933          * Bring the priority of a thread up if we want it to get
 1934          * killed in this lifetime.
 1935          */
 1936         if (action == SIG_DFL && (prop & SA_KILL)) {
 1937                 if (td->td_priority > PUSER)
 1938                         td->td_priority = PUSER;
 1939         }
 1940 
 1941         if (TD_ON_SLEEPQ(td)) {
 1942                 /*
 1943                  * If thread is sleeping uninterruptibly
 1944                  * we can't interrupt the sleep... the signal will
 1945                  * be noticed when the process returns through
 1946                  * trap() or syscall().
 1947                  */
 1948                 if ((td->td_flags & TDF_SINTR) == 0)
 1949                         return;
 1950                 /*
 1951                  * If SIGCONT is default (or ignored) and process is
 1952                  * asleep, we are finished; the process should not
 1953                  * be awakened.
 1954                  */
 1955                 if ((prop & SA_CONT) && action == SIG_DFL) {
 1956                         SIGDELSET(p->p_siglist, sig);
 1957                         /*
 1958                          * It may be on either list in this state.
 1959                          * Remove from both for now.
 1960                          */
 1961                         SIGDELSET(td->td_siglist, sig);
 1962                         return;
 1963                 }
 1964 
 1965                 /*
 1966                  * Give low priority threads a better chance to run.
 1967                  */
 1968                 if (td->td_priority > PUSER)
 1969                         td->td_priority = PUSER;
 1970 
 1971                 sleepq_abort(td);
 1972         } else {
 1973                 /*
 1974                  * Other states do nothing with the signal immediately,
 1975                  * other than kicking ourselves if we are running.
 1976                  * It will either never be noticed, or noticed very soon.
 1977                  */
 1978 #ifdef SMP
 1979                 if (TD_IS_RUNNING(td) && td != curthread)
 1980                         forward_signal(td);
 1981 #endif
 1982         }
 1983 }
 1984 
 1985 int
 1986 ptracestop(struct thread *td, int sig)
 1987 {
 1988         struct proc *p = td->td_proc;
 1989         struct thread *td0;
 1990 
 1991         PROC_LOCK_ASSERT(p, MA_OWNED);
 1992         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
 1993             &p->p_mtx.mtx_object, "Stopping for traced signal");
 1994 
 1995         mtx_lock_spin(&sched_lock);
 1996         td->td_flags |= TDF_XSIG;
 1997         mtx_unlock_spin(&sched_lock);
 1998         td->td_xsig = sig;
 1999         while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
 2000                 if (p->p_flag & P_SINGLE_EXIT) {
 2001                         mtx_lock_spin(&sched_lock);
 2002                         td->td_flags &= ~TDF_XSIG;
 2003                         mtx_unlock_spin(&sched_lock);
 2004                         return (sig);
 2005                 }
 2006                 /*
 2007                  * Just make wait() to work, the last stopped thread
 2008                  * will win.
 2009                  */
 2010                 p->p_xstat = sig;
 2011                 p->p_xthread = td;
 2012                 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
 2013                 mtx_lock_spin(&sched_lock);
 2014                 FOREACH_THREAD_IN_PROC(p, td0) {
 2015                         if (TD_IS_SLEEPING(td0) &&
 2016                             (td0->td_flags & TDF_SINTR) &&
 2017                             !TD_IS_SUSPENDED(td0)) {
 2018                                 thread_suspend_one(td0);
 2019                         } else if (td != td0) {
 2020                                 td0->td_flags |= TDF_ASTPENDING;
 2021                         }
 2022                 }
 2023 stopme:
 2024                 thread_stopped(p);
 2025                 thread_suspend_one(td);
 2026                 PROC_UNLOCK(p);
 2027                 DROP_GIANT();
 2028                 mi_switch(SW_VOL, NULL);
 2029                 mtx_unlock_spin(&sched_lock);
 2030                 PICKUP_GIANT();
 2031                 PROC_LOCK(p);
 2032                 if (!(p->p_flag & P_TRACED))
 2033                         break;
 2034                 if (td->td_flags & TDF_DBSUSPEND) {
 2035                         if (p->p_flag & P_SINGLE_EXIT)
 2036                                 break;
 2037                         mtx_lock_spin(&sched_lock);
 2038                         goto stopme;
 2039                 }
 2040         }
 2041         return (td->td_xsig);
 2042 }
 2043 
 2044 /*
 2045  * If the current process has received a signal (should be caught or cause
 2046  * termination, should interrupt current syscall), return the signal number.
 2047  * Stop signals with default action are processed immediately, then cleared;
 2048  * they aren't returned.  This is checked after each entry to the system for
 2049  * a syscall or trap (though this can usually be done without calling issignal
 2050  * by checking the pending signal masks in cursig.) The normal call
 2051  * sequence is
 2052  *
 2053  *      while (sig = cursig(curthread))
 2054  *              postsig(sig);
 2055  */
 2056 static int
 2057 issignal(td)
 2058         struct thread *td;
 2059 {
 2060         struct proc *p;
 2061         struct sigacts *ps;
 2062         sigset_t sigpending;
 2063         int sig, prop, newsig;
 2064         struct thread *td0;
 2065 
 2066         p = td->td_proc;
 2067         ps = p->p_sigacts;
 2068         mtx_assert(&ps->ps_mtx, MA_OWNED);
 2069         PROC_LOCK_ASSERT(p, MA_OWNED);
 2070         for (;;) {
 2071                 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
 2072 
 2073                 sigpending = td->td_siglist;
 2074                 SIGSETNAND(sigpending, td->td_sigmask);
 2075 
 2076                 if (p->p_flag & P_PPWAIT)
 2077                         SIG_STOPSIGMASK(sigpending);
 2078                 if (SIGISEMPTY(sigpending))     /* no signal to send */
 2079                         return (0);
 2080                 sig = sig_ffs(&sigpending);
 2081 
 2082                 if (p->p_stops & S_SIG) {
 2083                         mtx_unlock(&ps->ps_mtx);
 2084                         stopevent(p, S_SIG, sig);
 2085                         mtx_lock(&ps->ps_mtx);
 2086                 }
 2087 
 2088                 /*
 2089                  * We should see pending but ignored signals
 2090                  * only if P_TRACED was on when they were posted.
 2091                  */
 2092                 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
 2093                         SIGDELSET(td->td_siglist, sig);
 2094                         if (td->td_pflags & TDP_SA)
 2095                                 SIGADDSET(td->td_sigmask, sig);
 2096                         continue;
 2097                 }
 2098                 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
 2099                         /*
 2100                          * If traced, always stop.
 2101                          */
 2102                         mtx_unlock(&ps->ps_mtx);
 2103                         newsig = ptracestop(td, sig);
 2104                         mtx_lock(&ps->ps_mtx);
 2105 
 2106                         /*
 2107                          * If parent wants us to take the signal,
 2108                          * then it will leave it in p->p_xstat;
 2109                          * otherwise we just look for signals again.
 2110                          */
 2111                         SIGDELSET(td->td_siglist, sig); /* clear old signal */
 2112                         if (td->td_pflags & TDP_SA)
 2113                                 SIGADDSET(td->td_sigmask, sig);
 2114                         if (newsig == 0)
 2115                                 continue;
 2116                         sig = newsig;
 2117                         /*
 2118                          * If the traced bit got turned off, go back up
 2119                          * to the top to rescan signals.  This ensures
 2120                          * that p_sig* and p_sigact are consistent.
 2121                          */
 2122                         if ((p->p_flag & P_TRACED) == 0)
 2123                                 continue;
 2124 
 2125                         /*
 2126                          * Put the new signal into td_siglist.  If the
 2127                          * signal is being masked, look for other signals.
 2128                          */
 2129                         SIGADDSET(td->td_siglist, sig);
 2130                         if (td->td_pflags & TDP_SA)
 2131                                 SIGDELSET(td->td_sigmask, sig);
 2132                         if (SIGISMEMBER(td->td_sigmask, sig))
 2133                                 continue;
 2134                         signotify(td);
 2135                 }
 2136 
 2137                 prop = sigprop(sig);
 2138 
 2139                 /*
 2140                  * Decide whether the signal should be returned.
 2141                  * Return the signal's number, or fall through
 2142                  * to clear it from the pending mask.
 2143                  */
 2144                 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
 2145 
 2146                 case (intptr_t)SIG_DFL:
 2147                         /*
 2148                          * Don't take default actions on system processes.
 2149                          */
 2150                         if (p->p_pid <= 1) {
 2151 #ifdef DIAGNOSTIC
 2152                                 /*
 2153                                  * Are you sure you want to ignore SIGSEGV
 2154                                  * in init? XXX
 2155                                  */
 2156                                 printf("Process (pid %lu) got signal %d\n",
 2157                                         (u_long)p->p_pid, sig);
 2158 #endif
 2159                                 break;          /* == ignore */
 2160                         }
 2161                         /*
 2162                          * If there is a pending stop signal to process
 2163                          * with default action, stop here,
 2164                          * then clear the signal.  However,
 2165                          * if process is member of an orphaned
 2166                          * process group, ignore tty stop signals.
 2167                          */
 2168                         if (prop & SA_STOP) {
 2169                                 if (p->p_flag & P_TRACED ||
 2170                                     (p->p_pgrp->pg_jobc == 0 &&
 2171                                      prop & SA_TTYSTOP))
 2172                                         break;  /* == ignore */
 2173                                 mtx_unlock(&ps->ps_mtx);
 2174                                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
 2175                                     &p->p_mtx.mtx_object, "Catching SIGSTOP");
 2176                                 p->p_flag |= P_STOPPED_SIG;
 2177                                 p->p_xstat = sig;
 2178                                 mtx_lock_spin(&sched_lock);
 2179                                 FOREACH_THREAD_IN_PROC(p, td0) {
 2180                                         if (TD_IS_SLEEPING(td0) &&
 2181                                             (td0->td_flags & TDF_SINTR) &&
 2182                                             !TD_IS_SUSPENDED(td0)) {
 2183                                                 thread_suspend_one(td0);
 2184                                         } else if (td != td0) {
 2185                                                 td0->td_flags |= TDF_ASTPENDING;
 2186                                         }
 2187                                 }
 2188                                 thread_stopped(p);
 2189                                 thread_suspend_one(td);
 2190                                 PROC_UNLOCK(p);
 2191                                 DROP_GIANT();
 2192                                 mi_switch(SW_INVOL, NULL);
 2193                                 mtx_unlock_spin(&sched_lock);
 2194                                 PICKUP_GIANT();
 2195                                 PROC_LOCK(p);
 2196                                 mtx_lock(&ps->ps_mtx);
 2197                                 break;
 2198                         } else if (prop & SA_IGNORE) {
 2199                                 /*
 2200                                  * Except for SIGCONT, shouldn't get here.
 2201                                  * Default action is to ignore; drop it.
 2202                                  */
 2203                                 break;          /* == ignore */
 2204                         } else
 2205                                 return (sig);
 2206                         /*NOTREACHED*/
 2207 
 2208                 case (intptr_t)SIG_IGN:
 2209                         /*
 2210                          * Masking above should prevent us ever trying
 2211                          * to take action on an ignored signal other
 2212                          * than SIGCONT, unless process is traced.
 2213                          */
 2214                         if ((prop & SA_CONT) == 0 &&
 2215                             (p->p_flag & P_TRACED) == 0)
 2216                                 printf("issignal\n");
 2217                         break;          /* == ignore */
 2218 
 2219                 default:
 2220                         /*
 2221                          * This signal has an action, let
 2222                          * postsig() process it.
 2223                          */
 2224                         return (sig);
 2225                 }
 2226                 SIGDELSET(td->td_siglist, sig);         /* take the signal! */
 2227         }
 2228         /* NOTREACHED */
 2229 }
 2230 
 2231 /*
 2232  * MPSAFE
 2233  */
 2234 void
 2235 thread_stopped(struct proc *p)
 2236 {
 2237         struct proc *p1 = curthread->td_proc;
 2238         struct sigacts *ps;
 2239         int n;
 2240 
 2241         PROC_LOCK_ASSERT(p, MA_OWNED);
 2242         mtx_assert(&sched_lock, MA_OWNED);
 2243         n = p->p_suspcount;
 2244         if (p == p1)
 2245                 n++;
 2246         if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
 2247                 mtx_unlock_spin(&sched_lock);
 2248                 p->p_flag &= ~P_WAITED;
 2249                 PROC_LOCK(p->p_pptr);
 2250                 /*
 2251                  * Wake up parent sleeping in kern_wait(), also send
 2252                  * SIGCHLD to parent, but SIGCHLD does not guarantee
 2253                  * that parent will awake, because parent may masked
 2254                  * the signal.
 2255                  */
 2256                 p->p_pptr->p_flag |= P_STATCHILD;
 2257                 wakeup(p->p_pptr);
 2258                 ps = p->p_pptr->p_sigacts;
 2259                 mtx_lock(&ps->ps_mtx);
 2260                 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
 2261                         mtx_unlock(&ps->ps_mtx);
 2262                         psignal(p->p_pptr, SIGCHLD);
 2263                 } else
 2264                         mtx_unlock(&ps->ps_mtx);
 2265                 PROC_UNLOCK(p->p_pptr);
 2266                 mtx_lock_spin(&sched_lock);
 2267         }
 2268 }
 2269  
 2270 /*
 2271  * Take the action for the specified signal
 2272  * from the current set of pending signals.
 2273  */
 2274 void
 2275 postsig(sig)
 2276         register int sig;
 2277 {
 2278         struct thread *td = curthread;
 2279         register struct proc *p = td->td_proc;
 2280         struct sigacts *ps;
 2281         sig_t action;
 2282         sigset_t returnmask;
 2283         int code;
 2284 
 2285         KASSERT(sig != 0, ("postsig"));
 2286 
 2287         PROC_LOCK_ASSERT(p, MA_OWNED);
 2288         ps = p->p_sigacts;
 2289         mtx_assert(&ps->ps_mtx, MA_OWNED);
 2290         SIGDELSET(td->td_siglist, sig);
 2291         action = ps->ps_sigact[_SIG_IDX(sig)];
 2292 #ifdef KTRACE
 2293         if (KTRPOINT(td, KTR_PSIG))
 2294                 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
 2295                     &td->td_oldsigmask : &td->td_sigmask, 0);
 2296 #endif
 2297         if (p->p_stops & S_SIG) {
 2298                 mtx_unlock(&ps->ps_mtx);
 2299                 stopevent(p, S_SIG, sig);
 2300                 mtx_lock(&ps->ps_mtx);
 2301         }
 2302 
 2303         if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
 2304                 /*
 2305                  * Default action, where the default is to kill
 2306                  * the process.  (Other cases were ignored above.)
 2307                  */
 2308                 mtx_unlock(&ps->ps_mtx);
 2309                 sigexit(td, sig);
 2310                 /* NOTREACHED */
 2311         } else {
 2312                 if (td->td_pflags & TDP_SA) {
 2313                         if (sig == SIGKILL) {
 2314                                 mtx_unlock(&ps->ps_mtx);
 2315                                 sigexit(td, sig);
 2316                         }
 2317                 }
 2318 
 2319                 /*
 2320                  * If we get here, the signal must be caught.
 2321                  */
 2322                 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
 2323                     ("postsig action"));
 2324                 /*
 2325                  * Set the new mask value and also defer further
 2326                  * occurrences of this signal.
 2327                  *
 2328                  * Special case: user has done a sigsuspend.  Here the
 2329                  * current mask is not of interest, but rather the
 2330                  * mask from before the sigsuspend is what we want
 2331                  * restored after the signal processing is completed.
 2332                  */
 2333                 if (td->td_pflags & TDP_OLDMASK) {
 2334                         returnmask = td->td_oldsigmask;
 2335                         td->td_pflags &= ~TDP_OLDMASK;
 2336                 } else
 2337                         returnmask = td->td_sigmask;
 2338 
 2339                 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
 2340                 if (!SIGISMEMBER(ps->ps_signodefer, sig))
 2341                         SIGADDSET(td->td_sigmask, sig);
 2342 
 2343                 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
 2344                         /*
 2345                          * See kern_sigaction() for origin of this code.
 2346                          */
 2347                         SIGDELSET(ps->ps_sigcatch, sig);
 2348                         if (sig != SIGCONT &&
 2349                             sigprop(sig) & SA_IGNORE)
 2350                                 SIGADDSET(ps->ps_sigignore, sig);
 2351                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
 2352                 }
 2353                 p->p_stats->p_ru.ru_nsignals++;
 2354                 if (p->p_sig != sig) {
 2355                         code = 0;
 2356                 } else {
 2357                         code = p->p_code;
 2358                         p->p_code = 0;
 2359                         p->p_sig = 0;
 2360                 }
 2361                 if (td->td_pflags & TDP_SA)
 2362                         thread_signal_add(curthread, sig);
 2363                 else
 2364                         (*p->p_sysent->sv_sendsig)(action, sig,
 2365                             &returnmask, code);
 2366         }
 2367 }
 2368 
 2369 /*
 2370  * Kill the current process for stated reason.
 2371  */
 2372 void
 2373 killproc(p, why)
 2374         struct proc *p;
 2375         char *why;
 2376 {
 2377 
 2378         PROC_LOCK_ASSERT(p, MA_OWNED);
 2379         CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
 2380                 p, p->p_pid, p->p_comm);
 2381         log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
 2382                 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
 2383         psignal(p, SIGKILL);
 2384 }
 2385 
 2386 /*
 2387  * Force the current process to exit with the specified signal, dumping core
 2388  * if appropriate.  We bypass the normal tests for masked and caught signals,
 2389  * allowing unrecoverable failures to terminate the process without changing
 2390  * signal state.  Mark the accounting record with the signal termination.
 2391  * If dumping core, save the signal number for the debugger.  Calls exit and
 2392  * does not return.
 2393  *
 2394  * MPSAFE
 2395  */
 2396 void
 2397 sigexit(td, sig)
 2398         struct thread *td;
 2399         int sig;
 2400 {
 2401         struct proc *p = td->td_proc;
 2402 
 2403         PROC_LOCK_ASSERT(p, MA_OWNED);
 2404         p->p_acflag |= AXSIG;
 2405         if (sigprop(sig) & SA_CORE) {
 2406                 p->p_sig = sig;
 2407                 /*
 2408                  * Log signals which would cause core dumps
 2409                  * (Log as LOG_INFO to appease those who don't want
 2410                  * these messages.)
 2411                  * XXX : Todo, as well as euid, write out ruid too
 2412                  * Note that coredump() drops proc lock.
 2413                  */
 2414                 if (coredump(td) == 0)
 2415                         sig |= WCOREFLAG;
 2416                 if (kern_logsigexit)
 2417                         log(LOG_INFO,
 2418                             "pid %d (%s), uid %d: exited on signal %d%s\n",
 2419                             p->p_pid, p->p_comm,
 2420                             td->td_ucred ? td->td_ucred->cr_uid : -1,
 2421                             sig &~ WCOREFLAG,
 2422                             sig & WCOREFLAG ? " (core dumped)" : "");
 2423         } else
 2424                 PROC_UNLOCK(p);
 2425         exit1(td, W_EXITCODE(0, sig));
 2426         /* NOTREACHED */
 2427 }
 2428 
 2429 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
 2430 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
 2431               sizeof(corefilename), "process corefile name format string");
 2432 
 2433 /*
 2434  * expand_name(name, uid, pid)
 2435  * Expand the name described in corefilename, using name, uid, and pid.
 2436  * corefilename is a printf-like string, with three format specifiers:
 2437  *      %N      name of process ("name")
 2438  *      %P      process id (pid)
 2439  *      %U      user id (uid)
 2440  * For example, "%N.core" is the default; they can be disabled completely
 2441  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
 2442  * This is controlled by the sysctl variable kern.corefile (see above).
 2443  */
 2444 
 2445 static char *
 2446 expand_name(name, uid, pid)
 2447         const char *name;
 2448         uid_t uid;
 2449         pid_t pid;
 2450 {
 2451         const char *format, *appendstr;
 2452         char *temp;
 2453         char buf[11];           /* Buffer for pid/uid -- max 4B */
 2454         size_t i, l, n;
 2455 
 2456         format = corefilename;
 2457         temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
 2458         if (temp == NULL)
 2459                 return (NULL);
 2460         for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
 2461                 switch (format[i]) {
 2462                 case '%':       /* Format character */
 2463                         i++;
 2464                         switch (format[i]) {
 2465                         case '%':
 2466                                 appendstr = "%";
 2467                                 break;
 2468                         case 'N':       /* process name */
 2469                                 appendstr = name;
 2470                                 break;
 2471                         case 'P':       /* process id */
 2472                                 sprintf(buf, "%u", pid);
 2473                                 appendstr = buf;
 2474                                 break;
 2475                         case 'U':       /* user id */
 2476                                 sprintf(buf, "%u", uid);
 2477                                 appendstr = buf;
 2478                                 break;
 2479                         default:
 2480                                 appendstr = "";
 2481                                 log(LOG_ERR,
 2482                                     "Unknown format character %c in `%s'\n",
 2483                                     format[i], format);
 2484                         }
 2485                         l = strlen(appendstr);
 2486                         if ((n + l) >= MAXPATHLEN)
 2487                                 goto toolong;
 2488                         memcpy(temp + n, appendstr, l);
 2489                         n += l;
 2490                         break;
 2491                 default:
 2492                         temp[n++] = format[i];
 2493                 }
 2494         }
 2495         if (format[i] != '\0')
 2496                 goto toolong;
 2497         return (temp);
 2498 toolong:
 2499         log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
 2500             (long)pid, name, (u_long)uid);
 2501         free(temp, M_TEMP);
 2502         return (NULL);
 2503 }
 2504 
 2505 /*
 2506  * Dump a process' core.  The main routine does some
 2507  * policy checking, and creates the name of the coredump;
 2508  * then it passes on a vnode and a size limit to the process-specific
 2509  * coredump routine if there is one; if there _is not_ one, it returns
 2510  * ENOSYS; otherwise it returns the error from the process-specific routine.
 2511  */
 2512 
 2513 static int
 2514 coredump(struct thread *td)
 2515 {
 2516         struct proc *p = td->td_proc;
 2517         register struct vnode *vp;
 2518         register struct ucred *cred = td->td_ucred;
 2519         struct flock lf;
 2520         struct nameidata nd;
 2521         struct vattr vattr;
 2522         int error, error1, flags, locked;
 2523         struct mount *mp;
 2524         char *name;                     /* name of corefile */
 2525         off_t limit;
 2526 
 2527         PROC_LOCK_ASSERT(p, MA_OWNED);
 2528         _STOPEVENT(p, S_CORE, 0);
 2529 
 2530         if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
 2531                 PROC_UNLOCK(p);
 2532                 return (EFAULT);
 2533         }
 2534         
 2535         /*
 2536          * Note that the bulk of limit checking is done after
 2537          * the corefile is created.  The exception is if the limit
 2538          * for corefiles is 0, in which case we don't bother
 2539          * creating the corefile at all.  This layout means that
 2540          * a corefile is truncated instead of not being created,
 2541          * if it is larger than the limit.
 2542          */
 2543         limit = (off_t)lim_cur(p, RLIMIT_CORE);
 2544         PROC_UNLOCK(p);
 2545         if (limit == 0)
 2546                 return (EFBIG);
 2547 
 2548         mtx_lock(&Giant);
 2549 restart:
 2550         name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
 2551         if (name == NULL) {
 2552                 mtx_unlock(&Giant);
 2553                 return (EINVAL);
 2554         }
 2555         NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
 2556         flags = O_CREAT | FWRITE | O_NOFOLLOW;
 2557         error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
 2558         free(name, M_TEMP);
 2559         if (error) {
 2560                 mtx_unlock(&Giant);             
 2561                 return (error);
 2562         }
 2563         NDFREE(&nd, NDF_ONLY_PNBUF);
 2564         vp = nd.ni_vp;
 2565 
 2566         /* Don't dump to non-regular files or files with links. */
 2567         if (vp->v_type != VREG ||
 2568             VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
 2569                 VOP_UNLOCK(vp, 0, td);
 2570                 error = EFAULT;
 2571                 goto out;
 2572         }
 2573 
 2574         VOP_UNLOCK(vp, 0, td);
 2575         lf.l_whence = SEEK_SET;
 2576         lf.l_start = 0;
 2577         lf.l_len = 0;
 2578         lf.l_type = F_WRLCK;
 2579         locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
 2580 
 2581         if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
 2582                 lf.l_type = F_UNLCK;
 2583                 if (locked)
 2584                         VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
 2585                 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
 2586                         return (error);
 2587                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
 2588                         return (error);
 2589                 goto restart;
 2590         }
 2591 
 2592         VATTR_NULL(&vattr);
 2593         vattr.va_size = 0;
 2594         if (set_core_nodump_flag)
 2595                 vattr.va_flags = UF_NODUMP;
 2596         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
 2597         VOP_LEASE(vp, td, cred, LEASE_WRITE);
 2598         VOP_SETATTR(vp, &vattr, cred, td);
 2599         VOP_UNLOCK(vp, 0, td);
 2600         PROC_LOCK(p);
 2601         p->p_acflag |= ACORE;
 2602         PROC_UNLOCK(p);
 2603 
 2604         error = p->p_sysent->sv_coredump ?
 2605           p->p_sysent->sv_coredump(td, vp, limit) :
 2606           ENOSYS;
 2607 
 2608         if (locked) {
 2609                 lf.l_type = F_UNLCK;
 2610                 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
 2611         }
 2612         vn_finished_write(mp);
 2613 out:
 2614         error1 = vn_close(vp, FWRITE, cred, td);
 2615         mtx_unlock(&Giant);
 2616         if (error == 0)
 2617                 error = error1;
 2618         return (error);
 2619 }
 2620 
 2621 /*
 2622  * Nonexistent system call-- signal process (may want to handle it).
 2623  * Flag error in case process won't see signal immediately (blocked or ignored).
 2624  */
 2625 #ifndef _SYS_SYSPROTO_H_
 2626 struct nosys_args {
 2627         int     dummy;
 2628 };
 2629 #endif
 2630 /*
 2631  * MPSAFE
 2632  */
 2633 /* ARGSUSED */
 2634 int
 2635 nosys(td, args)
 2636         struct thread *td;
 2637         struct nosys_args *args;
 2638 {
 2639         struct proc *p = td->td_proc;
 2640 
 2641         PROC_LOCK(p);
 2642         psignal(p, SIGSYS);
 2643         PROC_UNLOCK(p);
 2644         return (ENOSYS);
 2645 }
 2646 
 2647 /*
 2648  * Send a SIGIO or SIGURG signal to a process or process group using
 2649  * stored credentials rather than those of the current process.
 2650  */
 2651 void
 2652 pgsigio(sigiop, sig, checkctty)
 2653         struct sigio **sigiop;
 2654         int sig, checkctty;
 2655 {
 2656         struct sigio *sigio;
 2657 
 2658         SIGIO_LOCK();
 2659         sigio = *sigiop;
 2660         if (sigio == NULL) {
 2661                 SIGIO_UNLOCK();
 2662                 return;
 2663         }
 2664         if (sigio->sio_pgid > 0) {
 2665                 PROC_LOCK(sigio->sio_proc);
 2666                 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
 2667                         psignal(sigio->sio_proc, sig);
 2668                 PROC_UNLOCK(sigio->sio_proc);
 2669         } else if (sigio->sio_pgid < 0) {
 2670                 struct proc *p;
 2671 
 2672                 PGRP_LOCK(sigio->sio_pgrp);
 2673                 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
 2674                         PROC_LOCK(p);
 2675                         if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
 2676                             (checkctty == 0 || (p->p_flag & P_CONTROLT)))
 2677                                 psignal(p, sig);
 2678                         PROC_UNLOCK(p);
 2679                 }
 2680                 PGRP_UNLOCK(sigio->sio_pgrp);
 2681         }
 2682         SIGIO_UNLOCK();
 2683 }
 2684 
 2685 static int
 2686 filt_sigattach(struct knote *kn)
 2687 {
 2688         struct proc *p = curproc;
 2689 
 2690         kn->kn_ptr.p_proc = p;
 2691         kn->kn_flags |= EV_CLEAR;               /* automatically set */
 2692 
 2693         knlist_add(&p->p_klist, kn, 0);
 2694 
 2695         return (0);
 2696 }
 2697 
 2698 static void
 2699 filt_sigdetach(struct knote *kn)
 2700 {
 2701         struct proc *p = kn->kn_ptr.p_proc;
 2702 
 2703         knlist_remove(&p->p_klist, kn, 0);
 2704 }
 2705 
 2706 /*
 2707  * signal knotes are shared with proc knotes, so we apply a mask to 
 2708  * the hint in order to differentiate them from process hints.  This
 2709  * could be avoided by using a signal-specific knote list, but probably
 2710  * isn't worth the trouble.
 2711  */
 2712 static int
 2713 filt_signal(struct knote *kn, long hint)
 2714 {
 2715 
 2716         if (hint & NOTE_SIGNAL) {
 2717                 hint &= ~NOTE_SIGNAL;
 2718 
 2719                 if (kn->kn_id == hint)
 2720                         kn->kn_data++;
 2721         }
 2722         return (kn->kn_data != 0);
 2723 }
 2724 
 2725 struct sigacts *
 2726 sigacts_alloc(void)
 2727 {
 2728         struct sigacts *ps;
 2729 
 2730         ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
 2731         ps->ps_refcnt = 1;
 2732         mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
 2733         return (ps);
 2734 }
 2735 
 2736 void
 2737 sigacts_free(struct sigacts *ps)
 2738 {
 2739 
 2740         mtx_lock(&ps->ps_mtx);
 2741         ps->ps_refcnt--;
 2742         if (ps->ps_refcnt == 0) {
 2743                 mtx_destroy(&ps->ps_mtx);
 2744                 free(ps, M_SUBPROC);
 2745         } else
 2746                 mtx_unlock(&ps->ps_mtx);
 2747 }
 2748 
 2749 struct sigacts *
 2750 sigacts_hold(struct sigacts *ps)
 2751 {
 2752         mtx_lock(&ps->ps_mtx);
 2753         ps->ps_refcnt++;
 2754         mtx_unlock(&ps->ps_mtx);
 2755         return (ps);
 2756 }
 2757 
 2758 void
 2759 sigacts_copy(struct sigacts *dest, struct sigacts *src)
 2760 {
 2761 
 2762         KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
 2763         mtx_lock(&src->ps_mtx);
 2764         bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
 2765         mtx_unlock(&src->ps_mtx);
 2766 }
 2767 
 2768 int
 2769 sigacts_shared(struct sigacts *ps)
 2770 {
 2771         int shared;
 2772 
 2773         mtx_lock(&ps->ps_mtx);
 2774         shared = ps->ps_refcnt > 1;
 2775         mtx_unlock(&ps->ps_mtx);
 2776         return (shared);
 2777 }

Cache object: 913e91288e17353345a5112a61e1ba74


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