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 (void)wait_for_pid(pid);
122 "%s test: Failed to open process file descriptor\n",
125 ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
130 "%s test: Managed to send signal to process even though it should have failed\n",
133 if (saved_errno != ESRCH)
135 "%s test: Expected to receive ESRCH as errno value but received %d instead\n",
136 test_name, saved_errno);
138 ksft_test_result_pass("%s test: Failed to send signal as expected\n",
144 * Maximum number of cycles we allow. This is equivalent to PID_MAX_DEFAULT.
145 * If users set a higher limit or we have cycled PIDFD_MAX_DEFAULT number of
146 * times then we skip the test to not go into an infinite loop or block for a
149 #define PIDFD_MAX_DEFAULT 0x8000
151 static int test_pidfd_send_signal_recycled_pid_fail(void)
155 const char *test_name = "pidfd_send_signal signal recycled pid";
157 if (!have_pidfd_send_signal) {
158 ksft_test_result_skip(
159 "%s test: pidfd_send_signal() syscall not supported\n",
164 ret = unshare(CLONE_NEWPID);
166 if (errno == EPERM) {
167 ksft_test_result_skip("%s test: Unsharing pid namespace not permitted\n",
171 ksft_exit_fail_msg("%s test: Failed to unshare pid namespace\n",
175 ret = unshare(CLONE_NEWNS);
177 if (errno == EPERM) {
178 ksft_test_result_skip("%s test: Unsharing mount namespace not permitted\n",
182 ksft_exit_fail_msg("%s test: Failed to unshare mount namespace\n",
186 ret = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
188 ksft_exit_fail_msg("%s test: Failed to remount / private\n",
191 /* pid 1 in new pid namespace */
194 ksft_exit_fail_msg("%s test: Failed to create new process\n",
202 (void)umount2("/proc", MNT_DETACH);
203 ret = mount("proc", "/proc", "proc", 0, NULL);
207 /* grab pid PID_RECYCLE */
208 for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
216 if (pid2 == PID_RECYCLE) {
217 snprintf(buf, sizeof(buf), "/proc/%d", pid2);
218 ksft_print_msg("pid to recycle is %d\n", pid2);
219 pidfd = open(buf, O_DIRECTORY | O_CLOEXEC);
222 if (wait_for_pid(pid2))
225 if (pid2 >= PID_RECYCLE)
230 * We want to be as predictable as we can so if we haven't been
231 * able to grab pid PID_RECYCLE skip the test.
233 if (pid2 != PID_RECYCLE) {
242 for (i = 0; i <= PIDFD_MAX_DEFAULT; i++) {
246 int child_ret = PIDFD_PASS;
248 ret = pipe2(pipe_fds, O_CLOEXEC);
252 recycled_pid = fork();
253 if (recycled_pid < 0)
256 if (recycled_pid == 0) {
258 (void)read(pipe_fds[0], &c, 1);
265 * Stop the child so we can inspect whether we have
266 * recycled pid PID_RECYCLE.
269 ret = kill(recycled_pid, SIGSTOP);
272 (void)wait_for_pid(recycled_pid);
277 * We have recycled the pid. Try to signal it. This
278 * needs to fail since this is a different process than
279 * the one the pidfd refers to.
281 if (recycled_pid == PID_RECYCLE) {
282 ret = sys_pidfd_send_signal(pidfd, SIGCONT,
284 if (ret && errno == ESRCH)
285 child_ret = PIDFD_XFAIL;
287 child_ret = PIDFD_FAIL;
290 /* let the process move on */
291 ret = kill(recycled_pid, SIGCONT);
293 (void)kill(recycled_pid, SIGKILL);
295 if (wait_for_pid(recycled_pid))
311 * If the user set a custom pid_max limit we could be
313 * Skip the test in this case.
315 if (recycled_pid > PIDFD_MAX_DEFAULT)
319 /* failed to recycle pid */
323 ret = wait_for_pid(pid1);
327 "%s test: Managed to signal recycled pid %d\n",
328 test_name, PID_RECYCLE);
330 ksft_exit_fail_msg("%s test: Failed to recycle pid %d\n",
331 test_name, PID_RECYCLE);
333 ksft_test_result_skip("%s test: Skipping test\n", test_name);
337 ksft_test_result_pass(
338 "%s test: Failed to signal recycled pid as expected\n",
342 default /* PIDFD_ERROR */:
343 ksft_exit_fail_msg("%s test: Error while running tests\n",
350 static int test_pidfd_send_signal_syscall_support(void)
353 const char *test_name = "pidfd_send_signal check for support";
355 pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC);
358 "%s test: Failed to open process file descriptor\n",
361 ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
363 if (errno == ENOSYS) {
364 ksft_test_result_skip(
365 "%s test: pidfd_send_signal() syscall not supported\n",
369 ksft_exit_fail_msg("%s test: Failed to send signal\n",
373 have_pidfd_send_signal = true;
375 ksft_test_result_pass(
376 "%s test: pidfd_send_signal() syscall is supported. Tests can be executed\n",
381 static void *test_pidfd_poll_exec_thread(void *priv)
383 ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
384 getpid(), syscall(SYS_gettid));
385 ksft_print_msg("Child Thread: doing exec of sleep\n");
387 execl("/bin/sleep", "sleep", str(CHILD_THREAD_MIN_WAIT), (char *)NULL);
389 ksft_print_msg("Child Thread: DONE. pid %d tid %d\n",
390 getpid(), syscall(SYS_gettid));
394 static void poll_pidfd(const char *test_name, int pidfd)
397 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
398 struct epoll_event event, events[MAX_EVENTS];
401 ksft_exit_fail_msg("%s test: Failed to create epoll file descriptor "
405 event.events = EPOLLIN;
406 event.data.fd = pidfd;
408 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pidfd, &event)) {
409 ksft_exit_fail_msg("%s test: Failed to add epoll file descriptor "
414 c = epoll_wait(epoll_fd, events, MAX_EVENTS, 5000);
415 if (c != 1 || !(events[0].events & EPOLLIN))
416 ksft_exit_fail_msg("%s test: Unexpected epoll_wait result (c=%d, events=%x) "
418 test_name, c, events[0].events, errno);
425 static int child_poll_exec_test(void *args)
429 ksft_print_msg("Child (pidfd): starting. pid %d tid %d\n", getpid(),
430 syscall(SYS_gettid));
431 pthread_create(&t1, NULL, test_pidfd_poll_exec_thread, NULL);
433 * Exec in the non-leader thread will destroy the leader immediately.
434 * If the wait in the parent returns too soon, the test fails.
442 static void test_pidfd_poll_exec(int use_waitpid)
446 time_t prog_start = time(NULL);
447 const char *test_name = "pidfd_poll check for premature notification on child thread exec";
449 ksft_print_msg("Parent: pid: %d\n", getpid());
450 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_exec_test);
452 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
453 test_name, pid, errno);
455 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
458 ret = waitpid(pid, &status, 0);
460 ksft_print_msg("Parent: error\n");
463 ksft_print_msg("Parent: Child process waited for.\n");
465 poll_pidfd(test_name, pidfd);
468 time_t prog_time = time(NULL) - prog_start;
470 ksft_print_msg("Time waited for child: %lu\n", prog_time);
474 if (prog_time < CHILD_THREAD_MIN_WAIT || prog_time > CHILD_THREAD_MIN_WAIT + 2)
475 ksft_exit_fail_msg("%s test: Failed\n", test_name);
477 ksft_test_result_pass("%s test: Passed\n", test_name);
480 static void *test_pidfd_poll_leader_exit_thread(void *priv)
482 ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
483 getpid(), syscall(SYS_gettid));
484 sleep(CHILD_THREAD_MIN_WAIT);
485 ksft_print_msg("Child Thread: DONE. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
489 static time_t *child_exit_secs;
490 static int child_poll_leader_exit_test(void *args)
494 ksft_print_msg("Child: starting. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
495 pthread_create(&t1, NULL, test_pidfd_poll_leader_exit_thread, NULL);
496 pthread_create(&t2, NULL, test_pidfd_poll_leader_exit_thread, NULL);
499 * glibc exit calls exit_group syscall, so explicity call exit only
500 * so that only the group leader exits, leaving the threads alone.
502 *child_exit_secs = time(NULL);
503 syscall(SYS_exit, 0);
504 /* Never reached, but appeases compiler thinking we should return. */
508 static void test_pidfd_poll_leader_exit(int use_waitpid)
512 const char *test_name = "pidfd_poll check for premature notification on non-empty"
515 child_exit_secs = mmap(NULL, sizeof *child_exit_secs, PROT_READ | PROT_WRITE,
516 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
518 if (child_exit_secs == MAP_FAILED)
519 ksft_exit_fail_msg("%s test: mmap failed (errno %d)\n",
522 ksft_print_msg("Parent: pid: %d\n", getpid());
523 pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_leader_exit_test);
525 ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
526 test_name, pid, errno);
528 ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
531 ret = waitpid(pid, &status, 0);
533 ksft_print_msg("Parent: error\n");
536 * This sleep tests for the case where if the child exits, and is in
537 * EXIT_ZOMBIE, but the thread group leader is non-empty, then the poll
538 * doesn't prematurely return even though there are active threads
541 poll_pidfd(test_name, pidfd);
545 ksft_print_msg("Parent: Child process waited for.\n");
547 time_t since_child_exit = time(NULL) - *child_exit_secs;
549 ksft_print_msg("Time since child exit: %lu\n", since_child_exit);
553 if (since_child_exit < CHILD_THREAD_MIN_WAIT ||
554 since_child_exit > CHILD_THREAD_MIN_WAIT + 2)
555 ksft_exit_fail_msg("%s test: Failed\n", test_name);
557 ksft_test_result_pass("%s test: Passed\n", test_name);
560 int main(int argc, char **argv)
565 test_pidfd_poll_exec(0);
566 test_pidfd_poll_exec(1);
567 test_pidfd_poll_leader_exit(0);
568 test_pidfd_poll_leader_exit(1);
569 test_pidfd_send_signal_syscall_support();
570 test_pidfd_send_signal_simple_success();
571 test_pidfd_send_signal_exited_fail();
572 test_pidfd_send_signal_recycled_pid_fail();
574 return ksft_exit_pass();