From: Andrew Ruthven Date: Thu, 27 Dec 2007 09:11:56 +0000 (+1300) Subject: Fix up handling of commands that are executable and add lots more debugging. X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48634510f32551bb3dac3e211c8fcfcaa14fb35c;p=spong.git Fix up handling of commands that are executable and add lots more debugging. Back when I was using Spong at a previous job years ago, and again now that I'm playing with it at home I ran into problems with lots of spong-network processes all claming to be checking the same server and check. I finally decided to dig into it. In the current case it is because the program wasn't executable and SafeExec wasn't working that out. The forked child was continuing to run for whatever reason. This appears to work correctly now, I don't have ghost spong-network processes hanging around anymore. --- diff --git a/src/lib/Spong/SafeExec.pm b/src/lib/Spong/SafeExec.pm index 86f1e6d..abee8b1 100755 --- a/src/lib/Spong/SafeExec.pm +++ b/src/lib/Spong/SafeExec.pm @@ -16,13 +16,14 @@ sub safe_exec { $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 = ; @@ -30,11 +31,17 @@ sub safe_exec { 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 @@ -48,7 +55,23 @@ sub safe_exec { } } 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;