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