From 37b15307ff46d7213bbfc37485acaccc8c471edd Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Wed, 20 Nov 2019 17:54:56 +0000 Subject: [PATCH] Fix: Don't call cPlugin::ConfigDirectory on non-main thread --- httpdclient.c | 10 ++++++---- httpdclient.h | 5 +++-- jsonserver.c | 2 +- vdrclient.c | 23 ++++------------------- vdrclient.h | 4 +++- 5 files changed, 17 insertions(+), 27 deletions(-) diff --git a/httpdclient.c b/httpdclient.c index e85af17..00083c1 100644 --- a/httpdclient.c +++ b/httpdclient.c @@ -16,10 +16,12 @@ std::shared_ptr HTTPDClient::logger; struct MHD_Daemon* HTTPDClient::httpdserver = NULL; std::string HTTPDClient::docRoot; +std::string HTTPDClient::configDir; -bool HTTPDClient::StartServer(std::string _docRoot, int port) +bool HTTPDClient::StartServer(std::string _docRoot, int port, const char* _configDir) { docRoot = _docRoot; + configDir = std::string(_configDir); logger = spd::get("jsonserver_spdlog"); @@ -78,7 +80,7 @@ void HTTPDClient::connection_notify(void *cls, struct MHD_Connection* mhd_connec { if (toe == MHD_CONNECTION_NOTIFY_STARTED) { - *socket_context = (void*)new HTTPDClient(mhd_connection); + *socket_context = (void*)new HTTPDClient(mhd_connection, configDir); } else if (toe == MHD_CONNECTION_NOTIFY_CLOSED) { @@ -172,8 +174,8 @@ int HTTPDClient::handle_connection(void* cls, struct MHD_Connection* mhd_connect // End of static callbacks, now for the client object itself -HTTPDClient::HTTPDClient(struct MHD_Connection* _mhd_connection) -: postprocessor(NULL), url(NULL), mhd_connection(_mhd_connection) +HTTPDClient::HTTPDClient(struct MHD_Connection* _mhd_connection, const std::string& _configDir) +: postprocessor(NULL), url(NULL), mhd_connection(_mhd_connection), vdrclient(_configDir) { // printf("HTTPDClient created %p\n", this); } diff --git a/httpdclient.h b/httpdclient.h index d161188..0721780 100644 --- a/httpdclient.h +++ b/httpdclient.h @@ -19,7 +19,7 @@ namespace spd = spdlog; class HTTPDClient { public: - static bool StartServer(std::string docRoot, int port); + static bool StartServer(std::string docRoot, int port, const char* configDir); static void StopServer(); // static callbacks from libmicrohttpd @@ -40,8 +40,9 @@ class HTTPDClient static std::shared_ptr logger; static struct MHD_Daemon* httpdserver; static std::string docRoot; + static std::string configDir; - HTTPDClient(struct MHD_Connection*); + HTTPDClient(struct MHD_Connection*, const std::string& configDir); ~HTTPDClient(); void setUrl(const char* url); diff --git a/jsonserver.c b/jsonserver.c index 38039fd..3a68e4a 100644 --- a/jsonserver.c +++ b/jsonserver.c @@ -168,7 +168,7 @@ bool cPluginJsonserver::Start(void) return false; } - if (!HTTPDClient::StartServer(cfgDocRoot, cfgPort)) + if (!HTTPDClient::StartServer(cfgDocRoot, cfgPort, configDir)) { logger->critical("Main: Failed to start MHD"); dsyslog("jsonserver: Failed to start MHD"); diff --git a/vdrclient.c b/vdrclient.c index b1d2a8a..47bdf66 100644 --- a/vdrclient.c +++ b/vdrclient.c @@ -26,7 +26,8 @@ critical always be done in the sequence Timers, Channels, Recordings, Schedules. */ -VDRClient::VDRClient() +VDRClient::VDRClient(const std::string& _configDir) +: configDir(_configDir) { logger = spd::get("jsonserver_spdlog"); } @@ -1487,7 +1488,6 @@ bool VDRClient::epgfilterdel(PFMap& postFields, Json::Value& js) // RETHROWS js["Error"] = "Filter file format error"; return true; } - logger->debug("loop: {} {}", c, p); if ((c == channel) && (p == programme)) { setting.remove(x); @@ -1546,7 +1546,6 @@ bool VDRClient::epgfilterget(PFMap& postFields, Json::Value& js) // RETHROWS js["Error"] = "Filter file format error"; return true; } - logger->debug("loop: {} {}", c, p); Json::Value oneFilter; oneFilter["c"] = c; oneFilter["p"] = p; @@ -1736,14 +1735,7 @@ std::string VDRClient::getVarString(PFMap& postFields, const char* paramName) // bool VDRClient::loadEpgFilter(libconfig::Config& epgFilter) { - const char* configDir = cPlugin::ConfigDirectory("jsonserver"); - if (!configDir) - { - logger->error("loadEpgFilter: Error: Could not get config dir from VDR"); - return false; - } - - std::string epgFilterFile(std::string(configDir) + std::string("/epgfilter.conf")); + std::string epgFilterFile(configDir + std::string("/epgfilter.conf")); FILE* fp = fopen(epgFilterFile.c_str(), "a"); if (!fp) { @@ -1788,14 +1780,7 @@ bool VDRClient::loadEpgFilter(libconfig::Config& epgFilter) bool VDRClient::saveEpgFilter(libconfig::Config& epgFilter) { - const char* configDir = cPlugin::ConfigDirectory("jsonserver"); - if (!configDir) - { - logger->error("saveEpgFilter: Error: Could not get config dir from VDR"); - return false; - } - - std::string epgFilterFile(std::string(configDir) + std::string("/epgfilter.conf")); + std::string epgFilterFile(configDir + std::string("/epgfilter.conf")); try { epgFilter.writeFile(epgFilterFile.c_str()); diff --git a/vdrclient.h b/vdrclient.h index 0c2eb34..b49c8b8 100644 --- a/vdrclient.h +++ b/vdrclient.h @@ -22,12 +22,14 @@ class VDRClient typedef std::map PFMap; public: - VDRClient(); + VDRClient(const std::string& configDir); ~VDRClient(); bool process(std::string& request, PFMap& postFields, std::string& returnData); private: + const std::string& configDir; + std::shared_ptr logger; bool gettime(PFMap& postFields, Json::Value& returnJSON); bool diskstats(PFMap& postFields, Json::Value& returnJSON); -- 2.39.2