sys_sig.c revision 1.41 1 /* $NetBSD: sys_sig.c,v 1.41 2013/03/08 09:32:59 apb 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.41 2013/03/08 09:32:59 apb 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/syscallargs.h>
77 #include <sys/kauth.h>
78 #include <sys/wait.h>
79 #include <sys/kmem.h>
80 #include <sys/module.h>
81
82 int
83 sys___sigaction_sigtramp(struct lwp *l,
84 const struct sys___sigaction_sigtramp_args *uap, register_t *retval)
85 {
86 /* {
87 syscallarg(int) signum;
88 syscallarg(const struct sigaction *) nsa;
89 syscallarg(struct sigaction *) osa;
90 syscallarg(void *) tramp;
91 syscallarg(int) vers;
92 } */
93 struct sigaction nsa, osa;
94 int error;
95
96 if (SCARG(uap, nsa)) {
97 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
98 if (error)
99 return (error);
100 }
101 error = sigaction1(l, SCARG(uap, signum),
102 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
103 SCARG(uap, tramp), SCARG(uap, vers));
104 if (error)
105 return (error);
106 if (SCARG(uap, osa)) {
107 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
108 if (error)
109 return (error);
110 }
111 return 0;
112 }
113
114 /*
115 * Manipulate signal mask. Note that we receive new mask, not pointer, and
116 * return old mask as return value; the library stub does the rest.
117 */
118 int
119 sys___sigprocmask14(struct lwp *l, const struct sys___sigprocmask14_args *uap,
120 register_t *retval)
121 {
122 /* {
123 syscallarg(int) how;
124 syscallarg(const sigset_t *) set;
125 syscallarg(sigset_t *) oset;
126 } */
127 struct proc *p = l->l_proc;
128 sigset_t nss, oss;
129 int error;
130
131 if (SCARG(uap, set)) {
132 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
133 if (error)
134 return error;
135 }
136 mutex_enter(p->p_lock);
137 error = sigprocmask1(l, SCARG(uap, how),
138 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
139 mutex_exit(p->p_lock);
140 if (error)
141 return error;
142 if (SCARG(uap, oset)) {
143 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
144 if (error)
145 return error;
146 }
147 return 0;
148 }
149
150 int
151 sys___sigpending14(struct lwp *l, const struct sys___sigpending14_args *uap,
152 register_t *retval)
153 {
154 /* {
155 syscallarg(sigset_t *) set;
156 } */
157 sigset_t ss;
158
159 sigpending1(l, &ss);
160 return copyout(&ss, SCARG(uap, set), sizeof(ss));
161 }
162
163 /*
164 * Suspend process until signal, providing mask to be set in the meantime.
165 * Note nonstandard calling convention: libc stub passes mask, not pointer,
166 * to save a copyin.
167 */
168 int
169 sys___sigsuspend14(struct lwp *l, const struct sys___sigsuspend14_args *uap,
170 register_t *retval)
171 {
172 /* {
173 syscallarg(const sigset_t *) set;
174 } */
175 sigset_t ss;
176 int error;
177
178 if (SCARG(uap, set)) {
179 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
180 if (error)
181 return error;
182 }
183 return sigsuspend1(l, SCARG(uap, set) ? &ss : 0);
184 }
185
186 int
187 sys___sigaltstack14(struct lwp *l, const struct sys___sigaltstack14_args *uap,
188 register_t *retval)
189 {
190 /* {
191 syscallarg(const struct sigaltstack *) nss;
192 syscallarg(struct sigaltstack *) oss;
193 } */
194 struct sigaltstack nss, oss;
195 int error;
196
197 if (SCARG(uap, nss)) {
198 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
199 if (error)
200 return error;
201 }
202 error = sigaltstack1(l,
203 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
204 if (error)
205 return error;
206 if (SCARG(uap, oss)) {
207 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
208 if (error)
209 return error;
210 }
211 return 0;
212 }
213
214
215 static int
216 kill1(struct lwp *l, pid_t pid, ksiginfo_t *ksi, register_t *retval)
217 {
218 int error;
219 struct proc *p;
220
221 if ((u_int)ksi->ksi_signo >= NSIG)
222 return EINVAL;
223
224 if (pid != l->l_proc->p_pid) {
225 if (ksi->ksi_pid != l->l_proc->p_pid)
226 return EPERM;
227
228 if (ksi->ksi_uid != kauth_cred_geteuid(l->l_cred))
229 return EPERM;
230
231 switch (ksi->ksi_code) {
232 case SI_USER:
233 case SI_QUEUE:
234 break;
235 default:
236 return EPERM;
237 }
238 }
239
240 if (pid > 0) {
241 /* kill single process */
242 mutex_enter(proc_lock);
243 p = proc_find_raw(pid);
244 if (p == NULL || (p->p_stat != SACTIVE && p->p_stat != SSTOP)) {
245 mutex_exit(proc_lock);
246 /* IEEE Std 1003.1-2001: return success for zombies */
247 return p ? 0 : ESRCH;
248 }
249 mutex_enter(p->p_lock);
250 error = kauth_authorize_process(l->l_cred,
251 KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(ksi->ksi_signo),
252 NULL, NULL);
253 if (!error && ksi->ksi_signo) {
254 kpsignal2(p, ksi);
255 }
256 mutex_exit(p->p_lock);
257 mutex_exit(proc_lock);
258 return error;
259 }
260
261 switch (pid) {
262 case -1: /* broadcast signal */
263 return killpg1(l, ksi, 0, 1);
264 case 0: /* signal own process group */
265 return killpg1(l, ksi, 0, 0);
266 default: /* negative explicit process group */
267 return killpg1(l, ksi, -pid, 0);
268 }
269 /* NOTREACHED */
270 }
271
272 int
273 sys_sigqueueinfo(struct lwp *l, const struct sys_sigqueueinfo_args *uap,
274 register_t *retval)
275 {
276 /* {
277 syscallarg(pid_t int) pid;
278 syscallarg(const siginfo_t *) info;
279 } */
280 ksiginfo_t ksi;
281 int error;
282
283 KSI_INIT(&ksi);
284
285 if ((error = copyin(&SCARG(uap, info)->_info, &ksi.ksi_info,
286 sizeof(ksi.ksi_info))) != 0)
287 return error;
288
289 return kill1(l, SCARG(uap, pid), &ksi, retval);
290 }
291
292 int
293 sys_kill(struct lwp *l, const struct sys_kill_args *uap, register_t *retval)
294 {
295 /* {
296 syscallarg(pid_t) pid;
297 syscallarg(int) signum;
298 } */
299 ksiginfo_t ksi;
300
301 KSI_INIT(&ksi);
302
303 ksi.ksi_signo = SCARG(uap, signum);
304 ksi.ksi_code = SI_USER;
305 ksi.ksi_pid = l->l_proc->p_pid;
306 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
307
308 return kill1(l, SCARG(uap, pid), &ksi, retval);
309 }
310
311 int
312 sys_getcontext(struct lwp *l, const struct sys_getcontext_args *uap,
313 register_t *retval)
314 {
315 /* {
316 syscallarg(struct __ucontext *) ucp;
317 } */
318 struct proc *p = l->l_proc;
319 ucontext_t uc;
320
321 memset(&uc, 0, sizeof(uc));
322
323 mutex_enter(p->p_lock);
324 getucontext(l, &uc);
325 mutex_exit(p->p_lock);
326
327 return copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)));
328 }
329
330 int
331 sys_setcontext(struct lwp *l, const struct sys_setcontext_args *uap,
332 register_t *retval)
333 {
334 /* {
335 syscallarg(const ucontext_t *) ucp;
336 } */
337 struct proc *p = l->l_proc;
338 ucontext_t uc;
339 int error;
340
341 error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
342 if (error)
343 return error;
344 if ((uc.uc_flags & _UC_CPU) == 0)
345 return EINVAL;
346 mutex_enter(p->p_lock);
347 error = setucontext(l, &uc);
348 mutex_exit(p->p_lock);
349 if (error)
350 return error;
351
352 return EJUSTRETURN;
353 }
354
355 /*
356 * sigtimedwait(2) system call, used also for implementation
357 * of sigwaitinfo() and sigwait().
358 *
359 * This only handles single LWP in signal wait. libpthread provides
360 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
361 */
362 int
363 sys_____sigtimedwait50(struct lwp *l,
364 const struct sys_____sigtimedwait50_args *uap, register_t *retval)
365 {
366
367 return sigtimedwait1(l, uap, retval, copyin, copyout, copyin, copyout);
368 }
369
370 int
371 sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
372 struct sigaction *osa, const void *tramp, int vers)
373 {
374 struct proc *p;
375 struct sigacts *ps;
376 sigset_t tset;
377 int prop, error;
378 ksiginfoq_t kq;
379 static bool v0v1valid;
380
381 if (signum <= 0 || signum >= NSIG)
382 return EINVAL;
383
384 p = l->l_proc;
385 error = 0;
386 ksiginfo_queue_init(&kq);
387
388 /*
389 * Trampoline ABI version 0 is reserved for the legacy kernel
390 * provided on-stack trampoline. Conversely, if we are using a
391 * non-0 ABI version, we must have a trampoline. Only validate the
392 * vers if a new sigaction was supplied. Emulations use legacy
393 * kernel trampolines with version 0, alternatively check for that
394 * too.
395 *
396 * If version < 2, we try to autoload the compat module. Note
397 * that we interlock with the unload check in compat_modcmd()
398 * using kernconfig_lock. If the autoload fails, we don't try it
399 * again for this process.
400 */
401 if (nsa != NULL) {
402 if (__predict_false(vers < 2)) {
403 if (p->p_flag & PK_32)
404 v0v1valid = true;
405 else if ((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
431 switch (vers) {
432 case 0:
433 /* sigcontext, kernel supplied trampoline. */
434 if (tramp != NULL || !v0v1valid) {
435 return EINVAL;
436 }
437 break;
438 case 1:
439 /* sigcontext, user supplied trampoline. */
440 if (tramp == NULL || !v0v1valid) {
441 return EINVAL;
442 }
443 break;
444 case 2:
445 case 3:
446 /* siginfo, user supplied trampoline. */
447 if (tramp == NULL) {
448 return EINVAL;
449 }
450 break;
451 default:
452 return EINVAL;
453 }
454 }
455
456 mutex_enter(p->p_lock);
457
458 ps = p->p_sigacts;
459 if (osa)
460 *osa = SIGACTION_PS(ps, signum);
461 if (!nsa)
462 goto out;
463
464 prop = sigprop[signum];
465 if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
466 error = EINVAL;
467 goto out;
468 }
469
470 SIGACTION_PS(ps, signum) = *nsa;
471 ps->sa_sigdesc[signum].sd_tramp = tramp;
472 ps->sa_sigdesc[signum].sd_vers = vers;
473 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
474
475 if ((prop & SA_NORESET) != 0)
476 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
477
478 if (signum == SIGCHLD) {
479 if (nsa->sa_flags & SA_NOCLDSTOP)
480 p->p_sflag |= PS_NOCLDSTOP;
481 else
482 p->p_sflag &= ~PS_NOCLDSTOP;
483 if (nsa->sa_flags & SA_NOCLDWAIT) {
484 /*
485 * Paranoia: since SA_NOCLDWAIT is implemented by
486 * reparenting the dying child to PID 1 (and trust
487 * it to reap the zombie), PID 1 itself is forbidden
488 * to set SA_NOCLDWAIT.
489 */
490 if (p->p_pid == 1)
491 p->p_flag &= ~PK_NOCLDWAIT;
492 else
493 p->p_flag |= PK_NOCLDWAIT;
494 } else
495 p->p_flag &= ~PK_NOCLDWAIT;
496
497 if (nsa->sa_handler == SIG_IGN) {
498 /*
499 * Paranoia: same as above.
500 */
501 if (p->p_pid == 1)
502 p->p_flag &= ~PK_CLDSIGIGN;
503 else
504 p->p_flag |= PK_CLDSIGIGN;
505 } else
506 p->p_flag &= ~PK_CLDSIGIGN;
507 }
508
509 if ((nsa->sa_flags & SA_NODEFER) == 0)
510 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
511 else
512 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
513
514 /*
515 * Set bit in p_sigctx.ps_sigignore for signals that are set to
516 * SIG_IGN, and for signals set to SIG_DFL where the default is to
517 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
518 * we have to restart the process.
519 */
520 if (nsa->sa_handler == SIG_IGN ||
521 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
522 /* Never to be seen again. */
523 sigemptyset(&tset);
524 sigaddset(&tset, signum);
525 sigclearall(p, &tset, &kq);
526 if (signum != SIGCONT) {
527 /* Easier in psignal */
528 sigaddset(&p->p_sigctx.ps_sigignore, signum);
529 }
530 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
531 } else {
532 sigdelset(&p->p_sigctx.ps_sigignore, signum);
533 if (nsa->sa_handler == SIG_DFL)
534 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
535 else
536 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
537 }
538
539 /*
540 * Previously held signals may now have become visible. Ensure that
541 * we check for them before returning to userspace.
542 */
543 if (sigispending(l, 0)) {
544 lwp_lock(l);
545 l->l_flag |= LW_PENDSIG;
546 lwp_unlock(l);
547 }
548 out:
549 mutex_exit(p->p_lock);
550 ksiginfo_queue_drain(&kq);
551
552 return error;
553 }
554
555 int
556 sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
557 {
558 sigset_t *mask = &l->l_sigmask;
559 bool more;
560
561 KASSERT(mutex_owned(l->l_proc->p_lock));
562
563 if (oss) {
564 *oss = *mask;
565 }
566
567 if (nss == NULL) {
568 return 0;
569 }
570
571 switch (how) {
572 case SIG_BLOCK:
573 sigplusset(nss, mask);
574 more = false;
575 break;
576 case SIG_UNBLOCK:
577 sigminusset(nss, mask);
578 more = true;
579 break;
580 case SIG_SETMASK:
581 *mask = *nss;
582 more = true;
583 break;
584 default:
585 return EINVAL;
586 }
587 sigminusset(&sigcantmask, mask);
588 if (more && sigispending(l, 0)) {
589 /*
590 * Check for pending signals on return to user.
591 */
592 lwp_lock(l);
593 l->l_flag |= LW_PENDSIG;
594 lwp_unlock(l);
595 }
596 return 0;
597 }
598
599 void
600 sigpending1(struct lwp *l, sigset_t *ss)
601 {
602 struct proc *p = l->l_proc;
603
604 mutex_enter(p->p_lock);
605 *ss = l->l_sigpend.sp_set;
606 sigplusset(&p->p_sigpend.sp_set, ss);
607 mutex_exit(p->p_lock);
608 }
609
610 void
611 sigsuspendsetup(struct lwp *l, const sigset_t *ss)
612 {
613 struct proc *p = l->l_proc;
614
615 /*
616 * When returning from sigsuspend/pselect/pollts, we want
617 * the old mask to be restored after the
618 * signal handler has finished. Thus, we
619 * save it here and mark the sigctx structure
620 * to indicate this.
621 */
622 mutex_enter(p->p_lock);
623 l->l_sigrestore = 1;
624 l->l_sigoldmask = l->l_sigmask;
625 l->l_sigmask = *ss;
626 sigminusset(&sigcantmask, &l->l_sigmask);
627
628 /* Check for pending signals when sleeping. */
629 if (sigispending(l, 0)) {
630 lwp_lock(l);
631 l->l_flag |= LW_PENDSIG;
632 lwp_unlock(l);
633 }
634 mutex_exit(p->p_lock);
635 }
636
637 void
638 sigsuspendteardown(struct lwp *l)
639 {
640 struct proc *p = l->l_proc;
641
642 mutex_enter(p->p_lock);
643 /* Check for pending signals when sleeping. */
644 if (l->l_sigrestore) {
645 if (sigispending(l, 0)) {
646 lwp_lock(l);
647 l->l_flag |= LW_PENDSIG;
648 lwp_unlock(l);
649 } else {
650 l->l_sigrestore = 0;
651 l->l_sigmask = l->l_sigoldmask;
652 }
653 }
654 mutex_exit(p->p_lock);
655 }
656
657 int
658 sigsuspend1(struct lwp *l, const sigset_t *ss)
659 {
660
661 if (ss)
662 sigsuspendsetup(l, ss);
663
664 while (kpause("pause", true, 0, NULL) == 0)
665 ;
666
667 /* always return EINTR rather than ERESTART... */
668 return EINTR;
669 }
670
671 int
672 sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
673 struct sigaltstack *oss)
674 {
675 struct proc *p = l->l_proc;
676 int error = 0;
677
678 mutex_enter(p->p_lock);
679
680 if (oss)
681 *oss = l->l_sigstk;
682
683 if (nss) {
684 if (nss->ss_flags & ~SS_ALLBITS)
685 error = EINVAL;
686 else if (nss->ss_flags & SS_DISABLE) {
687 if (l->l_sigstk.ss_flags & SS_ONSTACK)
688 error = EINVAL;
689 } else if (nss->ss_size < MINSIGSTKSZ)
690 error = ENOMEM;
691
692 if (!error)
693 l->l_sigstk = *nss;
694 }
695
696 mutex_exit(p->p_lock);
697
698 return error;
699 }
700
701 int
702 sigtimedwait1(struct lwp *l, const struct sys_____sigtimedwait50_args *uap,
703 register_t *retval, copyin_t fetchss, copyout_t storeinf, copyin_t fetchts,
704 copyout_t storets)
705 {
706 /* {
707 syscallarg(const sigset_t *) set;
708 syscallarg(siginfo_t *) info;
709 syscallarg(struct timespec *) timeout;
710 } */
711 struct proc *p = l->l_proc;
712 int error, signum, timo;
713 struct timespec ts, tsstart, tsnow;
714 ksiginfo_t ksi;
715
716 /*
717 * Calculate timeout, if it was specified.
718 *
719 * NULL pointer means an infinite timeout.
720 * {.tv_sec = 0, .tv_nsec = 0} means do not block.
721 */
722 if (SCARG(uap, timeout)) {
723 error = (*fetchts)(SCARG(uap, timeout), &ts, sizeof(ts));
724 if (error)
725 return error;
726
727 if ((error = itimespecfix(&ts)) != 0)
728 return error;
729
730 timo = tstohz(&ts);
731 if (timo == 0) {
732 if (ts.tv_sec == 0 && ts.tv_nsec == 0)
733 timo = -1; /* do not block */
734 else
735 timo = 1; /* the shortest possible timeout */
736 }
737
738 /*
739 * Remember current uptime, it would be used in
740 * ECANCELED/ERESTART case.
741 */
742 getnanouptime(&tsstart);
743 } else {
744 memset(&tsstart, 0, sizeof(tsstart)); /* XXXgcc */
745 timo = 0; /* infinite timeout */
746 }
747
748 error = (*fetchss)(SCARG(uap, set), &l->l_sigwaitset,
749 sizeof(l->l_sigwaitset));
750 if (error)
751 return error;
752
753 /*
754 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
755 * SA_CANTMASK signals in waitset, we do this only for the below
756 * siglist check.
757 */
758 sigminusset(&sigcantmask, &l->l_sigwaitset);
759
760 mutex_enter(p->p_lock);
761
762 /* Check for pending signals in the process, if no - then in LWP. */
763 if ((signum = sigget(&p->p_sigpend, &ksi, 0, &l->l_sigwaitset)) == 0)
764 signum = sigget(&l->l_sigpend, &ksi, 0, &l->l_sigwaitset);
765
766 if (signum != 0) {
767 /* If found a pending signal, just copy it out to the user. */
768 mutex_exit(p->p_lock);
769 goto out;
770 }
771
772 if (timo < 0) {
773 /* If not allowed to block, return an error */
774 mutex_exit(p->p_lock);
775 return EAGAIN;
776 }
777
778 /*
779 * Set up the sigwait list and wait for signal to arrive.
780 * We can either be woken up or time out.
781 */
782 l->l_sigwaited = &ksi;
783 LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
784 error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
785
786 /*
787 * Need to find out if we woke as a result of _lwp_wakeup() or a
788 * signal outside our wait set.
789 */
790 if (l->l_sigwaited != NULL) {
791 if (error == EINTR) {
792 /* Wakeup via _lwp_wakeup(). */
793 error = ECANCELED;
794 } else if (!error) {
795 /* Spurious wakeup - arrange for syscall restart. */
796 error = ERESTART;
797 }
798 l->l_sigwaited = NULL;
799 LIST_REMOVE(l, l_sigwaiter);
800 }
801 mutex_exit(p->p_lock);
802
803 /*
804 * If the sleep was interrupted (either by signal or wakeup), update
805 * the timeout and copyout new value back. It would be used when
806 * the syscall would be restarted or called again.
807 */
808 if (timo && (error == ERESTART || error == ECANCELED)) {
809 getnanouptime(&tsnow);
810
811 /* Compute how much time has passed since start. */
812 timespecsub(&tsnow, &tsstart, &tsnow);
813
814 /* Substract passed time from timeout. */
815 timespecsub(&ts, &tsnow, &ts);
816
817 if (ts.tv_sec < 0)
818 error = EAGAIN;
819 else {
820 /* Copy updated timeout to userland. */
821 error = (*storets)(&ts, SCARG(uap, timeout),
822 sizeof(ts));
823 }
824 }
825 out:
826 /*
827 * If a signal from the wait set arrived, copy it to userland.
828 * Copy only the used part of siginfo, the padding part is
829 * left unchanged (userland is not supposed to touch it anyway).
830 */
831 if (error == 0 && SCARG(uap, info)) {
832 error = (*storeinf)(&ksi.ksi_info, SCARG(uap, info),
833 sizeof(ksi.ksi_info));
834 }
835 if (error == 0)
836 *retval = ksi.ksi_info._signo;
837 return error;
838 }
839