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