[pve-devel] [PATCH common] Tools/df: reuse run_fork_with_timeout

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Dec 18 14:58:26 CET 2017


Use the later added run_fork_with_timeout for encapsulating df in a
safely manner.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/Tools.pm | 49 ++++++++-----------------------------------------
 1 file changed, 8 insertions(+), 41 deletions(-)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index f43424b..7efd0dd 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -978,49 +978,16 @@ sub run_fork {
 sub df {
     my ($path, $timeout) = @_;
 
-    my $res = {
-	total => 0,
-	used => 0,
-	avail => 0,
-    };
+    my $df = sub { return Filesys::Df::df($path, 1) };
 
-    my $pipe = IO::Pipe->new();
-    my $child = fork();
-    if (!defined($child)) {
-	warn "fork failed: $!\n";
-	return $res;
-    }
-
-    if (!$child) {
-	$pipe->writer();
-	eval {
-	    my $df = Filesys::Df::df($path, 1);
-	    print {$pipe} "$df->{blocks}\n$df->{used}\n$df->{bavail}\n"
-		if defined($df);
-	    $pipe->close();
-	};
-	if (my $err = $@) {
-	    warn $err;
-	    POSIX::_exit(1);
-	}
-	POSIX::_exit(0);
-    }
-
-    $pipe->reader();
-
-    my $readvalues = sub {
-	$res->{total} = int(((<$pipe> // 0) =~ /^(\d*)$/)[0]);
-	$res->{used}  = int(((<$pipe> // 0) =~ /^(\d*)$/)[0]);
-	$res->{avail} = int(((<$pipe> // 0) =~ /^(\d*)$/)[0]);
-    };
-    eval {
-	run_with_timeout($timeout, $readvalues);
-    };
+    my $res = eval { run_fork_with_timeout($timeout, $df) } // {};
     warn $@ if $@;
-    $pipe->close();
-    kill('KILL', $child);
-    waitpid($child, 0);
-    return $res;
+
+    return {
+	total => $res->{blocks} // 0,
+	used => $res->{used} // 0,
+	avail => $res->{bavail} // 0,
+    };
 }
 
 # UPID helper
-- 
2.11.0





More information about the pve-devel mailing list