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