[CRIU] [PATCH 05/12] util-net: Make send_fd/recv_fd being a particular case of send_fds/recv_fds

Cyrill Gorcunov gorcunov at openvz.org
Sun Mar 25 17:33:51 EDT 2012


The send_fd/recv_fd do transfer single file descriptor
which is a particular case of transferring multiple
descriptors, so make them being a wrappers over
send_fds/revc_fds to not duplicate the code.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/util-net.h |   19 +++++++++++++--
 util-net.c         |   62 ----------------------------------------------------
 2 files changed, 16 insertions(+), 65 deletions(-)

diff --git a/include/util-net.h b/include/util-net.h
index e96e603..2289d0e 100644
--- a/include/util-net.h
+++ b/include/util-net.h
@@ -28,10 +28,23 @@ struct scm_fdset {
 	int		msg;	/* We are to send at least one byte */
 };
 
-extern int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd);
-extern int recv_fd(int sock);
-
 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);
 
+static inline int send_fd(int sock, struct sockaddr_un *saddr, int saddr_len, int fd)
+{
+	return send_fds(sock, saddr, saddr_len, &fd, 1);
+}
+
+static inline int recv_fd(int sock)
+{
+	int fd, ret;
+
+	ret = recv_fds(sock, &fd, 1);
+	if (ret)
+		return -1;
+
+	return fd;
+}
+
 #endif
diff --git a/util-net.c b/util-net.c
index d4da6cc..1c1efae 100644
--- a/util-net.c
+++ b/util-net.c
@@ -122,65 +122,3 @@ int recv_fds(int sock, int *fds, int nr_fds)
 
 	return 0;
 }
-
-int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd)
-{
-	char cmsgbuf[CMSG_SPACE(sizeof(int))];
-	struct msghdr hdr = { };
-	struct iovec data = { };
-	struct cmsghdr* cmsg;
-	int *cmsg_data;
-	char dummy = '*';
-
-	data.iov_base	= &dummy;
-	data.iov_len	= sizeof(dummy);
-
-	hdr.msg_name	= (struct sockaddr *)saddr;
-	hdr.msg_namelen	= len;
-	hdr.msg_iov	= &data;
-	hdr.msg_iovlen	= 1;
-
-	hdr.msg_control = &cmsgbuf;
-	hdr.msg_controllen = CMSG_LEN(sizeof(int));
-
-	cmsg = CMSG_FIRSTHDR(&hdr);
-	cmsg->cmsg_len   = hdr.msg_controllen;
-	cmsg->cmsg_level = SOL_SOCKET;
-	cmsg->cmsg_type  = SCM_RIGHTS;
-
-	cmsg_data = (int *)CMSG_DATA(cmsg);
-	*cmsg_data = fd;
-
-	return sys_sendmsg(sock, &hdr, 0);
-}
-
-int recv_fd(int sock)
-{
-	char ccmsg[CMSG_SPACE(sizeof(int))];
-	struct msghdr msg = { };
-	struct iovec iov = { };
-	struct cmsghdr *cmsg;
-	int *cmsg_data;
-	char buf[1];
-	int ret;
-
-	iov.iov_base	= buf;
-	iov.iov_len	= 1;
-
-	msg.msg_iov	= &iov;
-	msg.msg_iovlen	= 1;
-	msg.msg_control	= ccmsg;
-	msg.msg_controllen = sizeof(ccmsg);
-
-	ret = sys_recvmsg(sock, &msg, 0);
-	if (ret < 0)
-		return ret;
-
-	cmsg = CMSG_FIRSTHDR(&msg);
-	if (!cmsg || (cmsg->cmsg_type != SCM_RIGHTS))
-		return -2;
-
-	cmsg_data = (int *)CMSG_DATA(cmsg);
-	return *cmsg_data;
-}
-
-- 
1.7.7.6



More information about the CRIU mailing list