]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/timerfd_create.2
MB_CUR_MAX.3, MB_LEN_MAX.3, btowc.3, fgetwc.3, fgetws.3, fputwc.3, fputws.3, fwide...
[thirdparty/man-pages.git] / man2 / timerfd_create.2
CommitLineData
45b81c9c
MK
1.\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2.\"
ad14cf74 3.\" %%%LICENSE_START(GPLv2+_3_PARA_SW)
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/>.
ad14cf74 17.\" %%%END_LICENSE
45b81c9c 18.\"
d09de287
MK
19.\" FIXME: Linux 3.0: timerfd_settime() adds a TFD_TIMER_CANCEL_ON_SET flag.
20.\"
009cdc22 21.TH TIMERFD_CREATE 2 2011-09-14 Linux "Linux Programmer's Manual"
45b81c9c
MK
22.SH NAME
23timerfd_create, timerfd_settime, timerfd_gettime \-
24timers that notify via file descriptors
25.SH SYNOPSIS
45b81c9c
MK
26.nf
27.B #include <sys/timerfd.h>
28.sp
29.BI "int timerfd_create(int " clockid ", int " flags );
30.sp
31.BI "int timerfd_settime(int " fd ", int " flags ,
32.BI " const struct itimerspec *" new_value ,
5c902597 33.BI " struct itimerspec *" old_value );
45b81c9c
MK
34.sp
35.BI "int timerfd_gettime(int " fd ", struct itimerspec *" curr_value );
36.fi
37.SH DESCRIPTION
38These system calls create and operate on a timer
39that delivers timer expiration notifications via a file descriptor.
40They provide an alternative to the use of
41.BR setitimer (2)
42or
804f03e6 43.BR timer_create (2),
45b81c9c
MK
44with the advantage that the file descriptor may be monitored by
45.BR select (2),
46.BR poll (2),
47and
48.BR epoll (7).
49
50The use of these three system calls is analogous to the use of
804f03e6
MK
51.BR timer_create (2),
52.BR timer_settime (2),
45b81c9c 53and
804f03e6 54.BR timer_gettime (2).
45b81c9c 55(There is no analog of
804f03e6 56.BR timer_getoverrun (2),
45b81c9c
MK
57since that functionality is provided by
58.BR read (2),
59as described below.)
60.\"
61.SS timerfd_create()
62.BR timerfd_create ()
63creates a new timer object,
64and returns a file descriptor that refers to that timer.
65The
66.I clockid
67argument specifies the clock that is used to mark the progress
68of the timer, and must be either
69.B CLOCK_REALTIME
70or
71.BR CLOCK_MONOTONIC .
72.B CLOCK_REALTIME
73is a settable system-wide clock.
74.B CLOCK_MONOTONIC
24b74457 75is a nonsettable clock that is not affected
45b81c9c
MK
76by discontinuous changes in the system clock
77(e.g., manual changes to system time).
78The current value of each of these clocks can be retrieved using
0eb44391 79.BR clock_gettime (2).
45b81c9c 80
3df1b973
MK
81Starting with Linux 2.6.27, the following values may be bitwise ORed in
82.IR flags
83to change the behavior of
84.BR timerfd_create ():
85.TP 14
86.B TFD_NONBLOCK
87Set the
88.BR O_NONBLOCK
b25c5b23 89file status flag on the new open file description.
3df1b973 90Using this flag saves extra calls to
ef7db4f5 91.BR fcntl (2)
3df1b973
MK
92to achieve the same result.
93.TP
94.B TFD_CLOEXEC
95Set the close-on-exec
96.RB ( FD_CLOEXEC )
97flag on the new file descriptor.
c5571b61 98See the description of the
3df1b973
MK
99.B O_CLOEXEC
100flag in
101.BR open (2)
102for reasons why this may be useful.
103.PP
104In Linux versions up to and including 2.6.26,
45b81c9c 105.I flags
3df1b973 106must be specified as zero.
45b81c9c
MK
107.SS timerfd_settime()
108.BR timerfd_settime ()
109arms (starts) or disarms (stops)
110the timer referred to by the file descriptor
111.IR fd .
112
113The
114.I new_value
115argument specifies the initial expiration and interval for the timer.
116The
117.I itimer
118structure used for this argument contains two fields,
119each of which is in turn a structure of type
120.IR timespec :
121.in +4n
122.nf
123
124struct timespec {
125 time_t tv_sec; /* Seconds */
126 long tv_nsec; /* Nanoseconds */
127};
128
129struct itimerspec {
130 struct timespec it_interval; /* Interval for periodic timer */
131 struct timespec it_value; /* Initial expiration */
132};
133.fi
134.in
135.PP
136.I new_value.it_value
137specifies the initial expiration of the timer,
138in seconds and nanoseconds.
139Setting either field of
140.I new_value.it_value
c7094399 141to a nonzero value arms the timer.
45b81c9c
MK
142Setting both fields of
143.I new_value.it_value
144to zero disarms the timer.
145
146Setting one or both fields of
147.I new_value.it_interval
c7094399 148to nonzero values specifies the period, in seconds and nanoseconds,
45b81c9c
MK
149for repeated timer expirations after the initial expiration.
150If both fields of
151.I new_value.it_interval
152are zero, the timer expires just once, at the time specified by
153.IR new_value.it_value .
154
155The
156.I flags
157argument is either 0, to start a relative timer
612144c5 158.RI ( new_value.it_value
45b81c9c
MK
159specifies a time relative to the current value of the clock specified by
160.IR clockid ),
161or
162.BR TFD_TIMER_ABSTIME ,
163to start an absolute timer
c3afed74 164.RI ( new_value.it_value
45b81c9c
MK
165specifies an absolute time for the clock specified by
166.IR clockid ;
167that is, the timer will expire when the value of that
168clock reaches the value specified in
33fb70ba 169.IR new_value.it_value ).
45b81c9c 170
009cdc22 171If the
5c902597 172.I old_value
009cdc22
MK
173argument is not NULL, then the
174.I itimerspec
175structure that it points to is used to return the setting of the timer
176that was current at the time of the call;
177see the description of
45b81c9c
MK
178.BR timerfd_gettime ()
179following.
180.\"
181.SS timerfd_gettime()
182.BR timerfd_gettime ()
183returns, in
184.IR curr_value ,
185an
186.IR itimerspec
dc55661b 187structure that contains the current setting of the timer
45b81c9c
MK
188referred to by the file descriptor
189.IR fd .
190
191The
192.I it_value
193field returns the amount of time
194until the timer will next expire.
195If both fields of this structure are zero,
196then the timer is currently disarmed.
197This field always contains a relative value, regardless of whether the
198.BR TFD_TIMER_ABSTIME
199flag was specified when setting the timer.
200
201The
202.I it_interval
203field returns the interval of the timer.
204If both fields of this structure are zero,
205then the timer is set to expire just once, at the time specified by
206.IR curr_value.it_value .
207.SS Operating on a timer file descriptor
208The file descriptor returned by
209.BR timerfd_create ()
210supports the following operations:
211.TP
212.BR read (2)
213If the timer has already expired one or more times since
214its settings were last modified using
215.BR timerfd_settime (),
216or since the last successful
217.BR read (2),
218then the buffer given to
219.BR read (2)
220returns an unsigned 8-byte integer
221.RI ( uint64_t )
222containing the number of expirations that have occurred.
223(The returned value is in host byte order,
224i.e., the native byte order for integers on the host machine.)
225.IP
226If no timer expirations have occurred at the time of the
227.BR read (2),
228then the call either blocks until the next timer expiration,
229or fails with the error
230.B EAGAIN
ff40dbb3 231if the file descriptor has been made nonblocking
45b81c9c
MK
232(via the use of the
233.BR fcntl (2)
234.B F_SETFL
235operation to set the
236.B O_NONBLOCK
237flag).
238.IP
239A
240.BR read (2)
241will fail with the error
242.B EINVAL
243if the size of the supplied buffer is less than 8 bytes.
244.TP
245.BR poll "(2), " select "(2) (and similar)"
246The file descriptor is readable
247(the
248.BR select (2)
249.I readfds
250argument; the
251.BR poll (2)
252.B POLLIN
253flag)
254if one or more timer expirations have occurred.
255.IP
256The file descriptor also supports the other file-descriptor
257multiplexing APIs:
258.BR pselect (2),
259.BR ppoll (2),
260and
261.BR epoll (7).
262.TP
263.BR close (2)
264When the file descriptor is no longer required it should be closed.
265When all file descriptors associated with the same timer object
266have been closed,
267the timer is disarmed and its resources are freed by the kernel.
268.\"
269.SS fork(2) semantics
270After a
271.BR fork (2),
272the child inherits a copy of the file descriptor created by
273.BR timerfd_create ().
274The file descriptor refers to the same underlying
275timer object as the corresponding file descriptor in the parent,
276and
277.BR read (2)s
278in the child will return information about
279expirations of the timer.
280.\"
281.SS execve(2) semantics
282A file descriptor created by
283.BR timerfd_create ()
284is preserved across
285.BR execve (2),
286and continues to generate timer expirations if the timer was armed.
47297adb 287.SH RETURN VALUE
45b81c9c
MK
288On success,
289.BR timerfd_create ()
290returns a new file descriptor.
291On error, \-1 is returned and
292.I errno
293is set to indicate the error.
294
295.BR timerfd_settime ()
296and
297.BR timerfd_gettime ()
298return 0 on success;
299on error they return \-1, and set
300.I errno
301to indicate the error.
302.SH ERRORS
303.BR timerfd_create ()
304can fail with the following errors:
305.TP
306.B EINVAL
307The
308.I clockid
309argument is neither
310.B CLOCK_MONOTONIC
311nor
312.BR CLOCK_REALTIME ;
3df1b973
MK
313.TP
314.B EINVAL
315.I flags
316is invalid;
317or, in Linux 2.6.26 or earlier,
45b81c9c 318.I flags
c7094399 319is nonzero.
45b81c9c
MK
320.TP
321.B EMFILE
322The per-process limit of open file descriptors has been reached.
323.TP
324.B ENFILE
325The system-wide limit on the total number of open files has been
326reached.
327.TP
328.B ENODEV
329Could not mount (internal) anonymous inode device.
330.TP
331.B ENOMEM
332There was insufficient kernel memory to create the timer.
333.PP
334.BR timerfd_settime ()
335and
336.BR timerfd_gettime ()
337can fail with the following errors:
338.TP
339.B EBADF
340.I fd
341is not a valid file descriptor.
342.TP
2bbb3907
MK
343.B EFAULT
344.IR new_value ,
345.IR old_value ,
346or
347.I curr_value
348is not valid a pointer.
349.TP
45b81c9c
MK
350.B EINVAL
351.I fd
352is not a valid timerfd file descriptor.
c71b054d
MK
353.PP
354.BR timerfd_settime ()
355can also fail with the following errors:
356.TP
357.B EINVAL
45b81c9c
MK
358.I new_value
359is not properly initialized (one of the
360.I tv_nsec
361falls outside the range zero to 999,999,999).
86d95cf2
MK
362.TP
363.B EINVAL
364.\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version.
365.\" In older kernel versions, no check was made for invalid flags.
366.I flags
367is invalid.
45b81c9c
MK
368.SH VERSIONS
369These system calls are available on Linux since kernel 2.6.25.
d6a54d42 370Library support is provided by glibc since version 2.8.
45b81c9c
MK
371.SH CONFORMING TO
372These system calls are Linux-specific.
7484d5a7
MK
373.SH BUGS
374Currently,
375.\" 2.6.29
376.BR timerfd_create ()
377supports fewer types of clock IDs than
378.BR timer_create (2).
45b81c9c
MK
379.SH EXAMPLE
380The following program creates a timer and then monitors its progress.
381The program accepts up to three command-line arguments.
382The first argument specifies the number of seconds for
383the initial expiration of the timer.
384The second argument specifies the interval for the timer, in seconds.
385The third argument specifies the number of times the program should
386allow the timer to expire before terminating.
387The second and third command-line arguments are optional.
388
389The following shell session demonstrates the use of the program:
390.in +4n
391.nf
392
b43a3b30 393.RB "$" " a.out 3 1 100"
45b81c9c
MK
3940.000: timer started
3953.000: read: 1; total=1
3964.000: read: 1; total=2
b43a3b30 397.BR "^Z " " # type control-Z to suspend the program"
45b81c9c 398[1]+ Stopped ./timerfd3_demo 3 1 100
b43a3b30 399.RB "$ " "fg" " # Resume execution after a few seconds"
45b81c9c
MK
400a.out 3 1 100
4019.660: read: 5; total=7
40210.000: read: 1; total=8
40311.000: read: 1; total=9
b43a3b30 404.BR "^C " " # type control-C to suspend the program"
45b81c9c
MK
405.fi
406.in
9c330504 407.SS Program source
d84d0300 408\&
45b81c9c 409.nf
45b81c9c
MK
410.\" The commented out code here is what we currently need until
411.\" the required stuff is in glibc
412.\"
413.\"
414.\"/* Link with -lrt */
415.\"#define _GNU_SOURCE
416.\"#include <sys/syscall.h>
417.\"#include <unistd.h>
418.\"#include <time.h>
419.\"#if defined(__i386__)
420.\"#define __NR_timerfd_create 322
421.\"#define __NR_timerfd_settime 325
422.\"#define __NR_timerfd_gettime 326
423.\"#endif
424.\"
425.\"static int
426.\"timerfd_create(int clockid, int flags)
427.\"{
428.\" return syscall(__NR_timerfd_create, clockid, flags);
429.\"}
430.\"
431.\"static int
432.\"timerfd_settime(int fd, int flags, struct itimerspec *new_value,
433.\" struct itimerspec *curr_value)
434.\"{
435.\" return syscall(__NR_timerfd_settime, fd, flags, new_value,
436.\" curr_value);
437.\"}
438.\"
439.\"static int
440.\"timerfd_gettime(int fd, struct itimerspec *curr_value)
441.\"{
442.\" return syscall(__NR_timerfd_gettime, fd, curr_value);
443.\"}
444.\"
445.\"#define TFD_TIMER_ABSTIME (1 << 0)
446.\"
447.\"////////////////////////////////////////////////////////////
45b81c9c
MK
448#include <sys/timerfd.h>
449#include <time.h>
450#include <unistd.h>
451#include <stdlib.h>
452#include <stdio.h>
453#include <stdint.h> /* Definition of uint64_t */
454
455#define handle_error(msg) \\
456 do { perror(msg); exit(EXIT_FAILURE); } while (0)
457
458static void
459print_elapsed_time(void)
460{
461 static struct timespec start;
462 struct timespec curr;
463 static int first_call = 1;
464 int secs, nsecs;
465
466 if (first_call) {
467 first_call = 0;
468 if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1)
469 handle_error("clock_gettime");
470 }
471
472 if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1)
473 handle_error("clock_gettime");
474
475 secs = curr.tv_sec \- start.tv_sec;
476 nsecs = curr.tv_nsec \- start.tv_nsec;
477 if (nsecs < 0) {
478 secs\-\-;
479 nsecs += 1000000000;
480 }
481 printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
482}
483
484int
485main(int argc, char *argv[])
486{
487 struct itimerspec new_value;
488 int max_exp, fd;
489 struct timespec now;
490 uint64_t exp, tot_exp;
491 ssize_t s;
492
493 if ((argc != 2) && (argc != 4)) {
494 fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\\n",
495 argv[0]);
496 exit(EXIT_FAILURE);
497 }
498
499 if (clock_gettime(CLOCK_REALTIME, &now) == \-1)
500 handle_error("clock_gettime");
501
502 /* Create a CLOCK_REALTIME absolute timer with initial
503 expiration and interval as specified in command line */
504
505 new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
506 new_value.it_value.tv_nsec = now.tv_nsec;
507 if (argc == 2) {
508 new_value.it_interval.tv_sec = 0;
509 max_exp = 1;
510 } else {
511 new_value.it_interval.tv_sec = atoi(argv[2]);
512 max_exp = atoi(argv[3]);
513 }
514 new_value.it_interval.tv_nsec = 0;
515
516 fd = timerfd_create(CLOCK_REALTIME, 0);
517 if (fd == \-1)
518 handle_error("timerfd_create");
519
a279595b 520 if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1)
45b81c9c
MK
521 handle_error("timerfd_settime");
522
523 print_elapsed_time();
524 printf("timer started\\n");
525
526 for (tot_exp = 0; tot_exp < max_exp;) {
527 s = read(fd, &exp, sizeof(uint64_t));
528 if (s != sizeof(uint64_t))
529 handle_error("read");
530
531 tot_exp += exp;
532 print_elapsed_time();
533 printf("read: %llu; total=%llu\\n",
534 (unsigned long long) exp,
535 (unsigned long long) tot_exp);
536 }
537
538 exit(EXIT_SUCCESS);
539}
540.fi
47297adb 541.SH SEE ALSO
45b81c9c
MK
542.BR eventfd (2),
543.BR poll (2),
544.BR read (2),
545.BR select (2),
546.BR setitimer (2),
547.BR signalfd (2),
804f03e6
MK
548.BR timer_create (2),
549.BR timer_gettime (2),
550.BR timer_settime (2),
45b81c9c
MK
551.BR epoll (7),
552.BR time (7)