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