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