diff options
author | Quentin Deslandes <qde@naccy.de> | 2023-01-26 19:39:08 +0100 |
---|---|---|
committer | Quentin Deslandes <qde@naccy.de> | 2023-02-08 16:48:58 +0100 |
commit | ee164216d38bc7ca51e040b90bdc96ed7518df37 (patch) | |
tree | 590008b2c2b6d1aee69617b92df91926bf002442 /src/basic/cgroup-util.c | |
parent | 9779079b5354ac66809a1b6e593ef2a2a3f75117 (diff) | |
download | systemd-ee164216d38bc7ca51e040b90bdc96ed7518df37.tar.gz |
core: add cg_path_get_unit_path()
From a given cgroup path, cg_path_get_unit() allows to retrieve the
unit's name. Although, this removes the path to the unit's cgroup,
preventing the result to be used to fetch xattrs.
Introduce cg_path_get_unit_path() which provides the path to the unit's
cgroup. This function behave similarly to cg_path_get_unit() (checking
the validity and escaping the unit's name).
Diffstat (limited to 'src/basic/cgroup-util.c')
-rw-r--r-- | src/basic/cgroup-util.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index a8e4a1bb2d..feda596939 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -1198,6 +1198,28 @@ int cg_path_get_unit(const char *path, char **ret) { return 0; } +int cg_path_get_unit_path(const char *path, char **ret) { + _cleanup_free_ char *path_copy = NULL; + char *unit_name; + + assert(path); + assert(ret); + + path_copy = strdup(path); + if (!path_copy) + return -ENOMEM; + + unit_name = (char *)skip_slices(path_copy); + unit_name[strcspn(unit_name, "/")] = 0; + + if (!unit_name_is_valid(cg_unescape(unit_name), UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) + return -ENXIO; + + *ret = TAKE_PTR(path_copy); + + return 0; +} + int cg_pid_get_unit(pid_t pid, char **unit) { _cleanup_free_ char *cgroup = NULL; int r; |