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 * Basic test for epoll_pwait and epoll_pwait2. Checks if data avaiable in a
11f08c3bdfSopenharmony_ci * file descriptor are reported correctly in the syscall return value.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <sys/epoll.h>
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci#include "epoll_pwait_var.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic int efd, sfd[2];
20f08c3bdfSopenharmony_cistatic struct epoll_event e;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic void run(void)
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	TEST(do_epoll_pwait(efd, &e, 1, -1, NULL));
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	if (TST_RET == 1) {
27f08c3bdfSopenharmony_ci		tst_res(TPASS, "do_epoll_pwait() succeeded");
28f08c3bdfSopenharmony_ci		return;
29f08c3bdfSopenharmony_ci	}
30f08c3bdfSopenharmony_ci	tst_res(TFAIL, "do_epoll_pwait() returned %li, expected 1", TST_RET);
31f08c3bdfSopenharmony_ci}
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void setup(void)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	epoll_pwait_init();
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	SAFE_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, sfd);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	efd = epoll_create(1);
40f08c3bdfSopenharmony_ci	if (efd == -1)
41f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "epoll_create()");
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	e.events = EPOLLIN;
44f08c3bdfSopenharmony_ci	if (epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e))
45f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
46f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, sfd[1], "w", 1);
47f08c3bdfSopenharmony_ci}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_cistatic void cleanup(void)
50f08c3bdfSopenharmony_ci{
51f08c3bdfSopenharmony_ci	if (efd > 0)
52f08c3bdfSopenharmony_ci		SAFE_CLOSE(efd);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	if (sfd[0] > 0)
55f08c3bdfSopenharmony_ci		SAFE_CLOSE(sfd[0]);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (sfd[1] > 0)
58f08c3bdfSopenharmony_ci		SAFE_CLOSE(sfd[1]);
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic struct tst_test test = {
62f08c3bdfSopenharmony_ci	.test_all = run,
63f08c3bdfSopenharmony_ci	.setup = setup,
64f08c3bdfSopenharmony_ci	.cleanup = cleanup,
65f08c3bdfSopenharmony_ci	.test_variants = TEST_VARIANTS,
66f08c3bdfSopenharmony_ci};
67