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