]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SpawnPP.pm
ae552dd8d93dbbd800ad26be782e68c043964c05
[public-inbox.git] / lib / PublicInbox / SpawnPP.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::SpawnPP;
4 use strict;
5 use warnings;
6 use POSIX qw(dup2);
7
8 # Pure Perl implementation for folks that do not use Inline::C
9 sub public_inbox_fork_exec ($$$$$$) {
10         my ($in, $out, $err, $f, $cmd, $env) = @_;
11         my $pid = fork;
12         if ($pid == 0) {
13                 if ($in != 0) {
14                         dup2($in, 0) or die "dup2 failed for stdin: $!";
15                 }
16                 if ($out != 1) {
17                         dup2($out, 1) or die "dup2 failed for stdout: $!";
18                 }
19                 if ($err != 2) {
20                         dup2($err, 2) or die "dup2 failed for stderr$!";
21                 }
22                 %ENV = ();
23                 foreach my $e (@$env) {
24                         my ($k, $v) = split('=', $e, 2);
25                         $ENV{$k} = $v;
26                 }
27                 exec @$cmd;
28                 exit 1;
29         }
30         $pid;
31 }
32
33 1;