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());
275 std::string startFile(settings->GetConfDir() + START_FILE);
278 if (ForkAndWait(settings->GetConfDir()) < 0)
280 STG_LOGGER & WriteServLog = GetStgLogger();
281 WriteServLog("Fork error!");
285 STG_LOGGER & WriteServLog = GetStgLogger();
286 WriteServLog.SetLogFileName(settings->GetLogFileName());
287 WriteServLog("Stg v. %s", SERVER_VERSION);
289 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
291 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
294 STG_LOGGER & WriteServLog = GetStgLogger();
295 WriteServLog("Start Script Executer error!");
305 PIDFile pidFile(settings->GetPIDFileName());
308 sigfillset(&signalSet);
309 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
313 if (!IsStgTimerRunning())
315 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
316 WriteServLog("Timer thread not started in 1 sec!");
319 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
321 STORE_LOADER storeLoader(*settings);
322 if (storeLoader.Load())
324 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
325 goto exitLblNotStarted;
330 WriteServLog("Event loop not started.");
331 goto exitLblNotStarted;
334 dataStore = storeLoader.GetStore();
335 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
337 tariffs = new TARIFFS_IMPL(dataStore);
338 admins = new ADMINS_IMPL(dataStore);
339 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
340 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
341 services = new SERVICES_IMPL(dataStore);
342 corps = new CORPORATIONS_IMPL(dataStore);
343 traffCnt->SetMonitorDir(settings->GetMonitorDir());
345 modSettings = settings->GetModulesSettings();
347 for (size_t i = 0; i < modSettings.size(); i++)
349 std::string modulePath = settings->GetModulesPath();
350 modulePath += "/mod_";
351 modulePath += modSettings[i].moduleName;
353 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
355 PLUGIN_RUNNER(modulePath,
368 modIter = modules.begin();
370 while (modIter != modules.end())
374 WriteServLog("Error loading module '%s': %s",
375 modIter->GetPlugin()->GetVersion().c_str(),
376 modIter->GetStrError().c_str());
377 goto exitLblNotStarted;
384 goto exitLblNotStarted;
386 WriteServLog("Users started successfully.");
388 if (traffCnt->Start())
390 goto exitLblNotStarted;
392 WriteServLog("Traffcounter started successfully.");
394 //Sort by start order
395 modules.sort(StartModCmp);
396 modIter = modules.begin();
398 while (modIter != modules.end())
400 if (modIter->Start())
402 WriteServLog("Error starting module '%s': %s",
403 modIter->GetPlugin()->GetVersion().c_str(),
404 modIter->GetStrError().c_str());
407 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
411 srandom(static_cast<unsigned int>(stgTime));
413 WriteServLog("Stg started successfully.");
414 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
417 creat(startFile.c_str(), S_IRUSR);
422 sigfillset(&signalSet);
424 sigwait(&signalSet, &sig);
428 std::set<pid_t>::iterator it;
433 modIter = modules.begin();
434 for (; modIter != modules.end(); ++modIter)
436 if (modIter->Reload())
438 WriteServLog("Error reloading module '%s': '%s'", modIter->GetPlugin()->GetVersion().c_str(),
439 modIter->GetStrError().c_str());
440 printfd(__FILE__, "Error reloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(),
441 modIter->GetStrError().c_str());
452 WriteServLog("Broken pipe!");
455 childPid = waitpid(-1, &status, WNOHANG);
457 it = executersPid.find(childPid);
458 if (it != executersPid.end())
460 executersPid.erase(it);
461 if (executersPid.empty())
466 WriteServLog("Ignore signel %d", sig);
475 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
477 //Sort by start order
478 modules.sort(StopModCmp);
479 modIter = modules.begin();
480 while (modIter != modules.end())
482 std::string name = modIter->GetFileName();
483 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
486 WriteServLog("Error stopping module '%s': %s",
487 modIter->GetPlugin()->GetVersion().c_str(),
488 modIter->GetStrError().c_str());
489 printfd(__FILE__, "Error stopping module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
493 WriteServLog("Module: '%s'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
500 WriteServLog("Event loop not stopped.");
505 modIter = modules.begin();
506 while (modIter != modules.end())
508 std::string name = modIter->GetFileName();
509 if (modIter->IsRunning())
511 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
515 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
516 if (modIter->Unload())
518 WriteServLog("Error unloading module '%s': '%s'",
519 modIter->GetPlugin()->GetVersion().c_str(),
520 modIter->GetStrError().c_str());
521 printfd(__FILE__, "Error unloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
530 WriteServLog("Traffcounter: Stop successfull.");
536 WriteServLog("Users: Stop successfull.");
540 int res = msgctl(msgID, IPC_RMID, NULL);
542 WriteServLog("Queue was not removed. id=%d", msgID);
544 WriteServLog("Queue removed successfully.");
549 WriteServLog("StgTimer: Stop successfull.");
559 WriteServLog("Stg stopped successfully.");
560 WriteServLog("---------------------------------------------");
564 //-----------------------------------------------------------------------------