do not call cleanup and exit on fatal conditions during startup (#19715)
Costa Tsaousis committed
Feb 26, 2025 at 12:20 UTC
147d100d1d34b5a0037a12ac2e9004a4e16dc32a
8 files changed
+34
-18
src/daemon/daemon-shutdown.h
+6
@@ -10,4 +10,10 @@ void cancel_main_threads(void);
10
void abort_on_fatal_disable(void);
11
void abort_on_fatal_enable(void);
12
13
+#ifdef OS_WINDOWS
14
+void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data);
15
+#else
16
+void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data) NORETURN;
17
+#endif
18
+
19
#endif //NETDATA_DAEMON_SHUTDOWN_H
src/daemon/daemon-systemd-watcher.c
+1
@@ -3,6 +3,7 @@
3
#include "libnetdata/libnetdata.h"
4
#include "daemon-systemd-watcher.h"
5
#include "daemon-service.h"
6
+#include "daemon-shutdown.h"
7
8
#ifdef ENABLE_SYSTEMD_DBUS
9
src/daemon/main.c
+12
@@ -229,6 +229,16 @@ int unittest_prepare_rrd(const char **user) {
229
return 0;
230
}
231
232
+static void fatal_cleanup_and_exit_cb(void) {
233
+ netdata_cleanup_and_exit(EXIT_REASON_FATAL, "fatal error", "exiting", NULL);
234
+ exit(1);
235
+}
236
+
237
+static void fatal_status_file_save(void) {
238
+ daemon_status_file_update_status(DAEMON_STATUS_NONE);
239
+ exit(1);
240
+}
241
+
242
int netdata_main(int argc, char **argv) {
243
libjudy_malloc_init();
244
string_init();
@@ -753,6 +763,7 @@ int netdata_main(int argc, char **argv) {
763
// initialize the log files
764
nd_log_initialize();
765
nd_log_register_event_cb(daemon_status_file_register_fatal);
766
+ nd_log_register_fatal_cb(fatal_status_file_save);
767
768
netdata_conf_section_global(); // get hostname, host prefix, profile, etc
769
registry_init(); // for machine_guid, must be after netdata_conf_section_global()
@@ -1057,6 +1068,7 @@ int netdata_main(int argc, char **argv) {
1068
1069
webrtc_initialize();
1070
1071
+ nd_log_register_fatal_cb(fatal_cleanup_and_exit_cb);
1072
daemon_status_file_startup_step(NULL);
1073
daemon_status_file_update_status(DAEMON_STATUS_RUNNING);
1074
return 10;
src/libnetdata/libnetdata.h
-6
@@ -56,12 +56,6 @@ char *find_and_replace(const char *src, const char *find, const char *replace, c
56
bool run_command_and_copy_output_to_stdout(const char *command, int max_line_length);
57
struct web_buffer *run_command_and_get_output_to_buffer(const char *command, int max_line_length);
58
59
-#ifdef OS_WINDOWS
60
-void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data);
61
-#else
62
-void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data) NORETURN;
63
-#endif
64
-
59
extern const char *netdata_configured_host_prefix;
60
61
// safe includes before O/S specific functions
src/libnetdata/log/nd_log-internals.h
+1
@@ -126,6 +126,7 @@ struct nd_log {
126
127
ND_LOG_SOURCES overwrite_process_source;
128
log_event_t log_event_cb;
129
+ fatal_event_t fatal_event_cb;
130
131
struct nd_log_source sources[_NDLS_MAX];
132
src/libnetdata/log/nd_log.c
+11
-2
@@ -135,6 +135,12 @@ void nd_log_register_event_cb(log_event_t cb) {
135
nd_log.log_event_cb = cb;
136
}
137
138
+// --------------------------------------------------------------------------------------------------------------------
139
+
140
+void nd_log_register_fatal_cb(fatal_event_t cb) {
141
+ nd_log.fatal_event_cb = cb;
142
+}
143
+
144
// --------------------------------------------------------------------------------------------------------------------
145
// high level logger
146
@@ -525,8 +531,11 @@ void netdata_logger_fatal(const char *file, const char *function, const unsigned
531
#endif
532
533
#ifdef NETDATA_INTERNAL_CHECKS
528
- abort();
534
+ // abort();
535
#endif
536
531
- netdata_cleanup_and_exit(EXIT_REASON_FATAL, "FATAL", action_result, action_data);
537
+ if(nd_log.fatal_event_cb)
538
+ nd_log.fatal_event_cb();
539
+
540
+ exit(1);
541
}
src/libnetdata/log/nd_log.h
+3
@@ -36,6 +36,9 @@ ND_UUID nd_log_get_invocation_id(void);
36
typedef void (*log_event_t)(const char *filename, const char *function, const char *message, const char *errno_str, const char *stack_trace, long line);
37
void nd_log_register_event_cb(log_event_t cb);
38
39
+typedef void (*fatal_event_t)(void);
40
+void nd_log_register_fatal_cb(fatal_event_t cb);
41
+
42
int nd_log_health_fd(void);
43
int nd_log_collectors_fd(void);
44
typedef bool (*log_formatter_callback_t)(BUFFER *wb, void *data);
src/libnetdata/required_dummies.h
-10
@@ -3,16 +3,6 @@
3
#ifndef NETDATA_LIB_DUMMIES_H
4
#define NETDATA_LIB_DUMMIES_H 1
5
6
-// callback required by fatal()
7
-void netdata_cleanup_and_exit(EXIT_REASON reason, const char *action, const char *action_result, const char *action_data)
8
-{
9
- (void)action;
10
- (void)action_result;
11
- (void)action_data;
12
-
13
- exit(reason == EXIT_REASON_FATAL ? 1 : 0);
14
-}
15
-
6
void rrdset_thread_rda_free(void){}
7
void sender_thread_buffer_free(void){}
8
void query_target_free(void){}