]> git.stg.codes - stg.git/commitdiff
Do not use pthread_kill to stop a thread
authorMaxim Mamontov <faust.madf@gmail.com>
Fri, 16 Sep 2011 19:34:12 +0000 (22:34 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Fri, 16 Sep 2011 19:34:12 +0000 (22:34 +0300)
projects/rscriptd/main.cpp
projects/stargazer/main.cpp
projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
projects/stargazer/plugins/other/ping/ping.cpp
projects/stargazer/plugins/other/radius/radius.cpp
projects/stargazer/plugins/other/rscript/rscript.cpp
projects/stargazer/plugins/other/smux/smux.cpp
projects/stargazer/traffcounter_impl.cpp
stglibs/pinger.lib/pinger.cpp

index 2739758ce400cb9f49cd62d1287736888fdf8614..93e1d6050c416e2ac90684c01e2f8da65d2ac2b7 100644 (file)
@@ -57,143 +57,9 @@ using namespace std;
 
 #define START_FILE "/._ST_ART_ED_"
 
-static bool childExited = false;
 set<pid_t> executersPid;
-static pid_t stgChildPid;
 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)
-{
-int status;
-pid_t childPid;
-childPid = waitpid(-1, &status, WNOHANG);
-
-set<pid_t>::iterator pid;
-pid = executersPid.find(childPid);
-if (pid != executersPid.end())
-    {
-    executersPid.erase(pid);
-    }
-if (childPid == stgChildPid)
-    {
-    childExited = true;
-    }
-}
-//-----------------------------------------------------------------------------
-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);
-}
 //-----------------------------------------------------------------------------
 void KillExecuters()
 {
@@ -307,9 +173,9 @@ int ForkAndWait(const string & confDir)
 #endif
 {
 #ifndef NO_DAEMON
-stgChildPid = fork();
+pid_t childPid = fork();
 
-switch (stgChildPid)
+switch (childPid)
     {
     case -1:    // Failure
         return -1;
@@ -332,15 +198,6 @@ return 0;
 //-----------------------------------------------------------------------------
 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;
@@ -385,7 +242,6 @@ cfg->ReadInt("UserTimeout", &userTimeout, 60);
 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
 
-SetSignalHandlers();
 if (ForkAndWait(confDir) < 0)
     {
     STG_LOGGER & WriteServLog = GetStgLogger();
@@ -428,10 +284,32 @@ listener->Start();
 WriteServLog("rscriptd started successfully.");
 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
-while (nonstop.GetStatus())
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
+while (true)
     {
-    struct timespec ts = {0, 100000000};
-    nanosleep(&ts, NULL);
+    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();
index 7e83b72dce6cf0489cdacd13e25e0f2b33bcbe26..80ec465b709bbeef47a85ae96553c0f53ba80f5b 100644 (file)
@@ -72,7 +72,6 @@ using namespace std;
 #define START_FILE "/._ST_ART_ED_"
 
 set<pid_t> executersPid;
-static pid_t stgChildPid;
 
 //-----------------------------------------------------------------------------
 bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
@@ -394,9 +393,12 @@ WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 creat(startFile.c_str(), S_IRUSR);
 #endif
 
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
 while (true)
     {
-    sigset_t signalSet;
     sigfillset(&signalSet);
     int sig = 0;
     sigwait(&signalSet, &sig);
index 1eac0ab1f7fc462bfe72556aec021aaecff17593..a6d2073abe709b4457f186f0e6bf4d2b3d58d25a 100644 (file)
@@ -431,31 +431,6 @@ if (isRunningRun)
         struct timespec ts = {0, 200000000};
         nanosleep(&ts, NULL);
         }
-
-    //after 5 seconds waiting thread still running. now killing it
-    if (isRunningRun)
-        {
-        if (pthread_kill(recvThread, SIGINT))
-            {
-            errorStr = "Cannot kill thread.";
-            printfd(__FILE__, "Cannot kill thread\n");
-            return -1;
-            }
-        for (int i = 0; i < 25 && isRunningRun; ++i)
-            {
-            struct timespec ts = {0, 200000000};
-            nanosleep(&ts, NULL);
-            }
-        if (isRunningRun)
-            {
-            printfd(__FILE__, "Failed to stop recv thread\n");
-            }
-        else
-            {
-            pthread_join(recvThread, NULL);
-            }
-        printfd(__FILE__, "AUTH_IA killed Run\n");
-        }
     }
 
 FinalizeNet();
@@ -468,33 +443,14 @@ if (isRunningRunTimeouter)
         struct timespec ts = {0, 200000000};
         nanosleep(&ts, NULL);
         }
-
-    //after 5 seconds waiting thread still running. now killing it
-    if (isRunningRunTimeouter)
-        {
-        if (pthread_kill(timeouterThread, SIGINT))
-            {
-            errorStr = "Cannot kill thread.";
-            return -1;
-            }
-        for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
-            {
-            struct timespec ts = {0, 200000000};
-            nanosleep(&ts, NULL);
-            }
-        if (isRunningRunTimeouter)
-            {
-            printfd(__FILE__, "Failed to stop timeouter thread\n");
-            }
-        else
-            {
-            pthread_join(timeouterThread, NULL);
-            }
-        printfd(__FILE__, "AUTH_IA killed Timeouter\n");
-        }
     }
-printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
+
 users->DelNotifierUserDel(&onDelUserNotifier);
+
+if (isRunningRun || isRunningRunTimeouter)
+    return -1;
+
+printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
 return 0;
 }
 //-----------------------------------------------------------------------------
index 3cd6b9010f5ddd46ca5af4bcfe0d814b9271f039..ea71a117b7bb925c1e753d00cf3665d5aea1c962 100644 (file)
@@ -127,18 +127,8 @@ for (i = 0; i < 25; i++)
     nanosleep(&ts, NULL);
     }
 
-//after 5 seconds waiting thread still running. now killing it
 if (isRunning)
-    {
-    //TODO pthread_cancel()
-    if (pthread_kill(thread, SIGINT))
-        {
-        errorStr = "Cannot kill thread.";
-        printfd(__FILE__, "Cannot kill thread\n");
-        return -1;
-        }
-    printfd(__FILE__, "STG_CONFIG killed\n");
-    }
+    return -1;
 
 return 0;
 }
index f4755c230491f73cbec8e7f8c63e22b4ea9bc45f..f0868e1225d21bd5a333094c412e9e4856d75861 100644 (file)
@@ -133,19 +133,6 @@ for (int i = 0; i < 25; i++)
     nanosleep(&ts, NULL);
     }
 
-//after 5 seconds waiting thread still running. now kill it
-if (isRunning)
-    {
-    printfd(__FILE__, "kill PING thread.\n");
-    if (pthread_kill(thread, SIGINT))
-        {
-        errorStr = "Cannot kill PING thread.";
-        printfd(__FILE__, "Cannot kill PING thread.\n");
-        return -1;
-        }
-    printfd(__FILE__, "PING killed\n");
-    }
-
 users->DelNotifierUserAdd(&onAddUserNotifier);
 users->DelNotifierUserDel(&onDelUserNotifier);
 
@@ -157,6 +144,9 @@ while (users_iter != usersList.end())
     ++users_iter;
     }
 
+if (isRunning)
+    return -1;
+
 return 0;
 }
 //-----------------------------------------------------------------------------
index c827330f0a7db7cd3791ef2a65d84cea6edd7327..18c1881670bc78827ec462b8b036930cac96a627 100644 (file)
@@ -239,20 +239,11 @@ if (isRunning)
         struct timespec ts = {0, 200000000};
         nanosleep(&ts, NULL);
         }
-
-    //after 5 seconds waiting thread still running. now killing it
-    if (isRunning)
-        {
-        if (pthread_kill(thread, SIGINT))
-            {
-            errorStr = "Cannot kill thread.";
-            printfd(__FILE__, "Cannot kill thread\n");
-            return -1;
-            }
-        printfd(__FILE__, "RADIUS::Stop killed Run\n");
-        }
     }
 
+if (isRunning)
+    return -1;
+
 return 0;
 }
 //-----------------------------------------------------------------------------
index 175980066128a43085a2bcdbee9f8caa16c84a76..c9fcc549b84f31a83f688f63ce6ec4f84fd1f7e6 100644 (file)
@@ -289,23 +289,14 @@ if (isRunning)
         struct timespec ts = {0, 200000000};
         nanosleep(&ts, NULL);
         }
-
-    //after 5 seconds waiting thread still running. now killing it
-    if (isRunning)
-        {
-        if (pthread_kill(thread, SIGINT))
-            {
-            errorStr = "Cannot kill thread.";
-            printfd(__FILE__, "Cannot kill thread\n");
-            return -1;
-            }
-        printfd(__FILE__, "REMOTE_SCRIPT killed Run\n");
-        }
     }
 
 users->DelNotifierUserDel(&onDelUserNotifier);
 users->DelNotifierUserAdd(&onAddUserNotifier);
 
+if (isRunning)
+    return -1;
+
 return 0;
 }
 //-----------------------------------------------------------------------------
index 5367e249b00c739fb548d772f852ae9f6789d8f0..fcc0a9c2349e5afbe070072f3ee9a85beb73f001 100644 (file)
@@ -224,22 +224,10 @@ if (!stopped)
         struct timespec ts = {0, 200000000};
         nanosleep(&ts, NULL);
         }
-
-    //after 5 seconds waiting thread still running. now killing it
-    if (!stopped)
-        {
-        printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
-        if (pthread_kill(thread, SIGINT))
-            {
-            errorStr = "Cannot kill thread.";
-            printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
-            return -1;
-            }
-        printfd(__FILE__, "SMUX::Stop() -  killed Run\n");
-        }
     }
 
-pthread_join(thread, NULL);
+if (stopped)
+    pthread_join(thread, NULL);
 
 ResetNotifiers();
 
@@ -259,6 +247,12 @@ sensors.erase(sensors.begin(), sensors.end());
 
 close(sock);
 
+if (!stopped)
+    {
+    running = true;
+    return -1;
+    }
+
 printfd(__FILE__, "SMUX::Stop() - After\n");
 return 0;
 }
index a02e3b6d520f2c0350cbd9eeec2b200a71088896..da64a0b8bb2ceb68ee7c26e63f5eb52989eeee28 100644 (file)
@@ -152,16 +152,9 @@ for (int i = 0; i < 25 && !stopped; i++)
     nanosleep(&ts, NULL);
     }
 
-//after 5 seconds waiting thread still running. now kill it
 if (!stopped)
-    {
-    printfd(__FILE__, "kill TRAFFCOUNTER thread.\n");
-    if (pthread_kill(thread, SIGINT))
-        {
-        return -1;
-        }
-    printfd(__FILE__, "TRAFFCOUNTER killed\n");
-    }
+    return -1;
+
 printfd(__FILE__, "TRAFFCOUNTER::Stop()\n");
 
 return 0;
index 8e6370ed4774c257bb9aea21cc78208025bedfbd..af761ffa8b73df2f511f68911507a128bf135a29 100644 (file)
@@ -98,16 +98,6 @@ if (isRunningRecver)
         struct timespec ts = {0, 200000000};
         nanosleep(&ts, NULL);
         }
-
-    //after 5 seconds waiting thread still running. now killing it
-    if (isRunningRecver)
-        {
-        if (pthread_kill(recvThread, SIGINT))
-            {
-            errorStr = "Cannot kill thread.";
-            return -1;
-            }
-        }
     }
 
 if (isRunningSender)
@@ -121,19 +111,13 @@ if (isRunningSender)
         struct timespec ts = {0, 200000000};
         nanosleep(&ts, NULL);
         }
-
-    //after 5 seconds waiting thread still running. now killing it
-    if (isRunningSender)
-        {
-        if (pthread_kill(sendThread, SIGINT))
-            {
-            errorStr = "Cannot kill thread.";
-            return -1;
-            }
-        }
     }
 
 close(sendSocket);
+
+if (isRunningSender || isRunningRecver)
+    return -1;
+
 return 0;
 }
 //-----------------------------------------------------------------------------