]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Spawn.pm
update copyrights for 2018
[public-inbox.git] / lib / PublicInbox / Spawn.pm
index c5a5c01223a6461b5ff13daf186241178f2f6344..91a3c123e736635a833fcc6fefa810616349fb9c 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # This allows vfork to be used for spawning subprocesses if
@@ -84,7 +84,7 @@ int public_inbox_fork_exec(int in, int out, int err,
        char **argv, **envp;
        I32 max;
        sigset_t set, old;
-       int ret;
+       int ret, errnum;
 
        argv = AV_ALLOCA(cmd, max);
        av2c_copy(argv, cmd, max);
@@ -104,14 +104,18 @@ int public_inbox_fork_exec(int in, int out, int err,
                REDIR(out, 1);
                REDIR(err, 2);
                for (sig = 1; sig < NSIG; sig++)
-                       signal(sig, SIG_DFL); /* ignore errorrs on signals */
-               ret = sigprocmask(SIG_SETMASK, &old, NULL);
-               if (ret != 0) xerr("sigprocmask failed in vfork child");
+                       signal(sig, SIG_DFL); /* ignore errors on signals */
+               /*
+                * don't bother unblocking, we don't want signals
+                * to the group taking out a subprocess
+                */
                execve(filename, argv, envp);
                xerr("execve failed");
        }
+       errnum = errno;
        ret = sigprocmask(SIG_SETMASK, &old, NULL);
        assert(ret == 0 && "BUG calling sigprocmask to restore");
+       errno = errnum;
 
        return (int)pid;
 }
@@ -144,6 +148,7 @@ unless (defined $vfork_spawn) {
        *public_inbox_fork_exec = *PublicInbox::SpawnPP::public_inbox_fork_exec
 }
 
+# n.b. we never use absolute paths with this
 sub which ($) {
        my ($file) = @_;
        foreach my $p (split(':', $ENV{PATH})) {
@@ -177,7 +182,8 @@ sub spawn ($;$$) {
        my $in = $opts->{0} || 0;
        my $out = $opts->{1} || 1;
        my $err = $opts->{2} || 2;
-       public_inbox_fork_exec($in, $out, $err, $f, $cmd, \@env);
+       my $pid = public_inbox_fork_exec($in, $out, $err, $f, $cmd, \@env);
+       $pid < 0 ? undef : $pid;
 }
 
 sub popen_rd {
@@ -188,6 +194,7 @@ sub popen_rd {
        IO::Handle::blocking($r, $blocking) if defined $blocking;
        $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;