X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSpawn.pm;h=2d9f734c1b6df46331a86f2802b1aa3972fee004;hb=95bdac7f09c69036efed537a4d03d5bdd2ae4eb6;hp=d624c521ade4cc20f16c04b3e6f634e3e7753d65;hpb=30afcadb13f446c99952883bbaa54e102757b682;p=public-inbox.git
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index d624c521..2d9f734c 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -1,4 +1,4 @@
-# Copyright (C) 2016-2019 all contributors
+# Copyright (C) 2016-2020 all contributors
# License: AGPL-3.0+
#
# This allows vfork to be used for spawning subprocesses if
@@ -15,7 +15,6 @@ use strict;
use warnings;
use base qw(Exporter);
use Symbol qw(gensym);
-use IO::Handle;
use PublicInbox::ProcessPipe;
our @EXPORT_OK = qw/which spawn popen_rd/;
sub RLIMITS () { qw(RLIMIT_CPU RLIMIT_CORE RLIMIT_DATA) }
@@ -56,26 +55,10 @@ my $vfork_spawn = <<'VFORK_SPAWN';
dst[real_len] = 0; \
} while (0)
-static void *deconst(const char *s)
-{
- union { const char *in; void *out; } u;
- u.in = s;
- return u.out;
-}
-
/* needs to be safe inside a vfork'ed process */
-static void xerr(const char *msg)
+static void exit_err(int *cerrnum)
{
- struct iovec iov[3];
- const char *err = strerror(errno); /* should be safe in practice */
-
- iov[0].iov_base = deconst(msg);
- iov[0].iov_len = strlen(msg);
- iov[1].iov_base = deconst(err);
- iov[1].iov_len = strlen(err);
- iov[2].iov_base = deconst("\n");
- iov[2].iov_len = 1;
- writev(2, iov, 3);
+ *cerrnum = errno;
_exit(1);
}
@@ -95,7 +78,7 @@ int pi_fork_exec(SV *redirref, SV *file, SV *cmdref, SV *envref, SV *rlimref,
pid_t pid;
char **argv, **envp;
sigset_t set, old;
- int ret, errnum;
+ int ret, perrnum, cerrnum = 0;
AV2C_COPY(argv, cmd);
AV2C_COPY(envp, env);
@@ -115,12 +98,12 @@ int pi_fork_exec(SV *redirref, SV *file, SV *cmdref, SV *envref, SV *rlimref,
if (parent_fd == child_fd)
continue;
if (dup2(parent_fd, child_fd) < 0)
- xerr("dup2");
+ exit_err(&cerrnum);
}
for (sig = 1; sig < NSIG; sig++)
signal(sig, SIG_DFL); /* ignore errors on signals */
if (*cd && chdir(cd) < 0)
- xerr("chdir");
+ exit_err(&cerrnum);
max = av_len(rlim);
for (i = 0; i < max; i += 3) {
@@ -132,7 +115,7 @@ int pi_fork_exec(SV *redirref, SV *file, SV *cmdref, SV *envref, SV *rlimref,
rl.rlim_cur = SvIV(*soft);
rl.rlim_max = SvIV(*hard);
if (setrlimit(SvIV(*res), &rl) < 0)
- xerr("sertlimit");
+ exit_err(&cerrnum);
}
/*
@@ -140,13 +123,19 @@ int pi_fork_exec(SV *redirref, SV *file, SV *cmdref, SV *envref, SV *rlimref,
* to the group taking out a subprocess
*/
execve(filename, argv, envp);
- xerr("execve failed");
+ exit_err(&cerrnum);
}
- errnum = errno;
+ perrnum = errno;
ret = sigprocmask(SIG_SETMASK, &old, NULL);
assert(ret == 0 && "BUG calling sigprocmask to restore");
- errno = errnum;
-
+ if (cerrnum) {
+ if (pid > 0)
+ waitpid(pid, NULL, 0);
+ pid = -1;
+ errno = cerrnum;
+ } else if (perrnum) {
+ errno = perrnum;
+ }
return (int)pid;
}
VFORK_SPAWN
@@ -221,7 +210,8 @@ sub spawn ($;$$) {
}
my $cd = $opts->{'-C'} // ''; # undef => NULL mapping doesn't work?
my $pid = pi_fork_exec($redir, $f, $cmd, \@env, $rlim, $cd);
- $pid < 0 ? undef : $pid;
+ die "fork_exec failed: $!\n" unless defined $pid;
+ $pid;
}
sub popen_rd {
@@ -230,7 +220,6 @@ sub popen_rd {
$opts ||= {};
$opts->{1} = fileno($w);
my $pid = spawn($cmd, $env, $opts);
- return unless defined $pid;
return ($r, $pid) if wantarray;
my $ret = gensym;
tie *$ret, 'PublicInbox::ProcessPipe', $pid, $r;