From 761baa2a300e426885675a01e4773193ab7101ff Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 29 Jun 2020 10:34:20 +0000 Subject: spawn: unblock SIGCHLD in subprocess Subprocess we spawn may want to use SIGCHLD for themselves. This also ensures we restore default signal handlers in the pure Perl version. --- lib/PublicInbox/Spawn.pm | 11 ++++++++--- lib/PublicInbox/SpawnPP.pm | 5 +++++ t/spawn.t | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm index 888283d0..f90d8f6d 100644 --- a/lib/PublicInbox/Spawn.pm +++ b/lib/PublicInbox/Spawn.pm @@ -78,7 +78,7 @@ int pi_fork_exec(SV *redirref, SV *file, SV *cmdref, SV *envref, SV *rlimref, 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); @@ -88,6 +88,10 @@ int pi_fork_exec(SV *redirref, SV *file, SV *cmdref, SV *envref, SV *rlimref, 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; @@ -120,9 +124,10 @@ int pi_fork_exec(SV *redirref, SV *file, SV *cmdref, SV *envref, SV *rlimref, } /* - * 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); } diff --git a/lib/PublicInbox/SpawnPP.pm b/lib/PublicInbox/SpawnPP.pm index 34ce2052..a72d5a2d 100644 --- a/lib/PublicInbox/SpawnPP.pm +++ b/lib/PublicInbox/SpawnPP.pm @@ -36,6 +36,11 @@ sub pi_fork_exec ($$$$$$) { 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"; diff --git a/t/spawn.t b/t/spawn.t index 44355f43..fd669e22 100644 --- a/t/spawn.t +++ b/t/spawn.t @@ -4,6 +4,7 @@ use strict; use warnings; use Test::More; use PublicInbox::Spawn qw(which spawn popen_rd); +use PublicInbox::Sigfd; { my $true = which('true'); @@ -17,6 +18,32 @@ use PublicInbox::Spawn qw(which spawn popen_rd); 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: $!"; -- cgit v1.2.3-24-ge0c7