[LTP] [PATCH v3 3/8] Add ioctl_pidfd01 test

Cyril Hrubis chrubis@suse.cz
Wed Jul 9 11:16:50 CEST 2025


Hi!
> Verify that ioctl() raises the right errors when an application provides
> the wrong file descriptor.
> 
> Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  runtest/syscalls                                |  2 +
>  testcases/kernel/syscalls/ioctl/.gitignore      |  1 +
>  testcases/kernel/syscalls/ioctl/ioctl_pidfd.h   | 38 ++++++++++++++++
>  testcases/kernel/syscalls/ioctl/ioctl_pidfd01.c | 58 +++++++++++++++++++++++++
>  4 files changed, 99 insertions(+)
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 582422ac9ca8ccae598c626a11cf6ee7c30f0e3a..7f6312ce5fa241a778d8dda7f8ee9edd0a8800e6 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -612,6 +612,8 @@ ioctl_ficlonerange01 ioctl_ficlonerange01
>  ioctl_ficlonerange02 ioctl_ficlonerange02
>  ioctl_fiemap01 ioctl_fiemap01
>  
> +ioctl_pidfd01 ioctl_pidfd01
> +
>  inotify_init1_01 inotify_init1_01
>  inotify_init1_02 inotify_init1_02
>  
> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
> index 53a82bb5770ba196811965150fd262ec5d4a6e01..aa952c1a7bae0ae2dbb04de0595f10d508b6759a 100644
> --- a/testcases/kernel/syscalls/ioctl/.gitignore
> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
> @@ -29,3 +29,4 @@
>  /ioctl_ficlonerange01
>  /ioctl_ficlonerange02
>  /ioctl_fiemap01
> +/ioctl_pidfd01
> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_pidfd.h b/testcases/kernel/syscalls/ioctl/ioctl_pidfd.h
> new file mode 100644
> index 0000000000000000000000000000000000000000..1a87d90982c353d8ca75140e405b42a24be4408d
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ioctl/ioctl_pidfd.h
> @@ -0,0 +1,38 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +/*
> + * Copyright (c) 2025 Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +#ifndef IOCTL_PIDFD_H
> +#define IOCTL_PIDFD_H
> +
> +#include "tst_test.h"
> +#include "lapi/pidfd.h"
> +
> +static inline int ioctl_pidfd_supported(void)

This should be named ioctl_pidfd_info_exit_supported() since we are
checking specially for the IOCTL_INFO_EXIT flag.

> +{
> +	int ret = 0;
> +	pid_t pid;
> +	int pidfd;
> +	struct pidfd_info info;

We have to assume the support is in if the kernel is new enough,
otherwise we will not detect when the code in kernel breaks when it's
suposed to work:

	if (tst_kvercmp(6, 15, 0) >= 0)
		return 1;

> +	memset(&info, 0, sizeof(struct pidfd_info));
> +	info.mask = PIDFD_INFO_EXIT;
> +
> +	pid = SAFE_FORK();
> +	if (!pid)
> +		exit(100);
> +
> +	pidfd = SAFE_PIDFD_OPEN(pid, 0);
> +	SAFE_WAITPID(pid, NULL, 0);
> +
> +	SAFE_IOCTL(pidfd, PIDFD_GET_INFO, &info);
> +	SAFE_CLOSE(pidfd);
> +
> +	if (info.mask & PIDFD_INFO_EXIT)
> +		ret = 1;
> +
> +	return ret;
> +}
> +
> +#endif
> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_pidfd01.c b/testcases/kernel/syscalls/ioctl/ioctl_pidfd01.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..d9231e79b15c0866bd4c965634f6b01c157da1ce
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ioctl/ioctl_pidfd01.c
> @@ -0,0 +1,58 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2025 Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*\
> + * Verify that ioctl() raises the right errors when an application provides
> + * the wrong file descriptor.
> + */
> +
> +#include "ioctl_pidfd.h"
> +
> +static int exp_errnos[] = {
> +	EINVAL,
> +	EBADF,
> +	ENOTTY,
> +};
> +
> +static struct pidfd_info *info;
> +
> +static void test_bad_pidfd(struct tst_fd *fd_in)
> +{
> +	if (fd_in->type == TST_FD_PIDFD) {
> +		tst_res(TINFO, "Skipping pidfd: SUCCESS");
> +		return;
> +	}
> +
> +	TST_EXP_FAIL_ARR(ioctl(fd_in->fd, PIDFD_GET_INFO, info),
> +		  exp_errnos, ARRAY_SIZE(exp_errnos),
> +		  "ioctl(%s, PIDFD_GET_INFO, info)",
> +		  tst_fd_desc(fd_in));
> +}
> +
> +static void run(void)
> +{
> +	TST_FD_FOREACH(fd) {
> +		tst_res(TINFO, "%s -> ...", tst_fd_desc(&fd));
> +		test_bad_pidfd(&fd);
> +	}
> +}
> +
> +static void setup(void)
> +{
> +	if (!ioctl_pidfd_supported())
> +		tst_brk(TCONF, "PIDFD_INFO_EXIT is not supported by ioctl()");
> +
> +	info->mask = PIDFD_INFO_EXIT;
> +}
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.setup = setup,
> +	.forks_child = 1,
> +	.bufs = (struct tst_buffers []) {
> +		{&info, .size = sizeof(*info)},
> +		{}
> +	}
> +};
> 
> -- 
> 2.50.0
> 

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list