From 0499cee90307d9644271869c86fac2b5401df0e0 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 27 Sep 2015 00:54:42 +0200 Subject: [PATCH] lib: Support fd passing using the 4.3BSD way This is required on Solaris BUG: https://bugzilla.samba.org/show_bug.cgi?id=11053 Signed-off-by: Volker Lendecke Reviewed-by: Michael Adam --- source3/lib/msghdr.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/source3/lib/msghdr.c b/source3/lib/msghdr.c index d89b7c13db3..2aa2f2e0516 100644 --- a/source3/lib/msghdr.c +++ b/source3/lib/msghdr.c @@ -21,6 +21,8 @@ #include "lib/util/iov_buf.h" #include +#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) + ssize_t msghdr_prep_fds(struct msghdr *msg, uint8_t *buf, size_t bufsize, const int *fds, size_t num_fds) { @@ -106,6 +108,84 @@ size_t msghdr_extract_fds(struct msghdr *msg, int *fds, size_t fds_size) return num_fds; } +#elif defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS) + +ssize_t msghdr_prep_fds(struct msghdr *msg, uint8_t *buf, size_t bufsize, + const int *fds, size_t num_fds) +{ + size_t needed; + + if (num_fds > INT8_MAX) { + return -1; + } + + needed = sizeof(int) * num_fds; + + if ((msg == NULL) || (needed > bufsize)) { + return needed; + } + + memcpy(buf, fds, needed); + + msg->msg_accrights = (caddr_t) buf; + msg->msg_accrightslen = needed; + + return needed; +} + +size_t msghdr_prep_recv_fds(struct msghdr *msg, uint8_t *buf, size_t bufsize, + size_t num_fds) +{ + size_t ret = num_fds * sizeof(int); + + if (bufsize < ret) { + return ret; + } + + if (msg != NULL) { + if (num_fds != 0) { + msg->msg_accrights = (caddr_t) buf; + msg->msg_accrightslen = ret; + } else { + msg->msg_accrights = NULL; + msg->msg_accrightslen = 0; + } + } + return ret; +} + +size_t msghdr_extract_fds(struct msghdr *msg, int *fds, size_t fds_size) +{ + size_t num_fds = msg->msg_accrightslen / sizeof(int); + + if ((fds != 0) && (num_fds <= fds_size)) { + memcpy(fds, msg->msg_accrights, msg->msg_accrightslen); + } + + return num_fds; +} + +#else + +ssize_t msghdr_prep_fds(struct msghdr *msg, uint8_t *buf, size_t bufsize, + const int *fds, size_t num_fds) +{ + return -1; +} + +size_t msghdr_prep_recv_fds(struct msghdr *msg, uint8_t *buf, size_t bufsize, + size_t num_fds) +{ + return 0; +} + +size_t msghdr_extract_fds(struct msghdr *msg, int *fds, size_t fds_size) +{ + return 0; +} + +#endif + struct msghdr_buf { struct msghdr msg; struct sockaddr_storage addr; -- 2.11.4.GIT