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

Submitted by Andrei Vagin on Oct. 28, 2016, 5:41 p.m.

Details

Message ID 1477676516-28669-3-git-send-email-avagin@openvz.org
State Superseded
Series "Dump and restore nested network namespaces"
Headers show

Commit Message

Andrei Vagin Oct. 28, 2016, 5:41 p.m.
From: Andrew Vagin <avagin@virtuozzo.com>

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

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

Patch hide | download patch | download mbox

diff --git a/criu/files.c b/criu/files.c
index b945577..9227c90 100644
--- a/criu/files.c
+++ b/criu/files.c
@@ -124,6 +124,30 @@  void collect_used_fd(struct fdinfo_list_entry *new_fle, struct rst_info *ri)
 	list_add_tail(&new_fle->used_list, &fle->used_list);
 }
 
+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 2b6d58d..3882c19 100644
--- a/criu/include/files.h
+++ b/criu/include/files.h
@@ -120,6 +120,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 */