1 | // RUN: %clangxx -O0 -g %s -o %t && %run %t | FileCheck %s |
2 | #include <assert.h> |
3 | #include <stdio.h> |
4 | #include <string.h> |
5 | #include <time.h> |
6 | #include <sys/timerfd.h> |
7 | #include <unistd.h> |
8 | |
9 | int main (int argc, char** argv) |
10 | { |
11 | int fd = timerfd_create(CLOCK_REALTIME, flags: 0); |
12 | assert(fd >= 0); |
13 | |
14 | struct itimerspec its; |
15 | its.it_value.tv_sec = 0; |
16 | its.it_value.tv_nsec = 1000000; |
17 | its.it_interval.tv_sec = its.it_value.tv_sec; |
18 | its.it_interval.tv_nsec = its.it_value.tv_nsec; |
19 | |
20 | int res = timerfd_settime(ufd: fd, flags: 0, utmr: &its, NULL); |
21 | assert(res != -1); |
22 | |
23 | struct itimerspec its2; |
24 | res = timerfd_settime(ufd: fd, flags: 0, utmr: &its, otmr: &its2); |
25 | assert(res != -1); |
26 | assert(its2.it_interval.tv_sec == its.it_interval.tv_sec); |
27 | assert(its2.it_interval.tv_nsec == its.it_interval.tv_nsec); |
28 | assert(its2.it_value.tv_sec <= its.it_value.tv_sec); |
29 | assert(its2.it_value.tv_nsec <= its.it_value.tv_nsec); |
30 | |
31 | struct itimerspec its3; |
32 | res = timerfd_gettime(ufd: fd, otmr: &its3); |
33 | assert(res != -1); |
34 | assert(its3.it_interval.tv_sec == its.it_interval.tv_sec); |
35 | assert(its3.it_interval.tv_nsec == its.it_interval.tv_nsec); |
36 | assert(its3.it_value.tv_sec <= its.it_value.tv_sec); |
37 | assert(its3.it_value.tv_nsec <= its.it_value.tv_nsec); |
38 | |
39 | |
40 | unsigned long long buf; |
41 | res = read(fd: fd, buf: &buf, nbytes: sizeof(buf)); |
42 | assert(res == 8); |
43 | assert(buf >= 1); |
44 | |
45 | res = close(fd: fd); |
46 | assert(res != -1); |
47 | |
48 | printf(format: "DONE\n" ); |
49 | // CHECK: DONE |
50 | |
51 | return 0; |
52 | } |
53 | |