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