1 #include <unistd.h> // daemon
3 #include <cerrno> // errno
4 #include <cstring> // strerror
9 #include "snmp_pp/snmp_pp.h"
18 static bool running = true;
19 static bool reload = false;
21 void setSignalHandlers();
23 int main(int argc, char * argv[])
25 GTS::SettingsParser sParser;
27 // Set filter for logging
28 DefaultLog::log()->set_filter(ERROR_LOG, 7);
29 DefaultLog::log()->set_filter(WARNING_LOG, 7);
30 DefaultLog::log()->set_filter(EVENT_LOG, 0);
31 DefaultLog::log()->set_filter(INFO_LOG, 0);
32 DefaultLog::log()->set_filter(DEBUG_LOG, 0);
35 sParser.init(argc, argv);
37 catch (const std::exception & ex) {
38 logger << ex.what() << std::endl;
42 if (sParser.settings().isHelp()) {
47 if (sParser.settings().isVersion()) {
48 std::cout << "GTS SNMP Switch Management Daemon " << GTS::version << " (revision: " << GTS::revision << ")" << std::endl;
52 if (sParser.settings().isDebug()) {
53 std::cout << "Settings dump:\n"
54 << "\t- help: " << (sParser.settings().isHelp() ? "yes" : "no") << "\n"
55 << "\t- version: " << (sParser.settings().isVersion() ? "yes" : "no") << "\n"
56 << "\t- debug: " << (sParser.settings().isDebug() ? "yes" : "no") << "\n"
57 << "\t- daemon: " << (sParser.settings().isDaemon() ? "yes" : "no") << "\n"
58 << "\t- config file: " << sParser.settings().configFile() << "\n"
59 << "\t- log file: " << sParser.settings().logFile() << "\n"
60 << "\t- PID file: " << sParser.settings().PIDFile() << "\n"
61 << "\t- switch sync interval: " << sParser.settings().switchSyncInterval() << "\n"
62 << "\t- info sync interval: " << sParser.settings().infoSyncInterval() << "\n"
63 << "\t- switch's upload profile id: " << sParser.settings().upProfileId() << "\n"
64 << "\t- switch's download profile id: " << sParser.settings().downProfileId() << "\n"
65 << "\t- data URL: " << sParser.settings().dataURL() << std::endl;
68 if (sParser.settings().isDaemon()) {
69 if (daemon(0, sParser.settings().isDebug())) {
70 logger << "Failed to daemonize: " << strerror(errno) << std::endl;
74 logger.setLogFile(sParser.settings().logFile());
81 logger << "Failed to initialize SNMP" << std::endl;
85 GTS::PIDFile pf(sParser.settings().PIDFile());
86 GTS::Syncer syncer(sParser, snmp);
88 logger << "Starting gssmd main thread..." << std::endl;
93 syncer.run(running, reload);
95 logger << "Main thread stopped." << std::endl;
97 catch (std::exception & ex) {
98 logger << "Exception: " << ex.what() << std::endl;
109 struct sigaction newsa, oldsa;
112 sigemptyset(&sigmask);
113 sigaddset(&sigmask, SIGTERM);
114 newsa.sa_handler = SIG_IGN;
115 newsa.sa_mask = sigmask;
117 sigaction(SIGTERM, &newsa, &oldsa);
119 sigemptyset(&sigmask);
120 sigaddset(&sigmask, SIGINT);
121 newsa.sa_handler = SIG_IGN;
122 newsa.sa_mask = sigmask;
124 sigaction(SIGINT, &newsa, &oldsa);
132 void setSignalHandlers()
134 struct sigaction newsa, oldsa;
137 sigemptyset(&sigmask);
138 sigaddset(&sigmask, SIGTERM);
139 newsa.sa_handler = catchTERM;
140 newsa.sa_mask = sigmask;
142 sigaction(SIGTERM, &newsa, &oldsa);
144 sigemptyset(&sigmask);
145 sigaddset(&sigmask, SIGINT);
146 newsa.sa_handler = catchTERM;
147 newsa.sa_mask = sigmask;
149 sigaction(SIGINT, &newsa, &oldsa);
151 sigemptyset(&sigmask);
152 sigaddset(&sigmask, SIGPIPE);
153 newsa.sa_handler = SIG_IGN;
154 newsa.sa_mask = sigmask;
156 sigaction(SIGPIPE, &newsa, &oldsa);
158 sigemptyset(&sigmask);
159 sigaddset(&sigmask, SIGHUP);
160 newsa.sa_handler = catchHUP;
161 newsa.sa_mask = sigmask;
163 sigaction(SIGHUP, &newsa, &oldsa);