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).
This commit is contained in:
Quentin Deslandes
2023-02-08 16:48:58 +01:00
parent 9779079b53
commit ee164216d3
3 changed files with 50 additions and 0 deletions
+22
View File
@@ -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;
+1
View File
@@ -261,6 +261,7 @@ int cg_path_get_cgroupid(const char *path, uint64_t *ret);
int cg_path_get_session(const char *path, char **session);
int cg_path_get_owner_uid(const char *path, uid_t *uid);
int cg_path_get_unit(const char *path, char **unit);
int cg_path_get_unit_path(const char *path, char **unit);
int cg_path_get_user_unit(const char *path, char **unit);
int cg_path_get_machine_name(const char *path, char **machine);
int cg_path_get_slice(const char *path, char **slice);