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