Re: [PATCH] selftests: add tests for mount notification

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 8 Mar 2025 at 13:10, Christian Brauner <brauner@xxxxxxxxxx> wrote:

> setup_namespace() can just be called on each FIXTURE_SETUP() invocation
> and cleanup_namespace() on each FIXTURE_TEARDOWN(). They will always
> start with a clean slate this way.

Ah, hadn't realized that each test case will get a fresh process...

Attached further cleanup with this in mind.

Thanks,
Miklos
diff --git a/tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c b/tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c
index 2f0bd360166d..4a2d5c454fd1 100644
--- a/tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c
+++ b/tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c
@@ -36,10 +36,6 @@ struct fanotify_event_info_mnt {
 #define FAN_MARK_MNTNS 0x00000110
 #endif
 
-static char root_mntpoint[] = "/tmp/mount-notify_test_root.XXXXXX";
-static int orig_root, ns_fd;
-static uint64_t root_id;
-
 static uint64_t get_mnt_id(struct __test_metadata *const _metadata,
 			   const char *path)
 {
@@ -50,37 +46,39 @@ static uint64_t get_mnt_id(struct __test_metadata *const _metadata,
 	return sx.stx_mnt_id;
 }
 
-static void cleanup_namespace(struct __test_metadata *const _metadata)
-{
-	ASSERT_EQ(fchdir(orig_root), 0);
-
-	ASSERT_EQ(chroot("."), 0);
+static const char root_mntpoint_templ[] = "/tmp/mount-notify_test_root.XXXXXX";
 
-	EXPECT_EQ(umount2(root_mntpoint, MNT_DETACH), 0);
-	EXPECT_EQ(chdir(root_mntpoint), 0);
-	EXPECT_EQ(rmdir("a"), 0);
-	EXPECT_EQ(rmdir("b"), 0);
-	EXPECT_EQ(chdir("/"), 0);
-	EXPECT_EQ(rmdir(root_mntpoint), 0);
-}
+FIXTURE(fanotify) {
+	int fan_fd;
+	char buf[256];
+	unsigned int rem;
+	void *next;
+	char root_mntpoint[sizeof(root_mntpoint_templ)];
+	int orig_root;
+	int ns_fd;
+	uint64_t root_id;
+};
 
-static void setup_namespace(struct __test_metadata *const _metadata)
+FIXTURE_SETUP(fanotify)
 {
+	int ret;
+
 	ASSERT_EQ(unshare(CLONE_NEWNS), 0);
 
-	ns_fd = open("/proc/self/ns/mnt", O_RDONLY);
-	ASSERT_GE(ns_fd, 0);
+	self->ns_fd = open("/proc/self/ns/mnt", O_RDONLY);
+	ASSERT_GE(self->ns_fd, 0);
 
 	ASSERT_EQ(mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL), 0);
 
-	ASSERT_NE(mkdtemp(root_mntpoint), NULL);
+	strcpy(self->root_mntpoint, root_mntpoint_templ);
+	ASSERT_NE(mkdtemp(self->root_mntpoint), NULL);
 
-	orig_root = open("/", O_PATH | O_CLOEXEC);
-	ASSERT_GE(orig_root, 0);
+	self->orig_root = open("/", O_PATH | O_CLOEXEC);
+	ASSERT_GE(self->orig_root, 0);
 
-	ASSERT_EQ(mount(root_mntpoint, root_mntpoint, NULL, MS_BIND, NULL), 0);
+	ASSERT_EQ(mount("tmpfs", self->root_mntpoint, "tmpfs", 0, NULL), 0);
 
-	ASSERT_EQ(chroot(root_mntpoint), 0);
+	ASSERT_EQ(chroot(self->root_mntpoint), 0);
 
 	ASSERT_EQ(chdir("/"), 0);
 
@@ -88,58 +86,14 @@ static void setup_namespace(struct __test_metadata *const _metadata)
 
 	ASSERT_EQ(mkdir("b", 0700), 0);
 
-	root_id = get_mnt_id(_metadata, "/");
-	ASSERT_NE(root_id, 0);
-}
-
-FIXTURE(fanotify) {
-	int fan_fd;
-	char buf[256];
-	unsigned int rem;
-	void *next;
-};
-
-#define MAX_MNTS 256
-#define MAX_PATH 256
-
-FIXTURE_SETUP(fanotify)
-{
-	uint64_t list[MAX_MNTS];
-	ssize_t num;
-	size_t bufsize = sizeof(struct statmount) + MAX_PATH;
-	struct statmount *buf = alloca(bufsize);
-	unsigned int i;
-	int ret;
-
-	setup_namespace(_metadata);
-
-	// Clean up mount tree
-	ret = mount("", "/", NULL, MS_PRIVATE, NULL);
-	ASSERT_EQ(ret, 0);
-
-	num = listmount(LSMT_ROOT, 0, 0, list, MAX_MNTS, 0);
-	ASSERT_GE(num, 1);
-	ASSERT_LT(num, MAX_MNTS);
-
-	for (i = 0; i < num; i++) {
-		if (list[i] == root_id)
-			continue;
-		ret = statmount(list[i], 0, STATMOUNT_MNT_POINT, buf, bufsize, 0);
-		if (ret == 0 && buf->mask & STATMOUNT_MNT_POINT)
-			umount2(buf->str + buf->mnt_point, MNT_DETACH);
-	}
-	num = listmount(LSMT_ROOT, 0, 0, list, 2, 0);
-	ASSERT_EQ(num, 1);
-	ASSERT_EQ(list[0], root_id);
-
-	mkdir("/a", 0700);
-	mkdir("/b", 0700);
+	self->root_id = get_mnt_id(_metadata, "/");
+	ASSERT_NE(self->root_id, 0);
 
 	self->fan_fd = fanotify_init(FAN_REPORT_MNT, 0);
 	ASSERT_GE(self->fan_fd, 0);
 
 	ret = fanotify_mark(self->fan_fd, FAN_MARK_ADD | FAN_MARK_MNTNS,
-			    FAN_MNT_ATTACH | FAN_MNT_DETACH, ns_fd, NULL);
+			    FAN_MNT_ATTACH | FAN_MNT_DETACH, self->ns_fd, NULL);
 	ASSERT_EQ(ret, 0);
 
 	self->rem = 0;
@@ -147,9 +101,17 @@ FIXTURE_SETUP(fanotify)
 
 FIXTURE_TEARDOWN(fanotify)
 {
-	cleanup_namespace(_metadata);
 	ASSERT_EQ(self->rem, 0);
 	close(self->fan_fd);
+
+	ASSERT_EQ(fchdir(self->orig_root), 0);
+
+	ASSERT_EQ(chroot("."), 0);
+
+	EXPECT_EQ(umount2(self->root_mntpoint, MNT_DETACH), 0);
+	EXPECT_EQ(chdir(self->root_mntpoint), 0);
+	EXPECT_EQ(chdir("/"), 0);
+	EXPECT_EQ(rmdir(self->root_mntpoint), 0);
 }
 
 static uint64_t expect_notify(struct __test_metadata *const _metadata,
@@ -218,7 +180,6 @@ static void expect_notify_mask_n(struct __test_metadata *const _metadata,
 		mnts[i] = expect_notify_mask(_metadata, self, mask);
 }
 
-
 static void verify_mount_ids(struct __test_metadata *const _metadata,
 			     const uint64_t list1[], const uint64_t list2[],
 			     size_t num)
@@ -279,7 +240,7 @@ static void setup_mount_tree(struct __test_metadata *const _metadata,
 TEST_F(fanotify, bind)
 {
 	int ret;
-	uint64_t mnts[2] = { root_id };
+	uint64_t mnts[2] = { self->root_id };
 
 	ret = mount("/", "/", NULL, MS_BIND, NULL);
 	ASSERT_EQ(ret, 0);
@@ -303,7 +264,7 @@ TEST_F(fanotify, bind)
 TEST_F(fanotify, move)
 {
 	int ret;
-	uint64_t mnts[2] = { root_id };
+	uint64_t mnts[2] = { self->root_id };
 	uint64_t move_id;
 
 	ret = mount("/", "/a", NULL, MS_BIND, NULL);
@@ -337,7 +298,7 @@ TEST_F(fanotify, propagate)
 
 	expect_notify_mask_n(_metadata, self, FAN_MNT_ATTACH, num - 1, mnts + 1);
 
-	mnts[0] = root_id;
+	mnts[0] = self->root_id;
 	check_mounted(_metadata, mnts, num);
 
 	// Cleanup
@@ -349,7 +310,7 @@ TEST_F(fanotify, propagate)
 	ret = mount("", "/", NULL, MS_PRIVATE, NULL);
 	ASSERT_EQ(ret, 0);
 
-	mnts2[0] = root_id;
+	mnts2[0] = self->root_id;
 	expect_notify_mask_n(_metadata, self, FAN_MNT_DETACH, num - 1, mnts2 + 1);
 	verify_mount_ids(_metadata, mnts, mnts2, num);
 
@@ -359,7 +320,7 @@ TEST_F(fanotify, propagate)
 TEST_F(fanotify, fsmount)
 {
 	int ret, fs, mnt;
-	uint64_t mnts[2] = { root_id };
+	uint64_t mnts[2] = { self->root_id };
 
 	fs = fsopen("tmpfs", 0);
 	ASSERT_GE(fs, 0);
@@ -395,7 +356,7 @@ TEST_F(fanotify, fsmount)
 
 TEST_F(fanotify, reparent)
 {
-	uint64_t mnts[6] = { root_id };
+	uint64_t mnts[6] = { self->root_id };
 	uint64_t dmnts[3];
 	uint64_t masks[3];
 	unsigned int i;
@@ -471,7 +432,7 @@ TEST_F(fanotify, reparent)
 
 TEST_F(fanotify, rmdir)
 {
-	uint64_t mnts[3] = { root_id };
+	uint64_t mnts[3] = { self->root_id };
 	int ret;
 
 	ret = mount("/", "/a", NULL, MS_BIND, NULL);
@@ -507,14 +468,11 @@ TEST_F(fanotify, rmdir)
 	// Cleanup
 	ret = chdir("/");
 	ASSERT_EQ(ret, 0);
-
-	ret = mkdir("a", 0700);
-	ASSERT_EQ(ret, 0);
 }
 
 TEST_F(fanotify, pivot_root)
 {
-	uint64_t mnts[3] = { root_id };
+	uint64_t mnts[3] = { self->root_id };
 	uint64_t mnts2[3];
 	int ret;
 

[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [NTFS 3]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [NTFS 3]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux