[CRIU] [PATCH v2 12/13] pid_ns: Do cleanups in do_create_pid_ns_helper()

Kirill Tkhai ktkhai at virtuozzo.com
Tue May 16 09:27:14 PDT 2017


Move switch_ns() down because __pstree_pid_by_virt()
does not need cleanup.
Add more goto labels and restore ns back in case of fail.

Also delete pr_err(), because the error is already printed
by request_set_next_pid().

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/namespaces.c |   25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/criu/namespaces.c b/criu/namespaces.c
index 75465e157..f9ab66be4 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -2621,7 +2621,7 @@ static int pid_ns_helper(struct ns_id *ns, int sk)
 
 static int do_create_pid_ns_helper(void *arg, int sk, pid_t unused_pid)
 {
-	int pid_ns_fd, i, transport_fd, saved_errno;
+	int pid_ns_fd, i, transport_fd, saved_errno, ret = -1;
 	struct pstree_item *ns_reaper;
 	struct ns_id *ns, *tmp;
 	struct pid *pid;
@@ -2630,15 +2630,15 @@ static int do_create_pid_ns_helper(void *arg, int sk, pid_t unused_pid)
 	ns_reaper = *(struct pstree_item **)arg;
 	ns = lookup_ns_by_id(ns_reaper->ids->pid_ns_id, &pid_ns_desc);
 
-	if (switch_ns(ns_reaper->pid->real, &pid_ns_desc, &pid_ns_fd) < 0)
-		goto err;
-
 	pid = __pstree_pid_by_virt(ns, ns->ns_pid);
 	if (!pid) {
 		pr_err("Can't find helper reserved pid\n");
-		goto err;
+		goto close_sk;
 	}
 
+	if (switch_ns(ns_reaper->pid->real, &pid_ns_desc, &pid_ns_fd) < 0)
+		goto close_sk;
+
 	lock_last_pid();
 
 	transport_fd = get_service_fd(TRANSPORT_FD_OFF);
@@ -2650,9 +2650,8 @@ static int do_create_pid_ns_helper(void *arg, int sk, pid_t unused_pid)
 	 */
 	for (i = pid->level - 2, tmp = ns->parent; i >= 0; i--, tmp = tmp->parent)
 		if (request_set_next_pid(tmp->id, pid->ns[i].virt, transport_fd)) {
-			pr_err("Can't set next pid using helper\n");
 			unlock_last_pid();
-			goto err;
+			goto restore_ns;
 		}
 	child = fork();
 	if (!child)
@@ -2662,18 +2661,18 @@ static int do_create_pid_ns_helper(void *arg, int sk, pid_t unused_pid)
 	if (child < 0) {
 		errno = saved_errno;
 		pr_perror("Can't fork");
-		goto err;
+		goto restore_ns;
 	}
-	close(sk);
 	futex_set_and_wake(&ns->pid.helper_created, 1);
 	pid->real = child;
 
+	ret = 0;
+restore_ns:
 	if (restore_ns(pid_ns_fd, &pid_ns_desc) < 0)
-		return -1;
-	return 0;
-err:
+		ret = -1;
+close_sk:
 	close_safe(&sk);
-	return -1;
+	return ret;
 }
 
 /*



More information about the CRIU mailing list