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 SSMD::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 << "SNMP Switch Management Daemon " << SSMD::version << " (revision: " << SSMD::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() << "\n"
67 << "\t- scripts base dir: " << sParser.settings().scriptBase() << "\n"
68 << "\t- dump scripts: " << (sParser.settings().dumpScripts() ? "yes" : "no") << std::endl;
71 if (sParser.settings().isDaemon()) {
72 if (daemon(0, sParser.settings().isDebug())) {
73 logger << "Failed to daemonize: " << strerror(errno) << std::endl;
77 logger.setLogFile(sParser.settings().logFile());
84 logger << "Failed to initialize SNMP" << std::endl;
88 SSMD::PIDFile pf(sParser.settings().PIDFile());
89 SSMD::Syncer syncer(sParser, snmp);
91 logger << "Starting ssmd main thread..." << std::endl;
96 syncer.run(running, reload);
98 logger << "Main thread stopped." << std::endl;
100 catch (std::exception & ex) {
101 logger << "Exception: " << ex.what() << std::endl;
112 struct sigaction newsa, oldsa;
115 sigemptyset(&sigmask);
116 sigaddset(&sigmask, SIGTERM);
117 newsa.sa_handler = SIG_IGN;
118 newsa.sa_mask = sigmask;
120 sigaction(SIGTERM, &newsa, &oldsa);
122 sigemptyset(&sigmask);
123 sigaddset(&sigmask, SIGINT);
124 newsa.sa_handler = SIG_IGN;
125 newsa.sa_mask = sigmask;
127 sigaction(SIGINT, &newsa, &oldsa);
135 void setSignalHandlers()
137 struct sigaction newsa, oldsa;
140 sigemptyset(&sigmask);
141 sigaddset(&sigmask, SIGTERM);
142 newsa.sa_handler = catchTERM;
143 newsa.sa_mask = sigmask;
145 sigaction(SIGTERM, &newsa, &oldsa);
147 sigemptyset(&sigmask);
148 sigaddset(&sigmask, SIGINT);
149 newsa.sa_handler = catchTERM;
150 newsa.sa_mask = sigmask;
152 sigaction(SIGINT, &newsa, &oldsa);
154 sigemptyset(&sigmask);
155 sigaddset(&sigmask, SIGPIPE);
156 newsa.sa_handler = SIG_IGN;
157 newsa.sa_mask = sigmask;
159 sigaction(SIGPIPE, &newsa, &oldsa);
161 sigemptyset(&sigmask);
162 sigaddset(&sigmask, SIGHUP);
163 newsa.sa_handler = catchHUP;
164 newsa.sa_mask = sigmask;
166 sigaction(SIGHUP, &newsa, &oldsa);