Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/landlock: Add supports_filesystem() helper

Replace supports_overlayfs() with supports_filesystem() to be able to
check several filesystems. This will be useful in a following commit.

Only check for overlay filesystem once in the setup step, and then rely
on self->skip_test.

Cc: Guenter Roeck <groeck@chromium.org>
Cc: Jeff Xu <jeffxu@google.com>
Link: https://lore.kernel.org/r/20230612191430.339153-4-mic@digikod.net
Signed-off-by: Mickaël Salaün <mic@digikod.net>

+23 -13
+23 -13
tools/testing/selftests/landlock/fs_test.c
··· 107 107 return false; 108 108 } 109 109 110 - static bool supports_overlayfs(void) 110 + static bool supports_filesystem(const char *const filesystem) 111 111 { 112 + char str[32]; 113 + int len; 112 114 bool res; 113 115 FILE *const inf = fopen("/proc/filesystems", "r"); 114 116 ··· 121 119 if (!inf) 122 120 return true; 123 121 124 - res = fgrep(inf, "nodev\toverlay\n"); 122 + len = snprintf(str, sizeof(str), "nodev\t%s\n", filesystem); 123 + if (len >= sizeof(str)) 124 + /* Ignores too-long filesystem names. */ 125 + return true; 126 + 127 + res = fgrep(inf, str); 125 128 fclose(inf); 126 129 return res; 127 130 } ··· 4051 4044 * └── work 4052 4045 */ 4053 4046 4054 - /* clang-format off */ 4055 - FIXTURE(layout2_overlay) {}; 4056 - /* clang-format on */ 4047 + FIXTURE(layout2_overlay) 4048 + { 4049 + bool skip_test; 4050 + }; 4057 4051 4058 4052 FIXTURE_SETUP(layout2_overlay) 4059 4053 { 4060 - if (!supports_overlayfs()) 4061 - SKIP(return, "overlayfs is not supported"); 4054 + if (!supports_filesystem("overlay")) { 4055 + self->skip_test = true; 4056 + SKIP(return, "overlayfs is not supported (setup)"); 4057 + } 4062 4058 4063 4059 prepare_layout(_metadata); 4064 4060 ··· 4099 4089 4100 4090 FIXTURE_TEARDOWN(layout2_overlay) 4101 4091 { 4102 - if (!supports_overlayfs()) 4103 - SKIP(return, "overlayfs is not supported"); 4092 + if (self->skip_test) 4093 + SKIP(return, "overlayfs is not supported (teardown)"); 4104 4094 4105 4095 EXPECT_EQ(0, remove_path(lower_do1_fl3)); 4106 4096 EXPECT_EQ(0, remove_path(lower_dl1_fl2)); ··· 4133 4123 4134 4124 TEST_F_FORK(layout2_overlay, no_restriction) 4135 4125 { 4136 - if (!supports_overlayfs()) 4137 - SKIP(return, "overlayfs is not supported"); 4126 + if (self->skip_test) 4127 + SKIP(return, "overlayfs is not supported (test)"); 4138 4128 4139 4129 ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY)); 4140 4130 ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY)); ··· 4299 4289 size_t i; 4300 4290 const char *path_entry; 4301 4291 4302 - if (!supports_overlayfs()) 4303 - SKIP(return, "overlayfs is not supported"); 4292 + if (self->skip_test) 4293 + SKIP(return, "overlayfs is not supported (test)"); 4304 4294 4305 4295 /* Sets rules on base directories (i.e. outside overlay scope). */ 4306 4296 ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);