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"
66 #define MAIN_DEBUG (1)
70 #define START_FILE "/._ST_ART_ED_"
72 set<pid_t> executersPid;
74 //-----------------------------------------------------------------------------
75 bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
77 return lhs.GetStartPosition() < rhs.GetStartPosition();
79 //-----------------------------------------------------------------------------
80 bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
82 return lhs.GetStopPosition() > rhs.GetStopPosition();
84 //-----------------------------------------------------------------------------
85 static void StartTimer()
87 STG_LOGGER & WriteServLog = GetStgLogger();
91 WriteServLog("Cannot start timer. Fatal.");
92 //printfd(__FILE__, "Cannot start timer. Fatal.\n");
97 WriteServLog("Timer thread started successfully.");
98 //printfd(__FILE__, "Timer thread started successfully.\n");
101 //-----------------------------------------------------------------------------
102 int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings)
104 STG_LOGGER & WriteServLog = GetStgLogger();
106 if (*msgID == -11) // If msgID == -11 - first call. Create queue
108 for (int i = 0; i < 2; i++)
110 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
114 *msgID = msgget(msgKey, 0);
117 WriteServLog("Message queue not created.");
122 msgctl(*msgID, IPC_RMID, NULL);
127 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
133 pid_t executerPid = fork();
138 WriteServLog("Fork error!");
143 Executer(*msgID, executerPid, procName);
147 if (executersPid.empty()) {
148 Executer(*msgID, executerPid, NULL);
150 executersPid.insert(executerPid);
154 //-----------------------------------------------------------------------------
156 int ForkAndWait(const string & confDir)
158 int ForkAndWait(const string &)
162 pid_t childPid = fork();
163 string startFile = confDir + START_FILE;
164 unlink(startFile.c_str());
179 struct timespec ts = {0, 200000000};
180 for (int i = 0; i < 120 * 5; i++)
182 if (access(startFile.c_str(), F_OK) == 0)
184 unlink(startFile.c_str());
188 nanosleep(&ts, NULL);
190 unlink(startFile.c_str());
197 //-----------------------------------------------------------------------------
200 set<pid_t>::iterator pid;
201 pid = executersPid.begin();
202 while (pid != executersPid.end())
204 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
209 //-----------------------------------------------------------------------------
210 int main(int argc, char * argv[])
212 SETTINGS_IMPL * settings = NULL;
213 STORE * dataStore = NULL;
214 TARIFFS_IMPL * tariffs = NULL;
215 ADMINS_IMPL * admins = NULL;
216 USERS_IMPL * users = NULL;
217 TRAFFCOUNTER_IMPL * traffCnt = NULL;
218 SERVICES_IMPL * services = NULL;
219 CORPORATIONS_IMPL * corps = NULL;
223 STG_LOGGER & WriteServLog = GetStgLogger();
224 WriteServLog.SetLogFileName("/var/log/stargazer.log");
227 vector<MODULE_SETTINGS> modSettings;
228 list<PLUGIN_RUNNER> modules;
230 list<PLUGIN_RUNNER>::iterator modIter;
234 printf("You must be root. Exit.\n");
239 settings = new SETTINGS_IMPL(argv[1]);
241 settings = new SETTINGS_IMPL();
243 if (settings->ReadSettings())
245 STG_LOGGER & WriteServLog = GetStgLogger();
247 if (settings->GetLogFileName() != "")
248 WriteServLog.SetLogFileName(settings->GetLogFileName());
250 WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
255 string startFile(settings->GetConfDir() + START_FILE);
258 if (ForkAndWait(settings->GetConfDir()) < 0)
260 STG_LOGGER & WriteServLog = GetStgLogger();
261 WriteServLog("Fork error!");
265 STG_LOGGER & WriteServLog = GetStgLogger();
266 WriteServLog.SetLogFileName(settings->GetLogFileName());
267 WriteServLog("Stg v. %s", SERVER_VERSION);
269 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
271 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
274 STG_LOGGER & WriteServLog = GetStgLogger();
275 WriteServLog("Start Script Executer error!");
285 PIDFile pidFile(settings->GetPIDFileName());
288 sigfillset(&signalSet);
289 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
293 if (!IsStgTimerRunning())
295 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
296 WriteServLog("Timer thread not started in 1 sec!");
299 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
301 STORE_LOADER storeLoader(*settings);
302 if (storeLoader.Load())
304 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
305 goto exitLblNotStarted;
310 WriteServLog("Event loop not started.");
311 goto exitLblNotStarted;
314 dataStore = storeLoader.GetStore();
315 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
317 tariffs = new TARIFFS_IMPL(dataStore);
318 admins = new ADMINS_IMPL(dataStore);
319 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
320 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
321 services = new SERVICES_IMPL(dataStore);
322 corps = new CORPORATIONS_IMPL(dataStore);
323 traffCnt->SetMonitorDir(settings->GetMonitorDir());
325 modSettings = settings->GetModulesSettings();
327 for (size_t i = 0; i < modSettings.size(); i++)
329 string modulePath = settings->GetModulesPath();
330 modulePath += "/mod_";
331 modulePath += modSettings[i].moduleName;
333 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
335 PLUGIN_RUNNER(modulePath,
348 modIter = modules.begin();
350 while (modIter != modules.end())
354 WriteServLog("Error: %s",
355 modIter->GetStrError().c_str());
356 goto exitLblNotStarted;
363 goto exitLblNotStarted;
365 WriteServLog("Users started successfully.");
367 if (traffCnt->Start())
369 goto exitLblNotStarted;
371 WriteServLog("Traffcounter started successfully.");
373 //Sort by start order
374 modules.sort(StartModCmp);
375 modIter = modules.begin();
377 while (modIter != modules.end())
379 if (modIter->Start())
381 WriteServLog("Error: %s",
382 modIter->GetStrError().c_str());
385 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
391 WriteServLog("Stg started successfully.");
392 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
395 creat(startFile.c_str(), S_IRUSR);
400 sigfillset(&signalSet);
402 sigwait(&signalSet, &sig);
406 set<pid_t>::iterator it;
411 modIter = modules.begin();
412 for (; modIter != modules.end(); ++modIter)
414 if (modIter->Reload())
416 WriteServLog("Error reloading %s ('%s')", modIter->GetPlugin()->GetVersion().c_str(),
417 modIter->GetStrError().c_str());
418 printfd(__FILE__, "Error reloading %s ('%s')\n", modIter->GetPlugin()->GetVersion().c_str(),
419 modIter->GetStrError().c_str());
430 WriteServLog("Broken pipe!");
433 childPid = waitpid(-1, &status, WNOHANG);
435 it = executersPid.find(childPid);
436 if (it != executersPid.end())
438 executersPid.erase(it);
439 if (executersPid.empty())
444 WriteServLog("Ignore signel %d", sig);
453 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
455 //Sort by start order
456 modules.sort(StopModCmp);
457 modIter = modules.begin();
458 while (modIter != modules.end())
460 std::string name = modIter->GetFileName();
461 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
464 WriteServLog("Module \'%s\': Error: %s",
465 modIter->GetPlugin()->GetVersion().c_str(),
466 modIter->GetStrError().c_str());
467 printfd(__FILE__, "Failed to stop module '%s'\n", name.c_str());
469 WriteServLog("Module: \'%s\'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
475 WriteServLog("Event loop not stopped.");
480 modIter = modules.begin();
481 while (modIter != modules.end())
483 std::string name = modIter->GetFileName();
484 if (modIter->IsRunning())
486 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
490 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
491 if (modIter->Unload())
493 WriteServLog("Module \'%s\': Error: %s",
495 modIter->GetStrError().c_str());
496 printfd(__FILE__, "Failed to unload module '%s'\n", name.c_str());
505 WriteServLog("Traffcounter: Stop successfull.");
511 WriteServLog("Users: Stop successfull.");
515 int res = msgctl(msgID, IPC_RMID, NULL);
517 WriteServLog("Queue was not removed. id=%d", msgID);
519 WriteServLog("Queue removed successfully.");
524 WriteServLog("StgTimer: Stop successfull.");
534 WriteServLog("Stg stopped successfully.");
535 WriteServLog("---------------------------------------------");
539 //-----------------------------------------------------------------------------