[lxc-devel] [PATCH] lxc_global_config_value can return the default lxc.cgroup.pattern whether root or non-root

KATOH Yasufumi karma at jazz.email.ne.jp
Thu Oct 2 09:01:06 UTC 2014


>>> On Tue, 30 Sep 2014 19:48:09 +0000
    in message   "Re: [lxc-devel] [PATCH] lxc-config can show lxc.cgroup.(use|pattern)"
                  Serge Hallyn-san wrote:

> I think it would be worth also augmenting
> lxc_global_config_value() to return a default lxc.cgroup.use
> for 'all', and a default lxc.cgroup.pattern ("/lxc/%n" for root
> or "%n" for non-root).

lxc.cgroup.pattern is like this? (^_^;)

Signed-off-by: KATOH Yasufumi <karma at jazz.email.ne.jp>
---
 src/lxc/cgfs.c      | 10 +---------
 src/lxc/cgmanager.c | 11 ++---------
 src/lxc/utils.c     | 15 ++++++++++++++-
 3 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/src/lxc/cgfs.c b/src/lxc/cgfs.c
index 0f181c6..362043e 100644
--- a/src/lxc/cgfs.c
+++ b/src/lxc/cgfs.c
@@ -2226,15 +2226,7 @@ static void *cgfs_init(const char *name)
 	if (!d->name)
 		goto err1;
 
-	/* if we are running as root, use system cgroup pattern, otherwise
-	 * just create a cgroup under the current one. But also fall back to
-	 * that if for some reason reading the configuration fails and no
-	 * default value is available
-	 */
-	if (geteuid() == 0)
-		d->cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern");
-	if (!d->cgroup_pattern)
-		d->cgroup_pattern = "%n";
+	d->cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern");
 
 	d->meta = lxc_cgroup_load_meta();
 	if (!d->meta) {
diff --git a/src/lxc/cgmanager.c b/src/lxc/cgmanager.c
index 4038c41..11c48ab 100644
--- a/src/lxc/cgmanager.c
+++ b/src/lxc/cgmanager.c
@@ -478,15 +478,8 @@ static void *cgm_init(const char *name)
 		goto err1;
 	}
 
-	/* if we are running as root, use system cgroup pattern, otherwise
-	 * just create a cgroup under the current one. But also fall back to
-	 * that if for some reason reading the configuration fails and no
-	 * default value is available
-	 */
-	if (geteuid() == 0)
-		d->cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern");
-	if (!d->cgroup_pattern)
-		d->cgroup_pattern = "%n";
+	d->cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern");
+
 	// cgm_create immediately gets called so keep the connection open
 	return d;
 
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index ed34706..0bdb184 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -265,7 +265,7 @@ const char *lxc_global_config_value(const char *option_name)
 		{ "lxc.bdev.zfs.root",      DEFAULT_ZFSROOT },
 		{ "lxc.lxcpath",            NULL            },
 		{ "lxc.default_config",     NULL            },
-		{ "lxc.cgroup.pattern",     DEFAULT_CGROUP_PATTERN },
+		{ "lxc.cgroup.pattern",     NULL            },
 		{ "lxc.cgroup.use",         NULL            },
 		{ NULL, NULL },
 	};
@@ -279,6 +279,7 @@ const char *lxc_global_config_value(const char *option_name)
 	char *user_config_path = NULL;
 	char *user_default_config_path = NULL;
 	char *user_lxc_path = NULL;
+	char *user_cgroup_pattern = NULL;
 
 	if (geteuid() > 0) {
 		const char *user_home = getenv("HOME");
@@ -292,11 +293,13 @@ const char *lxc_global_config_value(const char *option_name)
 		sprintf(user_config_path, "%s/.config/lxc/lxc.conf", user_home);
 		sprintf(user_default_config_path, "%s/.config/lxc/default.conf", user_home);
 		sprintf(user_lxc_path, "%s/.local/share/lxc/", user_home);
+		user_cgroup_pattern = strdup("%n");
 	}
 	else {
 		user_config_path = strdup(LXC_GLOBAL_CONF);
 		user_default_config_path = strdup(LXC_DEFAULT_CONFIG);
 		user_lxc_path = strdup(LXCPATH);
+		user_cgroup_pattern = strdup(DEFAULT_CGROUP_PATTERN);
 	}
 
 	const char * const (*ptr)[2];
@@ -312,6 +315,7 @@ const char *lxc_global_config_value(const char *option_name)
 		free(user_config_path);
 		free(user_default_config_path);
 		free(user_lxc_path);
+		free(user_cgroup_pattern);
 		errno = EINVAL;
 		return NULL;
 	}
@@ -320,6 +324,7 @@ const char *lxc_global_config_value(const char *option_name)
 		free(user_config_path);
 		free(user_default_config_path);
 		free(user_lxc_path);
+		free(user_cgroup_pattern);
 		return values[i];
 	}
 
@@ -378,14 +383,22 @@ const char *lxc_global_config_value(const char *option_name)
 		remove_trailing_slashes(user_lxc_path);
 		values[i] = user_lxc_path;
 		free(user_default_config_path);
+		free(user_cgroup_pattern);
 	}
 	else if (strcmp(option_name, "lxc.default_config") == 0) {
 		values[i] = user_default_config_path;
 		free(user_lxc_path);
+		free(user_cgroup_pattern);
+	}
+	else if (strcmp(option_name, "lxc.cgroup.pattern") == 0) {
+		values[i] = user_cgroup_pattern;
+		free(user_default_config_path);
+		free(user_lxc_path);
 	}
 	else {
 		free(user_default_config_path);
 		free(user_lxc_path);
+		free(user_cgroup_pattern);
 		values[i] = (*ptr)[1];
 	}
 	/* special case: if default value is NULL,
-- 
2.0.4



More information about the lxc-devel mailing list