[CRIU] [PATCH cr 8/8] restore: mount proc for a new pid namespace

Andrey Vagin avagin at openvz.org
Wed May 16 04:32:41 EDT 2012


proc is needed for restoring shared mappings.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 cr-restore.c |   35 ++++++++++++++++++++++++++++++++---
 1 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/cr-restore.c b/cr-restore.c
index a257925..5bea27e 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -17,6 +17,7 @@
 #include <sys/wait.h>
 #include <sys/file.h>
 #include <sys/shm.h>
+#include <sys/mount.h>
 
 #include <sched.h>
 
@@ -506,9 +507,7 @@ static inline int fork_with_pid(int pid, unsigned long ns_clone_flags)
 		goto err_close;
 	}
 
-	if (pid == 1) {
-		ca.clone_flags |= CLONE_NEWPID;
-	} else
+	if (pid != 1)
 		if (write_img_buf(ca.fd, buf, strlen(buf)))
 			goto err_unlock;
 
@@ -601,6 +600,8 @@ static void restore_pgid(void)
 	}
 }
 
+static char proc_mountpoint[PATH_MAX] = "/proc";
+
 static int restore_task_with_children(void *_arg)
 {
 	struct cr_clone_arg *ca = _arg;
@@ -616,6 +617,15 @@ static int restore_task_with_children(void *_arg)
 		exit(-1);
 	}
 
+	if (pid == 1) { /* New pid namespace */
+		ret = mount("proc", proc_mountpoint, "proc", MS_MGC_VAL, NULL);
+		if (ret == -1) {
+			pr_err("mount failed");
+			exit(1);
+		}
+		set_proc_mountpoint(proc_mountpoint);
+	}
+
 	ret = log_init_by_pid();
 	if (ret < 0)
 		exit(1);
@@ -699,6 +709,15 @@ static int restore_root_task(pid_t pid, struct cr_options *opts)
 	 * this later.
 	 */
 
+	if (init->pid.pid == 1) {
+		sprintf(proc_mountpoint, "/tmp/crtools-proc.XXXXXX");
+		if (mkdtemp(proc_mountpoint) == NULL) {
+			pr_err("mkdtemp failed %m");
+			return -1;
+		}
+		opts->namespaces_flags |= CLONE_NEWPID;
+	}
+
 	ret = fork_with_pid(init->pid.pid, opts->namespaces_flags);
 	if (ret < 0)
 		return -1;
@@ -717,6 +736,16 @@ static int restore_root_task(pid_t pid, struct cr_options *opts)
 	ret = (int)futex_get(&task_entries->nr_in_progress);
 
 out:
+	if (init->pid.pid == 1) {
+		int err;
+		err = umount(proc_mountpoint);
+		if (err == -1)
+			pr_err("Can't umount %s", proc_mountpoint);
+		err = rmdir(proc_mountpoint);
+		if (err == -1)
+			pr_err("Can't delete %s", proc_mountpoint);
+	}
+
 	if (ret < 0) {
 		pr_err("Someone can't be restored\n");
 		struct pstree_item *pi;
-- 
1.7.1



More information about the CRIU mailing list