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- max ACL's per PDU: " << sParser.settings().maxACLPerPDU() << "\n"
66 << "\t- data URL: " << sParser.settings().dataURL() << std::endl;
69 if (sParser.settings().isDaemon()) {
70 if (daemon(0, sParser.settings().isDebug())) {
71 logger << "Failed to daemonize: " << strerror(errno) << std::endl;
75 logger.setLogFile(sParser.settings().logFile());
82 logger << "Failed to initialize SNMP" << std::endl;
86 GTS::PIDFile pf(sParser.settings().PIDFile());
87 GTS::Syncer syncer(sParser, snmp);
89 logger << "Starting gssmd main thread..." << std::endl;
94 syncer.run(running, reload);
96 logger << "Main thread stopped." << std::endl;
98 catch (std::exception & ex) {
99 logger << "Exception: " << ex.what() << std::endl;
110 struct sigaction newsa, oldsa;
113 sigemptyset(&sigmask);
114 sigaddset(&sigmask, SIGTERM);
115 newsa.sa_handler = SIG_IGN;
116 newsa.sa_mask = sigmask;
118 sigaction(SIGTERM, &newsa, &oldsa);
120 sigemptyset(&sigmask);
121 sigaddset(&sigmask, SIGINT);
122 newsa.sa_handler = SIG_IGN;
123 newsa.sa_mask = sigmask;
125 sigaction(SIGINT, &newsa, &oldsa);
133 void setSignalHandlers()
135 struct sigaction newsa, oldsa;
138 sigemptyset(&sigmask);
139 sigaddset(&sigmask, SIGTERM);
140 newsa.sa_handler = catchTERM;
141 newsa.sa_mask = sigmask;
143 sigaction(SIGTERM, &newsa, &oldsa);
145 sigemptyset(&sigmask);
146 sigaddset(&sigmask, SIGINT);
147 newsa.sa_handler = catchTERM;
148 newsa.sa_mask = sigmask;
150 sigaction(SIGINT, &newsa, &oldsa);
152 sigemptyset(&sigmask);
153 sigaddset(&sigmask, SIGPIPE);
154 newsa.sa_handler = SIG_IGN;
155 newsa.sa_mask = sigmask;
157 sigaction(SIGPIPE, &newsa, &oldsa);
159 sigemptyset(&sigmask);
160 sigaddset(&sigmask, SIGHUP);
161 newsa.sa_handler = catchHUP;
162 newsa.sa_mask = sigmask;
164 sigaction(SIGHUP, &newsa, &oldsa);