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