Skip to contentSkip to conversation

open: fix directory verification and misleading test description

PATCH In review Jinseok Kim 4 participants 31 messages Feb 11 – Apr 21

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 open: fix directory verification and misleading test description

The directory test treated S_IFDIR as a permission bit and verified it using a generic st_mode bitwise check, which is not appropriate for file type verification.

Use S_ISDIR() when verifying directories and keep bitwise checks only for permission bits such as S_ISVTX.

The directory test case also referred to a "directory bit", which is misleading since directories are identified by file type rather than a permission bit.

---
 testcases/kernel/syscalls/open/open01.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/open/open01.c +12 -6
index baf73ab11..1355592e1 100644
@@ -37,7 +37,7 @@ static struct tcase {
 	char *desc;
 } tcases[] = {
 	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
-	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"}
+	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "directory"}
 };
 
 static void verify_open(unsigned int n)
@@ -53,11 +53,17 @@ static void verify_open(unsigned int n)
 	fd = TST_RET;
 
 	SAFE_FSTAT(fd, &buf);
-	if (!(buf.st_mode & tc->tst_bit))
-		tst_res(TFAIL, "%s is cleared unexpectedly", tc->desc);
-	else
-		tst_res(TPASS, "%s is set as expected", tc->desc);
-
+	if (tc->tst_bit == S_ISVTX) {
+		if (!(buf.st_mode & S_ISVTX))
+			tst_res(TFAIL, "%s is cleared unexpectedly", tc->desc);
+		else
+			tst_res(TPASS, "%s is set as expected", tc->desc);
+	} else if (tc->tst_bit == S_IFDIR) {
+		if (!S_ISDIR(buf.st_mode))
+			tst_res(TFAIL, "%s is not a directory", tc->desc);
+		else
+			tst_res(TPASS, "%s is a directory", tc->desc);
+	}
 	SAFE_CLOSE(fd);
 	if (S_ISREG(buf.st_mode))
 		SAFE_UNLINK(tc->filename);

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Jinseok Kim

Hi!

Quoted 23 lines
testcases/kernel/syscalls/open/open01.c +2 -6
index baf73ab11..1355592e1 100644
@@ -37,7 +37,7 @@ static struct tcase {
 	char *desc;
 } tcases[] = {
 	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
-	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"}
+	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "directory"}
 };
 
 static void verify_open(unsigned int n)
@@ -53,11 +53,17 @@ static void verify_open(unsigned int n)
 	fd = TST_RET;
 
 	SAFE_FSTAT(fd, &buf);
-	if (!(buf.st_mode & tc->tst_bit))
-		tst_res(TFAIL, "%s is cleared unexpectedly", tc->desc);
-	else
-		tst_res(TPASS, "%s is set as expected", tc->desc);
-
+	if (tc->tst_bit == S_ISVTX) {

testcases/kernel/syscalls/open/open01.c static void verify_open(unsigned int n) · line 66

Quoted 10 lines
testcases/kernel/syscalls/open/open01.c +10 -0
+		if (!(buf.st_mode & S_ISVTX))
+			tst_res(TFAIL, "%s is cleared unexpectedly", tc->desc);
+		else
+			tst_res(TPASS, "%s is set as expected", tc->desc);
+	} else if (tc->tst_bit == S_IFDIR) {
+		if (!S_ISDIR(buf.st_mode))
+			tst_res(TFAIL, "%s is not a directory", tc->desc);
+		else
+			tst_res(TPASS, "%s is a directory", tc->desc);
+	}

Hmm, I guess that the check for S_ISDIR() is technically testing fstat() since unlike the sticky bit case the directory is created in the test setup.

I guess the cleaniest solutiont may be to add fstat test that would check all kinds of modes. Looking at the headers there are at least:

- S_ISDIR
- S_ISCHR
- S_ISBLK
- S_ISREG
- S_ISFIFO
- S_ISLNK

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 1/1 open: fix directory verification and misleading test description

Hi, Hrubis

Thanks for the review.

I checked fstat/:

None of the existing fstat tests actually verify file type bits as you suggested.

I think the cleanest solution is to introduce a new test fstat04.c that creates different file types (regular, directory, character, etc.) and explicitly checks S_ISREG(), S_ISDIR(), S_ISCHR() and so on.

How do you think about removing the O_DIRECTORY case from open01.c and creating a separate fstat test?

Thanks again,
Jinseok.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hi!

Quoted 2 lines

How do you think about removing the O_DIRECTORY case from open01.c and creating a separate fstat test?

Yes please.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH 1/2 open: remove O_DIRECTORY case (move to fstat test)

The O_DIRECTORY test case was mostly verifying fstat() behavior on an existing directory rather than testing any specific open() behavior.

Following review suggestion, remove it from open01.c. A separate fstat test will be added in a follow-up patch to cover file type checks (S_ISDIR, etc.) more comprehensively.

---
 testcases/kernel/syscalls/open/open01.c | 6 ------
 1 file changed, 6 deletions(-)
testcases/kernel/syscalls/open/open01.c +0 -6
index baf73ab11..801859e68 100644
@@ -8,16 +8,12 @@
 /*
  * DESCRIPTION
  *	Open a file with oflag = O_CREAT set, does it set the sticky bit off?
- *	Open a dir with O_DIRECTORY, does it set the S_IFDIR bit on?
  *
  * ALGORITHM
  *	1. open a new file with O_CREAT, fstat.st_mode should not have the
  *	   01000 bit on. In Linux, the save text bit is *NOT* cleared.
- *	2. open a new dir with O_DIRECTORY, fstat.st_mode should have the
- *	   040000 bit on.
  */
 
-#define _GNU_SOURCE		/* for O_DIRECTORY */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -25,7 +21,6 @@
 #include "tst_test.h"
 
 #define TEST_FILE	"testfile"
-#define TEST_DIR	"testdir"
 
 static int fd;
 
@@ -37,7 +32,6 @@ static struct tcase {
 	char *desc;
 } tcases[] = {
 	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
-	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"}
 };
 
 static void verify_open(unsigned int n)

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

PATCH 2/2 Add test for multiple file types using fstat

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.

---
 testcases/kernel/syscalls/fstat/fstat04.c | 103 ++++++++++++++++++++++
 1 file changed, 103 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
testcases/kernel/syscalls/fstat/fstat04.c new file +103 -0
new file mode 100644
index 000000000..6aa9a8e79
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+/*\
+ * Verify that fstat correctly identifies various file types
+ */
+
+#define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.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)
+{
+    SAFE_UNLINK(SYMLINK);
+    SAFE_UNLINK(REG_FILE);
+    SAFE_RMDIR(DIR_FILE);
+
+    SAFE_UNLINK(FIFO_FILE);
+    SAFE_UNLINK(CHR_DEV);
+    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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH 1/2] open: remove O_DIRECTORY case (move to fstat test)

In reply to Jinseok Kim

Hi!

The patch is not compiling. Please verify that patch is applicable before sending it:

https://patchwork.ozlabs.org/project/ltp/list/?series=493107&state=*
https://linux-test-project.readthedocs.io/en/latest/developers/writing_tests.html#testing-builds-with-github-actions

Signature · 7 lines
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

PATCH v2 1/2 open: remove O_DIRECTORY case (move to fstat test)

The O_DIRECTORY test case was mostly verifying fstat() behavior on an existing directory rather than testing any specific open() behavior.

Following review suggestion, remove it from open01.c. A separate fstat test will be added in a follow-up patch to cover file type checks (S_ISDIR, etc.) more comprehensively.

---
 testcases/kernel/syscalls/open/open01.c | 12 ------------
 1 file changed, 12 deletions(-)
testcases/kernel/syscalls/open/open01.c +0 -12
index baf73ab11..329c52586 100644
@@ -8,16 +8,12 @@
 /*
  * DESCRIPTION
  *	Open a file with oflag = O_CREAT set, does it set the sticky bit off?
- *	Open a dir with O_DIRECTORY, does it set the S_IFDIR bit on?
  *
  * ALGORITHM
  *	1. open a new file with O_CREAT, fstat.st_mode should not have the
  *	   01000 bit on. In Linux, the save text bit is *NOT* cleared.
- *	2. open a new dir with O_DIRECTORY, fstat.st_mode should have the
- *	   040000 bit on.
  */
 
-#define _GNU_SOURCE		/* for O_DIRECTORY */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -25,7 +21,6 @@
 #include "tst_test.h"
 
 #define TEST_FILE	"testfile"
-#define TEST_DIR	"testdir"
 
 static int fd;
 
@@ -37,7 +32,6 @@ static struct tcase {
 	char *desc;
 } tcases[] = {
 	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
-	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"}
 };
 
 static void verify_open(unsigned int n)
@@ -63,11 +57,6 @@ static void verify_open(unsigned int n)
 		SAFE_UNLINK(tc->filename);
 }
 
-static void setup(void)
-{
-	SAFE_MKDIR(TEST_DIR, 0755);
-}
-
 static void cleanup(void)
 {
 	if (fd > 0)
@@ -77,7 +66,6 @@ static void cleanup(void)
 static struct tst_test test = {
 	.tcnt = ARRAY_SIZE(tcases),
 	.needs_tmpdir = 1,
-	.setup = setup,
 	.cleanup = cleanup,
 	.test = verify_open,
 };

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

PATCH v2 2/2 fstat: add test for multiple file types using fstat

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.

---
 testcases/kernel/syscalls/fstat/fstat04.c | 103 ++++++++++++++++++++++
 1 file changed, 103 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
testcases/kernel/syscalls/fstat/fstat04.c new file +103 -0
new file mode 100644
index 000000000..6aa9a8e79
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+/*\
+ * Verify that fstat correctly identifies various file types
+ */
+
+#define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.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)
+{
+    SAFE_UNLINK(SYMLINK);
+    SAFE_UNLINK(REG_FILE);
+    SAFE_RMDIR(DIR_FILE);
+
+    SAFE_UNLINK(FIFO_FILE);
+    SAFE_UNLINK(CHR_DEV);
+    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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

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

Hi!

Let's fix the following issues first.

Quoted 21 lines · Jinseok Kim

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.

---
 testcases/kernel/syscalls/fstat/fstat04.c | 103 ++++++++++++++++++++++
 1 file changed, 103 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
testcases/kernel/syscalls/fstat/fstat04.c new file +5 -0
new file mode 100644
index 000000000..6aa9a8e79
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+/*\

testcases/kernel/syscalls/fstat/fstat04.c line 15

Quoted 10 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -0
+ * Verify that fstat correctly identifies various file types
+ */
+
+#define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>

More likely these imports are not stricly needed. Please take a look at it. Also _GNU_SOURCE is not needed.

Quoted 68 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +68 -0
+#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));
+}

testcases/kernel/syscalls/fstat/fstat04.c line 93

Quoted 10 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -0
+
+static void cleanup(void)
+{
+    SAFE_UNLINK(SYMLINK);
+    SAFE_UNLINK(REG_FILE);
+    SAFE_RMDIR(DIR_FILE);
+
+    SAFE_UNLINK(FIFO_FILE);
+    SAFE_UNLINK(CHR_DEV);
+    SAFE_UNLINK(BLK_DEV);

What if all these files/directories don't exist at cleanup?

Quoted 11 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -1
+}
+
+static struct tst_test test = {
+    .tcnt = ARRAY_SIZE(tcases),
+    .setup = setup,
+    .cleanup = cleanup,
+    .test = verify_fstat,
+    .needs_tmpdir = 1,
+    .needs_root = 1,
+};
--
Quoted 1 line

2.43.0

I also get:

fstat04.c:61: TPASS: regular file: S_IFREG() macro matches fstat04.c:61: TPASS: directory: S_IFDIR() macro matches fstat04.c:61: TPASS: FIFO (pipe): S_IFIFO() macro matches fstat04.c:63: TFAIL: symbolic link: S_IFLNK() macro does NOT match fstat04.c:45: TBROK: open(chrdev,2048,0000) failed: EACCES (13)

Please, also fix the following:

open01.c has 1 checkpatch error:
- Line 43: Code indent should use tabs where possible

fstat04.c has 9 checkpatch errors and 53 warnings:
- Lines 28-93: Uses spaces instead of tabs for indentation throughout
- Lines 58-63: Trailing statements should be on next line in switch cases
- Line 57: Missing blank line after declarations
- Line 49: Code indent should use tabs

- Add fstat04 to .gitignore
- Add fstat04 fstat04 to runtest/syscalls

Signature · 7 lines
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

PATCH v3 1/2 open: remove O_DIRECTORY case (move to fstat test)

The O_DIRECTORY test case was mostly verifying fstat() behavior on an existing directory rather than testing any specific open() behavior.

Following review suggestion, remove it from open01.c. A separate fstat test will be added in a follow-up patch to cover file type checks (S_ISDIR, etc.) more comprehensively.

---
 testcases/kernel/syscalls/open/open01.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
testcases/kernel/syscalls/open/open01.c +1 -13
index baf73ab11..3ae1b269b 100644
@@ -8,16 +8,12 @@
 /*
  * DESCRIPTION
  *	Open a file with oflag = O_CREAT set, does it set the sticky bit off?
- *	Open a dir with O_DIRECTORY, does it set the S_IFDIR bit on?
  *
  * ALGORITHM
  *	1. open a new file with O_CREAT, fstat.st_mode should not have the
  *	   01000 bit on. In Linux, the save text bit is *NOT* cleared.
- *	2. open a new dir with O_DIRECTORY, fstat.st_mode should have the
- *	   040000 bit on.
  */
 
-#define _GNU_SOURCE		/* for O_DIRECTORY */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -25,7 +21,6 @@
 #include "tst_test.h"
 
 #define TEST_FILE	"testfile"
-#define TEST_DIR	"testdir"
 
 static int fd;
 
@@ -37,7 +32,6 @@ static struct tcase {
 	char *desc;
 } tcases[] = {
 	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
-	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"}
 };
 
 static void verify_open(unsigned int n)
@@ -46,7 +40,7 @@ static void verify_open(unsigned int n)
 	struct stat buf;
 
 	TST_EXP_FD_SILENT(open(tc->filename, tc->flag, tc->mode),
-	           "open() with %s", tc->desc);
+				"open() with %s", tc->desc);
 	if (!TST_PASS)
 		return;
 
@@ -63,11 +57,6 @@ static void verify_open(unsigned int n)
 		SAFE_UNLINK(tc->filename);
 }
 
-static void setup(void)
-{
-	SAFE_MKDIR(TEST_DIR, 0755);
-}
-
 static void cleanup(void)
 {
 	if (fd > 0)
@@ -77,7 +66,6 @@ static void cleanup(void)
 static struct tst_test test = {
 	.tcnt = ARRAY_SIZE(tcases),
 	.needs_tmpdir = 1,
-	.setup = setup,
 	.cleanup = cleanup,
 	.test = verify_open,
 };

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

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

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.

---
 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
runtest/syscalls +2 -0
index 05bb3ceb1..5b34e6bdc 100644
@@ -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
testcases/kernel/syscalls/fstat/.gitignore +2 -0
index 9b1089438..a1f538f7e 100644
@@ -2,3 +2,5 @@
 /fstat02_64
 /fstat03
 /fstat03_64
+/fstat04
+/fstat04_64
testcases/kernel/syscalls/fstat/fstat04.c new file +121 -0

121 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

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

Hi!

testcases/kernel/syscalls/fstat/fstat04.c line 33

Quoted 13 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +13 -0
+
+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 },

Makes more sense to merge desc and macro_name together. Then see below:

Quoted 6 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +6 -0
+};
+
+static void verify_fstat(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+	struct stat buf;

testcases/kernel/syscalls/fstat/fstat04.c line 49

Quoted 10 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -0
+
+	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);

You can just use:

	SAFE_LSTAT(tc->path, &buf);

Otherwise test will fail with:

fstat04.c:74: TPASS: Expect: regular file: macro matches fstat04.c:74: TPASS: Expect: directory: macro matches fstat04.c:74: TPASS: Expect: FIFO (pipe): macro matches fstat04.c:74: TFAIL: Expect: symbolic link: macro matches fstat04.c:46: TBROK: open(chrdev,2048,0000) failed: EACCES (13)

Quoted 18 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +18 -0
+
+	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);

testcases/kernel/syscalls/fstat/fstat04.c line 77

Quoted 10 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -0
+		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);

We don't need an if statement here. We can just:

	TST_EXP_EXPR(is_correct, "%s macro matches", tc->desc);

testcases/kernel/syscalls/fstat/fstat04.c line 83

Quoted 4 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +4 -0
+static void setup(void)
+{
+        fd = SAFE_OPEN(REG_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+        SAFE_CLOSE(fd);

Just use SAFE_TOUCH() in here, so fd can be removed also from the static variables.

Signature · 7 lines
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw

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

Hi,

Thank you for the review.

If we switch to using lstat() only, this test would no longer exercise fstat() at all, which changes the original intent of the test.

In that case, should we consider renaming the test from fstat to lstat?

Jinseok.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#15 lore raw

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

Hi,

Quoted 10 lines · Jinseok Kim

Hi,

Thank you for the review.

If we switch to using lstat() only, this test would no longer exercise fstat() at all, which changes the original intent of the test.

In that case, should we consider renaming the test from fstat to lstat?

Jinseok.

Sorry for the confusion...of course. The point is if we want to test inode macros or fstat(), and of course we want to test fstat(). According to that, we should correctly use the syscall. For instance, when using fstat():

fstat04.c:63: TFAIL: symbolic link: S_IFLNK() macro does NOT match fstat04.c:45: TBROK: open(chrdev,2048,0000) failed: EACCES (13)

And this has to be fixed of course.

Signature · 7 lines
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#16 lore raw

PATCH v4 1/2 open: remove O_DIRECTORY case (move to fstat test)

The O_DIRECTORY test case was mostly verifying fstat() behavior on an existing directory rather than testing any specific open() behavior.

Following review suggestion, remove it from open01.c. A separate fstat test will be added in a follow-up patch to cover file type checks (S_ISDIR, etc.) more comprehensively.

---
 testcases/kernel/syscalls/open/open01.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
testcases/kernel/syscalls/open/open01.c +1 -13
index baf73ab11..3ae1b269b 100644
@@ -8,16 +8,12 @@
 /*
  * DESCRIPTION
  *	Open a file with oflag = O_CREAT set, does it set the sticky bit off?
- *	Open a dir with O_DIRECTORY, does it set the S_IFDIR bit on?
  *
  * ALGORITHM
  *	1. open a new file with O_CREAT, fstat.st_mode should not have the
  *	   01000 bit on. In Linux, the save text bit is *NOT* cleared.
- *	2. open a new dir with O_DIRECTORY, fstat.st_mode should have the
- *	   040000 bit on.
  */
 
-#define _GNU_SOURCE		/* for O_DIRECTORY */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -25,7 +21,6 @@
 #include "tst_test.h"
 
 #define TEST_FILE	"testfile"
-#define TEST_DIR	"testdir"
 
 static int fd;
 
@@ -37,7 +32,6 @@ static struct tcase {
 	char *desc;
 } tcases[] = {
 	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
-	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"}
 };
 
 static void verify_open(unsigned int n)
@@ -46,7 +40,7 @@ static void verify_open(unsigned int n)
 	struct stat buf;
 
 	TST_EXP_FD_SILENT(open(tc->filename, tc->flag, tc->mode),
-	           "open() with %s", tc->desc);
+				"open() with %s", tc->desc);
 	if (!TST_PASS)
 		return;
 
@@ -63,11 +57,6 @@ static void verify_open(unsigned int n)
 		SAFE_UNLINK(tc->filename);
 }
 
-static void setup(void)
-{
-	SAFE_MKDIR(TEST_DIR, 0755);
-}
-
 static void cleanup(void)
 {
 	if (fd > 0)
@@ -77,7 +66,6 @@ static void cleanup(void)
 static struct tst_test test = {
 	.tcnt = ARRAY_SIZE(tcases),
 	.needs_tmpdir = 1,
-	.setup = setup,
 	.cleanup = cleanup,
 	.test = verify_open,
 };

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#17 lore raw

PATCH v4 2/2 fstat: add test for multiple file types using fstat

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.

---
 runtest/syscalls                           |   2 +
 testcases/kernel/syscalls/fstat/.gitignore |   2 +
 testcases/kernel/syscalls/fstat/fstat04.c  | 119 +++++++++++++++++++++
 3 files changed, 123 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
runtest/syscalls +2 -0
index 05bb3ceb1..5b34e6bdc 100644
@@ -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
testcases/kernel/syscalls/fstat/.gitignore +2 -0
index 9b1089438..a1f538f7e 100644
@@ -2,3 +2,5 @@
 /fstat02_64
 /fstat03
 /fstat03_64
+/fstat04
+/fstat04_64
testcases/kernel/syscalls/fstat/fstat04.c new file +119 -0

119 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#18 lore raw

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

Hi!

Quoted 95 lines

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.

---
 runtest/syscalls                           |   2 +
 testcases/kernel/syscalls/fstat/.gitignore |   2 +
 testcases/kernel/syscalls/fstat/fstat04.c  | 119 +++++++++++++++++++++
 3 files changed, 123 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
runtest/syscalls +2 -0
index 05bb3ceb1..5b34e6bdc 100644
@@ -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
testcases/kernel/syscalls/fstat/.gitignore +2 -0
index 9b1089438..a1f538f7e 100644
@@ -2,3 +2,5 @@
 /fstat02_64
 /fstat03
 /fstat03_64
+/fstat04
+/fstat04_64
testcases/kernel/syscalls/fstat/fstat04.c new file +54 -0
new file mode 100644
index 000000000..a7f2302f6
@@ -0,0 +1,119 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+/*\
+ * 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 struct tcase {
+	const char *desc;
+	const char *path;
+	mode_t exp_type;
+} tcases[] = {
+	{ "S_IFREG",	REG_FILE,	S_IFREG },
+	{ "S_IFDIR",	DIR_FILE,	S_IFDIR },
+	{ "S_IFIFO",	FIFO_FILE,	S_IFIFO },
+	{ "S_IFLNK",	SYMLINK,	S_IFLNK },
+	{ "S_IFCHR",	CHR_DEV,	S_IFCHR },
+	{ "S_IFBLK",	BLK_DEV,	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;
+
+	int fd = open(tc->path, flags);
+
+	if (fd < 0) {
+		tst_res(TCONF | TERRNO, "cannot open %s, skipping", tc->desc);
+		return;
+	}
+
+	SAFE_FSTAT(fd, &buf);
+	SAFE_CLOSE(fd);
+
+	switch (tc->exp_type) {
+	case S_IFREG:

testcases/kernel/syscalls/fstat/fstat04.c line 64

Quoted 10 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -0
+		TST_EXP_EXPR(S_ISREG(buf.st_mode), "%s macro matches", tc->desc);
+		break;
+	case S_IFDIR:
+		TST_EXP_EXPR(S_ISDIR(buf.st_mode), "%s macro matches", tc->desc);
+		break;
+	case S_IFIFO:
+		TST_EXP_EXPR(S_ISFIFO(buf.st_mode), "%s macro matches", tc->desc);
+		break;
+	case S_IFLNK:
+		tst_res(TCONF, "Symbolic Link not available in this environment");

What's the point of always sending TCONF?

testcases/kernel/syscalls/fstat/fstat04.c line 73

Quoted 9 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +9 -0
+		break;
+	case S_IFCHR:
+		TST_EXP_EXPR(S_ISCHR(buf.st_mode), "%s macro matches", tc->desc);
+		break;
+	case S_IFBLK:
+		if (S_ISBLK(buf.st_mode))
+			tst_res(TPASS, "%s macro matches", tc->desc);
+		else
+			tst_res(TCONF, "Block device not available in this environment");

And here as well we can use TST_EXP_EXPR()

Quoted 47 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +46 -1
+		break;
+	}
+}
+
+static void setup(void)
+{
+	SAFE_TOUCH(REG_FILE, 0644, NULL);
+
+	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,
+};
+
--
Quoted 1 line

2.43.0

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#19 lore raw

PATCH v4 2/2 fstat: add test for multiple file types using fstat

Hi,

fstat() follows symbolic links and reports the type of the target file, so it cannot be used to detect S_IFLNK.

Would it be better to drop the symlink testcase from this test?

Jinseok.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#20 lore raw

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

Hi!

Quoted 8 lines

Hi,

fstat() follows symbolic links and reports the type of the target file, so it cannot be used to detect S_IFLNK.

Would it be better to drop the symlink testcase from this test?

Jinseok.

It doesn't make sense to add a test for a feature that we already know it can't be tested, so yes, it makes sense to remove this testcase.

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#21 lore raw

PATCH v5 1/2 open: remove O_DIRECTORY case (move to fstat test)

The O_DIRECTORY test case was mostly verifying fstat() behavior on an existing directory rather than testing any specific open() behavior.

Following review suggestion, remove it from open01.c. A separate fstat test will be added in a follow-up patch to cover file type checks (S_ISDIR, etc.) more comprehensively.

---
 testcases/kernel/syscalls/open/open01.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
testcases/kernel/syscalls/open/open01.c +1 -13
index baf73ab11..3ae1b269b 100644
@@ -8,16 +8,12 @@
 /*
  * DESCRIPTION
  *	Open a file with oflag = O_CREAT set, does it set the sticky bit off?
- *	Open a dir with O_DIRECTORY, does it set the S_IFDIR bit on?
  *
  * ALGORITHM
  *	1. open a new file with O_CREAT, fstat.st_mode should not have the
  *	   01000 bit on. In Linux, the save text bit is *NOT* cleared.
- *	2. open a new dir with O_DIRECTORY, fstat.st_mode should have the
- *	   040000 bit on.
  */
 
-#define _GNU_SOURCE		/* for O_DIRECTORY */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -25,7 +21,6 @@
 #include "tst_test.h"
 
 #define TEST_FILE	"testfile"
-#define TEST_DIR	"testdir"
 
 static int fd;
 
@@ -37,7 +32,6 @@ static struct tcase {
 	char *desc;
 } tcases[] = {
 	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
-	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"}
 };
 
 static void verify_open(unsigned int n)
@@ -46,7 +40,7 @@ static void verify_open(unsigned int n)
 	struct stat buf;
 
 	TST_EXP_FD_SILENT(open(tc->filename, tc->flag, tc->mode),
-	           "open() with %s", tc->desc);
+				"open() with %s", tc->desc);
 	if (!TST_PASS)
 		return;
 
@@ -63,11 +57,6 @@ static void verify_open(unsigned int n)
 		SAFE_UNLINK(tc->filename);
 }
 
-static void setup(void)
-{
-	SAFE_MKDIR(TEST_DIR, 0755);
-}
-
 static void cleanup(void)
 {
 	if (fd > 0)
@@ -77,7 +66,6 @@ static void cleanup(void)
 static struct tst_test test = {
 	.tcnt = ARRAY_SIZE(tcases),
 	.needs_tmpdir = 1,
-	.setup = setup,
 	.cleanup = cleanup,
 	.test = verify_open,
 };

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#22 lore raw

PATCH v5 2/2 fstat: add test for multiple file types using fstat

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

---
 runtest/syscalls                           |   2 +
 testcases/kernel/syscalls/fstat/.gitignore |   2 +
 testcases/kernel/syscalls/fstat/fstat04.c  | 106 +++++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
runtest/syscalls +2 -0
index 05bb3ceb1..5b34e6bdc 100644
@@ -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
testcases/kernel/syscalls/fstat/.gitignore +2 -0
index 9b1089438..a1f538f7e 100644
@@ -2,3 +2,5 @@
 /fstat02_64
 /fstat03
 /fstat03_64
+/fstat04
+/fstat04_64
testcases/kernel/syscalls/fstat/fstat04.c new file +106 -0

106 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#24 lore raw

PATCH v6 1/2 open: remove O_DIRECTORY case (move to fstat test)

The O_DIRECTORY test case was mostly verifying fstat() behavior on an existing directory rather than testing any specific open() behavior.

Following review suggestion, remove it from open01.c. A separate fstat test will be added in a follow-up patch to cover file type checks (S_ISDIR, etc.) more comprehensively.

---
 testcases/kernel/syscalls/open/open01.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
testcases/kernel/syscalls/open/open01.c +1 -12
index 1cefb4790..1413ccdec 100644
@@ -6,15 +6,12 @@
  */
 
 /*\
- * Basic :manpage:`open(2)` test, checking sticky/directory bit.
+ * Basic :manpage:`open(2)` test, checking sticky bit.
  *
  * 1. Open a new file with O_CREAT, fstat.st_mode should not have the
  *    01000 (S_ISVTX) bit on. In Linux, the save text bit is *NOT* cleared.
- * 2. Open a new directory with O_DIRECTORY, fstat.st_mode should have the
- *    040000 (S_IFDIR) bit on.
  */
 
-#define _GNU_SOURCE		/* for O_DIRECTORY */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -22,7 +19,6 @@
 #include "tst_test.h"
 
 #define TEST_FILE	"testfile"
-#define TEST_DIR	"testdir"
 
 static int fd;
 
@@ -34,7 +30,6 @@ static struct tcase {
 	char *desc;
 } tcases[] = {
 	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
-	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "directory bit"}
 };
 
 static void verify_open(unsigned int n)
@@ -60,11 +55,6 @@ static void verify_open(unsigned int n)
 		SAFE_UNLINK(tc->filename);
 }
 
-static void setup(void)
-{
-	SAFE_MKDIR(TEST_DIR, 0755);
-}
-
 static void cleanup(void)
 {
 	if (fd > 0)
@@ -74,7 +64,6 @@ static void cleanup(void)
 static struct tst_test test = {
 	.tcnt = ARRAY_SIZE(tcases),
 	.needs_tmpdir = 1,
-	.setup = setup,
 	.cleanup = cleanup,
 	.test = verify_open,
 };

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#25 lore raw

PATCH v6 2/2 fstat: add test for multiple file types using fstat

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

---
 runtest/syscalls                           |   2 +
 testcases/kernel/syscalls/fstat/.gitignore |   2 +
 testcases/kernel/syscalls/fstat/fstat04.c  | 105 +++++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
runtest/syscalls +2 -0
index 2179e007c..eadd921b8 100644
@@ -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
testcases/kernel/syscalls/fstat/.gitignore +2 -0
index 9b1089438..a1f538f7e 100644
@@ -2,3 +2,5 @@
 /fstat02_64
 /fstat03
 /fstat03_64
+/fstat04
+/fstat04_64
testcases/kernel/syscalls/fstat/fstat04.c new file +105 -0

105 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#26 lore raw

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

Hi Jinseok,

there's one thing to fix:

testcases/kernel/syscalls/fstat/fstat04.c line 46

Quoted 6 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +6 -0
+	int fd = open(tc->path, flags);
+
+	if (fd < 0) {
+		tst_res(TCONF | TERRNO, "cannot open %s, skipping", tc->desc);
+		return;
+	}

This must be SAFE_OPEN(). I guess your choice was related to the fact that open() is failing with EACCES when tc->exp_type is S_IFBLK or S_IFCHR. This happens because tmpfs is mounted with nodev. To enabled devs access, you need to specify a mount point and .needs_devfs = 1 inside the test struct definition. You can take fanotify22 test as a reference to understand how this works.

After achieving it, we can proceed with merge.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#27 lore raw

PATCH v7 1/1 fstat: add test for multiple file types using fstat

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

---
 runtest/syscalls                           |   2 +
 testcases/kernel/syscalls/fstat/.gitignore |   2 +
 testcases/kernel/syscalls/fstat/fstat04.c  | 103 +++++++++++++++++++++
 3 files changed, 107 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fstat/fstat04.c
runtest/syscalls +2 -0
index 6ba0227a8..5a2e8f048 100644
@@ -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
testcases/kernel/syscalls/fstat/.gitignore +2 -0
index 9b1089438..a1f538f7e 100644
@@ -2,3 +2,5 @@
 /fstat02_64
 /fstat03
 /fstat03_64
+/fstat04
+/fstat04_64
testcases/kernel/syscalls/fstat/fstat04.c new file +103 -0

103 lines not on this page · raw message

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#29 lore raw

Re: [LTP] [PATCH v7] fstat: add test for multiple file types using fstat

In reply to Jinseok Kim

Hi Jinseok,

Quoted 10 lines

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

testcases/kernel/syscalls/fstat/fstat04.c new file +0 -0
new file mode 100644
index 000000000..8a52de06a

testcases/kernel/syscalls/fstat/fstat04.c line 6

Quoted 6 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +6 -0
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+/*\
+ * Verify that fstat correctly identifies various file types

nit: Please use :manpage:fstat(2) instead of just fstat.
nit: missing dot at the end.

Quoted 9 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +9 -0
+ */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include "tst_test.h"
+
+#define MOUNT_PATH	"test_mnt"
+#define REG_FILE	"regfile"
+#define DIR_FILE	"dirfile"

testcases/kernel/syscalls/fstat/fstat04.c line 25

Quoted 10 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -0
+#define FIFO_FILE	"fifofile"
+#define CHR_DEV	MOUNT_PATH"/chrdev"
+#define BLK_DEV	MOUNT_PATH"/blkdev"
+
+static struct tcase {
+	const char *desc;
+	const char *path;
+	mode_t exp_type;
+} tcases[] = {
+	{ "S_IFREG",	REG_FILE,	S_IFREG },

FYI It's better to use macros to not repeat yourself ("S_IFDIR" and S_IFREG), together with designated initializers, e.g. what is being used in e.g. testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c.

#define DESC(x) .desc = #x, .exp_type = x

{ DESC(S_IFREG), .path = REG_FILE },

But I would prefer to drop desc entirely (useless see below).

testcases/kernel/syscalls/fstat/fstat04.c line 29

Quoted 4 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +4 -0
+	{ "S_IFDIR",	DIR_FILE,	S_IFDIR },
+	{ "S_IFIFO",	FIFO_FILE,	S_IFIFO },
+	{ "S_IFCHR",	CHR_DEV,	S_IFCHR },
+	{ "S_IFBLK",	BLK_DEV,	S_IFBLK },

How about others two? At least S_IFLNK (symlink) should be simple to add, I suppose S_IFSOCK (socket) as well.

Quoted 10 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -0
+};
+
+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)

testcases/kernel/syscalls/fstat/fstat04.c line 49

Quoted 10 lines
testcases/kernel/syscalls/fstat/fstat04.c new file +10 -0
+		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), "%s macro matches", tc->desc);
		TST_EXP_EXPR(S_ISREG(buf.st_mode));

Not using desc is IMHO good enough for debugging:
fstat04.c:49: TPASS: Expect: S_ISREG(buf.st_mode)

Original code did not have much of added value:

fstat04.c:49: TPASS: Expect: S_IFREG macro matches

The rest LGTM.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#30 lore raw

PATCH v7 1/1 fstat: add test for multiple file types using fstat

Hi,

Thanks for the review.

S_IFLNK is not suitable here since this test is based on fstat(), which follows symlinks. It is already covered by lstat tests (e.g. lstat01). This was also discussed here:
https://lore.kernel.org/ltp/[email protected]

S_IFSOCK is also excluded since filesystem sockets cannot be opened, so they cannot be verified via fstat().

I think both cases are better covered by lstat/stat based tests.

Thanks,
Jinseok

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp