libnv: add support for nvlist_send()/nvlist_recv() on Linux
This may be useful for cross build in the feature. Submitted by: Mindaugas Rasiukevicius <rmind@noxt.eu> MFC after: 2 weeks
This commit is contained in:
parent
eeb1f3ed51
commit
3810ba1b33
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=346218
@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -58,7 +59,12 @@ __FBSDID("$FreeBSD$");
|
|||||||
#define PJDLOG_ABORT(...) abort()
|
#define PJDLOG_ABORT(...) abort()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
/* Linux: arbitrary size, but must be lower than SCM_MAX_FD. */
|
||||||
|
#define PKG_MAX_SIZE ((64U - 1) * CMSG_SPACE(sizeof(int)))
|
||||||
|
#else
|
||||||
#define PKG_MAX_SIZE (MCLBYTES / CMSG_SPACE(sizeof(int)) - 1)
|
#define PKG_MAX_SIZE (MCLBYTES / CMSG_SPACE(sizeof(int)) - 1)
|
||||||
|
#endif
|
||||||
|
|
||||||
static int
|
static int
|
||||||
msghdr_add_fd(struct cmsghdr *cmsg, int fd)
|
msghdr_add_fd(struct cmsghdr *cmsg, int fd)
|
||||||
@ -74,31 +80,6 @@ msghdr_add_fd(struct cmsghdr *cmsg, int fd)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
msghdr_get_fd(struct cmsghdr *cmsg)
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
|
|
||||||
cmsg->cmsg_type != SCM_RIGHTS ||
|
|
||||||
cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
|
|
||||||
errno = EINVAL;
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd));
|
|
||||||
#ifndef MSG_CMSG_CLOEXEC
|
|
||||||
/*
|
|
||||||
* If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
|
|
||||||
* close-on-exec flag atomically, but we still want to set it for
|
|
||||||
* consistency.
|
|
||||||
*/
|
|
||||||
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return (fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
fd_wait(int fd, bool doread)
|
fd_wait(int fd, bool doread)
|
||||||
{
|
{
|
||||||
@ -157,14 +138,7 @@ msg_send(int sock, const struct msghdr *msg)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#ifdef __FreeBSD__
|
||||||
* MacOS/Linux do not define struct cmsgcred but we need to bootstrap libnv
|
|
||||||
* when building on non-FreeBSD systems. Since they are not used during
|
|
||||||
* bootstrap we can just omit these two functions there.
|
|
||||||
*/
|
|
||||||
#ifndef __FreeBSD__
|
|
||||||
#warning "cred_send() not supported on non-FreeBSD systems"
|
|
||||||
#else
|
|
||||||
int
|
int
|
||||||
cred_send(int sock)
|
cred_send(int sock)
|
||||||
{
|
{
|
||||||
@ -326,30 +300,54 @@ fd_package_recv(int sock, int *fds, size_t nfds)
|
|||||||
if (msg_recv(sock, &msg) == -1)
|
if (msg_recv(sock, &msg) == -1)
|
||||||
goto end;
|
goto end;
|
||||||
|
|
||||||
for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
|
i = 0;
|
||||||
i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
cmsg = CMSG_FIRSTHDR(&msg);
|
||||||
fds[i] = msghdr_get_fd(cmsg);
|
while (cmsg && i < nfds) {
|
||||||
if (fds[i] < 0)
|
unsigned int n;
|
||||||
|
|
||||||
|
if (cmsg->cmsg_level != SOL_SOCKET ||
|
||||||
|
cmsg->cmsg_type != SCM_RIGHTS) {
|
||||||
|
errno = EINVAL;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
||||||
|
if (i + n > nfds) {
|
||||||
|
errno = EINVAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bcopy(CMSG_DATA(cmsg), fds + i, sizeof(int) * n);
|
||||||
|
cmsg = CMSG_NXTHDR(&msg, cmsg);
|
||||||
|
i += n;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmsg != NULL || i < nfds) {
|
if (cmsg != NULL || i < nfds) {
|
||||||
int fd;
|
unsigned int last;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We need to close all received descriptors, even if we have
|
* We need to close all received descriptors, even if we have
|
||||||
* different control message (eg. SCM_CREDS) in between.
|
* different control message (eg. SCM_CREDS) in between.
|
||||||
*/
|
*/
|
||||||
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
|
last = i;
|
||||||
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
for (i = 0; i < last; i++) {
|
||||||
fd = msghdr_get_fd(cmsg);
|
if (fds[i] >= 0) {
|
||||||
if (fd >= 0)
|
close(fds[i]);
|
||||||
close(fd);
|
}
|
||||||
}
|
}
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef MSG_CMSG_CLOEXEC
|
||||||
|
/*
|
||||||
|
* If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
|
||||||
|
* close-on-exec flag atomically, but we still want to set it for
|
||||||
|
* consistency.
|
||||||
|
*/
|
||||||
|
for (i = 0; i < nfds; i++) {
|
||||||
|
(void) fcntl(fds[i], F_SETFD, FD_CLOEXEC);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
end:
|
end:
|
||||||
serrno = errno;
|
serrno = errno;
|
||||||
|
Loading…
Reference in New Issue
Block a user