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

Cache object: 039f53f2933cd3993537f58990d49d95


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