From aec4dbc6ff10d2b07d73e705cdd9d5e1b5a7ba06 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 7 Feb 2021 13:44:41 +0100 Subject: [PATCH] DB: Make connection initialization more predictable and avoid double-initialization Remove calls to zmDBConnect from various places to avoid possible side-effects/double initialization. The function should be called once from the main thread of the daemon. Also split config loading into 2 steps: static and DB config loading. Load the static config before zmDBConnect is called so it has a chance to succeed. --- src/zm_config.cpp | 9 +++++---- src/zm_config.h | 3 ++- src/zm_db.h | 2 ++ src/zm_logger.cpp | 2 +- src/zmc.cpp | 4 +++- src/zms.cpp | 5 ++++- src/zmu.cpp | 6 ++++-- 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/zm_config.cpp b/src/zm_config.cpp index a1ebedaab..3b297d166 100644 --- a/src/zm_config.cpp +++ b/src/zm_config.cpp @@ -30,8 +30,7 @@ // set the relevant ENV vars because the logger gets it's setting from the // config. -void zmLoadConfig() { - +void zmLoadStaticConfig() { // Process name, value pairs from the main config file first process_configfile(ZM_CONFIG); @@ -58,9 +57,11 @@ void zmLoadConfig() { globfree(&pglob); closedir(configSubFolder); } +} - if ( !zmDbConnect() ) { - Fatal("Can't connect to db. Can't continue."); +void zmLoadDBConfig() { + if ( !zmDbConnected ) { + Fatal("Not connected to the database. Can't continue."); } config.Load(); config.Assign(); diff --git a/src/zm_config.h b/src/zm_config.h index 43d38f6f8..daea8e14c 100644 --- a/src/zm_config.h +++ b/src/zm_config.h @@ -53,7 +53,8 @@ #define ZM_SAMPLE_RATE int(1000000/ZM_MAX_FPS) // A general nyquist sample frequency for delays etc #define ZM_SUSPENDED_RATE int(1000000/4) // A slower rate for when disabled etc -extern void zmLoadConfig(); +void zmLoadStaticConfig(); +void zmLoadDBConfig(); extern void process_configfile(char const *configFile); diff --git a/src/zm_db.h b/src/zm_db.h index b51ffd5b3..85dc53872 100644 --- a/src/zm_db.h +++ b/src/zm_db.h @@ -43,6 +43,8 @@ class zmDbRow { extern MYSQL dbconn; extern RecursiveMutex db_mutex; +extern bool zmDbConnected; + bool zmDbConnect(); void zmDbClose(); diff --git a/src/zm_logger.cpp b/src/zm_logger.cpp index 8be72c0f4..7364c6f84 100644 --- a/src/zm_logger.cpp +++ b/src/zm_logger.cpp @@ -342,7 +342,7 @@ Logger::Level Logger::databaseLevel(Logger::Level databaseLevel) { databaseLevel = limit(databaseLevel); if ( mDatabaseLevel != databaseLevel ) { if ( (databaseLevel > NOLOG) && (mDatabaseLevel <= NOLOG) ) { // <= NOLOG would be NOOPT - if ( !zmDbConnect() ) { + if ( !zmDbConnected ) { databaseLevel = NOLOG; } } // end if ( databaseLevel > NOLOG && mDatabaseLevel <= NOLOG ) diff --git a/src/zmc.cpp b/src/zmc.cpp index 90925aa48..8646bcb26 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -181,7 +181,9 @@ int main(int argc, char *argv[]) { } logInit(log_id_string); - zmLoadConfig(); + zmLoadStaticConfig(); + zmDbConnect(); + zmLoadDBConfig(); logInit(log_id_string); hwcaps_detect(); diff --git a/src/zms.cpp b/src/zms.cpp index d7a9cc04a..01df67fc7 100644 --- a/src/zms.cpp +++ b/src/zms.cpp @@ -81,9 +81,12 @@ int main(int argc, const char *argv[], char **envp) { nph = true; } - zmLoadConfig(); char log_id_string[32] = "zms"; logInit(log_id_string); + zmLoadStaticConfig(); + zmDbConnect(); + zmLoadDBConfig(); + for (char **env = envp; *env != 0; env++) { char *thisEnv = *env; Debug(1, "env: %s", thisEnv); diff --git a/src/zmu.cpp b/src/zmu.cpp index 32b9ad001..e44c4d8d4 100644 --- a/src/zmu.cpp +++ b/src/zmu.cpp @@ -417,8 +417,10 @@ int main(int argc, char *argv[]) { } //printf( "Monitor %d, Function %d\n", mon_id, function ); - zmLoadConfig(); - + logInit("zmu"); + zmLoadStaticConfig(); + zmDbConnect(); + zmLoadDBConfig(); logInit("zmu"); zmSetDefaultTermHandler();