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

Jinseok Kim always.starving0@gmail.com
Mon Apr 6 15:31:00 CEST 2026


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

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 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

diff --git a/runtest/syscalls b/runtest/syscalls
index 5a2e8f048..71c2b9ec5 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -134,6 +134,7 @@ clone304 clone304

 close01 close01
 close02 close02
+close03 close03

 close_range01 close_range01
 close_range02 close_range02
diff --git a/testcases/kernel/syscalls/close/.gitignore b/testcases/kernel/syscalls/close/.gitignore
index 07d25bccf..1ed7e45cf 100644
--- a/testcases/kernel/syscalls/close/.gitignore
+++ b/testcases/kernel/syscalls/close/.gitignore
@@ -1,2 +1,3 @@
 /close01
 /close02
+/close03
diff --git a/testcases/kernel/syscalls/close/close03.c b/testcases/kernel/syscalls/close/close03.c
new file mode 100644
index 000000000..38950d00c
--- /dev/null
+++ b/testcases/kernel/syscalls/close/close03.c
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Jinseok Kim <always.starving0@gmail.com>
+ */
+
+/*\
+ * 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


More information about the ltp mailing list