aboutsummaryrefslogtreecommitdiffstats
path: root/sendfd.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-11-12 14:44:35 +0100
committerMattias Andrée <maandree@kth.se>2017-11-12 14:54:29 +0100
commit67a395794b678d7da053c3daf61eb12d3f9437f2 (patch)
tree26be121e2738954a648d4032e8798723034960d7 /sendfd.c
parentMore functions and add makefile (diff)
downloadlibsimple-67a395794b678d7da053c3daf61eb12d3f9437f2.tar.gz
libsimple-67a395794b678d7da053c3daf61eb12d3f9437f2.tar.bz2
libsimple-67a395794b678d7da053c3daf61eb12d3f9437f2.tar.xz
Split into multiple units
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'sendfd.c')
-rw-r--r--sendfd.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/sendfd.c b/sendfd.c
new file mode 100644
index 0000000..ca26265
--- /dev/null
+++ b/sendfd.c
@@ -0,0 +1,31 @@
+/* See LICENSE file for copyright and license details. */
+#include "libsimple.h"
+
+
+int
+libsimple_sendfd(int sock, int fd)
+{
+ char buf[1];
+ struct iovec iov;
+ struct msghdr msg;
+ struct cmsghdr *cmsg;
+ char cms[CMSG_SPACE(sizeof(fd))];
+
+ buf[0] = 0;
+ iov.iov_base = buf;
+ iov.iov_len = 1;
+
+ memset(&msg, 0, sizeof(msg));
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = (caddr_t)cms;
+ msg.msg_controllen = CMSG_LEN(sizeof(fd));
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
+
+ return -(sendmsg(sock, &msg, 0) != (ssize_t)iov.iov_len);
+}