use warnings;
use Carp qw(cluck confess carp croak);
+use POSIX;
use IO::Socket::UNIX;
use Scalar::Util qw( looks_like_number );
return "\"$arg\"";
}
+# Handle socket errors.
+sub _socket_error {
+ my ($self, $where) = @_;
+
+ # If the peer has reset the connection, try to reconnect,
+ # otherwise fail.
+ if ($! == EPIPE) {
+ _debug "^^ error on $where: $!; reconnecting\n";
+ $self->destroy;
+ $self->{sock} = _create_socket ($self->{path}) or return 1;
+ return;
+ } else {
+ carp ("error on $where: $!; aborting action\n");
+ $self->{error} = $!;
+ return 1;
+ }
+}
+
# Send a command on a socket, including any required argument escaping.
# Return a single line of result.
sub _socket_command {
$command .= "\n";
}
_debug "-> $command";
- $fh->print($command);
+ while (not $fh->print($command)) {
+ return if $self->_socket_error ('print');
+ $fh = $self->{sock};
+ }
- my $response = $fh->getline;
+ my $response;
+ while (not defined ($response = $fh->getline)) {
+ return if $self->_socket_error ('getline');
+ $fh = $self->{sock};
+ }
chomp $response;
_debug "<- $response\n";
return $response;
for (1 .. $nresults)
{
- my $entry = $fh->getline;
+ my $entry;
+ while (not defined($entry = $fh->getline)) {
+ return if $self->_socket_error ('getline');
+ $fh = $self->{sock};
+ }
chomp $entry;
_debug "<- $entry\n";
$callback->($entry, $cbdata);
warn "Collectd::Unixsock->_send_message(\$msg): message is too long!" if length($msg) > 1024;
_debug "-> $msg";
- $fh->print($msg);
+ while (not $fh->print($msg)) {
+ return if $self->_socket_error ('print');
+ $fh = $self->{sock};
+ }
- $msg = <$fh>;
+ while (not defined ($msg = <$fh>)) {
+ return if $self->_socket_error ('readline');
+ $fh = $self->{sock};
+ }
chomp ($msg);
_debug "<- $msg\n";
for (1 .. $nresults)
{
- $msg = <$fh>;
+ while (not defined ($msg = <$fh>)) {
+ return if $self->_socket_error ('readline');
+ $fh = $self->{sock};
+ }
chomp $msg;
_debug "<- $msg\n";
next unless $msg =~ $pattern;
for (1 .. $nresults)
{
- $msg = <$fh>;
+ while (not defined ($msg = <$fh>)) {
+ return if $self->_socket_error ('readline');
+ $fh = $self->{sock};
+ }
chomp $msg;
_debug "<- $msg\n";