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/8.4/sys/kern/kern_sig.c 247350 2013-02-26 21:05:06Z jhb $");
   39 
   40 #include "opt_compat.h"
   41 #include "opt_kdtrace.h"
   42 #include "opt_ktrace.h"
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 #include <sys/signalvar.h>
   47 #include <sys/vnode.h>
   48 #include <sys/acct.h>
   49 #include <sys/condvar.h>
   50 #include <sys/event.h>
   51 #include <sys/fcntl.h>
   52 #include <sys/kernel.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/posix4.h>
   61 #include <sys/pioctl.h>
   62 #include <sys/resourcevar.h>
   63 #include <sys/sdt.h>
   64 #include <sys/sbuf.h>
   65 #include <sys/sleepqueue.h>
   66 #include <sys/smp.h>
   67 #include <sys/stat.h>
   68 #include <sys/sx.h>
   69 #include <sys/syscallsubr.h>
   70 #include <sys/sysctl.h>
   71 #include <sys/sysent.h>
   72 #include <sys/syslog.h>
   73 #include <sys/sysproto.h>
   74 #include <sys/timers.h>
   75 #include <sys/unistd.h>
   76 #include <sys/wait.h>
   77 #include <vm/vm.h>
   78 #include <vm/vm_extern.h>
   79 #include <vm/uma.h>
   80 
   81 #include <machine/cpu.h>
   82 
   83 #include <security/audit/audit.h>
   84 
   85 #define ONSIG   32              /* NSIG for osig* syscalls.  XXX. */
   86 
   87 SDT_PROVIDER_DECLARE(proc);
   88 SDT_PROBE_DEFINE(proc, kernel, , signal_send, signal-send);
   89 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 0, "struct thread *");
   90 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 1, "struct proc *");
   91 SDT_PROBE_ARGTYPE(proc, kernel, , signal_send, 2, "int");
   92 SDT_PROBE_DEFINE(proc, kernel, , signal_clear, signal-clear);
   93 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 0, "int");
   94 SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *");
   95 SDT_PROBE_DEFINE(proc, kernel, , signal_discard, signal-discard);
   96 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *");
   97 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
   98 SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
   99 
  100 static int      coredump(struct thread *);
  101 static char     *expand_name(const char *, uid_t, pid_t);
  102 static int      killpg1(struct thread *td, int sig, int pgid, int all,
  103                     ksiginfo_t *ksi);
  104 static int      issignal(struct thread *td, int stop_allowed);
  105 static int      sigprop(int sig);
  106 static void     tdsigwakeup(struct thread *, int, sig_t, int);
  107 static void     sig_suspend_threads(struct thread *, struct proc *, int);
  108 static int      filt_sigattach(struct knote *kn);
  109 static void     filt_sigdetach(struct knote *kn);
  110 static int      filt_signal(struct knote *kn, long hint);
  111 static struct thread *sigtd(struct proc *p, int sig, int prop);
  112 static void     sigqueue_start(void);
  113 
  114 static uma_zone_t       ksiginfo_zone = NULL;
  115 struct filterops sig_filtops =
  116         { 0, filt_sigattach, filt_sigdetach, filt_signal };
  117 
  118 int     kern_logsigexit = 1;
  119 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 
  120     &kern_logsigexit, 0, 
  121     "Log processes quitting on abnormal signals to syslog(3)");
  122 
  123 static int      kern_forcesigexit = 1;
  124 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
  125     &kern_forcesigexit, 0, "Force trap signal to be handled");
  126 
  127 SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0, "POSIX real time signal");
  128 
  129 static int      max_pending_per_proc = 128;
  130 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
  131     &max_pending_per_proc, 0, "Max pending signals per proc");
  132 
  133 static int      preallocate_siginfo = 1024;
  134 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
  135 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
  136     &preallocate_siginfo, 0, "Preallocated signal memory size");
  137 
  138 static int      signal_overflow = 0;
  139 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
  140     &signal_overflow, 0, "Number of signals overflew");
  141 
  142 static int      signal_alloc_fail = 0;
  143 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
  144     &signal_alloc_fail, 0, "signals failed to be allocated");
  145 
  146 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
  147 
  148 /*
  149  * Policy -- Can ucred cr1 send SIGIO to process cr2?
  150  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
  151  * in the right situations.
  152  */
  153 #define CANSIGIO(cr1, cr2) \
  154         ((cr1)->cr_uid == 0 || \
  155             (cr1)->cr_ruid == (cr2)->cr_ruid || \
  156             (cr1)->cr_uid == (cr2)->cr_ruid || \
  157             (cr1)->cr_ruid == (cr2)->cr_uid || \
  158             (cr1)->cr_uid == (cr2)->cr_uid)
  159 
  160 int sugid_coredump;
  161 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 
  162     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
  163 
  164 static int      do_coredump = 1;
  165 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
  166         &do_coredump, 0, "Enable/Disable coredumps");
  167 
  168 static int      set_core_nodump_flag = 0;
  169 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
  170         0, "Enable setting the NODUMP flag on coredump files");
  171 
  172 /*
  173  * Signal properties and actions.
  174  * The array below categorizes the signals and their default actions
  175  * according to the following properties:
  176  */
  177 #define SA_KILL         0x01            /* terminates process by default */
  178 #define SA_CORE         0x02            /* ditto and coredumps */
  179 #define SA_STOP         0x04            /* suspend process */
  180 #define SA_TTYSTOP      0x08            /* ditto, from tty */
  181 #define SA_IGNORE       0x10            /* ignore by default */
  182 #define SA_CONT         0x20            /* continue if suspended */
  183 #define SA_CANTMASK     0x40            /* non-maskable, catchable */
  184 #define SA_PROC         0x80            /* deliverable to any thread */
  185 
  186 static int sigproptbl[NSIG] = {
  187         SA_KILL|SA_PROC,                /* SIGHUP */
  188         SA_KILL|SA_PROC,                /* SIGINT */
  189         SA_KILL|SA_CORE|SA_PROC,        /* SIGQUIT */
  190         SA_KILL|SA_CORE,                /* SIGILL */
  191         SA_KILL|SA_CORE,                /* SIGTRAP */
  192         SA_KILL|SA_CORE,                /* SIGABRT */
  193         SA_KILL|SA_CORE|SA_PROC,        /* SIGEMT */
  194         SA_KILL|SA_CORE,                /* SIGFPE */
  195         SA_KILL|SA_PROC,                /* SIGKILL */
  196         SA_KILL|SA_CORE,                /* SIGBUS */
  197         SA_KILL|SA_CORE,                /* SIGSEGV */
  198         SA_KILL|SA_CORE,                /* SIGSYS */
  199         SA_KILL|SA_PROC,                /* SIGPIPE */
  200         SA_KILL|SA_PROC,                /* SIGALRM */
  201         SA_KILL|SA_PROC,                /* SIGTERM */
  202         SA_IGNORE|SA_PROC,              /* SIGURG */
  203         SA_STOP|SA_PROC,                /* SIGSTOP */
  204         SA_STOP|SA_TTYSTOP|SA_PROC,     /* SIGTSTP */
  205         SA_IGNORE|SA_CONT|SA_PROC,      /* SIGCONT */
  206         SA_IGNORE|SA_PROC,              /* SIGCHLD */
  207         SA_STOP|SA_TTYSTOP|SA_PROC,     /* SIGTTIN */
  208         SA_STOP|SA_TTYSTOP|SA_PROC,     /* SIGTTOU */
  209         SA_IGNORE|SA_PROC,              /* SIGIO */
  210         SA_KILL,                        /* SIGXCPU */
  211         SA_KILL,                        /* SIGXFSZ */
  212         SA_KILL|SA_PROC,                /* SIGVTALRM */
  213         SA_KILL|SA_PROC,                /* SIGPROF */
  214         SA_IGNORE|SA_PROC,              /* SIGWINCH  */
  215         SA_IGNORE|SA_PROC,              /* SIGINFO */
  216         SA_KILL|SA_PROC,                /* SIGUSR1 */
  217         SA_KILL|SA_PROC,                /* SIGUSR2 */
  218 };
  219 
  220 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
  221 
  222 static void
  223 sigqueue_start(void)
  224 {
  225         ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
  226                 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
  227         uma_prealloc(ksiginfo_zone, preallocate_siginfo);
  228         p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
  229         p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
  230         p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
  231 }
  232 
  233 ksiginfo_t *
  234 ksiginfo_alloc(int wait)
  235 {
  236         int flags;
  237 
  238         flags = M_ZERO;
  239         if (! wait)
  240                 flags |= M_NOWAIT;
  241         if (ksiginfo_zone != NULL)
  242                 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
  243         return (NULL);
  244 }
  245 
  246 void
  247 ksiginfo_free(ksiginfo_t *ksi)
  248 {
  249         uma_zfree(ksiginfo_zone, ksi);
  250 }
  251 
  252 static __inline int
  253 ksiginfo_tryfree(ksiginfo_t *ksi)
  254 {
  255         if (!(ksi->ksi_flags & KSI_EXT)) {
  256                 uma_zfree(ksiginfo_zone, ksi);
  257                 return (1);
  258         }
  259         return (0);
  260 }
  261 
  262 void
  263 sigqueue_init(sigqueue_t *list, struct proc *p)
  264 {
  265         SIGEMPTYSET(list->sq_signals);
  266         SIGEMPTYSET(list->sq_kill);
  267         TAILQ_INIT(&list->sq_list);
  268         list->sq_proc = p;
  269         list->sq_flags = SQ_INIT;
  270 }
  271 
  272 /*
  273  * Get a signal's ksiginfo.
  274  * Return:
  275  *      0       -       signal not found
  276  *      others  -       signal number
  277  */ 
  278 static int
  279 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
  280 {
  281         struct proc *p = sq->sq_proc;
  282         struct ksiginfo *ksi, *next;
  283         int count = 0;
  284 
  285         KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
  286 
  287         if (!SIGISMEMBER(sq->sq_signals, signo))
  288                 return (0);
  289 
  290         if (SIGISMEMBER(sq->sq_kill, signo)) {
  291                 count++;
  292                 SIGDELSET(sq->sq_kill, signo);
  293         }
  294 
  295         TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
  296                 if (ksi->ksi_signo == signo) {
  297                         if (count == 0) {
  298                                 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
  299                                 ksi->ksi_sigq = NULL;
  300                                 ksiginfo_copy(ksi, si);
  301                                 if (ksiginfo_tryfree(ksi) && p != NULL)
  302                                         p->p_pendingcnt--;
  303                         }
  304                         if (++count > 1)
  305                                 break;
  306                 }
  307         }
  308 
  309         if (count <= 1)
  310                 SIGDELSET(sq->sq_signals, signo);
  311         si->ksi_signo = signo;
  312         return (signo);
  313 }
  314 
  315 void
  316 sigqueue_take(ksiginfo_t *ksi)
  317 {
  318         struct ksiginfo *kp;
  319         struct proc     *p;
  320         sigqueue_t      *sq;
  321 
  322         if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
  323                 return;
  324 
  325         p = sq->sq_proc;
  326         TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
  327         ksi->ksi_sigq = NULL;
  328         if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
  329                 p->p_pendingcnt--;
  330 
  331         for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
  332              kp = TAILQ_NEXT(kp, ksi_link)) {
  333                 if (kp->ksi_signo == ksi->ksi_signo)
  334                         break;
  335         }
  336         if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
  337                 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
  338 }
  339 
  340 static int
  341 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
  342 {
  343         struct proc *p = sq->sq_proc;
  344         struct ksiginfo *ksi;
  345         int ret = 0;
  346 
  347         KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
  348         
  349         if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
  350                 SIGADDSET(sq->sq_kill, signo);
  351                 goto out_set_bit;
  352         }
  353 
  354         /* directly insert the ksi, don't copy it */
  355         if (si->ksi_flags & KSI_INS) {
  356                 if (si->ksi_flags & KSI_HEAD)
  357                         TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
  358                 else
  359                         TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
  360                 si->ksi_sigq = sq;
  361                 goto out_set_bit;
  362         }
  363 
  364         if (__predict_false(ksiginfo_zone == NULL)) {
  365                 SIGADDSET(sq->sq_kill, signo);
  366                 goto out_set_bit;
  367         }
  368         
  369         if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
  370                 signal_overflow++;
  371                 ret = EAGAIN;
  372         } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
  373                 signal_alloc_fail++;
  374                 ret = EAGAIN;
  375         } else {
  376                 if (p != NULL)
  377                         p->p_pendingcnt++;
  378                 ksiginfo_copy(si, ksi);
  379                 ksi->ksi_signo = signo;
  380                 if (si->ksi_flags & KSI_HEAD)
  381                         TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
  382                 else
  383                         TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
  384                 ksi->ksi_sigq = sq;
  385         }
  386 
  387         if ((si->ksi_flags & KSI_TRAP) != 0 ||
  388             (si->ksi_flags & KSI_SIGQ) == 0) {
  389                 if (ret != 0)
  390                         SIGADDSET(sq->sq_kill, signo);
  391                 ret = 0;
  392                 goto out_set_bit;
  393         }
  394 
  395         if (ret != 0)
  396                 return (ret);
  397         
  398 out_set_bit:
  399         SIGADDSET(sq->sq_signals, signo);
  400         return (ret);
  401 }
  402 
  403 void
  404 sigqueue_flush(sigqueue_t *sq)
  405 {
  406         struct proc *p = sq->sq_proc;
  407         ksiginfo_t *ksi;
  408 
  409         KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
  410 
  411         if (p != NULL)
  412                 PROC_LOCK_ASSERT(p, MA_OWNED);
  413 
  414         while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
  415                 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
  416                 ksi->ksi_sigq = NULL;
  417                 if (ksiginfo_tryfree(ksi) && p != NULL)
  418                         p->p_pendingcnt--;
  419         }
  420 
  421         SIGEMPTYSET(sq->sq_signals);
  422         SIGEMPTYSET(sq->sq_kill);
  423 }
  424 
  425 static void
  426 sigqueue_collect_set(sigqueue_t *sq, sigset_t *set)
  427 {
  428         ksiginfo_t *ksi;
  429 
  430         KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
  431 
  432         TAILQ_FOREACH(ksi, &sq->sq_list, ksi_link)
  433                 SIGADDSET(*set, ksi->ksi_signo);
  434         SIGSETOR(*set, sq->sq_kill);
  435 }
  436 
  437 static void
  438 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, sigset_t *setp)
  439 {
  440         sigset_t tmp, set;
  441         struct proc *p1, *p2;
  442         ksiginfo_t *ksi, *next;
  443 
  444         KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
  445         KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
  446         /*
  447          * make a copy, this allows setp to point to src or dst
  448          * sq_signals without trouble.
  449          */
  450         set = *setp;
  451         p1 = src->sq_proc;
  452         p2 = dst->sq_proc;
  453         /* Move siginfo to target list */
  454         TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
  455                 if (SIGISMEMBER(set, ksi->ksi_signo)) {
  456                         TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
  457                         if (p1 != NULL)
  458                                 p1->p_pendingcnt--;
  459                         TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
  460                         ksi->ksi_sigq = dst;
  461                         if (p2 != NULL)
  462                                 p2->p_pendingcnt++;
  463                 }
  464         }
  465 
  466         /* Move pending bits to target list */
  467         tmp = src->sq_kill;
  468         SIGSETAND(tmp, set);
  469         SIGSETOR(dst->sq_kill, tmp);
  470         SIGSETNAND(src->sq_kill, tmp);
  471 
  472         tmp = src->sq_signals;
  473         SIGSETAND(tmp, set);
  474         SIGSETOR(dst->sq_signals, tmp);
  475         SIGSETNAND(src->sq_signals, tmp);
  476 
  477         /* Finally, rescan src queue and set pending bits for it */
  478         sigqueue_collect_set(src, &src->sq_signals);
  479 }
  480 
  481 static void
  482 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
  483 {
  484         sigset_t set;
  485 
  486         SIGEMPTYSET(set);
  487         SIGADDSET(set, signo);
  488         sigqueue_move_set(src, dst, &set);
  489 }
  490 
  491 static void
  492 sigqueue_delete_set(sigqueue_t *sq, sigset_t *set)
  493 {
  494         struct proc *p = sq->sq_proc;
  495         ksiginfo_t *ksi, *next;
  496 
  497         KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
  498 
  499         /* Remove siginfo queue */
  500         TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
  501                 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
  502                         TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
  503                         ksi->ksi_sigq = NULL;
  504                         if (ksiginfo_tryfree(ksi) && p != NULL)
  505                                 p->p_pendingcnt--;
  506                 }
  507         }
  508         SIGSETNAND(sq->sq_kill, *set);
  509         SIGSETNAND(sq->sq_signals, *set);
  510         /* Finally, rescan queue and set pending bits for it */
  511         sigqueue_collect_set(sq, &sq->sq_signals);
  512 }
  513 
  514 void
  515 sigqueue_delete(sigqueue_t *sq, int signo)
  516 {
  517         sigset_t set;
  518 
  519         SIGEMPTYSET(set);
  520         SIGADDSET(set, signo);
  521         sigqueue_delete_set(sq, &set);
  522 }
  523 
  524 /* Remove a set of signals for a process */
  525 static void
  526 sigqueue_delete_set_proc(struct proc *p, sigset_t *set)
  527 {
  528         sigqueue_t worklist;
  529         struct thread *td0;
  530 
  531         PROC_LOCK_ASSERT(p, MA_OWNED);
  532 
  533         sigqueue_init(&worklist, NULL);
  534         sigqueue_move_set(&p->p_sigqueue, &worklist, set);
  535 
  536         FOREACH_THREAD_IN_PROC(p, td0)
  537                 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
  538 
  539         sigqueue_flush(&worklist);
  540 }
  541 
  542 void
  543 sigqueue_delete_proc(struct proc *p, int signo)
  544 {
  545         sigset_t set;
  546 
  547         SIGEMPTYSET(set);
  548         SIGADDSET(set, signo);
  549         sigqueue_delete_set_proc(p, &set);
  550 }
  551 
  552 static void
  553 sigqueue_delete_stopmask_proc(struct proc *p)
  554 {
  555         sigset_t set;
  556 
  557         SIGEMPTYSET(set);
  558         SIGADDSET(set, SIGSTOP);
  559         SIGADDSET(set, SIGTSTP);
  560         SIGADDSET(set, SIGTTIN);
  561         SIGADDSET(set, SIGTTOU);
  562         sigqueue_delete_set_proc(p, &set);
  563 }
  564 
  565 /*
  566  * Determine signal that should be delivered to process p, the current
  567  * process, 0 if none.  If there is a pending stop signal with default
  568  * action, the process stops in issignal().
  569  */
  570 int
  571 cursig(struct thread *td, int stop_allowed)
  572 {
  573         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
  574         KASSERT(stop_allowed == SIG_STOP_ALLOWED ||
  575             stop_allowed == SIG_STOP_NOT_ALLOWED, ("cursig: stop_allowed"));
  576         mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
  577         THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
  578         return (SIGPENDING(td) ? issignal(td, stop_allowed) : 0);
  579 }
  580 
  581 /*
  582  * Arrange for ast() to handle unmasked pending signals on return to user
  583  * mode.  This must be called whenever a signal is added to td_sigqueue or
  584  * unmasked in td_sigmask.
  585  */
  586 void
  587 signotify(struct thread *td)
  588 {
  589         struct proc *p;
  590 
  591         p = td->td_proc;
  592 
  593         PROC_LOCK_ASSERT(p, MA_OWNED);
  594 
  595         if (SIGPENDING(td)) {
  596                 thread_lock(td);
  597                 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
  598                 thread_unlock(td);
  599         }
  600 }
  601 
  602 int
  603 sigonstack(size_t sp)
  604 {
  605         struct thread *td = curthread;
  606 
  607         return ((td->td_pflags & TDP_ALTSTACK) ?
  608 #if defined(COMPAT_43)
  609             ((td->td_sigstk.ss_size == 0) ?
  610                 (td->td_sigstk.ss_flags & SS_ONSTACK) :
  611                 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
  612 #else
  613             ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
  614 #endif
  615             : 0);
  616 }
  617 
  618 static __inline int
  619 sigprop(int sig)
  620 {
  621 
  622         if (sig > 0 && sig < NSIG)
  623                 return (sigproptbl[_SIG_IDX(sig)]);
  624         return (0);
  625 }
  626 
  627 int
  628 sig_ffs(sigset_t *set)
  629 {
  630         int i;
  631 
  632         for (i = 0; i < _SIG_WORDS; i++)
  633                 if (set->__bits[i])
  634                         return (ffs(set->__bits[i]) + (i * 32));
  635         return (0);
  636 }
  637 
  638 /*
  639  * kern_sigaction
  640  * sigaction
  641  * freebsd4_sigaction
  642  * osigaction
  643  */
  644 int
  645 kern_sigaction(td, sig, act, oact, flags)
  646         struct thread *td;
  647         register int sig;
  648         struct sigaction *act, *oact;
  649         int flags;
  650 {
  651         struct sigacts *ps;
  652         struct proc *p = td->td_proc;
  653 
  654         if (!_SIG_VALID(sig))
  655                 return (EINVAL);
  656 
  657         PROC_LOCK(p);
  658         ps = p->p_sigacts;
  659         mtx_lock(&ps->ps_mtx);
  660         if (oact) {
  661                 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
  662                 oact->sa_flags = 0;
  663                 if (SIGISMEMBER(ps->ps_sigonstack, sig))
  664                         oact->sa_flags |= SA_ONSTACK;
  665                 if (!SIGISMEMBER(ps->ps_sigintr, sig))
  666                         oact->sa_flags |= SA_RESTART;
  667                 if (SIGISMEMBER(ps->ps_sigreset, sig))
  668                         oact->sa_flags |= SA_RESETHAND;
  669                 if (SIGISMEMBER(ps->ps_signodefer, sig))
  670                         oact->sa_flags |= SA_NODEFER;
  671                 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
  672                         oact->sa_flags |= SA_SIGINFO;
  673                         oact->sa_sigaction =
  674                             (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
  675                 } else
  676                         oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
  677                 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
  678                         oact->sa_flags |= SA_NOCLDSTOP;
  679                 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
  680                         oact->sa_flags |= SA_NOCLDWAIT;
  681         }
  682         if (act) {
  683                 if ((sig == SIGKILL || sig == SIGSTOP) &&
  684                     act->sa_handler != SIG_DFL) {
  685                         mtx_unlock(&ps->ps_mtx);
  686                         PROC_UNLOCK(p);
  687                         return (EINVAL);
  688                 }
  689 
  690                 /*
  691                  * Change setting atomically.
  692                  */
  693 
  694                 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
  695                 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
  696                 if (act->sa_flags & SA_SIGINFO) {
  697                         ps->ps_sigact[_SIG_IDX(sig)] =
  698                             (__sighandler_t *)act->sa_sigaction;
  699                         SIGADDSET(ps->ps_siginfo, sig);
  700                 } else {
  701                         ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
  702                         SIGDELSET(ps->ps_siginfo, sig);
  703                 }
  704                 if (!(act->sa_flags & SA_RESTART))
  705                         SIGADDSET(ps->ps_sigintr, sig);
  706                 else
  707                         SIGDELSET(ps->ps_sigintr, sig);
  708                 if (act->sa_flags & SA_ONSTACK)
  709                         SIGADDSET(ps->ps_sigonstack, sig);
  710                 else
  711                         SIGDELSET(ps->ps_sigonstack, sig);
  712                 if (act->sa_flags & SA_RESETHAND)
  713                         SIGADDSET(ps->ps_sigreset, sig);
  714                 else
  715                         SIGDELSET(ps->ps_sigreset, sig);
  716                 if (act->sa_flags & SA_NODEFER)
  717                         SIGADDSET(ps->ps_signodefer, sig);
  718                 else
  719                         SIGDELSET(ps->ps_signodefer, sig);
  720                 if (sig == SIGCHLD) {
  721                         if (act->sa_flags & SA_NOCLDSTOP)
  722                                 ps->ps_flag |= PS_NOCLDSTOP;
  723                         else
  724                                 ps->ps_flag &= ~PS_NOCLDSTOP;
  725                         if (act->sa_flags & SA_NOCLDWAIT) {
  726                                 /*
  727                                  * Paranoia: since SA_NOCLDWAIT is implemented
  728                                  * by reparenting the dying child to PID 1 (and
  729                                  * trust it to reap the zombie), PID 1 itself
  730                                  * is forbidden to set SA_NOCLDWAIT.
  731                                  */
  732                                 if (p->p_pid == 1)
  733                                         ps->ps_flag &= ~PS_NOCLDWAIT;
  734                                 else
  735                                         ps->ps_flag |= PS_NOCLDWAIT;
  736                         } else
  737                                 ps->ps_flag &= ~PS_NOCLDWAIT;
  738                         if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
  739                                 ps->ps_flag |= PS_CLDSIGIGN;
  740                         else
  741                                 ps->ps_flag &= ~PS_CLDSIGIGN;
  742                 }
  743                 /*
  744                  * Set bit in ps_sigignore for signals that are set to SIG_IGN,
  745                  * and for signals set to SIG_DFL where the default is to
  746                  * ignore. However, don't put SIGCONT in ps_sigignore, as we
  747                  * have to restart the process.
  748                  */
  749                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
  750                     (sigprop(sig) & SA_IGNORE &&
  751                      ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
  752                         /* never to be seen again */
  753                         sigqueue_delete_proc(p, sig);
  754                         if (sig != SIGCONT)
  755                                 /* easier in psignal */
  756                                 SIGADDSET(ps->ps_sigignore, sig);
  757                         SIGDELSET(ps->ps_sigcatch, sig);
  758                 } else {
  759                         SIGDELSET(ps->ps_sigignore, sig);
  760                         if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
  761                                 SIGDELSET(ps->ps_sigcatch, sig);
  762                         else
  763                                 SIGADDSET(ps->ps_sigcatch, sig);
  764                 }
  765 #ifdef COMPAT_FREEBSD4
  766                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
  767                     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
  768                     (flags & KSA_FREEBSD4) == 0)
  769                         SIGDELSET(ps->ps_freebsd4, sig);
  770                 else
  771                         SIGADDSET(ps->ps_freebsd4, sig);
  772 #endif
  773 #ifdef COMPAT_43
  774                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
  775                     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
  776                     (flags & KSA_OSIGSET) == 0)
  777                         SIGDELSET(ps->ps_osigset, sig);
  778                 else
  779                         SIGADDSET(ps->ps_osigset, sig);
  780 #endif
  781         }
  782         mtx_unlock(&ps->ps_mtx);
  783         PROC_UNLOCK(p);
  784         return (0);
  785 }
  786 
  787 #ifndef _SYS_SYSPROTO_H_
  788 struct sigaction_args {
  789         int     sig;
  790         struct  sigaction *act;
  791         struct  sigaction *oact;
  792 };
  793 #endif
  794 int
  795 sigaction(td, uap)
  796         struct thread *td;
  797         register struct sigaction_args *uap;
  798 {
  799         struct sigaction act, oact;
  800         register struct sigaction *actp, *oactp;
  801         int error;
  802 
  803         actp = (uap->act != NULL) ? &act : NULL;
  804         oactp = (uap->oact != NULL) ? &oact : NULL;
  805         if (actp) {
  806                 error = copyin(uap->act, actp, sizeof(act));
  807                 if (error)
  808                         return (error);
  809         }
  810         error = kern_sigaction(td, uap->sig, actp, oactp, 0);
  811         if (oactp && !error)
  812                 error = copyout(oactp, uap->oact, sizeof(oact));
  813         return (error);
  814 }
  815 
  816 #ifdef COMPAT_FREEBSD4
  817 #ifndef _SYS_SYSPROTO_H_
  818 struct freebsd4_sigaction_args {
  819         int     sig;
  820         struct  sigaction *act;
  821         struct  sigaction *oact;
  822 };
  823 #endif
  824 int
  825 freebsd4_sigaction(td, uap)
  826         struct thread *td;
  827         register struct freebsd4_sigaction_args *uap;
  828 {
  829         struct sigaction act, oact;
  830         register struct sigaction *actp, *oactp;
  831         int error;
  832 
  833 
  834         actp = (uap->act != NULL) ? &act : NULL;
  835         oactp = (uap->oact != NULL) ? &oact : NULL;
  836         if (actp) {
  837                 error = copyin(uap->act, actp, sizeof(act));
  838                 if (error)
  839                         return (error);
  840         }
  841         error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
  842         if (oactp && !error)
  843                 error = copyout(oactp, uap->oact, sizeof(oact));
  844         return (error);
  845 }
  846 #endif  /* COMAPT_FREEBSD4 */
  847 
  848 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
  849 #ifndef _SYS_SYSPROTO_H_
  850 struct osigaction_args {
  851         int     signum;
  852         struct  osigaction *nsa;
  853         struct  osigaction *osa;
  854 };
  855 #endif
  856 int
  857 osigaction(td, uap)
  858         struct thread *td;
  859         register struct osigaction_args *uap;
  860 {
  861         struct osigaction sa;
  862         struct sigaction nsa, osa;
  863         register struct sigaction *nsap, *osap;
  864         int error;
  865 
  866         if (uap->signum <= 0 || uap->signum >= ONSIG)
  867                 return (EINVAL);
  868 
  869         nsap = (uap->nsa != NULL) ? &nsa : NULL;
  870         osap = (uap->osa != NULL) ? &osa : NULL;
  871 
  872         if (nsap) {
  873                 error = copyin(uap->nsa, &sa, sizeof(sa));
  874                 if (error)
  875                         return (error);
  876                 nsap->sa_handler = sa.sa_handler;
  877                 nsap->sa_flags = sa.sa_flags;
  878                 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
  879         }
  880         error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
  881         if (osap && !error) {
  882                 sa.sa_handler = osap->sa_handler;
  883                 sa.sa_flags = osap->sa_flags;
  884                 SIG2OSIG(osap->sa_mask, sa.sa_mask);
  885                 error = copyout(&sa, uap->osa, sizeof(sa));
  886         }
  887         return (error);
  888 }
  889 
  890 #if !defined(__i386__)
  891 /* Avoid replicating the same stub everywhere */
  892 int
  893 osigreturn(td, uap)
  894         struct thread *td;
  895         struct osigreturn_args *uap;
  896 {
  897 
  898         return (nosys(td, (struct nosys_args *)uap));
  899 }
  900 #endif
  901 #endif /* COMPAT_43 */
  902 
  903 /*
  904  * Initialize signal state for process 0;
  905  * set to ignore signals that are ignored by default.
  906  */
  907 void
  908 siginit(p)
  909         struct proc *p;
  910 {
  911         register int i;
  912         struct sigacts *ps;
  913 
  914         PROC_LOCK(p);
  915         ps = p->p_sigacts;
  916         mtx_lock(&ps->ps_mtx);
  917         for (i = 1; i <= NSIG; i++)
  918                 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
  919                         SIGADDSET(ps->ps_sigignore, i);
  920         mtx_unlock(&ps->ps_mtx);
  921         PROC_UNLOCK(p);
  922 }
  923 
  924 /*
  925  * Reset signals for an exec of the specified process.
  926  */
  927 void
  928 execsigs(struct proc *p)
  929 {
  930         struct sigacts *ps;
  931         int sig;
  932         struct thread *td;
  933 
  934         /*
  935          * Reset caught signals.  Held signals remain held
  936          * through td_sigmask (unless they were caught,
  937          * and are now ignored by default).
  938          */
  939         PROC_LOCK_ASSERT(p, MA_OWNED);
  940         td = FIRST_THREAD_IN_PROC(p);
  941         ps = p->p_sigacts;
  942         mtx_lock(&ps->ps_mtx);
  943         while (SIGNOTEMPTY(ps->ps_sigcatch)) {
  944                 sig = sig_ffs(&ps->ps_sigcatch);
  945                 SIGDELSET(ps->ps_sigcatch, sig);
  946                 if (sigprop(sig) & SA_IGNORE) {
  947                         if (sig != SIGCONT)
  948                                 SIGADDSET(ps->ps_sigignore, sig);
  949                         sigqueue_delete_proc(p, sig);
  950                 }
  951                 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
  952         }
  953         /*
  954          * Reset stack state to the user stack.
  955          * Clear set of signals caught on the signal stack.
  956          */
  957         td->td_sigstk.ss_flags = SS_DISABLE;
  958         td->td_sigstk.ss_size = 0;
  959         td->td_sigstk.ss_sp = 0;
  960         td->td_pflags &= ~TDP_ALTSTACK;
  961         /*
  962          * Reset no zombies if child dies flag as Solaris does.
  963          */
  964         ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
  965         if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
  966                 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
  967         mtx_unlock(&ps->ps_mtx);
  968 }
  969 
  970 /*
  971  * kern_sigprocmask()
  972  *
  973  *      Manipulate signal mask.
  974  */
  975 int
  976 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
  977     int flags)
  978 {
  979         sigset_t new_block, oset1;
  980         struct proc *p;
  981         int error;
  982 
  983         p = td->td_proc;
  984         if (!(flags & SIGPROCMASK_PROC_LOCKED))
  985                 PROC_LOCK(p);
  986         if (oset != NULL)
  987                 *oset = td->td_sigmask;
  988 
  989         error = 0;
  990         SIGEMPTYSET(new_block);
  991         if (set != NULL) {
  992                 switch (how) {
  993                 case SIG_BLOCK:
  994                         SIG_CANTMASK(*set);
  995                         oset1 = td->td_sigmask;
  996                         SIGSETOR(td->td_sigmask, *set);
  997                         new_block = td->td_sigmask;
  998                         SIGSETNAND(new_block, oset1);
  999                         break;
 1000                 case SIG_UNBLOCK:
 1001                         SIGSETNAND(td->td_sigmask, *set);
 1002                         signotify(td);
 1003                         break;
 1004                 case SIG_SETMASK:
 1005                         SIG_CANTMASK(*set);
 1006                         oset1 = td->td_sigmask;
 1007                         if (flags & SIGPROCMASK_OLD)
 1008                                 SIGSETLO(td->td_sigmask, *set);
 1009                         else
 1010                                 td->td_sigmask = *set;
 1011                         new_block = td->td_sigmask;
 1012                         SIGSETNAND(new_block, oset1);
 1013                         signotify(td);
 1014                         break;
 1015                 default:
 1016                         error = EINVAL;
 1017                         break;
 1018                 }
 1019         }
 1020 
 1021         /*
 1022          * The new_block set contains signals that were not previously
 1023          * blocked, but are blocked now.
 1024          *
 1025          * In case we block any signal that was not previously blocked
 1026          * for td, and process has the signal pending, try to schedule
 1027          * signal delivery to some thread that does not block the signal,
 1028          * possibly waking it up.
 1029          */
 1030         if (p->p_numthreads != 1)
 1031                 reschedule_signals(p, new_block, flags);
 1032 
 1033         if (!(flags & SIGPROCMASK_PROC_LOCKED))
 1034                 PROC_UNLOCK(p);
 1035         return (error);
 1036 }
 1037 
 1038 #ifndef _SYS_SYSPROTO_H_
 1039 struct sigprocmask_args {
 1040         int     how;
 1041         const sigset_t *set;
 1042         sigset_t *oset;
 1043 };
 1044 #endif
 1045 int
 1046 sigprocmask(td, uap)
 1047         register struct thread *td;
 1048         struct sigprocmask_args *uap;
 1049 {
 1050         sigset_t set, oset;
 1051         sigset_t *setp, *osetp;
 1052         int error;
 1053 
 1054         setp = (uap->set != NULL) ? &set : NULL;
 1055         osetp = (uap->oset != NULL) ? &oset : NULL;
 1056         if (setp) {
 1057                 error = copyin(uap->set, setp, sizeof(set));
 1058                 if (error)
 1059                         return (error);
 1060         }
 1061         error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
 1062         if (osetp && !error) {
 1063                 error = copyout(osetp, uap->oset, sizeof(oset));
 1064         }
 1065         return (error);
 1066 }
 1067 
 1068 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
 1069 #ifndef _SYS_SYSPROTO_H_
 1070 struct osigprocmask_args {
 1071         int     how;
 1072         osigset_t mask;
 1073 };
 1074 #endif
 1075 int
 1076 osigprocmask(td, uap)
 1077         register struct thread *td;
 1078         struct osigprocmask_args *uap;
 1079 {
 1080         sigset_t set, oset;
 1081         int error;
 1082 
 1083         OSIG2SIG(uap->mask, set);
 1084         error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
 1085         SIG2OSIG(oset, td->td_retval[0]);
 1086         return (error);
 1087 }
 1088 #endif /* COMPAT_43 */
 1089 
 1090 int
 1091 sigwait(struct thread *td, struct sigwait_args *uap)
 1092 {
 1093         ksiginfo_t ksi;
 1094         sigset_t set;
 1095         int error;
 1096 
 1097         error = copyin(uap->set, &set, sizeof(set));
 1098         if (error) {
 1099                 td->td_retval[0] = error;
 1100                 return (0);
 1101         }
 1102 
 1103         error = kern_sigtimedwait(td, set, &ksi, NULL);
 1104         if (error) {
 1105                 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
 1106                         error = ERESTART;
 1107                 if (error == ERESTART)
 1108                         return (error);
 1109                 td->td_retval[0] = error;
 1110                 return (0);
 1111         }
 1112 
 1113         error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
 1114         td->td_retval[0] = error;
 1115         return (0);
 1116 }
 1117 
 1118 int
 1119 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
 1120 {
 1121         struct timespec ts;
 1122         struct timespec *timeout;
 1123         sigset_t set;
 1124         ksiginfo_t ksi;
 1125         int error;
 1126 
 1127         if (uap->timeout) {
 1128                 error = copyin(uap->timeout, &ts, sizeof(ts));
 1129                 if (error)
 1130                         return (error);
 1131 
 1132                 timeout = &ts;
 1133         } else
 1134                 timeout = NULL;
 1135 
 1136         error = copyin(uap->set, &set, sizeof(set));
 1137         if (error)
 1138                 return (error);
 1139 
 1140         error = kern_sigtimedwait(td, set, &ksi, timeout);
 1141         if (error)
 1142                 return (error);
 1143 
 1144         if (uap->info)
 1145                 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
 1146 
 1147         if (error == 0)
 1148                 td->td_retval[0] = ksi.ksi_signo;
 1149         return (error);
 1150 }
 1151 
 1152 int
 1153 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
 1154 {
 1155         ksiginfo_t ksi;
 1156         sigset_t set;
 1157         int error;
 1158 
 1159         error = copyin(uap->set, &set, sizeof(set));
 1160         if (error)
 1161                 return (error);
 1162 
 1163         error = kern_sigtimedwait(td, set, &ksi, NULL);
 1164         if (error)
 1165                 return (error);
 1166 
 1167         if (uap->info)
 1168                 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
 1169         
 1170         if (error == 0)
 1171                 td->td_retval[0] = ksi.ksi_signo;
 1172         return (error);
 1173 }
 1174 
 1175 int
 1176 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
 1177         struct timespec *timeout)
 1178 {
 1179         struct sigacts *ps;
 1180         sigset_t savedmask;
 1181         struct proc *p;
 1182         int error, sig, hz, i, timevalid = 0;
 1183         struct timespec rts, ets, ts;
 1184         struct timeval tv;
 1185 
 1186         p = td->td_proc;
 1187         error = 0;
 1188         sig = 0;
 1189         ets.tv_sec = 0;
 1190         ets.tv_nsec = 0;
 1191         SIG_CANTMASK(waitset);
 1192 
 1193         PROC_LOCK(p);
 1194         ps = p->p_sigacts;
 1195         savedmask = td->td_sigmask;
 1196         if (timeout) {
 1197                 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
 1198                         timevalid = 1;
 1199                         getnanouptime(&rts);
 1200                         ets = rts;
 1201                         timespecadd(&ets, timeout);
 1202                 }
 1203         }
 1204 
 1205 restart:
 1206         for (i = 1; i <= _SIG_MAXSIG; ++i) {
 1207                 if (!SIGISMEMBER(waitset, i))
 1208                         continue;
 1209                 if (!SIGISMEMBER(td->td_sigqueue.sq_signals, i)) {
 1210                         if (SIGISMEMBER(p->p_sigqueue.sq_signals, i)) {
 1211                                 sigqueue_move(&p->p_sigqueue,
 1212                                         &td->td_sigqueue, i);
 1213                         } else
 1214                                 continue;
 1215                 }
 1216 
 1217                 SIGFILLSET(td->td_sigmask);
 1218                 SIG_CANTMASK(td->td_sigmask);
 1219                 SIGDELSET(td->td_sigmask, i);
 1220                 mtx_lock(&ps->ps_mtx);
 1221                 sig = cursig(td, SIG_STOP_ALLOWED);
 1222                 mtx_unlock(&ps->ps_mtx);
 1223                 if (sig)
 1224                         goto out;
 1225                 else {
 1226                         /*
 1227                          * Because cursig() may have stopped current thread,
 1228                          * after it is resumed, things may have already been 
 1229                          * changed, it should rescan any pending signals.
 1230                          */
 1231                         goto restart;
 1232                 }
 1233         }
 1234 
 1235         if (error)
 1236                 goto out;
 1237 
 1238         /*
 1239          * POSIX says this must be checked after looking for pending
 1240          * signals.
 1241          */
 1242         if (timeout) {
 1243                 if (!timevalid) {
 1244                         error = EINVAL;
 1245                         goto out;
 1246                 }
 1247                 getnanouptime(&rts);
 1248                 if (timespeccmp(&rts, &ets, >=)) {
 1249                         error = EAGAIN;
 1250                         goto out;
 1251                 }
 1252                 ts = ets;
 1253                 timespecsub(&ts, &rts);
 1254                 TIMESPEC_TO_TIMEVAL(&tv, &ts);
 1255                 hz = tvtohz(&tv);
 1256         } else
 1257                 hz = 0;
 1258 
 1259         td->td_sigmask = savedmask;
 1260         SIGSETNAND(td->td_sigmask, waitset);
 1261         signotify(td);
 1262         error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
 1263         if (timeout) {
 1264                 if (error == ERESTART) {
 1265                         /* timeout can not be restarted. */
 1266                         error = EINTR;
 1267                 } else if (error == EAGAIN) {
 1268                         /* will calculate timeout by ourself. */
 1269                         error = 0;
 1270                 }
 1271         }
 1272         goto restart;
 1273 
 1274 out:
 1275         td->td_sigmask = savedmask;
 1276         signotify(td);
 1277         if (sig) {
 1278                 ksiginfo_init(ksi);
 1279                 sigqueue_get(&td->td_sigqueue, sig, ksi);
 1280                 ksi->ksi_signo = sig;
 1281 
 1282                 SDT_PROBE(proc, kernel, , signal_clear, sig, ksi, 0, 0, 0);
 1283                 
 1284                 if (ksi->ksi_code == SI_TIMER)
 1285                         itimer_accept(p, ksi->ksi_timerid, ksi);
 1286                 error = 0;
 1287 
 1288 #ifdef KTRACE
 1289                 if (KTRPOINT(td, KTR_PSIG)) {
 1290                         sig_t action;
 1291 
 1292                         mtx_lock(&ps->ps_mtx);
 1293                         action = ps->ps_sigact[_SIG_IDX(sig)];
 1294                         mtx_unlock(&ps->ps_mtx);
 1295                         ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
 1296                 }
 1297 #endif
 1298                 if (sig == SIGKILL)
 1299                         sigexit(td, sig);
 1300         }
 1301         PROC_UNLOCK(p);
 1302         return (error);
 1303 }
 1304 
 1305 #ifndef _SYS_SYSPROTO_H_
 1306 struct sigpending_args {
 1307         sigset_t        *set;
 1308 };
 1309 #endif
 1310 int
 1311 sigpending(td, uap)
 1312         struct thread *td;
 1313         struct sigpending_args *uap;
 1314 {
 1315         struct proc *p = td->td_proc;
 1316         sigset_t pending;
 1317 
 1318         PROC_LOCK(p);
 1319         pending = p->p_sigqueue.sq_signals;
 1320         SIGSETOR(pending, td->td_sigqueue.sq_signals);
 1321         PROC_UNLOCK(p);
 1322         return (copyout(&pending, uap->set, sizeof(sigset_t)));
 1323 }
 1324 
 1325 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
 1326 #ifndef _SYS_SYSPROTO_H_
 1327 struct osigpending_args {
 1328         int     dummy;
 1329 };
 1330 #endif
 1331 int
 1332 osigpending(td, uap)
 1333         struct thread *td;
 1334         struct osigpending_args *uap;
 1335 {
 1336         struct proc *p = td->td_proc;
 1337         sigset_t pending;
 1338 
 1339         PROC_LOCK(p);
 1340         pending = p->p_sigqueue.sq_signals;
 1341         SIGSETOR(pending, td->td_sigqueue.sq_signals);
 1342         PROC_UNLOCK(p);
 1343         SIG2OSIG(pending, td->td_retval[0]);
 1344         return (0);
 1345 }
 1346 #endif /* COMPAT_43 */
 1347 
 1348 #if defined(COMPAT_43)
 1349 /*
 1350  * Generalized interface signal handler, 4.3-compatible.
 1351  */
 1352 #ifndef _SYS_SYSPROTO_H_
 1353 struct osigvec_args {
 1354         int     signum;
 1355         struct  sigvec *nsv;
 1356         struct  sigvec *osv;
 1357 };
 1358 #endif
 1359 /* ARGSUSED */
 1360 int
 1361 osigvec(td, uap)
 1362         struct thread *td;
 1363         register struct osigvec_args *uap;
 1364 {
 1365         struct sigvec vec;
 1366         struct sigaction nsa, osa;
 1367         register struct sigaction *nsap, *osap;
 1368         int error;
 1369 
 1370         if (uap->signum <= 0 || uap->signum >= ONSIG)
 1371                 return (EINVAL);
 1372         nsap = (uap->nsv != NULL) ? &nsa : NULL;
 1373         osap = (uap->osv != NULL) ? &osa : NULL;
 1374         if (nsap) {
 1375                 error = copyin(uap->nsv, &vec, sizeof(vec));
 1376                 if (error)
 1377                         return (error);
 1378                 nsap->sa_handler = vec.sv_handler;
 1379                 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
 1380                 nsap->sa_flags = vec.sv_flags;
 1381                 nsap->sa_flags ^= SA_RESTART;   /* opposite of SV_INTERRUPT */
 1382         }
 1383         error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
 1384         if (osap && !error) {
 1385                 vec.sv_handler = osap->sa_handler;
 1386                 SIG2OSIG(osap->sa_mask, vec.sv_mask);
 1387                 vec.sv_flags = osap->sa_flags;
 1388                 vec.sv_flags &= ~SA_NOCLDWAIT;
 1389                 vec.sv_flags ^= SA_RESTART;
 1390                 error = copyout(&vec, uap->osv, sizeof(vec));
 1391         }
 1392         return (error);
 1393 }
 1394 
 1395 #ifndef _SYS_SYSPROTO_H_
 1396 struct osigblock_args {
 1397         int     mask;
 1398 };
 1399 #endif
 1400 int
 1401 osigblock(td, uap)
 1402         register struct thread *td;
 1403         struct osigblock_args *uap;
 1404 {
 1405         sigset_t set, oset;
 1406 
 1407         OSIG2SIG(uap->mask, set);
 1408         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
 1409         SIG2OSIG(oset, td->td_retval[0]);
 1410         return (0);
 1411 }
 1412 
 1413 #ifndef _SYS_SYSPROTO_H_
 1414 struct osigsetmask_args {
 1415         int     mask;
 1416 };
 1417 #endif
 1418 int
 1419 osigsetmask(td, uap)
 1420         struct thread *td;
 1421         struct osigsetmask_args *uap;
 1422 {
 1423         sigset_t set, oset;
 1424 
 1425         OSIG2SIG(uap->mask, set);
 1426         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
 1427         SIG2OSIG(oset, td->td_retval[0]);
 1428         return (0);
 1429 }
 1430 #endif /* COMPAT_43 */
 1431 
 1432 /*
 1433  * Suspend calling thread until signal, providing mask to be set in the
 1434  * meantime. 
 1435  */
 1436 #ifndef _SYS_SYSPROTO_H_
 1437 struct sigsuspend_args {
 1438         const sigset_t *sigmask;
 1439 };
 1440 #endif
 1441 /* ARGSUSED */
 1442 int
 1443 sigsuspend(td, uap)
 1444         struct thread *td;
 1445         struct sigsuspend_args *uap;
 1446 {
 1447         sigset_t mask;
 1448         int error;
 1449 
 1450         error = copyin(uap->sigmask, &mask, sizeof(mask));
 1451         if (error)
 1452                 return (error);
 1453         return (kern_sigsuspend(td, mask));
 1454 }
 1455 
 1456 int
 1457 kern_sigsuspend(struct thread *td, sigset_t mask)
 1458 {
 1459         struct proc *p = td->td_proc;
 1460         int has_sig, sig;
 1461 
 1462         /*
 1463          * When returning from sigsuspend, we want
 1464          * the old mask to be restored after the
 1465          * signal handler has finished.  Thus, we
 1466          * save it here and mark the sigacts structure
 1467          * to indicate this.
 1468          */
 1469         PROC_LOCK(p);
 1470         kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
 1471             SIGPROCMASK_PROC_LOCKED);
 1472         td->td_pflags |= TDP_OLDMASK;
 1473 
 1474         /*
 1475          * Process signals now. Otherwise, we can get spurious wakeup
 1476          * due to signal entered process queue, but delivered to other
 1477          * thread. But sigsuspend should return only on signal
 1478          * delivery.
 1479          */
 1480         (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
 1481         for (has_sig = 0; !has_sig;) {
 1482                 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
 1483                         0) == 0)
 1484                         /* void */;
 1485                 thread_suspend_check(0);
 1486                 mtx_lock(&p->p_sigacts->ps_mtx);
 1487                 while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
 1488                         has_sig += postsig(sig);
 1489                 mtx_unlock(&p->p_sigacts->ps_mtx);
 1490         }
 1491         PROC_UNLOCK(p);
 1492         td->td_errno = EINTR;
 1493         td->td_pflags |= TDP_NERRNO;
 1494         return (EJUSTRETURN);
 1495 }
 1496 
 1497 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
 1498 /*
 1499  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
 1500  * convention: libc stub passes mask, not pointer, to save a copyin.
 1501  */
 1502 #ifndef _SYS_SYSPROTO_H_
 1503 struct osigsuspend_args {
 1504         osigset_t mask;
 1505 };
 1506 #endif
 1507 /* ARGSUSED */
 1508 int
 1509 osigsuspend(td, uap)
 1510         struct thread *td;
 1511         struct osigsuspend_args *uap;
 1512 {
 1513         sigset_t mask;
 1514 
 1515         OSIG2SIG(uap->mask, mask);
 1516         return (kern_sigsuspend(td, mask));
 1517 }
 1518 #endif /* COMPAT_43 */
 1519 
 1520 #if defined(COMPAT_43)
 1521 #ifndef _SYS_SYSPROTO_H_
 1522 struct osigstack_args {
 1523         struct  sigstack *nss;
 1524         struct  sigstack *oss;
 1525 };
 1526 #endif
 1527 /* ARGSUSED */
 1528 int
 1529 osigstack(td, uap)
 1530         struct thread *td;
 1531         register struct osigstack_args *uap;
 1532 {
 1533         struct sigstack nss, oss;
 1534         int error = 0;
 1535 
 1536         if (uap->nss != NULL) {
 1537                 error = copyin(uap->nss, &nss, sizeof(nss));
 1538                 if (error)
 1539                         return (error);
 1540         }
 1541         oss.ss_sp = td->td_sigstk.ss_sp;
 1542         oss.ss_onstack = sigonstack(cpu_getstack(td));
 1543         if (uap->nss != NULL) {
 1544                 td->td_sigstk.ss_sp = nss.ss_sp;
 1545                 td->td_sigstk.ss_size = 0;
 1546                 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
 1547                 td->td_pflags |= TDP_ALTSTACK;
 1548         }
 1549         if (uap->oss != NULL)
 1550                 error = copyout(&oss, uap->oss, sizeof(oss));
 1551 
 1552         return (error);
 1553 }
 1554 #endif /* COMPAT_43 */
 1555 
 1556 #ifndef _SYS_SYSPROTO_H_
 1557 struct sigaltstack_args {
 1558         stack_t *ss;
 1559         stack_t *oss;
 1560 };
 1561 #endif
 1562 /* ARGSUSED */
 1563 int
 1564 sigaltstack(td, uap)
 1565         struct thread *td;
 1566         register struct sigaltstack_args *uap;
 1567 {
 1568         stack_t ss, oss;
 1569         int error;
 1570 
 1571         if (uap->ss != NULL) {
 1572                 error = copyin(uap->ss, &ss, sizeof(ss));
 1573                 if (error)
 1574                         return (error);
 1575         }
 1576         error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
 1577             (uap->oss != NULL) ? &oss : NULL);
 1578         if (error)
 1579                 return (error);
 1580         if (uap->oss != NULL)
 1581                 error = copyout(&oss, uap->oss, sizeof(stack_t));
 1582         return (error);
 1583 }
 1584 
 1585 int
 1586 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
 1587 {
 1588         struct proc *p = td->td_proc;
 1589         int oonstack;
 1590 
 1591         oonstack = sigonstack(cpu_getstack(td));
 1592 
 1593         if (oss != NULL) {
 1594                 *oss = td->td_sigstk;
 1595                 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
 1596                     ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
 1597         }
 1598 
 1599         if (ss != NULL) {
 1600                 if (oonstack)
 1601                         return (EPERM);
 1602                 if ((ss->ss_flags & ~SS_DISABLE) != 0)
 1603                         return (EINVAL);
 1604                 if (!(ss->ss_flags & SS_DISABLE)) {
 1605                         if (ss->ss_size < p->p_sysent->sv_minsigstksz)
 1606                                 return (ENOMEM);
 1607 
 1608                         td->td_sigstk = *ss;
 1609                         td->td_pflags |= TDP_ALTSTACK;
 1610                 } else {
 1611                         td->td_pflags &= ~TDP_ALTSTACK;
 1612                 }
 1613         }
 1614         return (0);
 1615 }
 1616 
 1617 /*
 1618  * Common code for kill process group/broadcast kill.
 1619  * cp is calling process.
 1620  */
 1621 static int
 1622 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
 1623 {
 1624         struct proc *p;
 1625         struct pgrp *pgrp;
 1626         int err;
 1627         int ret;
 1628 
 1629         ret = ESRCH;
 1630         if (all) {
 1631                 /*
 1632                  * broadcast
 1633                  */
 1634                 sx_slock(&allproc_lock);
 1635                 FOREACH_PROC_IN_SYSTEM(p) {
 1636                         PROC_LOCK(p);
 1637                         if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
 1638                             p == td->td_proc || p->p_state == PRS_NEW) {
 1639                                 PROC_UNLOCK(p);
 1640                                 continue;
 1641                         }
 1642                         err = p_cansignal(td, p, sig);
 1643                         if (err == 0) {
 1644                                 if (sig)
 1645                                         pksignal(p, sig, ksi);
 1646                                 ret = err;
 1647                         }
 1648                         else if (ret == ESRCH)
 1649                                 ret = err;
 1650                         PROC_UNLOCK(p);
 1651                 }
 1652                 sx_sunlock(&allproc_lock);
 1653         } else {
 1654                 sx_slock(&proctree_lock);
 1655                 if (pgid == 0) {
 1656                         /*
 1657                          * zero pgid means send to my process group.
 1658                          */
 1659                         pgrp = td->td_proc->p_pgrp;
 1660                         PGRP_LOCK(pgrp);
 1661                 } else {
 1662                         pgrp = pgfind(pgid);
 1663                         if (pgrp == NULL) {
 1664                                 sx_sunlock(&proctree_lock);
 1665                                 return (ESRCH);
 1666                         }
 1667                 }
 1668                 sx_sunlock(&proctree_lock);
 1669                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
 1670                         PROC_LOCK(p);         
 1671                         if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
 1672                                 p->p_state == PRS_NEW ) {
 1673                                 PROC_UNLOCK(p);
 1674                                 continue;
 1675                         }
 1676                         err = p_cansignal(td, p, sig);
 1677                         if (err == 0) {
 1678                                 if (sig)
 1679                                         pksignal(p, sig, ksi);
 1680                                 ret = err;
 1681                         }
 1682                         else if (ret == ESRCH)
 1683                                 ret = err;
 1684                         PROC_UNLOCK(p);
 1685                 }
 1686                 PGRP_UNLOCK(pgrp);
 1687         }
 1688         return (ret);
 1689 }
 1690 
 1691 #ifndef _SYS_SYSPROTO_H_
 1692 struct kill_args {
 1693         int     pid;
 1694         int     signum;
 1695 };
 1696 #endif
 1697 /* ARGSUSED */
 1698 int
 1699 kill(struct thread *td, struct kill_args *uap)
 1700 {
 1701         ksiginfo_t ksi;
 1702         struct proc *p;
 1703         int error;
 1704 
 1705         AUDIT_ARG_SIGNUM(uap->signum);
 1706         AUDIT_ARG_PID(uap->pid);
 1707         if ((u_int)uap->signum > _SIG_MAXSIG)
 1708                 return (EINVAL);
 1709 
 1710         ksiginfo_init(&ksi);
 1711         ksi.ksi_signo = uap->signum;
 1712         ksi.ksi_code = SI_USER;
 1713         ksi.ksi_pid = td->td_proc->p_pid;
 1714         ksi.ksi_uid = td->td_ucred->cr_ruid;
 1715 
 1716         if (uap->pid > 0) {
 1717                 /* kill single process */
 1718                 if ((p = pfind(uap->pid)) == NULL) {
 1719                         if ((p = zpfind(uap->pid)) == NULL)
 1720                                 return (ESRCH);
 1721                 }
 1722                 AUDIT_ARG_PROCESS(p);
 1723                 error = p_cansignal(td, p, uap->signum);
 1724                 if (error == 0 && uap->signum)
 1725                         pksignal(p, uap->signum, &ksi);
 1726                 PROC_UNLOCK(p);
 1727                 return (error);
 1728         }
 1729         switch (uap->pid) {
 1730         case -1:                /* broadcast signal */
 1731                 return (killpg1(td, uap->signum, 0, 1, &ksi));
 1732         case 0:                 /* signal own process group */
 1733                 return (killpg1(td, uap->signum, 0, 0, &ksi));
 1734         default:                /* negative explicit process group */
 1735                 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
 1736         }
 1737         /* NOTREACHED */
 1738 }
 1739 
 1740 #if defined(COMPAT_43)
 1741 #ifndef _SYS_SYSPROTO_H_
 1742 struct okillpg_args {
 1743         int     pgid;
 1744         int     signum;
 1745 };
 1746 #endif
 1747 /* ARGSUSED */
 1748 int
 1749 okillpg(struct thread *td, struct okillpg_args *uap)
 1750 {
 1751         ksiginfo_t ksi;
 1752 
 1753         AUDIT_ARG_SIGNUM(uap->signum);
 1754         AUDIT_ARG_PID(uap->pgid);
 1755         if ((u_int)uap->signum > _SIG_MAXSIG)
 1756                 return (EINVAL);
 1757 
 1758         ksiginfo_init(&ksi);
 1759         ksi.ksi_signo = uap->signum;
 1760         ksi.ksi_code = SI_USER;
 1761         ksi.ksi_pid = td->td_proc->p_pid;
 1762         ksi.ksi_uid = td->td_ucred->cr_ruid;
 1763         return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
 1764 }
 1765 #endif /* COMPAT_43 */
 1766 
 1767 #ifndef _SYS_SYSPROTO_H_
 1768 struct sigqueue_args {
 1769         pid_t pid;
 1770         int signum;
 1771         /* union sigval */ void *value;
 1772 };
 1773 #endif
 1774 int
 1775 sigqueue(struct thread *td, struct sigqueue_args *uap)
 1776 {
 1777         ksiginfo_t ksi;
 1778         struct proc *p;
 1779         int error;
 1780 
 1781         if ((u_int)uap->signum > _SIG_MAXSIG)
 1782                 return (EINVAL);
 1783 
 1784         /*
 1785          * Specification says sigqueue can only send signal to
 1786          * single process.
 1787          */
 1788         if (uap->pid <= 0)
 1789                 return (EINVAL);
 1790 
 1791         if ((p = pfind(uap->pid)) == NULL) {
 1792                 if ((p = zpfind(uap->pid)) == NULL)
 1793                         return (ESRCH);
 1794         }
 1795         error = p_cansignal(td, p, uap->signum);
 1796         if (error == 0 && uap->signum != 0) {
 1797                 ksiginfo_init(&ksi);
 1798                 ksi.ksi_flags = KSI_SIGQ;
 1799                 ksi.ksi_signo = uap->signum;
 1800                 ksi.ksi_code = SI_QUEUE;
 1801                 ksi.ksi_pid = td->td_proc->p_pid;
 1802                 ksi.ksi_uid = td->td_ucred->cr_ruid;
 1803                 ksi.ksi_value.sival_ptr = uap->value;
 1804                 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
 1805         }
 1806         PROC_UNLOCK(p);
 1807         return (error);
 1808 }
 1809 
 1810 /*
 1811  * Send a signal to a process group.
 1812  */
 1813 void
 1814 gsignal(int pgid, int sig, ksiginfo_t *ksi)
 1815 {
 1816         struct pgrp *pgrp;
 1817 
 1818         if (pgid != 0) {
 1819                 sx_slock(&proctree_lock);
 1820                 pgrp = pgfind(pgid);
 1821                 sx_sunlock(&proctree_lock);
 1822                 if (pgrp != NULL) {
 1823                         pgsignal(pgrp, sig, 0, ksi);
 1824                         PGRP_UNLOCK(pgrp);
 1825                 }
 1826         }
 1827 }
 1828 
 1829 /*
 1830  * Send a signal to a process group.  If checktty is 1,
 1831  * limit to members which have a controlling terminal.
 1832  */
 1833 void
 1834 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
 1835 {
 1836         struct proc *p;
 1837 
 1838         if (pgrp) {
 1839                 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
 1840                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
 1841                         PROC_LOCK(p);
 1842                         if (p->p_state == PRS_NORMAL &&
 1843                             (checkctty == 0 || p->p_flag & P_CONTROLT))
 1844                                 pksignal(p, sig, ksi);
 1845                         PROC_UNLOCK(p);
 1846                 }
 1847         }
 1848 }
 1849 
 1850 /*
 1851  * Send a signal caused by a trap to the current thread.  If it will be
 1852  * caught immediately, deliver it with correct code.  Otherwise, post it
 1853  * normally.
 1854  */
 1855 void
 1856 trapsignal(struct thread *td, ksiginfo_t *ksi)
 1857 {
 1858         struct sigacts *ps;
 1859         sigset_t mask;
 1860         struct proc *p;
 1861         int sig;
 1862         int code;
 1863 
 1864         p = td->td_proc;
 1865         sig = ksi->ksi_signo;
 1866         code = ksi->ksi_code;
 1867         KASSERT(_SIG_VALID(sig), ("invalid signal"));
 1868 
 1869         PROC_LOCK(p);
 1870         ps = p->p_sigacts;
 1871         mtx_lock(&ps->ps_mtx);
 1872         if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
 1873             !SIGISMEMBER(td->td_sigmask, sig)) {
 1874                 td->td_ru.ru_nsignals++;
 1875 #ifdef KTRACE
 1876                 if (KTRPOINT(curthread, KTR_PSIG))
 1877                         ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
 1878                             &td->td_sigmask, code);
 1879 #endif
 1880                 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], 
 1881                                 ksi, &td->td_sigmask);
 1882                 mask = ps->ps_catchmask[_SIG_IDX(sig)];
 1883                 if (!SIGISMEMBER(ps->ps_signodefer, sig))
 1884                         SIGADDSET(mask, sig);
 1885                 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
 1886                     SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
 1887                 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
 1888                         /*
 1889                          * See kern_sigaction() for origin of this code.
 1890                          */
 1891                         SIGDELSET(ps->ps_sigcatch, sig);
 1892                         if (sig != SIGCONT &&
 1893                             sigprop(sig) & SA_IGNORE)
 1894                                 SIGADDSET(ps->ps_sigignore, sig);
 1895                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
 1896                 }
 1897                 mtx_unlock(&ps->ps_mtx);
 1898         } else {
 1899                 /*
 1900                  * Avoid a possible infinite loop if the thread
 1901                  * masking the signal or process is ignoring the
 1902                  * signal.
 1903                  */
 1904                 if (kern_forcesigexit &&
 1905                     (SIGISMEMBER(td->td_sigmask, sig) ||
 1906                      ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
 1907                         SIGDELSET(td->td_sigmask, sig);
 1908                         SIGDELSET(ps->ps_sigcatch, sig);
 1909                         SIGDELSET(ps->ps_sigignore, sig);
 1910                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
 1911                 }
 1912                 mtx_unlock(&ps->ps_mtx);
 1913                 p->p_code = code;       /* XXX for core dump/debugger */
 1914                 p->p_sig = sig;         /* XXX to verify code */
 1915                 tdsignal(p, td, sig, ksi);
 1916         }
 1917         PROC_UNLOCK(p);
 1918 }
 1919 
 1920 static struct thread *
 1921 sigtd(struct proc *p, int sig, int prop)
 1922 {
 1923         struct thread *td, *signal_td;
 1924 
 1925         PROC_LOCK_ASSERT(p, MA_OWNED);
 1926 
 1927         /*
 1928          * Check if current thread can handle the signal without
 1929          * switching context to another thread.
 1930          */
 1931         if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
 1932                 return (curthread);
 1933         signal_td = NULL;
 1934         FOREACH_THREAD_IN_PROC(p, td) {
 1935                 if (!SIGISMEMBER(td->td_sigmask, sig)) {
 1936                         signal_td = td;
 1937                         break;
 1938                 }
 1939         }
 1940         if (signal_td == NULL)
 1941                 signal_td = FIRST_THREAD_IN_PROC(p);
 1942         return (signal_td);
 1943 }
 1944 
 1945 /*
 1946  * Send the signal to the process.  If the signal has an action, the action
 1947  * is usually performed by the target process rather than the caller; we add
 1948  * the signal to the set of pending signals for the process.
 1949  *
 1950  * Exceptions:
 1951  *   o When a stop signal is sent to a sleeping process that takes the
 1952  *     default action, the process is stopped without awakening it.
 1953  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
 1954  *     regardless of the signal action (eg, blocked or ignored).
 1955  *
 1956  * Other ignored signals are discarded immediately.
 1957  * 
 1958  * NB: This function may be entered from the debugger via the "kill" DDB
 1959  * command.  There is little that can be done to mitigate the possibly messy
 1960  * side effects of this unwise possibility.
 1961  */
 1962 void
 1963 psignal(struct proc *p, int sig)
 1964 {
 1965         ksiginfo_t ksi;
 1966 
 1967         ksiginfo_init(&ksi);
 1968         ksi.ksi_signo = sig;
 1969         ksi.ksi_code = SI_KERNEL;
 1970         (void) tdsignal(p, NULL, sig, &ksi);
 1971 }
 1972 
 1973 void
 1974 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
 1975 {
 1976 
 1977         (void) tdsignal(p, NULL, sig, ksi);
 1978 }
 1979 
 1980 int
 1981 psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
 1982 {
 1983         struct thread *td = NULL;
 1984 
 1985         PROC_LOCK_ASSERT(p, MA_OWNED);
 1986 
 1987         KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));
 1988 
 1989         /*
 1990          * ksi_code and other fields should be set before
 1991          * calling this function.
 1992          */
 1993         ksi->ksi_signo = sigev->sigev_signo;
 1994         ksi->ksi_value = sigev->sigev_value;
 1995         if (sigev->sigev_notify == SIGEV_THREAD_ID) {
 1996                 td = thread_find(p, sigev->sigev_notify_thread_id);
 1997                 if (td == NULL)
 1998                         return (ESRCH);
 1999         }
 2000         return (tdsignal(p, td, ksi->ksi_signo, ksi));
 2001 }
 2002 
 2003 void
 2004 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
 2005 {
 2006         ksiginfo_t ksi_thunk;
 2007 
 2008         /*
 2009          * If ksi is NULL, use ksi_thunk and provide semantics
 2010          * identical to tdsignal() in 9.0+.
 2011          */
 2012         if (ksi == NULL) {
 2013                 ksi = &ksi_thunk;
 2014                 ksiginfo_init(ksi);
 2015                 ksi->ksi_signo = sig;
 2016                 ksi->ksi_code = SI_KERNEL;
 2017         }
 2018         (void) tdsignal(td->td_proc, td, sig, ksi);
 2019 }
 2020 
 2021 int
 2022 tdsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
 2023 {
 2024         sig_t action;
 2025         sigqueue_t *sigqueue;
 2026         int prop;
 2027         struct sigacts *ps;
 2028         int intrval;
 2029         int ret = 0;
 2030         int wakeup_swapper;
 2031 
 2032         PROC_LOCK_ASSERT(p, MA_OWNED);
 2033 
 2034         if (!_SIG_VALID(sig))
 2035                 panic("tdsignal(): invalid signal %d", sig);
 2036 
 2037         KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("tdsignal: ksi on queue"));
 2038 
 2039         /*
 2040          * IEEE Std 1003.1-2001: return success when killing a zombie.
 2041          */
 2042         if (p->p_state == PRS_ZOMBIE) {
 2043                 if (ksi && (ksi->ksi_flags & KSI_INS))
 2044                         ksiginfo_tryfree(ksi);
 2045                 return (ret);
 2046         }
 2047 
 2048         ps = p->p_sigacts;
 2049         KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
 2050         prop = sigprop(sig);
 2051 
 2052         if (td == NULL) {
 2053                 td = sigtd(p, sig, prop);
 2054                 sigqueue = &p->p_sigqueue;
 2055         } else {
 2056                 KASSERT(td->td_proc == p, ("invalid thread"));
 2057                 sigqueue = &td->td_sigqueue;
 2058         }
 2059 
 2060         SDT_PROBE(proc, kernel, , signal_send, td, p, sig, 0, 0 );
 2061 
 2062         /*
 2063          * If the signal is being ignored,
 2064          * then we forget about it immediately.
 2065          * (Note: we don't set SIGCONT in ps_sigignore,
 2066          * and if it is set to SIG_IGN,
 2067          * action will be SIG_DFL here.)
 2068          */
 2069         mtx_lock(&ps->ps_mtx);
 2070         if (SIGISMEMBER(ps->ps_sigignore, sig)) {
 2071                 SDT_PROBE(proc, kernel, , signal_discard, td, p, sig, 0, 0 );
 2072 
 2073                 mtx_unlock(&ps->ps_mtx);
 2074                 if (ksi && (ksi->ksi_flags & KSI_INS))
 2075                         ksiginfo_tryfree(ksi);
 2076                 return (ret);
 2077         }
 2078         if (SIGISMEMBER(td->td_sigmask, sig))
 2079                 action = SIG_HOLD;
 2080         else if (SIGISMEMBER(ps->ps_sigcatch, sig))
 2081                 action = SIG_CATCH;
 2082         else
 2083                 action = SIG_DFL;
 2084         if (SIGISMEMBER(ps->ps_sigintr, sig))
 2085                 intrval = EINTR;
 2086         else
 2087                 intrval = ERESTART;
 2088         mtx_unlock(&ps->ps_mtx);
 2089 
 2090         if (prop & SA_CONT)
 2091                 sigqueue_delete_stopmask_proc(p);
 2092         else if (prop & SA_STOP) {
 2093                 /*
 2094                  * If sending a tty stop signal to a member of an orphaned
 2095                  * process group, discard the signal here if the action
 2096                  * is default; don't stop the process below if sleeping,
 2097                  * and don't clear any pending SIGCONT.
 2098                  */
 2099                 if ((prop & SA_TTYSTOP) &&
 2100                     (p->p_pgrp->pg_jobc == 0) &&
 2101                     (action == SIG_DFL)) {
 2102                         if (ksi && (ksi->ksi_flags & KSI_INS))
 2103                                 ksiginfo_tryfree(ksi);
 2104                         return (ret);
 2105                 }
 2106                 sigqueue_delete_proc(p, SIGCONT);
 2107                 if (p->p_flag & P_CONTINUED) {
 2108                         p->p_flag &= ~P_CONTINUED;
 2109                         PROC_LOCK(p->p_pptr);
 2110                         sigqueue_take(p->p_ksi);
 2111                         PROC_UNLOCK(p->p_pptr);
 2112                 }
 2113         }
 2114 
 2115         ret = sigqueue_add(sigqueue, sig, ksi);
 2116         if (ret != 0)
 2117                 return (ret);
 2118         signotify(td);
 2119         /*
 2120          * Defer further processing for signals which are held,
 2121          * except that stopped processes must be continued by SIGCONT.
 2122          */
 2123         if (action == SIG_HOLD &&
 2124             !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
 2125                 return (ret);
 2126         /*
 2127          * SIGKILL: Remove procfs STOPEVENTs.
 2128          */
 2129         if (sig == SIGKILL) {
 2130                 /* from procfs_ioctl.c: PIOCBIC */
 2131                 p->p_stops = 0;
 2132                 /* from procfs_ioctl.c: PIOCCONT */
 2133                 p->p_step = 0;
 2134                 wakeup(&p->p_step);
 2135         }
 2136         /*
 2137          * Some signals have a process-wide effect and a per-thread
 2138          * component.  Most processing occurs when the process next
 2139          * tries to cross the user boundary, however there are some
 2140          * times when processing needs to be done immediatly, such as
 2141          * waking up threads so that they can cross the user boundary.
 2142          * We try do the per-process part here.
 2143          */
 2144         if (P_SHOULDSTOP(p)) {
 2145                 KASSERT(!(p->p_flag & P_WEXIT),
 2146                     ("signal to stopped but exiting process"));
 2147                 if (sig == SIGKILL) {
 2148                         /*
 2149                          * If traced process is already stopped,
 2150                          * then no further action is necessary.
 2151                          */
 2152                         if (p->p_flag & P_TRACED)
 2153                                 goto out;
 2154                         /*
 2155                          * SIGKILL sets process running.
 2156                          * It will die elsewhere.
 2157                          * All threads must be restarted.
 2158                          */
 2159                         p->p_flag &= ~P_STOPPED_SIG;
 2160                         goto runfast;
 2161                 }
 2162 
 2163                 if (prop & SA_CONT) {
 2164                         /*
 2165                          * If traced process is already stopped,
 2166                          * then no further action is necessary.
 2167                          */
 2168                         if (p->p_flag & P_TRACED)
 2169                                 goto out;
 2170                         /*
 2171                          * If SIGCONT is default (or ignored), we continue the
 2172                          * process but don't leave the signal in sigqueue as
 2173                          * it has no further action.  If SIGCONT is held, we
 2174                          * continue the process and leave the signal in
 2175                          * sigqueue.  If the process catches SIGCONT, let it
 2176                          * handle the signal itself.  If it isn't waiting on
 2177                          * an event, it goes back to run state.
 2178                          * Otherwise, process goes back to sleep state.
 2179                          */
 2180                         p->p_flag &= ~P_STOPPED_SIG;
 2181                         PROC_SLOCK(p);
 2182                         if (p->p_numthreads == p->p_suspcount) {
 2183                                 PROC_SUNLOCK(p);
 2184                                 p->p_flag |= P_CONTINUED;
 2185                                 p->p_xstat = SIGCONT;
 2186                                 PROC_LOCK(p->p_pptr);
 2187                                 childproc_continued(p);
 2188                                 PROC_UNLOCK(p->p_pptr);
 2189                                 PROC_SLOCK(p);
 2190                         }
 2191                         if (action == SIG_DFL) {
 2192                                 thread_unsuspend(p);
 2193                                 PROC_SUNLOCK(p);
 2194                                 sigqueue_delete(sigqueue, sig);
 2195                                 goto out;
 2196                         }
 2197                         if (action == SIG_CATCH) {
 2198                                 /*
 2199                                  * The process wants to catch it so it needs
 2200                                  * to run at least one thread, but which one?
 2201                                  */
 2202                                 PROC_SUNLOCK(p);
 2203                                 goto runfast;
 2204                         }
 2205                         /*
 2206                          * The signal is not ignored or caught.
 2207                          */
 2208                         thread_unsuspend(p);
 2209                         PROC_SUNLOCK(p);
 2210                         goto out;
 2211                 }
 2212 
 2213                 if (prop & SA_STOP) {
 2214                         /*
 2215                          * If traced process is already stopped,
 2216                          * then no further action is necessary.
 2217                          */
 2218                         if (p->p_flag & P_TRACED)
 2219                                 goto out;
 2220                         /*
 2221                          * Already stopped, don't need to stop again
 2222                          * (If we did the shell could get confused).
 2223                          * Just make sure the signal STOP bit set.
 2224                          */
 2225                         p->p_flag |= P_STOPPED_SIG;
 2226                         sigqueue_delete(sigqueue, sig);
 2227                         goto out;
 2228                 }
 2229 
 2230                 /*
 2231                  * All other kinds of signals:
 2232                  * If a thread is sleeping interruptibly, simulate a
 2233                  * wakeup so that when it is continued it will be made
 2234                  * runnable and can look at the signal.  However, don't make
 2235                  * the PROCESS runnable, leave it stopped.
 2236                  * It may run a bit until it hits a thread_suspend_check().
 2237                  */
 2238                 wakeup_swapper = 0;
 2239                 PROC_SLOCK(p);
 2240                 thread_lock(td);
 2241                 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
 2242                         wakeup_swapper = sleepq_abort(td, intrval);
 2243                 thread_unlock(td);
 2244                 PROC_SUNLOCK(p);
 2245                 if (wakeup_swapper)
 2246                         kick_proc0();
 2247                 goto out;
 2248                 /*
 2249                  * Mutexes are short lived. Threads waiting on them will
 2250                  * hit thread_suspend_check() soon.
 2251                  */
 2252         } else if (p->p_state == PRS_NORMAL) {
 2253                 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
 2254                         tdsigwakeup(td, sig, action, intrval);
 2255                         goto out;
 2256                 }
 2257 
 2258                 MPASS(action == SIG_DFL);
 2259 
 2260                 if (prop & SA_STOP) {
 2261                         if (p->p_flag & (P_PPWAIT|P_WEXIT))
 2262                                 goto out;
 2263                         p->p_flag |= P_STOPPED_SIG;
 2264                         p->p_xstat = sig;
 2265                         PROC_SLOCK(p);
 2266                         sig_suspend_threads(td, p, 1);
 2267                         if (p->p_numthreads == p->p_suspcount) {
 2268                                 /*
 2269                                  * only thread sending signal to another
 2270                                  * process can reach here, if thread is sending
 2271                                  * signal to its process, because thread does
 2272                                  * not suspend itself here, p_numthreads
 2273                                  * should never be equal to p_suspcount.
 2274                                  */
 2275                                 thread_stopped(p);
 2276                                 PROC_SUNLOCK(p);
 2277                                 sigqueue_delete_proc(p, p->p_xstat);
 2278                         } else
 2279                                 PROC_SUNLOCK(p);
 2280                         goto out;
 2281                 }
 2282         } else {
 2283                 /* Not in "NORMAL" state. discard the signal. */
 2284                 sigqueue_delete(sigqueue, sig);
 2285                 goto out;
 2286         }
 2287 
 2288         /*
 2289          * The process is not stopped so we need to apply the signal to all the
 2290          * running threads.
 2291          */
 2292 runfast:
 2293         tdsigwakeup(td, sig, action, intrval);
 2294         PROC_SLOCK(p);
 2295         thread_unsuspend(p);
 2296         PROC_SUNLOCK(p);
 2297 out:
 2298         /* If we jump here, proc slock should not be owned. */
 2299         PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
 2300         return (ret);
 2301 }
 2302 
 2303 /*
 2304  * The force of a signal has been directed against a single
 2305  * thread.  We need to see what we can do about knocking it
 2306  * out of any sleep it may be in etc.
 2307  */
 2308 static void
 2309 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
 2310 {
 2311         struct proc *p = td->td_proc;
 2312         register int prop;
 2313         int wakeup_swapper;
 2314 
 2315         wakeup_swapper = 0;
 2316         PROC_LOCK_ASSERT(p, MA_OWNED);
 2317         prop = sigprop(sig);
 2318 
 2319         PROC_SLOCK(p);
 2320         thread_lock(td);
 2321         /*
 2322          * Bring the priority of a thread up if we want it to get
 2323          * killed in this lifetime.
 2324          */
 2325         if (action == SIG_DFL && (prop & SA_KILL) && td->td_priority > PUSER)
 2326                 sched_prio(td, PUSER);
 2327         if (TD_ON_SLEEPQ(td)) {
 2328                 /*
 2329                  * If thread is sleeping uninterruptibly
 2330                  * we can't interrupt the sleep... the signal will
 2331                  * be noticed when the process returns through
 2332                  * trap() or syscall().
 2333                  */
 2334                 if ((td->td_flags & TDF_SINTR) == 0)
 2335                         goto out;
 2336                 /*
 2337                  * If SIGCONT is default (or ignored) and process is
 2338                  * asleep, we are finished; the process should not
 2339                  * be awakened.
 2340                  */
 2341                 if ((prop & SA_CONT) && action == SIG_DFL) {
 2342                         thread_unlock(td);
 2343                         PROC_SUNLOCK(p);
 2344                         sigqueue_delete(&p->p_sigqueue, sig);
 2345                         /*
 2346                          * It may be on either list in this state.
 2347                          * Remove from both for now.
 2348                          */
 2349                         sigqueue_delete(&td->td_sigqueue, sig);
 2350                         return;
 2351                 }
 2352 
 2353                 /*
 2354                  * Give low priority threads a better chance to run.
 2355                  */
 2356                 if (td->td_priority > PUSER)
 2357                         sched_prio(td, PUSER);
 2358 
 2359                 wakeup_swapper = sleepq_abort(td, intrval);
 2360         } else {
 2361                 /*
 2362                  * Other states do nothing with the signal immediately,
 2363                  * other than kicking ourselves if we are running.
 2364                  * It will either never be noticed, or noticed very soon.
 2365                  */
 2366 #ifdef SMP
 2367                 if (TD_IS_RUNNING(td) && td != curthread)
 2368                         forward_signal(td);
 2369 #endif
 2370         }
 2371 out:
 2372         PROC_SUNLOCK(p);
 2373         thread_unlock(td);
 2374         if (wakeup_swapper)
 2375                 kick_proc0();
 2376 }
 2377 
 2378 static void
 2379 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
 2380 {
 2381         struct thread *td2;
 2382         int wakeup_swapper;
 2383 
 2384         PROC_LOCK_ASSERT(p, MA_OWNED);
 2385         PROC_SLOCK_ASSERT(p, MA_OWNED);
 2386 
 2387         wakeup_swapper = 0;
 2388         FOREACH_THREAD_IN_PROC(p, td2) {
 2389                 thread_lock(td2);
 2390                 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
 2391                 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
 2392                     (td2->td_flags & TDF_SINTR)) {
 2393                         if (td2->td_flags & TDF_SBDRY) {
 2394                                 if (TD_IS_SUSPENDED(td2))
 2395                                         wakeup_swapper |=
 2396                                             thread_unsuspend_one(td2);
 2397                                 if (TD_ON_SLEEPQ(td2))
 2398                                         wakeup_swapper |=
 2399                                             sleepq_abort(td2, ERESTART);
 2400                         } else if (!TD_IS_SUSPENDED(td2)) {
 2401                                 thread_suspend_one(td2);
 2402                         }
 2403                 } else if (!TD_IS_SUSPENDED(td2)) {
 2404                         if (sending || td != td2)
 2405                                 td2->td_flags |= TDF_ASTPENDING;
 2406 #ifdef SMP
 2407                         if (TD_IS_RUNNING(td2) && td2 != td)
 2408                                 forward_signal(td2);
 2409 #endif
 2410                 }
 2411                 thread_unlock(td2);
 2412         }
 2413         if (wakeup_swapper)
 2414                 kick_proc0();
 2415 }
 2416 
 2417 int
 2418 ptracestop(struct thread *td, int sig)
 2419 {
 2420         struct proc *p = td->td_proc;
 2421 
 2422         PROC_LOCK_ASSERT(p, MA_OWNED);
 2423         KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
 2424         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
 2425             &p->p_mtx.lock_object, "Stopping for traced signal");
 2426 
 2427         td->td_dbgflags |= TDB_XSIG;
 2428         td->td_xsig = sig;
 2429         PROC_SLOCK(p);
 2430         while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
 2431                 if (p->p_flag & P_SINGLE_EXIT) {
 2432                         td->td_dbgflags &= ~TDB_XSIG;
 2433                         PROC_SUNLOCK(p);
 2434                         return (sig);
 2435                 }
 2436                 /*
 2437                  * Just make wait() to work, the last stopped thread
 2438                  * will win.
 2439                  */
 2440                 p->p_xstat = sig;
 2441                 p->p_xthread = td;
 2442                 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
 2443                 sig_suspend_threads(td, p, 0);
 2444                 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
 2445                         td->td_dbgflags &= ~TDB_STOPATFORK;
 2446                         cv_broadcast(&p->p_dbgwait);
 2447                 }
 2448 stopme:
 2449                 thread_suspend_switch(td);
 2450                 if (!(p->p_flag & P_TRACED)) {
 2451                         break;
 2452                 }
 2453                 if (td->td_dbgflags & TDB_SUSPEND) {
 2454                         if (p->p_flag & P_SINGLE_EXIT)
 2455                                 break;
 2456                         goto stopme;
 2457                 }
 2458         }
 2459         PROC_SUNLOCK(p);
 2460         return (td->td_xsig);
 2461 }
 2462 
 2463 static void
 2464 reschedule_signals(struct proc *p, sigset_t block, int flags)
 2465 {
 2466         struct sigacts *ps;
 2467         struct thread *td;
 2468         int i;
 2469 
 2470         PROC_LOCK_ASSERT(p, MA_OWNED);
 2471 
 2472         ps = p->p_sigacts;
 2473         for (i = 1; !SIGISEMPTY(block); i++) {
 2474                 if (!SIGISMEMBER(block, i))
 2475                         continue;
 2476                 SIGDELSET(block, i);
 2477                 if (!SIGISMEMBER(p->p_siglist, i))
 2478                         continue;
 2479 
 2480                 td = sigtd(p, i, 0);
 2481                 signotify(td);
 2482                 if (!(flags & SIGPROCMASK_PS_LOCKED))
 2483                         mtx_lock(&ps->ps_mtx);
 2484                 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, i))
 2485                         tdsigwakeup(td, i, SIG_CATCH,
 2486                             (SIGISMEMBER(ps->ps_sigintr, i) ? EINTR :
 2487                              ERESTART));
 2488                 if (!(flags & SIGPROCMASK_PS_LOCKED))
 2489                         mtx_unlock(&ps->ps_mtx);
 2490         }
 2491 }
 2492 
 2493 void
 2494 tdsigcleanup(struct thread *td)
 2495 {
 2496         struct proc *p;
 2497         sigset_t unblocked;
 2498 
 2499         p = td->td_proc;
 2500         PROC_LOCK_ASSERT(p, MA_OWNED);
 2501 
 2502         sigqueue_flush(&td->td_sigqueue);
 2503         if (p->p_numthreads == 1)
 2504                 return;
 2505 
 2506         /*
 2507          * Since we cannot handle signals, notify signal post code
 2508          * about this by filling the sigmask.
 2509          *
 2510          * Also, if needed, wake up thread(s) that do not block the
 2511          * same signals as the exiting thread, since the thread might
 2512          * have been selected for delivery and woken up.
 2513          */
 2514         SIGFILLSET(unblocked);
 2515         SIGSETNAND(unblocked, td->td_sigmask);
 2516         SIGFILLSET(td->td_sigmask);
 2517         reschedule_signals(p, unblocked, 0);
 2518 
 2519 }
 2520 
 2521 /*
 2522  * If the current process has received a signal (should be caught or cause
 2523  * termination, should interrupt current syscall), return the signal number.
 2524  * Stop signals with default action are processed immediately, then cleared;
 2525  * they aren't returned.  This is checked after each entry to the system for
 2526  * a syscall or trap (though this can usually be done without calling issignal
 2527  * by checking the pending signal masks in cursig.) The normal call
 2528  * sequence is
 2529  *
 2530  *      while (sig = cursig(curthread))
 2531  *              postsig(sig);
 2532  */
 2533 static int
 2534 issignal(struct thread *td, int stop_allowed)
 2535 {
 2536         struct proc *p;
 2537         struct sigacts *ps;
 2538         struct sigqueue *queue;
 2539         sigset_t sigpending;
 2540         int sig, prop, newsig;
 2541 
 2542         p = td->td_proc;
 2543         ps = p->p_sigacts;
 2544         mtx_assert(&ps->ps_mtx, MA_OWNED);
 2545         PROC_LOCK_ASSERT(p, MA_OWNED);
 2546         for (;;) {
 2547                 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
 2548 
 2549                 sigpending = td->td_sigqueue.sq_signals;
 2550                 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
 2551                 SIGSETNAND(sigpending, td->td_sigmask);
 2552 
 2553                 if (p->p_flag & P_PPWAIT)
 2554                         SIG_STOPSIGMASK(sigpending);
 2555                 if (SIGISEMPTY(sigpending))     /* no signal to send */
 2556                         return (0);
 2557                 sig = sig_ffs(&sigpending);
 2558 
 2559                 if (p->p_stops & S_SIG) {
 2560                         mtx_unlock(&ps->ps_mtx);
 2561                         stopevent(p, S_SIG, sig);
 2562                         mtx_lock(&ps->ps_mtx);
 2563                 }
 2564 
 2565                 /*
 2566                  * We should see pending but ignored signals
 2567                  * only if P_TRACED was on when they were posted.
 2568                  */
 2569                 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
 2570                         sigqueue_delete(&td->td_sigqueue, sig);
 2571                         sigqueue_delete(&p->p_sigqueue, sig);
 2572                         continue;
 2573                 }
 2574                 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
 2575                         /*
 2576                          * If traced, always stop.
 2577                          * Remove old signal from queue before the stop.
 2578                          * XXX shrug off debugger, it causes siginfo to
 2579                          * be thrown away.
 2580                          */
 2581                         queue = &td->td_sigqueue;
 2582                         td->td_dbgksi.ksi_signo = 0;
 2583                         if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
 2584                                 queue = &p->p_sigqueue;
 2585                                 sigqueue_get(queue, sig, &td->td_dbgksi);
 2586                         }
 2587 
 2588                         mtx_unlock(&ps->ps_mtx);
 2589                         newsig = ptracestop(td, sig);
 2590                         mtx_lock(&ps->ps_mtx);
 2591 
 2592                         if (sig != newsig) {
 2593 
 2594                                 /*
 2595                                  * If parent wants us to take the signal,
 2596                                  * then it will leave it in p->p_xstat;
 2597                                  * otherwise we just look for signals again.
 2598                                 */
 2599                                 if (newsig == 0)
 2600                                         continue;
 2601                                 sig = newsig;
 2602 
 2603                                 /*
 2604                                  * Put the new signal into td_sigqueue. If the
 2605                                  * signal is being masked, look for other signals.
 2606                                  */
 2607                                 sigqueue_add(queue, sig, NULL);
 2608                                 if (SIGISMEMBER(td->td_sigmask, sig))
 2609                                         continue;
 2610                                 signotify(td);
 2611                         } else {
 2612                                 if (td->td_dbgksi.ksi_signo != 0) {
 2613                                         td->td_dbgksi.ksi_flags |= KSI_HEAD;
 2614                                         if (sigqueue_add(&td->td_sigqueue, sig,
 2615                                             &td->td_dbgksi) != 0)
 2616                                                 td->td_dbgksi.ksi_signo = 0;
 2617                                 }
 2618                                 if (td->td_dbgksi.ksi_signo == 0)
 2619                                         sigqueue_add(&td->td_sigqueue, sig,
 2620                                             NULL);
 2621                         }
 2622 
 2623                         /*
 2624                          * If the traced bit got turned off, go back up
 2625                          * to the top to rescan signals.  This ensures
 2626                          * that p_sig* and p_sigact are consistent.
 2627                          */
 2628                         if ((p->p_flag & P_TRACED) == 0)
 2629                                 continue;
 2630                 }
 2631 
 2632                 prop = sigprop(sig);
 2633 
 2634                 /*
 2635                  * Decide whether the signal should be returned.
 2636                  * Return the signal's number, or fall through
 2637                  * to clear it from the pending mask.
 2638                  */
 2639                 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
 2640 
 2641                 case (intptr_t)SIG_DFL:
 2642                         /*
 2643                          * Don't take default actions on system processes.
 2644                          */
 2645                         if (p->p_pid <= 1) {
 2646 #ifdef DIAGNOSTIC
 2647                                 /*
 2648                                  * Are you sure you want to ignore SIGSEGV
 2649                                  * in init? XXX
 2650                                  */
 2651                                 printf("Process (pid %lu) got signal %d\n",
 2652                                         (u_long)p->p_pid, sig);
 2653 #endif
 2654                                 break;          /* == ignore */
 2655                         }
 2656                         /*
 2657                          * If there is a pending stop signal to process
 2658                          * with default action, stop here,
 2659                          * then clear the signal.  However,
 2660                          * if process is member of an orphaned
 2661                          * process group, ignore tty stop signals.
 2662                          */
 2663                         if (prop & SA_STOP) {
 2664                                 if (p->p_flag & (P_TRACED|P_WEXIT) ||
 2665                                     (p->p_pgrp->pg_jobc == 0 &&
 2666                                      prop & SA_TTYSTOP))
 2667                                         break;  /* == ignore */
 2668 
 2669                                 /* Ignore, but do not drop the stop signal. */
 2670                                 if (stop_allowed != SIG_STOP_ALLOWED)
 2671                                         return (sig);
 2672                                 mtx_unlock(&ps->ps_mtx);
 2673                                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
 2674                                     &p->p_mtx.lock_object, "Catching SIGSTOP");
 2675                                 p->p_flag |= P_STOPPED_SIG;
 2676                                 p->p_xstat = sig;
 2677                                 PROC_SLOCK(p);
 2678                                 sig_suspend_threads(td, p, 0);
 2679                                 thread_suspend_switch(td);
 2680                                 PROC_SUNLOCK(p);
 2681                                 mtx_lock(&ps->ps_mtx);
 2682                                 break;
 2683                         } else if (prop & SA_IGNORE) {
 2684                                 /*
 2685                                  * Except for SIGCONT, shouldn't get here.
 2686                                  * Default action is to ignore; drop it.
 2687                                  */
 2688                                 break;          /* == ignore */
 2689                         } else
 2690                                 return (sig);
 2691                         /*NOTREACHED*/
 2692 
 2693                 case (intptr_t)SIG_IGN:
 2694                         /*
 2695                          * Masking above should prevent us ever trying
 2696                          * to take action on an ignored signal other
 2697                          * than SIGCONT, unless process is traced.
 2698                          */
 2699                         if ((prop & SA_CONT) == 0 &&
 2700                             (p->p_flag & P_TRACED) == 0)
 2701                                 printf("issignal\n");
 2702                         break;          /* == ignore */
 2703 
 2704                 default:
 2705                         /*
 2706                          * This signal has an action, let
 2707                          * postsig() process it.
 2708                          */
 2709                         return (sig);
 2710                 }
 2711                 sigqueue_delete(&td->td_sigqueue, sig);         /* take the signal! */
 2712                 sigqueue_delete(&p->p_sigqueue, sig);
 2713         }
 2714         /* NOTREACHED */
 2715 }
 2716 
 2717 void
 2718 thread_stopped(struct proc *p)
 2719 {
 2720         int n;
 2721 
 2722         PROC_LOCK_ASSERT(p, MA_OWNED);
 2723         PROC_SLOCK_ASSERT(p, MA_OWNED);
 2724         n = p->p_suspcount;
 2725         if (p == curproc)
 2726                 n++;
 2727         if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
 2728                 PROC_SUNLOCK(p);
 2729                 p->p_flag &= ~P_WAITED;
 2730                 PROC_LOCK(p->p_pptr);
 2731                 childproc_stopped(p, (p->p_flag & P_TRACED) ?
 2732                         CLD_TRAPPED : CLD_STOPPED);
 2733                 PROC_UNLOCK(p->p_pptr);
 2734                 PROC_SLOCK(p);
 2735         }
 2736 }
 2737  
 2738 /*
 2739  * Take the action for the specified signal
 2740  * from the current set of pending signals.
 2741  */
 2742 int
 2743 postsig(sig)
 2744         register int sig;
 2745 {
 2746         struct thread *td = curthread;
 2747         register struct proc *p = td->td_proc;
 2748         struct sigacts *ps;
 2749         sig_t action;
 2750         ksiginfo_t ksi;
 2751         sigset_t returnmask, mask;
 2752 
 2753         KASSERT(sig != 0, ("postsig"));
 2754 
 2755         PROC_LOCK_ASSERT(p, MA_OWNED);
 2756         ps = p->p_sigacts;
 2757         mtx_assert(&ps->ps_mtx, MA_OWNED);
 2758         ksiginfo_init(&ksi);
 2759         if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
 2760             sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
 2761                 return (0);
 2762         ksi.ksi_signo = sig;
 2763         if (ksi.ksi_code == SI_TIMER)
 2764                 itimer_accept(p, ksi.ksi_timerid, &ksi);
 2765         action = ps->ps_sigact[_SIG_IDX(sig)];
 2766 #ifdef KTRACE
 2767         if (KTRPOINT(td, KTR_PSIG))
 2768                 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
 2769                     &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
 2770 #endif
 2771         if (p->p_stops & S_SIG) {
 2772                 mtx_unlock(&ps->ps_mtx);
 2773                 stopevent(p, S_SIG, sig);
 2774                 mtx_lock(&ps->ps_mtx);
 2775         }
 2776 
 2777         if (action == SIG_DFL) {
 2778                 /*
 2779                  * Default action, where the default is to kill
 2780                  * the process.  (Other cases were ignored above.)
 2781                  */
 2782                 mtx_unlock(&ps->ps_mtx);
 2783                 sigexit(td, sig);
 2784                 /* NOTREACHED */
 2785         } else {
 2786                 /*
 2787                  * If we get here, the signal must be caught.
 2788                  */
 2789                 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
 2790                     ("postsig action"));
 2791                 /*
 2792                  * Set the new mask value and also defer further
 2793                  * occurrences of this signal.
 2794                  *
 2795                  * Special case: user has done a sigsuspend.  Here the
 2796                  * current mask is not of interest, but rather the
 2797                  * mask from before the sigsuspend is what we want
 2798                  * restored after the signal processing is completed.
 2799                  */
 2800                 if (td->td_pflags & TDP_OLDMASK) {
 2801                         returnmask = td->td_oldsigmask;
 2802                         td->td_pflags &= ~TDP_OLDMASK;
 2803                 } else
 2804                         returnmask = td->td_sigmask;
 2805 
 2806                 mask = ps->ps_catchmask[_SIG_IDX(sig)];
 2807                 if (!SIGISMEMBER(ps->ps_signodefer, sig))
 2808                         SIGADDSET(mask, sig);
 2809                 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
 2810                     SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
 2811 
 2812                 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
 2813                         /*
 2814                          * See kern_sigaction() for origin of this code.
 2815                          */
 2816                         SIGDELSET(ps->ps_sigcatch, sig);
 2817                         if (sig != SIGCONT &&
 2818                             sigprop(sig) & SA_IGNORE)
 2819                                 SIGADDSET(ps->ps_sigignore, sig);
 2820                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
 2821                 }
 2822                 td->td_ru.ru_nsignals++;
 2823                 if (p->p_sig == sig) {
 2824                         p->p_code = 0;
 2825                         p->p_sig = 0;
 2826                 }
 2827                 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
 2828         }
 2829         return (1);
 2830 }
 2831 
 2832 /*
 2833  * Kill the current process for stated reason.
 2834  */
 2835 void
 2836 killproc(p, why)
 2837         struct proc *p;
 2838         char *why;
 2839 {
 2840 
 2841         PROC_LOCK_ASSERT(p, MA_OWNED);
 2842         CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
 2843                 p, p->p_pid, p->p_comm);
 2844         log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
 2845                 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
 2846         p->p_flag |= P_WKILLED;
 2847         psignal(p, SIGKILL);
 2848 }
 2849 
 2850 /*
 2851  * Force the current process to exit with the specified signal, dumping core
 2852  * if appropriate.  We bypass the normal tests for masked and caught signals,
 2853  * allowing unrecoverable failures to terminate the process without changing
 2854  * signal state.  Mark the accounting record with the signal termination.
 2855  * If dumping core, save the signal number for the debugger.  Calls exit and
 2856  * does not return.
 2857  */
 2858 void
 2859 sigexit(td, sig)
 2860         struct thread *td;
 2861         int sig;
 2862 {
 2863         struct proc *p = td->td_proc;
 2864 
 2865         PROC_LOCK_ASSERT(p, MA_OWNED);
 2866         p->p_acflag |= AXSIG;
 2867         /*
 2868          * We must be single-threading to generate a core dump.  This
 2869          * ensures that the registers in the core file are up-to-date.
 2870          * Also, the ELF dump handler assumes that the thread list doesn't
 2871          * change out from under it.
 2872          *
 2873          * XXX If another thread attempts to single-thread before us
 2874          *     (e.g. via fork()), we won't get a dump at all.
 2875          */
 2876         if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
 2877                 p->p_sig = sig;
 2878                 /*
 2879                  * Log signals which would cause core dumps
 2880                  * (Log as LOG_INFO to appease those who don't want
 2881                  * these messages.)
 2882                  * XXX : Todo, as well as euid, write out ruid too
 2883                  * Note that coredump() drops proc lock.
 2884                  */
 2885                 if (coredump(td) == 0)
 2886                         sig |= WCOREFLAG;
 2887                 if (kern_logsigexit)
 2888                         log(LOG_INFO,
 2889                             "pid %d (%s), uid %d: exited on signal %d%s\n",
 2890                             p->p_pid, p->p_comm,
 2891                             td->td_ucred ? td->td_ucred->cr_uid : -1,
 2892                             sig &~ WCOREFLAG,
 2893                             sig & WCOREFLAG ? " (core dumped)" : "");
 2894         } else
 2895                 PROC_UNLOCK(p);
 2896         exit1(td, W_EXITCODE(0, sig));
 2897         /* NOTREACHED */
 2898 }
 2899 
 2900 /*
 2901  * Send queued SIGCHLD to parent when child process's state
 2902  * is changed.
 2903  */
 2904 static void
 2905 sigparent(struct proc *p, int reason, int status)
 2906 {
 2907         PROC_LOCK_ASSERT(p, MA_OWNED);
 2908         PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
 2909 
 2910         if (p->p_ksi != NULL) {
 2911                 p->p_ksi->ksi_signo  = SIGCHLD;
 2912                 p->p_ksi->ksi_code   = reason;
 2913                 p->p_ksi->ksi_status = status;
 2914                 p->p_ksi->ksi_pid    = p->p_pid;
 2915                 p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
 2916                 if (KSI_ONQ(p->p_ksi))
 2917                         return;
 2918         }
 2919         tdsignal(p->p_pptr, NULL, SIGCHLD, p->p_ksi);
 2920 }
 2921 
 2922 static void
 2923 childproc_jobstate(struct proc *p, int reason, int status)
 2924 {
 2925         struct sigacts *ps;
 2926 
 2927         PROC_LOCK_ASSERT(p, MA_OWNED);
 2928         PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
 2929 
 2930         /*
 2931          * Wake up parent sleeping in kern_wait(), also send
 2932          * SIGCHLD to parent, but SIGCHLD does not guarantee
 2933          * that parent will awake, because parent may masked
 2934          * the signal.
 2935          */
 2936         p->p_pptr->p_flag |= P_STATCHILD;
 2937         wakeup(p->p_pptr);
 2938 
 2939         ps = p->p_pptr->p_sigacts;
 2940         mtx_lock(&ps->ps_mtx);
 2941         if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
 2942                 mtx_unlock(&ps->ps_mtx);
 2943                 sigparent(p, reason, status);
 2944         } else
 2945                 mtx_unlock(&ps->ps_mtx);
 2946 }
 2947 
 2948 void
 2949 childproc_stopped(struct proc *p, int reason)
 2950 {
 2951         childproc_jobstate(p, reason, p->p_xstat);
 2952 }
 2953 
 2954 void
 2955 childproc_continued(struct proc *p)
 2956 {
 2957         childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
 2958 }
 2959 
 2960 void
 2961 childproc_exited(struct proc *p)
 2962 {
 2963         int reason;
 2964         int status = p->p_xstat; /* convert to int */
 2965 
 2966         reason = CLD_EXITED;
 2967         if (WCOREDUMP(status))
 2968                 reason = CLD_DUMPED;
 2969         else if (WIFSIGNALED(status))
 2970                 reason = CLD_KILLED;
 2971         /*
 2972          * XXX avoid calling wakeup(p->p_pptr), the work is
 2973          * done in exit1().
 2974          */
 2975         sigparent(p, reason, status);
 2976 }
 2977 
 2978 static char corefilename[MAXPATHLEN] = {"%N.core"};
 2979 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
 2980               sizeof(corefilename), "process corefile name format string");
 2981 
 2982 /*
 2983  * expand_name(name, uid, pid)
 2984  * Expand the name described in corefilename, using name, uid, and pid.
 2985  * corefilename is a printf-like string, with three format specifiers:
 2986  *      %N      name of process ("name")
 2987  *      %P      process id (pid)
 2988  *      %U      user id (uid)
 2989  * For example, "%N.core" is the default; they can be disabled completely
 2990  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
 2991  * This is controlled by the sysctl variable kern.corefile (see above).
 2992  */
 2993 static char *
 2994 expand_name(name, uid, pid)
 2995         const char *name;
 2996         uid_t uid;
 2997         pid_t pid;
 2998 {
 2999         struct sbuf sb;
 3000         const char *format;
 3001         char *temp;
 3002         size_t i;
 3003 
 3004         format = corefilename;
 3005         temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
 3006         if (temp == NULL)
 3007                 return (NULL);
 3008         (void)sbuf_new(&sb, temp, MAXPATHLEN, SBUF_FIXEDLEN);
 3009         for (i = 0; format[i]; i++) {
 3010                 switch (format[i]) {
 3011                 case '%':       /* Format character */
 3012                         i++;
 3013                         switch (format[i]) {
 3014                         case '%':
 3015                                 sbuf_putc(&sb, '%');
 3016                                 break;
 3017                         case 'N':       /* process name */
 3018                                 sbuf_printf(&sb, "%s", name);
 3019                                 break;
 3020                         case 'P':       /* process id */
 3021                                 sbuf_printf(&sb, "%u", pid);
 3022                                 break;
 3023                         case 'U':       /* user id */
 3024                                 sbuf_printf(&sb, "%u", uid);
 3025                                 break;
 3026                         default:
 3027                                 log(LOG_ERR,
 3028                                     "Unknown format character %c in "
 3029                                     "corename `%s'\n", format[i], format);
 3030                         }
 3031                         break;
 3032                 default:
 3033                         sbuf_putc(&sb, format[i]);
 3034                 }
 3035         }
 3036         if (sbuf_overflowed(&sb)) {
 3037                 sbuf_delete(&sb);
 3038                 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
 3039                     "long\n", (long)pid, name, (u_long)uid);
 3040                 free(temp, M_TEMP);
 3041                 return (NULL);
 3042         }
 3043         sbuf_finish(&sb);
 3044         sbuf_delete(&sb);
 3045         return (temp);
 3046 }
 3047 
 3048 /*
 3049  * Dump a process' core.  The main routine does some
 3050  * policy checking, and creates the name of the coredump;
 3051  * then it passes on a vnode and a size limit to the process-specific
 3052  * coredump routine if there is one; if there _is not_ one, it returns
 3053  * ENOSYS; otherwise it returns the error from the process-specific routine.
 3054  */
 3055 
 3056 static int
 3057 coredump(struct thread *td)
 3058 {
 3059         struct proc *p = td->td_proc;
 3060         register struct vnode *vp;
 3061         register struct ucred *cred = td->td_ucred;
 3062         struct flock lf;
 3063         struct nameidata nd;
 3064         struct vattr vattr;
 3065         int error, error1, flags, locked;
 3066         struct mount *mp;
 3067         char *name;                     /* name of corefile */
 3068         off_t limit;
 3069         int vfslocked;
 3070 
 3071         PROC_LOCK_ASSERT(p, MA_OWNED);
 3072         MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
 3073         _STOPEVENT(p, S_CORE, 0);
 3074 
 3075         name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
 3076         if (name == NULL) {
 3077                 PROC_UNLOCK(p);
 3078 #ifdef AUDIT
 3079                 audit_proc_coredump(td, NULL, EINVAL);
 3080 #endif
 3081                 return (EINVAL);
 3082         }
 3083         if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
 3084                 PROC_UNLOCK(p);
 3085 #ifdef AUDIT
 3086                 audit_proc_coredump(td, name, EFAULT);
 3087 #endif
 3088                 free(name, M_TEMP);
 3089                 return (EFAULT);
 3090         }
 3091         
 3092         /*
 3093          * Note that the bulk of limit checking is done after
 3094          * the corefile is created.  The exception is if the limit
 3095          * for corefiles is 0, in which case we don't bother
 3096          * creating the corefile at all.  This layout means that
 3097          * a corefile is truncated instead of not being created,
 3098          * if it is larger than the limit.
 3099          */
 3100         limit = (off_t)lim_cur(p, RLIMIT_CORE);
 3101         PROC_UNLOCK(p);
 3102         if (limit == 0) {
 3103 #ifdef AUDIT
 3104                 audit_proc_coredump(td, name, EFBIG);
 3105 #endif
 3106                 free(name, M_TEMP);
 3107                 return (EFBIG);
 3108         }
 3109 
 3110 restart:
 3111         NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, name, td);
 3112         flags = O_CREAT | FWRITE | O_NOFOLLOW;
 3113         error = vn_open_cred(&nd, &flags, S_IRUSR | S_IWUSR, VN_OPEN_NOAUDIT,
 3114             cred, NULL);
 3115         if (error) {
 3116 #ifdef AUDIT
 3117                 audit_proc_coredump(td, name, error);
 3118 #endif
 3119                 free(name, M_TEMP);
 3120                 return (error);
 3121         }
 3122         vfslocked = NDHASGIANT(&nd);
 3123         NDFREE(&nd, NDF_ONLY_PNBUF);
 3124         vp = nd.ni_vp;
 3125 
 3126         /* Don't dump to non-regular files or files with links. */
 3127         if (vp->v_type != VREG ||
 3128             VOP_GETATTR(vp, &vattr, cred) || vattr.va_nlink != 1) {
 3129                 VOP_UNLOCK(vp, 0);
 3130                 error = EFAULT;
 3131                 goto close;
 3132         }
 3133 
 3134         VOP_UNLOCK(vp, 0);
 3135         lf.l_whence = SEEK_SET;
 3136         lf.l_start = 0;
 3137         lf.l_len = 0;
 3138         lf.l_type = F_WRLCK;
 3139         locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
 3140 
 3141         if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
 3142                 lf.l_type = F_UNLCK;
 3143                 if (locked)
 3144                         VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
 3145                 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
 3146                         goto out;
 3147                 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
 3148                         goto out;
 3149                 VFS_UNLOCK_GIANT(vfslocked);
 3150                 goto restart;
 3151         }
 3152 
 3153         VATTR_NULL(&vattr);
 3154         vattr.va_size = 0;
 3155         if (set_core_nodump_flag)
 3156                 vattr.va_flags = UF_NODUMP;
 3157         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 3158         VOP_SETATTR(vp, &vattr, cred);
 3159         VOP_UNLOCK(vp, 0);
 3160         vn_finished_write(mp);
 3161         PROC_LOCK(p);
 3162         p->p_acflag |= ACORE;
 3163         PROC_UNLOCK(p);
 3164 
 3165         error = p->p_sysent->sv_coredump ?
 3166           p->p_sysent->sv_coredump(td, vp, limit) :
 3167           ENOSYS;
 3168 
 3169         if (locked) {
 3170                 lf.l_type = F_UNLCK;
 3171                 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
 3172         }
 3173 close:
 3174         error1 = vn_close(vp, FWRITE, cred, td);
 3175         if (error == 0)
 3176                 error = error1;
 3177 out:
 3178 #ifdef AUDIT
 3179         audit_proc_coredump(td, name, error);
 3180 #endif
 3181         free(name, M_TEMP);
 3182         VFS_UNLOCK_GIANT(vfslocked);
 3183         return (error);
 3184 }
 3185 
 3186 /*
 3187  * Nonexistent system call-- signal process (may want to handle it).  Flag
 3188  * error in case process won't see signal immediately (blocked or ignored).
 3189  */
 3190 #ifndef _SYS_SYSPROTO_H_
 3191 struct nosys_args {
 3192         int     dummy;
 3193 };
 3194 #endif
 3195 /* ARGSUSED */
 3196 int
 3197 nosys(td, args)
 3198         struct thread *td;
 3199         struct nosys_args *args;
 3200 {
 3201         struct proc *p = td->td_proc;
 3202 
 3203         PROC_LOCK(p);
 3204         psignal(p, SIGSYS);
 3205         PROC_UNLOCK(p);
 3206         return (ENOSYS);
 3207 }
 3208 
 3209 /*
 3210  * Send a SIGIO or SIGURG signal to a process or process group using stored
 3211  * credentials rather than those of the current process.
 3212  */
 3213 void
 3214 pgsigio(sigiop, sig, checkctty)
 3215         struct sigio **sigiop;
 3216         int sig, checkctty;
 3217 {
 3218         ksiginfo_t ksi;
 3219         struct sigio *sigio;
 3220 
 3221         ksiginfo_init(&ksi);
 3222         ksi.ksi_signo = sig;
 3223         ksi.ksi_code = SI_KERNEL;
 3224 
 3225         SIGIO_LOCK();
 3226         sigio = *sigiop;
 3227         if (sigio == NULL) {
 3228                 SIGIO_UNLOCK();
 3229                 return;
 3230         }
 3231         if (sigio->sio_pgid > 0) {
 3232                 PROC_LOCK(sigio->sio_proc);
 3233                 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
 3234                         psignal(sigio->sio_proc, sig);
 3235                 PROC_UNLOCK(sigio->sio_proc);
 3236         } else if (sigio->sio_pgid < 0) {
 3237                 struct proc *p;
 3238 
 3239                 PGRP_LOCK(sigio->sio_pgrp);
 3240                 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
 3241                         PROC_LOCK(p);
 3242                         if (p->p_state == PRS_NORMAL &&
 3243                             CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
 3244                             (checkctty == 0 || (p->p_flag & P_CONTROLT)))
 3245                                 psignal(p, sig);
 3246                         PROC_UNLOCK(p);
 3247                 }
 3248                 PGRP_UNLOCK(sigio->sio_pgrp);
 3249         }
 3250         SIGIO_UNLOCK();
 3251 }
 3252 
 3253 static int
 3254 filt_sigattach(struct knote *kn)
 3255 {
 3256         struct proc *p = curproc;
 3257 
 3258         kn->kn_ptr.p_proc = p;
 3259         kn->kn_flags |= EV_CLEAR;               /* automatically set */
 3260 
 3261         knlist_add(&p->p_klist, kn, 0);
 3262 
 3263         return (0);
 3264 }
 3265 
 3266 static void
 3267 filt_sigdetach(struct knote *kn)
 3268 {
 3269         struct proc *p = kn->kn_ptr.p_proc;
 3270 
 3271         knlist_remove(&p->p_klist, kn, 0);
 3272 }
 3273 
 3274 /*
 3275  * signal knotes are shared with proc knotes, so we apply a mask to 
 3276  * the hint in order to differentiate them from process hints.  This
 3277  * could be avoided by using a signal-specific knote list, but probably
 3278  * isn't worth the trouble.
 3279  */
 3280 static int
 3281 filt_signal(struct knote *kn, long hint)
 3282 {
 3283 
 3284         if (hint & NOTE_SIGNAL) {
 3285                 hint &= ~NOTE_SIGNAL;
 3286 
 3287                 if (kn->kn_id == hint)
 3288                         kn->kn_data++;
 3289         }
 3290         return (kn->kn_data != 0);
 3291 }
 3292 
 3293 struct sigacts *
 3294 sigacts_alloc(void)
 3295 {
 3296         struct sigacts *ps;
 3297 
 3298         ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
 3299         ps->ps_refcnt = 1;
 3300         mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
 3301         return (ps);
 3302 }
 3303 
 3304 void
 3305 sigacts_free(struct sigacts *ps)
 3306 {
 3307 
 3308         mtx_lock(&ps->ps_mtx);
 3309         ps->ps_refcnt--;
 3310         if (ps->ps_refcnt == 0) {
 3311                 mtx_destroy(&ps->ps_mtx);
 3312                 free(ps, M_SUBPROC);
 3313         } else
 3314                 mtx_unlock(&ps->ps_mtx);
 3315 }
 3316 
 3317 struct sigacts *
 3318 sigacts_hold(struct sigacts *ps)
 3319 {
 3320         mtx_lock(&ps->ps_mtx);
 3321         ps->ps_refcnt++;
 3322         mtx_unlock(&ps->ps_mtx);
 3323         return (ps);
 3324 }
 3325 
 3326 void
 3327 sigacts_copy(struct sigacts *dest, struct sigacts *src)
 3328 {
 3329 
 3330         KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
 3331         mtx_lock(&src->ps_mtx);
 3332         bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
 3333         mtx_unlock(&src->ps_mtx);
 3334 }
 3335 
 3336 int
 3337 sigacts_shared(struct sigacts *ps)
 3338 {
 3339         int shared;
 3340 
 3341         mtx_lock(&ps->ps_mtx);
 3342         shared = ps->ps_refcnt > 1;
 3343         mtx_unlock(&ps->ps_mtx);
 3344         return (shared);
 3345 }

Cache object: 724f9cb7e763504461b1c967b65c3da6


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