[CRIU] Re: [PATCH 06/11] util-net: Itroduce send_fds/recv_fds
routines
Cyrill Gorcunov
gorcunov at openvz.org
Wed Mar 28 15:44:24 EDT 2012
On Wed, Mar 28, 2012 at 10:47:13PM +0400, Pavel Emelyanov wrote:
> On 03/28/2012 05:36 PM, Cyrill Gorcunov wrote:
> >
> > We will need these helpers to transfer file
> > descriptors from dumpee to our space.
> >
> > Also make send_fd/recv_fd to be a wrappers over
> > send_fds/revc_fds to not duplicate the code.
> >
> > Note the return code -2 is dropped from recv_x
> > routines, it is never analyzed anyway.
> >
> > Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
> > ---
> > include/util-net.h | 21 +++++++++++-
> > util-net.c | 89 +++++++++++++++++++++++++++++++++++++++-------------
> > 2 files changed, 86 insertions(+), 24 deletions(-)
> >
>
> This kills the whole "beautification" effort of the previous
> patches. Patch send_fd and recv_fd (as they look after patch #5)
> so that they just wrap parts of their bodies into for() loops
> without this huge rewriting.
>
Something like below I hope?
Cyrill
-------------- next part --------------
>From 1f89f80dd0a2504a78ccfe4f812f44952119935a Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Wed, 28 Mar 2012 23:43:26 +0400
Subject: [PATCH] util-net: Itroduce send_fds/recv_fds routines
We will need these helpers to transfer file
descriptors from dumpee to our space.
Also make send_fd/recv_fd to be 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 | 21 ++++++++++++++++-
util-net.c | 61 ++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 64 insertions(+), 18 deletions(-)
diff --git a/include/util-net.h b/include/util-net.h
index 33806e3..2289d0e 100644
--- a/include/util-net.h
+++ b/include/util-net.h
@@ -28,6 +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 9d7bf12..24dc457 100644
--- a/util-net.c
+++ b/util-net.c
@@ -4,6 +4,7 @@
#include <errno.h>
#include "compiler.h"
+#include "builtins.h"
#include "types.h"
#include "syscall.h"
@@ -47,36 +48,64 @@ static int *scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr, i
return (int *)CMSG_DATA(cmsg);
}
-int send_fd(int sock, struct sockaddr_un *saddr, int len, int fd)
+int send_fds(int sock, struct sockaddr_un *saddr, int len, int *fds, int nr_fds)
{
struct scm_fdset fdset;
int *cmsg_data;
+ int i, min_fd, ret;
cmsg_data = scm_fdset_init(&fdset, saddr, len);
- scm_fdset_init_chunk(&fdset, 1);
- *cmsg_data = fd;
+ for (i = 0; i < nr_fds; i += min_fd) {
+ min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
+ scm_fdset_init_chunk(&fdset, min_fd);
+ builtin_memcpy(cmsg_data, &fds[i], sizeof(int) * min_fd);
- return sys_sendmsg(sock, &fdset.hdr, 0);
+ ret = sys_sendmsg(sock, &fdset.hdr, 0);
+ if (ret <= 0)
+ return ret ? : -1;
+ }
+
+ return 0;
}
-int recv_fd(int sock)
+int recv_fds(int sock, int *fds, int nr_fds)
{
struct scm_fdset fdset;
struct cmsghdr *cmsg;
int *cmsg_data;
int ret;
+ int i, min_fd;
cmsg_data = scm_fdset_init(&fdset, NULL, 0);
- scm_fdset_init_chunk(&fdset, 1);
-
- 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 -2;
-
- return *cmsg_data;
+ for (i = 0; i < nr_fds; i += min_fd) {
+ min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
+ scm_fdset_init_chunk(&fdset, min_fd);
+
+ ret = sys_recvmsg(sock, &fdset.hdr, 0);
+ if (ret <= 0)
+ return ret ? : -1;
+
+ cmsg = CMSG_FIRSTHDR(&fdset.hdr);
+ if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS)
+ return -EINVAL;
+
+ min_fd = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
+ /*
+ * In case if kernel screwed the recepient, most probably
+ * the caller stack frame will be overwriten, just scream
+ * and exit.
+ *
+ * FIXME Need to sanitize util.h to be able to include it
+ * into files which do not have glibc and a couple of
+ * sys_write_ helpers. Meawhile opencoded BUG_ON here.
+ */
+ if (unlikely(min_fd > CR_SCM_MAX_FD))
+ *(volatile unsigned long *)NULL = 0xdead0000 + __LINE__;
+ if (unlikely(min_fd <= 0))
+ return -1;
+ builtin_memcpy(&fds[i], cmsg_data, sizeof(int) * min_fd);
+ }
+
+ return 0;
}
--
1.7.7.6
More information about the CRIU
mailing list