I'm just playing with signal in Mac OS X.
Why does the following code not produce the default behavior of SIGSEGV after my signal handler has finished? Under Linux, the code works fine.
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
void err_crash();
void my_signal_handler (int signal)
{
fprintf(stderr, "signal == %i\n", signal);
fflush(stderr);
err_crash();
}
void err_crash()
{
static int count= 0;
if (count)
signal(SIGSEGV, SIG_DFL); /* break recursion loop if we recursed */
count++;
// one of the two should really crash ;)
((void)(*((int volatile *)NULL)));
*((int *)NULL) = 42;
abort(); /* in case we're not crashed yet... */
}
int main ()
{
signal(SIGSEGV, my_signal_handler);
err_crash();
return 0;
}
EDIT: The output I get is the following:
bonecrusher:devel sw$ g++ signal_problems.cpp -o foo
bonecrusher:devel sw$ ./foo
signal == 11
^C
bonecrusher:devel sw$
The problem is that I want that the program terminates after the output of signal == 11
, but it rans forever and I have to interrupt it.
According to POSIX:
Because SIGSEGV is blocked while in the SIGSEGV signal handler, the result is undefined in this case and any behavior is valid. If you don't want it to be blocked you could install the signal handler using
sigaction()
with theSA_NODEFER
flag, or usesigprocmask()
to unblock the signal within the signal handler.Run it in a debugger and single-step the instructions that you expect to crash.
There is no guarantee that writing to invalid addresses must create a segmentation fault. Perhaps Mac OS X maps those addresses for you, and you're overwriting something benign.
This actually caused me brain freeze for a few minutes, and the reason why one should never use
signal()
in this day and age only grew stronger in me.First of all, from the man pages for
signal()
and further down :
In the original Unix systems, when a handler was installed, the disposition was reset to SIG_DFL, did not block incoming signals of the same type, and then it ran the handler function. System V provided this, and the linux kernel does the same.
This means that, once the code is run on a linux system, once second exception is called, it will exit directly.
Now to the fun part. BSD tried to improve this behaviour. From the man pages again:
And since mac osx is partly based on BSD, once the code is run on a mac osx, once second exception is called, it will be pending and wait for the handler of the first exception to exit. But since you will never exit, you have a deadlock.
Thats why one should use
sigaction()
instead and neversignal()
.Now to some tips:
Handlers should be short, and return quickly. If you are performing calculations and calling other functions you are probably doing something wrong. Signals are not a substitute for an event driven framework.
Calling functions that are not async-safe is bad. Consider what would happen if an exception happened during a call to
fprintf
, and inside the handlerfprintf
was called again. Both the signal handlers and the programs data could be corrupted since they operate on the stream itself.Some more reading : "Do" and "Don't" inside A Signal Handler