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 # 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>
30 #define AV_ALLOCA(av, max) alloca((max = (av_len((av)) + 1)) * sizeof(char *))
32 static void av2c_copy(char **dst, AV *src, I32 max)
36 for (i = 0; i < max; i++) {
37 SV **sv = av_fetch(src, i, 0);
38 dst[i] = sv ? SvPV_nolen(*sv) : 0;
43 static void *deconst(const char *s)
45 union { const char *in; void *out; } u;
50 /* needs to be safe inside a vfork'ed process */
51 static void xerr(const char *msg)
54 const char *err = strerror(errno); /* should be safe in practice */
56 iov[0].iov_base = deconst(msg);
57 iov[0].iov_len = strlen(msg);
58 iov[1].iov_base = deconst(err);
59 iov[1].iov_len = strlen(err);
60 iov[2].iov_base = deconst("\n");
66 #define REDIR(var,fd) do { \
67 if (var != fd && dup2(var, fd) < 0) \
68 xerr("error redirecting std"#var ": "); \
72 * unstable internal API. This was easy to implement but does not
73 * support arbitrary redirects. It'll be updated depending on
74 * whatever we'll need in the future.
75 * Be sure to update PublicInbox::SpawnPP if this changes
77 int public_inbox_fork_exec(int in, int out, int err,
78 SV *file, SV *cmdref, SV *envref)
80 AV *cmd = (AV *)SvRV(cmdref);
81 AV *env = (AV *)SvRV(envref);
82 const char *filename = SvPV_nolen(file);
89 argv = AV_ALLOCA(cmd, max);
90 av2c_copy(argv, cmd, max);
92 envp = AV_ALLOCA(env, max);
93 av2c_copy(envp, env, max);
95 ret = sigfillset(&set);
96 assert(ret == 0 && "BUG calling sigfillset");
97 ret = sigprocmask(SIG_SETMASK, &set, &old);
98 assert(ret == 0 && "BUG calling sigprocmask to block");
106 for (sig = 1; sig < NSIG; sig++)
107 signal(sig, SIG_DFL); /* ignore errors on signals */
109 * don't bother unblocking, we don't want signals
110 * to the group taking out a subprocess
112 execve(filename, argv, envp);
113 xerr("execve failed");
116 ret = sigprocmask(SIG_SETMASK, &old, NULL);
117 assert(ret == 0 && "BUG calling sigprocmask to restore");
124 my $inline_dir = $ENV{PERL_INLINE_DIRECTORY};
125 $vfork_spawn = undef unless defined $inline_dir && -d $inline_dir && -w _;
126 if (defined $vfork_spawn) {
127 # Inline 0.64 or later has locking in multi-process env,
128 # but we support 0.5 on Debian wheezy
129 use Fcntl qw(:flock);
131 my $f = "$inline_dir/.public-inbox.lock";
132 open my $fh, '>', $f or die "failed to open $f: $!\n";
133 flock($fh, LOCK_EX) or die "LOCK_EX failed on $f: $!\n";
134 eval 'use Inline C => $vfork_spawn'; #, BUILD_NOISY => 1';
136 flock($fh, LOCK_UN) or die "LOCK_UN failed on $f: $!\n";
140 warn "Inline::C failed for vfork: $@\n";
141 $vfork_spawn = undef;
145 unless (defined $vfork_spawn) {
146 require PublicInbox::SpawnPP;
148 *public_inbox_fork_exec = *PublicInbox::SpawnPP::public_inbox_fork_exec
151 # n.b. we never use absolute paths with this
154 foreach my $p (split(':', $ENV{PATH})) {
162 my ($cmd, $env, $opts) = @_;
163 my $f = which($cmd->[0]);
164 defined $f or die "$cmd->[0]: command not found\n";
168 my %env = $opts->{-env} ? () : %ENV;
170 foreach my $k (keys %$env) {
179 while (my ($k, $v) = each %env) {
182 my $in = $opts->{0} || 0;
183 my $out = $opts->{1} || 1;
184 my $err = $opts->{2} || 2;
185 my $pid = public_inbox_fork_exec($in, $out, $err, $f, $cmd, \@env);
186 $pid < 0 ? undef : $pid;
190 my ($cmd, $env, $opts) = @_;
191 pipe(my ($r, $w)) or die "pipe: $!\n";
193 my $blocking = $opts->{Blocking};
194 IO::Handle::blocking($r, $blocking) if defined $blocking;
195 $opts->{1} = fileno($w);
196 my $pid = spawn($cmd, $env, $opts);
197 return unless defined $pid;
198 return ($r, $pid) if wantarray;
200 tie *$ret, 'PublicInbox::ProcessPipe', $pid, $r;