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