Make unexpected pipe errors fatal as precaution.

Addresses BIT-1260.
This commit is contained in:
Jon Siwek 2014-09-26 10:59:40 -05:00
parent cce09b75de
commit 57d0346789

View file

@ -1,6 +1,7 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include "Flare.h"
#include "Reporter.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
@ -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");
}
}