Skip to content

Commit 740378d

Browse files
joelagnelbrauner
authored andcommitted
pidfd: add polling selftests
Other than verifying pidfd based polling, the tests make sure that wait semantics are preserved with the pidfd poll. Notably the 2 cases: 1. If a thread group leader exits while threads still there, then no pidfd poll notifcation should happen. 2. If a non-thread group leader does an execve, then the thread group leader is signaled to exit and is replaced with the execing thread as the new leader, however the parent is not notified in this case. Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org> Signed-off-by: Christian Brauner <christian@brauner.io>
1 parent b53b0b9 commit 740378d

2 files changed

Lines changed: 211 additions & 1 deletion

File tree

‎tools/testing/selftests/pidfd/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CFLAGS += -g -I../../../../usr/include/
1+
CFLAGS += -g -I../../../../usr/include/ -lpthread
22

33
TEST_GEN_PROGS := pidfd_test
44

‎tools/testing/selftests/pidfd/pidfd_test.c‎

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,47 @@
44
#include <errno.h>
55
#include <fcntl.h>
66
#include <linux/types.h>
7+
#include <pthread.h>
78
#include <sched.h>
89
#include <signal.h>
910
#include <stdio.h>
1011
#include <stdlib.h>
1112
#include <string.h>
1213
#include <syscall.h>
14+
#include <sys/epoll.h>
15+
#include <sys/mman.h>
1316
#include <sys/mount.h>
1417
#include <sys/wait.h>
18+
#include <time.h>
1519
#include <unistd.h>
1620

1721
#include "../kselftest.h"
1822

23+
#define str(s) _str(s)
24+
#define _str(s) #s
25+
#define CHILD_THREAD_MIN_WAIT 3 /* seconds */
26+
27+
#define MAX_EVENTS 5
28+
#ifndef __NR_pidfd_send_signal
29+
#define __NR_pidfd_send_signal 424
30+
#endif
31+
32+
#ifndef CLONE_PIDFD
33+
#define CLONE_PIDFD 0x00001000
34+
#endif
35+
36+
static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *))
37+
{
38+
size_t stack_size = 1024;
39+
char *stack[1024] = { 0 };
40+
41+
#ifdef __ia64__
42+
return __clone2(fn, stack, stack_size, flags | SIGCHLD, NULL, pidfd);
43+
#else
44+
return clone(fn, stack + stack_size, flags | SIGCHLD, NULL, pidfd);
45+
#endif
46+
}
47+
1948
static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
2049
unsigned int flags)
2150
{
@@ -368,11 +397,192 @@ static int test_pidfd_send_signal_syscall_support(void)
368397
return 0;
369398
}
370399

400+
static void *test_pidfd_poll_exec_thread(void *priv)
401+
{
402+
ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
403+
getpid(), syscall(SYS_gettid));
404+
ksft_print_msg("Child Thread: doing exec of sleep\n");
405+
406+
execl("/bin/sleep", "sleep", str(CHILD_THREAD_MIN_WAIT), (char *)NULL);
407+
408+
ksft_print_msg("Child Thread: DONE. pid %d tid %d\n",
409+
getpid(), syscall(SYS_gettid));
410+
return NULL;
411+
}
412+
413+
static void poll_pidfd(const char *test_name, int pidfd)
414+
{
415+
int c;
416+
int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
417+
struct epoll_event event, events[MAX_EVENTS];
418+
419+
if (epoll_fd == -1)
420+
ksft_exit_fail_msg("%s test: Failed to create epoll file descriptor "
421+
"(errno %d)\n",
422+
test_name, errno);
423+
424+
event.events = EPOLLIN;
425+
event.data.fd = pidfd;
426+
427+
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pidfd, &event)) {
428+
ksft_exit_fail_msg("%s test: Failed to add epoll file descriptor "
429+
"(errno %d)\n",
430+
test_name, errno);
431+
}
432+
433+
c = epoll_wait(epoll_fd, events, MAX_EVENTS, 5000);
434+
if (c != 1 || !(events[0].events & EPOLLIN))
435+
ksft_exit_fail_msg("%s test: Unexpected epoll_wait result (c=%d, events=%x) ",
436+
"(errno %d)\n",
437+
test_name, c, events[0].events, errno);
438+
439+
close(epoll_fd);
440+
return;
441+
442+
}
443+
444+
static int child_poll_exec_test(void *args)
445+
{
446+
pthread_t t1;
447+
448+
ksft_print_msg("Child (pidfd): starting. pid %d tid %d\n", getpid(),
449+
syscall(SYS_gettid));
450+
pthread_create(&t1, NULL, test_pidfd_poll_exec_thread, NULL);
451+
/*
452+
* Exec in the non-leader thread will destroy the leader immediately.
453+
* If the wait in the parent returns too soon, the test fails.
454+
*/
455+
while (1)
456+
sleep(1);
457+
}
458+
459+
static void test_pidfd_poll_exec(int use_waitpid)
460+
{
461+
int pid, pidfd = 0;
462+
int status, ret;
463+
pthread_t t1;
464+
time_t prog_start = time(NULL);
465+
const char *test_name = "pidfd_poll check for premature notification on child thread exec";
466+
467+
ksft_print_msg("Parent: pid: %d\n", getpid());
468+
pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_exec_test);
469+
if (pid < 0)
470+
ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
471+
test_name, pid, errno);
472+
473+
ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
474+
475+
if (use_waitpid) {
476+
ret = waitpid(pid, &status, 0);
477+
if (ret == -1)
478+
ksft_print_msg("Parent: error\n");
479+
480+
if (ret == pid)
481+
ksft_print_msg("Parent: Child process waited for.\n");
482+
} else {
483+
poll_pidfd(test_name, pidfd);
484+
}
485+
486+
time_t prog_time = time(NULL) - prog_start;
487+
488+
ksft_print_msg("Time waited for child: %lu\n", prog_time);
489+
490+
close(pidfd);
491+
492+
if (prog_time < CHILD_THREAD_MIN_WAIT || prog_time > CHILD_THREAD_MIN_WAIT + 2)
493+
ksft_exit_fail_msg("%s test: Failed\n", test_name);
494+
else
495+
ksft_test_result_pass("%s test: Passed\n", test_name);
496+
}
497+
498+
static void *test_pidfd_poll_leader_exit_thread(void *priv)
499+
{
500+
ksft_print_msg("Child Thread: starting. pid %d tid %d ; and sleeping\n",
501+
getpid(), syscall(SYS_gettid));
502+
sleep(CHILD_THREAD_MIN_WAIT);
503+
ksft_print_msg("Child Thread: DONE. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
504+
return NULL;
505+
}
506+
507+
static time_t *child_exit_secs;
508+
static int child_poll_leader_exit_test(void *args)
509+
{
510+
pthread_t t1, t2;
511+
512+
ksft_print_msg("Child: starting. pid %d tid %d\n", getpid(), syscall(SYS_gettid));
513+
pthread_create(&t1, NULL, test_pidfd_poll_leader_exit_thread, NULL);
514+
pthread_create(&t2, NULL, test_pidfd_poll_leader_exit_thread, NULL);
515+
516+
/*
517+
* glibc exit calls exit_group syscall, so explicity call exit only
518+
* so that only the group leader exits, leaving the threads alone.
519+
*/
520+
*child_exit_secs = time(NULL);
521+
syscall(SYS_exit, 0);
522+
}
523+
524+
static void test_pidfd_poll_leader_exit(int use_waitpid)
525+
{
526+
int pid, pidfd = 0;
527+
int status, ret;
528+
time_t prog_start = time(NULL);
529+
const char *test_name = "pidfd_poll check for premature notification on non-empty"
530+
"group leader exit";
531+
532+
child_exit_secs = mmap(NULL, sizeof *child_exit_secs, PROT_READ | PROT_WRITE,
533+
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
534+
535+
if (child_exit_secs == MAP_FAILED)
536+
ksft_exit_fail_msg("%s test: mmap failed (errno %d)\n",
537+
test_name, errno);
538+
539+
ksft_print_msg("Parent: pid: %d\n", getpid());
540+
pid = pidfd_clone(CLONE_PIDFD, &pidfd, child_poll_leader_exit_test);
541+
if (pid < 0)
542+
ksft_exit_fail_msg("%s test: pidfd_clone failed (ret %d, errno %d)\n",
543+
test_name, pid, errno);
544+
545+
ksft_print_msg("Parent: Waiting for Child (%d) to complete.\n", pid);
546+
547+
if (use_waitpid) {
548+
ret = waitpid(pid, &status, 0);
549+
if (ret == -1)
550+
ksft_print_msg("Parent: error\n");
551+
} else {
552+
/*
553+
* This sleep tests for the case where if the child exits, and is in
554+
* EXIT_ZOMBIE, but the thread group leader is non-empty, then the poll
555+
* doesn't prematurely return even though there are active threads
556+
*/
557+
sleep(1);
558+
poll_pidfd(test_name, pidfd);
559+
}
560+
561+
if (ret == pid)
562+
ksft_print_msg("Parent: Child process waited for.\n");
563+
564+
time_t since_child_exit = time(NULL) - *child_exit_secs;
565+
566+
ksft_print_msg("Time since child exit: %lu\n", since_child_exit);
567+
568+
close(pidfd);
569+
570+
if (since_child_exit < CHILD_THREAD_MIN_WAIT ||
571+
since_child_exit > CHILD_THREAD_MIN_WAIT + 2)
572+
ksft_exit_fail_msg("%s test: Failed\n", test_name);
573+
else
574+
ksft_test_result_pass("%s test: Passed\n", test_name);
575+
}
576+
371577
int main(int argc, char **argv)
372578
{
373579
ksft_print_header();
374580
ksft_set_plan(4);
375581

582+
test_pidfd_poll_exec(0);
583+
test_pidfd_poll_exec(1);
584+
test_pidfd_poll_leader_exit(0);
585+
test_pidfd_poll_leader_exit(1);
376586
test_pidfd_send_signal_syscall_support();
377587
test_pidfd_send_signal_simple_success();
378588
test_pidfd_send_signal_exited_fail();

0 commit comments

Comments
 (0)