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