[Devel] [PATCH 2/3] libvzctl-4.14: join namespaces in explicitly provided order

Stanislav Kinsburskiy skinsbursky at virtuozzo.com
Wed Jan 10 19:15:30 MSK 2018


This is needed to make sure, that mnt ns is the last (otherwise join other
namespaces after mnt ns will fail).

Signed-off-by: Stanislav Kinsburskiy <skinsbursky at virtuozzo.com>
---
 lib/env_nsops.c |   39 +++++++++++----------------------------
 1 file changed, 11 insertions(+), 28 deletions(-)

diff --git a/lib/env_nsops.c b/lib/env_nsops.c
index 0771eb2..d885d1c 100644
--- a/lib/env_nsops.c
+++ b/lib/env_nsops.c
@@ -885,7 +885,7 @@ static int ns_is_env_run(struct vzctl_env_handle *h)
 	return cg_env_get_ve_state(EID(h));
 }
 
-int set_ns(pid_t pid, const char *name, int flags)
+static int set_ns(pid_t pid, const char *name, int flags)
 {
 	int ret, fd;
 	char path[PATH_MAX];
@@ -924,11 +924,10 @@ int enter_net_ns(struct vzctl_env_handle *h, pid_t *ct_pid)
 
 static int ns_env_enter(struct vzctl_env_handle *h, int flags)
 {
-	DIR *dp;
-	struct dirent *ep;
 	pid_t pid;
-	char path[PATH_MAX];
-	int ret;
+	int ret, i;
+	const char *ns[] = {"cgroup", "ipc", "net", "uts", "pid",
+			    "pid_for_children", "user", "mnt"};
 
 	ret = reset_loginuid();
 	if (ret)
@@ -939,37 +938,21 @@ static int ns_env_enter(struct vzctl_env_handle *h, int flags)
 
 	logger(10, 0, "* Attach by pid %d", pid);
 
-	snprintf(path, sizeof(path), "/proc/%d/ns", pid);
-	dp = opendir(path);
-	if (dp == NULL)
-		return vzctl_err(-1, errno, "Unable to open dir %s", path);
-
 	ret = cg_attach_task(EID(h), getpid(), NULL, NULL);
 	if (ret)
-		goto err;
-
-	while ((ep = readdir (dp))) {
-		if (!strcmp(ep->d_name, ".") ||
-		    !strcmp(ep->d_name, ".."))
-			continue;
+		return ret;
 
-		ret = set_ns(pid, ep->d_name, 0);
+	for (i = 0; i < sizeof(ns) / sizeof(ns[0]); ++i) {
+		ret = set_ns(pid, ns[i], 0);
 		if (ret)
-			goto err;
+			return ret;
 	}
 
 	/* Clear supplementary group IDs */
-	if (setgroups(0, NULL)) {
-		ret = vzctl_err(-1, errno, "ns_env_enter: setgroups()");
-		goto err;
-	}
-	
-	ret = set_personality32();
+	if (setgroups(0, NULL))
+		return vzctl_err(-1, errno, "ns_env_enter: setgroups()");
 
-err:
-	closedir(dp);
-
-	return ret;
+	return set_personality32();
 }
 
 static int ns_env_exec(struct vzctl_env_handle *h, struct exec_param *param,



More information about the Devel mailing list