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>
 
  21 #include "store_loader.h"
 
  22 #include "plugin_mgr.h"
 
  23 #include "plugin_runner.h"
 
  24 #include "users_impl.h"
 
  25 #include "admins_impl.h"
 
  26 #include "tariffs_impl.h"
 
  27 #include "services_impl.h"
 
  28 #include "corps_impl.h"
 
  29 #include "traffcounter_impl.h"
 
  30 #include "settings_impl.h"
 
  32 #include "eventloop.h"
 
  33 #include "stg_timer.h"
 
  36 #include "stg/common.h"
 
  37 #include "stg/plugin.h"
 
  38 #include "stg/logger.h"
 
  39 #include "stg/scriptexecuter.h"
 
  40 #include "stg/version.h"
 
  48 #include <cstdlib> // srandom, exit
 
  53 #include <sys/types.h>
 
  55 #include <sys/stat.h> // S_IRUSR
 
  56 #include <fcntl.h> // create
 
  62 #define START_FILE "/._ST_ART_ED_"
 
  66 std::set<pid_t> executers;
 
  69 int StartScriptExecuter(char * procName, int msgKey, int * msgID);
 
  70 int ForkAndWait(const std::string & confDir);
 
  73 //-----------------------------------------------------------------------------
 
  76 STG_LOGGER & WriteServLog = GetStgLogger();
 
  80     WriteServLog("Cannot start timer. Fatal.");
 
  81     //printfd(__FILE__, "Cannot start timer. Fatal.\n");
 
  86     WriteServLog("Timer thread started successfully.");
 
  87     //printfd(__FILE__, "Timer thread started successfully.\n");
 
  90 //-----------------------------------------------------------------------------
 
  91 #if defined(LINUX) || defined(DARWIN)
 
  92 int StartScriptExecuter(char * procName, int msgKey, int * msgID)
 
  94 int StartScriptExecuter(char *, int msgKey, int * msgID)
 
  97 STG_LOGGER & WriteServLog = GetStgLogger();
 
  99 if (*msgID == -11)   // If msgID == -11 - first call. Create queue
 
 101     for (int i = 0; i < 2; i++)
 
 103         *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
 
 107             *msgID = msgget(msgKey, 0);
 
 110                 WriteServLog("Message queue not created.");
 
 115                 msgctl(*msgID, IPC_RMID, NULL);
 
 120             WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
 
 131         WriteServLog("Fork error!");
 
 135 #if defined(LINUX) || defined(DARWIN)
 
 136         Executer(*msgID, pid, procName);
 
 138         Executer(*msgID, pid);
 
 143         if (executers.empty()) {
 
 144 #if defined(LINUX) || defined(DARWIN)
 
 145             Executer(*msgID, pid, NULL);
 
 147             Executer(*msgID, pid);
 
 150         executers.insert(pid);
 
 154 //-----------------------------------------------------------------------------
 
 156 int ForkAndWait(const std::string & confDir)
 
 158 int ForkAndWait(const std::string &)
 
 163 std::string startFile = confDir + START_FILE;
 
 164 unlink(startFile.c_str());
 
 179         struct timespec ts = {0, 200000000};
 
 180         for (int i = 0; i < 120 * 5; i++)
 
 182             if (access(startFile.c_str(), F_OK) == 0)
 
 184                 unlink(startFile.c_str());
 
 188             nanosleep(&ts, NULL);
 
 190         unlink(startFile.c_str());
 
 197 //-----------------------------------------------------------------------------
 
 200 std::set<pid_t>::iterator pid(executers.begin());
 
 201 while (pid != executers.end())
 
 203     printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
 
 208 //-----------------------------------------------------------------------------
 
 209 } // namespace anonymous
 
 210 //-----------------------------------------------------------------------------
 
 211 int main(int argc, char * argv[])
 
 215 GetStgLogger().SetLogFileName("/var/log/stargazer.log");
 
 219     printf("You must be root. Exit.\n");
 
 223 SETTINGS_IMPL settings(argc == 2 ? argv[1] : "");
 
 225 if (settings.ReadSettings())
 
 227     STG_LOGGER & WriteServLog = GetStgLogger();
 
 229     if (settings.GetLogFileName() != "")
 
 230         WriteServLog.SetLogFileName(settings.GetLogFileName());
 
 232     WriteServLog("ReadSettings error. %s", settings.GetStrError().c_str());
 
 237 std::string startFile(settings.GetConfDir() + START_FILE);
 
 240 if (ForkAndWait(settings.GetConfDir()) < 0)
 
 242     STG_LOGGER & WriteServLog = GetStgLogger();
 
 243     WriteServLog("Fork error!");
 
 247 STG_LOGGER & WriteServLog = GetStgLogger();
 
 248 WriteServLog.SetLogFileName(settings.GetLogFileName());
 
 249 WriteServLog("Stg v. %s", SERVER_VERSION);
 
 251 for (size_t i = 0; i < settings.GetExecutersNum(); i++)
 
 253     int ret = StartScriptExecuter(argv[0], settings.GetExecMsgKey(), &msgID);
 
 256         STG_LOGGER & WriteServLog = GetStgLogger();
 
 257         WriteServLog("Start Script Executer error!");
 
 267 PIDFile pidFile(settings.GetPIDFileName());
 
 270 sigfillset(&signalSet);
 
 271 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
 
 275 if (!IsStgTimerRunning())
 
 277     printfd(__FILE__, "Timer thread not started in 1 sec!\n");
 
 278     WriteServLog("Timer thread not started in 1 sec!");
 
 282 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
 
 284 STORE_LOADER storeLoader(settings);
 
 285 if (storeLoader.Load())
 
 287     printfd(__FILE__, "Storage plugin: '%s'\n", storeLoader.GetStrError().c_str());
 
 288     WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
 
 294     printfd(__FILE__, "Event loop not started.\n");
 
 295     WriteServLog("Event loop not started.");
 
 299 STORE & store(storeLoader.GetStore());
 
 300 WriteServLog("Storage plugin: %s. Loading successfull.", store.GetVersion().c_str());
 
 302 ADMINS_IMPL admins(&store);
 
 303 TARIFFS_IMPL tariffs(&store);
 
 304 SERVICES_IMPL services(&store);
 
 305 CORPORATIONS_IMPL corps(&store);
 
 306 USERS_IMPL users(&settings, &store, &tariffs, services, admins.GetSysAdmin());
 
 307 TRAFFCOUNTER_IMPL traffCnt(&users, settings.GetRulesFileName());
 
 308 traffCnt.SetMonitorDir(settings.GetMonitorDir());
 
 313 WriteServLog("Users started successfully.");
 
 315 if (traffCnt.Start())
 
 318 WriteServLog("Traffcounter started successfully.");
 
 320 STG::PluginManager manager(settings, store, admins, tariffs, services, corps, users, traffCnt);
 
 322 srandom(static_cast<unsigned int>(stgTime));
 
 324 WriteServLog("Stg started successfully.");
 
 325 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
 328 creat(startFile.c_str(), S_IRUSR);
 
 334     sigfillset(&signalSet);
 
 336     sigwait(&signalSet, &sig);
 
 342             SETTINGS_IMPL newSettings(settings);
 
 343             if (newSettings.ReadSettings())
 
 345                 STG_LOGGER & WriteServLog = GetStgLogger();
 
 347                 if (newSettings.GetLogFileName() != "")
 
 348                     WriteServLog.SetLogFileName(newSettings.GetLogFileName());
 
 350                 WriteServLog("ReadSettings error. %s", newSettings.GetStrError().c_str());
 
 353             settings = newSettings;
 
 355             manager.reload(settings);
 
 365             WriteServLog("Broken pipe!");
 
 368             executers.erase(waitpid(-1, &status, WNOHANG));
 
 369             if (executers.empty())
 
 373             WriteServLog("Ignore signal %d", sig);
 
 378 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
 383     WriteServLog("Event loop not stopped.");
 
 385 if (!traffCnt.Stop())
 
 386     WriteServLog("Traffcounter: Stop successfull.");
 
 389     WriteServLog("Users: Stop successfull.");
 
 392 int res = msgctl(msgID, IPC_RMID, NULL);
 
 394     WriteServLog("Queue was not removed. id=%d", msgID);
 
 396     WriteServLog("Queue removed successfully.");
 
 401 WriteServLog("StgTimer: Stop successfull.");
 
 403 WriteServLog("Stg stopped successfully.");
 
 404 WriteServLog("---------------------------------------------");
 
 408 //-----------------------------------------------------------------------------