(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / signals.c
blob50f55995a3644a23d658a1778213562868f296cc
1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
4 /* */
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. */
9 /* */
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 */
17 #include <errno.h>
18 #include <signal.h>
19 #include "pthread.h"
20 #include "internals.h"
21 #include "spinlock.h"
22 #include <ucontext.h>
25 int pthread_sigmask(int how, const sigset_t * newmask, sigset_t * oldmask)
27 sigset_t mask;
29 if (newmask != NULL) {
30 mask = *newmask;
31 /* Don't allow __pthread_sig_restart to be unmasked.
32 Don't allow __pthread_sig_cancel to be masked. */
33 switch(how) {
34 case SIG_SETMASK:
35 sigaddset(&mask, __pthread_sig_restart);
36 sigdelset(&mask, __pthread_sig_cancel);
37 if (__pthread_sig_debug > 0)
38 sigdelset(&mask, __pthread_sig_debug);
39 break;
40 case SIG_BLOCK:
41 sigdelset(&mask, __pthread_sig_cancel);
42 if (__pthread_sig_debug > 0)
43 sigdelset(&mask, __pthread_sig_debug);
44 break;
45 case SIG_UNBLOCK:
46 sigdelset(&mask, __pthread_sig_restart);
47 break;
49 newmask = &mask;
51 if (sigprocmask(how, newmask, oldmask) == -1)
52 return errno;
53 else
54 return 0;
57 int pthread_kill(pthread_t thread, int signo)
59 pthread_handle handle = thread_handle(thread);
60 int pid;
62 __pthread_lock(&handle->h_lock, NULL);
63 if (invalid_handle(handle, thread)) {
64 __pthread_unlock(&handle->h_lock);
65 return ESRCH;
67 pid = handle->h_descr->p_pid;
68 __pthread_unlock(&handle->h_lock);
69 if (kill(pid, signo) == -1)
70 return errno;
71 else
72 return 0;
75 union sighandler __sighandler[NSIG] =
76 { [1 ... NSIG - 1] = { (arch_sighandler_t) SIG_ERR } };
78 /* The wrapper around sigaction. Install our own signal handler
79 around the signal. */
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))
91 __set_errno (EINVAL);
92 return -1;
94 if (sig > 0 && sig < NSIG)
95 old = (__sighandler_t) __sighandler[sig].old;
96 if (act)
98 newact = *act;
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;
104 else
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;
109 newactp = &newact;
111 else
112 newactp = NULL;
113 if (__libc_sigaction(sig, newactp, oact) == -1)
115 if (act)
116 __sighandler[sig].old = (arch_sighandler_t) old;
117 return -1;
119 if (sig > 0 && sig < NSIG)
121 if (oact != NULL
122 /* We may have inherited SIG_IGN from the parent, so return the
123 kernel's idea of the signal handler the first time
124 through. */
125 && old != SIG_ERR)
126 oact->sa_handler = old;
127 if (act)
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;
132 return 0;
134 #ifdef SHARED
135 strong_alias(__pthread_sigaction, __sigaction)
136 strong_alias(__pthread_sigaction, sigaction)
137 #endif
139 /* sigwait -- synchronously wait for a signal */
140 int __pthread_sigwait(const sigset_t * set, int * sig)
142 volatile pthread_descr self = thread_self();
143 sigset_t mask;
144 int s;
145 sigjmp_buf jmpbuf;
146 struct sigaction sa;
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." */
154 sigfillset(&mask);
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) {
161 sigdelset(&mask, s);
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);
167 sa.sa_flags = 0;
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 */
182 sigsuspend(&mask);
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);
190 return 0;
192 #ifdef SHARED
193 strong_alias (__pthread_sigwait, sigwait)
194 #endif
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);
201 if (retcode == 0)
202 return 0;
203 else {
204 errno = retcode;
205 return -1;
208 #ifdef SHARED
209 strong_alias (__pthread_raise, raise)
210 #endif
212 /* This files handles cancellation internally. */
213 LIBC_CANCEL_HANDLED ();