Mercurial > syslog2iptables
comparison src/syslog2iptables.cpp @ 5:276c4edc8521
initial coding
author | carl |
---|---|
date | Fri, 02 Dec 2005 17:52:44 -0800 |
parents | 2737ab01659a |
children | d76f9ff42487 |
comparison
equal
deleted
inserted
replaced
4:2737ab01659a | 5:276c4edc8521 |
---|---|
16 * along with this program; if not, write to the * | 16 * along with this program; if not, write to the * |
17 * Free Software Foundation, Inc., * | 17 * Free Software Foundation, Inc., * |
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * | 18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 ***************************************************************************/ | 19 ***************************************************************************/ |
20 | 20 |
21 // debug levels: | |
22 // 4 - show syslog lines that match regex | |
23 // 3 - show files open/close | |
24 // 1 - show config files loading | |
21 | 25 |
22 #ifdef HAVE_CONFIG_H | 26 #ifdef HAVE_CONFIG_H |
23 #include <config.h> | 27 #include <config.h> |
24 #endif | 28 #endif |
25 | 29 |
54 // | 58 // |
55 void my_syslog(char *text) { | 59 void my_syslog(char *text) { |
56 if (use_syslog) { | 60 if (use_syslog) { |
57 pthread_mutex_lock(&syslog_mutex); | 61 pthread_mutex_lock(&syslog_mutex); |
58 if (!syslog_opened) { | 62 if (!syslog_opened) { |
59 openlog("syslog2iptables", LOG_PID, LOG_MAIL); | 63 openlog("syslog2iptables", LOG_PID, LOG_AUTHPRIV); |
60 syslog_opened = true; | 64 syslog_opened = true; |
61 } | 65 } |
62 syslog(LOG_NOTICE, "%s", text); | 66 syslog(LOG_NOTICE, "%s", text); |
63 pthread_mutex_unlock(&syslog_mutex); | 67 pthread_mutex_unlock(&syslog_mutex); |
64 } | 68 } |
164 | 168 |
165 | 169 |
166 void usage(char *prog); | 170 void usage(char *prog); |
167 void usage(char *prog) | 171 void usage(char *prog) |
168 { | 172 { |
173 fprintf(stderr, "Usage: %s [-d [level]] [-c]\n", prog); | |
174 fprintf(stderr, "-c will load and dump the config to stdout\n"); | |
175 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n"); | |
176 } | |
177 | |
178 | |
179 void worker(); | |
180 void worker() | |
181 { | |
182 time_t t = time(NULL); | |
183 CONFIG *c; | |
184 pthread_mutex_lock(&config_mutex); | |
185 c = config; | |
186 c->reference_count++; | |
187 pthread_mutex_unlock(&config_mutex); | |
188 while (true) { | |
189 if (c != config) { | |
190 pthread_mutex_lock(&config_mutex); | |
191 c->reference_count--; | |
192 c = config; | |
193 c->reference_count++; | |
194 pthread_mutex_unlock(&config_mutex); | |
195 } | |
196 c->read(); | |
197 c->sleep(10, t); | |
198 } | |
169 } | 199 } |
170 | 200 |
171 | 201 |
172 int main(int argc, char *argv[]) | 202 int main(int argc, char *argv[]) |
173 { | 203 { |
174 token_init(); | 204 token_init(); |
175 bool check = true; | 205 bool check = false; |
176 int c; | 206 int c; |
177 const char *args = "ch"; | 207 const char *args = "d:ch"; |
178 extern char *optarg; | 208 extern char *optarg; |
179 | 209 |
180 // Process command line options | 210 // Process command line options |
181 while ((c = getopt(argc, argv, args)) != -1) { | 211 while ((c = getopt(argc, argv, args)) != -1) { |
182 switch (c) { | 212 switch (c) { |
183 case 'c': | 213 case 'c': |
184 check = true; | 214 check = true; |
185 break; | 215 break; |
186 | 216 |
217 case 'd': | |
218 if (optarg == NULL || *optarg == '\0') debug_syslog = 1; | |
219 else debug_syslog = atoi(optarg); | |
220 break; | |
221 | |
187 case 'h': | 222 case 'h': |
188 default: | 223 default: |
189 usage(argv[0]); | 224 usage(argv[0]); |
190 exit(EX_USAGE); | 225 exit(EX_USAGE); |
191 } | 226 } |
195 use_syslog = false; | 230 use_syslog = false; |
196 debug_syslog = 10; | 231 debug_syslog = 10; |
197 config = new_conf(); | 232 config = new_conf(); |
198 if (config) { | 233 if (config) { |
199 config->dump(); | 234 config->dump(); |
200 { | |
201 // just for testing | |
202 // initialize the thread sync objects | |
203 pthread_mutex_init(&config_mutex, 0); | |
204 pthread_mutex_init(&syslog_mutex, 0); | |
205 | |
206 time_t t = time(NULL); | |
207 CONFIG *c; | |
208 pthread_mutex_lock(&config_mutex); | |
209 c = config; | |
210 c->reference_count++; | |
211 pthread_mutex_unlock(&config_mutex); | |
212 while (true) { | |
213 if (c != config) { | |
214 pthread_mutex_lock(&config_mutex); | |
215 c->reference_count--; | |
216 c = config; | |
217 c->reference_count++; | |
218 pthread_mutex_unlock(&config_mutex); | |
219 } | |
220 c->read(); | |
221 c->sleep(10, t); | |
222 } | |
223 } | |
224 delete config; | 235 delete config; |
225 return 0; | 236 return 0; |
226 } | 237 } |
227 else { | 238 else { |
228 return 1; // config failed to load | 239 return 1; // config failed to load |
282 if (pthread_create(&tid, 0, config_loader, 0)) | 293 if (pthread_create(&tid, 0, config_loader, 0)) |
283 my_syslog("failed to create config loader thread"); | 294 my_syslog("failed to create config loader thread"); |
284 if (pthread_detach(tid)) | 295 if (pthread_detach(tid)) |
285 my_syslog("failed to detach config loader thread"); | 296 my_syslog("failed to detach config loader thread"); |
286 | 297 |
287 loader_run = false; // eventually the config loader thread will terminate | 298 worker(); |
299 | |
288 return EXIT_SUCCESS; | 300 return EXIT_SUCCESS; |
289 } | 301 } |