[CRIU] [PATCH 2/2] restore: Don't close pstree_fd several times

Cyrill Gorcunov gorcunov at openvz.org
Mon Mar 5 11:44:22 EST 2012


There is a scenario when pstree_fd may be tried
to close several times -- if we start crtools
with "detach" option.

So simply make restore_root_task to close opened
file descriptor, this also simplifies the code.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 cr-restore.c |   20 +++++++++-----------
 1 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/cr-restore.c b/cr-restore.c
index cbae24a..8391403 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -1387,20 +1387,20 @@ static int restore_task_with_children(void *_arg)
 	return restore_one_task(me->pid);
 }
 
-static int restore_root_task(int fd, struct cr_options *opts)
+static int restore_root_task(int *fd, struct cr_options *opts)
 {
 	struct pstree_entry e;
 	int ret, i;
 	struct sigaction act, old_act;
 
-	ret = read(fd, &e, sizeof(e));
+	ret = read(*fd, &e, sizeof(e));
+	close_safe(fd);
+
 	if (ret != sizeof(e)) {
 		pr_perror("Can't read root pstree entry");
 		return -1;
 	}
 
-	close(fd);
-
 	ret = sigaction(SIGCHLD, NULL, &act);
 	if (ret < 0) {
 		perror("sigaction() failed\n");
@@ -1457,19 +1457,17 @@ static int restore_all_tasks(pid_t pid, struct cr_options *opts)
 {
 	int pstree_fd = -1;
 	u32 type = 0;
-	int ret = -1;
 
 	pstree_fd = open_image_ro(CR_FD_PSTREE, pstree_pid);
 	if (pstree_fd < 0)
 		return -1;
 
-	if (prepare_shared(pstree_fd))
-		goto out;
+	if (prepare_shared(pstree_fd)) {
+		close_safe(&pstree_fd);
+		return -1;
+	}
 
-	ret = restore_root_task(pstree_fd, opts);
-out:
-	close_safe(&pstree_fd);
-	return ret;
+	return restore_root_task(&pstree_fd, opts);
 }
 
 static long restorer_get_vma_hint(pid_t pid, struct list_head *self_vma_list, long vma_len)
-- 
1.7.7.6



More information about the CRIU mailing list