]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup-util: refactor cg_{ns,freezer,kill}_supported
authorMike Yuan <me@yhndnzj.com>
Tue, 30 Jul 2024 19:18:47 +0000 (21:18 +0200)
committerMike Yuan <me@yhndnzj.com>
Fri, 2 Aug 2024 14:36:08 +0000 (16:36 +0200)
src/basic/cgroup-util.c

index 5a2636841e9e0ea2d3101621767c10a9e09280c0..de618545d76d810d9dae8a70f23e3875852aada8 100644 (file)
@@ -208,19 +208,16 @@ int cg_read_event(
 }
 
 bool cg_ns_supported(void) {
-        static thread_local int enabled = -1;
-
-        if (enabled >= 0)
-                return enabled;
+        static thread_local int supported = -1;
 
-        if (access("/proc/self/ns/cgroup", F_OK) < 0) {
-                if (errno != ENOENT)
-                        log_debug_errno(errno, "Failed to check whether /proc/self/ns/cgroup is available, assuming not: %m");
-                enabled = false;
-        } else
-                enabled = true;
+        if (supported >= 0)
+                return supported;
 
-        return enabled;
+        if (access("/proc/self/ns/cgroup", F_OK) >= 0)
+                return (supported = true);
+        if (errno != ENOENT)
+                log_debug_errno(errno, "Failed to check whether /proc/self/ns/cgroup is available, assuming not: %m");
+        return (supported = false);
 }
 
 bool cg_freezer_supported(void) {
@@ -229,9 +226,14 @@ bool cg_freezer_supported(void) {
         if (supported >= 0)
                 return supported;
 
-        supported = cg_all_unified() > 0 && access("/sys/fs/cgroup/init.scope/cgroup.freeze", F_OK) == 0;
+        if (cg_all_unified() <= 0)
+                return (supported = false);
 
-        return supported;
+        if (access("/sys/fs/cgroup/init.scope/cgroup.freeze", F_OK) >= 0)
+                return (supported = true);
+        if (errno != ENOENT)
+                log_debug_errno(errno, "Failed to check whether cgroup freezer is available, assuming not: %m");
+        return (supported = false);
 }
 
 bool cg_kill_supported(void) {
@@ -241,15 +243,13 @@ bool cg_kill_supported(void) {
                 return supported;
 
         if (cg_all_unified() <= 0)
-                supported = false;
-        else if (access("/sys/fs/cgroup/init.scope/cgroup.kill", F_OK) < 0) {
-                if (errno != ENOENT)
-                        log_debug_errno(errno, "Failed to check if cgroup.kill is available, assuming not: %m");
-                supported = false;
-        } else
-                supported = true;
+                return (supported = false);
 
-        return supported;
+        if (access("/sys/fs/cgroup/init.scope/cgroup.kill", F_OK) >= 0)
+                return (supported = true);
+        if (errno != ENOENT)
+                log_debug_errno(errno, "Failed to check whether cgroup.kill is available, assuming not: %m");
+        return (supported = false);
 }
 
 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **ret) {