diff --git a/src/daemon/daemon-shutdown.h b/src/daemon/daemon-shutdown.h index 97e220ab1e73e2..c5de7ac1ad5580 100644 --- a/src/daemon/daemon-shutdown.h +++ b/src/daemon/daemon-shutdown.h @@ -10,4 +10,10 @@ void cancel_main_threads(void); void abort_on_fatal_disable(void); void abort_on_fatal_enable(void); +#ifdef OS_WINDOWS +void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data); +#else +void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data) NORETURN; +#endif + #endif //NETDATA_DAEMON_SHUTDOWN_H diff --git a/src/daemon/daemon-systemd-watcher.c b/src/daemon/daemon-systemd-watcher.c index d71e81ba7d85cc..386b7ffaac1e81 100644 --- a/src/daemon/daemon-systemd-watcher.c +++ b/src/daemon/daemon-systemd-watcher.c @@ -3,6 +3,7 @@ #include "libnetdata/libnetdata.h" #include "daemon-systemd-watcher.h" #include "daemon-service.h" +#include "daemon-shutdown.h" #ifdef ENABLE_SYSTEMD_DBUS diff --git a/src/daemon/main.c b/src/daemon/main.c index b706e61dfb9169..2e3bb9d0a392d0 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -229,6 +229,16 @@ int unittest_prepare_rrd(const char **user) { return 0; } +static void fatal_cleanup_and_exit_cb(void) { + netdata_cleanup_and_exit(EXIT_REASON_FATAL, "fatal error", "exiting", NULL); + exit(1); +} + +static void fatal_status_file_save(void) { + daemon_status_file_update_status(DAEMON_STATUS_NONE); + exit(1); +} + int netdata_main(int argc, char **argv) { libjudy_malloc_init(); string_init(); @@ -753,6 +763,7 @@ int netdata_main(int argc, char **argv) { // initialize the log files nd_log_initialize(); nd_log_register_event_cb(daemon_status_file_register_fatal); + nd_log_register_fatal_cb(fatal_status_file_save); netdata_conf_section_global(); // get hostname, host prefix, profile, etc registry_init(); // for machine_guid, must be after netdata_conf_section_global() @@ -1057,6 +1068,7 @@ int netdata_main(int argc, char **argv) { webrtc_initialize(); + nd_log_register_fatal_cb(fatal_cleanup_and_exit_cb); daemon_status_file_startup_step(NULL); daemon_status_file_update_status(DAEMON_STATUS_RUNNING); return 10; diff --git a/src/libnetdata/libnetdata.h b/src/libnetdata/libnetdata.h index dbc3ec55944fac..8a8c9e2323197c 100644 --- a/src/libnetdata/libnetdata.h +++ b/src/libnetdata/libnetdata.h @@ -56,12 +56,6 @@ char *find_and_replace(const char *src, const char *find, const char *replace, c bool run_command_and_copy_output_to_stdout(const char *command, int max_line_length); struct web_buffer *run_command_and_get_output_to_buffer(const char *command, int max_line_length); -#ifdef OS_WINDOWS -void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data); -#else -void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data) NORETURN; -#endif - extern const char *netdata_configured_host_prefix; // safe includes before O/S specific functions diff --git a/src/libnetdata/log/nd_log-internals.h b/src/libnetdata/log/nd_log-internals.h index ac4da828b32eb9..072c1973a8b07c 100644 --- a/src/libnetdata/log/nd_log-internals.h +++ b/src/libnetdata/log/nd_log-internals.h @@ -126,6 +126,7 @@ struct nd_log { ND_LOG_SOURCES overwrite_process_source; log_event_t log_event_cb; + fatal_event_t fatal_event_cb; struct nd_log_source sources[_NDLS_MAX]; diff --git a/src/libnetdata/log/nd_log.c b/src/libnetdata/log/nd_log.c index 856c0d0e452b6c..b948ea2b2bad60 100644 --- a/src/libnetdata/log/nd_log.c +++ b/src/libnetdata/log/nd_log.c @@ -135,6 +135,12 @@ void nd_log_register_event_cb(log_event_t cb) { nd_log.log_event_cb = cb; } +// -------------------------------------------------------------------------------------------------------------------- + +void nd_log_register_fatal_cb(fatal_event_t cb) { + nd_log.fatal_event_cb = cb; +} + // -------------------------------------------------------------------------------------------------------------------- // high level logger @@ -525,8 +531,11 @@ void netdata_logger_fatal(const char *file, const char *function, const unsigned #endif #ifdef NETDATA_INTERNAL_CHECKS - abort(); + // abort(); #endif - netdata_cleanup_and_exit(EXIT_REASON_FATAL, "FATAL", action_result, action_data); + if(nd_log.fatal_event_cb) + nd_log.fatal_event_cb(); + + exit(1); } diff --git a/src/libnetdata/log/nd_log.h b/src/libnetdata/log/nd_log.h index bf51eed11623b5..dfd928aade5047 100644 --- a/src/libnetdata/log/nd_log.h +++ b/src/libnetdata/log/nd_log.h @@ -36,6 +36,9 @@ ND_UUID nd_log_get_invocation_id(void); typedef void (*log_event_t)(const char *filename, const char *function, const char *message, const char *errno_str, const char *stack_trace, long line); void nd_log_register_event_cb(log_event_t cb); +typedef void (*fatal_event_t)(void); +void nd_log_register_fatal_cb(fatal_event_t cb); + int nd_log_health_fd(void); int nd_log_collectors_fd(void); typedef bool (*log_formatter_callback_t)(BUFFER *wb, void *data); diff --git a/src/libnetdata/required_dummies.h b/src/libnetdata/required_dummies.h index 14d7969d4e97a6..2819fb8f2f65fb 100644 --- a/src/libnetdata/required_dummies.h +++ b/src/libnetdata/required_dummies.h @@ -3,16 +3,6 @@ #ifndef NETDATA_LIB_DUMMIES_H #define NETDATA_LIB_DUMMIES_H 1 -// callback required by fatal() -void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data) -{ - (void)action; - (void)action_result; - (void)action_data; - - exit(reason == EXIT_REASON_FATAL ? 1 : 0); -} - void rrdset_thread_rda_free(void){} void sender_thread_buffer_free(void){} void query_target_free(void){}