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);
18 use PublicInbox::ProcessPipe;
19 our @EXPORT_OK = qw/which spawn popen_rd/;
21 my $vfork_spawn = <<'VFORK_SPAWN';
22 #include <sys/types.h>
27 #define AV_ALLOCA(av, max) alloca((max = (av_len((av)) + 1)) * sizeof(char *))
29 static void av2c_copy(char **dst, AV *src, I32 max)
33 for (i = 0; i < max; i++) {
34 SV **sv = av_fetch(src, i, 0);
35 dst[i] = sv ? SvPV_nolen(*sv) : 0;
40 static void *deconst(const char *s)
42 union { const char *in; void *out; } u;
47 /* needs to be safe inside a vfork'ed process */
48 static void xerr(const char *msg)
51 const char *err = strerror(errno); /* should be safe in practice */
53 iov[0].iov_base = deconst(msg);
54 iov[0].iov_len = strlen(msg);
55 iov[1].iov_base = deconst(err);
56 iov[1].iov_len = strlen(err);
57 iov[2].iov_base = deconst("\n");
63 #define REDIR(var,fd) do { \
64 if (var != fd && dup2(var, fd) < 0) \
65 xerr("error redirecting std"#var ": "); \
69 * unstable internal API. This was easy to implement but does not
70 * support arbitrary redirects. It'll be updated depending on
71 * whatever we'll need in the future.
72 * Be sure to update PublicInbox::SpawnPP if this changes
74 int public_inbox_fork_exec(int in, int out, int err,
75 SV *file, SV *cmdref, SV *envref)
77 AV *cmd = (AV *)SvRV(cmdref);
78 AV *env = (AV *)SvRV(envref);
79 const char *filename = SvPV_nolen(file);
84 argv = AV_ALLOCA(cmd, max);
85 av2c_copy(argv, cmd, max);
87 envp = AV_ALLOCA(env, max);
88 av2c_copy(envp, env, max);
95 execve(filename, argv, envp);
96 xerr("execve failed");
103 my $inline_dir = $ENV{PERL_INLINE_DIRECTORY};
104 $vfork_spawn = undef unless defined $inline_dir && -d $inline_dir && -w _;
105 if (defined $vfork_spawn) {
106 # Inline 0.64 or later has locking in multi-process env,
107 # but we support 0.5 on Debian wheezy
108 use Fcntl qw(:flock);
110 my $f = "$inline_dir/.public-inbox.lock";
111 open my $fh, '>', $f or die "failed to open $f: $!\n";
112 flock($fh, LOCK_EX) or die "LOCK_EX failed on $f: $!\n";
113 eval 'use Inline C => $vfork_spawn';
115 flock($fh, LOCK_UN) or die "LOCK_UN failed on $f: $!\n";
119 warn "Inline::C failed for vfork: $@\n";
120 $vfork_spawn = undef;
124 unless (defined $vfork_spawn) {
125 require PublicInbox::SpawnPP;
127 *public_inbox_fork_exec = *PublicInbox::SpawnPP::public_inbox_fork_exec
132 foreach my $p (split(':', $ENV{PATH})) {
140 my ($cmd, $env, $opts) = @_;
141 my $f = which($cmd->[0]);
142 defined $f or die "$cmd->[0]: command not found\n";
146 my %env = $opts->{-env} ? () : %ENV;
148 foreach my $k (keys %$env) {
157 while (my ($k, $v) = each %env) {
160 my $in = $opts->{0} || 0;
161 my $out = $opts->{1} || 1;
162 my $err = $opts->{2} || 2;
163 public_inbox_fork_exec($in, $out, $err, $f, $cmd, \@env);
167 my ($cmd, $env, $opts) = @_;
168 pipe(my ($r, $w)) or die "pipe: $!\n";
170 my $blocking = $opts->{Blocking};
171 $r->blocking($blocking) if defined $blocking;
172 $opts->{1} = fileno($w);
173 my $pid = spawn($cmd, $env, $opts);
174 return ($r, $pid) if wantarray;
176 tie *$ret, 'PublicInbox::ProcessPipe', $pid, $r;