Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3d2ca8c

Browse files
tmdsstephentoub
authored andcommitted
Don't change SIGINT/SIGQUIT handling when they are set to SIG_IGN (#25753)
1 parent d425f86 commit 3d2ca8c

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/Native/Unix/System.Native/pal_console.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -394,31 +394,18 @@ void* SignalHandlerLoop(void* arg)
394394
// be picked up by the previously registered handler. In the most common case,
395395
// this will be the default handler, causing the process to be torn down.
396396
// It could also be a custom handle registered by other code before us.
397-
// In the rare case where the signal is set to be ignored, though, we don't
398-
// want to do that, as we know our process will simply remain running yet our
399-
// handlers will never end up being invoked again. (It's possible that can
400-
// happen as well in the custom case, but we can't detect that or handle it well,
401-
// at which point we'll just stop responding to the relevant signal here if the
402-
// process does remain alive. We only unregister from the relevant handler, though,
403-
// so the handler(s) for the other signal(s) will still remain registered.)
404397

405398
if (signalCode == SIGINT)
406399
{
407-
if (reinterpret_cast<void*>(g_origSigIntHandler.sa_sigaction) != reinterpret_cast<void*>(SIG_IGN))
408-
{
409-
UninitializeConsole();
410-
sigaction(SIGINT, &g_origSigIntHandler, NULL);
411-
kill(getpid(), SIGINT);
412-
}
400+
UninitializeConsole();
401+
sigaction(SIGINT, &g_origSigIntHandler, NULL);
402+
kill(getpid(), SIGINT);
413403
}
414404
else if (signalCode == SIGQUIT)
415405
{
416-
if (reinterpret_cast<void*>(g_origSigQuitHandler.sa_sigaction) != reinterpret_cast<void*>(SIG_IGN))
417-
{
418-
UninitializeConsole();
419-
sigaction(SIGQUIT, &g_origSigQuitHandler, NULL);
420-
kill(getpid(), SIGQUIT);
421-
}
406+
UninitializeConsole();
407+
sigaction(SIGQUIT, &g_origSigQuitHandler, NULL);
408+
kill(getpid(), SIGQUIT);
422409
}
423410

424411
}
@@ -478,11 +465,23 @@ static bool InitializeSignalHandling()
478465
int rv;
479466

480467
// Hook up signal handlers for use with ctrl-C / ctrl-Break handling
468+
// We don't handle ignored signals. If we'd setup a handler, our child processes
469+
// would reset to the default on exec causing them to terminate on these signals.
481470
newAction.sa_sigaction = &TransferSignalToHandlerLoop;
482-
rv = sigaction(SIGINT, &newAction, &g_origSigIntHandler);
471+
rv = sigaction(SIGINT, NULL, &g_origSigIntHandler);
483472
assert(rv == 0);
484-
rv = sigaction(SIGQUIT, &newAction, &g_origSigQuitHandler);
473+
if (reinterpret_cast<void*>(g_origSigIntHandler.sa_sigaction) != reinterpret_cast<void*>(SIG_IGN))
474+
{
475+
rv = sigaction(SIGINT, &newAction, NULL);
476+
assert(rv == 0);
477+
}
478+
rv = sigaction(SIGQUIT, NULL, &g_origSigQuitHandler);
485479
assert(rv == 0);
480+
if (reinterpret_cast<void*>(g_origSigQuitHandler.sa_sigaction) != reinterpret_cast<void*>(SIG_IGN))
481+
{
482+
rv = sigaction(SIGQUIT, &newAction, NULL);
483+
assert(rv == 0);
484+
}
486485

487486
// Hook up signal handlers for use with signals that require us to reinitialize the terminal
488487
newAction.sa_sigaction = &HandleSignalForReinitialize;

0 commit comments

Comments
 (0)