Skip to content

Commit dd47a8c

Browse files
committed
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: #615 Reviewed-By: Sam Roberts <sam@strongloop.com>
1 parent 63ae1d2 commit dd47a8c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/node.cc

+16-1
Original file line numberDiff line numberDiff line change
@@ -3313,9 +3313,24 @@ inline void PlatformInit() {
33133313
sigemptyset(&sigmask);
33143314
sigaddset(&sigmask, SIGUSR1);
33153315
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
3316-
RegisterSignalHandler(SIGPIPE, SIG_IGN);
3316+
3317+
// Restore signal dispositions, the parent process may have changed them.
3318+
struct sigaction act;
3319+
memset(&act, 0, sizeof(act));
3320+
3321+
// The hard-coded upper limit is because NSIG is not very reliable; on Linux,
3322+
// it evaluates to 32, 34 or 64, depending on whether RT signals are enabled.
3323+
// Counting up to SIGRTMIN doesn't work for the same reason.
3324+
for (unsigned nr = 1; nr < 32; nr += 1) {
3325+
if (nr == SIGKILL || nr == SIGSTOP)
3326+
continue;
3327+
act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
3328+
CHECK_EQ(0, sigaction(nr, &act, nullptr));
3329+
}
3330+
33173331
RegisterSignalHandler(SIGINT, SignalExit, true);
33183332
RegisterSignalHandler(SIGTERM, SignalExit, true);
3333+
33193334
// Raise the open file descriptor limit.
33203335
struct rlimit lim;
33213336
if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {

0 commit comments

Comments
 (0)