X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FTools.pm;h=a7bcd355f95c690348c4822f6ee56bf9b7435519;hp=0e1af0908400f07e3bb6c6f62e5acd1d8943edc6;hb=78325766900d362d492d37270e2224840a919129;hpb=a956854f8df53f62fa60e5cf3ec2d953948a2e22 diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 0e1af09..a7bcd35 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -248,7 +248,7 @@ sub run_command { if (!ref($cmd)) { $cmdstr = $cmd; - if ($cmd =~ m/|/) { + if ($cmd =~ m/\|/) { # see 'man bash' for option pipefail $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmd" ]; } else { @@ -660,7 +660,7 @@ sub wait_for_vnc_port { } sub next_unused_port { - my ($range_start, $range_end) = @_; + my ($range_start, $range_end, $family) = @_; # We use a file to register allocated ports. # Those registrations expires after $expiretime. @@ -693,10 +693,11 @@ sub next_unused_port { next if $ports->{$p}; # reserved my $sock = IO::Socket::IP->new(Listen => 5, - LocalAddr => '0.0.0.0', LocalPort => $p, ReuseAddr => 1, - Proto => 0); + Family => $family, + Proto => 0, + GetAddrInfoFlags => 0); if ($sock) { close($sock); @@ -725,15 +726,18 @@ sub next_unused_port { } sub next_migrate_port { - return next_unused_port(60000, 60050); + my ($family) = @_; + return next_unused_port(60000, 60050, $family); } sub next_vnc_port { - return next_unused_port(5900, 6000); + my ($family) = @_; + return next_unused_port(5900, 6000, $family); } sub next_spice_port { - return next_unused_port(61000, 61099); + my ($family) = @_; + return next_unused_port(61000, 61099, $family); } # NOTE: NFS syscall can't be interrupted, so alarm does @@ -984,6 +988,37 @@ sub dump_logfile { return ($count, $lines); } +sub dump_journal { + my ($start, $limit, $filter) = @_; + + my $lines = []; + my $count = 0; + + $start = 0 if !$start; + $limit = 50 if !$limit; + + my $parser = sub { + my $line = shift; + + return if $count++ < $start; + return if $limit <= 0; + push @$lines, { n => int($count), t => $line}; + $limit--; + }; + + my $cmd = ['journalctl', '-o', 'short', '--no-pager']; + run_command($cmd, outfunc => $parser); + + # HACK: ExtJS store.guaranteeRange() does not like empty array + # so we add a line + if (!$count) { + $count++; + push @$lines, { n => $count, t => "no content"}; + } + + return ($count, $lines); +} + sub dir_glob_regex { my ($dir, $regex) = @_; @@ -1052,14 +1087,19 @@ sub unpack_sockaddr_in46 { return ($family, $port, $host); } -sub get_host_address_family { - my ($hostname, $socktype) = @_; +sub getaddrinfo_all { + my ($hostname, @opts) = @_; my %hints = ( flags => AI_V4MAPPED | AI_ALL, - socktype => $socktype ); + @opts ); my ($err, @res) = Socket::getaddrinfo($hostname, '0', \%hints); - die "failed to resolve $hostname: $err\n" if $err; + die "failed to get address info for: $hostname: $err\n" if $err; + return @res; +} - return ${res[0]}->{family}; +sub get_host_address_family { + my ($hostname, $socktype) = @_; + my @res = getaddrinfo_all($hostname, socktype => $socktype); + return $res[0]->{family}; } 1;