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 "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"
49 #include <cstdlib> // srandom, exit
54 #include <sys/types.h>
56 #include <sys/stat.h> // S_IRUSR
57 #include <fcntl.h> // create
59 #define START_FILE "/._ST_ART_ED_"
61 using STG::SettingsImpl;
62 using STG::AdminsImpl;
63 using STG::TraffCounterImpl;
65 using STG::TariffsImpl;
66 using STG::ServicesImpl;
67 using STG::CorporationsImpl;
68 using STG::StoreLoader;
72 std::set<pid_t> executers;
75 int StartScriptExecuter(char* procName, int msgKey, int* msgID);
76 int ForkAndWait(const std::string& confDir);
79 //-----------------------------------------------------------------------------
82 auto& WriteServLog = STG::Logger::get();
86 WriteServLog("Cannot start timer. Fatal.");
90 WriteServLog("Timer thread started successfully.");
92 //-----------------------------------------------------------------------------
93 #if defined(LINUX) || defined(DARWIN)
94 int StartScriptExecuter(char* procName, int msgKey, int* msgID)
96 int StartScriptExecuter(char*, int msgKey, int* msgID)
99 auto& WriteServLog = STG::Logger::get();
101 if (*msgID == -11) // If msgID == -11 - first call. Create queue
103 for (int i = 0; i < 2; i++)
105 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
109 *msgID = msgget(msgKey, 0);
112 WriteServLog("Message queue not created.");
116 msgctl(*msgID, IPC_RMID, NULL);
120 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
126 const auto pid = fork();
131 WriteServLog("Fork error!");
135 #if defined(LINUX) || defined(DARWIN)
136 Executer(*msgID, pid, procName);
138 Executer(*msgID, pid);
143 if (executers.empty()) {
144 #if defined(LINUX) || defined(DARWIN)
145 Executer(*msgID, pid, NULL);
147 Executer(*msgID, pid);
150 executers.insert(pid);
154 //-----------------------------------------------------------------------------
155 int ForkAndWait(const std::string& confDir)
157 const auto pid = fork();
158 const auto startFile = confDir + START_FILE;
159 unlink(startFile.c_str());
174 struct timespec ts = {0, 200000000};
175 for (int i = 0; i < 120 * 5; i++)
177 if (access(startFile.c_str(), F_OK) == 0)
179 unlink(startFile.c_str());
183 nanosleep(&ts, NULL);
185 unlink(startFile.c_str());
192 //-----------------------------------------------------------------------------
195 auto pid = executers.begin();
196 while (pid != executers.end())
198 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
204 void PrintHelp(const std::string& programName)
206 std::cout << "Usage: " << programName << "[-h/--help] [-v/--version] [-f/--foreground] [<conf-dir-path>]\n"
207 << "\t --help, -h - print this help;\n"
208 << "\t --version, -v - print version;\n"
209 << "\t --foreground, -f - do not go into background;\n"
210 << "\t <conf-dir-path> - path to the directory where the configuration file is located.\n";
213 void PrintVersion(const std::string& programName)
215 std::cout << programName << "\n"
216 << "Stargazer version" << " " << SERVER_VERSION << "\n";
218 //-----------------------------------------------------------------------------
219 } // namespace anonymous
220 //-----------------------------------------------------------------------------
221 int main(int argc, char* argv[])
225 STG::Logger::get().setFileName("/var/log/stargazer.log");
229 printf("You must be root. Exit.\n");
234 bool noDaemon(false);
240 for (int i = 1; i < argc; ++i)
242 const std::string arg(argv[i]);
243 if (arg == "--help" || arg == "-h")
248 if (arg == "--version" || arg == "-v")
250 PrintVersion(argv[0]);
253 if (arg == "--foreground" || arg == "-f")
260 SettingsImpl settings(path);
262 if (settings.ReadSettings())
264 auto& WriteServLog = STG::Logger::get();
266 if (settings.GetLogFileName() != "")
267 WriteServLog.setFileName(settings.GetLogFileName());
269 WriteServLog("ReadSettings error. %s", settings.GetStrError().c_str());
275 if (ForkAndWait(settings.GetConfDir()) < 0)
277 STG::Logger::get()("Fork error!");
282 auto& WriteServLog = STG::Logger::get();
283 WriteServLog.setFileName(settings.GetLogFileName());
284 WriteServLog("Stg v. %s", SERVER_VERSION);
286 for (size_t i = 0; i < settings.GetExecutersNum(); i++)
288 auto ret = StartScriptExecuter(argv[0], settings.GetExecMsgKey(), &msgID);
291 STG::Logger::get()("Start Script Executer error!");
298 PIDFile pidFile(settings.GetPIDFileName());
301 memset(&sa, 0, sizeof(sa));
302 sa.sa_handler = SIG_DFL;
303 sigaction(SIGHUP, &sa, NULL); // Apparently FreeBSD ignores SIGHUP by default when launched from rc.d at bot time.
306 sigfillset(&signalSet);
307 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
311 if (!IsStgTimerRunning())
313 printfd(__FILE__, "Timer thread not started in 1 sec!\n");
314 WriteServLog("Timer thread not started in 1 sec!");
318 STG::AsyncPoolST::start();
320 StoreLoader storeLoader(settings);
321 if (storeLoader.load())
323 printfd(__FILE__, "Storage plugin: '%s'\n", storeLoader.GetStrError().c_str());
324 WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str());
328 auto& store = storeLoader.get();
329 WriteServLog("Storage plugin: %s. Loading successfull.", store.GetVersion().c_str());
331 AdminsImpl admins(store);
332 TariffsImpl tariffs(&store);
333 tariffs.ReadTariffs();
334 ServicesImpl services(&store);
335 CorporationsImpl corps(&store);
336 UsersImpl users(&settings, &store, &tariffs, services, admins.sysAdmin());
337 TraffCounterImpl traffCnt(&users, settings.GetRulesFileName());
338 traffCnt.SetMonitorDir(settings.GetMonitorDir());
343 WriteServLog("Users started successfully.");
345 if (traffCnt.Start())
348 WriteServLog("Traffcounter started successfully.");
350 STG::PluginManager manager(settings, store, admins, tariffs, services, corps, users, traffCnt);
352 srandom(static_cast<unsigned int>(stgTime));
354 WriteServLog("Stg started successfully.");
355 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
359 const auto startFile = settings.GetConfDir() + START_FILE;
360 creat(startFile.c_str(), S_IRUSR);
366 sigfillset(&signalSet);
368 sigwait(&signalSet, &sig);
374 SettingsImpl newSettings(settings);
375 if (newSettings.ReadSettings())
376 WriteServLog("ReadSettings error. %s", newSettings.GetStrError().c_str());
378 settings = newSettings;
379 WriteServLog.setFileName(settings.GetLogFileName());
381 manager.reload(settings);
391 WriteServLog("Broken pipe!");
394 executers.erase(waitpid(-1, &status, WNOHANG));
395 if (executers.empty())
399 WriteServLog("Ignore signal %d", sig);
404 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
408 STG::AsyncPoolST::stop();
410 if (!traffCnt.Stop())
411 WriteServLog("Traffcounter: Stop successfull.");
414 WriteServLog("Users: Stop successfull.");
417 int res = msgctl(msgID, IPC_RMID, NULL);
419 WriteServLog("Queue was not removed. id=%d", msgID);
421 WriteServLog("Queue removed successfully.");
426 WriteServLog("StgTimer: Stop successfull.");
428 WriteServLog("Stg stopped successfully.");
429 WriteServLog("---------------------------------------------");
433 //-----------------------------------------------------------------------------