[CRIU] [PATCH cr 09/13] util: add ability to change a proc mount point (v2)

Andrey Vagin avagin at openvz.org
Tue Jun 19 07:53:13 EDT 2012


v2:  rework this by using openat() and service fds for proc root.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 cr-restore.c      |    2 +-
 include/crtools.h |    1 +
 include/util.h    |    2 ++
 util.c            |   44 ++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/cr-restore.c b/cr-restore.c
index 7c359c9..cfcf344 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -981,7 +981,7 @@ static int sigreturn_restore(pid_t pid, struct list_head *tgt_vmas, int nr_vmas)
 	restore_thread_vma_len	= 0;
 
 	ret = parse_smaps(pid, &self_vma_list, false);
-	close_pid_proc();
+	close_proc();
 	if (ret < 0)
 		goto err;
 
diff --git a/include/crtools.h b/include/crtools.h
index cf3f481..d99e5ab 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -84,6 +84,7 @@ enum {
 	LOG_FD_OFF = 1,
 	IMG_FD_OFF,
 	SELF_EXE_FD_OFF,
+	PROC_FD_OFF,
 };
 
 int get_service_fd(int type);
diff --git a/include/util.h b/include/util.h
index db850c3..669558c 100644
--- a/include/util.h
+++ b/include/util.h
@@ -168,6 +168,8 @@ extern int reopen_fd_as_safe(char *file, int line, int new_fd, int old_fd, bool
 #define reopen_fd_as(new_fd, old_fd)		reopen_fd_as_safe(__FILE__, __LINE__, new_fd, old_fd, false)
 #define reopen_fd_as_nocheck(new_fd, old_fd)	reopen_fd_as_safe(__FILE__, __LINE__, new_fd, old_fd, true)
 
+int set_proc_mountpoint(char *path);
+void close_proc(void);
 int open_pid_proc(pid_t pid);
 int close_pid_proc(void);
 
diff --git a/util.c b/util.c
index ee35f70..82980c7 100644
--- a/util.c
+++ b/util.c
@@ -205,6 +205,7 @@ void close_image_dir(void)
 
 static pid_t open_proc_pid = 0;
 static int open_proc_fd = -1;
+static int proc_dir_fd = -1;
 
 int close_pid_proc(void)
 {
@@ -219,6 +220,38 @@ int close_pid_proc(void)
 	return ret;
 }
 
+void close_proc()
+{
+	close_pid_proc();
+	if (proc_dir_fd > 0)
+		close(proc_dir_fd);
+	proc_dir_fd = -1;
+}
+
+int set_proc_mountpoint(char *path)
+{
+	int sfd = get_service_fd(PROC_FD_OFF), fd;
+
+	close_proc();
+
+	fd = open(path, O_DIRECTORY | O_RDONLY);
+	if (fd == -1) {
+		pr_err("Can't open %s\n", path);
+		return -1;
+	}
+
+	sfd = dup2(fd, sfd);
+	close(fd);
+	if (sfd < 0) {
+		pr_err("Can't set proc fd");
+		return -1;
+	}
+
+	proc_dir_fd = sfd;
+
+	return 0;
+}
+
 inline int open_pid_proc(pid_t pid)
 {
 	char path[18];
@@ -228,8 +261,15 @@ inline int open_pid_proc(pid_t pid)
 		return open_proc_fd;
 
 	close_pid_proc();
-	sprintf(path, "/proc/%d", pid);
-	fd = open(path, O_RDONLY);
+
+	if (proc_dir_fd == -1) {
+		fd = set_proc_mountpoint("/proc");
+		if (fd < 0)
+			return fd;
+	}
+
+	sprintf(path, "%d", pid);
+	fd = openat(proc_dir_fd, path, O_RDONLY);
 	if (fd < 0)
 		pr_perror("Can't open %s", path);
 	else {
-- 
1.7.1



More information about the CRIU mailing list