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());
270 WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
273 /*************************************************************************************************/
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 /*************************************************************************************************/
286 std::string startFile(settings->GetConfDir() + START_FILE);
289 if (ForkAndWait(settings->GetConfDir()) < 0)
291 STG_LOGGER & WriteServLog = GetStgLogger();
292 WriteServLog("Fork error!");
296 STG_LOGGER & WriteServLog = GetStgLogger();
297 WriteServLog.SetLogFileName(settings->GetLogFileName());
298 WriteServLog("Stg v. %s", SERVER_VERSION);
300 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
302 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
305 STG_LOGGER & WriteServLog = GetStgLogger();
306 WriteServLog("Start Script Executer error!");
316 PIDFile pidFile(settings->GetPIDFileName());
319 sigfillset(&signalSet);
320 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
324 if (!IsStgTimerRunning())
326 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
327 WriteServLog("Timer thread not started in 1 sec!");
330 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
332 STORE_LOADER storeLoader(*settings);
333 if (storeLoader.Load())
335 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
336 goto exitLblNotStarted;
341 WriteServLog("Event loop not started.");
342 goto exitLblNotStarted;
345 dataStore = storeLoader.GetStore();
346 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
348 tariffs = new TARIFFS_IMPL(dataStore);
349 admins = new ADMINS_IMPL(dataStore);
350 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
351 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
352 services = new SERVICES_IMPL(dataStore);
353 corps = new CORPORATIONS_IMPL(dataStore);
354 traffCnt->SetMonitorDir(settings->GetMonitorDir());
356 modSettings = settings->GetModulesSettings();
358 for (size_t i = 0; i < modSettings.size(); i++)
360 std::string modulePath = settings->GetModulesPath();
361 modulePath += "/mod_";
362 modulePath += modSettings[i].moduleName;
364 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
366 PLUGIN_RUNNER(modulePath,
379 modIter = modules.begin();
381 while (modIter != modules.end())
385 WriteServLog("Error loading module '%s': %s",
386 modIter->GetPlugin()->GetVersion().c_str(),
387 modIter->GetStrError().c_str());
388 goto exitLblNotStarted;
395 goto exitLblNotStarted;
397 WriteServLog("Users started successfully.");
399 if (traffCnt->Start())
401 goto exitLblNotStarted;
403 WriteServLog("Traffcounter started successfully.");
405 //Sort by start order
406 modules.sort(StartModCmp);
407 modIter = modules.begin();
409 while (modIter != modules.end())
411 if (modIter->Start())
413 WriteServLog("Error starting module '%s': %s",
414 modIter->GetPlugin()->GetVersion().c_str(),
415 modIter->GetStrError().c_str());
418 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
422 srandom(static_cast<unsigned int>(stgTime));
424 WriteServLog("Stg started successfully.");
425 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
428 creat(startFile.c_str(), S_IRUSR);
433 sigfillset(&signalSet);
435 sigwait(&signalSet, &sig);
439 std::set<pid_t>::iterator it;
444 modIter = modules.begin();
445 for (; modIter != modules.end(); ++modIter)
447 if (modIter->Reload())
449 WriteServLog("Error reloading module '%s': '%s'", modIter->GetPlugin()->GetVersion().c_str(),
450 modIter->GetStrError().c_str());
451 printfd(__FILE__, "Error reloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(),
452 modIter->GetStrError().c_str());
463 WriteServLog("Broken pipe!");
466 childPid = waitpid(-1, &status, WNOHANG);
468 it = executersPid.find(childPid);
469 if (it != executersPid.end())
471 executersPid.erase(it);
472 if (executersPid.empty())
477 WriteServLog("Ignore signel %d", sig);
486 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
488 //Sort by start order
489 modules.sort(StopModCmp);
490 modIter = modules.begin();
491 while (modIter != modules.end())
493 std::string name = modIter->GetFileName();
494 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
497 WriteServLog("Error stopping module '%s': %s",
498 modIter->GetPlugin()->GetVersion().c_str(),
499 modIter->GetStrError().c_str());
500 printfd(__FILE__, "Error stopping module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
504 WriteServLog("Module: '%s'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
511 WriteServLog("Event loop not stopped.");
516 modIter = modules.begin();
517 while (modIter != modules.end())
519 std::string name = modIter->GetFileName();
520 if (modIter->IsRunning())
522 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
526 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
527 if (modIter->Unload())
529 WriteServLog("Error unloading module '%s': '%s'",
530 modIter->GetPlugin()->GetVersion().c_str(),
531 modIter->GetStrError().c_str());
532 printfd(__FILE__, "Error unloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str());
541 WriteServLog("Traffcounter: Stop successfull.");
547 WriteServLog("Users: Stop successfull.");
551 int res = msgctl(msgID, IPC_RMID, NULL);
553 WriteServLog("Queue was not removed. id=%d", msgID);
555 WriteServLog("Queue removed successfully.");
560 WriteServLog("StgTimer: Stop successfull.");
570 WriteServLog("Stg stopped successfully.");
571 WriteServLog("---------------------------------------------");
575 //-----------------------------------------------------------------------------