#include // daemon #include // errno #include // strerror #include #include #include #include "snmp_pp/snmp_pp.h" #include "syncer.h" #include "version.h" #include "settings.h" #include "pidfile.h" #include "logger.h" GTS::Logger logger; static bool running = true; static bool reload = false; void setSignalHandlers(); int main(int argc, char * argv[]) { GTS::SettingsParser sParser; // Set filter for logging DefaultLog::log()->set_filter(ERROR_LOG, 7); DefaultLog::log()->set_filter(WARNING_LOG, 7); DefaultLog::log()->set_filter(EVENT_LOG, 0); DefaultLog::log()->set_filter(INFO_LOG, 0); DefaultLog::log()->set_filter(DEBUG_LOG, 0); try { sParser.init(argc, argv); } catch (const std::exception & ex) { logger << ex.what() << std::endl; return -1; } if (sParser.settings().isHelp()) { sParser.printHelp(); return 0; } if (sParser.settings().isVersion()) { std::cout << "GTS SNMP Switch Management Daemon " << GTS::version << " (revision: " << GTS::revision << ")" << std::endl; return 0; } if (sParser.settings().isDebug()) { std::cout << "Settings dump:\n" << "\t- help: " << (sParser.settings().isHelp() ? "yes" : "no") << "\n" << "\t- version: " << (sParser.settings().isVersion() ? "yes" : "no") << "\n" << "\t- debug: " << (sParser.settings().isDebug() ? "yes" : "no") << "\n" << "\t- daemon: " << (sParser.settings().isDaemon() ? "yes" : "no") << "\n" << "\t- config file: " << sParser.settings().configFile() << "\n" << "\t- log file: " << sParser.settings().logFile() << "\n" << "\t- PID file: " << sParser.settings().PIDFile() << "\n" << "\t- switch sync interval: " << sParser.settings().switchSyncInterval() << "\n" << "\t- info sync interval: " << sParser.settings().infoSyncInterval() << "\n" << "\t- switch's upload profile id: " << sParser.settings().upProfileId() << "\n" << "\t- switch's download profile id: " << sParser.settings().downProfileId() << "\n" << "\t- max ACL's per PDU: " << sParser.settings().maxACLPerPDU() << "\n" << "\t- data URL: " << sParser.settings().dataURL() << std::endl; } if (sParser.settings().isDaemon()) { if (daemon(0, sParser.settings().isDebug())) { logger << "Failed to daemonize: " << strerror(errno) << std::endl; return -1; } } logger.setLogFile(sParser.settings().logFile()); try { int status = 0; Snmp snmp(status); if (status) { logger << "Failed to initialize SNMP" << std::endl; return -1; } GTS::PIDFile pf(sParser.settings().PIDFile()); GTS::Syncer syncer(sParser, snmp); logger << "Starting gssmd main thread..." << std::endl; setSignalHandlers(); // Start main thread syncer.run(running, reload); logger << "Main thread stopped." << std::endl; } catch (std::exception & ex) { logger << "Exception: " << ex.what() << std::endl; return -1; } return 0; } void catchTERM(int) { running = false; struct sigaction newsa, oldsa; sigset_t sigmask; sigemptyset(&sigmask); sigaddset(&sigmask, SIGTERM); newsa.sa_handler = SIG_IGN; newsa.sa_mask = sigmask; newsa.sa_flags = 0; sigaction(SIGTERM, &newsa, &oldsa); sigemptyset(&sigmask); sigaddset(&sigmask, SIGINT); newsa.sa_handler = SIG_IGN; newsa.sa_mask = sigmask; newsa.sa_flags = 0; sigaction(SIGINT, &newsa, &oldsa); } void catchHUP(int) { reload = true; } void setSignalHandlers() { struct sigaction newsa, oldsa; sigset_t sigmask; sigemptyset(&sigmask); sigaddset(&sigmask, SIGTERM); newsa.sa_handler = catchTERM; newsa.sa_mask = sigmask; newsa.sa_flags = 0; sigaction(SIGTERM, &newsa, &oldsa); sigemptyset(&sigmask); sigaddset(&sigmask, SIGINT); newsa.sa_handler = catchTERM; newsa.sa_mask = sigmask; newsa.sa_flags = 0; sigaction(SIGINT, &newsa, &oldsa); sigemptyset(&sigmask); sigaddset(&sigmask, SIGPIPE); newsa.sa_handler = SIG_IGN; newsa.sa_mask = sigmask; newsa.sa_flags = 0; sigaction(SIGPIPE, &newsa, &oldsa); sigemptyset(&sigmask); sigaddset(&sigmask, SIGHUP); newsa.sa_handler = catchHUP; newsa.sa_mask = sigmask; newsa.sa_flags = 0; sigaction(SIGHUP, &newsa, &oldsa); }