Return value from system () when using the default SIGINT handler

I experience some weird return values ​​from system()when the child process receives SIGINT from the terminal. To explain, from a Perl script, parent.plI used system()to run another Perl script as a child process, but I also needed to run the child through the shell, so I used the form system 'sh', '-c', .... Thus, the parent of the child process became sh, and the parent process shbecame parent.pl. In addition, in order to avoid the process shreceiving the signal SIGINT, I locked it.

For example parent.pl:

use feature qw(say);
use strict;
use warnings;

for (1..3) {
    my $res = system 'sh', '-c', "trap '' INT; child$_.pl";
    say "Parent received return value: " . ($res >> 8);
}

where child1.pl:

local $SIG{INT} = "DEFAULT";
sleep 10;
say "Child timed out..";
exit 1;

child2.pl:

local $SIG{INT} = sub { die };
sleep 10;
say "Child timed out..";
exit 1;

and child3.pl:

eval {
    local $SIG{INT} = sub { die };
    sleep 10;
};
if ( $@ ) {
    print $@;
    exit 2;
}
say "Child timed out..";
exit 0;

parent.pl ( ) CTRL-C, , :

^CParent received return value: 130
^CDied at ./child2.pl line 7.
Parent received return value: 4
^CDied at ./child3.pl line 8.
Parent received return value: 2

, 130 1 4 2.

, , "DEFAULT" .

: , sh bash ( trap SIGINT INT bash).

. :

+4
2

, .

bash docs:

N, bash 128 + N .

SIGINT 2, 128 + 2 130.

Perl die , $! $? ( eval):

exit $! if $!;              # errno
exit $? >> 8 if $? >> 8;    # child exit status
exit 255;                   # last resort

, Perl as is, 8 .

errno 4 (. errno.h). $! . (, ), :

use v5.10;

local $SIG{INT}=sub{
    say "numeric errno is ", $!+0;
    die
    };
sleep 10;
print q(timed out);
exit 1;

:

$ bash -c "perl errno.pl"
^Cnumeric errno is 4
Died at errno.pl line 6.
$ echo $?
4
+6

:

, , DEFAULT .

"DEFAULT" , . (7). SIGINT .

, 130 1 4 2.

child1 SIGINT, . . SIGINT, . , (, , ), ( 2), .

, , SIGINT . , ( SIGINT). die() , , , , exit die.

+1

Source: https://habr.com/ru/post/1625626/


All Articles