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 "async_pool.h"
33 #include "eventloop.h"
34 #include "stg_timer.h"
37 #include "stg/common.h"
38 #include "stg/plugin.h"
39 #include "stg/logger.h"
40 #include "stg/scriptexecuter.h"
41 #include "stg/version.h"
49 #include <cstdlib> // srandom, exit
54 #include <sys/types.h>
56 #include <sys/stat.h> // S_IRUSR
57 #include <fcntl.h> // create
63 #define START_FILE "/._ST_ART_ED_"
65 using STG::SettingsImpl;
66 using STG::AdminsImpl;
67 using STG::TraffCounterImpl;
69 using STG::TariffsImpl;
70 using STG::ServicesImpl;
71 using STG::CorporationsImpl;
72 using STG::StoreLoader;
76 std::set<pid_t> executers;
79 int StartScriptExecuter(char* procName, int msgKey, int* msgID);
80 int ForkAndWait(const std::string& confDir);
83 //-----------------------------------------------------------------------------
86 auto& WriteServLog = STG::Logger::get();
90 WriteServLog("Cannot start timer. Fatal.");
94 WriteServLog("Timer thread started successfully.");
96 //-----------------------------------------------------------------------------
97 #if defined(LINUX) || defined(DARWIN)
98 int StartScriptExecuter(char* procName, int msgKey, int* msgID)
100 int StartScriptExecuter(char*, int msgKey, int* msgID)
103 auto& WriteServLog = STG::Logger::get();
105 if (*msgID == -11) // If msgID == -11 - first call. Create queue
107 for (int i = 0; i < 2; i++)
109 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
113 *msgID = msgget(msgKey, 0);
116 WriteServLog("Message queue not created.");
120 msgctl(*msgID, IPC_RMID, NULL);
124 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
130 const auto pid = fork();
135 WriteServLog("Fork error!");
139 #if defined(LINUX) || defined(DARWIN)
140 Executer(*msgID, pid, procName);
142 Executer(*msgID, pid);
147 if (executers.empty()) {
148 #if defined(LINUX) || defined(DARWIN)
149 Executer(*msgID, pid, NULL);
151 Executer(*msgID, pid);
154 executers.insert(pid);
158 //-----------------------------------------------------------------------------
160 int ForkAndWait(const std::string& confDir)
162 int ForkAndWait(const std::string&)
166 const auto pid = fork();
167 const auto startFile = confDir + START_FILE;
168 unlink(startFile.c_str());
183 struct timespec ts = {0, 200000000};
184 for (int i = 0; i < 120 * 5; i++)
186 if (access(startFile.c_str(), F_OK) == 0)
188 unlink(startFile.c_str());
192 nanosleep(&ts, NULL);
194 unlink(startFile.c_str());
201 //-----------------------------------------------------------------------------
204 auto pid = executers.begin();
205 while (pid != executers.end())
207 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
212 //-----------------------------------------------------------------------------
213 } // namespace anonymous
214 //-----------------------------------------------------------------------------
215 int main(int argc, char* argv[])
219 STG::Logger::get().setFileName("/var/log/stargazer.log");
223 printf("You must be root. Exit.\n");
227 SettingsImpl settings(argc == 2 ? argv[1] : "");
229 if (settings.ReadSettings())
231 auto& WriteServLog = STG::Logger::get();
233 if (settings.GetLogFileName() != "")
234 WriteServLog.setFileName(settings.GetLogFileName());
236 WriteServLog("ReadSettings error. %s", settings.GetStrError().c_str());
241 const auto startFile = settings.GetConfDir() + START_FILE;
244 if (ForkAndWait(settings.GetConfDir()) < 0)
246 STG::Logger::get()("Fork error!");
250 auto& WriteServLog = STG::Logger::get();
251 WriteServLog.setFileName(settings.GetLogFileName());
252 WriteServLog("Stg v. %s", SERVER_VERSION);
254 for (size_t i = 0; i < settings.GetExecutersNum(); i++)
256 auto ret = StartScriptExecuter(argv[0], settings.GetExecMsgKey(), &msgID);
259 STG::Logger::get()("Start Script Executer error!");
266 PIDFile pidFile(settings.GetPIDFileName());
269 memset(&sa, 0, sizeof(sa));
270 sa.sa_handler = SIG_DFL;
271 sigaction(SIGHUP, &sa, NULL); // Apparently FreeBSD ignores SIGHUP by default when launched from rc.d at bot time.
274 sigfillset(&signalSet);
275 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
279 if (!IsStgTimerRunning())
281 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
282 WriteServLog("Timer thread not started in 1 sec!");
286 auto& loop = EVENT_LOOP::instance();
287 STG::AsyncPoolST::start();
289 StoreLoader storeLoader(settings);
290 if (storeLoader.load())
292 printfd(__FILE__, "Storage plugin: '%s'\n", storeLoader.GetStrError().c_str());
293 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
299 printfd(__FILE__, "Event loop not started.\n");
300 WriteServLog("Event loop not started.");
304 auto& store = storeLoader.get();
305 WriteServLog("Storage plugin: %s. Loading successfull.", store.GetVersion().c_str());
307 AdminsImpl admins(store);
308 TariffsImpl tariffs(&store);
309 tariffs.ReadTariffs();
310 ServicesImpl services(&store);
311 CorporationsImpl corps(&store);
312 UsersImpl users(&settings, &store, &tariffs, services, admins.sysAdmin());
313 TraffCounterImpl traffCnt(&users, settings.GetRulesFileName());
314 traffCnt.SetMonitorDir(settings.GetMonitorDir());
319 WriteServLog("Users started successfully.");
321 if (traffCnt.Start())
324 WriteServLog("Traffcounter started successfully.");
326 STG::PluginManager manager(settings, store, admins, tariffs, services, corps, users, traffCnt);
328 srandom(static_cast<unsigned int>(stgTime));
330 WriteServLog("Stg started successfully.");
331 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
334 creat(startFile.c_str(), S_IRUSR);
340 sigfillset(&signalSet);
342 sigwait(&signalSet, &sig);
348 SettingsImpl newSettings(settings);
349 if (newSettings.ReadSettings())
350 WriteServLog("ReadSettings error. %s", newSettings.GetStrError().c_str());
352 settings = newSettings;
353 WriteServLog.setFileName(settings.GetLogFileName());
355 manager.reload(settings);
365 WriteServLog("Broken pipe!");
368 executers.erase(waitpid(-1, &status, WNOHANG));
369 if (executers.empty())
373 WriteServLog("Ignore signal %d", sig);
378 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
382 STG::AsyncPoolST::stop();
384 WriteServLog("Event loop not stopped.");
386 if (!traffCnt.Stop())
387 WriteServLog("Traffcounter: Stop successfull.");
390 WriteServLog("Users: Stop successfull.");
393 int res = msgctl(msgID, IPC_RMID, NULL);
395 WriteServLog("Queue was not removed. id=%d", msgID);
397 WriteServLog("Queue removed successfully.");
402 WriteServLog("StgTimer: Stop successfull.");
404 WriteServLog("Stg stopped successfully.");
405 WriteServLog("---------------------------------------------");
409 //-----------------------------------------------------------------------------