1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
15 /* Handling of signals */
20 #include "internals.h"
25 int pthread_sigmask(int how
, const sigset_t
* newmask
, sigset_t
* oldmask
)
29 if (newmask
!= NULL
) {
31 /* Don't allow __pthread_sig_restart to be unmasked.
32 Don't allow __pthread_sig_cancel to be masked. */
35 sigaddset(&mask
, __pthread_sig_restart
);
36 sigdelset(&mask
, __pthread_sig_cancel
);
39 sigdelset(&mask
, __pthread_sig_cancel
);
42 sigdelset(&mask
, __pthread_sig_restart
);
47 if (sigprocmask(how
, newmask
, oldmask
) == -1)
53 int pthread_kill(pthread_t thread
, int signo
)
55 pthread_handle handle
= thread_handle(thread
);
58 __pthread_lock(&handle
->h_lock
, NULL
);
59 if (invalid_handle(handle
, thread
)) {
60 __pthread_unlock(&handle
->h_lock
);
63 pid
= handle
->h_descr
->p_pid
;
64 __pthread_unlock(&handle
->h_lock
);
65 if (kill(pid
, signo
) == -1)
71 union sighandler __sighandler
[NSIG
] =
72 { [1 ... NSIG
- 1] = { (arch_sighandler_t
) SIG_ERR
} };
74 /* The wrapper around sigaction. Install our own signal handler
76 int __sigaction(int sig
, const struct sigaction
* act
,
77 struct sigaction
* oact
)
79 struct sigaction newact
;
80 struct sigaction
*newactp
;
82 if (sig
== __pthread_sig_restart
||
83 sig
== __pthread_sig_cancel
||
84 (sig
== __pthread_sig_debug
&& __pthread_sig_debug
> 0))
92 if (act
->sa_handler
!= SIG_IGN
&& act
->sa_handler
!= SIG_DFL
93 && sig
> 0 && sig
< NSIG
)
95 if (act
->sa_flags
& SA_SIGINFO
)
96 newact
.sa_handler
= (__sighandler_t
) __pthread_sighandler_rt
;
98 newact
.sa_handler
= (__sighandler_t
) __pthread_sighandler
;
104 if (__libc_sigaction(sig
, newactp
, oact
) == -1)
106 if (sig
> 0 && sig
< NSIG
)
109 /* We may have inherited SIG_IGN from the parent, so return the
110 kernel's idea of the signal handler the first time
112 && (__sighandler_t
) __sighandler
[sig
].old
!= SIG_ERR
)
113 oact
->sa_handler
= (__sighandler_t
) __sighandler
[sig
].old
;
115 /* For the assignment it does not matter whether it's a normal
116 or real-time signal. */
117 __sighandler
[sig
].old
= (arch_sighandler_t
) act
->sa_handler
;
121 strong_alias(__sigaction
, sigaction
)
123 /* sigwait -- synchronously wait for a signal */
124 int sigwait(const sigset_t
* set
, int * sig
)
126 volatile pthread_descr self
= thread_self();
132 /* Get ready to block all signals except those in set
133 and the cancellation signal.
134 Also check that handlers are installed on all signals in set,
135 and if not, install our dummy handler. This is conformant to
136 POSIX: "The effect of sigwait() on the signal actions for the
137 signals in set is unspecified." */
139 sigdelset(&mask
, __pthread_sig_cancel
);
140 for (s
= 1; s
< NSIG
; s
++) {
141 if (sigismember(set
, s
) &&
142 s
!= __pthread_sig_restart
&&
143 s
!= __pthread_sig_cancel
&&
144 s
!= __pthread_sig_debug
) {
146 if (__sighandler
[s
].old
== (arch_sighandler_t
) SIG_ERR
||
147 __sighandler
[s
].old
== (arch_sighandler_t
) SIG_DFL
||
148 __sighandler
[s
].old
== (arch_sighandler_t
) SIG_IGN
) {
149 sa
.sa_handler
= __pthread_null_sighandler
;
150 sigfillset(&sa
.sa_mask
);
152 sigaction(s
, &sa
, NULL
);
156 /* Test for cancellation */
157 if (sigsetjmp(jmpbuf
, 1) == 0) {
158 THREAD_SETMEM(self
, p_cancel_jmp
, &jmpbuf
);
159 if (! (THREAD_GETMEM(self
, p_canceled
)
160 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
)) {
161 /* Reset the signal count */
162 THREAD_SETMEM(self
, p_signal
, 0);
163 /* Say we're in sigwait */
164 THREAD_SETMEM(self
, p_sigwaiting
, 1);
165 /* Unblock the signals and wait for them */
169 THREAD_SETMEM(self
, p_cancel_jmp
, NULL
);
170 /* The signals are now reblocked. Check for cancellation */
171 pthread_testcancel();
172 /* We should have self->p_signal != 0 and equal to the signal received */
173 *sig
= THREAD_GETMEM(self
, p_signal
);
177 /* Redefine raise() to send signal to calling thread only,
178 as per POSIX 1003.1c */
181 int retcode
= pthread_kill(pthread_self(), sig
);