From 7530e8cdd506ecec1f4ad3bbd55f94de5a6d63ac Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 26 Oct 2020 14:21:53 +0000 Subject: Separate and expose ancil_recv_fd/ancil_send_fd The goal is to make late channel creation easy, as opposed to textclient which always creates a new channel at start time. This commit also moves posixishard.h inclusions as late as possible. --- src/include/skalibs/ancil.h | 9 +++ src/include/skalibs/unixonacid.h | 1 + src/libposixplz/setgroups.c | 4 +- src/libstddjb/cdb_read.c | 2 +- src/libstddjb/ipc_bind.c | 2 +- src/libstddjb/ipc_connect.c | 2 +- src/libstddjb/ipc_recv.c | 2 +- src/libstddjb/ipc_send.c | 2 +- src/libstddjb/netstring_get.c | 2 +- src/libstddjb/string_unquote_withdelim.c | 2 +- src/libunixonacid/ancil_recv_fd.c | 69 ++++++++++++++++++++++ src/libunixonacid/ancil_send_fd.c | 45 ++++++++++++++ src/libunixonacid/ipc_timed_send.c | 2 +- src/libunixonacid/ipc_timed_sendv.c | 2 +- src/libunixonacid/skaclient_default_cb.c | 2 +- src/libunixonacid/skaclient_server_ack.c | 2 +- src/libunixonacid/skaclient_start_cb.c | 2 +- src/libunixonacid/textclient_command.c | 2 +- src/libunixonacid/textclient_commandv.c | 2 +- .../textclient_server_init_frompipe.c | 2 +- .../textclient_server_init_fromsocket.c | 41 ++----------- src/libunixonacid/textclient_start.c | 65 +++----------------- src/libunixonacid/textclient_startf.c | 2 +- src/libunixonacid/unixmessage_put.c | 2 +- src/libunixonacid/unixmessage_receive.c | 2 +- src/libunixonacid/unixmessage_sender_flush.c | 2 +- 26 files changed, 157 insertions(+), 115 deletions(-) create mode 100644 src/include/skalibs/ancil.h create mode 100644 src/libunixonacid/ancil_recv_fd.c create mode 100644 src/libunixonacid/ancil_send_fd.c (limited to 'src') diff --git a/src/include/skalibs/ancil.h b/src/include/skalibs/ancil.h new file mode 100644 index 0000000..92270eb --- /dev/null +++ b/src/include/skalibs/ancil.h @@ -0,0 +1,9 @@ +/* ISC license. */ + +#ifndef SKALIBS_ANCIL_H +#define SKALIBS_ANCIL_H + +extern int ancil_recv_fd (int, char) ; +extern int ancil_send_fd (int, int, char) ; + +#endif diff --git a/src/include/skalibs/unixonacid.h b/src/include/skalibs/unixonacid.h index 7132f1c..76aca9d 100644 --- a/src/include/skalibs/unixonacid.h +++ b/src/include/skalibs/unixonacid.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include diff --git a/src/libposixplz/setgroups.c b/src/libposixplz/setgroups.c index fff507f..0c32222 100644 --- a/src/libposixplz/setgroups.c +++ b/src/libposixplz/setgroups.c @@ -5,11 +5,13 @@ #ifdef SKALIBS_HASSETGROUPS #include -#include + #include #include #include + #include +#include int setgroups_and_gid (gid_t g, size_t n, gid_t const *tab) { diff --git a/src/libstddjb/cdb_read.c b/src/libstddjb/cdb_read.c index 85e8dea..1d2f491 100644 --- a/src/libstddjb/cdb_read.c +++ b/src/libstddjb/cdb_read.c @@ -4,9 +4,9 @@ #include #include -#include #include #include +#include int cdb_read (struct cdb *c, char *buf, unsigned int len, uint32_t pos) { diff --git a/src/libstddjb/ipc_bind.c b/src/libstddjb/ipc_bind.c index b3388d7..d40a7ef 100644 --- a/src/libstddjb/ipc_bind.c +++ b/src/libstddjb/ipc_bind.c @@ -7,8 +7,8 @@ #include #include -#include #include +#include int ipc_bind (int s, char const *p) { diff --git a/src/libstddjb/ipc_connect.c b/src/libstddjb/ipc_connect.c index bb0422e..c00f683 100644 --- a/src/libstddjb/ipc_connect.c +++ b/src/libstddjb/ipc_connect.c @@ -7,8 +7,8 @@ #include #include -#include #include +#include int ipc_connect (int s, char const *p) { diff --git a/src/libstddjb/ipc_recv.c b/src/libstddjb/ipc_recv.c index e15e3c5..4c235bb 100644 --- a/src/libstddjb/ipc_recv.c +++ b/src/libstddjb/ipc_recv.c @@ -7,8 +7,8 @@ #include #include -#include #include +#include ssize_t ipc_recv (int fd, char *s, size_t len, char *path) { diff --git a/src/libstddjb/ipc_send.c b/src/libstddjb/ipc_send.c index 65826a6..2615a16 100644 --- a/src/libstddjb/ipc_send.c +++ b/src/libstddjb/ipc_send.c @@ -7,8 +7,8 @@ #include #include -#include #include +#include ssize_t ipc_send (int fd, char const *s, size_t len, char const *path) { diff --git a/src/libstddjb/netstring_get.c b/src/libstddjb/netstring_get.c index 0dfa907..32a6c49 100644 --- a/src/libstddjb/netstring_get.c +++ b/src/libstddjb/netstring_get.c @@ -2,13 +2,13 @@ #include -#include #include #include #include #include #include #include +#include int netstring_okeof (buffer *b, size_t w) { diff --git a/src/libstddjb/string_unquote_withdelim.c b/src/libstddjb/string_unquote_withdelim.c index 702d3a5..43da0db 100644 --- a/src/libstddjb/string_unquote_withdelim.c +++ b/src/libstddjb/string_unquote_withdelim.c @@ -2,10 +2,10 @@ #include -#include #include #include #include +#include #define PUSH0 0x40 #define PUSH 0x20 diff --git a/src/libunixonacid/ancil_recv_fd.c b/src/libunixonacid/ancil_recv_fd.c new file mode 100644 index 0000000..70f2c4f --- /dev/null +++ b/src/libunixonacid/ancil_recv_fd.c @@ -0,0 +1,69 @@ +/* ISC license. */ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +union aligner_u +{ + struct cmsghdr cmsghdr ; + int i ; +} ; + +int ancil_recv_fd (int sock, char expected_ch) +{ + static int const awesomeflags = +#ifdef SKALIBS_HASMSGDONTWAIT + MSG_DONTWAIT +#else + 0 +#endif + | +#ifdef SKALIBS_HASCMSGCLOEXEC + MSG_CMSG_CLOEXEC +#else + 0 +#endif + ; + struct cmsghdr *c ; + ssize_t r ; + char ch ; + struct iovec v = { .iov_base = &ch, .iov_len = 1 } ; + union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ; + struct msghdr msghdr = + { + .msg_name = 0, + .msg_namelen = 0, + .msg_iov = &v, + .msg_iovlen = 1, + .msg_flags = 0, + .msg_control = ancilbuf, + .msg_controllen = CMSG_SPACE(sizeof(int)) + } ; + do r = recvmsg(sock, &msghdr, awesomeflags) ; + while (r < 0 && errno == EINTR) ; + if (r < 0) return r ; + if (!r) return (errno = EPIPE, -1) ; + c = CMSG_FIRSTHDR(&msghdr) ; + if (ch != expected_ch + || !c + || c->cmsg_level != SOL_SOCKET + || c->cmsg_type != SCM_RIGHTS + || (size_t)(c->cmsg_len - (CMSG_DATA(c) - (unsigned char *)c)) != sizeof(int)) return (errno = EPROTO, -1) ; +#ifndef SKALIBS_HASCMSGCLOEXEC + if (coe(*(int *)CMSG_DATA(c)) < 0) + { + fd_close(*(int *)CMSG_DATA(c)) ; + return -1 ; + } +#endif + return *(int *)CMSG_DATA(c) ; +} diff --git a/src/libunixonacid/ancil_send_fd.c b/src/libunixonacid/ancil_send_fd.c new file mode 100644 index 0000000..98366c7 --- /dev/null +++ b/src/libunixonacid/ancil_send_fd.c @@ -0,0 +1,45 @@ +/* ISC license. */ + +#include + +#include +#include +#include +#include + +#include +#include +#include + +union aligner_u +{ + struct cmsghdr cmsghdr ; + int i ; +} ; + +int ancil_send_fd (int sock, int fd, char ch) +{ + ssize_t r ; + struct iovec v = { .iov_base = &ch, .iov_len = 1 } ; + union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ; + struct msghdr hdr = + { + .msg_name = 0, + .msg_namelen = 0, + .msg_iov = &v, + .msg_iovlen = 1, + .msg_control = ancilbuf, + .msg_controllen = CMSG_SPACE(sizeof(int)) + } ; + struct cmsghdr *c = CMSG_FIRSTHDR(&hdr) ; + memset(hdr.msg_control, 0, hdr.msg_controllen) ; + c->cmsg_level = SOL_SOCKET ; + c->cmsg_type = SCM_RIGHTS ; + c->cmsg_len = CMSG_LEN(sizeof(int)) ; + *(int *)CMSG_DATA(c) = fd ; + do r = sendmsg(sock, &hdr, MSG_NOSIGNAL) ; + while (r < 0 && errno == EINTR) ; + if (r <= 0) return 0 ; + fd_close(fd) ; + return 1 ; +} diff --git a/src/libunixonacid/ipc_timed_send.c b/src/libunixonacid/ipc_timed_send.c index e1e75a4..2f6ce88 100644 --- a/src/libunixonacid/ipc_timed_send.c +++ b/src/libunixonacid/ipc_timed_send.c @@ -5,10 +5,10 @@ #include #include -#include #include #include #include +#include int ipc_timed_send (int fd, char const *s, size_t len, tain_t const *deadline, tain_t *stamp) { diff --git a/src/libunixonacid/ipc_timed_sendv.c b/src/libunixonacid/ipc_timed_sendv.c index 0fe5b1e..1bdae5b 100644 --- a/src/libunixonacid/ipc_timed_sendv.c +++ b/src/libunixonacid/ipc_timed_sendv.c @@ -5,11 +5,11 @@ #include #include -#include #include #include #include #include +#include int ipc_timed_sendv (int fd, struct iovec const *v, unsigned int n, tain_t const *deadline, tain_t *stamp) { diff --git a/src/libunixonacid/skaclient_default_cb.c b/src/libunixonacid/skaclient_default_cb.c index 5bf0268..447d796 100644 --- a/src/libunixonacid/skaclient_default_cb.c +++ b/src/libunixonacid/skaclient_default_cb.c @@ -2,9 +2,9 @@ #include -#include #include #include +#include int skaclient_default_cb (unixmessage_t const *m, void *p) { diff --git a/src/libunixonacid/skaclient_server_ack.c b/src/libunixonacid/skaclient_server_ack.c index b55eb07..9bad7cc 100644 --- a/src/libunixonacid/skaclient_server_ack.c +++ b/src/libunixonacid/skaclient_server_ack.c @@ -3,11 +3,11 @@ #include #include -#include #include #include #include #include +#include int skaclient_server_ack (unixmessage_t const *clientmsg, unixmessage_sender_t *out, unixmessage_sender_t *asyncout, char const *before, size_t beforelen, char const *after, size_t afterlen) { diff --git a/src/libunixonacid/skaclient_start_cb.c b/src/libunixonacid/skaclient_start_cb.c index 0c4ef49..f385b6b 100644 --- a/src/libunixonacid/skaclient_start_cb.c +++ b/src/libunixonacid/skaclient_start_cb.c @@ -3,9 +3,9 @@ #include #include -#include #include #include +#include #include "skaclient-internal.h" int skaclient_start_cb (unixmessage_t const *m, skaclient_cbdata_t *blah) diff --git a/src/libunixonacid/textclient_command.c b/src/libunixonacid/textclient_command.c index 56c7122..a0015ac 100644 --- a/src/libunixonacid/textclient_command.c +++ b/src/libunixonacid/textclient_command.c @@ -3,8 +3,8 @@ #include #include -#include #include +#include int textclient_command (textclient_t *a, char const *s, size_t len, tain_t const *deadline, tain_t *stamp) { diff --git a/src/libunixonacid/textclient_commandv.c b/src/libunixonacid/textclient_commandv.c index ec61498..267e584 100644 --- a/src/libunixonacid/textclient_commandv.c +++ b/src/libunixonacid/textclient_commandv.c @@ -3,8 +3,8 @@ #include #include -#include #include +#include int textclient_commandv (textclient_t *a, struct iovec const *v, unsigned int n, tain_t const *deadline, tain_t *stamp) { diff --git a/src/libunixonacid/textclient_server_init_frompipe.c b/src/libunixonacid/textclient_server_init_frompipe.c index e606eeb..9cd549e 100644 --- a/src/libunixonacid/textclient_server_init_frompipe.c +++ b/src/libunixonacid/textclient_server_init_frompipe.c @@ -5,12 +5,12 @@ #include #include -#include #include #include #include #include #include +#include int textclient_server_init_frompipe (textmessage_receiver_t *in, textmessage_sender_t *syncout, textmessage_sender_t *asyncout, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp) { diff --git a/src/libunixonacid/textclient_server_init_fromsocket.c b/src/libunixonacid/textclient_server_init_fromsocket.c index b7f4e84..4964524 100644 --- a/src/libunixonacid/textclient_server_init_fromsocket.c +++ b/src/libunixonacid/textclient_server_init_fromsocket.c @@ -1,25 +1,15 @@ /* ISC license. */ -#include - -#include #include #include -#include +#include -#include -#include -#include #include #include +#include #include #include - -union aligner_u -{ - struct cmsghdr cmsghdr ; - int i ; -} ; +#include static int getfd (void *p) { @@ -35,30 +25,7 @@ static int one (void *p) static int sendit (void *p) { int *fd = p ; - union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ; - ssize_t r ; - char ch = '|' ; - struct iovec v = { .iov_base = &ch, .iov_len = 1 } ; - struct msghdr hdr = - { - .msg_name = 0, - .msg_namelen = 0, - .msg_iov = &v, - .msg_iovlen = 1, - .msg_control = ancilbuf, - .msg_controllen = CMSG_SPACE(sizeof(int)) - } ; - struct cmsghdr *c = CMSG_FIRSTHDR(&hdr) ; - memset(hdr.msg_control, 0, hdr.msg_controllen) ; - c->cmsg_level = SOL_SOCKET ; - c->cmsg_type = SCM_RIGHTS ; - c->cmsg_len = CMSG_LEN(sizeof(int)) ; - *(int *)CMSG_DATA(c) = fd[1] ; - do r = sendmsg(fd[0], &hdr, MSG_NOSIGNAL) ; - while (r < 0 && errno == EINTR) ; - if (r <= 0) return 0 ; - fd_close(fd[1]) ; - return 1 ; + return ancil_send_fd(fd[0], fd[1], '|') ; } int textclient_server_init_fromsocket (textmessage_receiver_t *in, textmessage_sender_t *syncout, textmessage_sender_t *asyncout, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp) diff --git a/src/libunixonacid/textclient_start.c b/src/libunixonacid/textclient_start.c index 8eb8b13..3074ae3 100644 --- a/src/libunixonacid/textclient_start.c +++ b/src/libunixonacid/textclient_start.c @@ -1,26 +1,18 @@ /* ISC license. */ -#include -#include - #include #include #include -#include -#include #include +#include #include #include #include +#include #include #include - -union aligner_u -{ - struct cmsghdr cmsghdr ; - int i ; -} ; +#include static int getfd (void *p) { @@ -29,56 +21,13 @@ static int getfd (void *p) static ssize_t get (void *p) { - static int const awesomeflags = -#ifdef SKALIBS_HASMSGDONTWAIT - MSG_DONTWAIT -#else - 0 -#endif - | -#ifdef SKALIBS_HASCMSGCLOEXEC - MSG_CMSG_CLOEXEC -#else - 0 -#endif - ; - struct cmsghdr *c ; int *fd = p ; - ssize_t r ; - union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ; - char ch ; - struct iovec v = { .iov_base = &ch, .iov_len = 1 } ; - struct msghdr msghdr = - { - .msg_name = 0, - .msg_namelen = 0, - .msg_iov = &v, - .msg_iovlen = 1, - .msg_flags = 0, - .msg_control = ancilbuf, - .msg_controllen = CMSG_SPACE(sizeof(int)) - } ; - do r = recvmsg(fd[0], &msghdr, awesomeflags) ; - while (r < 0 && errno == EINTR) ; - if (r <= 0) return sanitize_read(r) ; - c = CMSG_FIRSTHDR(&msghdr) ; - if (ch != '|' - || !c - || c->cmsg_level != SOL_SOCKET - || c->cmsg_type != SCM_RIGHTS - || (size_t)(c->cmsg_len - (CMSG_DATA(c) - (unsigned char *)c)) != sizeof(int)) return (errno = EPROTO, -1) ; -#ifndef SKALIBS_HASCMSGCLOEXEC - if (coe(*(int *)CMSG_DATA(c)) < 0) - { - fd_close(*(int *)CMSG_DATA(c)) ; - return -1 ; - } -#endif - fd[1] = *(int *)CMSG_DATA(c) ; + int r = ancil_recv_fd(fd[0], '|') ; + if (r < 0) return error_isagain(errno) ? (errno = 0, 0) : r ; + fd[1] = r ; return 1 ; } - - + int textclient_start (textclient_t *a, char const *path, uint32_t options, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp) { struct iovec v ; diff --git a/src/libunixonacid/textclient_startf.c b/src/libunixonacid/textclient_startf.c index 12c2b20..20ebe33 100644 --- a/src/libunixonacid/textclient_startf.c +++ b/src/libunixonacid/textclient_startf.c @@ -4,11 +4,11 @@ #include #include -#include #include #include #include #include +#include int textclient_startf (textclient_t *a, char const *const *argv, char const *const *envp, uint32_t options, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp) { diff --git a/src/libunixonacid/unixmessage_put.c b/src/libunixonacid/unixmessage_put.c index a2ac302..9ce103d 100644 --- a/src/libunixonacid/unixmessage_put.c +++ b/src/libunixonacid/unixmessage_put.c @@ -3,13 +3,13 @@ #include #include -#include #include #include #include #include #include #include +#include static inline int copyfds (char *s, int const *fds, unsigned int n, unsigned char const *bits, unixmessage_sender_closecb_func_t_ref closecb, void *closecbdata) { diff --git a/src/libunixonacid/unixmessage_receive.c b/src/libunixonacid/unixmessage_receive.c index 811baae..b4171f0 100644 --- a/src/libunixonacid/unixmessage_receive.c +++ b/src/libunixonacid/unixmessage_receive.c @@ -7,7 +7,6 @@ #include #include -#include #include #include #include @@ -15,6 +14,7 @@ #include #include #include +#include union aligner_u { diff --git a/src/libunixonacid/unixmessage_sender_flush.c b/src/libunixonacid/unixmessage_sender_flush.c index 3f096f0..96f9112 100644 --- a/src/libunixonacid/unixmessage_sender_flush.c +++ b/src/libunixonacid/unixmessage_sender_flush.c @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -17,6 +16,7 @@ #include #include #include +#include union aligner_u { -- cgit v1.2.3