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