I have encountered in a problem where segmentation fault is being raised within segmentation fault handler. Although I have already fixed the actual issue, but still I am confused that why below program is not going in infinite loop:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void C()
{
int *p = NULL;
*p = 3;
}
void B()
{
C();
}
void segfault_sigaction(int signal, siginfo_t *si, void *arg)
{
printf("Came in sigaction.\n");
//if(si)
//printf("Caught segfault at address %p\n", si->si_addr);
B();
}
int main(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = segfault_sigaction;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
segfault_sigaction(0, NULL, NULL);
return 0;
}
Output of the above code is as below:
Came in sigaction.
Came in sigaction.
Segmentation fault (core dumped)
In the comments of this similar post: Segmentation fault within segmentation fault handler some folks have commented that it should go in infinite loop but it's not the current behavior. Can someone explain to me what's happening ??