xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/epoll_pwait/epoll_pwait05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 <[email protected]>
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 	epoll_pwait2_supported();
42 
43 	SAFE_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, sfd);
44 
45 	efd = epoll_create(1);
46 	if (efd == -1)
47 		tst_brk(TBROK | TERRNO, "epoll_create()");
48 
49 	e.events = EPOLLIN;
50 	if (epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e))
51 		tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
52 	SAFE_WRITE(SAFE_WRITE_ALL, sfd[1], "w", 1);
53 }
54 
cleanup(void)55 static void cleanup(void)
56 {
57 	if (efd > 0)
58 		SAFE_CLOSE(efd);
59 
60 	if (sfd[0] > 0)
61 		SAFE_CLOSE(sfd[0]);
62 
63 	if (sfd[1] > 0)
64 		SAFE_CLOSE(sfd[1]);
65 }
66 
67 static struct tst_test test = {
68 	.test = run_all,
69 	.setup = setup,
70 	.cleanup = cleanup,
71 	.tcnt = ARRAY_SIZE(tc),
72 };
73