Cleanly handle select() returning EINTR.
authorEmil Mikulic <emikulic@gmail.com>
Sun, 10 Mar 2013 17:23:34 +0000 (04:23 +1100)
committerEmil Mikulic <emikulic@gmail.com>
Sun, 10 Mar 2013 17:26:24 +0000 (04:26 +1100)
This is usually the result of catching SIGTERM (via ^C).
After this change, we exit cleanly.

buftee.c

index 0ab9bd4..c31ba69 100644 (file)
--- a/buftee.c
+++ b/buftee.c
@@ -245,12 +245,17 @@ static int xwrite(const int fd, struct buf* const buf) {
 static int max(const int a, const int b) { return (a > b) ? a : b; }
 
 static void wait_until_readable(const int fd) {
-  int select_ret;
   fd_set read_fds;
   FD_ZERO(&read_fds);
   FD_SET(fd, &read_fds);
-  if ((select_ret = select(fd + 1, &read_fds, NULL, NULL, NULL)) == -1)
+  int select_ret = select(fd + 1, &read_fds, NULL, NULL, NULL);
+  if (select_ret == -1) {
+    if (errno == EINTR) {
+      assert(stopping);  // that should have been SIGTERM
+      return;
+    }
     err(1, "select() failed");
+  }
   if (!FD_ISSET(fd, &read_fds))
     errx(1, "select() did not return readable fd = %d", fd);
 }