diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2020-10-26 21:31:16 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2020-10-26 21:31:16 +0000 |
commit | bc714f8001edbb26ebf07411cf92e9ba2f756b35 (patch) | |
tree | e0812fa851135a518209640876b1dc4dd9b7dfad /src | |
parent | 7530e8cdd506ecec1f4ad3bbd55f94de5a6d63ac (diff) | |
download | skalibs-bc714f8001edbb26ebf07411cf92e9ba2f756b35.tar.xz |
Refactor textmessage/textclient with channel creation around ancil_*
Diffstat (limited to 'src')
-rw-r--r-- | src/include/skalibs/textmessage.h | 5 | ||||
-rw-r--r-- | src/libunixonacid/ancil_send_fd.c | 2 | ||||
-rw-r--r-- | src/libunixonacid/textclient_server_init_frompipe.c | 2 | ||||
-rw-r--r-- | src/libunixonacid/textclient_server_init_fromsocket.c | 34 | ||||
-rw-r--r-- | src/libunixonacid/textclient_start.c | 46 | ||||
-rw-r--r-- | src/libunixonacid/textclient_startf.c | 5 | ||||
-rw-r--r-- | src/libunixonacid/textmessage_create_send_channel.c | 47 | ||||
-rw-r--r-- | src/libunixonacid/textmessage_recv_channel.c | 47 |
8 files changed, 115 insertions, 73 deletions
diff --git a/src/include/skalibs/textmessage.h b/src/include/skalibs/textmessage.h index 6dea9cf..b7df659 100644 --- a/src/include/skalibs/textmessage.h +++ b/src/include/skalibs/textmessage.h @@ -82,6 +82,11 @@ extern int textmessage_timed_handle (textmessage_receiver_t *, textmessage_handl #define textmessage_timed_handle_g(tr, f, p, deadline) textmessage_timed_handle(tr, f, p, (deadline), &STAMP) + /* Creating new textmessage channels via fd-passing over a socket */ + +extern int textmessage_create_send_channel (int, textmessage_sender_t *, char const *, size_t, tain_t const *, tain_t *) ; +extern int textmessage_recv_channel (int, textmessage_receiver_t *, char *, size_t, char const *, size_t, tain_t const *, tain_t *) ; + /* Globals */ diff --git a/src/libunixonacid/ancil_send_fd.c b/src/libunixonacid/ancil_send_fd.c index 98366c7..939d06c 100644 --- a/src/libunixonacid/ancil_send_fd.c +++ b/src/libunixonacid/ancil_send_fd.c @@ -7,7 +7,6 @@ #include <sys/uio.h> #include <sys/socket.h> -#include <skalibs/djbunix.h> #include <skalibs/ancil.h> #include <skalibs/posixishard.h> @@ -40,6 +39,5 @@ int ancil_send_fd (int sock, int fd, char ch) 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/textclient_server_init_frompipe.c b/src/libunixonacid/textclient_server_init_frompipe.c index 9cd549e..515916a 100644 --- a/src/libunixonacid/textclient_server_init_frompipe.c +++ b/src/libunixonacid/textclient_server_init_frompipe.c @@ -24,8 +24,8 @@ int textclient_server_init_frompipe (textmessage_receiver_t *in, textmessage_sen if (sanitize_read(textmessage_timed_receive(in, &v, deadline, stamp)) <= 0) return 0 ; if (v.iov_len != beforelen || memcmp(v.iov_base, before, beforelen)) return (errno = EPROTO, 0) ; if (fcntl(asyncfd, F_GETFD) < 0) return 0 ; - if (!textmessage_timed_send(syncout, after, afterlen, deadline, stamp)) return 0 ; textmessage_sender_init(asyncout, asyncfd) ; if (!textmessage_timed_send(asyncout, after, afterlen, deadline, stamp)) return 0 ; + if (!textmessage_timed_send(syncout, after, afterlen, deadline, stamp)) return 0 ; return 1 ; } diff --git a/src/libunixonacid/textclient_server_init_fromsocket.c b/src/libunixonacid/textclient_server_init_fromsocket.c index 4964524..a662cfc 100644 --- a/src/libunixonacid/textclient_server_init_fromsocket.c +++ b/src/libunixonacid/textclient_server_init_fromsocket.c @@ -4,45 +4,17 @@ #include <string.h> #include <sys/uio.h> -#include <skalibs/djbunix.h> -#include <skalibs/unix-timed.h> -#include <skalibs/ancil.h> +#include <skalibs/allreadwrite.h> #include <skalibs/textmessage.h> #include <skalibs/textclient.h> #include <skalibs/posixishard.h> -static int getfd (void *p) -{ - return ((int *)p)[0] ; -} - -static int one (void *p) -{ - (void)p ; - return 1 ; -} - -static int sendit (void *p) -{ - int *fd = p ; - 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) { - int fd[3] = { textmessage_sender_fd(syncout) } ; struct iovec v ; if (sanitize_read(textmessage_timed_receive(in, &v, deadline, stamp)) <= 0) return 0 ; if (v.iov_len != beforelen || memcmp(v.iov_base, before, beforelen)) return (errno = EPROTO, 0) ; - if (pipenbcoe(fd+1) < 0) return 0 ; - if (!timed_flush(fd, &getfd, &one, &sendit, deadline, stamp)) goto err ; - if (!textmessage_timed_send(syncout, after, afterlen, deadline, stamp)) goto err ; - textmessage_sender_init(asyncout, fd[2]) ; - if (!textmessage_timed_send(asyncout, after, afterlen, deadline, stamp)) goto err ; + if (!textmessage_create_send_channel(textmessage_sender_fd(syncout), asyncout, after, afterlen, deadline, stamp)) return 0 ; + if (!textmessage_timed_send(syncout, after, afterlen, deadline, stamp)) return 0 ; return 1 ; - - err: - fd_close(fd[2]) ; - fd_close(fd[1]) ; - return 0 ; } diff --git a/src/libunixonacid/textclient_start.c b/src/libunixonacid/textclient_start.c index 3074ae3..e8a6d0c 100644 --- a/src/libunixonacid/textclient_start.c +++ b/src/libunixonacid/textclient_start.c @@ -4,48 +4,23 @@ #include <string.h> #include <errno.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/error.h> #include <skalibs/webipc.h> #include <skalibs/djbunix.h> -#include <skalibs/unix-timed.h> -#include <skalibs/ancil.h> #include <skalibs/textmessage.h> #include <skalibs/textclient.h> #include <skalibs/posixishard.h> -static int getfd (void *p) -{ - return ((int *)p)[0] ; -} - -static ssize_t get (void *p) -{ - int *fd = p ; - 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 ; - int fd[2] ; - ssize_t r ; - fd[0] = ipc_stream_nbcoe() ; - if (fd[0] < 0) return 0 ; - if (!ipc_timed_connect(fd[0], path, deadline, stamp)) goto err ; - textmessage_sender_init(&a->syncout, fd[0]) ; + int fd = ipc_stream_nbcoe() ; + if (fd < 0) return 0 ; + if (!ipc_timed_connect(fd, path, deadline, stamp)) goto err ; + textmessage_sender_init(&a->syncout, fd) ; if (!textmessage_timed_send(&a->syncout, before, beforelen, deadline, stamp)) goto ferr ; - textmessage_receiver_init(&a->syncin, fd[0], a->syncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ; - r = timed_get(fd, &getfd, &get, deadline, stamp) ; - if (!r) errno = EPIPE ; - if (r <= 0) goto aerr ; - if (sanitize_read(textmessage_timed_receive(&a->syncin, &v, deadline, stamp)) <= 0) goto perr ; - if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) { errno = EPROTO ; goto perr ; } - textmessage_receiver_init(&a->asyncin, fd[1], a->asyncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ; - if (sanitize_read(textmessage_timed_receive(&a->asyncin, &v, deadline, stamp)) <= 0) goto serr ; + if (!textmessage_recv_channel(fd, &a->asyncin, a->asyncbuf, TEXTCLIENT_BUFSIZE, after, afterlen, deadline, stamp)) goto ferr ; + textmessage_receiver_init(&a->syncin, fd, a->syncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ; + if (!textclient_timed_get(a, &v, deadline, stamp)) goto aerr ; if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto berr ; a->pid = 0 ; a->options = options & ~TEXTCLIENT_OPTION_WAITPID ; @@ -53,15 +28,12 @@ int textclient_start (textclient_t *a, char const *path, uint32_t options, char berr: errno = EPROTO ; - serr: - textmessage_receiver_free(&a->asyncin) ; - perr: - fd_close(fd[1]) ; aerr: textmessage_receiver_free(&a->syncin) ; + textmessage_receiver_free(&a->asyncin) ; ferr: textmessage_sender_free(&a->syncout) ; err: - fd_close(fd[0]) ; + fd_close(fd) ; return 0 ; } diff --git a/src/libunixonacid/textclient_startf.c b/src/libunixonacid/textclient_startf.c index 20ebe33..fc871fd 100644 --- a/src/libunixonacid/textclient_startf.c +++ b/src/libunixonacid/textclient_startf.c @@ -21,10 +21,11 @@ int textclient_startf (textclient_t *a, char const *const *argv, char const *con textmessage_sender_init(&a->syncout, fd[1]) ; a->pid = pid ; a->options = options ; - if (!textclient_exchange(a, before, beforelen, &v, deadline, stamp)) goto err ; - if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto errproto ; + if (!textclient_timed_send(a, before, beforelen, deadline, stamp)) goto err ; if (sanitize_read(textmessage_timed_receive(&a->asyncin, &v, deadline, stamp)) <= 0) goto err ; if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto errproto ; + if (!textclient_timed_get(a, &v, deadline, stamp)) goto err ; + if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto errproto ; return 1 ; errproto: diff --git a/src/libunixonacid/textmessage_create_send_channel.c b/src/libunixonacid/textmessage_create_send_channel.c new file mode 100644 index 0000000..40f7ff7 --- /dev/null +++ b/src/libunixonacid/textmessage_create_send_channel.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include <errno.h> +#include <string.h> + +#include <skalibs/djbunix.h> +#include <skalibs/unix-timed.h> +#include <skalibs/ancil.h> +#include <skalibs/textmessage.h> +#include <skalibs/textclient.h> +#include <skalibs/posixishard.h> + +static int getfd (void *p) +{ + return ((int *)p)[0] ; +} + +static int one (void *p) +{ + (void)p ; + return 1 ; +} + +static int sendit (void *p) +{ + int *fd = p ; + return ancil_send_fd(fd[0], fd[1], '|') ; +} + +int textmessage_create_send_channel (int sock, textmessage_sender_t *asyncout, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp) +{ + int fd[3] = { sock } ; + int r ; + if (pipenbcoe(fd+1) < 0) return 0 ; + r = timed_flush(fd, &getfd, &one, &sendit, deadline, stamp) ; + fd_close(fd[1]) ; + if (!r) goto err ; + textmessage_sender_init(asyncout, fd[2]) ; + if (!textmessage_timed_send(asyncout, after, afterlen, deadline, stamp)) goto ferr ; + return 1 ; + + ferr: + textmessage_sender_free(asyncout) ; + err: + fd_close(fd[2]) ; + return 0 ; +} diff --git a/src/libunixonacid/textmessage_recv_channel.c b/src/libunixonacid/textmessage_recv_channel.c new file mode 100644 index 0000000..69adbcf --- /dev/null +++ b/src/libunixonacid/textmessage_recv_channel.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include <sys/uio.h> +#include <string.h> +#include <errno.h> + +#include <skalibs/allreadwrite.h> +#include <skalibs/error.h> +#include <skalibs/djbunix.h> +#include <skalibs/unix-timed.h> +#include <skalibs/ancil.h> +#include <skalibs/textmessage.h> +#include <skalibs/posixishard.h> + +static int getfd (void *p) +{ + return ((int *)p)[0] ; +} + +static ssize_t get (void *p) +{ + int *fd = p ; + int r = ancil_recv_fd(fd[0], '|') ; + if (r < 0) return error_isagain(errno) ? (errno = 0, 0) : r ; + fd[1] = r ; + return 1 ; +} + +int textmessage_recv_channel (int sock, textmessage_receiver_t *asyncin, char *asyncbuf, size_t asyncbufsize, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp) +{ + struct iovec v ; + int fd[2] = { sock, -1 } ; + ssize_t r = timed_get(fd, &getfd, &get, deadline, stamp) ; + if (!r) errno = EPIPE ; + if (r <= 0) return 0 ; + textmessage_receiver_init(asyncin, fd[1], asyncbuf, asyncbufsize, TEXTMESSAGE_MAXLEN) ; + if (sanitize_read(textmessage_timed_receive(asyncin, &v, deadline, stamp)) <= 0) goto serr ; + if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto berr ; + return 1 ; + + berr: + errno = EPROTO ; + serr: + textmessage_receiver_free(asyncin) ; + fd_close(fd[1]) ; + return 0 ; +} |