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