$timeout = 15 if $timeout <= 0;
+ main::debug("Going to run $cmd, timeout of $timeout", 10);
my $pid = open(CMD, "-|");
if ($pid) { # I'm the parent
-
$message = "";
eval {
- local $SIG{'ALRM'} = sub { die };
+ local $SIG{'ALRM'} = sub { die "alarm\n" };
+ local $SIG{'PIPE'} = sub { die "pipe closed\n" };
alarm($timeout);
@output = <CMD>;
alarm(0);
};
+ main::debug("After eval for $cmd", 10);
+ if ($@) {
+ main::debug("Timeout for $cmd triggered: $@", 10);
+ }
+
close CMD or !$! or croak "Error closing CMD fh: $!";
alarm(0);
# Handle child process is not gone
if ( kill 0,$pid ) {
+ main::debug("Child process that was running '$cmd' hasn't finished, terminating", 10);
kill "INTR",$pid; # Try control+C
sleep 1; # Give it time to die
}
} else {
- exec($cmd) || croak "cannot exec program $cmd: $!";
+ $0 = "$0 - (SafeExec)";
+ main::debug("Child process running '$cmd'", 10);
+ system($cmd);
+
+ if ($? == -1) {
+ carp "cannot execute '$cmd': $!";
+ exit -1;
+ } elsif ($? & 127) {
+ carp "'$cmd' failed with signal " . ($? & 127);
+ exit -1;
+ } elsif ($? != 0) {
+ main::log("Command '$cmd' return error code: " . $? >> 8);
+ }
+
+ main::debug("Child process finished '$cmd'", 10);
+
+ exit;
}
wantarray ? @output : join '', @output;