[LTP] [PATCH v9] connect03: New test case for EPROTOTYPE and EACCES errors
Andrea Cervesato
andrea.cervesato@suse.com
Thu Jul 9 18:43:06 CEST 2026
Hi Wei,
we can remove privileged user needs for this test.
> +static int sys_connect(int sockfd, const struct sockaddr *addr,
> + socklen_t addrlen)
> +{
> + return tst_syscall(__NR_connect, sockfd, addr, addrlen);
> +}
we should probably use connect() here instead of raw syscall.
> +
> +static void setup(void)
> +{
> + sock_un.sun_family = AF_UNIX;
> + strncpy(sock_un.sun_path, SOCK_FILE, sizeof(sock_un.sun_path));
> +
> + fd_unix_server = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
> + SAFE_BIND(fd_unix_server, (struct sockaddr *)&sock_un, sizeof(sock_un));
> + SAFE_CHMOD(SOCK_FILE, 0700);
> + SAFE_LISTEN(fd_unix_server, 5);
> +
> + fd_unix_dgram = SAFE_SOCKET(AF_UNIX, SOCK_DGRAM, 0);
> + fd_unix_stream = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
> +
> + pw = SAFE_GETPWNAM("nobody");
we only need this to trigger EACCES, so probably chmod(0500) is enough.
Read below.
> +}
> +
> +static void cleanup(void)
> +{
> + if (fd_unix_dgram != -1)
> + SAFE_CLOSE(fd_unix_dgram);
> + if (fd_unix_stream != -1)
> + SAFE_CLOSE(fd_unix_stream);
> + if (fd_unix_server != -1)
> + SAFE_CLOSE(fd_unix_server);
> +}
> +
> +static void verify_connect(unsigned int i)
> +{
> + struct test_case_t *tc = &tcases[i];
> +
> + if (tc->exp_errno == EACCES) {
> + if (!SAFE_FORK()) {
> + SAFE_SETUID(pw->pw_uid);
> + TST_EXP_FAIL(sys_connect(*tc->fd, (const struct sockaddr *)tc->addr, tc->salen),
> + tc->exp_errno, "%s", tc->desc);
> + exit(0);
> + }
> + tst_reap_children();
> + } else {
> + TST_EXP_FAIL(sys_connect(*tc->fd, (const struct sockaddr *)tc->addr, tc->salen),
> + tc->exp_errno, "%s", tc->desc);
> + }
This can easily become:
SAFE_CHMOD(SOCK_FILE, tc->mode);
TST_EXP_FAIL(connect(*tc->fd, (struct sockaddr *)&sock_un, sizeof(sock_un)),
tc->exp_errno, "%s", tc->desc);
To make it works, we need a mode attribute in the tcase struct:
- 0700 for EPROTOTYPE
- 0500 for EACCES
> +}
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .cleanup = cleanup,
> + .tcnt = ARRAY_SIZE(tcases),
> + .test = verify_connect,
> + .forks_child = 1,
this is not needed anymore.
> + .needs_root = 1,
And root can go away.
> + .needs_tmpdir = 1,
> +};
> --
> 2.54.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
More information about the ltp
mailing list