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"
64 #define MAIN_DEBUG (1)
68 #define START_FILE "/._ST_ART_ED_"
72 std::set<pid_t> executersPid;
74 bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs);
75 bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs);
77 int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings);
78 int ForkAndWait(const std::string & confDir);
81 //-----------------------------------------------------------------------------
82 bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
84 return lhs.GetStartPosition() < rhs.GetStartPosition();
86 //-----------------------------------------------------------------------------
87 bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
89 return lhs.GetStopPosition() > rhs.GetStopPosition();
91 //-----------------------------------------------------------------------------
94 STG_LOGGER & WriteServLog = GetStgLogger();
98 WriteServLog("Cannot start timer. Fatal.");
99 //printfd(__FILE__, "Cannot start timer. Fatal.\n");
104 WriteServLog("Timer thread started successfully.");
105 //printfd(__FILE__, "Timer thread started successfully.\n");
108 //-----------------------------------------------------------------------------
109 #if defined(LINUX) || defined(DARWIN)
110 int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings)
112 int StartScriptExecuter(char *, int msgKey, int * msgID, SETTINGS_IMPL * settings)
115 STG_LOGGER & WriteServLog = GetStgLogger();
117 if (*msgID == -11) // If msgID == -11 - first call. Create queue
119 for (int i = 0; i < 2; i++)
121 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
125 *msgID = msgget(msgKey, 0);
128 WriteServLog("Message queue not created.");
133 msgctl(*msgID, IPC_RMID, NULL);
138 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
144 pid_t executerPid = fork();
149 WriteServLog("Fork error!");
154 #if defined(LINUX) || defined(DARWIN)
155 Executer(*msgID, executerPid, procName);
157 Executer(*msgID, executerPid);
162 if (executersPid.empty()) {
163 #if defined(LINUX) || defined(DARWIN)
164 Executer(*msgID, executerPid, NULL);
166 Executer(*msgID, executerPid);
169 executersPid.insert(executerPid);
173 //-----------------------------------------------------------------------------
175 int ForkAndWait(const std::string & confDir)
177 int ForkAndWait(const std::string &)
181 pid_t childPid = fork();
182 std::string startFile = confDir + START_FILE;
183 unlink(startFile.c_str());
198 struct timespec ts = {0, 200000000};
199 for (int i = 0; i < 120 * 5; i++)
201 if (access(startFile.c_str(), F_OK) == 0)
203 unlink(startFile.c_str());
207 nanosleep(&ts, NULL);
209 unlink(startFile.c_str());
216 //-----------------------------------------------------------------------------
219 std::set<pid_t>::iterator pid;
220 pid = executersPid.begin();
221 while (pid != executersPid.end())
223 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
228 //-----------------------------------------------------------------------------
229 } // namespace anonymous
230 //-----------------------------------------------------------------------------
231 int main(int argc, char * argv[])
233 SETTINGS_IMPL * settings = NULL;
234 STORE * dataStore = NULL;
235 TARIFFS_IMPL * tariffs = NULL;
236 ADMINS_IMPL * admins = NULL;
237 USERS_IMPL * users = NULL;
238 TRAFFCOUNTER_IMPL * traffCnt = NULL;
239 SERVICES_IMPL * services = NULL;
240 CORPORATIONS_IMPL * corps = NULL;
244 STG_LOGGER & WriteServLog = GetStgLogger();
245 WriteServLog.SetLogFileName("/var/log/stargazer.log");
248 std::vector<MODULE_SETTINGS> modSettings;
249 std::list<PLUGIN_RUNNER> modules;
251 std::list<PLUGIN_RUNNER>::iterator modIter;
255 printf("You must be root. Exit.\n");
260 settings = new SETTINGS_IMPL(argv[1]);
262 settings = new SETTINGS_IMPL();
264 if (settings->ReadSettings())
266 STG_LOGGER & WriteServLog = GetStgLogger();
268 if (settings->GetLogFileName() != "")
269 WriteServLog.SetLogFileName(settings->GetLogFileName());
271 WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
276 std::string startFile(settings->GetConfDir() + START_FILE);
279 if (ForkAndWait(settings->GetConfDir()) < 0)
281 STG_LOGGER & WriteServLog = GetStgLogger();
282 WriteServLog("Fork error!");
286 STG_LOGGER & WriteServLog = GetStgLogger();
287 WriteServLog.SetLogFileName(settings->GetLogFileName());
288 WriteServLog("Stg v. %s", SERVER_VERSION);
290 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
292 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
295 STG_LOGGER & WriteServLog = GetStgLogger();
296 WriteServLog("Start Script Executer error!");
306 PIDFile pidFile(settings->GetPIDFileName());
309 sigfillset(&signalSet);
310 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
314 if (!IsStgTimerRunning())
316 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
317 WriteServLog("Timer thread not started in 1 sec!");
320 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
322 STORE_LOADER storeLoader(*settings);
323 if (storeLoader.Load())
325 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
326 goto exitLblNotStarted;
331 WriteServLog("Event loop not started.");
332 goto exitLblNotStarted;
335 dataStore = storeLoader.GetStore();
336 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
338 tariffs = new TARIFFS_IMPL(dataStore);
339 admins = new ADMINS_IMPL(dataStore);
340 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
341 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
342 services = new SERVICES_IMPL(dataStore);
343 corps = new CORPORATIONS_IMPL(dataStore);
344 traffCnt->SetMonitorDir(settings->GetMonitorDir());
346 modSettings = settings->GetModulesSettings();
348 for (size_t i = 0; i < modSettings.size(); i++)
350 std::string modulePath = settings->GetModulesPath();
351 modulePath += "/mod_";
352 modulePath += modSettings[i].moduleName;
354 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
356 PLUGIN_RUNNER(modulePath,
369 modIter = modules.begin();
371 while (modIter != modules.end())
375 WriteServLog("Error loading module '%s': %s",
376 modIter->GetPlugin()->GetVersion().c_str(),
377 modIter->GetStrError().c_str());
378 goto exitLblNotStarted;
385 goto exitLblNotStarted;
387 WriteServLog("Users started successfully.");
389 if (traffCnt->Start())
391 goto exitLblNotStarted;
393 WriteServLog("Traffcounter started successfully.");
395 //Sort by start order
396 modules.sort(StartModCmp);
397 modIter = modules.begin();
399 while (modIter != modules.end())
401 if (modIter->Start())
403 WriteServLog("Error starting module '%s': %s",
404 modIter->GetPlugin()->GetVersion().c_str(),
405 modIter->GetStrError().c_str());
408 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
412 srandom(static_cast<unsigned int>(stgTime));
414 WriteServLog("Stg started successfully.");
415 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
418 creat(startFile.c_str(), S_IRUSR);
423 sigfillset(&signalSet);
425 sigwait(&signalSet, &sig);
429 std::set<pid_t>::iterator it;
434 modIter = modules.begin();
435 for (; modIter != modules.end(); ++modIter)
437 if (modIter->Reload())
439 WriteServLog("Error reloading module '%s': '%s'", modIter->GetPlugin()->GetVersion().c_str(),
440 modIter->GetStrError().c_str());
441 printfd(__FILE__, "Error reloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(),
442 modIter->GetStrError().c_str());
453 WriteServLog("Broken pipe!");
456 childPid = waitpid(-1, &status, WNOHANG);
458 it = executersPid.find(childPid);
459 if (it != executersPid.end())
461 executersPid.erase(it);
462 if (executersPid.empty())
467 WriteServLog("Ignore signel %d", sig);
476 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
478 //Sort by start order
479 modules.sort(StopModCmp);
480 modIter = modules.begin();
481 while (modIter != modules.end())
483 std::string name = modIter->GetFileName();
484 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
487 WriteServLog("Error stopping module '%s': %s",
488 modIter->GetPlugin()->GetVersion().c_str(),
489 modIter->GetStrError().c_str());
490 printfd(__FILE__, "Error stopping module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
494 WriteServLog("Module: '%s'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
501 WriteServLog("Event loop not stopped.");
506 modIter = modules.begin();
507 while (modIter != modules.end())
509 std::string name = modIter->GetFileName();
510 if (modIter->IsRunning())
512 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
516 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
517 if (modIter->Unload())
519 WriteServLog("Error unloading module '%s': '%s'",
520 modIter->GetPlugin()->GetVersion().c_str(),
521 modIter->GetStrError().c_str());
522 printfd(__FILE__, "Error unloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
531 WriteServLog("Traffcounter: Stop successfull.");
537 WriteServLog("Users: Stop successfull.");
541 int res = msgctl(msgID, IPC_RMID, NULL);
543 WriteServLog("Queue was not removed. id=%d", msgID);
545 WriteServLog("Queue removed successfully.");
550 WriteServLog("StgTimer: Stop successfull.");
560 WriteServLog("Stg stopped successfully.");
561 WriteServLog("---------------------------------------------");
565 //-----------------------------------------------------------------------------