]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/pidfd: add seventh PIDFD_INFO_EXIT selftest
authorChristian Brauner <brauner@kernel.org>
Wed, 5 Mar 2025 10:08:26 +0000 (11:08 +0100)
committerChristian Brauner <brauner@kernel.org>
Wed, 5 Mar 2025 12:26:21 +0000 (13:26 +0100)
Add a selftest for PIDFD_INFO_EXIT behavior.

Link: https://lore.kernel.org/r/20250305-work-pidfs-kill_on_last_close-v3-16-c8c3d8361705@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
tools/testing/selftests/pidfd/.gitignore
tools/testing/selftests/pidfd/Makefile
tools/testing/selftests/pidfd/pidfd.h
tools/testing/selftests/pidfd/pidfd_exec_helper.c [new file with mode: 0644]
tools/testing/selftests/pidfd/pidfd_info_test.c

index bddae1d4d7e438d18a048d88bd32e896c18376e1..0406a065deb4b1d5deba146acfd2c8452f4aedd5 100644 (file)
@@ -9,3 +9,4 @@ pidfd_setns_test
 pidfd_file_handle_test
 pidfd_bind_mount
 pidfd_info_test
+pidfd_exec_helper
index a94c2bc8d59411b350edbdc6ec261690d9364412..fcbefc0d77f6b15e219fec9d03d6869c285eeda3 100644 (file)
@@ -5,5 +5,7 @@ TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
        pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
        pidfd_file_handle_test pidfd_bind_mount pidfd_info_test
 
+TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper
+
 include ../lib.mk
 
index fee6fd3e67ddd113233066461f0f9719a4d5027f..cec22aa11cdfe2ab2f39ac172f52da0e7d8a274e 100644 (file)
@@ -254,4 +254,11 @@ static inline ssize_t write_nointr(int fd, const void *buf, size_t count)
        return ret;
 }
 
+static inline int sys_execveat(int dirfd, const char *pathname,
+                              char *const argv[], char *const envp[],
+                              int flags)
+{
+        return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
+}
+
 #endif /* __PIDFD_H */
diff --git a/tools/testing/selftests/pidfd/pidfd_exec_helper.c b/tools/testing/selftests/pidfd/pidfd_exec_helper.c
new file mode 100644 (file)
index 0000000..5516808
--- /dev/null
@@ -0,0 +1,12 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+       if (pause())
+               _exit(EXIT_FAILURE);
+
+       _exit(EXIT_SUCCESS);
+}
index 5e86e3df323bbd6319b8257fca8a03f16b9d09c8..09bc4ae7aed55a175a26e03a1e695c4fc6410cd7 100644 (file)
@@ -369,4 +369,129 @@ TEST_F(pidfd_info, thread_group)
        EXPECT_EQ(close(pidfd_thread), 0);
 }
 
+static void *pidfd_info_thread_exec(void *arg)
+{
+       pid_t pid_thread = gettid();
+       int ipc_socket = *(int *)arg;
+
+       /* Inform the grand-parent what the tid of this thread is. */
+       if (write_nointr(ipc_socket, &pid_thread, sizeof(pid_thread)) != sizeof(pid_thread))
+               return NULL;
+
+       if (read_nointr(ipc_socket, &pid_thread, sizeof(pid_thread)) != sizeof(pid_thread))
+               return NULL;
+
+       close(ipc_socket);
+
+       sys_execveat(AT_FDCWD, "pidfd_exec_helper", NULL, NULL, 0);
+       return NULL;
+}
+
+TEST_F(pidfd_info, thread_group_exec)
+{
+       pid_t pid_leader, pid_thread;
+       pthread_t thread;
+       int nevents, pidfd_leader, pidfd_leader_thread, pidfd_thread, ret;
+       int ipc_sockets[2];
+       struct pollfd fds = {};
+       struct pidfd_info info = {
+               .mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT,
+       };
+
+       ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
+       EXPECT_EQ(ret, 0);
+
+       pid_leader = create_child(&pidfd_leader, 0);
+       EXPECT_GE(pid_leader, 0);
+
+       if (pid_leader == 0) {
+               close(ipc_sockets[0]);
+
+               /* The thread will outlive the thread-group leader. */
+               if (pthread_create(&thread, NULL, pidfd_info_thread_exec, &ipc_sockets[1]))
+                       syscall(__NR_exit, EXIT_FAILURE);
+
+               /* Make the thread-group leader exit prematurely. */
+               syscall(__NR_exit, EXIT_SUCCESS);
+       }
+
+       /* Retrieve the tid of the thread. */
+       EXPECT_EQ(close(ipc_sockets[1]), 0);
+       ASSERT_EQ(read_nointr(ipc_sockets[0], &pid_thread, sizeof(pid_thread)), sizeof(pid_thread));
+
+       /* Opening a thread as a PIDFD_THREAD must succeed. */
+       pidfd_thread = sys_pidfd_open(pid_thread, PIDFD_THREAD);
+       ASSERT_GE(pidfd_thread, 0);
+
+       /* Open a thread-specific pidfd for the thread-group leader. */
+       pidfd_leader_thread = sys_pidfd_open(pid_leader, PIDFD_THREAD);
+       ASSERT_GE(pidfd_leader_thread, 0);
+
+       /*
+        * We can poll and wait for the old thread-group leader to exit
+        * using a thread-specific pidfd.
+        *
+        * This only works until the thread has execed. When the thread
+        * has execed it will have taken over the old thread-group
+        * leaders struct pid. Calling poll after the thread execed will
+        * thus block again because a new thread-group has started (Yes,
+        * it's fscked.).
+        */
+       fds.events = POLLIN;
+       fds.fd = pidfd_leader_thread;
+       nevents = poll(&fds, 1, -1);
+       ASSERT_EQ(nevents, 1);
+       /* The thread-group leader has exited. */
+       ASSERT_TRUE(!!(fds.revents & POLLIN));
+       /* The thread-group leader hasn't been reaped. */
+       ASSERT_FALSE(!!(fds.revents & POLLHUP));
+
+       /* Now that we've opened a thread-specific pidfd the thread can exec. */
+       ASSERT_EQ(write_nointr(ipc_sockets[0], &pid_thread, sizeof(pid_thread)), sizeof(pid_thread));
+       EXPECT_EQ(close(ipc_sockets[0]), 0);
+
+       /* Wait until the kernel has SIGKILLed the thread. */
+       fds.events = POLLHUP;
+       fds.fd = pidfd_thread;
+       nevents = poll(&fds, 1, -1);
+       ASSERT_EQ(nevents, 1);
+       /* The thread has been reaped. */
+       ASSERT_TRUE(!!(fds.revents & POLLHUP));
+
+       /* Retrieve thread-specific exit info from pidfd. */
+       ASSERT_EQ(ioctl(pidfd_thread, PIDFD_GET_INFO, &info), 0);
+       ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
+       ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
+       /*
+        * While the kernel will have SIGKILLed the whole thread-group
+        * during exec it will cause the individual threads to exit
+        * cleanly.
+        */
+       ASSERT_TRUE(WIFEXITED(info.exit_code));
+       ASSERT_EQ(WEXITSTATUS(info.exit_code), 0);
+
+       /*
+        * The thread-group leader is still alive, the thread has taken
+        * over its struct pid and thus its pid number.
+        */
+       info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
+       ASSERT_EQ(ioctl(pidfd_leader, PIDFD_GET_INFO, &info), 0);
+       ASSERT_TRUE(!!(info.mask & PIDFD_INFO_CREDS));
+       ASSERT_FALSE(!!(info.mask & PIDFD_INFO_EXIT));
+       ASSERT_EQ(info.pid, pid_leader);
+
+       /* Take down the thread-group leader. */
+       EXPECT_EQ(sys_pidfd_send_signal(pidfd_leader, SIGKILL, NULL, 0), 0);
+       EXPECT_EQ(sys_waitid(P_PIDFD, pidfd_leader, NULL, WEXITED), 0);
+
+       /* Retrieve exit information for the thread-group leader. */
+       info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
+       ASSERT_EQ(ioctl(pidfd_leader, PIDFD_GET_INFO, &info), 0);
+       ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
+       ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
+
+       EXPECT_EQ(close(pidfd_leader), 0);
+       EXPECT_EQ(close(pidfd_thread), 0);
+}
+
 TEST_HARNESS_MAIN