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

Cache object: 44e518a8648eccffaf82988ff9cfe0be


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