[CRIU] [PATCH v2 07/13] pid_ns: Simplify do_create_pid_ns_helper() using ns helpers

Kirill Tkhai ktkhai at virtuozzo.com
Tue May 16 09:26:36 PDT 2017


Get pid_ns fd from INIT_PID task of this namespace and
use switch_ns() and restore_ns().

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/namespaces.c |   21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)

diff --git a/criu/namespaces.c b/criu/namespaces.c
index 398ff671f..329a2aaf8 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -2621,30 +2621,17 @@ static int pid_ns_helper(struct ns_id *ns, int sk)
 
 static int do_create_pid_ns_helper(void *arg, int sk, pid_t unused_pid)
 {
-	int pid_ns_fd, mnt_ns_fd, fd, i, lock_fd, transport_fd;
+	int pid_ns_fd, mnt_ns_fd, i, lock_fd, transport_fd;
 	struct pstree_item *ns_reaper;
 	struct ns_id *ns, *tmp;
 	struct pid *pid;
 	pid_t child;
 
-	pid_ns_fd = open_proc(PROC_SELF, "ns/pid");
-	if (pid_ns_fd < 0) {
-		pr_perror("Can't open pid ns");
-		goto err;
-	}
 	ns_reaper = *(struct pstree_item **)arg;
 	ns = lookup_ns_by_id(ns_reaper->ids->pid_ns_id, &pid_ns_desc);
 
-	fd = fdstore_get(ns->pid.nsfd_id);
-	if (fd < 0) {
-		pr_err("Can't get pid_ns fd\n");
+	if (switch_ns(ns_reaper->pid->real, &pid_ns_desc, &pid_ns_fd) < 0)
 		goto err;
-	}
-	if (setns(fd, CLONE_NEWPID) < 0) {
-		pr_perror("Can't setns");
-		goto err;
-	}
-	close(fd);
 
 	pid = __pstree_pid_by_virt(ns, ns->ns_pid);
 	if (!pid) {
@@ -2709,10 +2696,8 @@ static int do_create_pid_ns_helper(void *arg, int sk, pid_t unused_pid)
 	close(lock_fd);
 	pid->real = child;
 
-	if (setns(pid_ns_fd, CLONE_NEWPID) < 0) {
-		pr_perror("Restore ns");
+	if (restore_ns(pid_ns_fd, &pid_ns_desc) < 0)
 		return -1;
-	}
 	return 0;
 err:
 	close_safe(&sk);



More information about the CRIU mailing list