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