[CRIU] [PATCH 05/15] crtools: add ability to create and close a service fd

Andrey Vagin avagin at openvz.org
Thu Jan 10 05:00:30 EST 2013


A service fd should be created, otherwise get_service_fd returns -1

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 include/crtools.h |  2 ++
 util.c            | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/include/crtools.h b/include/crtools.h
index 766f1c0..1ec1a3a 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -124,6 +124,8 @@ enum sfd_type {
 extern int clone_service_fd(bool to_init);
 extern int init_service_fd(void);
 extern int get_service_fd(enum sfd_type type);
+extern int open_service_fd(enum sfd_type type, int fd);
+extern int close_service_fd(enum sfd_type type);
 extern bool is_service_fd(int fd, enum sfd_type type);
 extern bool is_any_service_fd(int fd);
 
diff --git a/util.c b/util.c
index 1d11478..31adf30 100644
--- a/util.c
+++ b/util.c
@@ -315,12 +315,47 @@ static int __get_service_fd(enum sfd_type type, int service_fd_id)
 	return service_fd_rlim_cur - type - SERVICE_FD_MAX * service_fd_id;
 }
 
+static DECLARE_BITMAP(sfd_map, SERVICE_FD_MAX);
+
+int open_service_fd(enum sfd_type type, int fd)
+{
+	int sfd = __get_service_fd(type, service_fd_id);
+
+	BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
+
+	if (dup2(fd, sfd) != sfd) {
+		pr_perror("Dup %d -> %d failed", fd, sfd);
+		return -1;
+	}
+
+	set_bit(type, sfd_map);
+	return sfd;
+}
+
 int get_service_fd(enum sfd_type type)
 {
 	BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
+
+	if (!test_bit(type, sfd_map))
+		return -1;
+
 	return __get_service_fd(type, service_fd_id);
 }
 
+int close_service_fd(enum sfd_type type)
+{
+	int fd = get_service_fd(type);
+
+	if (fd < 0)
+		return 0;
+
+	if (close_safe(&fd))
+		return -1;
+
+	clear_bit(type, sfd_map);
+	return 0;
+}
+
 int clone_service_fd(bool to_init)
 {
 	int ret = -1, i;
-- 
1.7.11.7



More information about the CRIU mailing list