Skip to content

Commit

Permalink
do not call cleanup and exit on fatal conditions during startup (netd…
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsaou authored Feb 26, 2025
1 parent b71bc97 commit 147d100
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/daemon/daemon-shutdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/daemon/daemon-systemd-watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions src/daemon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions src/libnetdata/libnetdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/libnetdata/log/nd_log-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
13 changes: 11 additions & 2 deletions src/libnetdata/log/nd_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions src/libnetdata/log/nd_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions src/libnetdata/required_dummies.h
Original file line number Diff line number Diff line change
Expand Up @@ -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){}
Expand Down

0 comments on commit 147d100

Please sign in to comment.