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 #ifndef __NR_pidfd_send_signal
25 #define __NR_pidfd_send_signal -1
28 #define str(s) _str(s)
30 #define CHILD_THREAD_MIN_WAIT 3 /* seconds */
35 #define CLONE_PIDFD 0x00001000
38 static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *))
40 size_t stack_size = 1024;
41 char *stack[1024] = { 0 };
44 return __clone2(fn, stack, stack_size, flags | SIGCHLD, NULL, pidfd);
46 return clone(fn, stack + stack_size, flags | SIGCHLD, NULL, pidfd);
50 static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
53 return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
56 static int signal_received;
58 static void set_signal_received_on_sigusr1(int sig)
65 * Straightforward test to see whether pidfd_send_signal() works is to send
66 * a signal to ourself.
68 static int test_pidfd_send_signal_simple_success(void)
71 const char *test_name = "pidfd_send_signal send SIGUSR1";
73 pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
76 "%s test: Failed to open process file descriptor\n",
79 signal(SIGUSR1, set_signal_received_on_sigusr1);
81 ret = sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0);
84 ksft_exit_fail_msg("%s test: Failed to send signal\n",
87 if (signal_received != 1)
88 ksft_exit_fail_msg("%s test: Failed to receive signal\n",
92 ksft_test_result_pass("%s test: Sent signal\n", test_name);
96 static int test_pidfd_send_signal_exited_fail(void)
98 int pidfd, ret, saved_errno;
101 const char *test_name = "pidfd_send_signal signal exited process";
105 ksft_exit_fail_msg("%s test: Failed to create new process\n",
111 snprintf(buf, sizeof(buf), "/proc/%d", pid);
113 pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
115 (void)wait_for_pid(pid);
119 "%s test: Failed to open process file descriptor\n",
122 ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
127 "%s test: Managed to send signal to process even though it should have failed\n",
130 if (saved_errno != ESRCH)
132 "%s test: Expected to receive ESRCH as errno value but received %d instead\n",
133 test_name, saved_errno);
135 ksft_test_result_pass("%s test: Failed to send signal as expected\n",
141 * Maximum number of cycles we allow. This is equivalent to PID_MAX_DEFAULT.
142 * If users set a higher limit or we have cycled PIDFD_MAX_DEFAULT number of
143 * times then we skip the test to not go into an infinite loop or block for a
146 #define PIDFD_MAX_DEFAULT 0x8000
148 static int test_pidfd_send_signal_recycled_pid_fail(void)
152 const char *test_name = "pidfd_send_signal signal recycled pid";
154 ret = unshare(CLONE_NEWPID);
156 ksft_exit_fail_msg("%s test: Failed to unshare pid namespace\n",
159 ret = unshare(CLONE_NEWNS);
162 "%s test: Failed to unshare mount namespace\n",
165 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
167 ksft_exit_fail_msg("%s test: Failed to remount / private\n",
170 /* pid 1 in new pid namespace */
173 ksft_exit_fail_msg("%s test: Failed to create new process\n",
181 (void)umount2("/proc", MNT_DETACH);
182 ret = mount("proc", "/proc", "proc", 0, NULL);
186 /* grab pid PID_RECYCLE */
187 for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
195 if (pid2 == PID_RECYCLE) {
196 snprintf(buf, sizeof(buf), "/proc/%d", pid2);
197 ksft_print_msg("pid to recycle is %d\n", pid2);
198 pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
201 if (wait_for_pid(pid2))
204 if (pid2 >= PID_RECYCLE)
209 * We want to be as predictable as we can so if we haven't been
210 * able to grab pid PID_RECYCLE skip the test.
212 if (pid2 != PID_RECYCLE) {
221 for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
225 int child_ret = PIDFD_PASS;
227 ret = pipe2(pipe_fds, O_CLOEXEC);
231 recycled_pid = fork();
232 if (recycled_pid < 0)
235 if (recycled_pid == 0) {
237 (void)read(pipe_fds[0], &c, 1);
244 * Stop the child so we can inspect whether we have
245 * recycled pid PID_RECYCLE.
248 ret = kill(recycled_pid, SIGSTOP);
251 (void)wait_for_pid(recycled_pid);
256 * We have recycled the pid. Try to signal it. This
257 * needs to fail since this is a different process than
258 * the one the pidfd refers to.
260 if (recycled_pid == PID_RECYCLE) {
261 ret = sys_pidfd_send_signal(pidfd, SIGCONT,
263 if (ret && errno == ESRCH)
264 child_ret = PIDFD_XFAIL;
266 child_ret = PIDFD_FAIL;
269 /* let the process move on */
270 ret = kill(recycled_pid, SIGCONT);
272 (void)kill(recycled_pid, SIGKILL);
274 if (wait_for_pid(recycled_pid))
290 * If the user set a custom pid_max limit we could be
292 * Skip the test in this case.
294 if (recycled_pid > PIDFD_MAX_DEFAULT)
298 /* failed to recycle pid */
302 ret = wait_for_pid(pid1);
306 "%s test: Managed to signal recycled pid %d\n",
307 test_name, PID_RECYCLE);
309 ksft_exit_fail_msg("%s test: Failed to recycle pid %d\n",
310 test_name, PID_RECYCLE);
312 ksft_print_msg("%s test: Skipping test\n", test_name);
316 ksft_test_result_pass(
317 "%s test: Failed to signal recycled pid as expected\n",
321 default /* PIDFD_ERROR */:
322 ksft_exit_fail_msg("%s test: Error while running tests\n",
329 static int test_pidfd_send_signal_syscall_support(void)
332 const char *test_name = "pidfd_send_signal check for support";
334 pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
337 "%s test: Failed to open process file descriptor\n",
340 ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
344 "%s test: pidfd_send_signal() syscall not supported\n",
347 ksft_exit_fail_msg("%s test: Failed to send signal\n",
352 ksft_test_result_pass(
353 "%s test: pidfd_send_signal() syscall is supported. Tests can be executed\n",
358 static void *test_pidfd_poll_exec_thread(void *priv)
360 ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
361 getpid(), syscall(SYS_gettid));
362 ksft_print_msg("Child Thread: doing exec of sleep\n");
364 execl("/bin/sleep", "sleep", str(CHILD_THREAD_MIN_WAIT), (char *)NULL);
366 ksft_print_msg("Child Thread: DONE. pid %d tid %d\n",
367 getpid(), syscall(SYS_gettid));
371 static void poll_pidfd(const char *test_name, int pidfd)
374 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
375 struct epoll_event event, events[MAX_EVENTS];
378 ksft_exit_fail_msg("%s test: Failed to create epoll file descriptor "
382 event.events = EPOLLIN;
383 event.data.fd = pidfd;
385 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pidfd, &event)) {
386 ksft_exit_fail_msg("%s test: Failed to add epoll file descriptor "
391 c = epoll_wait(epoll_fd, events, MAX_EVENTS, 5000);
392 if (c != 1 || !(events[0].events & EPOLLIN))
393 ksft_exit_fail_msg("%s test: Unexpected epoll_wait result (c=%d, events=%x) ",
395 test_name, c, events[0].events, errno);
402 static int child_poll_exec_test(void *args)
406 ksft_print_msg("Child (pidfd): starting. pid %d tid %d\n", getpid(),
407 syscall(SYS_gettid));
408 pthread_create(&t1, NULL, test_pidfd_poll_exec_thread, NULL);
410 * Exec in the non-leader thread will destroy the leader immediately.
411 * If the wait in the parent returns too soon, the test fails.
417 static void test_pidfd_poll_exec(int use_waitpid)
422 time_t prog_start = time(NULL);
423 const char *test_name = "pidfd_poll check for premature notification on child thread exec";
425 ksft_print_msg("Parent: pid: %d\n", getpid());
426 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_exec_test);
428 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
429 test_name, pid, errno);
431 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
434 ret = waitpid(pid, &status, 0);
436 ksft_print_msg("Parent: error\n");
439 ksft_print_msg("Parent: Child process waited for.\n");
441 poll_pidfd(test_name, pidfd);
444 time_t prog_time = time(NULL) - prog_start;
446 ksft_print_msg("Time waited for child: %lu\n", prog_time);
450 if (prog_time < CHILD_THREAD_MIN_WAIT || prog_time > CHILD_THREAD_MIN_WAIT + 2)
451 ksft_exit_fail_msg("%s test: Failed\n", test_name);
453 ksft_test_result_pass("%s test: Passed\n", test_name);
456 static void *test_pidfd_poll_leader_exit_thread(void *priv)
458 ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
459 getpid(), syscall(SYS_gettid));
460 sleep(CHILD_THREAD_MIN_WAIT);
461 ksft_print_msg("Child Thread: DONE. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
465 static time_t *child_exit_secs;
466 static int child_poll_leader_exit_test(void *args)
470 ksft_print_msg("Child: starting. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
471 pthread_create(&t1, NULL, test_pidfd_poll_leader_exit_thread, NULL);
472 pthread_create(&t2, NULL, test_pidfd_poll_leader_exit_thread, NULL);
475 * glibc exit calls exit_group syscall, so explicity call exit only
476 * so that only the group leader exits, leaving the threads alone.
478 *child_exit_secs = time(NULL);
479 syscall(SYS_exit, 0);
482 static void test_pidfd_poll_leader_exit(int use_waitpid)
486 time_t prog_start = time(NULL);
487 const char *test_name = "pidfd_poll check for premature notification on non-empty"
490 child_exit_secs = mmap(NULL, sizeof *child_exit_secs, PROT_READ | PROT_WRITE,
491 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
493 if (child_exit_secs == MAP_FAILED)
494 ksft_exit_fail_msg("%s test: mmap failed (errno %d)\n",
497 ksft_print_msg("Parent: pid: %d\n", getpid());
498 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_leader_exit_test);
500 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
501 test_name, pid, errno);
503 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
506 ret = waitpid(pid, &status, 0);
508 ksft_print_msg("Parent: error\n");
511 * This sleep tests for the case where if the child exits, and is in
512 * EXIT_ZOMBIE, but the thread group leader is non-empty, then the poll
513 * doesn't prematurely return even though there are active threads
516 poll_pidfd(test_name, pidfd);
520 ksft_print_msg("Parent: Child process waited for.\n");
522 time_t since_child_exit = time(NULL) - *child_exit_secs;
524 ksft_print_msg("Time since child exit: %lu\n", since_child_exit);
528 if (since_child_exit < CHILD_THREAD_MIN_WAIT ||
529 since_child_exit > CHILD_THREAD_MIN_WAIT + 2)
530 ksft_exit_fail_msg("%s test: Failed\n", test_name);
532 ksft_test_result_pass("%s test: Passed\n", test_name);
535 int main(int argc, char **argv)
540 test_pidfd_poll_exec(0);
541 test_pidfd_poll_exec(1);
542 test_pidfd_poll_leader_exit(0);
543 test_pidfd_poll_leader_exit(1);
544 test_pidfd_send_signal_syscall_support();
545 test_pidfd_send_signal_simple_success();
546 test_pidfd_send_signal_exited_fail();
547 test_pidfd_send_signal_recycled_pid_fail();
549 return ksft_exit_pass();