]> git.etc.gen.nz Git - spong.git/commitdiff
added into CVS
authorStephen L Johnson <sjohnson@monsters.org>
Mon, 6 Aug 2001 23:07:25 +0000 (23:07 +0000)
committerStephen L Johnson <sjohnson@monsters.org>
Mon, 6 Aug 2001 23:07:25 +0000 (23:07 +0000)
src/lib/Spong/SafeExec.pm [new file with mode: 0755]

diff --git a/src/lib/Spong/SafeExec.pm b/src/lib/Spong/SafeExec.pm
new file mode 100755 (executable)
index 0000000..86f1e6d
--- /dev/null
@@ -0,0 +1,58 @@
+
+package Spong::SafeExec;
+
+use Carp qw(croak carp);
+use Exporter ();
+
+use vars    qw($VERSION @ISA @EXPORT_OK);
+
+$VERSION = 0.01;
+@ISA     = qw(Exporter);
+@EXPORT_OK  = qw(safe_exec);
+
+sub safe_exec {
+    my($cmd,$timeout) = @_;
+    my (@output);
+
+    $timeout = 15 if  $timeout <= 0;
+
+    my $pid = open(CMD, "-|");
+
+    if ($pid) { # I'm the parent
+
+        $message = "";
+        eval {
+            local $SIG{'ALRM'} = sub { die };
+            alarm($timeout);
+
+            @output = <CMD>;
+
+            alarm(0);
+        };
+
+        close CMD or !$! or croak "Error closing CMD fh: $!";
+        alarm(0);
+
+        # Handle child process is not gone
+        if ( kill 0,$pid ) {
+            kill "INTR",$pid;  # Try control+C
+            sleep 1;           # Give it time to die
+
+            if ( kill 0, $pid ) {  
+                kill 9,$pid;    # If still alive, nuke it
+                if ( kill 0, $pid ) {
+                     carp "Could not kill pid $pid";
+                }
+            }
+            
+        }
+
+    } else {
+        exec($cmd) || croak "cannot exec program $cmd: $!";
+    }
+
+   wantarray ? @output : join '', @output;
+}
+
+
+1;