]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/timerfd_create.2
_exit.2, bpf.2, cacheflush.2, capget.2, chdir.2, chmod.2, chroot.2, clock_getres...
[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.\"
35deeb87 22.TH TIMERFD_CREATE 2 2016-12-12 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).
50
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
4b76fffb 69of the timer, and must 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).
45b81c9c 114
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
b25c5b23 123file status flag on the new open file description.
3df1b973 124Using this flag saves extra calls to
ef7db4f5 125.BR fcntl (2)
3df1b973
MK
126to achieve the same result.
127.TP
128.B TFD_CLOEXEC
129Set the close-on-exec
130.RB ( FD_CLOEXEC )
131flag on the new file descriptor.
c5571b61 132See the description of the
3df1b973
MK
133.B O_CLOEXEC
134flag in
135.BR open (2)
136for reasons why this may be useful.
137.PP
138In Linux versions up to and including 2.6.26,
45b81c9c 139.I flags
3df1b973 140must be specified as zero.
45b81c9c
MK
141.SS timerfd_settime()
142.BR timerfd_settime ()
143arms (starts) or disarms (stops)
144the timer referred to by the file descriptor
145.IR fd .
146
147The
148.I new_value
149argument specifies the initial expiration and interval for the timer.
150The
151.I itimer
152structure used for this argument contains two fields,
153each of which is in turn a structure of type
154.IR timespec :
155.in +4n
156.nf
157
158struct timespec {
159 time_t tv_sec; /* Seconds */
160 long tv_nsec; /* Nanoseconds */
161};
162
163struct itimerspec {
164 struct timespec it_interval; /* Interval for periodic timer */
165 struct timespec it_value; /* Initial expiration */
166};
167.fi
168.in
169.PP
170.I new_value.it_value
171specifies the initial expiration of the timer,
172in seconds and nanoseconds.
173Setting either field of
174.I new_value.it_value
c7094399 175to a nonzero value arms the timer.
45b81c9c
MK
176Setting both fields of
177.I new_value.it_value
178to zero disarms the timer.
179
180Setting one or both fields of
181.I new_value.it_interval
c7094399 182to nonzero values specifies the period, in seconds and nanoseconds,
45b81c9c
MK
183for repeated timer expirations after the initial expiration.
184If both fields of
185.I new_value.it_interval
186are zero, the timer expires just once, at the time specified by
187.IR new_value.it_value .
188
51062576
MK
189By default,
190the initial expiration time specified in
191.I new_value
192is interpreted relative to the current time
193on the timer's clock at the time of the call (i.e.,
194.I new_value.it_value
195specifies a time relative to the current value of the clock specified by
196.IR clockid ).
197An absolute timeout can be selected via the
198.I flags
199argument.
200
45b81c9c
MK
201The
202.I flags
51062576
MK
203argument is a bit mask that can include the following values:
204.TP
205.B TFD_TIMER_ABSTIME
206Interpret
207.I new_value.it_value
208as an absolute value on the timer's clock.
209The timer will expire when the value of the timer's
45b81c9c 210clock reaches the value specified in
51062576 211.IR new_value.it_value .
e9477548
MK
212.TP
213.BR TFD_TIMER_CANCEL_ON_SET
214If this flag is specified along with
215.B TFD_TIMER_ABSTIME
216and the clock for this timer is
217.BR CLOCK_REALTIME
218or
219.BR CLOCK_REALTIME_ALARM ,
220then mark this timer as cancelable if the real-time clock
221undergoes a discontinuous change
222.RB ( settimeofday (2),
223.BR clock_settime (2),
224or similar).
225When such changes occur, a current or future
226.BR read (2)
227from the file descriptor will fail with the error
228.BR ECANCELED .
51062576 229.PP
009cdc22 230If the
5c902597 231.I old_value
009cdc22
MK
232argument is not NULL, then the
233.I itimerspec
234structure that it points to is used to return the setting of the timer
235that was current at the time of the call;
236see the description of
45b81c9c
MK
237.BR timerfd_gettime ()
238following.
239.\"
240.SS timerfd_gettime()
241.BR timerfd_gettime ()
242returns, in
243.IR curr_value ,
244an
245.IR itimerspec
dc55661b 246structure that contains the current setting of the timer
45b81c9c
MK
247referred to by the file descriptor
248.IR fd .
249
250The
251.I it_value
252field returns the amount of time
253until the timer will next expire.
254If both fields of this structure are zero,
255then the timer is currently disarmed.
256This field always contains a relative value, regardless of whether the
257.BR TFD_TIMER_ABSTIME
258flag was specified when setting the timer.
259
260The
261.I it_interval
262field returns the interval of the timer.
263If both fields of this structure are zero,
264then the timer is set to expire just once, at the time specified by
265.IR curr_value.it_value .
266.SS Operating on a timer file descriptor
267The file descriptor returned by
268.BR timerfd_create ()
269supports the following operations:
270.TP
271.BR read (2)
272If the timer has already expired one or more times since
273its settings were last modified using
274.BR timerfd_settime (),
275or since the last successful
276.BR read (2),
277then the buffer given to
278.BR read (2)
279returns an unsigned 8-byte integer
280.RI ( uint64_t )
281containing the number of expirations that have occurred.
88879aeb
MK
282(The returned value is in host byte order\(emthat is,
283the native byte order for integers on the host machine.)
45b81c9c
MK
284.IP
285If no timer expirations have occurred at the time of the
286.BR read (2),
287then the call either blocks until the next timer expiration,
288or fails with the error
289.B EAGAIN
ff40dbb3 290if the file descriptor has been made nonblocking
45b81c9c
MK
291(via the use of the
292.BR fcntl (2)
293.B F_SETFL
294operation to set the
295.B O_NONBLOCK
296flag).
297.IP
298A
299.BR read (2)
300will fail with the error
301.B EINVAL
302if the size of the supplied buffer is less than 8 bytes.
e9477548
MK
303
304If the associated clock is either
305.BR CLOCK_REALTIME
306or
307.BR CLOCK_REALTIME_ALARM ,
308the timer is absolute
309.RB ( TFD_TIMER_ABSTIME ),
310and the flag
311.BR TFD_TIMER_CANCEL_ON_SET
312was specified when calling
313.BR timerfd_settime (),
314then
315.BR read (2)
316will fail with the error
317.BR ECANCELED
318if the real-time clock undergoes a discontinuous change.
319(This allows the reading application to discover
320such discontinuous changes to the clock.)
45b81c9c
MK
321.TP
322.BR poll "(2), " select "(2) (and similar)"
323The file descriptor is readable
324(the
325.BR select (2)
326.I readfds
327argument; the
328.BR poll (2)
329.B POLLIN
330flag)
331if one or more timer expirations have occurred.
332.IP
333The file descriptor also supports the other file-descriptor
334multiplexing APIs:
335.BR pselect (2),
336.BR ppoll (2),
337and
338.BR epoll (7).
339.TP
5a3eb9cd 340.BR ioctl "(2)"
80d27367
MK
341The following timerfd-specific command is supported:
342.RS
343.TP
40f6b649
MK
344.BR TFD_IOC_SET_TICKS " (since Linux 3.17)"
345.\" commit 5442e9fbd7c23172a1c9bc736629cd123a9923f0
80d27367
MK
346Adjust the number of timer expirations that have occurred.
347The argument is a pointer to a nonzero 8-byte integer
5a3eb9cd
CG
348.RI ( uint64_t *)
349containing the new number of expirations.
80d27367 350Once the number is set, any waiter on the timer is woken up.
5a3eb9cd 351The only purpose of this command is to restore the expirations
80d27367
MK
352for the purpose of checkpoint/restore.
353This operation is available only if the kernel was configured with the
5a3eb9cd 354.BR CONFIG_CHECKPOINT_RESTORE
80d27367
MK
355option.
356.RE
5a3eb9cd 357.TP
45b81c9c
MK
358.BR close (2)
359When the file descriptor is no longer required it should be closed.
360When all file descriptors associated with the same timer object
361have been closed,
362the timer is disarmed and its resources are freed by the kernel.
363.\"
364.SS fork(2) semantics
365After a
366.BR fork (2),
367the child inherits a copy of the file descriptor created by
368.BR timerfd_create ().
369The file descriptor refers to the same underlying
370timer object as the corresponding file descriptor in the parent,
371and
372.BR read (2)s
373in the child will return information about
374expirations of the timer.
375.\"
376.SS execve(2) semantics
377A file descriptor created by
378.BR timerfd_create ()
379is preserved across
380.BR execve (2),
381and continues to generate timer expirations if the timer was armed.
47297adb 382.SH RETURN VALUE
45b81c9c
MK
383On success,
384.BR timerfd_create ()
385returns a new file descriptor.
386On error, \-1 is returned and
387.I errno
388is set to indicate the error.
389
390.BR timerfd_settime ()
391and
392.BR timerfd_gettime ()
393return 0 on success;
394on error they return \-1, and set
395.I errno
396to indicate the error.
397.SH ERRORS
398.BR timerfd_create ()
399can fail with the following errors:
400.TP
401.B EINVAL
402The
403.I clockid
404argument is neither
405.B CLOCK_MONOTONIC
406nor
407.BR CLOCK_REALTIME ;
3df1b973
MK
408.TP
409.B EINVAL
410.I flags
411is invalid;
412or, in Linux 2.6.26 or earlier,
45b81c9c 413.I flags
c7094399 414is nonzero.
45b81c9c
MK
415.TP
416.B EMFILE
26c32fab 417The per-process limit on the number of open file descriptors has been reached.
45b81c9c
MK
418.TP
419.B ENFILE
420The system-wide limit on the total number of open files has been
421reached.
422.TP
423.B ENODEV
424Could not mount (internal) anonymous inode device.
425.TP
426.B ENOMEM
427There was insufficient kernel memory to create the timer.
428.PP
429.BR timerfd_settime ()
430and
431.BR timerfd_gettime ()
432can fail with the following errors:
433.TP
434.B EBADF
435.I fd
436is not a valid file descriptor.
437.TP
2bbb3907
MK
438.B EFAULT
439.IR new_value ,
440.IR old_value ,
441or
442.I curr_value
443is not valid a pointer.
444.TP
45b81c9c
MK
445.B EINVAL
446.I fd
447is not a valid timerfd file descriptor.
c71b054d
MK
448.PP
449.BR timerfd_settime ()
450can also fail with the following errors:
451.TP
452.B EINVAL
45b81c9c
MK
453.I new_value
454is not properly initialized (one of the
455.I tv_nsec
456falls outside the range zero to 999,999,999).
86d95cf2
MK
457.TP
458.B EINVAL
459.\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version.
460.\" In older kernel versions, no check was made for invalid flags.
461.I flags
462is invalid.
45b81c9c
MK
463.SH VERSIONS
464These system calls are available on Linux since kernel 2.6.25.
d6a54d42 465Library support is provided by glibc since version 2.8.
45b81c9c
MK
466.SH CONFORMING TO
467These system calls are Linux-specific.
7484d5a7
MK
468.SH BUGS
469Currently,
470.\" 2.6.29
471.BR timerfd_create ()
472supports fewer types of clock IDs than
473.BR timer_create (2).
45b81c9c
MK
474.SH EXAMPLE
475The following program creates a timer and then monitors its progress.
476The program accepts up to three command-line arguments.
477The first argument specifies the number of seconds for
478the initial expiration of the timer.
479The second argument specifies the interval for the timer, in seconds.
480The third argument specifies the number of times the program should
481allow the timer to expire before terminating.
482The second and third command-line arguments are optional.
483
484The following shell session demonstrates the use of the program:
485.in +4n
486.nf
487
b43a3b30 488.RB "$" " a.out 3 1 100"
45b81c9c
MK
4890.000: timer started
4903.000: read: 1; total=1
4914.000: read: 1; total=2
b43a3b30 492.BR "^Z " " # type control-Z to suspend the program"
45b81c9c 493[1]+ Stopped ./timerfd3_demo 3 1 100
b43a3b30 494.RB "$ " "fg" " # Resume execution after a few seconds"
45b81c9c
MK
495a.out 3 1 100
4969.660: read: 5; total=7
49710.000: read: 1; total=8
49811.000: read: 1; total=9
b43a3b30 499.BR "^C " " # type control-C to suspend the program"
45b81c9c
MK
500.fi
501.in
9c330504 502.SS Program source
d84d0300 503\&
45b81c9c 504.nf
45b81c9c
MK
505.\" The commented out code here is what we currently need until
506.\" the required stuff is in glibc
507.\"
508.\"
509.\"/* Link with -lrt */
510.\"#define _GNU_SOURCE
511.\"#include <sys/syscall.h>
512.\"#include <unistd.h>
513.\"#include <time.h>
514.\"#if defined(__i386__)
515.\"#define __NR_timerfd_create 322
516.\"#define __NR_timerfd_settime 325
517.\"#define __NR_timerfd_gettime 326
518.\"#endif
519.\"
520.\"static int
521.\"timerfd_create(int clockid, int flags)
522.\"{
523.\" return syscall(__NR_timerfd_create, clockid, flags);
524.\"}
525.\"
526.\"static int
527.\"timerfd_settime(int fd, int flags, struct itimerspec *new_value,
528.\" struct itimerspec *curr_value)
529.\"{
530.\" return syscall(__NR_timerfd_settime, fd, flags, new_value,
531.\" curr_value);
532.\"}
533.\"
534.\"static int
535.\"timerfd_gettime(int fd, struct itimerspec *curr_value)
536.\"{
537.\" return syscall(__NR_timerfd_gettime, fd, curr_value);
538.\"}
539.\"
540.\"#define TFD_TIMER_ABSTIME (1 << 0)
541.\"
542.\"////////////////////////////////////////////////////////////
45b81c9c
MK
543#include <sys/timerfd.h>
544#include <time.h>
545#include <unistd.h>
546#include <stdlib.h>
547#include <stdio.h>
548#include <stdint.h> /* Definition of uint64_t */
549
550#define handle_error(msg) \\
551 do { perror(msg); exit(EXIT_FAILURE); } while (0)
552
553static void
554print_elapsed_time(void)
555{
556 static struct timespec start;
557 struct timespec curr;
558 static int first_call = 1;
559 int secs, nsecs;
560
561 if (first_call) {
562 first_call = 0;
563 if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1)
564 handle_error("clock_gettime");
565 }
566
567 if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1)
568 handle_error("clock_gettime");
569
570 secs = curr.tv_sec \- start.tv_sec;
571 nsecs = curr.tv_nsec \- start.tv_nsec;
572 if (nsecs < 0) {
573 secs\-\-;
574 nsecs += 1000000000;
575 }
576 printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
577}
578
579int
580main(int argc, char *argv[])
581{
582 struct itimerspec new_value;
583 int max_exp, fd;
584 struct timespec now;
585 uint64_t exp, tot_exp;
586 ssize_t s;
587
588 if ((argc != 2) && (argc != 4)) {
589 fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\\n",
590 argv[0]);
591 exit(EXIT_FAILURE);
592 }
593
594 if (clock_gettime(CLOCK_REALTIME, &now) == \-1)
595 handle_error("clock_gettime");
596
597 /* Create a CLOCK_REALTIME absolute timer with initial
598 expiration and interval as specified in command line */
599
600 new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
601 new_value.it_value.tv_nsec = now.tv_nsec;
602 if (argc == 2) {
603 new_value.it_interval.tv_sec = 0;
604 max_exp = 1;
605 } else {
606 new_value.it_interval.tv_sec = atoi(argv[2]);
607 max_exp = atoi(argv[3]);
608 }
609 new_value.it_interval.tv_nsec = 0;
610
611 fd = timerfd_create(CLOCK_REALTIME, 0);
612 if (fd == \-1)
613 handle_error("timerfd_create");
614
a279595b 615 if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1)
45b81c9c
MK
616 handle_error("timerfd_settime");
617
618 print_elapsed_time();
619 printf("timer started\\n");
620
621 for (tot_exp = 0; tot_exp < max_exp;) {
622 s = read(fd, &exp, sizeof(uint64_t));
623 if (s != sizeof(uint64_t))
624 handle_error("read");
625
626 tot_exp += exp;
627 print_elapsed_time();
628 printf("read: %llu; total=%llu\\n",
629 (unsigned long long) exp,
630 (unsigned long long) tot_exp);
631 }
632
633 exit(EXIT_SUCCESS);
634}
635.fi
47297adb 636.SH SEE ALSO
45b81c9c
MK
637.BR eventfd (2),
638.BR poll (2),
639.BR read (2),
640.BR select (2),
641.BR setitimer (2),
642.BR signalfd (2),
804f03e6
MK
643.BR timer_create (2),
644.BR timer_gettime (2),
645.BR timer_settime (2),
45b81c9c
MK
646.BR epoll (7),
647.BR time (7)