chiark / gitweb /
core: find the closest parent slice that has a specfic cgroup controller enabled...
[elogind.git] / src / shared / cgroup-util.c
index f2af8dcfda57ccde25b92c77fff40f91efa9f6b4..1aa81c2cd18f1700a75631a4d6b9b1659dba3e9b 100644 (file)
@@ -194,12 +194,12 @@ int cg_kill(const char *controller, const char *path, int sig, bool sigcont, boo
                         if (kill(pid, sig) < 0) {
                                 if (ret >= 0 && errno != ESRCH)
                                         ret = -errno;
-                        } else if (ret == 0) {
-
+                        } else {
                                 if (sigcont)
                                         kill(pid, SIGCONT);
 
-                                ret = 1;
+                                if (ret == 0)
+                                        ret = 1;
                         }
 
                         done = false;
@@ -746,10 +746,7 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
         } else
                 controller = SYSTEMD_CGROUP_CONTROLLER;
 
-        if (pid == 0)
-                fs = "/proc/self/cgroup";
-        else
-                fs = procfs_file_alloca(pid, "cgroup");
+        fs = procfs_file_alloca(pid, "cgroup");
 
         f = fopen(fs, "re");
         if (!f)
@@ -1285,39 +1282,18 @@ int cg_pid_get_user_unit(pid_t pid, char **unit) {
 }
 
 int cg_path_get_machine_name(const char *path, char **machine) {
-        const char *e, *n, *x;
-        char *s, *r;
-        size_t l;
-
-        assert(path);
-        assert(machine);
-
-        /* Skip slices, if there are any */
-        e = skip_slices(path);
-
-        n = strchrnul(e, '/');
-        if (e == n)
-                return -ENOENT;
-
-        s = strndupa(e, n - e);
-        s = cg_unescape(s);
-
-        x = startswith(s, "machine-");
-        if (!x)
-                return -ENOENT;
-        if (!endswith(x, ".scope"))
-                return -ENOENT;
+        _cleanup_free_ char *u = NULL, *sl = NULL;
+        int r;
 
-        l = strlen(x);
-        if (l <= 6)
-                return -ENOENT;
+        r = cg_path_get_unit(path, &u);
+        if (r < 0)
+                return r;
 
-        r = strndup(x, l - 6);
-        if (!r)
+        sl = strjoin("/run/systemd/machines/unit:", u, NULL);
+        if (!sl)
                 return -ENOMEM;
 
-        *machine = r;
-        return 0;
+        return readlink_malloc(sl, machine);
 }
 
 int cg_pid_get_machine_name(pid_t pid, char **machine) {
@@ -1335,11 +1311,10 @@ int cg_pid_get_machine_name(pid_t pid, char **machine) {
 
 int cg_path_get_session(const char *path, char **session) {
         const char *e, *n, *x;
-        char *s, *r;
+        char *s;
         size_t l;
 
         assert(path);
-        assert(session);
 
         /* Skip slices, if there are any */
         e = skip_slices(path);
@@ -1361,11 +1336,16 @@ int cg_path_get_session(const char *path, char **session) {
         if (l <= 6)
                 return -ENOENT;
 
-        r = strndup(x, l - 6);
-        if (!r)
-                return -ENOMEM;
+        if (session) {
+                char *r;
+
+                r = strndup(x, l - 6);
+                if (!r)
+                        return -ENOMEM;
+
+                *session = r;
+        }
 
-        *session = r;
         return 0;
 }
 
@@ -1373,8 +1353,6 @@ int cg_pid_get_session(pid_t pid, char **session) {
         _cleanup_free_ char *cgroup = NULL;
         int r;
 
-        assert(session);
-
         r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
         if (r < 0)
                 return r;
@@ -1690,7 +1668,7 @@ int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path,
         return r;
 }
 
-int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to) {
+int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
         CGroupControllerMask bit = 1;
         const char *n;
         int r;
@@ -1702,8 +1680,17 @@ int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, cons
         }
 
         NULSTR_FOREACH(n, mask_names) {
-                if (supported & bit)
-                        cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, to, false, false);
+                if (supported & bit) {
+                        const char *p = NULL;
+
+                        if (to_callback)
+                                p = to_callback(bit, userdata);
+
+                        if (!p)
+                                p = to;
+
+                        cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, p, false, false);
+                }
 
                 bit <<= 1;
         }