[CRIU] Re: [PATCH 5/6] util-net: Add send_fds and recv_fds

Cyrill Gorcunov gorcunov at openvz.org
Wed Mar 21 04:09:34 EDT 2012


On Wed, Mar 21, 2012 at 11:47:56AM +0400, Cyrill Gorcunov wrote:
> We will need these helpers to transfer file
> descriptors from dumpee to our space.
> 
> Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
> ---

Updated version enveloped. Sorry for inconvenience.

	Cyrill
-------------- next part --------------
>From 6159c9a0228e965c4f59e265fcfc7442cc7ea38f Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Wed, 21 Mar 2012 12:05:32 +0400
Subject: [PATCH 5/6] util-net: Add send_fds and recv_fds

We will need these helpers to transfer file
descriptors from dumpee to our space.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/util-net.h |    4 ++
 util-net.c         |  127 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/include/util-net.h b/include/util-net.h
index 030f518..89a116c 100644
--- a/include/util-net.h
+++ b/include/util-net.h
@@ -29,6 +29,10 @@ struct scm_fdset {
 	int		nr_fds;
 };
 
+extern int send_fds(int sock, struct sockaddr_un *saddr, int saddr_len, int *fds, int nr_fds);
+extern int recv_fds(int sock, int *fds, int nr_fds);
+
 extern int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd);
 extern int recv_fd(int sock);
+
 #endif
diff --git a/util-net.c b/util-net.c
index afd51bd..3ed3829 100644
--- a/util-net.c
+++ b/util-net.c
@@ -1,8 +1,135 @@
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <errno.h>
 
+#include "compiler.h"
+#include "types.h"
+#include "builtins.h"
 #include "syscall.h"
 
+#include "util-net.h"
+
+static void scm_fdset_update(struct scm_fdset *fdset, int nr_fds)
+{
+	int min_fd = min(nr_fds, CR_SCM_MAX_FD);
+	struct cmsghdr *cmsg;
+
+	cmsg				= CMSG_FIRSTHDR(&fdset->hdr);
+	fdset->hdr.msg_controllen	= CMSG_LEN(sizeof(int) * min_fd);
+	cmsg->cmsg_len			= fdset->hdr.msg_controllen;
+	fdset->nr_fds			= min_fd;
+}
+
+static int *scm_fdset_first(struct scm_fdset *fdset)
+{
+	struct cmsghdr *cmsg = CMSG_FIRSTHDR(&fdset->hdr);
+	return (int *)CMSG_DATA(cmsg);
+}
+
+static void scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr, int saddr_len)
+{
+	struct cmsghdr *cmsg;
+
+	BUILD_BUG_ON(CR_SCM_MAX_FD > SCM_MAX_FD);
+	BUILD_BUG_ON(sizeof(fdset->msg_buf) < (CMSG_SPACE(sizeof(int) * CR_SCM_MAX_FD)));
+
+	fdset->nr_fds			= CR_SCM_MAX_FD;
+	fdset->__pad			= 0;
+
+	fdset->iov.iov_base		= &fdset->__pad;
+	fdset->iov.iov_len		= sizeof(fdset->__pad);
+
+	fdset->hdr.msg_iov		= &fdset->iov;
+	fdset->hdr.msg_iovlen		= 1;
+	fdset->hdr.msg_name		= (struct sockaddr *)saddr;
+	fdset->hdr.msg_namelen		= saddr_len;
+
+	fdset->hdr.msg_control		= &fdset->msg_buf;
+	fdset->hdr.msg_controllen	= CMSG_LEN(sizeof(int) * CR_SCM_MAX_FD);
+
+	cmsg				= CMSG_FIRSTHDR(&fdset->hdr);
+	cmsg->cmsg_len			= fdset->hdr.msg_controllen;
+	cmsg->cmsg_level		= SOL_SOCKET;
+	cmsg->cmsg_type			= SCM_RIGHTS;
+}
+
+static int scm_fdset_send(int sock, struct scm_fdset *fdset)
+{
+	int ret = sys_sendmsg(sock, &fdset->hdr, 0);
+	if (ret <= 0)
+		return ret;
+
+	return fdset->nr_fds;
+}
+
+static int scm_fdset_recv(int sock, struct scm_fdset *fdset)
+{
+	struct cmsghdr *cmsg;
+	int min_fd;
+	int ret = 0;
+
+	scm_fdset_update(fdset, CR_SCM_MAX_FD);
+
+	ret = sys_recvmsg(sock, &fdset->hdr, 0);
+	if (ret < 0)
+		return ret;
+
+	cmsg = CMSG_FIRSTHDR(&fdset->hdr);
+	if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS)
+		return -EINVAL;
+
+	min_fd = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
+	min_fd = min(min_fd, CR_SCM_MAX_FD);
+
+	fdset->nr_fds = min_fd;
+
+	return 0;
+}
+
+int send_fds(int sock, struct sockaddr_un *saddr, int saddr_len, int *fds, int nr_fds)
+{
+	struct scm_fdset fdset;
+	int *__fds_tx;
+	int i, j, ret;
+
+	scm_fdset_init(&fdset, saddr, saddr_len);
+
+	__fds_tx = scm_fdset_first(&fdset);
+
+	for (i = 0; i < nr_fds; i += fdset.nr_fds) {
+		scm_fdset_update(&fdset, nr_fds - i);
+		builtin_memcpy(__fds_tx, &fds[i], sizeof(int) * fdset.nr_fds);
+
+		ret = scm_fdset_send(sock, &fdset);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+int recv_fds(int sock, int *fds, int nr_fds)
+{
+	struct scm_fdset fdset;
+	int *__fds_rx;
+	int i, j, ret;
+
+	scm_fdset_init(&fdset, NULL, 0);
+
+	__fds_rx = scm_fdset_first(&fdset);
+
+	for (i = 0; i < nr_fds; i += fdset.nr_fds) {
+		scm_fdset_update(&fdset, CR_SCM_MAX_FD);
+		ret = scm_fdset_recv(sock, &fdset);
+		if (ret < 0)
+			return ret;
+
+		builtin_memcpy(&fds[i], __fds_rx, sizeof(int) * fdset.nr_fds);
+	}
+
+	return 0;
+}
+
 int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd)
 {
 	char cmsgbuf[CMSG_SPACE(sizeof(int))];
-- 
1.7.7.6



More information about the CRIU mailing list