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