X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/55a204e46261850f1a26ebafc45fb3ba6f0604c6..79c8c4ef19419fd58a7aab7efd6a299b295e55b3:/projects/stargazer/main.cpp?ds=sidebyside diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 6a96519b..702bfb86 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -63,10 +63,10 @@ namespace { -std::set executersPid; +std::set executers; void StartTimer(); -int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings); +int StartScriptExecuter(char * procName, int msgKey, int * msgID); int ForkAndWait(const std::string & confDir); void KillExecuters(); @@ -89,9 +89,9 @@ else } //----------------------------------------------------------------------------- #if defined(LINUX) || defined(DARWIN) -int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings) +int StartScriptExecuter(char * procName, int msgKey, int * msgID) #else -int StartScriptExecuter(char *, int msgKey, int * msgID, SETTINGS_IMPL * settings) +int StartScriptExecuter(char *, int msgKey, int * msgID) #endif { STG_LOGGER & WriteServLog = GetStgLogger(); @@ -123,32 +123,31 @@ 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!"); return -1; 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 +159,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 +197,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 +210,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 +220,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 +245,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); if (ret < 0) { STG_LOGGER & WriteServLog = GetStgLogger(); @@ -270,7 +264,12 @@ for (size_t i = 0; i < settings->GetExecutersNum(); i++) } } -PIDFile pidFile(settings->GetPIDFileName()); +PIDFile pidFile(settings.GetPIDFileName()); + +struct sigaction sa; +memset(&sa, 0, sizeof(sa)); +sa.sa_handler = SIG_DFL; +sigaction(SIGHUP, &sa, NULL); // Apparently FreeBSD ignores SIGHUP by default when launched from rc.d at bot time. sigset_t signalSet; sigfillset(&signalSet); @@ -287,7 +286,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 +308,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 +322,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,51 +333,51 @@ 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: + { + SETTINGS_IMPL newSettings(settings); + if (newSettings.ReadSettings()) + WriteServLog("ReadSettings error. %s", newSettings.GetStrError().c_str()); + else + settings = newSettings; + WriteServLog.SetLogFileName(settings.GetLogFileName()); traffCnt.Reload(); - manager.reload(); + manager.reload(settings); + } 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("+++++++++++++++++++++++++++++++++++++++++++++"); +manager.stop(); + if (loop.Stop()) WriteServLog("Event loop not stopped."); @@ -400,8 +399,6 @@ KillExecuters(); StopStgTimer(); WriteServLog("StgTimer: Stop successfull."); -delete settings; - WriteServLog("Stg stopped successfully."); WriteServLog("---------------------------------------------");