123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- #include <config.h>
- #include <signal.h>
- #include <errno.h>
- #include <stdint.h>
- #include <stdlib.h>
- #undef signal
- #ifndef SIGKILL
- # define SIGKILL (-1)
- #endif
- #ifndef SIGSTOP
- # define SIGSTOP (-1)
- #endif
- #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
- # undef SIGABRT_COMPAT
- # define SIGABRT_COMPAT 6
- #endif
- #ifdef SIGABRT_COMPAT
- # define SIGABRT_COMPAT_MASK (1U << SIGABRT_COMPAT)
- #else
- # define SIGABRT_COMPAT_MASK 0
- #endif
- typedef void (*handler_t) (int);
- #if GNULIB_defined_SIGPIPE
- static handler_t SIGPIPE_handler = SIG_DFL;
- #endif
- #if GNULIB_defined_SIGPIPE
- static handler_t
- ext_signal (int sig, handler_t handler)
- {
- switch (sig)
- {
- case SIGPIPE:
- {
- handler_t old_handler = SIGPIPE_handler;
- SIGPIPE_handler = handler;
- return old_handler;
- }
- default:
- return signal (sig, handler);
- }
- }
- # define signal ext_signal
- #endif
- int
- sigismember (const sigset_t *set, int sig)
- {
- if (sig >= 0 && sig < NSIG)
- {
- #ifdef SIGABRT_COMPAT
- if (sig == SIGABRT_COMPAT)
- sig = SIGABRT;
- #endif
- return (*set >> sig) & 1;
- }
- else
- return 0;
- }
- int
- sigemptyset (sigset_t *set)
- {
- *set = 0;
- return 0;
- }
- int
- sigaddset (sigset_t *set, int sig)
- {
- if (sig >= 0 && sig < NSIG)
- {
- #ifdef SIGABRT_COMPAT
- if (sig == SIGABRT_COMPAT)
- sig = SIGABRT;
- #endif
- *set |= 1U << sig;
- return 0;
- }
- else
- {
- errno = EINVAL;
- return -1;
- }
- }
- int
- sigdelset (sigset_t *set, int sig)
- {
- if (sig >= 0 && sig < NSIG)
- {
- #ifdef SIGABRT_COMPAT
- if (sig == SIGABRT_COMPAT)
- sig = SIGABRT;
- #endif
- *set &= ~(1U << sig);
- return 0;
- }
- else
- {
- errno = EINVAL;
- return -1;
- }
- }
- int
- sigfillset (sigset_t *set)
- {
- *set = ((2U << (NSIG - 1)) - 1) & ~ SIGABRT_COMPAT_MASK;
- return 0;
- }
- static volatile sigset_t blocked_set ;
- static volatile sig_atomic_t pending_array[NSIG] ;
- static void
- blocked_handler (int sig)
- {
-
- signal (sig, blocked_handler);
- if (sig >= 0 && sig < NSIG)
- pending_array[sig] = 1;
- }
- int
- sigpending (sigset_t *set)
- {
- sigset_t pending = 0;
- int sig;
- for (sig = 0; sig < NSIG; sig++)
- if (pending_array[sig])
- pending |= 1U << sig;
- *set = pending;
- return 0;
- }
- static volatile handler_t old_handlers[NSIG];
- int
- sigprocmask (int operation, const sigset_t *set, sigset_t *old_set)
- {
- if (old_set != NULL)
- *old_set = blocked_set;
- if (set != NULL)
- {
- sigset_t new_blocked_set;
- sigset_t to_unblock;
- sigset_t to_block;
- switch (operation)
- {
- case SIG_BLOCK:
- new_blocked_set = blocked_set | *set;
- break;
- case SIG_SETMASK:
- new_blocked_set = *set;
- break;
- case SIG_UNBLOCK:
- new_blocked_set = blocked_set & ~*set;
- break;
- default:
- errno = EINVAL;
- return -1;
- }
- to_unblock = blocked_set & ~new_blocked_set;
- to_block = new_blocked_set & ~blocked_set;
- if (to_block != 0)
- {
- int sig;
- for (sig = 0; sig < NSIG; sig++)
- if ((to_block >> sig) & 1)
- {
- pending_array[sig] = 0;
- if ((old_handlers[sig] = signal (sig, blocked_handler)) != SIG_ERR)
- blocked_set |= 1U << sig;
- }
- }
- if (to_unblock != 0)
- {
- sig_atomic_t received[NSIG];
- int sig;
- for (sig = 0; sig < NSIG; sig++)
- if ((to_unblock >> sig) & 1)
- {
- if (signal (sig, old_handlers[sig]) != blocked_handler)
-
- abort ();
- received[sig] = pending_array[sig];
- blocked_set &= ~(1U << sig);
- pending_array[sig] = 0;
- }
- else
- received[sig] = 0;
- for (sig = 0; sig < NSIG; sig++)
- if (received[sig])
- raise (sig);
- }
- }
- return 0;
- }
- handler_t
- rpl_signal (int sig, handler_t handler)
- {
-
- if (sig >= 0 && sig < NSIG && sig != SIGKILL && sig != SIGSTOP
- && handler != SIG_ERR)
- {
- #ifdef SIGABRT_COMPAT
- if (sig == SIGABRT_COMPAT)
- sig = SIGABRT;
- #endif
- if (blocked_set & (1U << sig))
- {
-
- handler_t result = old_handlers[sig];
- old_handlers[sig] = handler;
- return result;
- }
- else
- return signal (sig, handler);
- }
- else
- {
- errno = EINVAL;
- return SIG_ERR;
- }
- }
- #if GNULIB_defined_SIGPIPE
- int
- rpl_raise (int sig)
- # undef raise
- {
- switch (sig)
- {
- case SIGPIPE:
- if (blocked_set & (1U << sig))
- pending_array[sig] = 1;
- else
- {
- handler_t handler = SIGPIPE_handler;
- if (handler == SIG_DFL)
- exit (128 + SIGPIPE);
- else if (handler != SIG_IGN)
- (*handler) (sig);
- }
- return 0;
- default:
- return raise (sig);
- }
- }
- #endif
|