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