#!/usr/bin/perl

=head1 NAME

signal-proxy -

=head1 DESCRIPTION

signal-proxy

=cut

use strict;
use warnings;
use lib qw(/usr/local/pf/lib);
use lib qw(/usr/local/pf/lib_perl/lib/perl5);
use EV;
use Getopt::Long;

my $timeout = 10;

GetOptions ("timeout=i" => \$timeout);
my ($prog, @args) = @ARGV;
my @watchers;
my $child;

print "$$\n";

$child = fork();

if (!defined $child) {
    exit 1;
}

if ($child == 0) {
   exec($prog, @args); 
   exit 0;
}

my $check = EV::check sub {};

my %skipped = map { $_ => 1 } qw(CHLD CLD IOT SEGV);
for my $sig (keys %SIG) {
    next if exists $skipped{$sig};
    $SIG{$sig} = sub {
        push @watchers, EV::timer $timeout, 0, sub {
            if (defined $child) {
                kill $sig, $child;
            }
        }; 
    };
}

my $w = EV::child $child, 0, sub {
    EV::break;
};

push @watchers, $w;

EV::run;

print "done\n";
exit 0;


=head1 AUTHOR

Inverse inc. <info@inverse.ca>

=head1 COPYRIGHT

Copyright (C) 2005-2024 Inverse inc.

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.

=cut

