about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-06-29 10:34:20 +0000
committerEric Wong <e@yhbt.net>2020-06-30 03:05:29 +0000
commit761baa2a300e426885675a01e4773193ab7101ff (patch)
tree2dc5ddff8959adb2b09060bb415fc4ede789cdd8
parent38c87c5d556e218abe59d874f0ad7968d17a79b8 (diff)
downloadpublic-inbox-761baa2a300e426885675a01e4773193ab7101ff.tar.gz
Subprocess we spawn may want to use SIGCHLD for themselves.
This also ensures we restore default signal handlers
in the pure Perl version.
-rw-r--r--lib/PublicInbox/Spawn.pm11
-rw-r--r--lib/PublicInbox/SpawnPP.pm5
-rw-r--r--t/spawn.t27
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: $!";