aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/epoll_ctl/epoll_ctl03.c
blob: f617295cc1c181e335c2ea8fe6f2749726eaa3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) Linux Test Project, 2021
 * Author: Xie Ziyao <ziyaoxie@outlook.com>
 */

/*\
 * [Description]
 *
 * Check that epoll_ctl returns zero with different combinations of events on
 * success.
 */

#include <poll.h>
#include <sys/epoll.h>

#include "tst_test.h"
#include "tst_bitmap.h"

#define NUM_EPOLL_EVENTS 8
#define WIDTH_EPOLL_EVENTS 256

static int epfd, fds[2];
static struct epoll_event events = {.events = EPOLLIN};

static unsigned int events_type[NUM_EPOLL_EVENTS] = {
		EPOLLIN, EPOLLOUT, EPOLLPRI, EPOLLERR,
		EPOLLHUP, EPOLLET, EPOLLONESHOT, EPOLLRDHUP
};

static void run_all(void)
{
	unsigned int i, j, events_bitmap;

	for (i = 0; i < WIDTH_EPOLL_EVENTS; i++) {
		events_bitmap = 0;

		for (j = 0; j < NUM_EPOLL_EVENTS; j++)
			events_bitmap |= (events_type[j] * TST_IS_BIT_SET(i, j));

		events.events = events_bitmap;
		TST_EXP_PASS(epoll_ctl(epfd, EPOLL_CTL_MOD, fds[0], &events),
			     "epoll_ctl(..., EPOLL_CTL_MOD, ...) with events.events=%08x",
			     events.events);
	}
}

static void setup(void)
{
	epfd = epoll_create(1);
	if (epfd == -1)
		tst_brk(TBROK | TERRNO, "fail to create epoll instance");

	SAFE_PIPE(fds);
	events.data.fd = fds[0];

	if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &events))
		tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
}

static void cleanup(void)
{
	if (epfd)
		SAFE_CLOSE(epfd);

	if (fds[0])
		SAFE_CLOSE(fds[0]);

	if (fds[1])
		SAFE_CLOSE(fds[1]);
}

static struct tst_test test = {
	.setup = setup,
	.cleanup = cleanup,
	.test_all = run_all,
};