[LTP] [PATCH v10 3/3] fstat: add test for multiple file types using fstat
Jinseok Kim <[email protected]> Wed, 29 Jul 2026 23:32:03 +0900
| Newsgroups | it.linux.lists.ltp |
|---|---|
| Message-ID | <[email protected]> |
Following review feedback, create a dedicated fstat test that verifies that fstat() correctly identifies regular files, directories, FIFOs, character devices, and block devices. Signed-off-by: Jinseok Kim <[email protected]> --- Changes in v10: - Document why root privileges are required. - Replace the S_IS*() switch with a single S_IFMT comparison. - Link to v9: https://lore.kernel.org/ltp/[email protected] Changes in v9: - Open test files with O_PATH to avoid unnecessary device access - Link to v8: https://lore.kernel.org/ltp/[email protected] Changes in v8: - Link to the man page - Remove desc from tcase and TST_EXP_EXPR - Link to v7: https://lore.kernel.org/ltp/[email protected] --- runtest/syscalls | 2 + testcases/kernel/syscalls/fstat/.gitignore | 2 + testcases/kernel/syscalls/fstat/fstat04.c | 94 ++++++++++++++++++++++ 3 files changed, 98 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..c9a83989b --- /dev/null +++ b/testcases/kernel/syscalls/fstat/fstat04.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2026 Jinseok Kim <[email protected]> + */ +/*\ + * Verify that :manpage:`fstat(2)` correctly identifies various + * file types. + * + * Root privileges are required to create character and block device nodes. + */ +#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; + int fd; + + if (tc->exp_type == S_IFDIR) + flags |= O_DIRECTORY; + + fd = SAFE_OPEN(tc->path, flags); + + SAFE_FSTAT(fd, &buf); + SAFE_CLOSE(fd); + + TST_EXP_EXPR((buf.st_mode & S_IFMT) == tc->exp_type, "checking %s", tc->path); +} + +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