]> git.proxmox.com Git - pve-storage.git/commitdiff
import: allow import from UNIX socket
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 13 Apr 2021 12:16:33 +0000 (14:16 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 16 Apr 2021 10:23:43 +0000 (12:23 +0200)
this allows forwarding over websockets without requiring a (free) port.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
PVE/CLI/pvesm.pm

index 7b46897e5f81421134d90d38cf37b84e036e886c..9206188fc58b4fbe0e1d33a33c3fbf5930289548 100755 (executable)
@@ -7,6 +7,10 @@ use POSIX qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
 use Fcntl ':flock';
 use File::Path;
 
+use IO::Socket::IP;
+use IO::Socket::UNIX;
+use Socket qw(SOCK_STREAM);
+
 use PVE::SafeSyslog;
 use PVE::Cluster;
 use PVE::INotify;
@@ -314,7 +318,8 @@ __PACKAGE__->register_method ({
            },
            filename => {
                description => "Source file name. For '-' stdin is used, the " .
-                 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
+                 "tcp://<IP-or-CIDR> format allows to use a TCP connection, " .
+                 "the unix://PATH-TO-SOCKET format a UNIX socket as input." .
                  "Else, the file is treated as common file.",
                type => 'string',
            },
@@ -392,6 +397,25 @@ __PACKAGE__->register_method ({
            alarm $prev_alarm;
            close($socket);
 
+           $infh = \*$client;
+       } elsif ($filename =~ m!^unix://(.*)$!) {
+           my $socket_path = $1;
+           my $socket = IO::Socket::UNIX->new(
+               Type => SOCK_STREAM(),
+               Local => $socket_path,
+               Listen => 1,
+           ) or die "failed to open socket: $!\n";
+
+           print "ready\n";
+           *STDOUT->flush();
+
+           my $prev_alarm = alarm 0;
+           local $SIG{ALRM} = sub { die "timed out waiting for client\n" };
+           alarm 30;
+           my $client = $socket->accept; # Wait for a client
+           alarm $prev_alarm;
+           close($socket);
+
            $infh = \*$client;
        } else {
            sysopen($infh, $filename, O_RDONLY)