]> git.stg.codes - stg.git/blobdiff - projects/rscriptd/main.cpp
TUT framework updated to svn version
[stg.git] / projects / rscriptd / main.cpp
index 8cf9384dc8319e2ea69e8a9c9000a1ec6a657e25..93e1d6050c416e2ac90684c01e2f8da65d2ac2b7 100644 (file)
@@ -36,6 +36,8 @@
 #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"
@@ -55,144 +57,22 @@ using namespace std;
 
 #define START_FILE "/._ST_ART_ED_"
 
 
 #define START_FILE "/._ST_ART_ED_"
 
-static bool childExited = false;
 set<pid_t> executersPid;
 set<pid_t> executersPid;
-static pid_t stgChildPid;
 volatile time_t stgTime = time(NULL);
 
 volatile time_t stgTime = time(NULL);
 
-class STG_STOPPER
-{
-public:
-    STG_STOPPER() { nonstop = true; }
-    bool GetStatus() const { return nonstop; };
-    void Stop(const char * __file__, int __line__)
-        {
-        #ifdef NO_DAEMON
-        printfd(__FILE__, "rscriptd stopped at %s:%d\n", __file__, __line__);
-        #endif
-        nonstop = false;
-        }
-private:
-    bool nonstop;
-};
-//-----------------------------------------------------------------------------
-STG_STOPPER nonstop;
-//-----------------------------------------------------------------------------
-void CatchPROF(int)
-{
-}
-//-----------------------------------------------------------------------------
-void CatchUSR1(int)
-{
-}
-//-----------------------------------------------------------------------------
-void CatchTERM(int sig)
-{
-/*
- *Function Name:CatchINT
- *Parameters: sig_num - signal number
- *Description: INT signal handler
- *Returns: Nothing
- */
-STG_LOGGER & WriteServLog = GetStgLogger();
-WriteServLog("Shutting down... %d", sig);
-
-nonstop.Stop(__FILE__, __LINE__);
-
-struct sigaction newsa, oldsa;
-sigset_t sigmask;
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGTERM);
-newsa.sa_handler = SIG_IGN;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGTERM, &newsa, &oldsa);
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGINT);
-newsa.sa_handler = SIG_IGN;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGINT, &newsa, &oldsa);
-}
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void CatchPIPE(int)
-{
-STG_LOGGER & WriteServLog = GetStgLogger();
-WriteServLog("Broken pipe!");
-}
-//-----------------------------------------------------------------------------
-void CatchHUP(int)
-{
-}
-//-----------------------------------------------------------------------------
-void CatchCHLD(int)
+void KillExecuters()
 {
 {
-int status;
-pid_t childPid;
-childPid = waitpid(-1, &status, WNOHANG);
-
 set<pid_t>::iterator pid;
 set<pid_t>::iterator pid;
-pid = executersPid.find(childPid);
-if (pid != executersPid.end())
-    {
-    executersPid.erase(pid);
-    }
-if (childPid == stgChildPid)
+pid = executersPid.begin();
+while (pid != executersPid.end())
     {
     {
-    childExited = true;
+    printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
+    kill(*pid, SIGUSR1);
+    ++pid;
     }
 }
 //-----------------------------------------------------------------------------
     }
 }
 //-----------------------------------------------------------------------------
-void SetSignalHandlers()
-{
-struct sigaction newsa, oldsa;
-sigset_t sigmask;
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGTERM);
-newsa.sa_handler = CatchTERM;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGTERM, &newsa, &oldsa);
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGUSR1);
-newsa.sa_handler = CatchUSR1;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGUSR1, &newsa, &oldsa);
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGINT);
-newsa.sa_handler = CatchTERM;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGINT, &newsa, &oldsa);
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGPIPE);
-newsa.sa_handler = CatchPIPE;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGPIPE, &newsa, &oldsa);
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGHUP);
-newsa.sa_handler = CatchHUP;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGHUP, &newsa, &oldsa);
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGCHLD);
-newsa.sa_handler = CatchCHLD;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGCHLD, &newsa, &oldsa);
-}
-//-----------------------------------------------------------------------------
 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 +117,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
@@ -255,9 +173,9 @@ int ForkAndWait(const string & confDir)
 #endif
 {
 #ifndef NO_DAEMON
 #endif
 {
 #ifndef NO_DAEMON
-stgChildPid = fork();
+pid_t childPid = fork();
 
 
-switch (stgChildPid)
+switch (childPid)
     {
     case -1:    // Failure
         return -1;
     {
     case -1:    // Failure
         return -1;
@@ -278,29 +196,8 @@ 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[])
 {
-
-/*
-  Initialization order:
-  - Logger
-  - Config
-  - Set signal nandlers
-  - Fork and exit
- */
-
 CONFIGFILE * cfg = NULL;
 LISTENER * listener = NULL;
 int msgID = -11;
 CONFIGFILE * cfg = NULL;
 LISTENER * listener = NULL;
 int msgID = -11;
@@ -345,7 +242,6 @@ cfg->ReadInt("UserTimeout", &userTimeout, 60);
 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
 
 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
 
-SetSignalHandlers();
 if (ForkAndWait(confDir) < 0)
     {
     STG_LOGGER & WriteServLog = GetStgLogger();
 if (ForkAndWait(confDir) < 0)
     {
     STG_LOGGER & WriteServLog = GetStgLogger();
@@ -388,22 +284,39 @@ listener->Start();
 WriteServLog("rscriptd started successfully.");
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
 WriteServLog("rscriptd started successfully.");
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
-while (nonstop.GetStatus())
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
+while (true)
     {
     {
-    usleep(100000);
+    sigfillset(&signalSet);
+    int sig = 0;
+    printfd(__FILE__, "Before sigwait\n");
+    sigwait(&signalSet, &sig);
+    printfd(__FILE__, "After sigwait. Signal: %d\n", sig);
+    bool stop = false;
+    switch (sig)
+        {
+        case SIGTERM:
+            stop = true;
+            break;
+        case SIGINT:
+            stop = true;
+            break;
+        default:
+            WriteServLog("Ignore signel %d", sig);
+            break;
+        }
+    if (stop)
+        break;
     }
 
 listener->Stop();
 
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
     }
 
 listener->Stop();
 
 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("---------------------------------------------");