skalibs

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

commit 6319ac5225aec738f134f338f68109822581bff1
parent 5d6810d9eb833f923f1543fbb369823f7cd289df
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Sat,  6 Dec 2014 17:51:14 +0000

Rewrite unixmessage_receiver without buffer, only cbuffer

Diffstat:
Mpackage/deps.mak | 7+++----
Msrc/include/skalibs/buffer.h | 13+++++--------
Msrc/include/skalibs/unixmessage.h | 27+++++++++++++--------------
Msrc/libstddjb/buffer_fill.c | 2+-
Msrc/libstddjb/buffer_flush.c | 4++--
Msrc/libstddjb/buffer_flush1read.c | 4++--
Msrc/libstddjb/buffer_init.c | 4++--
Msrc/libstddjb/buffer_read.c | 5++---
Msrc/libstddjb/buffer_write.c | 5++---
Msrc/libunixonacid/skaclient_server_bidi_ack.c | 2+-
Msrc/libunixonacid/skaclient_start_cb.c | 2+-
Dsrc/libunixonacid/unixmessage_read.c | 60------------------------------------------------------------
Msrc/libunixonacid/unixmessage_receive.c | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Msrc/libunixonacid/unixmessage_receiver_0.c | 2+-
Msrc/libunixonacid/unixmessage_receiver_free.c | 7++++---
Msrc/libunixonacid/unixmessage_receiver_init.c | 12++++++------
16 files changed, 132 insertions(+), 130 deletions(-)

diff --git a/package/deps.mak b/package/deps.mak @@ -226,7 +226,7 @@ src/libstddjb/buffer_getlen.o src/libstddjb/buffer_getlen.lo: src/libstddjb/buff src/libstddjb/buffer_getv.o src/libstddjb/buffer_getv.lo: src/libstddjb/buffer_getv.c src/include/skalibs/buffer.h src/include/skalibs/diuint.h src/include/skalibs/siovec.h src/libstddjb/buffer_getvall.o src/libstddjb/buffer_getvall.lo: src/libstddjb/buffer_getvall.c src/include/skalibs/buffer.h src/include/skalibs/diuint.h src/include/skalibs/siovec.h src/libstddjb/buffer_getvallnf.o src/libstddjb/buffer_getvallnf.lo: src/libstddjb/buffer_getvallnf.c src/include/skalibs/buffer.h src/include/skalibs/siovec.h -src/libstddjb/buffer_init.o src/libstddjb/buffer_init.lo: src/libstddjb/buffer_init.c src/include/skalibs/buffer.h +src/libstddjb/buffer_init.o src/libstddjb/buffer_init.lo: src/libstddjb/buffer_init.c src/include/skalibs/buffer.h src/include/skalibs/cbuffer.h src/libstddjb/buffer_put.o src/libstddjb/buffer_put.lo: src/libstddjb/buffer_put.c src/include/skalibs/buffer.h src/libstddjb/buffer_putall.o src/libstddjb/buffer_putall.lo: src/libstddjb/buffer_putall.c src/include/skalibs/buffer.h src/libstddjb/buffer_putallnf.o src/libstddjb/buffer_putallnf.lo: src/libstddjb/buffer_putallnf.c src/include/skalibs/buffer.h @@ -742,11 +742,10 @@ src/libunixonacid/unixmessage_bits_closeall.o src/libunixonacid/unixmessage_bits src/libunixonacid/unixmessage_bits_closenone.o src/libunixonacid/unixmessage_bits_closenone.lo: src/libunixonacid/unixmessage_bits_closenone.c src/include/skalibs/bitarray.h src/include/skalibs/unixmessage.h src/libunixonacid/unixmessage_handle.o src/libunixonacid/unixmessage_handle.lo: src/libunixonacid/unixmessage_handle.c src/include/skalibs/unixmessage.h src/libunixonacid/unixmessage_put.o src/libunixonacid/unixmessage_put.lo: src/libunixonacid/unixmessage_put.c src/include/skalibs/bitarray.h src/include/skalibs/bytestr.h src/include/skalibs/diuint.h src/include/skalibs/genalloc.h src/include/skalibs/siovec.h src/include/skalibs/stralloc.h src/include/skalibs/sysdeps.h src/include/skalibs/unixmessage.h -src/libunixonacid/unixmessage_read.o src/libunixonacid/unixmessage_read.lo: src/libunixonacid/unixmessage_read.c src/include/skalibs/buffer.h src/include/skalibs/cbuffer.h src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/nonposix.h src/include/skalibs/siovec.h src/include/skalibs/sysdeps.h src/include/skalibs/unixmessage.h -src/libunixonacid/unixmessage_receive.o src/libunixonacid/unixmessage_receive.lo: src/libunixonacid/unixmessage_receive.c src/include/skalibs/allreadwrite.h src/include/skalibs/buffer.h src/include/skalibs/cbuffer.h src/include/skalibs/error.h src/include/skalibs/stralloc.h src/include/skalibs/uint.h src/include/skalibs/unixmessage.h +src/libunixonacid/unixmessage_receive.o src/libunixonacid/unixmessage_receive.lo: src/libunixonacid/unixmessage_receive.c src/include/skalibs/allreadwrite.h src/include/skalibs/cbuffer.h src/include/skalibs/diuint.h src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/nonposix.h src/include/skalibs/siovec.h src/include/skalibs/stralloc.h src/include/skalibs/sysdeps.h src/include/skalibs/uint.h src/include/skalibs/unixmessage.h src/libunixonacid/unixmessage_receiver_0.o src/libunixonacid/unixmessage_receiver_0.lo: src/libunixonacid/unixmessage_receiver_0.c src/include/skalibs/unixmessage.h src/libunixonacid/unixmessage_receiver_free.o src/libunixonacid/unixmessage_receiver_free.lo: src/libunixonacid/unixmessage_receiver_free.c src/include/skalibs/stralloc.h src/include/skalibs/unixmessage.h -src/libunixonacid/unixmessage_receiver_init.o src/libunixonacid/unixmessage_receiver_init.lo: src/libunixonacid/unixmessage_receiver_init.c src/include/skalibs/buffer.h src/include/skalibs/cbuffer.h src/include/skalibs/stralloc.h src/include/skalibs/unixmessage.h +src/libunixonacid/unixmessage_receiver_init.o src/libunixonacid/unixmessage_receiver_init.lo: src/libunixonacid/unixmessage_receiver_init.c src/include/skalibs/cbuffer.h src/include/skalibs/stralloc.h src/include/skalibs/unixmessage.h src/libunixonacid/unixmessage_sender_1.o src/libunixonacid/unixmessage_sender_1.lo: src/libunixonacid/unixmessage_sender_1.c src/include/skalibs/unixmessage.h src/libunixonacid/unixmessage_sender_flush.o src/libunixonacid/unixmessage_sender_flush.lo: src/libunixonacid/unixmessage_sender_flush.c src/include/skalibs/diuint.h src/include/skalibs/djbunix.h src/include/skalibs/genalloc.h src/include/skalibs/nonposix.h src/include/skalibs/stralloc.h src/include/skalibs/sysdeps.h src/include/skalibs/uint.h src/include/skalibs/unixmessage.h src/libunixonacid/unixmessage_sender_free.o src/libunixonacid/unixmessage_sender_free.lo: src/libunixonacid/unixmessage_sender_free.c src/include/skalibs/diuint.h src/include/skalibs/genalloc.h src/include/skalibs/stralloc.h src/include/skalibs/sysdeps.h src/include/skalibs/unixmessage.h diff --git a/src/include/skalibs/buffer.h b/src/include/skalibs/buffer.h @@ -9,7 +9,7 @@ #include <skalibs/diuint.h> #include <skalibs/siovec.h> -typedef int buffer_io_func_t (int, siovec_t const *, unsigned int, void *) ; +typedef int buffer_io_func_t (int, siovec_t const *, unsigned int) ; typedef buffer_io_func_t *buffer_io_func_t_ref ; typedef struct buffer_s buffer, buffer_t, *buffer_ref, *buffer_t_ref ; @@ -18,14 +18,13 @@ struct buffer_s buffer_io_func_t *op ; int fd ; cbuffer_t c ; - void *aux ; } ; -#define BUFFER_ZERO { .op = 0, .fd = -1, .c = CBUFFER_ZERO, .aux = 0 } +#define BUFFER_ZERO { .op = 0, .fd = -1, .c = CBUFFER_ZERO } /* Circular buffers need to be 1 char bigger than the storage space, - so that the head == tail case is nonambiguous (empty). + so that the head == tail case is nonambiguous (empty, not full). */ #define BUFFER_INSIZE 8193 @@ -34,10 +33,8 @@ struct buffer_s #define BUFFER_INSIZE_SMALL 513 #define BUFFER_OUTSIZE_SMALL 513 -#define BUFFER_INIT_AUX(f, d, buf, len, data) { .op = (f), .fd = (d), .c = CBUFFER_INIT(buf, len), .aux = (data) } -#define BUFFER_INIT(f, d, buf, len) BUFFER_INIT_AUX(f, d, buf, (len), 0) -extern int buffer_init_aux (buffer *, buffer_io_func_t *, int, char *, unsigned int, void *) ; -#define buffer_init(b, f, d, buf, len) buffer_init_aux(b, f, d, buf, len, 0) +#define BUFFER_INIT(f, d, buf, len) { .op = (f), .fd = (d), .c = CBUFFER_INIT(buf, len) } +extern int buffer_init (buffer *, buffer_io_func_t *, int, char *, unsigned int) ; /* Writing */ diff --git a/src/include/skalibs/unixmessage.h b/src/include/skalibs/unixmessage.h @@ -6,6 +6,7 @@ #include <skalibs/buffer.h> #include <skalibs/cbuffer.h> #include <skalibs/gccattributes.h> +#include <skalibs/stralloc.h> #include <skalibs/genalloc.h> #include <skalibs/siovec.h> #include <skalibs/stralloc.h> @@ -81,36 +82,34 @@ extern int unixmessage_sender_timed_flush (unixmessage_sender_t *, tain_t const typedef struct unixmessage_receiver_s unixmessage_receiver_t, *unixmessage_receiver_t_ref ; struct unixmessage_receiver_s { - buffer mainb ; + int fd ; + cbuffer_t mainb ; cbuffer_t auxb ; unsigned int mainlen ; unsigned int auxlen ; - stralloc data ; - int fds[UNIXMESSAGE_MAXFDS] ; - unsigned int auxw ; + stralloc maindata ; + stralloc auxdata ; } ; -#define UNIXMESSAGE_RECEIVER_ZERO { .mainb = BUFFER_ZERO, .auxb = CBUFFER_ZERO, .mainlen = 0, .auxlen = 0, .data = STRALLOC_ZERO, .fds = { -1 }, .auxw = 0 } -#define UNIXMESSAGE_RECEIVER_INIT(var, fd, s, n, auxs, auxn) \ +#define UNIXMESSAGE_RECEIVER_ZERO { .fd = -1, .mainb = CBUFFER_ZERO, .auxb = CBUFFER_ZERO, .mainlen = 0, .auxlen = 0, .maindata = STRALLOC_ZERO, .auxdata = STRALLOC_ZERO } +#define UNIXMESSAGE_RECEIVER_INIT(d, mains, mainn, auxs, auxn) \ { \ - .mainb = BUFFER_INIT_AUX(&unixmessage_read, fd, s, n, &(var).auxb), \ + .fd = d, \ + .mainb = CBUFFER_INIT(mains, mainn), \ .auxb = CBUFFER_INIT(auxs, auxn), \ .mainlen = 0, \ .auxlen = 0, \ - .data = STRALLOC_ZERO, \ - .auxw = 0 \ + .maindata = STRALLOC_ZERO, \ + .auxdata = STRALLOC_ZERO \ } -#define UNIXMESSAGE_RECEIVER_DECLARE_AND_INIT(var, fd, s, n, auxs, auxn) unixmessage_receiver_t var = UNIXMESSAGE_RECEIVER_INIT(var, fd, s, n, auxs, auxn) extern int unixmessage_receiver_init (unixmessage_receiver_t *, int, char *, unsigned int, char *, unsigned int) ; extern void unixmessage_receiver_free (unixmessage_receiver_t *) ; -#define unixmessage_receiver_fd(b) buffer_fd(&(b)->mainb) -#define unixmessage_receiver_isempty(b) (buffer_isempty(&(b)->mainb) && cbuffer_isempty(&(b)->auxb)) +#define unixmessage_receiver_fd(b) ((b)->fd) +#define unixmessage_receiver_isempty(b) (cbuffer_isempty(&(b)->mainb) && cbuffer_isempty(&(b)->auxb)) extern int unixmessage_receive (unixmessage_receiver_t *, unixmessage_t *) ; extern int unixmessage_timed_receive (unixmessage_receiver_t *, unixmessage_t *, tain_t const *, tain_t *) ; #define unixmessage_timed_receive_g(receiver, msg, deadline) unixmessage_timed_receive(receiver, msg, (deadline), &STAMP) -extern buffer_io_func_t unixmessage_read ; - typedef int unixmessage_handler_func_t (unixmessage_t const *, void *) ; typedef unixmessage_handler_func_t *unixmessage_handler_func_t_ref ; diff --git a/src/libstddjb/buffer_fill.c b/src/libstddjb/buffer_fill.c @@ -10,7 +10,7 @@ int buffer_fill (buffer *b) register int r ; if (buffer_isfull(b)) return (errno = ENOBUFS, -1) ; buffer_wpeek(b, v) ; - r = (*b->op)(b->fd, v, 2, b->aux) ; + r = (*b->op)(b->fd, v, 2) ; if (r <= 0) return r ; cbuffer_WSEEK(&b->c, r) ; return r ; diff --git a/src/libstddjb/buffer_flush.c b/src/libstddjb/buffer_flush.c @@ -1,8 +1,8 @@ /* ISC license. */ -#include <skalibs/buffer.h> #include <skalibs/cbuffer.h> #include <skalibs/siovec.h> +#include <skalibs/buffer.h> int buffer_flush (buffer *b) { @@ -12,7 +12,7 @@ int buffer_flush (buffer *b) register int r ; buffer_rpeek(b, v) ; if (!v[0].len && !v[1].len) break ; - r = (*b->op)(b->fd, v, 2, b->aux) ; + r = (*b->op)(b->fd, v, 2) ; if (r <= 0) return 0 ; cbuffer_RSEEK(&b->c, r) ; } diff --git a/src/libstddjb/buffer_flush1read.c b/src/libstddjb/buffer_flush1read.c @@ -3,8 +3,8 @@ #include <skalibs/buffer.h> #include <skalibs/siovec.h> -int buffer_flush1read (int fd, siovec_t const *v, unsigned int n, void *aux) +int buffer_flush1read (int fd, siovec_t const *v, unsigned int n) { if (!buffer_flush(buffer_1)) return -1 ; - return buffer_read(fd, v, n, aux) ; + return buffer_read(fd, v, n) ; } diff --git a/src/libstddjb/buffer_init.c b/src/libstddjb/buffer_init.c @@ -1,13 +1,13 @@ /* ISC license. */ #include <errno.h> +#include <skalibs/cbuffer.h> #include <skalibs/buffer.h> -int buffer_init_aux (buffer *b, buffer_io_func_t *op, int fd, char *s, unsigned int len, void *aux) +int buffer_init (buffer *b, buffer_io_func_t *op, int fd, char *s, unsigned int len) { if (!cbuffer_init(&b->c, s, len)) return 0 ; b->fd = fd ; b->op = op ; - b->aux = aux ; return 1 ; } diff --git a/src/libstddjb/buffer_read.c b/src/libstddjb/buffer_read.c @@ -3,13 +3,12 @@ #include <sys/types.h> #include <sys/uio.h> #include <skalibs/allreadwrite.h> -#include <skalibs/buffer.h> #include <skalibs/siovec.h> +#include <skalibs/buffer.h> -int buffer_read (int fd, siovec_t const *v, unsigned int n, void *aux) +int buffer_read (int fd, siovec_t const *v, unsigned int n) { struct iovec iov[n] ; iovec_from_siovec(iov, v, n) ; - (void)aux ; return fd_readv(fd, iov, n) ; } diff --git a/src/libstddjb/buffer_write.c b/src/libstddjb/buffer_write.c @@ -3,13 +3,12 @@ #include <sys/types.h> #include <sys/uio.h> #include <skalibs/allreadwrite.h> -#include <skalibs/buffer.h> #include <skalibs/siovec.h> +#include <skalibs/buffer.h> -int buffer_write (int fd, siovec_t const *v, unsigned int n, void *aux) +int buffer_write (int fd, siovec_t const *v, unsigned int n) { struct iovec iov[n] ; iovec_from_siovec(iov, v, n) ; - (void)aux ; return fd_writev(fd, iov, n) ; } diff --git a/src/libunixonacid/skaclient_server_bidi_ack.c b/src/libunixonacid/skaclient_server_bidi_ack.c @@ -7,6 +7,6 @@ int skaclient_server_bidi_ack (unixmessage_t const *clientmsg, unixmessage_sende { if (!unixmessage_receiver_init(asyncin, -1, mainbuf, mainlen, auxbuf, auxlen)) return 0 ; if (!skaclient_server_ack(clientmsg, out, asyncout, before, beforelen, after, afterlen)) return 0 ; - asyncin->mainb.fd = unixmessage_sender_fd(asyncout) ; + asyncin->fd = unixmessage_sender_fd(asyncout) ; return 1 ; } diff --git a/src/libunixonacid/skaclient_start_cb.c b/src/libunixonacid/skaclient_start_cb.c @@ -12,7 +12,7 @@ int skaclient_start_cb (unixmessage_t const *m, skaclient_cbdata_t *blah) if (m->len != blah->afterlen || byte_diff(m->s, m->len, blah->after) || m->nfds != 1) return (errno = EPROTO, 0) ; - blah->asyncin->mainb.fd = m->fds[0] ; + blah->asyncin->fd = m->fds[0] ; blah->asyncout->fd = m->fds[0] ; return 1 ; } diff --git a/src/libunixonacid/unixmessage_read.c b/src/libunixonacid/unixmessage_read.c @@ -1,60 +0,0 @@ -/* ISC license. */ - -#define _XPG4_2 -#include <skalibs/sysdeps.h> -#include <skalibs/nonposix.h> -#include <errno.h> -#include <sys/socket.h> -#include <sys/uio.h> -#include <skalibs/buffer.h> -#include <skalibs/cbuffer.h> -#include <skalibs/djbunix.h> -#include <skalibs/error.h> -#include <skalibs/siovec.h> -#include <skalibs/unixmessage.h> - -int unixmessage_read (int fd, siovec_t const *v, unsigned int n, void *aux) -{ - int r ; - char ancilbuf[CMSG_SPACE(UNIXMESSAGE_MAXFDS * sizeof(int))] ; - struct iovec iov[n] ; - struct msghdr msghdr = - { - .msg_name = 0, - .msg_namelen = 0, - .msg_iov = iov, - .msg_iovlen = n, - .msg_flags = 0, - .msg_control = ancilbuf, - .msg_controllen = sizeof(ancilbuf) - } ; - iovec_from_siovec(iov, v, n) ; -#ifdef SKALIBS_HASCMSGCLOEXEC - r = recvmsg(fd, &msghdr, MSG_WAITALL | MSG_CMSG_CLOEXEC) ; -#else - r = recvmsg(fd, &msghdr, MSG_WAITALL) ; -#endif - if (r > 0) - { - struct cmsghdr *c = CMSG_FIRSTHDR(&msghdr) ; - if (c) - { - cbuffer_t *auxb = aux ; - unsigned int len ; - if (c->cmsg_level != SOL_SOCKET - || c->cmsg_type != SCM_RIGHTS) return (errno = EPROTO, -1-r) ; - len = (unsigned int)(c->cmsg_len - (CMSG_DATA(c) - (unsigned char *)c)) ; -#ifndef SKALIBS_HASCMSGCLOEXEC - { - register unsigned int i = 0 ; - for (; i < len/sizeof(int) ; i++) - if (coe(((int *)CMSG_DATA(c))[i]) < 0) return -1-r ; - } -#endif - if (msghdr.msg_flags | MSG_CTRUNC) return (errno = EPROTO, -1-r) ; - if (cbuffer_put(auxb, (char *)CMSG_DATA(c), len) < len) - return (errno = ENOBUFS, -1-r) ; - } - } - return r ; -} diff --git a/src/libunixonacid/unixmessage_receive.c b/src/libunixonacid/unixmessage_receive.c @@ -1,44 +1,112 @@ /* ISC license. */ +#define _XPG4_2 +#include <skalibs/sysdeps.h> +#include <skalibs/nonposix.h> #include <errno.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/buffer.h> +#include <sys/socket.h> +#include <sys/uio.h> +#include <skalibs/uint.h> +#include <skalibs/diuint.h> #include <skalibs/cbuffer.h> +#include <skalibs/djbunix.h> #include <skalibs/error.h> +#include <skalibs/allreadwrite.h> #include <skalibs/stralloc.h> -#include <skalibs/uint.h> +#include <skalibs/siovec.h> #include <skalibs/unixmessage.h> +static int unixmessage_receiver_fill (unixmessage_receiver_t *b, diuint *d) +{ + char ancilbuf[CMSG_SPACE(b->auxb.a - 1)] ; + struct iovec iov[2] ; + struct msghdr msghdr = + { + .msg_name = 0, + .msg_namelen = 0, + .msg_iov = iov, + .msg_iovlen = 2, + .msg_flags = 0, + .msg_control = ancilbuf, + .msg_controllen = sizeof(ancilbuf) + } ; + unsigned int auxlen ; + int r ; + if (cbuffer_isfull(&b->mainb) || cbuffer_isfull(&b->auxb)) + return (errno = ENOBUFS, -1) ; + { + siovec_t v[2] ; + cbuffer_wpeek(&b->mainb, v) ; + iovec_from_siovec(iov, v, 2) ; + } +#ifdef SKALIBS_HASCMSGCLOEXEC + r = recvmsg(b->fd, &msghdr, MSG_WAITALL | MSG_CMSG_CLOEXEC) ; +#else + r = recvmsg(b->fd, &msghdr, MSG_WAITALL) ; +#endif + if (r <= 0) return r ; + { + struct cmsghdr *c = CMSG_FIRSTHDR(&msghdr) ; + if (c) + { + if (c->cmsg_level != SOL_SOCKET + || c->cmsg_type != SCM_RIGHTS) return (errno = EPROTO, -1) ; + auxlen = (unsigned int)(c->cmsg_len - (CMSG_DATA(c) - (unsigned char *)c)) ; +#ifndef SKALIBS_HASCMSGCLOEXEC + { + register unsigned int i = 0 ; + for (; i < auxlen/sizeof(int) ; i++) + if (coe(((int *)CMSG_DATA(c))[i]) < 0) return -1 ; + } +#endif + if (msghdr.msg_flags & MSG_CTRUNC) return (errno = EPROTO, -1) ; + if (cbuffer_put(&b->auxb, (char *)CMSG_DATA(c), auxlen) < auxlen) + return (errno = ENOBUFS, -1) ; + d->right = auxlen / sizeof(int) ; + r -= c->cmsg_len ; + } + } + d->left = cbuffer_WSEEK(&b->mainb, r) ; + return 1 ; +} + int unixmessage_receive (unixmessage_receiver_t *b, unixmessage_t *m) { - if (b->data.len == b->mainlen) + if (b->maindata.len == b->mainlen && b->auxdata.len == b->auxlen) { char pack[sizeof(unsigned int) << 1] ; - if (buffer_len(&b->mainb) < sizeof(unsigned int) << 1) + if (cbuffer_len(&b->mainb) < sizeof(unsigned int) << 1) { - register int r = sanitize_read(buffer_fill(&b->mainb)) ; + diuint d ; + register int r = sanitize_read(unixmessage_receiver_fill(b, &d)) ; if (r <= 0) return r ; - if (r < sizeof(unsigned int) << 1) return (errno = EWOULDBLOCK, 0) ; + if (cbuffer_len(&b->mainb) < sizeof(unsigned int) << 1) + return (errno = EWOULDBLOCK, 0) ; } - buffer_getnofill(&b->mainb, pack, sizeof(unsigned int) << 1) ; + cbuffer_get(&b->mainb, pack, sizeof(unsigned int) << 1) ; uint_unpack_big(pack, &b->mainlen) ; uint_unpack_big(pack + sizeof(unsigned int), &b->auxlen) ; - if (b->auxlen > UNIXMESSAGE_MAXFDS) return (errno = EPROTO, -1) ; b->auxlen *= sizeof(int) ; - if (!stralloc_ready(&b->data, b->mainlen)) return -1 ; - b->data.len = 0 ; - b->auxw = cbuffer_get(&b->auxb, (char *)b->fds, b->auxlen) ; + if (!stralloc_ready(&b->maindata, b->mainlen)) return -1 ; + b->maindata.len = 0 ; + if (!stralloc_ready(&b->auxdata, b->auxlen)) return -1 ; + b->auxdata.len = 0 ; } + + for (;;) { - register int r = buffer_getall(&b->mainb, b->data.s, b->mainlen, &b->data.len) ; + diuint d ; + register int r ; + b->maindata.len += cbuffer_get(&b->mainb, b->maindata.s + b->maindata.len, cbuffer_len(&b->mainb)) ; + b->auxdata.len += cbuffer_get(&b->auxb, b->auxdata.s + b->auxdata.len, cbuffer_len(&b->auxb)) ; + if (b->maindata.len == b->mainlen && b->auxdata.len == b->auxlen) break ; + r = sanitize_read(unixmessage_receiver_fill(b, &d)) ; if (r <= 0) return r ; } - if (b->auxw < b->auxlen) - b->auxw += cbuffer_get(&b->auxb, (char *)b->fds, b->auxlen - b->auxw) ; - if (b->auxw < b->auxlen) return (errno = EPROTO, -1) ; - m->s = b->data.s ; - m->len = b->data.len ; - m->fds = b->fds ; + + m->s = b->maindata.s ; + m->len = b->maindata.len ; + m->fds = (int *)b->auxdata.s ; m->nfds = b->auxlen / sizeof(int) ; return 1 ; } diff --git a/src/libunixonacid/unixmessage_receiver_0.c b/src/libunixonacid/unixmessage_receiver_0.c @@ -7,4 +7,4 @@ static char mainbuf[UNIXMESSAGE_BUFSIZE] ; static char auxbuf[UNIXMESSAGE_AUXBUFSIZE] ; -UNIXMESSAGE_RECEIVER_DECLARE_AND_INIT(unixmessage_receiver_0_, 0, mainbuf, UNIXMESSAGE_BUFSIZE, auxbuf, UNIXMESSAGE_AUXBUFSIZE) ; +unixmessage_receiver_t unixmessage_receiver_0_ = UNIXMESSAGE_RECEIVER_INIT(0, mainbuf, UNIXMESSAGE_BUFSIZE, auxbuf, UNIXMESSAGE_AUXBUFSIZE) ; diff --git a/src/libunixonacid/unixmessage_receiver_free.c b/src/libunixonacid/unixmessage_receiver_free.c @@ -5,7 +5,8 @@ void unixmessage_receiver_free (unixmessage_receiver_t *b) { - stralloc_free(&b->data) ; - b->mainb.fd = -1 ; - b->mainlen = b->auxlen = b->auxw = 0 ; + b->fd = -1 ; + stralloc_free(&b->maindata) ; + stralloc_free(&b->auxdata) ; + b->mainlen = b->auxlen = 0 ; } diff --git a/src/libunixonacid/unixmessage_receiver_init.c b/src/libunixonacid/unixmessage_receiver_init.c @@ -1,17 +1,17 @@ /* ISC license. */ #include <errno.h> -#include <skalibs/buffer.h> #include <skalibs/cbuffer.h> #include <skalibs/stralloc.h> #include <skalibs/unixmessage.h> int unixmessage_receiver_init (unixmessage_receiver_t *b, int fd, char *mainbuf, unsigned int mainlen, char *auxbuf, unsigned int auxlen) { - if (mainlen < 9 || auxlen < UNIXMESSAGE_MAXFDS * sizeof(int) + 1) return (errno = EINVAL, 0) ; - if (!cbuffer_init(&b->auxb, auxbuf, auxlen)) return 0 ; - if (!buffer_init_aux(&b->mainb, &unixmessage_read, fd, mainbuf, mainlen, &b->auxb)) return 0 ; - b->mainlen = b->auxlen = b->auxw = 0 ; - b->data = stralloc_zero ; + if (!cbuffer_init(&b->mainb, mainbuf, mainlen) + || !cbuffer_init(&b->auxb, auxbuf, auxlen)) return 0 ; + b->fd = fd ; + b->mainlen = b->auxlen = 0 ; + b->maindata = stralloc_zero ; + b->auxdata = stralloc_zero ; return 1 ; }