[CRIU] [PATCH 1/2] namespaces: add switch_ns_by_fd

Andrei Vagin avagin at openvz.org
Wed Oct 26 23:18:52 PDT 2016


From: Andrei Vagin <avagin at virtuozzo.com>

It's like switch_ns, but it gets a namespace file descriptor instead of pid.

Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
 criu/include/namespaces.h |  1 +
 criu/namespaces.c         | 24 ++++++++++++++++--------
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/criu/include/namespaces.h b/criu/include/namespaces.h
index ece23d7..c9f3e8a 100644
--- a/criu/include/namespaces.h
+++ b/criu/include/namespaces.h
@@ -127,6 +127,7 @@ extern int prepare_namespace_before_tasks(void);
 extern int prepare_namespace(struct pstree_item *item, unsigned long clone_flags);
 
 extern int switch_ns(int pid, struct ns_desc *nd, int *rst);
+extern int switch_ns_by_fd(int nsfd, struct ns_desc *nd, int *rst);
 extern int restore_ns(int rst, struct ns_desc *nd);
 
 extern int dump_task_ns_ids(struct pstree_item *);
diff --git a/criu/namespaces.c b/criu/namespaces.c
index 3b257d0..5a434a1 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -221,39 +221,47 @@ bool check_ns_proc(struct fd_link *link)
 
 int switch_ns(int pid, struct ns_desc *nd, int *rst)
 {
-	char buf[32];
 	int nsfd;
-	int ret = -1;
+	int ret;
 
 	nsfd = open_proc(pid, "ns/%s", nd->str);
 	if (nsfd < 0) {
 		pr_perror("Can't open ns file");
-		goto err_ns;
+		return -1;
 	}
 
+	ret = switch_ns_by_fd(nsfd, nd, rst);
+
+	close(nsfd);
+
+	return ret;
+}
+
+int switch_ns_by_fd(int nsfd, struct ns_desc *nd, int *rst)
+{
+	char buf[32];
+	int ret = -1;
+
 	if (rst) {
 		snprintf(buf, sizeof(buf), "/proc/self/ns/%s", nd->str);
 		*rst = open(buf, O_RDONLY);
 		if (*rst < 0) {
 			pr_perror("Can't open ns file");
-			goto err_rst;
+			goto err_ns;
 		}
 	}
 
 	ret = setns(nsfd, nd->cflag);
 	if (ret < 0) {
-		pr_perror("Can't setns %d/%s", pid, nd->str);
+		pr_perror("Can't setns %d/%s", nsfd, nd->str);
 		goto err_set;
 	}
 
-	close(nsfd);
 	return 0;
 
 err_set:
 	if (rst)
 		close(*rst);
-err_rst:
-	close(nsfd);
 err_ns:
 	return -1;
 }
-- 
2.7.4



More information about the CRIU mailing list