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

Cache object: 17612aa0c47235deaf02ce3c58e866a3


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