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 //-----------------------------------------------------------------------------
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!");
155 Executer(*msgID, executerPid, procName);
157 Executer(*msgID, executerPid);
162 if (executersPid.empty()) {
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());
274 /*************************************************************************************************/
275 printfd(__FILE__, "--- Script params dump ---\n");
276 std::vector<std::string>::const_iterator it(settings->GetScriptParams().begin());
277 while (it != settings->GetScriptParams().end())
279 printfd(__FILE__, "%s\n", it->c_str());
282 printfd(__FILE__, "--- End dump ---\n");
283 /*************************************************************************************************/
285 std::string startFile(settings->GetConfDir() + START_FILE);
288 if (ForkAndWait(settings->GetConfDir()) < 0)
290 STG_LOGGER & WriteServLog = GetStgLogger();
291 WriteServLog("Fork error!");
295 STG_LOGGER & WriteServLog = GetStgLogger();
296 WriteServLog.SetLogFileName(settings->GetLogFileName());
297 WriteServLog("Stg v. %s", SERVER_VERSION);
299 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
301 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
304 STG_LOGGER & WriteServLog = GetStgLogger();
305 WriteServLog("Start Script Executer error!");
315 PIDFile pidFile(settings->GetPIDFileName());
318 sigfillset(&signalSet);
319 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
323 if (!IsStgTimerRunning())
325 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
326 WriteServLog("Timer thread not started in 1 sec!");
329 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
331 STORE_LOADER storeLoader(*settings);
332 if (storeLoader.Load())
334 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
335 goto exitLblNotStarted;
340 WriteServLog("Event loop not started.");
341 goto exitLblNotStarted;
344 dataStore = storeLoader.GetStore();
345 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
347 tariffs = new TARIFFS_IMPL(dataStore);
348 admins = new ADMINS_IMPL(dataStore);
349 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
350 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
351 services = new SERVICES_IMPL(dataStore);
352 corps = new CORPORATIONS_IMPL(dataStore);
353 traffCnt->SetMonitorDir(settings->GetMonitorDir());
355 modSettings = settings->GetModulesSettings();
357 for (size_t i = 0; i < modSettings.size(); i++)
359 std::string modulePath = settings->GetModulesPath();
360 modulePath += "/mod_";
361 modulePath += modSettings[i].moduleName;
363 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
365 PLUGIN_RUNNER(modulePath,
378 modIter = modules.begin();
380 while (modIter != modules.end())
384 WriteServLog("Error loading module '%s': %s",
385 modIter->GetPlugin()->GetVersion().c_str(),
386 modIter->GetStrError().c_str());
387 goto exitLblNotStarted;
394 goto exitLblNotStarted;
396 WriteServLog("Users started successfully.");
398 if (traffCnt->Start())
400 goto exitLblNotStarted;
402 WriteServLog("Traffcounter started successfully.");
404 //Sort by start order
405 modules.sort(StartModCmp);
406 modIter = modules.begin();
408 while (modIter != modules.end())
410 if (modIter->Start())
412 WriteServLog("Error starting module '%s': %s",
413 modIter->GetPlugin()->GetVersion().c_str(),
414 modIter->GetStrError().c_str());
417 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
421 srandom(static_cast<unsigned int>(stgTime));
423 WriteServLog("Stg started successfully.");
424 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
427 creat(startFile.c_str(), S_IRUSR);
432 sigfillset(&signalSet);
434 sigwait(&signalSet, &sig);
438 std::set<pid_t>::iterator it;
443 modIter = modules.begin();
444 for (; modIter != modules.end(); ++modIter)
446 if (modIter->Reload())
448 WriteServLog("Error reloading module '%s': '%s'", modIter->GetPlugin()->GetVersion().c_str(),
449 modIter->GetStrError().c_str());
450 printfd(__FILE__, "Error reloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(),
451 modIter->GetStrError().c_str());
462 WriteServLog("Broken pipe!");
465 childPid = waitpid(-1, &status, WNOHANG);
467 it = executersPid.find(childPid);
468 if (it != executersPid.end())
470 executersPid.erase(it);
471 if (executersPid.empty())
476 WriteServLog("Ignore signel %d", sig);
485 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
487 //Sort by start order
488 modules.sort(StopModCmp);
489 modIter = modules.begin();
490 while (modIter != modules.end())
492 std::string name = modIter->GetFileName();
493 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
496 WriteServLog("Error stopping module '%s': %s",
497 modIter->GetPlugin()->GetVersion().c_str(),
498 modIter->GetStrError().c_str());
499 printfd(__FILE__, "Error stopping module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
503 WriteServLog("Module: '%s'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
510 WriteServLog("Event loop not stopped.");
515 modIter = modules.begin();
516 while (modIter != modules.end())
518 std::string name = modIter->GetFileName();
519 if (modIter->IsRunning())
521 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
525 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
526 if (modIter->Unload())
528 WriteServLog("Error unloading module '%s': '%s'",
529 modIter->GetPlugin()->GetVersion().c_str(),
530 modIter->GetStrError().c_str());
531 printfd(__FILE__, "Error unloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
540 WriteServLog("Traffcounter: Stop successfull.");
546 WriteServLog("Users: Stop successfull.");
550 int res = msgctl(msgID, IPC_RMID, NULL);
552 WriteServLog("Queue was not removed. id=%d", msgID);
554 WriteServLog("Queue removed successfully.");
559 WriteServLog("StgTimer: Stop successfull.");
569 WriteServLog("Stg stopped successfully.");
570 WriteServLog("---------------------------------------------");
574 //-----------------------------------------------------------------------------