]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/Tools.pm
next_migrate_port: higher port range - all other ranges are 100 ports
[pve-common.git] / data / PVE / Tools.pm
index f8e9c4773dbe94ed7e57d1c9de5102d96410b85e..827ca58bcfec494e823b6d957599966f6b548ce7 100644 (file)
@@ -19,6 +19,9 @@ use Text::ParseWords;
 use String::ShellQuote;
 use Time::HiRes qw(usleep gettimeofday tv_interval);
 
+# avoid warning when parsing long hex values with hex()
+no warnings 'portable'; # Support for 64-bit ints required
+
 our @EXPORT_OK = qw(
 $IPV6RE
 $IPV4RE
@@ -142,8 +145,7 @@ sub lock_file_full {
        $err = $@;
     }
 
-    if ($lock_handles->{$$}->{$filename}) {
-        my $fh = $lock_handles->{$$}->{$filename};
+    if (my $fh = $lock_handles->{$$}->{$filename}) {
         $lock_handles->{$$}->{$filename} = undef;
         close ($fh);
     }
@@ -690,7 +692,7 @@ sub next_unused_port {
            next if $ports->{$p}; # reserved
 
            my $sock = IO::Socket::INET->new(Listen => 5,
-                                            LocalAddr => 'localhost',
+                                            LocalAddr => '0.0.0.0',
                                             LocalPort => $p,
                                             ReuseAddr => 1,
                                             Proto     => 0);
@@ -722,7 +724,7 @@ sub next_unused_port {
 }
 
 sub next_migrate_port {
-    return next_unused_port(60000, 60010);
+    return next_unused_port(60000, 60050);
 }
 
 sub next_vnc_port {
@@ -882,7 +884,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++) {
@@ -930,7 +934,7 @@ sub split_args {
 }
 
 sub dump_logfile {
-    my ($filename, $start, $limit) = @_;
+    my ($filename, $start, $limit, $filter) = @_;
 
     my $lines = [];
     my $count = 0;
@@ -946,12 +950,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);
@@ -996,4 +1013,34 @@ sub dir_glob_foreach {
     } 
 }
 
+sub assert_if_modified {
+    my ($digest1, $digest2) = @_;
+
+    if ($digest1 && $digest2 && ($digest1 ne $digest2)) {
+       die "detected modified configuration - file changed by other user? Try again.\n";
+    }
+}
+
+# Digest for short strings
+# like FNV32a, but we only return 31 bits (positive numbers)
+sub fnv31a {
+    my ($string) = @_;
+
+    my $hval = 0x811c9dc5;
+
+    foreach my $c (unpack('C*', $string)) {
+       $hval ^= $c;
+       $hval += (
+           (($hval << 1) ) +
+           (($hval << 4) ) +
+           (($hval << 7) ) +
+           (($hval << 8) ) +
+           (($hval << 24) ) );
+       $hval = $hval & 0xffffffff;
+    }
+    return $hval & 0x7fffffff;
+}
+
+sub fnv31a_hex { return sprintf("%X", fnv31a(@_)); }
+
 1;