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