1 /*
2 * Copyright (c) 2016 Fujitsu Ltd.
3 * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
4 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.
18 */
19
20 /*
21 * Check that epoll_wait(2) timeouts correctly.
22 */
23
24 #include <sys/epoll.h>
25 #include <unistd.h>
26 #include <errno.h>
27
28 #include "tst_timer_test.h"
29
30 static int epfd, fds[2];
31 static struct epoll_event epevs[1] = {
32 {.events = EPOLLIN}
33 };
34
sample_fn(int clk_id,long long usec)35 int sample_fn(int clk_id, long long usec)
36 {
37 unsigned int sleep_ms = usec / 1000;
38
39 tst_timer_start(clk_id);
40 TEST(epoll_wait(epfd, epevs, 1, sleep_ms));
41 tst_timer_stop();
42 tst_timer_sample();
43
44 if (TST_RET != 0) {
45 tst_res(TFAIL | TTERRNO,
46 "epoll_wait() returned %li", TST_RET);
47 return 1;
48 }
49
50 return 0;
51 }
52
setup(void)53 static void setup(void)
54 {
55 SAFE_PIPE(fds);
56
57 epfd = epoll_create(1);
58 if (epfd == -1)
59 tst_brk(TBROK | TERRNO, "epoll_create()");
60
61 epevs[0].data.fd = fds[0];
62
63 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &epevs[0]))
64 tst_brk(TBROK | TERRNO, "epoll_clt(..., EPOLL_CTL_ADD, ...)");
65 }
66
cleanup(void)67 static void cleanup(void)
68 {
69 if (epfd > 0)
70 SAFE_CLOSE(epfd);
71
72 if (fds[0] > 0)
73 SAFE_CLOSE(fds[0]);
74
75 if (fds[1] > 0)
76 SAFE_CLOSE(fds[1]);
77 }
78
79 static struct tst_test test = {
80 .scall = "epoll_wait()",
81 .sample = sample_fn,
82 .setup = setup,
83 .cleanup = cleanup,
84 };
85