18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci#define _GNU_SOURCE 48c2ecf20Sopenharmony_ci#include <errno.h> 58c2ecf20Sopenharmony_ci#include <linux/sched.h> 68c2ecf20Sopenharmony_ci#include <linux/types.h> 78c2ecf20Sopenharmony_ci#include <signal.h> 88c2ecf20Sopenharmony_ci#include <stdint.h> 98c2ecf20Sopenharmony_ci#include <stdio.h> 108c2ecf20Sopenharmony_ci#include <stdlib.h> 118c2ecf20Sopenharmony_ci#include <sched.h> 128c2ecf20Sopenharmony_ci#include <string.h> 138c2ecf20Sopenharmony_ci#include <sys/resource.h> 148c2ecf20Sopenharmony_ci#include <sys/time.h> 158c2ecf20Sopenharmony_ci#include <sys/types.h> 168c2ecf20Sopenharmony_ci#include <sys/wait.h> 178c2ecf20Sopenharmony_ci#include <unistd.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include "pidfd.h" 208c2ecf20Sopenharmony_ci#include "../kselftest_harness.h" 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr))) 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* Attempt to de-conflict with the selftests tree. */ 258c2ecf20Sopenharmony_ci#ifndef SKIP 268c2ecf20Sopenharmony_ci#define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__) 278c2ecf20Sopenharmony_ci#endif 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic pid_t sys_clone3(struct clone_args *args) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci return syscall(__NR_clone3, args, sizeof(struct clone_args)); 328c2ecf20Sopenharmony_ci} 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic int sys_waitid(int which, pid_t pid, siginfo_t *info, int options, 358c2ecf20Sopenharmony_ci struct rusage *ru) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci return syscall(__NR_waitid, which, pid, info, options, ru); 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ciTEST(wait_simple) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci int pidfd = -1; 438c2ecf20Sopenharmony_ci pid_t parent_tid = -1; 448c2ecf20Sopenharmony_ci struct clone_args args = { 458c2ecf20Sopenharmony_ci .parent_tid = ptr_to_u64(&parent_tid), 468c2ecf20Sopenharmony_ci .pidfd = ptr_to_u64(&pidfd), 478c2ecf20Sopenharmony_ci .flags = CLONE_PIDFD | CLONE_PARENT_SETTID, 488c2ecf20Sopenharmony_ci .exit_signal = SIGCHLD, 498c2ecf20Sopenharmony_ci }; 508c2ecf20Sopenharmony_ci pid_t pid; 518c2ecf20Sopenharmony_ci siginfo_t info = { 528c2ecf20Sopenharmony_ci .si_signo = 0, 538c2ecf20Sopenharmony_ci }; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci pidfd = open("/proc/self", O_DIRECTORY | O_RDONLY | O_CLOEXEC); 568c2ecf20Sopenharmony_ci ASSERT_GE(pidfd, 0); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL); 598c2ecf20Sopenharmony_ci ASSERT_NE(pid, 0); 608c2ecf20Sopenharmony_ci EXPECT_EQ(close(pidfd), 0); 618c2ecf20Sopenharmony_ci pidfd = -1; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci pidfd = open("/dev/null", O_RDONLY | O_CLOEXEC); 648c2ecf20Sopenharmony_ci ASSERT_GE(pidfd, 0); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL); 678c2ecf20Sopenharmony_ci ASSERT_NE(pid, 0); 688c2ecf20Sopenharmony_ci EXPECT_EQ(close(pidfd), 0); 698c2ecf20Sopenharmony_ci pidfd = -1; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci pid = sys_clone3(&args); 728c2ecf20Sopenharmony_ci ASSERT_GE(pid, 0); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (pid == 0) 758c2ecf20Sopenharmony_ci exit(EXIT_SUCCESS); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL); 788c2ecf20Sopenharmony_ci ASSERT_GE(pid, 0); 798c2ecf20Sopenharmony_ci ASSERT_EQ(WIFEXITED(info.si_status), true); 808c2ecf20Sopenharmony_ci ASSERT_EQ(WEXITSTATUS(info.si_status), 0); 818c2ecf20Sopenharmony_ci EXPECT_EQ(close(pidfd), 0); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_signo, SIGCHLD); 848c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_code, CLD_EXITED); 858c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_pid, parent_tid); 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ciTEST(wait_states) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci int pidfd = -1; 918c2ecf20Sopenharmony_ci pid_t parent_tid = -1; 928c2ecf20Sopenharmony_ci struct clone_args args = { 938c2ecf20Sopenharmony_ci .parent_tid = ptr_to_u64(&parent_tid), 948c2ecf20Sopenharmony_ci .pidfd = ptr_to_u64(&pidfd), 958c2ecf20Sopenharmony_ci .flags = CLONE_PIDFD | CLONE_PARENT_SETTID, 968c2ecf20Sopenharmony_ci .exit_signal = SIGCHLD, 978c2ecf20Sopenharmony_ci }; 988c2ecf20Sopenharmony_ci int ret; 998c2ecf20Sopenharmony_ci pid_t pid; 1008c2ecf20Sopenharmony_ci siginfo_t info = { 1018c2ecf20Sopenharmony_ci .si_signo = 0, 1028c2ecf20Sopenharmony_ci }; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci pid = sys_clone3(&args); 1058c2ecf20Sopenharmony_ci ASSERT_GE(pid, 0); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci if (pid == 0) { 1088c2ecf20Sopenharmony_ci kill(getpid(), SIGSTOP); 1098c2ecf20Sopenharmony_ci kill(getpid(), SIGSTOP); 1108c2ecf20Sopenharmony_ci exit(EXIT_SUCCESS); 1118c2ecf20Sopenharmony_ci } 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WSTOPPED, NULL), 0); 1148c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_signo, SIGCHLD); 1158c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_code, CLD_STOPPED); 1168c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_pid, parent_tid); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci ASSERT_EQ(sys_pidfd_send_signal(pidfd, SIGCONT, NULL, 0), 0); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WCONTINUED, NULL), 0); 1218c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_signo, SIGCHLD); 1228c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_code, CLD_CONTINUED); 1238c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_pid, parent_tid); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WUNTRACED, NULL), 0); 1268c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_signo, SIGCHLD); 1278c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_code, CLD_STOPPED); 1288c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_pid, parent_tid); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci ASSERT_EQ(sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0), 0); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL), 0); 1338c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_signo, SIGCHLD); 1348c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_code, CLD_KILLED); 1358c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_pid, parent_tid); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci EXPECT_EQ(close(pidfd), 0); 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ciTEST(wait_nonblock) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci int pidfd, status = 0; 1438c2ecf20Sopenharmony_ci unsigned int flags = 0; 1448c2ecf20Sopenharmony_ci pid_t parent_tid = -1; 1458c2ecf20Sopenharmony_ci struct clone_args args = { 1468c2ecf20Sopenharmony_ci .parent_tid = ptr_to_u64(&parent_tid), 1478c2ecf20Sopenharmony_ci .flags = CLONE_PARENT_SETTID, 1488c2ecf20Sopenharmony_ci .exit_signal = SIGCHLD, 1498c2ecf20Sopenharmony_ci }; 1508c2ecf20Sopenharmony_ci int ret; 1518c2ecf20Sopenharmony_ci pid_t pid; 1528c2ecf20Sopenharmony_ci siginfo_t info = { 1538c2ecf20Sopenharmony_ci .si_signo = 0, 1548c2ecf20Sopenharmony_ci }; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci /* 1578c2ecf20Sopenharmony_ci * Callers need to see ECHILD with non-blocking pidfds when no child 1588c2ecf20Sopenharmony_ci * processes exists. 1598c2ecf20Sopenharmony_ci */ 1608c2ecf20Sopenharmony_ci pidfd = sys_pidfd_open(getpid(), PIDFD_NONBLOCK); 1618c2ecf20Sopenharmony_ci EXPECT_GE(pidfd, 0) { 1628c2ecf20Sopenharmony_ci /* pidfd_open() doesn't support PIDFD_NONBLOCK. */ 1638c2ecf20Sopenharmony_ci ASSERT_EQ(errno, EINVAL); 1648c2ecf20Sopenharmony_ci SKIP(return, "Skipping PIDFD_NONBLOCK test"); 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci ret = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL); 1688c2ecf20Sopenharmony_ci ASSERT_LT(ret, 0); 1698c2ecf20Sopenharmony_ci ASSERT_EQ(errno, ECHILD); 1708c2ecf20Sopenharmony_ci EXPECT_EQ(close(pidfd), 0); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci pid = sys_clone3(&args); 1738c2ecf20Sopenharmony_ci ASSERT_GE(pid, 0); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci if (pid == 0) { 1768c2ecf20Sopenharmony_ci kill(getpid(), SIGSTOP); 1778c2ecf20Sopenharmony_ci exit(EXIT_SUCCESS); 1788c2ecf20Sopenharmony_ci } 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci pidfd = sys_pidfd_open(pid, PIDFD_NONBLOCK); 1818c2ecf20Sopenharmony_ci EXPECT_GE(pidfd, 0) { 1828c2ecf20Sopenharmony_ci /* pidfd_open() doesn't support PIDFD_NONBLOCK. */ 1838c2ecf20Sopenharmony_ci ASSERT_EQ(errno, EINVAL); 1848c2ecf20Sopenharmony_ci SKIP(return, "Skipping PIDFD_NONBLOCK test"); 1858c2ecf20Sopenharmony_ci } 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci flags = fcntl(pidfd, F_GETFL, 0); 1888c2ecf20Sopenharmony_ci ASSERT_GT(flags, 0); 1898c2ecf20Sopenharmony_ci ASSERT_GT((flags & O_NONBLOCK), 0); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci /* 1928c2ecf20Sopenharmony_ci * Callers need to see EAGAIN/EWOULDBLOCK with non-blocking pidfd when 1938c2ecf20Sopenharmony_ci * child processes exist but none have exited. 1948c2ecf20Sopenharmony_ci */ 1958c2ecf20Sopenharmony_ci ret = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL); 1968c2ecf20Sopenharmony_ci ASSERT_LT(ret, 0); 1978c2ecf20Sopenharmony_ci ASSERT_EQ(errno, EAGAIN); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* 2008c2ecf20Sopenharmony_ci * Callers need to continue seeing 0 with non-blocking pidfd and 2018c2ecf20Sopenharmony_ci * WNOHANG raised explicitly when child processes exist but none have 2028c2ecf20Sopenharmony_ci * exited. 2038c2ecf20Sopenharmony_ci */ 2048c2ecf20Sopenharmony_ci ret = sys_waitid(P_PIDFD, pidfd, &info, WEXITED | WNOHANG, NULL); 2058c2ecf20Sopenharmony_ci ASSERT_EQ(ret, 0); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci ASSERT_EQ(fcntl(pidfd, F_SETFL, (flags & ~O_NONBLOCK)), 0); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WSTOPPED, NULL), 0); 2108c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_signo, SIGCHLD); 2118c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_code, CLD_STOPPED); 2128c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_pid, parent_tid); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci ASSERT_EQ(sys_pidfd_send_signal(pidfd, SIGCONT, NULL, 0), 0); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL), 0); 2178c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_signo, SIGCHLD); 2188c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_code, CLD_EXITED); 2198c2ecf20Sopenharmony_ci ASSERT_EQ(info.si_pid, parent_tid); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci EXPECT_EQ(close(pidfd), 0); 2228c2ecf20Sopenharmony_ci} 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ciTEST_HARNESS_MAIN 225