t_timerfd.c revision 1.2 1 1.2 thorpej /* $NetBSD: t_timerfd.c,v 1.2 2021/09/19 15:51:28 thorpej Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.2 thorpej * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.2 thorpej * modification, are permitted provided that the following conditions
9 1.2 thorpej * are met:
10 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.2 thorpej *
16 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.2 thorpej */
28 1.2 thorpej
29 1.2 thorpej #include <sys/cdefs.h>
30 1.2 thorpej __COPYRIGHT("@(#) Copyright (c) 2020\
31 1.2 thorpej The NetBSD Foundation, inc. All rights reserved.");
32 1.2 thorpej __RCSID("$NetBSD: t_timerfd.c,v 1.2 2021/09/19 15:51:28 thorpej Exp $");
33 1.2 thorpej
34 1.2 thorpej #include <sys/types.h>
35 1.2 thorpej #include <sys/event.h>
36 1.2 thorpej #include <sys/select.h>
37 1.2 thorpej #include <sys/stat.h>
38 1.2 thorpej #include <sys/syscall.h>
39 1.2 thorpej #include <sys/timerfd.h>
40 1.2 thorpej #include <errno.h>
41 1.2 thorpej #include <poll.h>
42 1.2 thorpej #include <pthread.h>
43 1.2 thorpej #include <stdlib.h>
44 1.2 thorpej #include <stdio.h>
45 1.2 thorpej #include <time.h>
46 1.2 thorpej #include <unistd.h>
47 1.2 thorpej
48 1.2 thorpej #include <atf-c.h>
49 1.2 thorpej
50 1.2 thorpej struct helper_context {
51 1.2 thorpej int fd;
52 1.2 thorpej
53 1.2 thorpej pthread_barrier_t barrier;
54 1.2 thorpej };
55 1.2 thorpej
56 1.2 thorpej static void
57 1.2 thorpej init_helper_context(struct helper_context * const ctx)
58 1.2 thorpej {
59 1.2 thorpej
60 1.2 thorpej memset(ctx, 0, sizeof(*ctx));
61 1.2 thorpej
62 1.2 thorpej ATF_REQUIRE(pthread_barrier_init(&ctx->barrier, NULL, 2) == 0);
63 1.2 thorpej }
64 1.2 thorpej
65 1.2 thorpej static bool
66 1.2 thorpej wait_barrier(struct helper_context * const ctx)
67 1.2 thorpej {
68 1.2 thorpej int rv = pthread_barrier_wait(&ctx->barrier);
69 1.2 thorpej
70 1.2 thorpej return rv == 0 || rv == PTHREAD_BARRIER_SERIAL_THREAD;
71 1.2 thorpej }
72 1.2 thorpej
73 1.2 thorpej /*****************************************************************************/
74 1.2 thorpej
75 1.2 thorpej static int
76 1.2 thorpej timerfd_read(int fd, uint64_t *valp)
77 1.2 thorpej {
78 1.2 thorpej uint64_t val;
79 1.2 thorpej
80 1.2 thorpej switch (read(fd, &val, sizeof(val))) {
81 1.2 thorpej case -1:
82 1.2 thorpej return -1;
83 1.2 thorpej
84 1.2 thorpej case sizeof(val):
85 1.2 thorpej *valp = val;
86 1.2 thorpej return 0;
87 1.2 thorpej
88 1.2 thorpej default:
89 1.2 thorpej /* ?? Should never happen. */
90 1.2 thorpej errno = EIO;
91 1.2 thorpej return -1;
92 1.2 thorpej }
93 1.2 thorpej }
94 1.2 thorpej
95 1.2 thorpej /*****************************************************************************/
96 1.2 thorpej
97 1.2 thorpej ATF_TC(timerfd_create);
98 1.2 thorpej ATF_TC_HEAD(timerfd_create, tc)
99 1.2 thorpej {
100 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates timerfd_create()");
101 1.2 thorpej }
102 1.2 thorpej ATF_TC_BODY(timerfd_create, tc)
103 1.2 thorpej {
104 1.2 thorpej int fd;
105 1.2 thorpej
106 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
107 1.2 thorpej (void) close(fd);
108 1.2 thorpej
109 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
110 1.2 thorpej (void) close(fd);
111 1.2 thorpej
112 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
113 1.2 thorpej (fd = timerfd_create(CLOCK_VIRTUAL, 0)) == -1);
114 1.2 thorpej
115 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
116 1.2 thorpej (fd = timerfd_create(CLOCK_PROF, 0)) == -1);
117 1.2 thorpej
118 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
119 1.2 thorpej (fd = timerfd_create(CLOCK_REALTIME,
120 1.2 thorpej ~(TFD_CLOEXEC | TFD_NONBLOCK))) == -1);
121 1.2 thorpej }
122 1.2 thorpej
123 1.2 thorpej /*****************************************************************************/
124 1.2 thorpej
125 1.2 thorpej ATF_TC(timerfd_bogusfd);
126 1.2 thorpej ATF_TC_HEAD(timerfd_bogusfd, tc)
127 1.2 thorpej {
128 1.2 thorpej atf_tc_set_md_var(tc, "descr",
129 1.2 thorpej "validates rejection of bogus fds by timerfd_{get,set}time()");
130 1.2 thorpej }
131 1.2 thorpej ATF_TC_BODY(timerfd_bogusfd, tc)
132 1.2 thorpej {
133 1.2 thorpej struct itimerspec its = { 0 };
134 1.2 thorpej int fd;
135 1.2 thorpej
136 1.2 thorpej ATF_REQUIRE((fd = kqueue()) >= 0); /* arbitrary fd type */
137 1.2 thorpej
138 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
139 1.2 thorpej timerfd_gettime(fd, &its) == -1);
140 1.2 thorpej
141 1.2 thorpej its.it_value.tv_sec = 5;
142 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
143 1.2 thorpej timerfd_settime(fd, 0, &its, NULL) == -1);
144 1.2 thorpej
145 1.2 thorpej (void) close(fd);
146 1.2 thorpej }
147 1.2 thorpej
148 1.2 thorpej /*****************************************************************************/
149 1.2 thorpej
150 1.2 thorpej ATF_TC(timerfd_block);
151 1.2 thorpej ATF_TC_HEAD(timerfd_block, tc)
152 1.2 thorpej {
153 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates blocking behavior");
154 1.2 thorpej }
155 1.2 thorpej ATF_TC_BODY(timerfd_block, tc)
156 1.2 thorpej {
157 1.2 thorpej struct timespec then, now, delta;
158 1.2 thorpej uint64_t val;
159 1.2 thorpej int fd;
160 1.2 thorpej
161 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
162 1.2 thorpej
163 1.2 thorpej const struct itimerspec its = {
164 1.2 thorpej .it_value = { .tv_sec = 1, .tv_nsec = 0 },
165 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
166 1.2 thorpej };
167 1.2 thorpej
168 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
169 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
170 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
171 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
172 1.2 thorpej ATF_REQUIRE(val == 1);
173 1.2 thorpej
174 1.2 thorpej timespecsub(&now, &then, &delta);
175 1.2 thorpej ATF_REQUIRE(delta.tv_sec == 1);
176 1.2 thorpej
177 1.2 thorpej (void) close(fd);
178 1.2 thorpej }
179 1.2 thorpej
180 1.2 thorpej /*****************************************************************************/
181 1.2 thorpej
182 1.2 thorpej ATF_TC(timerfd_repeating);
183 1.2 thorpej ATF_TC_HEAD(timerfd_repeating, tc)
184 1.2 thorpej {
185 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates repeating timer behavior");
186 1.2 thorpej }
187 1.2 thorpej ATF_TC_BODY(timerfd_repeating, tc)
188 1.2 thorpej {
189 1.2 thorpej struct timespec then, now, delta;
190 1.2 thorpej uint64_t val;
191 1.2 thorpej int fd;
192 1.2 thorpej
193 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC,
194 1.2 thorpej TFD_NONBLOCK)) >= 0);
195 1.2 thorpej
196 1.2 thorpej const struct itimerspec its = {
197 1.2 thorpej .it_value = { .tv_sec = 0, .tv_nsec = 200000000 },
198 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 200000000 },
199 1.2 thorpej };
200 1.2 thorpej
201 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
202 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
203 1.2 thorpej ATF_REQUIRE(sleep(1) == 0);
204 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
205 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
206 1.2 thorpej ATF_REQUIRE(val >= 3 && val <= 5); /* allow some slop */
207 1.2 thorpej
208 1.2 thorpej timespecsub(&now, &then, &delta);
209 1.2 thorpej ATF_REQUIRE(delta.tv_sec == 1);
210 1.2 thorpej
211 1.2 thorpej (void) close(fd);
212 1.2 thorpej }
213 1.2 thorpej
214 1.2 thorpej /*****************************************************************************/
215 1.2 thorpej
216 1.2 thorpej ATF_TC(timerfd_abstime);
217 1.2 thorpej ATF_TC_HEAD(timerfd_abstime, tc)
218 1.2 thorpej {
219 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates specifying abstime");
220 1.2 thorpej }
221 1.2 thorpej ATF_TC_BODY(timerfd_abstime, tc)
222 1.2 thorpej {
223 1.2 thorpej struct timespec then, now, delta;
224 1.2 thorpej uint64_t val;
225 1.2 thorpej int fd;
226 1.2 thorpej
227 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
228 1.2 thorpej
229 1.2 thorpej struct itimerspec its = {
230 1.2 thorpej .it_value = { .tv_sec = 0, .tv_nsec = 0 },
231 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
232 1.2 thorpej };
233 1.2 thorpej
234 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
235 1.2 thorpej its.it_value = then;
236 1.2 thorpej its.it_value.tv_sec += 1;
237 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL) == 0);
238 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
239 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
240 1.2 thorpej ATF_REQUIRE(val == 1);
241 1.2 thorpej
242 1.2 thorpej timespecsub(&now, &then, &delta);
243 1.2 thorpej ATF_REQUIRE(delta.tv_sec == 1);
244 1.2 thorpej
245 1.2 thorpej (void) close(fd);
246 1.2 thorpej }
247 1.2 thorpej
248 1.2 thorpej /*****************************************************************************/
249 1.2 thorpej
250 1.2 thorpej ATF_TC(timerfd_cancel_on_set_immed);
251 1.2 thorpej ATF_TC_HEAD(timerfd_cancel_on_set_immed, tc)
252 1.2 thorpej {
253 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - immediate");
254 1.2 thorpej atf_tc_set_md_var(tc, "require.user", "root");
255 1.2 thorpej }
256 1.2 thorpej ATF_TC_BODY(timerfd_cancel_on_set_immed, tc)
257 1.2 thorpej {
258 1.2 thorpej struct timespec now;
259 1.2 thorpej uint64_t val;
260 1.2 thorpej int fd;
261 1.2 thorpej
262 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
263 1.2 thorpej
264 1.2 thorpej const struct itimerspec its = {
265 1.2 thorpej .it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
266 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
267 1.2 thorpej };
268 1.2 thorpej
269 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
270 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
271 1.2 thorpej &its, NULL) == 0);
272 1.2 thorpej ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
273 1.2 thorpej ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
274 1.2 thorpej
275 1.2 thorpej (void) close(fd);
276 1.2 thorpej }
277 1.2 thorpej
278 1.2 thorpej /*****************************************************************************/
279 1.2 thorpej
280 1.2 thorpej static void *
281 1.2 thorpej timerfd_cancel_on_set_block_helper(void * const v)
282 1.2 thorpej {
283 1.2 thorpej struct helper_context * const ctx = v;
284 1.2 thorpej struct timespec now;
285 1.2 thorpej
286 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
287 1.2 thorpej
288 1.2 thorpej ATF_REQUIRE(sleep(2) == 0);
289 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
290 1.2 thorpej ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
291 1.2 thorpej
292 1.2 thorpej return NULL;
293 1.2 thorpej }
294 1.2 thorpej
295 1.2 thorpej ATF_TC(timerfd_cancel_on_set_block);
296 1.2 thorpej ATF_TC_HEAD(timerfd_cancel_on_set_block, tc)
297 1.2 thorpej {
298 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - blocking");
299 1.2 thorpej atf_tc_set_md_var(tc, "require.user", "root");
300 1.2 thorpej }
301 1.2 thorpej ATF_TC_BODY(timerfd_cancel_on_set_block, tc)
302 1.2 thorpej {
303 1.2 thorpej struct helper_context ctx;
304 1.2 thorpej pthread_t helper;
305 1.2 thorpej void *join_val;
306 1.2 thorpej uint64_t val;
307 1.2 thorpej int fd;
308 1.2 thorpej
309 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
310 1.2 thorpej
311 1.2 thorpej const struct itimerspec its = {
312 1.2 thorpej .it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
313 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
314 1.2 thorpej };
315 1.2 thorpej
316 1.2 thorpej init_helper_context(&ctx);
317 1.2 thorpej
318 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
319 1.2 thorpej &its, NULL) == 0);
320 1.2 thorpej ATF_REQUIRE(pthread_create(&helper, NULL,
321 1.2 thorpej timerfd_cancel_on_set_block_helper, &ctx) == 0);
322 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
323 1.2 thorpej ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
324 1.2 thorpej
325 1.2 thorpej ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
326 1.2 thorpej
327 1.2 thorpej (void) close(fd);
328 1.2 thorpej }
329 1.2 thorpej
330 1.2 thorpej /*****************************************************************************/
331 1.2 thorpej
332 1.2 thorpej ATF_TC(timerfd_select_poll_kevent_immed);
333 1.2 thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_immed, tc)
334 1.2 thorpej {
335 1.2 thorpej atf_tc_set_md_var(tc, "descr",
336 1.2 thorpej "validates select/poll/kevent behavior - immediate return");
337 1.2 thorpej }
338 1.2 thorpej ATF_TC_BODY(timerfd_select_poll_kevent_immed, tc)
339 1.2 thorpej {
340 1.2 thorpej const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
341 1.2 thorpej struct itimerspec its;
342 1.2 thorpej struct timeval tv;
343 1.2 thorpej struct stat st;
344 1.2 thorpej struct pollfd fds[1];
345 1.2 thorpej uint64_t val;
346 1.2 thorpej fd_set readfds, writefds, exceptfds;
347 1.2 thorpej int fd;
348 1.2 thorpej int kq;
349 1.2 thorpej struct kevent kev[1];
350 1.2 thorpej
351 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
352 1.2 thorpej
353 1.2 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
354 1.2 thorpej EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
355 1.2 thorpej ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
356 1.2 thorpej
357 1.2 thorpej /*
358 1.2 thorpej * fd should be writable but not readable. Pass all of the
359 1.2 thorpej * event bits; we should only get back POLLOUT | POLLWRNORM.
360 1.2 thorpej * (It's writable only in so far as we'll get an error if we try.)
361 1.2 thorpej */
362 1.2 thorpej fds[0].fd = fd;
363 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
364 1.2 thorpej POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
365 1.2 thorpej fds[0].revents = 0;
366 1.2 thorpej ATF_REQUIRE(poll(fds, 1, 0) == 1);
367 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLOUT | POLLWRNORM));
368 1.2 thorpej
369 1.2 thorpej /*
370 1.2 thorpej * As above; fd should only be set in writefds upon return
371 1.2 thorpej * from the select() call.
372 1.2 thorpej */
373 1.2 thorpej FD_ZERO(&readfds);
374 1.2 thorpej FD_ZERO(&writefds);
375 1.2 thorpej FD_ZERO(&exceptfds);
376 1.2 thorpej tv.tv_sec = 0;
377 1.2 thorpej tv.tv_usec = 0;
378 1.2 thorpej FD_SET(fd, &readfds);
379 1.2 thorpej FD_SET(fd, &writefds);
380 1.2 thorpej FD_SET(fd, &exceptfds);
381 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 1);
382 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &readfds));
383 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &writefds));
384 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
385 1.2 thorpej
386 1.2 thorpej /*
387 1.2 thorpej * Now set a one-shot half-second timer, wait for it to expire, and
388 1.2 thorpej * then check again.
389 1.2 thorpej */
390 1.2 thorpej memset(&its, 0, sizeof(its));
391 1.2 thorpej its.it_value.tv_sec = 0;
392 1.2 thorpej its.it_value.tv_nsec = 500000000;
393 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
394 1.2 thorpej ATF_REQUIRE(sleep(2) == 0);
395 1.2 thorpej
396 1.2 thorpej /* Verify it actually fired via the stat() back-channel. */
397 1.2 thorpej ATF_REQUIRE(fstat(fd, &st) == 0);
398 1.2 thorpej ATF_REQUIRE(st.st_size == 1);
399 1.2 thorpej
400 1.2 thorpej fds[0].fd = fd;
401 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
402 1.2 thorpej POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
403 1.2 thorpej fds[0].revents = 0;
404 1.2 thorpej ATF_REQUIRE(poll(fds, 1, 0) == 1);
405 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM |
406 1.2 thorpej POLLOUT | POLLWRNORM));
407 1.2 thorpej
408 1.2 thorpej FD_ZERO(&readfds);
409 1.2 thorpej FD_ZERO(&writefds);
410 1.2 thorpej FD_ZERO(&exceptfds);
411 1.2 thorpej tv.tv_sec = 0;
412 1.2 thorpej tv.tv_usec = 0;
413 1.2 thorpej FD_SET(fd, &readfds);
414 1.2 thorpej FD_SET(fd, &writefds);
415 1.2 thorpej FD_SET(fd, &exceptfds);
416 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 2);
417 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &readfds));
418 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &writefds));
419 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
420 1.2 thorpej
421 1.2 thorpej /*
422 1.2 thorpej * Check that we get an EVFILT_READ event on fd.
423 1.2 thorpej */
424 1.2 thorpej memset(kev, 0, sizeof(kev));
425 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, &ts) == 1);
426 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
427 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_READ);
428 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
429 1.2 thorpej ATF_REQUIRE(kev[0].data == 1);
430 1.2 thorpej
431 1.2 thorpej /*
432 1.2 thorpej * Read the timerfd to ensure we get the correct numnber of
433 1.2 thorpej * expirations.
434 1.2 thorpej */
435 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
436 1.2 thorpej ATF_REQUIRE(val == 1);
437 1.2 thorpej
438 1.2 thorpej /* And ensure that we would block if we tried again. */
439 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN, timerfd_read(fd, &val) == -1);
440 1.2 thorpej
441 1.2 thorpej (void) close(kq);
442 1.2 thorpej (void) close(fd);
443 1.2 thorpej }
444 1.2 thorpej
445 1.2 thorpej /*****************************************************************************/
446 1.2 thorpej
447 1.2 thorpej ATF_TC(timerfd_select_poll_kevent_block);
448 1.2 thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_block, tc)
449 1.2 thorpej {
450 1.2 thorpej atf_tc_set_md_var(tc, "descr",
451 1.2 thorpej "validates select/poll/kevent behavior - blocking");
452 1.2 thorpej }
453 1.2 thorpej ATF_TC_BODY(timerfd_select_poll_kevent_block, tc)
454 1.2 thorpej {
455 1.2 thorpej const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
456 1.2 thorpej struct timespec then, now;
457 1.2 thorpej struct pollfd fds[1];
458 1.2 thorpej fd_set readfds;
459 1.2 thorpej int fd;
460 1.2 thorpej int kq;
461 1.2 thorpej struct kevent kev[1];
462 1.2 thorpej
463 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
464 1.2 thorpej
465 1.2 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
466 1.2 thorpej EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
467 1.2 thorpej ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
468 1.2 thorpej
469 1.2 thorpej /*
470 1.2 thorpej * For each of these tests, we do the following:
471 1.2 thorpej *
472 1.2 thorpej * - Get the current time.
473 1.2 thorpej * - Set a 1-second one-shot timer.
474 1.2 thorpej * - Block in the multiplexing call.
475 1.2 thorpej * - Get the current time and verify that the timer expiration
476 1.2 thorpej * interval has passed.
477 1.2 thorpej */
478 1.2 thorpej
479 1.2 thorpej const struct itimerspec its = {
480 1.2 thorpej .it_value = { .tv_sec = 1, .tv_nsec = 0 },
481 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
482 1.2 thorpej };
483 1.2 thorpej
484 1.2 thorpej /* poll(2) */
485 1.2 thorpej fds[0].fd = fd;
486 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM;
487 1.2 thorpej fds[0].revents = 0;
488 1.2 thorpej
489 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
490 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
491 1.2 thorpej ATF_REQUIRE(poll(fds, 1, INFTIM) == 1);
492 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
493 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
494 1.2 thorpej ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
495 1.2 thorpej
496 1.2 thorpej /* select(2) */
497 1.2 thorpej FD_ZERO(&readfds);
498 1.2 thorpej FD_SET(fd, &readfds);
499 1.2 thorpej
500 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
501 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
502 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, NULL, NULL, NULL) == 1);
503 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
504 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &readfds));
505 1.2 thorpej ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
506 1.2 thorpej
507 1.2 thorpej /* kevent(2) */
508 1.2 thorpej memset(kev, 0, sizeof(kev));
509 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
510 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
511 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
512 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
513 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
514 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_READ);
515 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
516 1.2 thorpej ATF_REQUIRE(kev[0].data == 1);
517 1.2 thorpej
518 1.2 thorpej (void) close(kq);
519 1.2 thorpej (void) close(fd);
520 1.2 thorpej }
521 1.2 thorpej
522 1.2 thorpej /*****************************************************************************/
523 1.2 thorpej
524 1.2 thorpej static void *
525 1.2 thorpej timerfd_restart_helper(void * const v)
526 1.2 thorpej {
527 1.2 thorpej struct helper_context * const ctx = v;
528 1.2 thorpej
529 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
530 1.2 thorpej
531 1.2 thorpej /*
532 1.2 thorpej * Wait 5 seconds (that should give the main thread time to
533 1.2 thorpej * block), and then close the descriptor.
534 1.2 thorpej */
535 1.2 thorpej ATF_REQUIRE(sleep(5) == 0);
536 1.2 thorpej ATF_REQUIRE(close(ctx->fd) == 0);
537 1.2 thorpej
538 1.2 thorpej return NULL;
539 1.2 thorpej }
540 1.2 thorpej
541 1.2 thorpej ATF_TC(timerfd_restart);
542 1.2 thorpej ATF_TC_HEAD(timerfd_restart, tc)
543 1.2 thorpej {
544 1.2 thorpej atf_tc_set_md_var(tc, "descr",
545 1.2 thorpej "exercises the 'restart' fileop code path");
546 1.2 thorpej }
547 1.2 thorpej ATF_TC_BODY(timerfd_restart, tc)
548 1.2 thorpej {
549 1.2 thorpej struct timespec then, now, delta;
550 1.2 thorpej struct helper_context ctx;
551 1.2 thorpej uint64_t val;
552 1.2 thorpej pthread_t helper;
553 1.2 thorpej void *join_val;
554 1.2 thorpej
555 1.2 thorpej init_helper_context(&ctx);
556 1.2 thorpej
557 1.2 thorpej ATF_REQUIRE((ctx.fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
558 1.2 thorpej
559 1.2 thorpej const struct itimerspec its = {
560 1.2 thorpej .it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
561 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
562 1.2 thorpej };
563 1.2 thorpej ATF_REQUIRE(timerfd_settime(ctx.fd, 0, &its, NULL) == 0);
564 1.2 thorpej
565 1.2 thorpej
566 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
567 1.2 thorpej ATF_REQUIRE(pthread_create(&helper, NULL,
568 1.2 thorpej timerfd_restart_helper, &ctx) == 0);
569 1.2 thorpej
570 1.2 thorpej /*
571 1.2 thorpej * Wait for the helper to be ready, and then immediately block
572 1.2 thorpej * in read(). The helper will close the file, and we should get
573 1.2 thorpej * EBADF after a few seconds.
574 1.2 thorpej */
575 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
576 1.2 thorpej ATF_REQUIRE_ERRNO(EBADF, timerfd_read(ctx.fd, &val) == -1);
577 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
578 1.2 thorpej
579 1.2 thorpej timespecsub(&now, &then, &delta);
580 1.2 thorpej ATF_REQUIRE(delta.tv_sec >= 5);
581 1.2 thorpej
582 1.2 thorpej /* Reap the helper. */
583 1.2 thorpej ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
584 1.2 thorpej }
585 1.2 thorpej
586 1.2 thorpej /*****************************************************************************/
587 1.2 thorpej
588 1.2 thorpej ATF_TP_ADD_TCS(tp)
589 1.2 thorpej {
590 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_create);
591 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_bogusfd);
592 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_block);
593 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_repeating);
594 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_abstime);
595 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_block);
596 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_immed);
597 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_immed);
598 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_block);
599 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_restart);
600 1.2 thorpej
601 1.2 thorpej return atf_no_error();
602 1.2 thorpej }
603