Mercurial > routeflapper
annotate src/routeflapper.cpp @ 2:bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 19 May 2008 21:45:45 -0700 |
parents | 48d06780cf77 |
children | 4a81cc2da570 |
rev | line source |
---|---|
0 | 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 while (loader_run) { | |
2
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
95 int ran = (rand() % 600) - 300; // random(-5min,+5min) |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
96 int hour = 3600 - 60 + ran; // 59 minutes +- random up to 5 minutes. |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
97 int period = (hour + 10) / 20; // twenty periods ... |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
98 int count = 0; |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
99 while (loader_run) { |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
100 sleep(period); |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
101 if (!loader_run) break; // ... so we only wait about 3 minutes to terminate |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
102 count++; |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
103 if (count == 20) { |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
104 routing_hourly_update(); // roughly an hour |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
105 count = 0; |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
106 }; |
bb3f804f13a0
add random unsynchronization to hourly timer, trust prefix only for same origin AS, ignore self adjacency
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
107 } |
0 | 108 } |
109 } | |
110 | |
111 | |
112 //////////////////////////////////////////////// | |
113 // thread to watch the old config files for changes | |
114 // and reload when needed. | |
115 // | |
116 void* config_loader(void *arg); | |
117 void* config_loader(void *arg) { | |
118 typedef set<CONFIG *> configp_set; | |
119 while (loader_run) { | |
120 sleep(180); // look for modifications every 3 minutes | |
121 if (!loader_run) break; | |
122 CONFIG &dc = *config; | |
123 time_t then = dc.load_time; | |
124 struct stat st; | |
125 bool reload = false; | |
126 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
127 char *fn = *i; | |
128 if (stat(fn, &st)) reload = true; // file disappeared | |
129 else if (st.st_mtime > then) reload = true; // file modified | |
130 if (reload) break; | |
131 } | |
132 if (reload) { | |
133 CONFIG *newc = new_conf(); | |
134 if (newc) { | |
135 // replace the global config pointer | |
136 pthread_mutex_lock(&config_mutex); | |
137 config = newc; | |
138 pthread_mutex_unlock(&config_mutex); | |
139 } | |
140 else { | |
141 // failed to load new config | |
142 my_syslog("failed to load new configuration"); | |
143 system("echo 'failed to load new /etc/routeflapper.conf' | mail -s 'error in /etc/routeflapper.conf' root"); | |
144 // update the load time on the current config to prevent complaining every 3 minutes | |
145 dc.load_time = time(NULL); | |
146 } | |
147 } | |
148 } | |
149 return NULL; | |
150 } | |
151 | |
152 | |
153 //////////////////////////////////////////////// | |
154 // The signal handler function for child process terminations, | |
155 // called when a child terminates. | |
156 // | |
157 void sigchld(int sig) | |
158 { | |
159 int status; | |
160 /* Wait for any child without blocking */ | |
161 while (waitpid(-1, &status, WNOHANG) > 0) { | |
162 // ignore child exit status, we only do this to cleanup zombies | |
163 } | |
164 } | |
165 | |
166 | |
167 //////////////////////////////////////////////// | |
168 // The termination signal handler function, called to | |
169 // request termination of this process. | |
170 // | |
171 void sigterm(int sig) | |
172 { | |
173 loader_run = false; | |
174 signal(sig, SIG_DFL); // quit on repeated signals | |
175 } | |
176 | |
177 | |
178 void usage(char *prog); | |
179 void usage(char *prog) | |
180 { | |
181 fprintf(stderr, "Usage: %s [-d [level]] [-c]\n", prog); | |
182 fprintf(stderr, "-c will load and dump the config to stdout\n"); | |
183 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n"); | |
184 } | |
185 | |
186 | |
187 void worker(); | |
188 void worker() | |
189 { | |
190 CONFIG *c; | |
191 pthread_mutex_lock(&config_mutex); | |
192 c = config; | |
193 c->reference_count++; | |
194 pthread_mutex_unlock(&config_mutex); | |
195 while (loader_run) { | |
196 if (c != config) { | |
197 pthread_mutex_lock(&config_mutex); | |
198 CONFIG *old = c; old->reference_count--; | |
199 c = config; c->reference_count++; | |
200 pthread_mutex_unlock(&config_mutex); | |
201 if (!old->reference_count) { | |
202 if (debug_syslog) { | |
203 char buf[maxlen]; | |
204 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation); | |
205 my_syslog(buf); | |
206 } | |
207 delete old; // destructor does all the work | |
208 } | |
209 } | |
210 c->read(); | |
211 ::sleep(2); | |
212 } | |
213 } | |
214 | |
215 | |
216 int main(int argc, char *argv[]) | |
217 { | |
218 token_init(); | |
219 bool check = false; | |
220 int c; | |
221 const char *args = "d:ch"; | |
222 extern char *optarg; | |
223 | |
224 // Process command line options | |
225 while ((c = getopt(argc, argv, args)) != -1) { | |
226 switch (c) { | |
227 case 'c': | |
228 check = true; | |
229 break; | |
230 | |
231 case 'd': | |
232 if (optarg == NULL || *optarg == '\0') debug_syslog = 1; | |
233 else debug_syslog = atoi(optarg); | |
234 break; | |
235 | |
236 case 'h': | |
237 default: | |
238 usage(argv[0]); | |
239 exit(EX_USAGE); | |
240 } | |
241 } | |
242 | |
243 if (check) { | |
244 use_syslog = false; | |
245 debug_syslog = 10; | |
246 config = new_conf(); | |
247 if (config) { | |
248 config->dump(); | |
249 delete config; | |
250 clear_strings(); // for valgrind checking | |
251 return 0; | |
252 } | |
253 else { | |
254 return 1; // config failed to load | |
255 } | |
256 } | |
257 | |
258 // switch to background mode | |
259 if (daemon(1,0) < 0) { | |
260 fprintf(stderr, "daemon() call failed\n"); | |
261 exit(EX_UNAVAILABLE); | |
262 } | |
263 | |
264 // write the pid | |
265 const char *pidpath = "/var/run/routeflapper.pid"; | |
266 unlink(pidpath); | |
267 FILE *f = fopen(pidpath, "w"); | |
268 if (f) { | |
269 #ifdef linux | |
270 // from a comment in the DCC source code: | |
271 // Linux threads are broken. Signals given the | |
272 // original process are delivered to only the | |
273 // thread that happens to have that PID. The | |
274 // sendmail libmilter thread that needs to hear | |
275 // SIGINT and other signals does not, and that breaks | |
276 // scripts that need to stop milters. | |
277 // However, signaling the process group works. | |
278 fprintf(f, "-%d\n", (u_int)getpgrp()); | |
279 #else | |
280 fprintf(f, "%d\n", (u_int)getpid()); | |
281 #endif | |
282 fclose(f); | |
283 } | |
284 | |
285 // setup signal handler for termination signals | |
286 signal(SIGHUP, sigterm); | |
287 signal(SIGTERM, sigterm); | |
288 signal(SIGINT, sigterm); | |
289 | |
290 // initialize the thread sync objects | |
291 pthread_mutex_init(&config_mutex, 0); | |
292 pthread_mutex_init(&syslog_mutex, 0); | |
293 | |
294 // load the initial config | |
295 config = new_conf(); | |
296 if (!config) { | |
297 my_syslog("failed to load initial configuration, quitting"); | |
298 exit(1); | |
299 } | |
300 | |
301 // setup sigchld handler to prevent zombies | |
302 struct sigaction act; | |
303 act.sa_handler = sigchld; // Assign sig_chld as our SIGCHLD handler | |
304 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example | |
305 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated | |
306 if (sigaction(SIGCHLD, &act, NULL) < 0) { | |
307 my_syslog("failed to setup SIGCHLD handler"); | |
308 exit(1); | |
309 } | |
310 | |
311 // only create threads after the fork() in daemon | |
312 pthread_t tid; | |
313 if (pthread_create(&tid, 0, config_loader, 0)) | |
314 my_syslog("failed to create config loader thread"); | |
315 if (pthread_detach(tid)) | |
316 my_syslog("failed to detach config loader thread"); | |
317 if (pthread_create(&tid, 0, hourly_update, 0)) | |
318 my_syslog("failed to create hourly update thread"); | |
319 if (pthread_detach(tid)) | |
320 my_syslog("failed to detach hourly update thread"); | |
321 | |
322 worker(); | |
323 if (config) delete config; // for valgrind checking | |
324 clear_strings(); // for valgrind checking | |
325 clear_rib(); // for valgrind checking | |
326 | |
327 return EXIT_SUCCESS; | |
328 } |