2  *    This program is free software; you can redistribute it and/or modify
 
   3  *    it under the terms of the GNU General Public License as published by
 
   4  *    the Free Software Foundation; either version 2 of the License, or
 
   5  *    (at your option) any later version.
 
   7  *    This program is distributed in the hope that it will be useful,
 
   8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  10  *    GNU General Public License for more details.
 
  12  *    You should have received a copy of the GNU General Public License
 
  13  *    along with this program; if not, write to the Free Software
 
  14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
 
  23  $Date: 2010/10/04 20:19:12 $
 
  30 #include <sys/types.h>
 
  32 #include <sys/stat.h> // S_IRUSR
 
  33 #include <fcntl.h> // create
 
  38 #include <cstdlib> // srandom, exit
 
  45 #include "stg/common.h"
 
  46 #include "stg/plugin.h"
 
  47 #include "stg/logger.h"
 
  48 #include "stg/scriptexecuter.h"
 
  49 #include "stg/version.h"
 
  50 #include "stg_timer.h"
 
  51 #include "settings_impl.h"
 
  52 #include "users_impl.h"
 
  53 #include "admins_impl.h"
 
  54 #include "tariffs_impl.h"
 
  55 #include "services_impl.h"
 
  56 #include "corps_impl.h"
 
  57 #include "traffcounter_impl.h"
 
  58 #include "plugin_runner.h"
 
  59 #include "store_loader.h"
 
  61 #include "eventloop.h"
 
  66     #define MAIN_DEBUG (1)
 
  70 #define START_FILE "/._ST_ART_ED_"
 
  72 set<pid_t> executersPid;
 
  74 //-----------------------------------------------------------------------------
 
  75 bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
 
  77 return lhs.GetStartPosition() < rhs.GetStartPosition();
 
  79 //-----------------------------------------------------------------------------
 
  80 bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
 
  82 return lhs.GetStopPosition() > rhs.GetStopPosition();
 
  84 //-----------------------------------------------------------------------------
 
  85 static void StartTimer()
 
  87 STG_LOGGER & WriteServLog = GetStgLogger();
 
  91     WriteServLog("Cannot start timer. Fatal.");
 
  92     //printfd(__FILE__, "Cannot start timer. Fatal.\n");
 
  97     WriteServLog("Timer thread started successfully.");
 
  98     //printfd(__FILE__, "Timer thread started successfully.\n");
 
 101 //-----------------------------------------------------------------------------
 
 103 int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings)
 
 105 int StartScriptExecuter(char *, int msgKey, int * msgID, SETTINGS_IMPL * settings)
 
 108 STG_LOGGER & WriteServLog = GetStgLogger();
 
 110 if (*msgID == -11)   // If msgID == -11 - first call. Create queue
 
 112     for (int i = 0; i < 2; i++)
 
 114         *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
 
 118             *msgID = msgget(msgKey, 0);
 
 121                 WriteServLog("Message queue not created.");
 
 126                 msgctl(*msgID, IPC_RMID, NULL);
 
 131             WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
 
 137 pid_t executerPid = fork();
 
 142         WriteServLog("Fork error!");
 
 148         Executer(*msgID, executerPid, procName);
 
 150         Executer(*msgID, executerPid);
 
 155         if (executersPid.empty()) {
 
 157             Executer(*msgID, executerPid, NULL);
 
 159             Executer(*msgID, executerPid);
 
 162         executersPid.insert(executerPid);
 
 166 //-----------------------------------------------------------------------------
 
 168 int ForkAndWait(const string & confDir)
 
 170 int ForkAndWait(const string &)
 
 174 pid_t childPid = fork();
 
 175 string startFile = confDir + START_FILE;
 
 176 unlink(startFile.c_str());
 
 191         struct timespec ts = {0, 200000000};
 
 192         for (int i = 0; i < 120 * 5; i++)
 
 194             if (access(startFile.c_str(), F_OK) == 0)
 
 196                 unlink(startFile.c_str());
 
 200             nanosleep(&ts, NULL);
 
 202         unlink(startFile.c_str());
 
 209 //-----------------------------------------------------------------------------
 
 212 set<pid_t>::iterator pid;
 
 213 pid = executersPid.begin();
 
 214 while (pid != executersPid.end())
 
 216     printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
 
 221 //-----------------------------------------------------------------------------
 
 222 int main(int argc, char * argv[])
 
 224 SETTINGS_IMPL * settings = NULL;
 
 225 STORE * dataStore = NULL;
 
 226 TARIFFS_IMPL * tariffs = NULL;
 
 227 ADMINS_IMPL * admins = NULL;
 
 228 USERS_IMPL * users = NULL;
 
 229 TRAFFCOUNTER_IMPL * traffCnt = NULL;
 
 230 SERVICES_IMPL * services = NULL;
 
 231 CORPORATIONS_IMPL * corps = NULL;
 
 235     STG_LOGGER & WriteServLog = GetStgLogger();
 
 236     WriteServLog.SetLogFileName("/var/log/stargazer.log");
 
 239 vector<MODULE_SETTINGS> modSettings;
 
 240 list<PLUGIN_RUNNER> modules;
 
 242 list<PLUGIN_RUNNER>::iterator modIter;
 
 246     printf("You must be root. Exit.\n");
 
 251     settings = new SETTINGS_IMPL(argv[1]);
 
 253     settings = new SETTINGS_IMPL();
 
 255 if (settings->ReadSettings())
 
 257     STG_LOGGER & WriteServLog = GetStgLogger();
 
 259     if (settings->GetLogFileName() != "")
 
 260         WriteServLog.SetLogFileName(settings->GetLogFileName());
 
 262     WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
 
 267 string startFile(settings->GetConfDir() + START_FILE);
 
 270 if (ForkAndWait(settings->GetConfDir()) < 0)
 
 272     STG_LOGGER & WriteServLog = GetStgLogger();
 
 273     WriteServLog("Fork error!");
 
 277 STG_LOGGER & WriteServLog = GetStgLogger();
 
 278 WriteServLog.SetLogFileName(settings->GetLogFileName());
 
 279 WriteServLog("Stg v. %s", SERVER_VERSION);
 
 281 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
 
 283     int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
 
 286         STG_LOGGER & WriteServLog = GetStgLogger();
 
 287         WriteServLog("Start Script Executer error!");
 
 297 PIDFile pidFile(settings->GetPIDFileName());
 
 300 sigfillset(&signalSet);
 
 301 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
 
 305 if (!IsStgTimerRunning())
 
 307     printfd(__FILE__, "Timer thread not started in 1 sec!\n");
 
 308     WriteServLog("Timer thread not started in 1 sec!");
 
 311 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
 
 313 STORE_LOADER storeLoader(*settings);
 
 314 if (storeLoader.Load())
 
 316     WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
 
 317     goto exitLblNotStarted;
 
 322     WriteServLog("Event loop not started.");
 
 323     goto exitLblNotStarted;
 
 326 dataStore = storeLoader.GetStore();
 
 327 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
 
 329 tariffs = new TARIFFS_IMPL(dataStore);
 
 330 admins = new ADMINS_IMPL(dataStore);
 
 331 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
 
 332 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
 
 333 services = new SERVICES_IMPL(dataStore);
 
 334 corps = new CORPORATIONS_IMPL(dataStore);
 
 335 traffCnt->SetMonitorDir(settings->GetMonitorDir());
 
 337 modSettings = settings->GetModulesSettings();
 
 339 for (size_t i = 0; i < modSettings.size(); i++)
 
 341     string modulePath = settings->GetModulesPath();
 
 342     modulePath += "/mod_";
 
 343     modulePath += modSettings[i].moduleName;
 
 345     printfd(__FILE__, "Module: %s\n", modulePath.c_str());
 
 347         PLUGIN_RUNNER(modulePath,
 
 360 modIter = modules.begin();
 
 362 while (modIter != modules.end())
 
 366         WriteServLog("Error loading module '%s': %s",
 
 367                      modIter->GetPlugin()->GetVersion().c_str(),
 
 368                      modIter->GetStrError().c_str());
 
 369         goto exitLblNotStarted;
 
 376     goto exitLblNotStarted;
 
 378 WriteServLog("Users started successfully.");
 
 380 if (traffCnt->Start())
 
 382     goto exitLblNotStarted;
 
 384 WriteServLog("Traffcounter started successfully.");
 
 386 //Sort by start order
 
 387 modules.sort(StartModCmp);
 
 388 modIter = modules.begin();
 
 390 while (modIter != modules.end())
 
 392     if (modIter->Start())
 
 394         WriteServLog("Error starting module '%s': %s",
 
 395                      modIter->GetPlugin()->GetVersion().c_str(),
 
 396                      modIter->GetStrError().c_str());
 
 399     WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
 
 405 WriteServLog("Stg started successfully.");
 
 406 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
 409 creat(startFile.c_str(), S_IRUSR);
 
 414     sigfillset(&signalSet);
 
 416     sigwait(&signalSet, &sig);
 
 420     set<pid_t>::iterator it;
 
 425             modIter = modules.begin();
 
 426             for (; modIter != modules.end(); ++modIter)
 
 428                 if (modIter->Reload())
 
 430                     WriteServLog("Error reloading module '%s': '%s'", modIter->GetPlugin()->GetVersion().c_str(),
 
 431                                                               modIter->GetStrError().c_str());
 
 432                     printfd(__FILE__, "Error reloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(),
 
 433                                                                      modIter->GetStrError().c_str());
 
 444             WriteServLog("Broken pipe!");
 
 447             childPid = waitpid(-1, &status, WNOHANG);
 
 449             it = executersPid.find(childPid);
 
 450             if (it != executersPid.end())
 
 452                 executersPid.erase(it);
 
 453                 if (executersPid.empty())
 
 458             WriteServLog("Ignore signel %d", sig);
 
 467 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
 469 //Sort by start order
 
 470 modules.sort(StopModCmp);
 
 471 modIter = modules.begin();
 
 472 while (modIter != modules.end())
 
 474     std::string name = modIter->GetFileName();
 
 475     printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
 
 478         WriteServLog("Error stopping module '%s': %s",
 
 479                      modIter->GetPlugin()->GetVersion().c_str(),
 
 480                      modIter->GetStrError().c_str());
 
 481         printfd(__FILE__, "Error stopping module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
 
 485         WriteServLog("Module: '%s'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
 
 492     WriteServLog("Event loop not stopped.");
 
 497 modIter = modules.begin();
 
 498 while (modIter != modules.end())
 
 500     std::string name = modIter->GetFileName();
 
 501     if (modIter->IsRunning())
 
 503         printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
 
 507         printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
 
 508         if (modIter->Unload())
 
 510             WriteServLog("Error unloading module '%s': '%s'",
 
 511                          modIter->GetPlugin()->GetVersion().c_str(),
 
 512                          modIter->GetStrError().c_str());
 
 513             printfd(__FILE__, "Error unloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
 
 522     WriteServLog("Traffcounter: Stop successfull.");
 
 528     WriteServLog("Users: Stop successfull.");
 
 532 int res = msgctl(msgID, IPC_RMID, NULL);
 
 534     WriteServLog("Queue was not removed. id=%d", msgID);
 
 536     WriteServLog("Queue removed successfully.");
 
 541 WriteServLog("StgTimer: Stop successfull.");
 
 551 WriteServLog("Stg stopped successfully.");
 
 552 WriteServLog("---------------------------------------------");
 
 556 //-----------------------------------------------------------------------------