xref: /netbsd-src/sys/kern/sys_sig.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: sys_sig.c,v 1.14 2008/04/24 18:39:24 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1982, 1986, 1989, 1991, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  * (c) UNIX System Laboratories, Inc.
43  * All or some portions of this file are derived from material licensed
44  * to the University of California by American Telephone and Telegraph
45  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
46  * the permission of UNIX System Laboratories, Inc.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.14 2008/04/24 18:39:24 ad Exp $");
77 
78 #include "opt_ptrace.h"
79 #include "opt_compat_netbsd.h"
80 #include "opt_compat_netbsd32.h"
81 
82 #include <sys/param.h>
83 #include <sys/kernel.h>
84 #include <sys/signalvar.h>
85 #include <sys/proc.h>
86 #include <sys/pool.h>
87 #include <sys/syscallargs.h>
88 #include <sys/kauth.h>
89 #include <sys/wait.h>
90 #include <sys/kmem.h>
91 
92 #ifdef COMPAT_16
93 /* ARGSUSED */
94 int
95 compat_16_sys___sigaction14(struct lwp *l, const struct compat_16_sys___sigaction14_args *uap, register_t *retval)
96 {
97 	/* {
98 		syscallarg(int)				signum;
99 		syscallarg(const struct sigaction *)	nsa;
100 		syscallarg(struct sigaction *)		osa;
101 	} */
102 	struct sigaction	nsa, osa;
103 	int			error;
104 
105 	if (SCARG(uap, nsa)) {
106 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
107 		if (error)
108 			return (error);
109 	}
110 	error = sigaction1(l, SCARG(uap, signum),
111 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
112 	    NULL, 0);
113 	if (error)
114 		return (error);
115 	if (SCARG(uap, osa)) {
116 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
117 		if (error)
118 			return (error);
119 	}
120 	return (0);
121 }
122 #endif
123 
124 /* ARGSUSED */
125 int
126 sys___sigaction_sigtramp(struct lwp *l, const struct sys___sigaction_sigtramp_args *uap, register_t *retval)
127 {
128 	/* {
129 		syscallarg(int)				signum;
130 		syscallarg(const struct sigaction *)	nsa;
131 		syscallarg(struct sigaction *)		osa;
132 		syscallarg(void *)			tramp;
133 		syscallarg(int)				vers;
134 	} */
135 	struct sigaction nsa, osa;
136 	int error;
137 
138 	if (SCARG(uap, nsa)) {
139 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
140 		if (error)
141 			return (error);
142 	}
143 	error = sigaction1(l, SCARG(uap, signum),
144 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
145 	    SCARG(uap, tramp), SCARG(uap, vers));
146 	if (error)
147 		return (error);
148 	if (SCARG(uap, osa)) {
149 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
150 		if (error)
151 			return (error);
152 	}
153 	return (0);
154 }
155 
156 /*
157  * Manipulate signal mask.  Note that we receive new mask, not pointer, and
158  * return old mask as return value; the library stub does the rest.
159  */
160 int
161 sys___sigprocmask14(struct lwp *l, const struct sys___sigprocmask14_args *uap, register_t *retval)
162 {
163 	/* {
164 		syscallarg(int)			how;
165 		syscallarg(const sigset_t *)	set;
166 		syscallarg(sigset_t *)		oset;
167 	} */
168 	struct proc	*p = l->l_proc;
169 	sigset_t	nss, oss;
170 	int		error;
171 
172 	if (SCARG(uap, set)) {
173 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
174 		if (error)
175 			return (error);
176 	}
177 	mutex_enter(p->p_lock);
178 	error = sigprocmask1(l, SCARG(uap, how),
179 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
180 	mutex_exit(p->p_lock);
181 	if (error)
182 		return (error);
183 	if (SCARG(uap, oset)) {
184 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
185 		if (error)
186 			return (error);
187 	}
188 	return (0);
189 }
190 
191 /* ARGSUSED */
192 int
193 sys___sigpending14(struct lwp *l, const struct sys___sigpending14_args *uap, register_t *retval)
194 {
195 	/* {
196 		syscallarg(sigset_t *)	set;
197 	} */
198 	sigset_t ss;
199 
200 	sigpending1(l, &ss);
201 	return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
202 }
203 
204 /*
205  * Suspend process until signal, providing mask to be set in the meantime.
206  * Note nonstandard calling convention: libc stub passes mask, not pointer,
207  * to save a copyin.
208  */
209 /* ARGSUSED */
210 int
211 sys___sigsuspend14(struct lwp *l, const struct sys___sigsuspend14_args *uap, register_t *retval)
212 {
213 	/* {
214 		syscallarg(const sigset_t *)	set;
215 	} */
216 	sigset_t	ss;
217 	int		error;
218 
219 	if (SCARG(uap, set)) {
220 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
221 		if (error)
222 			return (error);
223 	}
224 
225 	return (sigsuspend1(l, SCARG(uap, set) ? &ss : 0));
226 }
227 
228 /* ARGSUSED */
229 int
230 sys___sigaltstack14(struct lwp *l, const struct sys___sigaltstack14_args *uap, register_t *retval)
231 {
232 	/* {
233 		syscallarg(const struct sigaltstack *)	nss;
234 		syscallarg(struct sigaltstack *)	oss;
235 	} */
236 	struct sigaltstack	nss, oss;
237 	int			error;
238 
239 	if (SCARG(uap, nss)) {
240 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
241 		if (error)
242 			return (error);
243 	}
244 	error = sigaltstack1(l,
245 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
246 	if (error)
247 		return (error);
248 	if (SCARG(uap, oss)) {
249 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
250 		if (error)
251 			return (error);
252 	}
253 	return (0);
254 }
255 
256 /* ARGSUSED */
257 int
258 sys_kill(struct lwp *l, const struct sys_kill_args *uap, register_t *retval)
259 {
260 	/* {
261 		syscallarg(int)	pid;
262 		syscallarg(int)	signum;
263 	} */
264 	struct proc	*p;
265 	ksiginfo_t	ksi;
266 	int signum = SCARG(uap, signum);
267 	int error;
268 
269 	if ((u_int)signum >= NSIG)
270 		return (EINVAL);
271 	KSI_INIT(&ksi);
272 	ksi.ksi_signo = signum;
273 	ksi.ksi_code = SI_USER;
274 	ksi.ksi_pid = l->l_proc->p_pid;
275 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
276 	if (SCARG(uap, pid) > 0) {
277 		/* kill single process */
278 		mutex_enter(proc_lock);
279 		if ((p = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) {
280 			mutex_exit(proc_lock);
281 			return (ESRCH);
282 		}
283 		mutex_enter(p->p_lock);
284 		error = kauth_authorize_process(l->l_cred,
285 		    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum),
286 		    NULL, NULL);
287 		if (!error && signum) {
288 			kpsignal2(p, &ksi);
289 		}
290 		mutex_exit(p->p_lock);
291 		mutex_exit(proc_lock);
292 		return (error);
293 	}
294 	switch (SCARG(uap, pid)) {
295 	case -1:		/* broadcast signal */
296 		return (killpg1(l, &ksi, 0, 1));
297 	case 0:			/* signal own process group */
298 		return (killpg1(l, &ksi, 0, 0));
299 	default:		/* negative explicit process group */
300 		return (killpg1(l, &ksi, -SCARG(uap, pid), 0));
301 	}
302 	/* NOTREACHED */
303 }
304 
305 /* ARGSUSED */
306 int
307 sys_getcontext(struct lwp *l, const struct sys_getcontext_args *uap, register_t *retval)
308 {
309 	/* {
310 		syscallarg(struct __ucontext *) ucp;
311 	} */
312 	struct proc *p = l->l_proc;
313 	ucontext_t uc;
314 
315 	mutex_enter(p->p_lock);
316 	getucontext(l, &uc);
317 	mutex_exit(p->p_lock);
318 
319 	return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
320 }
321 
322 /* ARGSUSED */
323 int
324 sys_setcontext(struct lwp *l, const struct sys_setcontext_args *uap, register_t *retval)
325 {
326 	/* {
327 		syscallarg(const ucontext_t *) ucp;
328 	} */
329 	struct proc *p = l->l_proc;
330 	ucontext_t uc;
331 	int error;
332 
333 	error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
334 	if (error)
335 		return (error);
336 	if (!(uc.uc_flags & _UC_CPU))
337 		return (EINVAL);
338 	mutex_enter(p->p_lock);
339 	error = setucontext(l, &uc);
340 	mutex_exit(p->p_lock);
341 	if (error)
342  		return (error);
343 
344 	return (EJUSTRETURN);
345 }
346 
347 /*
348  * sigtimedwait(2) system call, used also for implementation
349  * of sigwaitinfo() and sigwait().
350  *
351  * This only handles single LWP in signal wait. libpthread provides
352  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
353  */
354 int
355 sys___sigtimedwait(struct lwp *l, const struct sys___sigtimedwait_args *uap, register_t *retval)
356 {
357 
358 	return __sigtimedwait1(l, uap, retval, copyout, copyin, copyout);
359 }
360 
361 int
362 sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
363 	struct sigaction *osa, const void *tramp, int vers)
364 {
365 	struct proc *p;
366 	struct sigacts *ps;
367 	sigset_t tset;
368 	int prop, error;
369 	ksiginfoq_t kq;
370 
371 	if (signum <= 0 || signum >= NSIG)
372 		return (EINVAL);
373 
374 	p = l->l_proc;
375 	error = 0;
376 	ksiginfo_queue_init(&kq);
377 
378 	/*
379 	 * Trampoline ABI version 0 is reserved for the legacy kernel
380 	 * provided on-stack trampoline.  Conversely, if we are using a
381 	 * non-0 ABI version, we must have a trampoline.  Only validate the
382 	 * vers if a new sigaction was supplied. Emulations use legacy
383 	 * kernel trampolines with version 0, alternatively check for that
384 	 * too.
385 	 */
386 	if ((vers != 0 && tramp == NULL) ||
387 #ifdef SIGTRAMP_VALID
388 	    (nsa != NULL &&
389 	    ((vers == 0) ?
390 		(p->p_emul->e_sigcode == NULL) :
391 		!SIGTRAMP_VALID(vers))) ||
392 #endif
393 	    (vers == 0 && tramp != NULL)) {
394 		return (EINVAL);
395 	}
396 
397 	mutex_enter(p->p_lock);
398 
399 	ps = p->p_sigacts;
400 	if (osa)
401 		*osa = SIGACTION_PS(ps, signum);
402 	if (!nsa)
403 		goto out;
404 
405 	prop = sigprop[signum];
406 	if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
407 		error = EINVAL;
408 		goto out;
409 	}
410 
411 	SIGACTION_PS(ps, signum) = *nsa;
412 	ps->sa_sigdesc[signum].sd_tramp = tramp;
413 	ps->sa_sigdesc[signum].sd_vers = vers;
414 	sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
415 
416 	if ((prop & SA_NORESET) != 0)
417 		SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
418 
419 	if (signum == SIGCHLD) {
420 		if (nsa->sa_flags & SA_NOCLDSTOP)
421 			p->p_sflag |= PS_NOCLDSTOP;
422 		else
423 			p->p_sflag &= ~PS_NOCLDSTOP;
424 		if (nsa->sa_flags & SA_NOCLDWAIT) {
425 			/*
426 			 * Paranoia: since SA_NOCLDWAIT is implemented by
427 			 * reparenting the dying child to PID 1 (and trust
428 			 * it to reap the zombie), PID 1 itself is forbidden
429 			 * to set SA_NOCLDWAIT.
430 			 */
431 			if (p->p_pid == 1)
432 				p->p_flag &= ~PK_NOCLDWAIT;
433 			else
434 				p->p_flag |= PK_NOCLDWAIT;
435 		} else
436 			p->p_flag &= ~PK_NOCLDWAIT;
437 
438 		if (nsa->sa_handler == SIG_IGN) {
439 			/*
440 			 * Paranoia: same as above.
441 			 */
442 			if (p->p_pid == 1)
443 				p->p_flag &= ~PK_CLDSIGIGN;
444 			else
445 				p->p_flag |= PK_CLDSIGIGN;
446 		} else
447 			p->p_flag &= ~PK_CLDSIGIGN;
448 	}
449 
450 	if ((nsa->sa_flags & SA_NODEFER) == 0)
451 		sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
452 	else
453 		sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
454 
455 	/*
456 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
457 	 * SIG_IGN, and for signals set to SIG_DFL where the default is to
458 	 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
459 	 * we have to restart the process.
460 	 */
461 	if (nsa->sa_handler == SIG_IGN ||
462 	    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
463 		/* Never to be seen again. */
464 		sigemptyset(&tset);
465 		sigaddset(&tset, signum);
466 		sigclearall(p, &tset, &kq);
467 		if (signum != SIGCONT) {
468 			/* Easier in psignal */
469 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
470 		}
471 		sigdelset(&p->p_sigctx.ps_sigcatch, signum);
472 	} else {
473 		sigdelset(&p->p_sigctx.ps_sigignore, signum);
474 		if (nsa->sa_handler == SIG_DFL)
475 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
476 		else
477 			sigaddset(&p->p_sigctx.ps_sigcatch, signum);
478 	}
479 
480 	/*
481 	 * Previously held signals may now have become visible.  Ensure that
482 	 * we check for them before returning to userspace.
483 	 */
484 	if (sigispending(l, 0)) {
485 		lwp_lock(l);
486 		l->l_flag |= LW_PENDSIG;
487 		lwp_unlock(l);
488 	}
489  out:
490 	mutex_exit(p->p_lock);
491 	ksiginfo_queue_drain(&kq);
492 
493 	return (error);
494 }
495 
496 int
497 sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
498 {
499 	int more;
500 
501 	KASSERT(mutex_owned(l->l_proc->p_lock));
502 
503 	if (oss)
504 		*oss = l->l_sigmask;
505 	if (nss) {
506 		switch (how) {
507 		case SIG_BLOCK:
508 			sigplusset(nss, &l->l_sigmask);
509 			more = 0;
510 			break;
511 		case SIG_UNBLOCK:
512 			sigminusset(nss, &l->l_sigmask);
513 			more = 1;
514 			break;
515 		case SIG_SETMASK:
516 			l->l_sigmask = *nss;
517 			more = 1;
518 			break;
519 		default:
520 			return (EINVAL);
521 		}
522 		sigminusset(&sigcantmask, &l->l_sigmask);
523 		if (more && sigispending(l, 0)) {
524 			/*
525 			 * Check for pending signals on return to user.
526 			 */
527 			lwp_lock(l);
528 			l->l_flag |= LW_PENDSIG;
529 			lwp_unlock(l);
530 		}
531 	}
532 
533 	return (0);
534 }
535 
536 void
537 sigpending1(struct lwp *l, sigset_t *ss)
538 {
539 	struct proc *p = l->l_proc;
540 
541 	mutex_enter(p->p_lock);
542 	*ss = l->l_sigpend.sp_set;
543 	sigplusset(&p->p_sigpend.sp_set, ss);
544 	sigminusset(&l->l_sigmask, ss);
545 	mutex_exit(p->p_lock);
546 }
547 
548 int
549 sigsuspend1(struct lwp *l, const sigset_t *ss)
550 {
551 	struct proc *p;
552 
553 	p = l->l_proc;
554 
555 	if (ss) {
556 		/*
557 		 * When returning from sigsuspend, we want
558 		 * the old mask to be restored after the
559 		 * signal handler has finished.  Thus, we
560 		 * save it here and mark the sigctx structure
561 		 * to indicate this.
562 		 */
563 		mutex_enter(p->p_lock);
564 		l->l_sigrestore = 1;
565 		l->l_sigoldmask = l->l_sigmask;
566 		l->l_sigmask = *ss;
567 		sigminusset(&sigcantmask, &l->l_sigmask);
568 
569 		/* Check for pending signals when sleeping. */
570 		if (sigispending(l, 0)) {
571 			lwp_lock(l);
572 			l->l_flag |= LW_PENDSIG;
573 			lwp_unlock(l);
574 		}
575 		mutex_exit(p->p_lock);
576 	}
577 
578 	while (kpause("pause", true, 0, NULL) == 0)
579 		;
580 
581 	/* always return EINTR rather than ERESTART... */
582 	return (EINTR);
583 }
584 
585 int
586 sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
587 	     struct sigaltstack *oss)
588 {
589 	struct proc *p = l->l_proc;
590 	int error = 0;
591 
592 	mutex_enter(p->p_lock);
593 
594 	if (oss)
595 		*oss = l->l_sigstk;
596 
597 	if (nss) {
598 		if (nss->ss_flags & ~SS_ALLBITS)
599 			error = EINVAL;
600 		else if (nss->ss_flags & SS_DISABLE) {
601 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
602 				error = EINVAL;
603 		} else if (nss->ss_size < MINSIGSTKSZ)
604 			error = ENOMEM;
605 
606 		if (!error)
607 			l->l_sigstk = *nss;
608 	}
609 
610 	mutex_exit(p->p_lock);
611 
612 	return (error);
613 }
614 
615 int
616 __sigtimedwait1(struct lwp *l, const struct sys___sigtimedwait_args *uap, register_t *retval,
617     copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
618 {
619 	/* {
620 		syscallarg(const sigset_t *) set;
621 		syscallarg(siginfo_t *) info;
622 		syscallarg(struct timespec *) timeout;
623 	} */
624 	struct proc *p = l->l_proc;
625 	int error, signum;
626 	int timo = 0;
627 	struct timespec ts, tsstart, tsnow;
628 	ksiginfo_t *ksi;
629 
630 	memset(&tsstart, 0, sizeof tsstart);	 /* XXX gcc */
631 
632 	/*
633 	 * Calculate timeout, if it was specified.
634 	 */
635 	if (SCARG(uap, timeout)) {
636 		uint64_t ms;
637 
638 		if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts))))
639 			return (error);
640 
641 		ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
642 		timo = mstohz(ms);
643 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
644 			timo = 1;
645 		if (timo <= 0)
646 			return (EAGAIN);
647 
648 		/*
649 		 * Remember current uptime, it would be used in
650 		 * ECANCELED/ERESTART case.
651 		 */
652 		getnanouptime(&tsstart);
653 	}
654 
655 	error = copyin(SCARG(uap, set), &l->l_sigwaitset,
656 	    sizeof(l->l_sigwaitset));
657 	if (error != 0)
658 		return (error);
659 
660 	/*
661 	 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
662 	 * SA_CANTMASK signals in waitset, we do this only for the below
663 	 * siglist check.
664 	 */
665 	sigminusset(&sigcantmask, &l->l_sigwaitset);
666 
667 	/*
668 	 * Allocate a ksi up front.  We can't sleep with the mutex held.
669 	 */
670 	ksi = ksiginfo_alloc(p, NULL, PR_WAITOK);
671 	if (ksi == NULL)
672 		return (ENOMEM);
673 
674 	mutex_enter(p->p_lock);
675 
676 	if ((signum = sigget(&p->p_sigpend, ksi, 0, &l->l_sigwaitset)) == 0)
677 		signum = sigget(&l->l_sigpend, ksi, 0, &l->l_sigwaitset);
678 
679 	if (signum != 0) {
680 		/*
681 		 * We found a pending signal - copy it out to the user.
682 		 */
683 		mutex_exit(p->p_lock);
684 		goto out;
685 	}
686 
687 	/*
688 	 * Set up the sigwait list.
689 	 */
690 	l->l_sigwaited = ksi;
691 	LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
692 
693 	/*
694 	 * Wait for signal to arrive. We can either be woken up or time out.
695 	 */
696 	error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
697 
698 	/*
699 	 * Need to find out if we woke as a result of lwp_wakeup() or a
700 	 * signal outside our wait set.
701 	 */
702 	if (l->l_sigwaited != NULL) {
703 		if (error == EINTR) {
704 			/* wakeup via _lwp_wakeup() */
705 			error = ECANCELED;
706 		} else if (!error) {
707 			/* spurious wakeup - arrange for syscall restart */
708 			error = ERESTART;
709 		}
710 		l->l_sigwaited = NULL;
711 		LIST_REMOVE(l, l_sigwaiter);
712 	}
713 
714 	mutex_exit(p->p_lock);
715 
716 	/*
717 	 * If the sleep was interrupted (either by signal or wakeup), update
718 	 * the timeout and copyout new value back.  It would be used when
719 	 * the syscall would be restarted or called again.
720 	 */
721 	if (timo && (error == ERESTART || error == ECANCELED)) {
722 		getnanouptime(&tsnow);
723 
724 		/* compute how much time has passed since start */
725 		timespecsub(&tsnow, &tsstart, &tsnow);
726 		/* substract passed time from timeout */
727 		timespecsub(&ts, &tsnow, &ts);
728 
729 		if (ts.tv_sec < 0)
730 			error = EAGAIN;
731 		else {
732 			/* copy updated timeout to userland */
733 			error = (*put_timeout)(&ts, SCARG(uap, timeout),
734 			    sizeof(ts));
735 		}
736 	}
737 
738 	/*
739 	 * If a signal from the wait set arrived, copy it to userland.
740 	 * Copy only the used part of siginfo, the padding part is
741 	 * left unchanged (userland is not supposed to touch it anyway).
742 	 */
743  out:
744 	if (error == 0)
745 		error = (*put_info)(&ksi->ksi_info, SCARG(uap, info),
746 		    sizeof(ksi->ksi_info));
747 
748 	ksiginfo_free(ksi);
749 
750 	return error;
751 }
752