18269e767SBrooks Davis.\" SPDX-License-Identifier: BSD-2-Clause 28269e767SBrooks Davis.\" 38269e767SBrooks Davis.\" Copyright (c) 2023 Jake Freeland <jfree@FreeBSD.org> 48269e767SBrooks Davis.\" 58269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 68269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 78269e767SBrooks Davis.\" are met: 88269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 98269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 108269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 118269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 128269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 138269e767SBrooks Davis.\" 148269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 158269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 168269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 178269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 188269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 198269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 208269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 218269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 228269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 238269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 248269e767SBrooks Davis.\" SUCH DAMAGE. 258269e767SBrooks Davis.\" 26*172328c4SCormac Stephenson.Dd June 28, 2024 278269e767SBrooks Davis.Dt TIMERFD 2 288269e767SBrooks Davis.Os 298269e767SBrooks Davis.Sh NAME 308269e767SBrooks Davis.Nm timerfd , 318269e767SBrooks Davis.Nm timerfd_create , 328269e767SBrooks Davis.Nm timerfd_gettime , 338269e767SBrooks Davis.Nm timerfd_settime 348269e767SBrooks Davis.Nd timers with file descriptor semantics 358269e767SBrooks Davis.Sh LIBRARY 368269e767SBrooks Davis.Lb libc 378269e767SBrooks Davis.Sh SYNOPSIS 388269e767SBrooks Davis.In sys/timerfd.h 398269e767SBrooks Davis.Ft int 408269e767SBrooks Davis.Fo timerfd_create 418269e767SBrooks Davis.Fa "int clockid" 428269e767SBrooks Davis.Fa "int flags" 438269e767SBrooks Davis.Fc 448269e767SBrooks Davis.Ft int 458269e767SBrooks Davis.Fo timerfd_gettime 468269e767SBrooks Davis.Fa "int fd" 478269e767SBrooks Davis.Fa "struct itimerspec *curr_value" 488269e767SBrooks Davis.Fc 498269e767SBrooks Davis.Ft int 508269e767SBrooks Davis.Fo timerfd_settime 518269e767SBrooks Davis.Fa "int fd" 528269e767SBrooks Davis.Fa "int flags" 538269e767SBrooks Davis.Fa "const struct itimerspec *new_value" 548269e767SBrooks Davis.Fa "struct itimerspec *old_value" 558269e767SBrooks Davis.Fc 568269e767SBrooks Davis.Sh DESCRIPTION 578269e767SBrooks DavisThe 588269e767SBrooks Davis.Nm 598269e767SBrooks Davissystem calls operate on timers, identified by special 608269e767SBrooks Davis.Nm 618269e767SBrooks Davisfile descriptors. 628269e767SBrooks DavisThese calls are analogous to 638269e767SBrooks Davis.Fn timer_create , 648269e767SBrooks Davis.Fn timer_gettime , 658269e767SBrooks Davisand 668269e767SBrooks Davis.Fn timer_settime 678269e767SBrooks Davisper-process timer functions, but use a 688269e767SBrooks Davis.Nm 698269e767SBrooks Davisdescriptor in place of 708269e767SBrooks Davis.Fa timerid . 718269e767SBrooks Davis.Pp 728269e767SBrooks DavisAll 738269e767SBrooks Davis.Nm 748269e767SBrooks Davisdescriptors possess traditional file descriptor semantics; they may be passed 758269e767SBrooks Davisto other processes, preserved across 768269e767SBrooks Davis.Xr fork 2 , 778269e767SBrooks Davisand monitored via 788269e767SBrooks Davis.Xr kevent 2 , 798269e767SBrooks Davis.Xr poll 2 , 808269e767SBrooks Davisor 818269e767SBrooks Davis.Xr select 2 . 828269e767SBrooks DavisWhen a 838269e767SBrooks Davis.Nm 848269e767SBrooks Davisdescriptor is no longer needed, it may be disposed of using 858269e767SBrooks Davis.Xr close 2 . 868269e767SBrooks Davis.Bl -tag -width "Fn timerfd_settime" 878269e767SBrooks Davis.It Fn timerfd_create 888269e767SBrooks DavisInitialize a 898269e767SBrooks Davis.Nm 908269e767SBrooks Davisobject and return its file descriptor. 918269e767SBrooks DavisThe 928269e767SBrooks Davis.Fa clockid 938269e767SBrooks Davisargument specifies the clock used as a timing base and may be: 948269e767SBrooks Davis.Pp 958269e767SBrooks Davis.Bl -tag -width "Dv CLOCK_MONOTONIC" -compact 968269e767SBrooks Davis.It Dv CLOCK_REALTIME 978269e767SBrooks DavisIncrements as a wall clock should. 98*172328c4SCormac Stephenson.It Dv CLOCK_BOOTTIME 998269e767SBrooks Davis.It Dv CLOCK_MONOTONIC 1008269e767SBrooks DavisIncrements monotonically in SI seconds. 101*172328c4SCormac Stephenson.It Dv CLOCK_UPTIME 102*172328c4SCormac StephensonIncrements monotonically in SI seconds, but is paused while the system is 103*172328c4SCormac Stephensonsuspended. 1048269e767SBrooks Davis.El 105*172328c4SCormac StephensonSee 106*172328c4SCormac Stephenson.Xr clock_gettime 2 107*172328c4SCormac Stephensonfor more precise definitions. 1088269e767SBrooks Davis.Pp 1098269e767SBrooks DavisThe 1108269e767SBrooks Davis.Fa flags 1118269e767SBrooks Davisargument may contain the result of 1128269e767SBrooks Davis.Em or Ns 'ing 1138269e767SBrooks Davisthe following values: 1148269e767SBrooks Davis.Pp 1158269e767SBrooks Davis.Bl -tag -width "Dv TFD_NONBLOCK" -compact 1168269e767SBrooks Davis.It Dv TFD_CLOEXEC 1178269e767SBrooks DavisThe newly generated file descriptor will close-on-exec. 1188269e767SBrooks Davis.It Dv TFD_NONBLOCK 1198269e767SBrooks DavisDo not block on read/write operations. 1208269e767SBrooks Davis.El 1218269e767SBrooks Davis.It Fn timerfd_gettime 1228269e767SBrooks DavisRetrieve the current state of the timer denoted by 1238269e767SBrooks Davis.Fa fd . 1248269e767SBrooks DavisThe result is stored in 1258269e767SBrooks Davis.Fa curr_value 1268269e767SBrooks Davisas a 1278269e767SBrooks Davis.Dv struct itimerspec . 1288269e767SBrooks DavisThe 1298269e767SBrooks Davis.Fa it_value 1308269e767SBrooks Davisand 1318269e767SBrooks Davis.Fa it_interval 1328269e767SBrooks Davismembers of 1338269e767SBrooks Davis.Fa curr_value 1348269e767SBrooks Davisrepresent the relative time until the next expiration and the interval 1358269e767SBrooks Davisreload value last set by 1368269e767SBrooks Davis.Fn timerfd_settime , 1378269e767SBrooks Davisrespectively. 1388269e767SBrooks Davis.It Fn timerfd_settime 1398269e767SBrooks DavisUpdate the timer denoted by 1408269e767SBrooks Davis.Fa fd 1418269e767SBrooks Daviswith the 1428269e767SBrooks Davis.Dv struct itimerspec 1438269e767SBrooks Davisin 1448269e767SBrooks Davis.Fa new_value . 1458269e767SBrooks DavisThe 1468269e767SBrooks Davis.Fa it_value 1478269e767SBrooks Davismember of 1488269e767SBrooks Davis.Fa new_value 1498269e767SBrooks Davisshould contain the amount of time before the timer expires, or zero if the 1508269e767SBrooks Davistimer should be disarmed. 1518269e767SBrooks DavisThe 1528269e767SBrooks Davis.Fa it_interval 1538269e767SBrooks Davismember should contain the reload time if an interval timer is desired. 1548269e767SBrooks Davis.Pp 1558269e767SBrooks DavisThe previous timer state will be stored in 1568269e767SBrooks Davis.Fa old_value 1578269e767SBrooks Davisgiven 1588269e767SBrooks Davis.Fa old_value 1598269e767SBrooks Davisis not 1608269e767SBrooks Davis.Dv NULL . 1618269e767SBrooks Davis.Pp 1628269e767SBrooks DavisThe 1638269e767SBrooks Davis.Fa flags 1648269e767SBrooks Davisargument may contain the result of 1658269e767SBrooks Davis.Em or Ns 'ing 1668269e767SBrooks Davisthe following values: 1678269e767SBrooks Davis.Pp 1688269e767SBrooks Davis.Bl -tag -width TFD_TIMER_CANCEL_ON_SET -compact 1698269e767SBrooks Davis.It Dv TFD_TIMER_ABSTIME 1708269e767SBrooks DavisExpiration will occur at the absolute time provided in 1718269e767SBrooks Davis.Fa new_value . 1728269e767SBrooks DavisNormally, 1738269e767SBrooks Davis.Fa new_value 1748269e767SBrooks Davisrepresents a relative time compared to the timer's 1758269e767SBrooks Davis.Fa clockid 1768269e767SBrooks Davisclock. 1778269e767SBrooks Davis.It Dv TFD_TIMER_CANCEL_ON_SET 1788269e767SBrooks DavisIf 1798269e767SBrooks Davis.Fa clockid 1808269e767SBrooks Davishas been set to 1818269e767SBrooks Davis.Dv CLOCK_REALTIME 1828269e767SBrooks Davisand the realtime clock has experienced a discontinuous jump, 1838269e767SBrooks Davisthen the timer will be canceled and the next 1848269e767SBrooks Davis.Xr read 2 1858269e767SBrooks Daviswill fail with 1868269e767SBrooks Davis.Dv ECANCELED . 1878269e767SBrooks Davis.El 1888269e767SBrooks Davis.El 1898269e767SBrooks Davis.Pp 1908269e767SBrooks DavisFile operations have the following semantics: 1918269e767SBrooks Davis.Bl -tag -width ioctl 1928269e767SBrooks Davis.It Xr read 2 1938269e767SBrooks DavisTransfer the number of timer expirations that have occurred since the last 1948269e767SBrooks Davissuccessful 1958269e767SBrooks Davis.Xr read 2 1968269e767SBrooks Davisor 1978269e767SBrooks Davis.Fn timerfd_settime 1988269e767SBrooks Davisinto the output buffer of size 1998269e767SBrooks Davis.Vt uint64_t . 2008269e767SBrooks DavisIf the expiration counter is zero, 2018269e767SBrooks Davis.Xr read 2 2028269e767SBrooks Davisblocks until a timer expiration occurs unless 2038269e767SBrooks Davis.Dv TFD_NONBLOCK 2048269e767SBrooks Davisis set, where 2058269e767SBrooks Davis.Dv EAGAIN 2068269e767SBrooks Davisis returned. 2078269e767SBrooks Davis.It Xr poll 2 2088269e767SBrooks DavisThe file descriptor is readable when its timer expiration counter is greater 2098269e767SBrooks Davisthan zero. 2108269e767SBrooks Davis.It Xr ioctl 2 2118269e767SBrooks Davis.Bl -tag -width FIONREAD 2128269e767SBrooks Davis.It Dv FIOASYNC int 2138269e767SBrooks DavisA non-zero input will set the FASYNC flag. 2148269e767SBrooks DavisA zero input will clear the FASYNC flag. 2158269e767SBrooks Davis.It Dv FIONBIO int 2168269e767SBrooks DavisA non-zero input will set the FNONBLOCK flag. 2178269e767SBrooks DavisA zero input will clear the FNONBLOCK flag. 2188269e767SBrooks Davis.El 2198269e767SBrooks Davis.El 2208269e767SBrooks Davis.Sh RETURN VALUES 2218269e767SBrooks DavisThe 2228269e767SBrooks Davis.Fn timerfd_create 2238269e767SBrooks Davissystem call creates a 2248269e767SBrooks Davis.Nm 2258269e767SBrooks Davisobject and returns its file descriptor. 2268269e767SBrooks DavisIf an error occurs, -1 is returned and the global variable 2278269e767SBrooks Davis.Fa errno 2288269e767SBrooks Davisis set to indicate the error. 2298269e767SBrooks Davis.Pp 2308269e767SBrooks DavisThe 2318269e767SBrooks Davis.Fn timerfd_gettime 2328269e767SBrooks Davisand 2338269e767SBrooks Davis.Fn timerfd_settime 2348269e767SBrooks Davissystem calls return 0 on success. 2358269e767SBrooks DavisIf an error occurs, -1 is returned and the global variable 2368269e767SBrooks Davis.Fa errno 2378269e767SBrooks Davisis set to indicate the error. 2388269e767SBrooks Davis.Sh ERRORS 2398269e767SBrooks DavisThe 2408269e767SBrooks Davis.Fn timerfd_create 2418269e767SBrooks Davissystem call fails if: 2428269e767SBrooks Davis.Bl -tag -width Er 2438269e767SBrooks Davis.It Bq Er EINVAL 2448269e767SBrooks DavisThe specified 2458269e767SBrooks Davis.Fa clockid 2468269e767SBrooks Davisis not supported. 2478269e767SBrooks Davis.It Bq Er EINVAL 2488269e767SBrooks DavisThe provided 2498269e767SBrooks Davis.Fa flags 2508269e767SBrooks Davisare invalid. 2518269e767SBrooks Davis.It Bq Er EMFILE 2528269e767SBrooks DavisThe per-process descriptor table is full. 2538269e767SBrooks Davis.It Bq Er ENFILE 2548269e767SBrooks DavisThe system file table is full. 2558269e767SBrooks Davis.It Bq Er ENOMEM 2568269e767SBrooks DavisThe kernel failed to allocate enough memory for the timer. 2578269e767SBrooks Davis.El 2588269e767SBrooks Davis.Pp 2598269e767SBrooks DavisBoth 2608269e767SBrooks Davis.Fn timerfd_gettime 2618269e767SBrooks Davisand 2628269e767SBrooks Davis.Fn timerfd_settime 2638269e767SBrooks Davissystem calls fail if: 2648269e767SBrooks Davis.Bl -tag -width Er 2658269e767SBrooks Davis.It Bq Er EBADF 2668269e767SBrooks DavisThe provided 2678269e767SBrooks Davis.Fa fd 2688269e767SBrooks Davisis invalid. 2698269e767SBrooks Davis.It Bq Er EFAULT 2708269e767SBrooks DavisThe addresses provided by 2718269e767SBrooks Davis.Fa curr_value , 2728269e767SBrooks Davis.Fa new_value , 2738269e767SBrooks Davisor 2748269e767SBrooks Davis.Fa old_value 2758269e767SBrooks Davisare invalid. 2768269e767SBrooks Davis.It Bq Er EINVAL 2778269e767SBrooks DavisThe provided 2788269e767SBrooks Davis.Fa fd 2798269e767SBrooks Davisis valid, but was not generated by 2808269e767SBrooks Davis.Fn timerfd_create . 2818269e767SBrooks Davis.El 2828269e767SBrooks Davis.Pp 2838269e767SBrooks DavisThe following errors only apply to 2848269e767SBrooks Davis.Fn timerfd_settime : 2858269e767SBrooks Davis.Bl -tag -width Er 2868269e767SBrooks Davis.It Bq Er EINVAL 2878269e767SBrooks DavisThe provided 2888269e767SBrooks Davis.Fa flags 2898269e767SBrooks Davisare invalid. 2908269e767SBrooks Davis.It Bq Er EINVAL 2918269e767SBrooks DavisA nanosecond field in the 2928269e767SBrooks Davis.Fa new_value 2938269e767SBrooks Davisargument specified a value less than zero, or greater than or equal to 10^9. 2948269e767SBrooks Davis.It Bq Er ECANCELED 2958269e767SBrooks DavisThe timer was created with the clock ID 2968269e767SBrooks Davis.Dv CLOCK_REALTIME , 2978269e767SBrooks Daviswas configured with the 2988269e767SBrooks Davis.Dv TFD_TIMER_CANCEL_ON_SET 2998269e767SBrooks Davisflag, and the system realtime clock experienced a discontinuous change without 3008269e767SBrooks Davisbeing read. 3018269e767SBrooks Davis.El 3028269e767SBrooks Davis.Pp 3038269e767SBrooks DavisA read from a 3048269e767SBrooks Davis.Nm 3058269e767SBrooks Davisobject fails if: 3068269e767SBrooks Davis.Bl -tag -width Er 3078269e767SBrooks Davis.It Bq Er EAGAIN 3088269e767SBrooks DavisThe timer's expiration counter is zero and the 3098269e767SBrooks Davis.Nm 310bbef63ecSGordon Berglingobject is set for non-blocking I/O. 3118269e767SBrooks Davis.It Bq Er ECANCELED 3128269e767SBrooks DavisThe timer was created with the clock ID 3138269e767SBrooks Davis.Dv CLOCK_REALTIME , 3148269e767SBrooks Daviswas configured with the 3158269e767SBrooks Davis.Dv TFD_TIMER_CANCEL_ON_SET 3168269e767SBrooks Davisflag, and the system realtime clock experienced a discontinuous change. 3178269e767SBrooks Davis.It Bq Er EINVAL 3188269e767SBrooks DavisThe size of the read buffer is not large enough to hold the 3198269e767SBrooks Davis.Vt uint64_t 3208269e767SBrooks Davissized timer expiration counter. 3218269e767SBrooks Davis.El 3228269e767SBrooks Davis.Sh SEE ALSO 3238269e767SBrooks Davis.Xr eventfd 2 , 3248269e767SBrooks Davis.Xr kqueue 2 , 3258269e767SBrooks Davis.Xr poll 2 , 3268269e767SBrooks Davis.Xr read 2 , 3278269e767SBrooks Davis.Xr timer_create 2 , 3288269e767SBrooks Davis.Xr timer_gettime 2 , 3298269e767SBrooks Davis.Xr timer_settime 2 3308269e767SBrooks Davis.Sh STANDARDS 3318269e767SBrooks DavisThe 3328269e767SBrooks Davis.Nm 3338269e767SBrooks Davissystem calls originated from Linux and are non-standard. 3348269e767SBrooks Davis.Sh HISTORY 3358269e767SBrooks Davis.An -nosplit 3368269e767SBrooks DavisThe 3378269e767SBrooks Davis.Nm 3388269e767SBrooks Davisfacility was originally ported to 3398269e767SBrooks Davis.Fx Ns 's 3408269e767SBrooks DavisLinux compatibility layer by 3418269e767SBrooks Davis.An Dmitry Chagin Aq Mt dchagin@FreeBSD.org 3428269e767SBrooks Davisin 3438269e767SBrooks Davis.Fx 12.0 . 3448269e767SBrooks DavisIt was revised and adapted to be native by 3458269e767SBrooks Davis.An Jake Freeland Aq Mt jfree@FreeBSD.org 3468269e767SBrooks Davisin 3478269e767SBrooks Davis.Fx 14.0 . 348