2015-08-05 14:43:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# PROTOCOL IO MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Protocol::IO;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
2016-04-12 21:50:25 +02:00
|
|
|
use Exporter qw(import);
|
|
|
|
our @EXPORT = qw();
|
2015-08-05 14:43:41 +02:00
|
|
|
use File::Basename qw(dirname);
|
2015-08-29 20:20:46 +02:00
|
|
|
use IPC::Open3 qw(open3);
|
2015-10-08 17:43:56 +02:00
|
|
|
use IO::Select;
|
2015-08-05 14:43:41 +02:00
|
|
|
use POSIX qw(:sys_wait_h);
|
2015-10-08 17:43:56 +02:00
|
|
|
use Symbol 'gensym';
|
|
|
|
use Time::HiRes qw(gettimeofday);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
use lib dirname($0) . '/../lib';
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::Common::Wait;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Operation constants
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant OP_IO_PROTOCOL => 'IO';
|
|
|
|
|
|
|
|
use constant OP_IO_PROTOCOL_NEW => OP_IO_PROTOCOL . "->new";
|
|
|
|
use constant OP_IO_PROTOCOL_NEW3 => OP_IO_PROTOCOL . "->new3";
|
|
|
|
|
2016-04-12 21:50:25 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Amount of time to attempt to retrieve errors when a process terminates unexpectedly
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant IO_ERROR_TIMEOUT => 5;
|
|
|
|
push @EXPORT, qw(IO_ERROR_TIMEOUT);
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
2015-08-29 20:20:46 +02:00
|
|
|
my $class = shift;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
(
|
|
|
|
my $strOperation,
|
|
|
|
$self->{hIn}, # Input stream
|
|
|
|
$self->{hOut}, # Output stream
|
|
|
|
$self->{hErr}, # Error stream
|
2015-10-08 17:43:56 +02:00
|
|
|
$self->{pId}, # Process ID
|
2016-07-26 22:57:38 +02:00
|
|
|
$self->{strId}, # Id for messages
|
2015-10-08 17:43:56 +02:00
|
|
|
$self->{iProtocolTimeout}, # Protocol timeout
|
|
|
|
$self->{iBufferMax} # Maximum buffer size
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_IO_PROTOCOL_NEW, \@_,
|
|
|
|
{name => 'hIn', required => false, trace => true},
|
|
|
|
{name => 'hOut', required => false, trace => true},
|
|
|
|
{name => 'hErr', required => false, trace => true},
|
2015-10-08 17:43:56 +02:00
|
|
|
{name => 'pId', required => false, trace => true},
|
2016-07-26 22:57:38 +02:00
|
|
|
{name => 'strId', required => false, trace => true},
|
2015-10-08 17:43:56 +02:00
|
|
|
{name => 'iProtocolTimeout', trace => true},
|
|
|
|
{name => 'iBufferMax', trace => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
if (defined($self->{hIn}))
|
|
|
|
{
|
|
|
|
$self->{oInSelect} = IO::Select->new();
|
|
|
|
$self->{oInSelect}->add($self->{hIn});
|
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# new3
|
|
|
|
#
|
|
|
|
# Use open3 to run the command and get the io handles.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new3
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-07-26 22:57:38 +02:00
|
|
|
$strId,
|
2015-10-08 17:43:56 +02:00
|
|
|
$strCommand,
|
|
|
|
$iProtocolTimeout, # Protocol timeout
|
|
|
|
$iBufferMax # Maximum buffer Size
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
OP_IO_PROTOCOL_NEW3, \@_,
|
2016-07-26 22:57:38 +02:00
|
|
|
{name => 'strId', trace => true},
|
2015-10-08 17:43:56 +02:00
|
|
|
{name => 'strCommand', trace => true},
|
|
|
|
{name => 'iProtocolTimeout', trace => true},
|
|
|
|
{name => 'iBufferMax', trace => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
# Use open3 to run the command
|
|
|
|
my ($pId, $hIn, $hOut, $hErr);
|
2015-10-08 17:43:56 +02:00
|
|
|
$hErr = gensym;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
$pId = IPC::Open3::open3($hIn, $hOut, $hErr, $strCommand);
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-07-26 22:57:38 +02:00
|
|
|
{name => 'self', value => $class->new($hOut, $hIn, $hErr, $pId, $strId, $iProtocolTimeout, $iBufferMax)}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Get pid/input/output handles
|
|
|
|
####################################################################################################################################
|
|
|
|
sub hInGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return $self->{hIn};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub hOutGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return $self->{hOut};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub pIdGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return $self->{pId};
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# kill
|
|
|
|
####################################################################################################################################
|
|
|
|
sub kill
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if (defined($self->{pId}))
|
|
|
|
{
|
|
|
|
kill 'KILL', $self->{pId};
|
|
|
|
waitpid($self->{pId}, 0);
|
|
|
|
undef($self->{pId});
|
|
|
|
undef($self->{hIn});
|
|
|
|
undef($self->{hOut});
|
|
|
|
undef($self->{hErr});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# lineRead
|
|
|
|
#
|
2015-10-08 17:43:56 +02:00
|
|
|
# Read an lf-terminated line from the input or error stream.
|
2015-08-05 14:43:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub lineRead
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-10-08 17:43:56 +02:00
|
|
|
my $iTimeout = shift;
|
|
|
|
my $bInRead = shift;
|
2015-12-24 17:32:25 +02:00
|
|
|
my $bError = shift;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# If there's already data in the buffer try to find the next linefeed
|
|
|
|
my $iLineFeedPos = defined($self->{strBuffer}) ? index($self->{strBuffer}, "\n", $self->{iBufferPos}) : -1;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Store the current time if a timeout is required
|
|
|
|
if ($iLineFeedPos == -1)
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
my $fTimeout = defined($iTimeout) ? $iTimeout : $self->{iProtocolTimeout};
|
|
|
|
my $fRemaining = $fTimeout;
|
|
|
|
my $fTimeStart = gettimeofday();
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# If no linefeed was found then load more data
|
|
|
|
do
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
# If the buffer already has data
|
|
|
|
if (defined($self->{strBuffer}) && $self->{iBufferPos} < $self->{iBufferSize})
|
|
|
|
{
|
|
|
|
# And the buffer position is not 0 then trim it so there's room for more data
|
|
|
|
if ($self->{iBufferPos} != 0)
|
|
|
|
{
|
|
|
|
$self->{strBuffer} = substr($self->{strBuffer}, $self->{iBufferPos});
|
|
|
|
$self->{iBufferSize} = $self->{iBufferSize} - $self->{iBufferPos};
|
|
|
|
$self->{iBufferPos} = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else the buffer is empty and data will need to be loaded
|
|
|
|
else
|
|
|
|
{
|
2016-04-14 01:05:31 +02:00
|
|
|
undef($self->{strBuffer}); # ??? Do we need this?
|
2015-10-08 17:43:56 +02:00
|
|
|
$self->{iBufferSize} = 0;
|
|
|
|
$self->{iBufferPos} = 0;
|
|
|
|
}
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Get stream handle and select object
|
|
|
|
my $hIn;
|
|
|
|
my $oSelect;
|
|
|
|
|
|
|
|
# If this is a normal input read
|
|
|
|
if (!defined($bInRead) || $bInRead)
|
|
|
|
{
|
|
|
|
$hIn = $self->{hIn};
|
|
|
|
$oSelect = $self->{oInSelect};
|
|
|
|
}
|
|
|
|
# Else this is an error read
|
|
|
|
else
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
# The select object will need to be created the first time an error is read
|
|
|
|
if (!defined($self->{oErrSelect}))
|
|
|
|
{
|
|
|
|
$self->{oErrSelect} = IO::Select->new();
|
|
|
|
$self->{oErrSelect}->add($self->{hErr});
|
|
|
|
}
|
|
|
|
|
|
|
|
$oSelect = $self->{oErrSelect};
|
|
|
|
$hIn = $self->{hErr};
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Load data into the buffer
|
|
|
|
my $iBufferRead = 0;
|
|
|
|
|
|
|
|
if ($oSelect->can_read($fRemaining))
|
|
|
|
{
|
|
|
|
$iBufferRead = sysread($hIn, $self->{strBuffer}, $self->{iBufferSize} >= $self->{iBufferMax} ?
|
|
|
|
$self->{iBufferMax} : $self->{iBufferMax} - $self->{iBufferSize}, $self->{iBufferSize});
|
|
|
|
|
|
|
|
# An error occurred if undef is returned
|
|
|
|
if (!defined($iBufferRead))
|
|
|
|
{
|
|
|
|
# Store the error message
|
|
|
|
my $strError = defined($!) && $! ne '' ? $! : undef;
|
|
|
|
|
|
|
|
# Check if the remote process exited unexpectedly
|
|
|
|
$self->waitPid();
|
|
|
|
|
|
|
|
# Raise the error
|
|
|
|
confess &log(ERROR, 'unable to read line' . (defined($strError) ? ": ${strError}" : ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Error on EOF (unless reading from error stream)
|
|
|
|
if ($iBufferRead == 0)
|
|
|
|
{
|
|
|
|
# Check if the remote process exited unexpectedly
|
2016-04-12 21:50:25 +02:00
|
|
|
$self->waitPid(0);
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
# Only error if reading from the input stream
|
2016-01-09 15:21:53 +02:00
|
|
|
if (defined($bError) && $bError)
|
2015-10-08 17:43:56 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, "unexpected EOF", ERROR_FILE_READ);
|
|
|
|
}
|
|
|
|
# If reading from error stream then just return undef
|
|
|
|
else
|
|
|
|
{
|
2016-02-23 16:25:22 +02:00
|
|
|
return;
|
2015-10-08 17:43:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# If data was read then check for a linefeed
|
|
|
|
if ($iBufferRead > 0)
|
|
|
|
{
|
|
|
|
$self->{iBufferSize} += $iBufferRead;
|
|
|
|
|
|
|
|
$iLineFeedPos = index($self->{strBuffer}, "\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Check if the remote process exited unexpectedly
|
2016-04-12 21:50:25 +02:00
|
|
|
$self->waitPid(0);
|
2015-10-08 17:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Calculate time remaining before timeout
|
|
|
|
if ($iLineFeedPos == -1)
|
|
|
|
{
|
|
|
|
$fRemaining = $fTimeout - (gettimeofday() - $fTimeStart);
|
|
|
|
}
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
2015-10-08 17:43:56 +02:00
|
|
|
while ($iLineFeedPos == -1 && $fRemaining > 0);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# If not linefeed was found within the time return undef
|
|
|
|
if ($iLineFeedPos == -1)
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2015-12-24 17:32:25 +02:00
|
|
|
if (!defined($bError) || $bError)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unable to read line after $self->{iProtocolTimeout} seconds", ERROR_PROTOCOL_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2016-02-23 16:25:22 +02:00
|
|
|
return;
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Return the line that was found and adjust the buffer position
|
2016-01-09 15:21:53 +02:00
|
|
|
my $strLine = $iLineFeedPos - $self->{iBufferPos} == 0 ? '' :
|
|
|
|
substr($self->{strBuffer}, $self->{iBufferPos}, $iLineFeedPos - $self->{iBufferPos});
|
2015-10-08 17:43:56 +02:00
|
|
|
$self->{iBufferPos} = $iLineFeedPos + 1;
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
return $strLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# errorWrite
|
|
|
|
#
|
|
|
|
# Write error data to the stream.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub errorWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strBuffer = shift;
|
|
|
|
|
|
|
|
if (defined($self->{pId}))
|
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
confess &log(ASSERT, 'errorWrite() not valid in master process');
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$self->lineWrite($strBuffer, $self->{hError});
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# lineWrite
|
|
|
|
#
|
|
|
|
# Write line to the stream.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub lineWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strBuffer = shift;
|
|
|
|
my $hOut = shift;
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Check if the process has exited abnormally (doesn't seem like we should need this, but the next syswrite does a hard
|
|
|
|
# abort if the remote process has already closed)
|
2016-04-12 21:50:25 +02:00
|
|
|
$self->waitPid(0);
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
# Write the data
|
2015-08-05 14:43:41 +02:00
|
|
|
my $iLineOut = syswrite(defined($hOut) ? $hOut : $self->{hOut}, (defined($strBuffer) ? $strBuffer : '') . "\n");
|
|
|
|
|
|
|
|
if (!defined($iLineOut) || $iLineOut != (defined($strBuffer) ? length($strBuffer) : 0) + 1)
|
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
# Check if the process has exited abnormally
|
|
|
|
$self->waitPid();
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
confess &log(ERROR, "unable to write ${strBuffer}: $!", ERROR_PROTOCOL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# bufferRead
|
|
|
|
#
|
|
|
|
# Read data from a stream.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub bufferRead
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-10-08 17:43:56 +02:00
|
|
|
my $tBufferRef = shift;
|
|
|
|
my $iRequestSize = shift;
|
2015-08-05 14:43:41 +02:00
|
|
|
my $iOffset = shift;
|
2015-10-08 17:43:56 +02:00
|
|
|
my $bBlock = shift;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Set working variables
|
|
|
|
my $iRemainingSize = $iRequestSize;
|
|
|
|
$iOffset = defined($iOffset) ? $iOffset : 0;
|
|
|
|
$bBlock = defined($bBlock) ? $bBlock : false;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# If there is data left over in the buffer from lineRead then use it
|
|
|
|
if (defined($self->{strBuffer}) && $self->{iBufferPos} < $self->{iBufferSize})
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
# There is enough data in the buffer to satisfy the entire request and have data left over
|
|
|
|
if ($iRemainingSize < $self->{iBufferSize} - $self->{iBufferPos})
|
|
|
|
{
|
|
|
|
$$tBufferRef = substr($self->{strBuffer}, $self->{iBufferPos}, $iRequestSize);
|
|
|
|
$self->{iBufferPos} += $iRequestSize;
|
|
|
|
return $iRequestSize;
|
|
|
|
}
|
|
|
|
# Else the entire buffer will be used (and more may be needed if blocking)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$$tBufferRef = substr($self->{strBuffer}, $self->{iBufferPos});
|
|
|
|
my $iReadSize = $self->{iBufferSize} - $self->{iBufferPos};
|
|
|
|
$iRemainingSize -= $iReadSize;
|
|
|
|
undef($self->{strBuffer});
|
|
|
|
|
|
|
|
return $iReadSize if !$bBlock || $iRemainingSize == 0;
|
|
|
|
|
|
|
|
$iOffset += $iReadSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# If this is a blocking read then loop until all bytes have been read - else error.
|
|
|
|
#
|
|
|
|
# This link (http://docstore.mik.ua/orelly/perl/cookbook/ch07_15.htm) demonstrates a way to implement this same loop without
|
|
|
|
# using select. Not sure if it would be better but the link has been left here so it can be found in the future if the
|
|
|
|
# implementation needs to be changed.
|
|
|
|
if ($bBlock)
|
|
|
|
{
|
|
|
|
my $fTimeStart = gettimeofday();
|
|
|
|
my $fRemaining = $self->{iProtocolTimeout};
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
# Check if the sysread call will block
|
|
|
|
if ($self->{oInSelect}->can_read($fRemaining))
|
|
|
|
{
|
|
|
|
# Read a data into the buffer
|
|
|
|
my $iReadSize = sysread($self->{hIn}, $$tBufferRef, $iRemainingSize, $iOffset);
|
|
|
|
|
|
|
|
# Process errors from the sysread
|
|
|
|
if (!defined($iReadSize))
|
|
|
|
{
|
|
|
|
my $strError = $!;
|
|
|
|
|
|
|
|
$self->waitPid();
|
|
|
|
confess &log(ERROR, 'unable to read' . (defined($strError) ? ": ${strError}" : ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check for EOF
|
|
|
|
if ($iReadSize == 0)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'unexpected EOF', ERROR_FILE_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update remaining size and return when it reaches 0
|
|
|
|
$iRemainingSize -= $iReadSize;
|
|
|
|
|
|
|
|
if ($iRemainingSize == 0)
|
|
|
|
{
|
|
|
|
return $iRequestSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update the offset to advance the next read further into the buffer
|
|
|
|
$iOffset += $iReadSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Calculate time remaining before timeout
|
|
|
|
$fRemaining = $self->{iProtocolTimeout} - (gettimeofday() - $fTimeStart);
|
|
|
|
}
|
|
|
|
while ($fRemaining > 0);
|
|
|
|
|
|
|
|
# Throw an error if timeout happened before required bytes were read
|
2015-12-24 17:32:25 +02:00
|
|
|
confess &log(ERROR, "unable to read ${iRequestSize} bytes after $self->{iProtocolTimeout} seconds", ERROR_PROTOCOL_TIMEOUT);
|
2015-10-08 17:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Otherwise do a non-blocking read and return whatever bytes are ready
|
|
|
|
my $iReadSize = sysread($self->{hIn}, $$tBufferRef, $iRemainingSize, $iOffset);
|
|
|
|
|
|
|
|
# Process errors from sysread
|
|
|
|
if (!defined($iReadSize))
|
|
|
|
{
|
|
|
|
my $strError = $!;
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
$self->waitPid();
|
2015-10-08 17:43:56 +02:00
|
|
|
confess &log(ERROR, 'unable to read' . (defined($strError) ? ": ${strError}" : ''));
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Return the number of bytes read
|
|
|
|
return $iReadSize;
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# bufferWrite
|
|
|
|
#
|
|
|
|
# Write data to a stream.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub bufferWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2015-10-08 17:43:56 +02:00
|
|
|
my $tBufferRef = shift;
|
|
|
|
my $iWriteSize = shift;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
# If block size is not defined, get it from buffer length
|
2015-10-08 17:43:56 +02:00
|
|
|
$iWriteSize = defined($iWriteSize) ? $iWriteSize : length($$tBufferRef);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
# Write the block
|
2015-10-08 17:43:56 +02:00
|
|
|
my $iWriteOut = syswrite($self->{hOut}, $$tBufferRef, $iWriteSize);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
# Report any errors
|
2015-10-08 17:43:56 +02:00
|
|
|
if (!defined($iWriteOut) || $iWriteOut != $iWriteSize)
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
|
|
|
my $strError = $!;
|
|
|
|
|
|
|
|
$self->waitPid();
|
2015-10-08 17:43:56 +02:00
|
|
|
confess "unable to write ${iWriteSize} bytes" . (defined($strError) ? ': ' . $strError : '');
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# waitPid
|
|
|
|
#
|
|
|
|
# See if the remote process has terminated unexpectedly.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub waitPid
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $fWaitTime = shift;
|
|
|
|
my $bReportError = shift;
|
|
|
|
|
|
|
|
# Record the start time and set initial sleep interval
|
2016-04-12 21:50:25 +02:00
|
|
|
my $oWait = waitInit(defined($fWaitTime) ? $fWaitTime : IO_ERROR_TIMEOUT);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
if (defined($self->{pId}))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
my $iResult = waitpid($self->{pId}, WNOHANG);
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# If there is no such process we'll assume it terminated previously
|
2015-08-05 14:43:41 +02:00
|
|
|
if ($iResult == -1)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# If the process exited then this is unexpected
|
2015-08-05 14:43:41 +02:00
|
|
|
if ($iResult > 0)
|
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
# Get the exit status so we can report it later
|
|
|
|
my $iExitStatus = ${^CHILD_ERROR_NATIVE} >> 8;
|
|
|
|
|
|
|
|
# Sometimes we'll expect an error so it won't always be reported
|
2015-08-05 14:43:41 +02:00
|
|
|
if (!defined($bReportError) || $bReportError)
|
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
# Default error
|
2015-08-05 14:43:41 +02:00
|
|
|
my $strError = 'no error on stderr';
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# If the error stream is already closed then we can't fetch the real error
|
2015-08-05 14:43:41 +02:00
|
|
|
if (!defined($self->{hErr}))
|
|
|
|
{
|
|
|
|
$strError = 'no error captured because stderr is already closed';
|
|
|
|
}
|
2015-10-08 17:43:56 +02:00
|
|
|
# Get whatever text we can from the error stream
|
2015-08-05 14:43:41 +02:00
|
|
|
else
|
|
|
|
{
|
2015-10-08 17:43:56 +02:00
|
|
|
eval
|
|
|
|
{
|
|
|
|
$strError = undef;
|
|
|
|
|
2015-12-24 17:32:25 +02:00
|
|
|
while (my $strLine = $self->lineRead(0, false, false))
|
2015-10-08 17:43:56 +02:00
|
|
|
{
|
|
|
|
if (defined($strError))
|
|
|
|
{
|
|
|
|
$strError .= "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$strError .= $strLine;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@ || !defined($strError))
|
|
|
|
{
|
|
|
|
$strError = 'no output from terminated process' . (defined($@) && $@ ne '' ? ": $@" : '');
|
|
|
|
}
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$self->{pId} = undef;
|
|
|
|
$self->{hIn} = undef;
|
|
|
|
$self->{hOut} = undef;
|
|
|
|
$self->{hErr} = undef;
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Finally, confess the error
|
2016-07-26 22:57:38 +02:00
|
|
|
confess &log(
|
|
|
|
ERROR, 'remote process terminated on ' . $self->{strId} . ' host' .
|
|
|
|
($iExitStatus < ERROR_MINIMUM || $iExitStatus > ERROR_MAXIMUM ? " (exit status ${iExitStatus})" : '') .
|
|
|
|
": ${strError}",
|
|
|
|
$iExitStatus >= ERROR_MINIMUM && $iExitStatus <= ERROR_MAXIMUM ? $iExitStatus : ERROR_HOST_CONNECT);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (waitMore($oWait));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|