]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup: Simplify cg_get_path_and_check
authorMichal Koutný <mkoutny@suse.com>
Tue, 26 Jan 2021 16:07:00 +0000 (17:07 +0100)
committerMichal Koutný <mkoutny@suse.com>
Thu, 11 Feb 2021 10:51:59 +0000 (11:51 +0100)
The function controller_is_accessible() doesn't do really much in case
of the unified hierarchy. Move common parts into cg_get_path_and_check
and make controller check v1 specific. This is refactoring only.

src/basic/cgroup-util.c

index bb20a12294d5406ed963c291013d1185fa2e7485..fcab1775bd599cca954bb96e27971b2636141bd0 100644 (file)
@@ -527,41 +527,16 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch
         return 0;
 }
 
-static int controller_is_accessible(const char *controller) {
-        int r;
+static int controller_is_v1_accessible(const char *controller) {
+        const char *cc, *dn;
 
         assert(controller);
 
-        /* Checks whether a specific controller is accessible,
-         * i.e. its hierarchy mounted. In the unified hierarchy all
-         * controllers are considered accessible, except for the named
-         * hierarchies */
-
-        if (!cg_controller_is_valid(controller))
-                return -EINVAL;
-
-        r = cg_all_unified();
-        if (r < 0)
-                return r;
-        if (r > 0) {
-                /* We don't support named hierarchies if we are using
-                 * the unified hierarchy. */
-
-                if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
-                        return 0;
-
-                if (startswith(controller, "name="))
-                        return -EOPNOTSUPP;
-
-        } else {
-                const char *cc, *dn;
-
-                dn = controller_to_dirname(controller);
-                cc = strjoina("/sys/fs/cgroup/", dn);
+        dn = controller_to_dirname(controller);
+        cc = strjoina("/sys/fs/cgroup/", dn);
 
-                if (laccess(cc, F_OK) < 0)
-                        return -errno;
-        }
+        if (laccess(cc, F_OK) < 0)
+                return -errno;
 
         return 0;
 }
@@ -572,10 +547,23 @@ int cg_get_path_and_check(const char *controller, const char *path, const char *
         assert(controller);
         assert(fs);
 
-        /* Check if the specified controller is actually accessible */
-        r = controller_is_accessible(controller);
+        if (!cg_controller_is_valid(controller))
+                return -EINVAL;
+
+        r = cg_all_unified();
         if (r < 0)
                 return r;
+        if (r > 0) {
+                /* In the unified hierarchy all controllers are considered accessible,
+                 * except for the named hierarchies */
+                if (startswith(controller, "name="))
+                        return -EOPNOTSUPP;
+        } else {
+                /* Check if the specified controller is actually accessible */
+                r = controller_is_v1_accessible(controller);
+                if (r < 0)
+                        return r;
+        }
 
         return cg_get_path(controller, path, suffix, fs);
 }
@@ -1909,7 +1897,7 @@ int cg_mask_supported(CGroupMask *ret) {
                                 continue;
 
                         n = cgroup_controller_to_string(c);
-                        if (controller_is_accessible(n) >= 0)
+                        if (controller_is_v1_accessible(n) >= 0)
                                 mask |= bit;
                 }
         }