1 # Copyright (C) 2016-2020 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 ($redir, $f, $cmd, $env, $rlim, $cd) = @_;
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";
30 for my $child_fd (0..$#$redir) {
31 my $parent_fd = $redir->[$child_fd];
32 next if $parent_fd == $child_fd;
33 dup2($parent_fd, $child_fd) or
34 die "dup2($parent_fd, $child_fd): $!\n";
37 chdir $cd or die "chdir $cd: $!";
39 $SIG{$_} = 'DEFAULT' for keys %SIG;
40 my $cset = POSIX::SigSet->new();
41 $cset->addset(POSIX::SIGCHLD) or die "can't add SIGCHLD: $!";
42 sigprocmask(SIG_UNBLOCK, $cset) or
43 die "can't unblock SIGCHLD: $!";
45 exec which('env'), '-i', @$env, @$cmd;
46 die "exec env -i ... $cmd->[0] failed: $!\n";
48 local %ENV = map { split(/=/, $_, 2) } @$env;
52 die "exec $cmd->[0] failed: $!\n";
55 sigprocmask(SIG_SETMASK, $old) or die "can't unblock signals: $!";