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

Andrea Cervesato andrea.cervesato@suse.de
Wed Jul 23 16:15:17 CEST 2025


From: Andrea Cervesato <andrea.cervesato@suse.com>

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   | 41 +++++++++++++++++
 testcases/kernel/syscalls/ioctl/ioctl_pidfd01.c | 58 +++++++++++++++++++++++++
 4 files changed, 102 insertions(+)

diff --git a/runtest/syscalls b/runtest/syscalls
index 3531c2a3c730ac2d8e3118ea32275dc90e95c0b1..2075c5d908c04bf31fcf57bba5cdf7af3e432237 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -613,6 +613,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 d10bf0a24bbc21057f399d14aef95f814b54f58d..72d6a309ee0dcbd185a0248b9ffc79fc83b7f04f 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -30,3 +30,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..8249ac753cf7fb8a3b749d55c7f0c3b30482c114
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_pidfd.h
@@ -0,0 +1,41 @@
+/* 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_info_exit_supported(void)
+{
+	int ret = 0;
+	pid_t pid;
+	int pidfd;
+	struct pidfd_info info;
+
+	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..92c51c6c0d0dcbb2308c1a8d82b2a92650f3a6b3
--- /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_info_exit_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.1



More information about the ltp mailing list