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
);
37 if (__pthread_sig_debug
> 0)
38 sigdelset(&mask
, __pthread_sig_debug
);
41 sigdelset(&mask
, __pthread_sig_cancel
);
42 if (__pthread_sig_debug
> 0)
43 sigdelset(&mask
, __pthread_sig_debug
);
46 sigdelset(&mask
, __pthread_sig_restart
);
51 if (sigprocmask(how
, newmask
, oldmask
) == -1)
57 int pthread_kill(pthread_t thread
, int signo
)
59 pthread_handle handle
= thread_handle(thread
);
62 __pthread_lock(&handle
->h_lock
, NULL
);
63 if (invalid_handle(handle
, thread
)) {
64 __pthread_unlock(&handle
->h_lock
);
67 pid
= handle
->h_descr
->p_pid
;
68 __pthread_unlock(&handle
->h_lock
);
69 if (kill(pid
, signo
) == -1)
75 union sighandler __sighandler
[NSIG
] =
76 { [1 ... NSIG
- 1] = { (arch_sighandler_t
) SIG_ERR
} };
78 /* The wrapper around sigaction. Install our own signal handler
80 int __pthread_sigaction(int sig
, const struct sigaction
* act
,
81 struct sigaction
* oact
)
83 struct sigaction newact
;
84 struct sigaction
*newactp
;
85 __sighandler_t old
= SIG_DFL
;
87 if (sig
== __pthread_sig_restart
||
88 sig
== __pthread_sig_cancel
||
89 (sig
== __pthread_sig_debug
&& __pthread_sig_debug
> 0))
94 if (sig
> 0 && sig
< NSIG
)
95 old
= (__sighandler_t
) __sighandler
[sig
].old
;
99 if (act
->sa_handler
!= SIG_IGN
&& act
->sa_handler
!= SIG_DFL
100 && sig
> 0 && sig
< NSIG
)
102 if (act
->sa_flags
& SA_SIGINFO
)
103 newact
.sa_handler
= (__sighandler_t
) __pthread_sighandler_rt
;
105 newact
.sa_handler
= (__sighandler_t
) __pthread_sighandler
;
106 if (old
== SIG_IGN
|| old
== SIG_DFL
|| old
== SIG_ERR
)
107 __sighandler
[sig
].old
= (arch_sighandler_t
) act
->sa_handler
;
113 if (__libc_sigaction(sig
, newactp
, oact
) == -1)
116 __sighandler
[sig
].old
= (arch_sighandler_t
) old
;
119 if (sig
> 0 && sig
< NSIG
)
122 /* We may have inherited SIG_IGN from the parent, so return the
123 kernel's idea of the signal handler the first time
126 oact
->sa_handler
= old
;
128 /* For the assignment it does not matter whether it's a normal
129 or real-time signal. */
130 __sighandler
[sig
].old
= (arch_sighandler_t
) act
->sa_handler
;
135 strong_alias(__pthread_sigaction
, __sigaction
)
136 strong_alias(__pthread_sigaction
, sigaction
)
139 /* sigwait -- synchronously wait for a signal */
140 int __pthread_sigwait(const sigset_t
* set
, int * sig
)
142 volatile pthread_descr self
= thread_self();
148 /* Get ready to block all signals except those in set
149 and the cancellation signal.
150 Also check that handlers are installed on all signals in set,
151 and if not, install our dummy handler. This is conformant to
152 POSIX: "The effect of sigwait() on the signal actions for the
153 signals in set is unspecified." */
155 sigdelset(&mask
, __pthread_sig_cancel
);
156 for (s
= 1; s
< NSIG
; s
++) {
157 if (sigismember(set
, s
) &&
158 s
!= __pthread_sig_restart
&&
159 s
!= __pthread_sig_cancel
&&
160 s
!= __pthread_sig_debug
) {
162 if (__sighandler
[s
].old
== (arch_sighandler_t
) SIG_ERR
||
163 __sighandler
[s
].old
== (arch_sighandler_t
) SIG_DFL
||
164 __sighandler
[s
].old
== (arch_sighandler_t
) SIG_IGN
) {
165 sa
.sa_handler
= __pthread_null_sighandler
;
166 sigfillset(&sa
.sa_mask
);
168 sigaction(s
, &sa
, NULL
);
172 /* Test for cancellation */
173 if (sigsetjmp(jmpbuf
, 1) == 0) {
174 THREAD_SETMEM(self
, p_cancel_jmp
, &jmpbuf
);
175 if (! (THREAD_GETMEM(self
, p_canceled
)
176 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
)) {
177 /* Reset the signal count */
178 THREAD_SETMEM(self
, p_signal
, 0);
179 /* Say we're in sigwait */
180 THREAD_SETMEM(self
, p_sigwaiting
, 1);
181 /* Unblock the signals and wait for them */
185 THREAD_SETMEM(self
, p_cancel_jmp
, NULL
);
186 /* The signals are now reblocked. Check for cancellation */
187 pthread_testcancel();
188 /* We should have self->p_signal != 0 and equal to the signal received */
189 *sig
= THREAD_GETMEM(self
, p_signal
);
193 strong_alias (__pthread_sigwait
, sigwait
)
196 /* Redefine raise() to send signal to calling thread only,
197 as per POSIX 1003.1c */
198 int __pthread_raise (int sig
)
200 int retcode
= pthread_kill(pthread_self(), sig
);
209 strong_alias (__pthread_raise
, raise
)
212 /* This files handles cancellation internally. */
213 LIBC_CANCEL_HANDLED ();