Skip to content

Commit 3379303

Browse files
laoarKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add a new cgroup helper get_cgroup_hierarchy_id()
A new cgroup helper function, get_cgroup1_hierarchy_id(), has been introduced to obtain the ID of a cgroup1 hierarchy based on the provided cgroup name. This cgroup name can be obtained from the /proc/self/cgroup file. Signed-off-by: Yafang Shao <[email protected]>
1 parent 18cca3a commit 3379303

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tools/testing/selftests/bpf/cgroup_helpers.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,52 @@ unsigned long long get_classid_cgroup_id(void)
637637
format_classid_path(cgroup_workdir);
638638
return get_cgroup_id_from_path(cgroup_workdir);
639639
}
640+
641+
/**
642+
* get_cgroup1_hierarchy_id - Retrieves the ID of a cgroup1 hierarchy from the cgroup1 name
643+
* @cgrp_name: The cgroup1 name, which can be retrieved from /proc/self/cgroup.
644+
*/
645+
int get_cgroup1_hierarchy_id(const char *cgrp_name)
646+
{
647+
char *c, *c2, *c3, *c4;
648+
bool found = false;
649+
char line[1024];
650+
FILE *file;
651+
int i, id;
652+
653+
if (!cgrp_name)
654+
return -1;
655+
656+
file = fopen("/proc/self/cgroup", "r");
657+
if (!file) {
658+
log_err("fopen /proc/self/cgroup");
659+
return -1;
660+
}
661+
662+
while (fgets(line, 1024, file)) {
663+
i = 0;
664+
for (c = strtok_r(line, ":", &c2); c && i < 2; c = strtok_r(NULL, ":", &c2)) {
665+
if (i == 0) {
666+
id = strtol(c, NULL, 10);
667+
} else if (i == 1) {
668+
if (!strcmp(c, cgrp_name)) {
669+
found = true;
670+
break;
671+
}
672+
673+
/* Multiple subsystems may share one single mount point */
674+
for (c3 = strtok_r(c, ",", &c4); c3;
675+
c3 = strtok_r(NULL, ",", &c4)) {
676+
if (!strcmp(c, cgrp_name)) {
677+
found = true;
678+
break;
679+
}
680+
}
681+
}
682+
i++;
683+
}
684+
if (found)
685+
break;
686+
}
687+
return found ? id : -1;
688+
}

tools/testing/selftests/bpf/cgroup_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ int get_root_cgroup(void);
2020
int create_and_get_cgroup(const char *relative_path);
2121
void remove_cgroup(const char *relative_path);
2222
unsigned long long get_cgroup_id(const char *relative_path);
23+
int get_cgroup1_hierarchy_id(const char *cgrp_name);
2324

2425
int join_cgroup(const char *relative_path);
2526
int join_root_cgroup(void);

0 commit comments

Comments
 (0)