Skip to content

Commit aed5a8d

Browse files
surenbaghdasaryanChristian Brauner
authored andcommitted
tests: add pidfd poll tests
This adds testing for polling on pidfd of a process being killed. Test runs 10000 iterations by default to stress test pidfd polling functionality. It accepts an optional command-line parameter to override the number or iterations to run. Specifically, it tests for: - pidfd_open on a child process succeeds - pidfd_send_signal on a child process succeeds - polling on pidfd succeeds and returns exactly one event - returned event is POLLIN - event is received within 3 secs of the process being killed 10000 iterations was chosen because of the race condition being tested which is not consistently reproducible but usually is revealed after less than 2000 iterations. Reveals race fixed by commit b191d64 ("pidfd: fix a poll race when setting exit_state") Signed-off-by: Suren Baghdasaryan <surenb@google.com> Link: https://lore.kernel.org/r/20190726162226.252750-2-surenb@google.com Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
1 parent 2ec2f99 commit aed5a8d

3 files changed

Lines changed: 119 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pidfd_open_test
2+
pidfd_poll_test
23
pidfd_test
34
pidfd_wait
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
CFLAGS += -g -I../../../../usr/include/ -lpthread
33

4-
TEST_GEN_PROGS := pidfd_test pidfd_open_test pidfd_wait
4+
TEST_GEN_PROGS := pidfd_test pidfd_open_test pidfd_poll_test pidfd_wait
55

66
include ../lib.mk
77

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#define _GNU_SOURCE
4+
#include <errno.h>
5+
#include <linux/types.h>
6+
#include <linux/wait.h>
7+
#include <poll.h>
8+
#include <signal.h>
9+
#include <stdbool.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include <syscall.h>
14+
#include <sys/wait.h>
15+
#include <unistd.h>
16+
17+
#include "pidfd.h"
18+
#include "../kselftest.h"
19+
20+
static bool timeout;
21+
22+
static void handle_alarm(int sig)
23+
{
24+
timeout = true;
25+
}
26+
27+
int main(int argc, char **argv)
28+
{
29+
struct pollfd fds;
30+
int iter, nevents;
31+
int nr_iterations = 10000;
32+
33+
fds.events = POLLIN;
34+
35+
if (argc > 2)
36+
ksft_exit_fail_msg("Unexpected command line argument\n");
37+
38+
if (argc == 2) {
39+
nr_iterations = atoi(argv[1]);
40+
if (nr_iterations <= 0)
41+
ksft_exit_fail_msg("invalid input parameter %s\n",
42+
argv[1]);
43+
}
44+
45+
ksft_print_msg("running pidfd poll test for %d iterations\n",
46+
nr_iterations);
47+
48+
for (iter = 0; iter < nr_iterations; iter++) {
49+
int pidfd;
50+
int child_pid = fork();
51+
52+
if (child_pid < 0) {
53+
if (errno == EAGAIN) {
54+
iter--;
55+
continue;
56+
}
57+
ksft_exit_fail_msg(
58+
"%s - failed to fork a child process\n",
59+
strerror(errno));
60+
}
61+
62+
if (child_pid == 0) {
63+
/* Child process just sleeps for a min and exits */
64+
sleep(60);
65+
exit(EXIT_SUCCESS);
66+
}
67+
68+
/* Parent kills the child and waits for its death */
69+
pidfd = sys_pidfd_open(child_pid, 0);
70+
if (pidfd < 0)
71+
ksft_exit_fail_msg("%s - pidfd_open failed\n",
72+
strerror(errno));
73+
74+
/* Setup 3 sec alarm - plenty of time */
75+
if (signal(SIGALRM, handle_alarm) == SIG_ERR)
76+
ksft_exit_fail_msg("%s - signal failed\n",
77+
strerror(errno));
78+
alarm(3);
79+
80+
/* Send SIGKILL to the child */
81+
if (sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0))
82+
ksft_exit_fail_msg("%s - pidfd_send_signal failed\n",
83+
strerror(errno));
84+
85+
/* Wait for the death notification */
86+
fds.fd = pidfd;
87+
nevents = poll(&fds, 1, -1);
88+
89+
/* Check for error conditions */
90+
if (nevents < 0)
91+
ksft_exit_fail_msg("%s - poll failed\n",
92+
strerror(errno));
93+
94+
if (nevents != 1)
95+
ksft_exit_fail_msg("unexpected poll result: %d\n",
96+
nevents);
97+
98+
if (!(fds.revents & POLLIN))
99+
ksft_exit_fail_msg(
100+
"unexpected event type received: 0x%x\n",
101+
fds.revents);
102+
103+
if (timeout)
104+
ksft_exit_fail_msg(
105+
"death notification wait timeout\n");
106+
107+
close(pidfd);
108+
/* Wait for child to prevent zombies */
109+
if (waitpid(child_pid, NULL, 0) < 0)
110+
ksft_exit_fail_msg("%s - waitpid failed\n",
111+
strerror(errno));
112+
113+
}
114+
115+
ksft_test_result_pass("pidfd poll test: pass\n");
116+
return ksft_exit_pass();
117+
}

0 commit comments

Comments
 (0)