about summary refs log tree commit homepage
path: root/lib/PublicInbox/SpawnPP.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-12-29 12:51:18 +0000
committerEric Wong <e@80x24.org>2019-12-30 00:06:23 +0000
commit267371b1273b518215939e817e53733584b68af7 (patch)
tree565515b689baf8e7384a384fd114d8d3f44546a2 /lib/PublicInbox/SpawnPP.pm
parent0a65fd278cb5b4e308fc2535753a3f1a5e78cbf4 (diff)
downloadpublic-inbox-267371b1273b518215939e817e53733584b68af7.tar.gz
We can save callers the trouble of {-hold} and {-dev_null}
refs as well as the trouble of calling fileno().
Diffstat (limited to 'lib/PublicInbox/SpawnPP.pm')
-rw-r--r--lib/PublicInbox/SpawnPP.pm18
1 files changed, 7 insertions, 11 deletions
diff --git a/lib/PublicInbox/SpawnPP.pm b/lib/PublicInbox/SpawnPP.pm
index 29b13371..2ac02c56 100644
--- a/lib/PublicInbox/SpawnPP.pm
+++ b/lib/PublicInbox/SpawnPP.pm
@@ -9,8 +9,8 @@ use warnings;
 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) = @_;
+sub pi_fork_exec ($$$$$) {
+        my ($redir, $f, $cmd, $env, $rlim) = @_;
         my $old = POSIX::SigSet->new();
         my $set = POSIX::SigSet->new();
         $set->fillset or die "fillset failed: $!";
@@ -27,16 +27,12 @@ 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 ($err != 2) {
-                        dup2($err, 2) or die "dup2 failed for stderr: $!";
-                }
-
                 if ($ENV{MOD_PERL}) {
                         exec which('env'), '-i', @$env, @$cmd;
                         die "exec env -i ... $cmd->[0] failed: $!\n";