chiark / gitweb /
Prep v234: Apply missing upstream fixes in src/basic (1/6)
[elogind.git] / src / basic / cgroup-util.c
index a0947b8c5851e20508e07ceecdc8a05d14ba0e22..315c449656aa073ce6aadad84e0506b82e246a6f 100644 (file)
@@ -55,6 +55,7 @@
 #include "stdio-util.h"
 #include "string-table.h"
 #include "string-util.h"
+#include "strv.h"
 #include "unit-name.h"
 #include "user-util.h"
 
@@ -845,7 +846,7 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
         if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
                 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, pid);
                 if (r < 0)
-                        log_warning_errno(r, "Failed to attach %d to compat systemd cgroup %s: %m", pid, path);
+                        log_warning_errno(r, "Failed to attach "PID_FMT" to compat systemd cgroup %s: %m", pid, path);
         }
 
         return 0;
@@ -1003,7 +1004,7 @@ int cg_get_xattr(const char *controller, const char *path, const char *name, voi
 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
         _cleanup_fclose_ FILE *f = NULL;
         char line[LINE_MAX];
-        const char *fs, *controller_str;
+        const char *fs, *controller_str = NULL;
         size_t cs = 0;
         int unified;
 
@@ -2265,6 +2266,60 @@ int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root)
 }
 #endif // 0
 
+int cg_mask_to_string(CGroupMask mask, char **ret) {
+        const char *controllers[_CGROUP_CONTROLLER_MAX + 1];
+        CGroupController c;
+        int i = 0;
+        char *s;
+
+        assert(ret);
+
+        if (mask == 0) {
+                *ret = NULL;
+                return 0;
+        }
+
+        for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+
+                if (!(mask & CGROUP_CONTROLLER_TO_MASK(c)))
+                        continue;
+
+                controllers[i++] = cgroup_controller_to_string(c);
+                controllers[i] = NULL;
+        }
+
+        s = strv_join((char **)controllers, NULL);
+        if (!s)
+                return -ENOMEM;
+
+        *ret = s;
+        return 0;
+}
+
+int cg_mask_from_string(const char *value, CGroupMask *mask) {
+        assert(mask);
+        assert(value);
+
+        for (;;) {
+                _cleanup_free_ char *n = NULL;
+                CGroupController v;
+                int r;
+
+                r = extract_first_word(&value, &n, NULL, 0);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
+
+                v = cgroup_controller_from_string(n);
+                if (v < 0)
+                        continue;
+
+                *mask |= CGROUP_CONTROLLER_TO_MASK(v);
+        }
+        return 0;
+}
+
 int cg_mask_supported(CGroupMask *ret) {
         CGroupMask mask = 0;
         int r;
@@ -2278,7 +2333,6 @@ int cg_mask_supported(CGroupMask *ret) {
                 return r;
         if (r > 0) {
                 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
-                const char *c;
 
                 /* In the unified hierarchy we can read the supported
                  * and accessible controllers from a the top-level
@@ -2296,23 +2350,9 @@ int cg_mask_supported(CGroupMask *ret) {
                 if (r < 0)
                         return r;
 
-                c = controllers;
-                for (;;) {
-                        _cleanup_free_ char *n = NULL;
-                        CGroupController v;
-
-                        r = extract_first_word(&c, &n, NULL, 0);
-                        if (r < 0)
-                                return r;
-                        if (r == 0)
-                                break;
-
-                        v = cgroup_controller_from_string(n);
-                        if (v < 0)
-                                continue;
-
-                        mask |= CGROUP_CONTROLLER_TO_MASK(v);
-                }
+                r = cg_mask_from_string(controllers, &mask);
+                if (r < 0)
+                        return r;
 
                 /* Currently, we support the cpu, memory, io and pids
                  * controller in the unified hierarchy, mask
@@ -2409,7 +2449,7 @@ static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
  */
 static thread_local bool unified_systemd_v232;
 
-static int cg_update_unified(void) {
+static int cg_unified_update(void) {
 
         struct statfs fs;
 
@@ -2460,7 +2500,7 @@ static int cg_update_unified(void) {
 int cg_unified_controller(const char *controller) {
         int r;
 
-        r = cg_update_unified();
+        r = cg_unified_update();
         if (r < 0)
                 return r;
 
@@ -2474,14 +2514,19 @@ int cg_unified_controller(const char *controller) {
 }
 
 int cg_all_unified(void) {
-        return cg_unified_controller(NULL);
+        int r;
+
+        r = cg_unified_update();
+        if (r < 0)
+                return r;
+
+        return unified_cache >= CGROUP_UNIFIED_ALL;
 }
 
-#if 0 /// UNNEEDED by elogind
 int cg_hybrid_unified(void) {
         int r;
 
-        r = cg_update_unified();
+        r = cg_unified_update();
         if (r < 0)
                 return r;
 
@@ -2491,9 +2536,10 @@ int cg_hybrid_unified(void) {
 int cg_unified_flush(void) {
         unified_cache = CGROUP_UNIFIED_UNKNOWN;
 
-        return cg_update_unified();
+        return cg_unified_update();
 }
 
+#if 0 /// UNNEEDED by elogind
 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
         _cleanup_free_ char *fs = NULL;
         CGroupController c;
@@ -2599,18 +2645,19 @@ bool cg_is_hybrid_wanted(void) {
          * Since checking is expensive, cache a non-error result. */
         r = proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b);
 
+        /* The meaning of the kernel option is reversed wrt. to the return value
+         * of this function, hence the negation. */
+        return (wanted = r > 0 ? !b : is_default);
+}
 #else
+bool cg_is_unified_wanted(void) {
+        return false;
+}
 bool cg_is_legacy_wanted(void) {
         return true;
-        /* The meaning of the kernel option is reversed wrt. to the return value
-         * of this function, hence the negation. */
-        return (wanted = r > 0 ? !b : false);
-        return (wanted = r > 0 ? b : false);
 }
-
-bool cg_is_legacy_systemd_controller_wanted(void) {
-        return cg_is_legacy_wanted() && !cg_is_unified_systemd_controller_wanted();
-        return (wanted = r > 0 ? !b : is_default);
+bool cg_is_hybrid_wanted(void) {
+        return false;
 }
 #endif // 0