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