1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/types.h>
15 #include <sys/epoll.h>
17 #include <sys/mount.h>
23 #include "../kselftest.h"
25 #define str(s) _str(s)
27 #define CHILD_THREAD_MIN_WAIT 3 /* seconds */
31 static bool have_pidfd_send_signal;
33 static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *))
35 size_t stack_size = 1024;
36 char *stack[1024] = { 0 };
39 return __clone2(fn, stack, stack_size, flags | SIGCHLD, NULL, pidfd);
41 return clone(fn, stack + stack_size, flags | SIGCHLD, NULL, pidfd);
45 static int signal_received;
47 static void set_signal_received_on_sigusr1(int sig)
54 * Straightforward test to see whether pidfd_send_signal() works is to send
55 * a signal to ourself.
57 static int test_pidfd_send_signal_simple_success(void)
60 const char *test_name = "pidfd_send_signal send SIGUSR1";
62 if (!have_pidfd_send_signal) {
63 ksft_test_result_skip(
64 "%s test: pidfd_send_signal() syscall not supported\n",
69 pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
72 "%s test: Failed to open process file descriptor\n",
75 signal(SIGUSR1, set_signal_received_on_sigusr1);
77 ret = sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0);
80 ksft_exit_fail_msg("%s test: Failed to send signal\n",
83 if (signal_received != 1)
84 ksft_exit_fail_msg("%s test: Failed to receive signal\n",
88 ksft_test_result_pass("%s test: Sent signal\n", test_name);
92 static int test_pidfd_send_signal_exited_fail(void)
94 int pidfd, ret, saved_errno;
97 const char *test_name = "pidfd_send_signal signal exited process";
99 if (!have_pidfd_send_signal) {
100 ksft_test_result_skip(
101 "%s test: pidfd_send_signal() syscall not supported\n",
108 ksft_exit_fail_msg("%s test: Failed to create new process\n",
114 snprintf(buf, sizeof(buf), "/proc/%d", pid);
116 pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
118 ret = wait_for_pid(pid);
119 ksft_print_msg("waitpid WEXITSTATUS=%d\n", ret);
123 "%s test: Failed to open process file descriptor\n",
126 ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
131 "%s test: Managed to send signal to process even though it should have failed\n",
134 if (saved_errno != ESRCH)
136 "%s test: Expected to receive ESRCH as errno value but received %d instead\n",
137 test_name, saved_errno);
139 ksft_test_result_pass("%s test: Failed to send signal as expected\n",
145 * Maximum number of cycles we allow. This is equivalent to PID_MAX_DEFAULT.
146 * If users set a higher limit or we have cycled PIDFD_MAX_DEFAULT number of
147 * times then we skip the test to not go into an infinite loop or block for a
150 #define PIDFD_MAX_DEFAULT 0x8000
152 static int test_pidfd_send_signal_recycled_pid_fail(void)
156 const char *test_name = "pidfd_send_signal signal recycled pid";
158 if (!have_pidfd_send_signal) {
159 ksft_test_result_skip(
160 "%s test: pidfd_send_signal() syscall not supported\n",
165 ret = unshare(CLONE_NEWPID);
167 if (errno == EPERM) {
168 ksft_test_result_skip("%s test: Unsharing pid namespace not permitted\n",
172 ksft_exit_fail_msg("%s test: Failed to unshare pid namespace\n",
176 ret = unshare(CLONE_NEWNS);
178 if (errno == EPERM) {
179 ksft_test_result_skip("%s test: Unsharing mount namespace not permitted\n",
183 ksft_exit_fail_msg("%s test: Failed to unshare mount namespace\n",
187 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
189 ksft_exit_fail_msg("%s test: Failed to remount / private\n",
192 /* pid 1 in new pid namespace */
195 ksft_exit_fail_msg("%s test: Failed to create new process\n",
203 (void)umount2("/proc", MNT_DETACH);
204 ret = mount("proc", "/proc", "proc", 0, NULL);
208 /* grab pid PID_RECYCLE */
209 for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
217 if (pid2 == PID_RECYCLE) {
218 snprintf(buf, sizeof(buf), "/proc/%d", pid2);
219 ksft_print_msg("pid to recycle is %d\n", pid2);
220 pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
223 if (wait_for_pid(pid2))
226 if (pid2 >= PID_RECYCLE)
231 * We want to be as predictable as we can so if we haven't been
232 * able to grab pid PID_RECYCLE skip the test.
234 if (pid2 != PID_RECYCLE) {
243 for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
247 int child_ret = PIDFD_PASS;
249 ret = pipe2(pipe_fds, O_CLOEXEC);
253 recycled_pid = fork();
254 if (recycled_pid < 0)
257 if (recycled_pid == 0) {
259 (void)read(pipe_fds[0], &c, 1);
266 * Stop the child so we can inspect whether we have
267 * recycled pid PID_RECYCLE.
270 ret = kill(recycled_pid, SIGSTOP);
273 (void)wait_for_pid(recycled_pid);
278 * We have recycled the pid. Try to signal it. This
279 * needs to fail since this is a different process than
280 * the one the pidfd refers to.
282 if (recycled_pid == PID_RECYCLE) {
283 ret = sys_pidfd_send_signal(pidfd, SIGCONT,
285 if (ret && errno == ESRCH)
286 child_ret = PIDFD_XFAIL;
288 child_ret = PIDFD_FAIL;
291 /* let the process move on */
292 ret = kill(recycled_pid, SIGCONT);
294 (void)kill(recycled_pid, SIGKILL);
296 if (wait_for_pid(recycled_pid))
312 * If the user set a custom pid_max limit we could be
314 * Skip the test in this case.
316 if (recycled_pid > PIDFD_MAX_DEFAULT)
320 /* failed to recycle pid */
324 ret = wait_for_pid(pid1);
328 "%s test: Managed to signal recycled pid %d\n",
329 test_name, PID_RECYCLE);
331 ksft_exit_fail_msg("%s test: Failed to recycle pid %d\n",
332 test_name, PID_RECYCLE);
334 ksft_test_result_skip("%s test: Skipping test\n", test_name);
338 ksft_test_result_pass(
339 "%s test: Failed to signal recycled pid as expected\n",
343 default /* PIDFD_ERROR */:
344 ksft_exit_fail_msg("%s test: Error while running tests\n",
351 static int test_pidfd_send_signal_syscall_support(void)
354 const char *test_name = "pidfd_send_signal check for support";
356 pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
359 "%s test: Failed to open process file descriptor\n",
362 ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
364 if (errno == ENOSYS) {
365 ksft_test_result_skip(
366 "%s test: pidfd_send_signal() syscall not supported\n",
370 ksft_exit_fail_msg("%s test: Failed to send signal\n",
374 have_pidfd_send_signal = true;
376 ksft_test_result_pass(
377 "%s test: pidfd_send_signal() syscall is supported. Tests can be executed\n",
382 static void *test_pidfd_poll_exec_thread(void *priv)
384 ksft_print_msg("Child Thread: starting. pid %d tid %ld ; and sleeping\n",
385 getpid(), syscall(SYS_gettid));
386 ksft_print_msg("Child Thread: doing exec of sleep\n");
388 execl("/bin/sleep", "sleep", str(CHILD_THREAD_MIN_WAIT), (char *)NULL);
390 ksft_print_msg("Child Thread: DONE. pid %d tid %ld\n",
391 getpid(), syscall(SYS_gettid));
395 static void poll_pidfd(const char *test_name, int pidfd)
398 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
399 struct epoll_event event, events[MAX_EVENTS];
402 ksft_exit_fail_msg("%s test: Failed to create epoll file descriptor "
406 event.events = EPOLLIN;
407 event.data.fd = pidfd;
409 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pidfd, &event)) {
410 ksft_exit_fail_msg("%s test: Failed to add epoll file descriptor "
415 c = epoll_wait(epoll_fd, events, MAX_EVENTS, 5000);
416 if (c != 1 || !(events[0].events & EPOLLIN))
417 ksft_exit_fail_msg("%s test: Unexpected epoll_wait result (c=%d, events=%x) "
419 test_name, c, events[0].events, errno);
426 static int child_poll_exec_test(void *args)
430 ksft_print_msg("Child (pidfd): starting. pid %d tid %ld\n", getpid(),
431 syscall(SYS_gettid));
432 pthread_create(&t1, NULL, test_pidfd_poll_exec_thread, NULL);
434 * Exec in the non-leader thread will destroy the leader immediately.
435 * If the wait in the parent returns too soon, the test fails.
443 static void test_pidfd_poll_exec(int use_waitpid)
447 time_t prog_start = time(NULL);
448 const char *test_name = "pidfd_poll check for premature notification on child thread exec";
450 ksft_print_msg("Parent: pid: %d\n", getpid());
451 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_exec_test);
453 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
454 test_name, pid, errno);
456 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
459 ret = waitpid(pid, &status, 0);
461 ksft_print_msg("Parent: error\n");
464 ksft_print_msg("Parent: Child process waited for.\n");
466 poll_pidfd(test_name, pidfd);
469 time_t prog_time = time(NULL) - prog_start;
471 ksft_print_msg("Time waited for child: %lu\n", prog_time);
475 if (prog_time < CHILD_THREAD_MIN_WAIT || prog_time > CHILD_THREAD_MIN_WAIT + 2)
476 ksft_exit_fail_msg("%s test: Failed\n", test_name);
478 ksft_test_result_pass("%s test: Passed\n", test_name);
481 static void *test_pidfd_poll_leader_exit_thread(void *priv)
483 ksft_print_msg("Child Thread: starting. pid %d tid %ld ; and sleeping\n",
484 getpid(), syscall(SYS_gettid));
485 sleep(CHILD_THREAD_MIN_WAIT);
486 ksft_print_msg("Child Thread: DONE. pid %d tid %ld\n", getpid(), syscall(SYS_gettid));
490 static time_t *child_exit_secs;
491 static int child_poll_leader_exit_test(void *args)
495 ksft_print_msg("Child: starting. pid %d tid %ld\n", getpid(), syscall(SYS_gettid));
496 pthread_create(&t1, NULL, test_pidfd_poll_leader_exit_thread, NULL);
497 pthread_create(&t2, NULL, test_pidfd_poll_leader_exit_thread, NULL);
500 * glibc exit calls exit_group syscall, so explicity call exit only
501 * so that only the group leader exits, leaving the threads alone.
503 *child_exit_secs = time(NULL);
504 syscall(SYS_exit, 0);
505 /* Never reached, but appeases compiler thinking we should return. */
509 static void test_pidfd_poll_leader_exit(int use_waitpid)
513 const char *test_name = "pidfd_poll check for premature notification on non-empty"
516 child_exit_secs = mmap(NULL, sizeof *child_exit_secs, PROT_READ | PROT_WRITE,
517 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
519 if (child_exit_secs == MAP_FAILED)
520 ksft_exit_fail_msg("%s test: mmap failed (errno %d)\n",
523 ksft_print_msg("Parent: pid: %d\n", getpid());
524 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_leader_exit_test);
526 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
527 test_name, pid, errno);
529 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
532 ret = waitpid(pid, &status, 0);
534 ksft_print_msg("Parent: error\n");
537 * This sleep tests for the case where if the child exits, and is in
538 * EXIT_ZOMBIE, but the thread group leader is non-empty, then the poll
539 * doesn't prematurely return even though there are active threads
542 poll_pidfd(test_name, pidfd);
546 ksft_print_msg("Parent: Child process waited for.\n");
548 time_t since_child_exit = time(NULL) - *child_exit_secs;
550 ksft_print_msg("Time since child exit: %lu\n", since_child_exit);
554 if (since_child_exit < CHILD_THREAD_MIN_WAIT ||
555 since_child_exit > CHILD_THREAD_MIN_WAIT + 2)
556 ksft_exit_fail_msg("%s test: Failed\n", test_name);
558 ksft_test_result_pass("%s test: Passed\n", test_name);
561 int main(int argc, char **argv)
566 test_pidfd_poll_exec(0);
567 test_pidfd_poll_exec(1);
568 test_pidfd_poll_leader_exit(0);
569 test_pidfd_poll_leader_exit(1);
570 test_pidfd_send_signal_syscall_support();
571 test_pidfd_send_signal_simple_success();
572 test_pidfd_send_signal_exited_fail();
573 test_pidfd_send_signal_recycled_pid_fail();