Encode the result or the error in JSON. This way complex objects or
exceptions may be passed to the parent in a generic way.
This allows to remove the second pipe 'pipe_err'.
Allow also to return undef without any warnings to our caller.
This avoids a "use of uninitialized variable ..." warning
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
use URI::Escape;
use Encode;
use Digest::SHA;
use URI::Escape;
use Encode;
use Digest::SHA;
use Text::ParseWords;
use String::ShellQuote;
use Time::HiRes qw(usleep gettimeofday tv_interval alarm);
use Text::ParseWords;
use String::ShellQuote;
use Time::HiRes qw(usleep gettimeofday tv_interval alarm);
my $res;
my $error;
my $pipe_out = IO::Pipe->new();
my $res;
my $error;
my $pipe_out = IO::Pipe->new();
- my $pipe_err = IO::Pipe->new();
# disable pending alarms, save their remaining time
my $prev_alarm = alarm 0;
# disable pending alarms, save their remaining time
my $prev_alarm = alarm 0;
if (!$child) {
$pipe_out->writer();
if (!$child) {
$pipe_out->writer();
- print {$pipe_out} "$res";
+ print {$pipe_out} encode_json({ result => $res });
$pipe_out->flush();
};
if (my $err = $@) {
$pipe_out->flush();
};
if (my $err = $@) {
- print {$pipe_err} "$err";
- $pipe_err->flush();
+ print {$pipe_out} encode_json({ error => $err });
+ $pipe_out->flush();
POSIX::_exit(1);
}
POSIX::_exit(0);
}
$pipe_out->reader();
POSIX::_exit(1);
}
POSIX::_exit(0);
}
$pipe_out->reader();
my $readvalues = sub {
local $/ = undef;
my $readvalues = sub {
local $/ = undef;
- $res = <$pipe_out>;
- $error = <$pipe_err>;
+ my $child_res = decode_json(scalar<$pipe_out>);
+ $res = $child_res->{result};
+ $error = $child_res->{error};
};
eval {
run_with_timeout($timeout, $readvalues);
};
warn $@ if $@;
$pipe_out->close();
};
eval {
run_with_timeout($timeout, $readvalues);
};
warn $@ if $@;
$pipe_out->close();
kill('KILL', $child);
waitpid($child, 0);
kill('KILL', $child);
waitpid($child, 0);