1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Verify that EPOLLONESHOT is correctly handled by epoll_wait.
10f08c3bdfSopenharmony_ci * We open a channel, write in it two times and verify that EPOLLIN has been
11f08c3bdfSopenharmony_ci * received only once.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <poll.h>
15f08c3bdfSopenharmony_ci#include <sys/epoll.h>
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci#include "tst_epoll.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic int fds[2];
20f08c3bdfSopenharmony_cistatic int epfd;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic void cleanup(void)
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	if (epfd > 0)
25f08c3bdfSopenharmony_ci		SAFE_CLOSE(epfd);
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	if (fds[0] > 0)
28f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[0]);
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	if (fds[1] > 0)
31f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[1]);
32f08c3bdfSopenharmony_ci}
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic void run(void)
35f08c3bdfSopenharmony_ci{
36f08c3bdfSopenharmony_ci	struct epoll_event evt_receive;
37f08c3bdfSopenharmony_ci	char buff = 'a';
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	SAFE_PIPE(fds);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	tst_res(TINFO, "Polling on channel with EPOLLONESHOT");
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	epfd = SAFE_EPOLL_CREATE1(0);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	SAFE_EPOLL_CTL(epfd, EPOLL_CTL_ADD, fds[0], &((struct epoll_event) {
46f08c3bdfSopenharmony_ci		.events = EPOLLIN | EPOLLONESHOT,
47f08c3bdfSopenharmony_ci		.data.fd = fds[0],
48f08c3bdfSopenharmony_ci	}));
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	tst_res(TINFO, "Write channel for the 1st time. EPOLLIN expected");
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	SAFE_WRITE(0, fds[1], &buff, 1);
53f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(SAFE_EPOLL_WAIT(epfd, &evt_receive, 10, 0), 1);
54f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(evt_receive.events & EPOLLIN, EPOLLIN);
55f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(evt_receive.data.fd, fds[0]);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	SAFE_READ(1, fds[0], &buff, 1);
58f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(SAFE_EPOLL_WAIT(epfd, &evt_receive, 10, 0), 0);
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	tst_res(TINFO, "Write channel for the 2nd time. No events expected");
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	SAFE_WRITE(0, fds[1], &buff, 1);
63f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(SAFE_EPOLL_WAIT(epfd, &evt_receive, 10, 0), 0);
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	SAFE_CLOSE(epfd);
66f08c3bdfSopenharmony_ci	SAFE_CLOSE(fds[0]);
67f08c3bdfSopenharmony_ci	SAFE_CLOSE(fds[1]);
68f08c3bdfSopenharmony_ci}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic struct tst_test test = {
71f08c3bdfSopenharmony_ci	.cleanup = cleanup,
72f08c3bdfSopenharmony_ci	.test_all = run,
73f08c3bdfSopenharmony_ci};
74