From: Maxim Mamontov Date: Fri, 16 Sep 2011 19:34:12 +0000 (+0300) Subject: Do not use pthread_kill to stop a thread X-Git-Tag: 2.408-rc1~31 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/a622b081808494796d3679acaebbf5ee364fe9de?ds=inline;hp=6c6628047db7319e630e6a8e660c5207ea83748e Do not use pthread_kill to stop a thread --- diff --git a/projects/rscriptd/main.cpp b/projects/rscriptd/main.cpp index 2739758c..93e1d605 100644 --- a/projects/rscriptd/main.cpp +++ b/projects/rscriptd/main.cpp @@ -57,143 +57,9 @@ using namespace std; #define START_FILE "/._ST_ART_ED_" -static bool childExited = false; set 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::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(); diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 7e83b72d..80ec465b 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -72,7 +72,6 @@ using namespace std; #define START_FILE "/._ST_ART_ED_" set 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); diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 1eac0ab1..a6d2073a 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -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; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp index 3cd6b901..ea71a117 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp @@ -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; } diff --git a/projects/stargazer/plugins/other/ping/ping.cpp b/projects/stargazer/plugins/other/ping/ping.cpp index f4755c23..f0868e12 100644 --- a/projects/stargazer/plugins/other/ping/ping.cpp +++ b/projects/stargazer/plugins/other/ping/ping.cpp @@ -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; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index c827330f..18c18816 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -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; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index 17598006..c9fcc549 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -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; } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 5367e249..fcc0a9c2 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -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; } diff --git a/projects/stargazer/traffcounter_impl.cpp b/projects/stargazer/traffcounter_impl.cpp index a02e3b6d..da64a0b8 100644 --- a/projects/stargazer/traffcounter_impl.cpp +++ b/projects/stargazer/traffcounter_impl.cpp @@ -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; diff --git a/stglibs/pinger.lib/pinger.cpp b/stglibs/pinger.lib/pinger.cpp index 8e6370ed..af761ffa 100644 --- a/stglibs/pinger.lib/pinger.cpp +++ b/stglibs/pinger.lib/pinger.cpp @@ -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; } //-----------------------------------------------------------------------------