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