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);
329 * pidfd_send_signal() will currently return ENOSYS when
330 * CONFIG_PROC_FS is not set.
334 "%s test: pidfd_send_signal() syscall not supported (Ensure that CONFIG_PROC_FS=y is set)\n",
337 ksft_exit_fail_msg("%s test: Failed to send signal\n",
342 ksft_test_result_pass(
343 "%s test: pidfd_send_signal() syscall is supported. Tests can be executed\n",
348 static void *test_pidfd_poll_exec_thread(void *priv)
350 ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
351 getpid(), syscall(SYS_gettid));
352 ksft_print_msg("Child Thread: doing exec of sleep\n");
354 execl("/bin/sleep", "sleep", str(CHILD_THREAD_MIN_WAIT), (char *)NULL);
356 ksft_print_msg("Child Thread: DONE. pid %d tid %d\n",
357 getpid(), syscall(SYS_gettid));
361 static void poll_pidfd(const char *test_name, int pidfd)
364 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
365 struct epoll_event event, events[MAX_EVENTS];
368 ksft_exit_fail_msg("%s test: Failed to create epoll file descriptor "
372 event.events = EPOLLIN;
373 event.data.fd = pidfd;
375 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pidfd, &event)) {
376 ksft_exit_fail_msg("%s test: Failed to add epoll file descriptor "
381 c = epoll_wait(epoll_fd, events, MAX_EVENTS, 5000);
382 if (c != 1 || !(events[0].events & EPOLLIN))
383 ksft_exit_fail_msg("%s test: Unexpected epoll_wait result (c=%d, events=%x) ",
385 test_name, c, events[0].events, errno);
392 static int child_poll_exec_test(void *args)
396 ksft_print_msg("Child (pidfd): starting. pid %d tid %d\n", getpid(),
397 syscall(SYS_gettid));
398 pthread_create(&t1, NULL, test_pidfd_poll_exec_thread, NULL);
400 * Exec in the non-leader thread will destroy the leader immediately.
401 * If the wait in the parent returns too soon, the test fails.
407 static void test_pidfd_poll_exec(int use_waitpid)
412 time_t prog_start = time(NULL);
413 const char *test_name = "pidfd_poll check for premature notification on child thread exec";
415 ksft_print_msg("Parent: pid: %d\n", getpid());
416 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_exec_test);
418 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
419 test_name, pid, errno);
421 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
424 ret = waitpid(pid, &status, 0);
426 ksft_print_msg("Parent: error\n");
429 ksft_print_msg("Parent: Child process waited for.\n");
431 poll_pidfd(test_name, pidfd);
434 time_t prog_time = time(NULL) - prog_start;
436 ksft_print_msg("Time waited for child: %lu\n", prog_time);
440 if (prog_time < CHILD_THREAD_MIN_WAIT || prog_time > CHILD_THREAD_MIN_WAIT + 2)
441 ksft_exit_fail_msg("%s test: Failed\n", test_name);
443 ksft_test_result_pass("%s test: Passed\n", test_name);
446 static void *test_pidfd_poll_leader_exit_thread(void *priv)
448 ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
449 getpid(), syscall(SYS_gettid));
450 sleep(CHILD_THREAD_MIN_WAIT);
451 ksft_print_msg("Child Thread: DONE. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
455 static time_t *child_exit_secs;
456 static int child_poll_leader_exit_test(void *args)
460 ksft_print_msg("Child: starting. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
461 pthread_create(&t1, NULL, test_pidfd_poll_leader_exit_thread, NULL);
462 pthread_create(&t2, NULL, test_pidfd_poll_leader_exit_thread, NULL);
465 * glibc exit calls exit_group syscall, so explicity call exit only
466 * so that only the group leader exits, leaving the threads alone.
468 *child_exit_secs = time(NULL);
469 syscall(SYS_exit, 0);
472 static void test_pidfd_poll_leader_exit(int use_waitpid)
476 time_t prog_start = time(NULL);
477 const char *test_name = "pidfd_poll check for premature notification on non-empty"
480 child_exit_secs = mmap(NULL, sizeof *child_exit_secs, PROT_READ | PROT_WRITE,
481 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
483 if (child_exit_secs == MAP_FAILED)
484 ksft_exit_fail_msg("%s test: mmap failed (errno %d)\n",
487 ksft_print_msg("Parent: pid: %d\n", getpid());
488 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_leader_exit_test);
490 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
491 test_name, pid, errno);
493 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
496 ret = waitpid(pid, &status, 0);
498 ksft_print_msg("Parent: error\n");
501 * This sleep tests for the case where if the child exits, and is in
502 * EXIT_ZOMBIE, but the thread group leader is non-empty, then the poll
503 * doesn't prematurely return even though there are active threads
506 poll_pidfd(test_name, pidfd);
510 ksft_print_msg("Parent: Child process waited for.\n");
512 time_t since_child_exit = time(NULL) - *child_exit_secs;
514 ksft_print_msg("Time since child exit: %lu\n", since_child_exit);
518 if (since_child_exit < CHILD_THREAD_MIN_WAIT ||
519 since_child_exit > CHILD_THREAD_MIN_WAIT + 2)
520 ksft_exit_fail_msg("%s test: Failed\n", test_name);
522 ksft_test_result_pass("%s test: Passed\n", test_name);
525 int main(int argc, char **argv)
530 test_pidfd_poll_exec(0);
531 test_pidfd_poll_exec(1);
532 test_pidfd_poll_leader_exit(0);
533 test_pidfd_poll_leader_exit(1);
534 test_pidfd_send_signal_syscall_support();
535 test_pidfd_send_signal_simple_success();
536 test_pidfd_send_signal_exited_fail();
537 test_pidfd_send_signal_recycled_pid_fail();
539 return ksft_exit_pass();