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 tariffs.ReadTariffs();
308 ServicesImpl services(&store);
309 CorporationsImpl corps(&store);
310 UsersImpl users(&settings, &store, &tariffs, services, admins.sysAdmin());
311 TraffCounterImpl traffCnt(&users, settings.GetRulesFileName());
312 traffCnt.SetMonitorDir(settings.GetMonitorDir());
317 WriteServLog("Users started successfully.");
319 if (traffCnt.Start())
322 WriteServLog("Traffcounter started successfully.");
324 STG::PluginManager manager(settings, store, admins, tariffs, services, corps, users, traffCnt);
326 srandom(static_cast<unsigned int>(stgTime));
328 WriteServLog("Stg started successfully.");
329 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
332 creat(startFile.c_str(), S_IRUSR);
338 sigfillset(&signalSet);
340 sigwait(&signalSet, &sig);
346 SettingsImpl newSettings(settings);
347 if (newSettings.ReadSettings())
348 WriteServLog("ReadSettings error. %s", newSettings.GetStrError().c_str());
350 settings = newSettings;
351 WriteServLog.setFileName(settings.GetLogFileName());
353 manager.reload(settings);
363 WriteServLog("Broken pipe!");
366 executers.erase(waitpid(-1, &status, WNOHANG));
367 if (executers.empty())
371 WriteServLog("Ignore signal %d", sig);
376 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
381 WriteServLog("Event loop not stopped.");
383 if (!traffCnt.Stop())
384 WriteServLog("Traffcounter: Stop successfull.");
387 WriteServLog("Users: Stop successfull.");
390 int res = msgctl(msgID, IPC_RMID, NULL);
392 WriteServLog("Queue was not removed. id=%d", msgID);
394 WriteServLog("Queue removed successfully.");
399 WriteServLog("StgTimer: Stop successfull.");
401 WriteServLog("Stg stopped successfully.");
402 WriteServLog("---------------------------------------------");
406 //-----------------------------------------------------------------------------