Skip to content

Commit

Permalink
src: set default signal dispositions at start-up
Browse files Browse the repository at this point in the history
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: #615
Reviewed-By: Sam Roberts <sam@strongloop.com>
  • Loading branch information
bnoordhuis committed Jan 28, 2015
1 parent 63ae1d2 commit dd47a8c
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit dd47a8c

Please sign in to comment.