xref: /netbsd-src/sys/kern/kern_sig.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: kern_sig.c,v 1.143 2003/06/29 22:31:22 fvdl Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.143 2003/06/29 22:31:22 fvdl Exp $");
45 
46 #include "opt_ktrace.h"
47 #include "opt_compat_sunos.h"
48 #include "opt_compat_netbsd32.h"
49 
50 #define	SIGPROP		/* include signal properties table */
51 #include <sys/param.h>
52 #include <sys/signalvar.h>
53 #include <sys/resourcevar.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/proc.h>
57 #include <sys/systm.h>
58 #include <sys/timeb.h>
59 #include <sys/times.h>
60 #include <sys/buf.h>
61 #include <sys/acct.h>
62 #include <sys/file.h>
63 #include <sys/kernel.h>
64 #include <sys/wait.h>
65 #include <sys/ktrace.h>
66 #include <sys/syslog.h>
67 #include <sys/stat.h>
68 #include <sys/core.h>
69 #include <sys/filedesc.h>
70 #include <sys/malloc.h>
71 #include <sys/pool.h>
72 #include <sys/ucontext.h>
73 #include <sys/sa.h>
74 #include <sys/savar.h>
75 #include <sys/exec.h>
76 
77 #include <sys/mount.h>
78 #include <sys/syscallargs.h>
79 
80 #include <machine/cpu.h>
81 
82 #include <sys/user.h>		/* for coredump */
83 
84 #include <uvm/uvm_extern.h>
85 
86 static void	proc_stop(struct proc *p);
87 static int	build_corename(struct proc *, char [MAXPATHLEN]);
88 sigset_t	contsigmask, stopsigmask, sigcantmask;
89 
90 struct pool	sigacts_pool;	/* memory pool for sigacts structures */
91 struct pool	siginfo_pool;	/* memory pool for siginfo structures */
92 
93 /*
94  * Can process p, with pcred pc, send the signal signum to process q?
95  */
96 #define	CANSIGNAL(p, pc, q, signum) \
97 	((pc)->pc_ucred->cr_uid == 0 || \
98 	    (pc)->p_ruid == (q)->p_cred->p_ruid || \
99 	    (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
100 	    (pc)->p_ruid == (q)->p_ucred->cr_uid || \
101 	    (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
102 	    ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
103 
104 /*
105  * Initialize signal-related data structures.
106  */
107 void
108 signal_init(void)
109 {
110 
111 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
112 	    &pool_allocator_nointr);
113 	pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
114 	    &pool_allocator_nointr);
115 }
116 
117 /*
118  * Create an initial sigctx structure, using the same signal state
119  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
120  * copy it from parent.
121  */
122 void
123 sigactsinit(struct proc *np, struct proc *pp, int share)
124 {
125 	struct sigacts *ps;
126 
127 	if (share) {
128 		np->p_sigacts = pp->p_sigacts;
129 		pp->p_sigacts->sa_refcnt++;
130 	} else {
131 		ps = pool_get(&sigacts_pool, PR_WAITOK);
132 		if (pp)
133 			memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
134 		else
135 			memset(ps, '\0', sizeof(struct sigacts));
136 		ps->sa_refcnt = 1;
137 		np->p_sigacts = ps;
138 	}
139 }
140 
141 /*
142  * Make this process not share its sigctx, maintaining all
143  * signal state.
144  */
145 void
146 sigactsunshare(struct proc *p)
147 {
148 	struct sigacts *oldps;
149 
150 	if (p->p_sigacts->sa_refcnt == 1)
151 		return;
152 
153 	oldps = p->p_sigacts;
154 	sigactsinit(p, NULL, 0);
155 
156 	if (--oldps->sa_refcnt == 0)
157 		pool_put(&sigacts_pool, oldps);
158 }
159 
160 /*
161  * Release a sigctx structure.
162  */
163 void
164 sigactsfree(struct proc *p)
165 {
166 	struct sigacts *ps;
167 
168 	ps = p->p_sigacts;
169 	if (--ps->sa_refcnt > 0)
170 		return;
171 
172 	pool_put(&sigacts_pool, ps);
173 }
174 
175 int
176 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
177 	struct sigaction *osa, void *tramp, int vers)
178 {
179 	struct sigacts	*ps;
180 	int		prop;
181 
182 	ps = p->p_sigacts;
183 	if (signum <= 0 || signum >= NSIG)
184 		return (EINVAL);
185 
186 	/*
187 	 * Trampoline ABI version 0 is reserved for the legacy
188 	 * kernel-provided on-stack trampoline.  Conversely, if
189 	 * we are using a non-0 ABI version, we must have a
190 	 * trampoline.
191 	 */
192 	if ((vers != 0 && tramp == NULL) ||
193 	    (vers == 0 && tramp != NULL))
194 		return (EINVAL);
195 
196 	if (osa)
197 		*osa = SIGACTION_PS(ps, signum);
198 
199 	if (nsa) {
200 		if (nsa->sa_flags & ~SA_ALLBITS)
201 			return (EINVAL);
202 
203 		prop = sigprop[signum];
204 		if (prop & SA_CANTMASK)
205 			return (EINVAL);
206 
207 		(void) splsched();	/* XXXSMP */
208 		SIGACTION_PS(ps, signum) = *nsa;
209 		ps->sa_sigdesc[signum].sd_tramp = tramp;
210 		ps->sa_sigdesc[signum].sd_vers = vers;
211 		sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
212 		if ((prop & SA_NORESET) != 0)
213 			SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
214 		if (signum == SIGCHLD) {
215 			if (nsa->sa_flags & SA_NOCLDSTOP)
216 				p->p_flag |= P_NOCLDSTOP;
217 			else
218 				p->p_flag &= ~P_NOCLDSTOP;
219 			if (nsa->sa_flags & SA_NOCLDWAIT) {
220 				/*
221 				 * Paranoia: since SA_NOCLDWAIT is implemented
222 				 * by reparenting the dying child to PID 1 (and
223 				 * trust it to reap the zombie), PID 1 itself
224 				 * is forbidden to set SA_NOCLDWAIT.
225 				 */
226 				if (p->p_pid == 1)
227 					p->p_flag &= ~P_NOCLDWAIT;
228 				else
229 					p->p_flag |= P_NOCLDWAIT;
230 			} else
231 				p->p_flag &= ~P_NOCLDWAIT;
232 		}
233 		if ((nsa->sa_flags & SA_NODEFER) == 0)
234 			sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
235 		else
236 			sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
237 		/*
238 	 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
239 		 * SIG_IGN, and for signals set to SIG_DFL where the default is
240 		 * to ignore. However, don't put SIGCONT in
241 		 * p_sigctx.ps_sigignore, as we have to restart the process.
242 	 	 */
243 		if (nsa->sa_handler == SIG_IGN ||
244 		    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
245 						/* never to be seen again */
246 			sigdelset(&p->p_sigctx.ps_siglist, signum);
247 			if (signum != SIGCONT) {
248 						/* easier in psignal */
249 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
250 			}
251 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
252 		} else {
253 			sigdelset(&p->p_sigctx.ps_sigignore, signum);
254 			if (nsa->sa_handler == SIG_DFL)
255 				sigdelset(&p->p_sigctx.ps_sigcatch, signum);
256 			else
257 				sigaddset(&p->p_sigctx.ps_sigcatch, signum);
258 		}
259 		(void) spl0();
260 	}
261 
262 	return (0);
263 }
264 
265 /* ARGSUSED */
266 int
267 sys___sigaction14(struct lwp *l, void *v, register_t *retval)
268 {
269 	struct sys___sigaction14_args /* {
270 		syscallarg(int)				signum;
271 		syscallarg(const struct sigaction *)	nsa;
272 		syscallarg(struct sigaction *)		osa;
273 	} */ *uap = v;
274 	struct proc		*p;
275 	struct sigaction	nsa, osa;
276 	int			error;
277 
278 	if (SCARG(uap, nsa)) {
279 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
280 		if (error)
281 			return (error);
282 	}
283 	p = l->l_proc;
284 	error = sigaction1(p, SCARG(uap, signum),
285 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
286 	    NULL, 0);
287 	if (error)
288 		return (error);
289 	if (SCARG(uap, osa)) {
290 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
291 		if (error)
292 			return (error);
293 	}
294 	return (0);
295 }
296 
297 /* ARGSUSED */
298 int
299 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
300 {
301 	struct sys___sigaction_sigtramp_args /* {
302 		syscallarg(int)				signum;
303 		syscallarg(const struct sigaction *)	nsa;
304 		syscallarg(struct sigaction *)		osa;
305 		syscallarg(void *)			tramp;
306 		syscallarg(int)				vers;
307 	} */ *uap = v;
308 	struct proc *p = l->l_proc;
309 	struct sigaction nsa, osa;
310 	int error;
311 
312 	if (SCARG(uap, nsa)) {
313 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
314 		if (error)
315 			return (error);
316 	}
317 	error = sigaction1(p, SCARG(uap, signum),
318 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
319 	    SCARG(uap, tramp), SCARG(uap, vers));
320 	if (error)
321 		return (error);
322 	if (SCARG(uap, osa)) {
323 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
324 		if (error)
325 			return (error);
326 	}
327 	return (0);
328 }
329 
330 /*
331  * Initialize signal state for process 0;
332  * set to ignore signals that are ignored by default and disable the signal
333  * stack.
334  */
335 void
336 siginit(struct proc *p)
337 {
338 	struct sigacts	*ps;
339 	int		signum, prop;
340 
341 	ps = p->p_sigacts;
342 	sigemptyset(&contsigmask);
343 	sigemptyset(&stopsigmask);
344 	sigemptyset(&sigcantmask);
345 	for (signum = 1; signum < NSIG; signum++) {
346 		prop = sigprop[signum];
347 		if (prop & SA_CONT)
348 			sigaddset(&contsigmask, signum);
349 		if (prop & SA_STOP)
350 			sigaddset(&stopsigmask, signum);
351 		if (prop & SA_CANTMASK)
352 			sigaddset(&sigcantmask, signum);
353 		if (prop & SA_IGNORE && signum != SIGCONT)
354 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
355 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
356 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
357 	}
358 	sigemptyset(&p->p_sigctx.ps_sigcatch);
359 	p->p_sigctx.ps_sigwaited = 0;
360 	p->p_flag &= ~P_NOCLDSTOP;
361 
362 	/*
363 	 * Reset stack state to the user stack.
364 	 */
365 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
366 	p->p_sigctx.ps_sigstk.ss_size = 0;
367 	p->p_sigctx.ps_sigstk.ss_sp = 0;
368 
369 	/* One reference. */
370 	ps->sa_refcnt = 1;
371 }
372 
373 /*
374  * Reset signals for an exec of the specified process.
375  */
376 void
377 execsigs(struct proc *p)
378 {
379 	struct sigacts	*ps;
380 	int		signum, prop;
381 
382 	sigactsunshare(p);
383 
384 	ps = p->p_sigacts;
385 
386 	/*
387 	 * Reset caught signals.  Held signals remain held
388 	 * through p_sigctx.ps_sigmask (unless they were caught,
389 	 * and are now ignored by default).
390 	 */
391 	for (signum = 1; signum < NSIG; signum++) {
392 		if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
393 			prop = sigprop[signum];
394 			if (prop & SA_IGNORE) {
395 				if ((prop & SA_CONT) == 0)
396 					sigaddset(&p->p_sigctx.ps_sigignore,
397 					    signum);
398 				sigdelset(&p->p_sigctx.ps_siglist, signum);
399 			}
400 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
401 		}
402 		sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
403 		SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
404 	}
405 	sigemptyset(&p->p_sigctx.ps_sigcatch);
406 	p->p_sigctx.ps_sigwaited = 0;
407 	p->p_flag &= ~P_NOCLDSTOP;
408 
409 	/*
410 	 * Reset stack state to the user stack.
411 	 */
412 	p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
413 	p->p_sigctx.ps_sigstk.ss_size = 0;
414 	p->p_sigctx.ps_sigstk.ss_sp = 0;
415 }
416 
417 int
418 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
419 {
420 
421 	if (oss)
422 		*oss = p->p_sigctx.ps_sigmask;
423 
424 	if (nss) {
425 		(void)splsched();	/* XXXSMP */
426 		switch (how) {
427 		case SIG_BLOCK:
428 			sigplusset(nss, &p->p_sigctx.ps_sigmask);
429 			break;
430 		case SIG_UNBLOCK:
431 			sigminusset(nss, &p->p_sigctx.ps_sigmask);
432 			CHECKSIGS(p);
433 			break;
434 		case SIG_SETMASK:
435 			p->p_sigctx.ps_sigmask = *nss;
436 			CHECKSIGS(p);
437 			break;
438 		default:
439 			(void)spl0();	/* XXXSMP */
440 			return (EINVAL);
441 		}
442 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
443 		(void)spl0();		/* XXXSMP */
444 	}
445 
446 	return (0);
447 }
448 
449 /*
450  * Manipulate signal mask.
451  * Note that we receive new mask, not pointer,
452  * and return old mask as return value;
453  * the library stub does the rest.
454  */
455 int
456 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
457 {
458 	struct sys___sigprocmask14_args /* {
459 		syscallarg(int)			how;
460 		syscallarg(const sigset_t *)	set;
461 		syscallarg(sigset_t *)		oset;
462 	} */ *uap = v;
463 	struct proc	*p;
464 	sigset_t	nss, oss;
465 	int		error;
466 
467 	if (SCARG(uap, set)) {
468 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
469 		if (error)
470 			return (error);
471 	}
472 	p = l->l_proc;
473 	error = sigprocmask1(p, SCARG(uap, how),
474 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
475 	if (error)
476 		return (error);
477 	if (SCARG(uap, oset)) {
478 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
479 		if (error)
480 			return (error);
481 	}
482 	return (0);
483 }
484 
485 void
486 sigpending1(struct proc *p, sigset_t *ss)
487 {
488 
489 	*ss = p->p_sigctx.ps_siglist;
490 	sigminusset(&p->p_sigctx.ps_sigmask, ss);
491 }
492 
493 /* ARGSUSED */
494 int
495 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
496 {
497 	struct sys___sigpending14_args /* {
498 		syscallarg(sigset_t *)	set;
499 	} */ *uap = v;
500 	struct proc	*p;
501 	sigset_t	ss;
502 
503 	p = l->l_proc;
504 	sigpending1(p, &ss);
505 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
506 }
507 
508 int
509 sigsuspend1(struct proc *p, const sigset_t *ss)
510 {
511 	struct sigacts *ps;
512 
513 	ps = p->p_sigacts;
514 	if (ss) {
515 		/*
516 		 * When returning from sigpause, we want
517 		 * the old mask to be restored after the
518 		 * signal handler has finished.  Thus, we
519 		 * save it here and mark the sigctx structure
520 		 * to indicate this.
521 		 */
522 		p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
523 		p->p_sigctx.ps_flags |= SAS_OLDMASK;
524 		(void) splsched();	/* XXXSMP */
525 		p->p_sigctx.ps_sigmask = *ss;
526 		CHECKSIGS(p);
527 		sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
528 		(void) spl0();		/* XXXSMP */
529 	}
530 
531 	while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
532 		/* void */;
533 	/* always return EINTR rather than ERESTART... */
534 	return (EINTR);
535 }
536 
537 /*
538  * Suspend process until signal, providing mask to be set
539  * in the meantime.  Note nonstandard calling convention:
540  * libc stub passes mask, not pointer, to save a copyin.
541  */
542 /* ARGSUSED */
543 int
544 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
545 {
546 	struct sys___sigsuspend14_args /* {
547 		syscallarg(const sigset_t *)	set;
548 	} */ *uap = v;
549 	struct proc	*p;
550 	sigset_t	ss;
551 	int		error;
552 
553 	if (SCARG(uap, set)) {
554 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
555 		if (error)
556 			return (error);
557 	}
558 
559 	p = l->l_proc;
560 	return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
561 }
562 
563 int
564 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
565 	struct sigaltstack *oss)
566 {
567 
568 	if (oss)
569 		*oss = p->p_sigctx.ps_sigstk;
570 
571 	if (nss) {
572 		if (nss->ss_flags & ~SS_ALLBITS)
573 			return (EINVAL);
574 
575 		if (nss->ss_flags & SS_DISABLE) {
576 			if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
577 				return (EINVAL);
578 		} else {
579 			if (nss->ss_size < MINSIGSTKSZ)
580 				return (ENOMEM);
581 		}
582 		p->p_sigctx.ps_sigstk = *nss;
583 	}
584 
585 	return (0);
586 }
587 
588 /* ARGSUSED */
589 int
590 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
591 {
592 	struct sys___sigaltstack14_args /* {
593 		syscallarg(const struct sigaltstack *)	nss;
594 		syscallarg(struct sigaltstack *)	oss;
595 	} */ *uap = v;
596 	struct proc		*p;
597 	struct sigaltstack	nss, oss;
598 	int			error;
599 
600 	if (SCARG(uap, nss)) {
601 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
602 		if (error)
603 			return (error);
604 	}
605 	p = l->l_proc;
606 	error = sigaltstack1(p,
607 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
608 	if (error)
609 		return (error);
610 	if (SCARG(uap, oss)) {
611 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
612 		if (error)
613 			return (error);
614 	}
615 	return (0);
616 }
617 
618 /* ARGSUSED */
619 int
620 sys_kill(struct lwp *l, void *v, register_t *retval)
621 {
622 	struct sys_kill_args /* {
623 		syscallarg(int)	pid;
624 		syscallarg(int)	signum;
625 	} */ *uap = v;
626 	struct proc	*cp, *p;
627 	struct pcred	*pc;
628 
629 	cp = l->l_proc;
630 	pc = cp->p_cred;
631 	if ((u_int)SCARG(uap, signum) >= NSIG)
632 		return (EINVAL);
633 	if (SCARG(uap, pid) > 0) {
634 		/* kill single process */
635 		if ((p = pfind(SCARG(uap, pid))) == NULL)
636 			return (ESRCH);
637 		if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
638 			return (EPERM);
639 		if (SCARG(uap, signum))
640 			psignal(p, SCARG(uap, signum));
641 		return (0);
642 	}
643 	switch (SCARG(uap, pid)) {
644 	case -1:		/* broadcast signal */
645 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
646 	case 0:			/* signal own process group */
647 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
648 	default:		/* negative explicit process group */
649 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
650 	}
651 	/* NOTREACHED */
652 }
653 
654 /*
655  * Common code for kill process group/broadcast kill.
656  * cp is calling process.
657  */
658 int
659 killpg1(struct proc *cp, int signum, int pgid, int all)
660 {
661 	struct proc	*p;
662 	struct pcred	*pc;
663 	struct pgrp	*pgrp;
664 	int		nfound;
665 
666 	pc = cp->p_cred;
667 	nfound = 0;
668 	if (all) {
669 		/*
670 		 * broadcast
671 		 */
672 		proclist_lock_read();
673 		LIST_FOREACH(p, &allproc, p_list) {
674 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
675 			    p == cp || !CANSIGNAL(cp, pc, p, signum))
676 				continue;
677 			nfound++;
678 			if (signum)
679 				psignal(p, signum);
680 		}
681 		proclist_unlock_read();
682 	} else {
683 		if (pgid == 0)
684 			/*
685 			 * zero pgid means send to my process group.
686 			 */
687 			pgrp = cp->p_pgrp;
688 		else {
689 			pgrp = pgfind(pgid);
690 			if (pgrp == NULL)
691 				return (ESRCH);
692 		}
693 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
694 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
695 			    !CANSIGNAL(cp, pc, p, signum))
696 				continue;
697 			nfound++;
698 			if (signum && P_ZOMBIE(p) == 0)
699 				psignal(p, signum);
700 		}
701 	}
702 	return (nfound ? 0 : ESRCH);
703 }
704 
705 /*
706  * Send a signal to a process group.
707  */
708 void
709 gsignal(int pgid, int signum)
710 {
711 	struct pgrp *pgrp;
712 
713 	if (pgid && (pgrp = pgfind(pgid)))
714 		pgsignal(pgrp, signum, 0);
715 }
716 
717 /*
718  * Send a signal to a process group. If checktty is 1,
719  * limit to members which have a controlling terminal.
720  */
721 void
722 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
723 {
724 	struct proc *p;
725 
726 	if (pgrp)
727 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
728 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
729 				psignal(p, signum);
730 }
731 
732 /*
733  * Send a signal caused by a trap to the current process.
734  * If it will be caught immediately, deliver it with correct code.
735  * Otherwise, post it normally.
736  */
737 void
738 trapsignal(struct lwp *l, int signum, u_long code)
739 {
740 	struct proc	*p;
741 	struct sigacts	*ps;
742 
743 	p = l->l_proc;
744 	ps = p->p_sigacts;
745 	if ((p->p_flag & P_TRACED) == 0 &&
746 	    sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
747 	    !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
748 		p->p_stats->p_ru.ru_nsignals++;
749 #ifdef KTRACE
750 		if (KTRPOINT(p, KTR_PSIG))
751 			ktrpsig(p, signum,
752 			    SIGACTION_PS(ps, signum).sa_handler,
753 			    &p->p_sigctx.ps_sigmask, code);
754 #endif
755 		psendsig(l, signum, &p->p_sigctx.ps_sigmask, code);
756 		(void) splsched();	/* XXXSMP */
757 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
758 		    &p->p_sigctx.ps_sigmask);
759 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
760 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
761 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
762 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
763 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
764 		}
765 		(void) spl0();		/* XXXSMP */
766 	} else {
767 		p->p_sigctx.ps_code = code;	/* XXX for core dump/debugger */
768 		p->p_sigctx.ps_sig = signum;	/* XXX to verify code */
769 		p->p_sigctx.ps_lwp = l->l_lid;
770 		psignal(p, signum);
771 	}
772 }
773 
774 /*
775  * Send the signal to the process.  If the signal has an action, the action
776  * is usually performed by the target process rather than the caller; we add
777  * the signal to the set of pending signals for the process.
778  *
779  * Exceptions:
780  *   o When a stop signal is sent to a sleeping process that takes the
781  *     default action, the process is stopped without awakening it.
782  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
783  *     regardless of the signal action (eg, blocked or ignored).
784  *
785  * Other ignored signals are discarded immediately.
786  *
787  * XXXSMP: Invoked as psignal() or sched_psignal().
788  */
789 void
790 psignal1(struct proc *p, int signum,
791 	int dolock)		/* XXXSMP: works, but icky */
792 {
793 	struct lwp *l, *suspended;
794 	int	s = 0, prop, allsusp;
795 	sig_t	action;
796 
797 #ifdef DIAGNOSTIC
798 	if (signum <= 0 || signum >= NSIG)
799 		panic("psignal signal number");
800 
801 	/* XXXSMP: works, but icky */
802 	if (dolock)
803 		SCHED_ASSERT_UNLOCKED();
804 	else
805 		SCHED_ASSERT_LOCKED();
806 #endif
807 	/*
808 	 * Notify any interested parties in the signal.
809 	 */
810 	KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
811 
812 	prop = sigprop[signum];
813 
814 	/*
815 	 * If proc is traced, always give parent a chance.
816 	 */
817 	if (p->p_flag & P_TRACED)
818 		action = SIG_DFL;
819 	else {
820 		/*
821 		 * If the signal is being ignored,
822 		 * then we forget about it immediately.
823 		 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
824 		 * and if it is set to SIG_IGN,
825 		 * action will be SIG_DFL here.)
826 		 */
827 		if (sigismember(&p->p_sigctx.ps_sigignore, signum))
828 			return;
829 		if (sigismember(&p->p_sigctx.ps_sigmask, signum))
830 			action = SIG_HOLD;
831 		else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
832 			action = SIG_CATCH;
833 		else {
834 			action = SIG_DFL;
835 
836 			if (prop & SA_KILL && p->p_nice > NZERO)
837 				p->p_nice = NZERO;
838 
839 			/*
840 			 * If sending a tty stop signal to a member of an
841 			 * orphaned process group, discard the signal here if
842 			 * the action is default; don't stop the process below
843 			 * if sleeping, and don't clear any pending SIGCONT.
844 			 */
845 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
846 				return;
847 		}
848 	}
849 
850 	if (prop & SA_CONT)
851 		sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
852 
853 	if (prop & SA_STOP)
854 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
855 
856 	sigaddset(&p->p_sigctx.ps_siglist, signum);
857 
858 	/* CHECKSIGS() is "inlined" here. */
859 	p->p_sigctx.ps_sigcheck = 1;
860 
861 	/*
862 	 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
863 	 * please!), check if anything waits on it. If yes, clear the
864 	 * pending signal from siglist set, save it to ps_sigwaited,
865 	 * clear sigwait list, and wakeup any sigwaiters.
866 	 * The signal won't be processed further here.
867 	 */
868 	if ((prop & SA_CANTMASK) == 0
869 	    && p->p_sigctx.ps_sigwaited < 0
870 	    && sigismember(&p->p_sigctx.ps_sigwait, signum)) {
871 		sigdelset(&p->p_sigctx.ps_siglist, signum);
872 		p->p_sigctx.ps_sigwaited = signum;
873 		sigemptyset(&p->p_sigctx.ps_sigwait);
874 
875 		if (dolock)
876 			wakeup_one(&p->p_sigctx.ps_sigwait);
877 		else
878 			sched_wakeup(&p->p_sigctx.ps_sigwait);
879 		return;
880 	}
881 
882 	/*
883 	 * Defer further processing for signals which are held,
884 	 * except that stopped processes must be continued by SIGCONT.
885 	 */
886 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
887 		return;
888 	/* XXXSMP: works, but icky */
889 	if (dolock)
890 		SCHED_LOCK(s);
891 
892 	if (p->p_nrlwps > 0) {
893 		/*
894 		 * At least one LWP is running or on a run queue.
895 		 * The signal will be noticed when one of them returns
896 		 * to userspace.
897 		 */
898 		signotify(p);
899 		/*
900 		 * The signal will be noticed very soon.
901 		 */
902 		goto out;
903 	} else {
904 		/* Process is sleeping or stopped */
905 		if (p->p_flag & P_SA) {
906 			l = p->p_sa->sa_idle;
907 		} else {
908 			/*
909 			 * Find out if any of the sleeps are interruptable,
910 			 * and if all the live LWPs remaining are suspended.
911 			 */
912 			allsusp = 1;
913 			LIST_FOREACH(l, &p->p_lwps, l_sibling) {
914 				if (l->l_stat == LSSLEEP &&
915 				    l->l_flag & L_SINTR)
916 					break;
917 				if (l->l_stat == LSSUSPENDED)
918 					suspended = l;
919 				else if ((l->l_stat != LSZOMB) &&
920 				         (l->l_stat != LSDEAD))
921 					allsusp = 0;
922 			}
923 		}
924 		if (p->p_stat == SACTIVE) {
925 			/* All LWPs must be sleeping */
926 			KDASSERT(((p->p_flag & P_SA) == 0) || (l != NULL));
927 
928 			if (l != NULL && (p->p_flag & P_TRACED))
929 				goto run;
930 
931 			/*
932 			 * If SIGCONT is default (or ignored) and process is
933 			 * asleep, we are finished; the process should not
934 			 * be awakened.
935 			 */
936 			if ((prop & SA_CONT) && action == SIG_DFL) {
937 				sigdelset(&p->p_sigctx.ps_siglist, signum);
938 				goto out;
939 			}
940 
941 			/*
942 			 * When a sleeping process receives a stop
943 			 * signal, process immediately if possible.
944 			 */
945 			if ((prop & SA_STOP) && action == SIG_DFL) {
946 				/*
947 				 * If a child holding parent blocked,
948 				 * stopping could cause deadlock.
949 				 */
950 				if (p->p_flag & P_PPWAIT)
951 					goto out;
952 				sigdelset(&p->p_sigctx.ps_siglist, signum);
953 				p->p_xstat = signum;
954 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
955 					/*
956 					 * XXXSMP: recursive call; don't lock
957 					 * the second time around.
958 					 */
959 					sched_psignal(p->p_pptr, SIGCHLD);
960 				}
961 				proc_stop(p);	/* XXXSMP: recurse? */
962 				goto out;
963 			}
964 
965 			if (l == NULL) {
966 				/*
967 				 * Special case: SIGKILL of a process
968 				 * which is entirely composed of
969 				 * suspended LWPs should succeed. We
970 				 * make this happen by unsuspending one of
971 				 * them.
972 				 */
973 				if (allsusp && (signum == SIGKILL))
974 					lwp_continue(suspended);
975 				goto out;
976 			}
977 			/*
978 			 * All other (caught or default) signals
979 			 * cause the process to run.
980 			 */
981 			goto runfast;
982 			/*NOTREACHED*/
983 		} else if (p->p_stat == SSTOP) {
984 			/* Process is stopped */
985 			/*
986 			 * If traced process is already stopped,
987 			 * then no further action is necessary.
988 			 */
989 			if (p->p_flag & P_TRACED)
990 				goto out;
991 
992 			/*
993 			 * Kill signal always sets processes running,
994 			 * if possible.
995 			 */
996 			if (signum == SIGKILL) {
997 				l = proc_unstop(p);
998 				if (l)
999 					goto runfast;
1000 				goto out;
1001 			}
1002 
1003 			if (prop & SA_CONT) {
1004 				/*
1005 				 * If SIGCONT is default (or ignored),
1006 				 * we continue the process but don't
1007 				 * leave the signal in ps_siglist, as
1008 				 * it has no further action.  If
1009 				 * SIGCONT is held, we continue the
1010 				 * process and leave the signal in
1011 				 * ps_siglist.  If the process catches
1012 				 * SIGCONT, let it handle the signal
1013 				 * itself.  If it isn't waiting on an
1014 				 * event, then it goes back to run
1015 				 * state.  Otherwise, process goes
1016 				 * back to sleep state.
1017 				 */
1018 				if (action == SIG_DFL)
1019 					sigdelset(&p->p_sigctx.ps_siglist,
1020 					signum);
1021 				l = proc_unstop(p);
1022 				if (l && (action == SIG_CATCH))
1023 					goto runfast;
1024 				goto out;
1025 			}
1026 
1027 			if (prop & SA_STOP) {
1028 				/*
1029 				 * Already stopped, don't need to stop again.
1030 				 * (If we did the shell could get confused.)
1031 				 */
1032 				sigdelset(&p->p_sigctx.ps_siglist, signum);
1033 				goto out;
1034 			}
1035 
1036 			/*
1037 			 * If a lwp is sleeping interruptibly, then
1038 			 * wake it up; it will run until the kernel
1039 			 * boundary, where it will stop in issignal(),
1040 			 * since p->p_stat is still SSTOP. When the
1041 			 * process is continued, it will be made
1042 			 * runnable and can look at the signal.
1043 			 */
1044 			if (l)
1045 				goto run;
1046 			goto out;
1047 		} else {
1048 			/* Else what? */
1049 			panic("psignal: Invalid process state %d.",
1050 				p->p_stat);
1051 		}
1052 	}
1053 	/*NOTREACHED*/
1054 
1055  runfast:
1056 	/*
1057 	 * Raise priority to at least PUSER.
1058 	 */
1059 	if (l->l_priority > PUSER)
1060 		l->l_priority = PUSER;
1061  run:
1062 	setrunnable(l);		/* XXXSMP: recurse? */
1063  out:
1064 	/* XXXSMP: works, but icky */
1065 	if (dolock)
1066 		SCHED_UNLOCK(s);
1067 }
1068 
1069 void
1070 psendsig(struct lwp *l, int sig, sigset_t *mask, u_long code)
1071 {
1072 	struct proc *p = l->l_proc;
1073 	struct lwp *le, *li;
1074 	siginfo_t *si;
1075 
1076 	if (p->p_flag & P_SA) {
1077 		si = pool_get(&siginfo_pool, PR_WAITOK);
1078 		si->si_signo = sig;
1079 		si->si_errno = 0;
1080 		si->si_code = code;
1081 		le = li = NULL;
1082 		if (code)
1083 			le = l;
1084 		else
1085 			li = l;
1086 
1087 		sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1088 			    sizeof(siginfo_t), si);
1089 		return;
1090 	}
1091 
1092 	(*p->p_emul->e_sendsig)(sig, mask, code);
1093 }
1094 
1095 static __inline int firstsig(const sigset_t *);
1096 
1097 static __inline int
1098 firstsig(const sigset_t *ss)
1099 {
1100 	int sig;
1101 
1102 	sig = ffs(ss->__bits[0]);
1103 	if (sig != 0)
1104 		return (sig);
1105 #if NSIG > 33
1106 	sig = ffs(ss->__bits[1]);
1107 	if (sig != 0)
1108 		return (sig + 32);
1109 #endif
1110 #if NSIG > 65
1111 	sig = ffs(ss->__bits[2]);
1112 	if (sig != 0)
1113 		return (sig + 64);
1114 #endif
1115 #if NSIG > 97
1116 	sig = ffs(ss->__bits[3]);
1117 	if (sig != 0)
1118 		return (sig + 96);
1119 #endif
1120 	return (0);
1121 }
1122 
1123 /*
1124  * If the current process has received a signal (should be caught or cause
1125  * termination, should interrupt current syscall), return the signal number.
1126  * Stop signals with default action are processed immediately, then cleared;
1127  * they aren't returned.  This is checked after each entry to the system for
1128  * a syscall or trap (though this can usually be done without calling issignal
1129  * by checking the pending signal masks in the CURSIG macro.) The normal call
1130  * sequence is
1131  *
1132  *	while (signum = CURSIG(curlwp))
1133  *		postsig(signum);
1134  */
1135 int
1136 issignal(struct lwp *l)
1137 {
1138 	struct proc	*p = l->l_proc;
1139 	int		s = 0, signum, prop;
1140 	int		dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
1141 	sigset_t	ss;
1142 
1143 	if (p->p_stat == SSTOP) {
1144 		/*
1145 		 * The process is stopped/stopping. Stop ourselves now that
1146 		 * we're on the kernel/userspace boundary.
1147 		 */
1148 		if (dolock)
1149 			SCHED_LOCK(s);
1150 		l->l_stat = LSSTOP;
1151 		p->p_nrlwps--;
1152 		if (p->p_flag & P_TRACED)
1153 			goto sigtraceswitch;
1154 		else
1155 			goto sigswitch;
1156 	}
1157 	for (;;) {
1158 		sigpending1(p, &ss);
1159 		if (p->p_flag & P_PPWAIT)
1160 			sigminusset(&stopsigmask, &ss);
1161 		signum = firstsig(&ss);
1162 		if (signum == 0) {		 	/* no signal to send */
1163 			p->p_sigctx.ps_sigcheck = 0;
1164 			if (locked && dolock)
1165 				SCHED_LOCK(s);
1166 			return (0);
1167 		}
1168 							/* take the signal! */
1169 		sigdelset(&p->p_sigctx.ps_siglist, signum);
1170 
1171 		/*
1172 		 * We should see pending but ignored signals
1173 		 * only if P_TRACED was on when they were posted.
1174 		 */
1175 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1176 		    (p->p_flag & P_TRACED) == 0)
1177 			continue;
1178 
1179 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1180 			/*
1181 			 * If traced, always stop, and stay
1182 			 * stopped until released by the debugger.
1183 			 */
1184 			p->p_xstat = signum;
1185 			if ((p->p_flag & P_FSTRACE) == 0)
1186 				psignal1(p->p_pptr, SIGCHLD, dolock);
1187 			if (dolock)
1188 				SCHED_LOCK(s);
1189 			proc_stop(p);
1190 		sigtraceswitch:
1191 			mi_switch(l, NULL);
1192 			SCHED_ASSERT_UNLOCKED();
1193 			if (dolock)
1194 				splx(s);
1195 			else
1196 				dolock = 1;
1197 
1198 			/*
1199 			 * If we are no longer being traced, or the parent
1200 			 * didn't give us a signal, look for more signals.
1201 			 */
1202 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1203 				continue;
1204 
1205 			/*
1206 			 * If the new signal is being masked, look for other
1207 			 * signals.
1208 			 */
1209 			signum = p->p_xstat;
1210 			p->p_xstat = 0;
1211 			/*
1212 			 * `p->p_sigctx.ps_siglist |= mask' is done
1213 			 * in setrunnable().
1214 			 */
1215 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1216 				continue;
1217 							/* take the signal! */
1218 			sigdelset(&p->p_sigctx.ps_siglist, signum);
1219 		}
1220 
1221 		prop = sigprop[signum];
1222 
1223 		/*
1224 		 * Decide whether the signal should be returned.
1225 		 * Return the signal's number, or fall through
1226 		 * to clear it from the pending mask.
1227 		 */
1228 		switch ((long)SIGACTION(p, signum).sa_handler) {
1229 
1230 		case (long)SIG_DFL:
1231 			/*
1232 			 * Don't take default actions on system processes.
1233 			 */
1234 			if (p->p_pid <= 1) {
1235 #ifdef DIAGNOSTIC
1236 				/*
1237 				 * Are you sure you want to ignore SIGSEGV
1238 				 * in init? XXX
1239 				 */
1240 				printf("Process (pid %d) got signal %d\n",
1241 				    p->p_pid, signum);
1242 #endif
1243 				break;		/* == ignore */
1244 			}
1245 			/*
1246 			 * If there is a pending stop signal to process
1247 			 * with default action, stop here,
1248 			 * then clear the signal.  However,
1249 			 * if process is member of an orphaned
1250 			 * process group, ignore tty stop signals.
1251 			 */
1252 			if (prop & SA_STOP) {
1253 				if (p->p_flag & P_TRACED ||
1254 		    		    (p->p_pgrp->pg_jobc == 0 &&
1255 				    prop & SA_TTYSTOP))
1256 					break;	/* == ignore */
1257 				p->p_xstat = signum;
1258 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1259 					psignal1(p->p_pptr, SIGCHLD, dolock);
1260 				if (dolock)
1261 					SCHED_LOCK(s);
1262 				proc_stop(p);
1263 			sigswitch:
1264 				mi_switch(l, NULL);
1265 				SCHED_ASSERT_UNLOCKED();
1266 				if (dolock)
1267 					splx(s);
1268 				else
1269 					dolock = 1;
1270 				break;
1271 			} else if (prop & SA_IGNORE) {
1272 				/*
1273 				 * Except for SIGCONT, shouldn't get here.
1274 				 * Default action is to ignore; drop it.
1275 				 */
1276 				break;		/* == ignore */
1277 			} else
1278 				goto keep;
1279 			/*NOTREACHED*/
1280 
1281 		case (long)SIG_IGN:
1282 			/*
1283 			 * Masking above should prevent us ever trying
1284 			 * to take action on an ignored signal other
1285 			 * than SIGCONT, unless process is traced.
1286 			 */
1287 #ifdef DEBUG_ISSIGNAL
1288 			if ((prop & SA_CONT) == 0 &&
1289 			    (p->p_flag & P_TRACED) == 0)
1290 				printf("issignal\n");
1291 #endif
1292 			break;		/* == ignore */
1293 
1294 		default:
1295 			/*
1296 			 * This signal has an action, let
1297 			 * postsig() process it.
1298 			 */
1299 			goto keep;
1300 		}
1301 	}
1302 	/* NOTREACHED */
1303 
1304  keep:
1305 						/* leave the signal for later */
1306 	sigaddset(&p->p_sigctx.ps_siglist, signum);
1307 	CHECKSIGS(p);
1308 	if (locked && dolock)
1309 		SCHED_LOCK(s);
1310 	return (signum);
1311 }
1312 
1313 /*
1314  * Put the argument process into the stopped state and notify the parent
1315  * via wakeup.  Signals are handled elsewhere.  The process must not be
1316  * on the run queue.
1317  */
1318 static void
1319 proc_stop(struct proc *p)
1320 {
1321 	struct lwp *l;
1322 
1323 	SCHED_ASSERT_LOCKED();
1324 
1325 	/* XXX lock process LWP state */
1326 	p->p_stat = SSTOP;
1327 	p->p_flag &= ~P_WAITED;
1328 
1329 	/*
1330 	 * Put as many LWP's as possible in stopped state.
1331 	 * Sleeping ones will notice the stopped state as they try to
1332 	 * return to userspace.
1333 	 */
1334 
1335 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1336 		if (l->l_stat == LSONPROC) {
1337 			/* XXX SMP this assumes that a LWP that is LSONPROC
1338 			 * is curlwp and hence is about to be mi_switched
1339 			 * away; the only callers of proc_stop() are:
1340 			 * - psignal
1341 			 * - issignal()
1342 			 * For the former, proc_stop() is only called when
1343 			 * no processes are running, so we don't worry.
1344 			 * For the latter, proc_stop() is called right
1345 			 * before mi_switch().
1346 			 */
1347 			l->l_stat = LSSTOP;
1348 			p->p_nrlwps--;
1349 		} else if (l->l_stat == LSRUN) {
1350 			/* Remove LWP from the run queue */
1351 			remrunqueue(l);
1352 			l->l_stat = LSSTOP;
1353 			p->p_nrlwps--;
1354 		} else if ((l->l_stat == LSSLEEP) ||
1355 		    (l->l_stat == LSSUSPENDED) ||
1356 		    (l->l_stat == LSZOMB) ||
1357 		    (l->l_stat == LSDEAD)) {
1358 			/*
1359 			 * Don't do anything; let sleeping LWPs
1360 			 * discover the stopped state of the process
1361 			 * on their way out of the kernel; otherwise,
1362 			 * things like NFS threads that sleep with
1363 			 * locks will block the rest of the system
1364 			 * from getting any work done.
1365 			 *
1366 			 * Suspended/dead/zombie LWPs aren't going
1367 			 * anywhere, so we don't need to touch them.
1368 			 */
1369 		}
1370 #ifdef DIAGNOSTIC
1371 		else {
1372 			panic("proc_stop: process %d lwp %d "
1373 			      "in unstoppable state %d.\n",
1374 			    p->p_pid, l->l_lid, l->l_stat);
1375 		}
1376 #endif
1377 	}
1378 	/* XXX unlock process LWP state */
1379 
1380 	sched_wakeup((caddr_t)p->p_pptr);
1381 }
1382 
1383 /*
1384  * Given a process in state SSTOP, set the state back to SACTIVE and
1385  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
1386  *
1387  * If no LWPs ended up runnable (and therefore able to take a signal),
1388  * return a LWP that is sleeping interruptably. The caller can wake
1389  * that LWP up to take a signal.
1390  */
1391 struct lwp *
1392 proc_unstop(struct proc *p)
1393 {
1394 	struct lwp *l, *lr = NULL;
1395 	int cantake = 0;
1396 
1397 	SCHED_ASSERT_LOCKED();
1398 
1399 	/*
1400 	 * Our caller wants to be informed if there are only sleeping
1401 	 * and interruptable LWPs left after we have run so that it
1402 	 * can invoke setrunnable() if required - return one of the
1403 	 * interruptable LWPs if this is the case.
1404 	 */
1405 
1406 	p->p_stat = SACTIVE;
1407 	if (p->p_flag & P_SA) {
1408 		/*
1409 		 * Preferentially select the idle LWP as the interruptable
1410 		 * LWP to return if it exists.
1411 		 */
1412 		lr = p->p_sa->sa_idle;
1413 		if (lr != NULL)
1414 			cantake = 1;
1415 	}
1416 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1417 		if (l->l_stat == LSRUN) {
1418 			lr = NULL;
1419 			cantake = 1;
1420 		}
1421 		if (l->l_stat != LSSTOP)
1422 			continue;
1423 
1424 		if (l->l_wchan != NULL) {
1425 			l->l_stat = LSSLEEP;
1426 			if ((cantake == 0) && (l->l_flag & L_SINTR)) {
1427 				lr = l;
1428 				cantake = 1;
1429 			}
1430 		} else {
1431 			setrunnable(l);
1432 			lr = NULL;
1433 			cantake = 1;
1434 		}
1435 	}
1436 
1437 	return lr;
1438 }
1439 
1440 /*
1441  * Take the action for the specified signal
1442  * from the current set of pending signals.
1443  */
1444 void
1445 postsig(int signum)
1446 {
1447 	struct lwp *l;
1448 	struct proc	*p;
1449 	struct sigacts	*ps;
1450 	sig_t		action;
1451 	u_long		code;
1452 	sigset_t	*returnmask;
1453 
1454 	l = curlwp;
1455 	p = l->l_proc;
1456 	ps = p->p_sigacts;
1457 #ifdef DIAGNOSTIC
1458 	if (signum == 0)
1459 		panic("postsig");
1460 #endif
1461 
1462 	KERNEL_PROC_LOCK(l);
1463 
1464 	sigdelset(&p->p_sigctx.ps_siglist, signum);
1465 	action = SIGACTION_PS(ps, signum).sa_handler;
1466 #ifdef KTRACE
1467 	if (KTRPOINT(p, KTR_PSIG))
1468 		ktrpsig(p,
1469 		    signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1470 		    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1471 #endif
1472 	if (action == SIG_DFL) {
1473 		/*
1474 		 * Default action, where the default is to kill
1475 		 * the process.  (Other cases were ignored above.)
1476 		 */
1477 		sigexit(l, signum);
1478 		/* NOTREACHED */
1479 	} else {
1480 		/*
1481 		 * If we get here, the signal must be caught.
1482 		 */
1483 #ifdef DIAGNOSTIC
1484 		if (action == SIG_IGN ||
1485 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
1486 			panic("postsig action");
1487 #endif
1488 		/*
1489 		 * Set the new mask value and also defer further
1490 		 * occurrences of this signal.
1491 		 *
1492 		 * Special case: user has done a sigpause.  Here the
1493 		 * current mask is not of interest, but rather the
1494 		 * mask from before the sigpause is what we want
1495 		 * restored after the signal processing is completed.
1496 		 */
1497 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1498 			returnmask = &p->p_sigctx.ps_oldmask;
1499 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1500 		} else
1501 			returnmask = &p->p_sigctx.ps_sigmask;
1502 		p->p_stats->p_ru.ru_nsignals++;
1503 		if (p->p_sigctx.ps_sig != signum) {
1504 			code = 0;
1505 		} else {
1506 			code = p->p_sigctx.ps_code;
1507 			p->p_sigctx.ps_code = 0;
1508 			p->p_sigctx.ps_lwp = 0;
1509 			p->p_sigctx.ps_sig = 0;
1510 		}
1511 		psendsig(l, signum, returnmask, code);
1512 		(void) splsched();	/* XXXSMP */
1513 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1514 		    &p->p_sigctx.ps_sigmask);
1515 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1516 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1517 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1518 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
1519 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1520 		}
1521 		(void) spl0();		/* XXXSMP */
1522 	}
1523 
1524 	KERNEL_PROC_UNLOCK(l);
1525 }
1526 
1527 /*
1528  * Kill the current process for stated reason.
1529  */
1530 void
1531 killproc(struct proc *p, const char *why)
1532 {
1533 
1534 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1535 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1536 	psignal(p, SIGKILL);
1537 }
1538 
1539 /*
1540  * Force the current process to exit with the specified signal, dumping core
1541  * if appropriate.  We bypass the normal tests for masked and caught signals,
1542  * allowing unrecoverable failures to terminate the process without changing
1543  * signal state.  Mark the accounting record with the signal termination.
1544  * If dumping core, save the signal number for the debugger.  Calls exit and
1545  * does not return.
1546  */
1547 
1548 #if defined(DEBUG)
1549 int	kern_logsigexit = 1;	/* not static to make public for sysctl */
1550 #else
1551 int	kern_logsigexit = 0;	/* not static to make public for sysctl */
1552 #endif
1553 
1554 static	const char logcoredump[] =
1555 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1556 static	const char lognocoredump[] =
1557 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1558 
1559 /* Wrapper function for use in p_userret */
1560 static void
1561 lwp_coredump_hook(struct lwp *l, void *arg)
1562 {
1563 	int s;
1564 
1565 	/*
1566 	 * Suspend ourselves, so that the kernel stack and therefore
1567 	 * the userland registers saved in the trapframe are around
1568 	 * for coredump() to write them out.
1569 	 */
1570 	KERNEL_PROC_LOCK(l);
1571 	l->l_flag &= ~L_DETACHED;
1572 	SCHED_LOCK(s);
1573 	l->l_stat = LSSUSPENDED;
1574 	l->l_proc->p_nrlwps--;
1575 	/* XXX NJWLWP check if this makes sense here: */
1576 	l->l_proc->p_stats->p_ru.ru_nvcsw++;
1577 	mi_switch(l, NULL);
1578 	SCHED_ASSERT_UNLOCKED();
1579 	splx(s);
1580 
1581 	lwp_exit(l);
1582 }
1583 
1584 void
1585 sigexit(struct lwp *l, int signum)
1586 {
1587 	struct proc	*p;
1588 	struct lwp	*l2;
1589 	int		error, exitsig;
1590 
1591 	p = l->l_proc;
1592 
1593 	/*
1594 	 * Don't permit coredump() or exit1() multiple times
1595 	 * in the same process.
1596 	 */
1597 	if (p->p_flag & P_WEXIT) {
1598 		KERNEL_PROC_UNLOCK(l);
1599 		(*p->p_userret)(l, p->p_userret_arg);
1600 	}
1601 	p->p_flag |= P_WEXIT;
1602 	/* We don't want to switch away from exiting. */
1603 	/* XXX multiprocessor: stop LWPs on other processors. */
1604 	if (p->p_flag & P_SA) {
1605 		LIST_FOREACH(l2, &p->p_lwps, l_sibling)
1606 		    l2->l_flag &= ~L_SA;
1607 		p->p_flag &= ~P_SA;
1608 	}
1609 
1610 	/* Make other LWPs stick around long enough to be dumped */
1611 	p->p_userret = lwp_coredump_hook;
1612 	p->p_userret_arg = NULL;
1613 
1614 	exitsig = signum;
1615 	p->p_acflag |= AXSIG;
1616 	if (sigprop[signum] & SA_CORE) {
1617 		p->p_sigctx.ps_sig = signum;
1618 		if ((error = coredump(l)) == 0)
1619 			exitsig |= WCOREFLAG;
1620 
1621 		if (kern_logsigexit) {
1622 			/* XXX What if we ever have really large UIDs? */
1623 			int uid = p->p_cred && p->p_ucred ?
1624 				(int) p->p_ucred->cr_uid : -1;
1625 
1626 			if (error)
1627 				log(LOG_INFO, lognocoredump, p->p_pid,
1628 				    p->p_comm, uid, signum, error);
1629 			else
1630 				log(LOG_INFO, logcoredump, p->p_pid,
1631 				    p->p_comm, uid, signum);
1632 		}
1633 
1634 	}
1635 
1636 	exit1(l, W_EXITCODE(0, exitsig));
1637 	/* NOTREACHED */
1638 }
1639 
1640 /*
1641  * Dump core, into a file named "progname.core" or "core" (depending on the
1642  * value of shortcorename), unless the process was setuid/setgid.
1643  */
1644 int
1645 coredump(struct lwp *l)
1646 {
1647 	struct vnode		*vp;
1648 	struct proc		*p;
1649 	struct vmspace		*vm;
1650 	struct ucred		*cred;
1651 	struct nameidata	nd;
1652 	struct vattr		vattr;
1653 	int			error, error1;
1654 	char			name[MAXPATHLEN];
1655 
1656 	p = l->l_proc;
1657 	vm = p->p_vmspace;
1658 	cred = p->p_cred->pc_ucred;
1659 
1660 	/*
1661 	 * Make sure the process has not set-id, to prevent data leaks.
1662 	 */
1663 	if (p->p_flag & P_SUGID)
1664 		return (EPERM);
1665 
1666 	/*
1667 	 * Refuse to core if the data + stack + user size is larger than
1668 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
1669 	 * data.
1670 	 */
1671 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1672 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1673 		return (EFBIG);		/* better error code? */
1674 
1675 	/*
1676 	 * The core dump will go in the current working directory.  Make
1677 	 * sure that the directory is still there and that the mount flags
1678 	 * allow us to write core dumps there.
1679 	 */
1680 	vp = p->p_cwdi->cwdi_cdir;
1681 	if (vp->v_mount == NULL ||
1682 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1683 		return (EPERM);
1684 
1685 	error = build_corename(p, name);
1686 	if (error)
1687 		return error;
1688 
1689 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1690 	error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
1691 	if (error)
1692 		return (error);
1693 	vp = nd.ni_vp;
1694 
1695 	/* Don't dump to non-regular files or files with links. */
1696 	if (vp->v_type != VREG ||
1697 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1698 		error = EINVAL;
1699 		goto out;
1700 	}
1701 	VATTR_NULL(&vattr);
1702 	vattr.va_size = 0;
1703 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1704 	VOP_SETATTR(vp, &vattr, cred, p);
1705 	p->p_acflag |= ACORE;
1706 
1707 	/* Now dump the actual core file. */
1708 	error = (*p->p_execsw->es_coredump)(l, vp, cred);
1709  out:
1710 	VOP_UNLOCK(vp, 0);
1711 	error1 = vn_close(vp, FWRITE, cred, p);
1712 	if (error == 0)
1713 		error = error1;
1714 	return (error);
1715 }
1716 
1717 /*
1718  * Nonexistent system call-- signal process (may want to handle it).
1719  * Flag error in case process won't see signal immediately (blocked or ignored).
1720  */
1721 /* ARGSUSED */
1722 int
1723 sys_nosys(struct lwp *l, void *v, register_t *retval)
1724 {
1725 	struct proc 	*p;
1726 
1727 	p = l->l_proc;
1728 	psignal(p, SIGSYS);
1729 	return (ENOSYS);
1730 }
1731 
1732 static int
1733 build_corename(struct proc *p, char dst[MAXPATHLEN])
1734 {
1735 	const char	*s;
1736 	char		*d, *end;
1737 	int		i;
1738 
1739 	for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1740 	    *s != '\0'; s++) {
1741 		if (*s == '%') {
1742 			switch (*(s + 1)) {
1743 			case 'n':
1744 				i = snprintf(d, end - d, "%s", p->p_comm);
1745 				break;
1746 			case 'p':
1747 				i = snprintf(d, end - d, "%d", p->p_pid);
1748 				break;
1749 			case 'u':
1750 				i = snprintf(d, end - d, "%.*s",
1751 				    (int)sizeof p->p_pgrp->pg_session->s_login,
1752 				    p->p_pgrp->pg_session->s_login);
1753 				break;
1754 			case 't':
1755 				i = snprintf(d, end - d, "%ld",
1756 				    p->p_stats->p_start.tv_sec);
1757 				break;
1758 			default:
1759 				goto copy;
1760 			}
1761 			d += i;
1762 			s++;
1763 		} else {
1764  copy:			*d = *s;
1765 			d++;
1766 		}
1767 		if (d >= end)
1768 			return (ENAMETOOLONG);
1769 	}
1770 	*d = '\0';
1771 	return 0;
1772 }
1773 
1774 void
1775 getucontext(struct lwp *l, ucontext_t *ucp)
1776 {
1777 	struct proc	*p;
1778 
1779 	p = l->l_proc;
1780 
1781 	ucp->uc_flags = 0;
1782 	ucp->uc_link = l->l_ctxlink;
1783 
1784 	(void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
1785 	ucp->uc_flags |= _UC_SIGMASK;
1786 
1787 	/*
1788 	 * The (unsupplied) definition of the `current execution stack'
1789 	 * in the System V Interface Definition appears to allow returning
1790 	 * the main context stack.
1791 	 */
1792 	if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
1793 		ucp->uc_stack.ss_sp = (void *)USRSTACK;
1794 		ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
1795 		ucp->uc_stack.ss_flags = 0;	/* XXX, def. is Very Fishy */
1796 	} else {
1797 		/* Simply copy alternate signal execution stack. */
1798 		ucp->uc_stack = p->p_sigctx.ps_sigstk;
1799 	}
1800 	ucp->uc_flags |= _UC_STACK;
1801 
1802 	cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
1803 }
1804 
1805 /* ARGSUSED */
1806 int
1807 sys_getcontext(struct lwp *l, void *v, register_t *retval)
1808 {
1809 	struct sys_getcontext_args /* {
1810 		syscallarg(struct __ucontext *) ucp;
1811 	} */ *uap = v;
1812 	ucontext_t uc;
1813 
1814 	getucontext(l, &uc);
1815 
1816 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
1817 }
1818 
1819 int
1820 setucontext(struct lwp *l, const ucontext_t *ucp)
1821 {
1822 	struct proc	*p;
1823 	int		error;
1824 
1825 	p = l->l_proc;
1826 	if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
1827 		return (error);
1828 	l->l_ctxlink = ucp->uc_link;
1829 	/*
1830 	 * We might want to take care of the stack portion here but currently
1831 	 * don't; see the comment in getucontext().
1832 	 */
1833 	if ((ucp->uc_flags & _UC_SIGMASK) != 0)
1834 		sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
1835 
1836 	return 0;
1837 }
1838 
1839 /* ARGSUSED */
1840 int
1841 sys_setcontext(struct lwp *l, void *v, register_t *retval)
1842 {
1843 	struct sys_setcontext_args /* {
1844 		syscallarg(const ucontext_t *) ucp;
1845 	} */ *uap = v;
1846 	ucontext_t uc;
1847 	int error;
1848 
1849 	if (SCARG(uap, ucp) == NULL)	/* i.e. end of uc_link chain */
1850 		exit1(l, W_EXITCODE(0, 0));
1851 	else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
1852 	    (error = setucontext(l, &uc)) != 0)
1853 		return (error);
1854 
1855 	return (EJUSTRETURN);
1856 }
1857 
1858 /*
1859  * sigtimedwait(2) system call, used also for implementation
1860  * of sigwaitinfo() and sigwait().
1861  *
1862  * This only handles single LWP in signal wait. libpthread provides
1863  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
1864  *
1865  * XXX no support for queued signals, si_code is always SI_USER.
1866  */
1867 int
1868 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
1869 {
1870 	struct sys___sigtimedwait_args /* {
1871 		syscallarg(const sigset_t *) set;
1872 		syscallarg(siginfo_t *) info;
1873 		syscallarg(struct timespec *) timeout;
1874 	} */ *uap = v;
1875 	sigset_t waitset, twaitset;
1876 	struct proc *p = l->l_proc;
1877 	int error, signum, s;
1878 	int timo = 0;
1879 	struct timeval tvstart;
1880 	struct timespec ts;
1881 
1882 	if ((error = copyin(SCARG(uap, set), &waitset, sizeof(waitset))))
1883 		return (error);
1884 
1885 	/*
1886 	 * Silently ignore SA_CANTMASK signals. psignal1() would
1887 	 * ignore SA_CANTMASK signals in waitset, we do this
1888 	 * only for the below siglist check.
1889 	 */
1890 	sigminusset(&sigcantmask, &waitset);
1891 
1892 	/*
1893 	 * First scan siglist and check if there is signal from
1894 	 * our waitset already pending.
1895 	 */
1896 	twaitset = waitset;
1897 	__sigandset(&p->p_sigctx.ps_siglist, &twaitset);
1898 	if ((signum = firstsig(&twaitset))) {
1899 		/* found pending signal */
1900 		sigdelset(&p->p_sigctx.ps_siglist, signum);
1901 		goto sig;
1902 	}
1903 
1904 	/*
1905 	 * Calculate timeout, if it was specified.
1906 	 */
1907 	if (SCARG(uap, timeout)) {
1908 		uint64_t ms;
1909 
1910 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
1911 			return (error);
1912 
1913 		ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
1914 		timo = mstohz(ms);
1915 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
1916 			timo = 1;
1917 		if (timo <= 0)
1918 			return (EAGAIN);
1919 
1920 		/*
1921 		 * Remember current mono_time, it would be used in
1922 		 * ECANCELED/ERESTART case.
1923 		 */
1924 		s = splclock();
1925 		tvstart = mono_time;
1926 		splx(s);
1927 	}
1928 
1929 	/*
1930 	 * Setup ps_sigwait list.
1931 	 */
1932 	p->p_sigctx.ps_sigwaited = -1;
1933 	p->p_sigctx.ps_sigwait = waitset;
1934 
1935 	/*
1936 	 * Wait for signal to arrive. We can either be woken up or
1937 	 * time out.
1938 	 */
1939 	error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
1940 
1941 	/*
1942 	 * Check if a signal from our wait set has arrived, or if it
1943 	 * was mere wakeup.
1944 	 */
1945 	if (!error) {
1946 		if ((signum = p->p_sigctx.ps_sigwaited) <= 0) {
1947 			/* wakeup via _lwp_wakeup() */
1948 			error = ECANCELED;
1949 		}
1950 	}
1951 
1952 	/*
1953 	 * On error, clear sigwait indication. psignal1() sets it
1954 	 * in !error case.
1955 	 */
1956 	if (error) {
1957 		p->p_sigctx.ps_sigwaited = 0;
1958 
1959 		/*
1960 		 * If the sleep was interrupted (either by signal or wakeup),
1961 		 * update the timeout and copyout new value back.
1962 		 * It would be used when the syscall would be restarted
1963 		 * or called again.
1964 		 */
1965 		if (timo && (error == ERESTART || error == ECANCELED)) {
1966 			struct timeval tvnow, tvtimo;
1967 			int err;
1968 
1969 			s = splclock();
1970 			tvnow = mono_time;
1971 			splx(s);
1972 
1973 			TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
1974 
1975 			/* compute how much time has passed since start */
1976 			timersub(&tvnow, &tvstart, &tvnow);
1977 			/* substract passed time from timeout */
1978 			timersub(&tvtimo, &tvnow, &tvtimo);
1979 
1980 			if (tvtimo.tv_sec < 0)
1981 				return (EAGAIN);
1982 
1983 			TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
1984 
1985 			/* copy updated timeout to userland */
1986 			if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts))))
1987 				return (err);
1988 		}
1989 
1990 		return (error);
1991 	}
1992 
1993 	/*
1994 	 * If a signal from the wait set arrived, copy it to userland.
1995 	 * XXX no queued signals for now
1996 	 */
1997 	if (signum > 0) {
1998 		siginfo_t si;
1999 
2000  sig:
2001 		memset(&si, 0, sizeof(si));
2002 		si.si_signo = signum;
2003 		si.si_code = SI_USER;
2004 
2005 		error = copyout(&si, SCARG(uap, info), sizeof(si));
2006 		if (error)
2007 			return (error);
2008 	}
2009 
2010 	return (0);
2011 }
2012 
2013 /*
2014  * Returns true if signal is ignored or masked for passed process.
2015  */
2016 int
2017 sigismasked(struct proc *p, int sig)
2018 {
2019 
2020 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
2021 	    sigismember(&p->p_sigctx.ps_sigmask, sig));
2022 }
2023 
2024 static int
2025 filt_sigattach(struct knote *kn)
2026 {
2027 	struct proc *p = curproc;
2028 
2029 	kn->kn_ptr.p_proc = p;
2030 	kn->kn_flags |= EV_CLEAR;               /* automatically set */
2031 
2032 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2033 
2034 	return (0);
2035 }
2036 
2037 static void
2038 filt_sigdetach(struct knote *kn)
2039 {
2040 	struct proc *p = kn->kn_ptr.p_proc;
2041 
2042 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2043 }
2044 
2045 /*
2046  * signal knotes are shared with proc knotes, so we apply a mask to
2047  * the hint in order to differentiate them from process hints.  This
2048  * could be avoided by using a signal-specific knote list, but probably
2049  * isn't worth the trouble.
2050  */
2051 static int
2052 filt_signal(struct knote *kn, long hint)
2053 {
2054 
2055 	if (hint & NOTE_SIGNAL) {
2056 		hint &= ~NOTE_SIGNAL;
2057 
2058 		if (kn->kn_id == hint)
2059 			kn->kn_data++;
2060 	}
2061 	return (kn->kn_data != 0);
2062 }
2063 
2064 const struct filterops sig_filtops = {
2065 	0, filt_sigattach, filt_sigdetach, filt_signal
2066 };
2067