]> git.stg.codes - stg.git/blobdiff - projects/stargazer/main.cpp
Merge branch 'naffanya-dev'
[stg.git] / projects / stargazer / main.cpp
index 7e83b72dce6cf0489cdacd13e25e0f2b33bcbe26..7cc8eccc6e54c32f7ead49ad48bd01357b7d88bf 100644 (file)
@@ -46,9 +46,7 @@
 #include "stg/plugin.h"
 #include "stg/logger.h"
 #include "stg/scriptexecuter.h"
-#include "stg/conffiles.h"
 #include "stg/version.h"
-#include "stg/pinger.h"
 #include "stg_timer.h"
 #include "settings_impl.h"
 #include "users_impl.h"
@@ -62,8 +60,6 @@
 #include "pidfile.h"
 #include "eventloop.h"
 
-using namespace std;
-
 #ifdef DEBUG
     #define MAIN_DEBUG (1)
     #define NO_DAEMON  (1)
@@ -71,8 +67,16 @@ using namespace std;
 
 #define START_FILE "/._ST_ART_ED_"
 
-set<pid_t> executersPid;
-static pid_t stgChildPid;
+namespace
+{
+std::set<pid_t> executersPid;
+
+bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs);
+bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs);
+void StartTimer();
+int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings);
+int ForkAndWait(const std::string & confDir);
+void KillExecuters();
 
 //-----------------------------------------------------------------------------
 bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
@@ -85,7 +89,7 @@ bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
 return lhs.GetStopPosition() > rhs.GetStopPosition();
 }
 //-----------------------------------------------------------------------------
-static void StartTimer()
+void StartTimer()
 {
 STG_LOGGER & WriteServLog = GetStgLogger();
 
@@ -102,7 +106,11 @@ else
     }
 }
 //-----------------------------------------------------------------------------
+#if defined(LINUX) || defined(DARWIN)
 int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings)
+#else
+int StartScriptExecuter(char *, int msgKey, int * msgID, SETTINGS_IMPL * settings)
+#endif
 {
 STG_LOGGER & WriteServLog = GetStgLogger();
 
@@ -143,12 +151,20 @@ switch (executerPid)
 
     case 0:
         delete settings;
+#if defined(LINUX) || defined(DARWIN)
         Executer(*msgID, executerPid, procName);
+#else
+        Executer(*msgID, executerPid);
+#endif
         return 1;
 
     default:
         if (executersPid.empty()) {
+#if defined(LINUX) || defined(DARWIN)
             Executer(*msgID, executerPid, NULL);
+#else
+            Executer(*msgID, executerPid);
+#endif
         }
         executersPid.insert(executerPid);
     }
@@ -156,17 +172,17 @@ return 0;
 }
 //-----------------------------------------------------------------------------
 #ifndef NO_DAEMON
-int ForkAndWait(const string & confDir)
+int ForkAndWait(const std::string & confDir)
 #else
-int ForkAndWait(const string &)
+int ForkAndWait(const std::string &)
 #endif
 {
 #ifndef NO_DAEMON
-stgChildPid = fork();
-string startFile = confDir + START_FILE;
+pid_t childPid = fork();
+std::string startFile = confDir + START_FILE;
 unlink(startFile.c_str());
 
-switch (stgChildPid)
+switch (childPid)
     {
     case -1:
         return -1;
@@ -200,7 +216,7 @@ return 0;
 //-----------------------------------------------------------------------------
 void KillExecuters()
 {
-set<pid_t>::iterator pid;
+std::set<pid_t>::iterator pid;
 pid = executersPid.begin();
 while (pid != executersPid.end())
     {
@@ -210,6 +226,8 @@ while (pid != executersPid.end())
     }
 }
 //-----------------------------------------------------------------------------
+} // namespace anonymous
+//-----------------------------------------------------------------------------
 int main(int argc, char * argv[])
 {
 SETTINGS_IMPL * settings = NULL;
@@ -227,10 +245,10 @@ int msgID = -11;
     WriteServLog.SetLogFileName("/var/log/stargazer.log");
     }
 
-vector<MODULE_SETTINGS> modSettings;
-list<PLUGIN_RUNNER> modules;
+std::vector<MODULE_SETTINGS> modSettings;
+std::list<PLUGIN_RUNNER> modules;
 
-list<PLUGIN_RUNNER>::iterator modIter;
+std::list<PLUGIN_RUNNER>::iterator modIter;
 
 if (getuid())
     {
@@ -253,9 +271,8 @@ if (settings->ReadSettings())
     WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
     exit(1);
     }
-
 #ifndef NO_DAEMON
-string startFile(settings->GetConfDir() + START_FILE);
+std::string startFile(settings->GetConfDir() + START_FILE);
 #endif
 
 if (ForkAndWait(settings->GetConfDir()) < 0)
@@ -287,6 +304,10 @@ for (size_t i = 0; i < settings->GetExecutersNum(); i++)
 
 PIDFile pidFile(settings->GetPIDFileName());
 
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
 StartTimer();
 WaitTimer();
 if (!IsStgTimerRunning())
@@ -325,7 +346,7 @@ modSettings = settings->GetModulesSettings();
 
 for (size_t i = 0; i < modSettings.size(); i++)
     {
-    string modulePath = settings->GetModulesPath();
+    std::string modulePath = settings->GetModulesPath();
     modulePath += "/mod_";
     modulePath += modSettings[i].moduleName;
     modulePath += ".so";
@@ -350,7 +371,8 @@ while (modIter != modules.end())
     {
     if (modIter->Load())
         {
-        WriteServLog("Error: %s",
+        WriteServLog("Error loading module '%s': %s",
+                     modIter->GetPlugin()->GetVersion().c_str(),
                      modIter->GetStrError().c_str());
         goto exitLblNotStarted;
         }
@@ -377,7 +399,8 @@ while (modIter != modules.end())
     {
     if (modIter->Start())
         {
-        WriteServLog("Error: %s",
+        WriteServLog("Error starting module '%s': %s",
+                     modIter->GetPlugin()->GetVersion().c_str(),
                      modIter->GetStrError().c_str());
         goto exitLbl;
         }
@@ -385,7 +408,7 @@ while (modIter != modules.end())
     ++modIter;
     }
 
-srandom(stgTime);
+srandom(static_cast<unsigned int>(stgTime));
 
 WriteServLog("Stg started successfully.");
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
@@ -396,14 +419,13 @@ creat(startFile.c_str(), S_IRUSR);
 
 while (true)
     {
-    sigset_t signalSet;
     sigfillset(&signalSet);
     int sig = 0;
     sigwait(&signalSet, &sig);
     bool stop = false;
     int status;
     pid_t childPid;
-    set<pid_t>::iterator it;
+    std::set<pid_t>::iterator it;
     switch (sig)
         {
         case SIGHUP:
@@ -413,9 +435,9 @@ while (true)
                 {
                 if (modIter->Reload())
                     {
-                    WriteServLog("Error reloading %s ('%s')", modIter->GetPlugin()->GetVersion().c_str(),
+                    WriteServLog("Error reloading module '%s': '%s'", modIter->GetPlugin()->GetVersion().c_str(),
                                                               modIter->GetStrError().c_str());
-                    printfd(__FILE__, "Error reloading %s ('%s')\n", modIter->GetPlugin()->GetVersion().c_str(),
+                    printfd(__FILE__, "Error reloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(),
                                                                      modIter->GetStrError().c_str());
                     }
                 }
@@ -461,12 +483,15 @@ while (modIter != modules.end())
     printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
     if (modIter->Stop())
         {
-        WriteServLog("Module \'%s\': Error: %s",
+        WriteServLog("Error stopping module '%s': %s",
                      modIter->GetPlugin()->GetVersion().c_str(),
                      modIter->GetStrError().c_str());
-        printfd(__FILE__, "Failed to stop module '%s'\n", name.c_str());
+        printfd(__FILE__, "Error stopping module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
+        }
+    else
+        {
+        WriteServLog("Module: '%s'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
         }
-    WriteServLog("Module: \'%s\'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
     ++modIter;
     }
 
@@ -490,10 +515,10 @@ while (modIter != modules.end())
         printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
         if (modIter->Unload())
             {
-            WriteServLog("Module \'%s\': Error: %s",
-                         name.c_str(),
+            WriteServLog("Error unloading module '%s': '%s'",
+                         modIter->GetPlugin()->GetVersion().c_str(),
                          modIter->GetStrError().c_str());
-            printfd(__FILE__, "Failed to unload module '%s'\n", name.c_str());
+            printfd(__FILE__, "Error unloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
             }
         }
     ++modIter;