cgroup-util: add cg_pidref_get_path() helper and use it
authorLennart Poettering <lennart@poettering.net>
Thu, 5 Oct 2023 09:49:07 +0000 (11:49 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 5 Oct 2023 11:26:25 +0000 (13:26 +0200)
src/basic/cgroup-util.c
src/basic/cgroup-util.h
src/core/cgroup.c

index ca697ed97a4af2cb47232271bc9b70b4e1650d2a..67473f6651224ce5337735de773b56c475ab8a97 100644 (file)
@@ -813,6 +813,28 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **ret_path) {
         }
 }
 
+int cg_pidref_get_path(const char *controller, PidRef *pidref, char **ret_path) {
+        _cleanup_free_ char *path = NULL;
+        int r;
+
+        assert(ret_path);
+
+        if (!pidref_is_set(pidref))
+                return -ESRCH;
+
+        r = cg_pid_get_path(controller, pidref->pid, &path);
+        if (r < 0)
+                return r;
+
+        /* Before we return the path, make sure the procfs entry for this pid still matches the pidref */
+        r = pidref_verify(pidref);
+        if (r < 0)
+                return r;
+
+        *ret_path = TAKE_PTR(path);
+        return 0;
+}
+
 int cg_install_release_agent(const char *controller, const char *agent) {
         _cleanup_free_ char *fs = NULL, *contents = NULL;
         const char *sc;
index b78c2475fa79d83c5cc584c83829ceaf2244359d..694986cffafd3d6da219af77ca9fb2bd4df7a2a5 100644 (file)
@@ -204,6 +204,7 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch
 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs);
 
 int cg_pid_get_path(const char *controller, pid_t pid, char **path);
+int cg_pidref_get_path(const char *controller, PidRef *pidref, char **path);
 
 int cg_rmdir(const char *controller, const char *path);
 
index 18be065045038123f773eb36a82aa080905e49e3..5f67b7a14c175c3fc69ce6ffd65c6ac827b8ef6c 100644 (file)
@@ -3784,10 +3784,7 @@ Unit *manager_get_unit_by_pidref_cgroup(Manager *m, PidRef *pid) {
 
         assert(m);
 
-        if (!pidref_is_set(pid))
-                return NULL;
-
-        if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid->pid, &cgroup) < 0)
+        if (cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup) < 0)
                 return NULL;
 
         return manager_get_unit_by_cgroup(m, cgroup);