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