From 590b924e19426c93e0c7f4b969ff80eed66ab29e Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Mon, 4 Dec 2017 11:30:11 +0100 Subject: [PATCH] wait_for_vnc_port: allow to enforce IP family Most times a port was requested for a specified IP family (v4, v6) only. Thus also ensure that the port from the respective family got ready, else we may return on a false positive. As we had no user setting the $timeout param we can add the $family param as second one, it'll get used more often, so no need to put it at the back. As we do nothing if not defined this does not changes the behavior of our users yet. Signed-off-by: Thomas Lamprecht --- src/PVE/Tools.pm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 2c64f6a..6d579d8 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -791,17 +791,20 @@ sub extract_param { # Note: we use this to wait until vncterm/spiceterm is ready sub wait_for_vnc_port { - my ($port, $timeout) = @_; + my ($port, $family, $timeout) = @_; $timeout = 5 if !$timeout; my $sleeptime = 0; my $starttime = [gettimeofday]; my $elapsed; + my $cmd = ['/bin/ss', '-Htln', "sport = :$port"]; + push @$cmd, $family == AF_INET6 ? '-6' : '-4' if defined($family); + my $found; while (($elapsed = tv_interval($starttime)) < $timeout) { # -Htln = don't print header, tcp, listening sockets only, numeric ports - run_command(['/bin/ss', '-Htln', "sport = :$port"], outfunc => sub { + run_command($cmd, outfunc => sub { my $line = shift; if ($line =~ m/^LISTEN\s+\d+\s+\d+\s+\S+:(\d+)\s/) { $found = 1 if ($port == $1); -- 2.39.2