X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FTools.pm;h=1a342ee04cc660f88dd567d4442ba895be5a0f43;hp=0c6dde6570bffad93003d95d47fc3a4c64f7b8d4;hb=771d18f5c7be2055f0c9012f7f921f6d5f6337db;hpb=b2613777279f5a55b1ce2b460768943d946ccb9d diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 0c6dde6..1a342ee 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -18,7 +18,7 @@ use Encode; use Digest::SHA; use Text::ParseWords; use String::ShellQuote; -use Time::HiRes qw(usleep gettimeofday tv_interval); +use Time::HiRes qw(usleep gettimeofday tv_interval alarm); # avoid warning when parsing long hex values with hex() no warnings 'portable'; # Support for 64-bit ints required @@ -63,35 +63,43 @@ our $IPV6RE = "(?:" . "(?:(?:(?:(?:$IPV6H16:){0,5}$IPV6H16)?::" . ")$IPV6H16)|" . "(?:(?:(?:(?:$IPV6H16:){0,6}$IPV6H16)?::" . ")))"; +use constant (CLONE_NEWNS => 0x00020000, + CLONE_NEWUTS => 0x04000000, + CLONE_NEWIPC => 0x08000000, + CLONE_NEWUSER => 0x10000000, + CLONE_NEWPID => 0x20000000, + CLONE_NEWNET => 0x40000000); + sub run_with_timeout { my ($timeout, $code, @param) = @_; die "got timeout\n" if $timeout <= 0; - my $prev_alarm; + my $prev_alarm = alarm 0; # suspend outer alarm early my $sigcount = 0; my $res; - local $SIG{ALRM} = sub { $sigcount++; }; # catch alarm outside eval - eval { local $SIG{ALRM} = sub { $sigcount++; die "got timeout\n"; }; local $SIG{PIPE} = sub { $sigcount++; die "broken pipe\n" }; local $SIG{__DIE__}; # see SA bug 4631 - $prev_alarm = alarm($timeout); + alarm($timeout); - $res = &$code(@param); + eval { $res = &$code(@param); }; alarm(0); # avoid race conditions + + die $@ if $@; }; my $err = $@; - alarm($prev_alarm) if defined($prev_alarm); + alarm $prev_alarm; + # this shouldn't happen anymore? die "unknown error" if $sigcount && !$err; # seems to happen sometimes die $err if $err; @@ -240,13 +248,52 @@ sub safe_read_from { return $input; } +# The $cmd parameter can be: +# -) a string +# This is generally executed by passing it to the shell with the -c option. +# However, it can be executed in one of two ways, depending on whether +# there's a pipe involved: +# *) with pipe: passed explicitly to bash -c, prefixed with: +# set -o pipefail && +# *) without a pipe: passed to perl's open3 which uses 'sh -c' +# (Note that this may result in two different syntax requirements!) +# FIXME? +# -) an array of arguments (strings) +# Will be executed without interference from a shell. (Parameters are passed +# as is, no escape sequences of strings will be touched.) +# -) an array of arrays +# Each array represents a command, and each command's output is piped into +# the following command's standard input. +# For this a shell command string is created with pipe symbols between each +# command. +# Each command is a list of strings meant to end up in the final command +# unchanged. In order to achieve this, every argument is shell-quoted. +# Quoting can be disabled for a particular argument by turning it into a +# reference, this allows inserting arbitrary shell options. +# For instance: the $cmd [ [ 'echo', 'hello', \'>/dev/null' ] ] will not +# produce any output, while the $cmd [ [ 'echo', 'hello', '>/dev/null' ] ] +# will literally print: hello >/dev/null sub run_command { my ($cmd, %param) = @_; my $old_umask; my $cmdstr; - if (!ref($cmd)) { + if (my $ref = ref($cmd)) { + if (ref($cmd->[0])) { + $cmdstr = 'set -o pipefail && '; + my $pipe = ''; + foreach my $command (@$cmd) { + # concatenate quoted parameters + # strings which are passed by reference are NOT shell quoted + $cmdstr .= $pipe . join(' ', map { ref($_) ? $$_ : shellquote($_) } @$command); + $pipe = ' | '; + } + $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmdstr" ]; + } else { + $cmdstr = cmd2string($cmd); + } + } else { $cmdstr = $cmd; if ($cmd =~ m/\|/) { # see 'man bash' for option pipefail @@ -254,8 +301,6 @@ sub run_command { } else { $cmd = [ $cmd ]; } - } else { - $cmdstr = cmd2string($cmd); } my $errmsg; @@ -1115,4 +1160,9 @@ sub parse_host_and_port { return; # nothing } +sub unshare { + my ($flags) = @_; + syscall 272, $flags; +} + 1;