summaryrefslogtreecommitdiff
path: root/src/libunixonacid/unixmessage_read.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-09-18 18:55:44 +0000
commit3534b428629be185e096be99e3bd5fdfe32d5544 (patch)
tree210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libunixonacid/unixmessage_read.c
downloadskalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libunixonacid/unixmessage_read.c')
-rw-r--r--src/libunixonacid/unixmessage_read.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/libunixonacid/unixmessage_read.c b/src/libunixonacid/unixmessage_read.c
new file mode 100644
index 0000000..5a23b84
--- /dev/null
+++ b/src/libunixonacid/unixmessage_read.c
@@ -0,0 +1,60 @@
+/* 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 ;
+}