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

selftests/landlock: Make mounts configurable

Add a new struct mnt_opt to define a mount point with the mount_opt()
helper. This doesn't change tests but prepare for the next commit.

Link: https://lore.kernel.org/r/20230612191430.339153-5-mic@digikod.net
Signed-off-by: Mickaël Salaün <mic@digikod.net>

+40 -5
+40 -5
tools/testing/selftests/landlock/fs_test.c
··· 213 213 return err; 214 214 } 215 215 216 - static void prepare_layout(struct __test_metadata *const _metadata) 216 + struct mnt_opt { 217 + const char *const source; 218 + const char *const type; 219 + const unsigned long flags; 220 + const char *const data; 221 + }; 222 + 223 + const struct mnt_opt mnt_tmp = { 224 + .type = "tmpfs", 225 + .data = "size=4m,mode=700", 226 + }; 227 + 228 + static int mount_opt(const struct mnt_opt *const mnt, const char *const target) 229 + { 230 + return mount(mnt->source ?: mnt->type, target, mnt->type, mnt->flags, 231 + mnt->data); 232 + } 233 + 234 + static void prepare_layout_opt(struct __test_metadata *const _metadata, 235 + const struct mnt_opt *const mnt) 217 236 { 218 237 disable_caps(_metadata); 219 238 umask(0077); ··· 244 225 */ 245 226 set_cap(_metadata, CAP_SYS_ADMIN); 246 227 ASSERT_EQ(0, unshare(CLONE_NEWNS)); 247 - ASSERT_EQ(0, mount("tmp", TMP_DIR, "tmpfs", 0, "size=4m,mode=700")); 228 + ASSERT_EQ(0, mount_opt(mnt, TMP_DIR)) 229 + { 230 + TH_LOG("Failed to mount the %s filesystem: %s", mnt->type, 231 + strerror(errno)); 232 + /* 233 + * FIXTURE_TEARDOWN() is not called when FIXTURE_SETUP() 234 + * failed, so we need to explicitly do a minimal cleanup to 235 + * avoid cascading errors with other tests that don't depend on 236 + * the same filesystem. 237 + */ 238 + remove_path(TMP_DIR); 239 + } 248 240 ASSERT_EQ(0, mount(NULL, TMP_DIR, NULL, MS_PRIVATE | MS_REC, NULL)); 249 241 clear_cap(_metadata, CAP_SYS_ADMIN); 242 + } 243 + 244 + static void prepare_layout(struct __test_metadata *const _metadata) 245 + { 246 + prepare_layout_opt(_metadata, &mnt_tmp); 250 247 } 251 248 252 249 static void cleanup_layout(struct __test_metadata *const _metadata) ··· 304 269 create_file(_metadata, file1_s3d1); 305 270 create_directory(_metadata, dir_s3d2); 306 271 set_cap(_metadata, CAP_SYS_ADMIN); 307 - ASSERT_EQ(0, mount("tmp", dir_s3d2, "tmpfs", 0, "size=4m,mode=700")); 272 + ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s3d2)); 308 273 clear_cap(_metadata, CAP_SYS_ADMIN); 309 274 310 275 ASSERT_EQ(0, mkdir(dir_s3d3, 0700)); ··· 4103 4068 create_directory(_metadata, LOWER_BASE); 4104 4069 set_cap(_metadata, CAP_SYS_ADMIN); 4105 4070 /* Creates tmpfs mount points to get deterministic overlayfs. */ 4106 - ASSERT_EQ(0, mount("tmp", LOWER_BASE, "tmpfs", 0, "size=4m,mode=700")); 4071 + ASSERT_EQ(0, mount_opt(&mnt_tmp, LOWER_BASE)); 4107 4072 clear_cap(_metadata, CAP_SYS_ADMIN); 4108 4073 create_file(_metadata, lower_fl1); 4109 4074 create_file(_metadata, lower_dl1_fl2); ··· 4113 4078 4114 4079 create_directory(_metadata, UPPER_BASE); 4115 4080 set_cap(_metadata, CAP_SYS_ADMIN); 4116 - ASSERT_EQ(0, mount("tmp", UPPER_BASE, "tmpfs", 0, "size=4m,mode=700")); 4081 + ASSERT_EQ(0, mount_opt(&mnt_tmp, UPPER_BASE)); 4117 4082 clear_cap(_metadata, CAP_SYS_ADMIN); 4118 4083 create_file(_metadata, upper_fu1); 4119 4084 create_file(_metadata, upper_du1_fu2);