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 */
21 #include "internals.h"
23 #include <bits/sigcontextinfo.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 /* User-provided signal handlers */
76 typedef void (*arch_sighandler_t) __PMT ((int, SIGCONTEXT));
79 arch_sighandler_t old;
80 void (*rt) (int, struct siginfo *, struct ucontext *);
83 /* The wrapper around user-provided signal handlers */
84 static void pthread_sighandler(int signo, SIGCONTEXT ctx)
86 pthread_descr self = thread_self();
88 /* If we're in a sigwait operation, just record the signal received
89 and return without calling the user's handler */
90 if (THREAD_GETMEM(self, p_sigwaiting)) {
91 THREAD_SETMEM(self, p_sigwaiting, 0);
92 THREAD_SETMEM(self, p_signal, signo);
95 /* Record that we're in a signal handler and call the user's
97 in_sighandler = THREAD_GETMEM(self, p_in_sighandler);
98 if (in_sighandler == NULL)
99 THREAD_SETMEM(self, p_in_sighandler, CURRENT_STACK_FRAME);
100 sighandler[signo].old(signo, SIGCONTEXT_EXTRA_ARGS ctx);
101 if (in_sighandler == NULL)
102 THREAD_SETMEM(self, p_in_sighandler, NULL);
105 /* The same, this time for real-time signals. */
106 static void pthread_sighandler_rt(int signo, struct siginfo *si,
109 pthread_descr self = thread_self();
110 char * in_sighandler;
111 /* If we're in a sigwait operation, just record the signal received
112 and return without calling the user's handler */
113 if (THREAD_GETMEM(self, p_sigwaiting)) {
114 THREAD_SETMEM(self, p_sigwaiting, 0);
115 THREAD_SETMEM(self, p_signal, signo);
118 /* Record that we're in a signal handler and call the user's
120 in_sighandler = THREAD_GETMEM(self, p_in_sighandler);
121 if (in_sighandler == NULL)
122 THREAD_SETMEM(self, p_in_sighandler, CURRENT_STACK_FRAME);
123 sighandler[signo].rt(signo, si, uc);
124 if (in_sighandler == NULL)
125 THREAD_SETMEM(self, p_in_sighandler, NULL);
128 /* The wrapper around sigaction. Install our own signal handler
129 around the signal. */
130 libpthread_hidden_proto(sigaction)
131 int sigaction(int sig, const struct sigaction * act,
132 struct sigaction * oact)
134 struct sigaction newact;
135 struct sigaction *newactp;
138 printf(__FUNCTION__": pthreads wrapper!\n");
140 if (sig == __pthread_sig_restart ||
141 sig == __pthread_sig_cancel ||
142 (sig == __pthread_sig_debug && __pthread_sig_debug > 0))
147 if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL
148 && sig > 0 && sig < NSIG)
150 if (act->sa_flags & SA_SIGINFO)
151 newact.sa_handler = (__sighandler_t) pthread_sighandler_rt;
153 newact.sa_handler = (__sighandler_t) pthread_sighandler;
159 if (__libc_sigaction(sig, newactp, oact) == -1)
162 printf(__FUNCTION__": sighandler installed, sigaction successful\n");
164 if (sig > 0 && sig < NSIG)
167 oact->sa_handler = (__sighandler_t) sighandler[sig].old;
169 /* For the assignment is does not matter whether it's a normal
170 or real-time signal. */
171 sighandler[sig].old = (arch_sighandler_t) act->sa_handler;
175 libpthread_hidden_def(sigaction)
177 /* A signal handler that does nothing */
178 static void pthread_null_sighandler(int sig attribute_unused) { }
180 /* sigwait -- synchronously wait for a signal */
181 int sigwait(const sigset_t * set, int * sig)
183 volatile pthread_descr self = thread_self();
189 /* Get ready to block all signals except those in set
190 and the cancellation signal.
191 Also check that handlers are installed on all signals in set,
192 and if not, install our dummy handler. This is conformant to
193 POSIX: "The effect of sigwait() on the signal actions for the
194 signals in set is unspecified." */
196 sigdelset(&mask, __pthread_sig_cancel);
197 for (s = 1; s <= NSIG; s++) {
198 if (sigismember(set, s) &&
199 s != __pthread_sig_restart &&
200 s != __pthread_sig_cancel &&
201 s != __pthread_sig_debug) {
203 if (sighandler[s].old == NULL ||
204 sighandler[s].old == (arch_sighandler_t) SIG_DFL ||
205 sighandler[s].old == (arch_sighandler_t) SIG_IGN) {
206 memset(&sa, 0, sizeof(sa));
207 sa.sa_handler = pthread_null_sighandler;
208 sigaction(s, &sa, NULL);
212 /* Test for cancellation */
213 if (sigsetjmp(jmpbuf, 1) == 0) {
214 THREAD_SETMEM(self, p_cancel_jmp, &jmpbuf);
215 if (! (THREAD_GETMEM(self, p_canceled)
216 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)) {
217 /* Reset the signal count */
218 THREAD_SETMEM(self, p_signal, 0);
219 /* Say we're in sigwait */
220 THREAD_SETMEM(self, p_sigwaiting, 1);
221 /* Unblock the signals and wait for them */
225 THREAD_SETMEM(self, p_cancel_jmp, NULL);
226 /* The signals are now reblocked. Check for cancellation */
227 pthread_testcancel();
228 /* We should have self->p_signal != 0 and equal to the signal received */
229 *sig = THREAD_GETMEM(self, p_signal);
233 /* Redefine raise() to send signal to calling thread only,
234 as per POSIX 1003.1c */
235 libpthread_hidden_proto(raise)
238 int retcode = pthread_kill(pthread_self(), sig);
246 libpthread_hidden_def(raise)