skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit bc714f8001edbb26ebf07411cf92e9ba2f756b35
parent 7530e8cdd506ecec1f4ad3bbd55f94de5a6d63ac
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Mon, 26 Oct 2020 21:31:16 +0000

 Refactor textmessage/textclient with channel creation around ancil_*

Diffstat:
Mpackage/deps.mak | 8+++++---
Msrc/include/skalibs/textmessage.h | 5+++++
Msrc/libunixonacid/ancil_send_fd.c | 2--
Msrc/libunixonacid/textclient_server_init_frompipe.c | 2+-
Msrc/libunixonacid/textclient_server_init_fromsocket.c | 34+++-------------------------------
Msrc/libunixonacid/textclient_start.c | 46+++++++++-------------------------------------
Msrc/libunixonacid/textclient_startf.c | 5+++--
Asrc/libunixonacid/textmessage_create_send_channel.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/libunixonacid/textmessage_recv_channel.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 120 insertions(+), 76 deletions(-)

diff --git a/package/deps.mak b/package/deps.mak @@ -741,7 +741,7 @@ src/libstddjb/xpathexec_r_name.o src/libstddjb/xpathexec_r_name.lo: src/libstddj src/libstddjb/xpathexec_run.o src/libstddjb/xpathexec_run.lo: src/libstddjb/xpathexec_run.c src/include/skalibs/djbunix.h src/include/skalibs/strerr2.h src/libunixonacid/access_at.o src/libunixonacid/access_at.lo: src/libunixonacid/access_at.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/include/skalibs/unix-transactional.h src/libunixonacid/ancil_recv_fd.o src/libunixonacid/ancil_recv_fd.lo: src/libunixonacid/ancil_recv_fd.c src/include/skalibs/allreadwrite.h src/include/skalibs/ancil.h src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/posixishard.h src/include/skalibs/sysdeps.h -src/libunixonacid/ancil_send_fd.o src/libunixonacid/ancil_send_fd.lo: src/libunixonacid/ancil_send_fd.c src/include/skalibs/ancil.h src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/posixishard.h +src/libunixonacid/ancil_send_fd.o src/libunixonacid/ancil_send_fd.lo: src/libunixonacid/ancil_send_fd.c src/include/skalibs/ancil.h src/include/skalibs/nonposix.h src/include/skalibs/posixishard.h src/libunixonacid/atomic_rm_rf.o src/libunixonacid/atomic_rm_rf.lo: src/libunixonacid/atomic_rm_rf.c src/include/skalibs/skamisc.h src/include/skalibs/unix-transactional.h src/libunixonacid/atomic_rm_rf_tmp.o src/libunixonacid/atomic_rm_rf_tmp.lo: src/libunixonacid/atomic_rm_rf_tmp.c src/include/skalibs/djbunix.h src/include/skalibs/random.h src/include/skalibs/stralloc.h src/include/skalibs/unix-transactional.h src/libunixonacid/atomic_symlink.o src/libunixonacid/atomic_symlink.lo: src/libunixonacid/atomic_symlink.c src/include/skalibs/posixplz.h src/include/skalibs/unix-transactional.h @@ -806,10 +806,11 @@ src/libunixonacid/textclient_commandv.o src/libunixonacid/textclient_commandv.lo src/libunixonacid/textclient_end.o src/libunixonacid/textclient_end.lo: src/libunixonacid/textclient_end.c src/include/skalibs/djbunix.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/libunixonacid/textclient_server_init.o src/libunixonacid/textclient_server_init.lo: src/libunixonacid/textclient_server_init.c src/include/skalibs/djbunix.h src/include/skalibs/textclient.h src/libunixonacid/textclient_server_init_frompipe.o src/libunixonacid/textclient_server_init_frompipe.lo: src/libunixonacid/textclient_server_init_frompipe.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/types.h -src/libunixonacid/textclient_server_init_fromsocket.o src/libunixonacid/textclient_server_init_fromsocket.lo: src/libunixonacid/textclient_server_init_fromsocket.c src/include/skalibs/ancil.h src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h -src/libunixonacid/textclient_start.o src/libunixonacid/textclient_start.lo: src/libunixonacid/textclient_start.c src/include/skalibs/allreadwrite.h src/include/skalibs/ancil.h src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h src/include/skalibs/webipc.h +src/libunixonacid/textclient_server_init_fromsocket.o src/libunixonacid/textclient_server_init_fromsocket.lo: src/libunixonacid/textclient_server_init_fromsocket.c src/include/skalibs/allreadwrite.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h +src/libunixonacid/textclient_start.o src/libunixonacid/textclient_start.lo: src/libunixonacid/textclient_start.c src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/webipc.h src/libunixonacid/textclient_startf.o src/libunixonacid/textclient_startf.lo: src/libunixonacid/textclient_startf.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/libunixonacid/textclient_zero.o src/libunixonacid/textclient_zero.lo: src/libunixonacid/textclient_zero.c src/include/skalibs/textclient.h +src/libunixonacid/textmessage_create_send_channel.o src/libunixonacid/textmessage_create_send_channel.lo: src/libunixonacid/textmessage_create_send_channel.c src/include/skalibs/ancil.h src/include/skalibs/djbunix.h src/include/skalibs/posixishard.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h src/libunixonacid/textmessage_handle.o src/libunixonacid/textmessage_handle.lo: src/libunixonacid/textmessage_handle.c src/include/skalibs/textmessage.h src/libunixonacid/textmessage_put.o src/libunixonacid/textmessage_put.lo: src/libunixonacid/textmessage_put.c src/include/skalibs/bufalloc.h src/include/skalibs/textmessage.h src/include/skalibs/uint32.h src/libunixonacid/textmessage_putv.o src/libunixonacid/textmessage_putv.lo: src/libunixonacid/textmessage_putv.c src/include/skalibs/bufalloc.h src/include/skalibs/siovec.h src/include/skalibs/textmessage.h src/include/skalibs/uint32.h @@ -819,6 +820,7 @@ src/libunixonacid/textmessage_receiver_free.o src/libunixonacid/textmessage_rece src/libunixonacid/textmessage_receiver_hasmsginbuf.o src/libunixonacid/textmessage_receiver_hasmsginbuf.lo: src/libunixonacid/textmessage_receiver_hasmsginbuf.c src/include/skalibs/buffer.h src/include/skalibs/siovec.h src/include/skalibs/textmessage.h src/include/skalibs/uint32.h src/libunixonacid/textmessage_receiver_init.o src/libunixonacid/textmessage_receiver_init.lo: src/libunixonacid/textmessage_receiver_init.c src/include/skalibs/buffer.h src/include/skalibs/stralloc.h src/include/skalibs/textmessage.h src/libunixonacid/textmessage_receiver_zero.o src/libunixonacid/textmessage_receiver_zero.lo: src/libunixonacid/textmessage_receiver_zero.c src/include/skalibs/textmessage.h +src/libunixonacid/textmessage_recv_channel.o src/libunixonacid/textmessage_recv_channel.lo: src/libunixonacid/textmessage_recv_channel.c src/include/skalibs/allreadwrite.h src/include/skalibs/ancil.h src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/posixishard.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h src/libunixonacid/textmessage_sender_1.o src/libunixonacid/textmessage_sender_1.lo: src/libunixonacid/textmessage_sender_1.c src/include/skalibs/textmessage.h src/libunixonacid/textmessage_sender_flush.o src/libunixonacid/textmessage_sender_flush.lo: src/libunixonacid/textmessage_sender_flush.c src/include/skalibs/bufalloc.h src/include/skalibs/textmessage.h src/libunixonacid/textmessage_sender_getfd.o src/libunixonacid/textmessage_sender_getfd.lo: src/libunixonacid/textmessage_sender_getfd.c src/include/skalibs/bufalloc.h src/include/skalibs/textmessage.h diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 ; +}