]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/rscript/rscript.cpp
Allow to start mod_rscript without subnets file
[stg.git] / projects / stargazer / plugins / other / rscript / rscript.cpp
index a772e5076cf1523fdfd13b53479812e63b55fbb0..9e560a5d6062394baa90c3ef19b7dd24a5e8e64c 100644 (file)
 
 #include <csignal>
 #include <cassert>
+#include <cstdlib>
 #include <algorithm>
 
+#include "stg/common.h"
+#include "stg/locker.h"
+#include "stg/user_property.h"
+#include "stg/plugin_creator.h"
+#include "stg/logger.h"
 #include "rscript.h"
-#include "common.h"
 #include "ur_functor.h"
 #include "send_functor.h"
-#include "stg_locker.h"
-#include "user_property.h"
 
 extern volatile const time_t stgTime;
 
@@ -45,30 +48,7 @@ extern volatile const time_t stgTime;
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-class RS_CREATOR
-{
-private:
-    REMOTE_SCRIPT * rs;
-
-public:
-    RS_CREATOR()
-        : rs(new REMOTE_SCRIPT())
-        {
-        };
-    ~RS_CREATOR()
-        {
-        delete rs;
-        };
-
-    REMOTE_SCRIPT * GetPlugin()
-        {
-        return rs;
-        };
-};
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-RS_CREATOR rsc;
+PLUGIN_CREATOR<REMOTE_SCRIPT> rsc;
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -79,41 +59,26 @@ return rsc.GetPlugin();
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-RS_USER::RS_USER()
-    : lastSentTime(0),
-      shortPacketsCount(0)
-{
-}
-//-----------------------------------------------------------------------------
-RS_USER::RS_USER(const std::vector<uint32_t> & r, USER_PTR it)
-    : lastSentTime(0),
-      user(it),
-      routers(r),
-      shortPacketsCount(0)
+RS_USER & RS_USER::operator=(const RS_USER & rvalue)
 {
+lastSentTime = rvalue.lastSentTime;
+user = rvalue.user;
+routers = rvalue.routers;
+shortPacketsCount = rvalue.shortPacketsCount;
+return *this;
 }
 //-----------------------------------------------------------------------------
 RS_SETTINGS::RS_SETTINGS()
     : sendPeriod(0),
-      port(0)
+      port(0),
+      errorStr(),
+      netRouters(),
+      userParams(),
+      password(),
+      subnetFile()
 {
 }
 //-----------------------------------------------------------------------------
-int RS_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
-{
-if (str2x(str.c_str(), *val))
-    {
-    errorStr = "Incorrect value \'" + str + "\'.";
-    return -1;
-    }
-if (*val < min || *val > max)
-    {
-    errorStr = "Value \'" + str + "\' out of range.";
-    return -1;
-    }
-return 0;
-}
-//-----------------------------------------------------------------------------
 int RS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
 {
 int p;
@@ -185,19 +150,13 @@ subnetFile = pvi->value[0];
 
 NRMapParser nrMapParser;
 
-if (nrMapParser.ReadFile(subnetFile))
+if (!nrMapParser.ReadFile(subnetFile))
     {
-    errorStr = nrMapParser.GetErrorStr();
-    return -1;
+    netRouters = nrMapParser.GetMap();
     }
-
-netRouters = nrMapParser.GetMap();
-
-if (netRouters.empty())
+else
     {
-    errorStr = "Parameter(s) \'Subnet*\' not found.";
-    printfd(__FILE__, "Parameter(s) 'Subnet*' not found\n");
-    return -1;
+    GetStgLogger()("mod_rscript: error opening subnets file '%s'", subnetFile.c_str());
     }
 
 return 0;
@@ -206,11 +165,20 @@ return 0;
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 REMOTE_SCRIPT::REMOTE_SCRIPT()
-    : sendPeriod(15),
+    : ctx(),
+      afterChgIPNotifierList(),
+      authorizedUsers(),
+      errorStr(),
+      rsSettings(),
+      settings(),
+      sendPeriod(15),
       halfPeriod(8),
       nonstop(false),
       isRunning(false),
       users(NULL),
+      netRouters(),
+      thread(),
+      mutex(),
       sock(0),
       onAddUserNotifier(*this),
       onDelUserNotifier(*this)
@@ -225,6 +193,10 @@ pthread_mutex_destroy(&mutex);
 //-----------------------------------------------------------------------------
 void * REMOTE_SCRIPT::Run(void * d)
 {
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
 REMOTE_SCRIPT * rs = static_cast<REMOTE_SCRIPT *>(d);
 
 rs->isRunning = true;
@@ -309,25 +281,17 @@ if (isRunning)
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && isRunning; i++)
         {
-        usleep(200000);
-        }
-
-    //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");
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
     }
 
 users->DelNotifierUserDel(&onDelUserNotifier);
 users->DelNotifierUserAdd(&onAddUserNotifier);
 
+if (isRunning)
+    return -1;
+
 return 0;
 }
 //-----------------------------------------------------------------------------
@@ -392,7 +356,11 @@ while (it != authorizedUsers.end())
     }
 }
 //-----------------------------------------------------------------------------
+#ifdef NDEBUG
+bool REMOTE_SCRIPT::PreparePacket(char * buf, size_t, uint32_t ip, RS_USER & rsu, bool forceDisconnect) const
+#else
 bool REMOTE_SCRIPT::PreparePacket(char * buf, size_t bufSize, uint32_t ip, RS_USER & rsu, bool forceDisconnect) const
+#endif
 {
 RS_PACKET_HEADER packetHead;