All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v10 1/3] open01: fix cleanup file descriptor check
@ 2026-07-29 14:32 Jinseok Kim
  2026-07-29 14:32 ` [LTP] [PATCH v10 2/3] open01: remove O_DIRECTORY case (move to fstat test) Jinseok Kim
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jinseok Kim @ 2026-07-29 14:32 UTC (permalink / raw)
  To: ltp

Initialize fd to -1 and check it against -1 in cleanup().

File descriptor 0 is valid, so checking fd > 0 may skip closing an
successfully opened file.

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 testcases/kernel/syscalls/open/open01.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/open/open01.c b/testcases/kernel/syscalls/open/open01.c
index 1cefb4790..f1a3720aa 100644
--- a/testcases/kernel/syscalls/open/open01.c
+++ b/testcases/kernel/syscalls/open/open01.c
@@ -24,7 +24,7 @@
 #define TEST_FILE	"testfile"
 #define TEST_DIR	"testdir"

-static int fd;
+static int fd = -1;

 static struct tcase {
 	char *filename;
@@ -67,7 +67,7 @@ static void setup(void)

 static void cleanup(void)
 {
-	if (fd > 0)
+	if (fd != -1)
 		SAFE_CLOSE(fd);
 }

--
2.43.0

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [LTP] [PATCH v10 2/3] open01: remove O_DIRECTORY case (move to fstat test)
  2026-07-29 14:32 [LTP] [PATCH v10 1/3] open01: fix cleanup file descriptor check Jinseok Kim
@ 2026-07-29 14:32 ` Jinseok Kim
  2026-07-29 14:32 ` [LTP] [PATCH v10 3/3] fstat: add test for multiple file types using fstat Jinseok Kim
  2026-07-29 16:27 ` [LTP] open01: fix cleanup file descriptor check linuxtestproject.agent
  2 siblings, 0 replies; 4+ messages in thread
From: Jinseok Kim @ 2026-07-29 14:32 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 testcases/kernel/syscalls/open/open01.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/testcases/kernel/syscalls/open/open01.c b/testcases/kernel/syscalls/open/open01.c
index f1a3720aa..b1b32510a 100644
--- a/testcases/kernel/syscalls/open/open01.c
+++ b/testcases/kernel/syscalls/open/open01.c
@@ -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 = -1;

@@ -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 != -1)
@@ -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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [LTP] [PATCH v10 3/3] fstat: add test for multiple file types using fstat
  2026-07-29 14:32 [LTP] [PATCH v10 1/3] open01: fix cleanup file descriptor check Jinseok Kim
  2026-07-29 14:32 ` [LTP] [PATCH v10 2/3] open01: remove O_DIRECTORY case (move to fstat test) Jinseok Kim
@ 2026-07-29 14:32 ` Jinseok Kim
  2026-07-29 16:27 ` [LTP] open01: fix cleanup file descriptor check linuxtestproject.agent
  2 siblings, 0 replies; 4+ messages in thread
From: Jinseok Kim @ 2026-07-29 14:32 UTC (permalink / raw)
  To: ltp

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 <always.starving0@gmail.com>
---
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/20260621090403.2236-2-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  | 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 <always.starving0@gmail.com>
+ */
+/*\
+ * 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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [LTP] open01: fix cleanup file descriptor check
  2026-07-29 14:32 [LTP] [PATCH v10 1/3] open01: fix cleanup file descriptor check Jinseok Kim
  2026-07-29 14:32 ` [LTP] [PATCH v10 2/3] open01: remove O_DIRECTORY case (move to fstat test) Jinseok Kim
  2026-07-29 14:32 ` [LTP] [PATCH v10 3/3] fstat: add test for multiple file types using fstat Jinseok Kim
@ 2026-07-29 16:27 ` linuxtestproject.agent
  2 siblings, 0 replies; 4+ messages in thread
From: linuxtestproject.agent @ 2026-07-29 16:27 UTC (permalink / raw)
  To: Jinseok Kim; +Cc: ltp

Hi Jinseok,

On 2026-07-29, Jinseok Kim wrote:
> open01: fix cleanup file descriptor check

--- [PATCH 1/3] ---

> File descriptor 0 is valid, so checking fd > 0 may skip closing an
> successfully opened file.

Typo: "an successfully" -> "a successfully".

--- [PATCH 2/3] ---

>  } tcases[] = {
>  	{TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
> -	{TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "directory bit"}
>  };

Only one entry is left in tcases[] now.

Is the .test + .tcnt parametrization still worth keeping here? The
convention is .test_all with a plain run() when there is a single test
case, unless more cases are planned on top of this.

>  	SAFE_CLOSE(fd);
>  	if (S_ISREG(buf.st_mode))
>  		SAFE_UNLINK(tc->filename);

This guard existed only to skip the unlink for the O_DIRECTORY case.

With that case removed the only remaining file is a regular one, so the
condition is always true. Can it be dropped and the unlink made
unconditional?

>   * 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.
>   */

The numbered list has a single "1." item left. Plain prose would read
better in the test catalog.

--- [PATCH 3/3] ---

> + * Verify that :manpage:`fstat(2)` correctly identifies various
> + * file types.

"various file types" does not say which ones. The commit message already
enumerates them, so how about moving that into the description, e.g.:

  Verify that :manpage:`fstat(2)` reports the correct file type in
  st_mode for regular files, directories, FIFOs, character devices and
  block devices.

> +#include <sys/sysmacros.h>
> +#include <fcntl.h>
> +
> +#include "tst_test.h"

O_PATH is used below but only <fcntl.h> is included. LTP carries a
fallback definition in include/lapi/fcntl.h for headers that predate
O_PATH, so "lapi/fcntl.h" should be included as well.

> +	TST_EXP_EXPR((buf.st_mode & S_IFMT) == tc->exp_type, "checking %s", tc->path);

On failure this only prints the path, not what was expected. Something
like "fstat() reports the expected type for %s" carries more information.

Also, for the character and block device cases only S_IFMT is checked
while st_rdev is ignored. Would it be worth comparing it against
makedev(1, 3) / makedev(7, 3) as well? statx01.c checks major/minor for
its device file.

> +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);
> +}

Is any of this needed? Everything is created inside the framework tmpdir,
which is removed recursively at the end of the test, and MOUNT_PATH is
either a plain directory in that tmpdir or a tmpfs the framework unmounts
itself (prepare_and_mount_dev_fs() in lib/tst_test.c). Nothing here
outlives the test.

open11.c does the same touch/mkdir/mknod setup with .needs_devfs and has
no cleanup() at all.

Minor: "access(DIR_FILE,  F_OK)" has a double space.

> +	.mntpoint = MOUNT_PATH,
> +	.needs_devfs = 1,
> +	.needs_tmpdir = 1,
> +	.needs_root = 1,

.needs_tmpdir is redundant here, .mntpoint already implies the tmpdir
(see needs_tmpdir() in lib/tst_test.c). open11.c and fsetxattr02.c set
only .needs_devfs and .mntpoint.

Verdict - Needs revision

Pre-existing issues:

open01.c includes <errno.h> but never uses errno.

---
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] 4+ messages in thread

end of thread, other threads:[~2026-07-29 16:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-29 14:32 [LTP] [PATCH v10 1/3] open01: fix cleanup file descriptor check Jinseok Kim
2026-07-29 14:32 ` [LTP] [PATCH v10 2/3] open01: remove O_DIRECTORY case (move to fstat test) Jinseok Kim
2026-07-29 14:32 ` [LTP] [PATCH v10 3/3] fstat: add test for multiple file types using fstat Jinseok Kim
2026-07-29 16:27 ` [LTP] open01: fix cleanup file descriptor check linuxtestproject.agent

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.