comparison src/syslog2iptables.cpp @ 1:551433a01cab

initial coding
author carl
date Wed, 23 Nov 2005 19:29:14 -0800
parents
children 6e88da080f08
comparison
equal deleted inserted replaced
0:a3b8d64b2ae5 1:551433a01cab
1 /***************************************************************************
2 * Copyright (C) 2005 by 510 Software Group *
3 * *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <iostream>
27 #include <cstdlib>
28 #include <errno.h>
29 #include <sysexits.h>
30 #include <unistd.h>
31 #include <pthread.h>
32 #include <syslog.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h> /* header for waitpid() and various macros */
36 #include <signal.h> /* header for signal functions */
37 #include "includes.h"
38
39 int debug_syslog = 0;
40 bool syslog_opened = false;
41 bool use_syslog = true; // false to printf
42 bool loader_run = true; // used to stop the config loader thread
43 CONFIG *config = NULL; // protected by the config_mutex
44 int generation = 0; // protected by the config_mutex
45 const int maxlen = 1000; // used for snprintf buffers
46
47 pthread_mutex_t config_mutex;
48 pthread_mutex_t syslog_mutex;
49
50
51 ////////////////////////////////////////////////
52 // syslog a message
53 //
54 void my_syslog(char *text) {
55 if (use_syslog) {
56 pthread_mutex_lock(&syslog_mutex);
57 if (!syslog_opened) {
58 openlog("syslog2iptables", LOG_PID, LOG_MAIL);
59 syslog_opened = true;
60 }
61 syslog(LOG_NOTICE, "%s", text);
62 pthread_mutex_unlock(&syslog_mutex);
63 }
64 else {
65 printf("%s \n", text);
66 }
67 }
68
69 ////////////////////////////////////////////////
70 // reload the config
71 //
72 CONFIG* new_conf();
73 CONFIG* new_conf() {
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, "syslog2iptables.conf")) {
84 newc->load_time = time(NULL);
85 return newc;
86 }
87 delete newc;
88 return NULL;
89 }
90
91
92 ////////////////////////////////////////////////
93 // thread to watch the old config files for changes
94 // and reload when needed. we also cleanup old
95 // configs whose reference count has gone to zero.
96 //
97 void* config_loader(void *arg);
98 void* config_loader(void *arg) {
99 typedef set<CONFIG *> configp_set;
100 configp_set old_configs;
101 while (loader_run) {
102 sleep(180); // look for modifications every 3 minutes
103 if (!loader_run) break;
104 CONFIG &dc = *config;
105 time_t then = dc.load_time;
106 struct stat st;
107 bool reload = false;
108 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
109 char *fn = *i;
110 if (stat(fn, &st)) reload = true; // file disappeared
111 else if (st.st_mtime > then) reload = true; // file modified
112 if (reload) break;
113 }
114 if (reload) {
115 CONFIG *newc = new_conf();
116 if (newc) {
117 // replace the global config pointer
118 pthread_mutex_lock(&config_mutex);
119 CONFIG *old = config;
120 config = newc;
121 pthread_mutex_unlock(&config_mutex);
122 if (old) old_configs.insert(old);
123 }
124 else {
125 // failed to load new config
126 my_syslog("failed to load new configuration");
127 system("echo 'failed to load new /etc/syslog2iptables.conf' | mail -s 'error in /etc/syslog2iptables.conf' root");
128 // update the load time on the current config to prevent complaining every 3 minutes
129 dc.load_time = time(NULL);
130 }
131 }
132 // now look for old configs with zero ref counts
133 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
134 CONFIG *old = *i;
135 if (!old->reference_count) {
136 if (debug_syslog) {
137 char buf[maxlen];
138 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation);
139 my_syslog(buf);
140 }
141 delete old; // destructor does all the work
142 old_configs.erase(i++);
143 }
144 else i++;
145 }
146 }
147 return NULL;
148 }
149
150
151 ////////////////////////////////////////////////
152 // The signal handler function -- only gets called when a SIGCHLD
153 // is received, ie when a child terminates
154 //
155 void sig_chld(int signo)
156 {
157 int status;
158 /* Wait for any child without blocking */
159 while (waitpid(-1, &status, WNOHANG) > 0) {
160 // ignore child exit status, we only do this to cleanup zombies
161 }
162 }
163
164
165 void usage(char *prog);
166 void usage(char *prog)
167 {
168 }
169
170
171 int main(int argc, char *argv[])
172 {
173 token_init();
174 bool check = true;
175 int c;
176 const char *args = "ch";
177 extern char *optarg;
178
179 // Process command line options
180 while ((c = getopt(argc, argv, args)) != -1) {
181 switch (c) {
182 case 'c':
183 check = true;
184 break;
185
186 case 'h':
187 default:
188 usage(argv[0]);
189 exit(EX_USAGE);
190 }
191 }
192
193 if (check) {
194 use_syslog = false;
195 debug_syslog = 10;
196 CONFIG *conf = new_conf();
197 if (conf) {
198 conf->dump();
199 delete conf;
200 return 0;
201 }
202 else {
203 return 1; // config failed to load
204 }
205 }
206
207 // switch to background mode
208 if (daemon(1,0) < 0) {
209 fprintf(stderr, "daemon() call failed\n");
210 exit(EX_UNAVAILABLE);
211 }
212
213 // write the pid
214 const char *pidpath = "/var/run/syslog2iptables.pid";
215 unlink(pidpath);
216 FILE *f = fopen(pidpath, "w");
217 if (f) {
218 #ifdef linux
219 // from a comment in the DCC source code:
220 // Linux threads are broken. Signals given the
221 // original process are delivered to only the
222 // thread that happens to have that PID. The
223 // sendmail libmilter thread that needs to hear
224 // SIGINT and other signals does not, and that breaks
225 // scripts that need to stop milters.
226 // However, signaling the process group works.
227 fprintf(f, "-%d\n", (u_int)getpgrp());
228 #else
229 fprintf(f, "%d\n", (u_int)getpid());
230 #endif
231 fclose(f);
232 }
233
234 // initialize the thread sync objects
235 pthread_mutex_init(&config_mutex, 0);
236 pthread_mutex_init(&syslog_mutex, 0);
237
238 // load the initial config
239 config = new_conf();
240 if (!config) {
241 my_syslog("failed to load initial configuration, quitting");
242 exit(1);
243 }
244
245 // only create threads after the fork() in daemon
246 pthread_t tid;
247 if (pthread_create(&tid, 0, config_loader, 0))
248 my_syslog("failed to create config loader thread");
249 if (pthread_detach(tid))
250 my_syslog("failed to detach config loader thread");
251
252 loader_run = false; // eventually the config loader thread will terminate
253 return EXIT_SUCCESS;
254 }