move gcc-specific signal handling out of main.c

This commit is contained in:
Enno Rehling 2020-09-13 20:34:45 +02:00
parent 10cafcbdec
commit 7eb26db2ec
4 changed files with 48 additions and 37 deletions

View file

@ -167,6 +167,7 @@ set(SERVER_SRC
bindings.c
console.c
helpers.c
signals.c
main.c
)
@ -178,10 +179,12 @@ set (SERVER_SRC ${SERVER_SRC}
)
endif(CURSES_FOUND)
#find_program(IWYU_PATH NAMES include-what-you-use iwyu)
#if(NOT IWYU_PATH)
# message(STATUS "Could not find the program include-what-you-use")
#endif()
find_program(IWYU_PATH NAMES include-what-you-use iwyu)
if(IWYU_PATH)
# set(C_INCLUDE_WHAT_YOU_USE "${IWYU_PATH} -Xiwyu --no_fwd_decls")
else(IWYU_PATH)
message(STATUS "Could not find the program include-what-you-use")
endif()
add_library(version STATIC ${VERSION_SRC})
add_library(parser ${PARSER_SRC})

View file

@ -17,6 +17,7 @@
#include "gmtool.h"
#endif
#include "signals.h"
#include "bindings.h"
#include <iniparser.h>
@ -247,39 +248,6 @@ static int parse_args(int argc, char **argv)
return 0;
}
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#include <signal.h>
static void *btrace[50];
static void report_segfault(int signo, siginfo_t * sinf, void *arg)
{
size_t size;
int fd = fileno(stderr);
fflush(stdout);
fputs("\n\nProgram received SIGSEGV, backtrace follows.\n", stderr);
size = backtrace(btrace, 50);
backtrace_symbols_fd(btrace, size, fd);
abort();
}
static int setup_signal_handler(void)
{
struct sigaction act;
act.sa_flags = SA_RESETHAND | SA_SIGINFO;
act.sa_sigaction = report_segfault;
sigfillset(&act.sa_mask);
return sigaction(SIGSEGV, &act, NULL);
}
#else
static int setup_signal_handler(void)
{
return 0;
}
#endif
void locale_init(void)
{
setlocale(LC_CTYPE, "");

36
src/signals.c Normal file
View file

@ -0,0 +1,36 @@
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <bits/types/siginfo_t.h>
static void *btrace[50];
static void report_segfault(int signo, siginfo_t * sinf, void *arg)
{
size_t size;
int fd = fileno(stderr);
fflush(stdout);
fputs("\n\nProgram received SIGSEGV, backtrace follows.\n", stderr);
size = backtrace(btrace, 50);
backtrace_symbols_fd(btrace, size, fd);
abort();
}
int setup_signal_handler(void)
{
struct sigaction act;
act.sa_flags = SA_RESETHAND | SA_SIGINFO;
act.sa_sigaction = report_segfault;
sigfillset(&act.sa_mask);
return sigaction(SIGSEGV, &act, 0);
}
#else
int setup_signal_handler(void)
{
return 0;
}
#endif

4
src/signals.h Normal file
View file

@ -0,0 +1,4 @@
#pragma once
int setup_signal_handler(void);