-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path21_1.c
72 lines (58 loc) · 1.3 KB
/
21_1.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* A easy way of implementing abort() function.
*/
#include <signal.h>
#include <assert.h>
#include <tlpi_hdr.h>
void mabort(void);
#define NORMAL
// #define PENDING
// #define IGNORE
// #define CAPTURE
void sigabrt_handler(int sig)
{
// do nothing
}
int main()
{
#ifdef NORMAL
mabort();
#endif
#ifdef PENDING
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGABRT);
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
errExit("sigprocmask");
mabort();
#endif
#ifdef IGNORE
signal(SIGABRT, SIG_IGN
mabort();
#endif
#ifdef CAPTURE
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = sigabrt_handler;
if (sigaction(SIGABRT, &act, NULL) == -1)
errExit("sigaction");
mabort();
#endif
assert(1 == 2);
return 0;
}
void mabort(void)
{
/* Unblock the sigabrt signal */
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGABRT);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
raise(SIGABRT); // if success, process will terminate here and generate a core-dump file
/* SIG_IGN or the signal was caught */
signal(SIGABRT, SIG_DFL);
// though signal() is not portable for setting hander functions,
// it's portable for setting SIG_DEF and SIG_IGN
raise(SIGABRT);
}