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