X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=data%2FPVE%2FTools.pm;h=5b30aedbdbb40079e270236d2160c018c632f407;hp=fd3c0563cd233e824460e3f2703e9cd7a6c40533;hb=eb7e5538615974e1e72a92c8e5812781374df25d;hpb=c36f332e32c91a66401b1b2a5005fff8168212df diff --git a/data/PVE/Tools.pm b/data/PVE/Tools.pm index fd3c056..5b30aed 100644 --- a/data/PVE/Tools.pm +++ b/data/PVE/Tools.pm @@ -17,8 +17,11 @@ use Encode; use Digest::SHA; use Text::ParseWords; use String::ShellQuote; +use Time::HiRes qw(usleep gettimeofday tv_interval); our @EXPORT_OK = qw( +$IPV6RE +$IPV4RE lock_file lock_file_full run_command @@ -40,6 +43,22 @@ my $pvetaskdir = "$pvelogdir/tasks"; mkdir $pvelogdir; mkdir $pvetaskdir; +my $IPV4OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])"; +our $IPV4RE = "(?:(?:$IPV4OCTET\\.){3}$IPV4OCTET)"; +my $IPV6H16 = "(?:[0-9a-fA-F]{1,4})"; +my $IPV6LS32 = "(?:(?:$IPV4RE|$IPV6H16:$IPV6H16))"; + +our $IPV6RE = "(?:" . + "(?:(?:" . "(?:$IPV6H16:){6})$IPV6LS32)|" . + "(?:(?:" . "::(?:$IPV6H16:){5})$IPV6LS32)|" . + "(?:(?:(?:" . "$IPV6H16)?::(?:$IPV6H16:){4})$IPV6LS32)|" . + "(?:(?:(?:(?:$IPV6H16:){0,1}$IPV6H16)?::(?:$IPV6H16:){3})$IPV6LS32)|" . + "(?:(?:(?:(?:$IPV6H16:){0,2}$IPV6H16)?::(?:$IPV6H16:){2})$IPV6LS32)|" . + "(?:(?:(?:(?:$IPV6H16:){0,3}$IPV6H16)?::(?:$IPV6H16:){1})$IPV6LS32)|" . + "(?:(?:(?:(?:$IPV6H16:){0,4}$IPV6H16)?::" . ")$IPV6LS32)|" . + "(?:(?:(?:(?:$IPV6H16:){0,5}$IPV6H16)?::" . ")$IPV6H16)|" . + "(?:(?:(?:(?:$IPV6H16:){0,6}$IPV6H16)?::" . ")))"; + sub run_with_timeout { my ($timeout, $code, @param) = @_; @@ -556,8 +575,8 @@ my $keymaphash = { 'dk' => ['Danish', 'da', 'qwerty/dk-latin1.kmap.gz', 'dk', 'nodeadkeys'], 'de' => ['German', 'de', 'qwertz/de-latin1-nodeadkeys.kmap.gz', 'de', 'nodeadkeys' ], 'de-ch' => ['Swiss-German', 'de-ch', 'qwertz/sg-latin1.kmap.gz', 'ch', 'de_nodeadkeys' ], - 'en-gb' => ['United Kingdom', 'en-gb', 'qwerty/uk.kmap.gz' , 'gb', 'intl' ], - 'en-us' => ['U.S. English', 'en-us', 'qwerty/us-latin1.kmap.gz', 'us', 'intl' ], + 'en-gb' => ['United Kingdom', 'en-gb', 'qwerty/uk.kmap.gz' , 'gb', undef], + 'en-us' => ['U.S. English', 'en-us', 'qwerty/us-latin1.kmap.gz', 'us', undef ], 'es' => ['Spanish', 'es', 'qwerty/es.kmap.gz', 'es', 'nodeadkeys'], #'et' => [], # Ethopia or Estonia ?? 'fi' => ['Finnish', 'fi', 'qwerty/fi-latin1.kmap.gz', 'fi', 'nodeadkeys'], @@ -609,13 +628,16 @@ sub extract_param { return $res; } -# Note: we use this to wait until vncterm is ready +# Note: we use this to wait until vncterm/spiceterm is ready sub wait_for_vnc_port { my ($port, $timeout) = @_; $timeout = 5 if !$timeout; + my $sleeptime = 0; + my $starttime = [gettimeofday]; + my $elapsed; - for (my $i = 0; $i < $timeout; $i++) { + while (($elapsed = tv_interval($starttime)) < $timeout) { if (my $fh = IO::File->new ("/proc/net/tcp", "r")) { while (defined (my $line = <$fh>)) { if ($line =~ m/^\s*\d+:\s+([0-9A-Fa-f]{8}):([0-9A-Fa-f]{4})\s/) { @@ -627,7 +649,8 @@ sub wait_for_vnc_port { } close($fh); } - sleep(1); + $sleeptime += 100000 if $sleeptime < 1000000; + usleep($sleeptime); } return undef; @@ -706,6 +729,10 @@ sub next_vnc_port { return next_unused_port(5900, 6000); } +sub next_spice_port { + return next_unused_port(61000, 61099); +} + # NOTE: NFS syscall can't be interrupted, so alarm does # not work to provide timeouts. # from 'man nfs': "Only SIGKILL can interrupt a pending NFS operation" @@ -747,6 +774,8 @@ sub df { sub upid_encode { my $d = shift; + # Note: pstart can be > 32bit if uptime > 497 days, so this can result in + # more that 8 characters for pstart return sprintf("UPID:%s:%08X:%08X:%08X:%s:%s:%s:", $d->{node}, $d->{pid}, $d->{pstart}, $d->{starttime}, $d->{type}, $d->{id}, $d->{user}); @@ -759,7 +788,8 @@ sub upid_decode { my $filename; # "UPID:$node:$pid:$pstart:$startime:$dtype:$id:$user" - if ($upid =~ m/^UPID:([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/) { + # Note: allow up to 9 characters for pstart (work until 20 years uptime) + if ($upid =~ m/^UPID:([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8,9}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/) { $res->{node} = $1; $res->{pid} = hex($3); $res->{pstart} = hex($4); @@ -852,7 +882,9 @@ sub decode_utf8_parameters { sub random_ether_addr { - my $rand = Digest::SHA::sha1_hex(rand(), time()); + my ($seconds, $microseconds) = gettimeofday; + + my $rand = Digest::SHA::sha1_hex($$, rand(), $seconds, $microseconds); my $mac = ''; for (my $i = 0; $i < 6; $i++) { @@ -900,7 +932,7 @@ sub split_args { } sub dump_logfile { - my ($filename, $start, $limit) = @_; + my ($filename, $start, $limit, $filter) = @_; my $lines = []; my $count = 0; @@ -916,12 +948,25 @@ sub dump_logfile { $limit = 50 if !$limit; my $line; - while (defined($line = <$fh>)) { - next if $count++ < $start; - next if $limit <= 0; - chomp $line; - push @$lines, { n => $count, t => $line}; - $limit--; + + if ($filter) { + # duplicate code, so that we do not slow down normal path + while (defined($line = <$fh>)) { + next if $line !~ m/$filter/; + next if $count++ < $start; + next if $limit <= 0; + chomp $line; + push @$lines, { n => $count, t => $line}; + $limit--; + } + } else { + while (defined($line = <$fh>)) { + next if $count++ < $start; + next if $limit <= 0; + chomp $line; + push @$lines, { n => $count, t => $line}; + $limit--; + } } close($fh); @@ -966,4 +1011,12 @@ sub dir_glob_foreach { } } +sub assert_if_modified { + my ($digest1, $digest2) = @_; + + if ($digest1 && $digest2 && ($digest1 ne $digest2)) { + die "detected modified configuration - file change by other user? Try again.\n"; + } +} + 1;