[CRIU] [PATCH 2/6] util: Prepare proc opening helpers to open any files

Pavel Emelyanov xemul at parallels.com
Thu Jun 5 09:15:24 PDT 2014


We have a set of routines that open /proc/$pid files via proc service
descriptor. Teach them to accept non-pids as pids to open /proc/self/*
and /proc/* files via the same engine.

Signed-f-off-by: Pavel Emelyanov <xemul at parallels.com>

---
 include/util.h | 11 +++++++++++
 util.c         | 17 ++++++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/include/util.h b/include/util.h
index 3605b3c..22a0f3d 100644
--- a/include/util.h
+++ b/include/util.h
@@ -130,6 +130,17 @@ extern int open_pid_proc(pid_t pid);
 extern int close_pid_proc(void);
 extern int set_proc_fd(int fd);
 
+/*
+ * Values for pid argument of the proc opening routines below.
+ * SELF would open file under /proc/self
+ * GEN would open a file under /proc itself
+ * NONE is internal, don't use it ;)
+ */
+
+#define PROC_SELF	0
+#define PROC_GEN	-1
+#define PROC_NONE	-2
+
 extern int do_open_proc(pid_t pid, int flags, const char *fmt, ...);
 
 #define __open_proc(pid, flags, fmt, ...)				\
diff --git a/util.c b/util.c
index 81f1741..975af30 100644
--- a/util.c
+++ b/util.c
@@ -160,7 +160,7 @@ int move_img_fd(int *img_fd, int want_fd)
 	return 0;
 }
 
-static pid_t open_proc_pid = 0;
+static pid_t open_proc_pid = PROC_NONE;
 static int open_proc_fd = -1;
 
 int close_pid_proc(void)
@@ -171,7 +171,7 @@ int close_pid_proc(void)
 		ret = close(open_proc_fd);
 
 	open_proc_fd = -1;
-	open_proc_pid = 0;
+	open_proc_pid = PROC_NONE;
 
 	return ret;
 }
@@ -228,7 +228,18 @@ inline int open_pid_proc(pid_t pid)
 		dfd = get_service_fd(PROC_FD_OFF);
 	}
 
-	snprintf(path, sizeof(path), "%d", pid);
+	if (pid == PROC_GEN)
+		/*
+		 * Don't cache it, close_pid_proc() would
+		 * close service descriptor otherwise.
+		 */
+		return dfd;
+
+	if (pid == PROC_SELF)
+		snprintf(path, sizeof(path), "self");
+	else
+		snprintf(path, sizeof(path), "%d", pid);
+
 	fd = openat(dfd, path, O_RDONLY);
 	if (fd < 0)
 		pr_perror("Can't open %s", path);
-- 
1.8.4.2


More information about the CRIU mailing list