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;
75 static pid_t stgChildPid;
77 //-----------------------------------------------------------------------------
78 bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
80 return lhs.GetStartPosition() < rhs.GetStartPosition();
82 //-----------------------------------------------------------------------------
83 bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs)
85 return lhs.GetStopPosition() > rhs.GetStopPosition();
87 //-----------------------------------------------------------------------------
88 static void StartTimer()
90 STG_LOGGER & WriteServLog = GetStgLogger();
94 WriteServLog("Cannot start timer. Fatal.");
95 //printfd(__FILE__, "Cannot start timer. Fatal.\n");
100 WriteServLog("Timer thread started successfully.");
101 //printfd(__FILE__, "Timer thread started successfully.\n");
104 //-----------------------------------------------------------------------------
105 int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings)
107 STG_LOGGER & WriteServLog = GetStgLogger();
109 if (*msgID == -11) // If msgID == -11 - first call. Create queue
111 for (int i = 0; i < 2; i++)
113 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
117 *msgID = msgget(msgKey, 0);
120 WriteServLog("Message queue not created.");
125 msgctl(*msgID, IPC_RMID, NULL);
130 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
136 pid_t executerPid = fork();
141 WriteServLog("Fork error!");
146 Executer(*msgID, executerPid, procName);
150 if (executersPid.empty()) {
151 Executer(*msgID, executerPid, NULL);
153 executersPid.insert(executerPid);
157 //-----------------------------------------------------------------------------
159 int ForkAndWait(const string & confDir)
161 int ForkAndWait(const string &)
165 stgChildPid = fork();
166 string startFile = confDir + START_FILE;
167 unlink(startFile.c_str());
182 struct timespec ts = {0, 200000000};
183 for (int i = 0; i < 120 * 5; i++)
185 if (access(startFile.c_str(), F_OK) == 0)
187 unlink(startFile.c_str());
191 nanosleep(&ts, NULL);
193 unlink(startFile.c_str());
200 //-----------------------------------------------------------------------------
203 set<pid_t>::iterator pid;
204 pid = executersPid.begin();
205 while (pid != executersPid.end())
207 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
212 //-----------------------------------------------------------------------------
213 int main(int argc, char * argv[])
215 SETTINGS_IMPL * settings = NULL;
216 STORE * dataStore = NULL;
217 TARIFFS_IMPL * tariffs = NULL;
218 ADMINS_IMPL * admins = NULL;
219 USERS_IMPL * users = NULL;
220 TRAFFCOUNTER_IMPL * traffCnt = NULL;
221 SERVICES_IMPL * services = NULL;
222 CORPORATIONS_IMPL * corps = NULL;
226 STG_LOGGER & WriteServLog = GetStgLogger();
227 WriteServLog.SetLogFileName("/var/log/stargazer.log");
230 vector<MODULE_SETTINGS> modSettings;
231 list<PLUGIN_RUNNER> modules;
233 list<PLUGIN_RUNNER>::iterator modIter;
237 printf("You must be root. Exit.\n");
242 settings = new SETTINGS_IMPL(argv[1]);
244 settings = new SETTINGS_IMPL();
246 if (settings->ReadSettings())
248 STG_LOGGER & WriteServLog = GetStgLogger();
250 if (settings->GetLogFileName() != "")
251 WriteServLog.SetLogFileName(settings->GetLogFileName());
253 WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
258 string startFile(settings->GetConfDir() + START_FILE);
261 if (ForkAndWait(settings->GetConfDir()) < 0)
263 STG_LOGGER & WriteServLog = GetStgLogger();
264 WriteServLog("Fork error!");
268 STG_LOGGER & WriteServLog = GetStgLogger();
269 WriteServLog.SetLogFileName(settings->GetLogFileName());
270 WriteServLog("Stg v. %s", SERVER_VERSION);
272 for (size_t i = 0; i < settings->GetExecutersNum(); i++)
274 int ret = StartScriptExecuter(argv[0], settings->GetExecMsgKey(), &msgID, settings);
277 STG_LOGGER & WriteServLog = GetStgLogger();
278 WriteServLog("Start Script Executer error!");
288 PIDFile pidFile(settings->GetPIDFileName());
292 if (!IsStgTimerRunning())
294 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
295 WriteServLog("Timer thread not started in 1 sec!");
298 EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance());
300 STORE_LOADER storeLoader(*settings);
301 if (storeLoader.Load())
303 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
304 goto exitLblNotStarted;
309 WriteServLog("Event loop not started.");
310 goto exitLblNotStarted;
313 dataStore = storeLoader.GetStore();
314 WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str());
316 tariffs = new TARIFFS_IMPL(dataStore);
317 admins = new ADMINS_IMPL(dataStore);
318 users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin());
319 traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName());
320 services = new SERVICES_IMPL(dataStore);
321 corps = new CORPORATIONS_IMPL(dataStore);
322 traffCnt->SetMonitorDir(settings->GetMonitorDir());
324 modSettings = settings->GetModulesSettings();
326 for (size_t i = 0; i < modSettings.size(); i++)
328 string modulePath = settings->GetModulesPath();
329 modulePath += "/mod_";
330 modulePath += modSettings[i].moduleName;
332 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
334 PLUGIN_RUNNER(modulePath,
347 modIter = modules.begin();
349 while (modIter != modules.end())
353 WriteServLog("Error: %s",
354 modIter->GetStrError().c_str());
355 goto exitLblNotStarted;
362 goto exitLblNotStarted;
364 WriteServLog("Users started successfully.");
366 if (traffCnt->Start())
368 goto exitLblNotStarted;
370 WriteServLog("Traffcounter started successfully.");
372 //Sort by start order
373 modules.sort(StartModCmp);
374 modIter = modules.begin();
376 while (modIter != modules.end())
378 if (modIter->Start())
380 WriteServLog("Error: %s",
381 modIter->GetStrError().c_str());
384 WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str());
390 WriteServLog("Stg started successfully.");
391 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
394 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 //-----------------------------------------------------------------------------