cgroup-util: add cg_path_get_cgroupid()

It returns the cgroupID from a cgroup path.
This commit is contained in:
Iago López Galeiras
2020-12-11 13:15:25 +01:00
committed by Iago Lopez Galeiras
parent 021d1e9612
commit 535e3dd091
2 changed files with 24 additions and 0 deletions

View File

@@ -1367,6 +1367,29 @@ int cg_pid_get_machine_name(pid_t pid, char **machine) {
return cg_path_get_machine_name(cgroup, machine);
}
int cg_path_get_cgroupid(const char *path, uint64_t *ret) {
int mnt_id = -1;
assert(path);
assert(ret);
union {
struct file_handle f_handle;
uint8_t space[offsetof(struct file_handle, f_handle) + sizeof(uint64_t)];
} buf = {
.f_handle.handle_bytes = sizeof(uint64_t),
};
/* This is cgroupfs so we know the size of the handle, thus no need to loop around like
* name_to_handle_at_loop() does in mountpoint-util.c */
if (name_to_handle_at(AT_FDCWD, path, &buf.f_handle, &mnt_id, 0) < 0)
return -errno;
*ret = *(uint64_t *) buf.f_handle.f_handle;
return 0;
}
int cg_path_get_session(const char *path, char **session) {
_cleanup_free_ char *unit = NULL;
char *start, *end;

View File

@@ -236,6 +236,7 @@ int cg_is_empty_recursive(const char *controller, const char *path);
int cg_get_root_path(char **path);
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);