Handle retry entirely within the fifo_read_all routine
[khatus.git] / x5 / khatus.c
index fad3994..62b3774 100644 (file)
@@ -373,7 +373,7 @@ fifo_read_all(Config *cfg, char *buf)
 {
        fd_set fds;
        int maxfd = -1;
-       int ready;
+       int ready = 0;
        struct stat st;
 
        FD_ZERO(&fds);
@@ -411,24 +411,43 @@ fifo_read_all(Config *cfg, char *buf)
        debug("ready: %d\n", ready);
        assert(ready != 0);
        if (ready < 0)
+               /* TODO: Do we really want to fail here? */
                fatal("%s", strerror(errno));
-       for (Fifo *f = cfg->fifos; f; f = f->next) {
-               if (FD_ISSET(f->fd, &fds)) {
-                       debug("reading: %s\n", f->name);
-                       switch (fifo_read_one(f, buf)) {
-                       case END_OF_FILE:
-                       case FAILURE:
-                               close(f->fd);
-                               f->fd = -1;
-                               break;
-                       case END_OF_MESSAGE:
-                       case RETRY:
-                               break;
-                       default:
-                               assert(0);
+       while (ready) {
+               for (Fifo *f = cfg->fifos; f; f = f->next) {
+                       if (FD_ISSET(f->fd, &fds)) {
+                               debug("reading: %s\n", f->name);
+                               switch (fifo_read_one(f, buf)) {
+                               /*
+                                * ### MESSAGE LOSS ###
+                                * is introduced by closing at EOM in addition
+                                * to EOF, since there may be unread messages
+                                * remaining in the pipe. However,
+                                *
+                                * ### INTER-MESSAGE PUSHBACK ###
+                                * is also gained, since pipes block at the
+                                * "open" call.
+                                *
+                                * This is an acceptable trade-off because we
+                                * are a stateless reporter of a _most-recent_
+                                * status, not a stateful accumulator.
+                                */
+                               case END_OF_MESSAGE:
+                               case END_OF_FILE:
+                               case FAILURE:
+                                       close(f->fd);
+                                       f->fd = -1;
+                                       ready--;
+                                       break;
+                               case RETRY:
+                                       break;
+                               default:
+                                       assert(0);
+                               }
                        }
                }
        }
+       assert(ready == 0);
 }
 
 int
This page took 0.028584 seconds and 4 git commands to generate.