Propagação do sinal aos pais ao usar o sistema
Eu escrevi um script de wrapper que inicia outro script usandosystem()
. O script filho interceptaSIGINT
e processa a exceção internamente. Portanto, ele não deve propagar oSIGINT
ao pai quando sai. No entanto, em alguns casos, o pai ainda recebe oSIGINT
. Por exemplo (parent.pl
):
use feature qw(say);
use strict;
use warnings;
use POSIX ();
my @data = (q(child.pl 'dummy'), q(child.pl), q(bash -c child.pl), q(sh -c child.pl));
for ( @data ) {
say "Running command '$_'..";
my $res = system $_;
my $signal = $res & 127;
my $rval = $res >> 8;
say "Parent received return value: $rval";
if ( $signal == POSIX::SIGINT ) {
say "Parent received SIGINT";
}
}
echild.pl
:
use feature qw(say);
use strict;
use warnings;
eval {
local $SIG{INT} = sub { die "Aborted by user.\n" };
sleep 10;
};
if ( $@ ) {
print "\n" . $@;
exit 0;
}
say "Timed out..";
exit 1;
Se eu pressionarCTRL-C antes do tempo limite, a saída se parece com:
Running command 'child.pl 'dummy''..
^C
Aborted by user.
Parent received return value: 0
Parent received SIGINT
Running command 'child.pl'..
^C
Aborted by user.
Parent received return value: 0
Running command 'bash -c child.pl'..
^C
Aborted by user.
Parent received return value: 0
Running command 'sh -c child.pl'..
^C
Aborted by user.
Parent received return value: 0
Parent received SIGINT
Portanto, no primeiro e no último caso, o pai recebe oSIGINT
, enquanto que para o segundo e terceiro casos não.
Qual é a razão para isto? E como pode ser corrigido para que o SIGINT não seja propagado para o primeiro e o último caso?
(Suspeito que esteja relacionado ao tipo de Shell, ou seja,sh
vsbash
)