From 6ecf660dfe04dfbd0cddc7670ee45e84977a5c95 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Thu, 27 Oct 2016 09:18:52 +0300 Subject: [PATCH] namespaces: add switch_ns_by_fd It's like switch_ns, but it gets a namespace file descriptor instead of pid. travis-ci: success for net: simplify restore of macvlan-s (rev2) Signed-off-by: Andrei Vagin Signed-off-by: Pavel Emelyanov --- 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 ece23d700..c9f3e8a74 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 3b257d095..5a434a1ef 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; }