diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2016-01-18 23:56:21 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2016-01-18 23:56:21 +0000 |
commit | c9c7e241a7b68f67e0cc8a7d3874510dced532d6 (patch) | |
tree | b4615c00c3f3c2f7265013b153688496df21f387 /src | |
parent | db086527035619204569f79672b7a01fc26d8aa4 (diff) | |
download | skalibs-c9c7e241a7b68f67e0cc8a7d3874510dced532d6.tar.xz |
Add support for advanced closing callbacks to unixmessage_sender_flush.
(Typically, this will be used for refcounting when broadcasting copies of
the same fd.)
Diffstat (limited to 'src')
-rw-r--r-- | src/include/skalibs/unixmessage.h | 10 | ||||
-rw-r--r-- | src/libunixonacid/unixmessage_sender_closecb.c | 10 | ||||
-rw-r--r-- | src/libunixonacid/unixmessage_sender_flush.c | 4 | ||||
-rw-r--r-- | src/libunixonacid/unixmessage_sender_init.c | 9 | ||||
-rw-r--r-- | src/libunixonacid/unixmessage_sender_init_withclosecb.c | 17 |
5 files changed, 39 insertions, 11 deletions
diff --git a/src/include/skalibs/unixmessage.h b/src/include/skalibs/unixmessage.h index 51d48bc..f971a65 100644 --- a/src/include/skalibs/unixmessage.h +++ b/src/include/skalibs/unixmessage.h @@ -49,6 +49,9 @@ extern unixmessage_v_t const unixmessage_v_zero ; /* Sender */ +typedef void unixmessage_sender_closecb_func_t (int, void *) ; +typedef unixmessage_sender_closecb_func_t *unixmessage_sender_closecb_func_t_ref ; + typedef struct unixmessage_sender_s unixmessage_sender_t, *unixmessage_sender_t_ref ; struct unixmessage_sender_s { @@ -58,12 +61,17 @@ struct unixmessage_sender_s genalloc offsets ; /* diuint */ unsigned int head ; unsigned int shorty ; + unixmessage_sender_closecb_func_t_ref closecb ; + void *closecbdata ; } ; #define UNIXMESSAGE_SENDER_ZERO UNIXMESSAGE_SENDER_INIT(-1) -#define UNIXMESSAGE_SENDER_INIT(s) { .fd = (s), .data = STRALLOC_ZERO, .fds = GENALLOC_ZERO, .offsets = GENALLOC_ZERO, .head = 0, .shorty = 0 } +#define UNIXMESSAGE_SENDER_INIT(s) UNIXMESSAGE_SENDER_INIT_WITHCLOSECB((s), &unixmessage_sender_closecb, 0) +#define UNIXMESSAGE_SENDER_INIT_WITHCLOSECB(s, f, p) { .fd = (s), .data = STRALLOC_ZERO, .fds = GENALLOC_ZERO, .offsets = GENALLOC_ZERO, .head = 0, .shorty = 0, .closecb = (f), .closecbdata = (p) } extern unixmessage_sender_t const unixmessage_sender_zero ; +extern unixmessage_sender_closecb_func_t unixmessage_sender_closecb ; extern void unixmessage_sender_init (unixmessage_sender_t *, int) ; +extern void unixmessage_sender_init_withclosecb (unixmessage_sender_t *, int, unixmessage_sender_closecb_func_t_ref, void *) ; extern void unixmessage_sender_free (unixmessage_sender_t *) ; #define unixmessage_sender_fd(b) ((b)->fd) extern int unixmessage_sender_getfd (unixmessage_sender_t const *) gccattr_pure ; diff --git a/src/libunixonacid/unixmessage_sender_closecb.c b/src/libunixonacid/unixmessage_sender_closecb.c new file mode 100644 index 0000000..1234ff5 --- /dev/null +++ b/src/libunixonacid/unixmessage_sender_closecb.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include <skalibs/djbunix.h> +#include <skalibs/unixmessage.h> + +void unixmessage_sender_closecb (int fd, void *p) +{ + fd_close(fd) ; + (void)p ; +} diff --git a/src/libunixonacid/unixmessage_sender_flush.c b/src/libunixonacid/unixmessage_sender_flush.c index 75412fb..6559083 100644 --- a/src/libunixonacid/unixmessage_sender_flush.c +++ b/src/libunixonacid/unixmessage_sender_flush.c @@ -97,13 +97,13 @@ int unixmessage_sender_flush (unixmessage_sender_t *b) while (r < 0 && errno == EINTR) ; if (r <= 0) return 0 ; #ifndef SKALIBS_HASANCILAUTOCLOSE - if (nfds) + if (nfds && b->closecb) { register unsigned int i = 0 ; for (; i < nfds ; i++) { register int fd = genalloc_s(int, &b->fds)[offsets[b->head].right + i] ; - if (fd < 0) fd_close(-(fd+1)) ; + if (fd < 0) (*b->closecb)(-(fd+1), b->closecbdata) ; } } #endif diff --git a/src/libunixonacid/unixmessage_sender_init.c b/src/libunixonacid/unixmessage_sender_init.c index d986f29..71a8b3b 100644 --- a/src/libunixonacid/unixmessage_sender_init.c +++ b/src/libunixonacid/unixmessage_sender_init.c @@ -1,15 +1,8 @@ /* ISC license. */ -#include <skalibs/stralloc.h> -#include <skalibs/genalloc.h> #include <skalibs/unixmessage.h> void unixmessage_sender_init (unixmessage_sender_t *b, int fd) { - b->fd = fd ; - b->data = stralloc_zero ; - b->fds = genalloc_zero ; - b->offsets = genalloc_zero ; - b->head = 0 ; - b->shorty = 0 ; + unixmessage_sender_init_withclosecb(b, fd, &unixmessage_sender_closecb, 0) ; } diff --git a/src/libunixonacid/unixmessage_sender_init_withclosecb.c b/src/libunixonacid/unixmessage_sender_init_withclosecb.c new file mode 100644 index 0000000..32743ec --- /dev/null +++ b/src/libunixonacid/unixmessage_sender_init_withclosecb.c @@ -0,0 +1,17 @@ +/* ISC license. */ + +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/unixmessage.h> + +void unixmessage_sender_init_withclosecb (unixmessage_sender_t *b, int fd, unixmessage_sender_closecb_func_t_ref f, void *p) +{ + b->fd = fd ; + b->data = stralloc_zero ; + b->fds = genalloc_zero ; + b->offsets = genalloc_zero ; + b->head = 0 ; + b->shorty = 0 ; + b->closecb = f ; + b->closecbdata = 0 ; +} |