Commit | Line | Data |
---|---|---|
77c76070 | 1 | #include <sys/select.h> |
bec93767 | 2 | #include <sys/stat.h> |
4d66492f | 3 | |
9b5ebc12 SK |
4 | #include <assert.h> |
5 | #include <ctype.h> | |
77c76070 | 6 | #include <errno.h> |
c5d1af8c | 7 | #include <fcntl.h> |
9b5ebc12 SK |
8 | #include <stdio.h> |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
b7487ec5 | 11 | #include <time.h> |
c5d1af8c SK |
12 | #include <unistd.h> |
13 | ||
14 | #include <X11/Xlib.h> | |
9b5ebc12 | 15 | |
b7487ec5 | 16 | #include "bsdtimespec.h" |
17a27e48 SK |
17 | #include "khatus_lib_log.h" |
18 | #include "khatus_lib_time.h" | |
b7487ec5 | 19 | |
17a27e48 | 20 | #define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);} |
e6c523cd SK |
21 | #define ERRMSG "ERROR" |
22 | ||
23 | static const char errmsg[] = ERRMSG; | |
24 | static const int errlen = sizeof(ERRMSG) - 1; | |
25 | ||
4d66492f | 26 | char *argv0; |
9b5ebc12 | 27 | |
1872c5c1 SK |
28 | /* TODO: Convert fifo list to fifo array. */ |
29 | typedef struct Fifo Fifo; | |
30 | struct Fifo { | |
4d66492f SK |
31 | char *name; |
32 | int fd; | |
9b5ebc12 | 33 | int width; |
4d66492f | 34 | int last_read; |
9b5ebc12 | 35 | int ttl; |
efa97b71 SK |
36 | int pos_init; /* Initial position on the output buffer. */ |
37 | int pos_curr; /* Current position on the output buffer. */ | |
38 | int pos_final; /* Final position on the output buffer. */ | |
1872c5c1 | 39 | Fifo *next; |
9b5ebc12 SK |
40 | }; |
41 | ||
4d66492f SK |
42 | typedef struct Config Config; |
43 | struct Config { | |
348d5f36 | 44 | double interval; |
4d66492f | 45 | char * separator; |
1872c5c1 SK |
46 | Fifo * fifos; |
47 | int fifo_count; | |
4d66492f | 48 | int total_width; |
fabb8771 | 49 | int output_to_x_root_window; |
4d66492f | 50 | } defaults = { |
348d5f36 | 51 | .interval = 1.0, |
4d66492f | 52 | .separator = "|", |
1872c5c1 SK |
53 | .fifos = NULL, |
54 | .fifo_count = 0, | |
4d66492f | 55 | .total_width = 0, |
fabb8771 | 56 | .output_to_x_root_window = 0, |
9b5ebc12 SK |
57 | }; |
58 | ||
efa97b71 SK |
59 | enum read_status { |
60 | FAIL_FINAL = -2, | |
61 | FAIL_TMP = -1, | |
62 | END_OF_FILE = 0, | |
63 | END_OF_MESSAGE = 1 | |
64 | }; | |
65 | ||
77c76070 | 66 | void |
17a27e48 | 67 | fifo_print_one(Fifo *f) |
77c76070 | 68 | { |
a6b13fa2 SK |
69 | info("Fifo " |
70 | "{" | |
71 | " name = %s," | |
72 | " fd = %d," | |
73 | " width = %d," | |
74 | " last_read = %d," | |
75 | " ttl = %d," | |
efa97b71 SK |
76 | " pos_init = %d," |
77 | " pos_curr = %d," | |
78 | " pos_final = %d," | |
a6b13fa2 SK |
79 | " next = %p," |
80 | " }\n", | |
81 | f->name, | |
82 | f->fd, | |
83 | f->width, | |
84 | f->last_read, | |
85 | f->ttl, | |
efa97b71 SK |
86 | f->pos_init, |
87 | f->pos_curr, | |
88 | f->pos_final, | |
a6b13fa2 | 89 | f->next |
77c76070 SK |
90 | ); |
91 | } | |
92 | ||
93 | void | |
17a27e48 | 94 | fifo_print_all(Fifo *head) |
77c76070 | 95 | { |
1872c5c1 | 96 | for (Fifo *f = head; f; f = f->next) { |
17a27e48 | 97 | fifo_print_one(f); |
77c76070 SK |
98 | } |
99 | } | |
100 | ||
101 | void | |
b6316e94 | 102 | config_print(Config *cfg) |
77c76070 | 103 | { |
b6316e94 | 104 | info( |
a6b13fa2 SK |
105 | "Config " |
106 | "{" | |
348d5f36 | 107 | " interval = %f," |
a6b13fa2 SK |
108 | " separator = %s," |
109 | " fifo_count = %d," | |
110 | " total_width = %d," | |
a6b13fa2 SK |
111 | " fifos = ..." |
112 | " }\n", | |
113 | cfg->interval, | |
114 | cfg->separator, | |
115 | cfg->fifo_count, | |
17a27e48 | 116 | cfg->total_width |
77c76070 | 117 | ); |
17a27e48 | 118 | fifo_print_all(cfg->fifos); |
77c76070 | 119 | } |
9b5ebc12 SK |
120 | |
121 | int | |
122 | is_pos_num(char *s) | |
123 | { | |
124 | while (*s != '\0') | |
125 | if (!isdigit(*(s++))) | |
126 | return 0; | |
127 | return 1; | |
128 | } | |
129 | ||
348d5f36 SK |
130 | int |
131 | is_decimal(char *s) | |
132 | { | |
133 | char c; | |
134 | int seen = 0; | |
135 | ||
136 | while ((c = *(s++)) != '\0') | |
137 | if (!isdigit(c)) { | |
138 | if (c == '.' && !seen++) | |
139 | continue; | |
140 | else | |
141 | return 0; | |
142 | } | |
143 | return 1; | |
144 | } | |
145 | ||
9b5ebc12 SK |
146 | void |
147 | print_usage() | |
148 | { | |
4d66492f | 149 | assert(argv0); |
9b5ebc12 | 150 | fprintf( |
a6b13fa2 SK |
151 | stderr, |
152 | "\n" | |
153 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" | |
154 | "\n" | |
155 | " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" | |
156 | " FILE_PATH = string\n" | |
157 | " DATA_WIDTH = int (* (positive) number of characters *)\n" | |
158 | " DATA_TTL = int (* (positive) number of seconds *)\n" | |
159 | " OPTION = -i INTERVAL\n" | |
160 | " | -s SEPARATOR\n" | |
161 | " | -x (* Output to X root window *)\n" | |
162 | " | -l LOG_LEVEL\n" | |
163 | " SEPARATOR = string\n" | |
164 | " INTERVAL = int (* (positive) number of seconds *)\n" | |
165 | " LOG_LEVEL = int (* %d through %d *)\n" | |
166 | "\n", | |
167 | argv0, | |
168 | Nothing, | |
169 | Debug | |
9b5ebc12 SK |
170 | ); |
171 | fprintf( | |
a6b13fa2 SK |
172 | stderr, |
173 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" | |
174 | "\n", | |
175 | argv0 | |
9b5ebc12 SK |
176 | ); |
177 | } | |
178 | ||
4d66492f | 179 | void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */ |
9b5ebc12 SK |
180 | |
181 | void | |
4d66492f | 182 | parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 | 183 | { |
4c438cef SK |
184 | char *param; |
185 | ||
186 | if (i >= argc) | |
9b5ebc12 | 187 | usage("Option -i parameter is missing.\n"); |
4c438cef | 188 | param = argv[i++]; |
348d5f36 | 189 | if (!is_decimal(param)) |
4c438cef | 190 | usage("Option -i parameter is invalid: \"%s\"\n", param); |
348d5f36 | 191 | cfg->interval = atof(param); |
4c438cef | 192 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
193 | } |
194 | ||
195 | void | |
4d66492f SK |
196 | parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) |
197 | { | |
4c438cef | 198 | if (i >= argc) |
4d66492f | 199 | usage("Option -s parameter is missing.\n"); |
4c438cef SK |
200 | cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); |
201 | strcpy(cfg->separator, argv[i]); | |
202 | opts_parse_any(cfg, argc, argv, ++i); | |
4d66492f SK |
203 | } |
204 | ||
b6316e94 SK |
205 | void |
206 | parse_opts_opt_l(Config *cfg, int argc, char *argv[], int i) | |
207 | { | |
4c438cef | 208 | char *param; |
b6316e94 SK |
209 | int log_level; |
210 | ||
4c438cef | 211 | if (i >= argc) |
b6316e94 | 212 | usage("Option -l parameter is missing.\n"); |
4c438cef SK |
213 | param = argv[i++]; |
214 | if (!is_pos_num(param)) | |
215 | usage("Option -l parameter is invalid: \"%s\"\n", param); | |
216 | log_level = atoi(param); | |
217 | if (log_level > Debug) | |
218 | usage("Option -l value (%d) exceeds maximum (%d)\n", log_level, Debug); | |
17a27e48 | 219 | _khatus_lib_log_level = log_level; |
4c438cef | 220 | opts_parse_any(cfg, argc, argv, i); |
b6316e94 SK |
221 | } |
222 | ||
4d66492f SK |
223 | void |
224 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) | |
9b5ebc12 SK |
225 | { |
226 | switch (argv[i][1]) { | |
ce552549 SK |
227 | case 'i': |
228 | /* TODO: Generic set_int */ | |
229 | parse_opts_opt_i(cfg, argc, argv, ++i); | |
230 | break; | |
231 | case 's': | |
232 | /* TODO: Generic set_str */ | |
233 | parse_opts_opt_s(cfg, argc, argv, ++i); | |
234 | break; | |
235 | case 'x': | |
236 | cfg->output_to_x_root_window = 1; | |
237 | opts_parse_any(cfg, argc, argv, ++i); | |
238 | break; | |
239 | case 'l': | |
240 | /* TODO: Generic set_int */ | |
241 | parse_opts_opt_l(cfg, argc, argv, ++i); | |
242 | break; | |
243 | default : | |
244 | usage("Option \"%s\" is invalid\n", argv[i]); | |
9b5ebc12 SK |
245 | } |
246 | } | |
247 | ||
248 | void | |
4d66492f | 249 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
250 | { |
251 | if ((i + 3) > argc) | |
1872c5c1 | 252 | usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv[i]); |
9b5ebc12 SK |
253 | |
254 | char *n = argv[i++]; | |
255 | char *w = argv[i++]; | |
256 | char *t = argv[i++]; | |
257 | ||
258 | if (!is_pos_num(w)) | |
1872c5c1 | 259 | usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w, n); |
9b5ebc12 | 260 | if (!is_pos_num(t)) |
1872c5c1 SK |
261 | usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t, n); |
262 | Fifo *f = calloc(1, sizeof(struct Fifo)); | |
9b5ebc12 | 263 | if (f) { |
4d66492f SK |
264 | f->name = n; |
265 | f->fd = -1; | |
266 | f->width = atoi(w); | |
267 | f->ttl = atoi(t); | |
268 | f->last_read = 0; | |
efa97b71 SK |
269 | f->pos_init = cfg->total_width; |
270 | f->pos_curr = f->pos_init; | |
271 | f->pos_final = f->pos_init + f->width - 1; | |
1872c5c1 | 272 | f->next = cfg->fifos; |
4d66492f | 273 | |
1872c5c1 | 274 | cfg->fifos = f; |
4d66492f | 275 | cfg->total_width += f->width; |
1872c5c1 | 276 | cfg->fifo_count++; |
9b5ebc12 | 277 | } else { |
4d66492f | 278 | fatal("[memory] Allocation failure."); |
9b5ebc12 | 279 | } |
4d66492f | 280 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
281 | } |
282 | ||
283 | void | |
4d66492f | 284 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
285 | { |
286 | if (i < argc) { | |
287 | switch (argv[i][0]) { | |
ce552549 SK |
288 | case '-': |
289 | parse_opts_opt(cfg, argc, argv, i); | |
290 | break; | |
291 | default : | |
292 | parse_opts_spec(cfg, argc, argv, i); | |
4d66492f SK |
293 | } |
294 | } | |
295 | } | |
296 | ||
297 | void | |
5400b86f | 298 | opts_parse(Config *cfg, int argc, char *argv[]) |
4d66492f SK |
299 | { |
300 | opts_parse_any(cfg, argc, argv, 1); | |
301 | ||
1872c5c1 SK |
302 | Fifo *last = cfg->fifos; |
303 | cfg->fifos = NULL; | |
304 | for (Fifo *f = last; f; ) { | |
305 | Fifo *next = f->next; | |
306 | f->next = cfg->fifos; | |
307 | cfg->fifos = f; | |
4d66492f SK |
308 | f = next; |
309 | } | |
310 | } | |
311 | ||
e6c523cd | 312 | void |
69b75a40 | 313 | fifo_read_error(Fifo *f, char *buf) |
e6c523cd SK |
314 | { |
315 | char *b; | |
316 | int i; | |
317 | ||
efa97b71 | 318 | b = buf + f->pos_init; |
e6c523cd SK |
319 | /* Copy as much of the error message as possible. |
320 | * EXCLUDING the reminating \0. */ | |
321 | for (i = 0; i < errlen && i < f->width; i++) | |
322 | b[i] = errmsg[i]; | |
323 | /* Any remaining slots: */ | |
324 | for (; i < f->width; i++) | |
325 | b[i] = '_'; | |
326 | } | |
327 | ||
efa97b71 | 328 | enum read_status |
17a27e48 | 329 | fifo_read_one(Fifo *f, char *buf) |
77c76070 | 330 | { |
efa97b71 SK |
331 | /* Initialize all to an impossible value: */ |
332 | ssize_t n = -5; /* Number of bytes read. */ | |
333 | char c = -1; /* Character read. */ | |
334 | int r = -1; /* Remaining unused slots in buffer range. */ | |
77c76070 | 335 | |
efa97b71 SK |
336 | for (;;) { |
337 | n = read(f->fd, &c, 1); | |
338 | assert(n >= -1 && n <= 1); | |
339 | switch (n) { | |
340 | case -1: | |
341 | error("Failed to read: \"%s\". errno: %d, msg: %s\n", | |
342 | f->name, errno, strerror(errno)); | |
343 | if (errno == 11) | |
344 | return FAIL_TMP; | |
345 | else | |
346 | return FAIL_FINAL; | |
347 | case 0: | |
348 | debug("%s: End of FILE\n", f->name); | |
349 | f->pos_curr = f->pos_init; | |
350 | return END_OF_FILE; | |
351 | case 1: | |
352 | /* TODO: Consider making msg term char a CLI option */ | |
353 | if (c == '\n' || c == '\0') { | |
354 | r = f->pos_final - f->pos_curr; | |
355 | if (r > 0) | |
356 | memset(buf + f->pos_curr, ' ', r); | |
357 | f->pos_curr = f->pos_init; | |
358 | return END_OF_MESSAGE; | |
359 | } else { | |
360 | if (f->pos_curr <= f->pos_final) | |
361 | buf[f->pos_curr++] = c; | |
362 | /* Drop beyond available range. */ | |
363 | } | |
364 | break; | |
365 | default: | |
366 | assert(0); | |
367 | } | |
574a4bff | 368 | } |
6c29392d | 369 | /* TODO Record timestamp read */ |
77c76070 SK |
370 | } |
371 | ||
4d66492f | 372 | void |
69b75a40 | 373 | fifo_read_all(Config *cfg, char *buf) |
4d66492f | 374 | { |
77c76070 | 375 | fd_set fds; |
bfab34f8 | 376 | int maxfd = -1; |
77c76070 | 377 | int ready; |
bec93767 | 378 | struct stat st; |
77c76070 SK |
379 | |
380 | FD_ZERO(&fds); | |
1872c5c1 | 381 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
bec93767 | 382 | /* TODO: Create the FIFO if it doesn't already exist. */ |
e6c523cd SK |
383 | if (lstat(f->name, &st) < 0) { |
384 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
69b75a40 | 385 | fifo_read_error(f, buf); |
e6c523cd SK |
386 | continue; |
387 | } | |
388 | if (!(st.st_mode & S_IFIFO)) { | |
389 | error("\"%s\" is not a FIFO\n", f->name); | |
69b75a40 | 390 | fifo_read_error(f, buf); |
e6c523cd SK |
391 | continue; |
392 | } | |
efa97b71 SK |
393 | if (f->fd < 0) { |
394 | debug("%s: closed. opening. fd: %d\n", f->name, f->fd); | |
77c76070 | 395 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
efa97b71 SK |
396 | } else { |
397 | debug("%s: already openned. fd: %d\n", f->name, f->fd); | |
398 | } | |
e6c523cd | 399 | if (f->fd == -1) { |
1872c5c1 | 400 | /* TODO: Consider backing off retries for failed fifos. */ |
e6c523cd | 401 | error("Failed to open \"%s\"\n", f->name); |
69b75a40 | 402 | fifo_read_error(f, buf); |
e6c523cd SK |
403 | continue; |
404 | } | |
efa97b71 | 405 | debug("%s: open. fd: %d\n", f->name, f->fd); |
77c76070 SK |
406 | if (f->fd > maxfd) |
407 | maxfd = f->fd; | |
408 | FD_SET(f->fd, &fds); | |
409 | } | |
410 | debug("selecting...\n"); | |
411 | ready = select(maxfd + 1, &fds, NULL, NULL, NULL); | |
412 | debug("ready: %d\n", ready); | |
413 | assert(ready != 0); | |
414 | if (ready < 0) | |
415 | fatal("%s", strerror(errno)); | |
1872c5c1 | 416 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
77c76070 SK |
417 | if (FD_ISSET(f->fd, &fds)) { |
418 | debug("reading: %s\n", f->name); | |
efa97b71 SK |
419 | switch (fifo_read_one(f, buf)) { |
420 | case END_OF_FILE: | |
421 | case FAIL_FINAL: | |
422 | close(f->fd); | |
423 | f->fd = -1; | |
424 | break; | |
425 | case END_OF_MESSAGE: | |
426 | case FAIL_TMP: | |
427 | break; | |
428 | default: | |
429 | assert(0); | |
430 | } | |
b7487ec5 SK |
431 | } |
432 | } | |
433 | } | |
434 | ||
9b5ebc12 | 435 | int |
4d66492f | 436 | main(int argc, char *argv[]) |
9b5ebc12 | 437 | { |
f277f405 | 438 | int width = 0; |
1872c5c1 | 439 | int nfifos = 0; |
f277f405 | 440 | int seplen = 0; |
4d66492f | 441 | int prefix = 0; |
e6c523cd | 442 | int errors = 0; |
4d66492f | 443 | char *buf; |
a6d4c9c3 SK |
444 | Config cfg0 = defaults; |
445 | Config *cfg = &cfg0; | |
bfab34f8 | 446 | Display *display = NULL; |
e6c523cd | 447 | struct stat st; |
b7487ec5 SK |
448 | struct timespec |
449 | t0, /* time stamp. before reading fifos */ | |
450 | t1, /* time stamp. after reading fifos */ | |
451 | ti, /* time interval desired (t1 - t0) */ | |
452 | td, /* time interval measured (t1 - t0) */ | |
453 | tc; /* time interval correction (ti - td) when td < ti */ | |
4d66492f SK |
454 | |
455 | argv0 = argv[0]; | |
456 | ||
5400b86f | 457 | opts_parse(cfg, argc, argv); |
4d66492f | 458 | debug("argv0 = %s\n", argv0); |
77c76070 | 459 | config_print(cfg); |
b7487ec5 | 460 | |
348d5f36 | 461 | ti = timespec_of_float(cfg->interval); |
b7487ec5 | 462 | |
1872c5c1 SK |
463 | if (cfg->fifos == NULL) |
464 | usage("No fifo specs were given!\n"); | |
4d66492f | 465 | |
e6c523cd SK |
466 | /* 1st pass to check file existence and type */ |
467 | for (Fifo *f = cfg->fifos; f; f = f->next) { | |
468 | if (lstat(f->name, &st) < 0) { | |
469 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
470 | errors++; | |
471 | continue; | |
472 | } | |
473 | if (!(st.st_mode & S_IFIFO)) { | |
474 | error("\"%s\" is not a FIFO\n", f->name); | |
475 | errors++; | |
476 | continue; | |
477 | } | |
478 | } | |
479 | if (errors) | |
480 | fatal("Encountered errors with the given file paths. See log.\n"); | |
481 | ||
4d66492f SK |
482 | width = cfg->total_width; |
483 | seplen = strlen(cfg->separator); | |
484 | ||
e6c523cd | 485 | /* 2nd pass to make space for separators */ |
1872c5c1 | 486 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
efa97b71 SK |
487 | f->pos_init += prefix; |
488 | f->pos_final += prefix; | |
489 | f->pos_curr = f->pos_init; | |
4d66492f | 490 | prefix += seplen; |
1872c5c1 | 491 | nfifos++; |
4d66492f | 492 | } |
1872c5c1 | 493 | width += (seplen * (nfifos - 1)); |
3c836bfd | 494 | buf = calloc(1, width + 1); |
4d66492f SK |
495 | if (buf == NULL) |
496 | fatal("[memory] Failed to allocate buffer of %d bytes", width); | |
497 | memset(buf, ' ', width); | |
498 | buf[width] = '\0'; | |
e6c523cd | 499 | /* 3rd pass to set the separators */ |
1872c5c1 | 500 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
efa97b71 | 501 | if (f->pos_init) { /* Skip the first, left-most */ |
77c76070 | 502 | /* Copying only seplen ensures we omit the '\0' byte. */ |
efa97b71 | 503 | strncpy(buf + (f->pos_init - seplen), cfg->separator, seplen); |
4d66492f SK |
504 | } |
505 | } | |
506 | ||
fabb8771 SK |
507 | if (cfg->output_to_x_root_window && !(display = XOpenDisplay(NULL))) |
508 | fatal("XOpenDisplay failed with: %p\n", display); | |
805f0d22 | 509 | /* TODO: Handle signals */ |
4d66492f | 510 | for (;;) { |
b7487ec5 SK |
511 | clock_gettime(CLOCK_MONOTONIC, &t0); // FIXME: check errors |
512 | /* TODO: Cache expiration. i.e. use the TTL */ | |
6c29392d | 513 | /* TODO: How to trigger TTL check? On select? Alarm signal? */ |
69b75a40 SK |
514 | /* TODO: Set timeout on fifo_read_all based on diff of last time of |
515 | * fifo_read_all and desired time of next TTL check. | |
b7487ec5 SK |
516 | */ |
517 | /* TODO: How long to wait on IO? Max TTL? */ | |
69b75a40 | 518 | fifo_read_all(cfg, buf); |
fabb8771 SK |
519 | if (cfg->output_to_x_root_window) { |
520 | if (XStoreName(display, DefaultRootWindow(display), buf) < 0) | |
521 | fatal("XStoreName failed.\n"); | |
522 | XFlush(display); | |
523 | } else { | |
524 | puts(buf); | |
525 | fflush(stdout); | |
526 | } | |
b7487ec5 SK |
527 | clock_gettime(CLOCK_MONOTONIC, &t1); // FIXME: check errors |
528 | timespecsub(&t1, &t0, &td); | |
529 | debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td.tv_sec, td.tv_nsec); | |
530 | if (timespeccmp(&td, &ti, <)) { | |
531 | /* Pushback on data producers by refusing to read the | |
532 | * pipe more frequently than the interval. | |
533 | */ | |
534 | timespecsub(&ti, &td, &tc); | |
535 | debug("snooze YES\n"); | |
17a27e48 | 536 | snooze(&tc); |
574a4bff | 537 | } else { |
b7487ec5 | 538 | debug("snooze NO\n"); |
574a4bff | 539 | } |
4d66492f | 540 | } |
3d7e82a8 SK |
541 | |
542 | return EXIT_SUCCESS; | |
9b5ebc12 | 543 | } |