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 * Verify that, epoll_pwait2() return -1 and set errno to EINVAL with an
11 * invalid timespec.
12 */
13
14 #include <sys/epoll.h>
15
16 #include "tst_test.h"
17 #include "tst_timer.h"
18 #include "lapi/epoll.h"
19
20 static int efd, sfd[2];
21 static struct epoll_event e;
22
23 static struct test_case_t {
24 struct timespec ts;
25 int exp_errno;
26 const char *desc;
27 } tc[] = {
28 {{.tv_sec = -1}, EINVAL, "ts.tv_sec < 0"},
29 {{.tv_nsec = -1}, EINVAL, "ts.tv_nsec < 0"},
30 {{.tv_nsec = NSEC_PER_SEC}, EINVAL, "ts.tv_nsec >= NSEC_PER_SEC"},
31 };
32
run_all(unsigned int n)33 static void run_all(unsigned int n)
34 {
35 TST_EXP_FAIL(epoll_pwait2(efd, &e, 1, &tc[n].ts, NULL),
36 tc[n].exp_errno, "with %s", tc[n].desc);
37 }
38
setup(void)39 static void setup(void)
40 {
41 SAFE_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, sfd);
42
43 efd = epoll_create(1);
44 if (efd == -1)
45 tst_brk(TBROK | TERRNO, "epoll_create()");
46
47 e.events = EPOLLIN;
48 if (epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e))
49 tst_brk(TBROK | TERRNO, "epoll_clt(..., EPOLL_CTL_ADD, ...)");
50 SAFE_WRITE(1, sfd[1], "w", 1);
51 }
52
cleanup(void)53 static void cleanup(void)
54 {
55 if (efd > 0)
56 SAFE_CLOSE(efd);
57
58 if (sfd[0] > 0)
59 SAFE_CLOSE(sfd[0]);
60
61 if (sfd[1] > 0)
62 SAFE_CLOSE(sfd[1]);
63 }
64
65 static struct tst_test test = {
66 .test = run_all,
67 .setup = setup,
68 .cleanup = cleanup,
69 .tcnt = ARRAY_SIZE(tc),
70 };
71