diff options
Diffstat (limited to 'src/libstddjb/selfpipe_trapset.c')
-rw-r--r-- | src/libstddjb/selfpipe_trapset.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/libstddjb/selfpipe_trapset.c b/src/libstddjb/selfpipe_trapset.c new file mode 100644 index 0000000..f8c35ee --- /dev/null +++ b/src/libstddjb/selfpipe_trapset.c @@ -0,0 +1,64 @@ +/* ISC license. */ + +/* MT-unsafe */ + +#include <errno.h> +#include <signal.h> +#include <skalibs/sysdeps.h> +#include "selfpipe-internal.h" +#include <skalibs/selfpipe.h> + +#ifdef SKALIBS_HASSIGNALFD + +#include <sys/signalfd.h> + +int selfpipe_trapset (sigset_t const *set) +{ + sigset_t old ; + if (selfpipe_fd < 0) return (errno = EBADF, -1) ; + if (sigprocmask(SIG_SETMASK, set, &old) < 0) return -1 ; + if (signalfd(selfpipe_fd, set, SFD_NONBLOCK | SFD_CLOEXEC) < 0) + { + int e = errno ; + sigprocmask(SIG_SETMASK, &old, 0) ; + errno = e ; + return -1 ; + } + selfpipe_caught = *set ; + return 0 ; +} + +#else + +#include <skalibs/sig.h> +#include <skalibs/nsig.h> + +int selfpipe_trapset (sigset_t const *set) +{ + unsigned int i = 1 ; + if (selfpipe_fd < 0) return (errno = EBADF, -1) ; + for (; i <= NSIG ; i++) + { + register int h = sigismember(set, i) ; + if (h < 0) continue ; + if (h) + { + if (sig_catcha(i, &selfpipe_ssa) < 0) break ; + } + else if (sigismember(&selfpipe_caught, i)) + { + if (sig_restore(i) < 0) break ; + } + } + if (i <= NSIG) + { + int e = errno ; + sig_restoreto(set, i) ; + errno = e ; + return -1 ; + } + selfpipe_caught = *set ; + return 0 ; +} + +#endif |