sub _timedFork1
{
my ($command, $time) = @_;
my @resp1;
my $pid;
my @pids;
local $SIG{ALRM} = sub {kill -15, $pid or die "kill: $!";
die "TIMEOUT!";
};
if ($pid = fork()) {
push(@pids, $pid);
alarm $time;
waitpid($pid, 0);
}
elsif (defined $pid) {
@resp1 = `$command`;
return @resp1;
exit;
}
return ($pid, @resp1)
}
####
sub _timedFork2
{
my ($command, $time) = @_;
my $pid;
my @resp2;
local $SIG{ALRM} = sub {kill 15, $pid or die "kill: $!";
die 'Timeout!';
};
eval {
$pid = fork();
unless (defined $pid) {
die "Fork failed: $!";
}
unless ($pid) {
@resp2 = `$command`;
}
alarm $time;
waitpid $pid => 0;
};
if ($@ and $@ !~ m/Timeout!/i) {
die $@;
}
return @resp2;
}
####
sub _forkFHCommand
{
my ($command, $time) = @_;
my @resp3;
my $pid = open COMMAND, "-|", $command or die $!;
my $endtime = time() + $time;
my $line;
while($line = ) {
push(@resp3, $line);
if (time > $endtime) {
kill 15, $pid;
last;
}
}
close COMMAND;
return (0, @resp3);
}