]>
Commit | Line | Data |
---|---|---|
e143e9d8 DM |
1 | package PVE::Tools; |
2 | ||
3 | use strict; | |
c36f332e | 4 | use warnings; |
b5d12b08 | 5 | use POSIX qw(EINTR); |
00dc9d0f | 6 | use IO::Socket::IP; |
a956854f | 7 | use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED); |
e143e9d8 DM |
8 | use IO::Select; |
9 | use File::Basename; | |
10 | use File::Path qw(make_path); | |
97c8c857 WB |
11 | use Filesys::Df (); # don't overwrite our df() |
12 | use IO::Pipe; | |
e143e9d8 | 13 | use IO::File; |
7eb283fb | 14 | use IO::Dir; |
e143e9d8 DM |
15 | use IPC::Open3; |
16 | use Fcntl qw(:DEFAULT :flock); | |
17 | use base 'Exporter'; | |
18 | use URI::Escape; | |
19 | use Encode; | |
568ba6a4 | 20 | use Digest::SHA; |
e38bcd35 | 21 | use Text::ParseWords; |
7514b23a | 22 | use String::ShellQuote; |
c38cea65 | 23 | use Time::HiRes qw(usleep gettimeofday tv_interval alarm); |
e143e9d8 | 24 | |
57eeea0c DM |
25 | # avoid warning when parsing long hex values with hex() |
26 | no warnings 'portable'; # Support for 64-bit ints required | |
27 | ||
e143e9d8 | 28 | our @EXPORT_OK = qw( |
602ec0cd DM |
29 | $IPV6RE |
30 | $IPV4RE | |
e143e9d8 | 31 | lock_file |
493004a2 | 32 | lock_file_full |
e143e9d8 DM |
33 | run_command |
34 | file_set_contents | |
35 | file_get_contents | |
36 | file_read_firstline | |
7eb283fb DM |
37 | dir_glob_regex |
38 | dir_glob_foreach | |
e143e9d8 DM |
39 | split_list |
40 | template_replace | |
41 | safe_print | |
42 | trim | |
43 | extract_param | |
44 | ); | |
45 | ||
46 | my $pvelogdir = "/var/log/pve"; | |
47 | my $pvetaskdir = "$pvelogdir/tasks"; | |
48 | ||
49 | mkdir $pvelogdir; | |
50 | mkdir $pvetaskdir; | |
51 | ||
cd9bd252 | 52 | my $IPV4OCTET = "(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])"; |
602ec0cd DM |
53 | our $IPV4RE = "(?:(?:$IPV4OCTET\\.){3}$IPV4OCTET)"; |
54 | my $IPV6H16 = "(?:[0-9a-fA-F]{1,4})"; | |
55 | my $IPV6LS32 = "(?:(?:$IPV4RE|$IPV6H16:$IPV6H16))"; | |
56 | ||
57 | our $IPV6RE = "(?:" . | |
58 | "(?:(?:" . "(?:$IPV6H16:){6})$IPV6LS32)|" . | |
59 | "(?:(?:" . "::(?:$IPV6H16:){5})$IPV6LS32)|" . | |
60 | "(?:(?:(?:" . "$IPV6H16)?::(?:$IPV6H16:){4})$IPV6LS32)|" . | |
61 | "(?:(?:(?:(?:$IPV6H16:){0,1}$IPV6H16)?::(?:$IPV6H16:){3})$IPV6LS32)|" . | |
62 | "(?:(?:(?:(?:$IPV6H16:){0,2}$IPV6H16)?::(?:$IPV6H16:){2})$IPV6LS32)|" . | |
63 | "(?:(?:(?:(?:$IPV6H16:){0,3}$IPV6H16)?::(?:$IPV6H16:){1})$IPV6LS32)|" . | |
64 | "(?:(?:(?:(?:$IPV6H16:){0,4}$IPV6H16)?::" . ")$IPV6LS32)|" . | |
65 | "(?:(?:(?:(?:$IPV6H16:){0,5}$IPV6H16)?::" . ")$IPV6H16)|" . | |
66 | "(?:(?:(?:(?:$IPV6H16:){0,6}$IPV6H16)?::" . ")))"; | |
67 | ||
176b1186 WB |
68 | our $IPRE = "(?:$IPV4RE|$IPV6RE)"; |
69 | ||
be8f0477 | 70 | use constant {CLONE_NEWNS => 0x00020000, |
952fd95e WB |
71 | CLONE_NEWUTS => 0x04000000, |
72 | CLONE_NEWIPC => 0x08000000, | |
73 | CLONE_NEWUSER => 0x10000000, | |
74 | CLONE_NEWPID => 0x20000000, | |
be8f0477 | 75 | CLONE_NEWNET => 0x40000000}; |
952fd95e | 76 | |
0f0990f1 DM |
77 | sub run_with_timeout { |
78 | my ($timeout, $code, @param) = @_; | |
e143e9d8 | 79 | |
0f0990f1 | 80 | die "got timeout\n" if $timeout <= 0; |
e143e9d8 | 81 | |
c38cea65 | 82 | my $prev_alarm = alarm 0; # suspend outer alarm early |
0f0990f1 DM |
83 | |
84 | my $sigcount = 0; | |
e143e9d8 DM |
85 | |
86 | my $res; | |
87 | ||
e143e9d8 | 88 | eval { |
0f0990f1 DM |
89 | local $SIG{ALRM} = sub { $sigcount++; die "got timeout\n"; }; |
90 | local $SIG{PIPE} = sub { $sigcount++; die "broken pipe\n" }; | |
91 | local $SIG{__DIE__}; # see SA bug 4631 | |
e143e9d8 | 92 | |
c38cea65 | 93 | alarm($timeout); |
e143e9d8 | 94 | |
c38cea65 | 95 | eval { $res = &$code(@param); }; |
0f0990f1 DM |
96 | |
97 | alarm(0); # avoid race conditions | |
c38cea65 WB |
98 | |
99 | die $@ if $@; | |
0f0990f1 DM |
100 | }; |
101 | ||
102 | my $err = $@; | |
103 | ||
c38cea65 | 104 | alarm $prev_alarm; |
0f0990f1 | 105 | |
c38cea65 | 106 | # this shouldn't happen anymore? |
0f0990f1 DM |
107 | die "unknown error" if $sigcount && !$err; # seems to happen sometimes |
108 | ||
109 | die $err if $err; | |
110 | ||
111 | return $res; | |
112 | } | |
e143e9d8 | 113 | |
0f0990f1 DM |
114 | # flock: we use one file handle per process, so lock file |
115 | # can be called multiple times and succeeds for the same process. | |
116 | ||
117 | my $lock_handles = {}; | |
118 | ||
493004a2 DM |
119 | sub lock_file_full { |
120 | my ($filename, $timeout, $shared, $code, @param) = @_; | |
0f0990f1 DM |
121 | |
122 | $timeout = 10 if !$timeout; | |
123 | ||
493004a2 DM |
124 | my $mode = $shared ? LOCK_SH : LOCK_EX; |
125 | ||
0f0990f1 | 126 | my $lock_func = sub { |
e143e9d8 DM |
127 | if (!$lock_handles->{$$}->{$filename}) { |
128 | $lock_handles->{$$}->{$filename} = new IO::File (">>$filename") || | |
0f0990f1 | 129 | die "can't open file - $!\n"; |
e143e9d8 DM |
130 | } |
131 | ||
493004a2 | 132 | if (!flock ($lock_handles->{$$}->{$filename}, $mode|LOCK_NB)) { |
e143e9d8 | 133 | print STDERR "trying to aquire lock..."; |
b5d12b08 DM |
134 | my $success; |
135 | while(1) { | |
493004a2 | 136 | $success = flock($lock_handles->{$$}->{$filename}, $mode); |
b5d12b08 DM |
137 | # try again on EINTR (see bug #273) |
138 | if ($success || ($! != EINTR)) { | |
139 | last; | |
140 | } | |
141 | } | |
142 | if (!$success) { | |
e143e9d8 | 143 | print STDERR " failed\n"; |
0f0990f1 | 144 | die "can't aquire lock - $!\n"; |
e143e9d8 DM |
145 | } |
146 | print STDERR " OK\n"; | |
147 | } | |
e143e9d8 DM |
148 | }; |
149 | ||
0f0990f1 | 150 | my $res; |
e143e9d8 | 151 | |
0f0990f1 DM |
152 | eval { run_with_timeout($timeout, $lock_func); }; |
153 | my $err = $@; | |
154 | if ($err) { | |
155 | $err = "can't lock file '$filename' - $err"; | |
156 | } else { | |
157 | eval { $res = &$code(@param) }; | |
158 | $err = $@; | |
159 | } | |
e143e9d8 | 160 | |
b192b930 | 161 | if (my $fh = $lock_handles->{$$}->{$filename}) { |
e143e9d8 DM |
162 | $lock_handles->{$$}->{$filename} = undef; |
163 | close ($fh); | |
164 | } | |
165 | ||
166 | if ($err) { | |
167 | $@ = $err; | |
168 | return undef; | |
169 | } | |
170 | ||
171 | $@ = undef; | |
172 | ||
173 | return $res; | |
174 | } | |
175 | ||
493004a2 DM |
176 | |
177 | sub lock_file { | |
178 | my ($filename, $timeout, $code, @param) = @_; | |
179 | ||
180 | return lock_file_full($filename, $timeout, 0, $code, @param); | |
181 | } | |
182 | ||
e143e9d8 DM |
183 | sub file_set_contents { |
184 | my ($filename, $data, $perm) = @_; | |
185 | ||
186 | $perm = 0644 if !defined($perm); | |
187 | ||
188 | my $tmpname = "$filename.tmp.$$"; | |
189 | ||
190 | eval { | |
191 | my $fh = IO::File->new($tmpname, O_WRONLY|O_CREAT, $perm); | |
192 | die "unable to open file '$tmpname' - $!\n" if !$fh; | |
193 | die "unable to write '$tmpname' - $!\n" unless print $fh $data; | |
194 | die "closing file '$tmpname' failed - $!\n" unless close $fh; | |
195 | }; | |
196 | my $err = $@; | |
197 | ||
198 | if ($err) { | |
199 | unlink $tmpname; | |
200 | die $err; | |
201 | } | |
202 | ||
203 | if (!rename($tmpname, $filename)) { | |
204 | my $msg = "close (rename) atomic file '$filename' failed: $!\n"; | |
205 | unlink $tmpname; | |
206 | die $msg; | |
207 | } | |
208 | } | |
209 | ||
210 | sub file_get_contents { | |
211 | my ($filename, $max) = @_; | |
212 | ||
213 | my $fh = IO::File->new($filename, "r") || | |
214 | die "can't open '$filename' - $!\n"; | |
215 | ||
216 | my $content = safe_read_from($fh, $max); | |
217 | ||
218 | close $fh; | |
219 | ||
220 | return $content; | |
221 | } | |
222 | ||
223 | sub file_read_firstline { | |
224 | my ($filename) = @_; | |
225 | ||
226 | my $fh = IO::File->new ($filename, "r"); | |
227 | return undef if !$fh; | |
228 | my $res = <$fh>; | |
88955a2e | 229 | chomp $res if $res; |
e143e9d8 DM |
230 | $fh->close; |
231 | return $res; | |
232 | } | |
233 | ||
234 | sub safe_read_from { | |
235 | my ($fh, $max, $oneline) = @_; | |
236 | ||
237 | $max = 32768 if !$max; | |
238 | ||
239 | my $br = 0; | |
240 | my $input = ''; | |
241 | my $count; | |
242 | while ($count = sysread($fh, $input, 8192, $br)) { | |
243 | $br += $count; | |
244 | die "input too long - aborting\n" if $br > $max; | |
245 | if ($oneline && $input =~ m/^(.*)\n/) { | |
246 | $input = $1; | |
247 | last; | |
248 | } | |
249 | } | |
250 | die "unable to read input - $!\n" if !defined($count); | |
251 | ||
252 | return $input; | |
253 | } | |
254 | ||
bd9c3a36 WB |
255 | # The $cmd parameter can be: |
256 | # -) a string | |
257 | # This is generally executed by passing it to the shell with the -c option. | |
258 | # However, it can be executed in one of two ways, depending on whether | |
259 | # there's a pipe involved: | |
260 | # *) with pipe: passed explicitly to bash -c, prefixed with: | |
261 | # set -o pipefail && | |
262 | # *) without a pipe: passed to perl's open3 which uses 'sh -c' | |
263 | # (Note that this may result in two different syntax requirements!) | |
264 | # FIXME? | |
265 | # -) an array of arguments (strings) | |
266 | # Will be executed without interference from a shell. (Parameters are passed | |
267 | # as is, no escape sequences of strings will be touched.) | |
fcdc0cfc WB |
268 | # -) an array of arrays |
269 | # Each array represents a command, and each command's output is piped into | |
270 | # the following command's standard input. | |
271 | # For this a shell command string is created with pipe symbols between each | |
272 | # command. | |
273 | # Each command is a list of strings meant to end up in the final command | |
274 | # unchanged. In order to achieve this, every argument is shell-quoted. | |
275 | # Quoting can be disabled for a particular argument by turning it into a | |
276 | # reference, this allows inserting arbitrary shell options. | |
277 | # For instance: the $cmd [ [ 'echo', 'hello', \'>/dev/null' ] ] will not | |
278 | # produce any output, while the $cmd [ [ 'echo', 'hello', '>/dev/null' ] ] | |
279 | # will literally print: hello >/dev/null | |
e143e9d8 DM |
280 | sub run_command { |
281 | my ($cmd, %param) = @_; | |
282 | ||
283 | my $old_umask; | |
ded47a61 | 284 | my $cmdstr; |
e143e9d8 | 285 | |
fcdc0cfc WB |
286 | if (my $ref = ref($cmd)) { |
287 | if (ref($cmd->[0])) { | |
288 | $cmdstr = 'set -o pipefail && '; | |
289 | my $pipe = ''; | |
290 | foreach my $command (@$cmd) { | |
291 | # concatenate quoted parameters | |
292 | # strings which are passed by reference are NOT shell quoted | |
293 | $cmdstr .= $pipe . join(' ', map { ref($_) ? $$_ : shellquote($_) } @$command); | |
294 | $pipe = ' | '; | |
295 | } | |
296 | $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmdstr" ]; | |
297 | } else { | |
298 | $cmdstr = cmd2string($cmd); | |
299 | } | |
300 | } else { | |
ded47a61 | 301 | $cmdstr = $cmd; |
22d4efe6 | 302 | if ($cmd =~ m/\|/) { |
e0cabd2c DM |
303 | # see 'man bash' for option pipefail |
304 | $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmd" ]; | |
305 | } else { | |
306 | $cmd = [ $cmd ]; | |
307 | } | |
ded47a61 | 308 | } |
e143e9d8 DM |
309 | |
310 | my $errmsg; | |
311 | my $laststderr; | |
312 | my $timeout; | |
313 | my $oldtimeout; | |
314 | my $pid; | |
315 | ||
4630cb95 DM |
316 | my $outfunc; |
317 | my $errfunc; | |
318 | my $logfunc; | |
319 | my $input; | |
320 | my $output; | |
a417477c | 321 | my $afterfork; |
4630cb95 | 322 | |
e143e9d8 | 323 | eval { |
e143e9d8 DM |
324 | |
325 | foreach my $p (keys %param) { | |
326 | if ($p eq 'timeout') { | |
327 | $timeout = $param{$p}; | |
328 | } elsif ($p eq 'umask') { | |
eb9e24df | 329 | $old_umask = umask($param{$p}); |
e143e9d8 DM |
330 | } elsif ($p eq 'errmsg') { |
331 | $errmsg = $param{$p}; | |
e143e9d8 DM |
332 | } elsif ($p eq 'input') { |
333 | $input = $param{$p}; | |
1c50a24a DM |
334 | } elsif ($p eq 'output') { |
335 | $output = $param{$p}; | |
e143e9d8 DM |
336 | } elsif ($p eq 'outfunc') { |
337 | $outfunc = $param{$p}; | |
338 | } elsif ($p eq 'errfunc') { | |
339 | $errfunc = $param{$p}; | |
776fbfa8 DM |
340 | } elsif ($p eq 'logfunc') { |
341 | $logfunc = $param{$p}; | |
a417477c DM |
342 | } elsif ($p eq 'afterfork') { |
343 | $afterfork = $param{$p}; | |
e143e9d8 DM |
344 | } else { |
345 | die "got unknown parameter '$p' for run_command\n"; | |
346 | } | |
347 | } | |
348 | ||
4630cb95 DM |
349 | if ($errmsg) { |
350 | my $origerrfunc = $errfunc; | |
351 | $errfunc = sub { | |
352 | if ($laststderr) { | |
353 | if ($origerrfunc) { | |
354 | &$origerrfunc("$laststderr\n"); | |
355 | } else { | |
356 | print STDERR "$laststderr\n" if $laststderr; | |
357 | } | |
358 | } | |
359 | $laststderr = shift; | |
360 | }; | |
361 | } | |
362 | ||
1c50a24a DM |
363 | my $reader = $output && $output =~ m/^>&/ ? $output : IO::File->new(); |
364 | my $writer = $input && $input =~ m/^<&/ ? $input : IO::File->new(); | |
365 | my $error = IO::File->new(); | |
366 | ||
e143e9d8 DM |
367 | # try to avoid locale related issues/warnings |
368 | my $lang = $param{lang} || 'C'; | |
369 | ||
370 | my $orig_pid = $$; | |
371 | ||
372 | eval { | |
329351e2 | 373 | local $ENV{LC_ALL} = $lang; |
e143e9d8 DM |
374 | |
375 | # suppress LVM warnings like: "File descriptor 3 left open"; | |
376 | local $ENV{LVM_SUPPRESS_FD_WARNINGS} = "1"; | |
377 | ||
378 | $pid = open3($writer, $reader, $error, @$cmd) || die $!; | |
f38995ab DM |
379 | |
380 | # if we pipe fron STDIN, open3 closes STDIN, so we we | |
381 | # a perl warning "Filehandle STDIN reopened as GENXYZ .. " | |
382 | # as soon as we open a new file. | |
383 | # to avoid that we open /dev/null | |
384 | if (!ref($writer) && !defined(fileno(STDIN))) { | |
385 | POSIX::close(0); | |
386 | open(STDIN, "</dev/null"); | |
387 | } | |
e143e9d8 DM |
388 | }; |
389 | ||
390 | my $err = $@; | |
391 | ||
392 | # catch exec errors | |
393 | if ($orig_pid != $$) { | |
394 | warn "ERROR: $err"; | |
395 | POSIX::_exit (1); | |
396 | kill ('KILL', $$); | |
397 | } | |
398 | ||
399 | die $err if $err; | |
400 | ||
401 | local $SIG{ALRM} = sub { die "got timeout\n"; } if $timeout; | |
402 | $oldtimeout = alarm($timeout) if $timeout; | |
403 | ||
a417477c DM |
404 | &$afterfork() if $afterfork; |
405 | ||
f38995ab DM |
406 | if (ref($writer)) { |
407 | print $writer $input if defined $input; | |
408 | close $writer; | |
409 | } | |
e143e9d8 DM |
410 | |
411 | my $select = new IO::Select; | |
f38995ab | 412 | $select->add($reader) if ref($reader); |
e143e9d8 DM |
413 | $select->add($error); |
414 | ||
415 | my $outlog = ''; | |
416 | my $errlog = ''; | |
417 | ||
418 | my $starttime = time(); | |
419 | ||
420 | while ($select->count) { | |
421 | my @handles = $select->can_read(1); | |
422 | ||
423 | foreach my $h (@handles) { | |
424 | my $buf = ''; | |
425 | my $count = sysread ($h, $buf, 4096); | |
426 | if (!defined ($count)) { | |
427 | my $err = $!; | |
428 | kill (9, $pid); | |
429 | waitpid ($pid, 0); | |
430 | die $err; | |
431 | } | |
432 | $select->remove ($h) if !$count; | |
433 | if ($h eq $reader) { | |
776fbfa8 | 434 | if ($outfunc || $logfunc) { |
e143e9d8 DM |
435 | eval { |
436 | $outlog .= $buf; | |
437 | while ($outlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) { | |
438 | my $line = $1; | |
776fbfa8 DM |
439 | &$outfunc($line) if $outfunc; |
440 | &$logfunc($line) if $logfunc; | |
e143e9d8 DM |
441 | } |
442 | }; | |
443 | my $err = $@; | |
444 | if ($err) { | |
445 | kill (9, $pid); | |
446 | waitpid ($pid, 0); | |
447 | die $err; | |
448 | } | |
449 | } else { | |
450 | print $buf; | |
451 | *STDOUT->flush(); | |
452 | } | |
453 | } elsif ($h eq $error) { | |
776fbfa8 | 454 | if ($errfunc || $logfunc) { |
e143e9d8 DM |
455 | eval { |
456 | $errlog .= $buf; | |
457 | while ($errlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) { | |
458 | my $line = $1; | |
776fbfa8 DM |
459 | &$errfunc($line) if $errfunc; |
460 | &$logfunc($line) if $logfunc; | |
e143e9d8 DM |
461 | } |
462 | }; | |
463 | my $err = $@; | |
464 | if ($err) { | |
465 | kill (9, $pid); | |
466 | waitpid ($pid, 0); | |
467 | die $err; | |
468 | } | |
469 | } else { | |
470 | print STDERR $buf; | |
471 | *STDERR->flush(); | |
472 | } | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | &$outfunc($outlog) if $outfunc && $outlog; | |
776fbfa8 DM |
478 | &$logfunc($outlog) if $logfunc && $outlog; |
479 | ||
e143e9d8 | 480 | &$errfunc($errlog) if $errfunc && $errlog; |
776fbfa8 | 481 | &$logfunc($errlog) if $logfunc && $errlog; |
e143e9d8 DM |
482 | |
483 | waitpid ($pid, 0); | |
484 | ||
485 | if ($? == -1) { | |
486 | die "failed to execute\n"; | |
487 | } elsif (my $sig = ($? & 127)) { | |
488 | die "got signal $sig\n"; | |
489 | } elsif (my $ec = ($? >> 8)) { | |
1c50a24a DM |
490 | if (!($ec == 24 && ($cmdstr =~ m|^(\S+/)?rsync\s|))) { |
491 | if ($errmsg && $laststderr) { | |
492 | my $lerr = $laststderr; | |
493 | $laststderr = undef; | |
494 | die "$lerr\n"; | |
495 | } | |
496 | die "exit code $ec\n"; | |
e143e9d8 | 497 | } |
e143e9d8 DM |
498 | } |
499 | ||
500 | alarm(0); | |
501 | }; | |
502 | ||
503 | my $err = $@; | |
504 | ||
505 | alarm(0); | |
506 | ||
4630cb95 DM |
507 | if ($errmsg && $laststderr) { |
508 | &$errfunc(undef); # flush laststderr | |
509 | } | |
e143e9d8 DM |
510 | |
511 | umask ($old_umask) if defined($old_umask); | |
512 | ||
513 | alarm($oldtimeout) if $oldtimeout; | |
514 | ||
515 | if ($err) { | |
516 | if ($pid && ($err eq "got timeout\n")) { | |
517 | kill (9, $pid); | |
518 | waitpid ($pid, 0); | |
519 | die "command '$cmdstr' failed: $err"; | |
520 | } | |
521 | ||
522 | if ($errmsg) { | |
adbc988d | 523 | $err =~ s/^usermod:\s*// if $cmdstr =~ m|^(\S+/)?usermod\s|; |
e143e9d8 DM |
524 | die "$errmsg: $err"; |
525 | } else { | |
526 | die "command '$cmdstr' failed: $err"; | |
527 | } | |
528 | } | |
1c50a24a DM |
529 | |
530 | return undef; | |
e143e9d8 DM |
531 | } |
532 | ||
533 | sub split_list { | |
534 | my $listtxt = shift || ''; | |
535 | ||
d2b0374d DM |
536 | return split (/\0/, $listtxt) if $listtxt =~ m/\0/; |
537 | ||
538 | $listtxt =~ s/[,;]/ /g; | |
e143e9d8 DM |
539 | $listtxt =~ s/^\s+//; |
540 | ||
541 | my @data = split (/\s+/, $listtxt); | |
542 | ||
543 | return @data; | |
544 | } | |
545 | ||
546 | sub trim { | |
547 | my $txt = shift; | |
548 | ||
549 | return $txt if !defined($txt); | |
550 | ||
551 | $txt =~ s/^\s+//; | |
552 | $txt =~ s/\s+$//; | |
553 | ||
554 | return $txt; | |
555 | } | |
556 | ||
557 | # simple uri templates like "/vms/{vmid}" | |
558 | sub template_replace { | |
559 | my ($tmpl, $data) = @_; | |
560 | ||
3250f5c7 DM |
561 | return $tmpl if !$tmpl; |
562 | ||
e143e9d8 DM |
563 | my $res = ''; |
564 | while ($tmpl =~ m/([^{]+)?({([^}]+)})?/g) { | |
565 | $res .= $1 if $1; | |
566 | $res .= ($data->{$3} || '-') if $2; | |
567 | } | |
568 | return $res; | |
569 | } | |
570 | ||
571 | sub safe_print { | |
572 | my ($filename, $fh, $data) = @_; | |
573 | ||
574 | return if !$data; | |
575 | ||
576 | my $res = print $fh $data; | |
577 | ||
578 | die "write to '$filename' failed\n" if !$res; | |
579 | } | |
580 | ||
581 | sub debmirrors { | |
582 | ||
583 | return { | |
584 | 'at' => 'ftp.at.debian.org', | |
585 | 'au' => 'ftp.au.debian.org', | |
586 | 'be' => 'ftp.be.debian.org', | |
587 | 'bg' => 'ftp.bg.debian.org', | |
588 | 'br' => 'ftp.br.debian.org', | |
589 | 'ca' => 'ftp.ca.debian.org', | |
590 | 'ch' => 'ftp.ch.debian.org', | |
591 | 'cl' => 'ftp.cl.debian.org', | |
592 | 'cz' => 'ftp.cz.debian.org', | |
593 | 'de' => 'ftp.de.debian.org', | |
594 | 'dk' => 'ftp.dk.debian.org', | |
595 | 'ee' => 'ftp.ee.debian.org', | |
596 | 'es' => 'ftp.es.debian.org', | |
597 | 'fi' => 'ftp.fi.debian.org', | |
598 | 'fr' => 'ftp.fr.debian.org', | |
599 | 'gr' => 'ftp.gr.debian.org', | |
600 | 'hk' => 'ftp.hk.debian.org', | |
601 | 'hr' => 'ftp.hr.debian.org', | |
602 | 'hu' => 'ftp.hu.debian.org', | |
603 | 'ie' => 'ftp.ie.debian.org', | |
604 | 'is' => 'ftp.is.debian.org', | |
605 | 'it' => 'ftp.it.debian.org', | |
606 | 'jp' => 'ftp.jp.debian.org', | |
607 | 'kr' => 'ftp.kr.debian.org', | |
608 | 'mx' => 'ftp.mx.debian.org', | |
609 | 'nl' => 'ftp.nl.debian.org', | |
610 | 'no' => 'ftp.no.debian.org', | |
611 | 'nz' => 'ftp.nz.debian.org', | |
612 | 'pl' => 'ftp.pl.debian.org', | |
613 | 'pt' => 'ftp.pt.debian.org', | |
614 | 'ro' => 'ftp.ro.debian.org', | |
615 | 'ru' => 'ftp.ru.debian.org', | |
616 | 'se' => 'ftp.se.debian.org', | |
617 | 'si' => 'ftp.si.debian.org', | |
618 | 'sk' => 'ftp.sk.debian.org', | |
619 | 'tr' => 'ftp.tr.debian.org', | |
620 | 'tw' => 'ftp.tw.debian.org', | |
621 | 'gb' => 'ftp.uk.debian.org', | |
622 | 'us' => 'ftp.us.debian.org', | |
623 | }; | |
624 | } | |
625 | ||
910d57b0 DM |
626 | my $keymaphash = { |
627 | 'dk' => ['Danish', 'da', 'qwerty/dk-latin1.kmap.gz', 'dk', 'nodeadkeys'], | |
628 | 'de' => ['German', 'de', 'qwertz/de-latin1-nodeadkeys.kmap.gz', 'de', 'nodeadkeys' ], | |
629 | 'de-ch' => ['Swiss-German', 'de-ch', 'qwertz/sg-latin1.kmap.gz', 'ch', 'de_nodeadkeys' ], | |
5a5ca434 DM |
630 | 'en-gb' => ['United Kingdom', 'en-gb', 'qwerty/uk.kmap.gz' , 'gb', undef], |
631 | 'en-us' => ['U.S. English', 'en-us', 'qwerty/us-latin1.kmap.gz', 'us', undef ], | |
910d57b0 DM |
632 | 'es' => ['Spanish', 'es', 'qwerty/es.kmap.gz', 'es', 'nodeadkeys'], |
633 | #'et' => [], # Ethopia or Estonia ?? | |
634 | 'fi' => ['Finnish', 'fi', 'qwerty/fi-latin1.kmap.gz', 'fi', 'nodeadkeys'], | |
635 | #'fo' => ['Faroe Islands', 'fo', ???, 'fo', 'nodeadkeys'], | |
636 | 'fr' => ['French', 'fr', 'azerty/fr-latin1.kmap.gz', 'fr', 'nodeadkeys'], | |
637 | 'fr-be' => ['Belgium-French', 'fr-be', 'azerty/be2-latin1.kmap.gz', 'be', 'nodeadkeys'], | |
638 | 'fr-ca' => ['Canada-French', 'fr-ca', 'qwerty/cf.kmap.gz', 'ca', 'fr-legacy'], | |
639 | 'fr-ch' => ['Swiss-French', 'fr-ch', 'qwertz/fr_CH-latin1.kmap.gz', 'ch', 'fr_nodeadkeys'], | |
640 | #'hr' => ['Croatia', 'hr', 'qwertz/croat.kmap.gz', 'hr', ??], # latin2? | |
641 | 'hu' => ['Hungarian', 'hu', 'qwertz/hu.kmap.gz', 'hu', undef], | |
642 | 'is' => ['Icelandic', 'is', 'qwerty/is-latin1.kmap.gz', 'is', 'nodeadkeys'], | |
643 | 'it' => ['Italian', 'it', 'qwerty/it2.kmap.gz', 'it', 'nodeadkeys'], | |
644 | 'jp' => ['Japanese', 'ja', 'qwerty/jp106.kmap.gz', 'jp', undef], | |
645 | 'lt' => ['Lithuanian', 'lt', 'qwerty/lt.kmap.gz', 'lt', 'std'], | |
646 | #'lv' => ['Latvian', 'lv', 'qwerty/lv-latin4.kmap.gz', 'lv', ??], # latin4 or latin7? | |
647 | 'mk' => ['Macedonian', 'mk', 'qwerty/mk.kmap.gz', 'mk', 'nodeadkeys'], | |
648 | 'nl' => ['Dutch', 'nl', 'qwerty/nl.kmap.gz', 'nl', undef], | |
649 | #'nl-be' => ['Belgium-Dutch', 'nl-be', ?, ?, ?], | |
650 | 'no' => ['Norwegian', 'no', 'qwerty/no-latin1.kmap.gz', 'no', 'nodeadkeys'], | |
651 | 'pl' => ['Polish', 'pl', 'qwerty/pl.kmap.gz', 'pl', undef], | |
652 | 'pt' => ['Portuguese', 'pt', 'qwerty/pt-latin1.kmap.gz', 'pt', 'nodeadkeys'], | |
653 | 'pt-br' => ['Brazil-Portuguese', 'pt-br', 'qwerty/br-latin1.kmap.gz', 'br', 'nodeadkeys'], | |
654 | #'ru' => ['Russian', 'ru', 'qwerty/ru.kmap.gz', 'ru', undef], # dont know? | |
655 | 'si' => ['Slovenian', 'sl', 'qwertz/slovene.kmap.gz', 'si', undef], | |
9934cd0b | 656 | 'se' => ['Swedish', 'sv', 'qwerty/se-latin1.kmap.gz', 'se', 'nodeadkeys'], |
910d57b0 | 657 | #'th' => [], |
c10cc112 | 658 | 'tr' => ['Turkish', 'tr', 'qwerty/trq.kmap.gz', 'tr', undef], |
910d57b0 DM |
659 | }; |
660 | ||
661 | my $kvmkeymaparray = []; | |
662 | foreach my $lc (keys %$keymaphash) { | |
663 | push @$kvmkeymaparray, $keymaphash->{$lc}->[1]; | |
664 | } | |
665 | ||
e143e9d8 | 666 | sub kvmkeymaps { |
910d57b0 DM |
667 | return $keymaphash; |
668 | } | |
669 | ||
670 | sub kvmkeymaplist { | |
671 | return $kvmkeymaparray; | |
e143e9d8 DM |
672 | } |
673 | ||
674 | sub extract_param { | |
675 | my ($param, $key) = @_; | |
676 | ||
677 | my $res = $param->{$key}; | |
678 | delete $param->{$key}; | |
679 | ||
680 | return $res; | |
681 | } | |
682 | ||
eb7047f6 | 683 | # Note: we use this to wait until vncterm/spiceterm is ready |
ec6d95b4 DM |
684 | sub wait_for_vnc_port { |
685 | my ($port, $timeout) = @_; | |
686 | ||
687 | $timeout = 5 if !$timeout; | |
eb7047f6 DM |
688 | my $sleeptime = 0; |
689 | my $starttime = [gettimeofday]; | |
690 | my $elapsed; | |
ec6d95b4 | 691 | |
eb7047f6 | 692 | while (($elapsed = tv_interval($starttime)) < $timeout) { |
ec6d95b4 DM |
693 | if (my $fh = IO::File->new ("/proc/net/tcp", "r")) { |
694 | while (defined (my $line = <$fh>)) { | |
695 | if ($line =~ m/^\s*\d+:\s+([0-9A-Fa-f]{8}):([0-9A-Fa-f]{4})\s/) { | |
696 | if ($port == hex($2)) { | |
697 | close($fh); | |
698 | return 1; | |
699 | } | |
700 | } | |
701 | } | |
702 | close($fh); | |
703 | } | |
eb7047f6 DM |
704 | $sleeptime += 100000 if $sleeptime < 1000000; |
705 | usleep($sleeptime); | |
ec6d95b4 DM |
706 | } |
707 | ||
708 | return undef; | |
709 | } | |
710 | ||
59b3f563 | 711 | sub next_unused_port { |
46775218 | 712 | my ($range_start, $range_end, $family) = @_; |
59b3f563 DM |
713 | |
714 | # We use a file to register allocated ports. | |
715 | # Those registrations expires after $expiretime. | |
716 | # We use this to avoid race conditions between | |
717 | # allocation and use of ports. | |
718 | ||
719 | my $filename = "/var/tmp/pve-reserved-ports"; | |
e143e9d8 | 720 | |
59b3f563 | 721 | my $code = sub { |
e143e9d8 | 722 | |
59b3f563 DM |
723 | my $expiretime = 5; |
724 | my $ctime = time(); | |
e143e9d8 | 725 | |
59b3f563 DM |
726 | my $ports = {}; |
727 | ||
728 | if (my $fh = IO::File->new ($filename, "r")) { | |
729 | while (my $line = <$fh>) { | |
730 | if ($line =~ m/^(\d+)\s(\d+)$/) { | |
731 | my ($port, $timestamp) = ($1, $2); | |
732 | if (($timestamp + $expiretime) > $ctime) { | |
733 | $ports->{$port} = $timestamp; # not expired | |
734 | } | |
735 | } | |
736 | } | |
e143e9d8 | 737 | } |
59b3f563 DM |
738 | |
739 | my $newport; | |
740 | ||
741 | for (my $p = $range_start; $p < $range_end; $p++) { | |
742 | next if $ports->{$p}; # reserved | |
743 | ||
00dc9d0f | 744 | my $sock = IO::Socket::IP->new(Listen => 5, |
00dc9d0f WB |
745 | LocalPort => $p, |
746 | ReuseAddr => 1, | |
46775218 | 747 | Family => $family, |
e43b3a0f WB |
748 | Proto => 0, |
749 | GetAddrInfoFlags => 0); | |
59b3f563 DM |
750 | |
751 | if ($sock) { | |
752 | close($sock); | |
753 | $newport = $p; | |
754 | $ports->{$p} = $ctime; | |
755 | last; | |
756 | } | |
757 | } | |
758 | ||
759 | my $data = ""; | |
760 | foreach my $p (keys %$ports) { | |
761 | $data .= "$p $ports->{$p}\n"; | |
762 | } | |
763 | ||
764 | file_set_contents($filename, $data); | |
e143e9d8 | 765 | |
59b3f563 DM |
766 | return $newport; |
767 | }; | |
768 | ||
769 | my $p = lock_file($filename, 10, $code); | |
770 | die $@ if $@; | |
771 | ||
772 | die "unable to find free port (${range_start}-${range_end})\n" if !$p; | |
773 | ||
774 | return $p; | |
775 | } | |
776 | ||
777 | sub next_migrate_port { | |
46775218 WB |
778 | my ($family) = @_; |
779 | return next_unused_port(60000, 60050, $family); | |
59b3f563 DM |
780 | } |
781 | ||
782 | sub next_vnc_port { | |
46775218 WB |
783 | my ($family) = @_; |
784 | return next_unused_port(5900, 6000, $family); | |
59b3f563 | 785 | } |
e143e9d8 | 786 | |
2f13cbb5 | 787 | sub next_spice_port { |
46775218 WB |
788 | my ($family) = @_; |
789 | return next_unused_port(61000, 61099, $family); | |
2f13cbb5 DM |
790 | } |
791 | ||
e143e9d8 DM |
792 | # NOTE: NFS syscall can't be interrupted, so alarm does |
793 | # not work to provide timeouts. | |
794 | # from 'man nfs': "Only SIGKILL can interrupt a pending NFS operation" | |
97c8c857 | 795 | # So fork() before using Filesys::Df |
e143e9d8 DM |
796 | sub df { |
797 | my ($path, $timeout) = @_; | |
798 | ||
e143e9d8 DM |
799 | my $res = { |
800 | total => 0, | |
801 | used => 0, | |
802 | avail => 0, | |
803 | }; | |
804 | ||
97c8c857 WB |
805 | my $pipe = IO::Pipe->new(); |
806 | my $child = fork(); | |
807 | if (!defined($child)) { | |
808 | warn "fork failed: $!\n"; | |
809 | return $res; | |
810 | } | |
811 | ||
812 | if (!$child) { | |
813 | $pipe->writer(); | |
814 | eval { | |
815 | my $df = Filesys::Df::df($path, 1); | |
816 | print {$pipe} "$df->{blocks}\n$df->{used}\n$df->{bavail}\n"; | |
817 | $pipe->close(); | |
818 | }; | |
819 | if (my $err = $@) { | |
820 | warn $err; | |
821 | POSIX::_exit(1); | |
e143e9d8 | 822 | } |
97c8c857 WB |
823 | POSIX::_exit(0); |
824 | } | |
825 | ||
826 | $pipe->reader(); | |
827 | ||
828 | my $readvalues = sub { | |
829 | $res->{total} = int(<$pipe>); | |
830 | $res->{used} = int(<$pipe>); | |
831 | $res->{avail} = int(<$pipe>); | |
832 | }; | |
833 | eval { | |
834 | run_with_timeout($timeout, $readvalues); | |
e143e9d8 | 835 | }; |
e143e9d8 | 836 | warn $@ if $@; |
97c8c857 WB |
837 | $pipe->close(); |
838 | kill('KILL', $child); | |
839 | waitpid($child, 0); | |
e143e9d8 DM |
840 | return $res; |
841 | } | |
842 | ||
843 | # UPID helper | |
844 | # We use this to uniquely identify a process. | |
845 | # An 'Unique Process ID' has the following format: | |
846 | # "UPID:$node:$pid:$pstart:$startime:$dtype:$id:$user" | |
847 | ||
848 | sub upid_encode { | |
849 | my $d = shift; | |
850 | ||
19cec230 DM |
851 | # Note: pstart can be > 32bit if uptime > 497 days, so this can result in |
852 | # more that 8 characters for pstart | |
e143e9d8 DM |
853 | return sprintf("UPID:%s:%08X:%08X:%08X:%s:%s:%s:", $d->{node}, $d->{pid}, |
854 | $d->{pstart}, $d->{starttime}, $d->{type}, $d->{id}, | |
855 | $d->{user}); | |
856 | } | |
857 | ||
858 | sub upid_decode { | |
859 | my ($upid, $noerr) = @_; | |
860 | ||
861 | my $res; | |
862 | my $filename; | |
863 | ||
864 | # "UPID:$node:$pid:$pstart:$startime:$dtype:$id:$user" | |
19cec230 DM |
865 | # Note: allow up to 9 characters for pstart (work until 20 years uptime) |
866 | 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]+):$/) { | |
e143e9d8 | 867 | $res->{node} = $1; |
3702f038 DM |
868 | $res->{pid} = hex($3); |
869 | $res->{pstart} = hex($4); | |
870 | $res->{starttime} = hex($5); | |
871 | $res->{type} = $6; | |
872 | $res->{id} = $7; | |
873 | $res->{user} = $8; | |
874 | ||
875 | my $subdir = substr($5, 7, 8); | |
e143e9d8 DM |
876 | $filename = "$pvetaskdir/$subdir/$upid"; |
877 | ||
878 | } else { | |
879 | return undef if $noerr; | |
880 | die "unable to parse worker upid '$upid'\n"; | |
881 | } | |
882 | ||
883 | return wantarray ? ($res, $filename) : $res; | |
884 | } | |
885 | ||
886 | sub upid_open { | |
887 | my ($upid) = @_; | |
888 | ||
889 | my ($task, $filename) = upid_decode($upid); | |
890 | ||
891 | my $dirname = dirname($filename); | |
892 | make_path($dirname); | |
893 | ||
894 | my $wwwid = getpwnam('www-data') || | |
895 | die "getpwnam failed"; | |
896 | ||
897 | my $perm = 0640; | |
898 | ||
899 | my $outfh = IO::File->new ($filename, O_WRONLY|O_CREAT|O_EXCL, $perm) || | |
900 | die "unable to create output file '$filename' - $!\n"; | |
2b8e0f12 | 901 | chown $wwwid, -1, $outfh; |
e143e9d8 DM |
902 | |
903 | return $outfh; | |
904 | }; | |
905 | ||
906 | sub upid_read_status { | |
907 | my ($upid) = @_; | |
908 | ||
909 | my ($task, $filename) = upid_decode($upid); | |
910 | my $fh = IO::File->new($filename, "r"); | |
911 | return "unable to open file - $!" if !$fh; | |
cc9121d3 | 912 | my $maxlen = 4096; |
e143e9d8 DM |
913 | sysseek($fh, -$maxlen, 2); |
914 | my $readbuf = ''; | |
915 | my $br = sysread($fh, $readbuf, $maxlen); | |
916 | close($fh); | |
917 | if ($br) { | |
918 | return "unable to extract last line" | |
919 | if $readbuf !~ m/\n?(.+)$/; | |
920 | my $line = $1; | |
921 | if ($line =~ m/^TASK OK$/) { | |
922 | return 'OK'; | |
923 | } elsif ($line =~ m/^TASK ERROR: (.+)$/) { | |
924 | return $1; | |
925 | } else { | |
926 | return "unexpected status"; | |
927 | } | |
928 | } | |
929 | return "unable to read tail (got $br bytes)"; | |
930 | } | |
931 | ||
932 | # useful functions to store comments in config files | |
933 | sub encode_text { | |
934 | my ($text) = @_; | |
935 | ||
936 | # all control and hi-bit characters, and ':' | |
937 | my $unsafe = "^\x20-\x39\x3b-\x7e"; | |
938 | return uri_escape(Encode::encode("utf8", $text), $unsafe); | |
939 | } | |
940 | ||
941 | sub decode_text { | |
942 | my ($data) = @_; | |
943 | ||
944 | return Encode::decode("utf8", uri_unescape($data)); | |
945 | } | |
946 | ||
815b2aba DM |
947 | sub decode_utf8_parameters { |
948 | my ($param) = @_; | |
949 | ||
34ebb226 | 950 | foreach my $p (qw(comment description firstname lastname)) { |
815b2aba DM |
951 | $param->{$p} = decode('utf8', $param->{$p}) if $param->{$p}; |
952 | } | |
953 | ||
954 | return $param; | |
955 | } | |
956 | ||
a413a515 DM |
957 | sub random_ether_addr { |
958 | ||
46a11c00 DM |
959 | my ($seconds, $microseconds) = gettimeofday; |
960 | ||
961 | my $rand = Digest::SHA::sha1_hex($$, rand(), $seconds, $microseconds); | |
a413a515 | 962 | |
85d5625a | 963 | # clear multicast, set local id |
de9a267f | 964 | vec($rand, 0, 8) = (vec($rand, 0, 8) & 0xfe) | 2; |
a413a515 | 965 | |
85d5625a | 966 | return sprintf("%02X:%02X:%02X:%02X:%02X:%02X", unpack("C6", $rand)); |
a413a515 | 967 | } |
e143e9d8 | 968 | |
762e3223 DM |
969 | sub shellquote { |
970 | my $str = shift; | |
971 | ||
7514b23a | 972 | return String::ShellQuote::shell_quote($str); |
762e3223 DM |
973 | } |
974 | ||
65e1d3fc DM |
975 | sub cmd2string { |
976 | my ($cmd) = @_; | |
977 | ||
978 | die "no arguments" if !$cmd; | |
979 | ||
980 | return $cmd if !ref($cmd); | |
981 | ||
982 | my @qa = (); | |
983 | foreach my $arg (@$cmd) { push @qa, shellquote($arg); } | |
984 | ||
985 | return join (' ', @qa); | |
986 | } | |
987 | ||
e38bcd35 | 988 | # split an shell argument string into an array, |
f9125663 DM |
989 | sub split_args { |
990 | my ($str) = @_; | |
991 | ||
e38bcd35 | 992 | return $str ? [ Text::ParseWords::shellwords($str) ] : []; |
f9125663 DM |
993 | } |
994 | ||
804b1041 | 995 | sub dump_logfile { |
eb7e5538 | 996 | my ($filename, $start, $limit, $filter) = @_; |
804b1041 DM |
997 | |
998 | my $lines = []; | |
999 | my $count = 0; | |
1000 | ||
1001 | my $fh = IO::File->new($filename, "r"); | |
1002 | if (!$fh) { | |
1003 | $count++; | |
1004 | push @$lines, { n => $count, t => "unable to open file - $!"}; | |
1005 | return ($count, $lines); | |
1006 | } | |
1007 | ||
1008 | $start = 0 if !$start; | |
1009 | $limit = 50 if !$limit; | |
1010 | ||
1011 | my $line; | |
eb7e5538 DM |
1012 | |
1013 | if ($filter) { | |
1014 | # duplicate code, so that we do not slow down normal path | |
1015 | while (defined($line = <$fh>)) { | |
1016 | next if $line !~ m/$filter/; | |
1017 | next if $count++ < $start; | |
1018 | next if $limit <= 0; | |
1019 | chomp $line; | |
1020 | push @$lines, { n => $count, t => $line}; | |
1021 | $limit--; | |
1022 | } | |
1023 | } else { | |
1024 | while (defined($line = <$fh>)) { | |
1025 | next if $count++ < $start; | |
1026 | next if $limit <= 0; | |
1027 | chomp $line; | |
1028 | push @$lines, { n => $count, t => $line}; | |
1029 | $limit--; | |
1030 | } | |
804b1041 DM |
1031 | } |
1032 | ||
1033 | close($fh); | |
1034 | ||
6de95a66 DM |
1035 | # HACK: ExtJS store.guaranteeRange() does not like empty array |
1036 | # so we add a line | |
1037 | if (!$count) { | |
1038 | $count++; | |
1039 | push @$lines, { n => $count, t => "no content"}; | |
1040 | } | |
1041 | ||
1042 | return ($count, $lines); | |
1043 | } | |
1044 | ||
1045 | sub dump_journal { | |
1046 | my ($start, $limit, $filter) = @_; | |
1047 | ||
1048 | my $lines = []; | |
1049 | my $count = 0; | |
1050 | ||
1051 | $start = 0 if !$start; | |
1052 | $limit = 50 if !$limit; | |
1053 | ||
1054 | my $parser = sub { | |
1055 | my $line = shift; | |
1056 | ||
1057 | return if $count++ < $start; | |
1058 | return if $limit <= 0; | |
1059 | push @$lines, { n => int($count), t => $line}; | |
1060 | $limit--; | |
1061 | }; | |
1062 | ||
1063 | my $cmd = ['journalctl', '-o', 'short', '--no-pager']; | |
1064 | run_command($cmd, outfunc => $parser); | |
1065 | ||
804b1041 DM |
1066 | # HACK: ExtJS store.guaranteeRange() does not like empty array |
1067 | # so we add a line | |
1068 | if (!$count) { | |
1069 | $count++; | |
1070 | push @$lines, { n => $count, t => "no content"}; | |
1071 | } | |
1072 | ||
1073 | return ($count, $lines); | |
1074 | } | |
1075 | ||
7eb283fb DM |
1076 | sub dir_glob_regex { |
1077 | my ($dir, $regex) = @_; | |
1078 | ||
1079 | my $dh = IO::Dir->new ($dir); | |
1080 | return wantarray ? () : undef if !$dh; | |
1081 | ||
1082 | while (defined(my $tmp = $dh->read)) { | |
1083 | if (my @res = $tmp =~ m/^($regex)$/) { | |
1084 | $dh->close; | |
1085 | return wantarray ? @res : $tmp; | |
1086 | } | |
1087 | } | |
1088 | $dh->close; | |
1089 | ||
1090 | return wantarray ? () : undef; | |
1091 | } | |
1092 | ||
1093 | sub dir_glob_foreach { | |
1094 | my ($dir, $regex, $func) = @_; | |
1095 | ||
1096 | my $dh = IO::Dir->new ($dir); | |
1097 | if (defined $dh) { | |
1098 | while (defined(my $tmp = $dh->read)) { | |
1099 | if (my @res = $tmp =~ m/^($regex)$/) { | |
1100 | &$func (@res); | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | } | |
1105 | ||
a345b9d5 DM |
1106 | sub assert_if_modified { |
1107 | my ($digest1, $digest2) = @_; | |
1108 | ||
1109 | if ($digest1 && $digest2 && ($digest1 ne $digest2)) { | |
212fc0b7 | 1110 | die "detected modified configuration - file changed by other user? Try again.\n"; |
a345b9d5 DM |
1111 | } |
1112 | } | |
1113 | ||
2d3bca34 DM |
1114 | # Digest for short strings |
1115 | # like FNV32a, but we only return 31 bits (positive numbers) | |
1116 | sub fnv31a { | |
1117 | my ($string) = @_; | |
1118 | ||
1119 | my $hval = 0x811c9dc5; | |
1120 | ||
1121 | foreach my $c (unpack('C*', $string)) { | |
1122 | $hval ^= $c; | |
1123 | $hval += ( | |
1124 | (($hval << 1) ) + | |
1125 | (($hval << 4) ) + | |
1126 | (($hval << 7) ) + | |
1127 | (($hval << 8) ) + | |
1128 | (($hval << 24) ) ); | |
1129 | $hval = $hval & 0xffffffff; | |
1130 | } | |
1131 | return $hval & 0x7fffffff; | |
1132 | } | |
1133 | ||
1134 | sub fnv31a_hex { return sprintf("%X", fnv31a(@_)); } | |
1135 | ||
8df6b794 WB |
1136 | sub unpack_sockaddr_in46 { |
1137 | my ($sin) = @_; | |
1138 | my $family = Socket::sockaddr_family($sin); | |
1139 | my ($port, $host) = ($family == AF_INET6 ? Socket::unpack_sockaddr_in6($sin) | |
1140 | : Socket::unpack_sockaddr_in($sin)); | |
1141 | return ($family, $port, $host); | |
1142 | } | |
1143 | ||
a0b6ef52 WB |
1144 | sub getaddrinfo_all { |
1145 | my ($hostname, @opts) = @_; | |
a956854f | 1146 | my %hints = ( flags => AI_V4MAPPED | AI_ALL, |
a0b6ef52 | 1147 | @opts ); |
a956854f | 1148 | my ($err, @res) = Socket::getaddrinfo($hostname, '0', \%hints); |
a0b6ef52 WB |
1149 | die "failed to get address info for: $hostname: $err\n" if $err; |
1150 | return @res; | |
1151 | } | |
a956854f | 1152 | |
a0b6ef52 WB |
1153 | sub get_host_address_family { |
1154 | my ($hostname, $socktype) = @_; | |
1155 | my @res = getaddrinfo_all($hostname, socktype => $socktype); | |
1156 | return $res[0]->{family}; | |
a956854f WB |
1157 | } |
1158 | ||
b2613777 WB |
1159 | # Parses any sane kind of host, or host+port pair: |
1160 | # The port is always optional and thus may be undef. | |
1161 | sub parse_host_and_port { | |
1162 | my ($address) = @_; | |
1163 | if ($address =~ /^($IPV4RE|[[:alnum:]\-.]+)(?::(\d+))?$/ || # ipv4 or host with optional ':port' | |
1164 | $address =~ /^\[($IPV6RE|$IPV4RE|[[:alnum:]\-.]+)\](?::(\d+))?$/ || # anything in brackets with optional ':port' | |
1165 | $address =~ /^($IPV6RE)(?:\.(\d+))?$/) # ipv6 with optional port separated by dot | |
1166 | { | |
1167 | return ($1, $2, 1); # end with 1 to support simple if(parse...) tests | |
1168 | } | |
1169 | return; # nothing | |
1170 | } | |
1171 | ||
817c6be0 | 1172 | sub unshare($) { |
952fd95e | 1173 | my ($flags) = @_; |
817c6be0 | 1174 | return 0 == syscall(272, $flags); |
952fd95e WB |
1175 | } |
1176 | ||
e143e9d8 | 1177 | 1; |