summaryrefslogtreecommitdiff
path: root/src/libunixonacid
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-12-06 17:51:14 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-12-06 17:51:14 +0000
commit6319ac5225aec738f134f338f68109822581bff1 (patch)
tree17e827cff6841eaae0de6b0b733ee2e8958cc9e0 /src/libunixonacid
parent5d6810d9eb833f923f1543fbb369823f7cd289df (diff)
downloadskalibs-6319ac5225aec738f134f338f68109822581bff1.tar.xz
Rewrite unixmessage_receiver without buffer, only cbuffer
Diffstat (limited to 'src/libunixonacid')
-rw-r--r--src/libunixonacid/skaclient_server_bidi_ack.c2
-rw-r--r--src/libunixonacid/skaclient_start_cb.c2
-rw-r--r--src/libunixonacid/unixmessage_read.c60
-rw-r--r--src/libunixonacid/unixmessage_receive.c106
-rw-r--r--src/libunixonacid/unixmessage_receiver_0.c2
-rw-r--r--src/libunixonacid/unixmessage_receiver_free.c7
-rw-r--r--src/libunixonacid/unixmessage_receiver_init.c12
7 files changed, 100 insertions, 91 deletions
diff --git a/src/libunixonacid/skaclient_server_bidi_ack.c b/src/libunixonacid/skaclient_server_bidi_ack.c
index eede82c..1d3cdbf 100644
--- 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
index 6e641e2..4a82b9b 100644
--- 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
deleted file mode 100644
index 5a23b84..0000000
--- a/src/libunixonacid/unixmessage_read.c
+++ /dev/null
@@ -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
index dc75263..fed1b03 100644
--- 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
index 6d48daa..76a537f 100644
--- 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
index c23cc77..353797b 100644
--- 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
index 982547c..5f702cf 100644
--- 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 ;
}