[CRIU] [PATCH] service: add ability to set inherit file descriptors (v3)

Andrew Vagin avagin at openvz.org
Mon Mar 30 02:35:51 PDT 2015


From: Andrey Vagin <avagin at openvz.org>

This is required to use criu swrk in libcontainer.

v2: remove useless function declaration
    allow to set inherit_fd only for swrk
v3: check swrk out of loop

Cc: Saied Kazemi <saied at google.com>
Signed-off-by: Andrey Vagin <avagin at openvz.org>
Signed-off-by: Andrew Vagin <avagin at openvz.org>
---
 cr-service.c       | 10 ++++++++++
 crtools.c          |  2 +-
 files.c            | 14 ++++++++++----
 include/files.h    |  3 ++-
 protobuf/rpc.proto |  6 ++++++
 5 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/cr-service.c b/cr-service.c
index d202d14..45db04b 100644
--- a/cr-service.c
+++ b/cr-service.c
@@ -18,6 +18,7 @@
 #include "util.h"
 #include "log.h"
 #include "cpu.h"
+#include "files.h"
 #include "pstree.h"
 #include "cr-service.h"
 #include "cr-service-const.h"
@@ -344,6 +345,15 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
 			goto err;
 	}
 
+	if (req->n_inherit_fd && !opts.swrk_restore) {
+		pr_err("inherit_fd is not allowed in standalone service\n");
+		goto err;
+	}
+	for (i = 0; i < req->n_inherit_fd; i++) {
+		if (inherit_fd_add(req->inherit_fd[i]->fd, req->inherit_fd[i]->key))
+			goto err;
+	}
+
 	for (i = 0; i < req->n_cg_root; i++) {
 		if (new_cg_root_add(req->cg_root[i]->ctrl,
 					req->cg_root[i]->path))
diff --git a/crtools.c b/crtools.c
index 0b3c497..4412e4c 100644
--- a/crtools.c
+++ b/crtools.c
@@ -409,7 +409,7 @@ int main(int argc, char *argv[], char *envp[])
 			}
 			break;
 		case 1062:
-			if (inherit_fd_add(optarg) < 0)
+			if (inherit_fd_parse(optarg) < 0)
 				return 1;
 			break;
 		case 1063:
diff --git a/files.c b/files.c
index acd5735..096caf4 100644
--- a/files.c
+++ b/files.c
@@ -1253,14 +1253,12 @@ static int inherit_fd_reused(struct inherit_fd *inh)
  * We can't print diagnostics messages in this function because the
  * log file isn't initialized yet.
  */
-int inherit_fd_add(char *optarg)
+int inherit_fd_parse(char *optarg)
 {
 	char *cp = NULL;
 	int n = -1;
 	int fd = -1;
 	int dbg = 0;
-	struct stat sbuf;
-	struct inherit_fd *inh;
 
 	/*
 	 * Parse the argument.
@@ -1295,6 +1293,14 @@ int inherit_fd_add(char *optarg)
 		return 0;
 	}
 
+	return inherit_fd_add(fd, cp);
+}
+
+int inherit_fd_add(int fd, char *key)
+{
+	struct inherit_fd *inh;
+	struct stat sbuf;
+
 	if (fstat(fd, &sbuf) == -1) {
 		pr_perror("Can't fstat inherit fd %d", fd);
 		return -1;
@@ -1304,7 +1310,7 @@ int inherit_fd_add(char *optarg)
 	if (inh == NULL)
 		return -1;
 
-	inh->inh_id = cp;
+	inh->inh_id = key;
 	inh->inh_fd = fd;
 	inh->inh_dev = sbuf.st_dev;
 	inh->inh_ino = sbuf.st_ino;
diff --git a/include/files.h b/include/files.h
index 67cb689..db7e108 100644
--- a/include/files.h
+++ b/include/files.h
@@ -169,7 +169,8 @@ extern struct collect_image_info ext_file_cinfo;
 extern int dump_unsupp_fd(struct fd_parms *p, int lfd,
 			  struct cr_img *, char *more, char *info);
 
-extern int inherit_fd_add(char *optarg);
+extern int inherit_fd_parse(char *optarg);
+extern int inherit_fd_add(int fd, char *key);
 extern void inherit_fd_log(void);
 extern int inherit_fd_resolve_clash(int fd);
 extern int inherit_fd_fini(void);
diff --git a/protobuf/rpc.proto b/protobuf/rpc.proto
index ed76d4f..5229a5f 100644
--- a/protobuf/rpc.proto
+++ b/protobuf/rpc.proto
@@ -15,6 +15,11 @@ message ext_mount_map {
 	required string		val	= 2;
 };
 
+message inherit_fd {
+	required string		key	= 1;
+	required int32		fd	= 2;
+};
+
 message cgroup_root {
 	optional string		ctrl	= 1;
 	required string		path	= 2;
@@ -55,6 +60,7 @@ message criu_opts {
 	repeated cgroup_root		cg_root		= 25;
 
 	optional bool			rst_sibling	= 26; /* swrk only */
+	repeated inherit_fd		inherit_fd	= 27; /* swrk only */
 }
 
 message criu_dump_resp {
-- 
2.1.0



More information about the CRIU mailing list