From 7e826928250804d0ca80aa6049f6353d1059bd53 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Mon, 30 Nov 2015 09:25:20 +0100 Subject: [PATCH] run_command: return exit code and add noerr 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 --- src/PVE/Tools.pm | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index ba3f137..ae06c12 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -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 { -- 2.39.2