* [LTP] [PATCH v9 2/2] fstat: add test for multiple file types using fstat
2026-06-21 9:03 [LTP] [PATCH v9 1/2] open: remove O_DIRECTORY case (move to fstat test) Jinseok Kim
@ 2026-06-21 9:04 ` Jinseok Kim
2026-06-21 10:08 ` [LTP] open: remove O_DIRECTORY case (move to fstat test) linuxtestproject.agent
1 sibling, 0 replies; 3+ messages in thread
From: Jinseok Kim @ 2026-06-21 9:04 UTC (permalink / raw)
To: ltp
Following review feedback, create a dedicated fstat test that verifies
S_ISREG(), S_ISDIR(), S_ISFIFO(), S_ISCHR(), S_ISBLK() on different file
types.
Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
Changes in v9:
- Open test files with O_PATH to avoid unnecessary device access
- Link to v8: https://lore.kernel.org/ltp/20260604142029.2077-2-always.starving0@gmail.com
Changes in v8:
- Link to the man page
- Remove desc from tcase and TST_EXP_EXPR
- Link to v7: https://lore.kernel.org/ltp/20260327152751.3637-1-always.starving0@gmail.com
---
runtest/syscalls | 2 +
testcases/kernel/syscalls/fstat/.gitignore | 2 +
testcases/kernel/syscalls/fstat/fstat04.c | 107 +++++++++++++++++++++
3 files changed, 111 insertions(+)
create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 43951df2a..a38743ded 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -436,6 +436,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..7147aa61f
--- /dev/null
+++ b/testcases/kernel/syscalls/fstat/fstat04.c
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <always.starving0@gmail.com>
+ */
+/*\
+ * Verify that :manpage:`fstat(2)` correctly identifies various
+ * file types.
+ */
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <fcntl.h>
+
+#include "tst_test.h"
+
+#define MOUNT_PATH "test_mnt"
+#define REG_FILE "regfile"
+#define DIR_FILE "dirfile"
+#define FIFO_FILE "fifofile"
+#define CHR_DEV MOUNT_PATH "/chrdev"
+#define BLK_DEV MOUNT_PATH "/blkdev"
+
+static struct tcase {
+ const char *path;
+ mode_t exp_type;
+} tcases[] = {
+ { REG_FILE, S_IFREG },
+ { DIR_FILE, S_IFDIR },
+ { FIFO_FILE, S_IFIFO },
+ { CHR_DEV, S_IFCHR },
+ { BLK_DEV, S_IFBLK },
+};
+
+static void verify_fstat(unsigned int i)
+{
+ struct tcase *tc = &tcases[i];
+ struct stat buf;
+
+ int flags = O_PATH;
+
+ if (tc->exp_type == S_IFDIR)
+ flags |= O_DIRECTORY;
+
+ int fd = SAFE_OPEN(tc->path, flags);
+
+ SAFE_FSTAT(fd, &buf);
+ SAFE_CLOSE(fd);
+
+ switch (tc->exp_type) {
+ case S_IFREG:
+ TST_EXP_EXPR(S_ISREG(buf.st_mode));
+ break;
+ case S_IFDIR:
+ TST_EXP_EXPR(S_ISDIR(buf.st_mode));
+ break;
+ case S_IFIFO:
+ TST_EXP_EXPR(S_ISFIFO(buf.st_mode));
+ break;
+ case S_IFCHR:
+ TST_EXP_EXPR(S_ISCHR(buf.st_mode));
+ break;
+ case S_IFBLK:
+ TST_EXP_EXPR(S_ISBLK(buf.st_mode));
+ break;
+ }
+}
+
+static void setup(void)
+{
+ SAFE_TOUCH(REG_FILE, 0644, NULL);
+ SAFE_MKDIR(DIR_FILE, 0755);
+
+ 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(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,
+ .mntpoint = MOUNT_PATH,
+ .needs_devfs = 1,
+ .needs_tmpdir = 1,
+ .needs_root = 1,
+};
--
2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [LTP] open: remove O_DIRECTORY case (move to fstat test)
2026-06-21 9:03 [LTP] [PATCH v9 1/2] open: remove O_DIRECTORY case (move to fstat test) Jinseok Kim
2026-06-21 9:04 ` [LTP] [PATCH v9 2/2] fstat: add test for multiple file types using fstat Jinseok Kim
@ 2026-06-21 10:08 ` linuxtestproject.agent
1 sibling, 0 replies; 3+ messages in thread
From: linuxtestproject.agent @ 2026-06-21 10:08 UTC (permalink / raw)
To: Jinseok Kim; +Cc: ltp
Hi Jinseok,
On Sun, 21 Jun 2026 18:03:59 +0900, Jinseok Kim wrote:
> open: remove O_DIRECTORY case (move to fstat test)
--- [PATCH 2/2] ---
> +/*\
> + * Verify that :manpage:`fstat(2)` correctly identifies various
> + * file types.
> + */
The test uses .needs_root = 1 for mknod() to create device nodes, but
the doc comment does not mention why root is required.
> + switch (tc->exp_type) {
> + case S_IFREG:
> + TST_EXP_EXPR(S_ISREG(buf.st_mode));
> + break;
> + case S_IFDIR:
> + TST_EXP_EXPR(S_ISDIR(buf.st_mode));
> + break;
> + case S_IFIFO:
> + TST_EXP_EXPR(S_ISFIFO(buf.st_mode));
> + break;
> + case S_IFCHR:
> + TST_EXP_EXPR(S_ISCHR(buf.st_mode));
> + break;
> + case S_IFBLK:
> + TST_EXP_EXPR(S_ISBLK(buf.st_mode));
> + break;
> + }
Each S_IS*() macro is equivalent to (st_mode & S_IFMT) == S_IF*, and
tc->exp_type already holds the expected S_IF* value. Could the whole
switch be replaced with a single check?
TST_EXP_EXPR((buf.st_mode & S_IFMT) == tc->exp_type,
"fstat on %s", tc->path);
This would also give more informative output on failure by
identifying the file being tested.
Verdict - Needs revision
Pre-existing issues in open01.c (not introduced by this series):
- static int fd; should be static int fd = -1;
- cleanup() uses if (fd > 0) which should be if (fd != -1)
---
Note:
The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 3+ messages in thread