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

selftests: add F_CREATED_QUERY tests

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>

+39
+39
tools/testing/selftests/core/close_range_test.c
··· 26 26 #define F_DUPFD_QUERY (F_LINUX_SPECIFIC_BASE + 3) 27 27 #endif 28 28 29 + #ifndef F_CREATED_QUERY 30 + #define F_CREATED_QUERY (F_LINUX_SPECIFIC_BASE + 4) 31 + #endif 32 + 29 33 static inline int sys_close_range(unsigned int fd, unsigned int max_fd, 30 34 unsigned int flags) 31 35 { ··· 626 622 EXPECT_EQ(waitpid(pid, &status, 0), pid); 627 623 EXPECT_EQ(true, WIFEXITED(status)); 628 624 EXPECT_EQ(0, WEXITSTATUS(status)); 625 + } 626 + 627 + TEST(fcntl_created) 628 + { 629 + for (int i = 0; i < 101; i++) { 630 + int fd; 631 + char path[PATH_MAX]; 632 + 633 + fd = open("/dev/null", O_RDONLY | O_CLOEXEC); 634 + ASSERT_GE(fd, 0) { 635 + if (errno == ENOENT) 636 + SKIP(return, 637 + "Skipping test since /dev/null does not exist"); 638 + } 639 + 640 + /* We didn't create "/dev/null". */ 641 + EXPECT_EQ(fcntl(fd, F_CREATED_QUERY, 0), 0); 642 + close(fd); 643 + 644 + sprintf(path, "aaaa_%d", i); 645 + fd = open(path, O_CREAT | O_RDONLY | O_CLOEXEC, 0600); 646 + ASSERT_GE(fd, 0); 647 + 648 + /* We created "aaaa_%d". */ 649 + EXPECT_EQ(fcntl(fd, F_CREATED_QUERY, 0), 1); 650 + close(fd); 651 + 652 + fd = open(path, O_RDONLY | O_CLOEXEC); 653 + ASSERT_GE(fd, 0); 654 + 655 + /* We're opening it again, so no positive creation check. */ 656 + EXPECT_EQ(fcntl(fd, F_CREATED_QUERY, 0), 0); 657 + close(fd); 658 + unlink(path); 659 + } 629 660 } 630 661 631 662 TEST_HARNESS_MAIN