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