[Devel] [PATCH 1/6] vzctl: split ct_env_create (v2)

Andrey Vagin avagin at openvz.org
Tue May 21 09:13:28 PDT 2013


ct_env_create will able to execute CT and to resume CT

v2: A return code contains one of known codes with inverted sign.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 src/lib/hooks_ct.c | 79 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 45 insertions(+), 34 deletions(-)

diff --git a/src/lib/hooks_ct.c b/src/lib/hooks_ct.c
index 7bc9814..837ec18 100644
--- a/src/lib/hooks_ct.c
+++ b/src/lib/hooks_ct.c
@@ -376,52 +376,26 @@ static int _env_create(void *data)
 	return exec_container_init(arg, &create_param);
 }
 
-static int ct_env_create(struct arg_start *arg)
+static int ct_env_create_real(struct arg_start *arg)
 {
 
 	long stack_size;
 	char *child_stack;
 	int clone_flags;
-	int ret;
-	char procpath[STR_SIZE];
-	char ctpath[STR_SIZE];
 	int userns_p[2];
-	int err;
+	int ret, err;
 
 	stack_size = get_pagesize();
 	if (stack_size < 0)
-		return VZ_RESOURCE_ERROR;
+		return -VZ_RESOURCE_ERROR;
 
 	child_stack = alloca(stack_size);
 	if (child_stack == NULL) {
 		logger(-1, 0, "Unable to alloc");
-		return VZ_RESOURCE_ERROR;
+		return -VZ_RESOURCE_ERROR;
 	}
 	child_stack += stack_size;
 
-	/* non-fatal */
-	if ((ret = ct_destroy(arg->h, arg->veid)))
-		logger(0, 0, "Could not properly cleanup container: %s",
-			container_error(ret));
-
-	snprintf(ctpath, STR_SIZE, "%s/%d", NETNS_RUN_DIR, arg->veid);
-	unlink(ctpath);
-
-	if ((ret = create_container(arg->veid))) {
-		logger(-1, 0, "Container creation failed: %s", container_error(ret));
-		return VZ_RESOURCE_ERROR;
-	}
-
-	if ((ret = ct_setlimits(arg->h, arg->veid, &arg->res->ub))) {
-		logger(-1, 0, "Could not apply container limits: %s", container_error(ret));
-		return VZ_RESOURCE_ERROR;
-	}
-
-	if ((ret = container_add_task(arg->veid))) {
-		logger(-1, 0, "Can't add task creator to container: %s", container_error(ret));
-		return VZ_RESOURCE_ERROR;
-	}
-
 	/*
 	 * Belong in the setup phase
 	 */
@@ -437,7 +411,7 @@ static int ct_env_create(struct arg_start *arg)
 		clone_flags |= CLONE_NEWUSER;
 		if (pipe(userns_p) < 0) {
 			logger(-1, errno, "Can not create userns pipe");
-			return VZ_RESOURCE_ERROR;
+			return -VZ_RESOURCE_ERROR;
 		}
 	}
 	arg->userns_p = userns_p[0];
@@ -449,7 +423,7 @@ static int ct_env_create(struct arg_start *arg)
 		/* FIXME: remove ourselves from container first */
 		close(userns_p[1]);
 		destroy_container(arg->veid);
-		return VZ_RESOURCE_ERROR;
+		return -VZ_RESOURCE_ERROR;
 	}
 
 	if (arg->h->can_join_userns) {
@@ -465,7 +439,7 @@ static int ct_env_create(struct arg_start *arg)
 			logger(-1, 0, "Can't write to userns mapping file");
 			close(userns_p[1]);
 			destroy_container(arg->veid);
-			return VZ_RESOURCE_ERROR;
+			return -VZ_RESOURCE_ERROR;
 		}
 		/*
 		 * Nothing should proceed userns wide until we have the
@@ -480,11 +454,48 @@ static int ct_env_create(struct arg_start *arg)
 			logger(-1, errno, "Unable to write to userns pipe");
 			close(userns_p[1]);
 			destroy_container(arg->veid);
-			return VZ_RESOURCE_ERROR;
+			return -VZ_RESOURCE_ERROR;
 		}
 		close(userns_p[1]);
 	}
 
+	return ret;
+}
+
+int ct_env_create(struct arg_start *arg)
+{
+	int ret;
+	char procpath[STR_SIZE];
+	char ctpath[STR_SIZE];
+
+	/* non-fatal */
+	if ((ret = ct_destroy(arg->h, arg->veid)))
+		logger(0, 0, "Could not properly cleanup container: %s",
+			container_error(ret));
+
+	snprintf(ctpath, STR_SIZE, "%s/%d", NETNS_RUN_DIR, arg->veid);
+	unlink(ctpath);
+
+	if ((ret = create_container(arg->veid))) {
+		logger(-1, 0, "Container creation failed: %s", container_error(ret));
+		return VZ_RESOURCE_ERROR;
+	}
+
+	if ((ret = ct_setlimits(arg->h, arg->veid, &arg->res->ub))) {
+		logger(-1, 0, "Could not apply container limits: %s", container_error(ret));
+		return VZ_RESOURCE_ERROR;
+	}
+
+	if ((ret = container_add_task(arg->veid))) {
+		logger(-1, 0, "Can't add task creator to container: %s", container_error(ret));
+		return VZ_RESOURCE_ERROR;
+	}
+
+	/* Return PID on success or -VZ_*_ERROR */
+	ret = ct_env_create_real(arg);
+	if (ret < 0)
+		return -ret;
+
 	snprintf(procpath, STR_SIZE, "/proc/%d/ns/net", ret);
 	ret = symlink(procpath, ctpath);
 	if (ret) {
-- 
1.8.2




More information about the Devel mailing list