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