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 //-----------------------------------------------------------------------------
103 int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings)
105 int StartScriptExecuter(char *, int msgKey, int * msgID, SETTINGS_IMPL * settings)
108 STG_LOGGER & WriteServLog = GetStgLogger();
110 if (*msgID == -11) // If msgID == -11 - first call. Create queue
112 for (int i = 0; i < 2; i++)
114 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
118 *msgID = msgget(msgKey, 0);
121 WriteServLog("Message queue not created.");
126 msgctl(*msgID, IPC_RMID, NULL);
131 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
137 pid_t executerPid = fork();
142 WriteServLog("Fork error!");
148 Executer(*msgID, executerPid, procName);
150 Executer(*msgID, executerPid);
155 if (executersPid.empty()) {
157 Executer(*msgID, executerPid, NULL);
159 Executer(*msgID, executerPid);
162 executersPid.insert(executerPid);
166 //-----------------------------------------------------------------------------
168 int ForkAndWait(const string & confDir)
170 int ForkAndWait(const string &)
174 pid_t childPid = fork();
175 string startFile = confDir + START_FILE;
176 unlink(startFile.c_str());
191 struct timespec ts = {0, 200000000};
192 for (int i = 0; i < 120 * 5; i++)
194 if (access(startFile.c_str(), F_OK) == 0)
196 unlink(startFile.c_str());
200 nanosleep(&ts, NULL);
202 unlink(startFile.c_str());
209 //-----------------------------------------------------------------------------
212 set<pid_t>::iterator pid;
213 pid = executersPid.begin();
214 while (pid != executersPid.end())
216 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
221 //-----------------------------------------------------------------------------
222 int main(int argc, char * argv[])
224 SETTINGS_IMPL * settings = NULL;
225 STORE * dataStore = NULL;
226 TARIFFS_IMPL * tariffs = NULL;
227 ADMINS_IMPL * admins = NULL;
228 USERS_IMPL * users = NULL;
229 TRAFFCOUNTER_IMPL * traffCnt = NULL;
230 SERVICES_IMPL * services = NULL;
231 CORPORATIONS_IMPL * corps = NULL;
235 STG_LOGGER & WriteServLog = GetStgLogger();
236 WriteServLog.SetLogFileName("/var/log/stargazer.log");
239 vector<MODULE_SETTINGS> modSettings;
240 list<PLUGIN_RUNNER> modules;
242 list<PLUGIN_RUNNER>::iterator modIter;
246 printf("You must be root. Exit.\n");
251 settings = new SETTINGS_IMPL(argv[1]);
253 settings = new SETTINGS_IMPL();
255 if (settings->ReadSettings())
257 STG_LOGGER & WriteServLog = GetStgLogger();
259 if (settings->GetLogFileName() != "")
260 WriteServLog.SetLogFileName(settings->GetLogFileName());
262 WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
267 string startFile(settings->GetConfDir() + START_FILE);
270 if (ForkAndWait(settings->GetConfDir()) < 0)
272 STG_LOGGER & WriteServLog = GetStgLogger();
273 WriteServLog("Fork error!");
277 STG_LOGGER & WriteServLog = GetStgLogger();
278 WriteServLog.SetLogFileName(settings->GetLogFileName());
279 WriteServLog("Stg v. %s", SERVER_VERSION);
281 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
283 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
286 STG_LOGGER & WriteServLog = GetStgLogger();
287 WriteServLog("Start Script Executer error!");
297 PIDFile pidFile(settings->GetPIDFileName());
300 sigfillset(&signalSet);
301 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
305 if (!IsStgTimerRunning())
307 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
308 WriteServLog("Timer thread not started in 1 sec!");
311 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
313 STORE_LOADER storeLoader(*settings);
314 if (storeLoader.Load())
316 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
317 goto exitLblNotStarted;
322 WriteServLog("Event loop not started.");
323 goto exitLblNotStarted;
326 dataStore = storeLoader.GetStore();
327 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
329 tariffs = new TARIFFS_IMPL(dataStore);
330 admins = new ADMINS_IMPL(dataStore);
331 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
332 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
333 services = new SERVICES_IMPL(dataStore);
334 corps = new CORPORATIONS_IMPL(dataStore);
335 traffCnt->SetMonitorDir(settings->GetMonitorDir());
337 modSettings = settings->GetModulesSettings();
339 for (size_t i = 0; i < modSettings.size(); i++)
341 string modulePath = settings->GetModulesPath();
342 modulePath += "/mod_";
343 modulePath += modSettings[i].moduleName;
345 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
347 PLUGIN_RUNNER(modulePath,
360 modIter = modules.begin();
362 while (modIter != modules.end())
366 WriteServLog("Error: %s",
367 modIter->GetStrError().c_str());
368 goto exitLblNotStarted;
375 goto exitLblNotStarted;
377 WriteServLog("Users started successfully.");
379 if (traffCnt->Start())
381 goto exitLblNotStarted;
383 WriteServLog("Traffcounter started successfully.");
385 //Sort by start order
386 modules.sort(StartModCmp);
387 modIter = modules.begin();
389 while (modIter != modules.end())
391 if (modIter->Start())
393 WriteServLog("Error: %s",
394 modIter->GetStrError().c_str());
397 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
403 WriteServLog("Stg started successfully.");
404 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
407 creat(startFile.c_str(), S_IRUSR);
412 sigfillset(&signalSet);
414 sigwait(&signalSet, &sig);
418 set<pid_t>::iterator it;
423 modIter = modules.begin();
424 for (; modIter != modules.end(); ++modIter)
426 if (modIter->Reload())
428 WriteServLog("Error reloading %s ('%s')", modIter->GetPlugin()->GetVersion().c_str(),
429 modIter->GetStrError().c_str());
430 printfd(__FILE__, "Error reloading %s ('%s')\n", modIter->GetPlugin()->GetVersion().c_str(),
431 modIter->GetStrError().c_str());
442 WriteServLog("Broken pipe!");
445 childPid = waitpid(-1, &status, WNOHANG);
447 it = executersPid.find(childPid);
448 if (it != executersPid.end())
450 executersPid.erase(it);
451 if (executersPid.empty())
456 WriteServLog("Ignore signel %d", sig);
465 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
467 //Sort by start order
468 modules.sort(StopModCmp);
469 modIter = modules.begin();
470 while (modIter != modules.end())
472 std::string name = modIter->GetFileName();
473 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
476 WriteServLog("Module \'%s\': Error: %s",
477 modIter->GetPlugin()->GetVersion().c_str(),
478 modIter->GetStrError().c_str());
479 printfd(__FILE__, "Failed to stop module '%s'\n", name.c_str());
483 WriteServLog("Module: \'%s\'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
490 WriteServLog("Event loop not stopped.");
495 modIter = modules.begin();
496 while (modIter != modules.end())
498 std::string name = modIter->GetFileName();
499 if (modIter->IsRunning())
501 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
505 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
506 if (modIter->Unload())
508 WriteServLog("Module \'%s\': Error: %s",
510 modIter->GetStrError().c_str());
511 printfd(__FILE__, "Failed to unload module '%s'\n", name.c_str());
520 WriteServLog("Traffcounter: Stop successfull.");
526 WriteServLog("Users: Stop successfull.");
530 int res = msgctl(msgID, IPC_RMID, NULL);
532 WriteServLog("Queue was not removed. id=%d", msgID);
534 WriteServLog("Queue removed successfully.");
539 WriteServLog("StgTimer: Stop successfull.");
549 WriteServLog("Stg stopped successfully.");
550 WriteServLog("---------------------------------------------");
554 //-----------------------------------------------------------------------------