From 0115ed129856bb4c8ee07fec9b7bc94ed632ca4a Mon Sep 17 00:00:00 2001 From: Mr-Dave Date: Sun, 18 Aug 2024 17:29:14 -0600 Subject: [PATCH] Add restart for dbse,logger and webu --- src/conf.cpp | 17 ++++++++++++ src/conf.hpp | 1 + src/dbse.cpp | 68 +++++++++++++++++++++++++--------------------- src/dbse.hpp | 3 +- src/logger.cpp | 21 +++++++++----- src/logger.hpp | 4 ++- src/motionplus.cpp | 47 ++++++++++++++++++++++++++++++-- src/motionplus.hpp | 1 + src/webu.cpp | 8 +++--- src/webu.hpp | 4 +-- 10 files changed, 125 insertions(+), 49 deletions(-) diff --git a/src/conf.cpp b/src/conf.cpp index 2679f5f..a2c751b 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -4232,6 +4232,23 @@ void cls_config::parms_copy(cls_config *src) } +void cls_config::parms_copy(cls_config *src, PARM_CAT p_cat) +{ + int indx; + std::string parm_nm, parm_val; + + indx = 0; + while (config_parms[indx].parm_name != "") { + if (config_parms[indx].parm_cat == p_cat) { + parm_nm =config_parms[indx].parm_name; + src->edit_get(parm_nm, parm_val, p_cat); + edit_set(parm_nm, parm_val); + } + indx++; + } + +} + void cls_config::init(cls_motapp *app) { std::string filename; diff --git a/src/conf.hpp b/src/conf.hpp index 1ec86c6..3731aa6 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -299,6 +299,7 @@ void parms_log(cls_motapp *app); void parms_write(cls_motapp *app); void parms_copy(cls_config *src); + void parms_copy(cls_config *src, PARM_CAT p_cat); private: void cmdline(cls_motapp *app); diff --git a/src/dbse.cpp b/src/dbse.cpp index 6ac0902..deef172 100644 --- a/src/dbse.cpp +++ b/src/dbse.cpp @@ -416,7 +416,7 @@ void cls_dbse::mariadb_exec (std::string sql) , mysql_error(database_mariadb) , retcd); if (retcd >= 2000) { - dbse_close(); + shutdown(); return; } } @@ -427,7 +427,7 @@ void cls_dbse::mariadb_exec (std::string sql) , _("MariaDB query commit failed. %s error code %d") , mysql_error(database_mariadb), retcd); if (retcd >= 2000) { - dbse_close(); + shutdown(); return; } } @@ -449,7 +449,7 @@ void cls_dbse::mariadb_recs (std::string sql) if (retcd != 0){ MOTPLS_LOG(ERR, TYPE_DB, NO_ERRNO , _("Query error: %s"),sql.c_str()); - dbse_close(); + shutdown(); return; } @@ -457,7 +457,7 @@ void cls_dbse::mariadb_recs (std::string sql) if (qry_result == nullptr) { MOTPLS_LOG(ERR, TYPE_DB, NO_ERRNO , _("Query store error: %s"),sql.c_str()); - dbse_close(); + shutdown(); return; } @@ -591,7 +591,7 @@ void cls_dbse::mariadb_init() MOTPLS_LOG(ERR, TYPE_DB, NO_ERRNO , _("MariaDB error was %s") , mysql_error(database_mariadb)); - dbse_close(); + shutdown(); return; } is_open = true; @@ -658,7 +658,7 @@ void cls_dbse::pgsqldb_exec(std::string sql) , app->cfg->database_dbname.c_str() , PQerrorMessage(database_pgsqldb)); PQclear(res); - dbse_close(); + shutdown(); return; } else { MOTPLS_LOG(INF, TYPE_DB, NO_ERRNO @@ -814,7 +814,7 @@ void cls_dbse::pgsqldb_init() , _("Connection to PostgreSQL database '%s' failed: %s") , app->cfg->database_dbname.c_str() , PQerrorMessage(database_pgsqldb)); - dbse_close(); + shutdown(); return; } is_open = true; @@ -853,25 +853,23 @@ bool cls_dbse::dbse_open() return false; } - pthread_mutex_lock(&mutex_dbse); - MOTPLS_LOG(DBG, TYPE_DB, NO_ERRNO,_("Opening database")); + MOTPLS_LOG(DBG, TYPE_DB, NO_ERRNO,_("Opening database")); - #ifdef HAVE_MARIADB - if (app->cfg->database_type == "mariadb") { - mariadb_init(); - } - #endif - #ifdef HAVE_PGSQLDB - if (app->cfg->database_type == "postgresql") { - pgsqldb_init(); - } - #endif - #ifdef HAVE_SQLITE3DB - if (app->cfg->database_type == "sqlite3") { - sqlite3db_init(); - } - #endif - pthread_mutex_unlock(&mutex_dbse); + #ifdef HAVE_MARIADB + if (app->cfg->database_type == "mariadb") { + mariadb_init(); + } + #endif + #ifdef HAVE_PGSQLDB + if (app->cfg->database_type == "postgresql") { + pgsqldb_init(); + } + #endif + #ifdef HAVE_SQLITE3DB + if (app->cfg->database_type == "sqlite3") { + sqlite3db_init(); + } + #endif return is_open; } @@ -907,7 +905,7 @@ void cls_dbse::movielist_get(int p_device_id, lst_movies *p_movielist) } -void cls_dbse::dbse_close() +void cls_dbse::shutdown() { #ifdef HAVE_MARIADB mariadb_close(); @@ -1083,22 +1081,30 @@ void cls_dbse::dbse_edits() } +void cls_dbse::startup() +{ + is_open = false; + + dbse_edits(); + + dbse_open(); +} + cls_dbse::cls_dbse(cls_motapp *p_app) { app = p_app; - is_open = false; - pthread_mutex_init(&mutex_dbse, nullptr); - dbse_edits(); + pthread_mutex_lock(&mutex_dbse); + startup(); + pthread_mutex_unlock(&mutex_dbse); - dbse_open(); } cls_dbse::~cls_dbse() { - dbse_close(); + shutdown(); pthread_mutex_destroy(&mutex_dbse); } diff --git a/src/dbse.hpp b/src/dbse.hpp index 225b9b9..1c9464a 100644 --- a/src/dbse.hpp +++ b/src/dbse.hpp @@ -90,6 +90,8 @@ class cls_dbse { void movielist_add(cls_camera *cam, cls_movie *movie, timespec *ts1); void movielist_get(int p_device_id, lst_movies *p_movielist); bool restart; + void shutdown(); + void startup(); private: #ifdef HAVE_SQLITE3DB sqlite3 *database_sqlite3db; @@ -137,7 +139,6 @@ class cls_dbse { void dbse_edits(); bool dbse_open(); - void dbse_close(); void exec_sql(std::string sql); }; diff --git a/src/logger.cpp b/src/logger.cpp index 7317175..662fb1d 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -191,7 +191,7 @@ void cls_log::write_msg(int loglvl, int msg_type, int flgerr, int flgfnc, ...) return; } - pthread_mutex_lock(&mtx); + pthread_mutex_lock(&mutex_log); err_save = errno; memset(msg_full, 0, sizeof(msg_full)); @@ -229,7 +229,7 @@ void cls_log::write_msg(int loglvl, int msg_type, int flgerr, int flgfnc, ...) if ((flood_cnt <= 5000) && mystreq(msg_flood, &msg_full[prefixlen])) { flood_cnt++; - pthread_mutex_unlock(&mtx); + pthread_mutex_unlock(&mutex_log); return; } @@ -237,11 +237,11 @@ void cls_log::write_msg(int loglvl, int msg_type, int flgerr, int flgfnc, ...) write_norm(loglvl, prefixlen); - pthread_mutex_unlock(&mtx); + pthread_mutex_unlock(&mutex_log); } -void cls_log::log_stop() +void cls_log::shutdown() { if (log_file_ptr != nullptr) { MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO, "Closing log_file (%s)." @@ -251,6 +251,13 @@ void cls_log::log_stop() } } +void cls_log::startup() +{ + motlog->log_level = app->cfg->log_level; + motlog->log_fflevel = app->cfg->log_fflevel; + motlog->set_log_file(app->cfg->log_file); +} + cls_log::cls_log(cls_motapp *p_app) { app = p_app; @@ -261,7 +268,7 @@ cls_log::cls_log(cls_motapp *p_app) log_file_name = ""; flood_cnt = 0; set_mode(LOGMODE_SYSLOG); - pthread_mutex_init(&mtx, NULL); + pthread_mutex_init(&mutex_log, NULL); memset(msg_prefix,0,sizeof(msg_prefix)); memset(msg_flood,0,sizeof(msg_flood)); memset(msg_full,0,sizeof(msg_full)); @@ -271,8 +278,8 @@ cls_log::cls_log(cls_motapp *p_app) cls_log::~cls_log() { - log_stop(); - pthread_mutex_destroy(&mtx); + shutdown(); + pthread_mutex_destroy(&mutex_log); } diff --git a/src/logger.hpp b/src/logger.hpp index 6b0ae31..35d6974 100644 --- a/src/logger.hpp +++ b/src/logger.hpp @@ -62,10 +62,12 @@ int log_fflevel; void set_log_file(std::string pname); void write_msg(int loglvl, int msg_type, int flgerr, int flgfnc, ...); + pthread_mutex_t mutex_log; + void shutdown(); + void startup(); bool restart; private: cls_motapp *app; - pthread_mutex_t mtx; int log_mode; FILE *log_file_ptr; std::string log_file_name; diff --git a/src/motionplus.cpp b/src/motionplus.cpp index 62e615e..85d37a6 100644 --- a/src/motionplus.cpp +++ b/src/motionplus.cpp @@ -601,6 +601,48 @@ void cls_motapp::watchdog(uint camindx) } +void cls_motapp::check_restart() +{ + std::string parm_pid_org, parm_pid_new; + + if (motlog->restart == true) { + cfg->edit_get("pid_file",parm_pid_org, PARM_CAT_00); + conf_src->edit_get("pid_file",parm_pid_new, PARM_CAT_00); + if (parm_pid_org != parm_pid_new) { + pid_remove(); + } + + pthread_mutex_lock(&motlog->mutex_log); + motlog->shutdown(); + cfg->parms_copy(conf_src, PARM_CAT_00); + motlog->startup(); + pthread_mutex_unlock(&motlog->mutex_log); + + mytranslate_text("",cfg->native_language); + if (parm_pid_org != parm_pid_new) { + pid_write(); + } + motlog->restart = false; + } + + if (dbse->restart == true) { + pthread_mutex_lock(&dbse->mutex_dbse); + dbse->shutdown(); + cfg->parms_copy(conf_src, PARM_CAT_15); + dbse->startup(); + pthread_mutex_lock(&dbse->mutex_dbse); + dbse->restart = false; + } + + if (webu->restart == true) { + webu->shutdown(); + cfg->parms_copy(conf_src, PARM_CAT_13); + webu->startup(); + webu->restart = false; + } + +} + bool cls_motapp::check_devices() { int indx; @@ -664,9 +706,7 @@ void cls_motapp::init(int p_argc, char *p_argv[]) cfg = new cls_config; cfg->parms_copy(conf_src); - motlog->log_level = cfg->log_level; - motlog->log_fflevel = cfg->log_fflevel; - motlog->set_log_file(cfg->log_file); + motlog->startup(); mytranslate_init(); @@ -813,6 +853,7 @@ int main (int p_argc, char **p_argv) } app->camera_add(); app->camera_delete(); + app->check_restart(); } MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Motionplus devices finished")); if (app->reload_all) { diff --git a/src/motionplus.hpp b/src/motionplus.hpp index ff7a041..f643a79 100644 --- a/src/motionplus.hpp +++ b/src/motionplus.hpp @@ -175,6 +175,7 @@ class cls_motapp { void signal_process(); bool check_devices(); + void check_restart(); void init(int p_argc, char *p_argv[]); void deinit(); void camera_add(); diff --git a/src/webu.cpp b/src/webu.cpp index 30e4cd2..52e40ec 100644 --- a/src/webu.cpp +++ b/src/webu.cpp @@ -470,7 +470,7 @@ void cls_webu::start_daemon_port2() } -void cls_webu::webu_start() +void cls_webu::startup() { unsigned int randnbr; wb_daemon = nullptr; @@ -506,7 +506,7 @@ void cls_webu::webu_start() } -void cls_webu::webu_stop() +void cls_webu::shutdown() { int chkcnt; @@ -542,10 +542,10 @@ void cls_webu::webu_stop() cls_webu::cls_webu(cls_motapp *p_app) { app = p_app; - webu_start(); + startup(); } cls_webu::~cls_webu() { - webu_stop(); + shutdown(); } diff --git a/src/webu.hpp b/src/webu.hpp index 976e57a..dbd89d3 100644 --- a/src/webu.hpp +++ b/src/webu.hpp @@ -98,12 +98,12 @@ std::list wb_clients; int cnct_cnt; bool restart; + void startup(); + void shutdown(); private: ctx_mhdstart *mhdst; cls_motapp *app; - void webu_start(); - void webu_stop(); void init_actions(); void start_daemon_port1(); void start_daemon_port2();