[CRIU] [PATCH 2/6] proc: Add parse_mountinfo helper

Cyrill Gorcunov gorcunov at openvz.org
Tue Apr 17 09:22:27 EDT 2012


To restore inotify we need to know the
mount point device numbers, so this helper
parses /proc/pid/mountinfo file for that.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/proc_parse.h |    8 ++++++++
 include/util.h       |    1 +
 proc_parse.c         |   43 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/include/proc_parse.h b/include/proc_parse.h
index e316caf..3356cbd 100644
--- a/include/proc_parse.h
+++ b/include/proc_parse.h
@@ -85,6 +85,14 @@ struct proc_status_creds {
 	unsigned int cap_bnd[PROC_CAP_SIZE];
 };
 
+struct proc_mountinfo {
+	int		mnt_id;
+	int		parent_mnt_id;
+	unsigned int	s_dev;
+	char		mnt_root[64];
+};
+
+extern int parse_mountinfo(pid_t pid, struct proc_mountinfo *mi, int nr_elems);
 extern int parse_pid_stat(pid_t pid, struct proc_pid_stat *s);
 extern int parse_pid_stat_small(pid_t pid, struct proc_pid_stat_small *s);
 extern int parse_maps(pid_t pid, struct list_head *vma_area_list, bool use_map_files);
diff --git a/include/util.h b/include/util.h
index 60ff126..aacf4d9 100644
--- a/include/util.h
+++ b/include/util.h
@@ -256,6 +256,7 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...);
 
 #define KDEV_MINORBITS	20
 #define KDEV_MINORMASK	((1UL << KDEV_MINORBITS) - 1)
+#define MKKDEV(ma,mi)	(((ma) << KDEV_MINORBITS) | (mi))
 
 static inline u32 kdev_major(u32 kdev)
 {
diff --git a/proc_parse.c b/proc_parse.c
index 2246369..021501d 100644
--- a/proc_parse.c
+++ b/proc_parse.c
@@ -389,3 +389,46 @@ err_parse:
 	fclose(f);
 	return 0;
 }
+
+int parse_mountinfo(pid_t pid, struct proc_mountinfo *mi, int nr_elems)
+{
+	FILE *f = NULL;
+	char str[256];
+	int i = 0;
+
+	snprintf(str, sizeof(str), "/proc/%d/mountinfo", pid);
+	f = fopen(str, "r");
+	if (!f) {
+		pr_perror("Can't open %d mountinfo", pid);
+		return -1;
+	}
+
+	while (fgets(str, sizeof(str), f)) {
+		unsigned int kmaj, kmin, parent_mnt_id;
+		char parent_mnt_root[63];
+		int ret;
+
+		if ((i + 1) >= nr_elems) {
+			i = -ENOMEM;
+			goto out_close;
+		}
+
+		ret = sscanf(str, "%i %i %u:%u %63s %63s",
+			     &mi[i].mnt_id, &parent_mnt_id,
+			     &kmaj, &kmin, parent_mnt_root,
+			     mi[i].mnt_root);
+		if (ret != 6) {
+			pr_err("Bad format in %d mountinfo\n", pid);
+			i = -1;
+			goto out_close;
+		}
+
+		mi[i].s_dev = MKKDEV(kmaj, kmin);
+		i++;
+	}
+
+out_close:
+	fclose(f);
+out:
+	return i;
+}
-- 
1.7.7.6



More information about the CRIU mailing list