comparison src/routeflapper.cpp @ 0:48d06780cf77

initial version
author Carl Byington <carl@five-ten-sg.com>
date Tue, 13 May 2008 14:03:10 -0700
parents
children bb3f804f13a0
comparison
equal deleted inserted replaced
-1:000000000000 0:48d06780cf77
1 /*
2
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
4 the GPL version 3 or any later version at your choice available at
5 http://www.gnu.org/licenses/gpl-3.0.txt
6
7 */
8
9
10 // debug levels:
11 //14 - show regex matches
12 // 4 - show route annoucement/withdraw actions
13 // 3 - show suspicious routes announcements, and hourly totals
14 // 2 - show files open/close
15 // 1 - show config files loading
16
17 #include "includes.h"
18 #include <iostream>
19 #include <cstdlib>
20 #include <errno.h>
21 #include <sysexits.h>
22 #include <pthread.h>
23 #include <syslog.h>
24 #include <sys/wait.h> /* header for waitpid() and various macros */
25 #include <signal.h> /* header for signal functions */
26
27 #ifndef HAVE_DAEMON
28 #include "daemon.h"
29 #include "daemon.c"
30 #endif
31
32 extern "C" {
33 void sigchld(int sig);
34 void sigterm(int sig);
35 }
36 int debug_syslog = 0;
37 bool syslog_opened = false;
38 bool use_syslog = true; // false to printf
39 bool loader_run = true; // used to stop the config loader thread
40 CONFIG *config = NULL; // protected by the config_mutex
41 int generation = 0; // protected by the config_mutex
42 const int maxlen = 1000; // used for snprintf buffers
43
44 pthread_mutex_t config_mutex;
45 pthread_mutex_t syslog_mutex;
46
47
48 ////////////////////////////////////////////////
49 // syslog a message
50 //
51 void my_syslog(char *text) {
52 if (use_syslog) {
53 pthread_mutex_lock(&syslog_mutex);
54 if (!syslog_opened) {
55 openlog("routeflapper", LOG_PID, LOG_AUTHPRIV);
56 syslog_opened = true;
57 }
58 syslog(LOG_NOTICE, "%s", text);
59 pthread_mutex_unlock(&syslog_mutex);
60 }
61 else {
62 printf("%s\n", text);
63 }
64 }
65
66 ////////////////////////////////////////////////
67 // reload the config
68 //
69 CONFIG* new_conf();
70 CONFIG* new_conf() {
71 CONFIG *newc = new CONFIG;
72 pthread_mutex_lock(&config_mutex);
73 newc->generation = generation++;
74 pthread_mutex_unlock(&config_mutex);
75 if (debug_syslog) {
76 char buf[maxlen];
77 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation);
78 my_syslog(buf);
79 }
80 if (load_conf(*newc, "routeflapper.conf")) {
81 newc->load_time = time(NULL);
82 return newc;
83 }
84 delete newc;
85 return NULL;
86 }
87
88
89 ////////////////////////////////////////////////
90 // thread to update hourly statistics
91 //
92 void* hourly_update(void *arg);
93 void* hourly_update(void *arg) {
94 int count = 0;
95 while (loader_run) {
96 sleep(180);
97 if (!loader_run) break;
98 count++;
99 if (count == 20) {
100 routing_hourly_update();
101 count = 0;
102 };
103 }
104 }
105
106
107 ////////////////////////////////////////////////
108 // thread to watch the old config files for changes
109 // and reload when needed.
110 //
111 void* config_loader(void *arg);
112 void* config_loader(void *arg) {
113 typedef set<CONFIG *> configp_set;
114 while (loader_run) {
115 sleep(180); // look for modifications every 3 minutes
116 if (!loader_run) break;
117 CONFIG &dc = *config;
118 time_t then = dc.load_time;
119 struct stat st;
120 bool reload = false;
121 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
122 char *fn = *i;
123 if (stat(fn, &st)) reload = true; // file disappeared
124 else if (st.st_mtime > then) reload = true; // file modified
125 if (reload) break;
126 }
127 if (reload) {
128 CONFIG *newc = new_conf();
129 if (newc) {
130 // replace the global config pointer
131 pthread_mutex_lock(&config_mutex);
132 config = newc;
133 pthread_mutex_unlock(&config_mutex);
134 }
135 else {
136 // failed to load new config
137 my_syslog("failed to load new configuration");
138 system("echo 'failed to load new /etc/routeflapper.conf' | mail -s 'error in /etc/routeflapper.conf' root");
139 // update the load time on the current config to prevent complaining every 3 minutes
140 dc.load_time = time(NULL);
141 }
142 }
143 }
144 return NULL;
145 }
146
147
148 ////////////////////////////////////////////////
149 // The signal handler function for child process terminations,
150 // called when a child terminates.
151 //
152 void sigchld(int sig)
153 {
154 int status;
155 /* Wait for any child without blocking */
156 while (waitpid(-1, &status, WNOHANG) > 0) {
157 // ignore child exit status, we only do this to cleanup zombies
158 }
159 }
160
161
162 ////////////////////////////////////////////////
163 // The termination signal handler function, called to
164 // request termination of this process.
165 //
166 void sigterm(int sig)
167 {
168 loader_run = false;
169 signal(sig, SIG_DFL); // quit on repeated signals
170 }
171
172
173 void usage(char *prog);
174 void usage(char *prog)
175 {
176 fprintf(stderr, "Usage: %s [-d [level]] [-c]\n", prog);
177 fprintf(stderr, "-c will load and dump the config to stdout\n");
178 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n");
179 }
180
181
182 void worker();
183 void worker()
184 {
185 CONFIG *c;
186 pthread_mutex_lock(&config_mutex);
187 c = config;
188 c->reference_count++;
189 pthread_mutex_unlock(&config_mutex);
190 while (loader_run) {
191 if (c != config) {
192 pthread_mutex_lock(&config_mutex);
193 CONFIG *old = c; old->reference_count--;
194 c = config; c->reference_count++;
195 pthread_mutex_unlock(&config_mutex);
196 if (!old->reference_count) {
197 if (debug_syslog) {
198 char buf[maxlen];
199 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation);
200 my_syslog(buf);
201 }
202 delete old; // destructor does all the work
203 }
204 }
205 c->read();
206 ::sleep(2);
207 }
208 }
209
210
211 int main(int argc, char *argv[])
212 {
213 token_init();
214 bool check = false;
215 int c;
216 const char *args = "d:ch";
217 extern char *optarg;
218
219 // Process command line options
220 while ((c = getopt(argc, argv, args)) != -1) {
221 switch (c) {
222 case 'c':
223 check = true;
224 break;
225
226 case 'd':
227 if (optarg == NULL || *optarg == '\0') debug_syslog = 1;
228 else debug_syslog = atoi(optarg);
229 break;
230
231 case 'h':
232 default:
233 usage(argv[0]);
234 exit(EX_USAGE);
235 }
236 }
237
238 if (check) {
239 use_syslog = false;
240 debug_syslog = 10;
241 config = new_conf();
242 if (config) {
243 config->dump();
244 delete config;
245 clear_strings(); // for valgrind checking
246 return 0;
247 }
248 else {
249 return 1; // config failed to load
250 }
251 }
252
253 // switch to background mode
254 if (daemon(1,0) < 0) {
255 fprintf(stderr, "daemon() call failed\n");
256 exit(EX_UNAVAILABLE);
257 }
258
259 // write the pid
260 const char *pidpath = "/var/run/routeflapper.pid";
261 unlink(pidpath);
262 FILE *f = fopen(pidpath, "w");
263 if (f) {
264 #ifdef linux
265 // from a comment in the DCC source code:
266 // Linux threads are broken. Signals given the
267 // original process are delivered to only the
268 // thread that happens to have that PID. The
269 // sendmail libmilter thread that needs to hear
270 // SIGINT and other signals does not, and that breaks
271 // scripts that need to stop milters.
272 // However, signaling the process group works.
273 fprintf(f, "-%d\n", (u_int)getpgrp());
274 #else
275 fprintf(f, "%d\n", (u_int)getpid());
276 #endif
277 fclose(f);
278 }
279
280 // setup signal handler for termination signals
281 signal(SIGHUP, sigterm);
282 signal(SIGTERM, sigterm);
283 signal(SIGINT, sigterm);
284
285 // initialize the thread sync objects
286 pthread_mutex_init(&config_mutex, 0);
287 pthread_mutex_init(&syslog_mutex, 0);
288
289 // load the initial config
290 config = new_conf();
291 if (!config) {
292 my_syslog("failed to load initial configuration, quitting");
293 exit(1);
294 }
295
296 // setup sigchld handler to prevent zombies
297 struct sigaction act;
298 act.sa_handler = sigchld; // Assign sig_chld as our SIGCHLD handler
299 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example
300 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated
301 if (sigaction(SIGCHLD, &act, NULL) < 0) {
302 my_syslog("failed to setup SIGCHLD handler");
303 exit(1);
304 }
305
306 // only create threads after the fork() in daemon
307 pthread_t tid;
308 if (pthread_create(&tid, 0, config_loader, 0))
309 my_syslog("failed to create config loader thread");
310 if (pthread_detach(tid))
311 my_syslog("failed to detach config loader thread");
312 if (pthread_create(&tid, 0, hourly_update, 0))
313 my_syslog("failed to create hourly update thread");
314 if (pthread_detach(tid))
315 my_syslog("failed to detach hourly update thread");
316
317 worker();
318 if (config) delete config; // for valgrind checking
319 clear_strings(); // for valgrind checking
320 clear_rib(); // for valgrind checking
321
322 return EXIT_SUCCESS;
323 }