chiark / gitweb /
Be slightly more verbose in error message
[elogind.git] / src / basic / cgroup-util.c
index 8cd064f0b53bea2f59d9e944f96b7e08dbf363d1..0f4c955a01491e9e3ac0054dcb6ff36c1362ca07 100644 (file)
@@ -845,7 +845,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)
         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;
         }
 
         return 0;
@@ -940,7 +940,7 @@ int cg_set_task_access(
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        r = cg_unified(controller);
+        r = cg_unified_controller(controller);
         if (r < 0)
                 return r;
         if (r == 0) {
         if (r < 0)
                 return r;
         if (r == 0) {
@@ -1016,7 +1016,7 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
         } else
                 controller = SYSTEMD_CGROUP_CONTROLLER;
 
         } else
                 controller = SYSTEMD_CGROUP_CONTROLLER;
 
-        unified = cg_unified(controller);
+        unified = cg_unified_controller(controller);
         if (unified < 0)
                 return unified;
         if (unified == 0) {
         if (unified < 0)
                 return unified;
         if (unified == 0) {
@@ -1094,7 +1094,7 @@ int cg_install_release_agent(const char *controller, const char *agent) {
 
         assert(agent);
 
 
         assert(agent);
 
-        r = cg_unified(controller);
+        r = cg_unified_controller(controller);
         if (r < 0)
                 return r;
         if (r > 0) /* doesn't apply to unified hierarchy */
         if (r < 0)
                 return r;
         if (r > 0) /* doesn't apply to unified hierarchy */
@@ -1145,7 +1145,7 @@ int cg_uninstall_release_agent(const char *controller) {
         _cleanup_free_ char *fs = NULL;
         int r;
 
         _cleanup_free_ char *fs = NULL;
         int r;
 
-        r = cg_unified(controller);
+        r = cg_unified_controller(controller);
         if (r < 0)
                 return r;
         if (r > 0) /* Doesn't apply to unified hierarchy */
         if (r < 0)
                 return r;
         if (r > 0) /* Doesn't apply to unified hierarchy */
@@ -1201,7 +1201,7 @@ int cg_is_empty_recursive(const char *controller, const char *path) {
         if (controller && (isempty(path) || path_equal(path, "/")))
                 return false;
 
         if (controller && (isempty(path) || path_equal(path, "/")))
                 return false;
 
-        r = cg_unified(controller);
+        r = cg_unified_controller(controller);
         if (r < 0)
                 return r;
         if (r > 0) {
         if (r < 0)
                 return r;
         if (r > 0) {
@@ -2265,6 +2265,60 @@ int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root)
 }
 #endif // 0
 
 }
 #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;
 int cg_mask_supported(CGroupMask *ret) {
         CGroupMask mask = 0;
         int r;
@@ -2278,7 +2332,6 @@ int cg_mask_supported(CGroupMask *ret) {
                 return r;
         if (r > 0) {
                 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
                 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
 
                 /* In the unified hierarchy we can read the supported
                  * and accessible controllers from a the top-level
@@ -2296,23 +2349,9 @@ int cg_mask_supported(CGroupMask *ret) {
                 if (r < 0)
                         return r;
 
                 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
 
                 /* Currently, we support the cpu, memory, io and pids
                  * controller in the unified hierarchy, mask
@@ -2398,21 +2437,18 @@ int cg_kernel_controllers(Set *controllers) {
 
 static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
 
 
 static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
 
-/* The hybrid mode was initially implemented in v232 and simply mounted
- * cgroup v2 on /sys/fs/cgroup/systemd.  This unfortunately broke other
- * tools (such as docker) which expected the v1 "name=systemd" hierarchy
- * on /sys/fs/cgroup/systemd.  From v233 and on, the hybrid mode mountnbs
- * v2 on /sys/fs/cgroup/unified and maintains "name=systemd" hierarchy
- * on /sys/fs/cgroup/systemd for compatibility with other tools.
+/* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd.  This
+ * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
+ * /sys/fs/cgroup/systemd.  From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
+ * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
  *
  *
- * To keep live upgrade working, we detect and support v232 layout.  When
- * v232 layout is detected, to keep cgroup v2 process management but
- * disable the compat dual layout, we return %true on
- * cg_unified(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
+ * To keep live upgrade working, we detect and support v232 layout.  When v232 layout is detected, to keep cgroup v2
+ * process management but disable the compat dual layout, we return %true on
+ * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
  */
 static thread_local bool unified_systemd_v232;
 
  */
 static thread_local bool unified_systemd_v232;
 
-static int cg_update_unified(void) {
+static int cg_unified_update(void) {
 
         struct statfs fs;
 
 
         struct statfs fs;
 
@@ -2460,10 +2496,10 @@ static int cg_update_unified(void) {
         return 0;
 }
 
         return 0;
 }
 
-int cg_unified(const char *controller) {
+int cg_unified_controller(const char *controller) {
         int r;
 
         int r;
 
-        r = cg_update_unified();
+        r = cg_unified_update();
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
@@ -2477,14 +2513,19 @@ int cg_unified(const char *controller) {
 }
 
 int cg_all_unified(void) {
 }
 
 int cg_all_unified(void) {
-        return cg_unified(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;
 
 int cg_hybrid_unified(void) {
         int r;
 
-        r = cg_update_unified();
+        r = cg_unified_update();
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
@@ -2494,9 +2535,10 @@ int cg_hybrid_unified(void) {
 int cg_unified_flush(void) {
         unified_cache = CGROUP_UNIFIED_UNKNOWN;
 
 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;
 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
         _cleanup_free_ char *fs = NULL;
         CGroupController c;
@@ -2602,18 +2644,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);
 
          * 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
 #else
+bool cg_is_unified_wanted(void) {
+        return false;
+}
 bool cg_is_legacy_wanted(void) {
         return true;
 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
 
 }
 #endif // 0