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


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

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

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

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

Cache object: 745220517efd061d4c71a9d2a1796c8a


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