]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
cgroups: introduce cgroup hierarchy type
authorChristian Brauner <christian.brauner@ubuntu.com>
Sun, 21 Feb 2021 00:28:45 +0000 (01:28 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 21 Feb 2021 00:28:45 +0000 (01:28 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/cgroups/cgfsng.c
src/lxc/cgroups/cgroup.h

index 8bb9272519fab0e2f5d3a8f5de7677d9ecf16730..96072f79d22eb3232a5b50b9ffdda35c2fd5a52a 100644 (file)
@@ -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);
        }
 
index 1d08bdb55a0ae2ab5ff8f0e2ddca8e3e0ce7d5c7..daca3f60ff4c3672d3b7230548cc0653c50344e7 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <sys/types.h>
+#include <linux/magic.h>
 
 #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;