1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # This allows vfork to be used for spawning subprocesses if
5 # PERL_INLINE_DIRECTORY is explicitly defined in the environment.
6 # Under Linux, vfork can make a big difference in spawning performance
7 # as process size increases (fork still needs to mark pages for CoW use).
8 # Currently, we only use this for code intended for long running
9 # daemons (inside the PSGI code (-httpd) and -nntpd). The short-lived
10 # scripts (-mda, -index, -learn, -init) either use IPC::run or standard
13 package PublicInbox::Spawn;
16 use base qw(Exporter);
17 use Symbol qw(gensym);
19 use PublicInbox::ProcessPipe;
20 our @EXPORT_OK = qw/which spawn popen_rd/;
22 my $vfork_spawn = <<'VFORK_SPAWN';
23 #include <sys/types.h>
28 #define AV_ALLOCA(av, max) alloca((max = (av_len((av)) + 1)) * sizeof(char *))
30 static void av2c_copy(char **dst, AV *src, I32 max)
34 for (i = 0; i < max; i++) {
35 SV **sv = av_fetch(src, i, 0);
36 dst[i] = sv ? SvPV_nolen(*sv) : 0;
41 static void *deconst(const char *s)
43 union { const char *in; void *out; } u;
48 /* needs to be safe inside a vfork'ed process */
49 static void xerr(const char *msg)
52 const char *err = strerror(errno); /* should be safe in practice */
54 iov[0].iov_base = deconst(msg);
55 iov[0].iov_len = strlen(msg);
56 iov[1].iov_base = deconst(err);
57 iov[1].iov_len = strlen(err);
58 iov[2].iov_base = deconst("\n");
64 #define REDIR(var,fd) do { \
65 if (var != fd && dup2(var, fd) < 0) \
66 xerr("error redirecting std"#var ": "); \
70 * unstable internal API. This was easy to implement but does not
71 * support arbitrary redirects. It'll be updated depending on
72 * whatever we'll need in the future.
73 * Be sure to update PublicInbox::SpawnPP if this changes
75 int public_inbox_fork_exec(int in, int out, int err,
76 SV *file, SV *cmdref, SV *envref)
78 AV *cmd = (AV *)SvRV(cmdref);
79 AV *env = (AV *)SvRV(envref);
80 const char *filename = SvPV_nolen(file);
85 argv = AV_ALLOCA(cmd, max);
86 av2c_copy(argv, cmd, max);
88 envp = AV_ALLOCA(env, max);
89 av2c_copy(envp, env, max);
96 execve(filename, argv, envp);
97 xerr("execve failed");
104 my $inline_dir = $ENV{PERL_INLINE_DIRECTORY};
105 $vfork_spawn = undef unless defined $inline_dir && -d $inline_dir && -w _;
106 if (defined $vfork_spawn) {
107 # Inline 0.64 or later has locking in multi-process env,
108 # but we support 0.5 on Debian wheezy
109 use Fcntl qw(:flock);
111 my $f = "$inline_dir/.public-inbox.lock";
112 open my $fh, '>', $f or die "failed to open $f: $!\n";
113 flock($fh, LOCK_EX) or die "LOCK_EX failed on $f: $!\n";
114 eval 'use Inline C => $vfork_spawn';
116 flock($fh, LOCK_UN) or die "LOCK_UN failed on $f: $!\n";
120 warn "Inline::C failed for vfork: $@\n";
121 $vfork_spawn = undef;
125 unless (defined $vfork_spawn) {
126 require PublicInbox::SpawnPP;
128 *public_inbox_fork_exec = *PublicInbox::SpawnPP::public_inbox_fork_exec
133 foreach my $p (split(':', $ENV{PATH})) {
141 my ($cmd, $env, $opts) = @_;
142 my $f = which($cmd->[0]);
143 defined $f or die "$cmd->[0]: command not found\n";
147 my %env = $opts->{-env} ? () : %ENV;
149 foreach my $k (keys %$env) {
158 while (my ($k, $v) = each %env) {
161 my $in = $opts->{0} || 0;
162 my $out = $opts->{1} || 1;
163 my $err = $opts->{2} || 2;
164 public_inbox_fork_exec($in, $out, $err, $f, $cmd, \@env);
168 my ($cmd, $env, $opts) = @_;
169 pipe(my ($r, $w)) or die "pipe: $!\n";
171 my $blocking = $opts->{Blocking};
172 IO::Handle::blocking($r, $blocking) if defined $blocking;
173 $opts->{1} = fileno($w);
174 my $pid = spawn($cmd, $env, $opts);
175 return ($r, $pid) if wantarray;
177 tie *$ret, 'PublicInbox::ProcessPipe', $pid, $r;