Currently script can stall if we read certain files (like /proc/kmsg). While we have a mechanism to skip these files once they are discovered it would be nice to not stall on as yet undiscovered files of this kind. Set a timer before each file is parsed, warn user if timer expires.
Unified · Split
| @@ -29,6 +29,9 @@ my $V = '0.01'; · 28 unmodified lines | |||
| 29 | # Directories to scan. | 29 | # Directories to scan. |
| 30 | my @DIRS = ('/proc', '/sys'); | 30 | my @DIRS = ('/proc', '/sys'); |
| 31 | 31 | ||
| 32 | # Timer for parsing each file, in seconds. | ||
| 33 | my $TIMEOUT = 10; | ||
| 34 | |||
| 32 | # Script can only grep for kernel addresses on the following architectures. If | 35 | # Script can only grep for kernel addresses on the following architectures. If |
| 33 | # your architecture is not listed here and has a grep'able kernel address please | 36 | # your architecture is not listed here and has a grep'able kernel address please |
| 34 | # consider submitting a patch. | 37 | # consider submitting a patch. |
| @@ -284,6 +287,23 @@ sub skip_parse · 249 unmodified lines | |||
| 284 | return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any); | 287 | return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any); |
| 285 | } | 288 | } |
| 286 | 289 | ||
| 290 | sub timed_parse_file | ||
| 291 | { | ||
| 292 | my ($file) = @_; | ||
| 293 | |||
| 294 | eval { | ||
| 295 | local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required. | ||
| 296 | alarm $TIMEOUT; | ||
| 297 | parse_file($file); | ||
| 298 | alarm 0; | ||
| 299 | }; | ||
| 300 | |||
| 301 | if ($@) { | ||
| 302 | die unless $@ eq "alarm\n"; # Propagate unexpected errors. | ||
| 303 | printf STDERR "timed out parsing: %s\n", $file; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 287 | sub parse_file | 307 | sub parse_file |
| 288 | { | 308 | { |
| 289 | my ($file) = @_; | 309 | my ($file) = @_; |
| @@ -335,7 +355,7 @@ sub walk · 45 unmodified lines | |||
| 335 | if (-d $path) { | 355 | if (-d $path) { |
| 336 | push @dirs, $path; | 356 | push @dirs, $path; |
| 337 | } else { | 357 | } else { |
| 338 | parse_file($path); | 358 | timed_parse_file($path); |
| 339 | } | 359 | } |
| 340 | } | 360 | } |
| 341 | } | 361 | } |