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!");
144 Executer(*msgID, executerPid, procName);
146 Executer(*msgID, executerPid);
151 if (executersPid.empty()) {
153 Executer(*msgID, executerPid, NULL);
155 Executer(*msgID, executerPid);
158 executersPid.insert(executerPid);
162 //-----------------------------------------------------------------------------
164 int ForkAndWait(const string & confDir)
166 int ForkAndWait(const string &)
170 pid_t childPid = fork();
171 string startFile = confDir + START_FILE;
172 unlink(startFile.c_str());
187 struct timespec ts = {0, 200000000};
188 for (int i = 0; i < 120 * 5; i++)
190 if (access(startFile.c_str(), F_OK) == 0)
192 unlink(startFile.c_str());
196 nanosleep(&ts, NULL);
198 unlink(startFile.c_str());
205 //-----------------------------------------------------------------------------
208 set<pid_t>::iterator pid;
209 pid = executersPid.begin();
210 while (pid != executersPid.end())
212 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
217 //-----------------------------------------------------------------------------
218 int main(int argc, char * argv[])
220 SETTINGS_IMPL * settings = NULL;
221 STORE * dataStore = NULL;
222 TARIFFS_IMPL * tariffs = NULL;
223 ADMINS_IMPL * admins = NULL;
224 USERS_IMPL * users = NULL;
225 TRAFFCOUNTER_IMPL * traffCnt = NULL;
226 SERVICES_IMPL * services = NULL;
227 CORPORATIONS_IMPL * corps = NULL;
231 STG_LOGGER & WriteServLog = GetStgLogger();
232 WriteServLog.SetLogFileName("/var/log/stargazer.log");
235 vector<MODULE_SETTINGS> modSettings;
236 list<PLUGIN_RUNNER> modules;
238 list<PLUGIN_RUNNER>::iterator modIter;
242 printf("You must be root. Exit.\n");
247 settings = new SETTINGS_IMPL(argv[1]);
249 settings = new SETTINGS_IMPL();
251 if (settings->ReadSettings())
253 STG_LOGGER & WriteServLog = GetStgLogger();
255 if (settings->GetLogFileName() != "")
256 WriteServLog.SetLogFileName(settings->GetLogFileName());
258 WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
263 string startFile(settings->GetConfDir() + START_FILE);
266 if (ForkAndWait(settings->GetConfDir()) < 0)
268 STG_LOGGER & WriteServLog = GetStgLogger();
269 WriteServLog("Fork error!");
273 STG_LOGGER & WriteServLog = GetStgLogger();
274 WriteServLog.SetLogFileName(settings->GetLogFileName());
275 WriteServLog("Stg v. %s", SERVER_VERSION);
277 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
279 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
282 STG_LOGGER & WriteServLog = GetStgLogger();
283 WriteServLog("Start Script Executer error!");
293 PIDFile pidFile(settings->GetPIDFileName());
296 sigfillset(&signalSet);
297 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
301 if (!IsStgTimerRunning())
303 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
304 WriteServLog("Timer thread not started in 1 sec!");
307 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
309 STORE_LOADER storeLoader(*settings);
310 if (storeLoader.Load())
312 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
313 goto exitLblNotStarted;
318 WriteServLog("Event loop not started.");
319 goto exitLblNotStarted;
322 dataStore = storeLoader.GetStore();
323 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
325 tariffs = new TARIFFS_IMPL(dataStore);
326 admins = new ADMINS_IMPL(dataStore);
327 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
328 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
329 services = new SERVICES_IMPL(dataStore);
330 corps = new CORPORATIONS_IMPL(dataStore);
331 traffCnt->SetMonitorDir(settings->GetMonitorDir());
333 modSettings = settings->GetModulesSettings();
335 for (size_t i = 0; i < modSettings.size(); i++)
337 string modulePath = settings->GetModulesPath();
338 modulePath += "/mod_";
339 modulePath += modSettings[i].moduleName;
341 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
343 PLUGIN_RUNNER(modulePath,
356 modIter = modules.begin();
358 while (modIter != modules.end())
362 WriteServLog("Error: %s",
363 modIter->GetStrError().c_str());
364 goto exitLblNotStarted;
371 goto exitLblNotStarted;
373 WriteServLog("Users started successfully.");
375 if (traffCnt->Start())
377 goto exitLblNotStarted;
379 WriteServLog("Traffcounter started successfully.");
381 //Sort by start order
382 modules.sort(StartModCmp);
383 modIter = modules.begin();
385 while (modIter != modules.end())
387 if (modIter->Start())
389 WriteServLog("Error: %s",
390 modIter->GetStrError().c_str());
393 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
399 WriteServLog("Stg started successfully.");
400 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
403 creat(startFile.c_str(), S_IRUSR);
408 sigfillset(&signalSet);
410 sigwait(&signalSet, &sig);
414 set<pid_t>::iterator it;
419 modIter = modules.begin();
420 for (; modIter != modules.end(); ++modIter)
422 if (modIter->Reload())
424 WriteServLog("Error reloading %s ('%s')", modIter->GetPlugin()->GetVersion().c_str(),
425 modIter->GetStrError().c_str());
426 printfd(__FILE__, "Error reloading %s ('%s')\n", modIter->GetPlugin()->GetVersion().c_str(),
427 modIter->GetStrError().c_str());
438 WriteServLog("Broken pipe!");
441 childPid = waitpid(-1, &status, WNOHANG);
443 it = executersPid.find(childPid);
444 if (it != executersPid.end())
446 executersPid.erase(it);
447 if (executersPid.empty())
452 WriteServLog("Ignore signel %d", sig);
461 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
463 //Sort by start order
464 modules.sort(StopModCmp);
465 modIter = modules.begin();
466 while (modIter != modules.end())
468 std::string name = modIter->GetFileName();
469 printfd(__FILE__, "Stopping module '%s'\n", name.c_str());
472 WriteServLog("Module \'%s\': Error: %s",
473 modIter->GetPlugin()->GetVersion().c_str(),
474 modIter->GetStrError().c_str());
475 printfd(__FILE__, "Failed to stop module '%s'\n", name.c_str());
477 WriteServLog("Module: \'%s\'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str());
483 WriteServLog("Event loop not stopped.");
488 modIter = modules.begin();
489 while (modIter != modules.end())
491 std::string name = modIter->GetFileName();
492 if (modIter->IsRunning())
494 printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str());
498 printfd(__FILE__, "Unloading module '%s'\n", name.c_str());
499 if (modIter->Unload())
501 WriteServLog("Module \'%s\': Error: %s",
503 modIter->GetStrError().c_str());
504 printfd(__FILE__, "Failed to unload module '%s'\n", name.c_str());
513 WriteServLog("Traffcounter: Stop successfull.");
519 WriteServLog("Users: Stop successfull.");
523 int res = msgctl(msgID, IPC_RMID, NULL);
525 WriteServLog("Queue was not removed. id=%d", msgID);
527 WriteServLog("Queue removed successfully.");
532 WriteServLog("StgTimer: Stop successfull.");
542 WriteServLog("Stg stopped successfully.");
543 WriteServLog("---------------------------------------------");
547 //-----------------------------------------------------------------------------