changeset 166:66fef65c34e7

watcher: exit if errors occur.
author John "Elwin" Edwards
date Wed, 07 Jan 2015 13:22:05 -0500
parents 59e62710cbb5
children fba1b34e7554
files watcher.c
diffstat 1 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/watcher.c	Wed Jan 07 13:18:35 2015 -0500
+++ b/watcher.c	Wed Jan 07 13:22:05 2015 -0500
@@ -43,7 +43,7 @@
 }
 
 int main(int argc, char *argv[]) {
-  int ifd, rsize, off, done, nwatchers, i;
+  int ifd, rsize, off, done, nwatchers, i, result;
   char typecode;
   struct inotify_event *iev;
   fd_set rfds;
@@ -69,7 +69,11 @@
     FD_ZERO(&rfds);
     FD_SET(0, &rfds);
     FD_SET(ifd, &rfds);
-    select(ifd + 1, &rfds, NULL, NULL, NULL);
+    result = select(ifd + 1, &rfds, NULL, NULL, NULL);
+    if (result < 0) {
+      /* Something went wrong. */
+      break;
+    }
     if (FD_ISSET(ifd, &rfds)) {
       off = 0;
       rsize = read(ifd, ibuf, sizeof(struct inotify_event) + NAME_MAX + 1);
@@ -90,7 +94,11 @@
       fflush(stdout);
     }
     if (FD_ISSET(0, &rfds)) {
-      read(0, &typecode, 1);
+      result = read(0, &typecode, 1);
+      if (result <= 0) {
+        /* EOF on stdin: controlling process probably died. */
+        done = 1;
+      }
       if (typecode == '\n')
         done = 1;
     }