]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
selftests: add F_CREATED_QUERY tests
authorChristian Brauner <brauner@kernel.org>
Wed, 24 Jul 2024 13:15:36 +0000 (15:15 +0200)
committerChristian Brauner <brauner@kernel.org>
Mon, 19 Aug 2024 11:45:00 +0000 (13:45 +0200)
Add simple selftests for fcntl(fd, F_CREATED_QUERY, 0).

Link: https://lore.kernel.org/r/20240724-work-fcntl-v1-2-e8153a2f1991@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
tools/testing/selftests/core/close_range_test.c

index 12b4eb9d04347f71f19a2c5a696632bf330cca12..e0d9851fe1c9c7cdab6ad81442006c6fd5d86779 100644 (file)
 #define F_DUPFD_QUERY (F_LINUX_SPECIFIC_BASE + 3)
 #endif
 
+#ifndef F_CREATED_QUERY
+#define F_CREATED_QUERY (F_LINUX_SPECIFIC_BASE + 4)
+#endif
+
 static inline int sys_close_range(unsigned int fd, unsigned int max_fd,
                                  unsigned int flags)
 {
@@ -624,4 +628,39 @@ TEST(close_range_bitmap_corruption)
        EXPECT_EQ(0, WEXITSTATUS(status));
 }
 
+TEST(fcntl_created)
+{
+       for (int i = 0; i < 101; i++) {
+               int fd;
+               char path[PATH_MAX];
+
+               fd = open("/dev/null", O_RDONLY | O_CLOEXEC);
+               ASSERT_GE(fd, 0) {
+                       if (errno == ENOENT)
+                               SKIP(return,
+                                          "Skipping test since /dev/null does not exist");
+               }
+
+               /* We didn't create "/dev/null". */
+               EXPECT_EQ(fcntl(fd, F_CREATED_QUERY, 0), 0);
+               close(fd);
+
+               sprintf(path, "aaaa_%d", i);
+               fd = open(path, O_CREAT | O_RDONLY | O_CLOEXEC, 0600);
+               ASSERT_GE(fd, 0);
+
+               /* We created "aaaa_%d". */
+               EXPECT_EQ(fcntl(fd, F_CREATED_QUERY, 0), 1);
+               close(fd);
+
+               fd = open(path, O_RDONLY | O_CLOEXEC);
+               ASSERT_GE(fd, 0);
+
+               /* We're opening it again, so no positive creation check. */
+               EXPECT_EQ(fcntl(fd, F_CREATED_QUERY, 0), 0);
+               close(fd);
+               unlink(path);
+       }
+}
+
 TEST_HARNESS_MAIN