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 * [Description]
10 *
11 * Check that epoll_wait(2) timeouts correctly.
12 */
13
14#include <sys/epoll.h>
15#include <unistd.h>
16#include <errno.h>
17
18#include "tst_timer_test.h"
19
20static int epfd, fds[2];
21static struct epoll_event epevs[1] = {
22	{.events = EPOLLIN}
23};
24
25int sample_fn(int clk_id, long long usec)
26{
27	unsigned int sleep_ms = usec / 1000;
28
29	tst_timer_start(clk_id);
30	TEST(epoll_wait(epfd, epevs, 1, sleep_ms));
31	tst_timer_stop();
32	tst_timer_sample();
33
34	if (TST_RET != 0) {
35		tst_res(TFAIL | TTERRNO, "epoll_wait() returned %li", TST_RET);
36		return 1;
37	}
38
39	return 0;
40}
41
42static void setup(void)
43{
44	SAFE_PIPE(fds);
45
46	epfd = epoll_create(1);
47	if (epfd == -1)
48		tst_brk(TBROK | TERRNO, "epoll_create()");
49
50	epevs[0].data.fd = fds[0];
51
52	if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &epevs[0]))
53		tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
54}
55
56static void cleanup(void)
57{
58	if (epfd > 0)
59		SAFE_CLOSE(epfd);
60
61	if (fds[0] > 0)
62		SAFE_CLOSE(fds[0]);
63
64	if (fds[1] > 0)
65		SAFE_CLOSE(fds[1]);
66}
67
68static struct tst_test test = {
69	.scall = "epoll_wait()",
70	.sample = sample_fn,
71	.setup = setup,
72	.cleanup = cleanup,
73};
74