1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2016 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Verify that epoll_ctl() fails with:
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * - EBADF if epfd is an invalid fd.
13f08c3bdfSopenharmony_ci * - EPERM if fd does not support epoll.
14f08c3bdfSopenharmony_ci * - EBADF if fd is an invalid fd.
15f08c3bdfSopenharmony_ci * - EINVAL if op is not supported.
16f08c3bdfSopenharmony_ci * - EINVAL if fd is the same as epfd.
17f08c3bdfSopenharmony_ci * - EINVAL if events is NULL.
18f08c3bdfSopenharmony_ci * - ENOENT if fd is not registered with EPOLL_CTL_DEL.
19f08c3bdfSopenharmony_ci * - ENOENT if fd is not registered with EPOLL_CTL_MOD.
20f08c3bdfSopenharmony_ci * - EEXIST if fd is already registered with EPOLL_CTL_ADD.
21f08c3bdfSopenharmony_ci */
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include <poll.h>
24f08c3bdfSopenharmony_ci#include <sys/epoll.h>
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#include "tst_test.h"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic int epfd;
29f08c3bdfSopenharmony_cistatic int fd[2], unsupported_fd;
30f08c3bdfSopenharmony_cistatic int inv = -1;
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_cistatic struct epoll_event events[2] = {
33f08c3bdfSopenharmony_ci	{.events = EPOLLIN},
34f08c3bdfSopenharmony_ci	{.events = EPOLLOUT},
35f08c3bdfSopenharmony_ci};
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic struct testcase {
38f08c3bdfSopenharmony_ci	int *epfd;
39f08c3bdfSopenharmony_ci	int opt;
40f08c3bdfSopenharmony_ci	int *fd;
41f08c3bdfSopenharmony_ci	struct epoll_event *event;
42f08c3bdfSopenharmony_ci	int exp_err;
43f08c3bdfSopenharmony_ci	const char *desc;
44f08c3bdfSopenharmony_ci} tc[] = {
45f08c3bdfSopenharmony_ci	{&inv, EPOLL_CTL_ADD, &fd[1], &events[1], EBADF, "epfd is an invalid fd"},
46f08c3bdfSopenharmony_ci	{&epfd, EPOLL_CTL_ADD, &unsupported_fd, &events[1], EPERM, "fd does not support epoll"},
47f08c3bdfSopenharmony_ci	{&epfd, EPOLL_CTL_ADD, &inv, &events[1], EBADF, "fd is an invalid fd"},
48f08c3bdfSopenharmony_ci	{&epfd, -1, &fd[1], &events[1], EINVAL, "op is not supported"},
49f08c3bdfSopenharmony_ci	{&epfd, EPOLL_CTL_ADD, &epfd, &events[1], EINVAL, "fd is the same as epfd"},
50f08c3bdfSopenharmony_ci	{&epfd, EPOLL_CTL_ADD, &fd[1], NULL, EFAULT, "events is NULL"},
51f08c3bdfSopenharmony_ci	{&epfd, EPOLL_CTL_DEL, &fd[1], &events[1], ENOENT, "fd is not registered with EPOLL_CTL_DEL"},
52f08c3bdfSopenharmony_ci	{&epfd, EPOLL_CTL_MOD, &fd[1], &events[1], ENOENT, "fd is not registered with EPOLL_CTL_MOD"},
53f08c3bdfSopenharmony_ci	{&epfd, EPOLL_CTL_ADD, &fd[0], &events[0], EEXIST, "fd is already registered with EPOLL_CTL_ADD"}
54f08c3bdfSopenharmony_ci};
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic void setup(void)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	unsupported_fd = SAFE_OPEN(".", O_RDONLY|O_DIRECTORY, 0);
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	epfd = epoll_create(2);
61f08c3bdfSopenharmony_ci	if (epfd == -1)
62f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "fail to create epoll instance");
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	SAFE_PIPE(fd);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	events[0].data.fd = fd[0];
67f08c3bdfSopenharmony_ci	events[1].data.fd = fd[1];
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd[0], &events[0]))
70f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic void cleanup(void)
74f08c3bdfSopenharmony_ci{
75f08c3bdfSopenharmony_ci	if (epfd)
76f08c3bdfSopenharmony_ci		SAFE_CLOSE(epfd);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if (fd[0])
79f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd[0]);
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	if (fd[1])
82f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd[1]);
83f08c3bdfSopenharmony_ci}
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_cistatic void verify_epoll_ctl(unsigned int n)
86f08c3bdfSopenharmony_ci{
87f08c3bdfSopenharmony_ci	TST_EXP_FAIL(epoll_ctl(*tc[n].epfd, tc[n].opt, *tc[n].fd, tc[n].event),
88f08c3bdfSopenharmony_ci		     tc[n].exp_err, "epoll_ctl(...) if %s", tc[n].desc);
89f08c3bdfSopenharmony_ci}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_cistatic struct tst_test test = {
92f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tc),
93f08c3bdfSopenharmony_ci	.setup = setup,
94f08c3bdfSopenharmony_ci	.cleanup = cleanup,
95f08c3bdfSopenharmony_ci	.test = verify_epoll_ctl,
96f08c3bdfSopenharmony_ci};
97