t_timerfd.c revision 1.3 1 1.3 hannken /* $NetBSD: t_timerfd.c,v 1.3 2021/11/01 14:33:41 hannken 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.3 hannken __RCSID("$NetBSD: t_timerfd.c,v 1.3 2021/11/01 14:33:41 hannken 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.3 hannken #include "isqemu.h"
51 1.3 hannken
52 1.2 thorpej struct helper_context {
53 1.2 thorpej int fd;
54 1.2 thorpej
55 1.2 thorpej pthread_barrier_t barrier;
56 1.2 thorpej };
57 1.2 thorpej
58 1.2 thorpej static void
59 1.2 thorpej init_helper_context(struct helper_context * const ctx)
60 1.2 thorpej {
61 1.2 thorpej
62 1.2 thorpej memset(ctx, 0, sizeof(*ctx));
63 1.2 thorpej
64 1.2 thorpej ATF_REQUIRE(pthread_barrier_init(&ctx->barrier, NULL, 2) == 0);
65 1.2 thorpej }
66 1.2 thorpej
67 1.2 thorpej static bool
68 1.2 thorpej wait_barrier(struct helper_context * const ctx)
69 1.2 thorpej {
70 1.2 thorpej int rv = pthread_barrier_wait(&ctx->barrier);
71 1.2 thorpej
72 1.2 thorpej return rv == 0 || rv == PTHREAD_BARRIER_SERIAL_THREAD;
73 1.2 thorpej }
74 1.2 thorpej
75 1.3 hannken static bool
76 1.3 hannken check_value_against_bounds(uint64_t value, uint64_t lower, uint64_t upper)
77 1.3 hannken {
78 1.3 hannken
79 1.3 hannken /*
80 1.3 hannken * If running under QEMU make sure the upper bound is large
81 1.3 hannken * enough for the effect of kern/43997
82 1.3 hannken */
83 1.3 hannken if (isQEMU()) {
84 1.3 hannken upper *= 4;
85 1.3 hannken }
86 1.3 hannken
87 1.3 hannken if (value < lower || value > upper) {
88 1.3 hannken printf("val %" PRIu64 " not in [ %" PRIu64 ", %" PRIu64 " ]\n",
89 1.3 hannken value, lower, upper);
90 1.3 hannken }
91 1.3 hannken
92 1.3 hannken return value >= lower && value <= upper;
93 1.3 hannken }
94 1.3 hannken
95 1.2 thorpej /*****************************************************************************/
96 1.2 thorpej
97 1.2 thorpej static int
98 1.2 thorpej timerfd_read(int fd, uint64_t *valp)
99 1.2 thorpej {
100 1.2 thorpej uint64_t val;
101 1.2 thorpej
102 1.2 thorpej switch (read(fd, &val, sizeof(val))) {
103 1.2 thorpej case -1:
104 1.2 thorpej return -1;
105 1.2 thorpej
106 1.2 thorpej case sizeof(val):
107 1.2 thorpej *valp = val;
108 1.2 thorpej return 0;
109 1.2 thorpej
110 1.2 thorpej default:
111 1.2 thorpej /* ?? Should never happen. */
112 1.2 thorpej errno = EIO;
113 1.2 thorpej return -1;
114 1.2 thorpej }
115 1.2 thorpej }
116 1.2 thorpej
117 1.2 thorpej /*****************************************************************************/
118 1.2 thorpej
119 1.2 thorpej ATF_TC(timerfd_create);
120 1.2 thorpej ATF_TC_HEAD(timerfd_create, tc)
121 1.2 thorpej {
122 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates timerfd_create()");
123 1.2 thorpej }
124 1.2 thorpej ATF_TC_BODY(timerfd_create, tc)
125 1.2 thorpej {
126 1.2 thorpej int fd;
127 1.2 thorpej
128 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
129 1.2 thorpej (void) close(fd);
130 1.2 thorpej
131 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
132 1.2 thorpej (void) close(fd);
133 1.2 thorpej
134 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
135 1.2 thorpej (fd = timerfd_create(CLOCK_VIRTUAL, 0)) == -1);
136 1.2 thorpej
137 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
138 1.2 thorpej (fd = timerfd_create(CLOCK_PROF, 0)) == -1);
139 1.2 thorpej
140 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
141 1.2 thorpej (fd = timerfd_create(CLOCK_REALTIME,
142 1.2 thorpej ~(TFD_CLOEXEC | TFD_NONBLOCK))) == -1);
143 1.2 thorpej }
144 1.2 thorpej
145 1.2 thorpej /*****************************************************************************/
146 1.2 thorpej
147 1.2 thorpej ATF_TC(timerfd_bogusfd);
148 1.2 thorpej ATF_TC_HEAD(timerfd_bogusfd, tc)
149 1.2 thorpej {
150 1.2 thorpej atf_tc_set_md_var(tc, "descr",
151 1.2 thorpej "validates rejection of bogus fds by timerfd_{get,set}time()");
152 1.2 thorpej }
153 1.2 thorpej ATF_TC_BODY(timerfd_bogusfd, tc)
154 1.2 thorpej {
155 1.2 thorpej struct itimerspec its = { 0 };
156 1.2 thorpej int fd;
157 1.2 thorpej
158 1.2 thorpej ATF_REQUIRE((fd = kqueue()) >= 0); /* arbitrary fd type */
159 1.2 thorpej
160 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
161 1.2 thorpej timerfd_gettime(fd, &its) == -1);
162 1.2 thorpej
163 1.2 thorpej its.it_value.tv_sec = 5;
164 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
165 1.2 thorpej timerfd_settime(fd, 0, &its, NULL) == -1);
166 1.2 thorpej
167 1.2 thorpej (void) close(fd);
168 1.2 thorpej }
169 1.2 thorpej
170 1.2 thorpej /*****************************************************************************/
171 1.2 thorpej
172 1.2 thorpej ATF_TC(timerfd_block);
173 1.2 thorpej ATF_TC_HEAD(timerfd_block, tc)
174 1.2 thorpej {
175 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates blocking behavior");
176 1.2 thorpej }
177 1.2 thorpej ATF_TC_BODY(timerfd_block, tc)
178 1.2 thorpej {
179 1.2 thorpej struct timespec then, now, delta;
180 1.2 thorpej uint64_t val;
181 1.2 thorpej int fd;
182 1.2 thorpej
183 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
184 1.2 thorpej
185 1.2 thorpej const struct itimerspec its = {
186 1.2 thorpej .it_value = { .tv_sec = 1, .tv_nsec = 0 },
187 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
188 1.2 thorpej };
189 1.2 thorpej
190 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
191 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
192 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
193 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
194 1.3 hannken ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
195 1.2 thorpej
196 1.2 thorpej timespecsub(&now, &then, &delta);
197 1.3 hannken ATF_REQUIRE(check_value_against_bounds(delta.tv_sec, 1, 1));
198 1.2 thorpej
199 1.2 thorpej (void) close(fd);
200 1.2 thorpej }
201 1.2 thorpej
202 1.2 thorpej /*****************************************************************************/
203 1.2 thorpej
204 1.2 thorpej ATF_TC(timerfd_repeating);
205 1.2 thorpej ATF_TC_HEAD(timerfd_repeating, tc)
206 1.2 thorpej {
207 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates repeating timer behavior");
208 1.2 thorpej }
209 1.2 thorpej ATF_TC_BODY(timerfd_repeating, tc)
210 1.2 thorpej {
211 1.2 thorpej struct timespec then, now, delta;
212 1.2 thorpej uint64_t val;
213 1.2 thorpej int fd;
214 1.2 thorpej
215 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC,
216 1.2 thorpej TFD_NONBLOCK)) >= 0);
217 1.2 thorpej
218 1.2 thorpej const struct itimerspec its = {
219 1.2 thorpej .it_value = { .tv_sec = 0, .tv_nsec = 200000000 },
220 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 200000000 },
221 1.2 thorpej };
222 1.2 thorpej
223 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
224 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
225 1.2 thorpej ATF_REQUIRE(sleep(1) == 0);
226 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
227 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
228 1.3 hannken /* allow some slop */
229 1.3 hannken ATF_REQUIRE(check_value_against_bounds(val, 3, 5));
230 1.2 thorpej
231 1.2 thorpej timespecsub(&now, &then, &delta);
232 1.3 hannken ATF_REQUIRE(check_value_against_bounds(delta.tv_sec, 1, 1));
233 1.2 thorpej
234 1.2 thorpej (void) close(fd);
235 1.2 thorpej }
236 1.2 thorpej
237 1.2 thorpej /*****************************************************************************/
238 1.2 thorpej
239 1.2 thorpej ATF_TC(timerfd_abstime);
240 1.2 thorpej ATF_TC_HEAD(timerfd_abstime, tc)
241 1.2 thorpej {
242 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates specifying abstime");
243 1.2 thorpej }
244 1.2 thorpej ATF_TC_BODY(timerfd_abstime, tc)
245 1.2 thorpej {
246 1.2 thorpej struct timespec then, now, delta;
247 1.2 thorpej uint64_t val;
248 1.2 thorpej int fd;
249 1.2 thorpej
250 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
251 1.2 thorpej
252 1.2 thorpej struct itimerspec its = {
253 1.2 thorpej .it_value = { .tv_sec = 0, .tv_nsec = 0 },
254 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
255 1.2 thorpej };
256 1.2 thorpej
257 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
258 1.2 thorpej its.it_value = then;
259 1.2 thorpej its.it_value.tv_sec += 1;
260 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL) == 0);
261 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
262 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
263 1.3 hannken ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
264 1.2 thorpej
265 1.2 thorpej timespecsub(&now, &then, &delta);
266 1.3 hannken ATF_REQUIRE(check_value_against_bounds(delta.tv_sec, 1, 1));
267 1.2 thorpej
268 1.2 thorpej (void) close(fd);
269 1.2 thorpej }
270 1.2 thorpej
271 1.2 thorpej /*****************************************************************************/
272 1.2 thorpej
273 1.2 thorpej ATF_TC(timerfd_cancel_on_set_immed);
274 1.2 thorpej ATF_TC_HEAD(timerfd_cancel_on_set_immed, tc)
275 1.2 thorpej {
276 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - immediate");
277 1.2 thorpej atf_tc_set_md_var(tc, "require.user", "root");
278 1.2 thorpej }
279 1.2 thorpej ATF_TC_BODY(timerfd_cancel_on_set_immed, tc)
280 1.2 thorpej {
281 1.2 thorpej struct timespec now;
282 1.2 thorpej uint64_t val;
283 1.2 thorpej int fd;
284 1.2 thorpej
285 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
286 1.2 thorpej
287 1.2 thorpej const struct itimerspec its = {
288 1.2 thorpej .it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
289 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
290 1.2 thorpej };
291 1.2 thorpej
292 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
293 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
294 1.2 thorpej &its, NULL) == 0);
295 1.2 thorpej ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
296 1.2 thorpej ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
297 1.2 thorpej
298 1.2 thorpej (void) close(fd);
299 1.2 thorpej }
300 1.2 thorpej
301 1.2 thorpej /*****************************************************************************/
302 1.2 thorpej
303 1.2 thorpej static void *
304 1.2 thorpej timerfd_cancel_on_set_block_helper(void * const v)
305 1.2 thorpej {
306 1.2 thorpej struct helper_context * const ctx = v;
307 1.2 thorpej struct timespec now;
308 1.2 thorpej
309 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
310 1.2 thorpej
311 1.2 thorpej ATF_REQUIRE(sleep(2) == 0);
312 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
313 1.2 thorpej ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
314 1.2 thorpej
315 1.2 thorpej return NULL;
316 1.2 thorpej }
317 1.2 thorpej
318 1.2 thorpej ATF_TC(timerfd_cancel_on_set_block);
319 1.2 thorpej ATF_TC_HEAD(timerfd_cancel_on_set_block, tc)
320 1.2 thorpej {
321 1.2 thorpej atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - blocking");
322 1.2 thorpej atf_tc_set_md_var(tc, "require.user", "root");
323 1.2 thorpej }
324 1.2 thorpej ATF_TC_BODY(timerfd_cancel_on_set_block, tc)
325 1.2 thorpej {
326 1.2 thorpej struct helper_context ctx;
327 1.2 thorpej pthread_t helper;
328 1.2 thorpej void *join_val;
329 1.2 thorpej uint64_t val;
330 1.2 thorpej int fd;
331 1.2 thorpej
332 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
333 1.2 thorpej
334 1.2 thorpej const struct itimerspec its = {
335 1.2 thorpej .it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
336 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
337 1.2 thorpej };
338 1.2 thorpej
339 1.2 thorpej init_helper_context(&ctx);
340 1.2 thorpej
341 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
342 1.2 thorpej &its, NULL) == 0);
343 1.2 thorpej ATF_REQUIRE(pthread_create(&helper, NULL,
344 1.2 thorpej timerfd_cancel_on_set_block_helper, &ctx) == 0);
345 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
346 1.2 thorpej ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
347 1.2 thorpej
348 1.2 thorpej ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
349 1.2 thorpej
350 1.2 thorpej (void) close(fd);
351 1.2 thorpej }
352 1.2 thorpej
353 1.2 thorpej /*****************************************************************************/
354 1.2 thorpej
355 1.2 thorpej ATF_TC(timerfd_select_poll_kevent_immed);
356 1.2 thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_immed, tc)
357 1.2 thorpej {
358 1.2 thorpej atf_tc_set_md_var(tc, "descr",
359 1.2 thorpej "validates select/poll/kevent behavior - immediate return");
360 1.2 thorpej }
361 1.2 thorpej ATF_TC_BODY(timerfd_select_poll_kevent_immed, tc)
362 1.2 thorpej {
363 1.2 thorpej const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
364 1.2 thorpej struct itimerspec its;
365 1.2 thorpej struct timeval tv;
366 1.2 thorpej struct stat st;
367 1.2 thorpej struct pollfd fds[1];
368 1.2 thorpej uint64_t val;
369 1.2 thorpej fd_set readfds, writefds, exceptfds;
370 1.2 thorpej int fd;
371 1.2 thorpej int kq;
372 1.2 thorpej struct kevent kev[1];
373 1.2 thorpej
374 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
375 1.2 thorpej
376 1.2 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
377 1.2 thorpej EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
378 1.2 thorpej ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
379 1.2 thorpej
380 1.2 thorpej /*
381 1.2 thorpej * fd should be writable but not readable. Pass all of the
382 1.2 thorpej * event bits; we should only get back POLLOUT | POLLWRNORM.
383 1.2 thorpej * (It's writable only in so far as we'll get an error if we try.)
384 1.2 thorpej */
385 1.2 thorpej fds[0].fd = fd;
386 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
387 1.2 thorpej POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
388 1.2 thorpej fds[0].revents = 0;
389 1.2 thorpej ATF_REQUIRE(poll(fds, 1, 0) == 1);
390 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLOUT | POLLWRNORM));
391 1.2 thorpej
392 1.2 thorpej /*
393 1.2 thorpej * As above; fd should only be set in writefds upon return
394 1.2 thorpej * from the select() call.
395 1.2 thorpej */
396 1.2 thorpej FD_ZERO(&readfds);
397 1.2 thorpej FD_ZERO(&writefds);
398 1.2 thorpej FD_ZERO(&exceptfds);
399 1.2 thorpej tv.tv_sec = 0;
400 1.2 thorpej tv.tv_usec = 0;
401 1.2 thorpej FD_SET(fd, &readfds);
402 1.2 thorpej FD_SET(fd, &writefds);
403 1.2 thorpej FD_SET(fd, &exceptfds);
404 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 1);
405 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &readfds));
406 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &writefds));
407 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
408 1.2 thorpej
409 1.2 thorpej /*
410 1.2 thorpej * Now set a one-shot half-second timer, wait for it to expire, and
411 1.2 thorpej * then check again.
412 1.2 thorpej */
413 1.2 thorpej memset(&its, 0, sizeof(its));
414 1.2 thorpej its.it_value.tv_sec = 0;
415 1.2 thorpej its.it_value.tv_nsec = 500000000;
416 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
417 1.2 thorpej ATF_REQUIRE(sleep(2) == 0);
418 1.2 thorpej
419 1.2 thorpej /* Verify it actually fired via the stat() back-channel. */
420 1.2 thorpej ATF_REQUIRE(fstat(fd, &st) == 0);
421 1.2 thorpej ATF_REQUIRE(st.st_size == 1);
422 1.2 thorpej
423 1.2 thorpej fds[0].fd = fd;
424 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
425 1.2 thorpej POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
426 1.2 thorpej fds[0].revents = 0;
427 1.2 thorpej ATF_REQUIRE(poll(fds, 1, 0) == 1);
428 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM |
429 1.2 thorpej POLLOUT | POLLWRNORM));
430 1.2 thorpej
431 1.2 thorpej FD_ZERO(&readfds);
432 1.2 thorpej FD_ZERO(&writefds);
433 1.2 thorpej FD_ZERO(&exceptfds);
434 1.2 thorpej tv.tv_sec = 0;
435 1.2 thorpej tv.tv_usec = 0;
436 1.2 thorpej FD_SET(fd, &readfds);
437 1.2 thorpej FD_SET(fd, &writefds);
438 1.2 thorpej FD_SET(fd, &exceptfds);
439 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 2);
440 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &readfds));
441 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &writefds));
442 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
443 1.2 thorpej
444 1.2 thorpej /*
445 1.2 thorpej * Check that we get an EVFILT_READ event on fd.
446 1.2 thorpej */
447 1.2 thorpej memset(kev, 0, sizeof(kev));
448 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, &ts) == 1);
449 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
450 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_READ);
451 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
452 1.2 thorpej ATF_REQUIRE(kev[0].data == 1);
453 1.2 thorpej
454 1.2 thorpej /*
455 1.2 thorpej * Read the timerfd to ensure we get the correct numnber of
456 1.2 thorpej * expirations.
457 1.2 thorpej */
458 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
459 1.2 thorpej ATF_REQUIRE(val == 1);
460 1.2 thorpej
461 1.2 thorpej /* And ensure that we would block if we tried again. */
462 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN, timerfd_read(fd, &val) == -1);
463 1.2 thorpej
464 1.2 thorpej (void) close(kq);
465 1.2 thorpej (void) close(fd);
466 1.2 thorpej }
467 1.2 thorpej
468 1.2 thorpej /*****************************************************************************/
469 1.2 thorpej
470 1.2 thorpej ATF_TC(timerfd_select_poll_kevent_block);
471 1.2 thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_block, tc)
472 1.2 thorpej {
473 1.2 thorpej atf_tc_set_md_var(tc, "descr",
474 1.2 thorpej "validates select/poll/kevent behavior - blocking");
475 1.2 thorpej }
476 1.2 thorpej ATF_TC_BODY(timerfd_select_poll_kevent_block, tc)
477 1.2 thorpej {
478 1.2 thorpej const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
479 1.2 thorpej struct timespec then, now;
480 1.2 thorpej struct pollfd fds[1];
481 1.2 thorpej fd_set readfds;
482 1.2 thorpej int fd;
483 1.2 thorpej int kq;
484 1.2 thorpej struct kevent kev[1];
485 1.2 thorpej
486 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
487 1.2 thorpej
488 1.2 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
489 1.2 thorpej EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
490 1.2 thorpej ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
491 1.2 thorpej
492 1.2 thorpej /*
493 1.2 thorpej * For each of these tests, we do the following:
494 1.2 thorpej *
495 1.2 thorpej * - Get the current time.
496 1.2 thorpej * - Set a 1-second one-shot timer.
497 1.2 thorpej * - Block in the multiplexing call.
498 1.2 thorpej * - Get the current time and verify that the timer expiration
499 1.2 thorpej * interval has passed.
500 1.2 thorpej */
501 1.2 thorpej
502 1.2 thorpej const struct itimerspec its = {
503 1.2 thorpej .it_value = { .tv_sec = 1, .tv_nsec = 0 },
504 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
505 1.2 thorpej };
506 1.2 thorpej
507 1.2 thorpej /* poll(2) */
508 1.2 thorpej fds[0].fd = fd;
509 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM;
510 1.2 thorpej fds[0].revents = 0;
511 1.2 thorpej
512 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
513 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
514 1.2 thorpej ATF_REQUIRE(poll(fds, 1, INFTIM) == 1);
515 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
516 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
517 1.2 thorpej ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
518 1.2 thorpej
519 1.2 thorpej /* select(2) */
520 1.2 thorpej FD_ZERO(&readfds);
521 1.2 thorpej FD_SET(fd, &readfds);
522 1.2 thorpej
523 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
524 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
525 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, NULL, NULL, NULL) == 1);
526 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
527 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &readfds));
528 1.2 thorpej ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
529 1.2 thorpej
530 1.2 thorpej /* kevent(2) */
531 1.2 thorpej memset(kev, 0, sizeof(kev));
532 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
533 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
534 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
535 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
536 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
537 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_READ);
538 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
539 1.2 thorpej ATF_REQUIRE(kev[0].data == 1);
540 1.2 thorpej
541 1.2 thorpej (void) close(kq);
542 1.2 thorpej (void) close(fd);
543 1.2 thorpej }
544 1.2 thorpej
545 1.2 thorpej /*****************************************************************************/
546 1.2 thorpej
547 1.2 thorpej static void *
548 1.2 thorpej timerfd_restart_helper(void * const v)
549 1.2 thorpej {
550 1.2 thorpej struct helper_context * const ctx = v;
551 1.2 thorpej
552 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
553 1.2 thorpej
554 1.2 thorpej /*
555 1.2 thorpej * Wait 5 seconds (that should give the main thread time to
556 1.2 thorpej * block), and then close the descriptor.
557 1.2 thorpej */
558 1.2 thorpej ATF_REQUIRE(sleep(5) == 0);
559 1.2 thorpej ATF_REQUIRE(close(ctx->fd) == 0);
560 1.2 thorpej
561 1.2 thorpej return NULL;
562 1.2 thorpej }
563 1.2 thorpej
564 1.2 thorpej ATF_TC(timerfd_restart);
565 1.2 thorpej ATF_TC_HEAD(timerfd_restart, tc)
566 1.2 thorpej {
567 1.2 thorpej atf_tc_set_md_var(tc, "descr",
568 1.2 thorpej "exercises the 'restart' fileop code path");
569 1.2 thorpej }
570 1.2 thorpej ATF_TC_BODY(timerfd_restart, tc)
571 1.2 thorpej {
572 1.2 thorpej struct timespec then, now, delta;
573 1.2 thorpej struct helper_context ctx;
574 1.2 thorpej uint64_t val;
575 1.2 thorpej pthread_t helper;
576 1.2 thorpej void *join_val;
577 1.2 thorpej
578 1.2 thorpej init_helper_context(&ctx);
579 1.2 thorpej
580 1.2 thorpej ATF_REQUIRE((ctx.fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
581 1.2 thorpej
582 1.2 thorpej const struct itimerspec its = {
583 1.2 thorpej .it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
584 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
585 1.2 thorpej };
586 1.2 thorpej ATF_REQUIRE(timerfd_settime(ctx.fd, 0, &its, NULL) == 0);
587 1.2 thorpej
588 1.2 thorpej
589 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
590 1.2 thorpej ATF_REQUIRE(pthread_create(&helper, NULL,
591 1.2 thorpej timerfd_restart_helper, &ctx) == 0);
592 1.2 thorpej
593 1.2 thorpej /*
594 1.2 thorpej * Wait for the helper to be ready, and then immediately block
595 1.2 thorpej * in read(). The helper will close the file, and we should get
596 1.2 thorpej * EBADF after a few seconds.
597 1.2 thorpej */
598 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
599 1.2 thorpej ATF_REQUIRE_ERRNO(EBADF, timerfd_read(ctx.fd, &val) == -1);
600 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
601 1.2 thorpej
602 1.2 thorpej timespecsub(&now, &then, &delta);
603 1.2 thorpej ATF_REQUIRE(delta.tv_sec >= 5);
604 1.2 thorpej
605 1.2 thorpej /* Reap the helper. */
606 1.2 thorpej ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
607 1.2 thorpej }
608 1.2 thorpej
609 1.2 thorpej /*****************************************************************************/
610 1.2 thorpej
611 1.2 thorpej ATF_TP_ADD_TCS(tp)
612 1.2 thorpej {
613 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_create);
614 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_bogusfd);
615 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_block);
616 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_repeating);
617 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_abstime);
618 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_block);
619 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_immed);
620 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_immed);
621 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_block);
622 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_restart);
623 1.2 thorpej
624 1.2 thorpej return atf_no_error();
625 1.2 thorpej }
626