1/*	$NetBSD$	*/
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$");
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
84int
85sys___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 */
120int
121sys___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
152int
153sys___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 */
170int
171sys___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
188int
189sys___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
217static int
218kill1(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_raw(pid);
246		if (p == NULL || (p->p_stat != SACTIVE && p->p_stat != SSTOP)) {
247			mutex_exit(proc_lock);
248			/* IEEE Std 1003.1-2001: return success for zombies */
249			return p ? 0 : ESRCH;
250		}
251		mutex_enter(p->p_lock);
252		error = kauth_authorize_process(l->l_cred,
253		    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(ksi->ksi_signo),
254		    NULL, NULL);
255		if (!error && ksi->ksi_signo) {
256			kpsignal2(p, ksi);
257		}
258		mutex_exit(p->p_lock);
259		mutex_exit(proc_lock);
260		return error;
261	}
262
263	switch (pid) {
264	case -1:		/* broadcast signal */
265		return killpg1(l, ksi, 0, 1);
266	case 0:			/* signal own process group */
267		return killpg1(l, ksi, 0, 0);
268	default:		/* negative explicit process group */
269		return killpg1(l, ksi, -pid, 0);
270	}
271	/* NOTREACHED */
272}
273
274int
275sys_sigqueueinfo(struct lwp *l, const struct sys_sigqueueinfo_args *uap,
276    register_t *retval)
277{
278	/* {
279		syscallarg(pid_t int)	pid;
280		syscallarg(const siginfo_t *)	info;
281	} */
282	ksiginfo_t	ksi;
283	int error;
284
285	KSI_INIT(&ksi);
286
287	if ((error = copyin(&SCARG(uap, info)->_info, &ksi.ksi_info,
288	    sizeof(ksi.ksi_info))) != 0)
289		return error;
290
291	return kill1(l, SCARG(uap, pid), &ksi, retval);
292}
293
294int
295sys_kill(struct lwp *l, const struct sys_kill_args *uap, register_t *retval)
296{
297	/* {
298		syscallarg(pid_t)	pid;
299		syscallarg(int)	signum;
300	} */
301	ksiginfo_t	ksi;
302
303	KSI_INIT(&ksi);
304
305	ksi.ksi_signo = SCARG(uap, signum);
306	ksi.ksi_code = SI_USER;
307	ksi.ksi_pid = l->l_proc->p_pid;
308	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
309
310	return kill1(l, SCARG(uap, pid), &ksi, retval);
311}
312
313int
314sys_getcontext(struct lwp *l, const struct sys_getcontext_args *uap,
315    register_t *retval)
316{
317	/* {
318		syscallarg(struct __ucontext *) ucp;
319	} */
320	struct proc *p = l->l_proc;
321	ucontext_t uc;
322
323	memset(&uc, 0, sizeof(uc));
324
325	mutex_enter(p->p_lock);
326	getucontext(l, &uc);
327	mutex_exit(p->p_lock);
328
329	return copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)));
330}
331
332int
333sys_setcontext(struct lwp *l, const struct sys_setcontext_args *uap,
334    register_t *retval)
335{
336	/* {
337		syscallarg(const ucontext_t *) ucp;
338	} */
339	struct proc *p = l->l_proc;
340	ucontext_t uc;
341	int error;
342
343	error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
344	if (error)
345		return error;
346	if ((uc.uc_flags & _UC_CPU) == 0)
347		return EINVAL;
348	mutex_enter(p->p_lock);
349	error = setucontext(l, &uc);
350	mutex_exit(p->p_lock);
351	if (error)
352 		return error;
353
354	return EJUSTRETURN;
355}
356
357/*
358 * sigtimedwait(2) system call, used also for implementation
359 * of sigwaitinfo() and sigwait().
360 *
361 * This only handles single LWP in signal wait. libpthread provides
362 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
363 */
364int
365sys_____sigtimedwait50(struct lwp *l,
366    const struct sys_____sigtimedwait50_args *uap, register_t *retval)
367{
368
369	return sigtimedwait1(l, uap, retval, copyin, copyout, copyin, copyout);
370}
371
372int
373sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
374	struct sigaction *osa, const void *tramp, int vers)
375{
376	struct proc *p;
377	struct sigacts *ps;
378	sigset_t tset;
379	int prop, error;
380	ksiginfoq_t kq;
381	static bool v0v1valid;
382
383	if (signum <= 0 || signum >= NSIG)
384		return EINVAL;
385
386	p = l->l_proc;
387	error = 0;
388	ksiginfo_queue_init(&kq);
389
390	/*
391	 * Trampoline ABI version 0 is reserved for the legacy kernel
392	 * provided on-stack trampoline.  Conversely, if we are using a
393	 * non-0 ABI version, we must have a trampoline.  Only validate the
394	 * vers if a new sigaction was supplied. Emulations use legacy
395	 * kernel trampolines with version 0, alternatively check for that
396	 * too.
397	 *
398	 * If version < 2, we try to autoload the compat module.  Note
399	 * that we interlock with the unload check in compat_modcmd()
400	 * using kernconfig_lock.  If the autoload fails, we don't try it
401	 * again for this process.
402	 */
403	if (nsa != NULL) {
404		if (__predict_false(vers < 2) &&
405		    (p->p_lflag & PL_SIGCOMPAT) == 0) {
406			kernconfig_lock();
407			if (sendsig_sigcontext_vec == NULL) {
408				(void)module_autoload("compat",
409				    MODULE_CLASS_ANY);
410			}
411			if (sendsig_sigcontext_vec != NULL) {
412				/*
413				 * We need to remember if the
414				 * sigcontext method may be useable,
415				 * because libc may use it even
416				 * if siginfo is available.
417				 */
418				v0v1valid = true;
419			}
420			mutex_enter(proc_lock);
421			/*
422			 * Prevent unload of compat module while
423			 * this process remains.
424			 */
425			p->p_lflag |= PL_SIGCOMPAT;
426			mutex_exit(proc_lock);
427			kernconfig_unlock();
428		}
429
430		switch (vers) {
431		case 0:
432			/* sigcontext, kernel supplied trampoline. */
433			if (tramp != NULL || !v0v1valid) {
434				return EINVAL;
435			}
436			break;
437		case 1:
438			/* sigcontext, user supplied trampoline. */
439			if (tramp == NULL || !v0v1valid) {
440				return EINVAL;
441			}
442			break;
443		case 2:
444		case 3:
445			/* siginfo, user supplied trampoline. */
446			if (tramp == NULL) {
447				return EINVAL;
448			}
449			break;
450		default:
451			return EINVAL;
452		}
453	}
454
455	mutex_enter(p->p_lock);
456
457	ps = p->p_sigacts;
458	if (osa)
459		*osa = SIGACTION_PS(ps, signum);
460	if (!nsa)
461		goto out;
462
463	prop = sigprop[signum];
464	if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
465		error = EINVAL;
466		goto out;
467	}
468
469	SIGACTION_PS(ps, signum) = *nsa;
470	ps->sa_sigdesc[signum].sd_tramp = tramp;
471	ps->sa_sigdesc[signum].sd_vers = vers;
472	sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
473
474	if ((prop & SA_NORESET) != 0)
475		SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
476
477	if (signum == SIGCHLD) {
478		if (nsa->sa_flags & SA_NOCLDSTOP)
479			p->p_sflag |= PS_NOCLDSTOP;
480		else
481			p->p_sflag &= ~PS_NOCLDSTOP;
482		if (nsa->sa_flags & SA_NOCLDWAIT) {
483			/*
484			 * Paranoia: since SA_NOCLDWAIT is implemented by
485			 * reparenting the dying child to PID 1 (and trust
486			 * it to reap the zombie), PID 1 itself is forbidden
487			 * to set SA_NOCLDWAIT.
488			 */
489			if (p->p_pid == 1)
490				p->p_flag &= ~PK_NOCLDWAIT;
491			else
492				p->p_flag |= PK_NOCLDWAIT;
493		} else
494			p->p_flag &= ~PK_NOCLDWAIT;
495
496		if (nsa->sa_handler == SIG_IGN) {
497			/*
498			 * Paranoia: same as above.
499			 */
500			if (p->p_pid == 1)
501				p->p_flag &= ~PK_CLDSIGIGN;
502			else
503				p->p_flag |= PK_CLDSIGIGN;
504		} else
505			p->p_flag &= ~PK_CLDSIGIGN;
506	}
507
508	if ((nsa->sa_flags & SA_NODEFER) == 0)
509		sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
510	else
511		sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
512
513	/*
514	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
515	 * SIG_IGN, and for signals set to SIG_DFL where the default is to
516	 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
517	 * we have to restart the process.
518	 */
519	if (nsa->sa_handler == SIG_IGN ||
520	    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
521		/* Never to be seen again. */
522		sigemptyset(&tset);
523		sigaddset(&tset, signum);
524		sigclearall(p, &tset, &kq);
525		if (signum != SIGCONT) {
526			/* Easier in psignal */
527			sigaddset(&p->p_sigctx.ps_sigignore, signum);
528		}
529		sigdelset(&p->p_sigctx.ps_sigcatch, signum);
530	} else {
531		sigdelset(&p->p_sigctx.ps_sigignore, signum);
532		if (nsa->sa_handler == SIG_DFL)
533			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
534		else
535			sigaddset(&p->p_sigctx.ps_sigcatch, signum);
536	}
537
538	/*
539	 * Previously held signals may now have become visible.  Ensure that
540	 * we check for them before returning to userspace.
541	 */
542	if (sigispending(l, 0)) {
543		lwp_lock(l);
544		l->l_flag |= LW_PENDSIG;
545		lwp_unlock(l);
546	}
547out:
548	mutex_exit(p->p_lock);
549	ksiginfo_queue_drain(&kq);
550
551	return error;
552}
553
554int
555sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
556{
557	int more;
558	struct proc *p = l->l_proc;
559	sigset_t *mask;
560	mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask;
561
562	KASSERT(mutex_owned(p->p_lock));
563
564	if (oss)
565		*oss = *mask;
566	if (nss) {
567		switch (how) {
568		case SIG_BLOCK:
569			sigplusset(nss, mask);
570			more = 0;
571			break;
572		case SIG_UNBLOCK:
573			sigminusset(nss, mask);
574			more = 1;
575			break;
576		case SIG_SETMASK:
577			*mask = *nss;
578			more = 1;
579			break;
580		default:
581			return (EINVAL);
582		}
583		sigminusset(&sigcantmask, mask);
584		if (more && sigispending(l, 0)) {
585			/*
586			 * Check for pending signals on return to user.
587			 */
588			lwp_lock(l);
589			l->l_flag |= LW_PENDSIG;
590			lwp_unlock(l);
591		}
592	}
593
594	return 0;
595}
596
597void
598sigpending1(struct lwp *l, sigset_t *ss)
599{
600	struct proc *p = l->l_proc;
601
602	mutex_enter(p->p_lock);
603	*ss = l->l_sigpend.sp_set;
604	sigplusset(&p->p_sigpend.sp_set, ss);
605	mutex_exit(p->p_lock);
606}
607
608void
609sigsuspendsetup(struct lwp *l, const sigset_t *ss)
610{
611	struct proc *p = l->l_proc;
612
613	/*
614	 * When returning from sigsuspend/pselect/pollts, we want
615	 * the old mask to be restored after the
616	 * signal handler has finished.  Thus, we
617	 * save it here and mark the sigctx structure
618	 * to indicate this.
619	 */
620	mutex_enter(p->p_lock);
621	l->l_sigrestore = 1;
622	l->l_sigoldmask = l->l_sigmask;
623	l->l_sigmask = *ss;
624	sigminusset(&sigcantmask, &l->l_sigmask);
625
626	/* Check for pending signals when sleeping. */
627	if (sigispending(l, 0)) {
628		lwp_lock(l);
629		l->l_flag |= LW_PENDSIG;
630		lwp_unlock(l);
631	}
632	mutex_exit(p->p_lock);
633}
634
635void
636sigsuspendteardown(struct lwp *l)
637{
638	struct proc *p = l->l_proc;
639
640	mutex_enter(p->p_lock);
641	/* Check for pending signals when sleeping. */
642	if (l->l_sigrestore) {
643		if (sigispending(l, 0)) {
644			lwp_lock(l);
645			l->l_flag |= LW_PENDSIG;
646			lwp_unlock(l);
647		} else {
648			l->l_sigrestore = 0;
649			l->l_sigmask = l->l_sigoldmask;
650		}
651	}
652	mutex_exit(p->p_lock);
653}
654
655int
656sigsuspend1(struct lwp *l, const sigset_t *ss)
657{
658
659	if (ss)
660		sigsuspendsetup(l, ss);
661
662	while (kpause("pause", true, 0, NULL) == 0)
663		;
664
665	/* always return EINTR rather than ERESTART... */
666	return EINTR;
667}
668
669int
670sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
671    struct sigaltstack *oss)
672{
673	struct proc *p = l->l_proc;
674	int error = 0;
675
676	mutex_enter(p->p_lock);
677
678	if (oss)
679		*oss = l->l_sigstk;
680
681	if (nss) {
682		if (nss->ss_flags & ~SS_ALLBITS)
683			error = EINVAL;
684		else if (nss->ss_flags & SS_DISABLE) {
685			if (l->l_sigstk.ss_flags & SS_ONSTACK)
686				error = EINVAL;
687		} else if (nss->ss_size < MINSIGSTKSZ)
688			error = ENOMEM;
689
690		if (!error)
691			l->l_sigstk = *nss;
692	}
693
694	mutex_exit(p->p_lock);
695
696	return error;
697}
698
699int
700sigtimedwait1(struct lwp *l, const struct sys_____sigtimedwait50_args *uap,
701    register_t *retval, copyin_t fetchss, copyout_t storeinf, copyin_t fetchts,
702    copyout_t storets)
703{
704	/* {
705		syscallarg(const sigset_t *) set;
706		syscallarg(siginfo_t *) info;
707		syscallarg(struct timespec *) timeout;
708	} */
709	struct proc *p = l->l_proc;
710	int error, signum, timo;
711	struct timespec ts, tsstart, tsnow;
712	ksiginfo_t ksi;
713
714	/*
715	 * Calculate timeout, if it was specified.
716	 */
717	if (SCARG(uap, timeout)) {
718		error = (*fetchts)(SCARG(uap, timeout), &ts, sizeof(ts));
719		if (error)
720			return error;
721
722		if ((error = itimespecfix(&ts)) != 0)
723			return error;
724
725		timo = tstohz(&ts);
726		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec != 0)
727			timo++;
728
729		/*
730		 * Remember current uptime, it would be used in
731		 * ECANCELED/ERESTART case.
732		 */
733		getnanouptime(&tsstart);
734	} else {
735		memset(&tsstart, 0, sizeof(tsstart)); /* XXXgcc */
736		timo = 0;
737	}
738
739	error = (*fetchss)(SCARG(uap, set), &l->l_sigwaitset,
740	    sizeof(l->l_sigwaitset));
741	if (error)
742		return error;
743
744	/*
745	 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
746	 * SA_CANTMASK signals in waitset, we do this only for the below
747	 * siglist check.
748	 */
749	sigminusset(&sigcantmask, &l->l_sigwaitset);
750
751	mutex_enter(p->p_lock);
752
753	/* SA processes can have no more than 1 sigwaiter. */
754	if ((p->p_sflag & PS_SA) != 0 && !LIST_EMPTY(&p->p_sigwaiters)) {
755		mutex_exit(p->p_lock);
756		error = EINVAL;
757		goto out;
758	}
759
760	/* Check for pending signals in the process, if no - then in LWP. */
761	if ((signum = sigget(&p->p_sigpend, &ksi, 0, &l->l_sigwaitset)) == 0)
762		signum = sigget(&l->l_sigpend, &ksi, 0, &l->l_sigwaitset);
763
764	if (signum != 0) {
765		/* If found a pending signal, just copy it out to the user. */
766		mutex_exit(p->p_lock);
767		goto out;
768	}
769
770	/*
771	 * Set up the sigwait list and wait for signal to arrive.
772	 * We can either be woken up or time out.
773	 */
774	l->l_sigwaited = &ksi;
775	LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
776	error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
777
778	/*
779	 * Need to find out if we woke as a result of _lwp_wakeup() or a
780	 * signal outside our wait set.
781	 */
782	if (l->l_sigwaited != NULL) {
783		if (error == EINTR) {
784			/* Wakeup via _lwp_wakeup(). */
785			error = ECANCELED;
786		} else if (!error) {
787			/* Spurious wakeup - arrange for syscall restart. */
788			error = ERESTART;
789		}
790		l->l_sigwaited = NULL;
791		LIST_REMOVE(l, l_sigwaiter);
792	}
793	mutex_exit(p->p_lock);
794
795	/*
796	 * If the sleep was interrupted (either by signal or wakeup), update
797	 * the timeout and copyout new value back.  It would be used when
798	 * the syscall would be restarted or called again.
799	 */
800	if (timo && (error == ERESTART || error == ECANCELED)) {
801		getnanouptime(&tsnow);
802
803		/* Compute how much time has passed since start. */
804		timespecsub(&tsnow, &tsstart, &tsnow);
805
806		/* Substract passed time from timeout. */
807		timespecsub(&ts, &tsnow, &ts);
808
809		if (ts.tv_sec < 0)
810			error = EAGAIN;
811		else {
812			/* Copy updated timeout to userland. */
813			error = (*storets)(&ts, SCARG(uap, timeout),
814			    sizeof(ts));
815		}
816	}
817out:
818	/*
819	 * If a signal from the wait set arrived, copy it to userland.
820	 * Copy only the used part of siginfo, the padding part is
821	 * left unchanged (userland is not supposed to touch it anyway).
822	 */
823	if (error == 0 && SCARG(uap, info)) {
824		error = (*storeinf)(&ksi.ksi_info, SCARG(uap, info),
825		    sizeof(ksi.ksi_info));
826	}
827	if (error == 0)
828		*retval = ksi.ksi_info._signo;
829	return error;
830}
831