From: Siraaj Khandkar Date: Sat, 21 Mar 2020 21:45:12 +0000 (-0400) Subject: Write to pipe form the sensor directly X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=707b2f7979d456160eac03289eb6558610385636;p=khatus.git Write to pipe form the sensor directly shell redirection isn't sufficient because the writer process is killed upon closing of the pipe by the reader. --- diff --git a/x5/khatus_sensor_time.c b/x5/khatus_sensor_time.c index f156f45..983ebdf 100644 --- a/x5/khatus_sensor_time.c +++ b/x5/khatus_sensor_time.c @@ -1,5 +1,8 @@ +#include +#include #include -#include +#include +#include #include #include #include @@ -9,19 +12,24 @@ #include "khatus_lib_log.h" #include "khatus_lib_time.h" +#define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);} + +#define MAX_LEN 20 +#define END_OF_MESSAGE '\n' + char *argv0; void -usage() +print_usage() { printf( - "%s: [OPT ...]\n" + "%s: [OPT ...] FIFO\n" "\n" + "FIFO = string # path to fifo file\n" "OPT = -i int # interval\n" " | -f string # format string\n" " | -h # help message (i.e. what you're reading now :) )\n", argv0); - fatal("usage\n"); } int @@ -34,10 +42,19 @@ main(int argc, char **argv) time_t t; struct timespec ti; - char buf[128]; + char buf[MAX_LEN]; char c; - memset(buf, '\0', 128); + char *fifo_name; + int fifo_fd = -1; + + int n = 0; /* written */ + int r = 0; /* remaining */ + int i = 0; /* buffer position */ + + signal(SIGPIPE, SIG_IGN); /* Handling manually */ + + memset(buf, '\0', MAX_LEN); while ((c = getopt(argc, argv, "f:i:h")) != -1) switch (c) { case 'f': @@ -48,17 +65,56 @@ main(int argc, char **argv) opt_interval = atof(optarg); break; case 'h': - usage(); - break; + print_usage(); + return 0; + case '?': + if (optopt == 'f' || optopt == 'i') + fprintf(stderr, + "Option -%c requires an argument.\n", + optopt); + else if (isprint(optopt)) + fprintf (stderr, + "Unknown option `-%c'.\n", + optopt); + else + fprintf(stderr, + "Unknown option character `\\x%x'.\n", + optopt); + return 1; default: - usage(); + assert(0); } + fifo_name = argv[optind]; + debug("fifo_name: %s\n", fifo_name); + if (!fifo_name) + usage("No filename was provided\n"); ti = timespec_of_float(opt_interval); for (;;) { + debug("openning \"%s\"\n", fifo_name); + fifo_fd = open(fifo_name, O_WRONLY); + if (fifo_fd < 0) + fatal("Failed to open FIFO file: \"%s\". Error: %s\n", + fifo_name, + strerror(errno)); + debug("openned. fd: %d\n", fifo_fd); t = time(NULL); - strftime(buf, sizeof(buf), opt_fmt, localtime(&t)); - puts(buf); - fflush(stdout); + strftime(buf, MAX_LEN, opt_fmt, localtime(&t)); + r = strlen(buf); + buf[r] = END_OF_MESSAGE; + for (i = 0; (n = write(fifo_fd, buf + i++, 1)) && r; r--) + ; + if (n < 0) + fatal("Failed to write to %s. Err num: %d, Err msg: %s\n", + fifo_name, + errno, + strerror(errno)); + if (close(fifo_fd) < 0) + fatal("Failed to close %s. Err num: %d, Err msg: %s\n", + fifo_name, + errno, + strerror(errno)); + fifo_fd = -1; + debug("closed. fd: %d\n", fifo_fd); snooze(&ti); } return EXIT_SUCCESS;