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 b988dd0a7d
commit 7712dcc0f0
4 changed files with 45 additions and 34 deletions

View File

@ -167,6 +167,7 @@ set(SERVER_SRC
bindings.c bindings.c
console.c console.c
helpers.c helpers.c
signals.c
main.c main.c
) )
@ -179,7 +180,9 @@ set (SERVER_SRC ${SERVER_SRC}
endif(CURSES_FOUND) endif(CURSES_FOUND)
find_program(IWYU_PATH NAMES include-what-you-use iwyu) find_program(IWYU_PATH NAMES include-what-you-use iwyu)
if(NOT IWYU_PATH) 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") message(STATUS "Could not find the program include-what-you-use")
endif() endif()

View File

@ -17,6 +17,7 @@
#include "gmtool.h" #include "gmtool.h"
#endif #endif
#include "signals.h"
#include "bindings.h" #include "bindings.h"
#include <iniparser.h> #include <iniparser.h>
@ -247,39 +248,6 @@ static int parse_args(int argc, char **argv)
return 0; 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) void locale_init(void)
{ {
setlocale(LC_CTYPE, ""); 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);