1*2c545067Schristos /* $NetBSD: t_epoll.c,v 1.2 2023/07/30 18:31:14 christos Exp $ */
2d11110f4Schristos
3d11110f4Schristos /*-
4d11110f4Schristos * Copyright (c) 2023 The NetBSD Foundation, Inc.
5d11110f4Schristos * All rights reserved.
6d11110f4Schristos *
7d11110f4Schristos * This code is derived from software contributed to The NetBSD Foundation
8d11110f4Schristos * by Theodore Preduta.
9d11110f4Schristos *
10d11110f4Schristos * Redistribution and use in source and binary forms, with or without
11d11110f4Schristos * modification, are permitted provided that the following conditions
12d11110f4Schristos * are met:
13d11110f4Schristos * 1. Redistributions of source code must retain the above copyright
14d11110f4Schristos * notice, this list of conditions and the following disclaimer.
15d11110f4Schristos * 2. Redistributions in binary form must reproduce the above copyright
16d11110f4Schristos * notice, this list of conditions and the following disclaimer in the
17d11110f4Schristos * documentation and/or other materials provided with the distribution.
18d11110f4Schristos *
19d11110f4Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d11110f4Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d11110f4Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d11110f4Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d11110f4Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d11110f4Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d11110f4Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d11110f4Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d11110f4Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d11110f4Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d11110f4Schristos * POSSIBILITY OF SUCH DAMAGE.
30d11110f4Schristos */
31d11110f4Schristos #include <sys/cdefs.h>
32*2c545067Schristos __RCSID("$NetBSD: t_epoll.c,v 1.2 2023/07/30 18:31:14 christos Exp $");
33d11110f4Schristos
34d11110f4Schristos #include <sys/param.h>
35d11110f4Schristos #include <sys/types.h>
36d11110f4Schristos #include <sys/epoll.h>
37*2c545067Schristos #include <sys/fcntl.h>
38d11110f4Schristos #include <errno.h>
39d11110f4Schristos
40d11110f4Schristos #include <atf-c.h>
41d11110f4Schristos
42d11110f4Schristos #include "h_macros.h"
43d11110f4Schristos
44d11110f4Schristos ATF_TC(create_size);
ATF_TC_HEAD(create_size,tc)45d11110f4Schristos ATF_TC_HEAD(create_size, tc)
46d11110f4Schristos {
47d11110f4Schristos
48d11110f4Schristos atf_tc_set_md_var(tc, "descr",
49d11110f4Schristos "Checks that epoll_create requires a non-positive size");
50d11110f4Schristos }
ATF_TC_BODY(create_size,tc)51d11110f4Schristos ATF_TC_BODY(create_size, tc)
52d11110f4Schristos {
53d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_create(-1), -1,
54d11110f4Schristos "epoll_create succeeded unexpectedly");
55d11110f4Schristos ATF_REQUIRE_ERRNO(EINVAL, true);
56d11110f4Schristos
57d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_create(0), -1,
58d11110f4Schristos "epoll_create succeeded unexpectedly");
59d11110f4Schristos ATF_REQUIRE_ERRNO(EINVAL, true);
60d11110f4Schristos
61d11110f4Schristos RL(epoll_create(1));
62d11110f4Schristos }
63d11110f4Schristos
64*2c545067Schristos ATF_TC(create_cloexec);
ATF_TC_HEAD(create_cloexec,tc)65*2c545067Schristos ATF_TC_HEAD(create_cloexec, tc)
66*2c545067Schristos {
67*2c545067Schristos
68*2c545067Schristos atf_tc_set_md_var(tc, "descr",
69*2c545067Schristos "Checks that epoll_create1 sets close on exec when desired");
70*2c545067Schristos }
ATF_TC_BODY(create_cloexec,tc)71*2c545067Schristos ATF_TC_BODY(create_cloexec, tc)
72*2c545067Schristos {
73*2c545067Schristos int fd;
74*2c545067Schristos
75*2c545067Schristos RL(fd = epoll_create1(0));
76*2c545067Schristos ATF_REQUIRE_MSG((fcntl(fd, F_GETFD) & FD_CLOEXEC) == 0,
77*2c545067Schristos "Close on exec set unexpectedly.");
78*2c545067Schristos
79*2c545067Schristos RL(fd = epoll_create1(EPOLL_CLOEXEC));
80*2c545067Schristos ATF_REQUIRE_MSG((fcntl(fd, F_GETFD) & FD_CLOEXEC) != 0,
81*2c545067Schristos "Close on exec was not set.");
82*2c545067Schristos }
83*2c545067Schristos
84d11110f4Schristos ATF_TC(bad_epfd);
ATF_TC_HEAD(bad_epfd,tc)85d11110f4Schristos ATF_TC_HEAD(bad_epfd, tc)
86d11110f4Schristos {
87d11110f4Schristos
88d11110f4Schristos atf_tc_set_md_var(tc, "descr",
89d11110f4Schristos "Checks that epoll_ctl detects an invalid epfd");
90d11110f4Schristos }
ATF_TC_BODY(bad_epfd,tc)91d11110f4Schristos ATF_TC_BODY(bad_epfd, tc)
92d11110f4Schristos {
93d11110f4Schristos int fd;
94d11110f4Schristos struct epoll_event event;
95d11110f4Schristos
96d11110f4Schristos RL(fd = epoll_create1(0));
97d11110f4Schristos event.events = EPOLLIN;
98d11110f4Schristos
99d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_ctl(-1, EPOLL_CTL_ADD, fd, &event), -1,
100d11110f4Schristos "epoll_ctl succeeded unexpectedly");
101d11110f4Schristos ATF_REQUIRE_ERRNO(EBADF, true);
102d11110f4Schristos }
103d11110f4Schristos
104d11110f4Schristos ATF_TC(bad_fd);
ATF_TC_HEAD(bad_fd,tc)105d11110f4Schristos ATF_TC_HEAD(bad_fd, tc)
106d11110f4Schristos {
107d11110f4Schristos
108d11110f4Schristos atf_tc_set_md_var(tc, "descr",
109d11110f4Schristos "Checks that epoll_ctl detects an invalid fd");
110d11110f4Schristos }
ATF_TC_BODY(bad_fd,tc)111d11110f4Schristos ATF_TC_BODY(bad_fd, tc)
112d11110f4Schristos {
113d11110f4Schristos int epfd;
114d11110f4Schristos struct epoll_event event;
115d11110f4Schristos
116d11110f4Schristos RL(epfd = epoll_create1(0));
117d11110f4Schristos event.events = EPOLLIN;
118d11110f4Schristos
119d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, -1, &event), -1,
120d11110f4Schristos "epoll_ctl succeeded unexpectedly");
121d11110f4Schristos ATF_REQUIRE_ERRNO(EBADF, true);
122d11110f4Schristos }
123d11110f4Schristos
124d11110f4Schristos ATF_TC(double_add);
ATF_TC_HEAD(double_add,tc)125d11110f4Schristos ATF_TC_HEAD(double_add, tc)
126d11110f4Schristos {
127d11110f4Schristos
128d11110f4Schristos atf_tc_set_md_var(tc, "descr",
129d11110f4Schristos "Checks that epoll_ctl detects if a fd has already been added");
130d11110f4Schristos }
ATF_TC_BODY(double_add,tc)131d11110f4Schristos ATF_TC_BODY(double_add, tc)
132d11110f4Schristos {
133d11110f4Schristos int epfd, fd;
134d11110f4Schristos struct epoll_event event;
135d11110f4Schristos
136d11110f4Schristos RL(epfd = epoll_create1(0));
137d11110f4Schristos RL(fd = epoll_create1(0));
138d11110f4Schristos event.events = EPOLLIN;
139d11110f4Schristos
140d11110f4Schristos RL(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event));
141d11110f4Schristos
142d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event), -1,
143d11110f4Schristos "epoll_ctl succeeded unexpectedly");
144d11110f4Schristos ATF_REQUIRE_ERRNO(EEXIST, true);
145d11110f4Schristos }
146d11110f4Schristos
147d11110f4Schristos ATF_TC(not_added);
ATF_TC_HEAD(not_added,tc)148d11110f4Schristos ATF_TC_HEAD(not_added, tc)
149d11110f4Schristos {
150d11110f4Schristos
151d11110f4Schristos atf_tc_set_md_var(tc, "descr",
152d11110f4Schristos "Checks that epoll_ctl detects if a fd has not been added");
153d11110f4Schristos }
ATF_TC_BODY(not_added,tc)154d11110f4Schristos ATF_TC_BODY(not_added, tc)
155d11110f4Schristos {
156d11110f4Schristos int epfd, fd;
157d11110f4Schristos struct epoll_event event;
158d11110f4Schristos
159d11110f4Schristos RL(epfd = epoll_create1(0));
160d11110f4Schristos RL(fd = epoll_create1(0));
161d11110f4Schristos event.events = EPOLLIN;
162d11110f4Schristos
163d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &event), -1,
164d11110f4Schristos "epoll_ctl succeeded unexpectedly");
165d11110f4Schristos ATF_REQUIRE_ERRNO(ENOENT, true);
166d11110f4Schristos
167d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL), -1,
168d11110f4Schristos "epoll_ctl succeeded unexpectedly");
169d11110f4Schristos ATF_REQUIRE_ERRNO(ENOENT, true);
170d11110f4Schristos }
171d11110f4Schristos
172d11110f4Schristos ATF_TC(watching_self);
ATF_TC_HEAD(watching_self,tc)173d11110f4Schristos ATF_TC_HEAD(watching_self, tc)
174d11110f4Schristos {
175d11110f4Schristos
176d11110f4Schristos atf_tc_set_md_var(tc, "descr",
177d11110f4Schristos "Checks that epoll disallows watching itself");
178d11110f4Schristos }
ATF_TC_BODY(watching_self,tc)179d11110f4Schristos ATF_TC_BODY(watching_self, tc)
180d11110f4Schristos {
181d11110f4Schristos int epfd;
182d11110f4Schristos struct epoll_event event;
183d11110f4Schristos
184d11110f4Schristos RL(epfd = epoll_create1(0));
185d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, epfd, &event), -1,
186d11110f4Schristos "epoll_ctl succeeded unexpectedly");
187d11110f4Schristos ATF_REQUIRE_ERRNO(EINVAL, true);
188d11110f4Schristos }
189d11110f4Schristos
190d11110f4Schristos ATF_TC(watch_loops);
ATF_TC_HEAD(watch_loops,tc)191d11110f4Schristos ATF_TC_HEAD(watch_loops, tc)
192d11110f4Schristos {
193d11110f4Schristos
194d11110f4Schristos atf_tc_set_md_var(tc, "descr", "Checks that epoll disallows loops");
195d11110f4Schristos }
ATF_TC_BODY(watch_loops,tc)196d11110f4Schristos ATF_TC_BODY(watch_loops, tc)
197d11110f4Schristos {
198d11110f4Schristos int epfd1, epfd2;
199d11110f4Schristos struct epoll_event event;
200d11110f4Schristos
201d11110f4Schristos event.events = EPOLLIN;
202d11110f4Schristos RL(epfd1 = epoll_create1(0));
203d11110f4Schristos RL(epfd2 = epoll_create1(0));
204d11110f4Schristos RL(epoll_ctl(epfd1, EPOLL_CTL_ADD, epfd2, &event));
205d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd2, EPOLL_CTL_ADD, epfd1, &event), -1,
206d11110f4Schristos "epoll_ctl succeeded unexpectedly");
207d11110f4Schristos ATF_REQUIRE_ERRNO(ELOOP, true);
208d11110f4Schristos }
209d11110f4Schristos
210d11110f4Schristos ATF_TC(watch_depth);
ATF_TC_HEAD(watch_depth,tc)211d11110f4Schristos ATF_TC_HEAD(watch_depth, tc)
212d11110f4Schristos {
213d11110f4Schristos
214d11110f4Schristos atf_tc_set_md_var(tc, "descr",
215d11110f4Schristos "Checks that epoll fails when the watch depth exceeds 5");
216d11110f4Schristos }
ATF_TC_BODY(watch_depth,tc)217d11110f4Schristos ATF_TC_BODY(watch_depth, tc)
218d11110f4Schristos {
219d11110f4Schristos int epfd, tmp;
220d11110f4Schristos struct epoll_event event;
221d11110f4Schristos
222d11110f4Schristos event.events = EPOLLIN;
223d11110f4Schristos RL(epfd = epoll_create1(0));
224d11110f4Schristos for (size_t i = 0; i < 4; i++) {
225d11110f4Schristos RL(tmp = epoll_create1(0));
226d11110f4Schristos RL(epoll_ctl(tmp, EPOLL_CTL_ADD, epfd, &event));
227d11110f4Schristos epfd = tmp;
228d11110f4Schristos }
229d11110f4Schristos RL(tmp = epoll_create1(0));
230d11110f4Schristos ATF_REQUIRE_EQ_MSG(epoll_ctl(tmp, EPOLL_CTL_ADD, epfd, &event), -1,
231d11110f4Schristos "epoll_ctl succeeded unexpectedly");
232d11110f4Schristos ATF_REQUIRE_ERRNO(EINVAL, true);
233d11110f4Schristos }
234d11110f4Schristos
ATF_TP_ADD_TCS(tp)235d11110f4Schristos ATF_TP_ADD_TCS(tp)
236d11110f4Schristos {
237d11110f4Schristos ATF_TP_ADD_TC(tp, create_size);
238*2c545067Schristos ATF_TP_ADD_TC(tp, create_cloexec);
239d11110f4Schristos ATF_TP_ADD_TC(tp, bad_epfd);
240d11110f4Schristos ATF_TP_ADD_TC(tp, bad_fd);
241d11110f4Schristos ATF_TP_ADD_TC(tp, not_added);
242d11110f4Schristos ATF_TP_ADD_TC(tp, watching_self);
243d11110f4Schristos ATF_TP_ADD_TC(tp, watch_loops);
244d11110f4Schristos ATF_TP_ADD_TC(tp, watch_depth);
245d11110f4Schristos
246d11110f4Schristos return atf_no_error();
247d11110f4Schristos }
248