]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
createSchema: include type property
[pve-common.git] / src / PVE / Tools.pm
index 4c35abc54b74a19aa999682c0b7b9c63303aa721..1a342ee04cc660f88dd567d4442ba895be5a0f43 100644 (file)
@@ -63,6 +63,13 @@ 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) = @_;
 
@@ -254,13 +261,39 @@ sub safe_read_from {
 #  -) 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
@@ -268,8 +301,6 @@ sub run_command {
        } else {
            $cmd = [ $cmd ];
        }
-    } else {
-       $cmdstr = cmd2string($cmd);
     }
 
     my $errmsg;
@@ -1129,4 +1160,9 @@ sub parse_host_and_port {
     return; # nothing
 }
 
+sub unshare {
+    my ($flags) = @_;
+    syscall 272, $flags;
+}
+
 1;