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