From: Maxim Mamontov Date: Wed, 27 Jul 2011 14:07:46 +0000 (+0300) Subject: scriptexecuter become a plain C library X-Git-Tag: 2.408-alpha~62 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/bdd372a64811c52e2924c6d391ce6801aabc8ada scriptexecuter become a plain C library --- diff --git a/include/stg/user_property.h b/include/stg/user_property.h index f0a6d18e..ade21f4e 100644 --- a/include/stg/user_property.h +++ b/include/stg/user_property.h @@ -363,7 +363,7 @@ std::string filePath = scriptsDir + "/OnChange"; if (access(filePath.c_str(), X_OK) == 0) { std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\""); - ScriptExec(execString); + ScriptExec(execString.c_str()); } else { diff --git a/projects/rscriptd/listener.cpp b/projects/rscriptd/listener.cpp index e9aacc19..39e590b6 100644 --- a/projects/rscriptd/listener.cpp +++ b/projects/rscriptd/listener.cpp @@ -410,7 +410,7 @@ bool LISTENER::Connect(const UserData & data) const printfd(__FILE__, "Connect %s\n", data.login.c_str()); if (access(scriptOnConnect.c_str(), X_OK) == 0) { - if (ScriptExec(scriptOnConnect + " " + data.params)) + if (ScriptExec((scriptOnConnect + " " + data.params).c_str())) { WriteServLog("Script %s cannot be executed for an unknown reason.", scriptOnConnect.c_str()); return true; @@ -429,7 +429,7 @@ bool LISTENER::Disconnect(const UserData & data) const printfd(__FILE__, "Disconnect %s\n", data.login.c_str()); if (access(scriptOnDisconnect.c_str(), X_OK) == 0) { - if (ScriptExec(scriptOnDisconnect + " " + data.params)) + if (ScriptExec((scriptOnDisconnect + " " + data.params).c_str())) { WriteServLog("Script %s cannot be executed for an unknown reson.", scriptOnDisconnect.c_str()); return true; diff --git a/projects/rscriptd/main.cpp b/projects/rscriptd/main.cpp index 344a2611..7e7f7f35 100644 --- a/projects/rscriptd/main.cpp +++ b/projects/rscriptd/main.cpp @@ -251,12 +251,12 @@ switch (executerPid) //close(1); //close(2); //setsid(); - Executer(msgKey, *msgID, executerPid, procName); + Executer(*msgID, executerPid, procName); return 1; default: // Parent if (executersPid.empty()) - Executer(msgKey, *msgID, executerPid, NULL); + Executer(*msgID, executerPid, NULL); executersPid.insert(executerPid); } return 0; diff --git a/stglibs/scriptexecuter.lib/Makefile b/stglibs/scriptexecuter.lib/Makefile index 146d4af9..92538b12 100644 --- a/stglibs/scriptexecuter.lib/Makefile +++ b/stglibs/scriptexecuter.lib/Makefile @@ -4,7 +4,7 @@ LIB_NAME = stgscriptexecuter -SRCS = scriptexecuter.cpp +SRCS = scriptexecuter.c INCS = scriptexecuter.h diff --git a/stglibs/scriptexecuter.lib/scriptexecuter.c b/stglibs/scriptexecuter.lib/scriptexecuter.c new file mode 100644 index 00000000..021407dd --- /dev/null +++ b/stglibs/scriptexecuter.lib/scriptexecuter.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "scriptexecuter.h" + + +#define MAX_SCRIPT_LEN (1100) + +static int msgid; +static int nonstop; + +//----------------------------------------------------------------------------- +struct SCRIPT_DATA +{ + long mtype; + char script[MAX_SCRIPT_LEN]; +} sd; +//----------------------------------------------------------------------------- +static void CatchUSR1Executer() +{ +nonstop = 0; +} +//----------------------------------------------------------------------------- +int ScriptExec(const char * str) +{ +if (strlen(str) >= MAX_SCRIPT_LEN) + return -1; + +strncpy(sd.script, str, MAX_SCRIPT_LEN); +sd.mtype = 1; +if (msgsnd(msgid, (void *)&sd, MAX_SCRIPT_LEN, 0) < 0) + return -1; + +return 0; +} +//----------------------------------------------------------------------------- +#ifdef LINUX +void Executer(int msgID, pid_t pid, char * procName) +#else +void Executer(int msgID, pid_t pid) +#endif +{ +msgid = msgID; +if (pid) + return; +nonstop = 1; + +#ifdef LINUX +memset(procName, 0, strlen(procName)); +strcpy(procName, "stg-exec"); +#else +setproctitle("stg-exec"); +#endif + +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); + +sigemptyset(&sigmask); +sigaddset(&sigmask, SIGHUP); +newsa.sa_handler = SIG_IGN; +newsa.sa_mask = sigmask; +newsa.sa_flags = 0; +sigaction(SIGHUP, &newsa, &oldsa); + +sigemptyset(&sigmask); +sigaddset(&sigmask, SIGUSR1); +newsa.sa_handler = CatchUSR1Executer; +newsa.sa_mask = sigmask; +newsa.sa_flags = 0; +sigaction(SIGUSR1, &newsa, &oldsa); + +int ret; + +struct SCRIPT_DATA sd; + +while (nonstop) + { + sd.mtype = 1; + ret = msgrcv(msgid, &sd, MAX_SCRIPT_LEN, 0, 0); + + if (ret < 0) + { + usleep(20000); + continue; + } + int ret = system(sd.script); + if (ret == -1) + { + // Fork failed + } + } +} +//----------------------------------------------------------------------------- diff --git a/stglibs/scriptexecuter.lib/scriptexecuter.cpp b/stglibs/scriptexecuter.lib/scriptexecuter.cpp deleted file mode 100644 index abe0185c..00000000 --- a/stglibs/scriptexecuter.lib/scriptexecuter.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "stg/common.h" -#include "scriptexecuter.h" - -using namespace std; - -#define MAX_SCRIPT_LEN (1100) - -static int msgid; -static bool nonstop; - -//----------------------------------------------------------------------------- -struct SCRIPT_DATA -{ - long mtype; - char script[MAX_SCRIPT_LEN]; -} sd; -//----------------------------------------------------------------------------- -static void CatchUSR1Executer(int) -{ -nonstop = false; -} -//----------------------------------------------------------------------------- -int ScriptExec(const string & str) -{ -if (str.length() >= MAX_SCRIPT_LEN) - { - printfd(__FILE__, "ScriptExec() - script params exceeds MAX_SCRIPT_LENGTH (%d > %d)\n", str.length(), MAX_SCRIPT_LEN); - return -1; - } - -strncpy(sd.script, str.c_str(), MAX_SCRIPT_LEN); -sd.mtype = 1; -if (msgsnd(msgid, (void *)&sd, MAX_SCRIPT_LEN, 0) < 0) - { - printfd(__FILE__, "ScriptExec() - failed to send message to the IPC queue: '%s'\n", strerror(errno)); - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- -#ifdef LINUX -void Executer(int, int msgID, pid_t pid, char * procName) -#else -void Executer(int, int msgID, pid_t pid, char *) -#endif -{ -msgid = msgID; -if (pid) - return; -nonstop = true; - -#ifdef LINUX -memset(procName, 0, strlen(procName)); -strcpy(procName, "stg-exec"); -#else -setproctitle("stg-exec"); -#endif - -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); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGHUP); -newsa.sa_handler = SIG_IGN; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGHUP, &newsa, &oldsa); - -sigemptyset(&sigmask); -sigaddset(&sigmask, SIGUSR1); -newsa.sa_handler = CatchUSR1Executer; -newsa.sa_mask = sigmask; -newsa.sa_flags = 0; -sigaction(SIGUSR1, &newsa, &oldsa); - -int ret; - -SCRIPT_DATA sd; - -while (nonstop) - { - sd.mtype = 1; - ret = msgrcv(msgid, &sd, MAX_SCRIPT_LEN, 0, 0); - - if (ret < 0) - { - usleep(20000); - continue; - } - int ret = system(sd.script); - if (ret == -1) - { - // Fork failed - } - } -} -//----------------------------------------------------------------------------- diff --git a/stglibs/scriptexecuter.lib/scriptexecuter.h b/stglibs/scriptexecuter.lib/scriptexecuter.h index d0db1dad..41b02a2f 100644 --- a/stglibs/scriptexecuter.lib/scriptexecuter.h +++ b/stglibs/scriptexecuter.lib/scriptexecuter.h @@ -1,9 +1,19 @@ #ifndef SCRIPT_EXECUTER_H #define SCRIPT_EXECUTER_H -#include +#ifdef __cplusplus +extern "C" { +#endif -int ScriptExec(const std::string & str); -void Executer(int msgKey, int msgID, pid_t pid, char * procName); +int ScriptExec(const char * str); +#ifdef LINUX +void Executer(int msgID, pid_t pid, char * procName); +#else +void Executer(int msgID, pid_t pid); +#endif -#endif //SCRIPT_EXECUTER_H +#ifdef __cplusplus +} +#endif + +#endif