Return value from system() when using SIGINT default handler
<p>I am experiencing some strange return values from <code>system()</code> when a child process receives a SIGINT from the terminal. To explain, from a Perl script <code>parent.pl</code> I used <code>system()</code> to run another Perl script as a child process, but I also needed to run the child through the shell, so I used the <code>system 'sh', '-c', ...</code> form.. So the parent of the child became the <code>sh</code> process and the parent of the <code>sh</code> process became <code>parent.pl</code>. Also, to avoid having the <code>sh</code> process receiving the <code>SIGINT</code> signal, I trapped it. </p>

<p>For example, <code>parent.pl</code>:</p>

<pre><code>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 &gt;&gt; 8);
}
</code></pre>

<p>where <code>child1.pl</code>:</p>

<pre><code>local $SIG{INT} = "DEFAULT";
sleep 10;
say "Child timed out..";
exit 1;
</code></pre>

<p><code>child2.pl</code>:</p>

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

<p>and <code>child3.pl</code> is:</p>

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

<p>If I run <code>parent.pl</code> (from the command line) and press CTRL-C to abort each child process, the output is:</p>

<pre><code>^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
</code></pre>

<p>Now, I would like to know why I get a return value of 130 for case 1, and a return value of 4 for case 2.</p>

<p>Also, it would be nice to know exactly what the <code>"DEFAULT"</code> signal handler does in this case.</p>

<p>Note: the same values are returned if I replace <code>sh</code> with <code>bash</code> ( and trap <code>SIGINT</code> instead of <code>INT</code> in <code>bash</code> ).</p>

<p>See also:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/34795478/propagation-of-signal-to-parent-when-using-system">Propagation of signal to parent when using system</a></li>
<li><a href="http://perldoc.perl.org/perlipc.html" rel="nofollow noreferrer">perlipc</a></li>
<li>Chapter 15, in <a href="http://shop.oreilly.com/product/9780596004927.do" rel="nofollow noreferrer">Programming Perl, 4th Edition</a></li>
</ul>