[CRIU] [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 03:47:57 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 | 17 +++++++++++-
util-net.c | 67 +--------------------------------------------------
2 files changed, 17 insertions(+), 67 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 b2573a5..3874d86 100644
--- a/util-net.c
+++ b/util-net.c
@@ -39,11 +39,10 @@ static void scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr, i
fdset->iov.iov_base = &fdset->__pad;
fdset->iov.iov_len = sizeof(fdset->__pad);
- /*
- * msg_name and msg_namelen should be assigned separately.
- */
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);
@@ -130,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