From 537c227f8752f5862cf23d74e025bf565bc4d148 Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Wed, 7 Jan 2015 13:22:05 -0500 Subject: [PATCH] watcher: exit if errors occur. --- watcher.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/watcher.c b/watcher.c index 378ea70..2f32ff1 100644 --- a/watcher.c +++ b/watcher.c @@ -43,7 +43,7 @@ int startwatch(int ifd, char *dir, struct watchdir *w) { } 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 @@ int main(int argc, char *argv[]) { 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 @@ int main(int argc, char *argv[]) { 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; }