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