src: set default signal dispositions at start-up
authorBen Noordhuis <info@bnoordhuis.nl>
Mon, 26 Jan 2015 23:07:34 +0000 (00:07 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Wed, 28 Jan 2015 16:44:07 +0000 (17:44 +0100)
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 <sam@strongloop.com>
src/node.cc

index 71dd3a8..72ddbe1 100644 (file)
@@ -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) {