From 6c85999917b78264ff8e2c2a1ad76c991b3679b0 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Wed, 31 Jul 2013 14:51:24 +0930 Subject: [PATCH] Log crashes and unexpected signals as FATAL --- commandline.c | 2 -- log.c | 12 ++++++------ log.h | 4 ++-- performance_timing.c | 4 ++-- serval.h | 2 +- server.c | 25 ++++++++++++++----------- 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/commandline.c b/commandline.c index 70e272f2..e4862e81 100644 --- a/commandline.c +++ b/commandline.c @@ -1120,8 +1120,6 @@ int app_config_schema(const struct cli_parsed *parsed, struct cli_context *conte { if (config.debug.verbose) DEBUG_cli_parsed(parsed); - if (create_serval_instance_dir() == -1) - return -1; struct cf_om_node *root = NULL; if (cf_sch_config_main(&root) == -1) { cf_om_free_node(&root); diff --git a/log.c b/log.c index 9bbc5bfd..3e5a8f2c 100644 --- a/log.c +++ b/log.c @@ -755,7 +755,7 @@ ssize_t get_self_executable_path(char *buf, size_t len) #endif } -int log_backtrace(struct __sourceloc whence) +int log_backtrace(int level, struct __sourceloc whence) { #ifndef NO_BACKTRACE _log_iterator it; @@ -812,7 +812,7 @@ int log_backtrace(struct __sourceloc whence) } // parent close(stdout_fds[1]); - _log_iterator_printf_nl(&it, LOG_LEVEL_DEBUG, whence, "GDB BACKTRACE"); + _log_iterator_printf_nl(&it, level, whence, "GDB BACKTRACE"); char buf[1024]; char *const bufe = buf + sizeof buf; char *linep = buf; @@ -824,14 +824,14 @@ int log_backtrace(struct __sourceloc whence) for (; p < readp; ++p) if (*p == '\n' || *p == '\0') { *p = '\0'; - _log_iterator_printf_nl(&it, LOG_LEVEL_DEBUG, __NOWHERE__, "%s", linep); + _log_iterator_printf_nl(&it, level, __NOWHERE__, "%s", linep); linep = p + 1; } if (readp >= bufe && linep == buf) { // Line does not fit into buffer. char t = bufe[-1]; bufe[-1] = '\0'; - _log_iterator_printf_nl(&it, LOG_LEVEL_DEBUG, __NOWHERE__, "%s", buf); + _log_iterator_printf_nl(&it, level, __NOWHERE__, "%s", buf); buf[0] = t; readp = buf + 1; } else if (readp + 120 >= bufe && linep != buf) { @@ -847,7 +847,7 @@ int log_backtrace(struct __sourceloc whence) WHY_perror("read"); if (readp > linep) { *readp = '\0'; - _log_iterator_printf_nl(&it, LOG_LEVEL_DEBUG, __NOWHERE__, "%s", linep); + _log_iterator_printf_nl(&it, level, __NOWHERE__, "%s", linep); } close(stdout_fds[0]); int status = 0; @@ -855,7 +855,7 @@ int log_backtrace(struct __sourceloc whence) WHY_perror("waitpid"); strbuf b = strbuf_local(buf, sizeof buf); strbuf_append_exit_status(b, status); - _log_iterator_printf_nl(&it, LOG_LEVEL_DEBUG, __NOWHERE__, "gdb %s", buf); + _log_iterator_printf_nl(&it, level, __NOWHERE__, "gdb %s", buf); unlink(tempfile); #endif return 0; diff --git a/log.h b/log.h index 15428dad..b6790ee3 100644 --- a/log.h +++ b/log.h @@ -106,7 +106,7 @@ void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list) void logConfigChanged(); int logDump(int level, struct __sourceloc whence, char *name, const unsigned char *addr, size_t len); ssize_t get_self_executable_path(char *buf, size_t len); -int log_backtrace(struct __sourceloc whence); +int log_backtrace(int level, struct __sourceloc whence); struct strbuf; #define __HERE__ ((struct __sourceloc){ .file = __FILE__, .line = __LINE__, .function = __FUNCTION__ }) @@ -158,6 +158,6 @@ struct strbuf; #define dump(X,A,N) logDump(LOG_LEVEL_DEBUG, __WHENCE__, (X), (const unsigned char *)(A), (size_t)(N)) -#define BACKTRACE log_backtrace(__WHENCE__) +#define BACKTRACE log_backtrace(LOG_LEVEL_FATAL, __WHENCE__) #endif // __SERVALD_LOG_H diff --git a/performance_timing.c b/performance_timing.c index 93085e99..86c4c6f1 100644 --- a/performance_timing.c +++ b/performance_timing.c @@ -198,12 +198,12 @@ void fd_periodicstats(struct sched_ent *alarm) schedule(alarm); } -void dump_stack() +void dump_stack(int log_level) { struct call_stats *call = current_call; while(call){ if (call->totals) - INFOF("%s",call->totals->name); + LOGF(log_level, "%s",call->totals->name); call=call->prev; } } diff --git a/serval.h b/serval.h index 7b7116e8..49b30a92 100644 --- a/serval.h +++ b/serval.h @@ -782,7 +782,7 @@ int fd_showstats(); int fd_checkalarms(); int fd_func_enter(struct __sourceloc __whence, struct call_stats *this_call); int fd_func_exit(struct __sourceloc __whence, struct call_stats *this_call); -void dump_stack(); +void dump_stack(int log_level); #define IN() static struct profile_total _aggregate_stats={NULL,0,__FUNCTION__,0,0,0}; \ struct call_stats _this_call={.totals=&_aggregate_stats}; \ diff --git a/server.c b/server.c index 011c61b3..31e52110 100644 --- a/server.c +++ b/server.c @@ -357,12 +357,6 @@ static void signame(char *buf, size_t len, int signal) void signal_handler(int signal) { - char buf[80]; - signame(buf, sizeof(buf), signal); - INFOF("Caught %s", buf); - WHYF("The following clue may help: %s",crash_handler_clue); - dump_stack(); - switch (signal) { case SIGHUP: case SIGINT: @@ -370,10 +364,18 @@ void signal_handler(int signal) rather than here, so we first try to tell the mainline code to do so. If, however, this is not the first time we have been asked to shut down, then we will do it here. */ server_shutdown_check(NULL); - WHY("Asking Serval process to shutdown cleanly"); + INFO("Attempting clean shutdown"); servalShutdown = 1; return; } + + char buf[80]; + signame(buf, sizeof(buf), signal); + + LOGF(LOG_LEVEL_FATAL, "Caught signal %s", buf); + LOGF(LOG_LEVEL_FATAL, "The following clue may help: %s",crash_handler_clue); + dump_stack(LOG_LEVEL_FATAL); + serverCleanUp(); exit(0); } @@ -383,9 +385,10 @@ void crash_handler(int signal) { char buf[80]; signame(buf, sizeof(buf), signal); - WHYF("Caught %s", buf); - WHYF("The following clue may help: %s",crash_handler_clue); - dump_stack(); + LOGF(LOG_LEVEL_FATAL, "Caught signal %s", buf); + LOGF(LOG_LEVEL_FATAL, "The following clue may help: %s",crash_handler_clue); + dump_stack(LOG_LEVEL_FATAL); + BACKTRACE; if (config.server.respawn_on_crash) { int i; @@ -411,7 +414,7 @@ void crash_handler(int signal) // If that didn't work, then die normally. INFOF("exit(%d)", -signal); exit(-signal); -} +} int getKeyring(char *backing_file) {