namespace
{
-std::set<pid_t> executersPid;
+std::set<pid_t> executers;
void StartTimer();
int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings);
}
}
-pid_t executerPid = fork();
+pid_t pid = fork();
-switch (executerPid)
+switch (pid)
{
case -1:
WriteServLog("Fork error!");
case 0:
delete settings;
#if defined(LINUX) || defined(DARWIN)
- Executer(*msgID, executerPid, procName);
+ Executer(*msgID, pid, procName);
#else
- Executer(*msgID, executerPid);
+ Executer(*msgID, pid);
#endif
return 1;
default:
- if (executersPid.empty()) {
+ if (executers.empty()) {
#if defined(LINUX) || defined(DARWIN)
- Executer(*msgID, executerPid, NULL);
+ Executer(*msgID, pid, NULL);
#else
- Executer(*msgID, executerPid);
+ Executer(*msgID, pid);
#endif
}
- executersPid.insert(executerPid);
+ executers.insert(pid);
}
return 0;
}
#endif
{
#ifndef NO_DAEMON
-pid_t childPid = fork();
+pid_t pid = fork();
std::string startFile = confDir + START_FILE;
unlink(startFile.c_str());
-switch (childPid)
+switch (pid)
{
case -1:
return -1;
//-----------------------------------------------------------------------------
void KillExecuters()
{
-std::set<pid_t>::iterator pid;
-pid = executersPid.begin();
-while (pid != executersPid.end())
+std::set<pid_t>::iterator pid(executers.begin());
+while (pid != executers.end())
{
printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
kill(*pid, SIGUSR1);
//-----------------------------------------------------------------------------
int main(int argc, char * argv[])
{
-SETTINGS_IMPL * settings = NULL;
int msgID = -11;
GetStgLogger().SetLogFileName("/var/log/stargazer.log");
return 1;
}
-if (argc == 2)
- settings = new SETTINGS_IMPL(argv[1]);
-else
- settings = new SETTINGS_IMPL();
+SETTINGS_IMPL settings(argc == 2 ? argv[1] : "");
-if (settings->ReadSettings())
+if (settings.ReadSettings())
{
STG_LOGGER & WriteServLog = GetStgLogger();
- if (settings->GetLogFileName() != "")
- WriteServLog.SetLogFileName(settings->GetLogFileName());
+ if (settings.GetLogFileName() != "")
+ WriteServLog.SetLogFileName(settings.GetLogFileName());
- WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
+ WriteServLog("ReadSettings error. %s", settings.GetStrError().c_str());
return -1;
}
#ifndef NO_DAEMON
-std::string startFile(settings->GetConfDir() + START_FILE);
+std::string startFile(settings.GetConfDir() + START_FILE);
#endif
-if (ForkAndWait(settings->GetConfDir()) < 0)
+if (ForkAndWait(settings.GetConfDir()) < 0)
{
STG_LOGGER & WriteServLog = GetStgLogger();
WriteServLog("Fork error!");
}
STG_LOGGER & WriteServLog = GetStgLogger();
-WriteServLog.SetLogFileName(settings->GetLogFileName());
+WriteServLog.SetLogFileName(settings.GetLogFileName());
WriteServLog("Stg v. %s", SERVER_VERSION);
-for (size_t i = 0; i < settings->GetExecutersNum(); i++)
+for (size_t i = 0; i < settings.GetExecutersNum(); i++)
{
- int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
+ int ret = StartScriptExecuter(argv[0], settings.GetExecMsgKey(), &msgID, &settings);
if (ret < 0)
{
STG_LOGGER & WriteServLog = GetStgLogger();
}
}
-PIDFile pidFile(settings->GetPIDFileName());
+PIDFile pidFile(settings.GetPIDFileName());
sigset_t signalSet;
sigfillset(&signalSet);
EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
-STORE_LOADER storeLoader(*settings);
+STORE_LOADER storeLoader(settings);
if (storeLoader.Load())
{
printfd(__FILE__, "Storage plugin: '%s'\n", storeLoader.GetStrError().c_str());
TARIFFS_IMPL tariffs(&store);
SERVICES_IMPL services(&store);
CORPORATIONS_IMPL corps(&store);
-USERS_IMPL users(settings, &store, &tariffs, services, admins.GetSysAdmin());
-TRAFFCOUNTER_IMPL traffCnt(&users, settings->GetRulesFileName());
-traffCnt.SetMonitorDir(settings->GetMonitorDir());
+USERS_IMPL users(&settings, &store, &tariffs, services, admins.GetSysAdmin());
+TRAFFCOUNTER_IMPL traffCnt(&users, settings.GetRulesFileName());
+traffCnt.SetMonitorDir(settings.GetMonitorDir());
if (users.Start())
return -1;
WriteServLog("Traffcounter started successfully.");
-STG::PluginManager manager(*settings, store, admins, tariffs, services, corps, users, traffCnt);
+STG::PluginManager manager(settings, store, admins, tariffs, services, corps, users, traffCnt);
srandom(static_cast<unsigned int>(stgTime));
creat(startFile.c_str(), S_IRUSR);
#endif
-while (true)
+bool running = true;
+while (running)
{
sigfillset(&signalSet);
int sig = 0;
sigwait(&signalSet, &sig);
- bool stop = false;
int status;
- pid_t childPid;
- std::set<pid_t>::iterator it;
switch (sig)
{
case SIGHUP:
manager.reload();
break;
case SIGTERM:
- stop = true;
+ running = false;
break;
case SIGINT:
- stop = true;
+ running = false;
break;
case SIGPIPE:
WriteServLog("Broken pipe!");
break;
case SIGCHLD:
- childPid = waitpid(-1, &status, WNOHANG);
-
- it = executersPid.find(childPid);
- if (it != executersPid.end())
- {
- executersPid.erase(it);
- if (executersPid.empty())
- stop = true;
- }
+ executers.erase(waitpid(-1, &status, WNOHANG));
+ if (executers.empty())
+ running = false;
break;
default:
WriteServLog("Ignore signal %d", sig);
break;
}
- if (stop)
- break;
}
WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
StopStgTimer();
WriteServLog("StgTimer: Stop successfull.");
-delete settings;
-
WriteServLog("Stg stopped successfully.");
WriteServLog("---------------------------------------------");
#include "stg/dotconfpp.h"
#include "settings_impl.h"
-//-----------------------------------------------------------------------------
-SETTINGS_IMPL::SETTINGS_IMPL()
- : SETTINGS(),
- strError(),
- modulesPath("/usr/lib/stg"),
- dirName(DIR_NUM),
- confDir("/etc/stargazer"),
- scriptsDir("/etc/stargazer"),
- rules("/etc/stargazer/rules"),
- logFile("/var/log/stargazer.log"),
- pidFile("/var/run/stargazer.pid"),
- monitorDir("/var/stargazer/monitoring"),
- monitoring(false),
- detailStatWritePeriod(dsPeriod_1_6),
- statWritePeriod(10),
- stgExecMsgKey(5555),
- executersNum(1),
- fullFee(false),
- dayFee(0),
- dayResetTraff(0),
- spreadFee(false),
- freeMbAllowInet(false),
- dayFeeIsLastDay(false),
- writeFreeMbTraffCost(false),
- showFeeInCash(true),
- messageTimeout(0),
- feeChargeType(0),
- reconnectOnTariffChange(false),
- modulesSettings(),
- storeModuleSettings(),
- logger(GetStgLogger())
-{
-}
//-----------------------------------------------------------------------------
SETTINGS_IMPL::SETTINGS_IMPL(const std::string & cd)
- : SETTINGS(),
- strError(),
- modulesPath("/usr/lib/stg"),
+ : modulesPath("/usr/lib/stg"),
dirName(DIR_NUM),
- confDir(cd),
- scriptsDir(cd),
- rules(cd + "/rules"),
+ confDir(cd.empty() ? "/etc/stargazer" : cd),
+ scriptsDir(confDir),
+ rules(confDir + "/rules"),
logFile("/var/log/stargazer.log"),
pidFile("/var/run/stargazer.pid"),
monitorDir("/var/stargazer/monitoring"),
messageTimeout(0),
feeChargeType(0),
reconnectOnTariffChange(false),
- modulesSettings(),
- storeModuleSettings(),
logger(GetStgLogger())
{
}