]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
Tools/df: reuse run_fork_with_timeout
[pve-common.git] / src / PVE / Tools.pm
index f43424bb7307095debd575840b6495bb828eaf48..d5373a4246062887bd778f35f4d2ac9cf2bb8bd1 100644 (file)
@@ -369,6 +369,7 @@ sub run_command {
     my $afterfork;
     my $noerr;
     my $keeplocale;
+    my $quiet;
 
     eval {
 
@@ -395,6 +396,8 @@ sub run_command {
                $noerr = $param{$p};
            } elsif ($p eq 'keeplocale') {
                $keeplocale = $param{$p};
+           } elsif ($p eq 'quiet') {
+               $quiet = $param{$p};
            } else {
                die "got unknown parameter '$p' for run_command\n";
            }
@@ -497,7 +500,7 @@ sub run_command {
                            waitpid ($pid, 0);
                            die $err;
                        }
-                   } else {
+                   } elsif (!$quiet) {
                        print $buf;
                        *STDOUT->flush();
                    }
@@ -517,7 +520,7 @@ sub run_command {
                            waitpid ($pid, 0);
                            die $err;
                        }
-                   } else {
+                   } elsif (!$quiet) {
                        print STDERR $buf;
                        *STDERR->flush();
                    }
@@ -978,49 +981,16 @@ sub run_fork {
 sub df {
     my ($path, $timeout) = @_;
 
-    my $res = {
-       total => 0,
-       used => 0,
-       avail => 0,
-    };
-
-    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);
-    }
+    my $df = sub { return Filesys::Df::df($path, 1) };
 
-    $pipe->reader();
+    my $res = eval { run_fork_with_timeout($timeout, $df) } // {};
+    warn $@ if $@;
 
-    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);
+    return {
+       total => $res->{blocks} // 0,
+       used => $res->{used} // 0,
+       avail => $res->{bavail} // 0,
     };
-    warn $@ if $@;
-    $pipe->close();
-    kill('KILL', $child);
-    waitpid($child, 0);
-    return $res;
 }
 
 # UPID helper