Subprocess we spawn may want to use SIGCHLD for themselves.
This also ensures we restore default signal handlers
in the pure Perl version.
const char *filename = SvPV_nolen(file);
pid_t pid;
char **argv, **envp;
- sigset_t set, old;
+ sigset_t set, old, cset;
int ret, perrnum, cerrnum = 0;
AV2C_COPY(argv, cmd);
assert(ret == 0 && "BUG calling sigfillset");
ret = sigprocmask(SIG_SETMASK, &set, &old);
assert(ret == 0 && "BUG calling sigprocmask to block");
+ ret = sigemptyset(&cset);
+ assert(ret == 0 && "BUG calling sigemptyset");
+ ret = sigaddset(&cset, SIGCHLD);
+ assert(ret == 0 && "BUG calling sigaddset for SIGCHLD");
pid = vfork();
if (pid == 0) {
int sig;
}
/*
- * don't bother unblocking, we don't want signals
- * to the group taking out a subprocess
+ * don't bother unblocking other signals for now, just SIGCHLD.
+ * we don't want signals to the group taking out a subprocess
*/
+ (void)sigprocmask(SIG_UNBLOCK, &cset, NULL);
execve(filename, argv, envp);
exit_err(&cerrnum);
}
if ($cd ne '') {
chdir $cd or die "chdir $cd: $!";
}
+ $SIG{$_} = 'DEFAULT' for keys %SIG;
+ my $cset = POSIX::SigSet->new();
+ $cset->addset(POSIX::SIGCHLD) or die "can't add SIGCHLD: $!";
+ sigprocmask(SIG_UNBLOCK, $cset) or
+ die "can't unblock SIGCHLD: $!";
if ($ENV{MOD_PERL}) {
exec which('env'), '-i', @$env, @$cmd;
die "exec env -i ... $cmd->[0] failed: $!\n";
use warnings;
use Test::More;
use PublicInbox::Spawn qw(which spawn popen_rd);
+use PublicInbox::Sigfd;
{
my $true = which('true');
is($?, 0, 'true exited successfully');
}
+{ # ensure waitpid(-1, 0) and SIGCHLD works in spawned process
+ my $script = <<'EOF';
+$| = 1; # unbuffer stdout
+defined(my $pid = fork) or die "fork: $!";
+if ($pid == 0) { exit }
+elsif ($pid > 0) {
+ my $waited = waitpid(-1, 0);
+ $waited == $pid or die "mismatched child $pid != $waited";
+ $? == 0 or die "child err: $>";
+ $SIG{CHLD} = sub { print "HI\n"; exit };
+ print "RDY $$\n";
+ sleep while 1;
+}
+EOF
+ my $oldset = PublicInbox::Sigfd::block_signals();
+ my $rd = popen_rd([$^X, '-e', $script]);
+ diag 'waiting for child to reap grandchild...';
+ chomp(my $line = readline($rd));
+ my ($rdy, $pid) = split(' ', $line);
+ is($rdy, 'RDY', 'got ready signal, waitpid(-1) works in child');
+ ok(kill('CHLD', $pid), 'sent SIGCHLD to child');
+ is(readline($rd), "HI\n", '$SIG{CHLD} works in child');
+ ok(close $rd, 'popen_rd close works');
+ PublicInbox::Sigfd::sig_setmask($oldset);
+}
+
{
my ($r, $w);
pipe $r, $w or die "pipe failed: $!";