File: timeout.pm

package info (click to toggle)
moarvm 2024.09%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,548 kB
  • sloc: cpp: 378,682; ansic: 293,618; perl: 8,274; java: 2,682; python: 1,296; makefile: 816; sh: 292
file content (31 lines) | stat: -rw-r--r-- 594 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package timeout;
use strict;
use warnings;
use Exporter qw(import);

our @EXPORT_OK = qw(run_timeout);

sub run_timeout {
    my ($command, $timeout) = @_;
    my $status;
    my $pid = fork();
    local $SIG{CHLD} = sub {};
    if ($pid) {
        local $SIG{ALRM} = sub {
            kill 'KILL', $pid;
        };
        alarm $timeout;
        waitpid $pid, 0;
        $status = $?;
        alarm 0 if $timeout;
    } elsif (defined $pid) {
        # child, does not return
        exec @$command;
    } else {
        die $!;
    }
    return wantarray ? ($status, $pid) : $status;

}

1;