chiark / gitweb /
bin/disorder-notify: Improve error handling in the `select' loop.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 2 Jun 2020 10:09:36 +0000 (11:09 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 2 Jun 2020 16:20:38 +0000 (17:20 +0100)
Don't crash out on read errors.  Report the problem in the `lost
connection' message.

bin/disorder-notify

index 3c570734a10df8d1658adb06a082992d1c934ed9..b632ef461fca10c7255fcc12901dc214f6a80ac4 100755 (executable)
@@ -191,6 +191,7 @@ sub watch_and_notify0 ($) {
   my $buffer = "";
   my @lines = ();
   my $rdin = ""; vec($rdin, (fileno $sk_log), 1) = 1;
+  my $loss;
 
   WATCH: for (;;) {
     for my $line (@lines) {
@@ -218,13 +219,13 @@ sub watch_and_notify0 ($) {
       }
     }
 
-    last WATCH unless $sk_log;
+    if (!$sk_log) { $loss = "EOF from server"; last WATCH; }
     select my $rdout = $rdin, undef, undef, undef;
     READ: for (;;) {
       my ($b, $n);
       eval { $n = sysread $sk_log, $b, 4096; };
       if ($@ && $@->errno == EAGAIN) { last READ; }
-      elsif ($@) { die $@; }
+      elsif ($@) { $loss = "error from read: " . $@->errno; last WATCH; }
       elsif (!$n) { close $sk_log; $sk_log = undef; }
       else { $buffer .= $b; }
     }
@@ -233,7 +234,7 @@ sub watch_and_notify0 ($) {
     $buffer = pop @lines;
   }
 
-  notify "$TITLE state", "Lost connection";
+  notify "$TITLE state", "Lost connection: $loss";
 
   close $sk;
   close $sk_log if defined $sk_log;