All of lore.kernel.org
 help / color / mirror / Atom feed
From: linuxtestproject.agent@gmail.com
To: Wei Gao <wegao@suse.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] connect01: Convert to new API
Date: Thu,  4 Jun 2026 07:15:25 +0000	[thread overview]
Message-ID: <20260604071525.4085-1-linuxtestproject.agent@gmail.com> (raw)
In-Reply-To: <20260604045008.29021-2-wegao@suse.com>

On Thu, Jun 04, 2026 at 04:49:55 +0000, Wei Gao wrote:
> connect01: Convert to new API
> connect01: Add negative tests

Hi Wei,

Overall the series is in good shape. The conversion to the new API is
clean and the new EPROTOTYPE/EACCES test cases make sense. One bug needs
fixing and there are a few minor observations.

---
Patch 1: connect01: Convert to new API

> Convert the connect01 test case from the legacy LTP API to the new
> tst_test API.
>
> Refactor the test according to reviewer feedback:
> - Simplified the server child process to a single accept/exit loop.
> - Moved the server lifecycle to the global setup/cleanup.
> - The server now accepts a single connection for the EISCONN test case
>   and exits immediately, which avoids blocking the test runner.
> - Centralized test case initialization into the global setup.
> - Used tst_get_bad_addr() for the EFAULT test case to ensure
>   architectural compatibility.

The bulleted list is fine, though LTP commit messages generally prefer
plain prose over bullet points for the body.

> +static void start_server(struct sockaddr_in *sock)
> +{
> +	socklen_t slen = sizeof(*sock);
> +
> +	sock->sin_family = AF_INET;
> +	sock->sin_port = 0;
> +	sock->sin_addr.s_addr = INADDR_ANY;
> +
> +	fd_server = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
> +	SAFE_BIND(fd_server, (struct sockaddr *)sock, slen);
> +	SAFE_LISTEN(fd_server, 10);
> +	SAFE_GETSOCKNAME(fd_server, (struct sockaddr *)sock, &slen);
> +
> +	pid = SAFE_FORK();
> +
> +	if (!pid) {
> +		int nfd = SAFE_ACCEPT(fd_server, NULL, NULL);
> +
> +		SAFE_CLOSE(nfd);
> +		exit(0);
> +	}
> +	SAFE_CLOSE(fd_server);
> +}

The single-accept server child is a nice simplification. Note that
fd_server is closed in the parent here but the static variable still
holds the (now-closed) fd number. cleanup() doesn't close fd_server,
which is correct since the parent already closed it. No issue, but
mentioning for clarity.

> +	if (fd_socket > 0)
> +		SAFE_CLOSE(fd_socket);
> +	if (fd_null > 0)
> +		SAFE_CLOSE(fd_null);
> +	if (fd_connected > 0)
> +		SAFE_CLOSE(fd_connected);

Nit: these guards should be `>= 0` (or `!= -1`) since a valid fd can
theoretically be 0 if stdin is closed before the test runs. Using `> 0`
works in practice (socket fds are typically > 2) but is technically
incorrect. This is a pre-existing LTP pattern, so not a blocker.

Patch 1 otherwise looks correct. The test case order is fine — after
ECONNREFUSED the TCP socket returns to TCP_CLOSE state, and the
subsequent EAFNOSUPPORT check happens before any connection attempt, so
fd_socket reuse is safe here.

---
Patch 2: connect01: Add negative tests

> +	{&fd_unix_dgram, &sock4, sizeof(sock4), EPROTOTYPE,
> +		"socket type does not support the protocol"},

The EPROTOTYPE errno is correct: connecting an AF_UNIX SOCK_DGRAM
socket to a path bound by a SOCK_STREAM server returns -EPROTOTYPE in
unix_find_other(). ✓

> +	{&fd_unix_stream, &sock4, sizeof(sock4), EACCES,
> +		"write permission is denied on the socket file"},

> +	SAFE_CHMOD(SOCK_FILE, 0700);

Correct: the socket file is owned by root with mode 0700. The "nobody"
user has no write permission on it, so connect() returns EACCES. ✓

> +static void verify_connect(unsigned int i)
> +{
> +	struct test_case_t *tc = &tcases[i];
> +	void *addr = tc->addr ? tc->addr : bad_addr;
> +
> +	if (tc->exp_errno == EACCES) {
> +		if (!SAFE_FORK()) {
> +			SAFE_SETUID(pw->pw_uid);
> +			TST_EXP_FAIL(sys_connect(*tc->fd, addr, tc->salen),
> +				tc->exp_errno, "%s", tc->desc);
> +			exit(0);
> +		}
> +	} else {

BUG: The parent never waits for the child. After SAFE_FORK() returns
the child PID (the `if (!SAFE_FORK())` branch is false in the parent),
the parent returns immediately from verify_connect() without reaping
the child. The child becomes a zombie and there is a race condition:
the next test iteration may start before the child has reported its
result via TST_EXP_FAIL.

The established LTP pattern for this (see e.g. msgget02.c) is to call
tst_reap_children() in the parent after the fork:

	if (tc->exp_errno == EACCES) {
		if (!SAFE_FORK()) {
			SAFE_SETUID(pw->pw_uid);
			TST_EXP_FAIL(sys_connect(*tc->fd, addr, tc->salen),
				tc->exp_errno, "%s", tc->desc);
			exit(0);
		}
		tst_reap_children();
	} else {
		TST_EXP_FAIL(sys_connect(*tc->fd, addr, tc->salen),
			tc->exp_errno, "%s", tc->desc);
	}

> +	.needs_root = 1,
> +	.needs_tmpdir = 1,

Both are necessary and correct given the chmod and socket file creation.

---
Summary:

- Patch 1: Reviewed, looks good (nits only, not blocking)
- Patch 2: Needs fix — add tst_reap_children() after the SAFE_FORK()
  block in verify_connect()

Please send a v2 with the fix applied to patch 2.

Thanks,

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

  reply	other threads:[~2026-06-04  7:15 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-14  4:51 [LTP] [PATCH v2 1/2] connect01: Convert to new API Yang Xu via ltp
2024-05-14  4:51 ` [LTP] [PATCH v2 2/2] connect01: Add negative tests Yang Xu via ltp
2024-07-17 12:30 ` [LTP] [PATCH v2 1/2] connect01: Convert to new API Cyril Hrubis
2026-05-25  9:02 ` [LTP] [PATCH v3 0/2] " Wei Gao via ltp
2026-05-25  9:02   ` [LTP] [PATCH v3 1/2] " Wei Gao via ltp
2026-05-25 10:12     ` [LTP] " linuxtestproject.agent
2026-05-28  4:43       ` Wei Gao via ltp
2026-06-04  4:49     ` [LTP] [PATCH v4 0/2] " Wei Gao via ltp
2026-06-04  4:49       ` [LTP] [PATCH v4 1/2] " Wei Gao via ltp
2026-06-04  7:15         ` linuxtestproject.agent [this message]
2026-06-16  1:47         ` [LTP] [PATCH v5 0/2] " Wei Gao via ltp
2026-06-16  1:47           ` [LTP] [PATCH v5 1/2] " Wei Gao via ltp
2026-06-16  4:07             ` [LTP] " linuxtestproject.agent
2026-06-16  5:24             ` [LTP] [PATCH v6 0/2] " Wei Gao via ltp
2026-06-16  5:24               ` [LTP] [PATCH v6 1/2] " Wei Gao via ltp
2026-06-16  8:35                 ` [LTP] " linuxtestproject.agent
2026-06-16  9:31                 ` [LTP] [PATCH v7 0/2] " Wei Gao via ltp
2026-06-16  9:31                   ` [LTP] [PATCH v7 1/2] " Wei Gao via ltp
2026-06-16  9:31                   ` [LTP] [PATCH v7 2/2] connect01: Add negative tests Wei Gao via ltp
2026-06-16  5:24               ` [LTP] [PATCH v6 " Wei Gao via ltp
2026-06-16  1:47           ` [LTP] [PATCH v5 " Wei Gao via ltp
2026-06-04  4:49       ` [LTP] [PATCH v4 " Wei Gao via ltp
2026-05-25  9:02   ` [LTP] [PATCH v3 " Wei Gao via ltp

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=20260604071525.4085-1-linuxtestproject.agent@gmail.com \
    --to=linuxtestproject.agent@gmail.com \
    --cc=ltp@lists.linux.it \
    --cc=wegao@suse.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.