From 131a18f68dc73f208bb76a944fc6d42c86cfad3b Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 31 Dec 2014 16:27:44 +0000 Subject: Safe wrappers around sendmsg and recvmsg in unixmessage primitives --- src/libunixonacid/unixmessage_receive.c | 9 ++++++--- src/libunixonacid/unixmessage_sender_flush.c | 11 +++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/libunixonacid/unixmessage_receive.c b/src/libunixonacid/unixmessage_receive.c index d25195b..21491fa 100644 --- a/src/libunixonacid/unixmessage_receive.c +++ b/src/libunixonacid/unixmessage_receive.c @@ -49,7 +49,7 @@ static int unixmessage_receiver_fill (unixmessage_receiver_t *b) .msg_controllen = sizeof(ancilbuf) } ; unsigned int auxlen ; - int r ; + int r = -1 ; if (cbuffer_isfull(&b->mainb) || cbuffer_isfull(&b->auxb)) return (errno = ENOBUFS, -1) ; { @@ -57,8 +57,11 @@ static int unixmessage_receiver_fill (unixmessage_receiver_t *b) cbuffer_wpeek(&b->mainb, v) ; iovec_from_siovec(iov, v, 2) ; } - r = recvmsg(b->fd, &msghdr, awesomeflags) ; - if (r <= 0) return r ; + while (r < 0) + { + r = recvmsg(b->fd, &msghdr, awesomeflags) ; + if (!r || (r < 0 && errno != EINTR)) return r ; + } { struct cmsghdr *c = CMSG_FIRSTHDR(&msghdr) ; if (c) diff --git a/src/libunixonacid/unixmessage_sender_flush.c b/src/libunixonacid/unixmessage_sender_flush.c index a115c58..6a0a04b 100644 --- a/src/libunixonacid/unixmessage_sender_flush.c +++ b/src/libunixonacid/unixmessage_sender_flush.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -63,8 +64,14 @@ int unixmessage_sender_flush (unixmessage_sender_t *b) ((int *)CMSG_DATA(cp))[i] = fd < 0 ? -(fd+1) : fd ; } } - if (sendmsg(b->fd, &hdr, MSG_NOSIGNAL) < (int)(len + (sizeof(unsigned int) << 1))) - return -(int)(b->head-oldhead)-1 ; + for (;;) + { + register int r = sendmsg(b->fd, &hdr, MSG_NOSIGNAL) ; + if (r == -1 && errno == EINTR) continue ; + if (r < (int)(len + (sizeof(unsigned int) << 1))) + return -(int)(b->head-oldhead)-1 ; + break ; + } #ifndef SKALIBS_HASANCILAUTOCLOSE if (nfds) { -- cgit v1.2.3