[CRIU] [PATCH 1/2] fsnotify: parse_fdinfo_pid_s() returns allocated object for inotify wd

Andrey Vagin avagin at openvz.org
Wed Aug 20 03:31:17 PDT 2014


We are going to collect all objects in a list and write them into
an inotify image. The inotify wd image will be depricated.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 cr-check.c           |  3 ++-
 fsnotify.c           | 18 ++++++++++++++----
 include/fsnotify.h   |  9 +++++++++
 include/proc_parse.h |  5 ++++-
 proc_parse.c         | 44 ++++++++++++++++++++++++++++++--------------
 5 files changed, 59 insertions(+), 20 deletions(-)

diff --git a/cr-check.c b/cr-check.c
index df51484..fe7d03c 100644
--- a/cr-check.c
+++ b/cr-check.c
@@ -353,7 +353,8 @@ pipe_err:
 
 static int check_one_inotify(union fdinfo_entries *e, void *arg)
 {
-	*(int *)arg = e->ify.wd;
+	*(int *)arg = e->ify.e.wd;
+	free_inotify_wd_entry(e);
 	return 0;
 }
 
diff --git a/fsnotify.c b/fsnotify.c
index 77e3332..2b18b3f 100644
--- a/fsnotify.c
+++ b/fsnotify.c
@@ -199,7 +199,7 @@ err:
 
 static int dump_inotify_entry(union fdinfo_entries *e, void *arg)
 {
-	InotifyWdEntry *we = &e->ify;
+	InotifyWdEntry *we = &e->ify.e;
 
 	we->id = *(u32 *)arg;
 	pr_info("wd: wd 0x%08x s_dev 0x%08x i_ino 0x%16"PRIx64" mask 0x%08x\n",
@@ -211,7 +211,12 @@ static int dump_inotify_entry(union fdinfo_entries *e, void *arg)
 	if (check_open_handle(we->s_dev, we->i_ino, we->f_handle))
 		return -1;
 
-	return pb_write_one(fdset_fd(glob_fdset, CR_FD_INOTIFY_WD), we, PB_INOTIFY_WD);
+	if (pb_write_one(fdset_fd(glob_fdset, CR_FD_INOTIFY_WD), we, PB_INOTIFY_WD))
+		return -1;
+
+	free_inotify_wd_entry(e);
+
+	return 0;
 }
 
 static int dump_one_inotify(int lfd, u32 id, const struct fd_parms *p)
@@ -231,8 +236,13 @@ static int dump_one_inotify(int lfd, u32 id, const struct fd_parms *p)
 
 static int pre_dump_inotify_entry(union fdinfo_entries *e, void *arg)
 {
-	InotifyWdEntry *we = &e->ify;
-	return irmap_queue_cache(we->s_dev, we->i_ino, we->f_handle);
+	InotifyWdEntry *we = &e->ify.e;
+	int ret;
+
+	ret = irmap_queue_cache(we->s_dev, we->i_ino, we->f_handle);
+	free_inotify_wd_entry(e);
+
+	return ret;
 }
 
 static int pre_dump_one_inotify(int pid, int lfd)
diff --git a/include/fsnotify.h b/include/fsnotify.h
index cabf525..1f4b59e 100644
--- a/include/fsnotify.h
+++ b/include/fsnotify.h
@@ -4,6 +4,9 @@
 #include "asm/types.h"
 #include "files.h"
 
+#include "protobuf.h"
+#include "protobuf/fsnotify.pb-c.h"
+
 struct fsnotify_params {
 	u32	id;
 	u32	faflags;
@@ -19,4 +22,10 @@ extern struct collect_image_info inotify_mark_cinfo;
 extern struct collect_image_info fanotify_cinfo;
 extern struct collect_image_info fanotify_mark_cinfo;
 
+struct inotify_wd_entry {
+	InotifyWdEntry e;
+	FhEntry f_handle;
+	struct list_head node;
+};
+
 #endif /* __CR_FSNOTIFY_H__ */
diff --git a/include/proc_parse.h b/include/proc_parse.h
index 35d4292..f0c72db 100644
--- a/include/proc_parse.h
+++ b/include/proc_parse.h
@@ -6,6 +6,7 @@
 #include "image.h"
 #include "list.h"
 #include "cgroup.h"
+#include "fsnotify.h"
 
 #include "protobuf/eventfd.pb-c.h"
 #include "protobuf/eventpoll.pb-c.h"
@@ -170,11 +171,13 @@ union fdinfo_entries {
 	EventfdFileEntry efd;
 	EventpollTfdEntry epl;
 	SignalfdEntry sfd;
-	InotifyWdEntry ify;
+	struct inotify_wd_entry ify;
 	FanotifyMarkEntry ffy;
 	TimerfdEntry tfy;
 };
 
+extern void free_inotify_wd_entry(union fdinfo_entries *e);
+
 struct fdinfo_common {
 	off64_t pos;
 	int flags;
diff --git a/proc_parse.c b/proc_parse.c
index 271d46b..7f5e466 100644
--- a/proc_parse.c
+++ b/proc_parse.c
@@ -1021,6 +1021,11 @@ static void free_fhandle(FhEntry *fh)
 		xfree(fh->handle);
 }
 
+void free_inotify_wd_entry(union fdinfo_entries *e) {
+	free_fhandle(e->ify.e.f_handle);
+	xfree(e);
+}
+
 static void parse_fhandle_encoded(char *tok, FhEntry *fh)
 {
 	char *d = (char *)fh->handle;
@@ -1273,39 +1278,50 @@ static int parse_fdinfo_pid_s(char *pid, int fd, int type,
 			continue;
 		}
 		if (fdinfo_field(str, "inotify wd")) {
-			FhEntry f_handle = FH_ENTRY__INIT;
+			InotifyWdEntry *ify;
+			union fdinfo_entries *e;
 			int hoff;
 
-			inotify_wd_entry__init(&entry.ify);
-			entry.ify.f_handle = &f_handle;
-
 			if (type != FD_TYPES__INOTIFY)
 				goto parse_err;
+
+			e = xmalloc(sizeof(*e));
+			if (!e)
+				goto parse_err;
+			ify = &e->ify.e;
+
+			inotify_wd_entry__init(ify);
+			ify->f_handle = &e->ify.f_handle;
+			fh_entry__init(ify->f_handle);
+
 			ret = sscanf(str,
 					"inotify wd:%x ino:%"PRIx64" sdev:%x "
 					"mask:%x ignored_mask:%x "
 					"fhandle-bytes:%x fhandle-type:%x "
 					"f_handle: %n",
-					&entry.ify.wd, &entry.ify.i_ino, &entry.ify.s_dev,
-					&entry.ify.mask, &entry.ify.ignored_mask,
-					&entry.ify.f_handle->bytes, &entry.ify.f_handle->type,
+					&ify->wd, &ify->i_ino, &ify->s_dev,
+					&ify->mask, &ify->ignored_mask,
+					&ify->f_handle->bytes, &ify->f_handle->type,
 					&hoff);
-			if (ret != 7)
+			if (ret != 7) {
+				free_inotify_wd_entry(e);
 				goto parse_err;
+			}
 
-			if (alloc_fhandle(&f_handle)) {
+			if (alloc_fhandle(ify->f_handle)) {
+				free_inotify_wd_entry(e);
 				ret = -1;
 				goto out;
 			}
 
-			parse_fhandle_encoded(str + hoff, entry.ify.f_handle);
-
-			ret = cb(&entry, arg);
+			parse_fhandle_encoded(str + hoff, ify->f_handle);
 
-			free_fhandle(&f_handle);
+			ret = cb(e, arg);
 
-			if (ret)
+			if (ret) {
+				free_inotify_wd_entry(e);
 				goto out;
+			}
 
 			entry_met = true;
 			continue;
-- 
1.9.3



More information about the CRIU mailing list