t_timerfd.c revision 1.5 1 1.5 riastrad /* $NetBSD: t_timerfd.c,v 1.5 2023/07/08 15:32:58 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.5 riastrad __RCSID("$NetBSD: t_timerfd.c,v 1.5 2023/07/08 15:32:58 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.2 thorpej * fd should be writable but not readable. Pass all of the
395 1.2 thorpej * event bits; we should only get back POLLOUT | POLLWRNORM.
396 1.2 thorpej * (It's writable only in so far as we'll get an error if we try.)
397 1.2 thorpej */
398 1.2 thorpej fds[0].fd = fd;
399 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
400 1.2 thorpej POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
401 1.2 thorpej fds[0].revents = 0;
402 1.2 thorpej ATF_REQUIRE(poll(fds, 1, 0) == 1);
403 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLOUT | POLLWRNORM));
404 1.2 thorpej
405 1.2 thorpej /*
406 1.2 thorpej * As above; fd should only be set in writefds upon return
407 1.2 thorpej * from the select() call.
408 1.2 thorpej */
409 1.2 thorpej FD_ZERO(&readfds);
410 1.2 thorpej FD_ZERO(&writefds);
411 1.2 thorpej FD_ZERO(&exceptfds);
412 1.2 thorpej tv.tv_sec = 0;
413 1.2 thorpej tv.tv_usec = 0;
414 1.2 thorpej FD_SET(fd, &readfds);
415 1.2 thorpej FD_SET(fd, &writefds);
416 1.2 thorpej FD_SET(fd, &exceptfds);
417 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 1);
418 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &readfds));
419 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &writefds));
420 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
421 1.2 thorpej
422 1.2 thorpej /*
423 1.2 thorpej * Now set a one-shot half-second timer, wait for it to expire, and
424 1.2 thorpej * then check again.
425 1.2 thorpej */
426 1.2 thorpej memset(&its, 0, sizeof(its));
427 1.2 thorpej its.it_value.tv_sec = 0;
428 1.2 thorpej its.it_value.tv_nsec = 500000000;
429 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
430 1.2 thorpej ATF_REQUIRE(sleep(2) == 0);
431 1.2 thorpej
432 1.2 thorpej /* Verify it actually fired via the stat() back-channel. */
433 1.2 thorpej ATF_REQUIRE(fstat(fd, &st) == 0);
434 1.2 thorpej ATF_REQUIRE(st.st_size == 1);
435 1.2 thorpej
436 1.2 thorpej fds[0].fd = fd;
437 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
438 1.2 thorpej POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
439 1.2 thorpej fds[0].revents = 0;
440 1.2 thorpej ATF_REQUIRE(poll(fds, 1, 0) == 1);
441 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM |
442 1.2 thorpej POLLOUT | POLLWRNORM));
443 1.2 thorpej
444 1.2 thorpej FD_ZERO(&readfds);
445 1.2 thorpej FD_ZERO(&writefds);
446 1.2 thorpej FD_ZERO(&exceptfds);
447 1.2 thorpej tv.tv_sec = 0;
448 1.2 thorpej tv.tv_usec = 0;
449 1.2 thorpej FD_SET(fd, &readfds);
450 1.2 thorpej FD_SET(fd, &writefds);
451 1.2 thorpej FD_SET(fd, &exceptfds);
452 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 2);
453 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &readfds));
454 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &writefds));
455 1.2 thorpej ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
456 1.2 thorpej
457 1.2 thorpej /*
458 1.2 thorpej * Check that we get an EVFILT_READ event on fd.
459 1.2 thorpej */
460 1.2 thorpej memset(kev, 0, sizeof(kev));
461 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, &ts) == 1);
462 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
463 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_READ);
464 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
465 1.2 thorpej ATF_REQUIRE(kev[0].data == 1);
466 1.2 thorpej
467 1.2 thorpej /*
468 1.2 thorpej * Read the timerfd to ensure we get the correct numnber of
469 1.2 thorpej * expirations.
470 1.2 thorpej */
471 1.2 thorpej ATF_REQUIRE(timerfd_read(fd, &val) == 0);
472 1.2 thorpej ATF_REQUIRE(val == 1);
473 1.2 thorpej
474 1.2 thorpej /* And ensure that we would block if we tried again. */
475 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN, timerfd_read(fd, &val) == -1);
476 1.2 thorpej
477 1.2 thorpej (void) close(kq);
478 1.2 thorpej (void) close(fd);
479 1.2 thorpej }
480 1.2 thorpej
481 1.2 thorpej /*****************************************************************************/
482 1.2 thorpej
483 1.2 thorpej ATF_TC(timerfd_select_poll_kevent_block);
484 1.2 thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_block, tc)
485 1.2 thorpej {
486 1.2 thorpej atf_tc_set_md_var(tc, "descr",
487 1.2 thorpej "validates select/poll/kevent behavior - blocking");
488 1.2 thorpej }
489 1.2 thorpej ATF_TC_BODY(timerfd_select_poll_kevent_block, tc)
490 1.2 thorpej {
491 1.2 thorpej const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
492 1.2 thorpej struct timespec then, now;
493 1.2 thorpej struct pollfd fds[1];
494 1.2 thorpej fd_set readfds;
495 1.2 thorpej int fd;
496 1.2 thorpej int kq;
497 1.2 thorpej struct kevent kev[1];
498 1.2 thorpej
499 1.2 thorpej ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
500 1.2 thorpej
501 1.2 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
502 1.2 thorpej EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
503 1.2 thorpej ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
504 1.2 thorpej
505 1.2 thorpej /*
506 1.2 thorpej * For each of these tests, we do the following:
507 1.2 thorpej *
508 1.2 thorpej * - Get the current time.
509 1.2 thorpej * - Set a 1-second one-shot timer.
510 1.2 thorpej * - Block in the multiplexing call.
511 1.2 thorpej * - Get the current time and verify that the timer expiration
512 1.2 thorpej * interval has passed.
513 1.2 thorpej */
514 1.2 thorpej
515 1.2 thorpej const struct itimerspec its = {
516 1.2 thorpej .it_value = { .tv_sec = 1, .tv_nsec = 0 },
517 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
518 1.2 thorpej };
519 1.2 thorpej
520 1.2 thorpej /* poll(2) */
521 1.2 thorpej fds[0].fd = fd;
522 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM;
523 1.2 thorpej fds[0].revents = 0;
524 1.2 thorpej
525 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
526 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
527 1.2 thorpej ATF_REQUIRE(poll(fds, 1, INFTIM) == 1);
528 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
529 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
530 1.2 thorpej ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
531 1.2 thorpej
532 1.2 thorpej /* select(2) */
533 1.2 thorpej FD_ZERO(&readfds);
534 1.2 thorpej FD_SET(fd, &readfds);
535 1.2 thorpej
536 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
537 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
538 1.2 thorpej ATF_REQUIRE(select(fd + 1, &readfds, NULL, NULL, NULL) == 1);
539 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
540 1.2 thorpej ATF_REQUIRE(FD_ISSET(fd, &readfds));
541 1.2 thorpej ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
542 1.2 thorpej
543 1.2 thorpej /* kevent(2) */
544 1.2 thorpej memset(kev, 0, sizeof(kev));
545 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
546 1.2 thorpej ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
547 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
548 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
549 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
550 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_READ);
551 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
552 1.2 thorpej ATF_REQUIRE(kev[0].data == 1);
553 1.2 thorpej
554 1.2 thorpej (void) close(kq);
555 1.2 thorpej (void) close(fd);
556 1.2 thorpej }
557 1.2 thorpej
558 1.2 thorpej /*****************************************************************************/
559 1.2 thorpej
560 1.2 thorpej static void *
561 1.2 thorpej timerfd_restart_helper(void * const v)
562 1.2 thorpej {
563 1.2 thorpej struct helper_context * const ctx = v;
564 1.2 thorpej
565 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
566 1.2 thorpej
567 1.2 thorpej /*
568 1.2 thorpej * Wait 5 seconds (that should give the main thread time to
569 1.2 thorpej * block), and then close the descriptor.
570 1.2 thorpej */
571 1.2 thorpej ATF_REQUIRE(sleep(5) == 0);
572 1.2 thorpej ATF_REQUIRE(close(ctx->fd) == 0);
573 1.2 thorpej
574 1.2 thorpej return NULL;
575 1.2 thorpej }
576 1.2 thorpej
577 1.2 thorpej ATF_TC(timerfd_restart);
578 1.2 thorpej ATF_TC_HEAD(timerfd_restart, tc)
579 1.2 thorpej {
580 1.2 thorpej atf_tc_set_md_var(tc, "descr",
581 1.2 thorpej "exercises the 'restart' fileop code path");
582 1.2 thorpej }
583 1.2 thorpej ATF_TC_BODY(timerfd_restart, tc)
584 1.2 thorpej {
585 1.2 thorpej struct timespec then, now, delta;
586 1.2 thorpej struct helper_context ctx;
587 1.2 thorpej uint64_t val;
588 1.2 thorpej pthread_t helper;
589 1.2 thorpej void *join_val;
590 1.2 thorpej
591 1.2 thorpej init_helper_context(&ctx);
592 1.2 thorpej
593 1.2 thorpej ATF_REQUIRE((ctx.fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
594 1.2 thorpej
595 1.2 thorpej const struct itimerspec its = {
596 1.2 thorpej .it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
597 1.2 thorpej .it_interval = { .tv_sec = 0, .tv_nsec = 0 },
598 1.2 thorpej };
599 1.2 thorpej ATF_REQUIRE(timerfd_settime(ctx.fd, 0, &its, NULL) == 0);
600 1.2 thorpej
601 1.2 thorpej
602 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
603 1.2 thorpej ATF_REQUIRE(pthread_create(&helper, NULL,
604 1.2 thorpej timerfd_restart_helper, &ctx) == 0);
605 1.2 thorpej
606 1.2 thorpej /*
607 1.2 thorpej * Wait for the helper to be ready, and then immediately block
608 1.2 thorpej * in read(). The helper will close the file, and we should get
609 1.2 thorpej * EBADF after a few seconds.
610 1.2 thorpej */
611 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
612 1.2 thorpej ATF_REQUIRE_ERRNO(EBADF, timerfd_read(ctx.fd, &val) == -1);
613 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
614 1.2 thorpej
615 1.2 thorpej timespecsub(&now, &then, &delta);
616 1.2 thorpej ATF_REQUIRE(delta.tv_sec >= 5);
617 1.2 thorpej
618 1.2 thorpej /* Reap the helper. */
619 1.2 thorpej ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
620 1.2 thorpej }
621 1.2 thorpej
622 1.2 thorpej /*****************************************************************************/
623 1.2 thorpej
624 1.4 thorpej ATF_TC(timerfd_fcntl);
625 1.4 thorpej ATF_TC_HEAD(timerfd_fcntl, tc)
626 1.4 thorpej {
627 1.4 thorpej atf_tc_set_md_var(tc, "descr",
628 1.4 thorpej "validates fcntl behavior");
629 1.4 thorpej }
630 1.4 thorpej
631 1.4 thorpej ATF_TC_BODY(timerfd_fcntl, tc)
632 1.4 thorpej {
633 1.4 thorpej int tfd;
634 1.4 thorpej int val;
635 1.4 thorpej
636 1.4 thorpej ATF_REQUIRE((tfd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
637 1.4 thorpej ATF_REQUIRE((fcntl(tfd, F_GETFL) & O_NONBLOCK) == 0);
638 1.4 thorpej ATF_REQUIRE(fcntl(tfd, F_SETFL, O_NONBLOCK) == 0);
639 1.4 thorpej ATF_REQUIRE((fcntl(tfd, F_GETFL) & O_NONBLOCK) != 0);
640 1.4 thorpej ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) == 0);
641 1.4 thorpej
642 1.4 thorpej /* If the timer hasn't fired, there is no readable data. */
643 1.4 thorpej ATF_REQUIRE(ioctl(tfd, FIONREAD, &val) == 0);
644 1.4 thorpej ATF_REQUIRE(val == 0);
645 1.4 thorpej
646 1.4 thorpej ATF_REQUIRE_ERRNO(ENOTTY, ioctl(tfd, FIONWRITE, &val) == -1);
647 1.4 thorpej ATF_REQUIRE_ERRNO(ENOTTY, ioctl(tfd, FIONSPACE, &val) == -1);
648 1.4 thorpej (void)close(tfd);
649 1.4 thorpej
650 1.4 thorpej ATF_REQUIRE((tfd = timerfd_create(CLOCK_MONOTONIC,
651 1.4 thorpej TFD_NONBLOCK | TFD_CLOEXEC)) >= 0);
652 1.4 thorpej ATF_REQUIRE((fcntl(tfd, F_GETFL) & ~O_ACCMODE) == O_NONBLOCK);
653 1.4 thorpej ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) != 0);
654 1.4 thorpej ATF_REQUIRE(fcntl(tfd, F_SETFD, 0) == 0);
655 1.4 thorpej ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) == 0);
656 1.4 thorpej ATF_REQUIRE(fcntl(tfd, F_SETFD, FD_CLOEXEC) == 0);
657 1.4 thorpej ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) != 0);
658 1.4 thorpej (void)close(tfd);
659 1.4 thorpej }
660 1.4 thorpej
661 1.4 thorpej /*****************************************************************************/
662 1.4 thorpej
663 1.2 thorpej ATF_TP_ADD_TCS(tp)
664 1.2 thorpej {
665 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_create);
666 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_bogusfd);
667 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_block);
668 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_repeating);
669 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_abstime);
670 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_block);
671 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_immed);
672 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_immed);
673 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_block);
674 1.2 thorpej ATF_TP_ADD_TC(tp, timerfd_restart);
675 1.4 thorpej ATF_TP_ADD_TC(tp, timerfd_fcntl);
676 1.2 thorpej
677 1.2 thorpej return atf_no_error();
678 1.2 thorpej }
679