[1/3] proc_parse: Add helpers for debug sake

Submitted by Cyrill Gorcunov on Dec. 7, 2018, 12:43 p.m.

Details

Message ID 20181207124331.30751-2-gorcunov@gmail.com
State New
Series "mount: Fix the case where tmp directory in already mounted"
Headers show

Commit Message

Cyrill Gorcunov Dec. 7, 2018, 12:43 p.m.
Suitable when need to look into details of data without
calling some external tools.

Note there is no real use of these helpers now but I've
been using them a lot when inspecting problems with
restore: instead of stopping restore I simply call
the helpers from various restore stages and compared
the outputs.

Still feel free to simply drop the patch.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
---
 criu/include/proc_parse.h |  4 ++
 criu/proc_parse.c         | 77 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)

Patch hide | download patch | download mbox

diff --git a/criu/include/proc_parse.h b/criu/include/proc_parse.h
index 96a097b3d8f3..01bac412f3d9 100644
--- a/criu/include/proc_parse.h
+++ b/criu/include/proc_parse.h
@@ -102,4 +102,8 @@  extern bool is_vma_range_fmt(char *line);
 extern void parse_vmflags(char *buf, u32 *flags, u64 *madv, int *io_pf);
 extern int parse_uptime(uint64_t *upt);
 
+extern void debug_mountinfo(char *prefix, pid_t pid);
+extern void debug_lsdir(char *prefix, char *path);
+extern void debug_lsdirfd(char *prefix, int rfd, char *path);
+
 #endif /* __CR_PROC_PARSE_H__ */
diff --git a/criu/proc_parse.c b/criu/proc_parse.c
index 1a5722eaf42b..b62c4395e5cb 100644
--- a/criu/proc_parse.c
+++ b/criu/proc_parse.c
@@ -41,6 +41,7 @@ 
 #include "timerfd.h"
 #include "path.h"
 #include "fault-injection.h"
+#include "criu-log.h"
 
 #include "protobuf.h"
 #include "images/fdinfo.pb-c.h"
@@ -1465,6 +1466,82 @@  static bool should_skip_mount(const char *mountpoint)
 	return false;
 }
 
+void debug_mountinfo(char *prefix, pid_t pid)
+{
+	char buf[4096];
+	char path[64];
+	FILE *f;
+
+	if (pr_quelled(LOG_DEBUG))
+		return;
+
+	snprintf(path, sizeof(path), "/proc/%d/mountinfo", pid);
+	f = fopen(path, "r");
+	if (!f) {
+		pr_perror("%s:%s %d: Can't open %s", __func__, prefix, pid, path);
+		return;
+	}
+
+	while (fgets(buf, sizeof(buf), f))
+		pr_debug("%s:%s %d: %s", __func__, prefix, pid, buf);
+	fclose(f);
+}
+
+void debug_lsdirfd(char *prefix, int rfd, char *path)
+{
+	struct dirent *de;
+	DIR *dir;
+	int fd;
+
+	if (pr_quelled(LOG_DEBUG))
+		return;
+
+	fd = openat(rfd, path, O_RDONLY | O_DIRECTORY, 0);
+	if (fd < 0) {
+		pr_perror("%s:%s Can't open %d:%s", __func__, prefix, rfd, path);
+		return;
+	}
+
+	dir = fdopendir(fd);
+	if (!dir) {
+		pr_perror("%s:%s Can't open %d:%s", __func__, prefix, rfd, path);
+		close(fd);
+		return;
+	}
+	for (de = readdir(dir); de; de = readdir(dir)) {
+		if (!strcmp(de->d_name, ".") ||
+		    !strcmp(de->d_name, ".."))
+			continue;
+		pr_debug("%s:%s d_ino %8ld d_name %s\n",
+			 __func__, prefix, de->d_ino, de->d_name);
+	}
+	closedir(dir);
+	close(fd);
+}
+
+void debug_lsdir(char *prefix, char *path)
+{
+	struct dirent *de;
+	DIR *dir;
+
+	if (pr_quelled(LOG_DEBUG))
+		return;
+
+	dir = opendir(path);
+	if (!dir) {
+		pr_perror("%s:%s Can't open %s", __func__, prefix, path);
+		return;
+	}
+	for (de = readdir(dir); de; de = readdir(dir)) {
+		if (!strcmp(de->d_name, ".") ||
+		    !strcmp(de->d_name, ".."))
+			continue;
+		pr_debug("%s:%s d_ino %8ld d_name %s\n",
+			 __func__, prefix, de->d_ino, de->d_name);
+	}
+	closedir(dir);
+}
+
 struct mount_info *parse_mountinfo(pid_t pid, struct ns_id *nsid, bool for_dump)
 {
 	struct mount_info *list = NULL;