diff --git a/src/Flare.cc b/src/Flare.cc index 960e66cbf4..dcb5fa2c1f 100644 --- a/src/Flare.cc +++ b/src/Flare.cc @@ -1,6 +1,7 @@ // See the file "COPYING" in the main distribution directory for copyright. #include "Flare.h" +#include "Reporter.h" #include #include #include @@ -12,6 +13,13 @@ Flare::Flare() { } +static void bad_pipe_op(const char* which) + { + char buf[256]; + strerror_r(errno, buf, sizeof(buf)); + reporter->FatalErrorWithCore("unexpected pipe %s failure: %s", which, buf); + } + void Flare::Fire() { char tmp; @@ -24,11 +32,20 @@ void Flare::Fire() // Success -- wrote a byte to pipe. break; - if ( n < 0 && errno == EAGAIN ) - // Success -- pipe is full and just need at least one byte in it. - break; + if ( n < 0 ) + { + if ( errno == EAGAIN ) + // Success: pipe is full and just need at least one byte in it. + break; - // Loop because either the byte wasn't written or got EINTR error. + if ( errno == EINTR ) + // Interrupted: try again. + continue; + + bad_pipe_op("write"); + } + + // No error, but didn't write a byte: try again. } } @@ -37,7 +54,21 @@ void Flare::Extinguish() char tmp[256]; for ( ; ; ) - if ( read(pipe.ReadFD(), &tmp, sizeof(tmp)) == -1 && errno == EAGAIN ) - // Pipe is now drained. + { + int n = read(pipe.ReadFD(), &tmp, sizeof(tmp)); + + if ( n >= 0 ) + // Pipe may not be empty yet: try again. + continue; + + if ( errno == EAGAIN ) + // Success: pipe is now empty. break; + + if ( errno == EINTR ) + // Interrupted: try again. + continue; + + bad_pipe_op("read"); + } }