Skip to contentSkip to conversation

open01: fix cleanup file descriptor check

PATCH In review Jinseok Kim 2 participants 4 messages Jul 29

lore
Share & tools
lore
#0 lore raw

PATCH v10 1/3 open01: fix cleanup file descriptor check

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.

---
 testcases/kernel/syscalls/open/open01.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/open/open01.c +2 -2
index 1cefb4790..f1a3720aa 100644
@@ -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

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

PATCH v10 2/3 open01: 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 f1a3720aa..b1b32510a 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 = -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

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

PATCH v10 3/3 fstat: add test for multiple file types using fstat

In reply to Jinseok Kim

Following review feedback, create a dedicated fstat test that verifies that fstat() correctly identifies regular files, directories, FIFOs, character devices, and block devices.

---
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
runtest/syscalls +2 -0
index 43951df2a..a38743ded 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 +94 -0

94 lines not on this page · raw message

--
2.43.0

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

Hi Jinseok,

Quoted 1 line · Jinseok Kim

open01: fix cleanup file descriptor check

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

Quoted 2 lines

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

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

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

Review

Quoted 4 lines

} tcases[] = {

(quoted diff) +0 -1
 	{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.

Review

Quoted 3 lines
(quoted diff) +0 -0
 	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?

Quoted 5 lines

* 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] ---

Quoted 2 lines

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

Review

Quoted 4 lines
(quoted diff) +4 -0
+#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.

Review

Quoted 1 line
(quoted diff) +1 -0
+	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.

Quoted 7 lines
(quoted diff) +7 -0
+static void cleanup(void)
+{
+	if (!access(REG_FILE, F_OK))
+		SAFE_UNLINK(REG_FILE);
+
+	if (!access(DIR_FILE,  F_OK))
+		SAFE_RMDIR(DIR_FILE);

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	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.

Review

Quoted 4 lines
(quoted diff) +4 -0
+	.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

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