1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2021
4f08c3bdfSopenharmony_ci * Author: Xie Ziyao <ziyaoxie@outlook.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Check that epoll_ctl returns zero with different combinations of events on
11f08c3bdfSopenharmony_ci * success.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <poll.h>
15f08c3bdfSopenharmony_ci#include <sys/epoll.h>
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci#include "tst_bitmap.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#define NUM_EPOLL_EVENTS 8
21f08c3bdfSopenharmony_ci#define WIDTH_EPOLL_EVENTS 256
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic int epfd, fds[2];
24f08c3bdfSopenharmony_cistatic struct epoll_event events = {.events = EPOLLIN};
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic unsigned int events_type[NUM_EPOLL_EVENTS] = {
27f08c3bdfSopenharmony_ci		EPOLLIN, EPOLLOUT, EPOLLPRI, EPOLLERR,
28f08c3bdfSopenharmony_ci		EPOLLHUP, EPOLLET, EPOLLONESHOT, EPOLLRDHUP
29f08c3bdfSopenharmony_ci};
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void run_all(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	unsigned int i, j, events_bitmap;
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	for (i = 0; i < WIDTH_EPOLL_EVENTS; i++) {
36f08c3bdfSopenharmony_ci		events_bitmap = 0;
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci		for (j = 0; j < NUM_EPOLL_EVENTS; j++)
39f08c3bdfSopenharmony_ci			events_bitmap |= (events_type[j] * TST_IS_BIT_SET(i, j));
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci		events.events = events_bitmap;
42f08c3bdfSopenharmony_ci		TST_EXP_PASS(epoll_ctl(epfd, EPOLL_CTL_MOD, fds[0], &events),
43f08c3bdfSopenharmony_ci			     "epoll_ctl(..., EPOLL_CTL_MOD, ...) with events.events=%08x",
44f08c3bdfSopenharmony_ci			     events.events);
45f08c3bdfSopenharmony_ci	}
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void setup(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	epfd = epoll_create(1);
51f08c3bdfSopenharmony_ci	if (epfd == -1)
52f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "fail to create epoll instance");
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	SAFE_PIPE(fds);
55f08c3bdfSopenharmony_ci	events.data.fd = fds[0];
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &events))
58f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic void cleanup(void)
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	if (epfd)
64f08c3bdfSopenharmony_ci		SAFE_CLOSE(epfd);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	if (fds[0])
67f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[0]);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	if (fds[1])
70f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[1]);
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic struct tst_test test = {
74f08c3bdfSopenharmony_ci	.setup = setup,
75f08c3bdfSopenharmony_ci	.cleanup = cleanup,
76f08c3bdfSopenharmony_ci	.test_all = run_all,
77f08c3bdfSopenharmony_ci};
78