[LTP] [PATCH v4 4/8] Add ioctl_pidfd02 test

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


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

Check if the ioctl() function allows retrieval of a child's exit code
using PIDFD_INFO_EXIT from a process that can be isolated or not from
the child.

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/syscalls                                |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore      |  1 +
 testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c | 85 +++++++++++++++++++++++++
 3 files changed, 87 insertions(+)

diff --git a/runtest/syscalls b/runtest/syscalls
index 2075c5d908c04bf31fcf57bba5cdf7af3e432237..fbb38b1a52ed9d7f6634336d83c226b5cce522e8 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -614,6 +614,7 @@ ioctl_ficlonerange02 ioctl_ficlonerange02
 ioctl_fiemap01 ioctl_fiemap01
 
 ioctl_pidfd01 ioctl_pidfd01
+ioctl_pidfd02 ioctl_pidfd02
 
 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 72d6a309ee0dcbd185a0248b9ffc79fc83b7f04f..a984744a67173b70872446823a6fdeb6a44fe2c8 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -31,3 +31,4 @@
 /ioctl_ficlonerange02
 /ioctl_fiemap01
 /ioctl_pidfd01
+/ioctl_pidfd02
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c b/testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c
new file mode 100644
index 0000000000000000000000000000000000000000..858ec461af373667c1fea1a16f7cb5e4f516b758
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * Check if the ioctl() function allows retrieval of a child's exit code
+ * using PIDFD_INFO_EXIT from a process that can be isolated or not from the
+ * child.
+ */
+
+#include "ioctl_pidfd.h"
+#include "lapi/sched.h"
+
+static struct tst_clone_args *args;
+static struct pidfd_info *info0, *info1;
+
+static void run(unsigned int isolate)
+{
+	int status;
+	int pidfd = 0;
+	pid_t pid_child;
+
+	memset(args, 0, sizeof(struct tst_clone_args));
+	memset(info0, 0, sizeof(struct pidfd_info));
+	memset(info1, 0, sizeof(struct pidfd_info));
+
+	if (isolate) {
+		args->flags = CLONE_PIDFD | CLONE_NEWUSER | CLONE_NEWPID;
+		args->pidfd = (uint64_t)&pidfd;
+		args->exit_signal = SIGCHLD;
+
+		pid_child = SAFE_CLONE(args);
+	} else {
+		pid_child = SAFE_FORK();
+	}
+
+	if (!pid_child) {
+		TST_CHECKPOINT_WAIT(0);
+		exit(100);
+	}
+
+	if (!isolate)
+		pidfd = SAFE_PIDFD_OPEN(pid_child, 0);
+
+	/* child is not reaped and ioctl() won't provide any exit status info */
+	info0->mask = PIDFD_INFO_EXIT;
+	SAFE_IOCTL(pidfd, PIDFD_GET_INFO, info0);
+	TST_EXP_EQ_LI(info0->mask & PIDFD_INFO_EXIT, 0);
+	TST_EXP_EQ_LI(info0->exit_code, 0);
+
+	TST_CHECKPOINT_WAKE(0);
+
+	SAFE_WAITPID(pid_child, &status, 0);
+
+	/* child is now reaped and ioctl() will provide the exit status */
+	info1->mask = PIDFD_INFO_EXIT;
+	SAFE_IOCTL(pidfd, PIDFD_GET_INFO, info1);
+	SAFE_CLOSE(pidfd);
+
+	TST_EXP_EQ_LI(info1->mask & PIDFD_INFO_EXIT, PIDFD_INFO_EXIT);
+	TST_EXP_EQ_LI(info1->exit_code, status);
+
+	TST_EXP_EQ_LI(WEXITSTATUS(info1->exit_code), 100);
+}
+
+static void setup(void)
+{
+	if (!ioctl_pidfd_info_exit_supported())
+		tst_brk(TCONF, "PIDFD_INFO_EXIT is not supported by ioctl()");
+}
+
+static struct tst_test test = {
+	.test = run,
+	.setup = setup,
+	.tcnt = 2,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+	.bufs = (struct tst_buffers []) {
+		{&args, .size = sizeof(*args)},
+		{&info0, .size = sizeof(*info0)},
+		{&info1, .size = sizeof(*info1)},
+		{}
+	}
+};

-- 
2.50.1



More information about the ltp mailing list