summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-04-07 01:17:04 +0000
committerLaurent Bercot <ska@appnovation.com>2023-04-07 01:17:04 +0000
commit51e253133a87a72cfde9ce6bd8c12544b54721cb (patch)
tree67b532e4342f569cb89967a5c2349acddbfb5133 /src
parent1d152882ecd60c177a09e84db4ea8766945e87b2 (diff)
downloadskalibs-51e253133a87a72cfde9ce6bd8c12544b54721cb.tar.xz
Add selfpipe_fd(), refactor selfpipe
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/selfpipe.h1
-rw-r--r--src/libstddjb/selfpipe-internal.h28
-rw-r--r--src/libstddjb/selfpipe.c184
-rw-r--r--src/libstddjb/selfpipe_finish.c53
-rw-r--r--src/libstddjb/selfpipe_init.c36
-rw-r--r--src/libstddjb/selfpipe_internal.c33
-rw-r--r--src/libstddjb/selfpipe_read.c31
-rw-r--r--src/libstddjb/selfpipe_trap.c46
-rw-r--r--src/libstddjb/selfpipe_trapset.c72
-rw-r--r--src/libstddjb/sig_restoreto.c3
10 files changed, 188 insertions, 299 deletions
diff --git a/src/include/skalibs/selfpipe.h b/src/include/skalibs/selfpipe.h
index 980af90..4b453e0 100644
--- a/src/include/skalibs/selfpipe.h
+++ b/src/include/skalibs/selfpipe.h
@@ -8,6 +8,7 @@
extern int selfpipe_init (void) ;
extern int selfpipe_trap (int) ;
extern int selfpipe_trapset (sigset_t const *) ;
+extern int selfpipe_fd (void) ;
extern int selfpipe_read (void) ;
extern void selfpipe_finish (void) ;
diff --git a/src/libstddjb/selfpipe-internal.h b/src/libstddjb/selfpipe-internal.h
deleted file mode 100644
index dcde350..0000000
--- a/src/libstddjb/selfpipe-internal.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/* ISC license. */
-
-/* MT-unsafe */
-
-#ifndef SELFPIPE_INTERNAL_H
-#define SELFPIPE_INTERNAL_H
-
-#include <signal.h>
-#include <skalibs/sysdeps.h>
-
-extern sigset_t selfpipe_caught ;
-
-#ifdef SKALIBS_HASSIGNALFD
-
-extern int selfpipe_fd ;
-
-#else
-
-#include <skalibs/sig.h>
-
-extern int selfpipe[2] ;
-#define selfpipe_fd selfpipe[0]
-
-extern sig_func selfpipe_tophalf ;
-
-#endif
-
-#endif
diff --git a/src/libstddjb/selfpipe.c b/src/libstddjb/selfpipe.c
new file mode 100644
index 0000000..c4609c8
--- /dev/null
+++ b/src/libstddjb/selfpipe.c
@@ -0,0 +1,184 @@
+/* ISC license. */
+
+/* MT-unsafe */
+
+#include <skalibs/sysdeps.h>
+
+#ifdef SKALIBS_HASSIGNALFD
+
+#include <errno.h>
+#include <signal.h>
+#include <sys/signalfd.h>
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/sig.h>
+#include <skalibs/djbunix.h>
+
+struct selfpipe_s
+{
+ sigset_t caught ;
+ int fd ;
+} ;
+
+static struct selfpipe_s sp = { .fd = -1 } ;
+
+int selfpipe_fd ()
+{
+ return sp.fd ;
+}
+
+int selfpipe_read ()
+{
+ struct signalfd_siginfo buf ;
+ ssize_t r = sanitize_read(fd_read(sp.fd, (char *)&buf, sizeof(struct signalfd_siginfo))) ;
+ return (r <= 0) ? r : buf.ssi_signo ;
+}
+
+int selfpipe_trap (int sig)
+{
+ sigset_t set = sp.caught ;
+ sigset_t old ;
+ if (sp.fd == -1) return (errno = EBADF, 0) ;
+ if (sigaddset(&set, sig) == -1 || sigprocmask(SIG_BLOCK, &set, &old) == -1) return 0 ;
+ if (signalfd(sp.fd, &set, SFD_NONBLOCK | SFD_CLOEXEC) == -1)
+ {
+ int e = errno ;
+ sigprocmask(SIG_SETMASK, &old, 0) ;
+ errno = e ;
+ return 0 ;
+ }
+ sp.caught = set ;
+ return 1 ;
+}
+
+int selfpipe_trapset (sigset_t const *set)
+{
+ sigset_t old ;
+ if (sp.fd == -1) return (errno = EBADF, 0) ;
+ if (sigprocmask(SIG_SETMASK, set, &old) == -1) return 0 ;
+ if (signalfd(sp.fd, set, SFD_NONBLOCK | SFD_CLOEXEC) == -1)
+ {
+ int e = errno ;
+ sigprocmask(SIG_SETMASK, &old, 0) ;
+ errno = e ;
+ return 0 ;
+ }
+ sp.caught = *set ;
+ return 1 ;
+}
+
+void selfpipe_finish ()
+{
+ int e = errno ;
+ fd_close(sp.fd) ; sp.fd = -1 ;
+ sigprocmask(SIG_UNBLOCK, &sp.caught, 0) ;
+ sigemptyset(&sp.caught) ;
+ errno = e ;
+}
+
+int selfpipe_init ()
+{
+ sigemptyset(&sp.caught) ;
+ sig_blocknone() ;
+ sp.fd = signalfd(sp.fd, &sp.caught, SFD_NONBLOCK | SFD_CLOEXEC) ;
+ return sp.fd ;
+}
+
+#else
+
+#include <skalibs/nonposix.h>
+
+#include <errno.h>
+#include <signal.h>
+#include <unistd.h>
+
+#include <skalibs/nsig.h>
+#include <skalibs/sig.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/djbunix.h>
+
+struct selfpipe_s
+{
+ sigset_t caught ;
+ int fd[2] ;
+} ;
+
+static struct selfpipe_s sp = { .fd = { -1, -1 } } ;
+
+static void selfpipe_tophalf (int s)
+{
+ int e = errno ;
+ unsigned char c = (unsigned char)s ;
+ write(sp.fd[1], (char *)&c, 1) ;
+ errno = e ;
+}
+
+int selfpipe_fd ()
+{
+ return sp.fd[0] ;
+}
+
+int selfpipe_read ()
+{
+ char c ;
+ ssize_t r = sanitize_read((fd_read(sp.fd[0], &c, 1))) ;
+ return (r <= 0) ? r : c ;
+}
+
+int selfpipe_trap (int sig)
+{
+ if (sp.fd[0] == -1) return (errno = EBADF, 0) ;
+ if (!sig_catch(sig, &selfpipe_tophalf)) return 0 ;
+ sigaddset(&sp.caught, sig) ;
+ sig_unblock(sig) ;
+ return 1 ;
+}
+
+int selfpipe_trapset (sigset_t const *set)
+{
+ unsigned int i = 1 ;
+ if (sp.fd[0] == -1) return (errno = EBADF, 0) ;
+ for (; i < SKALIBS_NSIG ; i++)
+ {
+ int h = sigismember(set, i) ;
+ if (h < 0) continue ;
+ if (h)
+ {
+ if (!sig_catch(i, &selfpipe_tophalf)) goto err ;
+ }
+ else if (sigismember(&sp.caught, i))
+ {
+ if (!sig_restore(i)) goto err ;
+ }
+ }
+ sig_blocknone() ;
+ sp.caught = *set ;
+ return 1 ;
+
+ err:
+ sig_restoreto(set, i) ;
+ return 0 ;
+}
+
+void selfpipe_finish ()
+{
+ int e = errno ;
+ sigprocmask(SIG_BLOCK, &sp.caught, 0) ;
+ sig_restoreto(&sp.caught, SKALIBS_NSIG) ;
+ fd_close(sp.fd[1]) ;
+ fd_close(sp.fd[0]) ;
+ sigprocmask(SIG_UNBLOCK, &sp.caught, 0) ;
+ sigemptyset(&sp.caught) ;
+ sp.fd[0] = sp.fd[1] = -1 ;
+ errno = e ;
+}
+
+int selfpipe_init ()
+{
+ if (sp.fd[0] >= 0) selfpipe_finish() ;
+ else sigemptyset(&sp.caught) ;
+ sig_blocknone() ;
+ return pipenbcoe(sp.fd) < 0 ? -1 : sp.fd[0] ;
+}
+
+#endif
diff --git a/src/libstddjb/selfpipe_finish.c b/src/libstddjb/selfpipe_finish.c
deleted file mode 100644
index db42430..0000000
--- a/src/libstddjb/selfpipe_finish.c
+++ /dev/null
@@ -1,53 +0,0 @@
-/* ISC license. */
-
-/* MT-unsafe */
-
-#include <skalibs/sysdeps.h>
-
-#ifdef SKALIBS_HASSIGNALFD
-
-#include <errno.h>
-#include <signal.h>
-
-#include <skalibs/djbunix.h>
-#include <skalibs/selfpipe.h>
-#include "selfpipe-internal.h"
-
-void selfpipe_finish (void)
-{
- int e = errno ;
- fd_close(selfpipe_fd) ;
- sigprocmask(SIG_UNBLOCK, &selfpipe_caught, 0) ;
- sigemptyset(&selfpipe_caught) ;
- selfpipe_fd = -1 ;
- errno = e ;
-}
-
-#else
-
-#include <skalibs/nonposix.h>
-#include <skalibs/bsdsnowflake.h>
-
-#include <errno.h>
-#include <signal.h>
-
-#include <skalibs/nsig.h>
-#include <skalibs/sig.h>
-#include <skalibs/djbunix.h>
-#include <skalibs/selfpipe.h>
-#include "selfpipe-internal.h"
-
-void selfpipe_finish (void)
-{
- int e = errno ;
- sigprocmask(SIG_BLOCK, &selfpipe_caught, 0) ;
- sig_restoreto(&selfpipe_caught, SKALIBS_NSIG) ;
- fd_close(selfpipe[1]) ;
- fd_close(selfpipe[0]) ;
- sigprocmask(SIG_UNBLOCK, &selfpipe_caught, 0) ;
- sigemptyset(&selfpipe_caught) ;
- selfpipe[0] = selfpipe[1] = -1 ;
- errno = e ;
-}
-
-#endif
diff --git a/src/libstddjb/selfpipe_init.c b/src/libstddjb/selfpipe_init.c
deleted file mode 100644
index 8dcdcca..0000000
--- a/src/libstddjb/selfpipe_init.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/* ISC license. */
-
-/* MT-unsafe */
-
-#include <signal.h>
-
-#include <skalibs/sysdeps.h>
-#include <skalibs/sig.h>
-#include <skalibs/selfpipe.h>
-#include "selfpipe-internal.h"
-
-#ifdef SKALIBS_HASSIGNALFD
-
-#include <sys/signalfd.h>
-
-int selfpipe_init (void)
-{
- sigemptyset(&selfpipe_caught) ;
- selfpipe_fd = signalfd(selfpipe_fd, &selfpipe_caught, SFD_NONBLOCK | SFD_CLOEXEC) ;
- sig_blocknone() ;
- return selfpipe_fd ;
-}
-
-#else
-
-#include <skalibs/djbunix.h>
-
-int selfpipe_init (void)
-{
- if (selfpipe_fd >= 0) selfpipe_finish() ;
- sigemptyset(&selfpipe_caught) ;
- sig_blocknone() ;
- return pipenbcoe(selfpipe) < 0 ? -1 : selfpipe_fd ;
-}
-
-#endif
diff --git a/src/libstddjb/selfpipe_internal.c b/src/libstddjb/selfpipe_internal.c
deleted file mode 100644
index 45063bc..0000000
--- a/src/libstddjb/selfpipe_internal.c
+++ /dev/null
@@ -1,33 +0,0 @@
-/* ISC license. */
-
-/* MT-unsafe */
-
-#include <signal.h>
-
-#include <skalibs/sysdeps.h>
-#include "selfpipe-internal.h"
-
-sigset_t selfpipe_caught ;
-
-#ifdef SKALIBS_HASSIGNALFD
-
-int selfpipe_fd = -1 ;
-
-#else
-
-#include <unistd.h>
-#include <errno.h>
-
-#include <skalibs/allreadwrite.h>
-
-int selfpipe[2] = { -1, -1 } ;
-
-void selfpipe_tophalf (int s)
-{
- int e = errno ;
- unsigned char c = (unsigned char)s ;
- write(selfpipe[1], (char *)&c, 1) ;
- errno = e ;
-}
-
-#endif
diff --git a/src/libstddjb/selfpipe_read.c b/src/libstddjb/selfpipe_read.c
deleted file mode 100644
index 4bf1988..0000000
--- a/src/libstddjb/selfpipe_read.c
+++ /dev/null
@@ -1,31 +0,0 @@
-/* ISC license. */
-
-/* MT-unsafe */
-
-#include <skalibs/sysdeps.h>
-#include <skalibs/allreadwrite.h>
-#include <skalibs/selfpipe.h>
-#include "selfpipe-internal.h"
-
-#ifdef SKALIBS_HASSIGNALFD
-
-#include <sys/signalfd.h>
-
-int selfpipe_read (void)
-{
- struct signalfd_siginfo buf ;
- ssize_t r = sanitize_read(fd_read(selfpipe_fd, (char *)&buf, sizeof(struct signalfd_siginfo))) ;
- return (r <= 0) ? r : buf.ssi_signo ;
-}
-
-#else
-
-int selfpipe_read (void)
-{
- char c ;
- ssize_t r = sanitize_read((fd_read(selfpipe_fd, &c, 1))) ;
- return (r <= 0) ? r : c ;
-}
-
-#endif
-
diff --git a/src/libstddjb/selfpipe_trap.c b/src/libstddjb/selfpipe_trap.c
deleted file mode 100644
index 9970e8c..0000000
--- a/src/libstddjb/selfpipe_trap.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/* ISC license. */
-
-/* MT-unsafe */
-
-#include <errno.h>
-#include <signal.h>
-
-#include <skalibs/sysdeps.h>
-#include <skalibs/selfpipe.h>
-#include "selfpipe-internal.h"
-
-#ifdef SKALIBS_HASSIGNALFD
-
-#include <sys/signalfd.h>
-
-int selfpipe_trap (int sig)
-{
- sigset_t ss = selfpipe_caught ;
- sigset_t old ;
- if (selfpipe_fd < 0) return (errno = EBADF, 0) ;
- if ((sigaddset(&ss, sig) < 0) || (sigprocmask(SIG_BLOCK, &ss, &old) < 0)) return 0 ;
- if (signalfd(selfpipe_fd, &ss, SFD_NONBLOCK | SFD_CLOEXEC) < 0)
- {
- int e = errno ;
- sigprocmask(SIG_SETMASK, &old, 0) ;
- errno = e ;
- return 0 ;
- }
- selfpipe_caught = ss ;
- return 1 ;
-}
-
-#else
-
-#include <skalibs/sig.h>
-
-int selfpipe_trap (int sig)
-{
- if (selfpipe_fd < 0) return (errno = EBADF, 0) ;
- if (!sig_catch(sig, &selfpipe_tophalf)) return 0 ;
- sigaddset(&selfpipe_caught, sig) ;
- sig_unblock(sig) ;
- return 1 ;
-}
-
-#endif
diff --git a/src/libstddjb/selfpipe_trapset.c b/src/libstddjb/selfpipe_trapset.c
deleted file mode 100644
index 3bb64d4..0000000
--- a/src/libstddjb/selfpipe_trapset.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/sysdeps.h>
-
-#ifdef SKALIBS_HASSIGNALFD
-
-#include <errno.h>
-#include <signal.h>
-#include <sys/signalfd.h>
-
-#include <skalibs/selfpipe.h>
-#include "selfpipe-internal.h"
-
-int selfpipe_trapset (sigset_t const *set)
-{
- sigset_t old ;
- if (selfpipe_fd < 0) return (errno = EBADF, 0) ;
- if (sigprocmask(SIG_SETMASK, set, &old) < 0) return 0 ;
- if (signalfd(selfpipe_fd, set, SFD_NONBLOCK | SFD_CLOEXEC) < 0)
- {
- int e = errno ;
- sigprocmask(SIG_SETMASK, &old, 0) ;
- errno = e ;
- return 0 ;
- }
- selfpipe_caught = *set ;
- return 1 ;
-}
-
-#else
-
-#include <skalibs/nonposix.h>
-
-#include <errno.h>
-#include <signal.h>
-
-#include <skalibs/nsig.h>
-#include <skalibs/sig.h>
-#include <skalibs/selfpipe.h>
-#include "selfpipe-internal.h"
-
-int selfpipe_trapset (sigset_t const *set)
-{
- unsigned int i = 1 ;
- if (selfpipe_fd < 0) return (errno = EBADF, 0) ;
- for (; i < SKALIBS_NSIG ; i++)
- {
- int h = sigismember(set, i) ;
- if (h < 0) continue ;
- if (h)
- {
- if (!sig_catch(i, &selfpipe_tophalf)) goto err ;
- }
- else if (sigismember(&selfpipe_caught, i))
- {
- if (!sig_restore(i)) goto err ;
- }
- }
- sig_blocknone() ;
- selfpipe_caught = *set ;
- return 1 ;
-
- err:
- {
- int e = errno ;
- sig_restoreto(set, i) ;
- errno = e ;
- }
- return 0 ;
-}
-
-#endif
diff --git a/src/libstddjb/sig_restoreto.c b/src/libstddjb/sig_restoreto.c
index 1a9250e..e40f75b 100644
--- a/src/libstddjb/sig_restoreto.c
+++ b/src/libstddjb/sig_restoreto.c
@@ -2,12 +2,14 @@
/* MT-unsafe */
+#include <errno.h>
#include <signal.h>
#include <skalibs/sig.h>
void sig_restoreto (sigset_t const *set, unsigned int n)
{
+ int e = errno ;
unsigned int i = 1 ;
for (; i < n ; i++)
{
@@ -15,4 +17,5 @@ void sig_restoreto (sigset_t const *set, unsigned int n)
if (h < 0) continue ;
if (h) sig_restore(i) ;
}
+ errno = e ;
}