From 7530e8cdd506ecec1f4ad3bbd55f94de5a6d63ac Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 26 Oct 2020 14:21:53 +0000 Subject: Separate and expose ancil_recv_fd/ancil_send_fd The goal is to make late channel creation easy, as opposed to textclient which always creates a new channel at start time. This commit also moves posixishard.h inclusions as late as possible. --- src/libunixonacid/ancil_send_fd.c | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/libunixonacid/ancil_send_fd.c (limited to 'src/libunixonacid/ancil_send_fd.c') diff --git a/src/libunixonacid/ancil_send_fd.c b/src/libunixonacid/ancil_send_fd.c new file mode 100644 index 0000000..98366c7 --- /dev/null +++ b/src/libunixonacid/ancil_send_fd.c @@ -0,0 +1,45 @@ +/* ISC license. */ + +#include + +#include +#include +#include +#include + +#include +#include +#include + +union aligner_u +{ + struct cmsghdr cmsghdr ; + int i ; +} ; + +int ancil_send_fd (int sock, int fd, char ch) +{ + ssize_t r ; + struct iovec v = { .iov_base = &ch, .iov_len = 1 } ; + union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ; + struct msghdr hdr = + { + .msg_name = 0, + .msg_namelen = 0, + .msg_iov = &v, + .msg_iovlen = 1, + .msg_control = ancilbuf, + .msg_controllen = CMSG_SPACE(sizeof(int)) + } ; + struct cmsghdr *c = CMSG_FIRSTHDR(&hdr) ; + memset(hdr.msg_control, 0, hdr.msg_controllen) ; + c->cmsg_level = SOL_SOCKET ; + c->cmsg_type = SCM_RIGHTS ; + c->cmsg_len = CMSG_LEN(sizeof(int)) ; + *(int *)CMSG_DATA(c) = fd ; + do r = sendmsg(sock, &hdr, MSG_NOSIGNAL) ; + while (r < 0 && errno == EINTR) ; + if (r <= 0) return 0 ; + fd_close(fd) ; + return 1 ; +} -- cgit v1.2.3