fix insecure migration failing if waiting on lock

both STDOUT and STDERR are written into `$info` which is then parsed for
IP and port of the target socket listening.
when the ports file can't be locked immediately `trying to acquire
lock...` is printed on STDERR and in turn written into `$info`.
trying to parse the IP then fails, resulting in a migration or
replication failing.

the bare open3 call is replaced by the run_command wrapper from
pve-common to use a safe wrapper around open3 with the same
functionality.
STDERR is read separatey from STDOUT and the last line of STDERR is
kept in case of errors.

Fixes: 57acd6a ("fix #1452: also log stderr of remote command with
insecure storage migration")

Signed-off-by: Mira Limbeck <m.limbeck@proxmox.com>
This commit is contained in:
Mira Limbeck
2024-04-17 11:48:57 +02:00
committed by Thomas Lamprecht
parent d47460eec7
commit b6fc9de14a

View File

@ -851,43 +851,62 @@ sub storage_migrate {
eval { eval {
if ($insecure) { if ($insecure) {
my $input = IO::File->new(); my $ip;
my $info = IO::File->new(); my $port;
open3($input, $info, $info, @$recv) my $socket;
or die "receive command failed: $!\n"; my $send_error;
close($input);
my $try_ip = <$info> // ''; my $handle_insecure_migration = sub {
my ($ip) = $try_ip =~ /^($PVE::Tools::IPRE)$/ # untaint my $line = shift;
or die "no tunnel IP received, got '$try_ip'\n";
my $try_port = <$info> // ''; if (!$ip) {
my ($port) = $try_port =~ /^(\d+)$/ # untaint ($ip) = $line =~ /^($PVE::Tools::IPRE)$/ # untaint
or die "no tunnel port received, got '$try_port'\n"; or die "no tunnel IP received, got '$line'\n";
} elsif (!$port) {
($port) = $line =~ /^(\d+)$/ # untaint
or die "no tunnel port received, got '$line'\n";
my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM) # create socket, run command
or die "failed to connect to tunnel at $ip:$port\n"; $socket = IO::Socket::IP->new(
# we won't be reading from the socket PeerHost => $ip,
shutdown($socket, 0); PeerPort => $port,
Type => SOCK_STREAM,
);
die "failed to connect to tunnel at $ip:$port\n" if !$socket;
# we won't be reading from the socket
shutdown($socket, 0);
eval { run_command($cmds, output => '>&'.fileno($socket), errfunc => $match_volid_and_log); }; eval {
my $send_error = $@; run_command(
$cmds,
output => '>&'.fileno($socket),
errfunc => $match_volid_and_log,
);
};
$send_error = $@;
# don't close the connection entirely otherwise the receiving end # don't close the connection entirely otherwise the receiving end
# might not get all buffered data (and fails with 'connection reset by peer') # might not get all buffered data (and fails with 'connection reset by peer')
shutdown($socket, 1); shutdown($socket, 1);
} else {
$match_volid_and_log->("[$target_sshinfo->{name}] $line");
}
};
# wait for the remote process to finish eval {
while (my $line = <$info>) { run_command(
$match_volid_and_log->("[$target_sshinfo->{name}] $line"); $recv,
} outfunc => $handle_insecure_migration,
errfunc => sub {
my $line = shift;
# now close the socket $match_volid_and_log->("[$target_sshinfo->{name}] $line");
close($socket); },
if (!close($info)) { # does waitpid() );
die "import failed: $!\n" if $!; };
die "import failed: exit code ".($?>>8)."\n"; my $recv_err = $@;
} close($socket) if $socket;
die "failed to run insecure migration: $recv_err\n" if $recv_err;
die $send_error if $send_error; die $send_error if $send_error;
} else { } else {