]> Sergey Matveev's repositories - public-inbox.git/commitdiff
spawn: unblock SIGCHLD in subprocess
authorEric Wong <e@yhbt.net>
Mon, 29 Jun 2020 10:34:20 +0000 (10:34 +0000)
committerEric Wong <e@yhbt.net>
Tue, 30 Jun 2020 03:05:29 +0000 (03:05 +0000)
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
lib/PublicInbox/SpawnPP.pm
t/spawn.t

index 888283d0d090af72eaf1fd7f1a05c867164957d8..f90d8f6d3dd2d9d7f1ea6a73c8f41a70eaf718bf 100644 (file)
@@ -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);
        }
index 34ce2052c6047d1e4f8d2131c1c9c6cd1c56e2c4..a72d5a2d86fb02a0ea4469eb0e38d5c650c13f9f 100644 (file)
@@ -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";
index 44355f43655582845755b674847494eb18b5df4c..fd669e222e1bd7031e1a13cb7ebd23580930a1c1 100644 (file)
--- 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: $!";