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