diff options
-rw-r--r-- | NEWS | 6 | ||||
-rw-r--r-- | doc/index.html | 2 | ||||
-rw-r--r-- | doc/libstddjb/selfpipe.html | 15 | ||||
-rw-r--r-- | doc/upgrade.html | 6 | ||||
-rw-r--r-- | package/deps.mak | 8 | ||||
-rw-r--r-- | package/info | 2 | ||||
-rw-r--r-- | src/include/skalibs/selfpipe.h | 1 | ||||
-rw-r--r-- | src/libstddjb/selfpipe-internal.h | 28 | ||||
-rw-r--r-- | src/libstddjb/selfpipe.c | 184 | ||||
-rw-r--r-- | src/libstddjb/selfpipe_finish.c | 53 | ||||
-rw-r--r-- | src/libstddjb/selfpipe_init.c | 36 | ||||
-rw-r--r-- | src/libstddjb/selfpipe_internal.c | 33 | ||||
-rw-r--r-- | src/libstddjb/selfpipe_read.c | 31 | ||||
-rw-r--r-- | src/libstddjb/selfpipe_trap.c | 46 | ||||
-rw-r--r-- | src/libstddjb/selfpipe_trapset.c | 72 | ||||
-rw-r--r-- | src/libstddjb/sig_restoreto.c | 3 |
16 files changed, 218 insertions, 308 deletions
@@ -1,5 +1,11 @@ Changelog for skalibs. +In 2.13.2.0 +----------- + + - New accessor function: selfpipe_fd(). + + In 2.13.1.1 ----------- diff --git a/doc/index.html b/doc/index.html index d61c875..7b7ce35 100644 --- a/doc/index.html +++ b/doc/index.html @@ -60,7 +60,7 @@ with a standard C development environment </li> <h3> Download </h3> <ul> - <li> The current released version of skalibs is <a href="skalibs-2.13.1.1.tar.gz">2.13.1.1</a>. </li> + <li> The current released version of skalibs is <a href="skalibs-2.13.2.0.tar.gz">2.13.2.0</a>. </li> <li> Alternatively, you can checkout a copy of the <a href="//git.skarnet.org/cgi-bin/cgit.cgi/skalibs/">skalibs git repository</a>: diff --git a/doc/libstddjb/selfpipe.html b/doc/libstddjb/selfpipe.html index 7c13e0e..54c0f36 100644 --- a/doc/libstddjb/selfpipe.html +++ b/doc/libstddjb/selfpipe.html @@ -182,6 +182,21 @@ it's a serious one and your system has become very unstable. is positive, it is the number of the signal that was caught. </p> +<h3> Accessing the selfpipe </h3> + +<pre> +int fd = selfpipe_fd() ; +</pre> + +<p> + Sometimes you need to access the fd of the selfpipe in two +very distinct translation units (typically to poll on it), and you +rightly don't want to add a global variable to store it, especially +since it's already stored in a global internal variable in skalibs. +No need to bloat your binary anymore: <tt>selfpipe_fd()</tt> will +now retrieve the value for you, wherever you are. +</p> + <h3> Finishing </h3> <pre> diff --git a/doc/upgrade.html b/doc/upgrade.html index 66cecd3..ed83b9b 100644 --- a/doc/upgrade.html +++ b/doc/upgrade.html @@ -17,6 +17,12 @@ </p> +<h2> in 2.13.2.0 </h2> + +<ul> + <li> New accessor function: <tt>selfpipe_fd()</tt> </li> +</ul> + <h2> in 2.13.1.1 </h2> <ul> diff --git a/package/deps.mak b/package/deps.mak index f792c63..787e042 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -65,7 +65,6 @@ src/libstdcrypto/sha512-internal.h: src/include/skalibs/sha512.h src/libstddjb/cdb-internal.h: src/include/skalibs/cdb.h src/include/skalibs/gccattributes.h src/libstddjb/djbtime-internal.h: src/include/skalibs/uint64.h src/libstddjb/fmtscan-internal.h: src/include/skalibs/fmtscan.h src/include/skalibs/uint64.h -src/libstddjb/selfpipe-internal.h: src/include/skalibs/sig.h src/include/skalibs/sysdeps.h src/libunixonacid/skaclient-internal.h: src/include/skalibs/kolbak.h src/include/skalibs/skaclient.h src/include/skalibs/unixmessage.h src/libdatastruct/avlnode_delete.o src/libdatastruct/avlnode_delete.lo: src/libdatastruct/avlnode_delete.c src/libdatastruct/avlnode-internal.h src/include/skalibs/avlnode.h src/libdatastruct/avlnode_doublerotate.o src/libdatastruct/avlnode_doublerotate.lo: src/libdatastruct/avlnode_doublerotate.c src/libdatastruct/avlnode-internal.h src/include/skalibs/avlnode.h @@ -464,12 +463,7 @@ src/libstddjb/sareadlink.o src/libstddjb/sareadlink.lo: src/libstddjb/sareadlink src/libstddjb/sarealpath.o src/libstddjb/sarealpath.lo: src/libstddjb/sarealpath.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h src/libstddjb/satmp.o src/libstddjb/satmp.lo: src/libstddjb/satmp.c src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h src/libstddjb/sauniquename.o src/libstddjb/sauniquename.lo: src/libstddjb/sauniquename.c src/include/skalibs/djbunix.h src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h src/include/skalibs/types.h -src/libstddjb/selfpipe_finish.o src/libstddjb/selfpipe_finish.lo: src/libstddjb/selfpipe_finish.c src/include/skalibs/bsdsnowflake.h src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/nsig.h src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h -src/libstddjb/selfpipe_init.o src/libstddjb/selfpipe_init.lo: src/libstddjb/selfpipe_init.c src/include/skalibs/djbunix.h src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h -src/libstddjb/selfpipe_internal.o src/libstddjb/selfpipe_internal.lo: src/libstddjb/selfpipe_internal.c src/include/skalibs/allreadwrite.h src/libstddjb/selfpipe-internal.h src/include/skalibs/sysdeps.h -src/libstddjb/selfpipe_read.o src/libstddjb/selfpipe_read.lo: src/libstddjb/selfpipe_read.c src/include/skalibs/allreadwrite.h src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sysdeps.h -src/libstddjb/selfpipe_trap.o src/libstddjb/selfpipe_trap.lo: src/libstddjb/selfpipe_trap.c src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h -src/libstddjb/selfpipe_trapset.o src/libstddjb/selfpipe_trapset.lo: src/libstddjb/selfpipe_trapset.c src/include/skalibs/nonposix.h src/include/skalibs/nsig.h src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h +src/libstddjb/selfpipe.o src/libstddjb/selfpipe.lo: src/libstddjb/selfpipe.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/nsig.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h src/libstddjb/sgetopt.o src/libstddjb/sgetopt.lo: src/libstddjb/sgetopt.c src/include/skalibs/buffer.h src/include/skalibs/sgetopt.h src/libstddjb/sig0_scan.o src/libstddjb/sig0_scan.lo: src/libstddjb/sig0_scan.c src/include/skalibs/sig.h src/include/skalibs/types.h src/libstddjb/sig_altignore.o src/libstddjb/sig_altignore.lo: src/libstddjb/sig_altignore.c src/include/skalibs/sig.h diff --git a/package/info b/package/info index 0750d09..dea00a7 100644 --- a/package/info +++ b/package/info @@ -1,4 +1,4 @@ package=skalibs -version=2.13.1.1 +version=2.13.2.0 category=prog package_macro_name=SKALIBS 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 ; } |