cgfsng: respect lxc.cgroup.use

If lxc.cgroup.use is specified then only those controllers listed in there will be used others will be skipped. Closes #2447. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 15dcdac4
...@@ -695,8 +695,7 @@ static bool controller_found(struct hierarchy **hlist, char *entry) ...@@ -695,8 +695,7 @@ static bool controller_found(struct hierarchy **hlist, char *entry)
*/ */
static bool all_controllers_found(struct cgroup_ops *ops) static bool all_controllers_found(struct cgroup_ops *ops)
{ {
char *p; char **cur;
char *saveptr = NULL;
struct hierarchy **hlist = ops->hierarchies; struct hierarchy **hlist = ops->hierarchies;
if (!controller_found(hlist, "freezer")) { if (!controller_found(hlist, "freezer")) {
...@@ -707,9 +706,9 @@ static bool all_controllers_found(struct cgroup_ops *ops) ...@@ -707,9 +706,9 @@ static bool all_controllers_found(struct cgroup_ops *ops)
if (!ops->cgroup_use) if (!ops->cgroup_use)
return true; return true;
for (; (p = strtok_r(ops->cgroup_use, ",", &saveptr)); ops->cgroup_use = NULL) for (cur = ops->cgroup_use; cur && *cur; cur++)
if (!controller_found(hlist, p)) { if (!controller_found(hlist, *cur)) {
ERROR("No %s controller mountpoint found", p); ERROR("No %s controller mountpoint found", *cur);
return false; return false;
} }
...@@ -2251,6 +2250,34 @@ static bool cgfsng_setup_limits(struct cgroup_ops *ops, struct lxc_conf *conf, ...@@ -2251,6 +2250,34 @@ static bool cgfsng_setup_limits(struct cgroup_ops *ops, struct lxc_conf *conf,
return __cg_unified_setup_limits(ops, &conf->cgroup2); return __cg_unified_setup_limits(ops, &conf->cgroup2);
} }
static bool cgroup_use_wants_controllers(const struct cgroup_ops *ops,
char **controllers)
{
char **cur_ctrl, **cur_use;
if (!ops->cgroup_use)
return true;
for (cur_ctrl = controllers; cur_ctrl && *cur_ctrl; cur_ctrl++) {
bool found = false;
for (cur_use = ops->cgroup_use; cur_use && *cur_use; cur_use++) {
if (strcmp(*cur_use, *cur_ctrl) != 0)
continue;
found = true;
break;
}
if (found)
continue;
return false;
}
return true;
}
/* At startup, parse_hierarchies finds all the info we need about cgroup /* At startup, parse_hierarchies finds all the info we need about cgroup
* mountpoints and current cgroups, and stores it in @d. * mountpoints and current cgroups, and stores it in @d.
*/ */
...@@ -2366,6 +2393,10 @@ static bool cg_hybrid_init(struct cgroup_ops *ops) ...@@ -2366,6 +2393,10 @@ static bool cg_hybrid_init(struct cgroup_ops *ops)
} }
} }
/* Exclude all controllers that cgroup use does not want. */
if (!cgroup_use_wants_controllers(ops, controller_list))
goto next;
new = add_hierarchy(&ops->hierarchies, controller_list, mountpoint, base_cgroup, type); new = add_hierarchy(&ops->hierarchies, controller_list, mountpoint, base_cgroup, type);
if (type == CGROUP2_SUPER_MAGIC && !ops->unified) if (type == CGROUP2_SUPER_MAGIC && !ops->unified)
ops->unified = new; ops->unified = new;
...@@ -2498,8 +2529,18 @@ static bool cg_init(struct cgroup_ops *ops) ...@@ -2498,8 +2529,18 @@ static bool cg_init(struct cgroup_ops *ops)
const char *tmp; const char *tmp;
tmp = lxc_global_config_value("lxc.cgroup.use"); tmp = lxc_global_config_value("lxc.cgroup.use");
if (tmp) if (tmp) {
ops->cgroup_use = must_copy_string(tmp); char *chop, *cur, *pin;
char *saveptr = NULL;
pin = must_copy_string(tmp);
chop = pin;
for (; (cur = strtok_r(chop, ",", &saveptr)); chop = NULL)
must_append_string(&ops->cgroup_use, cur);
free(pin);
}
ret = cg_unified_init(ops); ret = cg_unified_init(ops);
if (ret < 0) if (ret < 0)
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -63,12 +64,15 @@ struct cgroup_ops *cgroup_init(struct lxc_handler *handler) ...@@ -63,12 +64,15 @@ struct cgroup_ops *cgroup_init(struct lxc_handler *handler)
void cgroup_exit(struct cgroup_ops *ops) void cgroup_exit(struct cgroup_ops *ops)
{ {
char **cur;
struct hierarchy **it; struct hierarchy **it;
if (!ops) if (!ops)
return; return;
free(ops->cgroup_use); for (cur = ops->cgroup_use; cur && *cur; cur++)
free(*cur);
free(ops->cgroup_pattern); free(ops->cgroup_pattern);
free(ops->container_cgroup); free(ops->container_cgroup);
......
...@@ -89,7 +89,7 @@ struct cgroup_ops { ...@@ -89,7 +89,7 @@ struct cgroup_ops {
const char *version; const char *version;
/* What controllers is the container supposed to use. */ /* What controllers is the container supposed to use. */
char *cgroup_use; char **cgroup_use;
char *cgroup_pattern; char *cgroup_pattern;
char *container_cgroup; char *container_cgroup;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment