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 epoll_pwait and epoll_pwait2 timeouts correctly.
11 */
12
13 #include <sys/epoll.h>
14
15 #include "tst_timer_test.h"
16 #include "epoll_pwait_var.h"
17
18 #define USEC_PER_MSEC (1000L)
19
20 static int efd, sfd[2];
21 static struct epoll_event e;
22
sample_fn(int clk_id,long long usec)23 int sample_fn(int clk_id, long long usec)
24 {
25 unsigned int ms = usec / USEC_PER_MSEC;
26
27 tst_timer_start(clk_id);
28 TEST(do_epoll_pwait(efd, &e, 1, ms, NULL));
29 tst_timer_stop();
30 tst_timer_sample();
31
32 if (TST_RET != 0) {
33 tst_res(TFAIL | TTERRNO,
34 "do_epoll_pwait() returned %li, expected 0", TST_RET);
35 return 1;
36 }
37
38 return 0;
39 }
40
setup(void)41 static void setup(void)
42 {
43 epoll_pwait_info();
44
45 SAFE_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, sfd);
46
47 efd = epoll_create(1);
48 if (efd == -1)
49 tst_brk(TBROK | TERRNO, "epoll_create()");
50
51 e.events = EPOLLIN;
52 if (epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e))
53 tst_brk(TBROK | TERRNO, "epoll_clt(..., EPOLL_CTL_ADD, ...)");
54 }
55
cleanup(void)56 static void cleanup(void)
57 {
58 if (efd > 0)
59 SAFE_CLOSE(efd);
60
61 if (sfd[0] > 0)
62 SAFE_CLOSE(sfd[0]);
63
64 if (sfd[1] > 0)
65 SAFE_CLOSE(sfd[1]);
66 }
67
68 static struct tst_test test = {
69 .scall = "do_epoll_pwait()",
70 .sample = sample_fn,
71 .setup = setup,
72 .cleanup = cleanup,
73 .test_variants = TEST_VARIANTS,
74 };
75