From 97c4f35cd541baa2f985ff702a5b9f0b8b524a53 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 10 Apr 2010 21:40:40 +0200 Subject: [PATCH] main: install crash handler for creating core dumps --- main.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/main.c b/main.c index 082ac4bb..4aa63cba 100644 --- a/main.c +++ b/main.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "manager.h" #include "log.h" @@ -44,6 +45,66 @@ static enum { static char *default_unit = NULL; static ManagerRunningAs running_as = _MANAGER_RUNNING_AS_INVALID; +static bool dump_core = true; + +_noreturn static void freeze(void) { + for (;;) + pause(); +} + +_noreturn static void crash(int sig) { + + if (!dump_core) + log_error("Caught <%s>, not dumping core.", strsignal(sig)); + else { + pid_t pid; + + log_warning("Caugh <%s>, dumping core.", strsignal(sig)); + + if ((pid = fork()) < 0) + log_error("Caught <%s>, cannot dump core: %s", strsignal(sig), strerror(errno)); + + else if (pid == 0) { + struct sigaction sa; + struct rlimit rl; + + /* Enable default signal handler for core dump */ + zero(sa); + sa.sa_handler = SIG_DFL; + assert_se(sigaction(sig, &sa, NULL) == 0); + + /* Don't limit the core dump size */ + zero(rl); + rl.rlim_cur = RLIM_INFINITY; + rl.rlim_max = RLIM_INFINITY; + setrlimit(RLIMIT_CORE, &rl); + + /* Just to be sure... */ + assert_se(chdir("/") == 0); + + /* Raise the signal again */ + raise(sig); + + assert_not_reached("We shouldn't be here..."); + _exit(1); + } + } + + log_error("Freezing execution."); + freeze(); +} + +static void install_crash_handler(void) { + struct sigaction sa; + + zero(sa); + + sa.sa_handler = crash; + sa.sa_flags = SA_NODEFER; + + assert_se(sigaction(SIGSEGV, &sa, NULL) == 0); + assert_se(sigaction(SIGABRT, &sa, NULL) == 0); +} static int set_default_unit(const char *u) { char *c; @@ -307,6 +368,9 @@ int main(int argc, char *argv[]) { log_open_syslog(); log_open_kmsg(); + install_crash_handler(); + assert(!"esel"); + log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as)); if (running_as == MANAGER_INIT) -- 2.39.5