xref: /freebsd/lib/libsys/timerfd.2 (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
1.\" SPDX-License-Identifier: BSD-2-Clause
2.\"
3.\" Copyright (c) 2023 Jake Freeland <jfree@FreeBSD.org>
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.Dd June 28, 2024
27.Dt TIMERFD 2
28.Os
29.Sh NAME
30.Nm timerfd ,
31.Nm timerfd_create ,
32.Nm timerfd_gettime ,
33.Nm timerfd_settime
34.Nd timers with file descriptor semantics
35.Sh LIBRARY
36.Lb libc
37.Sh SYNOPSIS
38.In sys/timerfd.h
39.Ft int
40.Fo timerfd_create
41.Fa "int clockid"
42.Fa "int flags"
43.Fc
44.Ft int
45.Fo timerfd_gettime
46.Fa "int fd"
47.Fa "struct itimerspec *curr_value"
48.Fc
49.Ft int
50.Fo timerfd_settime
51.Fa "int fd"
52.Fa "int flags"
53.Fa "const struct itimerspec *new_value"
54.Fa "struct itimerspec *old_value"
55.Fc
56.Sh DESCRIPTION
57The
58.Nm
59system calls operate on timers, identified by special
60.Nm
61file descriptors.
62These calls are analogous to
63.Fn timer_create ,
64.Fn timer_gettime ,
65and
66.Fn timer_settime
67per-process timer functions, but use a
68.Nm
69descriptor in place of
70.Fa timerid .
71.Pp
72All
73.Nm
74descriptors possess traditional file descriptor semantics; they may be passed
75to other processes, preserved across
76.Xr fork 2 ,
77and monitored via
78.Xr kevent 2 ,
79.Xr poll 2 ,
80or
81.Xr select 2 .
82When a
83.Nm
84descriptor is no longer needed, it may be disposed of using
85.Xr close 2 .
86.Bl -tag -width "Fn timerfd_settime"
87.It Fn timerfd_create
88Initialize a
89.Nm
90object and return its file descriptor.
91The
92.Fa clockid
93argument specifies the clock used as a timing base and may be:
94.Pp
95.Bl -tag -width "Dv CLOCK_MONOTONIC" -compact
96.It Dv CLOCK_REALTIME
97Increments as a wall clock should.
98.It Dv CLOCK_BOOTTIME
99.It Dv CLOCK_MONOTONIC
100Increments monotonically in SI seconds.
101.It Dv CLOCK_UPTIME
102Increments monotonically in SI seconds, but is paused while the system is
103suspended.
104.El
105See
106.Xr clock_gettime 2
107for more precise definitions.
108.Pp
109The
110.Fa flags
111argument may contain the result of
112.Em or Ns 'ing
113the following values:
114.Pp
115.Bl -tag -width "Dv TFD_NONBLOCK" -compact
116.It Dv TFD_CLOEXEC
117The newly generated file descriptor will close-on-exec.
118.It Dv TFD_NONBLOCK
119Do not block on read/write operations.
120.El
121.It Fn timerfd_gettime
122Retrieve the current state of the timer denoted by
123.Fa fd .
124The result is stored in
125.Fa curr_value
126as a
127.Dv struct itimerspec .
128The
129.Fa it_value
130and
131.Fa it_interval
132members of
133.Fa curr_value
134represent the relative time until the next expiration and the interval
135reload value last set by
136.Fn timerfd_settime ,
137respectively.
138.It Fn timerfd_settime
139Update the timer denoted by
140.Fa fd
141with the
142.Dv struct itimerspec
143in
144.Fa new_value .
145The
146.Fa it_value
147member of
148.Fa new_value
149should contain the amount of time before the timer expires, or zero if the
150timer should be disarmed.
151The
152.Fa it_interval
153member should contain the reload time if an interval timer is desired.
154.Pp
155The previous timer state will be stored in
156.Fa old_value
157given
158.Fa old_value
159is not
160.Dv NULL .
161.Pp
162The
163.Fa flags
164argument may contain the result of
165.Em or Ns 'ing
166the following values:
167.Pp
168.Bl -tag -width TFD_TIMER_CANCEL_ON_SET -compact
169.It Dv TFD_TIMER_ABSTIME
170Expiration will occur at the absolute time provided in
171.Fa new_value .
172Normally,
173.Fa new_value
174represents a relative time compared to the timer's
175.Fa clockid
176clock.
177.It Dv TFD_TIMER_CANCEL_ON_SET
178If
179.Fa clockid
180has been set to
181.Dv CLOCK_REALTIME
182and the realtime clock has experienced a discontinuous jump,
183then the timer will be canceled and the next
184.Xr read 2
185will fail with
186.Dv ECANCELED .
187.El
188.El
189.Pp
190File operations have the following semantics:
191.Bl -tag -width ioctl
192.It Xr read 2
193Transfer the number of timer expirations that have occurred since the last
194successful
195.Xr read 2
196or
197.Fn timerfd_settime
198into the output buffer of size
199.Vt uint64_t .
200If the expiration counter is zero,
201.Xr read 2
202blocks until a timer expiration occurs unless
203.Dv TFD_NONBLOCK
204is set, where
205.Dv EAGAIN
206is returned.
207.It Xr poll 2
208The file descriptor is readable when its timer expiration counter is greater
209than zero.
210.It Xr ioctl 2
211.Bl -tag -width FIONREAD
212.It Dv FIOASYNC int
213A non-zero input will set the FASYNC flag.
214A zero input will clear the FASYNC flag.
215.It Dv FIONBIO int
216A non-zero input will set the FNONBLOCK flag.
217A zero input will clear the FNONBLOCK flag.
218.El
219.El
220.Sh RETURN VALUES
221The
222.Fn timerfd_create
223system call creates a
224.Nm
225object and returns its file descriptor.
226If an error occurs, -1 is returned and the global variable
227.Fa errno
228is set to indicate the error.
229.Pp
230The
231.Fn timerfd_gettime
232and
233.Fn timerfd_settime
234system calls return 0 on success.
235If an error occurs, -1 is returned and the global variable
236.Fa errno
237is set to indicate the error.
238.Sh ERRORS
239The
240.Fn timerfd_create
241system call fails if:
242.Bl -tag -width Er
243.It Bq Er EINVAL
244The specified
245.Fa clockid
246is not supported.
247.It Bq Er EINVAL
248The provided
249.Fa flags
250are invalid.
251.It Bq Er EMFILE
252The per-process descriptor table is full.
253.It Bq Er ENFILE
254The system file table is full.
255.It Bq Er ENOMEM
256The kernel failed to allocate enough memory for the timer.
257.El
258.Pp
259Both
260.Fn timerfd_gettime
261and
262.Fn timerfd_settime
263system calls fail if:
264.Bl -tag -width Er
265.It Bq Er EBADF
266The provided
267.Fa fd
268is invalid.
269.It Bq Er EFAULT
270The addresses provided by
271.Fa curr_value ,
272.Fa new_value ,
273or
274.Fa old_value
275are invalid.
276.It Bq Er EINVAL
277The provided
278.Fa fd
279is valid, but was not generated by
280.Fn timerfd_create .
281.El
282.Pp
283The following errors only apply to
284.Fn timerfd_settime :
285.Bl -tag -width Er
286.It Bq Er EINVAL
287The provided
288.Fa flags
289are invalid.
290.It Bq Er EINVAL
291A nanosecond field in the
292.Fa new_value
293argument specified a value less than zero, or greater than or equal to 10^9.
294.It Bq Er ECANCELED
295The timer was created with the clock ID
296.Dv CLOCK_REALTIME ,
297was configured with the
298.Dv TFD_TIMER_CANCEL_ON_SET
299flag, and the system realtime clock experienced a discontinuous change without
300being read.
301.El
302.Pp
303A read from a
304.Nm
305object fails if:
306.Bl -tag -width Er
307.It Bq Er EAGAIN
308The timer's expiration counter is zero and the
309.Nm
310object is set for non-blocking I/O.
311.It Bq Er ECANCELED
312The timer was created with the clock ID
313.Dv CLOCK_REALTIME ,
314was configured with the
315.Dv TFD_TIMER_CANCEL_ON_SET
316flag, and the system realtime clock experienced a discontinuous change.
317.It Bq Er EINVAL
318The size of the read buffer is not large enough to hold the
319.Vt uint64_t
320sized timer expiration counter.
321.El
322.Sh SEE ALSO
323.Xr eventfd 2 ,
324.Xr kqueue 2 ,
325.Xr poll 2 ,
326.Xr read 2 ,
327.Xr timer_create 2 ,
328.Xr timer_gettime 2 ,
329.Xr timer_settime 2
330.Sh STANDARDS
331The
332.Nm
333system calls originated from Linux and are non-standard.
334.Sh HISTORY
335.An -nosplit
336The
337.Nm
338facility was originally ported to
339.Fx Ns 's
340Linux compatibility layer by
341.An Dmitry Chagin Aq Mt dchagin@FreeBSD.org
342in
343.Fx 12.0 .
344It was revised and adapted to be native by
345.An Jake Freeland Aq Mt jfree@FreeBSD.org
346in
347.Fx 14.0 .
348