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/conffiles.h"
50 #include "stg/version.h"
51 #include "stg/pinger.h"
52 #include "stg_timer.h"
53 #include "settings_impl.h"
54 #include "users_impl.h"
55 #include "admins_impl.h"
56 #include "tariffs_impl.h"
57 #include "services_impl.h"
58 #include "corps_impl.h"
59 #include "traffcounter_impl.h"
60 #include "plugin_runner.h"
61 #include "store_loader.h"
63 #include "eventloop.h"
68 #define MAIN_DEBUG (1)
72 #define START_FILE "/._ST_ART_ED_"
74 set<pid_t> executersPid;
76 //-----------------------------------------------------------------------------
77 bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
79 return lhs.GetStartPosition() < rhs.GetStartPosition();
81 //-----------------------------------------------------------------------------
82 bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
84 return lhs.GetStopPosition() > rhs.GetStopPosition();
86 //-----------------------------------------------------------------------------
87 static void StartTimer()
89 STG_LOGGER & WriteServLog = GetStgLogger();
93 WriteServLog("Cannot start timer. Fatal.");
94 //printfd(__FILE__, "Cannot start timer. Fatal.\n");
99 WriteServLog("Timer thread started successfully.");
100 //printfd(__FILE__, "Timer thread started successfully.\n");
103 //-----------------------------------------------------------------------------
104 int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings)
106 STG_LOGGER & WriteServLog = GetStgLogger();
108 if (*msgID == -11) // If msgID == -11 - first call. Create queue
110 for (int i = 0; i < 2; i++)
112 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
116 *msgID = msgget(msgKey, 0);
119 WriteServLog("Message queue not created.");
124 msgctl(*msgID, IPC_RMID, NULL);
129 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
135 pid_t executerPid = fork();
140 WriteServLog("Fork error!");
145 Executer(*msgID, executerPid, procName);
149 if (executersPid.empty()) {
150 Executer(*msgID, executerPid, NULL);
152 executersPid.insert(executerPid);
156 //-----------------------------------------------------------------------------
158 int ForkAndWait(const string & confDir)
160 int ForkAndWait(const string &)
164 stgChildPid = fork();
165 string startFile = confDir + START_FILE;
166 unlink(startFile.c_str());
181 struct timespec ts = {0, 200000000};
182 for (int i = 0; i < 120 * 5; i++)
184 if (access(startFile.c_str(), F_OK) == 0)
186 unlink(startFile.c_str());
190 nanosleep(&ts, NULL);
192 unlink(startFile.c_str());
199 //-----------------------------------------------------------------------------
202 set<pid_t>::iterator pid;
203 pid = executersPid.begin();
204 while (pid != executersPid.end())
206 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
211 //-----------------------------------------------------------------------------
212 int main(int argc, char * argv[])
214 SETTINGS_IMPL * settings = NULL;
215 STORE * dataStore = NULL;
216 TARIFFS_IMPL * tariffs = NULL;
217 ADMINS_IMPL * admins = NULL;
218 USERS_IMPL * users = NULL;
219 TRAFFCOUNTER_IMPL * traffCnt = NULL;
220 SERVICES_IMPL * services = NULL;
221 CORPORATIONS_IMPL * corps = NULL;
225 STG_LOGGER & WriteServLog = GetStgLogger();
226 WriteServLog.SetLogFileName("/var/log/stargazer.log");
229 vector<MODULE_SETTINGS> modSettings;
230 list<PLUGIN_RUNNER> modules;
232 list<PLUGIN_RUNNER>::iterator modIter;
236 printf("You must be root. Exit.\n");
241 settings = new SETTINGS_IMPL(argv[1]);
243 settings = new SETTINGS_IMPL();
245 if (settings->ReadSettings())
247 STG_LOGGER & WriteServLog = GetStgLogger();
249 if (settings->GetLogFileName() != "")
250 WriteServLog.SetLogFileName(settings->GetLogFileName());
252 WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
257 string startFile(settings->GetConfDir() + START_FILE);
260 if (ForkAndWait(settings->GetConfDir()) < 0)
262 STG_LOGGER & WriteServLog = GetStgLogger();
263 WriteServLog("Fork error!");
267 STG_LOGGER & WriteServLog = GetStgLogger();
268 WriteServLog.SetLogFileName(settings->GetLogFileName());
269 WriteServLog("Stg v. %s", SERVER_VERSION);
271 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
273 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
276 STG_LOGGER & WriteServLog = GetStgLogger();
277 WriteServLog("Start Script Executer error!");
287 PIDFile pidFile(settings->GetPIDFileName());
291 if (!IsStgTimerRunning())
293 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
294 WriteServLog("Timer thread not started in 1 sec!");
297 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
299 STORE_LOADER storeLoader(*settings);
300 if (storeLoader.Load())
302 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
303 goto exitLblNotStarted;
308 WriteServLog("Event loop not started.");
309 goto exitLblNotStarted;
312 dataStore = storeLoader.GetStore();
313 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
315 tariffs = new TARIFFS_IMPL(dataStore);
316 admins = new ADMINS_IMPL(dataStore);
317 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
318 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
319 services = new SERVICES_IMPL(dataStore);
320 corps = new CORPORATIONS_IMPL(dataStore);
321 traffCnt->SetMonitorDir(settings->GetMonitorDir());
323 modSettings = settings->GetModulesSettings();
325 for (size_t i = 0; i < modSettings.size(); i++)
327 string modulePath = settings->GetModulesPath();
328 modulePath += "/mod_";
329 modulePath += modSettings[i].moduleName;
331 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
333 PLUGIN_RUNNER(modulePath,
346 modIter = modules.begin();
348 while (modIter != modules.end())
352 WriteServLog("Error: %s",
353 modIter->GetStrError().c_str());
354 goto exitLblNotStarted;
361 goto exitLblNotStarted;
363 WriteServLog("Users started successfully.");
365 if (traffCnt->Start())
367 goto exitLblNotStarted;
369 WriteServLog("Traffcounter started successfully.");
371 //Sort by start order
372 modules.sort(StartModCmp);
373 modIter = modules.begin();
375 while (modIter != modules.end())
377 if (modIter->Start())
379 WriteServLog("Error: %s",
380 modIter->GetStrError().c_str());
383 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
389 WriteServLog("Stg started successfully.");
390 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
393 creat(startFile.c_str(), S_IRUSR);
397 sigfillset(&signalSet);
398 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
402 sigfillset(&signalSet);
404 sigwait(&signalSet, &sig);
408 set<pid_t>::iterator it;
413 modIter = modules.begin();
414 for (; modIter != modules.end(); ++modIter)
416 if (modIter->Reload())
418 WriteServLog("Error reloading %s ('%s')", modIter->GetPlugin()->GetVersion().c_str(),
419 modIter->GetStrError().c_str());
420 printfd(__FILE__, "Error reloading %s ('%s')\n", modIter->GetPlugin()->GetVersion().c_str(),
421 modIter->GetStrError().c_str());
432 WriteServLog("Broken pipe!");
435 childPid = waitpid(-1, &status, WNOHANG);
437 it = executersPid.find(childPid);
438 if (it != executersPid.end())
440 executersPid.erase(it);
441 if (executersPid.empty())
446 WriteServLog("Ignore signel %d", sig);
455 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
457 //Sort by start order
458 modules.sort(StopModCmp);
459 modIter = modules.begin();
460 while (modIter != modules.end())
462 std::string name = modIter->GetFileName();
463 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
466 WriteServLog("Module \'%s\': Error: %s",
467 modIter->GetPlugin()->GetVersion().c_str(),
468 modIter->GetStrError().c_str());
469 printfd(__FILE__, "Failed to stop module '%s'\n", name.c_str());
471 WriteServLog("Module: \'%s\'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
477 WriteServLog("Event loop not stopped.");
482 modIter = modules.begin();
483 while (modIter != modules.end())
485 std::string name = modIter->GetFileName();
486 if (modIter->IsRunning())
488 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
492 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
493 if (modIter->Unload())
495 WriteServLog("Module \'%s\': Error: %s",
497 modIter->GetStrError().c_str());
498 printfd(__FILE__, "Failed to unload module '%s'\n", name.c_str());
507 WriteServLog("Traffcounter: Stop successfull.");
513 WriteServLog("Users: Stop successfull.");
517 int res = msgctl(msgID, IPC_RMID, NULL);
519 WriteServLog("Queue was not removed. id=%d", msgID);
521 WriteServLog("Queue removed successfully.");
526 WriteServLog("StgTimer: Stop successfull.");
536 WriteServLog("Stg stopped successfully.");
537 WriteServLog("---------------------------------------------");
541 //-----------------------------------------------------------------------------