diff options
Diffstat (limited to 'tools/testing/selftests/bpf/cgroup_helpers.c')
| -rw-r--r-- | tools/testing/selftests/bpf/cgroup_helpers.c | 57 | 
1 files changed, 57 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index f3bca3ade0f3..c87b4e052ce9 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -6,6 +6,7 @@  #include <sys/types.h>  #include <linux/limits.h>  #include <stdio.h> +#include <stdlib.h>  #include <linux/sched.h>  #include <fcntl.h>  #include <unistd.h> @@ -176,3 +177,59 @@ int create_and_get_cgroup(char *path)  	return fd;  } + +/** + * get_cgroup_id() - Get cgroup id for a particular cgroup path + * @path: The cgroup path, relative to the workdir, to join + * + * On success, it returns the cgroup id. On failure it returns 0, + * which is an invalid cgroup id. + * If there is a failure, it prints the error to stderr. + */ +unsigned long long get_cgroup_id(char *path) +{ +	int dirfd, err, flags, mount_id, fhsize; +	union { +		unsigned long long cgid; +		unsigned char raw_bytes[8]; +	} id; +	char cgroup_workdir[PATH_MAX + 1]; +	struct file_handle *fhp, *fhp2; +	unsigned long long ret = 0; + +	format_cgroup_path(cgroup_workdir, path); + +	dirfd = AT_FDCWD; +	flags = 0; +	fhsize = sizeof(*fhp); +	fhp = calloc(1, fhsize); +	if (!fhp) { +		log_err("calloc"); +		return 0; +	} +	err = name_to_handle_at(dirfd, cgroup_workdir, fhp, &mount_id, flags); +	if (err >= 0 || fhp->handle_bytes != 8) { +		log_err("name_to_handle_at"); +		goto free_mem; +	} + +	fhsize = sizeof(struct file_handle) + fhp->handle_bytes; +	fhp2 = realloc(fhp, fhsize); +	if (!fhp2) { +		log_err("realloc"); +		goto free_mem; +	} +	err = name_to_handle_at(dirfd, cgroup_workdir, fhp2, &mount_id, flags); +	fhp = fhp2; +	if (err < 0) { +		log_err("name_to_handle_at"); +		goto free_mem; +	} + +	memcpy(id.raw_bytes, fhp->f_handle, 8); +	ret = id.cgid; + +free_mem: +	free(fhp); +	return ret; +}  | 
