}
exit $rv;
}
-usage(1) unless GetOptions(\%CONFIG, qw/config=s quiet|q target=s@ debug help|h repair delete rename/);
+
+my @COMMANDS = qw/repair delete rename/;
+usage(1) unless GetOptions(\%CONFIG, qw/config=s quiet|q target=s@ debug help|h/, @COMMANDS);
usage(0) if $CONFIG{help};
my $COMMAND = do {
- my @command = grep {exists $CONFIG{$_}} qw/repair delete rename/;
+ my @command = grep {exists $CONFIG{$_}} @COMMANDS;
usage(1) if $#command>0;
$command[0]
};
sub msg($@) {
my $name = shift;
return unless @_;
- logger($name, @_) if defined $LOGGER_FD and $LOGGER_FD->fileno != fileno STDERR;
+ logger($name, @_) if defined $LOGGER_FD and defined $LOGGER_FD->fileno
+ and $LOGGER_FD->fileno != fileno STDERR;
my $prefix = defined $name ? "$name: " : '';
print STDERR $prefix, @_, "\n";
}
my $name = shift;
return unless @_ and defined $LOGGER_FD;
my $prefix = '';
- if ($LOGGER_FD->fileno != fileno STDERR) {
+ if (defined $LOGGER_FD and defined $LOGGER_FD->fileno
+ and $LOGGER_FD->fileno != fileno STDERR) {
my ($s, $us) = Time::HiRes::gettimeofday();
$prefix = POSIX::strftime("%b %e %H:%M:%S", localtime($s)).".$us ";
}
use IO::Select ();
use List::Util 'first';
use Socket 'SO_KEEPALIVE';
+use POSIX ':signal_h';
use Exporter 'import';
BEGIN {
$self->{_STATE} = '';
if ($self->{type} eq 'tunnel') {
- require 'IPC/Open2.pm';
my $command = $self->{command} // $self->fail("Missing tunnel command");
- my $pid = IPC::Open2::open2(@$self{qw/STDOUT STDIN/}, $command)
- or $self->panic("Can't fork: $!");
+
+ pipe $self->{STDOUT}, my $wd or $self->panic("Can't pipe: $!");
+ pipe my $rd, $self->{STDIN} or $self->panic("Can't pipe: $!");
+
+ my $pid = fork // $self->panic("Can't fork: $!");
+
+ unless ($pid) {
+ # children
+ foreach (\*STDIN, \*STDOUT, $self->{STDIN}, $self->{STDOUT}) {
+ close $_ or $self->panic("Can't close: $!");
+ }
+ open STDIN, '<&', $rd or $self->panic("Can't dup: $!");
+ open STDOUT, '>&', $wd or $self->panic("Can't dup: $!");
+
+ my $sigset = POSIX::SigSet::->new(SIGINT);
+ my $oldsigset = POSIX::SigSet::->new();
+
+ sigprocmask(SIG_BLOCK, $sigset, $oldsigset) // $self->panic("Can't block SIGINT: $!");
+
+ exec $command or $self->panic("Can't exec: $!");
+ }
+
+ # parent
+ foreach ($rd, $wd) {
+ close $_ or $self->panic("Can't close: $!");
+ }
}
else {
my %args = (Proto => 'tcp', Blocking => 1);
# are considered.
$self->{_MODIFIED} = {};
- if (defined $self->{'logger-fd'} and $self->{'logger-fd'}->fileno != fileno STDERR) {
- require 'POSIX.pm';
+ if (defined $self->{'logger-fd'} and defined $self->{'logger-fd'}->fileno
+ and $self->{'logger-fd'}->fileno != fileno STDERR) {
require 'Time/HiRes.pm';
}
sub log($@) {
my $self = shift;
return unless @_;
- $self->logger(@_) if defined $self->{'logger-fd'} and $self->{'logger-fd'}->fileno != fileno STDERR;
+ $self->logger(@_) if defined $self->{'logger-fd'} and defined $self->{'logger-fd'}->fileno
+ and $self->{'logger-fd'}->fileno != fileno STDERR;
my $prefix = defined $self->{name} ? $self->{name} : '';
$prefix .= "($self->{_SELECTED})" if $self->{_STATE} eq 'SELECTED';
print STDERR $prefix, ': ', @_, "\n";
my $self = shift;
return unless @_ and defined $self->{'logger-fd'};
my $prefix = '';
- if ($self->{'logger-fd'}->fileno != fileno STDERR) {
+ if (defined $self->{'logger-fd'}->fileno and defined $self->{'logger-fd'}->fileno
+ and $self->{'logger-fd'}->fileno != fileno STDERR) {
my ($s, $us) = Time::HiRes::gettimeofday();
$prefix = POSIX::strftime("%b %e %H:%M:%S", localtime($s)).".$us ";
}