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();
267 if (settings->GetLogFileName() != "")
268 WriteServLog.SetLogFileName(settings->GetLogFileName());
269 WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
272 /*************************************************************************************************/
273 printfd(__FILE__, "--- Script params dump ---\n");
274 std::vector<std::string>::const_iterator it(settings->GetScriptParams().begin());
275 while (it != settings->GetScriptParams().end())
277 printfd(__FILE__, "%s\n", it->c_str());
280 printfd(__FILE__, "--- End dump ---\n");
281 /*************************************************************************************************/
283 std::string startFile(settings->GetConfDir() + START_FILE);
286 if (ForkAndWait(settings->GetConfDir()) < 0)
288 STG_LOGGER & WriteServLog = GetStgLogger();
289 WriteServLog("Fork error!");
293 STG_LOGGER & WriteServLog = GetStgLogger();
294 WriteServLog.SetLogFileName(settings->GetLogFileName());
295 WriteServLog("Stg v. %s", SERVER_VERSION);
297 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
299 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
302 STG_LOGGER & WriteServLog = GetStgLogger();
303 WriteServLog("Start Script Executer error!");
313 PIDFile pidFile(settings->GetPIDFileName());
316 sigfillset(&signalSet);
317 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
321 if (!IsStgTimerRunning())
323 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
324 WriteServLog("Timer thread not started in 1 sec!");
327 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
329 STORE_LOADER storeLoader(*settings);
330 if (storeLoader.Load())
332 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
333 goto exitLblNotStarted;
338 WriteServLog("Event loop not started.");
339 goto exitLblNotStarted;
342 dataStore = storeLoader.GetStore();
343 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
345 tariffs = new TARIFFS_IMPL(dataStore);
346 admins = new ADMINS_IMPL(dataStore);
347 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
348 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
349 services = new SERVICES_IMPL(dataStore);
350 corps = new CORPORATIONS_IMPL(dataStore);
351 traffCnt->SetMonitorDir(settings->GetMonitorDir());
353 modSettings = settings->GetModulesSettings();
355 for (size_t i = 0; i < modSettings.size(); i++)
357 std::string modulePath = settings->GetModulesPath();
358 modulePath += "/mod_";
359 modulePath += modSettings[i].moduleName;
361 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
363 PLUGIN_RUNNER(modulePath,
376 modIter = modules.begin();
378 while (modIter != modules.end())
382 WriteServLog("Error loading module '%s': %s",
383 modIter->GetPlugin()->GetVersion().c_str(),
384 modIter->GetStrError().c_str());
385 goto exitLblNotStarted;
392 goto exitLblNotStarted;
394 WriteServLog("Users started successfully.");
396 if (traffCnt->Start())
398 goto exitLblNotStarted;
400 WriteServLog("Traffcounter started successfully.");
402 //Sort by start order
403 modules.sort(StartModCmp);
404 modIter = modules.begin();
406 while (modIter != modules.end())
408 if (modIter->Start())
410 WriteServLog("Error starting module '%s': %s",
411 modIter->GetPlugin()->GetVersion().c_str(),
412 modIter->GetStrError().c_str());
415 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
419 srandom(static_cast<unsigned int>(stgTime));
421 WriteServLog("Stg started successfully.");
422 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
425 creat(startFile.c_str(), S_IRUSR);
430 sigfillset(&signalSet);
432 sigwait(&signalSet, &sig);
436 std::set<pid_t>::iterator it;
441 modIter = modules.begin();
442 for (; modIter != modules.end(); ++modIter)
444 if (modIter->Reload())
446 WriteServLog("Error reloading module '%s': '%s'", modIter->GetPlugin()->GetVersion().c_str(),
447 modIter->GetStrError().c_str());
448 printfd(__FILE__, "Error reloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(),
449 modIter->GetStrError().c_str());
460 WriteServLog("Broken pipe!");
463 childPid = waitpid(-1, &status, WNOHANG);
465 it = executersPid.find(childPid);
466 if (it != executersPid.end())
468 executersPid.erase(it);
469 if (executersPid.empty())
474 WriteServLog("Ignore signel %d", sig);
483 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
485 //Sort by start order
486 modules.sort(StopModCmp);
487 modIter = modules.begin();
488 while (modIter != modules.end())
490 std::string name = modIter->GetFileName();
491 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
494 WriteServLog("Error stopping module '%s': %s",
495 modIter->GetPlugin()->GetVersion().c_str(),
496 modIter->GetStrError().c_str());
497 printfd(__FILE__, "Error stopping module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
501 WriteServLog("Module: '%s'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
508 WriteServLog("Event loop not stopped.");
513 modIter = modules.begin();
514 while (modIter != modules.end())
516 std::string name = modIter->GetFileName();
517 if (modIter->IsRunning())
519 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
523 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
524 if (modIter->Unload())
526 WriteServLog("Error unloading module '%s': '%s'",
527 modIter->GetPlugin()->GetVersion().c_str(),
528 modIter->GetStrError().c_str());
529 printfd(__FILE__, "Error unloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
538 WriteServLog("Traffcounter: Stop successfull.");
544 WriteServLog("Users: Stop successfull.");
548 int res = msgctl(msgID, IPC_RMID, NULL);
550 WriteServLog("Queue was not removed. id=%d", msgID);
552 WriteServLog("Queue removed successfully.");
557 WriteServLog("StgTimer: Stop successfull.");
567 WriteServLog("Stg stopped successfully.");
568 WriteServLog("---------------------------------------------");
572 //-----------------------------------------------------------------------------