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

Li Wang
2026-04-13T04:33:45Z

On Sat, 11 Apr 2026 at 19:04, Jinseok Kim <always.starving0@gmail.com> wrote:
>
> 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 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 | 42 ++++++++++++++++++++---
> 1 file changed, 38 insertions(+), 4 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/close/close02.c b/testcases/kernel/syscalls/close/close02.c
> index 617c48237..768361e56 100644
> --- a/testcases/kernel/syscalls/close/close02.c
> +++ b/testcases/kernel/syscalls/close/close02.c
> @@ -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);

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:

Reviewed-by: Li Wang <wangli.ahau@gmail.com>

--
Regards,
Li Wang
Email: wangli.ahau@gmail.com