]> git.stg.codes - stg.git/blobdiff - projects/stargazer/main.cpp
Ticket 26. The const MODULE_SETTINGS & ms parameter added to the
[stg.git] / projects / stargazer / main.cpp
index 6a96519bc5c35ecbd4f43cc0fb0f77eaf4f5180a..b878b39c41cf5ca146a81ce7712fb3f1e7c4c684 100644 (file)
 
 namespace
 {
-std::set<pid_t> executersPid;
+std::set<pid_t> 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<pid_t>::iterator pid;
-pid = executersPid.begin();
-while (pid != executersPid.end())
+std::set<pid_t>::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,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 +281,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 +303,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 +317,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<unsigned int>(stgTime));
 
@@ -334,47 +328,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<pid_t>::iterator it;
     switch (sig)
         {
         case SIGHUP:
+            {
+            SETTINGS_IMPL newSettings(settings);
+            if (newSettings.ReadSettings())
+                {
+                STG_LOGGER & WriteServLog = GetStgLogger();
+
+                if (newSettings.GetLogFileName() != "")
+                    WriteServLog.SetLogFileName(newSettings.GetLogFileName());
+
+                WriteServLog("ReadSettings error. %s", newSettings.GetStrError().c_str());
+                return -1;
+                }
+            settings = newSettings;
             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("+++++++++++++++++++++++++++++++++++++++++++++");
@@ -400,8 +398,6 @@ KillExecuters();
 StopStgTimer();
 WriteServLog("StgTimer: Stop successfull.");
 
-delete settings;
-
 WriteServLog("Stg stopped successfully.");
 WriteServLog("---------------------------------------------");