From bdd372a64811c52e2924c6d391ce6801aabc8ada Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Wed, 27 Jul 2011 17:07:46 +0300 Subject: [PATCH] scriptexecuter become a plain C library --- include/stg/user_property.h | 2 +- projects/rscriptd/listener.cpp | 4 +-- projects/rscriptd/main.cpp | 4 +-- stglibs/scriptexecuter.lib/Makefile | 2 +- .../{scriptexecuter.cpp => scriptexecuter.c} | 35 ++++++++----------- stglibs/scriptexecuter.lib/scriptexecuter.h | 18 +++++++--- 6 files changed, 34 insertions(+), 31 deletions(-) rename stglibs/scriptexecuter.lib/{scriptexecuter.cpp => scriptexecuter.c} (74%) 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.cpp b/stglibs/scriptexecuter.lib/scriptexecuter.c similarity index 74% rename from stglibs/scriptexecuter.lib/scriptexecuter.cpp rename to stglibs/scriptexecuter.lib/scriptexecuter.c index abe0185c..021407dd 100644 --- a/stglibs/scriptexecuter.lib/scriptexecuter.cpp +++ b/stglibs/scriptexecuter.lib/scriptexecuter.c @@ -4,19 +4,17 @@ #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; +static int nonstop; //----------------------------------------------------------------------------- struct SCRIPT_DATA @@ -25,39 +23,34 @@ struct SCRIPT_DATA char script[MAX_SCRIPT_LEN]; } sd; //----------------------------------------------------------------------------- -static void CatchUSR1Executer(int) +static void CatchUSR1Executer() { -nonstop = false; +nonstop = 0; } //----------------------------------------------------------------------------- -int ScriptExec(const string & str) +int ScriptExec(const char * str) { -if (str.length() >= MAX_SCRIPT_LEN) - { - printfd(__FILE__, "ScriptExec() - script params exceeds MAX_SCRIPT_LENGTH (%d > %d)\n", str.length(), MAX_SCRIPT_LEN); +if (strlen(str) >= MAX_SCRIPT_LEN) return -1; - } -strncpy(sd.script, str.c_str(), MAX_SCRIPT_LEN); +strncpy(sd.script, 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) +void Executer(int msgID, pid_t pid, char * procName) #else -void Executer(int, int msgID, pid_t pid, char *) +void Executer(int msgID, pid_t pid) #endif { msgid = msgID; if (pid) return; -nonstop = true; +nonstop = 1; #ifdef LINUX memset(procName, 0, strlen(procName)); @@ -99,7 +92,7 @@ sigaction(SIGUSR1, &newsa, &oldsa); int ret; -SCRIPT_DATA sd; +struct SCRIPT_DATA sd; while (nonstop) { 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 -- 2.44.2