]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SpawnPP.pm
www: use WwwStream for dumping thread and search views
[public-inbox.git] / lib / PublicInbox / SpawnPP.pm
index ae552dd8d93dbbd800ad26be782e68c043964c05..179aba5e737ccb524947dfd424d208c2618abed8 100644 (file)
@@ -3,12 +3,21 @@
 package PublicInbox::SpawnPP;
 use strict;
 use warnings;
-use POSIX qw(dup2);
+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) = @_;
+       my $old = POSIX::SigSet->new();
+       my $set = POSIX::SigSet->new();
+       $set->fillset or die "fillset failed: $!";
+       sigprocmask(SIG_SETMASK, $set, $old) or die "can't block signals: $!";
+       my $syserr;
        my $pid = fork;
+       unless (defined $pid) { # compat with Inline::C version
+               $syserr = $!;
+               $pid = -1;
+       }
        if ($pid == 0) {
                if ($in != 0) {
                        dup2($in, 0) or die "dup2 failed for stdin: $!";
@@ -17,16 +26,20 @@ sub public_inbox_fork_exec ($$$$$$) {
                        dup2($out, 1) or die "dup2 failed for stdout: $!";
                }
                if ($err != 2) {
-                       dup2($err, 2) or die "dup2 failed for stderr$!";
+                       dup2($err, 2) or die "dup2 failed for stderr$!";
                }
-               %ENV = ();
-               foreach my $e (@$env) {
-                       my ($k, $v) = split('=', $e, 2);
-                       $ENV{$k} = $v;
+
+               if ($ENV{MOD_PERL}) {
+                       exec qw(env -i), @$env, @$cmd;
+                       die "exec env -i ... $cmd->[0] failed: $!\n";
+               } else {
+                       local %ENV = map { split(/=/, $_, 2) } @$env;
+                       exec @$cmd;
+                       die "exec $cmd->[0] failed: $!\n";
                }
-               exec @$cmd;
-               exit 1;
        }
+       sigprocmask(SIG_SETMASK, $old) or die "can't unblock signals: $!";
+       $! = $syserr;
        $pid;
 }