-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcatch-SIGSEGV.c
56 lines (47 loc) · 1.43 KB
/
catch-SIGSEGV.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* The UNIX spec does not define what happens if synchronnous signals (SIGSEGV,
* SIGILL, SIGBUS, and SIGFPE) are masked or ignored. Most of the systems just
* kill the process no matter how the signal mask is set. However, you should
* always be able to catch such a signal in order to print a user friendly
* message, for example. Note that after that, you better exit, never move on,
* since what happens after returning from the handler is also undefined by the
* standard.
*
* Remember that to safely exit from signal handler, _exit() should be used.
*
* Note that the approach of handling SIGSEGV might result in no core file
* to be generated so consider calling abort() in the handler (although this
* may not be safe as it could flush the I/O streams).
*
* (c) jp@devnull.cz, vlada@devnull.cz
*/
#define _XOPEN_SOURCE 700
#include <signal.h>
#include <unistd.h>
#define MESSAGE "Signal caught !\n"
void
handle_segfault(int sig)
{
write(1, MESSAGE, sizeof (MESSAGE) - 1);
/*
* Consider calling backtrace(3) after the signal is handled
* to get stack trace information.
*/
}
int
main(void)
{
struct sigaction act = { 0 };
act.sa_handler = handle_segfault;
sigaction(SIGSEGV, &act, NULL);
/*
* Intentionally in the loop. Let's see what happens if we catch the
* signal and not exit.
*/
while (1) {
int *p = NULL;
sleep(1);
/* The following line definitely generates the SEGV signal. */
*p = 1;
}
}