]> git.stg.codes - stg.git/blobdiff - projects/rscriptd/main.cpp
Add rules count in STG-MIB
[stg.git] / projects / rscriptd / main.cpp
index 2f5f0baa5a642e29c7d59b5b80a9b8725e737e6b..7e7f7f355e257f2e10d61a3cec9d65d53eaf241a 100644 (file)
 #include <cstdlib>
 #include <cstdio>
 #include <csignal>
 #include <cstdlib>
 #include <cstdio>
 #include <csignal>
+#include <cerrno>
+#include <cstring> // strerror
 #include <set>
 
 #include "stg/common.h"
 #include <set>
 
 #include "stg/common.h"
-#include "stg/stg_logger.h"
-#include "stg/script_executer.h"
+#include "stg/logger.h"
+#include "stg/scriptexecuter.h"
 #include "stg/conffiles.h"
 #include "stg/version.h"
 #include "listener.h"
 #include "stg/conffiles.h"
 #include "stg/version.h"
 #include "listener.h"
@@ -193,6 +195,18 @@ newsa.sa_flags = 0;
 sigaction(SIGCHLD, &newsa, &oldsa);
 }
 //-----------------------------------------------------------------------------
 sigaction(SIGCHLD, &newsa, &oldsa);
 }
 //-----------------------------------------------------------------------------
+void KillExecuters()
+{
+set<pid_t>::iterator pid;
+pid = executersPid.begin();
+while (pid != executersPid.end())
+    {
+    printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
+    kill(*pid, SIGUSR1);
+    ++pid;
+    }
+}
+//-----------------------------------------------------------------------------
 int StartScriptExecuter(char * procName, int msgKey, int * msgID)
 {
 STG_LOGGER & WriteServLog = GetStgLogger();
 int StartScriptExecuter(char * procName, int msgKey, int * msgID)
 {
 STG_LOGGER & WriteServLog = GetStgLogger();
@@ -237,17 +251,55 @@ switch (executerPid)
         //close(1);
         //close(2);
         //setsid();
         //close(1);
         //close(2);
         //setsid();
-        Executer(msgKey, *msgID, executerPid, procName);
+        Executer(*msgID, executerPid, procName);
         return 1;
 
     default:    // Parent
         if (executersPid.empty())
         return 1;
 
     default:    // Parent
         if (executersPid.empty())
-            Executer(msgKey, *msgID, executerPid, NULL);
+            Executer(*msgID, executerPid, NULL);
         executersPid.insert(executerPid);
     }
 return 0;
 }
 //-----------------------------------------------------------------------------
         executersPid.insert(executerPid);
     }
 return 0;
 }
 //-----------------------------------------------------------------------------
+void StopScriptExecuter(int msgID)
+{
+STG_LOGGER & WriteServLog = GetStgLogger();
+
+for (int i = 0; i < 5; ++i)
+    {
+    struct msqid_ds data;
+    if (msgctl(msgID, IPC_STAT, &data))
+        {
+        int e = errno;
+        printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
+        WriteServLog( "Failed to check queue emptiness: '%s'", strerror(e));
+        break;
+        }
+
+    WriteServLog("Messages in queue: %d", data.msg_qnum);
+
+    if (data.msg_qnum == 0)
+        break;
+
+    struct timespec ts = {1, 0};
+    nanosleep(&ts, NULL);
+    }
+
+if (msgctl(msgID, IPC_RMID, NULL))
+    {
+    int e = errno;
+    printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
+    WriteServLog("Failed to remove queue: '%s'", strerror(e));
+    }
+else
+    {
+    WriteServLog("Queue removed successfully.");
+    }
+
+KillExecuters();
+}
+//-----------------------------------------------------------------------------
 #ifdef NO_DAEMON
 int ForkAndWait(const string &)
 #else
 #ifdef NO_DAEMON
 int ForkAndWait(const string &)
 #else
@@ -256,8 +308,6 @@ int ForkAndWait(const string & confDir)
 {
 #ifndef NO_DAEMON
 stgChildPid = fork();
 {
 #ifndef NO_DAEMON
 stgChildPid = fork();
-string startFile = confDir + START_FILE;
-unlink(startFile.c_str());
 
 switch (stgChildPid)
     {
 
 switch (stgChildPid)
     {
@@ -273,23 +323,6 @@ switch (stgChildPid)
         break;
 
     default:    // Parent
         break;
 
     default:    // Parent
-        for (int i = 0; i < 120 * 5; i++)
-            {
-            if (access(startFile.c_str(), F_OK) == 0)
-                {
-                //printf("Fork successfull. Exit.\n");
-                unlink(startFile.c_str());
-                exit(0);
-                }
-
-            if (childExited)
-                {
-                unlink(startFile.c_str());
-                exit(1);
-                }
-            usleep(200000);
-            }
-        unlink(startFile.c_str());
         exit(1);
         break;
     }
         exit(1);
         break;
     }
@@ -297,18 +330,6 @@ switch (stgChildPid)
 return 0;
 }
 //-----------------------------------------------------------------------------
 return 0;
 }
 //-----------------------------------------------------------------------------
-void KillExecuters()
-{
-set<pid_t>::iterator pid;
-pid = executersPid.begin();
-while (pid != executersPid.end())
-    {
-    printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
-    kill(*pid, SIGUSR1);
-    ++pid;
-    }
-}
-//-----------------------------------------------------------------------------
 int main(int argc, char * argv[])
 {
 
 int main(int argc, char * argv[])
 {
 
@@ -407,11 +428,6 @@ listener->Start();
 WriteServLog("rscriptd started successfully.");
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
 WriteServLog("rscriptd started successfully.");
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
-#ifndef NO_DAEMON
-string startFile(confDir + START_FILE);
-creat(startFile.c_str(), S_IRUSR);
-#endif
-
 while (nonstop.GetStatus())
     {
     usleep(100000);
 while (nonstop.GetStatus())
     {
     usleep(100000);
@@ -421,13 +437,7 @@ listener->Stop();
 
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
 
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
-int res = msgctl(msgID, IPC_RMID, NULL);
-if (res)
-    WriteServLog("Queue was not removed. id=%d", msgID);
-else
-    WriteServLog("Queue removed successfully.");
-
-KillExecuters();
+StopScriptExecuter(msgID);
 
 WriteServLog("rscriptd stopped successfully.");
 WriteServLog("---------------------------------------------");
 
 WriteServLog("rscriptd stopped successfully.");
 WriteServLog("---------------------------------------------");