netget: watchdog() now returns a result

Currently this can only be a scaler value.
On timeout, calls the fail function and returns its result instead.
This commit is contained in:
Andrew Johnson
2021-03-22 13:19:56 -05:00
committed by Dirk Zimoch
parent 220e2f9b12
commit 19d10b3161
+15 -5
View File
@@ -50,11 +50,21 @@ sub kill_bail {
}
sub watchdog (&$$) {
my ($do, $timeout, $abort) = @_;
$SIG{ALRM} = $abort;
alarm $timeout;
&$do;
alarm 0;
my ($code, $timeout, $fail) = @_;
my $bark = "Woof $$\n";
my $result;
eval {
local $SIG{__DIE__};
local $SIG{ALRM} = sub { die $bark };
alarm $timeout;
$result = &$code;
alarm 0;
};
if ($@) {
die if $@ ne $bark;
$result = &$fail;
}
return $result;
}