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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: b5e0171dd9a233ac0e71a6073433dbc4


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