[CRIU] Re: [PATCH 6/6] util-net: Make send_fd/recv_fd being a
particular case of send_fds/recv_fds
Cyrill Gorcunov
gorcunov at openvz.org
Wed Mar 21 04:10:14 EDT 2012
On Wed, Mar 21, 2012 at 11:47:57AM +0400, Cyrill Gorcunov wrote:
> 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>
> ---
Updated version enveloped.
Cyrill
-------------- next part --------------
>From a96c72783addd5058fe2ac74c54d76f16af6ec92 Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Wed, 21 Mar 2012 11:42:02 +0400
Subject: [PATCH 6/6] util-net: Make send_fd/recv_fd being a particular case
of send_fds/recv_fds
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 | 17 ++++++++++++-
util-net.c | 62 ----------------------------------------------------
2 files changed, 15 insertions(+), 64 deletions(-)
diff --git a/include/util-net.h b/include/util-net.h
index 89a116c..aa9414e 100644
--- a/include/util-net.h
+++ b/include/util-net.h
@@ -32,7 +32,20 @@ struct scm_fdset {
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);
+static int send_fd(int sock, struct sockaddr_un *saddr, int saddr_len, int fd)
+{
+ return send_fds(sock, saddr, saddr_len, &fd, 1);
+}
+
+static 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 3ed3829..3874d86 100644
--- a/util-net.c
+++ b/util-net.c
@@ -129,65 +129,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