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