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