5ba94d34669e46f34c72229cbe24b475b71245ad
[khatus.git] / x5 / khatus.c
1 #include <sys/select.h>
2 #include <sys/stat.h>
3
4 #include <assert.h>
5 #include <ctype.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <unistd.h>
13
14 #include <X11/Xlib.h>
15
16 #include "bsdtimespec.h"
17 #include "khlib_log.h"
18 #include "khlib_time.h"
19
20 #define usage(...) { \
21 print_usage(); \
22 fprintf(stderr, "Error:\n " __VA_ARGS__); \
23 exit(EXIT_FAILURE); \
24 }
25 #define ERRMSG "ERROR"
26
27 static const char errmsg[] = ERRMSG;
28 static const int errlen = sizeof(ERRMSG) - 1;
29
30 char *argv0;
31
32 /* TODO: Convert fifo list to fifo array. */
33 typedef struct Fifo Fifo;
34 struct Fifo {
35 char *name;
36 int fd;
37 int width;
38 struct timespec last_read;
39 struct timespec ttl;
40 int pos_init; /* Initial position on the output buffer. */
41 int pos_curr; /* Current position on the output buffer. */
42 int pos_final; /* Final position on the output buffer. */
43 Fifo *next;
44 };
45
46 typedef struct Config Config;
47 struct Config {
48 double interval;
49 char * separator;
50 Fifo * fifos;
51 int fifo_count;
52 int total_width;
53 int output_to_x_root_window;
54 };
55
56 enum read_status {
57 END_OF_FILE,
58 END_OF_MESSAGE,
59 RETRY,
60 FAILURE
61 };
62
63 void
64 fifo_print_one(Fifo *f)
65 {
66 khlib_info("Fifo "
67 "{"
68 " name = %s,"
69 " fd = %d,"
70 " width = %d,"
71 " last_read = {tv_sec = %ld, tv_nsec = %ld}"
72 " ttl = {tv_sec = %ld, tv_nsec = %ld},"
73 " pos_init = %d,"
74 " pos_curr = %d,"
75 " pos_final = %d,"
76 " next = %p,"
77 " }\n",
78 f->name,
79 f->fd,
80 f->width,
81 f->last_read.tv_sec,
82 f->last_read.tv_nsec,
83 f->ttl.tv_sec,
84 f->ttl.tv_nsec,
85 f->pos_init,
86 f->pos_curr,
87 f->pos_final,
88 f->next
89 );
90 }
91
92 void
93 fifo_print_all(Fifo *head)
94 {
95 for (Fifo *f = head; f; f = f->next) {
96 fifo_print_one(f);
97 }
98 }
99
100 void
101 config_print(Config *cfg)
102 {
103 khlib_info(
104 "Config "
105 "{"
106 " interval = %f,"
107 " separator = %s,"
108 " fifo_count = %d,"
109 " total_width = %d,"
110 " fifos = ..."
111 " }\n",
112 cfg->interval,
113 cfg->separator,
114 cfg->fifo_count,
115 cfg->total_width
116 );
117 fifo_print_all(cfg->fifos);
118 }
119
120 int
121 is_pos_num(char *s)
122 {
123 while (*s != '\0')
124 if (!isdigit(*(s++)))
125 return 0;
126 return 1;
127 }
128
129 int
130 is_decimal(char *s)
131 {
132 char c;
133 int seen = 0;
134
135 while ((c = *(s++)) != '\0')
136 if (!isdigit(c)) {
137 if (c == '.' && !seen++)
138 continue;
139 else
140 return 0;
141 }
142 return 1;
143 }
144
145 void
146 print_usage()
147 {
148 assert(argv0);
149 fprintf(
150 stderr,
151 "\n"
152 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
153 "\n"
154 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
155 " FILE_PATH = string\n"
156 " DATA_WIDTH = int (* (positive) number of characters *)\n"
157 " DATA_TTL = float (* (positive) number of seconds *)\n"
158 " OPTION = -i INTERVAL\n"
159 " | -s SEPARATOR\n"
160 " | -x (* Output to X root window *)\n"
161 " | -l LOG_LEVEL\n"
162 " SEPARATOR = string\n"
163 " INTERVAL = float (* (positive) number of seconds *)\n"
164 " LOG_LEVEL = int (* %d through %d *)\n"
165 "\n",
166 argv0,
167 Nothing,
168 Debug
169 );
170 fprintf(
171 stderr,
172 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
173 "\n",
174 argv0
175 );
176 }
177
178 /* For mutually-recursive calls. */
179 void opts_parse_any(Config *, int, char *[], int);
180
181 void
182 parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i)
183 {
184 char *param;
185
186 if (i >= argc)
187 usage("Option -i parameter is missing.\n");
188 param = argv[i++];
189 if (!is_decimal(param))
190 usage("Option -i parameter is invalid: \"%s\"\n", param);
191 cfg->interval = atof(param);
192 opts_parse_any(cfg, argc, argv, i);
193 }
194
195 void
196 parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i)
197 {
198 if (i >= argc)
199 usage("Option -s parameter is missing.\n");
200 cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char));
201 strcpy(cfg->separator, argv[i]);
202 opts_parse_any(cfg, argc, argv, ++i);
203 }
204
205 void
206 parse_opts_opt_l(Config *cfg, int argc, char *argv[], int i)
207 {
208 char *param;
209 int log_level;
210
211 if (i >= argc)
212 usage("Option -l parameter is missing.\n");
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(
219 "Option -l value (%d) exceeds maximum (%d)\n",
220 log_level,
221 Debug
222 );
223 _khlib_log_level = log_level;
224 opts_parse_any(cfg, argc, argv, i);
225 }
226
227 void
228 parse_opts_opt(Config *cfg, int argc, char *argv[], int i)
229 {
230 switch (argv[i][1]) {
231 case 'i':
232 /* TODO: Generic set_int */
233 parse_opts_opt_i(cfg, argc, argv, ++i);
234 break;
235 case 's':
236 /* TODO: Generic set_str */
237 parse_opts_opt_s(cfg, argc, argv, ++i);
238 break;
239 case 'x':
240 cfg->output_to_x_root_window = 1;
241 opts_parse_any(cfg, argc, argv, ++i);
242 break;
243 case 'l':
244 /* TODO: Generic set_int */
245 parse_opts_opt_l(cfg, argc, argv, ++i);
246 break;
247 default :
248 usage("Option \"%s\" is invalid\n", argv[i]);
249 }
250 }
251
252 void
253 parse_opts_spec(Config *cfg, int argc, char *argv[], int i)
254 {
255 if ((i + 3) > argc)
256 usage(
257 "[spec] Parameter(s) missing for fifo \"%s\".\n",
258 argv[i]
259 );
260
261 char *n = argv[i++];
262 char *w = argv[i++];
263 char *t = argv[i++];
264
265 struct timespec last_read;
266
267 if (!is_pos_num(w))
268 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w, n);
269 if (!is_decimal(t))
270 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t, n);
271 last_read.tv_sec = 0;
272 last_read.tv_nsec = 0;
273 Fifo *f = calloc(1, sizeof(struct Fifo));
274 if (f) {
275 f->name = n;
276 f->fd = -1;
277 f->width = atoi(w);
278 f->ttl = khlib_timespec_of_float(atof(t));
279 f->last_read = last_read;
280 f->pos_init = cfg->total_width;
281 f->pos_curr = f->pos_init;
282 f->pos_final = f->pos_init + f->width - 1;
283 f->next = cfg->fifos;
284
285 cfg->fifos = f;
286 cfg->total_width += f->width;
287 cfg->fifo_count++;
288 } else {
289 khlib_fatal("[memory] Allocation failure.");
290 }
291 opts_parse_any(cfg, argc, argv, i);
292 }
293
294 void
295 opts_parse_any(Config *cfg, int argc, char *argv[], int i)
296 {
297 if (i < argc) {
298 switch (argv[i][0]) {
299 case '-':
300 parse_opts_opt(cfg, argc, argv, i);
301 break;
302 default :
303 parse_opts_spec(cfg, argc, argv, i);
304 }
305 }
306 }
307
308 void
309 opts_parse(Config *cfg, int argc, char *argv[])
310 {
311 opts_parse_any(cfg, argc, argv, 1);
312
313 Fifo *last = cfg->fifos;
314 cfg->fifos = NULL;
315 for (Fifo *f = last; f; ) {
316 Fifo *next = f->next;
317 f->next = cfg->fifos;
318 cfg->fifos = f;
319 f = next;
320 }
321 }
322
323 void
324 fifo_expire(Fifo *f, struct timespec t, char *buf)
325 {
326 struct timespec td;
327
328 timespecsub(&t, &(f->last_read), &td);
329 if (timespeccmp(&td, &(f->ttl), >=)) {
330 /* TODO: Maybe configurable expiry character. */
331 memset(buf + f->pos_init, '_', f->pos_final - f->pos_init);
332 khlib_warn("Data source expired: \"%s\"\n", f->name);
333 }
334 }
335
336 void
337 fifo_read_error(Fifo *f, char *buf)
338 {
339 char *b;
340 int i;
341
342 b = buf + f->pos_init;
343 /* Copy as much of the error message as possible.
344 * EXCLUDING the terminating \0. */
345 for (i = 0; i < errlen && i < f->width; i++)
346 b[i] = errmsg[i];
347 /* Any remaining slots: */
348 for (; i < f->width; i++)
349 b[i] = '_';
350 }
351
352 enum read_status
353 fifo_read_one(Fifo *f, struct timespec t, char *buf)
354 {
355 char c; /* Character read. */
356 int r; /* Remaining unused slots in buffer range. */
357
358 for (;;) {
359 switch (read(f->fd, &c, 1)) {
360 case -1:
361 khlib_error(
362 "Failed to read: \"%s\". errno: %d, msg: %s\n",
363 f->name, errno, strerror(errno)
364 );
365 switch (errno) {
366 case EINTR:
367 case EAGAIN:
368 return RETRY;
369 default:
370 return FAILURE;
371 }
372 case 0:
373 khlib_debug("%s: End of FILE\n", f->name);
374 f->pos_curr = f->pos_init;
375 return END_OF_FILE;
376 case 1:
377 /* TODO: Consider making msg term char a CLI option */
378 if (c == '\n' || c == '\0') {
379 r = f->pos_final - f->pos_curr;
380 if (r > 0)
381 memset(buf + f->pos_curr, ' ', r);
382 f->pos_curr = f->pos_init;
383 f->last_read = t;
384 return END_OF_MESSAGE;
385 } else {
386 if (f->pos_curr <= f->pos_final)
387 buf[f->pos_curr++] = c;
388 /* Drop beyond available range. */
389 /*
390 * TODO Define max after which we stop reading.
391 * To ensure that a rogue large message
392 * doesn't trap us here.
393 */
394 }
395 break;
396 default:
397 assert(0);
398 }
399 }
400 }
401
402 void
403 fifo_read_all(Config *cfg, struct timespec *ti, char *buf)
404 {
405 fd_set fds;
406 int maxfd = -1;
407 int ready = 0;
408 struct stat st;
409 struct timespec t;
410
411 FD_ZERO(&fds);
412 for (Fifo *f = cfg->fifos; f; f = f->next) {
413 /* TODO: Create the FIFO if it doesn't already exist. */
414 if (lstat(f->name, &st) < 0) {
415 khlib_error(
416 "Cannot stat \"%s\". Error: %s\n",
417 f->name,
418 strerror(errno)
419 );
420 fifo_read_error(f, buf);
421 continue;
422 }
423 if (!(st.st_mode & S_IFIFO)) {
424 khlib_error("\"%s\" is not a FIFO\n", f->name);
425 fifo_read_error(f, buf);
426 continue;
427 }
428 if (f->fd < 0) {
429 khlib_debug(
430 "%s: closed. opening. fd: %d\n",
431 f->name,
432 f->fd
433 );
434 f->fd = open(f->name, O_RDONLY | O_NONBLOCK);
435 } else {
436 khlib_debug(
437 "%s: already openned. fd: %d\n",
438 f->name,
439 f->fd
440 );
441 }
442 if (f->fd == -1) {
443 /* TODO Consider backing off retries for failed fifos */
444 khlib_error("Failed to open \"%s\"\n", f->name);
445 fifo_read_error(f, buf);
446 continue;
447 }
448 khlib_debug("%s: open. fd: %d\n", f->name, f->fd);
449 if (f->fd > maxfd)
450 maxfd = f->fd;
451 FD_SET(f->fd, &fds);
452 }
453 khlib_debug("selecting...\n");
454 ready = pselect(maxfd + 1, &fds, NULL, NULL, ti, NULL);
455 khlib_debug("ready: %d\n", ready);
456 clock_gettime(CLOCK_MONOTONIC, &t);
457 if (ready == -1) {
458 switch (errno) {
459 case EINTR:
460 khlib_error(
461 "pselect temp failure: %d, errno: %d, msg: %s\n",
462 ready,
463 errno,
464 strerror(errno)
465 );
466 /* TODO: Reconsider what to do here. */
467 return;
468 default:
469 khlib_fatal(
470 "pselect failed: %d, errno: %d, msg: %s\n",
471 ready,
472 errno,
473 strerror(errno)
474 );
475 }
476 }
477 /* At-least-once ensures that expiries are still checked on timeouts. */
478 do {
479 for (Fifo *f = cfg->fifos; f; f = f->next) {
480 if (FD_ISSET(f->fd, &fds)) {
481 khlib_debug("reading: %s\n", f->name);
482 switch (fifo_read_one(f, t, buf)) {
483 /*
484 * ### MESSAGE LOSS ###
485 * is introduced by closing at EOM in addition
486 * to EOF, since there may be unread messages
487 * remaining in the pipe. However,
488 *
489 * ### INTER-MESSAGE PUSHBACK ###
490 * is also gained, since pipes block at the
491 * "open" call.
492 *
493 * This is an acceptable trade-off because we
494 * are a stateless reporter of a _most-recent_
495 * status, not a stateful accumulator.
496 */
497 case END_OF_MESSAGE:
498 case END_OF_FILE:
499 case FAILURE:
500 close(f->fd);
501 f->fd = -1;
502 ready--;
503 break;
504 case RETRY:
505 break;
506 default:
507 assert(0);
508 }
509 } else {
510 fifo_expire(f, t, buf);
511 }
512 }
513 } while (ready);
514 assert(ready == 0);
515 }
516
517 int
518 main(int argc, char *argv[])
519 {
520 Config cfg = {
521 .interval = 1.0,
522 .separator = "|",
523 .fifos = NULL,
524 .fifo_count = 0,
525 .total_width = 0,
526 .output_to_x_root_window = 0,
527 };
528
529 int width = 0;
530 int nfifos = 0;
531 int seplen = 0;
532 int prefix = 0;
533 int errors = 0;
534 char *buf;
535 Display *d = NULL;
536 struct stat st;
537 struct timespec
538 t0, /* time stamp. before reading fifos */
539 t1, /* time stamp. after reading fifos */
540 ti, /* time interval desired (t1 - t0) */
541 td, /* time interval measured (t1 - t0) */
542 tc; /* time interval correction (ti - td) when td < ti */
543
544 argv0 = argv[0];
545
546 opts_parse(&cfg, argc, argv);
547 khlib_debug("argv0 = %s\n", argv0);
548 config_print(&cfg);
549
550 ti = khlib_timespec_of_float(cfg.interval);
551
552 if (cfg.fifos == NULL)
553 usage("No fifo specs were given!\n");
554
555 /* 1st pass to check file existence and type */
556 for (Fifo *f = cfg.fifos; f; f = f->next) {
557 if (lstat(f->name, &st) < 0) {
558 khlib_error(
559 "Cannot stat \"%s\". Error: %s\n",
560 f->name,
561 strerror(errno)
562 );
563 errors++;
564 continue;
565 }
566 if (!(st.st_mode & S_IFIFO)) {
567 khlib_error("\"%s\" is not a FIFO\n", f->name);
568 errors++;
569 continue;
570 }
571 }
572 if (errors)
573 khlib_fatal(
574 "Encountered errors with given file paths. See log.\n"
575 );
576
577 width = cfg.total_width;
578 seplen = strlen(cfg.separator);
579
580 /* 2nd pass to make space for separators */
581 for (Fifo *f = cfg.fifos; f; f = f->next) {
582 f->pos_init += prefix;
583 f->pos_final += prefix;
584 f->pos_curr = f->pos_init;
585 prefix += seplen;
586 nfifos++;
587 }
588 width += (seplen * (nfifos - 1));
589 buf = calloc(1, width + 1);
590 if (buf == NULL)
591 khlib_fatal(
592 "[memory] Failed to allocate buffer of %d bytes",
593 width
594 );
595 memset(buf, ' ', width);
596 buf[width] = '\0';
597 /* 3rd pass to set the separators */
598 for (Fifo *f = cfg.fifos; f; f = f->next) {
599 if (f->pos_init) { /* Skip the first, left-most */
600 /* Copying only seplen ensures we omit the '\0' byte. */
601 strncpy(
602 buf + (f->pos_init - seplen),
603 cfg.separator,
604 seplen
605 );
606 }
607 }
608
609 if (cfg.output_to_x_root_window && !(d = XOpenDisplay(NULL)))
610 khlib_fatal("XOpenDisplay failed with: %p\n", d);
611 /* TODO: Handle signals */
612 for (;;) {
613 clock_gettime(CLOCK_MONOTONIC, &t0); // FIXME: check errors
614 fifo_read_all(&cfg, &ti, buf);
615 if (cfg.output_to_x_root_window) {
616 if (XStoreName(d, DefaultRootWindow(d), buf) < 0)
617 khlib_fatal("XStoreName failed.\n");
618 XFlush(d);
619 } else {
620 puts(buf);
621 fflush(stdout);
622 }
623 clock_gettime(CLOCK_MONOTONIC, &t1); // FIXME: check errors
624 timespecsub(&t1, &t0, &td);
625 khlib_debug(
626 "td {tv_sec = %ld, tv_nsec = %ld}\n",
627 td.tv_sec,
628 td.tv_nsec
629 );
630 if (timespeccmp(&td, &ti, <)) {
631 /* Pushback on data producers by refusing to read the
632 * pipe more frequently than the interval.
633 */
634 timespecsub(&ti, &td, &tc);
635 khlib_debug("khlib_sleep YES\n");
636 khlib_sleep(&tc);
637 } else {
638 khlib_debug("khlib_sleep NO\n");
639 }
640 }
641
642 return EXIT_SUCCESS;
643 }
This page took 0.137883 seconds and 4 git commands to generate.