[CRIU] [PATCH v3 13/16] files: find_unused_fd helper introduced

Stanislav Kinsburskiy skinsbursky at virtuozzo.com
Thu Dec 10 07:17:22 PST 2015


Simple helper to find unused file descriptor.
Return "hint" if unused or last used descriptor plus one.

Signed-off-by: Stanislav Kinsburskiy <skinsbursky at virtuozzo.com>
---
 include/files.h |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/include/files.h b/include/files.h
index fdfd641..d2d4822 100644
--- a/include/files.h
+++ b/include/files.h
@@ -127,6 +127,22 @@ static inline void collect_gen_fd(struct fdinfo_list_entry *fle, struct rst_info
 	collect_used_fd(fle, ri);
 }
 
+static inline int find_unused_fd(struct list_head *head, int hint_fd)
+{
+	struct fdinfo_list_entry *fle;
+	bool hint_fd_used = false;
+	int last_used_fd = 0;
+
+	list_for_each_entry(fle, head, used_list) {
+		if (fle->fe->fd == hint_fd)
+			hint_fd_used = true;
+		last_used_fd = fle->fe->fd;
+	}
+	if (hint_fd_used || (hint_fd < 0))
+		return last_used_fd + 1;
+	return hint_fd;
+}
+
 struct file_desc {
 	u32			id;		/* File id, unique */
 	struct hlist_node	hash;		/* Descriptor hashing and lookup */



More information about the CRIU mailing list