[LTP] [PATCH v3 2/2] fstat: add test for multiple file types using fstat

Jinseok Kim always.starving0@gmail.com
Wed Feb 25 14:19:19 CET 2026


Following review feedback, create a dedicated fstat test that verifies
S_ISREG(), S_ISDIR(), S_ISFIFO(), S_ISLNK(), S_ISCHR(), S_ISBLK()
on different file types.

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 runtest/syscalls                           |   2 +
 testcases/kernel/syscalls/fstat/.gitignore |   2 +
 testcases/kernel/syscalls/fstat/fstat04.c  | 121 +++++++++++++++++++++
 3 files changed, 125 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 05bb3ceb1..5b34e6bdc 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -435,6 +435,8 @@ fstat02 fstat02
 fstat02_64 fstat02_64
 fstat03 fstat03
 fstat03_64 fstat03_64
+fstat04 fstat04
+fstat04_64 fstat04_64

 #fstatat64/newfstatat test cases
 fstatat01 fstatat01
diff --git a/testcases/kernel/syscalls/fstat/.gitignore b/testcases/kernel/syscalls/fstat/.gitignore
index 9b1089438..a1f538f7e 100644
--- a/testcases/kernel/syscalls/fstat/.gitignore
+++ b/testcases/kernel/syscalls/fstat/.gitignore
@@ -2,3 +2,5 @@
 /fstat02_64
 /fstat03
 /fstat03_64
+/fstat04
+/fstat04_64
diff --git a/testcases/kernel/syscalls/fstat/fstat04.c b/testcases/kernel/syscalls/fstat/fstat04.c
new file mode 100644
index 000000000..97d0d6f50
--- /dev/null
+++ b/testcases/kernel/syscalls/fstat/fstat04.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <always.starving0@gmail.com>
+ */
+/*\
+ * Verify that fstat correctly identifies various file types
+ */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include "tst_test.h"
+
+#define REG_FILE    "regfile"
+#define DIR_FILE    "dirfile"
+#define FIFO_FILE   "fifofile"
+#define SYMLINK     "symlink"
+#define CHR_DEV     "chrdev"
+#define BLK_DEV     "blkdev"
+
+static int fd = -1;
+
+static struct tcase {
+	const char *desc;
+	const char *path;
+	const char *macro_name;
+	mode_t exp_type;
+} tcases[] = {
+	{ "regular file",   REG_FILE,  "S_IFREG", S_IFREG },
+	{ "directory",      DIR_FILE,  "S_IFDIR", S_IFDIR },
+	{ "FIFO (pipe)",    FIFO_FILE, "S_IFIFO", S_IFIFO },
+	{ "symbolic link",  SYMLINK,   "S_IFLNK", S_IFLNK },
+	{ "character dev",  CHR_DEV,   "S_IFCHR", S_IFCHR },
+	{ "block dev",      BLK_DEV,   "S_IFBLK", S_IFBLK },
+};
+
+static void verify_fstat(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+	struct stat buf;
+
+	int flags = O_RDONLY | O_NONBLOCK;
+
+	if (tc->exp_type == S_IFDIR)
+		flags |= O_DIRECTORY;
+
+	fd = SAFE_OPEN(tc->path, flags);
+
+	SAFE_FSTAT(fd, &buf);
+	SAFE_CLOSE(fd);
+
+	int is_correct = 0;
+
+	switch (tc->exp_type) {
+	case S_IFREG:
+		is_correct = S_ISREG(buf.st_mode);
+		break;
+	case S_IFDIR:
+		is_correct = S_ISDIR(buf.st_mode);
+		break;
+	case S_IFIFO:
+		is_correct = S_ISFIFO(buf.st_mode);
+		break;
+	case S_IFLNK:
+		is_correct = S_ISLNK(buf.st_mode);
+		break;
+	case S_IFCHR:
+		is_correct = S_ISCHR(buf.st_mode);
+		break;
+	case S_IFBLK:
+		is_correct = S_ISBLK(buf.st_mode);
+		break;
+	}
+
+	if (is_correct)
+		tst_res(TPASS, "%s: %s() macro matches", tc->desc, tc->macro_name);
+	else
+		tst_res(TFAIL, "%s: %s() macro does NOT match", tc->desc, tc->macro_name);
+}
+
+static void setup(void)
+{
+	fd = SAFE_OPEN(REG_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+	SAFE_CLOSE(fd);
+
+	SAFE_MKDIR(DIR_FILE, 0755);
+	SAFE_SYMLINK(REG_FILE, SYMLINK);
+
+	SAFE_MKNOD(FIFO_FILE, S_IFIFO | 0777, 0);
+	SAFE_MKNOD(CHR_DEV, S_IFCHR | 0777, makedev(1, 3));
+	SAFE_MKNOD(BLK_DEV, S_IFBLK | 0777, makedev(7, 3));
+}
+
+static void cleanup(void)
+{
+	if (!access(SYMLINK, F_OK))
+		SAFE_UNLINK(SYMLINK);
+
+	if (!access(REG_FILE, F_OK))
+		SAFE_UNLINK(REG_FILE);
+
+	if (!access(DIR_FILE,  F_OK))
+		SAFE_RMDIR(DIR_FILE);
+
+	if (!access(FIFO_FILE, F_OK))
+		SAFE_UNLINK(FIFO_FILE);
+
+	if (!access(CHR_DEV, F_OK))
+		SAFE_UNLINK(CHR_DEV);
+
+	if (!access(BLK_DEV, F_OK))
+		SAFE_UNLINK(BLK_DEV);
+}
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_fstat,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
--
2.43.0


More information about the ltp mailing list