Skip to content

Commit

Permalink
platform: handle SIG_IGN properly in SignalHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
magiblot committed Oct 15, 2023
1 parent 581dd6c commit 69f975c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions source/platform/sighandl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,23 @@ void SignalHandler::handleSignal(int signo, siginfo_t *info, void *context)
}
}

template <class T>
static inline bool isCustomHandler(T &&handler)
{
return (void *) handler != (void *) SIG_DFL
&& (void *) handler != (void *) SIG_IGN;
}

bool SignalHandler::invokeHandlerOrDefault( int signo, const struct sigaction &action,
siginfo_t *info, void *context ) noexcept
{
// If the handler is a custom one, invoke it directly.
if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction)
if ((action.sa_flags & SA_SIGINFO) && isCustomHandler(action.sa_sigaction))
action.sa_sigaction(signo, info, context);
else if (!(action.sa_flags & SA_SIGINFO) && action.sa_handler)
else if (!(action.sa_flags & SA_SIGINFO) && isCustomHandler(action.sa_handler))
action.sa_handler(signo);
else
// Run default handler by re-raising the signal.
return invokeDefault(signo, info);
return false;
}
Expand Down
12 changes: 12 additions & 0 deletions test/platform/sighandl.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ TEST_F(SignalHandlerTest, ShouldOnlyInvokeCallbackOnEnterWhenSignalKillsTheProce
ASSERT_EQ(counters->callbackExitInvocations, 0);
}

TEST_F(SignalHandlerTest, ShouldInvokeCallbackOnEnterAndExitWhenSignalIsIgnored)
{
installSignalHandler(SIGINT, SIG_IGN);
SignalHandler::enable(signalCallbackThatTemporarilyDisablesSignalHandler);
for (int i = 1; i <= 2; ++i)
{
ASSERT_NE(raise(SIGINT), -1);
ASSERT_EQ(counters->callbackEnterInvocations, i);
ASSERT_EQ(counters->callbackExitInvocations, i);
}
}

TEST_F(SignalHandlerTest, ShouldFallBackToDefaultHandlerWhenCallbackAborts)
{
installSignalHandler(SIGABRT, signalHandlerThatDoesNotKillTheProcess);
Expand Down

0 comments on commit 69f975c

Please sign in to comment.