The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_sig.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: kern_sig.c,v 1.404 2022/04/09 23:38:33 riastradh Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Andrew Doran.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   29  * POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 
   32 /*
   33  * Copyright (c) 1982, 1986, 1989, 1991, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  * (c) UNIX System Laboratories, Inc.
   36  * All or some portions of this file are derived from material licensed
   37  * to the University of California by American Telephone and Telegraph
   38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   39  * the permission of UNIX System Laboratories, Inc.
   40  *
   41  * Redistribution and use in source and binary forms, with or without
   42  * modification, are permitted provided that the following conditions
   43  * are met:
   44  * 1. Redistributions of source code must retain the above copyright
   45  *    notice, this list of conditions and the following disclaimer.
   46  * 2. Redistributions in binary form must reproduce the above copyright
   47  *    notice, this list of conditions and the following disclaimer in the
   48  *    documentation and/or other materials provided with the distribution.
   49  * 3. Neither the name of the University nor the names of its contributors
   50  *    may be used to endorse or promote products derived from this software
   51  *    without specific prior written permission.
   52  *
   53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   63  * SUCH DAMAGE.
   64  *
   65  *      @(#)kern_sig.c  8.14 (Berkeley) 5/14/95
   66  */
   67 
   68 /*
   69  * Signal subsystem.
   70  */
   71 
   72 #include <sys/cdefs.h>
   73 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.404 2022/04/09 23:38:33 riastradh Exp $");
   74 
   75 #include "opt_execfmt.h"
   76 #include "opt_ptrace.h"
   77 #include "opt_dtrace.h"
   78 #include "opt_compat_sunos.h"
   79 #include "opt_compat_netbsd.h"
   80 #include "opt_compat_netbsd32.h"
   81 #include "opt_pax.h"
   82 
   83 #define SIGPROP         /* include signal properties table */
   84 #include <sys/param.h>
   85 #include <sys/signalvar.h>
   86 #include <sys/proc.h>
   87 #include <sys/ptrace.h>
   88 #include <sys/systm.h>
   89 #include <sys/wait.h>
   90 #include <sys/ktrace.h>
   91 #include <sys/syslog.h>
   92 #include <sys/filedesc.h>
   93 #include <sys/file.h>
   94 #include <sys/pool.h>
   95 #include <sys/ucontext.h>
   96 #include <sys/exec.h>
   97 #include <sys/kauth.h>
   98 #include <sys/acct.h>
   99 #include <sys/callout.h>
  100 #include <sys/atomic.h>
  101 #include <sys/cpu.h>
  102 #include <sys/module.h>
  103 #include <sys/sdt.h>
  104 #include <sys/exec_elf.h>
  105 #include <sys/compat_stub.h>
  106 
  107 #ifdef PAX_SEGVGUARD
  108 #include <sys/pax.h>
  109 #endif /* PAX_SEGVGUARD */
  110 
  111 #include <uvm/uvm_extern.h>
  112 
  113 /* Many hard-coded assumptions that there are <= 4 x 32bit signal mask bits */
  114 __CTASSERT(NSIG <= 128);
  115 
  116 #define SIGQUEUE_MAX    32
  117 static pool_cache_t     sigacts_cache   __read_mostly;
  118 static pool_cache_t     ksiginfo_cache  __read_mostly;
  119 static callout_t        proc_stop_ch    __cacheline_aligned;
  120 
  121 sigset_t                contsigmask     __cacheline_aligned;
  122 sigset_t                stopsigmask     __cacheline_aligned;
  123 static sigset_t         vforksigmask    __cacheline_aligned;
  124 sigset_t                sigcantmask     __cacheline_aligned;
  125 
  126 static void     ksiginfo_exechook(struct proc *, void *);
  127 static void     proc_stop(struct proc *, int);
  128 static void     proc_stop_done(struct proc *, int);
  129 static void     proc_stop_callout(void *);
  130 static int      sigchecktrace(void);
  131 static int      sigpost(struct lwp *, sig_t, int, int);
  132 static int      sigput(sigpend_t *, struct proc *, ksiginfo_t *);
  133 static int      sigunwait(struct proc *, const ksiginfo_t *);
  134 static void     sigswitch(int, int, bool);
  135 static void     sigswitch_unlock_and_switch_away(struct lwp *);
  136 
  137 static void     sigacts_poolpage_free(struct pool *, void *);
  138 static void     *sigacts_poolpage_alloc(struct pool *, int);
  139 
  140 /*
  141  * DTrace SDT provider definitions
  142  */
  143 SDT_PROVIDER_DECLARE(proc);
  144 SDT_PROBE_DEFINE3(proc, kernel, , signal__send,
  145     "struct lwp *",     /* target thread */
  146     "struct proc *",    /* target process */
  147     "int");             /* signal */
  148 SDT_PROBE_DEFINE3(proc, kernel, , signal__discard,
  149     "struct lwp *",     /* target thread */
  150     "struct proc *",    /* target process */
  151     "int");             /* signal */
  152 SDT_PROBE_DEFINE3(proc, kernel, , signal__handle,
  153     "int",              /* signal */
  154     "ksiginfo_t *",     /* signal info */
  155     "void (*)(void)");  /* handler address */
  156 
  157 
  158 static struct pool_allocator sigactspool_allocator = {
  159         .pa_alloc = sigacts_poolpage_alloc,
  160         .pa_free = sigacts_poolpage_free
  161 };
  162 
  163 #ifdef DEBUG
  164 int     kern_logsigexit = 1;
  165 #else
  166 int     kern_logsigexit = 0;
  167 #endif
  168 
  169 static const char logcoredump[] =
  170     "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
  171 static const char lognocoredump[] =
  172     "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
  173 
  174 static kauth_listener_t signal_listener;
  175 
  176 static int
  177 signal_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
  178     void *arg0, void *arg1, void *arg2, void *arg3)
  179 {
  180         struct proc *p;
  181         int result, signum;
  182 
  183         result = KAUTH_RESULT_DEFER;
  184         p = arg0;
  185         signum = (int)(unsigned long)arg1;
  186 
  187         if (action != KAUTH_PROCESS_SIGNAL)
  188                 return result;
  189 
  190         if (kauth_cred_uidmatch(cred, p->p_cred) ||
  191             (signum == SIGCONT && (curproc->p_session == p->p_session)))
  192                 result = KAUTH_RESULT_ALLOW;
  193 
  194         return result;
  195 }
  196 
  197 static int
  198 sigacts_ctor(void *arg __unused, void *obj, int flags __unused)
  199 {
  200         memset(obj, 0, sizeof(struct sigacts));
  201         return 0;
  202 }
  203 
  204 /*
  205  * signal_init:
  206  *
  207  *      Initialize global signal-related data structures.
  208  */
  209 void
  210 signal_init(void)
  211 {
  212 
  213         sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
  214 
  215         sigacts_cache = pool_cache_init(sizeof(struct sigacts), 0, 0, 0,
  216             "sigacts", sizeof(struct sigacts) > PAGE_SIZE ?
  217             &sigactspool_allocator : NULL, IPL_NONE, sigacts_ctor, NULL, NULL);
  218         ksiginfo_cache = pool_cache_init(sizeof(ksiginfo_t), 0, 0, 0,
  219             "ksiginfo", NULL, IPL_VM, NULL, NULL, NULL);
  220 
  221         exechook_establish(ksiginfo_exechook, NULL);
  222 
  223         callout_init(&proc_stop_ch, CALLOUT_MPSAFE);
  224         callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL);
  225 
  226         signal_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
  227             signal_listener_cb, NULL);
  228 }
  229 
  230 /*
  231  * sigacts_poolpage_alloc:
  232  *
  233  *      Allocate a page for the sigacts memory pool.
  234  */
  235 static void *
  236 sigacts_poolpage_alloc(struct pool *pp, int flags)
  237 {
  238 
  239         return (void *)uvm_km_alloc(kernel_map,
  240             PAGE_SIZE * 2, PAGE_SIZE * 2,
  241             ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
  242             | UVM_KMF_WIRED);
  243 }
  244 
  245 /*
  246  * sigacts_poolpage_free:
  247  *
  248  *      Free a page on behalf of the sigacts memory pool.
  249  */
  250 static void
  251 sigacts_poolpage_free(struct pool *pp, void *v)
  252 {
  253 
  254         uvm_km_free(kernel_map, (vaddr_t)v, PAGE_SIZE * 2, UVM_KMF_WIRED);
  255 }
  256 
  257 /*
  258  * sigactsinit:
  259  *
  260  *      Create an initial sigacts structure, using the same signal state
  261  *      as of specified process.  If 'share' is set, share the sigacts by
  262  *      holding a reference, otherwise just copy it from parent.
  263  */
  264 struct sigacts *
  265 sigactsinit(struct proc *pp, int share)
  266 {
  267         struct sigacts *ps = pp->p_sigacts, *ps2;
  268 
  269         if (__predict_false(share)) {
  270                 atomic_inc_uint(&ps->sa_refcnt);
  271                 return ps;
  272         }
  273         ps2 = pool_cache_get(sigacts_cache, PR_WAITOK);
  274         mutex_init(&ps2->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
  275         ps2->sa_refcnt = 1;
  276 
  277         mutex_enter(&ps->sa_mutex);
  278         memcpy(ps2->sa_sigdesc, ps->sa_sigdesc, sizeof(ps2->sa_sigdesc));
  279         mutex_exit(&ps->sa_mutex);
  280         return ps2;
  281 }
  282 
  283 /*
  284  * sigactsunshare:
  285  *
  286  *      Make this process not share its sigacts, maintaining all signal state.
  287  */
  288 void
  289 sigactsunshare(struct proc *p)
  290 {
  291         struct sigacts *ps, *oldps = p->p_sigacts;
  292 
  293         if (__predict_true(oldps->sa_refcnt == 1))
  294                 return;
  295 
  296         ps = pool_cache_get(sigacts_cache, PR_WAITOK);
  297         mutex_init(&ps->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
  298         memcpy(ps->sa_sigdesc, oldps->sa_sigdesc, sizeof(ps->sa_sigdesc));
  299         ps->sa_refcnt = 1;
  300 
  301         p->p_sigacts = ps;
  302         sigactsfree(oldps);
  303 }
  304 
  305 /*
  306  * sigactsfree;
  307  *
  308  *      Release a sigacts structure.
  309  */
  310 void
  311 sigactsfree(struct sigacts *ps)
  312 {
  313 
  314         membar_release();
  315         if (atomic_dec_uint_nv(&ps->sa_refcnt) == 0) {
  316                 membar_acquire();
  317                 mutex_destroy(&ps->sa_mutex);
  318                 pool_cache_put(sigacts_cache, ps);
  319         }
  320 }
  321 
  322 /*
  323  * siginit:
  324  *
  325  *      Initialize signal state for process 0; set to ignore signals that
  326  *      are ignored by default and disable the signal stack.  Locking not
  327  *      required as the system is still cold.
  328  */
  329 void
  330 siginit(struct proc *p)
  331 {
  332         struct lwp *l;
  333         struct sigacts *ps;
  334         int signo, prop;
  335 
  336         ps = p->p_sigacts;
  337         sigemptyset(&contsigmask);
  338         sigemptyset(&stopsigmask);
  339         sigemptyset(&vforksigmask);
  340         sigemptyset(&sigcantmask);
  341         for (signo = 1; signo < NSIG; signo++) {
  342                 prop = sigprop[signo];
  343                 if (prop & SA_CONT)
  344                         sigaddset(&contsigmask, signo);
  345                 if (prop & SA_STOP)
  346                         sigaddset(&stopsigmask, signo);
  347                 if (prop & SA_STOP && signo != SIGSTOP)
  348                         sigaddset(&vforksigmask, signo);
  349                 if (prop & SA_CANTMASK)
  350                         sigaddset(&sigcantmask, signo);
  351                 if (prop & SA_IGNORE && signo != SIGCONT)
  352                         sigaddset(&p->p_sigctx.ps_sigignore, signo);
  353                 sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
  354                 SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
  355         }
  356         sigemptyset(&p->p_sigctx.ps_sigcatch);
  357         p->p_sflag &= ~PS_NOCLDSTOP;
  358 
  359         ksiginfo_queue_init(&p->p_sigpend.sp_info);
  360         sigemptyset(&p->p_sigpend.sp_set);
  361 
  362         /*
  363          * Reset per LWP state.
  364          */
  365         l = LIST_FIRST(&p->p_lwps);
  366         l->l_sigwaited = NULL;
  367         l->l_sigstk = SS_INIT;
  368         ksiginfo_queue_init(&l->l_sigpend.sp_info);
  369         sigemptyset(&l->l_sigpend.sp_set);
  370 
  371         /* One reference. */
  372         ps->sa_refcnt = 1;
  373 }
  374 
  375 /*
  376  * execsigs:
  377  *
  378  *      Reset signals for an exec of the specified process.
  379  */
  380 void
  381 execsigs(struct proc *p)
  382 {
  383         struct sigacts *ps;
  384         struct lwp *l;
  385         int signo, prop;
  386         sigset_t tset;
  387         ksiginfoq_t kq;
  388 
  389         KASSERT(p->p_nlwps == 1);
  390 
  391         sigactsunshare(p);
  392         ps = p->p_sigacts;
  393 
  394         /*
  395          * Reset caught signals.  Held signals remain held through
  396          * l->l_sigmask (unless they were caught, and are now ignored
  397          * by default).
  398          *
  399          * No need to lock yet, the process has only one LWP and
  400          * at this point the sigacts are private to the process.
  401          */
  402         sigemptyset(&tset);
  403         for (signo = 1; signo < NSIG; signo++) {
  404                 if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
  405                         prop = sigprop[signo];
  406                         if (prop & SA_IGNORE) {
  407                                 if ((prop & SA_CONT) == 0)
  408                                         sigaddset(&p->p_sigctx.ps_sigignore,
  409                                             signo);
  410                                 sigaddset(&tset, signo);
  411                         }
  412                         SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
  413                 }
  414                 sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
  415                 SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
  416         }
  417         ksiginfo_queue_init(&kq);
  418 
  419         mutex_enter(p->p_lock);
  420         sigclearall(p, &tset, &kq);
  421         sigemptyset(&p->p_sigctx.ps_sigcatch);
  422 
  423         /*
  424          * Reset no zombies if child dies flag as Solaris does.
  425          */
  426         p->p_flag &= ~(PK_NOCLDWAIT | PK_CLDSIGIGN);
  427         if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN)
  428                 SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL;
  429 
  430         /*
  431          * Reset per-LWP state.
  432          */
  433         l = LIST_FIRST(&p->p_lwps);
  434         l->l_sigwaited = NULL;
  435         l->l_sigstk = SS_INIT;
  436         ksiginfo_queue_init(&l->l_sigpend.sp_info);
  437         sigemptyset(&l->l_sigpend.sp_set);
  438         mutex_exit(p->p_lock);
  439 
  440         ksiginfo_queue_drain(&kq);
  441 }
  442 
  443 /*
  444  * ksiginfo_exechook:
  445  *
  446  *      Free all pending ksiginfo entries from a process on exec.
  447  *      Additionally, drain any unused ksiginfo structures in the
  448  *      system back to the pool.
  449  *
  450  *      XXX This should not be a hook, every process has signals.
  451  */
  452 static void
  453 ksiginfo_exechook(struct proc *p, void *v)
  454 {
  455         ksiginfoq_t kq;
  456 
  457         ksiginfo_queue_init(&kq);
  458 
  459         mutex_enter(p->p_lock);
  460         sigclearall(p, NULL, &kq);
  461         mutex_exit(p->p_lock);
  462 
  463         ksiginfo_queue_drain(&kq);
  464 }
  465 
  466 /*
  467  * ksiginfo_alloc:
  468  *
  469  *      Allocate a new ksiginfo structure from the pool, and optionally copy
  470  *      an existing one.  If the existing ksiginfo_t is from the pool, and
  471  *      has not been queued somewhere, then just return it.  Additionally,
  472  *      if the existing ksiginfo_t does not contain any information beyond
  473  *      the signal number, then just return it.
  474  */
  475 ksiginfo_t *
  476 ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags)
  477 {
  478         ksiginfo_t *kp;
  479 
  480         if (ok != NULL) {
  481                 if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) ==
  482                     KSI_FROMPOOL)
  483                         return ok;
  484                 if (KSI_EMPTY_P(ok))
  485                         return ok;
  486         }
  487 
  488         kp = pool_cache_get(ksiginfo_cache, flags);
  489         if (kp == NULL) {
  490 #ifdef DIAGNOSTIC
  491                 printf("Out of memory allocating ksiginfo for pid %d\n",
  492                     p->p_pid);
  493 #endif
  494                 return NULL;
  495         }
  496 
  497         if (ok != NULL) {
  498                 memcpy(kp, ok, sizeof(*kp));
  499                 kp->ksi_flags &= ~KSI_QUEUED;
  500         } else
  501                 KSI_INIT_EMPTY(kp);
  502 
  503         kp->ksi_flags |= KSI_FROMPOOL;
  504 
  505         return kp;
  506 }
  507 
  508 /*
  509  * ksiginfo_free:
  510  *
  511  *      If the given ksiginfo_t is from the pool and has not been queued,
  512  *      then free it.
  513  */
  514 void
  515 ksiginfo_free(ksiginfo_t *kp)
  516 {
  517 
  518         if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL)
  519                 return;
  520         pool_cache_put(ksiginfo_cache, kp);
  521 }
  522 
  523 /*
  524  * ksiginfo_queue_drain:
  525  *
  526  *      Drain a non-empty ksiginfo_t queue.
  527  */
  528 void
  529 ksiginfo_queue_drain0(ksiginfoq_t *kq)
  530 {
  531         ksiginfo_t *ksi;
  532 
  533         KASSERT(!TAILQ_EMPTY(kq));
  534 
  535         while (!TAILQ_EMPTY(kq)) {
  536                 ksi = TAILQ_FIRST(kq);
  537                 TAILQ_REMOVE(kq, ksi, ksi_list);
  538                 pool_cache_put(ksiginfo_cache, ksi);
  539         }
  540 }
  541 
  542 static int
  543 siggetinfo(sigpend_t *sp, ksiginfo_t *out, int signo)
  544 {
  545         ksiginfo_t *ksi, *nksi;
  546 
  547         if (sp == NULL)
  548                 goto out;
  549 
  550         /* Find siginfo and copy it out. */
  551         int count = 0;
  552         TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, nksi) {
  553                 if (ksi->ksi_signo != signo)
  554                         continue;
  555                 if (count++ > 0) /* Only remove the first, count all of them */
  556                         continue;
  557                 TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
  558                 KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
  559                 KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
  560                 ksi->ksi_flags &= ~KSI_QUEUED;
  561                 if (out != NULL) {
  562                         memcpy(out, ksi, sizeof(*out));
  563                         out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED);
  564                 }
  565                 ksiginfo_free(ksi);
  566         }
  567         if (count)
  568                 return count;
  569 
  570 out:
  571         /* If there is no siginfo, then manufacture it. */
  572         if (out != NULL) {
  573                 KSI_INIT(out);
  574                 out->ksi_info._signo = signo;
  575                 out->ksi_info._code = SI_NOINFO;
  576         }
  577         return 0;
  578 }
  579 
  580 /*
  581  * sigget:
  582  *
  583  *      Fetch the first pending signal from a set.  Optionally, also fetch
  584  *      or manufacture a ksiginfo element.  Returns the number of the first
  585  *      pending signal, or zero.
  586  */
  587 int
  588 sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask)
  589 {
  590         sigset_t tset;
  591         int count;
  592 
  593         /* If there's no pending set, the signal is from the debugger. */
  594         if (sp == NULL)
  595                 goto out;
  596 
  597         /* Construct mask from signo, and 'mask'. */
  598         if (signo == 0) {
  599                 if (mask != NULL) {
  600                         tset = *mask;
  601                         __sigandset(&sp->sp_set, &tset);
  602                 } else
  603                         tset = sp->sp_set;
  604 
  605                 /* If there are no signals pending - return. */
  606                 if ((signo = firstsig(&tset)) == 0)
  607                         goto out;
  608         } else {
  609                 KASSERT(sigismember(&sp->sp_set, signo));
  610         }
  611 
  612         sigdelset(&sp->sp_set, signo);
  613 out:
  614         count = siggetinfo(sp, out, signo);
  615         if (count > 1)
  616                 sigaddset(&sp->sp_set, signo);
  617         return signo;
  618 }
  619 
  620 /*
  621  * sigput:
  622  *
  623  *      Append a new ksiginfo element to the list of pending ksiginfo's.
  624  */
  625 static int
  626 sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi)
  627 {
  628         ksiginfo_t *kp;
  629 
  630         KASSERT(mutex_owned(p->p_lock));
  631         KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
  632 
  633         sigaddset(&sp->sp_set, ksi->ksi_signo);
  634 
  635         /*
  636          * If there is no siginfo, we are done.
  637          */
  638         if (KSI_EMPTY_P(ksi))
  639                 return 0;
  640 
  641         KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
  642 
  643         size_t count = 0;
  644         TAILQ_FOREACH(kp, &sp->sp_info, ksi_list) {
  645                 count++;
  646                 if (ksi->ksi_signo >= SIGRTMIN && ksi->ksi_signo <= SIGRTMAX)
  647                         continue;
  648                 if (kp->ksi_signo == ksi->ksi_signo) {
  649                         KSI_COPY(ksi, kp);
  650                         kp->ksi_flags |= KSI_QUEUED;
  651                         return 0;
  652                 }
  653         }
  654 
  655         if (count >= SIGQUEUE_MAX) {
  656 #ifdef DIAGNOSTIC
  657                 printf("%s(%d): Signal queue is full signal=%d\n",
  658                     p->p_comm, p->p_pid, ksi->ksi_signo);
  659 #endif
  660                 return EAGAIN;
  661         }
  662         ksi->ksi_flags |= KSI_QUEUED;
  663         TAILQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list);
  664 
  665         return 0;
  666 }
  667 
  668 /*
  669  * sigclear:
  670  *
  671  *      Clear all pending signals in the specified set.
  672  */
  673 void
  674 sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq)
  675 {
  676         ksiginfo_t *ksi, *next;
  677 
  678         if (mask == NULL)
  679                 sigemptyset(&sp->sp_set);
  680         else
  681                 sigminusset(mask, &sp->sp_set);
  682 
  683         TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, next) {
  684                 if (mask == NULL || sigismember(mask, ksi->ksi_signo)) {
  685                         TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
  686                         KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
  687                         KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
  688                         TAILQ_INSERT_TAIL(kq, ksi, ksi_list);
  689                 }
  690         }
  691 }
  692 
  693 /*
  694  * sigclearall:
  695  *
  696  *      Clear all pending signals in the specified set from a process and
  697  *      its LWPs.
  698  */
  699 void
  700 sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
  701 {
  702         struct lwp *l;
  703 
  704         KASSERT(mutex_owned(p->p_lock));
  705 
  706         sigclear(&p->p_sigpend, mask, kq);
  707 
  708         LIST_FOREACH(l, &p->p_lwps, l_sibling) {
  709                 sigclear(&l->l_sigpend, mask, kq);
  710         }
  711 }
  712 
  713 /*
  714  * sigispending:
  715  *
  716  *      Return the first signal number if there are pending signals for the
  717  *      current LWP.  May be called unlocked provided that LW_PENDSIG is set,
  718  *      and that the signal has been posted to the appopriate queue before
  719  *      LW_PENDSIG is set.
  720  *
  721  *      This should only ever be called with (l == curlwp), unless the
  722  *      result does not matter (procfs, sysctl).
  723  */
  724 int
  725 sigispending(struct lwp *l, int signo)
  726 {
  727         struct proc *p = l->l_proc;
  728         sigset_t tset;
  729 
  730         membar_consumer();
  731 
  732         tset = l->l_sigpend.sp_set;
  733         sigplusset(&p->p_sigpend.sp_set, &tset);
  734         sigminusset(&p->p_sigctx.ps_sigignore, &tset);
  735         sigminusset(&l->l_sigmask, &tset);
  736 
  737         if (signo == 0) {
  738                 return firstsig(&tset);
  739         }
  740         return sigismember(&tset, signo) ? signo : 0;
  741 }
  742 
  743 void
  744 getucontext(struct lwp *l, ucontext_t *ucp)
  745 {
  746         struct proc *p = l->l_proc;
  747 
  748         KASSERT(mutex_owned(p->p_lock));
  749 
  750         ucp->uc_flags = 0;
  751         ucp->uc_link = l->l_ctxlink;
  752         ucp->uc_sigmask = l->l_sigmask;
  753         ucp->uc_flags |= _UC_SIGMASK;
  754 
  755         /*
  756          * The (unsupplied) definition of the `current execution stack'
  757          * in the System V Interface Definition appears to allow returning
  758          * the main context stack.
  759          */
  760         if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
  761                 ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase;
  762                 ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
  763                 ucp->uc_stack.ss_flags = 0;     /* XXX, def. is Very Fishy */
  764         } else {
  765                 /* Simply copy alternate signal execution stack. */
  766                 ucp->uc_stack = l->l_sigstk;
  767         }
  768         ucp->uc_flags |= _UC_STACK;
  769         mutex_exit(p->p_lock);
  770         cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
  771         mutex_enter(p->p_lock);
  772 }
  773 
  774 int
  775 setucontext(struct lwp *l, const ucontext_t *ucp)
  776 {
  777         struct proc *p = l->l_proc;
  778         int error;
  779 
  780         KASSERT(mutex_owned(p->p_lock));
  781 
  782         if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
  783                 error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
  784                 if (error != 0)
  785                         return error;
  786         }
  787 
  788         mutex_exit(p->p_lock);
  789         error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags);
  790         mutex_enter(p->p_lock);
  791         if (error != 0)
  792                 return (error);
  793 
  794         l->l_ctxlink = ucp->uc_link;
  795 
  796         /*
  797          * If there was stack information, update whether or not we are
  798          * still running on an alternate signal stack.
  799          */
  800         if ((ucp->uc_flags & _UC_STACK) != 0) {
  801                 if (ucp->uc_stack.ss_flags & SS_ONSTACK)
  802                         l->l_sigstk.ss_flags |= SS_ONSTACK;
  803                 else
  804                         l->l_sigstk.ss_flags &= ~SS_ONSTACK;
  805         }
  806 
  807         return 0;
  808 }
  809 
  810 /*
  811  * killpg1: common code for kill process group/broadcast kill.
  812  */
  813 int
  814 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
  815 {
  816         struct proc     *p, *cp;
  817         kauth_cred_t    pc;
  818         struct pgrp     *pgrp;
  819         int             nfound;
  820         int             signo = ksi->ksi_signo;
  821 
  822         cp = l->l_proc;
  823         pc = l->l_cred;
  824         nfound = 0;
  825 
  826         mutex_enter(&proc_lock);
  827         if (all) {
  828                 /*
  829                  * Broadcast.
  830                  */
  831                 PROCLIST_FOREACH(p, &allproc) {
  832                         if (p->p_pid <= 1 || p == cp ||
  833                             (p->p_flag & PK_SYSTEM) != 0)
  834                                 continue;
  835                         mutex_enter(p->p_lock);
  836                         if (kauth_authorize_process(pc,
  837                             KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL,
  838                             NULL) == 0) {
  839                                 nfound++;
  840                                 if (signo)
  841                                         kpsignal2(p, ksi);
  842                         }
  843                         mutex_exit(p->p_lock);
  844                 }
  845         } else {
  846                 if (pgid == 0)
  847                         /* Zero pgid means send to my process group. */
  848                         pgrp = cp->p_pgrp;
  849                 else {
  850                         pgrp = pgrp_find(pgid);
  851                         if (pgrp == NULL)
  852                                 goto out;
  853                 }
  854                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
  855                         if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM)
  856                                 continue;
  857                         mutex_enter(p->p_lock);
  858                         if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL,
  859                             p, KAUTH_ARG(signo), NULL, NULL) == 0) {
  860                                 nfound++;
  861                                 if (signo && P_ZOMBIE(p) == 0)
  862                                         kpsignal2(p, ksi);
  863                         }
  864                         mutex_exit(p->p_lock);
  865                 }
  866         }
  867 out:
  868         mutex_exit(&proc_lock);
  869         return nfound ? 0 : ESRCH;
  870 }
  871 
  872 /*
  873  * Send a signal to a process group.  If checktty is set, limit to members
  874  * which have a controlling terminal.
  875  */
  876 void
  877 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
  878 {
  879         ksiginfo_t ksi;
  880 
  881         KASSERT(!cpu_intr_p());
  882         KASSERT(mutex_owned(&proc_lock));
  883 
  884         KSI_INIT_EMPTY(&ksi);
  885         ksi.ksi_signo = sig;
  886         kpgsignal(pgrp, &ksi, NULL, checkctty);
  887 }
  888 
  889 void
  890 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
  891 {
  892         struct proc *p;
  893 
  894         KASSERT(!cpu_intr_p());
  895         KASSERT(mutex_owned(&proc_lock));
  896         KASSERT(pgrp != NULL);
  897 
  898         LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
  899                 if (checkctty == 0 || p->p_lflag & PL_CONTROLT)
  900                         kpsignal(p, ksi, data);
  901 }
  902 
  903 /*
  904  * Send a signal caused by a trap to the current LWP.  If it will be caught
  905  * immediately, deliver it with correct code.  Otherwise, post it normally.
  906  */
  907 void
  908 trapsignal(struct lwp *l, ksiginfo_t *ksi)
  909 {
  910         struct proc     *p;
  911         struct sigacts  *ps;
  912         int signo = ksi->ksi_signo;
  913         sigset_t *mask;
  914         sig_t action;
  915 
  916         KASSERT(KSI_TRAP_P(ksi));
  917 
  918         ksi->ksi_lid = l->l_lid;
  919         p = l->l_proc;
  920 
  921         KASSERT(!cpu_intr_p());
  922         mutex_enter(&proc_lock);
  923         mutex_enter(p->p_lock);
  924 
  925 repeat:
  926         /*
  927          * If we are exiting, demise now.
  928          *
  929          * This avoids notifying tracer and deadlocking.
  930          */
  931         if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
  932                 mutex_exit(p->p_lock);
  933                 mutex_exit(&proc_lock);
  934                 lwp_exit(l);
  935                 panic("trapsignal");
  936                 /* NOTREACHED */
  937         }
  938 
  939         /*
  940          * The process is already stopping.
  941          */
  942         if ((p->p_sflag & PS_STOPPING) != 0) {
  943                 mutex_exit(&proc_lock);
  944                 sigswitch_unlock_and_switch_away(l);
  945                 mutex_enter(&proc_lock);
  946                 mutex_enter(p->p_lock);
  947                 goto repeat;
  948         }
  949 
  950         mask = &l->l_sigmask;
  951         ps = p->p_sigacts;
  952         action = SIGACTION_PS(ps, signo).sa_handler;
  953 
  954         if (ISSET(p->p_slflag, PSL_TRACED) &&
  955             !(p->p_pptr == p->p_opptr && ISSET(p->p_lflag, PL_PPWAIT)) &&
  956             p->p_xsig != SIGKILL &&
  957             !sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
  958                 p->p_xsig = signo;
  959                 p->p_sigctx.ps_faked = true;
  960                 p->p_sigctx.ps_lwp = ksi->ksi_lid;
  961                 p->p_sigctx.ps_info = ksi->ksi_info;
  962                 sigswitch(0, signo, true);
  963 
  964                 if (ktrpoint(KTR_PSIG)) {
  965                         if (p->p_emul->e_ktrpsig)
  966                                 p->p_emul->e_ktrpsig(signo, action, mask, ksi);
  967                         else
  968                                 ktrpsig(signo, action, mask, ksi);
  969                 }
  970                 return;
  971         }
  972 
  973         const bool caught = sigismember(&p->p_sigctx.ps_sigcatch, signo);
  974         const bool masked = sigismember(mask, signo);
  975         if (caught && !masked) {
  976                 mutex_exit(&proc_lock);
  977                 l->l_ru.ru_nsignals++;
  978                 kpsendsig(l, ksi, mask);
  979                 mutex_exit(p->p_lock);
  980 
  981                 if (ktrpoint(KTR_PSIG)) {
  982                         if (p->p_emul->e_ktrpsig)
  983                                 p->p_emul->e_ktrpsig(signo, action, mask, ksi);
  984                         else
  985                                 ktrpsig(signo, action, mask, ksi);
  986                 }
  987                 return;
  988         }
  989 
  990         /*
  991          * If the signal is masked or ignored, then unmask it and
  992          * reset it to the default action so that the process or
  993          * its tracer will be notified.
  994          */
  995         const bool ignored = action == SIG_IGN;
  996         if (masked || ignored) {
  997                 mutex_enter(&ps->sa_mutex);
  998                 sigdelset(mask, signo);
  999                 sigdelset(&p->p_sigctx.ps_sigcatch, signo);
 1000                 sigdelset(&p->p_sigctx.ps_sigignore, signo);
 1001                 sigdelset(&SIGACTION_PS(ps, signo).sa_mask, signo);
 1002                 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
 1003                 mutex_exit(&ps->sa_mutex);
 1004         }
 1005 
 1006         kpsignal2(p, ksi);
 1007         mutex_exit(p->p_lock);
 1008         mutex_exit(&proc_lock);
 1009 }
 1010 
 1011 /*
 1012  * Fill in signal information and signal the parent for a child status change.
 1013  */
 1014 void
 1015 child_psignal(struct proc *p, int mask)
 1016 {
 1017         ksiginfo_t ksi;
 1018         struct proc *q;
 1019         int xsig;
 1020 
 1021         KASSERT(mutex_owned(&proc_lock));
 1022         KASSERT(mutex_owned(p->p_lock));
 1023 
 1024         xsig = p->p_xsig;
 1025 
 1026         KSI_INIT(&ksi);
 1027         ksi.ksi_signo = SIGCHLD;
 1028         ksi.ksi_code = (xsig == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
 1029         ksi.ksi_pid = p->p_pid;
 1030         ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
 1031         ksi.ksi_status = xsig;
 1032         ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
 1033         ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
 1034 
 1035         q = p->p_pptr;
 1036 
 1037         mutex_exit(p->p_lock);
 1038         mutex_enter(q->p_lock);
 1039 
 1040         if ((q->p_sflag & mask) == 0)
 1041                 kpsignal2(q, &ksi);
 1042 
 1043         mutex_exit(q->p_lock);
 1044         mutex_enter(p->p_lock);
 1045 }
 1046 
 1047 void
 1048 psignal(struct proc *p, int signo)
 1049 {
 1050         ksiginfo_t ksi;
 1051 
 1052         KASSERT(!cpu_intr_p());
 1053         KASSERT(mutex_owned(&proc_lock));
 1054 
 1055         KSI_INIT_EMPTY(&ksi);
 1056         ksi.ksi_signo = signo;
 1057         mutex_enter(p->p_lock);
 1058         kpsignal2(p, &ksi);
 1059         mutex_exit(p->p_lock);
 1060 }
 1061 
 1062 void
 1063 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
 1064 {
 1065         fdfile_t *ff;
 1066         file_t *fp;
 1067         fdtab_t *dt;
 1068 
 1069         KASSERT(!cpu_intr_p());
 1070         KASSERT(mutex_owned(&proc_lock));
 1071 
 1072         if ((p->p_sflag & PS_WEXIT) == 0 && data) {
 1073                 size_t fd;
 1074                 filedesc_t *fdp = p->p_fd;
 1075 
 1076                 /* XXXSMP locking */
 1077                 ksi->ksi_fd = -1;
 1078                 dt = atomic_load_consume(&fdp->fd_dt);
 1079                 for (fd = 0; fd < dt->dt_nfiles; fd++) {
 1080                         if ((ff = dt->dt_ff[fd]) == NULL)
 1081                                 continue;
 1082                         if ((fp = atomic_load_consume(&ff->ff_file)) == NULL)
 1083                                 continue;
 1084                         if (fp->f_data == data) {
 1085                                 ksi->ksi_fd = fd;
 1086                                 break;
 1087                         }
 1088                 }
 1089         }
 1090         mutex_enter(p->p_lock);
 1091         kpsignal2(p, ksi);
 1092         mutex_exit(p->p_lock);
 1093 }
 1094 
 1095 /*
 1096  * sigismasked:
 1097  *
 1098  *      Returns true if signal is ignored or masked for the specified LWP.
 1099  */
 1100 int
 1101 sigismasked(struct lwp *l, int sig)
 1102 {
 1103         struct proc *p = l->l_proc;
 1104 
 1105         return sigismember(&p->p_sigctx.ps_sigignore, sig) ||
 1106             sigismember(&l->l_sigmask, sig);
 1107 }
 1108 
 1109 /*
 1110  * sigpost:
 1111  *
 1112  *      Post a pending signal to an LWP.  Returns non-zero if the LWP may
 1113  *      be able to take the signal.
 1114  */
 1115 static int
 1116 sigpost(struct lwp *l, sig_t action, int prop, int sig)
 1117 {
 1118         int rv, masked;
 1119         struct proc *p = l->l_proc;
 1120 
 1121         KASSERT(mutex_owned(p->p_lock));
 1122 
 1123         /*
 1124          * If the LWP is on the way out, sigclear() will be busy draining all
 1125          * pending signals.  Don't give it more.
 1126          */
 1127         if (l->l_stat == LSZOMB)
 1128                 return 0;
 1129 
 1130         SDT_PROBE(proc, kernel, , signal__send, l, p, sig, 0, 0);
 1131 
 1132         lwp_lock(l);
 1133         if (__predict_false((l->l_flag & LW_DBGSUSPEND) != 0)) {
 1134                 if ((prop & SA_KILL) != 0)
 1135                         l->l_flag &= ~LW_DBGSUSPEND;
 1136                 else {
 1137                         lwp_unlock(l);
 1138                         return 0;
 1139                 }
 1140         }
 1141 
 1142         /*
 1143          * Have the LWP check for signals.  This ensures that even if no LWP
 1144          * is found to take the signal immediately, it should be taken soon.
 1145          */
 1146         signotify(l);
 1147 
 1148         /*
 1149          * SIGCONT can be masked, but if LWP is stopped, it needs restart.
 1150          * Note: SIGKILL and SIGSTOP cannot be masked.
 1151          */
 1152         masked = sigismember(&l->l_sigmask, sig);
 1153         if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
 1154                 lwp_unlock(l);
 1155                 return 0;
 1156         }
 1157 
 1158         /*
 1159          * If killing the process, make it run fast.
 1160          */
 1161         if (__predict_false((prop & SA_KILL) != 0) &&
 1162             action == SIG_DFL && l->l_priority < MAXPRI_USER) {
 1163                 KASSERT(l->l_class == SCHED_OTHER);
 1164                 lwp_changepri(l, MAXPRI_USER);
 1165         }
 1166 
 1167         /*
 1168          * If the LWP is running or on a run queue, then we win.  If it's
 1169          * sleeping interruptably, wake it and make it take the signal.  If
 1170          * the sleep isn't interruptable, then the chances are it will get
 1171          * to see the signal soon anyhow.  If suspended, it can't take the
 1172          * signal right now.  If it's LWP private or for all LWPs, save it
 1173          * for later; otherwise punt.
 1174          */
 1175         rv = 0;
 1176 
 1177         switch (l->l_stat) {
 1178         case LSRUN:
 1179         case LSONPROC:
 1180                 rv = 1;
 1181                 break;
 1182 
 1183         case LSSLEEP:
 1184                 if ((l->l_flag & LW_SINTR) != 0) {
 1185                         /* setrunnable() will release the lock. */
 1186                         setrunnable(l);
 1187                         return 1;
 1188                 }
 1189                 break;
 1190 
 1191         case LSSUSPENDED:
 1192                 if ((prop & SA_KILL) != 0 && (l->l_flag & LW_WCORE) != 0) {
 1193                         /* lwp_continue() will release the lock. */
 1194                         lwp_continue(l);
 1195                         return 1;
 1196                 }
 1197                 break;
 1198 
 1199         case LSSTOP:
 1200                 if ((prop & SA_STOP) != 0)
 1201                         break;
 1202 
 1203                 /*
 1204                  * If the LWP is stopped and we are sending a continue
 1205                  * signal, then start it again.
 1206                  */
 1207                 if ((prop & SA_CONT) != 0) {
 1208                         if (l->l_wchan != NULL) {
 1209                                 l->l_stat = LSSLEEP;
 1210                                 p->p_nrlwps++;
 1211                                 rv = 1;
 1212                                 break;
 1213                         }
 1214                         /* setrunnable() will release the lock. */
 1215                         setrunnable(l);
 1216                         return 1;
 1217                 } else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
 1218                         /* setrunnable() will release the lock. */
 1219                         setrunnable(l);
 1220                         return 1;
 1221                 }
 1222                 break;
 1223 
 1224         default:
 1225                 break;
 1226         }
 1227 
 1228         lwp_unlock(l);
 1229         return rv;
 1230 }
 1231 
 1232 /*
 1233  * Notify an LWP that it has a pending signal.
 1234  */
 1235 void
 1236 signotify(struct lwp *l)
 1237 {
 1238         KASSERT(lwp_locked(l, NULL));
 1239 
 1240         l->l_flag |= LW_PENDSIG;
 1241         lwp_need_userret(l);
 1242 }
 1243 
 1244 /*
 1245  * Find an LWP within process p that is waiting on signal ksi, and hand
 1246  * it on.
 1247  */
 1248 static int
 1249 sigunwait(struct proc *p, const ksiginfo_t *ksi)
 1250 {
 1251         struct lwp *l;
 1252         int signo;
 1253 
 1254         KASSERT(mutex_owned(p->p_lock));
 1255 
 1256         signo = ksi->ksi_signo;
 1257 
 1258         if (ksi->ksi_lid != 0) {
 1259                 /*
 1260                  * Signal came via _lwp_kill().  Find the LWP and see if
 1261                  * it's interested.
 1262                  */
 1263                 if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
 1264                         return 0;
 1265                 if (l->l_sigwaited == NULL ||
 1266                     !sigismember(&l->l_sigwaitset, signo))
 1267                         return 0;
 1268         } else {
 1269                 /*
 1270                  * Look for any LWP that may be interested.
 1271                  */
 1272                 LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
 1273                         KASSERT(l->l_sigwaited != NULL);
 1274                         if (sigismember(&l->l_sigwaitset, signo))
 1275                                 break;
 1276                 }
 1277         }
 1278 
 1279         if (l != NULL) {
 1280                 l->l_sigwaited->ksi_info = ksi->ksi_info;
 1281                 l->l_sigwaited = NULL;
 1282                 LIST_REMOVE(l, l_sigwaiter);
 1283                 cv_signal(&l->l_sigcv);
 1284                 return 1;
 1285         }
 1286 
 1287         return 0;
 1288 }
 1289 
 1290 /*
 1291  * Send the signal to the process.  If the signal has an action, the action
 1292  * is usually performed by the target process rather than the caller; we add
 1293  * the signal to the set of pending signals for the process.
 1294  *
 1295  * Exceptions:
 1296  *   o When a stop signal is sent to a sleeping process that takes the
 1297  *     default action, the process is stopped without awakening it.
 1298  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
 1299  *     regardless of the signal action (eg, blocked or ignored).
 1300  *
 1301  * Other ignored signals are discarded immediately.
 1302  */
 1303 int
 1304 kpsignal2(struct proc *p, ksiginfo_t *ksi)
 1305 {
 1306         int prop, signo = ksi->ksi_signo;
 1307         struct lwp *l = NULL;
 1308         ksiginfo_t *kp;
 1309         lwpid_t lid;
 1310         sig_t action;
 1311         bool toall;
 1312         bool traced;
 1313         int error = 0;
 1314 
 1315         KASSERT(!cpu_intr_p());
 1316         KASSERT(mutex_owned(&proc_lock));
 1317         KASSERT(mutex_owned(p->p_lock));
 1318         KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
 1319         KASSERT(signo > 0 && signo < NSIG);
 1320 
 1321         /*
 1322          * If the process is being created by fork, is a zombie or is
 1323          * exiting, then just drop the signal here and bail out.
 1324          */
 1325         if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
 1326                 return 0;
 1327 
 1328         /*
 1329          * Notify any interested parties of the signal.
 1330          */
 1331         KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
 1332 
 1333         /*
 1334          * Some signals including SIGKILL must act on the entire process.
 1335          */
 1336         kp = NULL;
 1337         prop = sigprop[signo];
 1338         toall = ((prop & SA_TOALL) != 0);
 1339         lid = toall ? 0 : ksi->ksi_lid;
 1340         traced = ISSET(p->p_slflag, PSL_TRACED) &&
 1341             !sigismember(&p->p_sigctx.ps_sigpass, signo);
 1342 
 1343         /*
 1344          * If proc is traced, always give parent a chance.
 1345          */
 1346         if (traced) {
 1347                 action = SIG_DFL;
 1348 
 1349                 if (lid == 0) {
 1350                         /*
 1351                          * If the process is being traced and the signal
 1352                          * is being caught, make sure to save any ksiginfo.
 1353                          */
 1354                         if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
 1355                                 goto discard;
 1356                         if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
 1357                                 goto out;
 1358                 }
 1359         } else {
 1360 
 1361                 /*
 1362                  * If the signal is being ignored, then drop it.  Note: we
 1363                  * don't set SIGCONT in ps_sigignore, and if it is set to
 1364                  * SIG_IGN, action will be SIG_DFL here.
 1365                  */
 1366                 if (sigismember(&p->p_sigctx.ps_sigignore, signo))
 1367                         goto discard;
 1368 
 1369                 else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
 1370                         action = SIG_CATCH;
 1371                 else {
 1372                         action = SIG_DFL;
 1373 
 1374                         /*
 1375                          * If sending a tty stop signal to a member of an
 1376                          * orphaned process group, discard the signal here if
 1377                          * the action is default; don't stop the process below
 1378                          * if sleeping, and don't clear any pending SIGCONT.
 1379                          */
 1380                         if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
 1381                                 goto discard;
 1382 
 1383                         if (prop & SA_KILL && p->p_nice > NZERO)
 1384                                 p->p_nice = NZERO;
 1385                 }
 1386         }
 1387 
 1388         /*
 1389          * If stopping or continuing a process, discard any pending
 1390          * signals that would do the inverse.
 1391          */
 1392         if ((prop & (SA_CONT | SA_STOP)) != 0) {
 1393                 ksiginfoq_t kq;
 1394 
 1395                 ksiginfo_queue_init(&kq);
 1396                 if ((prop & SA_CONT) != 0)
 1397                         sigclear(&p->p_sigpend, &stopsigmask, &kq);
 1398                 if ((prop & SA_STOP) != 0)
 1399                         sigclear(&p->p_sigpend, &contsigmask, &kq);
 1400                 ksiginfo_queue_drain(&kq);      /* XXXSMP */
 1401         }
 1402 
 1403         /*
 1404          * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
 1405          * please!), check if any LWPs are waiting on it.  If yes, pass on
 1406          * the signal info.  The signal won't be processed further here.
 1407          */
 1408         if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
 1409             p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
 1410             sigunwait(p, ksi))
 1411                 goto discard;
 1412 
 1413         /*
 1414          * XXXSMP Should be allocated by the caller, we're holding locks
 1415          * here.
 1416          */
 1417         if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
 1418                 goto discard;
 1419 
 1420         /*
 1421          * LWP private signals are easy - just find the LWP and post
 1422          * the signal to it.
 1423          */
 1424         if (lid != 0) {
 1425                 l = lwp_find(p, lid);
 1426                 if (l != NULL) {
 1427                         if ((error = sigput(&l->l_sigpend, p, kp)) != 0)
 1428                                 goto out;
 1429                         membar_producer();
 1430                         if (sigpost(l, action, prop, kp->ksi_signo) != 0)
 1431                                 signo = -1;
 1432                 }
 1433                 goto out;
 1434         }
 1435 
 1436         /*
 1437          * Some signals go to all LWPs, even if posted with _lwp_kill()
 1438          * or for an SA process.
 1439          */
 1440         if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
 1441                 if (traced)
 1442                         goto deliver;
 1443 
 1444                 /*
 1445                  * If SIGCONT is default (or ignored) and process is
 1446                  * asleep, we are finished; the process should not
 1447                  * be awakened.
 1448                  */
 1449                 if ((prop & SA_CONT) != 0 && action == SIG_DFL)
 1450                         goto out;
 1451         } else {
 1452                 /*
 1453                  * Process is stopped or stopping.
 1454                  * - If traced, then no action is needed, unless killing.
 1455                  * - Run the process only if sending SIGCONT or SIGKILL.
 1456                  */
 1457                 if (traced && signo != SIGKILL) {
 1458                         goto out;
 1459                 }
 1460                 if ((prop & SA_CONT) != 0 || signo == SIGKILL) {
 1461                         /*
 1462                          * Re-adjust p_nstopchild if the process was
 1463                          * stopped but not yet collected by its parent.
 1464                          */
 1465                         if (p->p_stat == SSTOP && !p->p_waited)
 1466                                 p->p_pptr->p_nstopchild--;
 1467                         p->p_stat = SACTIVE;
 1468                         p->p_sflag &= ~PS_STOPPING;
 1469                         if (traced) {
 1470                                 KASSERT(signo == SIGKILL);
 1471                                 goto deliver;
 1472                         }
 1473                         /*
 1474                          * Do not make signal pending if SIGCONT is default.
 1475                          *
 1476                          * If the process catches SIGCONT, let it handle the
 1477                          * signal itself (if waiting on event - process runs,
 1478                          * otherwise continues sleeping).
 1479                          */
 1480                         if ((prop & SA_CONT) != 0) {
 1481                                 p->p_xsig = SIGCONT;
 1482                                 p->p_sflag |= PS_CONTINUED;
 1483                                 child_psignal(p, 0);
 1484                                 if (action == SIG_DFL) {
 1485                                         KASSERT(signo != SIGKILL);
 1486                                         goto deliver;
 1487                                 }
 1488                         }
 1489                 } else if ((prop & SA_STOP) != 0) {
 1490                         /*
 1491                          * Already stopped, don't need to stop again.
 1492                          * (If we did the shell could get confused.)
 1493                          */
 1494                         goto out;
 1495                 }
 1496         }
 1497         /*
 1498          * Make signal pending.
 1499          */
 1500         KASSERT(!traced);
 1501         if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
 1502                 goto out;
 1503 deliver:
 1504         /*
 1505          * Before we set LW_PENDSIG on any LWP, ensure that the signal is
 1506          * visible on the per process list (for sigispending()).  This
 1507          * is unlikely to be needed in practice, but...
 1508          */
 1509         membar_producer();
 1510 
 1511         /*
 1512          * Try to find an LWP that can take the signal.
 1513          */
 1514         LIST_FOREACH(l, &p->p_lwps, l_sibling) {
 1515                 if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
 1516                         break;
 1517         }
 1518         signo = -1;
 1519 out:
 1520         /*
 1521          * If the ksiginfo wasn't used, then bin it.  XXXSMP freeing memory
 1522          * with locks held.  The caller should take care of this.
 1523          */
 1524         ksiginfo_free(kp);
 1525         if (signo == -1)
 1526                 return error;
 1527 discard:
 1528         SDT_PROBE(proc, kernel, , signal__discard, l, p, signo, 0, 0);
 1529         return error;
 1530 }
 1531 
 1532 void
 1533 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
 1534 {
 1535         struct proc *p = l->l_proc;
 1536 
 1537         KASSERT(mutex_owned(p->p_lock));
 1538         (*p->p_emul->e_sendsig)(ksi, mask);
 1539 }
 1540 
 1541 /*
 1542  * Stop any LWPs sleeping interruptably.
 1543  */
 1544 static void
 1545 proc_stop_lwps(struct proc *p)
 1546 {
 1547         struct lwp *l;
 1548 
 1549         KASSERT(mutex_owned(p->p_lock));
 1550         KASSERT((p->p_sflag & PS_STOPPING) != 0);
 1551 
 1552         LIST_FOREACH(l, &p->p_lwps, l_sibling) {
 1553                 lwp_lock(l);
 1554                 if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
 1555                         l->l_stat = LSSTOP;
 1556                         p->p_nrlwps--;
 1557                 }
 1558                 lwp_unlock(l);
 1559         }
 1560 }
 1561 
 1562 /*
 1563  * Finish stopping of a process.  Mark it stopped and notify the parent.
 1564  *
 1565  * Drop p_lock briefly if ppsig is true.
 1566  */
 1567 static void
 1568 proc_stop_done(struct proc *p, int ppmask)
 1569 {
 1570 
 1571         KASSERT(mutex_owned(&proc_lock));
 1572         KASSERT(mutex_owned(p->p_lock));
 1573         KASSERT((p->p_sflag & PS_STOPPING) != 0);
 1574         KASSERT(p->p_nrlwps == 0 || (p->p_nrlwps == 1 && p == curproc));
 1575 
 1576         p->p_sflag &= ~PS_STOPPING;
 1577         p->p_stat = SSTOP;
 1578         p->p_waited = 0;
 1579         p->p_pptr->p_nstopchild++;
 1580 
 1581         /* child_psignal drops p_lock briefly. */
 1582         child_psignal(p, ppmask);
 1583         cv_broadcast(&p->p_pptr->p_waitcv);
 1584 }
 1585 
 1586 /*
 1587  * Stop the current process and switch away to the debugger notifying
 1588  * an event specific to a traced process only.
 1589  */
 1590 void
 1591 eventswitch(int code, int pe_report_event, int entity)
 1592 {
 1593         struct lwp *l = curlwp;
 1594         struct proc *p = l->l_proc;
 1595         struct sigacts *ps;
 1596         sigset_t *mask;
 1597         sig_t action;
 1598         ksiginfo_t ksi;
 1599         const int signo = SIGTRAP;
 1600 
 1601         KASSERT(mutex_owned(&proc_lock));
 1602         KASSERT(mutex_owned(p->p_lock));
 1603         KASSERT(p->p_pptr != initproc);
 1604         KASSERT(l->l_stat == LSONPROC);
 1605         KASSERT(ISSET(p->p_slflag, PSL_TRACED));
 1606         KASSERT(!ISSET(l->l_flag, LW_SYSTEM));
 1607         KASSERT(p->p_nrlwps > 0);
 1608         KASSERT((code == TRAP_CHLD) || (code == TRAP_LWP) ||
 1609                 (code == TRAP_EXEC));
 1610         KASSERT((code != TRAP_CHLD) || (entity > 1)); /* prevent pid1 */
 1611         KASSERT((code != TRAP_LWP) || (entity > 0));
 1612 
 1613 repeat:
 1614         /*
 1615          * If we are exiting, demise now.
 1616          *
 1617          * This avoids notifying tracer and deadlocking.
 1618          */
 1619         if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
 1620                 mutex_exit(p->p_lock);
 1621                 mutex_exit(&proc_lock);
 1622 
 1623                 if (pe_report_event == PTRACE_LWP_EXIT) {
 1624                         /* Avoid double lwp_exit() and panic. */
 1625                         return;
 1626                 }
 1627 
 1628                 lwp_exit(l);
 1629                 panic("eventswitch");
 1630                 /* NOTREACHED */
 1631         }
 1632 
 1633         /*
 1634          * If we are no longer traced, abandon this event signal.
 1635          *
 1636          * This avoids killing a process after detaching the debugger.
 1637          */
 1638         if (__predict_false(!ISSET(p->p_slflag, PSL_TRACED))) {
 1639                 mutex_exit(p->p_lock);
 1640                 mutex_exit(&proc_lock);
 1641                 return;
 1642         }
 1643 
 1644         /*
 1645          * If there's a pending SIGKILL process it immediately.
 1646          */
 1647         if (p->p_xsig == SIGKILL ||
 1648             sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
 1649                 mutex_exit(p->p_lock);
 1650                 mutex_exit(&proc_lock);
 1651                 return;
 1652         }
 1653 
 1654         /*
 1655          * The process is already stopping.
 1656          */
 1657         if ((p->p_sflag & PS_STOPPING) != 0) {
 1658                 mutex_exit(&proc_lock);
 1659                 sigswitch_unlock_and_switch_away(l);
 1660                 mutex_enter(&proc_lock);
 1661                 mutex_enter(p->p_lock);
 1662                 goto repeat;
 1663         }
 1664 
 1665         KSI_INIT_TRAP(&ksi);
 1666         ksi.ksi_lid = l->l_lid;
 1667         ksi.ksi_signo = signo;
 1668         ksi.ksi_code = code;
 1669         ksi.ksi_pe_report_event = pe_report_event;
 1670 
 1671         CTASSERT(sizeof(ksi.ksi_pe_other_pid) == sizeof(ksi.ksi_pe_lwp));
 1672         ksi.ksi_pe_other_pid = entity;
 1673 
 1674         /* Needed for ktrace */
 1675         ps = p->p_sigacts;
 1676         action = SIGACTION_PS(ps, signo).sa_handler;
 1677         mask = &l->l_sigmask;
 1678 
 1679         p->p_xsig = signo;
 1680         p->p_sigctx.ps_faked = true;
 1681         p->p_sigctx.ps_lwp = ksi.ksi_lid;
 1682         p->p_sigctx.ps_info = ksi.ksi_info;
 1683 
 1684         sigswitch(0, signo, true);
 1685 
 1686         if (code == TRAP_CHLD) {
 1687                 mutex_enter(&proc_lock);
 1688                 while (l->l_vforkwaiting)
 1689                         cv_wait(&l->l_waitcv, &proc_lock);
 1690                 mutex_exit(&proc_lock);
 1691         }
 1692 
 1693         if (ktrpoint(KTR_PSIG)) {
 1694                 if (p->p_emul->e_ktrpsig)
 1695                         p->p_emul->e_ktrpsig(signo, action, mask, &ksi);
 1696                 else
 1697                         ktrpsig(signo, action, mask, &ksi);
 1698         }
 1699 }
 1700 
 1701 void
 1702 eventswitchchild(struct proc *p, int code, int pe_report_event)
 1703 {
 1704         mutex_enter(&proc_lock);
 1705         mutex_enter(p->p_lock);
 1706         if ((p->p_slflag & (PSL_TRACED|PSL_TRACEDCHILD)) !=
 1707             (PSL_TRACED|PSL_TRACEDCHILD)) {
 1708                 mutex_exit(p->p_lock);
 1709                 mutex_exit(&proc_lock);
 1710                 return;
 1711         }
 1712         eventswitch(code, pe_report_event, p->p_oppid);
 1713 }
 1714 
 1715 /*
 1716  * Stop the current process and switch away when being stopped or traced.
 1717  */
 1718 static void
 1719 sigswitch(int ppmask, int signo, bool proc_lock_held)
 1720 {
 1721         struct lwp *l = curlwp;
 1722         struct proc *p = l->l_proc;
 1723 
 1724         KASSERT(mutex_owned(p->p_lock));
 1725         KASSERT(l->l_stat == LSONPROC);
 1726         KASSERT(p->p_nrlwps > 0);
 1727 
 1728         if (proc_lock_held) {
 1729                 KASSERT(mutex_owned(&proc_lock));
 1730         } else {
 1731                 KASSERT(!mutex_owned(&proc_lock));
 1732         }
 1733 
 1734         /*
 1735          * On entry we know that the process needs to stop.  If it's
 1736          * the result of a 'sideways' stop signal that has been sourced
 1737          * through issignal(), then stop other LWPs in the process too.
 1738          */
 1739         if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
 1740                 KASSERT(signo != 0);
 1741                 proc_stop(p, signo);
 1742                 KASSERT(p->p_nrlwps > 0);
 1743         }
 1744 
 1745         /*
 1746          * If we are the last live LWP, and the stop was a result of
 1747          * a new signal, then signal the parent.
 1748          */
 1749         if ((p->p_sflag & PS_STOPPING) != 0) {
 1750                 if (!proc_lock_held && !mutex_tryenter(&proc_lock)) {
 1751                         mutex_exit(p->p_lock);
 1752                         mutex_enter(&proc_lock);
 1753                         mutex_enter(p->p_lock);
 1754                 }
 1755 
 1756                 if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
 1757                         /*
 1758                          * Note that proc_stop_done() can drop
 1759                          * p->p_lock briefly.
 1760                          */
 1761                         proc_stop_done(p, ppmask);
 1762                 }
 1763 
 1764                 mutex_exit(&proc_lock);
 1765         }
 1766 
 1767         sigswitch_unlock_and_switch_away(l);
 1768 }
 1769 
 1770 /*
 1771  * Unlock and switch away.
 1772  */
 1773 static void
 1774 sigswitch_unlock_and_switch_away(struct lwp *l)
 1775 {
 1776         struct proc *p;
 1777         int biglocks;
 1778 
 1779         p = l->l_proc;
 1780 
 1781         KASSERT(mutex_owned(p->p_lock));
 1782         KASSERT(!mutex_owned(&proc_lock));
 1783 
 1784         KASSERT(l->l_stat == LSONPROC);
 1785         KASSERT(p->p_nrlwps > 0);
 1786 
 1787         KERNEL_UNLOCK_ALL(l, &biglocks);
 1788         if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
 1789                 p->p_nrlwps--;
 1790                 lwp_lock(l);
 1791                 KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
 1792                 l->l_stat = LSSTOP;
 1793                 lwp_unlock(l);
 1794         }
 1795 
 1796         mutex_exit(p->p_lock);
 1797         lwp_lock(l);
 1798         spc_lock(l->l_cpu);
 1799         mi_switch(l);
 1800         KERNEL_LOCK(biglocks, l);
 1801 }
 1802 
 1803 /*
 1804  * Check for a signal from the debugger.
 1805  */
 1806 static int
 1807 sigchecktrace(void)
 1808 {
 1809         struct lwp *l = curlwp;
 1810         struct proc *p = l->l_proc;
 1811         int signo;
 1812 
 1813         KASSERT(mutex_owned(p->p_lock));
 1814 
 1815         /* If there's a pending SIGKILL, process it immediately. */
 1816         if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
 1817                 return 0;
 1818 
 1819         /*
 1820          * If we are no longer being traced, or the parent didn't
 1821          * give us a signal, or we're stopping, look for more signals.
 1822          */
 1823         if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xsig == 0 ||
 1824             (p->p_sflag & PS_STOPPING) != 0)
 1825                 return 0;
 1826 
 1827         /*
 1828          * If the new signal is being masked, look for other signals.
 1829          * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
 1830          */
 1831         signo = p->p_xsig;
 1832         p->p_xsig = 0;
 1833         if (sigismember(&l->l_sigmask, signo)) {
 1834                 signo = 0;
 1835         }
 1836         return signo;
 1837 }
 1838 
 1839 /*
 1840  * If the current process has received a signal (should be caught or cause
 1841  * termination, should interrupt current syscall), return the signal number.
 1842  *
 1843  * Stop signals with default action are processed immediately, then cleared;
 1844  * they aren't returned.  This is checked after each entry to the system for
 1845  * a syscall or trap.
 1846  *
 1847  * We will also return -1 if the process is exiting and the current LWP must
 1848  * follow suit.
 1849  */
 1850 int
 1851 issignal(struct lwp *l)
 1852 {
 1853         struct proc *p;
 1854         int siglwp, signo, prop;
 1855         sigpend_t *sp;
 1856         sigset_t ss;
 1857         bool traced;
 1858 
 1859         p = l->l_proc;
 1860         sp = NULL;
 1861         signo = 0;
 1862 
 1863         KASSERT(p == curproc);
 1864         KASSERT(mutex_owned(p->p_lock));
 1865 
 1866         for (;;) {
 1867                 /* Discard any signals that we have decided not to take. */
 1868                 if (signo != 0) {
 1869                         (void)sigget(sp, NULL, signo, NULL);
 1870                 }
 1871 
 1872                 /*
 1873                  * If the process is stopped/stopping, then stop ourselves
 1874                  * now that we're on the kernel/userspace boundary.  When
 1875                  * we awaken, check for a signal from the debugger.
 1876                  */
 1877                 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
 1878                         sigswitch_unlock_and_switch_away(l);
 1879                         mutex_enter(p->p_lock);
 1880                         continue;
 1881                 } else if (p->p_stat == SACTIVE)
 1882                         signo = sigchecktrace();
 1883                 else
 1884                         signo = 0;
 1885 
 1886                 /* Signals from the debugger are "out of band". */
 1887                 sp = NULL;
 1888 
 1889                 /*
 1890                  * If the debugger didn't provide a signal, find a pending
 1891                  * signal from our set.  Check per-LWP signals first, and
 1892                  * then per-process.
 1893                  */
 1894                 if (signo == 0) {
 1895                         sp = &l->l_sigpend;
 1896                         ss = sp->sp_set;
 1897                         siglwp = l->l_lid;
 1898                         if ((p->p_lflag & PL_PPWAIT) != 0)
 1899                                 sigminusset(&vforksigmask, &ss);
 1900                         sigminusset(&l->l_sigmask, &ss);
 1901 
 1902                         if ((signo = firstsig(&ss)) == 0) {
 1903                                 sp = &p->p_sigpend;
 1904                                 ss = sp->sp_set;
 1905                                 siglwp = 0;
 1906                                 if ((p->p_lflag & PL_PPWAIT) != 0)
 1907                                         sigminusset(&vforksigmask, &ss);
 1908                                 sigminusset(&l->l_sigmask, &ss);
 1909 
 1910                                 if ((signo = firstsig(&ss)) == 0) {
 1911                                         /*
 1912                                          * No signal pending - clear the
 1913                                          * indicator and bail out.
 1914                                          */
 1915                                         lwp_lock(l);
 1916                                         l->l_flag &= ~LW_PENDSIG;
 1917                                         lwp_unlock(l);
 1918                                         sp = NULL;
 1919                                         break;
 1920                                 }
 1921                         }
 1922                 }
 1923 
 1924                 traced = ISSET(p->p_slflag, PSL_TRACED) &&
 1925                     !sigismember(&p->p_sigctx.ps_sigpass, signo);
 1926 
 1927                 if (sp) {
 1928                         /* Overwrite process' signal context to correspond
 1929                          * to the currently reported LWP.  This is necessary
 1930                          * for PT_GET_SIGINFO to report the correct signal when
 1931                          * multiple LWPs have pending signals.  We do this only
 1932                          * when the signal comes from the queue, for signals
 1933                          * created by the debugger we assume it set correct
 1934                          * siginfo.
 1935                          */
 1936                         ksiginfo_t *ksi = TAILQ_FIRST(&sp->sp_info);
 1937                         if (ksi) {
 1938                                 p->p_sigctx.ps_lwp = ksi->ksi_lid;
 1939                                 p->p_sigctx.ps_info = ksi->ksi_info;
 1940                         } else {
 1941                                 p->p_sigctx.ps_lwp = siglwp;
 1942                                 memset(&p->p_sigctx.ps_info, 0,
 1943                                     sizeof(p->p_sigctx.ps_info));
 1944                                 p->p_sigctx.ps_info._signo = signo;
 1945                                 p->p_sigctx.ps_info._code = SI_NOINFO;
 1946                         }
 1947                 }
 1948 
 1949                 /*
 1950                  * We should see pending but ignored signals only if
 1951                  * we are being traced.
 1952                  */
 1953                 if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
 1954                     !traced) {
 1955                         /* Discard the signal. */
 1956                         continue;
 1957                 }
 1958 
 1959                 /*
 1960                  * If traced, always stop, and stay stopped until released
 1961                  * by the debugger.  If the our parent is our debugger waiting
 1962                  * for us and we vforked, don't hang as we could deadlock.
 1963                  */
 1964                 if (traced && signo != SIGKILL &&
 1965                     !(ISSET(p->p_lflag, PL_PPWAIT) &&
 1966                      (p->p_pptr == p->p_opptr))) {
 1967                         /*
 1968                          * Take the signal, but don't remove it from the
 1969                          * siginfo queue, because the debugger can send
 1970                          * it later.
 1971                          */
 1972                         if (sp)
 1973                                 sigdelset(&sp->sp_set, signo);
 1974                         p->p_xsig = signo;
 1975 
 1976                         /* Handling of signal trace */
 1977                         sigswitch(0, signo, false);
 1978                         mutex_enter(p->p_lock);
 1979 
 1980                         /* Check for a signal from the debugger. */
 1981                         if ((signo = sigchecktrace()) == 0)
 1982                                 continue;
 1983 
 1984                         /* Signals from the debugger are "out of band". */
 1985                         sp = NULL;
 1986                 }
 1987 
 1988                 prop = sigprop[signo];
 1989 
 1990                 /*
 1991                  * Decide whether the signal should be returned.
 1992                  */
 1993                 switch ((long)SIGACTION(p, signo).sa_handler) {
 1994                 case (long)SIG_DFL:
 1995                         /*
 1996                          * Don't take default actions on system processes.
 1997                          */
 1998                         if (p->p_pid <= 1) {
 1999 #ifdef DIAGNOSTIC
 2000                                 /*
 2001                                  * Are you sure you want to ignore SIGSEGV
 2002                                  * in init? XXX
 2003                                  */
 2004                                 printf_nolog("Process (pid %d) got sig %d\n",
 2005                                     p->p_pid, signo);
 2006 #endif
 2007                                 continue;
 2008                         }
 2009 
 2010                         /*
 2011                          * If there is a pending stop signal to process with
 2012                          * default action, stop here, then clear the signal.
 2013                          * However, if process is member of an orphaned
 2014                          * process group, ignore tty stop signals.
 2015                          */
 2016                         if (prop & SA_STOP) {
 2017                                 /*
 2018                                  * XXX Don't hold proc_lock for p_lflag,
 2019                                  * but it's not a big deal.
 2020                                  */
 2021                                 if ((traced &&
 2022                                      !(ISSET(p->p_lflag, PL_PPWAIT) &&
 2023                                      (p->p_pptr == p->p_opptr))) ||
 2024                                     ((p->p_lflag & PL_ORPHANPG) != 0 &&
 2025                                     prop & SA_TTYSTOP)) {
 2026                                         /* Ignore the signal. */
 2027                                         continue;
 2028                                 }
 2029                                 /* Take the signal. */
 2030                                 (void)sigget(sp, NULL, signo, NULL);
 2031                                 p->p_xsig = signo;
 2032                                 p->p_sflag &= ~PS_CONTINUED;
 2033                                 signo = 0;
 2034                                 sigswitch(PS_NOCLDSTOP, p->p_xsig, false);
 2035                                 mutex_enter(p->p_lock);
 2036                         } else if (prop & SA_IGNORE) {
 2037                                 /*
 2038                                  * Except for SIGCONT, shouldn't get here.
 2039                                  * Default action is to ignore; drop it.
 2040                                  */
 2041                                 continue;
 2042                         }
 2043                         break;
 2044 
 2045                 case (long)SIG_IGN:
 2046 #ifdef DEBUG_ISSIGNAL
 2047                         /*
 2048                          * Masking above should prevent us ever trying
 2049                          * to take action on an ignored signal other
 2050                          * than SIGCONT, unless process is traced.
 2051                          */
 2052                         if ((prop & SA_CONT) == 0 && !traced)
 2053                                 printf_nolog("issignal\n");
 2054 #endif
 2055                         continue;
 2056 
 2057                 default:
 2058                         /*
 2059                          * This signal has an action, let postsig() process
 2060                          * it.
 2061                          */
 2062                         break;
 2063                 }
 2064 
 2065                 break;
 2066         }
 2067 
 2068         l->l_sigpendset = sp;
 2069         return signo;
 2070 }
 2071 
 2072 /*
 2073  * Take the action for the specified signal
 2074  * from the current set of pending signals.
 2075  */
 2076 void
 2077 postsig(int signo)
 2078 {
 2079         struct lwp      *l;
 2080         struct proc     *p;
 2081         struct sigacts  *ps;
 2082         sig_t           action;
 2083         sigset_t        *returnmask;
 2084         ksiginfo_t      ksi;
 2085 
 2086         l = curlwp;
 2087         p = l->l_proc;
 2088         ps = p->p_sigacts;
 2089 
 2090         KASSERT(mutex_owned(p->p_lock));
 2091         KASSERT(signo > 0);
 2092 
 2093         /*
 2094          * Set the new mask value and also defer further occurrences of this
 2095          * signal.
 2096          *
 2097          * Special case: user has done a sigsuspend.  Here the current mask is
 2098          * not of interest, but rather the mask from before the sigsuspend is
 2099          * what we want restored after the signal processing is completed.
 2100          */
 2101         if (l->l_sigrestore) {
 2102                 returnmask = &l->l_sigoldmask;
 2103                 l->l_sigrestore = 0;
 2104         } else
 2105                 returnmask = &l->l_sigmask;
 2106 
 2107         /*
 2108          * Commit to taking the signal before releasing the mutex.
 2109          */
 2110         action = SIGACTION_PS(ps, signo).sa_handler;
 2111         l->l_ru.ru_nsignals++;
 2112         if (l->l_sigpendset == NULL) {
 2113                 /* From the debugger */
 2114                 if (p->p_sigctx.ps_faked &&
 2115                     signo == p->p_sigctx.ps_info._signo) {
 2116                         KSI_INIT(&ksi);
 2117                         ksi.ksi_info = p->p_sigctx.ps_info;
 2118                         ksi.ksi_lid = p->p_sigctx.ps_lwp;
 2119                         p->p_sigctx.ps_faked = false;
 2120                 } else {
 2121                         if (!siggetinfo(&l->l_sigpend, &ksi, signo))
 2122                                 (void)siggetinfo(&p->p_sigpend, &ksi, signo);
 2123                 }
 2124         } else
 2125                 sigget(l->l_sigpendset, &ksi, signo, NULL);
 2126 
 2127         if (ktrpoint(KTR_PSIG)) {
 2128                 mutex_exit(p->p_lock);
 2129                 if (p->p_emul->e_ktrpsig)
 2130                         p->p_emul->e_ktrpsig(signo, action,
 2131                             returnmask, &ksi);
 2132                 else
 2133                         ktrpsig(signo, action, returnmask, &ksi);
 2134                 mutex_enter(p->p_lock);
 2135         }
 2136 
 2137         SDT_PROBE(proc, kernel, , signal__handle, signo, &ksi, action, 0, 0);
 2138 
 2139         if (action == SIG_DFL) {
 2140                 /*
 2141                  * Default action, where the default is to kill
 2142                  * the process.  (Other cases were ignored above.)
 2143                  */
 2144                 sigexit(l, signo);
 2145                 return;
 2146         }
 2147 
 2148         /*
 2149          * If we get here, the signal must be caught.
 2150          */
 2151 #ifdef DIAGNOSTIC
 2152         if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
 2153                 panic("postsig action");
 2154 #endif
 2155 
 2156         kpsendsig(l, &ksi, returnmask);
 2157 }
 2158 
 2159 /*
 2160  * sendsig:
 2161  *
 2162  *      Default signal delivery method for NetBSD.
 2163  */
 2164 void
 2165 sendsig(const struct ksiginfo *ksi, const sigset_t *mask)
 2166 {
 2167         struct sigacts *sa;
 2168         int sig;
 2169 
 2170         sig = ksi->ksi_signo;
 2171         sa = curproc->p_sigacts;
 2172 
 2173         switch (sa->sa_sigdesc[sig].sd_vers)  {
 2174         case __SIGTRAMP_SIGCODE_VERSION:
 2175 #ifdef __HAVE_STRUCT_SIGCONTEXT
 2176         case __SIGTRAMP_SIGCONTEXT_VERSION_MIN ...
 2177              __SIGTRAMP_SIGCONTEXT_VERSION_MAX:
 2178                 /* Compat for 1.6 and earlier. */
 2179                 MODULE_HOOK_CALL_VOID(sendsig_sigcontext_16_hook, (ksi, mask),
 2180                     break);
 2181                 return;
 2182 #endif /* __HAVE_STRUCT_SIGCONTEXT */
 2183         case __SIGTRAMP_SIGINFO_VERSION_MIN ...
 2184              __SIGTRAMP_SIGINFO_VERSION_MAX:
 2185                 sendsig_siginfo(ksi, mask);
 2186                 return;
 2187         default:
 2188                 break;
 2189         }
 2190 
 2191         printf("sendsig: bad version %d\n", sa->sa_sigdesc[sig].sd_vers);
 2192         sigexit(curlwp, SIGILL);
 2193 }
 2194 
 2195 /*
 2196  * sendsig_reset:
 2197  *
 2198  *      Reset the signal action.  Called from emulation specific sendsig()
 2199  *      before unlocking to deliver the signal.
 2200  */
 2201 void
 2202 sendsig_reset(struct lwp *l, int signo)
 2203 {
 2204         struct proc *p = l->l_proc;
 2205         struct sigacts *ps = p->p_sigacts;
 2206 
 2207         KASSERT(mutex_owned(p->p_lock));
 2208 
 2209         p->p_sigctx.ps_lwp = 0;
 2210         memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
 2211 
 2212         mutex_enter(&ps->sa_mutex);
 2213         sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
 2214         if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
 2215                 sigdelset(&p->p_sigctx.ps_sigcatch, signo);
 2216                 if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
 2217                         sigaddset(&p->p_sigctx.ps_sigignore, signo);
 2218                 SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
 2219         }
 2220         mutex_exit(&ps->sa_mutex);
 2221 }
 2222 
 2223 /*
 2224  * Kill the current process for stated reason.
 2225  */
 2226 void
 2227 killproc(struct proc *p, const char *why)
 2228 {
 2229 
 2230         KASSERT(mutex_owned(&proc_lock));
 2231 
 2232         log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
 2233         uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
 2234         psignal(p, SIGKILL);
 2235 }
 2236 
 2237 /*
 2238  * Force the current process to exit with the specified signal, dumping core
 2239  * if appropriate.  We bypass the normal tests for masked and caught
 2240  * signals, allowing unrecoverable failures to terminate the process without
 2241  * changing signal state.  Mark the accounting record with the signal
 2242  * termination.  If dumping core, save the signal number for the debugger.
 2243  * Calls exit and does not return.
 2244  */
 2245 void
 2246 sigexit(struct lwp *l, int signo)
 2247 {
 2248         int exitsig, error, docore;
 2249         struct proc *p;
 2250         struct lwp *t;
 2251 
 2252         p = l->l_proc;
 2253 
 2254         KASSERT(mutex_owned(p->p_lock));
 2255         KERNEL_UNLOCK_ALL(l, NULL);
 2256 
 2257         /*
 2258          * Don't permit coredump() multiple times in the same process.
 2259          * Call back into sigexit, where we will be suspended until
 2260          * the deed is done.  Note that this is a recursive call, but
 2261          * LW_WCORE will prevent us from coming back this way.
 2262          */
 2263         if ((p->p_sflag & PS_WCORE) != 0) {
 2264                 lwp_lock(l);
 2265                 l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
 2266                 lwp_unlock(l);
 2267                 mutex_exit(p->p_lock);
 2268                 lwp_userret(l);
 2269                 panic("sigexit 1");
 2270                 /* NOTREACHED */
 2271         }
 2272 
 2273         /* If process is already on the way out, then bail now. */
 2274         if ((p->p_sflag & PS_WEXIT) != 0) {
 2275                 mutex_exit(p->p_lock);
 2276                 lwp_exit(l);
 2277                 panic("sigexit 2");
 2278                 /* NOTREACHED */
 2279         }
 2280 
 2281         /*
 2282          * Prepare all other LWPs for exit.  If dumping core, suspend them
 2283          * so that their registers are available long enough to be dumped.
 2284          */
 2285         if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
 2286                 p->p_sflag |= PS_WCORE;
 2287                 for (;;) {
 2288                         LIST_FOREACH(t, &p->p_lwps, l_sibling) {
 2289                                 lwp_lock(t);
 2290                                 if (t == l) {
 2291                                         t->l_flag &=
 2292                                             ~(LW_WSUSPEND | LW_DBGSUSPEND);
 2293                                         lwp_unlock(t);
 2294                                         continue;
 2295                                 }
 2296                                 t->l_flag |= (LW_WCORE | LW_WEXIT);
 2297                                 lwp_suspend(l, t);
 2298                         }
 2299 
 2300                         if (p->p_nrlwps == 1)
 2301                                 break;
 2302 
 2303                         /*
 2304                          * Kick any LWPs sitting in lwp_wait1(), and wait
 2305                          * for everyone else to stop before proceeding.
 2306                          */
 2307                         p->p_nlwpwait++;
 2308                         cv_broadcast(&p->p_lwpcv);
 2309                         cv_wait(&p->p_lwpcv, p->p_lock);
 2310                         p->p_nlwpwait--;
 2311                 }
 2312         }
 2313 
 2314         exitsig = signo;
 2315         p->p_acflag |= AXSIG;
 2316         memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
 2317         p->p_sigctx.ps_info._signo = signo;
 2318         p->p_sigctx.ps_info._code = SI_NOINFO;
 2319 
 2320         if (docore) {
 2321                 mutex_exit(p->p_lock);
 2322                 MODULE_HOOK_CALL(coredump_hook, (l, NULL), enosys(), error);
 2323 
 2324                 if (kern_logsigexit) {
 2325                         int uid = l->l_cred ?
 2326                             (int)kauth_cred_geteuid(l->l_cred) : -1;
 2327 
 2328                         if (error)
 2329                                 log(LOG_INFO, lognocoredump, p->p_pid,
 2330                                     p->p_comm, uid, signo, error);
 2331                         else
 2332                                 log(LOG_INFO, logcoredump, p->p_pid,
 2333                                     p->p_comm, uid, signo);
 2334                 }
 2335 
 2336 #ifdef PAX_SEGVGUARD
 2337                 rw_enter(&exec_lock, RW_WRITER);
 2338                 pax_segvguard(l, p->p_textvp, p->p_comm, true);
 2339                 rw_exit(&exec_lock);
 2340 #endif /* PAX_SEGVGUARD */
 2341 
 2342                 /* Acquire the sched state mutex.  exit1() will release it. */
 2343                 mutex_enter(p->p_lock);
 2344                 if (error == 0)
 2345                         p->p_sflag |= PS_COREDUMP;
 2346         }
 2347 
 2348         /* No longer dumping core. */
 2349         p->p_sflag &= ~PS_WCORE;
 2350 
 2351         exit1(l, 0, exitsig);
 2352         /* NOTREACHED */
 2353 }
 2354 
 2355 /*
 2356  * Since the "real" code may (or may not) be present in loadable module,
 2357  * we provide routines here which calls the module hooks.
 2358  */
 2359 
 2360 int
 2361 coredump_netbsd(struct lwp *l, struct coredump_iostate *iocookie)
 2362 {
 2363 
 2364         int retval;
 2365 
 2366         MODULE_HOOK_CALL(coredump_netbsd_hook, (l, iocookie), ENOSYS, retval);
 2367         return retval;
 2368 }
 2369 
 2370 int
 2371 coredump_netbsd32(struct lwp *l, struct coredump_iostate *iocookie)
 2372 {
 2373 
 2374         int retval;
 2375 
 2376         MODULE_HOOK_CALL(coredump_netbsd32_hook, (l, iocookie), ENOSYS, retval);
 2377         return retval;
 2378 }
 2379 
 2380 int
 2381 coredump_elf32(struct lwp *l, struct coredump_iostate *iocookie)
 2382 {
 2383         int retval;
 2384 
 2385         MODULE_HOOK_CALL(coredump_elf32_hook, (l, iocookie), ENOSYS, retval);
 2386         return retval;
 2387 }
 2388 
 2389 int
 2390 coredump_elf64(struct lwp *l, struct coredump_iostate *iocookie)
 2391 {
 2392         int retval;
 2393 
 2394         MODULE_HOOK_CALL(coredump_elf64_hook, (l, iocookie), ENOSYS, retval);
 2395         return retval;
 2396 }
 2397 
 2398 /*
 2399  * Put process 'p' into the stopped state and optionally, notify the parent.
 2400  */
 2401 void
 2402 proc_stop(struct proc *p, int signo)
 2403 {
 2404         struct lwp *l;
 2405 
 2406         KASSERT(mutex_owned(p->p_lock));
 2407 
 2408         /*
 2409          * First off, set the stopping indicator and bring all sleeping
 2410          * LWPs to a halt so they are included in p->p_nrlwps.  We musn't
 2411          * unlock between here and the p->p_nrlwps check below.
 2412          */
 2413         p->p_sflag |= PS_STOPPING;
 2414         membar_producer();
 2415 
 2416         proc_stop_lwps(p);
 2417 
 2418         /*
 2419          * If there are no LWPs available to take the signal, then we
 2420          * signal the parent process immediately.  Otherwise, the last
 2421          * LWP to stop will take care of it.
 2422          */
 2423 
 2424         if (p->p_nrlwps == 0) {
 2425                 proc_stop_done(p, PS_NOCLDSTOP);
 2426         } else {
 2427                 /*
 2428                  * Have the remaining LWPs come to a halt, and trigger
 2429                  * proc_stop_callout() to ensure that they do.
 2430                  */
 2431                 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
 2432                         sigpost(l, SIG_DFL, SA_STOP, signo);
 2433                 }
 2434                 callout_schedule(&proc_stop_ch, 1);
 2435         }
 2436 }
 2437 
 2438 /*
 2439  * When stopping a process, we do not immediately set sleeping LWPs stopped,
 2440  * but wait for them to come to a halt at the kernel-user boundary.  This is
 2441  * to allow LWPs to release any locks that they may hold before stopping.
 2442  *
 2443  * Non-interruptable sleeps can be long, and there is the potential for an
 2444  * LWP to begin sleeping interruptably soon after the process has been set
 2445  * stopping (PS_STOPPING).  These LWPs will not notice that the process is
 2446  * stopping, and so complete halt of the process and the return of status
 2447  * information to the parent could be delayed indefinitely.
 2448  *
 2449  * To handle this race, proc_stop_callout() runs once per tick while there
 2450  * are stopping processes in the system.  It sets LWPs that are sleeping
 2451  * interruptably into the LSSTOP state.
 2452  *
 2453  * Note that we are not concerned about keeping all LWPs stopped while the
 2454  * process is stopped: stopped LWPs can awaken briefly to handle signals.
 2455  * What we do need to ensure is that all LWPs in a stopping process have
 2456  * stopped at least once, so that notification can be sent to the parent
 2457  * process.
 2458  */
 2459 static void
 2460 proc_stop_callout(void *cookie)
 2461 {
 2462         bool more, restart;
 2463         struct proc *p;
 2464 
 2465         (void)cookie;
 2466 
 2467         do {
 2468                 restart = false;
 2469                 more = false;
 2470 
 2471                 mutex_enter(&proc_lock);
 2472                 PROCLIST_FOREACH(p, &allproc) {
 2473                         mutex_enter(p->p_lock);
 2474 
 2475                         if ((p->p_sflag & PS_STOPPING) == 0) {
 2476                                 mutex_exit(p->p_lock);
 2477                                 continue;
 2478                         }
 2479 
 2480                         /* Stop any LWPs sleeping interruptably. */
 2481                         proc_stop_lwps(p);
 2482                         if (p->p_nrlwps == 0) {
 2483                                 /*
 2484                                  * We brought the process to a halt.
 2485                                  * Mark it as stopped and notify the
 2486                                  * parent.
 2487                                  *
 2488                                  * Note that proc_stop_done() will
 2489                                  * drop p->p_lock briefly.
 2490                                  * Arrange to restart and check
 2491                                  * all processes again.
 2492                                  */
 2493                                 restart = true;
 2494                                 proc_stop_done(p, PS_NOCLDSTOP);
 2495                         } else
 2496                                 more = true;
 2497 
 2498                         mutex_exit(p->p_lock);
 2499                         if (restart)
 2500                                 break;
 2501                 }
 2502                 mutex_exit(&proc_lock);
 2503         } while (restart);
 2504 
 2505         /*
 2506          * If we noted processes that are stopping but still have
 2507          * running LWPs, then arrange to check again in 1 tick.
 2508          */
 2509         if (more)
 2510                 callout_schedule(&proc_stop_ch, 1);
 2511 }
 2512 
 2513 /*
 2514  * Given a process in state SSTOP, set the state back to SACTIVE and
 2515  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
 2516  */
 2517 void
 2518 proc_unstop(struct proc *p)
 2519 {
 2520         struct lwp *l;
 2521         int sig;
 2522 
 2523         KASSERT(mutex_owned(&proc_lock));
 2524         KASSERT(mutex_owned(p->p_lock));
 2525 
 2526         p->p_stat = SACTIVE;
 2527         p->p_sflag &= ~PS_STOPPING;
 2528         sig = p->p_xsig;
 2529 
 2530         if (!p->p_waited)
 2531                 p->p_pptr->p_nstopchild--;
 2532 
 2533         LIST_FOREACH(l, &p->p_lwps, l_sibling) {
 2534                 lwp_lock(l);
 2535                 if (l->l_stat != LSSTOP || (l->l_flag & LW_DBGSUSPEND) != 0) {
 2536                         lwp_unlock(l);
 2537                         continue;
 2538                 }
 2539                 if (l->l_wchan == NULL) {
 2540                         setrunnable(l);
 2541                         continue;
 2542                 }
 2543                 if (sig && (l->l_flag & LW_SINTR) != 0) {
 2544                         setrunnable(l);
 2545                         sig = 0;
 2546                 } else {
 2547                         l->l_stat = LSSLEEP;
 2548                         p->p_nrlwps++;
 2549                         lwp_unlock(l);
 2550                 }
 2551         }
 2552 }
 2553 
 2554 void
 2555 proc_stoptrace(int trapno, int sysnum, const register_t args[],
 2556                const register_t *ret, int error)
 2557 {
 2558         struct lwp *l = curlwp;
 2559         struct proc *p = l->l_proc;
 2560         struct sigacts *ps;
 2561         sigset_t *mask;
 2562         sig_t action;
 2563         ksiginfo_t ksi;
 2564         size_t i, sy_narg;
 2565         const int signo = SIGTRAP;
 2566 
 2567         KASSERT((trapno == TRAP_SCE) || (trapno == TRAP_SCX));
 2568         KASSERT(p->p_pptr != initproc);
 2569         KASSERT(ISSET(p->p_slflag, PSL_TRACED));
 2570         KASSERT(ISSET(p->p_slflag, PSL_SYSCALL));
 2571 
 2572         sy_narg = p->p_emul->e_sysent[sysnum].sy_narg;
 2573 
 2574         KSI_INIT_TRAP(&ksi);
 2575         ksi.ksi_lid = l->l_lid;
 2576         ksi.ksi_signo = signo;
 2577         ksi.ksi_code = trapno;
 2578 
 2579         ksi.ksi_sysnum = sysnum;
 2580         if (trapno == TRAP_SCE) {
 2581                 ksi.ksi_retval[0] = 0;
 2582                 ksi.ksi_retval[1] = 0;
 2583                 ksi.ksi_error = 0;
 2584         } else {
 2585                 ksi.ksi_retval[0] = ret[0];
 2586                 ksi.ksi_retval[1] = ret[1];
 2587                 ksi.ksi_error = error;
 2588         }
 2589 
 2590         memset(ksi.ksi_args, 0, sizeof(ksi.ksi_args));
 2591 
 2592         for (i = 0; i < sy_narg; i++)
 2593                 ksi.ksi_args[i] = args[i];
 2594 
 2595         mutex_enter(p->p_lock);
 2596 
 2597 repeat:
 2598         /*
 2599          * If we are exiting, demise now.
 2600          *
 2601          * This avoids notifying tracer and deadlocking.
 2602          */
 2603         if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
 2604                 mutex_exit(p->p_lock);
 2605                 lwp_exit(l);
 2606                 panic("proc_stoptrace");
 2607                 /* NOTREACHED */
 2608         }
 2609 
 2610         /*
 2611          * If there's a pending SIGKILL process it immediately.
 2612          */
 2613         if (p->p_xsig == SIGKILL ||
 2614             sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
 2615                 mutex_exit(p->p_lock);
 2616                 return;
 2617         }
 2618 
 2619         /*
 2620          * If we are no longer traced, abandon this event signal.
 2621          *
 2622          * This avoids killing a process after detaching the debugger.
 2623          */
 2624         if (__predict_false(!ISSET(p->p_slflag, PSL_TRACED))) {
 2625                 mutex_exit(p->p_lock);
 2626                 return;
 2627         }
 2628 
 2629         /*
 2630          * The process is already stopping.
 2631          */
 2632         if ((p->p_sflag & PS_STOPPING) != 0) {
 2633                 sigswitch_unlock_and_switch_away(l);
 2634                 mutex_enter(p->p_lock);
 2635                 goto repeat;
 2636         }
 2637 
 2638         /* Needed for ktrace */
 2639         ps = p->p_sigacts;
 2640         action = SIGACTION_PS(ps, signo).sa_handler;
 2641         mask = &l->l_sigmask;
 2642 
 2643         p->p_xsig = signo;
 2644         p->p_sigctx.ps_lwp = ksi.ksi_lid;
 2645         p->p_sigctx.ps_info = ksi.ksi_info;
 2646         sigswitch(0, signo, false);
 2647 
 2648         if (ktrpoint(KTR_PSIG)) {
 2649                 if (p->p_emul->e_ktrpsig)
 2650                         p->p_emul->e_ktrpsig(signo, action, mask, &ksi);
 2651                 else
 2652                         ktrpsig(signo, action, mask, &ksi);
 2653         }
 2654 }
 2655 
 2656 static int
 2657 filt_sigattach(struct knote *kn)
 2658 {
 2659         struct proc *p = curproc;
 2660 
 2661         kn->kn_obj = p;
 2662         kn->kn_flags |= EV_CLEAR;       /* automatically set */
 2663 
 2664         mutex_enter(p->p_lock);
 2665         klist_insert(&p->p_klist, kn);
 2666         mutex_exit(p->p_lock);
 2667 
 2668         return 0;
 2669 }
 2670 
 2671 static void
 2672 filt_sigdetach(struct knote *kn)
 2673 {
 2674         struct proc *p = kn->kn_obj;
 2675 
 2676         mutex_enter(p->p_lock);
 2677         klist_remove(&p->p_klist, kn);
 2678         mutex_exit(p->p_lock);
 2679 }
 2680 
 2681 /*
 2682  * Signal knotes are shared with proc knotes, so we apply a mask to
 2683  * the hint in order to differentiate them from process hints.  This
 2684  * could be avoided by using a signal-specific knote list, but probably
 2685  * isn't worth the trouble.
 2686  */
 2687 static int
 2688 filt_signal(struct knote *kn, long hint)
 2689 {
 2690 
 2691         if (hint & NOTE_SIGNAL) {
 2692                 hint &= ~NOTE_SIGNAL;
 2693 
 2694                 if (kn->kn_id == hint)
 2695                         kn->kn_data++;
 2696         }
 2697         return (kn->kn_data != 0);
 2698 }
 2699 
 2700 const struct filterops sig_filtops = {
 2701         .f_flags = FILTEROP_MPSAFE,
 2702         .f_attach = filt_sigattach,
 2703         .f_detach = filt_sigdetach,
 2704         .f_event = filt_signal,
 2705 };

Cache object: fc4bc6324be831ae6cb555466eb31c7e


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