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