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>
21 #include "store_loader.h"
22 #include "plugin_mgr.h"
23 #include "plugin_runner.h"
24 #include "users_impl.h"
25 #include "admins_impl.h"
26 #include "tariffs_impl.h"
27 #include "services_impl.h"
28 #include "corps_impl.h"
29 #include "traffcounter_impl.h"
30 #include "settings_impl.h"
32 #include "eventloop.h"
33 #include "stg_timer.h"
36 #include "stg/common.h"
37 #include "stg/plugin.h"
38 #include "stg/logger.h"
39 #include "stg/scriptexecuter.h"
40 #include "stg/version.h"
48 #include <cstdlib> // srandom, exit
53 #include <sys/types.h>
55 #include <sys/stat.h> // S_IRUSR
56 #include <fcntl.h> // create
62 #define START_FILE "/._ST_ART_ED_"
64 using STG::SettingsImpl;
65 using STG::AdminsImpl;
66 using STG::TraffCounterImpl;
68 using STG::TariffsImpl;
69 using STG::ServicesImpl;
70 using STG::CorporationsImpl;
71 using STG::StoreLoader;
75 std::set<pid_t> executers;
78 int StartScriptExecuter(char* procName, int msgKey, int* msgID);
79 int ForkAndWait(const std::string& confDir);
82 //-----------------------------------------------------------------------------
85 auto& WriteServLog = STG::Logger::get();
89 WriteServLog("Cannot start timer. Fatal.");
93 WriteServLog("Timer thread started successfully.");
95 //-----------------------------------------------------------------------------
96 #if defined(LINUX) || defined(DARWIN)
97 int StartScriptExecuter(char* procName, int msgKey, int* msgID)
99 int StartScriptExecuter(char*, int msgKey, int* msgID)
102 auto& WriteServLog = STG::Logger::get();
104 if (*msgID == -11) // If msgID == -11 - first call. Create queue
106 for (int i = 0; i < 2; i++)
108 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
112 *msgID = msgget(msgKey, 0);
115 WriteServLog("Message queue not created.");
119 msgctl(*msgID, IPC_RMID, NULL);
123 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
129 const auto pid = fork();
134 WriteServLog("Fork error!");
138 #if defined(LINUX) || defined(DARWIN)
139 Executer(*msgID, pid, procName);
141 Executer(*msgID, pid);
146 if (executers.empty()) {
147 #if defined(LINUX) || defined(DARWIN)
148 Executer(*msgID, pid, NULL);
150 Executer(*msgID, pid);
153 executers.insert(pid);
157 //-----------------------------------------------------------------------------
159 int ForkAndWait(const std::string& confDir)
161 int ForkAndWait(const std::string&)
165 const auto pid = fork();
166 const auto 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 auto pid = executers.begin();
204 while (pid != executers.end())
206 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
211 //-----------------------------------------------------------------------------
212 } // namespace anonymous
213 //-----------------------------------------------------------------------------
214 int main(int argc, char* argv[])
218 STG::Logger::get().setFileName("/var/log/stargazer.log");
222 printf("You must be root. Exit.\n");
226 SettingsImpl settings(argc == 2 ? argv[1] : "");
228 if (settings.ReadSettings())
230 auto& WriteServLog = STG::Logger::get();
232 if (settings.GetLogFileName() != "")
233 WriteServLog.setFileName(settings.GetLogFileName());
235 WriteServLog("ReadSettings error. %s", settings.GetStrError().c_str());
240 const auto startFile = settings.GetConfDir() + START_FILE;
243 if (ForkAndWait(settings.GetConfDir()) < 0)
245 STG::Logger::get()("Fork error!");
249 auto& WriteServLog = STG::Logger::get();
250 WriteServLog.setFileName(settings.GetLogFileName());
251 WriteServLog("Stg v. %s", SERVER_VERSION);
253 for (size_t i = 0; i < settings.GetExecutersNum(); i++)
255 auto ret = StartScriptExecuter(argv[0], settings.GetExecMsgKey(), &msgID);
258 STG::Logger::get()("Start Script Executer error!");
265 PIDFile pidFile(settings.GetPIDFileName());
268 memset(&sa, 0, sizeof(sa));
269 sa.sa_handler = SIG_DFL;
270 sigaction(SIGHUP, &sa, NULL); // Apparently FreeBSD ignores SIGHUP by default when launched from rc.d at bot time.
273 sigfillset(&signalSet);
274 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
278 if (!IsStgTimerRunning())
280 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
281 WriteServLog("Timer thread not started in 1 sec!");
285 auto& loop = EVENT_LOOP_SINGLETON::GetInstance();
287 StoreLoader storeLoader(settings);
288 if (storeLoader.load())
290 printfd(__FILE__, "Storage plugin: '%s'\n", storeLoader.GetStrError().c_str());
291 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
297 printfd(__FILE__, "Event loop not started.\n");
298 WriteServLog("Event loop not started.");
302 auto& store = storeLoader.get();
303 WriteServLog("Storage plugin: %s. Loading successfull.", store.GetVersion().c_str());
305 AdminsImpl admins(&store);
306 TariffsImpl tariffs(&store);
307 ServicesImpl services(&store);
308 CorporationsImpl corps(&store);
309 UsersImpl users(&settings, &store, &tariffs, services, admins.GetSysAdmin());
310 TraffCounterImpl traffCnt(&users, settings.GetRulesFileName());
311 traffCnt.SetMonitorDir(settings.GetMonitorDir());
316 WriteServLog("Users started successfully.");
318 if (traffCnt.Start())
321 WriteServLog("Traffcounter started successfully.");
323 STG::PluginManager manager(settings, store, admins, tariffs, services, corps, users, traffCnt);
325 srandom(static_cast<unsigned int>(stgTime));
327 WriteServLog("Stg started successfully.");
328 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
331 creat(startFile.c_str(), S_IRUSR);
337 sigfillset(&signalSet);
339 sigwait(&signalSet, &sig);
345 SettingsImpl newSettings(settings);
346 if (newSettings.ReadSettings())
347 WriteServLog("ReadSettings error. %s", newSettings.GetStrError().c_str());
349 settings = newSettings;
350 WriteServLog.setFileName(settings.GetLogFileName());
352 manager.reload(settings);
362 WriteServLog("Broken pipe!");
365 executers.erase(waitpid(-1, &status, WNOHANG));
366 if (executers.empty())
370 WriteServLog("Ignore signal %d", sig);
375 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
380 WriteServLog("Event loop not stopped.");
382 if (!traffCnt.Stop())
383 WriteServLog("Traffcounter: Stop successfull.");
386 WriteServLog("Users: Stop successfull.");
389 int res = msgctl(msgID, IPC_RMID, NULL);
391 WriteServLog("Queue was not removed. id=%d", msgID);
393 WriteServLog("Queue removed successfully.");
398 WriteServLog("StgTimer: Stop successfull.");
400 WriteServLog("Stg stopped successfully.");
401 WriteServLog("---------------------------------------------");
405 //-----------------------------------------------------------------------------