X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSpawnPP.pm;h=b0ad4da5c6f7ca79467a158249587f21eb61c555;hb=af0b0fb7a454470a32c452119d0392e0dedb3fe1;hp=d0df94d0ded0f4cf179a2420a274e13da062be6d;hpb=3d41aa23f35501ca92aab8aa42980fa73f7fa74f;p=public-inbox.git diff --git a/lib/PublicInbox/SpawnPP.pm b/lib/PublicInbox/SpawnPP.pm index d0df94d0..b0ad4da5 100644 --- a/lib/PublicInbox/SpawnPP.pm +++ b/lib/PublicInbox/SpawnPP.pm @@ -1,13 +1,16 @@ -# Copyright (C) 2016-2018 all contributors +# Copyright (C) 2016-2021 all contributors # License: AGPL-3.0+ + +# 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,33 @@ 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: $!"; } - + $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 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"; } }