1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Xie Ziyao <xieziyao@huawei.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Check that a timeout equal to zero causes epoll_wait() to return immediately.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#include <sys/epoll.h>
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci#include "tst_timer_test.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#define USEC_PRECISION 1000	/* Error margin allowed in usec */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic int epfd, fds[2];
21f08c3bdfSopenharmony_cistatic struct epoll_event epevs[1] = {
22f08c3bdfSopenharmony_ci	{.events = EPOLLIN}
23f08c3bdfSopenharmony_ci};
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic void run(void)
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci	tst_timer_start(CLOCK_MONOTONIC);
28f08c3bdfSopenharmony_ci	TEST(epoll_wait(epfd, epevs, 1, 0));
29f08c3bdfSopenharmony_ci	tst_timer_stop();
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	if (TST_RET != 0)
32f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "epoll_wait() returned %li", TST_RET);
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	if (tst_timer_elapsed_us() <= USEC_PRECISION)
35f08c3bdfSopenharmony_ci		tst_res(TPASS, "epoll_wait() returns immediately with a timeout equal to zero");
36f08c3bdfSopenharmony_ci	else
37f08c3bdfSopenharmony_ci		tst_res(TFAIL, "epoll_wait() waited for %llius with a timeout equal to zero",
38f08c3bdfSopenharmony_ci			tst_timer_elapsed_us());
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void setup(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	SAFE_PIPE(fds);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	epfd = epoll_create(1);
46f08c3bdfSopenharmony_ci	if (epfd == -1)
47f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "epoll_create()");
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	epevs[0].data.fd = fds[0];
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &epevs[0]))
52f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
53f08c3bdfSopenharmony_ci}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic void cleanup(void)
56f08c3bdfSopenharmony_ci{
57f08c3bdfSopenharmony_ci	if (epfd > 0)
58f08c3bdfSopenharmony_ci		SAFE_CLOSE(epfd);
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	if (fds[0] > 0)
61f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[0]);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	if (fds[1] > 0)
64f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[1]);
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic struct tst_test test = {
68f08c3bdfSopenharmony_ci	.test_all = run,
69f08c3bdfSopenharmony_ci	.setup = setup,
70f08c3bdfSopenharmony_ci	.cleanup = cleanup,
71f08c3bdfSopenharmony_ci};
72