Skip to content

Commit d74cd78

Browse files
Werkovhtejun
authored andcommitted
selftests: cgroup: Optionally set up v1 environment
Use the missing mount of the unifier hierarchy as a hint of legacy system and prepare our own named v1 hierarchy for tests. The code is only in test_core.c and not cgroup_util.c because other selftests are related to controllers which will be exposed on v2 hierarchy but named hierarchies are only v1 thing. Signed-off-by: Michal Koutný <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent dd7588e commit d74cd78

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

tools/testing/selftests/cgroup/test_core.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <linux/sched.h>
66
#include <sys/types.h>
77
#include <sys/mman.h>
8+
#include <sys/mount.h>
9+
#include <sys/stat.h>
810
#include <sys/wait.h>
911
#include <unistd.h>
1012
#include <fcntl.h>
@@ -863,6 +865,38 @@ static int test_cgcore_lesser_ns_open(const char *root)
863865
return ret;
864866
}
865867

868+
static int setup_named_v1_root(char *root, size_t len, const char *name)
869+
{
870+
char options[PATH_MAX];
871+
int r;
872+
873+
r = snprintf(root, len, "/mnt/cg_selftest");
874+
if (r < 0)
875+
return r;
876+
877+
r = snprintf(options, sizeof(options), "none,name=%s", name);
878+
if (r < 0)
879+
return r;
880+
881+
r = mkdir(root, 0755);
882+
if (r < 0 && errno != EEXIST)
883+
return r;
884+
885+
r = mount("none", root, "cgroup", 0, options);
886+
if (r < 0)
887+
return r;
888+
889+
return 0;
890+
}
891+
892+
static void cleanup_named_v1_root(char *root)
893+
{
894+
if (!cg_test_v1_named)
895+
return;
896+
umount(root);
897+
rmdir(root);
898+
}
899+
866900
#define T(x) { x, #x }
867901
struct corecg_test {
868902
int (*fn)(const char *root);
@@ -888,13 +922,18 @@ int main(int argc, char *argv[])
888922
char root[PATH_MAX];
889923
int i, ret = EXIT_SUCCESS;
890924

891-
if (cg_find_unified_root(root, sizeof(root), &nsdelegate))
892-
ksft_exit_skip("cgroup v2 isn't mounted\n");
925+
if (cg_find_unified_root(root, sizeof(root), &nsdelegate)) {
926+
if (setup_named_v1_root(root, sizeof(root), CG_NAMED_NAME))
927+
ksft_exit_skip("cgroup v2 isn't mounted and could not setup named v1 hierarchy\n");
928+
cg_test_v1_named = true;
929+
goto post_v2_setup;
930+
}
893931

894932
if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))
895933
if (cg_write(root, "cgroup.subtree_control", "+memory"))
896934
ksft_exit_skip("Failed to set memory controller\n");
897935

936+
post_v2_setup:
898937
for (i = 0; i < ARRAY_SIZE(tests); i++) {
899938
switch (tests[i].fn(root)) {
900939
case KSFT_PASS:
@@ -910,5 +949,6 @@ int main(int argc, char *argv[])
910949
}
911950
}
912951

952+
cleanup_named_v1_root(root);
913953
return ret;
914954
}

0 commit comments

Comments
 (0)