From: Christian Brauner Date: Sun, 21 Feb 2021 00:28:45 +0000 (+0100) Subject: cgroups: introduce cgroup hierarchy type X-Git-Tag: lxc-5.0.0~274^2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8572e8c2f5eddbf47cf3de441bfe8deb1c033a1;p=thirdparty%2Flxc.git cgroups: introduce cgroup hierarchy type Signed-off-by: Christian Brauner --- diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 8bb927251..96072f79d 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -269,7 +269,7 @@ static ssize_t get_max_cpus(char *cpulist) static inline bool is_unified_hierarchy(const struct hierarchy *h) { - return h->version == CGROUP2_SUPER_MAGIC; + return h->fs_type == UNIFIED_HIERARCHY; } /* Return true if the controller @entry is found in the null-terminated list of @@ -389,7 +389,7 @@ static bool skip_hierarchy(const struct cgroup_ops *ops, char **controllers) static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt, int dfd_base, char *base_cgroup, - char **controllers, int type) + char **controllers, cgroupfs_type_magic_t fs_type) { __do_free struct hierarchy *new = NULL; int idx; @@ -405,7 +405,7 @@ static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt, new->cgfd_limit = -EBADF; new->cgfd_mon = -EBADF; - new->version = type; + new->fs_type = fs_type; new->controllers = controllers; new->mountpoint = mnt; new->container_base_path = base_cgroup; @@ -422,7 +422,7 @@ static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt, if (idx < 0) return ret_errno(idx); - if (type == CGROUP2_SUPER_MAGIC) + if (fs_type == UNIFIED_HIERARCHY) ops->unified = new; (ops->hierarchies)[idx] = move_ptr(new); @@ -1318,12 +1318,12 @@ static int chown_cgroup_wrapper(void *data) * files (which systemd in wily insists on doing). */ - if (arg->hierarchies[i]->version == CGROUP_SUPER_MAGIC) + if (arg->hierarchies[i]->fs_type == LEGACY_HIERARCHY) (void)fchowmodat(dirfd, "tasks", destuid, nsgid, 0664); (void)fchowmodat(dirfd, "cgroup.procs", destuid, nsgid, 0664); - if (arg->hierarchies[i]->version != CGROUP2_SUPER_MAGIC) + if (arg->hierarchies[i]->fs_type != UNIFIED_HIERARCHY) continue; for (char **p = arg->hierarchies[i]->cgroup2_chown; p && *p; p++) @@ -2323,7 +2323,7 @@ __cgfsng_ops static bool cgfsng_attach(struct cgroup_ops *ops, __do_free char *fullpath = NULL, *path = NULL; struct hierarchy *h = ops->hierarchies[i]; - if (h->version == CGROUP2_SUPER_MAGIC) { + if (h->fs_type == UNIFIED_HIERARCHY) { ret = __cg_unified_attach(h, conf, name, lxcpath, pid, h->controllers[0]); if (ret < 0) @@ -3078,6 +3078,8 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative, if (unified_cgroup(line)) { char *unified_mnt; + type = UNIFIED_HIERARCHY; + current_cgroup = current_unified_cgroup(relative, line); if (IS_ERR(current_cgroup)) return PTR_ERR(current_cgroup); @@ -3121,13 +3123,14 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative, return syserrno(-ENOMEM, "Failed to create empty controller list"); } - type = CGROUP2_SUPER_MAGIC; controllers = strdup(unified_mnt); if (!controllers) return ret_errno(ENOMEM); } else { char *__controllers, *__current_cgroup; + type = LEGACY_HIERARCHY; + __controllers = strchr(line, ':'); if (!__controllers) return ret_errno(EINVAL); @@ -3195,7 +3198,6 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative, if (skip_hierarchy(ops, controller_list)) continue; - type = CGROUP_SUPER_MAGIC; ops->cgroup_layout = CGROUP_LAYOUT_LEGACY; } @@ -3210,7 +3212,7 @@ static int __initialize_cgroups(struct cgroup_ops *ops, bool relative, move_ptr(current_cgroup); move_ptr(controllers); move_ptr(controller_list); - if (type == CGROUP2_SUPER_MAGIC) + if (type == UNIFIED_HIERARCHY) ops->unified->cgroup2_chown = move_ptr(delegate); } diff --git a/src/lxc/cgroups/cgroup.h b/src/lxc/cgroups/cgroup.h index 1d08bdb55..daca3f60f 100644 --- a/src/lxc/cgroups/cgroup.h +++ b/src/lxc/cgroups/cgroup.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "compiler.h" #include "macro.h" @@ -32,6 +33,11 @@ typedef enum { CGROUP_LAYOUT_UNIFIED = 2, } cgroup_layout_t; +typedef enum { + LEGACY_HIERARCHY = CGROUP_SUPER_MAGIC, + UNIFIED_HIERARCHY = CGROUP2_SUPER_MAGIC, +} cgroupfs_type_magic_t; + /* A descriptor for a mounted hierarchy * * @controllers @@ -81,7 +87,7 @@ struct hierarchy { char *container_base_path; char *container_full_path; char *container_limit_path; - int version; + cgroupfs_type_magic_t fs_type; /* cgroup2 only */ unsigned int bpf_device_controller:1;