1 /* POSIX compatible signal blocking.
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "sigprocmask.h"
28 /* We assume that a platform without POSIX signal blocking functions also
29 does not have the POSIX sigaction() function, only the signal() function.
30 This is true for Woe32 platforms. */
32 /* A signal handler. */
33 typedef void (*handler_t) (int signal);
36 sigismember (const sigset_t *set, int sig)
38 if (sig >= 0 && sig < NSIG)
39 return (*set >> sig) & 1;
45 sigemptyset (sigset_t *set)
52 sigaddset (sigset_t *set, int sig)
54 if (sig >= 0 && sig < NSIG)
67 sigdelset (sigset_t *set, int sig)
69 if (sig >= 0 && sig < NSIG)
82 sigfillset (sigset_t *set)
84 *set = (2U << (NSIG - 1)) - 1;
88 /* Set of currently blocked signals. */
89 static sigset_t blocked_set /* = 0 */;
91 /* Set of currently blocked and pending signals. */
92 static volatile sig_atomic_t pending_array[NSIG] /* = { 0 } */;
94 /* Signal handler that is installed for blocked signals. */
96 blocked_handler (int sig)
98 if (sig >= 0 && sig < NSIG)
99 pending_array[sig] = 1;
103 sigpending (sigset_t *set)
105 sigset_t pending = 0;
108 for (sig = 0; sig < NSIG; sig++)
109 if (pending_array[sig])
110 pending |= 1U << sig;
114 /* The previous signal handlers.
115 Only the array elements corresponding to blocked signals are relevant. */
116 static handler_t old_handlers[NSIG];
119 sigprocmask (int operation, const sigset_t *set, sigset_t *old_set)
122 *old_set = blocked_set;
126 sigset_t new_blocked_set;
133 new_blocked_set = blocked_set | *set;
136 new_blocked_set = *set;
139 new_blocked_set = blocked_set & ~*set;
145 to_unblock = blocked_set & ~new_blocked_set;
146 to_block = new_blocked_set & ~blocked_set;
152 for (sig = 0; sig < NSIG; sig++)
153 if ((to_block >> sig) & 1)
155 pending_array[sig] = 0;
156 if ((old_handlers[sig] = signal (sig, blocked_handler)) != SIG_ERR)
157 blocked_set |= 1U << sig;
163 sig_atomic_t received[NSIG];
166 for (sig = 0; sig < NSIG; sig++)
167 if ((to_unblock >> sig) & 1)
169 if (signal (sig, old_handlers[sig]) != blocked_handler)
170 /* The application changed a signal handler while the signal
171 was blocked. We don't support this. */
173 received[sig] = pending_array[sig];
174 blocked_set &= ~(1U << sig);
175 pending_array[sig] = 0;
180 for (sig = 0; sig < NSIG; sig++)
186 kill (getpid (), sig);