From 1ebd925dcfdebadb9b1831652e73aaaf484b75a8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fabian=20Gr=C3=BCnbichler?= Date: Tue, 13 Apr 2021 14:16:33 +0200 Subject: [PATCH] import: allow import from UNIX socket MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit this allows forwarding over websockets without requiring a (free) port. Signed-off-by: Fabian Grünbichler --- PVE/CLI/pvesm.pm | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/PVE/CLI/pvesm.pm b/PVE/CLI/pvesm.pm index 7b46897..9206188 100755 --- a/PVE/CLI/pvesm.pm +++ b/PVE/CLI/pvesm.pm @@ -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:// format allows to use a TCP connection as input. " . + "tcp:// 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) -- 2.39.2