Skip to contentSkip to conversation

close: remove unused headers and fix minor style issues

PATCH In review Jinseok Kim 5 participants 24 messages Apr 6 – May 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 close: remove unused headers and fix minor style issues

Remove unused headers (<stdio.h>, <errno.h>, <sys/stat.h>) from close01.c and (<stdio.h>, <sys/stat.h>) from close02.c.

Also make struct test_case_t static and apply minor style fixes.

---
 testcases/kernel/syscalls/close/close01.c | 6 ++----
 testcases/kernel/syscalls/close/close02.c | 2 --
 2 files changed, 2 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/close/close01.c +2 -4
index 4e4f107ef..616752595 100644
@@ -8,10 +8,7 @@
  * Test that closing a file/pipe/socket works correctly.
  */
 
-#include <stdio.h>
-#include <errno.h>
 #include <fcntl.h>
-#include <sys/stat.h>
 #include "tst_test.h"
 
 #define FILENAME "close01_testfile"
@@ -24,6 +21,7 @@ static int get_fd_file(void)
 static int get_fd_pipe(void)
 {
 	int pipefildes[2];
+
 	SAFE_PIPE(pipefildes);
 	SAFE_CLOSE(pipefildes[1]);
 	return pipefildes[0];
@@ -34,7 +32,7 @@ static int get_fd_socket(void)
 	return SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
 }
 
-struct test_case_t {
+static struct test_case_t {
 	int (*get_fd) ();
 	char *type;
 } tc[] = {
testcases/kernel/syscalls/close/close02.c +0 -2
index 2016453f8..617c48237 100644
@@ -8,9 +8,7 @@
  * Call close(-1) and expects it to return EBADF.
  */
 
-#include <stdio.h>
 #include <errno.h>
-#include <sys/stat.h>
 #include "tst_test.h"
 
 static void run(void)

--
2.43.0

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

PATCH 2/2 close: add test for double close EBADF

Verify that calling close() on an already closed file descriptor fails with EBADF.

The existing close tests cover:
- close01: successful close on valid file descriptors
- close02: invalid file descriptor (-1)

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
 runtest/syscalls                           |  1 +
 testcases/kernel/syscalls/close/.gitignore |  1 +
 testcases/kernel/syscalls/close/close03.c  | 26 ++++++++++++++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 testcases/kernel/syscalls/close/close03.c
runtest/syscalls +1 -0
index 5a2e8f048..71c2b9ec5 100644
@@ -134,6 +134,7 @@ clone304 clone304
 
 close01 close01
 close02 close02
+close03 close03
 
 close_range01 close_range01
 close_range02 close_range02
testcases/kernel/syscalls/close/.gitignore +1 -0
index 07d25bccf..1ed7e45cf 100644
@@ -1,2 +1,3 @@
 /close01
 /close02
+/close03
testcases/kernel/syscalls/close/close03.c new file +26 -0
new file mode 100644
index 000000000..38950d00c
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+
+/*\
+ * Verify that calling close() twice on the same fd returns EBADF on
+ * the second call.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include "tst_test.h"
+
+static void run(void)
+{
+	int fd = SAFE_OPEN("close03", O_CREAT, 0600);
+
+	TST_EXP_PASS(close(fd));
+	TST_EXP_FAIL(close(fd), EBADF);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test_all = run,
+};

--
2.43.0

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

Re: [LTP] [PATCH 2/2] close: add test for double close EBADF

Quoted 9 lines

Verify that calling close() on an already closed file descriptor fails with EBADF.

The existing close tests cover:
- close01: successful close on valid file descriptors
- close02: invalid file descriptor (-1)

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

IMHO it'd make sense to add this errno check to close02.c (using .tcnt).

...

testcases/kernel/syscalls/close/close03.c line 7

Quoted 10 lines
testcases/kernel/syscalls/close/close03.c new file +7 -0
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <[email protected]>
+ */
+
+/*\
+ * Verify that calling close() twice on the same fd returns EBADF on

Please instead of close() use:
:manpage:close(2)

That will provide link to man page
https://man7.org/linux/man-pages/man2/close.2.html

in our test catalog:
https://linux-test-project.readthedocs.io/en/latest/users/test_catalog.html

Kind regards,
Petr

Quoted 19 lines
testcases/kernel/syscalls/close/close03.c new file +19 -0
+ * the second call.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include "tst_test.h"
+
+static void run(void)
+{
+	int fd = SAFE_OPEN("close03", O_CREAT, 0600);
+
+	TST_EXP_PASS(close(fd));
+	TST_EXP_FAIL(close(fd), EBADF);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test_all = run,
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v2 1/1 close: add test for double close EBADF

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 42 ++++++++++++++++++++---
 1 file changed, 38 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +38 -4
index 617c48237..768361e56 100644
@@ -5,17 +5,51 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+enum case_type {
+	INVALID_FD,
+	DOUBLE_CLOSE,
+};
+
+static struct tcase {
+	const char *desc;
+	enum case_type type;
+} tcases[] = {
+	{ "close(-1)", INVALID_FD },
+	{ "close same fd twice", DOUBLE_CLOSE },
+};
+
+static void verify_close(unsigned int i)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	int fd;
+	struct tcase *tc = &tcases[i];
+
+	switch (tc->type) {
+	case INVALID_FD:
+		TST_EXP_FAIL(close(-1), EBADF, "%s", tc->desc);
+		break;
+
+	case DOUBLE_CLOSE:
+		fd = SAFE_OPEN("close02", O_CREAT, 0600);
+
+		TST_EXP_PASS(close(fd), "%s: first close()", tc->desc);
+		TST_EXP_FAIL(close(fd), EBADF, "%s: second close()", tc->desc);
+		break;
+	}
 }
 
 static struct tst_test test = {
-	.test_all = run,
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_close,
 };

--
2.43.0

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

Re: [LTP] [PATCH v2] close: add test for double close EBADF

Quoted 54 lines · Jinseok Kim

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 42 ++++++++++++++++++++---
 1 file changed, 38 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +20 -3
index 617c48237..768361e56 100644
@@ -5,17 +5,51 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+enum case_type {
+       INVALID_FD,
+       DOUBLE_CLOSE,
+};
+
+static struct tcase {
+       const char *desc;
+       enum case_type type;
+} tcases[] = {
+       { "close(-1)", INVALID_FD },
+       { "close same fd twice", DOUBLE_CLOSE },
+};
+
+static void verify_close(unsigned int i)
 {
-       TST_EXP_FAIL(close(-1), EBADF);

testcases/kernel/syscalls/close/close02.c line 43

Quoted 10 lines
testcases/kernel/syscalls/close/close02.c +10 -0
+       int fd;
+       struct tcase *tc = &tcases[i];
+
+       switch (tc->type) {
+       case INVALID_FD:
+               TST_EXP_FAIL(close(-1), EBADF, "%s", tc->desc);
+               break;
+
+       case DOUBLE_CLOSE:
+               fd = SAFE_OPEN("close02", O_CREAT, 0600);

According to the open(2) man page, the flags argument must include one of the access modes: O_RDONLY, O_WRONLY, or O_RDWR.

For example:

  fd = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);

With the flag updated to include an access mode (like O_RDWR), it will look good to me:

Signature · 6 lines
Regards,
Li Wang
Email: [email protected]

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

Re: [LTP] [PATCH v2] close: add test for double close EBADF

Hi all,

...

Quoted 8 lines
Quoted 8 lines
testcases/kernel/syscalls/close/close02.c +6 -1
+static void verify_close(unsigned int i)
 {
-       TST_EXP_FAIL(close(-1), EBADF);
+       int fd;
+       struct tcase *tc = &tcases[i];
+
+       switch (tc->type) {
+       case INVALID_FD:
Quoted 8 lines

testcases/kernel/syscalls/close/close02.c line 43

Quoted 5 lines
testcases/kernel/syscalls/close/close02.c +5 -0
+               TST_EXP_FAIL(close(-1), EBADF, "%s", tc->desc);
+               break;
+
+       case DOUBLE_CLOSE:
+               fd = SAFE_OPEN("close02", O_CREAT, 0600);

According to the open(2) man page, the flags argument must include one of the access modes: O_RDONLY, O_WRONLY, or O_RDWR.

Good catch, Li.

Quoted 8 lines

For example:

  fd = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);

With the flag updated to include an access mode (like O_RDWR), it will look good to me:

Agree.

And welcome back. Feel free to update .mailmap to the new address.

Kind regards,
Petr

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

PATCH v3 1/1 close: add test for double close EBADF

In reply to Li Wang

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v3:
- Add O_RDWR flag to SAFE_OPEN
- Link to v2: https://lore.kernel.org/ltp/[email protected]

Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 42 ++++++++++++++++++++---
 1 file changed, 38 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +38 -4
index 617c48237..3b3c61b1e 100644
@@ -5,17 +5,51 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+enum case_type {
+	INVALID_FD,
+	DOUBLE_CLOSE,
+};
+
+static struct tcase {
+	const char *desc;
+	enum case_type type;
+} tcases[] = {
+	{ "close(-1)", INVALID_FD },
+	{ "close same fd twice", DOUBLE_CLOSE },
+};
+
+static void verify_close(unsigned int i)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	int fd;
+	struct tcase *tc = &tcases[i];
+
+	switch (tc->type) {
+	case INVALID_FD:
+		TST_EXP_FAIL(close(-1), EBADF, "%s", tc->desc);
+		break;
+
+	case DOUBLE_CLOSE:
+		fd = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+
+		TST_EXP_PASS(close(fd), "%s: first close()", tc->desc);
+		TST_EXP_FAIL(close(fd), EBADF, "%s: second close()", tc->desc);
+		break;
+	}
 }
 
 static struct tst_test test = {
-	.test_all = run,
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_close,
 };

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by LTP AI Reviewer on v3 1/1
#7 lore raw

Re: [LTP] close: add test for double close EBADF

Hi Jinseok,

our agent completed the review of the patch.

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.

Quoted 1 line · Jinseok Kim

close: add test for double close EBADF

Regards,
LTP AI Reviewer

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

Re: [LTP] [PATCH v3] close: add test for double close EBADF

In reply to Jinseok Kim

Hi Jinseok,

Quoted 41 lines

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v3:
- Add O_RDWR flag to SAFE_OPEN
- Link to v2: https://lore.kernel.org/ltp/[email protected]
Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 42 ++++++++++++++++++++---
 1 file changed, 38 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +7 -2
index 617c48237..3b3c61b1e 100644
@@ -5,17 +5,51 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+enum case_type {

testcases/kernel/syscalls/close/close02.c line 29

Quoted 10 lines
testcases/kernel/syscalls/close/close02.c +10 -0
+	INVALID_FD,
+	DOUBLE_CLOSE,
+};
+
+static struct tcase {
+	const char *desc;
+	enum case_type type;
+} tcases[] = {
+	{ "close(-1)", INVALID_FD },
+	{ "close same fd twice", DOUBLE_CLOSE },

I'd slightly prefer to have fd and errnos here in the struct:

	int fd;
	int exp_errno;

Specially with other changes below. This way will follow more LTP errno test approach and enum case_type will not be needed.

Quoted 7 lines
testcases/kernel/syscalls/close/close02.c +5 -1
+};
+
+static void verify_close(unsigned int i)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	int fd;
+	struct tcase *tc = &tcases[i];

testcases/kernel/syscalls/close/close02.c line 45

Quoted 10 lines
testcases/kernel/syscalls/close/close02.c +10 -0
+
+	switch (tc->type) {
+	case INVALID_FD:
+		TST_EXP_FAIL(close(-1), EBADF, "%s", tc->desc);
+		break;
+
+	case DOUBLE_CLOSE:
+		fd = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+
+		TST_EXP_PASS(close(fd), "%s: first close()", tc->desc);

Thinking about it twice, I would move SAFE_OPEN() together with this line converted to SAFE_CLOSE() to setup(). Why? We test errnos, therefore this is really just a preparation.

Then, the runtime would really be

	TST_EXP_PASS(close(tc->fd), "%s", tc->desc);

Kind regards,
Petr

Quoted 11 lines
testcases/kernel/syscalls/close/close02.c +6 -1
+		TST_EXP_FAIL(close(fd), EBADF, "%s: second close()", tc->desc);
+		break;
+	}
 }
 
 static struct tst_test test = {
-	.test_all = run,
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_close,
 };
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

PATCH v4 1/1 close02: add test for double close EBADF

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v4:
- Remove enum, add fd/expected errno in tcases, and move preparation
to setup().
- Link to v3: https://lore.kernel.org/ltp/[email protected]

Changes in v3:
- Add O_RDWR flag to SAFE_OPEN
- Link to v2: https://lore.kernel.org/ltp/[email protected]

Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 35 ++++++++++++++++++++---
 1 file changed, 31 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +31 -4
index 617c48237..1919a27fb 100644
@@ -5,17 +5,44 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+static struct tcase {
+	const char *desc;
+	int fd;
+	int exp_errno;
+} tcases[] = {
+	{ "close(-1)", -1, EBADF },
+	{ "close same fd twice", -1, EBADF },
+};
+
+static void verify_close(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	TST_EXP_FAIL(close(tc->fd), tc->exp_errno, "%s", tc->desc);
+}
+
+static void setup(void)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	int fd = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+
+	SAFE_CLOSE(fd);
+	tcases[1].fd = fd;
 }
 
 static struct tst_test test = {
-	.test_all = run,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_close,
 };

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by LTP AI Reviewer on v4 1/1
#10 lore raw

Re: [LTP] close02: add test for double close EBADF

Hi Jinseok,

Quoted 1 line · Jinseok Kim

close02: add test for double close EBADF

---
Note:

Our agent completed the review of the patch. 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
#12 lore raw

Re: [LTP] [PATCH v4] close02: add test for double close EBADF

In reply to Jinseok Kim

Hi Jinseok,

...

Quoted 14 lines
testcases/kernel/syscalls/close/close02.c +13 -1
-static void run(void)
+static struct tcase {
+	const char *desc;
+	int fd;
+	int exp_errno;
+} tcases[] = {
+	{ "close(-1)", -1, EBADF },
+	{ "close same fd twice", -1, EBADF },
+};
+
+static void verify_close(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+

testcases/kernel/syscalls/close/close02.c line 40

Quoted 10 lines
testcases/kernel/syscalls/close/close02.c +8 -1
+	TST_EXP_FAIL(close(tc->fd), tc->exp_errno, "%s", tc->desc);
+}
+
+static void setup(void)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	int fd = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+
+	SAFE_CLOSE(fd);
+	tcases[1].fd = fd;

Depending on index is error-prone. I know that test array has only 2 members, but could you please use our usual approach to use file descriptors as static? See fd_ebadf and fd_enotdir in testcases/kernel/syscalls/bind/bind01.c.

Kind regards,
Petr

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

PATCH v5 1/1 close02: add test for double close EBADF

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v5:
- Change file descriptor as static
- Link to v4 : https://lore.kernel.org/ltp/[email protected]

Changes in v4:
- Remove enum, add fd/expected errno in tcases, and move preparation
to setup().
- Link to v3: https://lore.kernel.org/ltp/[email protected]

Changes in v3:
- Add O_RDWR flag to SAFE_OPEN
- Link to v2: https://lore.kernel.org/ltp/[email protected]

Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 37 ++++++++++++++++++++---
 1 file changed, 33 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +33 -4
index 617c48237..a48570d12 100644
@@ -5,17 +5,46 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+static int fd_closed = -1;
+
+static struct tcase {
+	const char *desc;
+	int fd;
+	int exp_errno;
+} tcases[] = {
+	{ "close(-1)", -1, EBADF },
+	{ "close same fd twice", -1, EBADF },
+};
+
+static void verify_close(unsigned int i)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	struct tcase *tc = &tcases[i];
+
+	TST_EXP_FAIL(close(tc->fd), tc->exp_errno, "%s", tc->desc);
+}
+
+static void setup(void)
+{
+	fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+
+	SAFE_CLOSE(fd_closed);
+	tcases[1].fd = fd_closed;
 }
 
 static struct tst_test test = {
-	.test_all = run,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_close,
 };

--
2.43.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by LTP AI Reviewer on v5 1/1
#14 lore raw

Re: [LTP] close02: add test for double close EBADF

Hi Jinseok,

Quoted 1 line · Jinseok Kim

close02: add test for double close EBADF

---
Note:

Our agent completed the review of the patch. The full review can be found at: https://github.com/linux-test-project/ltp-agent/actions/runs/24730323743

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
#15 lore raw

Re: [LTP] [PATCH v5] close02: add test for double close EBADF

In reply to Jinseok Kim

Hi Jinseok,

Quoted 10 lines

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v5:
- Change file descriptor as static

Well, I asked for static pointer.

Quoted 35 lines

- Link to v4 : https://lore.kernel.org/ltp/[email protected]

Changes in v4:
- Remove enum, add fd/expected errno in tcases, and move preparation
to setup().
- Link to v3: https://lore.kernel.org/ltp/[email protected]

Changes in v3:
- Add O_RDWR flag to SAFE_OPEN
- Link to v2: https://lore.kernel.org/ltp/[email protected]

Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 37 ++++++++++++++++++++---
 1 file changed, 33 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +4 -1
index 617c48237..a48570d12 100644
@@ -5,17 +5,46 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>

testcases/kernel/syscalls/close/close02.c line 23

Quoted 10 lines
testcases/kernel/syscalls/close/close02.c +7 -1
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+static int fd_closed = -1;
+
+static struct tcase {
+	const char *desc;
+	int fd;

ie instead of "int fd" define:

	int *fd;
Quoted 10 lines
testcases/kernel/syscalls/close/close02.c +8 -1
+	int exp_errno;
+} tcases[] = {
+	{ "close(-1)", -1, EBADF },
+	{ "close same fd twice", -1, EBADF },
+};
+
+static void verify_close(unsigned int i)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	struct tcase *tc = &tcases[i];

testcases/kernel/syscalls/close/close02.c line 42

Quoted 10 lines
testcases/kernel/syscalls/close/close02.c +10 -0
+
+	TST_EXP_FAIL(close(tc->fd), tc->exp_errno, "%s", tc->desc);
+}
+
+static void setup(void)
+{
+	fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+
+	SAFE_CLOSE(fd_closed);
+	tcases[1].fd = fd_closed;

We're getting there but not yet :). My point was not to assign file descriptor here in the setup function, but above in tcases[] array. For that you need to define in struct tcases "int *fd" (i.e. pointer) instead of "int fd".

I pointed in v4 testcases/kernel/syscalls/bind/bind01.c, which uses int *socket_fd, could you do it that way (use pointer)?

Kind regards,
Petr

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

PATCH v6 1/1 close02: add test for double close EBADF

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v6:
- Use a pointer for fd, referencing the bind01.c approach
- Link to v5 : https://lore.kernel.org/ltp/[email protected]

Changes in v5:
- Change file descriptor as static
- Link to v4 : https://lore.kernel.org/ltp/[email protected]

Changes in v4:
- Remove enum, add fd/expected errno in tcases, and move preparation
to setup().
- Link to v3: https://lore.kernel.org/ltp/[email protected]

Changes in v3:
- Add O_RDWR flag to SAFE_OPEN
- Link to v2: https://lore.kernel.org/ltp/[email protected]

Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 36 ++++++++++++++++++++---
 1 file changed, 32 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +32 -4
index 617c48237..c54789e7e 100644
@@ -5,17 +5,45 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+static int fd_invalid = -1;
+static int fd_closed = -1;
+
+static struct tcase {
+	const char *desc;
+	int *fd;
+	int exp_errno;
+} tcases[] = {
+	{ "close(-1)", &fd_invalid, EBADF },
+	{ "close same fd twice", &fd_closed, EBADF },
+};
+
+static void verify_close(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	TST_EXP_FAIL(close(*tc->fd), tc->exp_errno, "%s", tc->desc);
+}
+
+static void setup(void)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+	SAFE_CLOSE(fd_closed);
 }
 
 static struct tst_test test = {
-	.test_all = run,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_close,
 };

--
2.43.0

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

Re: [LTP] close02: add test for double close EBADF

Hi Jinseok,

testcases/kernel/syscalls/close/close02.c

Quoted 7 lines · Jinseok Kim

close02: add test for double close EBADF

testcases/kernel/syscalls/close/close02.c +5 -0
+static void setup(void)
+{
+	fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+	SAFE_CLOSE(fd_closed);
+}

SAFE_CLOSE(fd_closed) resets fd_closed to -1 (the macro does fd = -1 after the close), so both test cases end up calling close(-1). The second case never exercises the double-close path. Use a local variable to preserve the fd value:

    int fd = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
    fd_closed = fd;
    SAFE_CLOSE(fd);

---
Note:

Our agent completed the review of the patch.

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
#18 lore raw

Re: [LTP] close02: add test for double close EBADF

Hi Jinseok,

The review agent is actually correct here. If we close the fd with SAFE_CLOSE() it will end up having -1 value. You need to use the raw close() plus checking its return value.

Quoted 23 lines

Hi Jinseok,

testcases/kernel/syscalls/close/close02.c

Quoted 7 lines · Jinseok Kim

close02: add test for double close EBADF

testcases/kernel/syscalls/close/close02.c +5 -0
+static void setup(void)
+{
+	fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+	SAFE_CLOSE(fd_closed);
+}

SAFE_CLOSE(fd_closed) resets fd_closed to -1 (the macro does fd = -1 after the close), so both test cases end up calling close(-1). The second case never exercises the double-close path. Use a local variable to preserve the fd value:

    int fd = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
    fd_closed = fd;
    SAFE_CLOSE(fd);

---
Note:

Quoted 11 lines

Our agent completed the review of the patch.

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

--
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 v7 1/1 close02: add test for double close EBADF

Verify that calling close() on an already closed file descriptor fails with EBADF.

This test adds coverage for a common state transition case where a previously valid file descriptor becomes invalid after close().

---
Changes in v7:
- Use the raw close() to avoid setting the fd to -1.
- Link to v6 : https://lore.kernel.org/ltp/[email protected]

Changes in v6:
- Use a pointer for fd, referencing the bind01.c approach
- Link to v5 : https://lore.kernel.org/ltp/[email protected]

Changes in v5:
- Change file descriptor as static
- Link to v4 : https://lore.kernel.org/ltp/[email protected]

Changes in v4:
- Remove enum, add fd/expected errno in tcases, and move preparation
to setup().
- Link to v3: https://lore.kernel.org/ltp/[email protected]

Changes in v3:
- Add O_RDWR flag to SAFE_OPEN
- Link to v2: https://lore.kernel.org/ltp/[email protected]

Changes in v2:
- Add additional test coverage to close02 instead of creating a separate close03 test.
- Link to v1: https://lore.kernel.org/ltp/[email protected]

---
 testcases/kernel/syscalls/close/close02.c | 37 ++++++++++++++++++++---
 1 file changed, 33 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/close/close02.c +33 -4
index 617c48237..03cce109a 100644
@@ -5,17 +5,46 @@
  */
 
 /*\
- * Call close(-1) and expects it to return EBADF.
+ * Verify :manpage:`close(2)` failure cases:
+ *
+ * 1) close(-1) returns EBADF.
+ * 2) closing the same fd twice returns EBADF on the second call.
  */
 
 #include <errno.h>
+#include <fcntl.h>
+
 #include "tst_test.h"
 
-static void run(void)
+static int fd_invalid = -1;
+static int fd_closed = -1;
+
+static struct tcase {
+	const char *desc;
+	int *fd;
+	int exp_errno;
+} tcases[] = {
+	{ "close(-1)", &fd_invalid, EBADF },
+	{ "close same fd twice", &fd_closed, EBADF },
+};
+
+static void verify_close(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	TST_EXP_FAIL(close(*tc->fd), tc->exp_errno, "%s", tc->desc);
+}
+
+static void setup(void)
 {
-	TST_EXP_FAIL(close(-1), EBADF);
+	fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+	if (close(fd_closed) == -1)
+		tst_brk(TBROK | TERRNO, "close(%d) failed", fd_closed);
 }
 
 static struct tst_test test = {
-	.test_all = run,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_close,
 };

--
2.43.0

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

Re: [LTP] close02: add test for double close EBADF

Hi Jinseok,

testcases/kernel/syscalls/close/close02.c

Quoted 8 lines · Jinseok Kim

close02: add test for double close EBADF

testcases/kernel/syscalls/close/close02.c +6 -0
+static void setup(void)
+{
+       fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+       if (close(fd_closed) == -1)
+               tst_brk(TBROK | TERRNO, "close(%d) failed", fd_closed);
+}

Use SAFE_CLOSE instead of a raw close(). Since SAFE_CLOSE sets the variable to -1, save the fd value first:

    int tmp_fd;
    fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
    tmp_fd = fd_closed;
    SAFE_CLOSE(tmp_fd);

---
Note:

Our agent completed the review of the patch. The full review can be found at: https://github.com/linux-test-project/ltp-agent/actions/runs/25750070063

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
#21 lore raw

Re: [LTP] close02: add test for double close EBADF

Hi Jinseok,

Quoted 15 lines

Hi Jinseok,

testcases/kernel/syscalls/close/close02.c

Quoted 8 lines · Jinseok Kim

close02: add test for double close EBADF

testcases/kernel/syscalls/close/close02.c +6 -0
+static void setup(void)
+{
+       fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
+       if (close(fd_closed) == -1)
+               tst_brk(TBROK | TERRNO, "close(%d) failed", fd_closed);
+}

Use SAFE_CLOSE instead of a raw close(). Since SAFE_CLOSE sets the variable to -1, save the fd value first:

Quoted 4 lines
    int tmp_fd;
    fd_closed = SAFE_OPEN("close02", O_CREAT | O_RDWR, 0600);
    tmp_fd = fd_closed;
    SAFE_CLOSE(tmp_fd);

This is a corner case, please ignore.

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

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

Re: [LTP] [PATCH v7] close02: add test for double close EBADF

In reply to Jinseok Kim

Hi Jinseok,

merged, thank you!

Kind regards,
Petr

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

Hi Jinseok,

Quoted 4 lines

Remove unused headers (<stdio.h>, <errno.h>, <sys/stat.h>) from close01.c and (<stdio.h>, <sys/stat.h>) from close02.c.

Also make struct test_case_t static and apply minor style fixes.

Thanks, patch merged.

Kind regards,
Petr

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