]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SpawnPP.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / SpawnPP.pm
index 29b1337103f59c164a6bd1c2fe79ea2d125cc743..b0ad4da5c6f7ca79467a158249587f21eb61c555 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # Pure-Perl implementation of "spawn".  This can't take advantage
@@ -10,7 +10,7 @@ use POSIX qw(dup2 :signal_h);
 
 # Pure Perl implementation for folks that do not use Inline::C
 sub pi_fork_exec ($$$$$$) {
-       my ($in, $out, $err, $f, $cmd, $env, $rlim) = @_;
+       my ($redir, $f, $cmd, $env, $rlim, $cd) = @_;
        my $old = POSIX::SigSet->new();
        my $set = POSIX::SigSet->new();
        $set->fillset or die "fillset failed: $!";
@@ -27,16 +27,20 @@ sub pi_fork_exec ($$$$$$) {
                        BSD::Resource::setrlimit($r, $soft, $hard) or
                          warn "failed to set $r=[$soft,$hard]\n";
                }
-               if ($in != 0) {
-                       dup2($in, 0) or die "dup2 failed for stdin: $!";
+               for my $child_fd (0..$#$redir) {
+                       my $parent_fd = $redir->[$child_fd];
+                       next if $parent_fd == $child_fd;
+                       dup2($parent_fd, $child_fd) or
+                               die "dup2($parent_fd, $child_fd): $!\n";
                }
-               if ($out != 1) {
-                       dup2($out, 1) or die "dup2 failed for stdout: $!";
+               if ($cd ne '') {
+                       chdir $cd or die "chdir $cd: $!";
                }
-               if ($err != 2) {
-                       dup2($err, 2) or die "dup2 failed for stderr: $!";
-               }
-
+               $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";