]> git.proxmox.com Git - pve-common.git/commitdiff
run_command: return exit code and add noerr
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 30 Nov 2015 08:25:20 +0000 (09:25 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 30 Nov 2015 11:59:45 +0000 (12:59 +0100)
Allow to return the exit code of the executed command.
And as we do not reach the return of the exit code if it was not 0,
a noerr parameter is also needed so we can suppress the 'command
failed' die in case of an exit code unequal to 0.

This is required as some programs return another value than 0 when
they succeed, For example `systemctl list-jobs` returns  a value
>= 0 on a successful execution, normally 1.
Without this patch a run_command call to `systemctl list-jobs` gets
marked as failed although it was successful.

This does not break current behaviour in any way as setting the
noerr parameter is required to return something other than 0 or
undef, which are equal in a boolean comparison.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/Tools.pm

index ba3f13702bff4faf2409e4b78ae35bd35675d012..ae06c12b224b873dc7a27d5965078f54a8d9a851 100644 (file)
@@ -319,6 +319,7 @@ sub run_command {
     my $timeout;
     my $oldtimeout;
     my $pid;
+    my $exitcode;
 
     my $outfunc;
     my $errfunc;
@@ -326,6 +327,7 @@ sub run_command {
     my $input;
     my $output;
     my $afterfork;
+    my $noerr;
 
     eval {
 
@@ -348,6 +350,8 @@ sub run_command {
                $logfunc = $param{$p};
            } elsif ($p eq 'afterfork') {
                $afterfork = $param{$p};
+           } elsif ($p eq 'noerr') {
+               $noerr = $param{$p};
            } else {
                die "got unknown parameter '$p' for run_command\n";
            }
@@ -493,14 +497,14 @@ sub run_command {
            die "failed to execute\n";
        } elsif (my $sig = ($? & 127)) {
            die "got signal $sig\n";
-       } elsif (my $ec = ($? >> 8)) {
-           if (!($ec == 24 && ($cmdstr =~ m|^(\S+/)?rsync\s|))) {
+       } elsif ($exitcode = ($? >> 8)) {
+           if (!($exitcode == 24 && ($cmdstr =~ m|^(\S+/)?rsync\s|))) {
                if ($errmsg && $laststderr) {
                    my $lerr = $laststderr;
                    $laststderr = undef;
                    die "$lerr\n";
                }
-               die "exit code $ec\n";
+               die "exit code $exitcode\n";
            }
        }
 
@@ -529,12 +533,12 @@ sub run_command {
        if ($errmsg) {
            $err =~ s/^usermod:\s*// if $cmdstr =~ m|^(\S+/)?usermod\s|;
            die "$errmsg: $err";
-       } else {
+       } elsif(!$noerr) {
            die "command '$cmdstr' failed: $err";
        }
     }
 
-    return undef;
+    return $exitcode;
 }
 
 sub split_list {