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