From: Lennart Poettering Date: Thu, 5 Oct 2023 09:49:07 +0000 (+0200) Subject: cgroup-util: add cg_pidref_get_path() helper and use it X-Git-Tag: v255-rc1~325^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a906224288f9d5e6f5e06e5314353782e9027df6;p=thirdparty%2Fsystemd.git cgroup-util: add cg_pidref_get_path() helper and use it --- diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index ca697ed97a4..67473f66512 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -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; diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h index b78c2475fa7..694986cffaf 100644 --- a/src/basic/cgroup-util.h +++ b/src/basic/cgroup-util.h @@ -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); diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 18be0650450..5f67b7a14c1 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -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);