public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Petr Vorel <pvorel@suse.cz>
To: Jinseok Kim <always.starving0@gmail.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v3] close: add test for double close EBADF
Date: Mon, 13 Apr 2026 22:45:19 +0200	[thread overview]
Message-ID: <20260413204519.GB153189@pevik> (raw)
In-Reply-To: <20260413165457.1349-1-always.starving0@gmail.com>

Hi Jinseok,

> 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 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 | 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..3b3c61b1e 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 },
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.

> +};
> +
> +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);
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

> +		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,
>  };

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2026-04-13 20:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-06 13:30 [LTP] [PATCH 1/2] close: remove unused headers and fix minor style issues Jinseok Kim
2026-04-06 13:31 ` [LTP] [PATCH 2/2] close: add test for double close EBADF Jinseok Kim
2026-04-09  8:15   ` Petr Vorel
2026-04-11 11:04     ` [LTP] [PATCH v2] " Jinseok Kim
2026-04-13  4:33       ` Li Wang
2026-04-13 11:30         ` Petr Vorel
2026-04-13 16:52         ` [LTP] [PATCH v3] " Jinseok Kim
2026-04-13 18:04           ` [LTP] " linuxtestproject.agent
2026-04-13 20:45           ` Petr Vorel [this message]
2026-04-09  8:08 ` [LTP] [PATCH 1/2] close: remove unused headers and fix minor style issues Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260413204519.GB153189@pevik \
    --to=pvorel@suse.cz \
    --cc=always.starving0@gmail.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox