1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Pure-Perl implementation of "spawn". This can't take advantage
5 # of vfork, so no speedups under Linux for spawning from large processes.
6 package PublicInbox::SpawnPP;
9 use POSIX qw(dup2 :signal_h);
11 # Pure Perl implementation for folks that do not use Inline::C
12 sub pi_fork_exec ($$$$$$) {
13 my ($in, $out, $err, $f, $cmd, $env, $rlim) = @_;
14 my $old = POSIX::SigSet->new();
15 my $set = POSIX::SigSet->new();
16 $set->fillset or die "fillset failed: $!";
17 sigprocmask(SIG_SETMASK, $set, $old) or die "can't block signals: $!";
20 unless (defined $pid) { # compat with Inline::C version
26 my ($r, $soft, $hard) = splice(@$rlim, 0, 3);
27 BSD::Resource::setrlimit($r, $soft, $hard) or
28 warn "failed to set $r=[$soft,$hard]\n";
31 dup2($in, 0) or die "dup2 failed for stdin: $!";
34 dup2($out, 1) or die "dup2 failed for stdout: $!";
37 dup2($err, 2) or die "dup2 failed for stderr: $!";
41 exec qw(env -i), @$env, @$cmd;
42 die "exec env -i ... $cmd->[0] failed: $!\n";
44 local %ENV = map { split(/=/, $_, 2) } @$env;
46 die "exec $cmd->[0] failed: $!\n";
49 sigprocmask(SIG_SETMASK, $old) or die "can't unblock signals: $!";