I am trying to handle SIGSEGV signal from my code. I have written the following code in C under Fedora 15. My problem is that the signal handler is not getting called for the second segmentation fault. Can anybody kindly point out what i am doing wrong.
typedef struct _ST_DEMO
{
int m_iUnused;
_ST_DEMO()
{
m_iUnused = 0;
}
} ST_DEMO;
jmp_buf ex_buf__;
static void sig_hdl (int sig, siginfo_t *siginfo, void *context)
{
cout<<"Inside Signal Handler."<<endl;
longjmp(ex_buf__, 1);
}
int main (int argc, char *argv[])
{
ST_DEMO* pStDemo = 0;
struct sigaction act;
memset (&act, '\0', sizeof(act));
/* Use the sa_sigaction field because the handles has two additional parameters */
act.sa_sigaction = &sig_hdl;
/* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGSEGV, &act, NULL) < 0)
{
perror ("sigaction");
return 1;
}
if(!setjmp(ex_buf__))
{
cout<<"Before First Seg Fault."<<endl;
cout<<pStDemo->m_iUnused<<endl;
}
else
{
cout<<"After jump."<<endl;
}
cout<<"Before Second Seg Fault."<<endl;
cout<<pStDemo->m_iUnused<<endl;
while(1)
{
sleep(1);
}
return 0;
}