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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: kern_sig.c,v 1.189.2.7 2005/12/29 01:29:55 riz Exp $   */
    2 
    3 /*
    4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  * (c) UNIX System Laboratories, Inc.
    7  * All or some portions of this file are derived from material licensed
    8  * to the University of California by American Telephone and Telegraph
    9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   10  * the permission of UNIX System Laboratories, Inc.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)kern_sig.c  8.14 (Berkeley) 5/14/95
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.189.2.7 2005/12/29 01:29:55 riz Exp $");
   41 
   42 #include "opt_ktrace.h"
   43 #include "opt_compat_sunos.h"
   44 #include "opt_compat_netbsd.h"
   45 #include "opt_compat_netbsd32.h"        
   46 
   47 #define SIGPROP         /* include signal properties table */
   48 #include <sys/param.h>
   49 #include <sys/signalvar.h>
   50 #include <sys/resourcevar.h>
   51 #include <sys/namei.h>
   52 #include <sys/vnode.h>
   53 #include <sys/proc.h>
   54 #include <sys/systm.h>
   55 #include <sys/timeb.h>
   56 #include <sys/times.h>
   57 #include <sys/buf.h>
   58 #include <sys/acct.h>
   59 #include <sys/file.h>
   60 #include <sys/kernel.h>
   61 #include <sys/wait.h>
   62 #include <sys/ktrace.h>
   63 #include <sys/syslog.h>
   64 #include <sys/stat.h>
   65 #include <sys/core.h>
   66 #include <sys/filedesc.h>
   67 #include <sys/malloc.h>
   68 #include <sys/pool.h>
   69 #include <sys/ucontext.h>
   70 #include <sys/sa.h>
   71 #include <sys/savar.h>
   72 #include <sys/exec.h>
   73 
   74 #include <sys/mount.h>
   75 #include <sys/syscallargs.h>
   76 
   77 #include <machine/cpu.h>
   78 
   79 #include <sys/user.h>           /* for coredump */
   80 
   81 #include <uvm/uvm.h>
   82 #include <uvm/uvm_extern.h>
   83 
   84 static void     child_psignal(struct proc *, int);
   85 static int      build_corename(struct proc *, char [MAXPATHLEN]);
   86 static void     ksiginfo_exithook(struct proc *, void *);
   87 static void     ksiginfo_put(struct proc *, const ksiginfo_t *);
   88 static ksiginfo_t *ksiginfo_get(struct proc *, int);
   89 static void     kpsignal2(struct proc *, const ksiginfo_t *, int);
   90 
   91 sigset_t        contsigmask, stopsigmask, sigcantmask;
   92 
   93 struct pool     sigacts_pool;   /* memory pool for sigacts structures */
   94 
   95 /*
   96  * struct sigacts memory pool allocator.
   97  */
   98 
   99 static void *
  100 sigacts_poolpage_alloc(struct pool *pp, int flags)
  101 {
  102 
  103         return (void *)uvm_km_kmemalloc1(kernel_map,
  104             uvm.kernel_object, (PAGE_SIZE)*2, (PAGE_SIZE)*2, UVM_UNKNOWN_OFFSET,
  105             (flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK);
  106 }
  107 
  108 static void
  109 sigacts_poolpage_free(struct pool *pp, void *v)
  110 {
  111         uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2);
  112 }
  113 
  114 static struct pool_allocator sigactspool_allocator = {
  115         sigacts_poolpage_alloc, sigacts_poolpage_free,
  116 };
  117 
  118 struct pool     siginfo_pool;   /* memory pool for siginfo structures */
  119 struct pool     ksiginfo_pool;  /* memory pool for ksiginfo structures */
  120 
  121 /*
  122  * Can process p, with pcred pc, send the signal signum to process q?
  123  */
  124 #define CANSIGNAL(p, pc, q, signum) \
  125         ((pc)->pc_ucred->cr_uid == 0 || \
  126             (pc)->p_ruid == (q)->p_cred->p_ruid || \
  127             (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
  128             (pc)->p_ruid == (q)->p_ucred->cr_uid || \
  129             (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
  130             ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
  131 
  132 /*
  133  * Remove and return the first ksiginfo element that matches our requested
  134  * signal, or return NULL if one not found.
  135  */
  136 static ksiginfo_t *
  137 ksiginfo_get(struct proc *p, int signo)
  138 {
  139         ksiginfo_t *ksi;
  140         int s;
  141 
  142         s = splsoftclock();
  143         simple_lock(&p->p_sigctx.ps_silock);
  144         CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) {
  145                 if (ksi->ksi_signo == signo) {
  146                         CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
  147                         goto out;
  148                 }
  149         }
  150         ksi = NULL;
  151 out:
  152         simple_unlock(&p->p_sigctx.ps_silock);
  153         splx(s);
  154         return ksi;
  155 }
  156 
  157 /*
  158  * Append a new ksiginfo element to the list of pending ksiginfo's, if
  159  * we need to (SA_SIGINFO was requested). We replace non RT signals if
  160  * they already existed in the queue and we add new entries for RT signals,
  161  * or for non RT signals with non-existing entries.
  162  */
  163 static void
  164 ksiginfo_put(struct proc *p, const ksiginfo_t *ksi)
  165 {
  166         ksiginfo_t *kp;
  167         struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo);
  168         int s;
  169 
  170         if ((sa->sa_flags & SA_SIGINFO) == 0)
  171                 return;
  172         /*
  173          * If there's no info, don't save it.
  174          */
  175         if (KSI_EMPTY_P(ksi))
  176                 return;
  177 
  178         s = splsoftclock();
  179         simple_lock(&p->p_sigctx.ps_silock);
  180 #ifdef notyet   /* XXX: QUEUING */
  181         if (ksi->ksi_signo < SIGRTMIN)
  182 #endif
  183         {
  184                 CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) {
  185                         if (kp->ksi_signo == ksi->ksi_signo) {
  186                                 KSI_COPY(ksi, kp);
  187                                 goto out;
  188                         }
  189                 }
  190         }
  191         kp = pool_get(&ksiginfo_pool, PR_NOWAIT);
  192         if (kp == NULL) {
  193 #ifdef DIAGNOSTIC
  194                 printf("Out of memory allocating siginfo for pid %d\n",
  195                     p->p_pid);
  196 #endif
  197                 goto out;
  198         }
  199         *kp = *ksi;
  200         CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list);
  201 out:
  202         simple_unlock(&p->p_sigctx.ps_silock);
  203         splx(s);
  204 }
  205 
  206 /*
  207  * free all pending ksiginfo on exit
  208  */
  209 static void
  210 ksiginfo_exithook(struct proc *p, void *v)
  211 {
  212         int s;
  213 
  214         s = splsoftclock();
  215         simple_lock(&p->p_sigctx.ps_silock);
  216         while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) {
  217                 ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo);
  218                 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
  219                 pool_put(&ksiginfo_pool, ksi);
  220         }
  221         simple_unlock(&p->p_sigctx.ps_silock);
  222         splx(s);
  223 }
  224 
  225 /*
  226  * Initialize signal-related data structures.
  227  */
  228 void
  229 signal_init(void)
  230 {
  231 
  232         sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
  233 
  234         pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
  235             sizeof(struct sigacts) > PAGE_SIZE ?
  236             &sigactspool_allocator : &pool_allocator_nointr);
  237         pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
  238             &pool_allocator_nointr);
  239         pool_init(&ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo",
  240             NULL);
  241         exithook_establish(ksiginfo_exithook, NULL);
  242         exechook_establish(ksiginfo_exithook, NULL);
  243 }
  244 
  245 /*
  246  * Create an initial sigctx structure, using the same signal state
  247  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
  248  * copy it from parent.
  249  */
  250 void
  251 sigactsinit(struct proc *np, struct proc *pp, int share)
  252 {
  253         struct sigacts *ps;
  254 
  255         if (share) {
  256                 np->p_sigacts = pp->p_sigacts;
  257                 pp->p_sigacts->sa_refcnt++;
  258         } else {
  259                 ps = pool_get(&sigacts_pool, PR_WAITOK);
  260                 if (pp)
  261                         memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
  262                 else
  263                         memset(ps, '\0', sizeof(struct sigacts));
  264                 ps->sa_refcnt = 1;
  265                 np->p_sigacts = ps;
  266         }
  267 }
  268 
  269 /*
  270  * Make this process not share its sigctx, maintaining all
  271  * signal state.
  272  */
  273 void
  274 sigactsunshare(struct proc *p)
  275 {
  276         struct sigacts *oldps;
  277 
  278         if (p->p_sigacts->sa_refcnt == 1)
  279                 return;
  280 
  281         oldps = p->p_sigacts;
  282         sigactsinit(p, NULL, 0);
  283 
  284         if (--oldps->sa_refcnt == 0)
  285                 pool_put(&sigacts_pool, oldps);
  286 }
  287 
  288 /*
  289  * Release a sigctx structure.
  290  */
  291 void
  292 sigactsfree(struct proc *p)
  293 {
  294         struct sigacts *ps;
  295 
  296         ps = p->p_sigacts;
  297         if (--ps->sa_refcnt > 0)
  298                 return;
  299 
  300         pool_put(&sigacts_pool, ps);
  301 }
  302 
  303 int
  304 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
  305         struct sigaction *osa, const void *tramp, int vers)
  306 {
  307         struct sigacts  *ps;
  308         int             prop;
  309 
  310         ps = p->p_sigacts;
  311         if (signum <= 0 || signum >= NSIG)
  312                 return (EINVAL);
  313 
  314         /*
  315          * Trampoline ABI version 0 is reserved for the legacy
  316          * kernel-provided on-stack trampoline.  Conversely, if we are
  317          * using a non-0 ABI version, we must have a trampoline.  Only
  318          * validate the vers if a new sigaction was supplied. Emulations
  319          * use legacy kernel trampolines with version 0, alternatively
  320          * check for that too.
  321          */
  322         if ((vers != 0 && tramp == NULL) ||
  323 #ifdef SIGTRAMP_VALID
  324             (nsa != NULL && 
  325             ((vers == 0) ? 
  326                 (p->p_emul->e_sigcode == NULL) :
  327                 !SIGTRAMP_VALID(vers))) ||
  328 #endif
  329             (vers == 0 && tramp != NULL))
  330                 return (EINVAL);
  331 
  332         if (osa)
  333                 *osa = SIGACTION_PS(ps, signum);
  334 
  335         if (nsa) {
  336                 if (nsa->sa_flags & ~SA_ALLBITS)
  337                         return (EINVAL);
  338 
  339                 prop = sigprop[signum];
  340                 if (prop & SA_CANTMASK)
  341                         return (EINVAL);
  342 
  343                 (void) splsched();      /* XXXSMP */
  344                 SIGACTION_PS(ps, signum) = *nsa;
  345                 ps->sa_sigdesc[signum].sd_tramp = tramp;
  346                 ps->sa_sigdesc[signum].sd_vers = vers;
  347                 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
  348                 if ((prop & SA_NORESET) != 0)
  349                         SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
  350                 if (signum == SIGCHLD) {
  351                         if (nsa->sa_flags & SA_NOCLDSTOP)
  352                                 p->p_flag |= P_NOCLDSTOP;
  353                         else
  354                                 p->p_flag &= ~P_NOCLDSTOP;
  355                         if (nsa->sa_flags & SA_NOCLDWAIT) {
  356                                 /*
  357                                  * Paranoia: since SA_NOCLDWAIT is implemented
  358                                  * by reparenting the dying child to PID 1 (and
  359                                  * trust it to reap the zombie), PID 1 itself
  360                                  * is forbidden to set SA_NOCLDWAIT.
  361                                  */
  362                                 if (p->p_pid == 1)
  363                                         p->p_flag &= ~P_NOCLDWAIT;
  364                                 else
  365                                         p->p_flag |= P_NOCLDWAIT;
  366                         } else
  367                                 p->p_flag &= ~P_NOCLDWAIT;
  368                 }
  369                 if ((nsa->sa_flags & SA_NODEFER) == 0)
  370                         sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
  371                 else
  372                         sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
  373                 /*
  374                  * Set bit in p_sigctx.ps_sigignore for signals that are set to
  375                  * SIG_IGN, and for signals set to SIG_DFL where the default is
  376                  * to ignore. However, don't put SIGCONT in
  377                  * p_sigctx.ps_sigignore, as we have to restart the process.
  378                  */
  379                 if (nsa->sa_handler == SIG_IGN ||
  380                     (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
  381                                                 /* never to be seen again */
  382                         sigdelset(&p->p_sigctx.ps_siglist, signum);
  383                         if (signum != SIGCONT) {
  384                                                 /* easier in psignal */
  385                                 sigaddset(&p->p_sigctx.ps_sigignore, signum);
  386                         }
  387                         sigdelset(&p->p_sigctx.ps_sigcatch, signum);
  388                 } else {
  389                         sigdelset(&p->p_sigctx.ps_sigignore, signum);
  390                         if (nsa->sa_handler == SIG_DFL)
  391                                 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
  392                         else
  393                                 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
  394                 }
  395                 (void) spl0();
  396         }
  397 
  398         return (0);
  399 }
  400 
  401 #ifdef COMPAT_16
  402 /* ARGSUSED */
  403 int
  404 compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
  405 {
  406         struct compat_16_sys___sigaction14_args /* {
  407                 syscallarg(int)                         signum;
  408                 syscallarg(const struct sigaction *)    nsa;
  409                 syscallarg(struct sigaction *)          osa;
  410         } */ *uap = v;
  411         struct proc             *p;
  412         struct sigaction        nsa, osa;
  413         int                     error;
  414 
  415         if (SCARG(uap, nsa)) {
  416                 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
  417                 if (error)
  418                         return (error);
  419         }
  420         p = l->l_proc;
  421         error = sigaction1(p, SCARG(uap, signum),
  422             SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
  423             NULL, 0);
  424         if (error)
  425                 return (error);
  426         if (SCARG(uap, osa)) {
  427                 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
  428                 if (error)
  429                         return (error);
  430         }
  431         return (0);
  432 }
  433 #endif
  434 
  435 /* ARGSUSED */
  436 int
  437 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
  438 {
  439         struct sys___sigaction_sigtramp_args /* {
  440                 syscallarg(int)                         signum;
  441                 syscallarg(const struct sigaction *)    nsa;
  442                 syscallarg(struct sigaction *)          osa;
  443                 syscallarg(void *)                      tramp;
  444                 syscallarg(int)                         vers;
  445         } */ *uap = v;
  446         struct proc *p = l->l_proc;
  447         struct sigaction nsa, osa;
  448         int error;
  449 
  450         if (SCARG(uap, nsa)) {
  451                 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
  452                 if (error)
  453                         return (error);
  454         }
  455         error = sigaction1(p, SCARG(uap, signum),
  456             SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
  457             SCARG(uap, tramp), SCARG(uap, vers));
  458         if (error)
  459                 return (error);
  460         if (SCARG(uap, osa)) {
  461                 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
  462                 if (error)
  463                         return (error);
  464         }
  465         return (0);
  466 }
  467 
  468 /*
  469  * Initialize signal state for process 0;
  470  * set to ignore signals that are ignored by default and disable the signal
  471  * stack.
  472  */
  473 void
  474 siginit(struct proc *p)
  475 {
  476         struct sigacts  *ps;
  477         int             signum, prop;
  478 
  479         ps = p->p_sigacts;
  480         sigemptyset(&contsigmask);
  481         sigemptyset(&stopsigmask);
  482         sigemptyset(&sigcantmask);
  483         for (signum = 1; signum < NSIG; signum++) {
  484                 prop = sigprop[signum];
  485                 if (prop & SA_CONT)
  486                         sigaddset(&contsigmask, signum);
  487                 if (prop & SA_STOP)
  488                         sigaddset(&stopsigmask, signum);
  489                 if (prop & SA_CANTMASK)
  490                         sigaddset(&sigcantmask, signum);
  491                 if (prop & SA_IGNORE && signum != SIGCONT)
  492                         sigaddset(&p->p_sigctx.ps_sigignore, signum);
  493                 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
  494                 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
  495         }
  496         sigemptyset(&p->p_sigctx.ps_sigcatch);
  497         p->p_sigctx.ps_sigwaited = NULL;
  498         p->p_flag &= ~P_NOCLDSTOP;
  499 
  500         /*
  501          * Reset stack state to the user stack.
  502          */
  503         p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
  504         p->p_sigctx.ps_sigstk.ss_size = 0;
  505         p->p_sigctx.ps_sigstk.ss_sp = 0;
  506 
  507         /* One reference. */
  508         ps->sa_refcnt = 1;
  509 }
  510 
  511 /*
  512  * Reset signals for an exec of the specified process.
  513  */
  514 void
  515 execsigs(struct proc *p)
  516 {
  517         struct sigacts  *ps;
  518         int             signum, prop;
  519 
  520         sigactsunshare(p);
  521 
  522         ps = p->p_sigacts;
  523 
  524         /*
  525          * Reset caught signals.  Held signals remain held
  526          * through p_sigctx.ps_sigmask (unless they were caught,
  527          * and are now ignored by default).
  528          */
  529         for (signum = 1; signum < NSIG; signum++) {
  530                 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
  531                         prop = sigprop[signum];
  532                         if (prop & SA_IGNORE) {
  533                                 if ((prop & SA_CONT) == 0)
  534                                         sigaddset(&p->p_sigctx.ps_sigignore,
  535                                             signum);
  536                                 sigdelset(&p->p_sigctx.ps_siglist, signum);
  537                         }
  538                         SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
  539                 }
  540                 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
  541                 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
  542         }
  543         sigemptyset(&p->p_sigctx.ps_sigcatch);
  544         p->p_sigctx.ps_sigwaited = NULL;
  545         p->p_flag &= ~P_NOCLDSTOP;
  546 
  547         /*
  548          * Reset stack state to the user stack.
  549          */
  550         p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
  551         p->p_sigctx.ps_sigstk.ss_size = 0;
  552         p->p_sigctx.ps_sigstk.ss_sp = 0;
  553 }
  554 
  555 int
  556 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
  557 {
  558 
  559         if (oss)
  560                 *oss = p->p_sigctx.ps_sigmask;
  561 
  562         if (nss) {
  563                 (void)splsched();       /* XXXSMP */
  564                 switch (how) {
  565                 case SIG_BLOCK:
  566                         sigplusset(nss, &p->p_sigctx.ps_sigmask);
  567                         break;
  568                 case SIG_UNBLOCK:
  569                         sigminusset(nss, &p->p_sigctx.ps_sigmask);
  570                         CHECKSIGS(p);
  571                         break;
  572                 case SIG_SETMASK:
  573                         p->p_sigctx.ps_sigmask = *nss;
  574                         CHECKSIGS(p);
  575                         break;
  576                 default:
  577                         (void)spl0();   /* XXXSMP */
  578                         return (EINVAL);
  579                 }
  580                 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
  581                 (void)spl0();           /* XXXSMP */
  582         }
  583 
  584         return (0);
  585 }
  586         
  587 /*
  588  * Manipulate signal mask.
  589  * Note that we receive new mask, not pointer,
  590  * and return old mask as return value;
  591  * the library stub does the rest.
  592  */
  593 int
  594 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
  595 {
  596         struct sys___sigprocmask14_args /* {
  597                 syscallarg(int)                 how;
  598                 syscallarg(const sigset_t *)    set;
  599                 syscallarg(sigset_t *)          oset;
  600         } */ *uap = v;
  601         struct proc     *p;
  602         sigset_t        nss, oss;
  603         int             error;
  604 
  605         if (SCARG(uap, set)) {
  606                 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
  607                 if (error)
  608                         return (error);
  609         }
  610         p = l->l_proc;
  611         error = sigprocmask1(p, SCARG(uap, how),
  612             SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
  613         if (error)
  614                 return (error);
  615         if (SCARG(uap, oset)) {
  616                 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
  617                 if (error)
  618                         return (error);
  619         }
  620         return (0);
  621 }
  622 
  623 void
  624 sigpending1(struct proc *p, sigset_t *ss)
  625 {
  626 
  627         *ss = p->p_sigctx.ps_siglist;
  628         sigminusset(&p->p_sigctx.ps_sigmask, ss);
  629 }
  630 
  631 /* ARGSUSED */
  632 int
  633 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
  634 {
  635         struct sys___sigpending14_args /* {
  636                 syscallarg(sigset_t *)  set;
  637         } */ *uap = v;
  638         struct proc     *p;
  639         sigset_t        ss;
  640 
  641         p = l->l_proc;
  642         sigpending1(p, &ss);
  643         return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
  644 }
  645 
  646 int
  647 sigsuspend1(struct proc *p, const sigset_t *ss)
  648 {
  649         struct sigacts *ps;
  650 
  651         ps = p->p_sigacts;
  652         if (ss) {
  653                 /*
  654                  * When returning from sigpause, we want
  655                  * the old mask to be restored after the
  656                  * signal handler has finished.  Thus, we
  657                  * save it here and mark the sigctx structure
  658                  * to indicate this.
  659                  */
  660                 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
  661                 p->p_sigctx.ps_flags |= SAS_OLDMASK;
  662                 (void) splsched();      /* XXXSMP */
  663                 p->p_sigctx.ps_sigmask = *ss;
  664                 CHECKSIGS(p);
  665                 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
  666                 (void) spl0();          /* XXXSMP */
  667         }
  668 
  669         while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
  670                 /* void */;
  671         
  672         /* always return EINTR rather than ERESTART... */
  673         return (EINTR);
  674 }
  675 
  676 /*
  677  * Suspend process until signal, providing mask to be set
  678  * in the meantime.  Note nonstandard calling convention:
  679  * libc stub passes mask, not pointer, to save a copyin.
  680  */
  681 /* ARGSUSED */
  682 int
  683 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
  684 {
  685         struct sys___sigsuspend14_args /* {
  686                 syscallarg(const sigset_t *)    set;
  687         } */ *uap = v;
  688         struct proc     *p;
  689         sigset_t        ss;
  690         int             error;
  691 
  692         if (SCARG(uap, set)) {
  693                 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
  694                 if (error)
  695                         return (error);
  696         }
  697 
  698         p = l->l_proc;
  699         return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
  700 }
  701 
  702 int
  703 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
  704         struct sigaltstack *oss)
  705 {
  706 
  707         if (oss)
  708                 *oss = p->p_sigctx.ps_sigstk;
  709 
  710         if (nss) {
  711                 if (nss->ss_flags & ~SS_ALLBITS)
  712                         return (EINVAL);
  713 
  714                 if (nss->ss_flags & SS_DISABLE) {
  715                         if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
  716                                 return (EINVAL);
  717                 } else {
  718                         if (nss->ss_size < MINSIGSTKSZ)
  719                                 return (ENOMEM);
  720                 }
  721                 p->p_sigctx.ps_sigstk = *nss;
  722         }
  723 
  724         return (0);
  725 }
  726 
  727 /* ARGSUSED */
  728 int
  729 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
  730 {
  731         struct sys___sigaltstack14_args /* {
  732                 syscallarg(const struct sigaltstack *)  nss;
  733                 syscallarg(struct sigaltstack *)        oss;
  734         } */ *uap = v;
  735         struct proc             *p;
  736         struct sigaltstack      nss, oss;
  737         int                     error;
  738 
  739         if (SCARG(uap, nss)) {
  740                 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
  741                 if (error)
  742                         return (error);
  743         }
  744         p = l->l_proc;
  745         error = sigaltstack1(p,
  746             SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
  747         if (error)
  748                 return (error);
  749         if (SCARG(uap, oss)) {
  750                 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
  751                 if (error)
  752                         return (error);
  753         }
  754         return (0);
  755 }
  756 
  757 /* ARGSUSED */
  758 int
  759 sys_kill(struct lwp *l, void *v, register_t *retval)
  760 {
  761         struct sys_kill_args /* {
  762                 syscallarg(int) pid;
  763                 syscallarg(int) signum;
  764         } */ *uap = v;
  765         struct proc     *cp, *p;
  766         struct pcred    *pc;
  767         ksiginfo_t      ksi;
  768 
  769         cp = l->l_proc;
  770         pc = cp->p_cred;
  771         if ((u_int)SCARG(uap, signum) >= NSIG)
  772                 return (EINVAL);
  773         KSI_INIT(&ksi);
  774         ksi.ksi_signo = SCARG(uap, signum);
  775         ksi.ksi_code = SI_USER;
  776         ksi.ksi_pid = cp->p_pid;
  777         ksi.ksi_uid = cp->p_ucred->cr_uid;
  778         if (SCARG(uap, pid) > 0) {
  779                 /* kill single process */
  780                 if ((p = pfind(SCARG(uap, pid))) == NULL)
  781                         return (ESRCH);
  782                 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
  783                         return (EPERM);
  784                 if (SCARG(uap, signum))
  785                         kpsignal2(p, &ksi, 1);
  786                 return (0);
  787         }
  788         switch (SCARG(uap, pid)) {
  789         case -1:                /* broadcast signal */
  790                 return (killpg1(cp, &ksi, 0, 1));
  791         case 0:                 /* signal own process group */
  792                 return (killpg1(cp, &ksi, 0, 0));
  793         default:                /* negative explicit process group */
  794                 return (killpg1(cp, &ksi, -SCARG(uap, pid), 0));
  795         }
  796         /* NOTREACHED */
  797 }
  798 
  799 /*
  800  * Common code for kill process group/broadcast kill.
  801  * cp is calling process.
  802  */
  803 int
  804 killpg1(struct proc *cp, ksiginfo_t *ksi, int pgid, int all)
  805 {
  806         struct proc     *p;
  807         struct pcred    *pc;
  808         struct pgrp     *pgrp;
  809         int             nfound;
  810         int             signum = ksi->ksi_signo;
  811         
  812         pc = cp->p_cred;
  813         nfound = 0;
  814         if (all) {
  815                 /* 
  816                  * broadcast 
  817                  */
  818                 proclist_lock_read();
  819                 LIST_FOREACH(p, &allproc, p_list) {
  820                         if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || 
  821                             p == cp || !CANSIGNAL(cp, pc, p, signum))
  822                                 continue;
  823                         nfound++;
  824                         if (signum)
  825                                 kpsignal2(p, ksi, 1);
  826                 }
  827                 proclist_unlock_read();
  828         } else {
  829                 if (pgid == 0)          
  830                         /* 
  831                          * zero pgid means send to my process group.
  832                          */
  833                         pgrp = cp->p_pgrp;
  834                 else {
  835                         pgrp = pgfind(pgid);
  836                         if (pgrp == NULL)
  837                                 return (ESRCH);
  838                 }
  839                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
  840                         if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
  841                             !CANSIGNAL(cp, pc, p, signum))
  842                                 continue;
  843                         nfound++;
  844                         if (signum && P_ZOMBIE(p) == 0)
  845                                 kpsignal2(p, ksi, 1);
  846                 }
  847         }
  848         return (nfound ? 0 : ESRCH);
  849 }
  850 
  851 /*
  852  * Send a signal to a process group.
  853  */
  854 void
  855 gsignal(int pgid, int signum)
  856 {
  857         ksiginfo_t ksi;
  858         KSI_INIT_EMPTY(&ksi);
  859         ksi.ksi_signo = signum;
  860         kgsignal(pgid, &ksi, NULL);
  861 }
  862 
  863 void
  864 kgsignal(int pgid, ksiginfo_t *ksi, void *data)
  865 {
  866         struct pgrp *pgrp;
  867 
  868         if (pgid && (pgrp = pgfind(pgid)))
  869                 kpgsignal(pgrp, ksi, data, 0);
  870 }
  871 
  872 /*
  873  * Send a signal to a process group. If checktty is 1,
  874  * limit to members which have a controlling terminal.
  875  */
  876 void
  877 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
  878 {
  879         ksiginfo_t ksi;
  880         KSI_INIT_EMPTY(&ksi);
  881         ksi.ksi_signo = sig;
  882         kpgsignal(pgrp, &ksi, NULL, checkctty);
  883 }
  884 
  885 void
  886 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
  887 {
  888         struct proc *p;
  889 
  890         if (pgrp)
  891                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
  892                         if (checkctty == 0 || p->p_flag & P_CONTROLT)
  893                                 kpsignal(p, ksi, data);
  894 }
  895 
  896 /*
  897  * Send a signal caused by a trap to the current process.
  898  * If it will be caught immediately, deliver it with correct code.
  899  * Otherwise, post it normally.
  900  */
  901 void
  902 trapsignal(struct lwp *l, const ksiginfo_t *ksi)
  903 {
  904         struct proc     *p;
  905         struct sigacts  *ps;
  906         int signum = ksi->ksi_signo;
  907 
  908         KASSERT(KSI_TRAP_P(ksi));
  909 
  910         p = l->l_proc;
  911         ps = p->p_sigacts;
  912         if ((p->p_flag & P_TRACED) == 0 &&
  913             sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
  914             !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
  915                 p->p_stats->p_ru.ru_nsignals++;
  916 #ifdef KTRACE
  917                 if (KTRPOINT(p, KTR_PSIG))
  918                         ktrpsig(p, signum, SIGACTION_PS(ps, signum).sa_handler,
  919                             &p->p_sigctx.ps_sigmask, ksi);
  920 #endif
  921                 kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask);
  922                 (void) splsched();      /* XXXSMP */
  923                 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
  924                     &p->p_sigctx.ps_sigmask);
  925                 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
  926                         sigdelset(&p->p_sigctx.ps_sigcatch, signum);
  927                         if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
  928                                 sigaddset(&p->p_sigctx.ps_sigignore, signum);
  929                         SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
  930                 }
  931                 (void) spl0();          /* XXXSMP */
  932         } else {
  933                 p->p_sigctx.ps_lwp = l->l_lid;
  934                 /* XXX for core dump/debugger */
  935                 p->p_sigctx.ps_signo = ksi->ksi_signo;
  936                 p->p_sigctx.ps_code = ksi->ksi_trap;
  937                 kpsignal2(p, ksi, 1);
  938         }
  939 }
  940 
  941 /*
  942  * Fill in signal information and signal the parent for a child status change.
  943  */
  944 static void
  945 child_psignal(struct proc *p, int dolock)
  946 {
  947         ksiginfo_t ksi;
  948 
  949         KSI_INIT(&ksi);
  950         ksi.ksi_signo = SIGCHLD;
  951         ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED;
  952         ksi.ksi_pid = p->p_pid;
  953         ksi.ksi_uid = p->p_ucred->cr_uid;
  954         ksi.ksi_status = p->p_xstat;
  955         ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
  956         ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
  957         kpsignal2(p->p_pptr, &ksi, dolock);
  958 }
  959 
  960 /*
  961  * Send the signal to the process.  If the signal has an action, the action
  962  * is usually performed by the target process rather than the caller; we add
  963  * the signal to the set of pending signals for the process.
  964  *
  965  * Exceptions:
  966  *   o When a stop signal is sent to a sleeping process that takes the
  967  *     default action, the process is stopped without awakening it.
  968  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
  969  *     regardless of the signal action (eg, blocked or ignored).
  970  *
  971  * Other ignored signals are discarded immediately.
  972  *
  973  * XXXSMP: Invoked as psignal() or sched_psignal().
  974  */
  975 void
  976 psignal1(struct proc *p, int signum, int dolock)
  977 {
  978         ksiginfo_t ksi;
  979 
  980         KSI_INIT_EMPTY(&ksi);
  981         ksi.ksi_signo = signum;
  982         kpsignal2(p, &ksi, dolock);
  983 }
  984 
  985 void
  986 kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data, int dolock)
  987 {
  988 
  989         if ((p->p_flag & P_WEXIT) == 0 && data) {
  990                 size_t fd;
  991                 struct filedesc *fdp = p->p_fd;
  992 
  993                 ksi->ksi_fd = -1;
  994                 for (fd = 0; fd < fdp->fd_nfiles; fd++) {
  995                         struct file *fp = fdp->fd_ofiles[fd];
  996                         /* XXX: lock? */
  997                         if (fp && fp->f_data == data) {
  998                                 ksi->ksi_fd = fd;
  999                                 break;
 1000                         }
 1001                 }
 1002         }
 1003         kpsignal2(p, ksi, dolock);
 1004 }
 1005 
 1006 static void 
 1007 kpsignal2(struct proc *p, const ksiginfo_t *ksi, int dolock)
 1008 {
 1009         struct lwp *l, *suspended = NULL;
 1010         struct sadata_vp *vp;
 1011         int     s = 0, prop, allsusp;
 1012         sig_t   action;
 1013         int     signum = ksi->ksi_signo;
 1014 
 1015 #ifdef DIAGNOSTIC
 1016         if (signum <= 0 || signum >= NSIG)
 1017                 panic("psignal signal number %d", signum);
 1018 
 1019         /* XXXSMP: works, but icky */
 1020         if (dolock)
 1021                 SCHED_ASSERT_UNLOCKED();
 1022         else
 1023                 SCHED_ASSERT_LOCKED();
 1024 #endif
 1025 
 1026         /*
 1027          * Notify any interested parties in the signal.
 1028          */
 1029         KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
 1030 
 1031         prop = sigprop[signum];
 1032 
 1033         /*
 1034          * If proc is traced, always give parent a chance.
 1035          */
 1036         if (p->p_flag & P_TRACED) {
 1037                 action = SIG_DFL;
 1038 
 1039                 /*
 1040                  * If the process is being traced and the signal is being
 1041                  * caught, make sure to save any ksiginfo.
 1042                  */
 1043                 if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
 1044                         ksiginfo_put(p, ksi);
 1045         } else {
 1046                 /*
 1047                  * If the signal was the result of a trap, reset it
 1048                  * to default action if it's currently masked, so that it would
 1049                  * coredump immediatelly instead of spinning repeatedly
 1050                  * taking the signal.
 1051                  */
 1052                 if (KSI_TRAP_P(ksi)
 1053                     && sigismember(&p->p_sigctx.ps_sigmask, signum)
 1054                     && !sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
 1055                         sigdelset(&p->p_sigctx.ps_sigignore, signum);
 1056                         sigdelset(&p->p_sigctx.ps_sigcatch, signum);
 1057                         sigdelset(&p->p_sigctx.ps_sigmask, signum);
 1058                         SIGACTION(p, signum).sa_handler = SIG_DFL;
 1059                 }
 1060 
 1061                 /*
 1062                  * If the signal is being ignored,
 1063                  * then we forget about it immediately.
 1064                  * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
 1065                  * and if it is set to SIG_IGN,
 1066                  * action will be SIG_DFL here.)
 1067                  */
 1068                 if (sigismember(&p->p_sigctx.ps_sigignore, signum))
 1069                         return;
 1070                 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
 1071                         action = SIG_HOLD;
 1072                 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
 1073                         action = SIG_CATCH;
 1074                 else {
 1075                         action = SIG_DFL;
 1076 
 1077                         if (prop & SA_KILL && p->p_nice > NZERO)
 1078                                 p->p_nice = NZERO;
 1079 
 1080                         /*
 1081                          * If sending a tty stop signal to a member of an
 1082                          * orphaned process group, discard the signal here if
 1083                          * the action is default; don't stop the process below
 1084                          * if sleeping, and don't clear any pending SIGCONT.
 1085                          */
 1086                         if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
 1087                                 return;
 1088                 }
 1089         }
 1090 
 1091         if (prop & SA_CONT)
 1092                 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
 1093 
 1094         if (prop & SA_STOP)
 1095                 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
 1096 
 1097         /*
 1098          * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
 1099          * please!), check if anything waits on it. If yes, save the
 1100          * info into provided ps_sigwaited, and wake-up the waiter.
 1101          * The signal won't be processed further here.
 1102          */
 1103         if ((prop & SA_CANTMASK) == 0
 1104             && p->p_sigctx.ps_sigwaited
 1105             && sigismember(p->p_sigctx.ps_sigwait, signum)
 1106             && p->p_stat != SSTOP) {
 1107                 p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info;
 1108                 p->p_sigctx.ps_sigwaited = NULL;
 1109                 if (dolock)
 1110                         wakeup_one(&p->p_sigctx.ps_sigwait);
 1111                 else
 1112                         sched_wakeup(&p->p_sigctx.ps_sigwait);
 1113                 return;
 1114         }
 1115 
 1116         sigaddset(&p->p_sigctx.ps_siglist, signum);
 1117 
 1118         /* CHECKSIGS() is "inlined" here. */
 1119         p->p_sigctx.ps_sigcheck = 1;
 1120 
 1121         /*
 1122          * Defer further processing for signals which are held,
 1123          * except that stopped processes must be continued by SIGCONT.
 1124          */
 1125         if (action == SIG_HOLD &&
 1126             ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) {
 1127                 ksiginfo_put(p, ksi);
 1128                 return;
 1129         }
 1130         /* XXXSMP: works, but icky */
 1131         if (dolock)
 1132                 SCHED_LOCK(s);
 1133 
 1134         if (p->p_flag & P_SA) {
 1135                 allsusp = 0;
 1136                 l = NULL;
 1137                 if (p->p_stat == SACTIVE) {
 1138                         SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
 1139                                 l = vp->savp_lwp;
 1140                                 KDASSERT(l != NULL);
 1141                                 if (l->l_flag & L_SA_IDLE) {
 1142                                         /* wakeup idle LWP */
 1143                                         goto found;
 1144                                         /*NOTREACHED*/
 1145                                 } else if (l->l_flag & L_SA_YIELD) {
 1146                                         /* idle LWP is already waking up */
 1147                                         goto out;
 1148                                         /*NOTREACHED*/
 1149                                 }
 1150                         }
 1151                         SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
 1152                                 l = vp->savp_lwp;
 1153                                 if (l->l_stat == LSRUN ||
 1154                                     l->l_stat == LSONPROC) {
 1155                                         signotify(p);
 1156                                         goto out;
 1157                                         /*NOTREACHED*/
 1158                                 }
 1159                                 if (l->l_stat == LSSLEEP && 
 1160                                     l->l_flag & L_SINTR) {
 1161                                         /* ok to signal vp lwp */
 1162                                         break;
 1163                                 } else
 1164                                         l = NULL;
 1165                         }
 1166                 } else if (p->p_stat == SSTOP) {
 1167                         SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
 1168                                 l = vp->savp_lwp;
 1169                                 if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0)
 1170                                         break;
 1171                                 l = NULL;
 1172                         }
 1173                 }
 1174         } else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) {
 1175                 /*
 1176                  * At least one LWP is running or on a run queue. 
 1177                  * The signal will be noticed when one of them returns 
 1178                  * to userspace.
 1179                  */
 1180                 signotify(p);
 1181                 /* 
 1182                  * The signal will be noticed very soon.
 1183                  */
 1184                 goto out;
 1185                 /*NOTREACHED*/
 1186         } else {
 1187                 /*
 1188                  * Find out if any of the sleeps are interruptable,
 1189                  * and if all the live LWPs remaining are suspended.
 1190                  */
 1191                 allsusp = 1;
 1192                 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
 1193                         if (l->l_stat == LSSLEEP && 
 1194                             l->l_flag & L_SINTR)
 1195                                 break;
 1196                         if (l->l_stat == LSSUSPENDED)
 1197                                 suspended = l;
 1198                         else if ((l->l_stat != LSZOMB) && 
 1199                             (l->l_stat != LSDEAD))
 1200                                 allsusp = 0;
 1201                 }
 1202         }
 1203 
 1204  found:
 1205         switch (p->p_stat) {
 1206         case SACTIVE:
 1207 
 1208                 if (l != NULL && (p->p_flag & P_TRACED))
 1209                         goto run;
 1210                              
 1211                 /*
 1212                  * If SIGCONT is default (or ignored) and process is
 1213                  * asleep, we are finished; the process should not
 1214                  * be awakened.
 1215                  */
 1216                 if ((prop & SA_CONT) && action == SIG_DFL) {
 1217                         sigdelset(&p->p_sigctx.ps_siglist, signum);
 1218                         goto done;
 1219                 }
 1220 
 1221                 /*
 1222                  * When a sleeping process receives a stop
 1223                  * signal, process immediately if possible.
 1224                  */
 1225                 if ((prop & SA_STOP) && action == SIG_DFL) {
 1226                         /*
 1227                          * If a child holding parent blocked,
 1228                          * stopping could cause deadlock.
 1229                          */
 1230                         if (p->p_flag & P_PPWAIT) {
 1231                                 goto out;
 1232                         }
 1233                         sigdelset(&p->p_sigctx.ps_siglist, signum);
 1234                         p->p_xstat = signum;
 1235                         if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
 1236                                 /*
 1237                                  * XXXSMP: recursive call; don't lock
 1238                                  * the second time around.
 1239                                  */
 1240                                 child_psignal(p, 0);
 1241                         }
 1242                         proc_stop(p, 1);        /* XXXSMP: recurse? */
 1243                         goto done;
 1244                 }
 1245 
 1246                 if (l == NULL) {
 1247                         /*
 1248                          * Special case: SIGKILL of a process
 1249                          * which is entirely composed of
 1250                          * suspended LWPs should succeed. We
 1251                          * make this happen by unsuspending one of
 1252                          * them.
 1253                          */
 1254                         if (allsusp && (signum == SIGKILL)) {
 1255                                 lwp_continue(suspended);
 1256                         }
 1257                         goto done;
 1258                 }
 1259                 /*
 1260                  * All other (caught or default) signals
 1261                  * cause the process to run.
 1262                  */
 1263                 goto runfast;
 1264                 /*NOTREACHED*/
 1265         case SSTOP:
 1266                 /* Process is stopped */
 1267                 /*
 1268                  * If traced process is already stopped,
 1269                  * then no further action is necessary.
 1270                  */
 1271                 if (p->p_flag & P_TRACED)
 1272                         goto done;
 1273 
 1274                 /*
 1275                  * Kill signal always sets processes running,
 1276                  * if possible.
 1277                  */
 1278                 if (signum == SIGKILL) {
 1279                         l = proc_unstop(p);
 1280                         if (l)
 1281                                 goto runfast;
 1282                         goto done;
 1283                 }
 1284                         
 1285                 if (prop & SA_CONT) {
 1286                         /*
 1287                          * If SIGCONT is default (or ignored),
 1288                          * we continue the process but don't
 1289                          * leave the signal in ps_siglist, as
 1290                          * it has no further action.  If
 1291                          * SIGCONT is held, we continue the
 1292                          * process and leave the signal in
 1293                          * ps_siglist.  If the process catches
 1294                          * SIGCONT, let it handle the signal
 1295                          * itself.  If it isn't waiting on an
 1296                          * event, then it goes back to run
 1297                          * state.  Otherwise, process goes
 1298                          * back to sleep state.  
 1299                          */
 1300                         if (action == SIG_DFL)
 1301                                 sigdelset(&p->p_sigctx.ps_siglist, 
 1302                                     signum);
 1303                         l = proc_unstop(p);
 1304                         if (l && (action == SIG_CATCH))
 1305                                 goto runfast;
 1306                         goto out;
 1307                 }
 1308 
 1309                 if (prop & SA_STOP) {
 1310                         /*
 1311                          * Already stopped, don't need to stop again.
 1312                          * (If we did the shell could get confused.)
 1313                          */
 1314                         sigdelset(&p->p_sigctx.ps_siglist, signum);
 1315                         goto done;
 1316                 }
 1317 
 1318                 /*
 1319                  * If a lwp is sleeping interruptibly, then
 1320                  * wake it up; it will run until the kernel
 1321                  * boundary, where it will stop in issignal(),
 1322                  * since p->p_stat is still SSTOP. When the
 1323                  * process is continued, it will be made
 1324                  * runnable and can look at the signal.
 1325                  */
 1326                 if (l)
 1327                         goto run;
 1328                 goto out;
 1329         case SIDL:
 1330                 /* Process is being created by fork */
 1331                 /* XXX: We are not ready to receive signals yet */
 1332                 goto done;
 1333         default:
 1334                 /* Else what? */
 1335                 panic("psignal: Invalid process state %d.", p->p_stat);
 1336         }
 1337         /*NOTREACHED*/
 1338 
 1339  runfast:
 1340         if (action == SIG_CATCH) {
 1341                 ksiginfo_put(p, ksi);
 1342                 action = SIG_HOLD;
 1343         }
 1344         /*
 1345          * Raise priority to at least PUSER.
 1346          */
 1347         if (l->l_priority > PUSER)
 1348                 l->l_priority = PUSER;
 1349  run:
 1350         if (action == SIG_CATCH) {
 1351                 ksiginfo_put(p, ksi);
 1352                 action = SIG_HOLD;
 1353         }
 1354         
 1355         setrunnable(l);         /* XXXSMP: recurse? */
 1356  out:
 1357         if (action == SIG_CATCH)
 1358                 ksiginfo_put(p, ksi);
 1359  done:
 1360         /* XXXSMP: works, but icky */
 1361         if (dolock)
 1362                 SCHED_UNLOCK(s);
 1363 }
 1364 
 1365 void
 1366 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
 1367 {
 1368         struct proc *p = l->l_proc;
 1369         struct lwp *le, *li;
 1370         siginfo_t *si;
 1371         int f;
 1372 
 1373         if (p->p_flag & P_SA) {
 1374 
 1375                 /* XXXUPSXXX What if not on sa_vp ? */
 1376 
 1377                 f = l->l_flag & L_SA;
 1378                 l->l_flag &= ~L_SA; 
 1379                 si = pool_get(&siginfo_pool, PR_WAITOK);
 1380                 si->_info = ksi->ksi_info;
 1381                 le = li = NULL;
 1382                 if (KSI_TRAP_P(ksi))
 1383                         le = l;
 1384                 else
 1385                         li = l;
 1386 
 1387                 sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li, 
 1388                             sizeof(siginfo_t), si);
 1389                 l->l_flag |= f;
 1390                 return;
 1391         }
 1392 
 1393         (*p->p_emul->e_sendsig)(ksi, mask);
 1394 }
 1395 
 1396 static __inline int firstsig(const sigset_t *);
 1397 
 1398 static __inline int
 1399 firstsig(const sigset_t *ss)
 1400 {
 1401         int sig;
 1402 
 1403         sig = ffs(ss->__bits[0]);
 1404         if (sig != 0)
 1405                 return (sig);
 1406 #if NSIG > 33
 1407         sig = ffs(ss->__bits[1]);
 1408         if (sig != 0)
 1409                 return (sig + 32);
 1410 #endif
 1411 #if NSIG > 65
 1412         sig = ffs(ss->__bits[2]);
 1413         if (sig != 0)
 1414                 return (sig + 64);
 1415 #endif
 1416 #if NSIG > 97
 1417         sig = ffs(ss->__bits[3]);
 1418         if (sig != 0)
 1419                 return (sig + 96);
 1420 #endif
 1421         return (0);
 1422 }
 1423 
 1424 /*
 1425  * If the current process has received a signal (should be caught or cause
 1426  * termination, should interrupt current syscall), return the signal number.
 1427  * Stop signals with default action are processed immediately, then cleared;
 1428  * they aren't returned.  This is checked after each entry to the system for
 1429  * a syscall or trap (though this can usually be done without calling issignal
 1430  * by checking the pending signal masks in the CURSIG macro.) The normal call
 1431  * sequence is
 1432  *
 1433  *      while (signum = CURSIG(curlwp))
 1434  *              postsig(signum);
 1435  */
 1436 int
 1437 issignal(struct lwp *l)
 1438 {
 1439         struct proc     *p = l->l_proc;
 1440         int             s = 0, signum, prop;
 1441         int             dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
 1442         sigset_t        ss;
 1443 
 1444         /* Bail out if we do not own the virtual processor */
 1445         if (l->l_flag & L_SA && l->l_savp->savp_lwp != l)
 1446                 return 0;
 1447 
 1448         if (p->p_stat == SSTOP) {
 1449                 /*
 1450                  * The process is stopped/stopping. Stop ourselves now that
 1451                  * we're on the kernel/userspace boundary.
 1452                  */
 1453                 if (dolock)
 1454                         SCHED_LOCK(s);
 1455                 l->l_stat = LSSTOP;
 1456                 p->p_nrlwps--;
 1457                 if (p->p_flag & P_TRACED)
 1458                         goto sigtraceswitch;
 1459                 else
 1460                         goto sigswitch;
 1461         }
 1462         for (;;) {
 1463                 sigpending1(p, &ss);
 1464                 if (p->p_flag & P_PPWAIT)
 1465                         sigminusset(&stopsigmask, &ss);
 1466                 signum = firstsig(&ss);
 1467                 if (signum == 0) {                      /* no signal to send */
 1468                         p->p_sigctx.ps_sigcheck = 0;
 1469                         if (locked && dolock)
 1470                                 SCHED_LOCK(s);
 1471                         return (0);
 1472                 }
 1473                                                         /* take the signal! */
 1474                 sigdelset(&p->p_sigctx.ps_siglist, signum);
 1475 
 1476                 /*
 1477                  * We should see pending but ignored signals
 1478                  * only if P_TRACED was on when they were posted.
 1479                  */
 1480                 if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
 1481                     (p->p_flag & P_TRACED) == 0)
 1482                         continue;
 1483 
 1484                 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
 1485                         /*
 1486                          * If traced, always stop, and stay
 1487                          * stopped until released by the debugger.
 1488                          */
 1489                         p->p_xstat = signum;
 1490 
 1491                         /* Emulation-specific handling of signal trace */
 1492                         if ((p->p_emul->e_tracesig != NULL) &&
 1493                             ((*p->p_emul->e_tracesig)(p, signum) != 0))
 1494                                 goto childresumed;
 1495 
 1496                         if ((p->p_flag & P_FSTRACE) == 0)
 1497                                 child_psignal(p, dolock);
 1498                         if (dolock)
 1499                                 SCHED_LOCK(s);
 1500                         proc_stop(p, 1);
 1501                 sigtraceswitch:
 1502                         mi_switch(l, NULL);
 1503                         SCHED_ASSERT_UNLOCKED();
 1504                         if (dolock)
 1505                                 splx(s);
 1506                         else
 1507                                 dolock = 1;
 1508 
 1509                 childresumed:
 1510                         /*
 1511                          * If we are no longer being traced, or the parent
 1512                          * didn't give us a signal, look for more signals.
 1513                          */
 1514                         if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
 1515                                 continue;
 1516 
 1517                         /*
 1518                          * If the new signal is being masked, look for other
 1519                          * signals.
 1520                          */
 1521                         signum = p->p_xstat;
 1522                         p->p_xstat = 0;
 1523                         /*
 1524                          * `p->p_sigctx.ps_siglist |= mask' is done
 1525                          * in setrunnable().
 1526                          */
 1527                         if (sigismember(&p->p_sigctx.ps_sigmask, signum))
 1528                                 continue;
 1529                                                         /* take the signal! */
 1530                         sigdelset(&p->p_sigctx.ps_siglist, signum);
 1531                 }
 1532 
 1533                 prop = sigprop[signum];
 1534 
 1535                 /*
 1536                  * Decide whether the signal should be returned.
 1537                  * Return the signal's number, or fall through
 1538                  * to clear it from the pending mask.
 1539                  */
 1540                 switch ((long)SIGACTION(p, signum).sa_handler) {
 1541 
 1542                 case (long)SIG_DFL:
 1543                         /*
 1544                          * Don't take default actions on system processes.
 1545                          */
 1546                         if (p->p_pid <= 1) {
 1547 #ifdef DIAGNOSTIC
 1548                                 /*
 1549                                  * Are you sure you want to ignore SIGSEGV
 1550                                  * in init? XXX
 1551                                  */
 1552                                 printf("Process (pid %d) got signal %d\n",
 1553                                     p->p_pid, signum);
 1554 #endif
 1555                                 break;          /* == ignore */
 1556                         }
 1557                         /*
 1558                          * If there is a pending stop signal to process
 1559                          * with default action, stop here,
 1560                          * then clear the signal.  However,
 1561                          * if process is member of an orphaned
 1562                          * process group, ignore tty stop signals.
 1563                          */
 1564                         if (prop & SA_STOP) {
 1565                                 if (p->p_flag & P_TRACED ||
 1566                                     (p->p_pgrp->pg_jobc == 0 &&
 1567                                     prop & SA_TTYSTOP))
 1568                                         break;  /* == ignore */
 1569                                 p->p_xstat = signum;
 1570                                 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
 1571                                         child_psignal(p, dolock);
 1572                                 if (dolock)
 1573                                         SCHED_LOCK(s);
 1574                                 proc_stop(p, 1);
 1575                         sigswitch:
 1576                                 mi_switch(l, NULL);
 1577                                 SCHED_ASSERT_UNLOCKED();
 1578                                 if (dolock)
 1579                                         splx(s);
 1580                                 else
 1581                                         dolock = 1;
 1582                                 break;
 1583                         } else if (prop & SA_IGNORE) {
 1584                                 /*
 1585                                  * Except for SIGCONT, shouldn't get here.
 1586                                  * Default action is to ignore; drop it.
 1587                                  */
 1588                                 break;          /* == ignore */
 1589                         } else
 1590                                 goto keep;
 1591                         /*NOTREACHED*/
 1592 
 1593                 case (long)SIG_IGN:
 1594                         /*
 1595                          * Masking above should prevent us ever trying
 1596                          * to take action on an ignored signal other
 1597                          * than SIGCONT, unless process is traced.
 1598                          */
 1599 #ifdef DEBUG_ISSIGNAL
 1600                         if ((prop & SA_CONT) == 0 &&
 1601                             (p->p_flag & P_TRACED) == 0)
 1602                                 printf("issignal\n");
 1603 #endif
 1604                         break;          /* == ignore */
 1605 
 1606                 default:
 1607                         /*
 1608                          * This signal has an action, let
 1609                          * postsig() process it.
 1610                          */
 1611                         goto keep;
 1612                 }
 1613         }
 1614         /* NOTREACHED */
 1615 
 1616  keep:
 1617                                                 /* leave the signal for later */
 1618         sigaddset(&p->p_sigctx.ps_siglist, signum);
 1619         CHECKSIGS(p);
 1620         if (locked && dolock)
 1621                 SCHED_LOCK(s);
 1622         return (signum);
 1623 }
 1624 
 1625 /*
 1626  * Put the argument process into the stopped state and notify the parent
 1627  * via wakeup.  Signals are handled elsewhere.  The process must not be
 1628  * on the run queue.
 1629  */
 1630 void
 1631 proc_stop(struct proc *p, int wakeup)
 1632 {
 1633         struct lwp *l;
 1634         struct proc *parent;
 1635         struct sadata_vp *vp;
 1636 
 1637         SCHED_ASSERT_LOCKED();
 1638 
 1639         /* XXX lock process LWP state */
 1640         p->p_flag &= ~P_WAITED;
 1641         p->p_stat = SSTOP;
 1642         parent = p->p_pptr;
 1643         parent->p_nstopchild++;
 1644 
 1645         if (p->p_flag & P_SA) {
 1646                 /*
 1647                  * Only (try to) put the LWP on the VP in stopped
 1648                  * state. 
 1649                  * All other LWPs will suspend in sa_setwoken()
 1650                  * because the VP-LWP in stopped state cannot be
 1651                  * repossessed.
 1652                  */
 1653                 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
 1654                         l = vp->savp_lwp;
 1655                         if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) {
 1656                                 l->l_stat = LSSTOP;
 1657                                 p->p_nrlwps--;
 1658                         } else if (l->l_stat == LSRUN) {
 1659                                 /* Remove LWP from the run queue */
 1660                                 remrunqueue(l);
 1661                                 l->l_stat = LSSTOP;
 1662                                 p->p_nrlwps--;
 1663                         } else if (l->l_stat == LSSLEEP &&
 1664                             l->l_flag & L_SA_IDLE) {
 1665                                 l->l_flag &= ~L_SA_IDLE;
 1666                                 l->l_stat = LSSTOP;
 1667                         }
 1668                 }
 1669                 goto out;
 1670         }
 1671 
 1672         /* 
 1673          * Put as many LWP's as possible in stopped state. 
 1674          * Sleeping ones will notice the stopped state as they try to
 1675          * return to userspace.
 1676          */
 1677 
 1678         LIST_FOREACH(l, &p->p_lwps, l_sibling) {
 1679                 if (l->l_stat == LSONPROC) {
 1680                         /* XXX SMP this assumes that a LWP that is LSONPROC
 1681                          * is curlwp and hence is about to be mi_switched 
 1682                          * away; the only callers of proc_stop() are:
 1683                          * - psignal
 1684                          * - issignal()
 1685                          * For the former, proc_stop() is only called when
 1686                          * no processes are running, so we don't worry.
 1687                          * For the latter, proc_stop() is called right
 1688                          * before mi_switch().
 1689                          */
 1690                         l->l_stat = LSSTOP;
 1691                         p->p_nrlwps--;
 1692                 } else if (l->l_stat == LSRUN) {
 1693                         /* Remove LWP from the run queue */
 1694                         remrunqueue(l);
 1695                         l->l_stat = LSSTOP;
 1696                         p->p_nrlwps--;
 1697                 } else if ((l->l_stat == LSSLEEP) ||
 1698                     (l->l_stat == LSSUSPENDED) || 
 1699                     (l->l_stat == LSZOMB) ||
 1700                     (l->l_stat == LSDEAD)) {
 1701                         /*
 1702                          * Don't do anything; let sleeping LWPs
 1703                          * discover the stopped state of the process
 1704                          * on their way out of the kernel; otherwise,
 1705                          * things like NFS threads that sleep with
 1706                          * locks will block the rest of the system
 1707                          * from getting any work done.
 1708                          *
 1709                          * Suspended/dead/zombie LWPs aren't going
 1710                          * anywhere, so we don't need to touch them.
 1711                          */
 1712                 }
 1713 #ifdef DIAGNOSTIC
 1714                 else {
 1715                         panic("proc_stop: process %d lwp %d "
 1716                               "in unstoppable state %d.\n", 
 1717                             p->p_pid, l->l_lid, l->l_stat);
 1718                 }
 1719 #endif
 1720         }
 1721 
 1722  out:
 1723         /* XXX unlock process LWP state */
 1724 
 1725         if (wakeup)
 1726                 sched_wakeup((caddr_t)p->p_pptr);
 1727 }
 1728 
 1729 /*
 1730  * Given a process in state SSTOP, set the state back to SACTIVE and 
 1731  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
 1732  *
 1733  * If no LWPs ended up runnable (and therefore able to take a signal),
 1734  * return a LWP that is sleeping interruptably. The caller can wake
 1735  * that LWP up to take a signal.
 1736  */
 1737 struct lwp *
 1738 proc_unstop(struct proc *p)
 1739 {
 1740         struct lwp *l, *lr = NULL;
 1741         struct sadata_vp *vp;
 1742         int cantake = 0;
 1743 
 1744         SCHED_ASSERT_LOCKED();
 1745 
 1746         /*
 1747          * Our caller wants to be informed if there are only sleeping
 1748          * and interruptable LWPs left after we have run so that it
 1749          * can invoke setrunnable() if required - return one of the
 1750          * interruptable LWPs if this is the case.
 1751          */
 1752 
 1753         if (!(p->p_flag & P_WAITED))
 1754                 p->p_pptr->p_nstopchild--;
 1755         p->p_stat = SACTIVE;
 1756         LIST_FOREACH(l, &p->p_lwps, l_sibling) {
 1757                 if (l->l_stat == LSRUN) {
 1758                         lr = NULL;
 1759                         cantake = 1;
 1760                 }
 1761                 if (l->l_stat != LSSTOP)
 1762                         continue;
 1763 
 1764                 if (l->l_wchan != NULL) {
 1765                         l->l_stat = LSSLEEP;
 1766                         if ((cantake == 0) && (l->l_flag & L_SINTR)) {
 1767                                 lr = l;
 1768                                 cantake = 1;
 1769                         }
 1770                 } else {
 1771                         setrunnable(l);
 1772                         lr = NULL;
 1773                         cantake = 1;
 1774                 }
 1775         }
 1776         if (p->p_flag & P_SA) {
 1777                 /* Only consider returning the LWP on the VP. */
 1778                 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
 1779                         lr = vp->savp_lwp;
 1780                         if (lr->l_stat == LSSLEEP) {
 1781                                 if (lr->l_flag & L_SA_YIELD) {
 1782                                         setrunnable(lr);
 1783                                         break;
 1784                                 } else if (lr->l_flag & L_SINTR)
 1785                                         return lr;
 1786                         }
 1787                 }
 1788                 return NULL;
 1789         }
 1790         return lr;
 1791 }
 1792 
 1793 /*
 1794  * Take the action for the specified signal
 1795  * from the current set of pending signals.
 1796  */
 1797 void
 1798 postsig(int signum)
 1799 {
 1800         struct lwp *l;
 1801         struct proc     *p;
 1802         struct sigacts  *ps;
 1803         sig_t           action;
 1804         sigset_t        *returnmask;
 1805 
 1806         l = curlwp;
 1807         p = l->l_proc;
 1808         ps = p->p_sigacts;
 1809 #ifdef DIAGNOSTIC
 1810         if (signum == 0)
 1811                 panic("postsig");
 1812 #endif
 1813 
 1814         KERNEL_PROC_LOCK(l);
 1815 
 1816 #ifdef MULTIPROCESSOR
 1817         /*
 1818          * On MP, issignal() can return the same signal to multiple
 1819          * LWPs.  The LWPs will block above waiting for the kernel
 1820          * lock and the first LWP which gets through will then remove
 1821          * the signal from ps_siglist.  All other LWPs exit here.
 1822          */
 1823         if (!sigismember(&p->p_sigctx.ps_siglist, signum)) {
 1824                 KERNEL_PROC_UNLOCK(l);
 1825                 return;
 1826         }
 1827 #endif
 1828         sigdelset(&p->p_sigctx.ps_siglist, signum);
 1829         action = SIGACTION_PS(ps, signum).sa_handler;
 1830         if (action == SIG_DFL) {
 1831 #ifdef KTRACE
 1832                 if (KTRPOINT(p, KTR_PSIG))
 1833                         ktrpsig(p, signum, action,
 1834                             p->p_sigctx.ps_flags & SAS_OLDMASK ?
 1835                             &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
 1836                             NULL);
 1837 #endif
 1838                 /*
 1839                  * Default action, where the default is to kill
 1840                  * the process.  (Other cases were ignored above.)
 1841                  */
 1842                 sigexit(l, signum);
 1843                 /* NOTREACHED */
 1844         } else {
 1845                 ksiginfo_t *ksi;
 1846                 /*
 1847                  * If we get here, the signal must be caught.
 1848                  */
 1849 #ifdef DIAGNOSTIC
 1850                 if (action == SIG_IGN ||
 1851                     sigismember(&p->p_sigctx.ps_sigmask, signum))
 1852                         panic("postsig action");
 1853 #endif
 1854                 /*
 1855                  * Set the new mask value and also defer further
 1856                  * occurrences of this signal.
 1857                  *
 1858                  * Special case: user has done a sigpause.  Here the
 1859                  * current mask is not of interest, but rather the
 1860                  * mask from before the sigpause is what we want
 1861                  * restored after the signal processing is completed.
 1862                  */
 1863                 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
 1864                         returnmask = &p->p_sigctx.ps_oldmask;
 1865                         p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
 1866                 } else
 1867                         returnmask = &p->p_sigctx.ps_sigmask;
 1868                 p->p_stats->p_ru.ru_nsignals++;
 1869                 ksi = ksiginfo_get(p, signum);
 1870 #ifdef KTRACE
 1871                 if (KTRPOINT(p, KTR_PSIG))
 1872                         ktrpsig(p, signum, action,
 1873                             p->p_sigctx.ps_flags & SAS_OLDMASK ?
 1874                             &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
 1875                             ksi);
 1876 #endif
 1877                 if (ksi == NULL) {
 1878                         ksiginfo_t ksi1;
 1879                         /*
 1880                          * we did not save any siginfo for this, either
 1881                          * because the signal was not caught, or because the
 1882                          * user did not request SA_SIGINFO
 1883                          */
 1884                         KSI_INIT_EMPTY(&ksi1);
 1885                         ksi1.ksi_signo = signum;
 1886                         kpsendsig(l, &ksi1, returnmask);
 1887                 } else {
 1888                         kpsendsig(l, ksi, returnmask);
 1889                         pool_put(&ksiginfo_pool, ksi);
 1890                 }
 1891                 p->p_sigctx.ps_lwp = 0;
 1892                 p->p_sigctx.ps_code = 0;
 1893                 p->p_sigctx.ps_signo = 0;
 1894                 (void) splsched();      /* XXXSMP */
 1895                 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
 1896                     &p->p_sigctx.ps_sigmask);
 1897                 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
 1898                         sigdelset(&p->p_sigctx.ps_sigcatch, signum);
 1899                         if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
 1900                                 sigaddset(&p->p_sigctx.ps_sigignore, signum);
 1901                         SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
 1902                 }
 1903                 (void) spl0();          /* XXXSMP */
 1904         }
 1905 
 1906         KERNEL_PROC_UNLOCK(l);
 1907 }
 1908 
 1909 /*
 1910  * Kill the current process for stated reason.
 1911  */
 1912 void
 1913 killproc(struct proc *p, const char *why)
 1914 {
 1915         log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
 1916         uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
 1917         psignal(p, SIGKILL);
 1918 }
 1919 
 1920 /*
 1921  * Force the current process to exit with the specified signal, dumping core
 1922  * if appropriate.  We bypass the normal tests for masked and caught signals,
 1923  * allowing unrecoverable failures to terminate the process without changing
 1924  * signal state.  Mark the accounting record with the signal termination.
 1925  * If dumping core, save the signal number for the debugger.  Calls exit and
 1926  * does not return.
 1927  */
 1928 
 1929 #if defined(DEBUG)
 1930 int     kern_logsigexit = 1;    /* not static to make public for sysctl */
 1931 #else
 1932 int     kern_logsigexit = 0;    /* not static to make public for sysctl */
 1933 #endif
 1934 
 1935 static  const char logcoredump[] =
 1936         "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
 1937 static  const char lognocoredump[] =
 1938         "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
 1939 
 1940 /* Wrapper function for use in p_userret */
 1941 static void
 1942 lwp_coredump_hook(struct lwp *l, void *arg)
 1943 {
 1944         int s;
 1945 
 1946         /*
 1947          * Suspend ourselves, so that the kernel stack and therefore
 1948          * the userland registers saved in the trapframe are around
 1949          * for coredump() to write them out.
 1950          */
 1951         KERNEL_PROC_LOCK(l);
 1952         l->l_flag &= ~L_DETACHED;
 1953         SCHED_LOCK(s);
 1954         l->l_stat = LSSUSPENDED;
 1955         l->l_proc->p_nrlwps--;
 1956         /* XXX NJWLWP check if this makes sense here: */
 1957         l->l_proc->p_stats->p_ru.ru_nvcsw++; 
 1958         mi_switch(l, NULL);
 1959         SCHED_ASSERT_UNLOCKED();
 1960         splx(s);
 1961 
 1962         lwp_exit(l);
 1963 }
 1964 
 1965 void
 1966 sigexit(struct lwp *l, int signum)
 1967 {
 1968         struct proc     *p;
 1969 #if 0
 1970         struct lwp      *l2;
 1971 #endif
 1972         int             error, exitsig;
 1973 
 1974         p = l->l_proc;
 1975 
 1976         /*
 1977          * Don't permit coredump() or exit1() multiple times 
 1978          * in the same process.
 1979          */
 1980         if (p->p_flag & P_WEXIT) {
 1981                 KERNEL_PROC_UNLOCK(l);
 1982                 (*p->p_userret)(l, p->p_userret_arg);
 1983         }
 1984         p->p_flag |= P_WEXIT;
 1985         /* We don't want to switch away from exiting. */
 1986         /* XXX multiprocessor: stop LWPs on other processors. */
 1987 #if 0
 1988         if (p->p_flag & P_SA) {
 1989                 LIST_FOREACH(l2, &p->p_lwps, l_sibling)
 1990                     l2->l_flag &= ~L_SA;
 1991                 p->p_flag &= ~P_SA;
 1992         }
 1993 #endif
 1994 
 1995         /* Make other LWPs stick around long enough to be dumped */
 1996         p->p_userret = lwp_coredump_hook;
 1997         p->p_userret_arg = NULL;
 1998 
 1999         exitsig = signum;
 2000         p->p_acflag |= AXSIG;
 2001         if (sigprop[signum] & SA_CORE) {
 2002                 p->p_sigctx.ps_signo = signum;
 2003                 if ((error = coredump(l)) == 0)
 2004                         exitsig |= WCOREFLAG;
 2005 
 2006                 if (kern_logsigexit) {
 2007                         /* XXX What if we ever have really large UIDs? */
 2008                         int uid = p->p_cred && p->p_ucred ? 
 2009                                 (int) p->p_ucred->cr_uid : -1;
 2010 
 2011                         if (error) 
 2012                                 log(LOG_INFO, lognocoredump, p->p_pid,
 2013                                     p->p_comm, uid, signum, error);
 2014                         else
 2015                                 log(LOG_INFO, logcoredump, p->p_pid,
 2016                                     p->p_comm, uid, signum);
 2017                 }
 2018 
 2019         }
 2020 
 2021         exit1(l, W_EXITCODE(0, exitsig));
 2022         /* NOTREACHED */
 2023 }
 2024 
 2025 /*
 2026  * Dump core, into a file named "progname.core" or "core" (depending on the 
 2027  * value of shortcorename), unless the process was setuid/setgid.
 2028  */
 2029 int
 2030 coredump(struct lwp *l)
 2031 {
 2032         struct vnode            *vp;
 2033         struct proc             *p;
 2034         struct vmspace          *vm;
 2035         struct ucred            *cred;
 2036         struct nameidata        nd;
 2037         struct vattr            vattr;
 2038         struct mount            *mp;
 2039         int                     error, error1;
 2040         char                    name[MAXPATHLEN];
 2041 
 2042         p = l->l_proc;
 2043         vm = p->p_vmspace;
 2044         cred = p->p_cred->pc_ucred;
 2045 
 2046         /*
 2047          * Make sure the process has not set-id, to prevent data leaks.
 2048          */
 2049         if (p->p_flag & P_SUGID)
 2050                 return (EPERM);
 2051 
 2052         /*
 2053          * Refuse to core if the data + stack + user size is larger than
 2054          * the core dump limit.  XXX THIS IS WRONG, because of mapped
 2055          * data.
 2056          */
 2057         if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
 2058             p->p_rlimit[RLIMIT_CORE].rlim_cur)
 2059                 return (EFBIG);         /* better error code? */
 2060 
 2061 restart:
 2062         /*
 2063          * The core dump will go in the current working directory.  Make
 2064          * sure that the directory is still there and that the mount flags
 2065          * allow us to write core dumps there.
 2066          */
 2067         vp = p->p_cwdi->cwdi_cdir;
 2068         if (vp->v_mount == NULL ||
 2069             (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
 2070                 return (EPERM);
 2071 
 2072         error = build_corename(p, name);
 2073         if (error)
 2074                 return error;
 2075 
 2076         NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
 2077         error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
 2078         if (error)
 2079                 return (error);
 2080         vp = nd.ni_vp;
 2081 
 2082         if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
 2083                 VOP_UNLOCK(vp, 0);
 2084                 if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
 2085                         return (error);
 2086                 if ((error = vn_start_write(NULL, &mp,
 2087                     V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
 2088                         return (error);
 2089                 goto restart;
 2090         }
 2091 
 2092         /* Don't dump to non-regular files or files with links. */
 2093         if (vp->v_type != VREG ||
 2094             VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
 2095                 error = EINVAL;
 2096                 goto out;
 2097         }
 2098         VATTR_NULL(&vattr);
 2099         vattr.va_size = 0;
 2100         VOP_LEASE(vp, p, cred, LEASE_WRITE);
 2101         VOP_SETATTR(vp, &vattr, cred, p);
 2102         p->p_acflag |= ACORE;
 2103 
 2104         /* Now dump the actual core file. */
 2105         error = (*p->p_execsw->es_coredump)(l, vp, cred);
 2106  out:
 2107         VOP_UNLOCK(vp, 0);
 2108         vn_finished_write(mp, 0);
 2109         error1 = vn_close(vp, FWRITE, cred, p);
 2110         if (error == 0)
 2111                 error = error1;
 2112         return (error);
 2113 }
 2114 
 2115 /*
 2116  * Nonexistent system call-- signal process (may want to handle it).
 2117  * Flag error in case process won't see signal immediately (blocked or ignored).
 2118  */
 2119 /* ARGSUSED */
 2120 int
 2121 sys_nosys(struct lwp *l, void *v, register_t *retval)
 2122 {
 2123         struct proc     *p;
 2124 
 2125         p = l->l_proc;
 2126         psignal(p, SIGSYS);
 2127         return (ENOSYS);
 2128 }
 2129 
 2130 static int
 2131 build_corename(struct proc *p, char dst[MAXPATHLEN])
 2132 {
 2133         const char      *s;
 2134         char            *d, *end;
 2135         int             i;
 2136         
 2137         for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
 2138             *s != '\0'; s++) {
 2139                 if (*s == '%') {
 2140                         switch (*(s + 1)) {
 2141                         case 'n':
 2142                                 i = snprintf(d, end - d, "%s", p->p_comm);
 2143                                 break;
 2144                         case 'p':
 2145                                 i = snprintf(d, end - d, "%d", p->p_pid);
 2146                                 break;
 2147                         case 'u':
 2148                                 i = snprintf(d, end - d, "%.*s",
 2149                                     (int)sizeof p->p_pgrp->pg_session->s_login,
 2150                                     p->p_pgrp->pg_session->s_login);
 2151                                 break;
 2152                         case 't':
 2153                                 i = snprintf(d, end - d, "%ld",
 2154                                     p->p_stats->p_start.tv_sec);
 2155                                 break;
 2156                         default:
 2157                                 goto copy;
 2158                         }
 2159                         d += i;
 2160                         s++;
 2161                 } else {
 2162  copy:                  *d = *s;
 2163                         d++;
 2164                 }
 2165                 if (d >= end)
 2166                         return (ENAMETOOLONG);
 2167         }
 2168         *d = '\0';
 2169         return 0;
 2170 }
 2171 
 2172 void
 2173 getucontext(struct lwp *l, ucontext_t *ucp)
 2174 {
 2175         struct proc     *p;
 2176 
 2177         p = l->l_proc;
 2178 
 2179         ucp->uc_flags = 0;
 2180         ucp->uc_link = l->l_ctxlink;
 2181 
 2182         (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
 2183         ucp->uc_flags |= _UC_SIGMASK;
 2184 
 2185         /*
 2186          * The (unsupplied) definition of the `current execution stack'
 2187          * in the System V Interface Definition appears to allow returning
 2188          * the main context stack.
 2189          */
 2190         if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
 2191                 ucp->uc_stack.ss_sp = (void *)USRSTACK;
 2192                 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
 2193                 ucp->uc_stack.ss_flags = 0;     /* XXX, def. is Very Fishy */
 2194         } else {
 2195                 /* Simply copy alternate signal execution stack. */
 2196                 ucp->uc_stack = p->p_sigctx.ps_sigstk;
 2197         }
 2198         ucp->uc_flags |= _UC_STACK;
 2199 
 2200         cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
 2201 }
 2202 
 2203 /* ARGSUSED */
 2204 int
 2205 sys_getcontext(struct lwp *l, void *v, register_t *retval)
 2206 {
 2207         struct sys_getcontext_args /* {
 2208                 syscallarg(struct __ucontext *) ucp;
 2209         } */ *uap = v;
 2210         ucontext_t uc;
 2211 
 2212         getucontext(l, &uc);
 2213 
 2214         return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
 2215 }
 2216 
 2217 int
 2218 setucontext(struct lwp *l, const ucontext_t *ucp)
 2219 {
 2220         struct proc     *p;
 2221         int             error;
 2222 
 2223         p = l->l_proc;
 2224         if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
 2225                 return (error);
 2226         l->l_ctxlink = ucp->uc_link;
 2227 
 2228         if ((ucp->uc_flags & _UC_SIGMASK) != 0)
 2229                 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
 2230 
 2231         /*
 2232          * If there was stack information, update whether or not we are
 2233          * still running on an alternate signal stack.
 2234          */
 2235         if ((ucp->uc_flags & _UC_STACK) != 0) {
 2236                 if (ucp->uc_stack.ss_flags & SS_ONSTACK)
 2237                         p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
 2238                 else
 2239                         p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
 2240         }
 2241 
 2242         return 0;
 2243 }
 2244 
 2245 /* ARGSUSED */
 2246 int
 2247 sys_setcontext(struct lwp *l, void *v, register_t *retval)
 2248 {
 2249         struct sys_setcontext_args /* {
 2250                 syscallarg(const ucontext_t *) ucp;
 2251         } */ *uap = v;
 2252         ucontext_t uc;
 2253         int error;
 2254 
 2255         if (SCARG(uap, ucp) == NULL)    /* i.e. end of uc_link chain */
 2256                 exit1(l, W_EXITCODE(0, 0));
 2257         else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
 2258             (error = setucontext(l, &uc)) != 0)
 2259                 return (error);
 2260 
 2261         return (EJUSTRETURN);
 2262 }
 2263 
 2264 /*
 2265  * sigtimedwait(2) system call, used also for implementation
 2266  * of sigwaitinfo() and sigwait().
 2267  *
 2268  * This only handles single LWP in signal wait. libpthread provides
 2269  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
 2270  */
 2271 int
 2272 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
 2273 {
 2274         struct sys___sigtimedwait_args /* {
 2275                 syscallarg(const sigset_t *) set;
 2276                 syscallarg(siginfo_t *) info;
 2277                 syscallarg(struct timespec *) timeout;
 2278         } */ *uap = v;
 2279         sigset_t *waitset, twaitset;
 2280         struct proc *p = l->l_proc;
 2281         int error, signum, s;
 2282         int timo = 0;
 2283         struct timeval tvstart;
 2284         struct timespec ts;
 2285         ksiginfo_t *ksi;
 2286 
 2287         MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
 2288 
 2289         if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
 2290                 FREE(waitset, M_TEMP);
 2291                 return (error);
 2292         }
 2293 
 2294         /*
 2295          * Silently ignore SA_CANTMASK signals. psignal1() would
 2296          * ignore SA_CANTMASK signals in waitset, we do this
 2297          * only for the below siglist check.
 2298          */
 2299         sigminusset(&sigcantmask, waitset);
 2300 
 2301         /*
 2302          * First scan siglist and check if there is signal from
 2303          * our waitset already pending.
 2304          */
 2305         twaitset = *waitset;
 2306         __sigandset(&p->p_sigctx.ps_siglist, &twaitset);
 2307         if ((signum = firstsig(&twaitset))) {
 2308                 /* found pending signal */
 2309                 sigdelset(&p->p_sigctx.ps_siglist, signum);
 2310                 ksi = ksiginfo_get(p, signum);
 2311                 if (!ksi) {
 2312                         /* No queued siginfo, manufacture one */
 2313                         ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
 2314                         KSI_INIT(ksi);
 2315                         ksi->ksi_info._signo = signum;
 2316                         ksi->ksi_info._code = SI_USER;
 2317                 }
 2318                         
 2319                 goto sig;
 2320         }
 2321 
 2322         /*
 2323          * Calculate timeout, if it was specified.
 2324          */
 2325         if (SCARG(uap, timeout)) {
 2326                 uint64_t ms;
 2327 
 2328                 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
 2329                         return (error);
 2330 
 2331                 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
 2332                 timo = mstohz(ms);
 2333                 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
 2334                         timo = 1;
 2335                 if (timo <= 0)
 2336                         return (EAGAIN);
 2337 
 2338                 /*
 2339                  * Remember current mono_time, it would be used in
 2340                  * ECANCELED/ERESTART case.
 2341                  */
 2342                 s = splclock();
 2343                 tvstart = mono_time;
 2344                 splx(s);
 2345         }
 2346 
 2347         /*
 2348          * Setup ps_sigwait list. Pass pointer to malloced memory
 2349          * here; it's not possible to pass pointer to a structure
 2350          * on current process's stack, the current process might
 2351          * be swapped out at the time the signal would get delivered.
 2352          */
 2353         ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
 2354         p->p_sigctx.ps_sigwaited = ksi;
 2355         p->p_sigctx.ps_sigwait = waitset;
 2356 
 2357         /*
 2358          * Wait for signal to arrive. We can either be woken up or
 2359          * time out.
 2360          */
 2361         error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
 2362 
 2363         /*
 2364          * Need to find out if we woke as a result of lwp_wakeup()
 2365          * or a signal outside our wait set.
 2366          */
 2367         if (error == EINTR && p->p_sigctx.ps_sigwaited
 2368             && !firstsig(&p->p_sigctx.ps_siglist)) {
 2369                 /* wakeup via _lwp_wakeup() */
 2370                 error = ECANCELED;
 2371         } else if (!error && p->p_sigctx.ps_sigwaited) {
 2372                 /* spurious wakeup - arrange for syscall restart */
 2373                 error = ERESTART;
 2374                 goto fail;
 2375         }
 2376 
 2377         /*
 2378          * On error, clear sigwait indication. psignal1() clears it
 2379          * in !error case.
 2380          */
 2381         if (error) {
 2382                 p->p_sigctx.ps_sigwaited = NULL;
 2383 
 2384                 /*
 2385                  * If the sleep was interrupted (either by signal or wakeup),
 2386                  * update the timeout and copyout new value back.
 2387                  * It would be used when the syscall would be restarted
 2388                  * or called again.
 2389                  */
 2390                 if (timo && (error == ERESTART || error == ECANCELED)) {
 2391                         struct timeval tvnow, tvtimo;
 2392                         int err;
 2393 
 2394                         s = splclock();
 2395                         tvnow = mono_time;
 2396                         splx(s);
 2397 
 2398                         TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
 2399 
 2400                         /* compute how much time has passed since start */
 2401                         timersub(&tvnow, &tvstart, &tvnow);
 2402                         /* substract passed time from timeout */
 2403                         timersub(&tvtimo, &tvnow, &tvtimo);
 2404 
 2405                         if (tvtimo.tv_sec < 0) {
 2406                                 error = EAGAIN;
 2407                                 goto fail;
 2408                         }
 2409 
 2410                         TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
 2411 
 2412                         /* copy updated timeout to userland */
 2413                         if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts)))) {
 2414                                 error = err;
 2415                                 goto fail;
 2416                         }
 2417                 }
 2418 
 2419                 goto fail;
 2420         }
 2421 
 2422         /*
 2423          * If a signal from the wait set arrived, copy it to userland.
 2424          * Copy only the used part of siginfo, the padding part is
 2425          * left unchanged (userland is not supposed to touch it anyway).
 2426          */
 2427  sig:
 2428         error = copyout(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info));
 2429 
 2430  fail:
 2431         FREE(waitset, M_TEMP);
 2432         pool_put(&ksiginfo_pool, ksi);
 2433         p->p_sigctx.ps_sigwait = NULL;
 2434 
 2435         return (error);
 2436 }
 2437 
 2438 /*
 2439  * Returns true if signal is ignored or masked for passed process.
 2440  */
 2441 int
 2442 sigismasked(struct proc *p, int sig)
 2443 {
 2444 
 2445         return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
 2446             sigismember(&p->p_sigctx.ps_sigmask, sig));
 2447 }
 2448 
 2449 static int
 2450 filt_sigattach(struct knote *kn)
 2451 {
 2452         struct proc *p = curproc;
 2453 
 2454         kn->kn_ptr.p_proc = p;
 2455         kn->kn_flags |= EV_CLEAR;               /* automatically set */
 2456 
 2457         SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
 2458 
 2459         return (0);
 2460 }
 2461 
 2462 static void
 2463 filt_sigdetach(struct knote *kn)
 2464 {
 2465         struct proc *p = kn->kn_ptr.p_proc;
 2466 
 2467         SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
 2468 }
 2469 
 2470 /*
 2471  * signal knotes are shared with proc knotes, so we apply a mask to
 2472  * the hint in order to differentiate them from process hints.  This
 2473  * could be avoided by using a signal-specific knote list, but probably
 2474  * isn't worth the trouble.
 2475  */
 2476 static int
 2477 filt_signal(struct knote *kn, long hint)
 2478 {
 2479 
 2480         if (hint & NOTE_SIGNAL) {
 2481                 hint &= ~NOTE_SIGNAL;
 2482 
 2483                 if (kn->kn_id == hint)
 2484                         kn->kn_data++;
 2485         }
 2486         return (kn->kn_data != 0);
 2487 }
 2488 
 2489 const struct filterops sig_filtops = {
 2490         0, filt_sigattach, filt_sigdetach, filt_signal
 2491 };

Cache object: a5833f81fbd90098f95f5f6c98056fe1


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