]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/timerfd_create.2
execve.2, exec.3: Consistently use the term 'pathname' (not 'path')
[thirdparty/man-pages.git] / man2 / timerfd_create.2
CommitLineData
45b81c9c
MK
1.\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2.\"
f0008367 3.\" %%%LICENSE_START(GPLv2+_SW_3_PARA)
45b81c9c
MK
4.\" This program is free software; you can redistribute it and/or modify
5.\" it under the terms of the GNU General Public License as published by
6.\" the Free Software Foundation; either version 2 of the License, or
7.\" (at your option) any later version.
8.\"
9.\" This program is distributed in the hope that it will be useful,
10.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
11.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12.\" GNU General Public License for more details.
13.\"
68fa4398
MK
14.\" You should have received a copy of the GNU General Public
15.\" License along with this manual; if not, see
16.\" <http://www.gnu.org/licenses/>.
8ff7380d 17.\" %%%LICENSE_END
45b81c9c 18.\"
bea08fec
MK
19.\" FIXME Linux 3.0: timerfd_settime() adds a TFD_TIMER_CANCEL_ON_SET flag;
20.\" This flag needs to documented.
d09de287 21.\"
9ba01802 22.TH TIMERFD_CREATE 2 2019-03-06 Linux "Linux Programmer's Manual"
45b81c9c
MK
23.SH NAME
24timerfd_create, timerfd_settime, timerfd_gettime \-
25timers that notify via file descriptors
26.SH SYNOPSIS
45b81c9c
MK
27.nf
28.B #include <sys/timerfd.h>
68e4db0a 29.PP
45b81c9c 30.BI "int timerfd_create(int " clockid ", int " flags );
68e4db0a 31.PP
45b81c9c
MK
32.BI "int timerfd_settime(int " fd ", int " flags ,
33.BI " const struct itimerspec *" new_value ,
5c902597 34.BI " struct itimerspec *" old_value );
68e4db0a 35.PP
45b81c9c
MK
36.BI "int timerfd_gettime(int " fd ", struct itimerspec *" curr_value );
37.fi
38.SH DESCRIPTION
39These system calls create and operate on a timer
40that delivers timer expiration notifications via a file descriptor.
41They provide an alternative to the use of
42.BR setitimer (2)
43or
804f03e6 44.BR timer_create (2),
45b81c9c
MK
45with the advantage that the file descriptor may be monitored by
46.BR select (2),
47.BR poll (2),
48and
49.BR epoll (7).
efeece04 50.PP
45b81c9c 51The use of these three system calls is analogous to the use of
804f03e6
MK
52.BR timer_create (2),
53.BR timer_settime (2),
45b81c9c 54and
804f03e6 55.BR timer_gettime (2).
45b81c9c 56(There is no analog of
804f03e6 57.BR timer_getoverrun (2),
45b81c9c
MK
58since that functionality is provided by
59.BR read (2),
60as described below.)
61.\"
62.SS timerfd_create()
63.BR timerfd_create ()
64creates a new timer object,
65and returns a file descriptor that refers to that timer.
66The
67.I clockid
68argument specifies the clock that is used to mark the progress
205b1bce 69of the timer, and must be one of the following:
4b76fffb 70.TP
45b81c9c 71.B CLOCK_REALTIME
4b76fffb
MK
72A settable system-wide real-time clock.
73.TP
45b81c9c 74.B CLOCK_MONOTONIC
4b76fffb
MK
75A nonsettable monotonically increasing clock that measures time
76from some unspecified point in the past that does not change
77after system startup.
4c747119
MK
78.TP
79.BR CLOCK_BOOTTIME " (Since Linux 3.15)"
80.\" commit 4a2378a943f09907fb1ae35c15de917f60289c14
81Like
82.BR CLOCK_MONOTONIC ,
83this is a monotonically increasing clock.
84However, whereas the
85.BR CLOCK_MONOTONIC
86clock does not measure the time while a system is suspended, the
87.BR CLOCK_BOOTTIME
88clock does include the time during which the system is suspended.
89This is useful for applications that need to be suspend-aware.
90.BR CLOCK_REALTIME
91is not suitable for such applications, since that clock is affected
92by discontinuous changes to the system clock.
93.TP
94.BR CLOCK_REALTIME_ALARM " (since Linux 3.11)"
95.\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8
96This clock is like
97.BR CLOCK_REALTIME ,
98but will wake the system if it is suspended.
99The caller must have the
100.B CAP_WAKE_ALARM
101capability in order to set a timer against this clock.
102.TP
103.BR CLOCK_BOOTTIME_ALARM " (since Linux 3.11)"
104.\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8
105This clock is like
106.BR CLOCK_BOOTTIME ,
107but will wake the system if it is suspended.
108The caller must have the
109.B CAP_WAKE_ALARM
110capability in order to set a timer against this clock.
4b76fffb 111.PP
45b81c9c 112The current value of each of these clocks can be retrieved using
0eb44391 113.BR clock_gettime (2).
efeece04 114.PP
3df1b973
MK
115Starting with Linux 2.6.27, the following values may be bitwise ORed in
116.IR flags
117to change the behavior of
118.BR timerfd_create ():
119.TP 14
120.B TFD_NONBLOCK
121Set the
122.BR O_NONBLOCK
7f11e32c
MK
123file status flag on the open file description (see
124.BR open (2))
125referred to by the new file descriptor.
3df1b973 126Using this flag saves extra calls to
ef7db4f5 127.BR fcntl (2)
3df1b973
MK
128to achieve the same result.
129.TP
130.B TFD_CLOEXEC
131Set the close-on-exec
132.RB ( FD_CLOEXEC )
133flag on the new file descriptor.
c5571b61 134See the description of the
3df1b973
MK
135.B O_CLOEXEC
136flag in
137.BR open (2)
138for reasons why this may be useful.
139.PP
140In Linux versions up to and including 2.6.26,
45b81c9c 141.I flags
3df1b973 142must be specified as zero.
45b81c9c
MK
143.SS timerfd_settime()
144.BR timerfd_settime ()
145arms (starts) or disarms (stops)
146the timer referred to by the file descriptor
147.IR fd .
efeece04 148.PP
45b81c9c
MK
149The
150.I new_value
151argument specifies the initial expiration and interval for the timer.
152The
251ebc1e 153.I itimerspec
45b81c9c
MK
154structure used for this argument contains two fields,
155each of which is in turn a structure of type
156.IR timespec :
e646a1ba 157.PP
45b81c9c 158.in +4n
e646a1ba 159.EX
45b81c9c
MK
160struct timespec {
161 time_t tv_sec; /* Seconds */
162 long tv_nsec; /* Nanoseconds */
163};
164
165struct itimerspec {
166 struct timespec it_interval; /* Interval for periodic timer */
167 struct timespec it_value; /* Initial expiration */
168};
b8302363 169.EE
45b81c9c
MK
170.in
171.PP
172.I new_value.it_value
173specifies the initial expiration of the timer,
174in seconds and nanoseconds.
175Setting either field of
176.I new_value.it_value
c7094399 177to a nonzero value arms the timer.
45b81c9c
MK
178Setting both fields of
179.I new_value.it_value
180to zero disarms the timer.
efeece04 181.PP
45b81c9c
MK
182Setting one or both fields of
183.I new_value.it_interval
c7094399 184to nonzero values specifies the period, in seconds and nanoseconds,
45b81c9c
MK
185for repeated timer expirations after the initial expiration.
186If both fields of
187.I new_value.it_interval
188are zero, the timer expires just once, at the time specified by
189.IR new_value.it_value .
efeece04 190.PP
51062576
MK
191By default,
192the initial expiration time specified in
193.I new_value
194is interpreted relative to the current time
195on the timer's clock at the time of the call (i.e.,
196.I new_value.it_value
197specifies a time relative to the current value of the clock specified by
198.IR clockid ).
199An absolute timeout can be selected via the
200.I flags
201argument.
efeece04 202.PP
45b81c9c
MK
203The
204.I flags
51062576
MK
205argument is a bit mask that can include the following values:
206.TP
207.B TFD_TIMER_ABSTIME
208Interpret
209.I new_value.it_value
210as an absolute value on the timer's clock.
211The timer will expire when the value of the timer's
45b81c9c 212clock reaches the value specified in
51062576 213.IR new_value.it_value .
e9477548
MK
214.TP
215.BR TFD_TIMER_CANCEL_ON_SET
216If this flag is specified along with
217.B TFD_TIMER_ABSTIME
218and the clock for this timer is
219.BR CLOCK_REALTIME
220or
221.BR CLOCK_REALTIME_ALARM ,
222then mark this timer as cancelable if the real-time clock
223undergoes a discontinuous change
224.RB ( settimeofday (2),
225.BR clock_settime (2),
226or similar).
227When such changes occur, a current or future
228.BR read (2)
229from the file descriptor will fail with the error
230.BR ECANCELED .
51062576 231.PP
009cdc22 232If the
5c902597 233.I old_value
009cdc22
MK
234argument is not NULL, then the
235.I itimerspec
236structure that it points to is used to return the setting of the timer
237that was current at the time of the call;
238see the description of
45b81c9c
MK
239.BR timerfd_gettime ()
240following.
241.\"
242.SS timerfd_gettime()
243.BR timerfd_gettime ()
244returns, in
245.IR curr_value ,
246an
247.IR itimerspec
dc55661b 248structure that contains the current setting of the timer
45b81c9c
MK
249referred to by the file descriptor
250.IR fd .
efeece04 251.PP
45b81c9c
MK
252The
253.I it_value
254field returns the amount of time
255until the timer will next expire.
256If both fields of this structure are zero,
257then the timer is currently disarmed.
258This field always contains a relative value, regardless of whether the
259.BR TFD_TIMER_ABSTIME
260flag was specified when setting the timer.
efeece04 261.PP
45b81c9c
MK
262The
263.I it_interval
264field returns the interval of the timer.
265If both fields of this structure are zero,
266then the timer is set to expire just once, at the time specified by
267.IR curr_value.it_value .
268.SS Operating on a timer file descriptor
269The file descriptor returned by
270.BR timerfd_create ()
271supports the following operations:
272.TP
273.BR read (2)
274If the timer has already expired one or more times since
275its settings were last modified using
276.BR timerfd_settime (),
277or since the last successful
278.BR read (2),
279then the buffer given to
280.BR read (2)
281returns an unsigned 8-byte integer
282.RI ( uint64_t )
283containing the number of expirations that have occurred.
88879aeb
MK
284(The returned value is in host byte order\(emthat is,
285the native byte order for integers on the host machine.)
45b81c9c
MK
286.IP
287If no timer expirations have occurred at the time of the
288.BR read (2),
289then the call either blocks until the next timer expiration,
290or fails with the error
291.B EAGAIN
ff40dbb3 292if the file descriptor has been made nonblocking
45b81c9c
MK
293(via the use of the
294.BR fcntl (2)
295.B F_SETFL
296operation to set the
297.B O_NONBLOCK
298flag).
299.IP
300A
301.BR read (2)
26cd31fd 302fails with the error
45b81c9c
MK
303.B EINVAL
304if the size of the supplied buffer is less than 8 bytes.
efeece04 305.IP
e9477548
MK
306If the associated clock is either
307.BR CLOCK_REALTIME
308or
309.BR CLOCK_REALTIME_ALARM ,
310the timer is absolute
311.RB ( TFD_TIMER_ABSTIME ),
312and the flag
313.BR TFD_TIMER_CANCEL_ON_SET
314was specified when calling
315.BR timerfd_settime (),
316then
317.BR read (2)
26cd31fd 318fails with the error
e9477548
MK
319.BR ECANCELED
320if the real-time clock undergoes a discontinuous change.
321(This allows the reading application to discover
322such discontinuous changes to the clock.)
45b81c9c
MK
323.TP
324.BR poll "(2), " select "(2) (and similar)"
325The file descriptor is readable
326(the
327.BR select (2)
328.I readfds
329argument; the
330.BR poll (2)
331.B POLLIN
332flag)
333if one or more timer expirations have occurred.
334.IP
335The file descriptor also supports the other file-descriptor
336multiplexing APIs:
337.BR pselect (2),
338.BR ppoll (2),
339and
340.BR epoll (7).
341.TP
6614e292 342.BR ioctl (2)
80d27367
MK
343The following timerfd-specific command is supported:
344.RS
345.TP
40f6b649
MK
346.BR TFD_IOC_SET_TICKS " (since Linux 3.17)"
347.\" commit 5442e9fbd7c23172a1c9bc736629cd123a9923f0
80d27367
MK
348Adjust the number of timer expirations that have occurred.
349The argument is a pointer to a nonzero 8-byte integer
5a3eb9cd
CG
350.RI ( uint64_t *)
351containing the new number of expirations.
80d27367 352Once the number is set, any waiter on the timer is woken up.
5a3eb9cd 353The only purpose of this command is to restore the expirations
80d27367
MK
354for the purpose of checkpoint/restore.
355This operation is available only if the kernel was configured with the
5a3eb9cd 356.BR CONFIG_CHECKPOINT_RESTORE
80d27367
MK
357option.
358.RE
5a3eb9cd 359.TP
45b81c9c
MK
360.BR close (2)
361When the file descriptor is no longer required it should be closed.
362When all file descriptors associated with the same timer object
363have been closed,
364the timer is disarmed and its resources are freed by the kernel.
365.\"
366.SS fork(2) semantics
367After a
368.BR fork (2),
369the child inherits a copy of the file descriptor created by
370.BR timerfd_create ().
371The file descriptor refers to the same underlying
372timer object as the corresponding file descriptor in the parent,
373and
374.BR read (2)s
375in the child will return information about
376expirations of the timer.
377.\"
378.SS execve(2) semantics
379A file descriptor created by
380.BR timerfd_create ()
381is preserved across
382.BR execve (2),
383and continues to generate timer expirations if the timer was armed.
47297adb 384.SH RETURN VALUE
45b81c9c
MK
385On success,
386.BR timerfd_create ()
387returns a new file descriptor.
388On error, \-1 is returned and
389.I errno
390is set to indicate the error.
efeece04 391.PP
45b81c9c
MK
392.BR timerfd_settime ()
393and
394.BR timerfd_gettime ()
395return 0 on success;
396on error they return \-1, and set
397.I errno
398to indicate the error.
399.SH ERRORS
400.BR timerfd_create ()
401can fail with the following errors:
402.TP
403.B EINVAL
404The
405.I clockid
406argument is neither
407.B CLOCK_MONOTONIC
408nor
409.BR CLOCK_REALTIME ;
3df1b973
MK
410.TP
411.B EINVAL
412.I flags
413is invalid;
414or, in Linux 2.6.26 or earlier,
45b81c9c 415.I flags
c7094399 416is nonzero.
45b81c9c
MK
417.TP
418.B EMFILE
26c32fab 419The per-process limit on the number of open file descriptors has been reached.
45b81c9c
MK
420.TP
421.B ENFILE
422The system-wide limit on the total number of open files has been
423reached.
424.TP
425.B ENODEV
426Could not mount (internal) anonymous inode device.
427.TP
428.B ENOMEM
429There was insufficient kernel memory to create the timer.
430.PP
431.BR timerfd_settime ()
432and
433.BR timerfd_gettime ()
434can fail with the following errors:
435.TP
436.B EBADF
437.I fd
438is not a valid file descriptor.
439.TP
2bbb3907
MK
440.B EFAULT
441.IR new_value ,
442.IR old_value ,
443or
444.I curr_value
445is not valid a pointer.
446.TP
45b81c9c
MK
447.B EINVAL
448.I fd
449is not a valid timerfd file descriptor.
c71b054d
MK
450.PP
451.BR timerfd_settime ()
452can also fail with the following errors:
453.TP
454.B EINVAL
45b81c9c
MK
455.I new_value
456is not properly initialized (one of the
457.I tv_nsec
458falls outside the range zero to 999,999,999).
86d95cf2
MK
459.TP
460.B EINVAL
461.\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version.
462.\" In older kernel versions, no check was made for invalid flags.
463.I flags
464is invalid.
45b81c9c
MK
465.SH VERSIONS
466These system calls are available on Linux since kernel 2.6.25.
d6a54d42 467Library support is provided by glibc since version 2.8.
45b81c9c
MK
468.SH CONFORMING TO
469These system calls are Linux-specific.
7484d5a7
MK
470.SH BUGS
471Currently,
472.\" 2.6.29
473.BR timerfd_create ()
474supports fewer types of clock IDs than
475.BR timer_create (2).
45b81c9c
MK
476.SH EXAMPLE
477The following program creates a timer and then monitors its progress.
478The program accepts up to three command-line arguments.
479The first argument specifies the number of seconds for
480the initial expiration of the timer.
481The second argument specifies the interval for the timer, in seconds.
482The third argument specifies the number of times the program should
483allow the timer to expire before terminating.
484The second and third command-line arguments are optional.
efeece04 485.PP
45b81c9c 486The following shell session demonstrates the use of the program:
e646a1ba 487.PP
45b81c9c 488.in +4n
e646a1ba 489.EX
b43a3b30 490.RB "$" " a.out 3 1 100"
45b81c9c
MK
4910.000: timer started
4923.000: read: 1; total=1
4934.000: read: 1; total=2
b43a3b30 494.BR "^Z " " # type control-Z to suspend the program"
45b81c9c 495[1]+ Stopped ./timerfd3_demo 3 1 100
b43a3b30 496.RB "$ " "fg" " # Resume execution after a few seconds"
45b81c9c
MK
497a.out 3 1 100
4989.660: read: 5; total=7
49910.000: read: 1; total=8
50011.000: read: 1; total=9
b43a3b30 501.BR "^C " " # type control-C to suspend the program"
b8302363 502.EE
45b81c9c 503.in
9c330504 504.SS Program source
d84d0300 505\&
e7d0bb47 506.EX
45b81c9c
MK
507.\" The commented out code here is what we currently need until
508.\" the required stuff is in glibc
509.\"
510.\"
511.\"/* Link with -lrt */
512.\"#define _GNU_SOURCE
513.\"#include <sys/syscall.h>
514.\"#include <unistd.h>
515.\"#include <time.h>
516.\"#if defined(__i386__)
517.\"#define __NR_timerfd_create 322
518.\"#define __NR_timerfd_settime 325
519.\"#define __NR_timerfd_gettime 326
520.\"#endif
521.\"
522.\"static int
523.\"timerfd_create(int clockid, int flags)
524.\"{
525.\" return syscall(__NR_timerfd_create, clockid, flags);
526.\"}
527.\"
528.\"static int
529.\"timerfd_settime(int fd, int flags, struct itimerspec *new_value,
530.\" struct itimerspec *curr_value)
531.\"{
532.\" return syscall(__NR_timerfd_settime, fd, flags, new_value,
533.\" curr_value);
534.\"}
535.\"
536.\"static int
537.\"timerfd_gettime(int fd, struct itimerspec *curr_value)
538.\"{
539.\" return syscall(__NR_timerfd_gettime, fd, curr_value);
540.\"}
541.\"
542.\"#define TFD_TIMER_ABSTIME (1 << 0)
543.\"
544.\"////////////////////////////////////////////////////////////
45b81c9c
MK
545#include <sys/timerfd.h>
546#include <time.h>
547#include <unistd.h>
548#include <stdlib.h>
549#include <stdio.h>
550#include <stdint.h> /* Definition of uint64_t */
551
d1a71985 552#define handle_error(msg) \e
45b81c9c
MK
553 do { perror(msg); exit(EXIT_FAILURE); } while (0)
554
555static void
556print_elapsed_time(void)
557{
558 static struct timespec start;
559 struct timespec curr;
560 static int first_call = 1;
561 int secs, nsecs;
562
563 if (first_call) {
564 first_call = 0;
565 if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1)
566 handle_error("clock_gettime");
567 }
568
569 if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1)
570 handle_error("clock_gettime");
571
572 secs = curr.tv_sec \- start.tv_sec;
573 nsecs = curr.tv_nsec \- start.tv_nsec;
574 if (nsecs < 0) {
575 secs\-\-;
576 nsecs += 1000000000;
577 }
578 printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
579}
580
581int
582main(int argc, char *argv[])
583{
584 struct itimerspec new_value;
585 int max_exp, fd;
586 struct timespec now;
587 uint64_t exp, tot_exp;
588 ssize_t s;
589
590 if ((argc != 2) && (argc != 4)) {
d1a71985 591 fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\en",
45b81c9c
MK
592 argv[0]);
593 exit(EXIT_FAILURE);
594 }
595
596 if (clock_gettime(CLOCK_REALTIME, &now) == \-1)
597 handle_error("clock_gettime");
598
599 /* Create a CLOCK_REALTIME absolute timer with initial
600 expiration and interval as specified in command line */
601
602 new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
603 new_value.it_value.tv_nsec = now.tv_nsec;
604 if (argc == 2) {
605 new_value.it_interval.tv_sec = 0;
606 max_exp = 1;
607 } else {
608 new_value.it_interval.tv_sec = atoi(argv[2]);
609 max_exp = atoi(argv[3]);
610 }
611 new_value.it_interval.tv_nsec = 0;
612
613 fd = timerfd_create(CLOCK_REALTIME, 0);
614 if (fd == \-1)
615 handle_error("timerfd_create");
616
a279595b 617 if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1)
45b81c9c
MK
618 handle_error("timerfd_settime");
619
620 print_elapsed_time();
d1a71985 621 printf("timer started\en");
45b81c9c
MK
622
623 for (tot_exp = 0; tot_exp < max_exp;) {
624 s = read(fd, &exp, sizeof(uint64_t));
625 if (s != sizeof(uint64_t))
626 handle_error("read");
627
628 tot_exp += exp;
629 print_elapsed_time();
d1a71985 630 printf("read: %llu; total=%llu\en",
45b81c9c
MK
631 (unsigned long long) exp,
632 (unsigned long long) tot_exp);
633 }
634
635 exit(EXIT_SUCCESS);
636}
e7d0bb47 637.EE
47297adb 638.SH SEE ALSO
45b81c9c
MK
639.BR eventfd (2),
640.BR poll (2),
641.BR read (2),
642.BR select (2),
643.BR setitimer (2),
644.BR signalfd (2),
804f03e6
MK
645.BR timer_create (2),
646.BR timer_gettime (2),
647.BR timer_settime (2),
45b81c9c
MK
648.BR epoll (7),
649.BR time (7)