xref: /dragonfly/sys/kern/kern_sig.c (revision 7d3e9a5b)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
35  * $FreeBSD: src/sys/kern/kern_sig.c,v 1.72.2.17 2003/05/16 16:34:34 obrien Exp $
36  */
37 
38 #include "opt_ktrace.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/sysmsg.h>
44 #include <sys/signalvar.h>
45 #include <sys/resourcevar.h>
46 #include <sys/vnode.h>
47 #include <sys/event.h>
48 #include <sys/proc.h>
49 #include <sys/nlookup.h>
50 #include <sys/pioctl.h>
51 #include <sys/acct.h>
52 #include <sys/fcntl.h>
53 #include <sys/lock.h>
54 #include <sys/wait.h>
55 #include <sys/ktrace.h>
56 #include <sys/syslog.h>
57 #include <sys/stat.h>
58 #include <sys/sysent.h>
59 #include <sys/sysctl.h>
60 #include <sys/malloc.h>
61 #include <sys/interrupt.h>
62 #include <sys/unistd.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/vkernel.h>
65 
66 #include <sys/signal2.h>
67 #include <sys/thread2.h>
68 #include <sys/spinlock2.h>
69 
70 #include <machine/cpu.h>
71 #include <machine/smp.h>
72 
73 static int	coredump(struct lwp *, int);
74 static char	*expand_name(const char *, uid_t, pid_t);
75 static int	dokillpg(int sig, int pgid, int all);
76 static int	sig_ffs(sigset_t *set);
77 static int	sigprop(int sig);
78 static void	lwp_signotify(struct lwp *lp);
79 static void	lwp_signotify_remote(void *arg);
80 static int	kern_sigtimedwait(sigset_t set, siginfo_t *info,
81 		    struct timespec *timeout);
82 static void	proc_stopwait(struct proc *p);
83 
84 static int	filt_sigattach(struct knote *kn);
85 static void	filt_sigdetach(struct knote *kn);
86 static int	filt_signal(struct knote *kn, long hint);
87 
88 struct filterops sig_filtops =
89 	{ FILTEROP_MPSAFE, filt_sigattach, filt_sigdetach, filt_signal };
90 
91 static int	kern_logsigexit = 1;
92 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
93     &kern_logsigexit, 0,
94     "Log processes quitting on abnormal signals to syslog(3)");
95 
96 /*
97  * Can process p send the signal sig to process q?  Only processes within
98  * the current reaper or children of the current reaper can be signaled.
99  * Normally the reaper itself cannot be signalled, unless initok is set.
100  */
101 #define	CANSIGNAL(q, sig, initok)				\
102 	((!p_trespass(curproc->p_ucred, (q)->p_ucred) &&	\
103 	reaper_sigtest(curproc, p, initok)) ||			\
104 	((sig) == SIGCONT && (q)->p_session == curproc->p_session))
105 
106 /*
107  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
108  */
109 #define	CANSIGIO(ruid, uc, q) \
110 	((uc)->cr_uid == 0 || \
111 	    (ruid) == (q)->p_ucred->cr_ruid || \
112 	    (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
113 	    (ruid) == (q)->p_ucred->cr_uid || \
114 	    (uc)->cr_uid == (q)->p_ucred->cr_uid)
115 
116 int sugid_coredump;
117 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
118 	&sugid_coredump, 0, "Enable coredumping set user/group ID processes");
119 
120 static int	do_coredump = 1;
121 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
122 	&do_coredump, 0, "Enable/Disable coredumps");
123 
124 /*
125  * Signal properties and actions.
126  * The array below categorizes the signals and their default actions
127  * according to the following properties:
128  */
129 #define	SA_KILL		0x01		/* terminates process by default */
130 #define	SA_CORE		0x02		/* ditto and coredumps */
131 #define	SA_STOP		0x04		/* suspend process */
132 #define	SA_TTYSTOP	0x08		/* ditto, from tty */
133 #define	SA_IGNORE	0x10		/* ignore by default */
134 #define	SA_CONT		0x20		/* continue if suspended */
135 #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
136 #define	SA_CKPT		0x80		/* checkpoint process */
137 
138 
139 static int sigproptbl[NSIG] = {
140 	SA_KILL,		/* SIGHUP */
141 	SA_KILL,		/* SIGINT */
142 	SA_KILL|SA_CORE,	/* SIGQUIT */
143 	SA_KILL|SA_CORE,	/* SIGILL */
144 	SA_KILL|SA_CORE,	/* SIGTRAP */
145 	SA_KILL|SA_CORE,	/* SIGABRT */
146 	SA_KILL|SA_CORE,	/* SIGEMT */
147 	SA_KILL|SA_CORE,	/* SIGFPE */
148 	SA_KILL,		/* SIGKILL */
149 	SA_KILL|SA_CORE,	/* SIGBUS */
150 	SA_KILL|SA_CORE,	/* SIGSEGV */
151 	SA_KILL|SA_CORE,	/* SIGSYS */
152 	SA_KILL,		/* SIGPIPE */
153 	SA_KILL,		/* SIGALRM */
154 	SA_KILL,		/* SIGTERM */
155 	SA_IGNORE,		/* SIGURG */
156 	SA_STOP,		/* SIGSTOP */
157 	SA_STOP|SA_TTYSTOP,	/* SIGTSTP */
158 	SA_IGNORE|SA_CONT,	/* SIGCONT */
159 	SA_IGNORE,		/* SIGCHLD */
160 	SA_STOP|SA_TTYSTOP,	/* SIGTTIN */
161 	SA_STOP|SA_TTYSTOP,	/* SIGTTOU */
162 	SA_IGNORE,		/* SIGIO */
163 	SA_KILL,		/* SIGXCPU */
164 	SA_KILL,		/* SIGXFSZ */
165 	SA_KILL,		/* SIGVTALRM */
166 	SA_KILL,		/* SIGPROF */
167 	SA_IGNORE,		/* SIGWINCH  */
168 	SA_IGNORE,		/* SIGINFO */
169 	SA_KILL,		/* SIGUSR1 */
170 	SA_KILL,		/* SIGUSR2 */
171 	SA_IGNORE,		/* SIGTHR */
172 	SA_CKPT,		/* SIGCKPT */
173 	SA_KILL|SA_CKPT,	/* SIGCKPTEXIT */
174 	SA_IGNORE,
175 	SA_IGNORE,
176 	SA_IGNORE,
177 	SA_IGNORE,
178 	SA_IGNORE,
179 	SA_IGNORE,
180 	SA_IGNORE,
181 	SA_IGNORE,
182 	SA_IGNORE,
183 	SA_IGNORE,
184 	SA_IGNORE,
185 	SA_IGNORE,
186 	SA_IGNORE,
187 	SA_IGNORE,
188 	SA_IGNORE,
189 	SA_IGNORE,
190 	SA_IGNORE,
191 	SA_IGNORE,
192 	SA_IGNORE,
193 	SA_IGNORE,
194 	SA_IGNORE,
195 	SA_IGNORE,
196 	SA_IGNORE,
197 	SA_IGNORE,
198 	SA_IGNORE,
199 	SA_IGNORE,
200 	SA_IGNORE,
201 	SA_IGNORE,
202 	SA_IGNORE,
203 	SA_IGNORE,
204 };
205 
206 __read_mostly sigset_t sigcantmask_mask;
207 
208 static __inline int
209 sigprop(int sig)
210 {
211 
212 	if (sig > 0 && sig < NSIG)
213 		return (sigproptbl[_SIG_IDX(sig)]);
214 
215 	return (0);
216 }
217 
218 static __inline int
219 sig_ffs(sigset_t *set)
220 {
221 	int i;
222 
223 	for (i = 0; i < _SIG_WORDS; i++)
224 		if (set->__bits[i])
225 			return (ffs(set->__bits[i]) + (i * 32));
226 	return (0);
227 }
228 
229 /*
230  * Allows us to populate siginfo->si_pid and si_uid in the target process
231  * (p) from the originating thread (td).  This function must work properly
232  * even if a kernel thread is sending the signal.
233  *
234  * NOTE: Signals are not queued, so if multiple signals are received the
235  *	 signal handler will only see the most recent pid and uid for any
236  *	 given signal number.
237  */
238 static __inline void
239 sigsetfrompid(thread_t td, struct proc *p, int sig)
240 {
241 	struct sigacts *sap;
242 
243 	if ((sap = p->p_sigacts) == NULL)
244 		return;
245 	if (td->td_proc) {
246 		sap->ps_frominfo[sig].pid = td->td_proc->p_pid;
247 		sap->ps_frominfo[sig].uid = td->td_ucred->cr_uid;
248 	} else {
249 		sap->ps_frominfo[sig].pid = 0;
250 		sap->ps_frominfo[sig].uid = 0;
251 	}
252 }
253 
254 /*
255  * No requirements.
256  */
257 int
258 kern_sigaction(int sig, struct sigaction *act, struct sigaction *oact)
259 {
260 	struct thread *td = curthread;
261 	struct proc *p = td->td_proc;
262 	struct lwp *lp;
263 	struct sigacts *ps = p->p_sigacts;
264 
265 	if (sig <= 0 || sig >= _SIG_MAXSIG)
266 		return (EINVAL);
267 
268 	lwkt_gettoken(&p->p_token);
269 
270 	if (oact) {
271 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
272 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
273 		oact->sa_flags = 0;
274 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
275 			oact->sa_flags |= SA_ONSTACK;
276 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
277 			oact->sa_flags |= SA_RESTART;
278 		if (SIGISMEMBER(ps->ps_sigreset, sig))
279 			oact->sa_flags |= SA_RESETHAND;
280 		if (SIGISMEMBER(ps->ps_signodefer, sig))
281 			oact->sa_flags |= SA_NODEFER;
282 		if (SIGISMEMBER(ps->ps_siginfo, sig))
283 			oact->sa_flags |= SA_SIGINFO;
284 		if (sig == SIGCHLD && p->p_sigacts->ps_flag & PS_NOCLDSTOP)
285 			oact->sa_flags |= SA_NOCLDSTOP;
286 		if (sig == SIGCHLD && p->p_sigacts->ps_flag & PS_NOCLDWAIT)
287 			oact->sa_flags |= SA_NOCLDWAIT;
288 	}
289 	if (act) {
290 		/*
291 		 * Check for invalid requests.  KILL and STOP cannot be
292 		 * caught.
293 		 */
294 		if (sig == SIGKILL || sig == SIGSTOP) {
295 			if (act->sa_handler != SIG_DFL) {
296 				lwkt_reltoken(&p->p_token);
297 				return (EINVAL);
298 			}
299 		}
300 
301 		/*
302 		 * Change setting atomically.
303 		 */
304 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
305 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
306 		if (act->sa_flags & SA_SIGINFO) {
307 			ps->ps_sigact[_SIG_IDX(sig)] =
308 			    (__sighandler_t *)act->sa_sigaction;
309 			SIGADDSET(ps->ps_siginfo, sig);
310 		} else {
311 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
312 			SIGDELSET(ps->ps_siginfo, sig);
313 		}
314 		if (!(act->sa_flags & SA_RESTART))
315 			SIGADDSET(ps->ps_sigintr, sig);
316 		else
317 			SIGDELSET(ps->ps_sigintr, sig);
318 		if (act->sa_flags & SA_ONSTACK)
319 			SIGADDSET(ps->ps_sigonstack, sig);
320 		else
321 			SIGDELSET(ps->ps_sigonstack, sig);
322 		if (act->sa_flags & SA_RESETHAND)
323 			SIGADDSET(ps->ps_sigreset, sig);
324 		else
325 			SIGDELSET(ps->ps_sigreset, sig);
326 		if (act->sa_flags & SA_NODEFER)
327 			SIGADDSET(ps->ps_signodefer, sig);
328 		else
329 			SIGDELSET(ps->ps_signodefer, sig);
330 		if (sig == SIGCHLD) {
331 			if (act->sa_flags & SA_NOCLDSTOP)
332 				p->p_sigacts->ps_flag |= PS_NOCLDSTOP;
333 			else
334 				p->p_sigacts->ps_flag &= ~PS_NOCLDSTOP;
335 			if (act->sa_flags & SA_NOCLDWAIT) {
336 				/*
337 				 * Paranoia: since SA_NOCLDWAIT is implemented
338 				 * by reparenting the dying child to PID 1 (and
339 				 * trust it to reap the zombie), PID 1 itself
340 				 * is forbidden to set SA_NOCLDWAIT.
341 				 */
342 				if (p->p_pid == 1)
343 					p->p_sigacts->ps_flag &= ~PS_NOCLDWAIT;
344 				else
345 					p->p_sigacts->ps_flag |= PS_NOCLDWAIT;
346 			} else {
347 				p->p_sigacts->ps_flag &= ~PS_NOCLDWAIT;
348 			}
349 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
350 				ps->ps_flag |= PS_CLDSIGIGN;
351 			else
352 				ps->ps_flag &= ~PS_CLDSIGIGN;
353 		}
354 		/*
355 		 * Set bit in p_sigignore for signals that are set to SIG_IGN,
356 		 * and for signals set to SIG_DFL where the default is to
357 		 * ignore. However, don't put SIGCONT in p_sigignore, as we
358 		 * have to restart the process.
359 		 *
360 		 * Also remove the signal from the process and lwp signal
361 		 * list.
362 		 */
363 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
364 		    (sigprop(sig) & SA_IGNORE &&
365 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
366 			SIGDELSET_ATOMIC(p->p_siglist, sig);
367 			FOREACH_LWP_IN_PROC(lp, p) {
368 				spin_lock(&lp->lwp_spin);
369 				SIGDELSET(lp->lwp_siglist, sig);
370 				spin_unlock(&lp->lwp_spin);
371 			}
372 			if (sig != SIGCONT) {
373 				/* easier in ksignal */
374 				SIGADDSET(p->p_sigignore, sig);
375 			}
376 			SIGDELSET(p->p_sigcatch, sig);
377 		} else {
378 			SIGDELSET(p->p_sigignore, sig);
379 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
380 				SIGDELSET(p->p_sigcatch, sig);
381 			else
382 				SIGADDSET(p->p_sigcatch, sig);
383 		}
384 	}
385 	lwkt_reltoken(&p->p_token);
386 	return (0);
387 }
388 
389 int
390 sys_sigaction(struct sysmsg *sysmsg, const struct sigaction_args *uap)
391 {
392 	struct sigaction act, oact;
393 	struct sigaction *actp, *oactp;
394 	int error;
395 
396 	actp = (uap->act != NULL) ? &act : NULL;
397 	oactp = (uap->oact != NULL) ? &oact : NULL;
398 	if (actp) {
399 		error = copyin(uap->act, actp, sizeof(act));
400 		if (error)
401 			return (error);
402 	}
403 	error = kern_sigaction(uap->sig, actp, oactp);
404 	if (oactp && !error) {
405 		error = copyout(oactp, uap->oact, sizeof(oact));
406 	}
407 	return (error);
408 }
409 
410 /*
411  * Initialize signal state for process 0;
412  * set to ignore signals that are ignored by default.
413  */
414 void
415 siginit(struct proc *p)
416 {
417 	int i;
418 
419 	for (i = 1; i <= NSIG; i++) {
420 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
421 			SIGADDSET(p->p_sigignore, i);
422 	}
423 
424 	/*
425 	 * Also initialize signal-related global state.
426 	 */
427 	SIGSETOR_CANTMASK(sigcantmask_mask);
428 }
429 
430 /*
431  * Reset signals for an exec of the specified process.
432  */
433 void
434 execsigs(struct proc *p)
435 {
436 	struct sigacts *ps = p->p_sigacts;
437 	struct lwp *lp;
438 	int sig;
439 
440 	lp = ONLY_LWP_IN_PROC(p);
441 
442 	/*
443 	 * Reset caught signals.  Held signals remain held
444 	 * through p_sigmask (unless they were caught,
445 	 * and are now ignored by default).
446 	 */
447 	while (SIGNOTEMPTY(p->p_sigcatch)) {
448 		sig = sig_ffs(&p->p_sigcatch);
449 		SIGDELSET(p->p_sigcatch, sig);
450 		if (sigprop(sig) & SA_IGNORE) {
451 			if (sig != SIGCONT)
452 				SIGADDSET(p->p_sigignore, sig);
453 			SIGDELSET_ATOMIC(p->p_siglist, sig);
454 			/* don't need spinlock */
455 			SIGDELSET(lp->lwp_siglist, sig);
456 		}
457 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
458 	}
459 
460 	/*
461 	 * Reset stack state to the user stack.
462 	 * Clear set of signals caught on the signal stack.
463 	 */
464 	lp->lwp_sigstk.ss_flags = SS_DISABLE;
465 	lp->lwp_sigstk.ss_size = 0;
466 	lp->lwp_sigstk.ss_sp = NULL;
467 	lp->lwp_flags &= ~LWP_ALTSTACK;
468 	/*
469 	 * Reset no zombies if child dies flag as Solaris does.
470 	 */
471 	p->p_sigacts->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
472 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
473 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
474 }
475 
476 /*
477  * kern_sigprocmask() - MP SAFE ONLY IF p == curproc
478  *
479  *	Manipulate signal mask.  This routine is MP SAFE *ONLY* if
480  *	p == curproc.
481  */
482 int
483 kern_sigprocmask(int how, sigset_t *set, sigset_t *oset)
484 {
485 	struct thread *td = curthread;
486 	struct lwp *lp = td->td_lwp;
487 	struct proc *p = td->td_proc;
488 	int error;
489 
490 	lwkt_gettoken(&p->p_token);
491 
492 	if (oset != NULL)
493 		*oset = lp->lwp_sigmask;
494 
495 	error = 0;
496 	if (set != NULL) {
497 		switch (how) {
498 		case SIG_BLOCK:
499 			SIG_CANTMASK(*set);
500 			SIGSETOR(lp->lwp_sigmask, *set);
501 			break;
502 		case SIG_UNBLOCK:
503 			SIGSETNAND(lp->lwp_sigmask, *set);
504 			break;
505 		case SIG_SETMASK:
506 			SIG_CANTMASK(*set);
507 			lp->lwp_sigmask = *set;
508 			break;
509 		default:
510 			error = EINVAL;
511 			break;
512 		}
513 	}
514 
515 	lwkt_reltoken(&p->p_token);
516 
517 	return (error);
518 }
519 
520 /*
521  * sigprocmask()
522  *
523  * MPSAFE
524  */
525 int
526 sys_sigprocmask(struct sysmsg *sysmsg, const struct sigprocmask_args *uap)
527 {
528 	sigset_t set, oset;
529 	sigset_t *setp, *osetp;
530 	int error;
531 
532 	setp = (uap->set != NULL) ? &set : NULL;
533 	osetp = (uap->oset != NULL) ? &oset : NULL;
534 	if (setp) {
535 		error = copyin(uap->set, setp, sizeof(set));
536 		if (error)
537 			return (error);
538 	}
539 	error = kern_sigprocmask(uap->how, setp, osetp);
540 	if (osetp && !error) {
541 		error = copyout(osetp, uap->oset, sizeof(oset));
542 	}
543 	return (error);
544 }
545 
546 /*
547  * MPSAFE
548  */
549 int
550 kern_sigpending(sigset_t *set)
551 {
552 	struct lwp *lp = curthread->td_lwp;
553 
554 	*set = lwp_sigpend(lp);
555 
556 	return (0);
557 }
558 
559 /*
560  * MPSAFE
561  */
562 int
563 sys_sigpending(struct sysmsg *sysmsg, const struct sigpending_args *uap)
564 {
565 	sigset_t set;
566 	int error;
567 
568 	error = kern_sigpending(&set);
569 
570 	if (error == 0)
571 		error = copyout(&set, uap->set, sizeof(set));
572 	return (error);
573 }
574 
575 /*
576  * Suspend process until signal, providing mask to be set
577  * in the meantime.
578  *
579  * MPSAFE
580  */
581 int
582 kern_sigsuspend(sigset_t *set)
583 {
584 	struct thread *td = curthread;
585 	struct lwp *lp = td->td_lwp;
586 	struct proc *p = td->td_proc;
587 	struct sigacts *ps = p->p_sigacts;
588 
589 	/*
590 	 * When returning from sigsuspend, we want
591 	 * the old mask to be restored after the
592 	 * signal handler has finished.  Thus, we
593 	 * save it here and mark the sigacts structure
594 	 * to indicate this.
595 	 */
596 	lp->lwp_oldsigmask = lp->lwp_sigmask;
597 	lp->lwp_flags |= LWP_OLDMASK;
598 
599 	SIG_CANTMASK(*set);
600 	lp->lwp_sigmask = *set;
601 	while (tsleep(ps, PCATCH, "pause", 0) == 0)
602 		/* void */;
603 	/* always return EINTR rather than ERESTART... */
604 	return (EINTR);
605 }
606 
607 /*
608  * Note nonstandard calling convention: libc stub passes mask, not
609  * pointer, to save a copyin.
610  *
611  * MPSAFE
612  */
613 int
614 sys_sigsuspend(struct sysmsg *sysmsg, const struct sigsuspend_args *uap)
615 {
616 	sigset_t mask;
617 	int error;
618 
619 	error = copyin(uap->sigmask, &mask, sizeof(mask));
620 	if (error)
621 		return (error);
622 
623 	error = kern_sigsuspend(&mask);
624 
625 	return (error);
626 }
627 
628 /*
629  * MPSAFE
630  */
631 int
632 kern_sigaltstack(stack_t *ss, stack_t *oss)
633 {
634 	struct thread *td = curthread;
635 	struct lwp *lp = td->td_lwp;
636 	struct proc *p = td->td_proc;
637 
638 	if ((lp->lwp_flags & LWP_ALTSTACK) == 0)
639 		lp->lwp_sigstk.ss_flags |= SS_DISABLE;
640 
641 	if (oss)
642 		*oss = lp->lwp_sigstk;
643 
644 	if (ss) {
645 		if (ss->ss_flags & ~SS_DISABLE)
646 			return (EINVAL);
647 		if (ss->ss_flags & SS_DISABLE) {
648 			if (lp->lwp_sigstk.ss_flags & SS_ONSTACK)
649 				return (EPERM);
650 			lp->lwp_flags &= ~LWP_ALTSTACK;
651 			lp->lwp_sigstk.ss_flags = ss->ss_flags;
652 		} else {
653 			if (ss->ss_size < p->p_sysent->sv_minsigstksz)
654 				return (ENOMEM);
655 			lp->lwp_flags |= LWP_ALTSTACK;
656 			lp->lwp_sigstk = *ss;
657 		}
658 	}
659 
660 	return (0);
661 }
662 
663 /*
664  * MPSAFE
665  */
666 int
667 sys_sigaltstack(struct sysmsg *sysmsg, const struct sigaltstack_args *uap)
668 {
669 	stack_t ss, oss;
670 	int error;
671 
672 	if (uap->ss) {
673 		error = copyin(uap->ss, &ss, sizeof(ss));
674 		if (error)
675 			return (error);
676 	}
677 
678 	error = kern_sigaltstack(uap->ss ? &ss : NULL, uap->oss ? &oss : NULL);
679 
680 	if (error == 0 && uap->oss)
681 		error = copyout(&oss, uap->oss, sizeof(*uap->oss));
682 	return (error);
683 }
684 
685 /*
686  * Common code for kill process group/broadcast kill.
687  * cp is calling process.
688  */
689 struct killpg_info {
690 	int nfound;
691 	int sig;
692 };
693 
694 static int killpg_all_callback(struct proc *p, void *data);
695 
696 static int
697 dokillpg(int sig, int pgid, int all)
698 {
699 	struct killpg_info info;
700 	struct proc *cp = curproc;
701 	struct proc *p;
702 	struct pgrp *pgrp;
703 
704 	info.nfound = 0;
705 	info.sig = sig;
706 
707 	if (all) {
708 		/*
709 		 * broadcast
710 		 */
711 		allproc_scan(killpg_all_callback, &info, 0);
712 	} else {
713 		if (pgid == 0) {
714 			/*
715 			 * zero pgid means send to my process group.
716 			 */
717 			pgrp = cp->p_pgrp;
718 			pgref(pgrp);
719 		} else {
720 			pgrp = pgfind(pgid);
721 			if (pgrp == NULL)
722 				return (ESRCH);
723 		}
724 
725 		/*
726 		 * Must interlock all signals against fork
727 		 */
728 		lockmgr(&pgrp->pg_lock, LK_EXCLUSIVE);
729 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
730 			if (p->p_pid <= 1 ||
731 			    p->p_stat == SZOMB ||
732 			    (p->p_flags & P_SYSTEM) ||
733 			    !CANSIGNAL(p, sig, 0)) {
734 				continue;
735 			}
736 			++info.nfound;
737 			if (sig)
738 				ksignal(p, sig);
739 		}
740 		lockmgr(&pgrp->pg_lock, LK_RELEASE);
741 		pgrel(pgrp);
742 	}
743 	return (info.nfound ? 0 : ESRCH);
744 }
745 
746 static int
747 killpg_all_callback(struct proc *p, void *data)
748 {
749 	struct killpg_info *info = data;
750 
751 	if (p->p_pid <= 1 || (p->p_flags & P_SYSTEM) ||
752 	    p == curproc || !CANSIGNAL(p, info->sig, 0)) {
753 		return (0);
754 	}
755 	++info->nfound;
756 	if (info->sig)
757 		ksignal(p, info->sig);
758 	return(0);
759 }
760 
761 /*
762  * Send a general signal to a process or LWPs within that process.
763  *
764  * Note that new signals cannot be sent if a process is exiting or already
765  * a zombie, but we return success anyway as userland is likely to not handle
766  * the race properly.
767  *
768  * No requirements.
769  */
770 int
771 kern_kill(int sig, pid_t pid, lwpid_t tid)
772 {
773 	int t;
774 
775 	if ((u_int)sig >= _SIG_MAXSIG)
776 		return (EINVAL);
777 
778 	if (pid > 0) {
779 		struct proc *p;
780 		struct lwp *lp = NULL;
781 
782 		/*
783 		 * Send a signal to a single process.  If the kill() is
784 		 * racing an exiting process which has not yet been reaped
785 		 * act as though the signal was delivered successfully but
786 		 * don't actually try to deliver the signal.
787 		 */
788 		if ((p = pfind(pid)) == NULL) {
789 			if ((p = zpfind(pid)) == NULL)
790 				return (ESRCH);
791 			PRELE(p);
792 			return (0);
793 		}
794 		if (p != curproc) {
795 			lwkt_gettoken_shared(&p->p_token);
796 			if (!CANSIGNAL(p, sig, 1)) {
797 				lwkt_reltoken(&p->p_token);
798 				PRELE(p);
799 				return (EPERM);
800 			}
801 			lwkt_reltoken(&p->p_token);
802 		}
803 
804 		/*
805 		 * NOP if the process is exiting.  Note that lwpsignal() is
806 		 * called directly with P_WEXIT set to kill individual LWPs
807 		 * during exit, which is allowed.
808 		 */
809 		if (p->p_flags & P_WEXIT) {
810 			PRELE(p);
811 			return (0);
812 		}
813 		if (tid != -1) {
814 			lwkt_gettoken_shared(&p->p_token);
815 			lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, tid);
816 			if (lp == NULL) {
817 				lwkt_reltoken(&p->p_token);
818 				PRELE(p);
819 				return (ESRCH);
820 			}
821 			LWPHOLD(lp);
822 			lwkt_reltoken(&p->p_token);
823 		}
824 		if (sig)
825 			lwpsignal(p, lp, sig);
826 		if (lp)
827 			LWPRELE(lp);
828 		PRELE(p);
829 
830 		return (0);
831 	}
832 
833 	/*
834 	 * If we come here, pid is a special broadcast pid.
835 	 * This doesn't mix with a tid.
836 	 */
837 	if (tid != -1)
838 		return (EINVAL);
839 
840 	switch (pid) {
841 	case -1:		/* broadcast signal */
842 		t = (dokillpg(sig, 0, 1));
843 		break;
844 	case 0:			/* signal own process group */
845 		t = (dokillpg(sig, 0, 0));
846 		break;
847 	default:		/* negative explicit process group */
848 		t = (dokillpg(sig, -pid, 0));
849 		break;
850 	}
851 	return t;
852 }
853 
854 int
855 sys_kill(struct sysmsg *sysmsg, const struct kill_args *uap)
856 {
857 	int error;
858 
859 	error = kern_kill(uap->signum, uap->pid, -1);
860 	return (error);
861 }
862 
863 int
864 sys_lwp_kill(struct sysmsg *sysmsg, const struct lwp_kill_args *uap)
865 {
866 	int error;
867 	pid_t pid = uap->pid;
868 
869 	/*
870 	 * A tid is mandatory for lwp_kill(), otherwise
871 	 * you could simply use kill().
872 	 */
873 	if (uap->tid == -1)
874 		return (EINVAL);
875 
876 	/*
877 	 * To save on a getpid() function call for intra-process
878 	 * signals, pid == -1 means current process.
879 	 */
880 	if (pid == -1)
881 		pid = curproc->p_pid;
882 
883 	error = kern_kill(uap->signum, pid, uap->tid);
884 	return (error);
885 }
886 
887 /*
888  * Send a signal to a process group.
889  */
890 void
891 gsignal(int pgid, int sig)
892 {
893 	struct pgrp *pgrp;
894 
895 	if (pgid && (pgrp = pgfind(pgid)))
896 		pgsignal(pgrp, sig, 0);
897 }
898 
899 /*
900  * Send a signal to a process group.  If checktty is 1,
901  * limit to members which have a controlling terminal.
902  *
903  * pg_lock interlocks against a fork that might be in progress, to
904  * ensure that the new child process picks up the signal.
905  */
906 void
907 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
908 {
909 	struct proc *p;
910 
911 	/*
912 	 * Must interlock all signals against fork
913 	 */
914 	if (pgrp) {
915 		pgref(pgrp);
916 		lockmgr(&pgrp->pg_lock, LK_EXCLUSIVE);
917 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
918 			if (checkctty == 0 || p->p_flags & P_CONTROLT)
919 				ksignal(p, sig);
920 		}
921 		lockmgr(&pgrp->pg_lock, LK_RELEASE);
922 		pgrel(pgrp);
923 	}
924 }
925 
926 /*
927  * Send a signal caused by a trap to the current lwp.  If it will be caught
928  * immediately, deliver it with correct code.  Otherwise, post it normally.
929  *
930  * These signals may ONLY be delivered to the specified lwp and may never
931  * be delivered to the process generically.
932  *
933  * lpmap->blockallsigs is ignored.
934  */
935 void
936 trapsignal(struct lwp *lp, int sig, u_long code)
937 {
938 	struct proc *p = lp->lwp_proc;
939 	struct sigacts *ps = p->p_sigacts;
940 
941 	/*
942 	 * If we are a virtual kernel running an emulated user process
943 	 * context, switch back to the virtual kernel context before
944 	 * trying to post the signal.
945 	 */
946 	if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
947 		struct trapframe *tf = lp->lwp_md.md_regs;
948 		tf->tf_trapno = 0;
949 		vkernel_trap(lp, tf);
950 	}
951 
952 	if ((p->p_flags & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
953 	    !SIGISMEMBER(lp->lwp_sigmask, sig)) {
954 		lp->lwp_ru.ru_nsignals++;
955 #ifdef KTRACE
956 		if (KTRPOINT(lp->lwp_thread, KTR_PSIG))
957 			ktrpsig(lp, sig, ps->ps_sigact[_SIG_IDX(sig)],
958 				&lp->lwp_sigmask, code);
959 #endif
960 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
961 						&lp->lwp_sigmask, code);
962 		SIGSETOR(lp->lwp_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
963 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
964 			SIGADDSET(lp->lwp_sigmask, sig);
965 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
966 			/*
967 			 * See kern_sigaction() for origin of this code.
968 			 */
969 			SIGDELSET(p->p_sigcatch, sig);
970 			if (sig != SIGCONT &&
971 			    sigprop(sig) & SA_IGNORE)
972 				SIGADDSET(p->p_sigignore, sig);
973 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
974 		}
975 	} else {
976 		lp->lwp_code = code;	/* XXX for core dump/debugger */
977 		lp->lwp_sig = sig;	/* XXX to verify code */
978 		lwpsignal(p, lp, sig);
979 	}
980 }
981 
982 /*
983  * Find a suitable lwp to deliver the signal to.  Returns NULL if all
984  * lwps hold the signal blocked.
985  *
986  * Caller must hold p->p_token.
987  *
988  * Returns a lp or NULL.  If non-NULL the lp is held and its token is
989  * acquired.
990  */
991 static struct lwp *
992 find_lwp_for_signal(struct proc *p, int sig)
993 {
994 	struct lwp *lp;
995 	struct lwp *run, *sleep, *stop;
996 
997 	/*
998 	 * If the running/preempted thread belongs to the proc to which
999 	 * the signal is being delivered and this thread does not block
1000 	 * the signal, then we can avoid a context switch by delivering
1001 	 * the signal to this thread, because it will return to userland
1002 	 * soon anyways.
1003 	 */
1004 	lp = lwkt_preempted_proc();
1005 	if (lp != NULL && lp->lwp_proc == p) {
1006 		LWPHOLD(lp);
1007 		lwkt_gettoken(&lp->lwp_token);
1008 		if (!SIGISMEMBER(lp->lwp_sigmask, sig)) {
1009 			/* return w/ token held */
1010 			return (lp);
1011 		}
1012 		lwkt_reltoken(&lp->lwp_token);
1013 		LWPRELE(lp);
1014 	}
1015 
1016 	run = sleep = stop = NULL;
1017 	FOREACH_LWP_IN_PROC(lp, p) {
1018 		/*
1019 		 * If the signal is being blocked by the lwp, then this
1020 		 * lwp is not eligible for receiving the signal.
1021 		 */
1022 		LWPHOLD(lp);
1023 		lwkt_gettoken(&lp->lwp_token);
1024 
1025 		if (SIGISMEMBER(lp->lwp_sigmask, sig)) {
1026 			lwkt_reltoken(&lp->lwp_token);
1027 			LWPRELE(lp);
1028 			continue;
1029 		}
1030 
1031 		switch (lp->lwp_stat) {
1032 		case LSRUN:
1033 			if (sleep) {
1034 				lwkt_token_swap();
1035 				lwkt_reltoken(&sleep->lwp_token);
1036 				LWPRELE(sleep);
1037 				sleep = NULL;
1038 				run = lp;
1039 			} else if (stop) {
1040 				lwkt_token_swap();
1041 				lwkt_reltoken(&stop->lwp_token);
1042 				LWPRELE(stop);
1043 				stop = NULL;
1044 				run = lp;
1045 			} else {
1046 				run = lp;
1047 			}
1048 			break;
1049 		case LSSLEEP:
1050 			if (lp->lwp_flags & LWP_SINTR) {
1051 				if (sleep) {
1052 					lwkt_reltoken(&lp->lwp_token);
1053 					LWPRELE(lp);
1054 				} else if (stop) {
1055 					lwkt_token_swap();
1056 					lwkt_reltoken(&stop->lwp_token);
1057 					LWPRELE(stop);
1058 					stop = NULL;
1059 					sleep = lp;
1060 				} else {
1061 					sleep = lp;
1062 				}
1063 			} else {
1064 				lwkt_reltoken(&lp->lwp_token);
1065 				LWPRELE(lp);
1066 			}
1067 			break;
1068 		case LSSTOP:
1069 			if (sleep) {
1070 				lwkt_reltoken(&lp->lwp_token);
1071 				LWPRELE(lp);
1072 			} else if (stop) {
1073 				lwkt_reltoken(&lp->lwp_token);
1074 				LWPRELE(lp);
1075 			} else {
1076 				stop = lp;
1077 			}
1078 			break;
1079 		}
1080 		if (run)
1081 			break;
1082 	}
1083 
1084 	if (run != NULL)
1085 		return (run);
1086 	else if (sleep != NULL)
1087 		return (sleep);
1088 	else
1089 		return (stop);
1090 }
1091 
1092 /*
1093  * Send the signal to the process.  If the signal has an action, the action
1094  * is usually performed by the target process rather than the caller; we add
1095  * the signal to the set of pending signals for the process.
1096  *
1097  * Exceptions:
1098  *   o When a stop signal is sent to a sleeping process that takes the
1099  *     default action, the process is stopped without awakening it.
1100  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1101  *     regardless of the signal action (eg, blocked or ignored).
1102  *
1103  * Other ignored signals are discarded immediately.
1104  *
1105  * If the caller wishes to call this function from a hard code section the
1106  * caller must already hold p->p_token (see kern_clock.c).
1107  *
1108  * No requirements.
1109  */
1110 void
1111 ksignal(struct proc *p, int sig)
1112 {
1113 	lwpsignal(p, NULL, sig);
1114 }
1115 
1116 /*
1117  * The core for ksignal.  lp may be NULL, then a suitable thread
1118  * will be chosen.  If not, lp MUST be a member of p.
1119  *
1120  * If the caller wishes to call this function from a hard code section the
1121  * caller must already hold p->p_token.
1122  *
1123  * No requirements.
1124  */
1125 void
1126 lwpsignal(struct proc *p, struct lwp *lp, int sig)
1127 {
1128 	struct proc *q;
1129 	sig_t action;
1130 	int prop;
1131 
1132 	if (sig >= _SIG_MAXSIG || sig <= 0) {
1133 		kprintf("lwpsignal: signal %d\n", sig);
1134 		panic("lwpsignal signal number");
1135 	}
1136 
1137 	KKASSERT(lp == NULL || lp->lwp_proc == p);
1138 
1139 	/*
1140 	 * We don't want to race... well, all sorts of things.  Get appropriate
1141 	 * tokens.
1142 	 *
1143 	 * Don't try to deliver a generic signal to an exiting process,
1144 	 * the signal structures could be in flux.  We check the LWP later
1145 	 * on.
1146 	 */
1147 	PHOLD(p);
1148 	if (lp) {
1149 		LWPHOLD(lp);
1150 		lwkt_gettoken(&lp->lwp_token);
1151 	} else {
1152 		lwkt_gettoken(&p->p_token);
1153 		if (p->p_flags & P_WEXIT)
1154 			goto out;
1155 	}
1156 
1157 	prop = sigprop(sig);
1158 
1159 	/*
1160 	 * If proc is traced, always give parent a chance;
1161 	 * if signal event is tracked by procfs, give *that*
1162 	 * a chance, as well.
1163 	 */
1164 	if ((p->p_flags & P_TRACED) || (p->p_stops & S_SIG)) {
1165 		action = SIG_DFL;
1166 	} else {
1167 		/*
1168 		 * Do not try to deliver signals to an exiting lwp other
1169 		 * than SIGKILL.  Note that we must still deliver the signal
1170 		 * if P_WEXIT is set in the process flags.
1171 		 */
1172 		if (lp && (lp->lwp_mpflags & LWP_MP_WEXIT) && sig != SIGKILL) {
1173 			lwkt_reltoken(&lp->lwp_token);
1174 			LWPRELE(lp);
1175 			PRELE(p);
1176 			return;
1177 		}
1178 
1179 		/*
1180 		 * If the signal is being ignored, then we forget about
1181 		 * it immediately.  NOTE: We don't set SIGCONT in p_sigignore,
1182 		 * and if it is set to SIG_IGN, action will be SIG_DFL here.
1183 		 */
1184 		if (SIGISMEMBER(p->p_sigignore, sig)) {
1185 			/*
1186 			 * Even if a signal is set SIG_IGN, it may still be
1187 			 * lurking in a kqueue.
1188 			 */
1189 			KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1190 			if (lp) {
1191 				lwkt_reltoken(&lp->lwp_token);
1192 				LWPRELE(lp);
1193 			} else {
1194 				lwkt_reltoken(&p->p_token);
1195 			}
1196 			PRELE(p);
1197 			return;
1198 		}
1199 		if (SIGISMEMBER(p->p_sigcatch, sig))
1200 			action = SIG_CATCH;
1201 		else
1202 			action = SIG_DFL;
1203 	}
1204 
1205 	/*
1206 	 * If continuing, clear any pending STOP signals for the whole
1207 	 * process.
1208 	 */
1209 	if (prop & SA_CONT) {
1210 		lwkt_gettoken(&p->p_token);
1211 		SIG_STOPSIGMASK_ATOMIC(p->p_siglist);
1212 		lwkt_reltoken(&p->p_token);
1213 	}
1214 
1215 	if (prop & SA_STOP) {
1216 		/*
1217 		 * If sending a tty stop signal to a member of an orphaned
1218 		 * process group, discard the signal here if the action
1219 		 * is default; don't stop the process below if sleeping,
1220 		 * and don't clear any pending SIGCONT.
1221 		 */
1222 		if ((prop & SA_TTYSTOP) && p->p_pgrp->pg_jobc == 0 &&
1223 		    action == SIG_DFL) {
1224 			if (lp) {
1225 				lwkt_reltoken(&lp->lwp_token);
1226 				LWPRELE(lp);
1227 			} else {
1228 				lwkt_reltoken(&p->p_token);
1229 			}
1230 			PRELE(p);
1231 			return;
1232 		}
1233 		lwkt_gettoken(&p->p_token);
1234 		SIG_CONTSIGMASK_ATOMIC(p->p_siglist);
1235 		p->p_flags &= ~P_CONTINUED;
1236 		lwkt_reltoken(&p->p_token);
1237 	}
1238 
1239 	if (p->p_stat == SSTOP) {
1240 		/*
1241 		 * Nobody can handle this signal, add it to the lwp or
1242 		 * process pending list
1243 		 */
1244 		lwkt_gettoken(&p->p_token);
1245 		if (p->p_stat != SSTOP) {
1246 			lwkt_reltoken(&p->p_token);
1247 			goto not_stopped;
1248 		}
1249 		sigsetfrompid(curthread, p, sig);
1250 		if (lp) {
1251 			spin_lock(&lp->lwp_spin);
1252 			SIGADDSET(lp->lwp_siglist, sig);
1253 			spin_unlock(&lp->lwp_spin);
1254 		} else {
1255 			SIGADDSET_ATOMIC(p->p_siglist, sig);
1256 		}
1257 
1258 		/*
1259 		 * If the process is stopped and is being traced, then no
1260 		 * further action is necessary.
1261 		 */
1262 		if (p->p_flags & P_TRACED) {
1263 			lwkt_reltoken(&p->p_token);
1264 			goto out;
1265 		}
1266 
1267 		/*
1268 		 * If the process is stopped and receives a KILL signal,
1269 		 * make the process runnable.
1270 		 */
1271 		if (sig == SIGKILL) {
1272 			proc_unstop(p, SSTOP);
1273 			lwkt_reltoken(&p->p_token);
1274 			goto active_process;
1275 		}
1276 
1277 		/*
1278 		 * If the process is stopped and receives a CONT signal,
1279 		 * then try to make the process runnable again.
1280 		 */
1281 		if (prop & SA_CONT) {
1282 			/*
1283 			 * If SIGCONT is default (or ignored), we continue the
1284 			 * process but don't leave the signal in p_siglist, as
1285 			 * it has no further action.  If SIGCONT is held, we
1286 			 * continue the process and leave the signal in
1287 			 * p_siglist.  If the process catches SIGCONT, let it
1288 			 * handle the signal itself.
1289 			 *
1290 			 * XXX what if the signal is being held blocked?
1291 			 *
1292 			 * Token required to interlock kern_wait().
1293 			 * Reparenting can also cause a race so we have to
1294 			 * hold (q).
1295 			 */
1296 			q = p->p_pptr;
1297 			PHOLD(q);
1298 			lwkt_gettoken(&q->p_token);
1299 			p->p_flags |= P_CONTINUED;
1300 			wakeup(q);
1301 			if (action == SIG_DFL)
1302 				SIGDELSET_ATOMIC(p->p_siglist, sig);
1303 			proc_unstop(p, SSTOP);
1304 			lwkt_reltoken(&q->p_token);
1305 			PRELE(q);
1306 			lwkt_reltoken(&p->p_token);
1307 			if (action == SIG_CATCH)
1308 				goto active_process;
1309 			goto out;
1310 		}
1311 
1312 		/*
1313 		 * If the process is stopped and receives another STOP
1314 		 * signal, we do not need to stop it again.  If we did
1315 		 * the shell could get confused.
1316 		 *
1317 		 * However, if the current/preempted lwp is part of the
1318 		 * process receiving the signal, we need to keep it,
1319 		 * so that this lwp can stop in issignal() later, as
1320 		 * we don't want to wait until it reaches userret!
1321 		 */
1322 		if (prop & SA_STOP) {
1323 			if (lwkt_preempted_proc() == NULL ||
1324 			    lwkt_preempted_proc()->lwp_proc != p) {
1325 				SIGDELSET_ATOMIC(p->p_siglist, sig);
1326 			}
1327 		}
1328 
1329 		/*
1330 		 * Otherwise the process is stopped and it received some
1331 		 * signal, which does not change its stopped state.  When
1332 		 * the process is continued a wakeup(p) will be issued which
1333 		 * will wakeup any threads sleeping in tstop().
1334 		 */
1335 		lwkt_reltoken(&p->p_token);
1336 		goto out;
1337 		/* NOTREACHED */
1338 	}
1339 not_stopped:
1340 	;
1341 	/* else not stopped */
1342 active_process:
1343 
1344 	/*
1345 	 * Never deliver a lwp-specific signal to a random lwp.
1346 	 */
1347 	if (lp == NULL) {
1348 		/* NOTE: returns lp w/ token held */
1349 		lp = find_lwp_for_signal(p, sig);
1350 		if (lp) {
1351 			if (SIGISMEMBER(lp->lwp_sigmask, sig)) {
1352 				lwkt_reltoken(&lp->lwp_token);
1353 				LWPRELE(lp);
1354 				lp = NULL;
1355 				/* maintain proc token */
1356 			} else {
1357 				lwkt_token_swap();
1358 				lwkt_reltoken(&p->p_token);
1359 				/* maintain lp token */
1360 			}
1361 		}
1362 	}
1363 
1364 	/*
1365 	 * Deliver to the process generically if (1) the signal is being
1366 	 * sent to any thread or (2) we could not find a thread to deliver
1367 	 * it to.
1368 	 */
1369 	if (lp == NULL) {
1370 		sigsetfrompid(curthread, p, sig);
1371 		KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1372 		SIGADDSET_ATOMIC(p->p_siglist, sig);
1373 		goto out;
1374 	}
1375 
1376 	/*
1377 	 * Deliver to a specific LWP whether it masks it or not.  It will
1378 	 * not be dispatched if masked but we must still deliver it.
1379 	 */
1380 	if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1381 	    (p->p_flags & P_TRACED) == 0) {
1382 		lwkt_gettoken(&p->p_token);
1383 		p->p_nice = NZERO;
1384 		lwkt_reltoken(&p->p_token);
1385 	}
1386 
1387 	/*
1388 	 * If the process receives a STOP signal which indeed needs to
1389 	 * stop the process, do so.  If the process chose to catch the
1390 	 * signal, it will be treated like any other signal.
1391 	 */
1392 	if ((prop & SA_STOP) && action == SIG_DFL) {
1393 		/*
1394 		 * If a child holding parent blocked, stopping
1395 		 * could cause deadlock.  Take no action at this
1396 		 * time.
1397 		 */
1398 		lwkt_gettoken(&p->p_token);
1399 		if (p->p_flags & P_PPWAIT) {
1400 			sigsetfrompid(curthread, p, sig);
1401 			SIGADDSET_ATOMIC(p->p_siglist, sig);
1402 			lwkt_reltoken(&p->p_token);
1403 			goto out;
1404 		}
1405 
1406 		/*
1407 		 * Do not actually try to manipulate the process, but simply
1408 		 * stop it.  Lwps will stop as soon as they safely can.
1409 		 *
1410 		 * Ignore stop if the process is exiting.
1411 		 */
1412 		if ((p->p_flags & P_WEXIT) == 0) {
1413 			p->p_xstat = sig;
1414 			proc_stop(p, SSTOP);
1415 		}
1416 		lwkt_reltoken(&p->p_token);
1417 		goto out;
1418 	}
1419 
1420 	/*
1421 	 * If it is a CONT signal with default action, just ignore it.
1422 	 */
1423 	if ((prop & SA_CONT) && action == SIG_DFL)
1424 		goto out;
1425 
1426 	/*
1427 	 * Mark signal pending at this specific thread.
1428 	 */
1429 	sigsetfrompid(curthread, p, sig);
1430 	spin_lock(&lp->lwp_spin);
1431 	SIGADDSET(lp->lwp_siglist, sig);
1432 	spin_unlock(&lp->lwp_spin);
1433 
1434 	lwp_signotify(lp);
1435 
1436 out:
1437 	if (lp) {
1438 		lwkt_reltoken(&lp->lwp_token);
1439 		LWPRELE(lp);
1440 	} else {
1441 		lwkt_reltoken(&p->p_token);
1442 	}
1443 	PRELE(p);
1444 }
1445 
1446 /*
1447  * Notify the LWP that a signal has arrived.  The LWP does not have to be
1448  * sleeping on the current cpu.
1449  *
1450  * p->p_token and lp->lwp_token must be held on call.
1451  *
1452  * We can only safely schedule the thread on its current cpu and only if
1453  * one of the SINTR flags is set.  If an SINTR flag is set AND we are on
1454  * the correct cpu we are properly interlocked, otherwise we could be
1455  * racing other thread transition states (or the lwp is on the user scheduler
1456  * runq but not scheduled) and must not do anything.
1457  *
1458  * Since we hold the lwp token we know the lwp cannot be ripped out from
1459  * under us so we can safely hold it to prevent it from being ripped out
1460  * from under us if we are forced to IPI another cpu to make the local
1461  * checks there.
1462  *
1463  * Adjustment of lp->lwp_stat can only occur when we hold the lwp_token,
1464  * which we won't in an IPI so any fixups have to be done here, effectively
1465  * replicating part of what setrunnable() does.
1466  */
1467 static void
1468 lwp_signotify(struct lwp *lp)
1469 {
1470 	thread_t dtd;
1471 
1472 	ASSERT_LWKT_TOKEN_HELD(&lp->lwp_token);
1473 	dtd = lp->lwp_thread;
1474 
1475 	crit_enter();
1476 	if (lp == lwkt_preempted_proc()) {
1477 		/*
1478 		 * lwp is on the current cpu AND it is currently running
1479 		 * (we preempted it).
1480 		 */
1481 		signotify();
1482 	} else if (lp->lwp_flags & LWP_SINTR) {
1483 		/*
1484 		 * lwp is sitting in tsleep() with PCATCH set
1485 		 */
1486 		if (dtd->td_gd == mycpu) {
1487 			setrunnable(lp);
1488 		} else {
1489 			/*
1490 			 * We can only adjust lwp_stat while we hold the
1491 			 * lwp_token, and we won't in the IPI function.
1492 			 */
1493 			LWPHOLD(lp);
1494 			if (lp->lwp_stat == LSSTOP)
1495 				lp->lwp_stat = LSSLEEP;
1496 			lwkt_send_ipiq(dtd->td_gd, lwp_signotify_remote, lp);
1497 		}
1498 	} else if (dtd->td_flags & TDF_SINTR) {
1499 		/*
1500 		 * lwp is sitting in lwkt_sleep() with PCATCH set.
1501 		 */
1502 		if (dtd->td_gd == mycpu) {
1503 			setrunnable(lp);
1504 		} else {
1505 			/*
1506 			 * We can only adjust lwp_stat while we hold the
1507 			 * lwp_token, and we won't in the IPI function.
1508 			 */
1509 			LWPHOLD(lp);
1510 			if (lp->lwp_stat == LSSTOP)
1511 				lp->lwp_stat = LSSLEEP;
1512 			lwkt_send_ipiq(dtd->td_gd, lwp_signotify_remote, lp);
1513 		}
1514 	} else {
1515 		/*
1516 		 * Otherwise the lwp is either in some uninterruptible state
1517 		 * or it is on the userland scheduler's runqueue waiting to
1518 		 * be scheduled to a cpu, or it is running in userland.  We
1519 		 * generally want to send an IPI so a running target gets the
1520 		 * signal ASAP, otherwise a scheduler-tick worth of latency
1521 		 * will occur.
1522 		 *
1523 		 * Issue an IPI to the remote cpu to knock it into the kernel,
1524 		 * remote cpu will issue the cpu-local signotify() if the IPI
1525 		 * preempts the desired thread.
1526 		 */
1527 		if (dtd->td_gd != mycpu) {
1528 			LWPHOLD(lp);
1529 			lwkt_send_ipiq(dtd->td_gd, lwp_signotify_remote, lp);
1530 		}
1531 	}
1532 	crit_exit();
1533 }
1534 
1535 /*
1536  * This function is called via an IPI so we cannot call setrunnable() here
1537  * (because while we hold the lp we don't own its token, and can't get it
1538  * from an IPI).
1539  *
1540  * We are interlocked by virtue of being on the same cpu as the target.  If
1541  * we still are and LWP_SINTR or TDF_SINTR is set we can safely schedule
1542  * the target thread.
1543  */
1544 static void
1545 lwp_signotify_remote(void *arg)
1546 {
1547 	struct lwp *lp = arg;
1548 	thread_t td = lp->lwp_thread;
1549 
1550 	if (lp == lwkt_preempted_proc()) {
1551 		signotify();
1552 		LWPRELE(lp);
1553 	} else if (td->td_gd == mycpu) {
1554 		if ((lp->lwp_flags & LWP_SINTR) ||
1555 		    (td->td_flags & TDF_SINTR)) {
1556 			lwkt_schedule(td);
1557 		}
1558 		LWPRELE(lp);
1559 	} else {
1560 		lwkt_send_ipiq(td->td_gd, lwp_signotify_remote, lp);
1561 		/* LWPHOLD() is forwarded to the target cpu */
1562 	}
1563 }
1564 
1565 /*
1566  * Caller must hold p->p_token
1567  */
1568 void
1569 proc_stop(struct proc *p, int stat)
1570 {
1571 	struct proc *q;
1572 	struct lwp *lp;
1573 
1574 	ASSERT_LWKT_TOKEN_HELD(&p->p_token);
1575 
1576 	/*
1577 	 * If somebody raced us, be happy with it.  SCORE overrides SSTOP.
1578 	 */
1579 	if (stat == SCORE) {
1580 		if (p->p_stat == SCORE || p->p_stat == SZOMB)
1581 			return;
1582 	} else {
1583 		if (p->p_stat == SSTOP || p->p_stat == SCORE ||
1584 		    p->p_stat == SZOMB) {
1585 			return;
1586 		}
1587 	}
1588 	p->p_stat = stat;
1589 
1590 	FOREACH_LWP_IN_PROC(lp, p) {
1591 		LWPHOLD(lp);
1592 		lwkt_gettoken(&lp->lwp_token);
1593 
1594 		switch (lp->lwp_stat) {
1595 		case LSSTOP:
1596 			/*
1597 			 * Do nothing, we are already counted in
1598 			 * p_nstopped.
1599 			 */
1600 			break;
1601 
1602 		case LSSLEEP:
1603 			/*
1604 			 * We're sleeping, but we will stop before
1605 			 * returning to userspace, so count us
1606 			 * as stopped as well.  We set LWP_MP_WSTOP
1607 			 * to signal the lwp that it should not
1608 			 * increase p_nstopped when reaching tstop().
1609 			 *
1610 			 * LWP_MP_WSTOP is protected by lp->lwp_token.
1611 			 */
1612 			if ((lp->lwp_mpflags & LWP_MP_WSTOP) == 0) {
1613 				atomic_set_int(&lp->lwp_mpflags, LWP_MP_WSTOP);
1614 				++p->p_nstopped;
1615 			}
1616 			break;
1617 
1618 		case LSRUN:
1619 			/*
1620 			 * We might notify ourself, but that's not
1621 			 * a problem.
1622 			 */
1623 			lwp_signotify(lp);
1624 			break;
1625 		}
1626 		lwkt_reltoken(&lp->lwp_token);
1627 		LWPRELE(lp);
1628 	}
1629 
1630 	if (p->p_nstopped == p->p_nthreads) {
1631 		/*
1632 		 * Token required to interlock kern_wait().  Reparenting can
1633 		 * also cause a race so we have to hold (q).
1634 		 */
1635 		q = p->p_pptr;
1636 		PHOLD(q);
1637 		lwkt_gettoken(&q->p_token);
1638 		p->p_flags &= ~P_WAITED;
1639 		wakeup(q);
1640 		if ((q->p_sigacts->ps_flag & PS_NOCLDSTOP) == 0)
1641 			ksignal(p->p_pptr, SIGCHLD);
1642 		lwkt_reltoken(&q->p_token);
1643 		PRELE(q);
1644 	}
1645 }
1646 
1647 /*
1648  * Caller must hold p_token
1649  */
1650 void
1651 proc_unstop(struct proc *p, int stat)
1652 {
1653 	struct lwp *lp;
1654 
1655 	ASSERT_LWKT_TOKEN_HELD(&p->p_token);
1656 
1657 	if (p->p_stat != stat)
1658 		return;
1659 
1660 	p->p_stat = SACTIVE;
1661 
1662 	FOREACH_LWP_IN_PROC(lp, p) {
1663 		LWPHOLD(lp);
1664 		lwkt_gettoken(&lp->lwp_token);
1665 
1666 		switch (lp->lwp_stat) {
1667 		case LSRUN:
1668 			/*
1669 			 * Uh?  Not stopped?  Well, I guess that's okay.
1670 			 */
1671 			if (bootverbose)
1672 				kprintf("proc_unstop: lwp %d/%d not sleeping\n",
1673 					p->p_pid, lp->lwp_tid);
1674 			break;
1675 
1676 		case LSSLEEP:
1677 			/*
1678 			 * Still sleeping.  Don't bother waking it up.
1679 			 * However, if this thread was counted as
1680 			 * stopped, undo this.
1681 			 *
1682 			 * Nevertheless we call setrunnable() so that it
1683 			 * will wake up in case a signal or timeout arrived
1684 			 * in the meantime.
1685 			 *
1686 			 * LWP_MP_WSTOP is protected by lp->lwp_token.
1687 			 */
1688 			if (lp->lwp_mpflags & LWP_MP_WSTOP) {
1689 				atomic_clear_int(&lp->lwp_mpflags,
1690 						 LWP_MP_WSTOP);
1691 				--p->p_nstopped;
1692 			} else {
1693 				if (bootverbose)
1694 					kprintf("proc_unstop: lwp %d/%d sleeping, not stopped\n",
1695 						p->p_pid, lp->lwp_tid);
1696 			}
1697 			/* FALLTHROUGH */
1698 
1699 		case LSSTOP:
1700 			/*
1701 			 * This handles any lwp's waiting in a tsleep with
1702 			 * SIGCATCH.
1703 			 */
1704 			lwp_signotify(lp);
1705 			break;
1706 
1707 		}
1708 		lwkt_reltoken(&lp->lwp_token);
1709 		LWPRELE(lp);
1710 	}
1711 
1712 	/*
1713 	 * This handles any lwp's waiting in tstop().  We have interlocked
1714 	 * the setting of p_stat by acquiring and releasing each lpw's
1715 	 * token.
1716 	 */
1717 	wakeup(p);
1718 }
1719 
1720 /*
1721  * Wait for all threads except the current thread to stop.
1722  */
1723 static void
1724 proc_stopwait(struct proc *p)
1725 {
1726 	while ((p->p_stat == SSTOP || p->p_stat == SCORE) &&
1727 	       p->p_nstopped < p->p_nthreads - 1) {
1728 		tsleep_interlock(&p->p_nstopped, 0);
1729 		if (p->p_nstopped < p->p_nthreads - 1) {
1730 			tsleep(&p->p_nstopped, PINTERLOCKED, "stopwt", hz);
1731 		}
1732 	}
1733 }
1734 
1735 /*
1736  * No requirements.
1737  */
1738 static int
1739 kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
1740 {
1741 	sigset_t savedmask, set;
1742 	struct proc *p = curproc;
1743 	struct lwp *lp = curthread->td_lwp;
1744 	int error, sig, hz, timevalid = 0;
1745 	struct timespec rts, ets, ts;
1746 	struct timeval tv;
1747 
1748 	error = 0;
1749 	sig = 0;
1750 	ets.tv_sec = 0;		/* silence compiler warning */
1751 	ets.tv_nsec = 0;	/* silence compiler warning */
1752 	SIG_CANTMASK(waitset);
1753 	savedmask = lp->lwp_sigmask;
1754 
1755 	if (timeout) {
1756 		if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 &&
1757 		    timeout->tv_nsec < 1000000000) {
1758 			timevalid = 1;
1759 			getnanouptime(&rts);
1760 			timespecadd(&rts, timeout, &ets);
1761 		}
1762 	}
1763 
1764 	for (;;) {
1765 		set = lwp_sigpend(lp);
1766 		SIGSETAND(set, waitset);
1767 		if ((sig = sig_ffs(&set)) != 0) {
1768 			SIGFILLSET(lp->lwp_sigmask);
1769 			SIGDELSET(lp->lwp_sigmask, sig);
1770 			SIG_CANTMASK(lp->lwp_sigmask);
1771 			sig = issignal(lp, 1, 0);
1772 			/*
1773 			 * It may be a STOP signal, in the case, issignal
1774 			 * returns 0, because we may stop there, and new
1775 			 * signal can come in, we should restart if we got
1776 			 * nothing.
1777 			 */
1778 			if (sig == 0)
1779 				continue;
1780 			else
1781 				break;
1782 		}
1783 
1784 		/*
1785 		 * Previous checking got nothing, and we retried but still
1786 		 * got nothing, we should return the error status.
1787 		 */
1788 		if (error)
1789 			break;
1790 
1791 		/*
1792 		 * POSIX says this must be checked after looking for pending
1793 		 * signals.
1794 		 */
1795 		if (timeout) {
1796 			if (timevalid == 0) {
1797 				error = EINVAL;
1798 				break;
1799 			}
1800 			getnanouptime(&rts);
1801 			if (timespeccmp(&rts, &ets, >=)) {
1802 				error = EAGAIN;
1803 				break;
1804 			}
1805 			timespecsub(&ets, &rts, &ts);
1806 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
1807 			hz = tvtohz_high(&tv);
1808 		} else {
1809 			hz = 0;
1810 		}
1811 
1812 		lp->lwp_sigmask = savedmask;
1813 		SIGSETNAND(lp->lwp_sigmask, waitset);
1814 		/*
1815 		 * We won't ever be woken up.  Instead, our sleep will
1816 		 * be broken in lwpsignal().
1817 		 */
1818 		error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz);
1819 		if (timeout) {
1820 			if (error == ERESTART) {
1821 				/* can not restart a timeout wait. */
1822 				error = EINTR;
1823 			} else if (error == EAGAIN) {
1824 				/* will calculate timeout by ourself. */
1825 				error = 0;
1826 			}
1827 		}
1828 		/* Retry ... */
1829 	}
1830 
1831 	lp->lwp_sigmask = savedmask;
1832 	if (sig) {
1833 		error = 0;
1834 		bzero(info, sizeof(*info));
1835 		info->si_signo = sig;
1836 		spin_lock(&lp->lwp_spin);
1837 		lwp_delsig(lp, sig, 1);	/* take the signal! */
1838 		spin_unlock(&lp->lwp_spin);
1839 
1840 		if (sig == SIGKILL) {
1841 			sigexit(lp, sig);
1842 			/* NOT REACHED */
1843 		}
1844 	}
1845 
1846 	return (error);
1847 }
1848 
1849 /*
1850  * MPALMOSTSAFE
1851  */
1852 int
1853 sys_sigtimedwait(struct sysmsg *sysmsg, const struct sigtimedwait_args *uap)
1854 {
1855 	struct timespec ts;
1856 	struct timespec *timeout;
1857 	sigset_t set;
1858 	siginfo_t info;
1859 	int error;
1860 
1861 	if (uap->timeout) {
1862 		error = copyin(uap->timeout, &ts, sizeof(ts));
1863 		if (error)
1864 			return (error);
1865 		timeout = &ts;
1866 	} else {
1867 		timeout = NULL;
1868 	}
1869 	error = copyin(uap->set, &set, sizeof(set));
1870 	if (error)
1871 		return (error);
1872 	error = kern_sigtimedwait(set, &info, timeout);
1873 	if (error)
1874 		return (error);
1875 	if (uap->info)
1876 		error = copyout(&info, uap->info, sizeof(info));
1877 	/* Repost if we got an error. */
1878 	/*
1879 	 * XXX lwp
1880 	 *
1881 	 * This could transform a thread-specific signal to another
1882 	 * thread / process pending signal.
1883 	 */
1884 	if (error) {
1885 		ksignal(curproc, info.si_signo);
1886 	} else {
1887 		sysmsg->sysmsg_result = info.si_signo;
1888 	}
1889 	return (error);
1890 }
1891 
1892 /*
1893  * MPALMOSTSAFE
1894  */
1895 int
1896 sys_sigwaitinfo(struct sysmsg *sysmsg, const struct sigwaitinfo_args *uap)
1897 {
1898 	siginfo_t info;
1899 	sigset_t set;
1900 	int error;
1901 
1902 	error = copyin(uap->set, &set, sizeof(set));
1903 	if (error)
1904 		return (error);
1905 	error = kern_sigtimedwait(set, &info, NULL);
1906 	if (error)
1907 		return (error);
1908 	if (uap->info)
1909 		error = copyout(&info, uap->info, sizeof(info));
1910 	/* Repost if we got an error. */
1911 	/*
1912 	 * XXX lwp
1913 	 *
1914 	 * This could transform a thread-specific signal to another
1915 	 * thread / process pending signal.
1916 	 */
1917 	if (error) {
1918 		ksignal(curproc, info.si_signo);
1919 	} else {
1920 		sysmsg->sysmsg_result = info.si_signo;
1921 	}
1922 	return (error);
1923 }
1924 
1925 /*
1926  * If the current process has received a signal that would interrupt a
1927  * system call, return EINTR or ERESTART as appropriate.
1928  */
1929 int
1930 iscaught(struct lwp *lp)
1931 {
1932 	struct proc *p = lp->lwp_proc;
1933 	int sig;
1934 
1935 	if (p) {
1936 		if ((sig = CURSIG(lp)) != 0) {
1937 			if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
1938 				return (EINTR);
1939 			return (ERESTART);
1940 		}
1941 	}
1942 	return(EWOULDBLOCK);
1943 }
1944 
1945 /*
1946  * If the current lwp/proc has received a signal (should be caught or cause
1947  * termination, should interrupt current syscall), return the signal number.
1948  * Stop signals with default action are processed immediately, then cleared;
1949  * they aren't returned.  This is checked after each entry to the system for
1950  * a syscall or trap (though this can usually be done without calling issignal
1951  * by checking the pending signal masks in the CURSIG macro).
1952  *
1953  * This routine is called via CURSIG/__cursig.  We will acquire and release
1954  * p->p_token but if the caller needs to interlock the test the caller must
1955  * also hold p->p_token.
1956  *
1957  *	while (sig = CURSIG(curproc))
1958  *		postsig(sig);
1959  */
1960 int
1961 issignal(struct lwp *lp, int maytrace, int *ptokp)
1962 {
1963 	struct proc *p = lp->lwp_proc;
1964 	sigset_t mask;
1965 	int sig, prop;
1966 	int haveptok;
1967 
1968 	for (;;) {
1969 		int traced = (p->p_flags & P_TRACED) || (p->p_stops & S_SIG);
1970 
1971 		haveptok = 0;
1972 
1973 		/*
1974 		 * NOTE: Do not tstop here.  Issue the proc_stop()
1975 		 *	 so other parties see that we know we need
1976 		 *	 to stop, but don't block here.  Locks might
1977 		 *	 be held.
1978 		 *
1979 		 * XXX If this process is supposed to stop, stop this thread.
1980 		 *     removed.
1981 		 */
1982 #if 0
1983 		if (STOPLWP(p, lp)) {
1984 			lwkt_gettoken(&p->p_token);
1985 			tstop();
1986 			lwkt_reltoken(&p->p_token);
1987 		}
1988 #endif
1989 
1990 		/*
1991 		 * Quick check without token
1992 		 */
1993 		mask = lwp_sigpend(lp);
1994 		SIGSETNAND(mask, lp->lwp_sigmask);
1995 		if (p->p_flags & P_PPWAIT)
1996 			SIG_STOPSIGMASK(mask);
1997 		SIG_CONDBLOCKALLSIGS(mask, lp);
1998 
1999 		if (SIGISEMPTY(mask)) 		/* no signal to send */
2000 			return (0);
2001 
2002 		/*
2003 		 * If the signal is a member of the process signal set
2004 		 * we need p_token (even if it is also a member of the
2005 		 * lwp signal set).
2006 		 */
2007 		sig = sig_ffs(&mask);
2008 		if (SIGISMEMBER(p->p_siglist, sig)) {
2009 			/*
2010 			 * Recheck with token
2011 			 */
2012 			haveptok = 1;
2013 			lwkt_gettoken(&p->p_token);
2014 
2015 			mask = lwp_sigpend(lp);
2016 			SIGSETNAND(mask, lp->lwp_sigmask);
2017 			if (p->p_flags & P_PPWAIT)
2018 				SIG_STOPSIGMASK(mask);
2019 			if (SIGISEMPTY(mask)) {		/* no signal to send */
2020 				/* haveptok is TRUE */
2021 				lwkt_reltoken(&p->p_token);
2022 				return (0);
2023 			}
2024 			sig = sig_ffs(&mask);
2025 		}
2026 
2027 		STOPEVENT(p, S_SIG, sig);
2028 
2029 		/*
2030 		 * We should see pending but ignored signals
2031 		 * only if P_TRACED was on when they were posted.
2032 		 */
2033 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
2034 			spin_lock(&lp->lwp_spin);
2035 			lwp_delsig(lp, sig, haveptok);
2036 			spin_unlock(&lp->lwp_spin);
2037 			if (haveptok)
2038 				lwkt_reltoken(&p->p_token);
2039 			continue;
2040 		}
2041 		if (maytrace &&
2042 		    (p->p_flags & P_TRACED) &&
2043 		    (p->p_flags & P_PPWAIT) == 0) {
2044 			/*
2045 			 * If traced, always stop, and stay stopped until
2046 			 * released by the parent.
2047 			 *
2048 			 * NOTE: SSTOP may get cleared during the loop, but
2049 			 *	 we do not re-notify the parent if we have
2050 			 *	 to loop several times waiting for the parent
2051 			 *	 to let us continue.  XXX not sure if this is
2052 			 * 	 still true
2053 			 *
2054 			 * NOTE: Do not tstop here.  Issue the proc_stop()
2055 			 *	 so other parties see that we know we need
2056 			 *	 to stop, but don't block here.  Locks might
2057 			 *	 be held.
2058 			 */
2059 			if (haveptok == 0) {
2060 				lwkt_gettoken(&p->p_token);
2061 				haveptok = 1;
2062 			}
2063 			p->p_xstat = sig;
2064 			proc_stop(p, SSTOP);
2065 
2066 			/*
2067 			 * Normally we don't stop until we return to userland, but
2068 			 * make an exception when tracing and 'maytrace' is asserted.
2069 			 */
2070 			if (p->p_flags & P_TRACED)
2071 				tstop();
2072 
2073 			/*
2074 			 * If parent wants us to take the signal,
2075 			 * then it will leave it in p->p_xstat;
2076 			 * otherwise we just look for signals again.
2077 			 */
2078 			spin_lock(&lp->lwp_spin);
2079 			lwp_delsig(lp, sig, 1);	/* clear old signal */
2080 			spin_unlock(&lp->lwp_spin);
2081 			sig = p->p_xstat;
2082 			if (sig == 0) {
2083 				/* haveptok is TRUE */
2084 				lwkt_reltoken(&p->p_token);
2085 				continue;
2086 			}
2087 
2088 			/*
2089 			 * Put the new signal into p_siglist.  If the
2090 			 * signal is being masked, look for other signals.
2091 			 *
2092 			 * XXX lwp might need a call to ksignal()
2093 			 */
2094 			SIGADDSET_ATOMIC(p->p_siglist, sig);
2095 			if (SIGISMEMBER(lp->lwp_sigmask, sig)) {
2096 				/* haveptok is TRUE */
2097 				lwkt_reltoken(&p->p_token);
2098 				continue;
2099 			}
2100 
2101 			/*
2102 			 * If the traced bit got turned off, go back up
2103 			 * to the top to rescan signals.  This ensures
2104 			 * that p_sig* and ps_sigact are consistent.
2105 			 */
2106 			if ((p->p_flags & P_TRACED) == 0) {
2107 				/* haveptok is TRUE */
2108 				lwkt_reltoken(&p->p_token);
2109 				continue;
2110 			}
2111 		}
2112 
2113 		/*
2114 		 * p_token may be held here
2115 		 */
2116 		prop = sigprop(sig);
2117 
2118 		/*
2119 		 * Decide whether the signal should be returned.
2120 		 * Return the signal's number, or fall through
2121 		 * to clear it from the pending mask.
2122 		 */
2123 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2124 		case (intptr_t)SIG_DFL:
2125 			/*
2126 			 * Don't take default actions on system processes.
2127 			 */
2128 			if (p->p_pid <= 1) {
2129 #ifdef DIAGNOSTIC
2130 				/*
2131 				 * Are you sure you want to ignore SIGSEGV
2132 				 * in init? XXX
2133 				 */
2134 				kprintf("Process (pid %lu) got signal %d\n",
2135 					(u_long)p->p_pid, sig);
2136 #endif
2137 				break;		/* == ignore */
2138 			}
2139 
2140 			/*
2141 			 * Handle the in-kernel checkpoint action
2142 			 */
2143 			if (prop & SA_CKPT) {
2144 				if (haveptok == 0) {
2145 					lwkt_gettoken(&p->p_token);
2146 					haveptok = 1;
2147 				}
2148 				checkpoint_signal_handler(lp);
2149 				break;
2150 			}
2151 
2152 			/*
2153 			 * If there is a pending stop signal to process
2154 			 * with default action, stop here,
2155 			 * then clear the signal.  However,
2156 			 * if process is member of an orphaned
2157 			 * process group, ignore tty stop signals.
2158 			 */
2159 			if (prop & SA_STOP) {
2160 				if (haveptok == 0) {
2161 					lwkt_gettoken(&p->p_token);
2162 					haveptok = 1;
2163 				}
2164 				if (p->p_flags & P_TRACED ||
2165 		    		    (p->p_pgrp->pg_jobc == 0 &&
2166 				    prop & SA_TTYSTOP))
2167 					break;	/* == ignore */
2168 				if ((p->p_flags & P_WEXIT) == 0) {
2169 					/*
2170 					 * NOTE: We do not block here.  Issue
2171 					 *	 stopthe stop so other parties
2172 					 *	 see that we know we need to
2173 					 *	 stop.  Locks might be held.
2174 					 */
2175 					p->p_xstat = sig;
2176 					proc_stop(p, SSTOP);
2177 
2178 #if 0
2179 					tstop();
2180 #endif
2181 				}
2182 				break;
2183 			} else if (prop & SA_IGNORE) {
2184 				/*
2185 				 * Except for SIGCONT, shouldn't get here.
2186 				 * Default action is to ignore; drop it.
2187 				 */
2188 				break;		/* == ignore */
2189 			} else {
2190 				if (ptokp)
2191 					*ptokp = haveptok;
2192 				else if (haveptok)
2193 					lwkt_reltoken(&p->p_token);
2194 				return (sig);
2195 			}
2196 
2197 			/*NOTREACHED*/
2198 
2199 		case (intptr_t)SIG_IGN:
2200 			/*
2201 			 * Masking above should prevent us ever trying
2202 			 * to take action on an ignored signal other
2203 			 * than SIGCONT, unless process is traced.
2204 			 */
2205 			if ((prop & SA_CONT) == 0 &&
2206 			    (p->p_flags & P_TRACED) == 0)
2207 				kprintf("issignal\n");
2208 			break;		/* == ignore */
2209 
2210 		default:
2211 			/*
2212 			 * This signal has an action, let
2213 			 * postsig() process it.
2214 			 */
2215 			if (ptokp)
2216 				*ptokp = haveptok;
2217 			else if (haveptok)
2218 				lwkt_reltoken(&p->p_token);
2219 			return (sig);
2220 		}
2221 		spin_lock(&lp->lwp_spin);
2222 		lwp_delsig(lp, sig, haveptok);		/* take the signal! */
2223 		spin_unlock(&lp->lwp_spin);
2224 
2225 		if (haveptok)
2226 			lwkt_reltoken(&p->p_token);
2227 	}
2228 	/* NOTREACHED */
2229 }
2230 
2231 /*
2232  * Take the action for the specified signal from the current set of
2233  * pending signals.
2234  *
2235  * haveptok indicates whether the caller is holding p->p_token.  If the
2236  * caller is, we are responsible for releasing it.
2237  *
2238  * This routine can only be called from the top-level trap from usermode.
2239  * It is expecting to be able to modify the top-level stack frame.
2240  */
2241 void
2242 postsig(int sig, int haveptok)
2243 {
2244 	struct lwp *lp = curthread->td_lwp;
2245 	struct proc *p = lp->lwp_proc;
2246 	struct sigacts *ps = p->p_sigacts;
2247 	sig_t action;
2248 	sigset_t returnmask;
2249 	int code;
2250 
2251 	KASSERT(sig != 0, ("postsig"));
2252 
2253 	/*
2254 	 * If we are a virtual kernel running an emulated user process
2255 	 * context, switch back to the virtual kernel context before
2256 	 * trying to post the signal.
2257 	 */
2258 	if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
2259 		struct trapframe *tf = lp->lwp_md.md_regs;
2260 		tf->tf_trapno = 0;
2261 		vkernel_trap(lp, tf);
2262 	}
2263 
2264 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
2265 
2266 	spin_lock(&lp->lwp_spin);
2267 	lwp_delsig(lp, sig, haveptok);
2268 	spin_unlock(&lp->lwp_spin);
2269 	action = ps->ps_sigact[_SIG_IDX(sig)];
2270 #ifdef KTRACE
2271 	if (KTRPOINT(lp->lwp_thread, KTR_PSIG))
2272 		ktrpsig(lp, sig, action, lp->lwp_flags & LWP_OLDMASK ?
2273 			&lp->lwp_oldsigmask : &lp->lwp_sigmask, 0);
2274 #endif
2275 	/*
2276 	 * We don't need p_token after this point.
2277 	 */
2278 	if (haveptok)
2279 		lwkt_reltoken(&p->p_token);
2280 
2281 	STOPEVENT(p, S_SIG, sig);
2282 
2283 	if (action == SIG_DFL) {
2284 		/*
2285 		 * Default action, where the default is to kill
2286 		 * the process.  (Other cases were ignored above.)
2287 		 */
2288 		sigexit(lp, sig);
2289 		/* NOTREACHED */
2290 	} else {
2291 		/*
2292 		 * If we get here, the signal must be caught.
2293 		 */
2294 		KASSERT(action != SIG_IGN && !SIGISMEMBER(lp->lwp_sigmask, sig),
2295 		    ("postsig action"));
2296 
2297 		/*
2298 		 * Reset the signal handler if asked to
2299 		 */
2300 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2301 			/*
2302 			 * See kern_sigaction() for origin of this code.
2303 			 */
2304 			SIGDELSET(p->p_sigcatch, sig);
2305 			if (sig != SIGCONT &&
2306 			    sigprop(sig) & SA_IGNORE)
2307 				SIGADDSET(p->p_sigignore, sig);
2308 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2309 		}
2310 
2311 		/*
2312 		 * Set the signal mask and calculate the mask to restore
2313 		 * when the signal function returns.
2314 		 *
2315 		 * Special case: user has done a sigsuspend.  Here the
2316 		 * current mask is not of interest, but rather the
2317 		 * mask from before the sigsuspend is what we want
2318 		 * restored after the signal processing is completed.
2319 		 */
2320 		if (lp->lwp_flags & LWP_OLDMASK) {
2321 			returnmask = lp->lwp_oldsigmask;
2322 			lp->lwp_flags &= ~LWP_OLDMASK;
2323 		} else {
2324 			returnmask = lp->lwp_sigmask;
2325 		}
2326 
2327 		SIGSETOR(lp->lwp_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2328 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
2329 			SIGADDSET(lp->lwp_sigmask, sig);
2330 
2331 		lp->lwp_ru.ru_nsignals++;
2332 		if (lp->lwp_sig != sig) {
2333 			code = 0;
2334 		} else {
2335 			code = lp->lwp_code;
2336 			lp->lwp_code = 0;
2337 			lp->lwp_sig = 0;
2338 		}
2339 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
2340 	}
2341 }
2342 
2343 /*
2344  * Kill the current process for stated reason.
2345  */
2346 void
2347 killproc(struct proc *p, char *why)
2348 {
2349 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n",
2350 		p->p_pid, p->p_comm,
2351 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2352 	ksignal(p, SIGKILL);
2353 }
2354 
2355 /*
2356  * Force the current process to exit with the specified signal, dumping core
2357  * if appropriate.  We bypass the normal tests for masked and caught signals,
2358  * allowing unrecoverable failures to terminate the process without changing
2359  * signal state.  Mark the accounting record with the signal termination.
2360  * If dumping core, save the signal number for the debugger.  Calls exit and
2361  * does not return.
2362  *
2363  * This routine does not return.
2364  */
2365 void
2366 sigexit(struct lwp *lp, int sig)
2367 {
2368 	struct proc *p = lp->lwp_proc;
2369 
2370 	lwkt_gettoken(&p->p_token);
2371 	p->p_acflag |= AXSIG;
2372 	if (sigprop(sig) & SA_CORE) {
2373 		lp->lwp_sig = sig;
2374 
2375 		/*
2376 		 * All threads must be stopped before we can safely coredump.
2377 		 * Stop threads using SCORE, which cannot be overridden.
2378 		 */
2379 		if (p->p_stat != SCORE) {
2380 			proc_stop(p, SCORE);
2381 			proc_stopwait(p);
2382 
2383 			if (coredump(lp, sig) == 0)
2384 				sig |= WCOREFLAG;
2385 			p->p_stat = SSTOP;
2386 		}
2387 
2388 		/*
2389 		 * Log signals which would cause core dumps
2390 		 * (Log as LOG_INFO to appease those who don't want
2391 		 * these messages.)
2392 		 * XXX : Todo, as well as euid, write out ruid too
2393 		 */
2394 		if (kern_logsigexit) {
2395 			log(LOG_INFO,
2396 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
2397 			    p->p_pid, p->p_comm,
2398 			    p->p_ucred ? p->p_ucred->cr_uid : -1,
2399 			    sig &~ WCOREFLAG,
2400 			    sig & WCOREFLAG ? " (core dumped)" : "");
2401 			if (kern_logsigexit > 1)
2402 				kprintf("DEBUG - waiting on kern.logsigexit\n");
2403 			while (kern_logsigexit > 1) {
2404 				tsleep(&kern_logsigexit, 0, "DEBUG", hz);
2405 			}
2406 		}
2407 	}
2408 	lwkt_reltoken(&p->p_token);
2409 	exit1(W_EXITCODE(0, sig));
2410 	/* NOTREACHED */
2411 }
2412 
2413 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2414 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2415 	      sizeof(corefilename), "process corefile name format string");
2416 
2417 /*
2418  * expand_name(name, uid, pid)
2419  * Expand the name described in corefilename, using name, uid, and pid.
2420  * corefilename is a kprintf-like string, with three format specifiers:
2421  *	%N	name of process ("name")
2422  *	%P	process id (pid)
2423  *	%U	user id (uid)
2424  * For example, "%N.core" is the default; they can be disabled completely
2425  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2426  * This is controlled by the sysctl variable kern.corefile (see above).
2427  */
2428 
2429 static char *
2430 expand_name(const char *name, uid_t uid, pid_t pid)
2431 {
2432 	char *temp;
2433 	char buf[11];		/* Buffer for pid/uid -- max 4B */
2434 	int i, n;
2435 	char *format = corefilename;
2436 	size_t namelen;
2437 
2438 	temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
2439 	if (temp == NULL)
2440 		return NULL;
2441 	namelen = strlen(name);
2442 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2443 		int l;
2444 		switch (format[i]) {
2445 		case '%':	/* Format character */
2446 			i++;
2447 			switch (format[i]) {
2448 			case '%':
2449 				temp[n++] = '%';
2450 				break;
2451 			case 'N':	/* process name */
2452 				if ((n + namelen) > MAXPATHLEN) {
2453 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
2454 					    pid, name, uid, temp, name);
2455 					kfree(temp, M_TEMP);
2456 					return NULL;
2457 				}
2458 				memcpy(temp+n, name, namelen);
2459 				n += namelen;
2460 				break;
2461 			case 'P':	/* process id */
2462 				l = ksprintf(buf, "%u", pid);
2463 				if ((n + l) > MAXPATHLEN) {
2464 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
2465 					    pid, name, uid, temp, name);
2466 					kfree(temp, M_TEMP);
2467 					return NULL;
2468 				}
2469 				memcpy(temp+n, buf, l);
2470 				n += l;
2471 				break;
2472 			case 'U':	/* user id */
2473 				l = ksprintf(buf, "%u", uid);
2474 				if ((n + l) > MAXPATHLEN) {
2475 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
2476 					    pid, name, uid, temp, name);
2477 					kfree(temp, M_TEMP);
2478 					return NULL;
2479 				}
2480 				memcpy(temp+n, buf, l);
2481 				n += l;
2482 				break;
2483 			default:
2484 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
2485 			}
2486 			break;
2487 		default:
2488 			temp[n++] = format[i];
2489 		}
2490 	}
2491 	temp[n] = '\0';
2492 	return temp;
2493 }
2494 
2495 /*
2496  * Dump a process' core.  The main routine does some
2497  * policy checking, and creates the name of the coredump;
2498  * then it passes on a vnode and a size limit to the process-specific
2499  * coredump routine if there is one; if there _is not_ one, it returns
2500  * ENOSYS; otherwise it returns the error from the process-specific routine.
2501  *
2502  * The parameter `lp' is the lwp which triggered the coredump.
2503  */
2504 
2505 static int
2506 coredump(struct lwp *lp, int sig)
2507 {
2508 	struct proc *p = lp->lwp_proc;
2509 	struct vnode *vp;
2510 	struct ucred *cred = p->p_ucred;
2511 	struct flock lf;
2512 	struct nlookupdata nd;
2513 	struct vattr vattr;
2514 	int error, error1;
2515 	char *name;			/* name of corefile */
2516 	off_t limit;
2517 
2518 	STOPEVENT(p, S_CORE, 0);
2519 
2520 	if (((sugid_coredump == 0) && p->p_flags & P_SUGID) || do_coredump == 0)
2521 		return (EFAULT);
2522 
2523 	/*
2524 	 * Note that the bulk of limit checking is done after
2525 	 * the corefile is created.  The exception is if the limit
2526 	 * for corefiles is 0, in which case we don't bother
2527 	 * creating the corefile at all.  This layout means that
2528 	 * a corefile is truncated instead of not being created,
2529 	 * if it is larger than the limit.
2530 	 */
2531 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2532 	if (limit == 0)
2533 		return EFBIG;
2534 
2535 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
2536 	if (name == NULL)
2537 		return (EINVAL);
2538 	error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP);
2539 	if (error == 0)
2540 		error = vn_open(&nd, NULL,
2541 				O_CREAT | FWRITE | O_NOFOLLOW,
2542 				S_IRUSR | S_IWUSR);
2543 	kfree(name, M_TEMP);
2544 	if (error) {
2545 		nlookup_done(&nd);
2546 		return (error);
2547 	}
2548 	vp = nd.nl_open_vp;
2549 	nd.nl_open_vp = NULL;
2550 	nlookup_done(&nd);
2551 
2552 	vn_unlock(vp);
2553 	lf.l_whence = SEEK_SET;
2554 	lf.l_start = 0;
2555 	lf.l_len = 0;
2556 	lf.l_type = F_WRLCK;
2557 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, 0);
2558 	if (error)
2559 		goto out2;
2560 
2561 	/* Don't dump to non-regular files or files with links. */
2562 	if (vp->v_type != VREG ||
2563 	    VOP_GETATTR(vp, &vattr) || vattr.va_nlink != 1) {
2564 		error = EFAULT;
2565 		goto out1;
2566 	}
2567 
2568 	/* Don't dump to files current user does not own */
2569 	if (vattr.va_uid != p->p_ucred->cr_uid) {
2570 		error = EFAULT;
2571 		goto out1;
2572 	}
2573 
2574 	VATTR_NULL(&vattr);
2575 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2576 	vattr.va_size = 0;
2577 	VOP_SETATTR(vp, &vattr, cred);
2578 	p->p_acflag |= ACORE;
2579 	vn_unlock(vp);
2580 
2581 	error = p->p_sysent->sv_coredump ?
2582 		  p->p_sysent->sv_coredump(lp, sig, vp, limit) : ENOSYS;
2583 
2584 out1:
2585 	lf.l_type = F_UNLCK;
2586 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, 0);
2587 out2:
2588 	error1 = vn_close(vp, FWRITE, NULL);
2589 	if (error == 0)
2590 		error = error1;
2591 	return (error);
2592 }
2593 
2594 /*
2595  * Nonexistent system call-- signal process (may want to handle it).
2596  * Flag error in case process won't see signal immediately (blocked or ignored).
2597  *
2598  * MPALMOSTSAFE
2599  */
2600 /* ARGSUSED */
2601 int
2602 sys_nosys(struct sysmsg *sysmsg, const struct nosys_args *args)
2603 {
2604 	lwpsignal(curproc, curthread->td_lwp, SIGSYS);
2605 	return (EINVAL);
2606 }
2607 
2608 /*
2609  * Send a SIGIO or SIGURG signal to a process or process group using
2610  * stored credentials rather than those of the current process.
2611  */
2612 void
2613 pgsigio(struct sigio *sigio, int sig, int checkctty)
2614 {
2615 	if (sigio == NULL)
2616 		return;
2617 
2618 	if (sigio->sio_pgid > 0) {
2619 		if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
2620 			     sigio->sio_proc))
2621 			ksignal(sigio->sio_proc, sig);
2622 	} else if (sigio->sio_pgid < 0) {
2623 		struct proc *p;
2624 		struct pgrp *pg = sigio->sio_pgrp;
2625 
2626 		/*
2627 		 * Must interlock all signals against fork
2628 		 */
2629 		pgref(pg);
2630 		lockmgr(&pg->pg_lock, LK_EXCLUSIVE);
2631 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
2632 			if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
2633 			    (checkctty == 0 || (p->p_flags & P_CONTROLT)))
2634 				ksignal(p, sig);
2635 		}
2636 		lockmgr(&pg->pg_lock, LK_RELEASE);
2637 		pgrel(pg);
2638 	}
2639 }
2640 
2641 static int
2642 filt_sigattach(struct knote *kn)
2643 {
2644 	struct proc *p = curproc;
2645 
2646 	kn->kn_ptr.p_proc = p;
2647 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2648 
2649 	/* XXX lock the proc here while adding to the list? */
2650 	knote_insert(&p->p_klist, kn);
2651 
2652 	return (0);
2653 }
2654 
2655 static void
2656 filt_sigdetach(struct knote *kn)
2657 {
2658 	struct proc *p = kn->kn_ptr.p_proc;
2659 
2660 	knote_remove(&p->p_klist, kn);
2661 }
2662 
2663 /*
2664  * signal knotes are shared with proc knotes, so we apply a mask to
2665  * the hint in order to differentiate them from process hints.  This
2666  * could be avoided by using a signal-specific knote list, but probably
2667  * isn't worth the trouble.
2668  */
2669 static int
2670 filt_signal(struct knote *kn, long hint)
2671 {
2672 	if (hint & NOTE_SIGNAL) {
2673 		hint &= ~NOTE_SIGNAL;
2674 
2675 		if (kn->kn_id == hint)
2676 			kn->kn_data++;
2677 	}
2678 	return (kn->kn_data != 0);
2679 }
2680