1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Fujitsu Ltd.
4 * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
5 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
6 */
7
8 /*
9 * Check that epoll_wait(2) timeouts correctly.
10 */
11
12 #include <sys/epoll.h>
13 #include <unistd.h>
14 #include <errno.h>
15
16 #include "tst_timer_test.h"
17
18 static int epfd, fds[2];
19 static struct epoll_event epevs[1] = {
20 {.events = EPOLLIN}
21 };
22
sample_fn(int clk_id,long long usec)23 int sample_fn(int clk_id, long long usec)
24 {
25 unsigned int sleep_ms = usec / 1000;
26
27 tst_timer_start(clk_id);
28 TEST(epoll_wait(epfd, epevs, 1, sleep_ms));
29 tst_timer_stop();
30 tst_timer_sample();
31
32 if (TST_RET != 0) {
33 tst_res(TFAIL | TTERRNO,
34 "epoll_wait() returned %li", TST_RET);
35 return 1;
36 }
37
38 return 0;
39 }
40
setup(void)41 static void setup(void)
42 {
43 SAFE_PIPE(fds);
44
45 epfd = epoll_create(1);
46 if (epfd == -1)
47 tst_brk(TBROK | TERRNO, "epoll_create()");
48
49 epevs[0].data.fd = fds[0];
50
51 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &epevs[0]))
52 tst_brk(TBROK | TERRNO, "epoll_clt(..., EPOLL_CTL_ADD, ...)");
53 }
54
cleanup(void)55 static void cleanup(void)
56 {
57 if (epfd > 0)
58 SAFE_CLOSE(epfd);
59
60 if (fds[0] > 0)
61 SAFE_CLOSE(fds[0]);
62
63 if (fds[1] > 0)
64 SAFE_CLOSE(fds[1]);
65 }
66
67 static struct tst_test test = {
68 .scall = "epoll_wait()",
69 .sample = sample_fn,
70 .setup = setup,
71 .cleanup = cleanup,
72 };
73