[CRIU] [PATCH 1/3] files: add a function to reopen fd as an unused fd
Andrey Vagin
avagin at openvz.org
Wed Jul 27 11:21:05 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: Andrew 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 a66a142..5320ba8 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 e0b853d..32e23a7 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