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