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