-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabort.c
More file actions
executable file
·48 lines (39 loc) · 1.26 KB
/
abort.c
File metadata and controls
executable file
·48 lines (39 loc) · 1.26 KB
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
/*
An implementation of abort()
The abort function must guarentee that the program terminates and produces a core dump file, unless a
custom SIGABRT handler has been set which does not return (e.g. longjmps...)
*/
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
void my_abort() {
struct sigaction sigact;
// Raise a SIGABRT
raise(SIGABRT);
// Program hasn't terminated. Set disposition of SIGABT to default
memset(&sigact, 0, sizeof (struct sigaction));
sigact.sa_handler = SIG_DFL;
sigaction(SIGABRT, &sigact, NULL);
// Raise SIGABRT again!
raise(SIGABRT);
}
// Test the above abort!
static void handler(int sig) {
// Should be safe, as both of these functions are async-signal-safe as of SUSv3.
// As opposed to using STDIO functions, which due to stdio buffering are not!
write(STDOUT_FILENO, "handler called!\n", 16);
fsync(STDOUT_FILENO);
}
int main(int argc, char **argv) {
struct sigaction sigact;
// Set disposition of SIGABRT to something which does not terminate the program and returns
memset(&sigact, 0, sizeof (struct sigaction));
sigact.sa_handler = handler;
sigaction(SIGABRT, &sigact, NULL);
puts("Calling my_abort!");
my_abort();
puts("This shouldn't be reached!");
exit(EXIT_SUCCESS);
}