[pve-devel] r6134 - in pve-common/trunk/data: . PVE

svn-commits at proxmox.com svn-commits at proxmox.com
Tue Jun 21 14:33:17 CEST 2011


Author: dietmar
Date: 2011-06-21 14:33:17 +0200 (Tue, 21 Jun 2011)
New Revision: 6134

Modified:
   pve-common/trunk/data/ChangeLog
   pve-common/trunk/data/PVE/Tools.pm
Log:
use alarm


Modified: pve-common/trunk/data/ChangeLog
===================================================================
--- pve-common/trunk/data/ChangeLog	2011-06-21 09:33:39 UTC (rev 6133)
+++ pve-common/trunk/data/ChangeLog	2011-06-21 12:33:17 UTC (rev 6134)
@@ -1,5 +1,7 @@
 2011-06-21  Proxmox Support Team  <support at proxmox.com>
 
+	* PVE/Tools.pm (run_command): use alarm to impl. timeout
+
 	* PVE/RESTHandler.pm (api_dump): new - used to generate docu
 
 	* PVE/Tools.pm (upid_decode): fix upid parser

Modified: pve-common/trunk/data/PVE/Tools.pm
===================================================================
--- pve-common/trunk/data/PVE/Tools.pm	2011-06-21 09:33:39 UTC (rev 6133)
+++ pve-common/trunk/data/PVE/Tools.pm	2011-06-21 12:33:17 UTC (rev 6134)
@@ -169,13 +169,15 @@
 
     my $errmsg;
     my $laststderr;
+    my $timeout;
+    my $oldtimeout;
+    my $pid;
 
     eval {
 	my $reader = IO::File->new();
 	my $writer = IO::File->new();
 	my $error  = IO::File->new();
 
-	my $timeout;
 	my $input;
 	my $outfunc;
 	my $errfunc;
@@ -207,7 +209,6 @@
  
 	my $orig_pid = $$;
 
-	my $pid;
 	eval {
 	    local $ENV{LANG} = $lang;
 
@@ -228,6 +229,9 @@
 
 	die $err if $err;
 
+	local $SIG{ALRM} = sub { die "got timeout\n"; } if $timeout;
+	$oldtimeout = alarm($timeout) if $timeout;
+
 	print $writer $input if defined $input;
 	close $writer;
 
@@ -238,15 +242,11 @@
 	my $outlog = '';
 	my $errlog = '';
 
+	my $starttime = time();
+
 	while ($select->count) {
-	    my @handles = $select->can_read($timeout);
+	    my @handles = $select->can_read(1);
 
-	    if (defined ($timeout) && (scalar (@handles) == 0)) {
-		kill (9, $pid);
-		waitpid ($pid, 0);
-		die "timeout\n";
-	    }
-
 	    foreach my $h (@handles) {
 		my $buf = '';
 		my $count = sysread ($h, $buf, 4096);
@@ -314,14 +314,24 @@
 	}
 
 	print STDERR "$laststderr\n" if $laststderr;
-
+        alarm(0);
     };
 
     my $err = $@;
 
+    alarm(0);
+
     umask ($old_umask) if defined($old_umask);
 
+    alarm($oldtimeout) if $oldtimeout;
+
     if ($err) {
+	if ($pid && ($err eq "got timeout\n")) {
+	    kill (9, $pid);
+	    waitpid ($pid, 0);
+	    die "command '$cmdstr' failed: $err";
+	}
+
 	if ($errmsg) {
 	    die "$errmsg: $err";
 	} else {




More information about the pve-devel mailing list