From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 3F4391F8DB for ; Mon, 29 Jun 2020 10:34:25 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 4/5] spawn: unblock SIGCHLD in subprocess Date: Mon, 29 Jun 2020 10:34:20 +0000 Message-Id: <20200629103421.31016-5-e@yhbt.net> In-Reply-To: <20200629103421.31016-1-e@yhbt.net> References: <20200629103421.31016-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 888283d0d09..f90d8f6d3dd 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 34ce2052c60..a72d5a2d86f 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 44355f43655..fd669e222e1 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: $!";