]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SpawnPP.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / SpawnPP.pm
index 179aba5e737ccb524947dfd424d208c2618abed8..34ce2052c6047d1e4f8d2131c1c9c6cd1c56e2c4 100644 (file)
@@ -1,13 +1,16 @@
-# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2020 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
+# of vfork, so no speedups under Linux for spawning from large processes.
 package PublicInbox::SpawnPP;
 use strict;
 use warnings;
 use POSIX qw(dup2 :signal_h);
 
 # Pure Perl implementation for folks that do not use Inline::C
-sub public_inbox_fork_exec ($$$$$$) {
-       my ($in, $out, $err, $f, $cmd, $env) = @_;
+sub pi_fork_exec ($$$$$$) {
+       my ($redir, $f, $cmd, $env, $rlim, $cd) = @_;
        my $old = POSIX::SigSet->new();
        my $set = POSIX::SigSet->new();
        $set->fillset or die "fillset failed: $!";
@@ -19,22 +22,28 @@ sub public_inbox_fork_exec ($$$$$$) {
                $pid = -1;
        }
        if ($pid == 0) {
-               if ($in != 0) {
-                       dup2($in, 0) or die "dup2 failed for stdin: $!";
+               while (@$rlim) {
+                       my ($r, $soft, $hard) = splice(@$rlim, 0, 3);
+                       BSD::Resource::setrlimit($r, $soft, $hard) or
+                         warn "failed to set $r=[$soft,$hard]\n";
                }
-               if ($out != 1) {
-                       dup2($out, 1) or die "dup2 failed for stdout: $!";
+               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 ($err != 2) {
-                       dup2($err, 2) or die "dup2 failed for stderr: $!";
+               if ($cd ne '') {
+                       chdir $cd or die "chdir $cd: $!";
                }
-
                if ($ENV{MOD_PERL}) {
-                       exec qw(env -i), @$env, @$cmd;
+                       exec which('env'), '-i', @$env, @$cmd;
                        die "exec env -i ... $cmd->[0] failed: $!\n";
                } else {
                        local %ENV = map { split(/=/, $_, 2) } @$env;
-                       exec @$cmd;
+                       my @cmd = @$cmd;
+                       $cmd[0] = $f;
+                       exec @cmd;
                        die "exec $cmd->[0] failed: $!\n";
                }
        }