user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] spawn: proper signal handling for vfork
Date: Mon,  2 May 2016 08:55:01 +0000	[thread overview]
Message-ID: <20160502085501.8185-1-e@80x24.org> (raw)

We cannot afford to fire Perl-level signal handlers in the
vforked child process since they're not designed to run in
the child like that.

Thus we need to block all signals before calling vfork, reset
signal dispositions in the child, and restore the signal mask in
the parent.

ref: https://ewontfix.com/7
---
 lib/PublicInbox/Spawn.pm | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index 23f303f..c5a5c01 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -24,6 +24,8 @@ my $vfork_spawn = <<'VFORK_SPAWN';
 #include <sys/uio.h>
 #include <unistd.h>
 #include <alloca.h>
+#include <signal.h>
+#include <assert.h>
 
 #define AV_ALLOCA(av, max) alloca((max = (av_len((av)) + 1)) * sizeof(char *))
 
@@ -81,6 +83,8 @@ int public_inbox_fork_exec(int in, int out, int err,
 	pid_t pid;
 	char **argv, **envp;
 	I32 max;
+	sigset_t set, old;
+	int ret;
 
 	argv = AV_ALLOCA(cmd, max);
 	av2c_copy(argv, cmd, max);
@@ -88,14 +92,26 @@ int public_inbox_fork_exec(int in, int out, int err,
 	envp = AV_ALLOCA(env, max);
 	av2c_copy(envp, env, max);
 
+	ret = sigfillset(&set);
+	assert(ret == 0 && "BUG calling sigfillset");
+	ret = sigprocmask(SIG_SETMASK, &set, &old);
+	assert(ret == 0 && "BUG calling sigprocmask to block");
 	pid = vfork();
 	if (pid == 0) {
+		int sig;
+
 		REDIR(in, 0);
 		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");
 		execve(filename, argv, envp);
 		xerr("execve failed");
 	}
+	ret = sigprocmask(SIG_SETMASK, &old, NULL);
+	assert(ret == 0 && "BUG calling sigprocmask to restore");
 
 	return (int)pid;
 }
@@ -111,7 +127,7 @@ if (defined $vfork_spawn) {
 		my $f = "$inline_dir/.public-inbox.lock";
 		open my $fh, '>', $f or die "failed to open $f: $!\n";
 		flock($fh, LOCK_EX) or die "LOCK_EX failed on $f: $!\n";
-		eval 'use Inline C => $vfork_spawn';
+		eval 'use Inline C => $vfork_spawn'; #, BUILD_NOISY => 1';
 		my $err = $@;
 		flock($fh, LOCK_UN) or die "LOCK_UN failed on $f: $!\n";
 		die $err if $err;
-- 
EW


                 reply	other threads:[~2016-05-02  8:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160502085501.8185-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).