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

Andrei Vagin avagin at openvz.org
Wed Aug 31 15:55:25 PDT 2016


From: Andrew Vagin <avagin at virtuozzo.com>

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

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

diff --git a/criu/files.c b/criu/files.c
index d2e45c5..1aaee70 100644
--- a/criu/files.c
+++ b/criu/files.c
@@ -111,6 +111,30 @@ struct fdinfo_list_entry *find_used_fd(struct list_head *head, int fd)
 	return NULL;
 }
 
+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 19fe977..efb2f7f 100644
--- a/criu/include/files.h
+++ b/criu/include/files.h
@@ -130,6 +130,8 @@ static inline void collect_gen_fd(struct fdinfo_list_entry *fle, struct rst_info
 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