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

Jinseok Kim always.starving0@gmail.com
Tue Apr 21 16:42:44 CEST 2026


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().

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
Changes in v5:
- Change file descriptor as static
- Link to v4 : https://lore.kernel.org/ltp/20260416125554.2920-1-always.starving0@gmail.com

Changes in v4:
- Remove enum, add fd/expected errno in tcases, and move preparation
  to setup().
- Link to v3: https://lore.kernel.org/ltp/20260413165457.1349-1-always.starving0@gmail.com

Changes in v3:
- Add O_RDWR flag to SAFE_OPEN
- Link to v2: https://lore.kernel.org/ltp/20260411110405.7330-1-always.starving0@gmail.com

Changes in v2:
- Add additional test coverage to close02 instead of creating a separate
  close03 test.
- Link to v1: https://lore.kernel.org/ltp/20260406133134.17238-2-always.starving0@gmail.com
---
 testcases/kernel/syscalls/close/close02.c | 37 ++++++++++++++++++++---
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/testcases/kernel/syscalls/close/close02.c b/testcases/kernel/syscalls/close/close02.c
index 617c48237..a48570d12 100644
--- a/testcases/kernel/syscalls/close/close02.c
+++ b/testcases/kernel/syscalls/close/close02.c
@@ -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


More information about the ltp mailing list