Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / memcheck / tests / freebsd / timerfd.c
blob7ed964cc9b901b45183b0ff593f472ca81aae94d
1 /* Copied from the Linux manpage
2 * with the printed times rounded to
3 * seconds for reproducibility.
4 * And some errors added.
5 */
7 #include <err.h>
8 #include <inttypes.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/timerfd.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include <math.h>
15 #include <errno.h>
16 #include <assert.h>
17 #include "../../memcheck.h"
19 static void
20 print_elapsed_time(void)
22 int secs, nsecs;
23 static int first_call = 1;
24 struct timespec curr;
25 static struct timespec start;
26 int round_secs;
28 if (first_call) {
29 first_call = 0;
30 if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)
31 err(EXIT_FAILURE, "clock_gettime");
34 if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1)
35 err(EXIT_FAILURE, "clock_gettime");
37 secs = curr.tv_sec - start.tv_sec;
38 nsecs = curr.tv_nsec - start.tv_nsec;
39 if (nsecs < 0) {
40 secs--;
41 nsecs += 1000000000;
43 round_secs = round((secs*1e9 + nsecs)/1e9);
44 printf("%d: ", round_secs);
47 int
48 main(int argc, char *argv[])
50 int fd;
51 ssize_t s;
52 uint64_t exp, tot_exp, max_exp;
53 struct timespec now;
54 struct itimerspec new_value;
56 if (argc != 2 && argc != 4) {
57 fprintf(stderr, "%s init-secs [interval-secs max-exp]\n",
58 argv[0]);
59 exit(EXIT_FAILURE);
62 if (clock_gettime(CLOCK_REALTIME, &now) == -1)
63 err(EXIT_FAILURE, "clock_gettime");
65 /* Create a CLOCK_REALTIME absolute timer with initial
66 expiration and interval as specified in command line. */
68 new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
69 new_value.it_value.tv_nsec = now.tv_nsec;
70 if (argc == 2) {
71 new_value.it_interval.tv_sec = 0;
72 max_exp = 1;
73 } else {
74 new_value.it_interval.tv_sec = atoi(argv[2]);
75 max_exp = atoi(argv[3]);
77 new_value.it_interval.tv_nsec = 0;
79 fd = timerfd_create(CLOCK_REALTIME, 0);
80 if (fd == -1)
81 err(EXIT_FAILURE, "timerfd_create");
83 if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)
84 err(EXIT_FAILURE, "timerfd_settime");
86 print_elapsed_time();
87 printf("timer started\n");
89 for (tot_exp = 0; tot_exp < max_exp;) {
90 s = read(fd, &exp, sizeof(uint64_t));
91 if (s != sizeof(uint64_t))
92 err(EXIT_FAILURE, "read");
94 tot_exp += exp;
95 print_elapsed_time();
96 printf("read: %" PRIu64 "; total=%" PRIu64 "\n", exp, tot_exp);
99 close(fd);
102 int a = CLOCK_REALTIME;
103 int b = TFD_CLOEXEC;
104 int c = TFD_TIMER_ABSTIME;
105 VALGRIND_MAKE_MEM_UNDEFINED(&a, sizeof(a));
106 VALGRIND_MAKE_MEM_UNDEFINED(&b, sizeof(b));
107 VALGRIND_MAKE_MEM_UNDEFINED(&c, sizeof(c));
108 struct itimerspec * get_ts = malloc(sizeof(*get_ts) - 2);
109 struct itimerspec * set_ts = malloc(sizeof(*set_ts) - 2);
110 struct itimerspec ts;
111 int retval;
113 errno = 0;
114 /* uninit clockid and flag but should work */
115 int fd2 = timerfd_create(a, b);
116 assert(fd2 != -1);
117 assert(errno == 0);
118 /* bad flag should fail */
119 timerfd_create(CLOCK_REALTIME, 1000000);
120 /* bad clockid should fail */
121 timerfd_create(1000000, TFD_CLOEXEC);
123 /* memory too small for requested get */
124 timerfd_gettime(fd2, get_ts);
125 /* should succeed */
126 timerfd_gettime(fd2, &ts);
127 ts.it_interval.tv_nsec += 100000;
129 /* uninit flag */
130 timerfd_settime(fd2, c, &ts, NULL);
131 errno = 0;
132 ts.it_interval.tv_nsec += 100000;
133 /* memory too small for requested old value */
134 retval = timerfd_settime(fd2, TFD_TIMER_ABSTIME, &ts, set_ts);
135 assert(retval == 0);
136 assert(errno == 0);
138 VALGRIND_MAKE_MEM_UNDEFINED(&fd2, sizeof(fd2));
139 timerfd_gettime(fd2, &ts);
140 ts.it_interval.tv_nsec += 100000;
141 timerfd_settime(fd2, TFD_TIMER_ABSTIME, &ts, NULL);
144 free(get_ts);
145 free(set_ts);
148 exit(EXIT_SUCCESS);