[CRIU] [PATCH] crtools: close all desriptors only for the root task

Andrey Vagin avagin at openvz.org
Thu Nov 7 00:14:11 PST 2013


For all other tasks only unsed service descriptors will be closed.

This change allows to have file descriptors, which may be used for
restoring namespaces. All non-server descriptors must be closed before
restoring files.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 cr-restore.c        | 11 ++++++-----
 include/servicefd.h |  1 +
 util.c              | 13 +++++++++++++
 3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/cr-restore.c b/cr-restore.c
index aaf65e1..db6b03b 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -1253,6 +1253,9 @@ static int restore_task_with_children(void *_arg)
 
 	/* Restore root task */
 	if (current->parent == NULL) {
+		if (close_old_fds(current))
+			exit(1);
+
 		if (collect_mount_info(getpid()))
 			exit(1);
 
@@ -1290,11 +1293,9 @@ static int restore_task_with_children(void *_arg)
 	if (prepare_mappings(pid))
 		exit(1);
 
-	if (!(ca->clone_flags & CLONE_FILES)) {
-		ret = close_old_fds(current);
-		if (ret)
-			exit(1);
-	}
+	if ((!(ca->clone_flags & CLONE_FILES)) &&
+	    current->parent && current->parent->rst->fdt)
+		close_old_servie_fd(current->parent->rst->fdt->nr);
 
 	if (create_children_and_session())
 		exit(1);
diff --git a/include/servicefd.h b/include/servicefd.h
index 85526b0..648646d 100644
--- a/include/servicefd.h
+++ b/include/servicefd.h
@@ -27,6 +27,7 @@ extern int get_service_fd(enum sfd_type type);
 extern int reserve_service_fd(enum sfd_type type);
 extern int install_service_fd(enum sfd_type type, int fd);
 extern int close_service_fd(enum sfd_type type);
+extern void close_old_servie_fd(int nr);
 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 0b10f0d..18b0c95 100644
--- a/util.c
+++ b/util.c
@@ -334,6 +334,19 @@ int close_service_fd(enum sfd_type type)
 	return 0;
 }
 
+/* Close all unused service descriptors on a depth of nr. */
+void close_old_servie_fd(int nr)
+{
+	int level, i;
+	for (level = 0; level < nr; level++) {
+		if (level == service_fd_id)
+			continue;
+		for (i = SERVICE_FD_MIN + 1; i < SERVICE_FD_MAX; i++) {
+			close(__get_service_fd(i, level));
+		}
+	}
+}
+
 int clone_service_fd(int id)
 {
 	int ret = -1, i;
-- 
1.8.3.1



More information about the CRIU mailing list