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

Cache object: c7517a1aa096aabd17925829eef4403e


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