chiark / gitweb /
Prep v234: Eventually fix the cgroup stuff. elogind is not init.
[elogind.git] / src / basic / cgroup-util.c
index b5b669c0e3d7ed51cf690208c3ae9b6f811aa2e6..323c637fe926badb6400e8d983f6bf88bff43cec 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;
 
@@ -1087,6 +1088,7 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
         return -ENODATA;
 }
 
+#if 0 /// UNNEEDED by elogind
 int cg_install_release_agent(const char *controller, const char *agent) {
         _cleanup_free_ char *fs = NULL, *contents = NULL;
         const char *sc;
@@ -1109,6 +1111,7 @@ int cg_install_release_agent(const char *controller, const char *agent) {
                 return r;
 
         sc = strstrip(contents);
+
         if (isempty(sc)) {
                 r = write_string_file(fs, agent, 0);
                 if (r < 0)
@@ -1171,6 +1174,7 @@ int cg_uninstall_release_agent(const char *controller) {
 
         return 0;
 }
+#endif // 0
 
 int cg_is_empty(const char *controller, const char *path) {
         _cleanup_fclose_ FILE *f = NULL;
@@ -1359,7 +1363,6 @@ int cg_mangle_path(const char *path, char **result) {
 }
 
 int cg_get_root_path(char **path) {
-#if 0 /// elogind does not support systemd scopes and slices
         char *p, *e;
         int r;
 
@@ -1369,20 +1372,20 @@ int cg_get_root_path(char **path) {
         if (r < 0)
                 return r;
 
+#if 0 /// elogind does not support systemd scopes and slices
         e = endswith(p, "/" SPECIAL_INIT_SCOPE);
         if (!e)
                 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
         if (!e)
                 e = endswith(p, "/system"); /* even more legacy */
+#else
+        e = endswith(p, "/elogind");
+#endif // 0
         if (e)
                 *e = 0;
 
         *path = p;
         return 0;
-#else
-        assert(path);
-        return cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, path);
-#endif // 0
 }
 
 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
@@ -2265,6 +2268,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 +2335,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 +2352,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
@@ -2610,9 +2652,15 @@ bool cg_is_hybrid_wanted(void) {
         return (wanted = r > 0 ? !b : is_default);
 }
 #else
+bool cg_is_unified_wanted(void) {
+        return false;
+}
 bool cg_is_legacy_wanted(void) {
         return true;
 }
+bool cg_is_hybrid_wanted(void) {
+        return false;
+}
 #endif // 0
 
 #if 0 /// UNNEEDED by elogind