From a65f0d5b294d9a5d90e4767ef50857c4cf3589b2 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 25 Oct 2014 06:27:00 +0300 Subject: [PATCH] Cosmetic changes. --- projects/stargazer/main.cpp | 89 +++++++++++----------------- projects/stargazer/plugin_mgr.cpp | 4 +- projects/stargazer/settings_impl.cpp | 45 ++------------ projects/stargazer/settings_impl.h | 1 - 4 files changed, 42 insertions(+), 97 deletions(-) diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 6a96519b..3db3b266 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -63,7 +63,7 @@ namespace { -std::set executersPid; +std::set executers; void StartTimer(); int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings); @@ -123,9 +123,9 @@ if (*msgID == -11) // If msgID == -11 - first call. Create queue } } -pid_t executerPid = fork(); +pid_t pid = fork(); -switch (executerPid) +switch (pid) { case -1: WriteServLog("Fork error!"); @@ -134,21 +134,21 @@ switch (executerPid) case 0: delete settings; #if defined(LINUX) || defined(DARWIN) - Executer(*msgID, executerPid, procName); + Executer(*msgID, pid, procName); #else - Executer(*msgID, executerPid); + Executer(*msgID, pid); #endif return 1; default: - if (executersPid.empty()) { + if (executers.empty()) { #if defined(LINUX) || defined(DARWIN) - Executer(*msgID, executerPid, NULL); + Executer(*msgID, pid, NULL); #else - Executer(*msgID, executerPid); + Executer(*msgID, pid); #endif } - executersPid.insert(executerPid); + executers.insert(pid); } return 0; } @@ -160,11 +160,11 @@ int ForkAndWait(const std::string &) #endif { #ifndef NO_DAEMON -pid_t childPid = fork(); +pid_t pid = fork(); std::string startFile = confDir + START_FILE; unlink(startFile.c_str()); -switch (childPid) +switch (pid) { case -1: return -1; @@ -198,9 +198,8 @@ return 0; //----------------------------------------------------------------------------- void KillExecuters() { -std::set::iterator pid; -pid = executersPid.begin(); -while (pid != executersPid.end()) +std::set::iterator pid(executers.begin()); +while (pid != executers.end()) { printfd(__FILE__, "KillExecuters pid=%d\n", *pid); kill(*pid, SIGUSR1); @@ -212,7 +211,6 @@ while (pid != executersPid.end()) //----------------------------------------------------------------------------- int main(int argc, char * argv[]) { -SETTINGS_IMPL * settings = NULL; int msgID = -11; GetStgLogger().SetLogFileName("/var/log/stargazer.log"); @@ -223,27 +221,24 @@ if (getuid()) return 1; } -if (argc == 2) - settings = new SETTINGS_IMPL(argv[1]); -else - settings = new SETTINGS_IMPL(); +SETTINGS_IMPL settings(argc == 2 ? argv[1] : ""); -if (settings->ReadSettings()) +if (settings.ReadSettings()) { STG_LOGGER & WriteServLog = GetStgLogger(); - if (settings->GetLogFileName() != "") - WriteServLog.SetLogFileName(settings->GetLogFileName()); + if (settings.GetLogFileName() != "") + WriteServLog.SetLogFileName(settings.GetLogFileName()); - WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str()); + WriteServLog("ReadSettings error. %s", settings.GetStrError().c_str()); return -1; } #ifndef NO_DAEMON -std::string startFile(settings->GetConfDir() + START_FILE); +std::string startFile(settings.GetConfDir() + START_FILE); #endif -if (ForkAndWait(settings->GetConfDir()) < 0) +if (ForkAndWait(settings.GetConfDir()) < 0) { STG_LOGGER & WriteServLog = GetStgLogger(); WriteServLog("Fork error!"); @@ -251,12 +246,12 @@ if (ForkAndWait(settings->GetConfDir()) < 0) } STG_LOGGER & WriteServLog = GetStgLogger(); -WriteServLog.SetLogFileName(settings->GetLogFileName()); +WriteServLog.SetLogFileName(settings.GetLogFileName()); WriteServLog("Stg v. %s", SERVER_VERSION); -for (size_t i = 0; i < settings->GetExecutersNum(); i++) +for (size_t i = 0; i < settings.GetExecutersNum(); i++) { - int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings); + int ret = StartScriptExecuter(argv[0], settings.GetExecMsgKey(), &msgID, &settings); if (ret < 0) { STG_LOGGER & WriteServLog = GetStgLogger(); @@ -270,7 +265,7 @@ for (size_t i = 0; i < settings->GetExecutersNum(); i++) } } -PIDFile pidFile(settings->GetPIDFileName()); +PIDFile pidFile(settings.GetPIDFileName()); sigset_t signalSet; sigfillset(&signalSet); @@ -287,7 +282,7 @@ if (!IsStgTimerRunning()) EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance()); -STORE_LOADER storeLoader(*settings); +STORE_LOADER storeLoader(settings); if (storeLoader.Load()) { printfd(__FILE__, "Storage plugin: '%s'\n", storeLoader.GetStrError().c_str()); @@ -309,9 +304,9 @@ ADMINS_IMPL admins(&store); TARIFFS_IMPL tariffs(&store); SERVICES_IMPL services(&store); CORPORATIONS_IMPL corps(&store); -USERS_IMPL users(settings, &store, &tariffs, services, admins.GetSysAdmin()); -TRAFFCOUNTER_IMPL traffCnt(&users, settings->GetRulesFileName()); -traffCnt.SetMonitorDir(settings->GetMonitorDir()); +USERS_IMPL users(&settings, &store, &tariffs, services, admins.GetSysAdmin()); +TRAFFCOUNTER_IMPL traffCnt(&users, settings.GetRulesFileName()); +traffCnt.SetMonitorDir(settings.GetMonitorDir()); if (users.Start()) return -1; @@ -323,7 +318,7 @@ if (traffCnt.Start()) WriteServLog("Traffcounter started successfully."); -STG::PluginManager manager(*settings, store, admins, tariffs, services, corps, users, traffCnt); +STG::PluginManager manager(settings, store, admins, tariffs, services, corps, users, traffCnt); srandom(static_cast(stgTime)); @@ -334,15 +329,13 @@ WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); creat(startFile.c_str(), S_IRUSR); #endif -while (true) +bool running = true; +while (running) { sigfillset(&signalSet); int sig = 0; sigwait(&signalSet, &sig); - bool stop = false; int status; - pid_t childPid; - std::set::iterator it; switch (sig) { case SIGHUP: @@ -350,31 +343,23 @@ while (true) manager.reload(); break; case SIGTERM: - stop = true; + running = false; break; case SIGINT: - stop = true; + running = false; break; case SIGPIPE: WriteServLog("Broken pipe!"); break; case SIGCHLD: - childPid = waitpid(-1, &status, WNOHANG); - - it = executersPid.find(childPid); - if (it != executersPid.end()) - { - executersPid.erase(it); - if (executersPid.empty()) - stop = true; - } + executers.erase(waitpid(-1, &status, WNOHANG)); + if (executers.empty()) + running = false; break; default: WriteServLog("Ignore signal %d", sig); break; } - if (stop) - break; } WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); @@ -400,8 +385,6 @@ KillExecuters(); StopStgTimer(); WriteServLog("StgTimer: Stop successfull."); -delete settings; - WriteServLog("Stg stopped successfully."); WriteServLog("---------------------------------------------"); diff --git a/projects/stargazer/plugin_mgr.cpp b/projects/stargazer/plugin_mgr.cpp index 9a3dab00..5346db8b 100644 --- a/projects/stargazer/plugin_mgr.cpp +++ b/projects/stargazer/plugin_mgr.cpp @@ -84,7 +84,7 @@ PluginManager::PluginManager(const SETTINGS_IMPL& settings, { m_log("Failed to start module '%s': '%s'", plugin.GetVersion().c_str(), plugin.GetStrError().c_str()); - printfd(__FILE__, "Failed to start module '%s': '%s'", plugin.GetVersion().c_str(), + printfd(__FILE__, "Failed to start module '%s': '%s'\n", plugin.GetVersion().c_str(), plugin.GetStrError().c_str()); } else @@ -105,7 +105,7 @@ PluginManager::~PluginManager() { m_log("Failed to stop module '%s': '%s'", plugin.GetVersion().c_str(), plugin.GetStrError().c_str()); - printfd(__FILE__, "Failed to stop module '%s': '%s'", plugin.GetVersion().c_str(), + printfd(__FILE__, "Failed to stop module '%s': '%s'\n", plugin.GetVersion().c_str(), plugin.GetStrError().c_str()); } else diff --git a/projects/stargazer/settings_impl.cpp b/projects/stargazer/settings_impl.cpp index f6a40db3..d115919d 100644 --- a/projects/stargazer/settings_impl.cpp +++ b/projects/stargazer/settings_impl.cpp @@ -36,48 +36,13 @@ $Author: faust $ #include "stg/dotconfpp.h" #include "settings_impl.h" -//----------------------------------------------------------------------------- -SETTINGS_IMPL::SETTINGS_IMPL() - : SETTINGS(), - strError(), - modulesPath("/usr/lib/stg"), - dirName(DIR_NUM), - confDir("/etc/stargazer"), - scriptsDir("/etc/stargazer"), - rules("/etc/stargazer/rules"), - logFile("/var/log/stargazer.log"), - pidFile("/var/run/stargazer.pid"), - monitorDir("/var/stargazer/monitoring"), - monitoring(false), - detailStatWritePeriod(dsPeriod_1_6), - statWritePeriod(10), - stgExecMsgKey(5555), - executersNum(1), - fullFee(false), - dayFee(0), - dayResetTraff(0), - spreadFee(false), - freeMbAllowInet(false), - dayFeeIsLastDay(false), - writeFreeMbTraffCost(false), - showFeeInCash(true), - messageTimeout(0), - feeChargeType(0), - reconnectOnTariffChange(false), - modulesSettings(), - storeModuleSettings(), - logger(GetStgLogger()) -{ -} //----------------------------------------------------------------------------- SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd) - : SETTINGS(), - strError(), - modulesPath("/usr/lib/stg"), + : modulesPath("/usr/lib/stg"), dirName(DIR_NUM), - confDir(cd), - scriptsDir(cd), - rules(cd + "/rules"), + confDir(cd.empty() ? "/etc/stargazer" : cd), + scriptsDir(confDir), + rules(confDir + "/rules"), logFile("/var/log/stargazer.log"), pidFile("/var/run/stargazer.pid"), monitorDir("/var/stargazer/monitoring"), @@ -97,8 +62,6 @@ SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd) messageTimeout(0), feeChargeType(0), reconnectOnTariffChange(false), - modulesSettings(), - storeModuleSettings(), logger(GetStgLogger()) { } diff --git a/projects/stargazer/settings_impl.h b/projects/stargazer/settings_impl.h index 7ba2708f..69013764 100644 --- a/projects/stargazer/settings_impl.h +++ b/projects/stargazer/settings_impl.h @@ -58,7 +58,6 @@ class DOTCONFDocumentNode; //----------------------------------------------------------------------------- class SETTINGS_IMPL : public SETTINGS { public: - SETTINGS_IMPL(); SETTINGS_IMPL(const std::string &); SETTINGS_IMPL(const SETTINGS_IMPL &); virtual ~SETTINGS_IMPL() {} -- 2.43.2