]> git.stg.codes - ssmd.git/blob - src/main.cpp
Initial adding
[ssmd.git] / src / main.cpp
1 #include <unistd.h> // daemon
2
3 #include <cerrno> // errno
4 #include <cstring> // strerror
5 #include <csignal>
6 #include <iostream>
7 #include <exception>
8
9 #include "snmp_pp/snmp_pp.h"
10
11 #include "syncer.h"
12 #include "version.h"
13 #include "settings.h"
14 #include "pidfile.h"
15 #include "logger.h"
16
17 GTS::Logger logger;
18 static bool running = true;
19 static bool reload = false;
20
21 void setSignalHandlers();
22
23 int main(int argc, char * argv[])
24 {
25     GTS::SettingsParser sParser;
26
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);
33
34     try {
35         sParser.init(argc, argv);
36     }
37     catch (const std::exception & ex) {
38         logger << ex.what() << std::endl;
39         return -1;
40     }
41
42     if (sParser.settings().isHelp()) {
43         sParser.printHelp();
44         return 0;
45     }
46
47     if (sParser.settings().isVersion()) {
48         std::cout << "GTS SNMP Switch Management Daemon " << GTS::version << " (revision: " << GTS::revision << ")" << std::endl;
49         return 0;
50     }
51
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;
66     }
67
68     if (sParser.settings().isDaemon()) {
69         if (daemon(0, sParser.settings().isDebug())) {
70             logger << "Failed to daemonize: " << strerror(errno) << std::endl;
71             return -1;
72         }
73     }
74     logger.setLogFile(sParser.settings().logFile());
75
76     try {
77         int status = 0;
78         Snmp snmp(status);
79
80         if (status) {
81             logger << "Failed to initialize SNMP" << std::endl;
82             return -1;
83         }
84
85         GTS::PIDFile pf(sParser.settings().PIDFile());
86         GTS::Syncer syncer(sParser, snmp);
87
88         logger << "Starting gssmd main thread..." << std::endl;
89
90         setSignalHandlers();
91
92         // Start main thread
93         syncer.run(running, reload);
94
95         logger << "Main thread stopped." << std::endl;
96     }
97     catch (std::exception & ex) {
98         logger << "Exception: " << ex.what() << std::endl;
99         return -1;
100     }
101
102     return 0;
103 }
104
105 void catchTERM(int)
106 {
107     running = false;
108
109     struct sigaction newsa, oldsa;
110     sigset_t sigmask;
111
112     sigemptyset(&sigmask);
113     sigaddset(&sigmask, SIGTERM);
114     newsa.sa_handler = SIG_IGN;
115     newsa.sa_mask = sigmask;
116     newsa.sa_flags = 0;
117     sigaction(SIGTERM, &newsa, &oldsa);
118
119     sigemptyset(&sigmask);
120     sigaddset(&sigmask, SIGINT);
121     newsa.sa_handler = SIG_IGN;
122     newsa.sa_mask = sigmask;
123     newsa.sa_flags = 0;
124     sigaction(SIGINT, &newsa, &oldsa);
125 }
126
127 void catchHUP(int)
128 {
129     reload = true;
130 }
131
132 void setSignalHandlers()
133 {
134     struct sigaction newsa, oldsa;
135     sigset_t sigmask;
136
137     sigemptyset(&sigmask);
138     sigaddset(&sigmask, SIGTERM);
139     newsa.sa_handler = catchTERM;
140     newsa.sa_mask = sigmask;
141     newsa.sa_flags = 0;
142     sigaction(SIGTERM, &newsa, &oldsa);
143
144     sigemptyset(&sigmask);
145     sigaddset(&sigmask, SIGINT);
146     newsa.sa_handler = catchTERM;
147     newsa.sa_mask = sigmask;
148     newsa.sa_flags = 0;
149     sigaction(SIGINT, &newsa, &oldsa);
150
151     sigemptyset(&sigmask);
152     sigaddset(&sigmask, SIGPIPE);
153     newsa.sa_handler = SIG_IGN;
154     newsa.sa_mask = sigmask;
155     newsa.sa_flags = 0;
156     sigaction(SIGPIPE, &newsa, &oldsa);
157
158     sigemptyset(&sigmask);
159     sigaddset(&sigmask, SIGHUP);
160     newsa.sa_handler = catchHUP;
161     newsa.sa_mask = sigmask;
162     newsa.sa_flags = 0;
163     sigaction(SIGHUP, &newsa, &oldsa);
164 }