Why does my signal handler (which throws an exception) run more than once?

I am trying to set up an exception handler using sigaction. It works great for the first exception. But the sigaction handler is not called after the first exception, and the program suddenly terminates when the second signal occurs.

#include <iostream> #include <signal.h> #include <exception> #include <string.h> typedef void (*SigactionHandlerPointer)(int iSignal, siginfo_t * psSiginfo, void * psContext); using namespace std; void SigactionHookHandler( int iSignal, siginfo_t * psSiginfo, void * psContext ) { cout << "Signal Handler Exception Caught: std::exception -- signal : " << iSignal << " from SigactionHookHandler()" << endl; throw std::exception(); } class A { public: A() {} virtual ~A() {} virtual void fnct1(); virtual void fnct2() { fnct3(); } virtual void fnct3() { fnct4(); } virtual void fnct4(); }; void A::fnct1() { try { fnct2(); } catch( std::exception &ex ) { cerr << "Signal Handler Exception Caught" << endl; } catch (...) { cerr << "Unknow Exception Caught: " << endl; } } void A::fnct4() { *(int *) 0 = 0; // Access violation } int main() { struct sigaction oNewSigAction; struct sigaction oOldSigAction; memset(&oNewSigAction, 0, sizeof oNewSigAction); oNewSigAction.sa_sigaction = SigactionHookHandler; oNewSigAction.sa_flags = SA_SIGINFO; int iResult = sigaction( SIGSEGV, &oNewSigAction, &oOldSigAction ); cout << "sigaction installed handler with status " << iResult << endl; A * pA = new A(); cout << "Next message expected is : <<Signal Handler Exception Caught: std::exception>> to pass this test" << endl; pA->fnct1(); // Second exception will never be call the sigaction handler. cout << "Next message expected is : <<Signal Handler Exception Caught: std::exception>> to pass this test" << endl; pA->fnct1(); return 0; } 
+2
source share
2 answers

Signals and exceptions are not related to each other. What you use (excluding exceptions from async signal handlers) is only transferred between several compilers that support it, such as GCC and Intel C / C ++ with -fnon-call-exceptions .

However, what you forgot to do is to unblock the signal: when the signal handler is executed, the delivery of the same signal is blocked and it will not be unlocked when the signal handler leaves the exception. Change the signal handler as follows:

 void SigactionHookHandler( int iSignal, siginfo_t * psSiginfo, void * psContext { cout << "Signal Handler Exception Caught: std::exception -- signal : " << iSignal << " from SigactionHookHandler()" << endl; sigset_t x; sigemptyset (&x); sigaddset(&x, SIGSEGV); sigprocmask(SIG_UNBLOCK, &x, NULL); throw std::exception(); } 
+8
source

The C ++ standard does not say anything about signals or how they interact with exceptions. What you are trying to do will be completely specific to your OS platform and, possibly, to the specific compiler with which you will compile your code.

+2
source

Source: https://habr.com/ru/post/1434041/


All Articles