From dd47a8c78547db14ea0c7fc2f3375e8c9cb1a129 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 27 Jan 2015 00:07:34 +0100 Subject: [PATCH] src: set default signal dispositions at start-up Signal dispositions are inherited by child processes. Restore ours to sane defaults in case our parent process changed it, to prevent quirky behavior when the parent does something silly like ignoring SIGSEGV. PR-URL: https://github.com/iojs/io.js/pull/615 Reviewed-By: Sam Roberts --- src/node.cc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index 71dd3a8a9d..72ddbe178e 100644 --- a/src/node.cc +++ b/src/node.cc @@ -3313,9 +3313,24 @@ inline void PlatformInit() { sigemptyset(&sigmask); sigaddset(&sigmask, SIGUSR1); CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr)); - RegisterSignalHandler(SIGPIPE, SIG_IGN); + + // Restore signal dispositions, the parent process may have changed them. + struct sigaction act; + memset(&act, 0, sizeof(act)); + + // The hard-coded upper limit is because NSIG is not very reliable; on Linux, + // it evaluates to 32, 34 or 64, depending on whether RT signals are enabled. + // Counting up to SIGRTMIN doesn't work for the same reason. + for (unsigned nr = 1; nr < 32; nr += 1) { + if (nr == SIGKILL || nr == SIGSTOP) + continue; + act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL; + CHECK_EQ(0, sigaction(nr, &act, nullptr)); + } + RegisterSignalHandler(SIGINT, SignalExit, true); RegisterSignalHandler(SIGTERM, SignalExit, true); + // Raise the open file descriptor limit. struct rlimit lim; if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {