1

If I have set a signal handler for SIGSEGV, whereas a segmentation fault is generated like:

int *a = NULL;
*a = 1;

The handler will be invoked, but this signal handler will be invoked only once. So, I guess Linux kernel will reset the signal handler to SIG_DFL, but when? I want to know the details, so I checked the Linux kernel source code, but couldn't find the clue yet. Please show me the code if you know the details.

red0ct
  • 4,840
  • 3
  • 17
  • 44
cong
  • 1,105
  • 1
  • 12
  • 29

2 Answers2

4

It depends on how you register the signal handler. With sigaction and without the SA_RESETHAND flag, there will be no resetting to SIG_DFL (although returning from a signal handler run in response to a SIGSEGV delivered due to a segmentation fault is technically UB). With SA_RESETHAND it will get reset, and if you register the handler with signal, then whether the handler will be reset or not is unspecified (so don't use signal()).

Example:

#include <signal.h>
#include <unistd.h>

int volatile*a;
void h(int Sig) { write(1,"h\n", 2); }
int main()
{
    //sigaction(SIGSEGV,&(struct sigaction){.sa_handler=h}, 0); //won't reset the handler, will likely loop
    sigaction(SIGSEGV,&(struct sigaction){.sa_handler=h,.sa_flags=SA_RESETHAND}, 0); //will reset the handler
    //signal(SIGSEGV,h); //may or may not reset the handler
    *a=1;
    return 0;
}
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
2

As rightly answered by @PSkocik, in normal scenarios kernel do not take trouble to change the signal action to default unless user have installed the signal handler with SA_RESETHAND flag in sigaction,.

However, there is a scenario where kernel do changes the signal action of SIGSEGV to default. Suppose, user has installed the signal handler and later exhaust whole memory before generating the signal. In this case, kernel will fail to create frame to execute signal handler as no memory is left. Upon sensing this situation kernel does the following:

  1. Check if user has installed handler for SIGSEGV. If yes, changes the action to default also unblock SIGSEGV if user has blocked it.
  2. Deliver SIGSEGV signal to user to terminate the user process.
Monk
  • 756
  • 5
  • 16