sys_sig.c revision 1.2 1 /* $NetBSD: sys_sig.c,v 1.2 2007/02/09 21:55:31 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2006 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.2 2007/02/09 21:55:31 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, void *v, register_t *retval)
96 {
97 struct compat_16_sys___sigaction14_args /* {
98 syscallarg(int) signum;
99 syscallarg(const struct sigaction *) nsa;
100 syscallarg(struct sigaction *) osa;
101 } */ *uap = v;
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, void *v, register_t *retval)
127 {
128 struct sys___sigaction_sigtramp_args /* {
129 syscallarg(int) signum;
130 syscallarg(const struct sigaction *) nsa;
131 syscallarg(struct sigaction *) osa;
132 syscallarg(void *) tramp;
133 syscallarg(int) vers;
134 } */ *uap = v;
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, void *v, register_t *retval)
162 {
163 struct sys___sigprocmask14_args /* {
164 syscallarg(int) how;
165 syscallarg(const sigset_t *) set;
166 syscallarg(sigset_t *) oset;
167 } */ *uap = v;
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_smutex);
178 error = sigprocmask1(l, SCARG(uap, how),
179 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
180 mutex_exit(&p->p_smutex);
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, void *v, register_t *retval)
194 {
195 struct sys___sigpending14_args /* {
196 syscallarg(sigset_t *) set;
197 } */ *uap = v;
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, void *v, register_t *retval)
212 {
213 struct sys___sigsuspend14_args /* {
214 syscallarg(const sigset_t *) set;
215 } */ *uap = v;
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, void *v, register_t *retval)
231 {
232 struct sys___sigaltstack14_args /* {
233 syscallarg(const struct sigaltstack *) nss;
234 syscallarg(struct sigaltstack *) oss;
235 } */ *uap = v;
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, void *v, register_t *retval)
259 {
260 struct sys_kill_args /* {
261 syscallarg(int) pid;
262 syscallarg(int) signum;
263 } */ *uap = v;
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 if ((p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL)) == NULL)
279 return (ESRCH);
280 mutex_enter(&p->p_mutex);
281 error = kauth_authorize_process(l->l_cred,
282 KAUTH_PROCESS_CANSIGNAL, p, (void *)(uintptr_t)signum,
283 NULL, NULL);
284 if (!error && signum) {
285 mutex_enter(&proclist_mutex);
286 mutex_enter(&p->p_smutex);
287 kpsignal2(p, &ksi);
288 mutex_exit(&p->p_smutex);
289 mutex_exit(&proclist_mutex);
290 }
291 mutex_exit(&p->p_mutex);
292 rw_exit(&proclist_lock);
293 return (error);
294 }
295 switch (SCARG(uap, pid)) {
296 case -1: /* broadcast signal */
297 return (killpg1(l, &ksi, 0, 1));
298 case 0: /* signal own process group */
299 return (killpg1(l, &ksi, 0, 0));
300 default: /* negative explicit process group */
301 return (killpg1(l, &ksi, -SCARG(uap, pid), 0));
302 }
303 /* NOTREACHED */
304 }
305
306 /*
307 * Nonexistent system call-- signal process (may want to handle it). Flag
308 * error in case process won't see signal immediately (blocked or ignored).
309 *
310 * XXX This should not be here.
311 */
312 #ifndef PTRACE
313 __weak_alias(sys_ptrace, sys_nosys);
314 #endif
315
316 /* ARGSUSED */
317 int
318 sys_nosys(struct lwp *l, void *v, register_t *retval)
319 {
320
321 mutex_enter(&proclist_mutex);
322 psignal(l->l_proc, SIGSYS);
323 mutex_exit(&proclist_mutex);
324 return (ENOSYS);
325 }
326
327 /* ARGSUSED */
328 int
329 sys_getcontext(struct lwp *l, void *v, register_t *retval)
330 {
331 struct sys_getcontext_args /* {
332 syscallarg(struct __ucontext *) ucp;
333 } */ *uap = v;
334 struct proc *p = l->l_proc;
335 ucontext_t uc;
336
337 mutex_enter(&p->p_smutex);
338 getucontext(l, &uc);
339 mutex_exit(&p->p_smutex);
340
341 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
342 }
343
344 /* ARGSUSED */
345 int
346 sys_setcontext(struct lwp *l, void *v, register_t *retval)
347 {
348 struct sys_setcontext_args /* {
349 syscallarg(const ucontext_t *) ucp;
350 } */ *uap = v;
351 struct proc *p = l->l_proc;
352 ucontext_t uc;
353 int error;
354
355 error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
356 if (error)
357 return (error);
358 if (!(uc.uc_flags & _UC_CPU))
359 return (EINVAL);
360 mutex_enter(&p->p_smutex);
361 error = setucontext(l, &uc);
362 mutex_exit(&p->p_smutex);
363 if (error)
364 return (error);
365
366 return (EJUSTRETURN);
367 }
368
369 /*
370 * sigtimedwait(2) system call, used also for implementation
371 * of sigwaitinfo() and sigwait().
372 *
373 * This only handles single LWP in signal wait. libpthread provides
374 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
375 */
376 int
377 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
378 {
379
380 return __sigtimedwait1(l, v, retval, copyout, copyin, copyout);
381 }
382
383 int
384 sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
385 struct sigaction *osa, const void *tramp, int vers)
386 {
387 struct proc *p;
388 struct sigacts *ps;
389 sigset_t tset;
390 int prop, error;
391 ksiginfoq_t kq;
392
393 if (signum <= 0 || signum >= NSIG)
394 return (EINVAL);
395
396 p = l->l_proc;
397 error = 0;
398 ksiginfo_queue_init(&kq);
399
400 /*
401 * Trampoline ABI version 0 is reserved for the legacy kernel
402 * provided on-stack trampoline. Conversely, if we are using a
403 * non-0 ABI version, we must have a trampoline. Only validate the
404 * vers if a new sigaction was supplied. Emulations use legacy
405 * kernel trampolines with version 0, alternatively check for that
406 * too.
407 */
408 if ((vers != 0 && tramp == NULL) ||
409 #ifdef SIGTRAMP_VALID
410 (nsa != NULL &&
411 ((vers == 0) ?
412 (p->p_emul->e_sigcode == NULL) :
413 !SIGTRAMP_VALID(vers))) ||
414 #endif
415 (vers == 0 && tramp != NULL)) {
416 return (EINVAL);
417 }
418
419 mutex_enter(&p->p_mutex); /* p_flag */
420 mutex_enter(&p->p_smutex);
421
422 ps = p->p_sigacts;
423 if (osa)
424 *osa = SIGACTION_PS(ps, signum);
425 if (!nsa)
426 goto out;
427
428 prop = sigprop[signum];
429 if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
430 error = EINVAL;
431 goto out;
432 }
433
434 SIGACTION_PS(ps, signum) = *nsa;
435 ps->sa_sigdesc[signum].sd_tramp = tramp;
436 ps->sa_sigdesc[signum].sd_vers = vers;
437 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
438
439 if ((prop & SA_NORESET) != 0)
440 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
441
442 if (signum == SIGCHLD) {
443 if (nsa->sa_flags & SA_NOCLDSTOP)
444 p->p_sflag |= PS_NOCLDSTOP;
445 else
446 p->p_sflag &= ~PS_NOCLDSTOP;
447 if (nsa->sa_flags & SA_NOCLDWAIT) {
448 /*
449 * Paranoia: since SA_NOCLDWAIT is implemented by
450 * reparenting the dying child to PID 1 (and trust
451 * it to reap the zombie), PID 1 itself is forbidden
452 * to set SA_NOCLDWAIT.
453 */
454 if (p->p_pid == 1)
455 p->p_flag &= ~P_NOCLDWAIT;
456 else
457 p->p_flag |= P_NOCLDWAIT;
458 } else
459 p->p_flag &= ~P_NOCLDWAIT;
460
461 if (nsa->sa_handler == SIG_IGN) {
462 /*
463 * Paranoia: same as above.
464 */
465 if (p->p_pid == 1)
466 p->p_flag &= ~P_CLDSIGIGN;
467 else
468 p->p_flag |= P_CLDSIGIGN;
469 } else
470 p->p_flag &= ~P_CLDSIGIGN;
471 }
472
473 if ((nsa->sa_flags & SA_NODEFER) == 0)
474 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
475 else
476 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
477
478 /*
479 * Set bit in p_sigctx.ps_sigignore for signals that are set to
480 * SIG_IGN, and for signals set to SIG_DFL where the default is to
481 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
482 * we have to restart the process.
483 */
484 if (nsa->sa_handler == SIG_IGN ||
485 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
486 /* Never to be seen again. */
487 sigemptyset(&tset);
488 sigaddset(&tset, signum);
489 sigclearall(p, &tset, &kq);
490 if (signum != SIGCONT) {
491 /* Easier in psignal */
492 sigaddset(&p->p_sigctx.ps_sigignore, signum);
493 }
494 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
495 } else {
496 sigdelset(&p->p_sigctx.ps_sigignore, signum);
497 if (nsa->sa_handler == SIG_DFL)
498 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
499 else
500 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
501 }
502
503 /*
504 * Previously held signals may now have become visible. Ensure that
505 * we check for them before returning to userspace.
506 */
507 lwp_lock(l);
508 l->l_flag |= L_PENDSIG;
509 lwp_unlock(l);
510 out:
511 mutex_exit(&p->p_smutex);
512 mutex_exit(&p->p_mutex);
513 ksiginfo_queue_drain(&kq);
514
515 return (error);
516 }
517
518 int
519 sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
520 {
521 int more;
522
523 LOCK_ASSERT(mutex_owned(&l->l_proc->p_smutex));
524
525 if (oss)
526 *oss = l->l_sigmask;
527 if (nss) {
528 switch (how) {
529 case SIG_BLOCK:
530 sigplusset(nss, &l->l_sigmask);
531 more = 0;
532 break;
533 case SIG_UNBLOCK:
534 sigminusset(nss, &l->l_sigmask);
535 more = 1;
536 break;
537 case SIG_SETMASK:
538 l->l_sigmask = *nss;
539 more = 1;
540 break;
541 default:
542 return (EINVAL);
543 }
544 sigminusset(&sigcantmask, &l->l_sigmask);
545 if (more) {
546 /*
547 * Check for pending signals on return to user.
548 */
549 lwp_lock(l);
550 l->l_flag |= L_PENDSIG;
551 lwp_unlock(l);
552 }
553 }
554
555 return (0);
556 }
557
558 void
559 sigpending1(struct lwp *l, sigset_t *ss)
560 {
561 struct proc *p = l->l_proc;
562
563 mutex_enter(&p->p_smutex);
564 *ss = l->l_sigpend.sp_set;
565 sigplusset(&p->p_sigpend.sp_set, ss);
566 sigminusset(&l->l_sigmask, ss);
567 mutex_exit(&p->p_smutex);
568 }
569
570 int
571 sigsuspend1(struct lwp *l, const sigset_t *ss)
572 {
573 struct proc *p;
574
575 p = l->l_proc;
576
577 if (ss) {
578 /*
579 * When returning from sigpause, we want
580 * the old mask to be restored after the
581 * signal handler has finished. Thus, we
582 * save it here and mark the sigctx structure
583 * to indicate this.
584 */
585 mutex_enter(&p->p_smutex);
586 l->l_sigrestore = 1;
587 l->l_sigoldmask = l->l_sigmask;
588 l->l_sigmask = *ss;
589 sigminusset(&sigcantmask, &l->l_sigmask);
590
591 /* Check for pending signals when sleeping. */
592 lwp_lock(l);
593 l->l_flag |= L_PENDSIG;
594 lwp_unlock(l);
595 mutex_exit(&p->p_smutex);
596 }
597
598 while (kpause("pause", TRUE, 0, NULL) == 0)
599 ;
600
601 /* always return EINTR rather than ERESTART... */
602 return (EINTR);
603 }
604
605 int
606 sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
607 struct sigaltstack *oss)
608 {
609 struct proc *p = l->l_proc;
610 int error = 0;
611
612 mutex_enter(&p->p_smutex);
613
614 if (oss)
615 *oss = l->l_sigstk;
616
617 if (nss) {
618 if (nss->ss_flags & ~SS_ALLBITS)
619 error = EINVAL;
620 else if (nss->ss_flags & SS_DISABLE) {
621 if (l->l_sigstk.ss_flags & SS_ONSTACK)
622 error = EINVAL;
623 } else if (nss->ss_size < MINSIGSTKSZ)
624 error = ENOMEM;
625
626 if (!error)
627 l->l_sigstk = *nss;
628 }
629
630 mutex_exit(&p->p_smutex);
631
632 return (error);
633 }
634
635 int
636 __sigtimedwait1(struct lwp *l, void *v, register_t *retval,
637 copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
638 {
639 struct sys___sigtimedwait_args /* {
640 syscallarg(const sigset_t *) set;
641 syscallarg(siginfo_t *) info;
642 syscallarg(struct timespec *) timeout;
643 } */ *uap = v;
644 struct proc *p = l->l_proc;
645 int error, signum;
646 int timo = 0;
647 struct timespec ts, tsstart, tsnow;
648 ksiginfo_t *ksi;
649
650 memset(&tsstart, 0, sizeof tsstart); /* XXX gcc */
651
652 /*
653 * Calculate timeout, if it was specified.
654 */
655 if (SCARG(uap, timeout)) {
656 uint64_t ms;
657
658 if ((error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts))))
659 return (error);
660
661 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
662 timo = mstohz(ms);
663 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
664 timo = 1;
665 if (timo <= 0)
666 return (EAGAIN);
667
668 /*
669 * Remember current uptime, it would be used in
670 * ECANCELED/ERESTART case.
671 */
672 getnanouptime(&tsstart);
673 }
674
675 error = copyin(SCARG(uap, set), &l->l_sigwaitset,
676 sizeof(l->l_sigwaitset));
677 if (error != 0)
678 return (error);
679
680 /*
681 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
682 * SA_CANTMASK signals in waitset, we do this only for the below
683 * siglist check.
684 */
685 sigminusset(&sigcantmask, &l->l_sigwaitset);
686
687 /*
688 * Allocate a ksi up front. We can't sleep with the mutex held.
689 */
690 KERNEL_LOCK(1, l); /* XXXSMP ksiginfo_alloc() -> pool_get() */
691 ksi = ksiginfo_alloc(p, NULL, PR_WAITOK);
692 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
693 if (ksi == NULL)
694 return (ENOMEM);
695
696 mutex_enter(&p->p_smutex);
697
698 if ((signum = sigget(&p->p_sigpend, ksi, 0, &l->l_sigwaitset)) == 0)
699 signum = sigget(&l->l_sigpend, ksi, 0, &l->l_sigwaitset);
700
701 if (signum != 0) {
702 /*
703 * We found a pending signal - copy it out to the user.
704 */
705 mutex_exit(&p->p_smutex);
706 goto out;
707 }
708
709 /*
710 * Set up the sigwait list.
711 */
712 l->l_sigwaited = ksi;
713 LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
714
715 /*
716 * Wait for signal to arrive. We can either be woken up or time out.
717 */
718 error = cv_timedwait_sig(&l->l_sigcv, &p->p_smutex, timo);
719
720 /*
721 * Need to find out if we woke as a result of lwp_wakeup() or a
722 * signal outside our wait set.
723 */
724 if (l->l_sigwaited != NULL) {
725 if (error == EINTR) {
726 /* wakeup via _lwp_wakeup() */
727 error = ECANCELED;
728 } else if (!error) {
729 /* spurious wakeup - arrange for syscall restart */
730 error = ERESTART;
731 }
732 l->l_sigwaited = NULL;
733 LIST_REMOVE(l, l_sigwaiter);
734 }
735
736 mutex_exit(&p->p_smutex);
737
738 /*
739 * If the sleep was interrupted (either by signal or wakeup), update
740 * the timeout and copyout new value back. It would be used when
741 * the syscall would be restarted or called again.
742 */
743 if (timo && (error == ERESTART || error == ECANCELED)) {
744 getnanouptime(&tsnow);
745
746 /* compute how much time has passed since start */
747 timespecsub(&tsnow, &tsstart, &tsnow);
748 /* substract passed time from timeout */
749 timespecsub(&ts, &tsnow, &ts);
750
751 if (ts.tv_sec < 0)
752 error = EAGAIN;
753 else {
754 /* copy updated timeout to userland */
755 error = (*put_timeout)(&ts, SCARG(uap, timeout),
756 sizeof(ts));
757 }
758 }
759
760 /*
761 * If a signal from the wait set arrived, copy it to userland.
762 * Copy only the used part of siginfo, the padding part is
763 * left unchanged (userland is not supposed to touch it anyway).
764 */
765 out:
766 if (error == 0)
767 error = (*put_info)(&ksi->ksi_info, SCARG(uap, info),
768 sizeof(ksi->ksi_info));
769
770 KERNEL_LOCK(1, l); /* XXXSMP ksiginfo_free() -> pool_put() */
771 ksiginfo_free(ksi);
772 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
773
774 return error;
775 }
776