xref: /netbsd-src/sys/kern/kern_sig.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /*	$NetBSD: kern_sig.c,v 1.319 2013/11/22 21:04:11 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1989, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  * (c) UNIX System Laboratories, Inc.
36  * All or some portions of this file are derived from material licensed
37  * to the University of California by American Telephone and Telegraph
38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39  * the permission of UNIX System Laboratories, Inc.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
66  */
67 
68 /*
69  * Signal subsystem.
70  */
71 
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.319 2013/11/22 21:04:11 christos 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,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,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,signal-clear,
145 	    "int", NULL,		/* signal */
146 	    NULL, NULL, NULL, NULL,
147 	    NULL, NULL, NULL, NULL);
148 SDT_PROBE_DEFINE(proc,,,signal_handle,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(!TAILQ_EMPTY(kq));
523 
524 	while (!TAILQ_EMPTY(kq)) {
525 		ksi = TAILQ_FIRST(kq);
526 		TAILQ_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 	TAILQ_FOREACH(ksi, &sp->sp_info, ksi_list) {
541 		if (ksi->ksi_signo != signo)
542 			continue;
543 		TAILQ_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 		TAILQ_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 	TAILQ_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 	TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, next) {
658 		if (mask == NULL || sigismember(mask, ksi->ksi_signo)) {
659 			TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
660 			KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
661 			KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
662 			TAILQ_INSERT_TAIL(kq, ksi, ksi_list);
663 		}
664 	}
665 }
666 
667 /*
668  * sigclearall:
669  *
670  *	Clear all pending signals in the specified set from a process and
671  *	its LWPs.
672  */
673 void
674 sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
675 {
676 	struct lwp *l;
677 
678 	KASSERT(mutex_owned(p->p_lock));
679 
680 	sigclear(&p->p_sigpend, mask, kq);
681 
682 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
683 		sigclear(&l->l_sigpend, mask, kq);
684 	}
685 }
686 
687 /*
688  * sigispending:
689  *
690  *	Return the first signal number if there are pending signals for the
691  *	current LWP.  May be called unlocked provided that LW_PENDSIG is set,
692  *	and that the signal has been posted to the appopriate queue before
693  *	LW_PENDSIG is set.
694  */
695 int
696 sigispending(struct lwp *l, int signo)
697 {
698 	struct proc *p = l->l_proc;
699 	sigset_t tset;
700 
701 	membar_consumer();
702 
703 	tset = l->l_sigpend.sp_set;
704 	sigplusset(&p->p_sigpend.sp_set, &tset);
705 	sigminusset(&p->p_sigctx.ps_sigignore, &tset);
706 	sigminusset(&l->l_sigmask, &tset);
707 
708 	if (signo == 0) {
709 		return firstsig(&tset);
710 	}
711 	return sigismember(&tset, signo) ? signo : 0;
712 }
713 
714 void
715 getucontext(struct lwp *l, ucontext_t *ucp)
716 {
717 	struct proc *p = l->l_proc;
718 
719 	KASSERT(mutex_owned(p->p_lock));
720 
721 	ucp->uc_flags = 0;
722 	ucp->uc_link = l->l_ctxlink;
723 	ucp->uc_sigmask = l->l_sigmask;
724 	ucp->uc_flags |= _UC_SIGMASK;
725 
726 	/*
727 	 * The (unsupplied) definition of the `current execution stack'
728 	 * in the System V Interface Definition appears to allow returning
729 	 * the main context stack.
730 	 */
731 	if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
732 		ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase;
733 		ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
734 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
735 	} else {
736 		/* Simply copy alternate signal execution stack. */
737 		ucp->uc_stack = l->l_sigstk;
738 	}
739 	ucp->uc_flags |= _UC_STACK;
740 	mutex_exit(p->p_lock);
741 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
742 	mutex_enter(p->p_lock);
743 }
744 
745 int
746 setucontext(struct lwp *l, const ucontext_t *ucp)
747 {
748 	struct proc *p = l->l_proc;
749 	int error;
750 
751 	KASSERT(mutex_owned(p->p_lock));
752 
753 	if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
754 		error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
755 		if (error != 0)
756 			return error;
757 	}
758 
759 	mutex_exit(p->p_lock);
760 	error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags);
761 	mutex_enter(p->p_lock);
762 	if (error != 0)
763 		return (error);
764 
765 	l->l_ctxlink = ucp->uc_link;
766 
767 	/*
768 	 * If there was stack information, update whether or not we are
769 	 * still running on an alternate signal stack.
770 	 */
771 	if ((ucp->uc_flags & _UC_STACK) != 0) {
772 		if (ucp->uc_stack.ss_flags & SS_ONSTACK)
773 			l->l_sigstk.ss_flags |= SS_ONSTACK;
774 		else
775 			l->l_sigstk.ss_flags &= ~SS_ONSTACK;
776 	}
777 
778 	return 0;
779 }
780 
781 /*
782  * killpg1: common code for kill process group/broadcast kill.
783  */
784 int
785 killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
786 {
787 	struct proc	*p, *cp;
788 	kauth_cred_t	pc;
789 	struct pgrp	*pgrp;
790 	int		nfound;
791 	int		signo = ksi->ksi_signo;
792 
793 	cp = l->l_proc;
794 	pc = l->l_cred;
795 	nfound = 0;
796 
797 	mutex_enter(proc_lock);
798 	if (all) {
799 		/*
800 		 * Broadcast.
801 		 */
802 		PROCLIST_FOREACH(p, &allproc) {
803 			if (p->p_pid <= 1 || p == cp ||
804 			    (p->p_flag & PK_SYSTEM) != 0)
805 				continue;
806 			mutex_enter(p->p_lock);
807 			if (kauth_authorize_process(pc,
808 			    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL,
809 			    NULL) == 0) {
810 				nfound++;
811 				if (signo)
812 					kpsignal2(p, ksi);
813 			}
814 			mutex_exit(p->p_lock);
815 		}
816 	} else {
817 		if (pgid == 0)
818 			/* Zero pgid means send to my process group. */
819 			pgrp = cp->p_pgrp;
820 		else {
821 			pgrp = pgrp_find(pgid);
822 			if (pgrp == NULL)
823 				goto out;
824 		}
825 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
826 			if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM)
827 				continue;
828 			mutex_enter(p->p_lock);
829 			if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL,
830 			    p, KAUTH_ARG(signo), NULL, NULL) == 0) {
831 				nfound++;
832 				if (signo && P_ZOMBIE(p) == 0)
833 					kpsignal2(p, ksi);
834 			}
835 			mutex_exit(p->p_lock);
836 		}
837 	}
838 out:
839 	mutex_exit(proc_lock);
840 	return nfound ? 0 : ESRCH;
841 }
842 
843 /*
844  * Send a signal to a process group.  If checktty is set, limit to members
845  * which have a controlling terminal.
846  */
847 void
848 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
849 {
850 	ksiginfo_t ksi;
851 
852 	KASSERT(!cpu_intr_p());
853 	KASSERT(mutex_owned(proc_lock));
854 
855 	KSI_INIT_EMPTY(&ksi);
856 	ksi.ksi_signo = sig;
857 	kpgsignal(pgrp, &ksi, NULL, checkctty);
858 }
859 
860 void
861 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
862 {
863 	struct proc *p;
864 
865 	KASSERT(!cpu_intr_p());
866 	KASSERT(mutex_owned(proc_lock));
867 	KASSERT(pgrp != NULL);
868 
869 	LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
870 		if (checkctty == 0 || p->p_lflag & PL_CONTROLT)
871 			kpsignal(p, ksi, data);
872 }
873 
874 /*
875  * Send a signal caused by a trap to the current LWP.  If it will be caught
876  * immediately, deliver it with correct code.  Otherwise, post it normally.
877  */
878 void
879 trapsignal(struct lwp *l, ksiginfo_t *ksi)
880 {
881 	struct proc	*p;
882 	struct sigacts	*ps;
883 	int signo = ksi->ksi_signo;
884 	sigset_t *mask;
885 
886 	KASSERT(KSI_TRAP_P(ksi));
887 
888 	ksi->ksi_lid = l->l_lid;
889 	p = l->l_proc;
890 
891 	KASSERT(!cpu_intr_p());
892 	mutex_enter(proc_lock);
893 	mutex_enter(p->p_lock);
894 	mask = &l->l_sigmask;
895 	ps = p->p_sigacts;
896 
897 	if ((p->p_slflag & PSL_TRACED) == 0 &&
898 	    sigismember(&p->p_sigctx.ps_sigcatch, signo) &&
899 	    !sigismember(mask, signo)) {
900 		mutex_exit(proc_lock);
901 		l->l_ru.ru_nsignals++;
902 		kpsendsig(l, ksi, mask);
903 		mutex_exit(p->p_lock);
904 		ktrpsig(signo, SIGACTION_PS(ps, signo).sa_handler, mask, ksi);
905 	} else {
906 		/* XXX for core dump/debugger */
907 		p->p_sigctx.ps_lwp = l->l_lid;
908 		p->p_sigctx.ps_signo = ksi->ksi_signo;
909 		p->p_sigctx.ps_code = ksi->ksi_trap;
910 		kpsignal2(p, ksi);
911 		mutex_exit(p->p_lock);
912 		mutex_exit(proc_lock);
913 	}
914 }
915 
916 /*
917  * Fill in signal information and signal the parent for a child status change.
918  */
919 void
920 child_psignal(struct proc *p, int mask)
921 {
922 	ksiginfo_t ksi;
923 	struct proc *q;
924 	int xstat;
925 
926 	KASSERT(mutex_owned(proc_lock));
927 	KASSERT(mutex_owned(p->p_lock));
928 
929 	xstat = p->p_xstat;
930 
931 	KSI_INIT(&ksi);
932 	ksi.ksi_signo = SIGCHLD;
933 	ksi.ksi_code = (xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
934 	ksi.ksi_pid = p->p_pid;
935 	ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
936 	ksi.ksi_status = xstat;
937 	ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
938 	ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
939 
940 	q = p->p_pptr;
941 
942 	mutex_exit(p->p_lock);
943 	mutex_enter(q->p_lock);
944 
945 	if ((q->p_sflag & mask) == 0)
946 		kpsignal2(q, &ksi);
947 
948 	mutex_exit(q->p_lock);
949 	mutex_enter(p->p_lock);
950 }
951 
952 void
953 psignal(struct proc *p, int signo)
954 {
955 	ksiginfo_t ksi;
956 
957 	KASSERT(!cpu_intr_p());
958 	KASSERT(mutex_owned(proc_lock));
959 
960 	KSI_INIT_EMPTY(&ksi);
961 	ksi.ksi_signo = signo;
962 	mutex_enter(p->p_lock);
963 	kpsignal2(p, &ksi);
964 	mutex_exit(p->p_lock);
965 }
966 
967 void
968 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
969 {
970 	fdfile_t *ff;
971 	file_t *fp;
972 	fdtab_t *dt;
973 
974 	KASSERT(!cpu_intr_p());
975 	KASSERT(mutex_owned(proc_lock));
976 
977 	if ((p->p_sflag & PS_WEXIT) == 0 && data) {
978 		size_t fd;
979 		filedesc_t *fdp = p->p_fd;
980 
981 		/* XXXSMP locking */
982 		ksi->ksi_fd = -1;
983 		dt = fdp->fd_dt;
984 		for (fd = 0; fd < dt->dt_nfiles; fd++) {
985 			if ((ff = dt->dt_ff[fd]) == NULL)
986 				continue;
987 			if ((fp = ff->ff_file) == NULL)
988 				continue;
989 			if (fp->f_data == data) {
990 				ksi->ksi_fd = fd;
991 				break;
992 			}
993 		}
994 	}
995 	mutex_enter(p->p_lock);
996 	kpsignal2(p, ksi);
997 	mutex_exit(p->p_lock);
998 }
999 
1000 /*
1001  * sigismasked:
1002  *
1003  *	Returns true if signal is ignored or masked for the specified LWP.
1004  */
1005 int
1006 sigismasked(struct lwp *l, int sig)
1007 {
1008 	struct proc *p = l->l_proc;
1009 
1010 	return sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1011 	    sigismember(&l->l_sigmask, sig);
1012 }
1013 
1014 /*
1015  * sigpost:
1016  *
1017  *	Post a pending signal to an LWP.  Returns non-zero if the LWP may
1018  *	be able to take the signal.
1019  */
1020 static int
1021 sigpost(struct lwp *l, sig_t action, int prop, int sig)
1022 {
1023 	int rv, masked;
1024 	struct proc *p = l->l_proc;
1025 
1026 	KASSERT(mutex_owned(p->p_lock));
1027 
1028 	/*
1029 	 * If the LWP is on the way out, sigclear() will be busy draining all
1030 	 * pending signals.  Don't give it more.
1031 	 */
1032 	if (l->l_refcnt == 0)
1033 		return 0;
1034 
1035 	SDT_PROBE(proc,,,signal_send, l, p, sig,  0, 0);
1036 
1037 	/*
1038 	 * Have the LWP check for signals.  This ensures that even if no LWP
1039 	 * is found to take the signal immediately, it should be taken soon.
1040 	 */
1041 	lwp_lock(l);
1042 	l->l_flag |= LW_PENDSIG;
1043 
1044 	/*
1045 	 * SIGCONT can be masked, but if LWP is stopped, it needs restart.
1046 	 * Note: SIGKILL and SIGSTOP cannot be masked.
1047 	 */
1048 	masked = sigismember(&l->l_sigmask, sig);
1049 	if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
1050 		lwp_unlock(l);
1051 		return 0;
1052 	}
1053 
1054 	/*
1055 	 * If killing the process, make it run fast.
1056 	 */
1057 	if (__predict_false((prop & SA_KILL) != 0) &&
1058 	    action == SIG_DFL && l->l_priority < MAXPRI_USER) {
1059 		KASSERT(l->l_class == SCHED_OTHER);
1060 		lwp_changepri(l, MAXPRI_USER);
1061 	}
1062 
1063 	/*
1064 	 * If the LWP is running or on a run queue, then we win.  If it's
1065 	 * sleeping interruptably, wake it and make it take the signal.  If
1066 	 * the sleep isn't interruptable, then the chances are it will get
1067 	 * to see the signal soon anyhow.  If suspended, it can't take the
1068 	 * signal right now.  If it's LWP private or for all LWPs, save it
1069 	 * for later; otherwise punt.
1070 	 */
1071 	rv = 0;
1072 
1073 	switch (l->l_stat) {
1074 	case LSRUN:
1075 	case LSONPROC:
1076 		lwp_need_userret(l);
1077 		rv = 1;
1078 		break;
1079 
1080 	case LSSLEEP:
1081 		if ((l->l_flag & LW_SINTR) != 0) {
1082 			/* setrunnable() will release the lock. */
1083 			setrunnable(l);
1084 			return 1;
1085 		}
1086 		break;
1087 
1088 	case LSSUSPENDED:
1089 		if ((prop & SA_KILL) != 0 && (l->l_flag & LW_WCORE) != 0) {
1090 			/* lwp_continue() will release the lock. */
1091 			lwp_continue(l);
1092 			return 1;
1093 		}
1094 		break;
1095 
1096 	case LSSTOP:
1097 		if ((prop & SA_STOP) != 0)
1098 			break;
1099 
1100 		/*
1101 		 * If the LWP is stopped and we are sending a continue
1102 		 * signal, then start it again.
1103 		 */
1104 		if ((prop & SA_CONT) != 0) {
1105 			if (l->l_wchan != NULL) {
1106 				l->l_stat = LSSLEEP;
1107 				p->p_nrlwps++;
1108 				rv = 1;
1109 				break;
1110 			}
1111 			/* setrunnable() will release the lock. */
1112 			setrunnable(l);
1113 			return 1;
1114 		} else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
1115 			/* setrunnable() will release the lock. */
1116 			setrunnable(l);
1117 			return 1;
1118 		}
1119 		break;
1120 
1121 	default:
1122 		break;
1123 	}
1124 
1125 	lwp_unlock(l);
1126 	return rv;
1127 }
1128 
1129 /*
1130  * Notify an LWP that it has a pending signal.
1131  */
1132 void
1133 signotify(struct lwp *l)
1134 {
1135 	KASSERT(lwp_locked(l, NULL));
1136 
1137 	l->l_flag |= LW_PENDSIG;
1138 	lwp_need_userret(l);
1139 }
1140 
1141 /*
1142  * Find an LWP within process p that is waiting on signal ksi, and hand
1143  * it on.
1144  */
1145 static int
1146 sigunwait(struct proc *p, const ksiginfo_t *ksi)
1147 {
1148 	struct lwp *l;
1149 	int signo;
1150 
1151 	KASSERT(mutex_owned(p->p_lock));
1152 
1153 	signo = ksi->ksi_signo;
1154 
1155 	if (ksi->ksi_lid != 0) {
1156 		/*
1157 		 * Signal came via _lwp_kill().  Find the LWP and see if
1158 		 * it's interested.
1159 		 */
1160 		if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
1161 			return 0;
1162 		if (l->l_sigwaited == NULL ||
1163 		    !sigismember(&l->l_sigwaitset, signo))
1164 			return 0;
1165 	} else {
1166 		/*
1167 		 * Look for any LWP that may be interested.
1168 		 */
1169 		LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
1170 			KASSERT(l->l_sigwaited != NULL);
1171 			if (sigismember(&l->l_sigwaitset, signo))
1172 				break;
1173 		}
1174 	}
1175 
1176 	if (l != NULL) {
1177 		l->l_sigwaited->ksi_info = ksi->ksi_info;
1178 		l->l_sigwaited = NULL;
1179 		LIST_REMOVE(l, l_sigwaiter);
1180 		cv_signal(&l->l_sigcv);
1181 		return 1;
1182 	}
1183 
1184 	return 0;
1185 }
1186 
1187 /*
1188  * Send the signal to the process.  If the signal has an action, the action
1189  * is usually performed by the target process rather than the caller; we add
1190  * the signal to the set of pending signals for the process.
1191  *
1192  * Exceptions:
1193  *   o When a stop signal is sent to a sleeping process that takes the
1194  *     default action, the process is stopped without awakening it.
1195  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1196  *     regardless of the signal action (eg, blocked or ignored).
1197  *
1198  * Other ignored signals are discarded immediately.
1199  */
1200 void
1201 kpsignal2(struct proc *p, ksiginfo_t *ksi)
1202 {
1203 	int prop, signo = ksi->ksi_signo;
1204 	struct sigacts *sa;
1205 	struct lwp *l;
1206 	ksiginfo_t *kp;
1207 	lwpid_t lid;
1208 	sig_t action;
1209 	bool toall;
1210 
1211 	KASSERT(!cpu_intr_p());
1212 	KASSERT(mutex_owned(proc_lock));
1213 	KASSERT(mutex_owned(p->p_lock));
1214 	KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
1215 	KASSERT(signo > 0 && signo < NSIG);
1216 
1217 	/*
1218 	 * If the process is being created by fork, is a zombie or is
1219 	 * exiting, then just drop the signal here and bail out.
1220 	 */
1221 	if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1222 		return;
1223 
1224 	/*
1225 	 * Notify any interested parties of the signal.
1226 	 */
1227 	KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
1228 
1229 	/*
1230 	 * Some signals including SIGKILL must act on the entire process.
1231 	 */
1232 	kp = NULL;
1233 	prop = sigprop[signo];
1234 	toall = ((prop & SA_TOALL) != 0);
1235 	lid = toall ? 0 : ksi->ksi_lid;
1236 
1237 	/*
1238 	 * If proc is traced, always give parent a chance.
1239 	 */
1240 	if (p->p_slflag & PSL_TRACED) {
1241 		action = SIG_DFL;
1242 
1243 		if (lid == 0) {
1244 			/*
1245 			 * If the process is being traced and the signal
1246 			 * is being caught, make sure to save any ksiginfo.
1247 			 */
1248 			if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1249 				return;
1250 			sigput(&p->p_sigpend, p, kp);
1251 		}
1252 	} else {
1253 		/*
1254 		 * If the signal was the result of a trap and is not being
1255 		 * caught, then reset it to default action so that the
1256 		 * process dumps core immediately.
1257 		 */
1258 		if (KSI_TRAP_P(ksi)) {
1259 			sa = p->p_sigacts;
1260 			mutex_enter(&sa->sa_mutex);
1261 			if (!sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
1262 				sigdelset(&p->p_sigctx.ps_sigignore, signo);
1263 				SIGACTION(p, signo).sa_handler = SIG_DFL;
1264 			}
1265 			mutex_exit(&sa->sa_mutex);
1266 		}
1267 
1268 		/*
1269 		 * If the signal is being ignored, then drop it.  Note: we
1270 		 * don't set SIGCONT in ps_sigignore, and if it is set to
1271 		 * SIG_IGN, action will be SIG_DFL here.
1272 		 */
1273 		if (sigismember(&p->p_sigctx.ps_sigignore, signo))
1274 			return;
1275 
1276 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
1277 			action = SIG_CATCH;
1278 		else {
1279 			action = SIG_DFL;
1280 
1281 			/*
1282 			 * If sending a tty stop signal to a member of an
1283 			 * orphaned process group, discard the signal here if
1284 			 * the action is default; don't stop the process below
1285 			 * if sleeping, and don't clear any pending SIGCONT.
1286 			 */
1287 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1288 				return;
1289 
1290 			if (prop & SA_KILL && p->p_nice > NZERO)
1291 				p->p_nice = NZERO;
1292 		}
1293 	}
1294 
1295 	/*
1296 	 * If stopping or continuing a process, discard any pending
1297 	 * signals that would do the inverse.
1298 	 */
1299 	if ((prop & (SA_CONT | SA_STOP)) != 0) {
1300 		ksiginfoq_t kq;
1301 
1302 		ksiginfo_queue_init(&kq);
1303 		if ((prop & SA_CONT) != 0)
1304 			sigclear(&p->p_sigpend, &stopsigmask, &kq);
1305 		if ((prop & SA_STOP) != 0)
1306 			sigclear(&p->p_sigpend, &contsigmask, &kq);
1307 		ksiginfo_queue_drain(&kq);	/* XXXSMP */
1308 	}
1309 
1310 	/*
1311 	 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1312 	 * please!), check if any LWPs are waiting on it.  If yes, pass on
1313 	 * the signal info.  The signal won't be processed further here.
1314 	 */
1315 	if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
1316 	    p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
1317 	    sigunwait(p, ksi))
1318 		return;
1319 
1320 	/*
1321 	 * XXXSMP Should be allocated by the caller, we're holding locks
1322 	 * here.
1323 	 */
1324 	if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1325 		return;
1326 
1327 	/*
1328 	 * LWP private signals are easy - just find the LWP and post
1329 	 * the signal to it.
1330 	 */
1331 	if (lid != 0) {
1332 		l = lwp_find(p, lid);
1333 		if (l != NULL) {
1334 			sigput(&l->l_sigpend, p, kp);
1335 			membar_producer();
1336 			(void)sigpost(l, action, prop, kp->ksi_signo);
1337 		}
1338 		goto out;
1339 	}
1340 
1341 	/*
1342 	 * Some signals go to all LWPs, even if posted with _lwp_kill()
1343 	 * or for an SA process.
1344 	 */
1345 	if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1346 		if ((p->p_slflag & PSL_TRACED) != 0)
1347 			goto deliver;
1348 
1349 		/*
1350 		 * If SIGCONT is default (or ignored) and process is
1351 		 * asleep, we are finished; the process should not
1352 		 * be awakened.
1353 		 */
1354 		if ((prop & SA_CONT) != 0 && action == SIG_DFL)
1355 			goto out;
1356 	} else {
1357 		/*
1358 		 * Process is stopped or stopping.
1359 		 * - If traced, then no action is needed, unless killing.
1360 		 * - Run the process only if sending SIGCONT or SIGKILL.
1361 		 */
1362 		if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL) {
1363 			goto out;
1364 		}
1365 		if ((prop & SA_CONT) != 0 || signo == SIGKILL) {
1366 			/*
1367 			 * Re-adjust p_nstopchild if the process wasn't
1368 			 * collected by its parent.
1369 			 */
1370 			p->p_stat = SACTIVE;
1371 			p->p_sflag &= ~PS_STOPPING;
1372 			if (!p->p_waited) {
1373 				p->p_pptr->p_nstopchild--;
1374 			}
1375 			if (p->p_slflag & PSL_TRACED) {
1376 				KASSERT(signo == SIGKILL);
1377 				goto deliver;
1378 			}
1379 			/*
1380 			 * Do not make signal pending if SIGCONT is default.
1381 			 *
1382 			 * If the process catches SIGCONT, let it handle the
1383 			 * signal itself (if waiting on event - process runs,
1384 			 * otherwise continues sleeping).
1385 			 */
1386 			if ((prop & SA_CONT) != 0 && action == SIG_DFL) {
1387 				KASSERT(signo != SIGKILL);
1388 				goto deliver;
1389 			}
1390 		} else if ((prop & SA_STOP) != 0) {
1391 			/*
1392 			 * Already stopped, don't need to stop again.
1393 			 * (If we did the shell could get confused.)
1394 			 */
1395 			goto out;
1396 		}
1397 	}
1398 	/*
1399 	 * Make signal pending.
1400 	 */
1401 	KASSERT((p->p_slflag & PSL_TRACED) == 0);
1402 	sigput(&p->p_sigpend, p, kp);
1403 
1404 deliver:
1405 	/*
1406 	 * Before we set LW_PENDSIG on any LWP, ensure that the signal is
1407 	 * visible on the per process list (for sigispending()).  This
1408 	 * is unlikely to be needed in practice, but...
1409 	 */
1410 	membar_producer();
1411 
1412 	/*
1413 	 * Try to find an LWP that can take the signal.
1414 	 */
1415 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1416 		if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
1417 			break;
1418 	}
1419 out:
1420 	/*
1421 	 * If the ksiginfo wasn't used, then bin it.  XXXSMP freeing memory
1422 	 * with locks held.  The caller should take care of this.
1423 	 */
1424 	ksiginfo_free(kp);
1425 }
1426 
1427 void
1428 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1429 {
1430 	struct proc *p = l->l_proc;
1431 
1432 	KASSERT(mutex_owned(p->p_lock));
1433 	(*p->p_emul->e_sendsig)(ksi, mask);
1434 }
1435 
1436 /*
1437  * Stop any LWPs sleeping interruptably.
1438  */
1439 static void
1440 proc_stop_lwps(struct proc *p)
1441 {
1442 	struct lwp *l;
1443 
1444 	KASSERT(mutex_owned(p->p_lock));
1445 	KASSERT((p->p_sflag & PS_STOPPING) != 0);
1446 
1447 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1448 		lwp_lock(l);
1449 		if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
1450 			l->l_stat = LSSTOP;
1451 			p->p_nrlwps--;
1452 		}
1453 		lwp_unlock(l);
1454 	}
1455 }
1456 
1457 /*
1458  * Finish stopping of a process.  Mark it stopped and notify the parent.
1459  *
1460  * Drop p_lock briefly if PS_NOTIFYSTOP is set and ppsig is true.
1461  */
1462 static void
1463 proc_stop_done(struct proc *p, bool ppsig, int ppmask)
1464 {
1465 
1466 	KASSERT(mutex_owned(proc_lock));
1467 	KASSERT(mutex_owned(p->p_lock));
1468 	KASSERT((p->p_sflag & PS_STOPPING) != 0);
1469 	KASSERT(p->p_nrlwps == 0 || (p->p_nrlwps == 1 && p == curproc));
1470 
1471 	p->p_sflag &= ~PS_STOPPING;
1472 	p->p_stat = SSTOP;
1473 	p->p_waited = 0;
1474 	p->p_pptr->p_nstopchild++;
1475 	if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
1476 		if (ppsig) {
1477 			/* child_psignal drops p_lock briefly. */
1478 			child_psignal(p, ppmask);
1479 		}
1480 		cv_broadcast(&p->p_pptr->p_waitcv);
1481 	}
1482 }
1483 
1484 /*
1485  * Stop the current process and switch away when being stopped or traced.
1486  */
1487 static void
1488 sigswitch(bool ppsig, int ppmask, int signo)
1489 {
1490 	struct lwp *l = curlwp;
1491 	struct proc *p = l->l_proc;
1492 	int biglocks;
1493 
1494 	KASSERT(mutex_owned(p->p_lock));
1495 	KASSERT(l->l_stat == LSONPROC);
1496 	KASSERT(p->p_nrlwps > 0);
1497 
1498 	/*
1499 	 * On entry we know that the process needs to stop.  If it's
1500 	 * the result of a 'sideways' stop signal that has been sourced
1501 	 * through issignal(), then stop other LWPs in the process too.
1502 	 */
1503 	if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
1504 		KASSERT(signo != 0);
1505 		proc_stop(p, 1, signo);
1506 		KASSERT(p->p_nrlwps > 0);
1507 	}
1508 
1509 	/*
1510 	 * If we are the last live LWP, and the stop was a result of
1511 	 * a new signal, then signal the parent.
1512 	 */
1513 	if ((p->p_sflag & PS_STOPPING) != 0) {
1514 		if (!mutex_tryenter(proc_lock)) {
1515 			mutex_exit(p->p_lock);
1516 			mutex_enter(proc_lock);
1517 			mutex_enter(p->p_lock);
1518 		}
1519 
1520 		if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
1521 			/*
1522 			 * Note that proc_stop_done() can drop
1523 			 * p->p_lock briefly.
1524 			 */
1525 			proc_stop_done(p, ppsig, ppmask);
1526 		}
1527 
1528 		mutex_exit(proc_lock);
1529 	}
1530 
1531 	/*
1532 	 * Unlock and switch away.
1533 	 */
1534 	KERNEL_UNLOCK_ALL(l, &biglocks);
1535 	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1536 		p->p_nrlwps--;
1537 		lwp_lock(l);
1538 		KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
1539 		l->l_stat = LSSTOP;
1540 		lwp_unlock(l);
1541 	}
1542 
1543 	mutex_exit(p->p_lock);
1544 	lwp_lock(l);
1545 	mi_switch(l);
1546 	KERNEL_LOCK(biglocks, l);
1547 	mutex_enter(p->p_lock);
1548 }
1549 
1550 /*
1551  * Check for a signal from the debugger.
1552  */
1553 static int
1554 sigchecktrace(void)
1555 {
1556 	struct lwp *l = curlwp;
1557 	struct proc *p = l->l_proc;
1558 	int signo;
1559 
1560 	KASSERT(mutex_owned(p->p_lock));
1561 
1562 	/* If there's a pending SIGKILL, process it immediately. */
1563 	if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
1564 		return 0;
1565 
1566 	/*
1567 	 * If we are no longer being traced, or the parent didn't
1568 	 * give us a signal, or we're stopping, look for more signals.
1569 	 */
1570 	if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xstat == 0 ||
1571 	    (p->p_sflag & PS_STOPPING) != 0)
1572 		return 0;
1573 
1574 	/*
1575 	 * If the new signal is being masked, look for other signals.
1576 	 * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
1577 	 */
1578 	signo = p->p_xstat;
1579 	p->p_xstat = 0;
1580 	if (sigismember(&l->l_sigmask, signo)) {
1581 		signo = 0;
1582 	}
1583 	return signo;
1584 }
1585 
1586 /*
1587  * If the current process has received a signal (should be caught or cause
1588  * termination, should interrupt current syscall), return the signal number.
1589  *
1590  * Stop signals with default action are processed immediately, then cleared;
1591  * they aren't returned.  This is checked after each entry to the system for
1592  * a syscall or trap.
1593  *
1594  * We will also return -1 if the process is exiting and the current LWP must
1595  * follow suit.
1596  */
1597 int
1598 issignal(struct lwp *l)
1599 {
1600 	struct proc *p;
1601 	int signo, prop;
1602 	sigpend_t *sp;
1603 	sigset_t ss;
1604 
1605 	p = l->l_proc;
1606 	sp = NULL;
1607 	signo = 0;
1608 
1609 	KASSERT(p == curproc);
1610 	KASSERT(mutex_owned(p->p_lock));
1611 
1612 	for (;;) {
1613 		/* Discard any signals that we have decided not to take. */
1614 		if (signo != 0) {
1615 			(void)sigget(sp, NULL, signo, NULL);
1616 		}
1617 
1618 		/*
1619 		 * If the process is stopped/stopping, then stop ourselves
1620 		 * now that we're on the kernel/userspace boundary.  When
1621 		 * we awaken, check for a signal from the debugger.
1622 		 */
1623 		if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1624 			sigswitch(true, PS_NOCLDSTOP, 0);
1625 			signo = sigchecktrace();
1626 		} else
1627 			signo = 0;
1628 
1629 		/* Signals from the debugger are "out of band". */
1630 		sp = NULL;
1631 
1632 		/*
1633 		 * If the debugger didn't provide a signal, find a pending
1634 		 * signal from our set.  Check per-LWP signals first, and
1635 		 * then per-process.
1636 		 */
1637 		if (signo == 0) {
1638 			sp = &l->l_sigpend;
1639 			ss = sp->sp_set;
1640 			if ((p->p_lflag & PL_PPWAIT) != 0)
1641 				sigminusset(&stopsigmask, &ss);
1642 			sigminusset(&l->l_sigmask, &ss);
1643 
1644 			if ((signo = firstsig(&ss)) == 0) {
1645 				sp = &p->p_sigpend;
1646 				ss = sp->sp_set;
1647 				if ((p->p_lflag & PL_PPWAIT) != 0)
1648 					sigminusset(&stopsigmask, &ss);
1649 				sigminusset(&l->l_sigmask, &ss);
1650 
1651 				if ((signo = firstsig(&ss)) == 0) {
1652 					/*
1653 					 * No signal pending - clear the
1654 					 * indicator and bail out.
1655 					 */
1656 					lwp_lock(l);
1657 					l->l_flag &= ~LW_PENDSIG;
1658 					lwp_unlock(l);
1659 					sp = NULL;
1660 					break;
1661 				}
1662 			}
1663 		}
1664 
1665 		/*
1666 		 * We should see pending but ignored signals only if
1667 		 * we are being traced.
1668 		 */
1669 		if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
1670 		    (p->p_slflag & PSL_TRACED) == 0) {
1671 			/* Discard the signal. */
1672 			continue;
1673 		}
1674 
1675 		/*
1676 		 * If traced, always stop, and stay stopped until released
1677 		 * by the debugger.  If the our parent process is waiting
1678 		 * for us, don't hang as we could deadlock.
1679 		 */
1680 		if ((p->p_slflag & PSL_TRACED) != 0 &&
1681 		    (p->p_lflag & PL_PPWAIT) == 0 && signo != SIGKILL) {
1682 			/*
1683 			 * Take the signal, but don't remove it from the
1684 			 * siginfo queue, because the debugger can send
1685 			 * it later.
1686 			 */
1687 			if (sp)
1688 				sigdelset(&sp->sp_set, signo);
1689 			p->p_xstat = signo;
1690 
1691 			/* Emulation-specific handling of signal trace */
1692 			if (p->p_emul->e_tracesig == NULL ||
1693 			    (*p->p_emul->e_tracesig)(p, signo) == 0)
1694 				sigswitch(!(p->p_slflag & PSL_FSTRACE), 0,
1695 				    signo);
1696 
1697 			/* Check for a signal from the debugger. */
1698 			if ((signo = sigchecktrace()) == 0)
1699 				continue;
1700 
1701 			/* Signals from the debugger are "out of band". */
1702 			sp = NULL;
1703 		}
1704 
1705 		prop = sigprop[signo];
1706 
1707 		/* XXX no siginfo? */
1708 		SDT_PROBE(proc,,,signal_handle, signo, 0,
1709 			SIGACTION(p, signo).sa_handler, 0, 0);
1710 
1711 		/*
1712 		 * Decide whether the signal should be returned.
1713 		 */
1714 		switch ((long)SIGACTION(p, signo).sa_handler) {
1715 		case (long)SIG_DFL:
1716 			/*
1717 			 * Don't take default actions on system processes.
1718 			 */
1719 			if (p->p_pid <= 1) {
1720 #ifdef DIAGNOSTIC
1721 				/*
1722 				 * Are you sure you want to ignore SIGSEGV
1723 				 * in init? XXX
1724 				 */
1725 				printf_nolog("Process (pid %d) got sig %d\n",
1726 				    p->p_pid, signo);
1727 #endif
1728 				continue;
1729 			}
1730 
1731 			/*
1732 			 * If there is a pending stop signal to process with
1733 			 * default action, stop here, then clear the signal.
1734 			 * However, if process is member of an orphaned
1735 			 * process group, ignore tty stop signals.
1736 			 */
1737 			if (prop & SA_STOP) {
1738 				/*
1739 				 * XXX Don't hold proc_lock for p_lflag,
1740 				 * but it's not a big deal.
1741 				 */
1742 				if (p->p_slflag & PSL_TRACED ||
1743 				    ((p->p_lflag & PL_ORPHANPG) != 0 &&
1744 				    prop & SA_TTYSTOP)) {
1745 					/* Ignore the signal. */
1746 					continue;
1747 				}
1748 				/* Take the signal. */
1749 				(void)sigget(sp, NULL, signo, NULL);
1750 				p->p_xstat = signo;
1751 				signo = 0;
1752 				sigswitch(true, PS_NOCLDSTOP, p->p_xstat);
1753 			} else if (prop & SA_IGNORE) {
1754 				/*
1755 				 * Except for SIGCONT, shouldn't get here.
1756 				 * Default action is to ignore; drop it.
1757 				 */
1758 				continue;
1759 			}
1760 			break;
1761 
1762 		case (long)SIG_IGN:
1763 #ifdef DEBUG_ISSIGNAL
1764 			/*
1765 			 * Masking above should prevent us ever trying
1766 			 * to take action on an ignored signal other
1767 			 * than SIGCONT, unless process is traced.
1768 			 */
1769 			if ((prop & SA_CONT) == 0 &&
1770 			    (p->p_slflag & PSL_TRACED) == 0)
1771 				printf_nolog("issignal\n");
1772 #endif
1773 			continue;
1774 
1775 		default:
1776 			/*
1777 			 * This signal has an action, let postsig() process
1778 			 * it.
1779 			 */
1780 			break;
1781 		}
1782 
1783 		break;
1784 	}
1785 
1786 	l->l_sigpendset = sp;
1787 	return signo;
1788 }
1789 
1790 /*
1791  * Take the action for the specified signal
1792  * from the current set of pending signals.
1793  */
1794 void
1795 postsig(int signo)
1796 {
1797 	struct lwp	*l;
1798 	struct proc	*p;
1799 	struct sigacts	*ps;
1800 	sig_t		action;
1801 	sigset_t	*returnmask;
1802 	ksiginfo_t	ksi;
1803 
1804 	l = curlwp;
1805 	p = l->l_proc;
1806 	ps = p->p_sigacts;
1807 
1808 	KASSERT(mutex_owned(p->p_lock));
1809 	KASSERT(signo > 0);
1810 
1811 	/*
1812 	 * Set the new mask value and also defer further occurrences of this
1813 	 * signal.
1814 	 *
1815 	 * Special case: user has done a sigsuspend.  Here the current mask is
1816 	 * not of interest, but rather the mask from before the sigsuspend is
1817 	 * what we want restored after the signal processing is completed.
1818 	 */
1819 	if (l->l_sigrestore) {
1820 		returnmask = &l->l_sigoldmask;
1821 		l->l_sigrestore = 0;
1822 	} else
1823 		returnmask = &l->l_sigmask;
1824 
1825 	/*
1826 	 * Commit to taking the signal before releasing the mutex.
1827 	 */
1828 	action = SIGACTION_PS(ps, signo).sa_handler;
1829 	l->l_ru.ru_nsignals++;
1830 	if (l->l_sigpendset == NULL) {
1831 		/* From the debugger */
1832 		if (!siggetinfo(&l->l_sigpend, &ksi, signo))
1833 			(void)siggetinfo(&p->p_sigpend, &ksi, signo);
1834 	} else
1835 		sigget(l->l_sigpendset, &ksi, signo, NULL);
1836 
1837 	if (ktrpoint(KTR_PSIG)) {
1838 		mutex_exit(p->p_lock);
1839 		ktrpsig(signo, action, returnmask, &ksi);
1840 		mutex_enter(p->p_lock);
1841 	}
1842 
1843 	if (action == SIG_DFL) {
1844 		/*
1845 		 * Default action, where the default is to kill
1846 		 * the process.  (Other cases were ignored above.)
1847 		 */
1848 		sigexit(l, signo);
1849 		return;
1850 	}
1851 
1852 	/*
1853 	 * If we get here, the signal must be caught.
1854 	 */
1855 #ifdef DIAGNOSTIC
1856 	if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
1857 		panic("postsig action");
1858 #endif
1859 
1860 	kpsendsig(l, &ksi, returnmask);
1861 }
1862 
1863 /*
1864  * sendsig:
1865  *
1866  *	Default signal delivery method for NetBSD.
1867  */
1868 void
1869 sendsig(const struct ksiginfo *ksi, const sigset_t *mask)
1870 {
1871 	struct sigacts *sa;
1872 	int sig;
1873 
1874 	sig = ksi->ksi_signo;
1875 	sa = curproc->p_sigacts;
1876 
1877 	switch (sa->sa_sigdesc[sig].sd_vers)  {
1878 	case 0:
1879 	case 1:
1880 		/* Compat for 1.6 and earlier. */
1881 		if (sendsig_sigcontext_vec == NULL) {
1882 			break;
1883 		}
1884 		(*sendsig_sigcontext_vec)(ksi, mask);
1885 		return;
1886 	case 2:
1887 	case 3:
1888 		sendsig_siginfo(ksi, mask);
1889 		return;
1890 	default:
1891 		break;
1892 	}
1893 
1894 	printf("sendsig: bad version %d\n", sa->sa_sigdesc[sig].sd_vers);
1895 	sigexit(curlwp, SIGILL);
1896 }
1897 
1898 /*
1899  * sendsig_reset:
1900  *
1901  *	Reset the signal action.  Called from emulation specific sendsig()
1902  *	before unlocking to deliver the signal.
1903  */
1904 void
1905 sendsig_reset(struct lwp *l, int signo)
1906 {
1907 	struct proc *p = l->l_proc;
1908 	struct sigacts *ps = p->p_sigacts;
1909 
1910 	KASSERT(mutex_owned(p->p_lock));
1911 
1912 	p->p_sigctx.ps_lwp = 0;
1913 	p->p_sigctx.ps_code = 0;
1914 	p->p_sigctx.ps_signo = 0;
1915 
1916 	mutex_enter(&ps->sa_mutex);
1917 	sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
1918 	if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
1919 		sigdelset(&p->p_sigctx.ps_sigcatch, signo);
1920 		if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
1921 			sigaddset(&p->p_sigctx.ps_sigignore, signo);
1922 		SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
1923 	}
1924 	mutex_exit(&ps->sa_mutex);
1925 }
1926 
1927 /*
1928  * Kill the current process for stated reason.
1929  */
1930 void
1931 killproc(struct proc *p, const char *why)
1932 {
1933 
1934 	KASSERT(mutex_owned(proc_lock));
1935 
1936 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1937 	uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
1938 	psignal(p, SIGKILL);
1939 }
1940 
1941 /*
1942  * Force the current process to exit with the specified signal, dumping core
1943  * if appropriate.  We bypass the normal tests for masked and caught
1944  * signals, allowing unrecoverable failures to terminate the process without
1945  * changing signal state.  Mark the accounting record with the signal
1946  * termination.  If dumping core, save the signal number for the debugger.
1947  * Calls exit and does not return.
1948  */
1949 void
1950 sigexit(struct lwp *l, int signo)
1951 {
1952 	int exitsig, error, docore;
1953 	struct proc *p;
1954 	struct lwp *t;
1955 
1956 	p = l->l_proc;
1957 
1958 	KASSERT(mutex_owned(p->p_lock));
1959 	KERNEL_UNLOCK_ALL(l, NULL);
1960 
1961 	/*
1962 	 * Don't permit coredump() multiple times in the same process.
1963 	 * Call back into sigexit, where we will be suspended until
1964 	 * the deed is done.  Note that this is a recursive call, but
1965 	 * LW_WCORE will prevent us from coming back this way.
1966 	 */
1967 	if ((p->p_sflag & PS_WCORE) != 0) {
1968 		lwp_lock(l);
1969 		l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
1970 		lwp_unlock(l);
1971 		mutex_exit(p->p_lock);
1972 		lwp_userret(l);
1973 		panic("sigexit 1");
1974 		/* NOTREACHED */
1975 	}
1976 
1977 	/* If process is already on the way out, then bail now. */
1978 	if ((p->p_sflag & PS_WEXIT) != 0) {
1979 		mutex_exit(p->p_lock);
1980 		lwp_exit(l);
1981 		panic("sigexit 2");
1982 		/* NOTREACHED */
1983 	}
1984 
1985 	/*
1986 	 * Prepare all other LWPs for exit.  If dumping core, suspend them
1987 	 * so that their registers are available long enough to be dumped.
1988  	 */
1989 	if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
1990 		p->p_sflag |= PS_WCORE;
1991 		for (;;) {
1992 			LIST_FOREACH(t, &p->p_lwps, l_sibling) {
1993 				lwp_lock(t);
1994 				if (t == l) {
1995 					t->l_flag &= ~LW_WSUSPEND;
1996 					lwp_unlock(t);
1997 					continue;
1998 				}
1999 				t->l_flag |= (LW_WCORE | LW_WEXIT);
2000 				lwp_suspend(l, t);
2001 			}
2002 
2003 			if (p->p_nrlwps == 1)
2004 				break;
2005 
2006 			/*
2007 			 * Kick any LWPs sitting in lwp_wait1(), and wait
2008 			 * for everyone else to stop before proceeding.
2009 			 */
2010 			p->p_nlwpwait++;
2011 			cv_broadcast(&p->p_lwpcv);
2012 			cv_wait(&p->p_lwpcv, p->p_lock);
2013 			p->p_nlwpwait--;
2014 		}
2015 	}
2016 
2017 	exitsig = signo;
2018 	p->p_acflag |= AXSIG;
2019 	p->p_sigctx.ps_signo = signo;
2020 
2021 	if (docore) {
2022 		mutex_exit(p->p_lock);
2023 		if ((error = (*coredump_vec)(l, NULL)) == 0)
2024 			exitsig |= WCOREFLAG;
2025 
2026 		if (kern_logsigexit) {
2027 			int uid = l->l_cred ?
2028 			    (int)kauth_cred_geteuid(l->l_cred) : -1;
2029 
2030 			if (error)
2031 				log(LOG_INFO, lognocoredump, p->p_pid,
2032 				    p->p_comm, uid, signo, error);
2033 			else
2034 				log(LOG_INFO, logcoredump, p->p_pid,
2035 				    p->p_comm, uid, signo);
2036 		}
2037 
2038 #ifdef PAX_SEGVGUARD
2039 		pax_segvguard(l, p->p_textvp, p->p_comm, true);
2040 #endif /* PAX_SEGVGUARD */
2041 		/* Acquire the sched state mutex.  exit1() will release it. */
2042 		mutex_enter(p->p_lock);
2043 	}
2044 
2045 	/* No longer dumping core. */
2046 	p->p_sflag &= ~PS_WCORE;
2047 
2048 	exit1(l, W_EXITCODE(0, exitsig));
2049 	/* NOTREACHED */
2050 }
2051 
2052 /*
2053  * Put process 'p' into the stopped state and optionally, notify the parent.
2054  */
2055 void
2056 proc_stop(struct proc *p, int notify, int signo)
2057 {
2058 	struct lwp *l;
2059 
2060 	KASSERT(mutex_owned(p->p_lock));
2061 
2062 	/*
2063 	 * First off, set the stopping indicator and bring all sleeping
2064 	 * LWPs to a halt so they are included in p->p_nrlwps.  We musn't
2065 	 * unlock between here and the p->p_nrlwps check below.
2066 	 */
2067 	p->p_sflag |= PS_STOPPING;
2068 	if (notify)
2069 		p->p_sflag |= PS_NOTIFYSTOP;
2070 	else
2071 		p->p_sflag &= ~PS_NOTIFYSTOP;
2072 	membar_producer();
2073 
2074 	proc_stop_lwps(p);
2075 
2076 	/*
2077 	 * If there are no LWPs available to take the signal, then we
2078 	 * signal the parent process immediately.  Otherwise, the last
2079 	 * LWP to stop will take care of it.
2080 	 */
2081 
2082 	if (p->p_nrlwps == 0) {
2083 		proc_stop_done(p, true, PS_NOCLDSTOP);
2084 	} else {
2085 		/*
2086 		 * Have the remaining LWPs come to a halt, and trigger
2087 		 * proc_stop_callout() to ensure that they do.
2088 		 */
2089 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2090 			sigpost(l, SIG_DFL, SA_STOP, signo);
2091 		}
2092 		callout_schedule(&proc_stop_ch, 1);
2093 	}
2094 }
2095 
2096 /*
2097  * When stopping a process, we do not immediatly set sleeping LWPs stopped,
2098  * but wait for them to come to a halt at the kernel-user boundary.  This is
2099  * to allow LWPs to release any locks that they may hold before stopping.
2100  *
2101  * Non-interruptable sleeps can be long, and there is the potential for an
2102  * LWP to begin sleeping interruptably soon after the process has been set
2103  * stopping (PS_STOPPING).  These LWPs will not notice that the process is
2104  * stopping, and so complete halt of the process and the return of status
2105  * information to the parent could be delayed indefinitely.
2106  *
2107  * To handle this race, proc_stop_callout() runs once per tick while there
2108  * are stopping processes in the system.  It sets LWPs that are sleeping
2109  * interruptably into the LSSTOP state.
2110  *
2111  * Note that we are not concerned about keeping all LWPs stopped while the
2112  * process is stopped: stopped LWPs can awaken briefly to handle signals.
2113  * What we do need to ensure is that all LWPs in a stopping process have
2114  * stopped at least once, so that notification can be sent to the parent
2115  * process.
2116  */
2117 static void
2118 proc_stop_callout(void *cookie)
2119 {
2120 	bool more, restart;
2121 	struct proc *p;
2122 
2123 	(void)cookie;
2124 
2125 	do {
2126 		restart = false;
2127 		more = false;
2128 
2129 		mutex_enter(proc_lock);
2130 		PROCLIST_FOREACH(p, &allproc) {
2131 			mutex_enter(p->p_lock);
2132 
2133 			if ((p->p_sflag & PS_STOPPING) == 0) {
2134 				mutex_exit(p->p_lock);
2135 				continue;
2136 			}
2137 
2138 			/* Stop any LWPs sleeping interruptably. */
2139 			proc_stop_lwps(p);
2140 			if (p->p_nrlwps == 0) {
2141 				/*
2142 				 * We brought the process to a halt.
2143 				 * Mark it as stopped and notify the
2144 				 * parent.
2145 				 */
2146 				if ((p->p_sflag & PS_NOTIFYSTOP) != 0) {
2147 					/*
2148 					 * Note that proc_stop_done() will
2149 					 * drop p->p_lock briefly.
2150 					 * Arrange to restart and check
2151 					 * all processes again.
2152 					 */
2153 					restart = true;
2154 				}
2155 				proc_stop_done(p, true, PS_NOCLDSTOP);
2156 			} else
2157 				more = true;
2158 
2159 			mutex_exit(p->p_lock);
2160 			if (restart)
2161 				break;
2162 		}
2163 		mutex_exit(proc_lock);
2164 	} while (restart);
2165 
2166 	/*
2167 	 * If we noted processes that are stopping but still have
2168 	 * running LWPs, then arrange to check again in 1 tick.
2169 	 */
2170 	if (more)
2171 		callout_schedule(&proc_stop_ch, 1);
2172 }
2173 
2174 /*
2175  * Given a process in state SSTOP, set the state back to SACTIVE and
2176  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
2177  */
2178 void
2179 proc_unstop(struct proc *p)
2180 {
2181 	struct lwp *l;
2182 	int sig;
2183 
2184 	KASSERT(mutex_owned(proc_lock));
2185 	KASSERT(mutex_owned(p->p_lock));
2186 
2187 	p->p_stat = SACTIVE;
2188 	p->p_sflag &= ~PS_STOPPING;
2189 	sig = p->p_xstat;
2190 
2191 	if (!p->p_waited)
2192 		p->p_pptr->p_nstopchild--;
2193 
2194 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2195 		lwp_lock(l);
2196 		if (l->l_stat != LSSTOP) {
2197 			lwp_unlock(l);
2198 			continue;
2199 		}
2200 		if (l->l_wchan == NULL) {
2201 			setrunnable(l);
2202 			continue;
2203 		}
2204 		if (sig && (l->l_flag & LW_SINTR) != 0) {
2205 			setrunnable(l);
2206 			sig = 0;
2207 		} else {
2208 			l->l_stat = LSSLEEP;
2209 			p->p_nrlwps++;
2210 			lwp_unlock(l);
2211 		}
2212 	}
2213 }
2214 
2215 static int
2216 filt_sigattach(struct knote *kn)
2217 {
2218 	struct proc *p = curproc;
2219 
2220 	kn->kn_obj = p;
2221 	kn->kn_flags |= EV_CLEAR;	/* automatically set */
2222 
2223 	mutex_enter(p->p_lock);
2224 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2225 	mutex_exit(p->p_lock);
2226 
2227 	return 0;
2228 }
2229 
2230 static void
2231 filt_sigdetach(struct knote *kn)
2232 {
2233 	struct proc *p = kn->kn_obj;
2234 
2235 	mutex_enter(p->p_lock);
2236 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2237 	mutex_exit(p->p_lock);
2238 }
2239 
2240 /*
2241  * Signal knotes are shared with proc knotes, so we apply a mask to
2242  * the hint in order to differentiate them from process hints.  This
2243  * could be avoided by using a signal-specific knote list, but probably
2244  * isn't worth the trouble.
2245  */
2246 static int
2247 filt_signal(struct knote *kn, long hint)
2248 {
2249 
2250 	if (hint & NOTE_SIGNAL) {
2251 		hint &= ~NOTE_SIGNAL;
2252 
2253 		if (kn->kn_id == hint)
2254 			kn->kn_data++;
2255 	}
2256 	return (kn->kn_data != 0);
2257 }
2258 
2259 const struct filterops sig_filtops = {
2260 	0, filt_sigattach, filt_sigdetach, filt_signal
2261 };
2262