1 /* $NetBSD: sys_sig.c,v 1.24 2009/12/19 18:25:54 rmind Exp $ */
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
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.
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
65 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.24 2009/12/19 18:25:54 rmind Exp $");
71 #include <sys/param.h>
72 #include <sys/kernel.h>
73 #include <sys/signalvar.h>
77 #include <sys/savar.h>
78 #include <sys/syscallargs.h>
79 #include <sys/kauth.h>
82 #include <sys/module.h>
85 sys___sigaction_sigtramp(struct lwp
*l
,
86 const struct sys___sigaction_sigtramp_args
*uap
, register_t
*retval
)
89 syscallarg(int) signum;
90 syscallarg(const struct sigaction *) nsa;
91 syscallarg(struct sigaction *) osa;
92 syscallarg(void *) tramp;
95 struct sigaction nsa
, osa
;
98 if (SCARG(uap
, nsa
)) {
99 error
= copyin(SCARG(uap
, nsa
), &nsa
, sizeof(nsa
));
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
));
108 if (SCARG(uap
, osa
)) {
109 error
= copyout(&osa
, SCARG(uap
, osa
), sizeof(osa
));
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.
121 sys___sigprocmask14(struct lwp
*l
, const struct sys___sigprocmask14_args
*uap
,
126 syscallarg(const sigset_t *) set;
127 syscallarg(sigset_t *) oset;
129 struct proc
*p
= l
->l_proc
;
133 if (SCARG(uap
, set
)) {
134 error
= copyin(SCARG(uap
, set
), &nss
, sizeof(nss
));
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
);
144 if (SCARG(uap
, oset
)) {
145 error
= copyout(&oss
, SCARG(uap
, oset
), sizeof(oss
));
153 sys___sigpending14(struct lwp
*l
, const struct sys___sigpending14_args
*uap
,
157 syscallarg(sigset_t *) set;
162 return copyout(&ss
, SCARG(uap
, set
), sizeof(ss
));
166 * Suspend process until signal, providing mask to be set in the meantime.
167 * Note nonstandard calling convention: libc stub passes mask, not pointer,
171 sys___sigsuspend14(struct lwp
*l
, const struct sys___sigsuspend14_args
*uap
,
175 syscallarg(const sigset_t *) set;
180 if (SCARG(uap
, set
)) {
181 error
= copyin(SCARG(uap
, set
), &ss
, sizeof(ss
));
185 return sigsuspend1(l
, SCARG(uap
, set
) ? &ss
: 0);
189 sys___sigaltstack14(struct lwp
*l
, const struct sys___sigaltstack14_args
*uap
,
193 syscallarg(const struct sigaltstack *) nss;
194 syscallarg(struct sigaltstack *) oss;
196 struct sigaltstack nss
, oss
;
199 if (SCARG(uap
, nss
)) {
200 error
= copyin(SCARG(uap
, nss
), &nss
, sizeof(nss
));
204 error
= sigaltstack1(l
,
205 SCARG(uap
, nss
) ? &nss
: 0, SCARG(uap
, oss
) ? &oss
: 0);
208 if (SCARG(uap
, oss
)) {
209 error
= copyout(&oss
, SCARG(uap
, oss
), sizeof(oss
));
217 sys_kill(struct lwp
*l
, const struct sys_kill_args
*uap
, register_t
*retval
)
221 syscallarg(int) signum;
225 int signum
= SCARG(uap
, signum
);
228 if ((u_int
)signum
>= NSIG
)
231 ksi
.ksi_signo
= signum
;
232 ksi
.ksi_code
= SI_USER
;
233 ksi
.ksi_pid
= l
->l_proc
->p_pid
;
234 ksi
.ksi_uid
= kauth_cred_geteuid(l
->l_cred
);
235 if (SCARG(uap
, pid
) > 0) {
236 /* kill single process */
237 mutex_enter(proc_lock
);
238 if ((p
= p_find(SCARG(uap
, pid
), PFIND_LOCKED
)) == NULL
) {
239 mutex_exit(proc_lock
);
242 mutex_enter(p
->p_lock
);
243 error
= kauth_authorize_process(l
->l_cred
,
244 KAUTH_PROCESS_SIGNAL
, p
, KAUTH_ARG(signum
),
246 if (!error
&& signum
) {
249 mutex_exit(p
->p_lock
);
250 mutex_exit(proc_lock
);
253 switch (SCARG(uap
, pid
)) {
254 case -1: /* broadcast signal */
255 return killpg1(l
, &ksi
, 0, 1);
256 case 0: /* signal own process group */
257 return killpg1(l
, &ksi
, 0, 0);
258 default: /* negative explicit process group */
259 return killpg1(l
, &ksi
, -SCARG(uap
, pid
), 0);
265 sys_getcontext(struct lwp
*l
, const struct sys_getcontext_args
*uap
,
269 syscallarg(struct __ucontext *) ucp;
271 struct proc
*p
= l
->l_proc
;
274 mutex_enter(p
->p_lock
);
276 mutex_exit(p
->p_lock
);
278 return copyout(&uc
, SCARG(uap
, ucp
), sizeof (*SCARG(uap
, ucp
)));
282 sys_setcontext(struct lwp
*l
, const struct sys_setcontext_args
*uap
,
286 syscallarg(const ucontext_t *) ucp;
288 struct proc
*p
= l
->l_proc
;
292 error
= copyin(SCARG(uap
, ucp
), &uc
, sizeof (uc
));
295 if ((uc
.uc_flags
& _UC_CPU
) == 0)
297 mutex_enter(p
->p_lock
);
298 error
= setucontext(l
, &uc
);
299 mutex_exit(p
->p_lock
);
307 * sigtimedwait(2) system call, used also for implementation
308 * of sigwaitinfo() and sigwait().
310 * This only handles single LWP in signal wait. libpthread provides
311 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
314 sys_____sigtimedwait50(struct lwp
*l
,
315 const struct sys_____sigtimedwait50_args
*uap
, register_t
*retval
)
318 return __sigtimedwait1(l
, uap
, retval
, copyout
, copyin
, copyout
);
322 sigaction1(struct lwp
*l
, int signum
, const struct sigaction
*nsa
,
323 struct sigaction
*osa
, const void *tramp
, int vers
)
330 static bool v0v1valid
;
332 if (signum
<= 0 || signum
>= NSIG
)
337 ksiginfo_queue_init(&kq
);
340 * Trampoline ABI version 0 is reserved for the legacy kernel
341 * provided on-stack trampoline. Conversely, if we are using a
342 * non-0 ABI version, we must have a trampoline. Only validate the
343 * vers if a new sigaction was supplied. Emulations use legacy
344 * kernel trampolines with version 0, alternatively check for that
347 * If version < 2, we try to autoload the compat module. Note
348 * that we interlock with the unload check in compat_modcmd()
349 * using module_lock. If the autoload fails, we don't try it
350 * again for this process.
353 if (__predict_false(vers
< 2) &&
354 (p
->p_lflag
& PL_SIGCOMPAT
) == 0) {
355 mutex_enter(&module_lock
);
356 if (sendsig_sigcontext_vec
== NULL
) {
357 (void)module_autoload("compat",
360 if (sendsig_sigcontext_vec
!= NULL
) {
362 * We need to remember if the
363 * sigcontext method may be useable,
364 * because libc may use it even
365 * if siginfo is available.
369 mutex_enter(proc_lock
);
371 * Prevent unload of compat module while
372 * this process remains.
374 p
->p_lflag
|= PL_SIGCOMPAT
;
375 mutex_exit(proc_lock
);
376 mutex_exit(&module_lock
);
381 /* sigcontext, kernel supplied trampoline. */
382 if (tramp
!= NULL
|| !v0v1valid
) {
387 /* sigcontext, user supplied trampoline. */
388 if (tramp
== NULL
|| !v0v1valid
) {
394 /* siginfo, user supplied trampoline. */
404 mutex_enter(p
->p_lock
);
408 *osa
= SIGACTION_PS(ps
, signum
);
412 prop
= sigprop
[signum
];
413 if ((nsa
->sa_flags
& ~SA_ALLBITS
) || (prop
& SA_CANTMASK
)) {
418 SIGACTION_PS(ps
, signum
) = *nsa
;
419 ps
->sa_sigdesc
[signum
].sd_tramp
= tramp
;
420 ps
->sa_sigdesc
[signum
].sd_vers
= vers
;
421 sigminusset(&sigcantmask
, &SIGACTION_PS(ps
, signum
).sa_mask
);
423 if ((prop
& SA_NORESET
) != 0)
424 SIGACTION_PS(ps
, signum
).sa_flags
&= ~SA_RESETHAND
;
426 if (signum
== SIGCHLD
) {
427 if (nsa
->sa_flags
& SA_NOCLDSTOP
)
428 p
->p_sflag
|= PS_NOCLDSTOP
;
430 p
->p_sflag
&= ~PS_NOCLDSTOP
;
431 if (nsa
->sa_flags
& SA_NOCLDWAIT
) {
433 * Paranoia: since SA_NOCLDWAIT is implemented by
434 * reparenting the dying child to PID 1 (and trust
435 * it to reap the zombie), PID 1 itself is forbidden
436 * to set SA_NOCLDWAIT.
439 p
->p_flag
&= ~PK_NOCLDWAIT
;
441 p
->p_flag
|= PK_NOCLDWAIT
;
443 p
->p_flag
&= ~PK_NOCLDWAIT
;
445 if (nsa
->sa_handler
== SIG_IGN
) {
447 * Paranoia: same as above.
450 p
->p_flag
&= ~PK_CLDSIGIGN
;
452 p
->p_flag
|= PK_CLDSIGIGN
;
454 p
->p_flag
&= ~PK_CLDSIGIGN
;
457 if ((nsa
->sa_flags
& SA_NODEFER
) == 0)
458 sigaddset(&SIGACTION_PS(ps
, signum
).sa_mask
, signum
);
460 sigdelset(&SIGACTION_PS(ps
, signum
).sa_mask
, signum
);
463 * Set bit in p_sigctx.ps_sigignore for signals that are set to
464 * SIG_IGN, and for signals set to SIG_DFL where the default is to
465 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
466 * we have to restart the process.
468 if (nsa
->sa_handler
== SIG_IGN
||
469 (nsa
->sa_handler
== SIG_DFL
&& (prop
& SA_IGNORE
) != 0)) {
470 /* Never to be seen again. */
472 sigaddset(&tset
, signum
);
473 sigclearall(p
, &tset
, &kq
);
474 if (signum
!= SIGCONT
) {
475 /* Easier in psignal */
476 sigaddset(&p
->p_sigctx
.ps_sigignore
, signum
);
478 sigdelset(&p
->p_sigctx
.ps_sigcatch
, signum
);
480 sigdelset(&p
->p_sigctx
.ps_sigignore
, signum
);
481 if (nsa
->sa_handler
== SIG_DFL
)
482 sigdelset(&p
->p_sigctx
.ps_sigcatch
, signum
);
484 sigaddset(&p
->p_sigctx
.ps_sigcatch
, signum
);
488 * Previously held signals may now have become visible. Ensure that
489 * we check for them before returning to userspace.
491 if (sigispending(l
, 0)) {
493 l
->l_flag
|= LW_PENDSIG
;
497 mutex_exit(p
->p_lock
);
498 ksiginfo_queue_drain(&kq
);
504 sigprocmask1(struct lwp
*l
, int how
, const sigset_t
*nss
, sigset_t
*oss
)
507 struct proc
*p
= l
->l_proc
;
509 mask
= (p
->p_sa
!= NULL
) ? &p
->p_sa
->sa_sigmask
: &l
->l_sigmask
;
511 KASSERT(mutex_owned(p
->p_lock
));
518 sigplusset(nss
, mask
);
522 sigminusset(nss
, mask
);
532 sigminusset(&sigcantmask
, mask
);
533 if (more
&& sigispending(l
, 0)) {
535 * Check for pending signals on return to user.
538 l
->l_flag
|= LW_PENDSIG
;
547 sigpending1(struct lwp
*l
, sigset_t
*ss
)
549 struct proc
*p
= l
->l_proc
;
551 mutex_enter(p
->p_lock
);
552 *ss
= l
->l_sigpend
.sp_set
;
553 sigplusset(&p
->p_sigpend
.sp_set
, ss
);
554 mutex_exit(p
->p_lock
);
558 sigsuspend1(struct lwp
*l
, const sigset_t
*ss
)
560 struct proc
*p
= l
->l_proc
;
564 * When returning from sigsuspend, we want
565 * the old mask to be restored after the
566 * signal handler has finished. Thus, we
567 * save it here and mark the sigctx structure
570 mutex_enter(p
->p_lock
);
572 l
->l_sigoldmask
= l
->l_sigmask
;
574 sigminusset(&sigcantmask
, &l
->l_sigmask
);
576 /* Check for pending signals when sleeping. */
577 if (sigispending(l
, 0)) {
579 l
->l_flag
|= LW_PENDSIG
;
582 mutex_exit(p
->p_lock
);
585 while (kpause("pause", true, 0, NULL
) == 0)
588 /* always return EINTR rather than ERESTART... */
593 sigaltstack1(struct lwp
*l
, const struct sigaltstack
*nss
,
594 struct sigaltstack
*oss
)
596 struct proc
*p
= l
->l_proc
;
599 mutex_enter(p
->p_lock
);
605 if (nss
->ss_flags
& ~SS_ALLBITS
)
607 else if (nss
->ss_flags
& SS_DISABLE
) {
608 if (l
->l_sigstk
.ss_flags
& SS_ONSTACK
)
610 } else if (nss
->ss_size
< MINSIGSTKSZ
)
617 mutex_exit(p
->p_lock
);
623 __sigtimedwait1(struct lwp
*l
, const struct sys_____sigtimedwait50_args
*uap
,
624 register_t
*retval
, copyout_t storeinf
, copyin_t fetchts
, copyout_t storets
)
627 syscallarg(const sigset_t *) set;
628 syscallarg(siginfo_t *) info;
629 syscallarg(struct timespec *) timeout;
631 struct proc
*p
= l
->l_proc
;
632 int error
, signum
, timo
;
633 struct timespec ts
, tsstart
, tsnow
;
637 * Calculate timeout, if it was specified.
639 if (SCARG(uap
, timeout
)) {
640 error
= (*fetchts
)(SCARG(uap
, timeout
), &ts
, sizeof(ts
));
644 if ((error
= itimespecfix(&ts
)) != 0)
648 if (timo
== 0 && ts
.tv_sec
== 0 && ts
.tv_nsec
!= 0)
652 * Remember current uptime, it would be used in
653 * ECANCELED/ERESTART case.
655 getnanouptime(&tsstart
);
657 memset(&tsstart
, 0, sizeof(tsstart
)); /* XXXgcc */
661 error
= copyin(SCARG(uap
, set
), &l
->l_sigwaitset
,
662 sizeof(l
->l_sigwaitset
));
667 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
668 * SA_CANTMASK signals in waitset, we do this only for the below
671 sigminusset(&sigcantmask
, &l
->l_sigwaitset
);
673 mutex_enter(p
->p_lock
);
675 /* SA processes can have no more than 1 sigwaiter. */
676 if ((p
->p_sflag
& PS_SA
) != 0 && !LIST_EMPTY(&p
->p_sigwaiters
)) {
677 mutex_exit(p
->p_lock
);
682 /* Check for pending signals in the process, if no - then in LWP. */
683 if ((signum
= sigget(&p
->p_sigpend
, &ksi
, 0, &l
->l_sigwaitset
)) == 0)
684 signum
= sigget(&l
->l_sigpend
, &ksi
, 0, &l
->l_sigwaitset
);
687 /* If found a pending signal, just copy it out to the user. */
688 mutex_exit(p
->p_lock
);
693 * Set up the sigwait list and wait for signal to arrive.
694 * We can either be woken up or time out.
696 l
->l_sigwaited
= &ksi
;
697 LIST_INSERT_HEAD(&p
->p_sigwaiters
, l
, l_sigwaiter
);
698 error
= cv_timedwait_sig(&l
->l_sigcv
, p
->p_lock
, timo
);
701 * Need to find out if we woke as a result of _lwp_wakeup() or a
702 * signal outside our wait set.
704 if (l
->l_sigwaited
!= NULL
) {
705 if (error
== EINTR
) {
706 /* Wakeup via _lwp_wakeup(). */
709 /* Spurious wakeup - arrange for syscall restart. */
712 l
->l_sigwaited
= NULL
;
713 LIST_REMOVE(l
, l_sigwaiter
);
715 mutex_exit(p
->p_lock
);
718 * If the sleep was interrupted (either by signal or wakeup), update
719 * the timeout and copyout new value back. It would be used when
720 * the syscall would be restarted or called again.
722 if (timo
&& (error
== ERESTART
|| error
== ECANCELED
)) {
723 getnanouptime(&tsnow
);
725 /* Compute how much time has passed since start. */
726 timespecsub(&tsnow
, &tsstart
, &tsnow
);
728 /* Substract passed time from timeout. */
729 timespecsub(&ts
, &tsnow
, &ts
);
734 /* Copy updated timeout to userland. */
735 error
= (*storets
)(&ts
, SCARG(uap
, timeout
),
741 * If a signal from the wait set arrived, copy it to userland.
742 * Copy only the used part of siginfo, the padding part is
743 * left unchanged (userland is not supposed to touch it anyway).
746 error
= (*storeinf
)(&ksi
.ksi_info
, SCARG(uap
, info
),
747 sizeof(ksi
.ksi_info
));