aboutsummaryrefslogtreecommitdiff
path: root/src/sig_handlers.c
blob: 601e454cbb8cc8bd242cf2e2ed53cf60e7e7d4e5 (plain)
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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void abnormal_termination_handler(int signo) {
	exit(signo);
}

static void __attribute__((constructor)) register_handlers(void)
{
	struct sigaction sa, old_sa;
	sa.sa_handler = abnormal_termination_handler;
	sigemptyset(&sa.sa_mask);

	sigaction(SIGABRT, NULL, &old_sa);
	if (old_sa.sa_handler == SIG_DFL) {
		if (sigaction(SIGABRT, &sa, NULL) == -1) {
			perror("sigaction");
			exit(1);
		}
	} else {
		fprintf(stderr, "SIGABRT handler already set");
	}

	sigaction(SIGSEGV, NULL, &old_sa);
	if (old_sa.sa_handler == SIG_DFL) {
		if (sigaction(SIGSEGV, &sa, NULL) == -1) {
			perror("sigaction");
			exit(1);
		}
	} else {
		fprintf(stderr, "SIGSEGV handler already set");
	}
}