[CRIU] [PATCH 04/11] files: add a function to reopen fd as an unused fd

Andrei Vagin avagin at openvz.org
Wed Feb 1 16:04:24 PST 2017


From: Andrew Vagin <avagin at virtuozzo.com>

This function finds an unused fd and reopens a specified fd into it.

Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
 criu/files.c         | 37 +++++++++++++++++++++++++++++++++----
 criu/include/files.h |  2 ++
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/criu/files.c b/criu/files.c
index 4ef75b3..8e4ed1b 100644
--- a/criu/files.c
+++ b/criu/files.c
@@ -112,13 +112,10 @@ struct fdinfo_list_entry *find_used_fd(struct list_head *head, int fd)
 	return NULL;
 }
 
-void collect_task_fd(struct fdinfo_list_entry *new_fle, struct rst_info *ri)
+static void collect_used_fd(struct fdinfo_list_entry *new_fle, struct rst_info *ri)
 {
 	struct fdinfo_list_entry *fle;
 
-	/* fles in fds list are disordered */
-	list_add_tail(&new_fle->ps_list, &ri->fds);
-
 	/* fles in used list are ordered by fd */
 	list_for_each_entry(fle, &ri->used, used_list) {
 		if (new_fle->fe->fd < fle->fe->fd)
@@ -128,6 +125,38 @@ void collect_task_fd(struct fdinfo_list_entry *new_fle, struct rst_info *ri)
 	list_add_tail(&new_fle->used_list, &fle->used_list);
 }
 
+void collect_task_fd(struct fdinfo_list_entry *new_fle, struct rst_info *ri)
+{
+	/* fles in fds list are disordered */
+	list_add_tail(&new_fle->ps_list, &ri->fds);
+
+	return collect_used_fd(new_fle, ri);
+}
+
+int reopen_as_unused_fd(int fd, struct rst_info *rst)
+{
+	struct {
+		struct fdinfo_list_entry fle;
+		FdinfoEntry		 fe;
+	} *fd_entry;
+	int tfd;
+
+	tfd = find_unused_fd(&rst->used, fd);
+	if (tfd < 0)
+		return -1;
+
+	if (reopen_fd_as(tfd, fd))
+		return -1;
+
+	fd_entry = xmalloc(sizeof(*fd_entry));
+	fd_entry->fle.fe = &fd_entry->fe;
+	fd_entry->fe.fd = tfd;
+
+	collect_used_fd(&fd_entry->fle, rst);
+
+	return tfd;
+}
+
 unsigned int find_unused_fd(struct list_head *head, int hint_fd)
 {
 	struct fdinfo_list_entry *fle;
diff --git a/criu/include/files.h b/criu/include/files.h
index f1216a1..30a5615 100644
--- a/criu/include/files.h
+++ b/criu/include/files.h
@@ -120,6 +120,8 @@ void collect_task_fd(struct fdinfo_list_entry *new_fle, struct rst_info *ri);
 unsigned int find_unused_fd(struct list_head *head, int hint_fd);
 struct fdinfo_list_entry *find_used_fd(struct list_head *head, int fd);
 
+int reopen_as_unused_fd(int fd, struct rst_info *rst);
+
 struct file_desc {
 	u32			id;		/* File id, unique */
 	struct hlist_node	hash;		/* Descriptor hashing and lookup */
-- 
2.7.4



More information about the CRIU mailing list