1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
14 #include <sys/epoll.h>
16 #include <sys/mount.h>
22 #include "../kselftest.h"
24 #define str(s) _str(s)
26 #define CHILD_THREAD_MIN_WAIT 3 /* seconds */
30 static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *))
32 size_t stack_size = 1024;
33 char *stack[1024] = { 0 };
36 return __clone2(fn, stack, stack_size, flags | SIGCHLD, NULL, pidfd);
38 return clone(fn, stack + stack_size, flags | SIGCHLD, NULL, pidfd);
42 static int signal_received;
44 static void set_signal_received_on_sigusr1(int sig)
51 * Straightforward test to see whether pidfd_send_signal() works is to send
52 * a signal to ourself.
54 static int test_pidfd_send_signal_simple_success(void)
57 const char *test_name = "pidfd_send_signal send SIGUSR1";
59 pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
62 "%s test: Failed to open process file descriptor\n",
65 signal(SIGUSR1, set_signal_received_on_sigusr1);
67 ret = sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0);
70 ksft_exit_fail_msg("%s test: Failed to send signal\n",
73 if (signal_received != 1)
74 ksft_exit_fail_msg("%s test: Failed to receive signal\n",
78 ksft_test_result_pass("%s test: Sent signal\n", test_name);
82 static int test_pidfd_send_signal_exited_fail(void)
84 int pidfd, ret, saved_errno;
87 const char *test_name = "pidfd_send_signal signal exited process";
91 ksft_exit_fail_msg("%s test: Failed to create new process\n",
97 snprintf(buf, sizeof(buf), "/proc/%d", pid);
99 pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
101 (void)wait_for_pid(pid);
105 "%s test: Failed to open process file descriptor\n",
108 ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
113 "%s test: Managed to send signal to process even though it should have failed\n",
116 if (saved_errno != ESRCH)
118 "%s test: Expected to receive ESRCH as errno value but received %d instead\n",
119 test_name, saved_errno);
121 ksft_test_result_pass("%s test: Failed to send signal as expected\n",
127 * Maximum number of cycles we allow. This is equivalent to PID_MAX_DEFAULT.
128 * If users set a higher limit or we have cycled PIDFD_MAX_DEFAULT number of
129 * times then we skip the test to not go into an infinite loop or block for a
132 #define PIDFD_MAX_DEFAULT 0x8000
134 static int test_pidfd_send_signal_recycled_pid_fail(void)
138 const char *test_name = "pidfd_send_signal signal recycled pid";
140 ret = unshare(CLONE_NEWPID);
142 ksft_exit_fail_msg("%s test: Failed to unshare pid namespace\n",
145 ret = unshare(CLONE_NEWNS);
148 "%s test: Failed to unshare mount namespace\n",
151 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
153 ksft_exit_fail_msg("%s test: Failed to remount / private\n",
156 /* pid 1 in new pid namespace */
159 ksft_exit_fail_msg("%s test: Failed to create new process\n",
167 (void)umount2("/proc", MNT_DETACH);
168 ret = mount("proc", "/proc", "proc", 0, NULL);
172 /* grab pid PID_RECYCLE */
173 for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
181 if (pid2 == PID_RECYCLE) {
182 snprintf(buf, sizeof(buf), "/proc/%d", pid2);
183 ksft_print_msg("pid to recycle is %d\n", pid2);
184 pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
187 if (wait_for_pid(pid2))
190 if (pid2 >= PID_RECYCLE)
195 * We want to be as predictable as we can so if we haven't been
196 * able to grab pid PID_RECYCLE skip the test.
198 if (pid2 != PID_RECYCLE) {
207 for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
211 int child_ret = PIDFD_PASS;
213 ret = pipe2(pipe_fds, O_CLOEXEC);
217 recycled_pid = fork();
218 if (recycled_pid < 0)
221 if (recycled_pid == 0) {
223 (void)read(pipe_fds[0], &c, 1);
230 * Stop the child so we can inspect whether we have
231 * recycled pid PID_RECYCLE.
234 ret = kill(recycled_pid, SIGSTOP);
237 (void)wait_for_pid(recycled_pid);
242 * We have recycled the pid. Try to signal it. This
243 * needs to fail since this is a different process than
244 * the one the pidfd refers to.
246 if (recycled_pid == PID_RECYCLE) {
247 ret = sys_pidfd_send_signal(pidfd, SIGCONT,
249 if (ret && errno == ESRCH)
250 child_ret = PIDFD_XFAIL;
252 child_ret = PIDFD_FAIL;
255 /* let the process move on */
256 ret = kill(recycled_pid, SIGCONT);
258 (void)kill(recycled_pid, SIGKILL);
260 if (wait_for_pid(recycled_pid))
276 * If the user set a custom pid_max limit we could be
278 * Skip the test in this case.
280 if (recycled_pid > PIDFD_MAX_DEFAULT)
284 /* failed to recycle pid */
288 ret = wait_for_pid(pid1);
292 "%s test: Managed to signal recycled pid %d\n",
293 test_name, PID_RECYCLE);
295 ksft_exit_fail_msg("%s test: Failed to recycle pid %d\n",
296 test_name, PID_RECYCLE);
298 ksft_print_msg("%s test: Skipping test\n", test_name);
302 ksft_test_result_pass(
303 "%s test: Failed to signal recycled pid as expected\n",
307 default /* PIDFD_ERROR */:
308 ksft_exit_fail_msg("%s test: Error while running tests\n",
315 static int test_pidfd_send_signal_syscall_support(void)
318 const char *test_name = "pidfd_send_signal check for support";
320 pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
323 "%s test: Failed to open process file descriptor\n",
326 ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
330 "%s test: pidfd_send_signal() syscall not supported\n",
333 ksft_exit_fail_msg("%s test: Failed to send signal\n",
338 ksft_test_result_pass(
339 "%s test: pidfd_send_signal() syscall is supported. Tests can be executed\n",
344 static void *test_pidfd_poll_exec_thread(void *priv)
346 ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
347 getpid(), syscall(SYS_gettid));
348 ksft_print_msg("Child Thread: doing exec of sleep\n");
350 execl("/bin/sleep", "sleep", str(CHILD_THREAD_MIN_WAIT), (char *)NULL);
352 ksft_print_msg("Child Thread: DONE. pid %d tid %d\n",
353 getpid(), syscall(SYS_gettid));
357 static void poll_pidfd(const char *test_name, int pidfd)
360 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
361 struct epoll_event event, events[MAX_EVENTS];
364 ksft_exit_fail_msg("%s test: Failed to create epoll file descriptor "
368 event.events = EPOLLIN;
369 event.data.fd = pidfd;
371 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pidfd, &event)) {
372 ksft_exit_fail_msg("%s test: Failed to add epoll file descriptor "
377 c = epoll_wait(epoll_fd, events, MAX_EVENTS, 5000);
378 if (c != 1 || !(events[0].events & EPOLLIN))
379 ksft_exit_fail_msg("%s test: Unexpected epoll_wait result (c=%d, events=%x) ",
381 test_name, c, events[0].events, errno);
388 static int child_poll_exec_test(void *args)
392 ksft_print_msg("Child (pidfd): starting. pid %d tid %d\n", getpid(),
393 syscall(SYS_gettid));
394 pthread_create(&t1, NULL, test_pidfd_poll_exec_thread, NULL);
396 * Exec in the non-leader thread will destroy the leader immediately.
397 * If the wait in the parent returns too soon, the test fails.
403 static void test_pidfd_poll_exec(int use_waitpid)
408 time_t prog_start = time(NULL);
409 const char *test_name = "pidfd_poll check for premature notification on child thread exec";
411 ksft_print_msg("Parent: pid: %d\n", getpid());
412 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_exec_test);
414 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
415 test_name, pid, errno);
417 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
420 ret = waitpid(pid, &status, 0);
422 ksft_print_msg("Parent: error\n");
425 ksft_print_msg("Parent: Child process waited for.\n");
427 poll_pidfd(test_name, pidfd);
430 time_t prog_time = time(NULL) - prog_start;
432 ksft_print_msg("Time waited for child: %lu\n", prog_time);
436 if (prog_time < CHILD_THREAD_MIN_WAIT || prog_time > CHILD_THREAD_MIN_WAIT + 2)
437 ksft_exit_fail_msg("%s test: Failed\n", test_name);
439 ksft_test_result_pass("%s test: Passed\n", test_name);
442 static void *test_pidfd_poll_leader_exit_thread(void *priv)
444 ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
445 getpid(), syscall(SYS_gettid));
446 sleep(CHILD_THREAD_MIN_WAIT);
447 ksft_print_msg("Child Thread: DONE. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
451 static time_t *child_exit_secs;
452 static int child_poll_leader_exit_test(void *args)
456 ksft_print_msg("Child: starting. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
457 pthread_create(&t1, NULL, test_pidfd_poll_leader_exit_thread, NULL);
458 pthread_create(&t2, NULL, test_pidfd_poll_leader_exit_thread, NULL);
461 * glibc exit calls exit_group syscall, so explicity call exit only
462 * so that only the group leader exits, leaving the threads alone.
464 *child_exit_secs = time(NULL);
465 syscall(SYS_exit, 0);
468 static void test_pidfd_poll_leader_exit(int use_waitpid)
472 time_t prog_start = time(NULL);
473 const char *test_name = "pidfd_poll check for premature notification on non-empty"
476 child_exit_secs = mmap(NULL, sizeof *child_exit_secs, PROT_READ | PROT_WRITE,
477 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
479 if (child_exit_secs == MAP_FAILED)
480 ksft_exit_fail_msg("%s test: mmap failed (errno %d)\n",
483 ksft_print_msg("Parent: pid: %d\n", getpid());
484 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_leader_exit_test);
486 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
487 test_name, pid, errno);
489 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
492 ret = waitpid(pid, &status, 0);
494 ksft_print_msg("Parent: error\n");
497 * This sleep tests for the case where if the child exits, and is in
498 * EXIT_ZOMBIE, but the thread group leader is non-empty, then the poll
499 * doesn't prematurely return even though there are active threads
502 poll_pidfd(test_name, pidfd);
506 ksft_print_msg("Parent: Child process waited for.\n");
508 time_t since_child_exit = time(NULL) - *child_exit_secs;
510 ksft_print_msg("Time since child exit: %lu\n", since_child_exit);
514 if (since_child_exit < CHILD_THREAD_MIN_WAIT ||
515 since_child_exit > CHILD_THREAD_MIN_WAIT + 2)
516 ksft_exit_fail_msg("%s test: Failed\n", test_name);
518 ksft_test_result_pass("%s test: Passed\n", test_name);
521 int main(int argc, char **argv)
526 test_pidfd_poll_exec(0);
527 test_pidfd_poll_exec(1);
528 test_pidfd_poll_leader_exit(0);
529 test_pidfd_poll_leader_exit(1);
530 test_pidfd_send_signal_syscall_support();
531 test_pidfd_send_signal_simple_success();
532 test_pidfd_send_signal_exited_fail();
533 test_pidfd_send_signal_recycled_pid_fail();
535 return ksft_exit_pass();