Mercurial > wflogs-daemon
comparison src/wflogs-daemon.cpp @ 0:0aa1171aebd2 stable-1-0-0
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 15 May 2013 13:15:59 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0aa1171aebd2 |
---|---|
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 // 3 - show syslog lines that match regex | |
12 // 2 - show files open/close | |
13 // 1 - show config files loading | |
14 | |
15 #include "includes.h" | |
16 #include <iostream> | |
17 #include <cstdlib> | |
18 #include <errno.h> | |
19 #include <sysexits.h> | |
20 #include <pthread.h> | |
21 #include <syslog.h> | |
22 #include <sys/wait.h> /* header for waitpid() and various macros */ | |
23 #include <signal.h> /* header for signal functions */ | |
24 | |
25 #ifndef HAVE_DAEMON | |
26 #include "daemon.h" | |
27 #include "daemon.c" | |
28 #endif | |
29 | |
30 extern "C" { | |
31 void sigchld(int sig); | |
32 void sigterm(int sig); | |
33 } | |
34 int debug_syslog = 0; | |
35 bool syslog_opened = false; | |
36 bool use_syslog = true; // false to printf | |
37 bool loader_run = true; // used to stop the config loader thread | |
38 CONFIG *config = NULL; // protected by the config_mutex | |
39 int generation = 0; // protected by the config_mutex | |
40 const int maxlen = 1000; // used for snprintf buffers | |
41 | |
42 pthread_mutex_t config_mutex; | |
43 pthread_mutex_t syslog_mutex; | |
44 | |
45 | |
46 //////////////////////////////////////////////// | |
47 // syslog a message | |
48 // | |
49 void my_syslog(const char *text) { | |
50 if (use_syslog) { | |
51 pthread_mutex_lock(&syslog_mutex); | |
52 if (!syslog_opened) { | |
53 openlog("wflogs-daemon", LOG_PID, LOG_AUTHPRIV); | |
54 syslog_opened = true; | |
55 } | |
56 syslog(LOG_NOTICE, "%s", text); | |
57 pthread_mutex_unlock(&syslog_mutex); | |
58 } | |
59 else { | |
60 printf("%s\n", text); | |
61 } | |
62 } | |
63 | |
64 //////////////////////////////////////////////// | |
65 // reload the config | |
66 // | |
67 CONFIG* new_conf(); | |
68 CONFIG* new_conf() { | |
69 CONFIG *newc = new CONFIG; | |
70 pthread_mutex_lock(&config_mutex); | |
71 newc->generation = generation++; | |
72 pthread_mutex_unlock(&config_mutex); | |
73 if (debug_syslog) { | |
74 char buf[maxlen]; | |
75 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation); | |
76 my_syslog(buf); | |
77 } | |
78 if (load_conf(*newc, "wflogs-daemon.conf")) { | |
79 newc->load_time = time(NULL); | |
80 return newc; | |
81 } | |
82 delete newc; | |
83 return NULL; | |
84 } | |
85 | |
86 | |
87 //////////////////////////////////////////////// | |
88 // thread to watch the old config files for changes | |
89 // and reload when needed. | |
90 // | |
91 void* config_loader(void *arg); | |
92 void* config_loader(void *arg) { | |
93 typedef set<CONFIG *> configp_set; | |
94 while (loader_run) { | |
95 sleep(180); // look for modifications every 3 minutes | |
96 if (!loader_run) break; | |
97 CONFIG &dc = *config; | |
98 time_t then = dc.load_time; | |
99 struct stat st; | |
100 bool reload = false; | |
101 for (string_set::const_iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
102 const char *fn = *i; | |
103 if (stat(fn, &st)) reload = true; // file disappeared | |
104 else if (st.st_mtime > then) reload = true; // file modified | |
105 if (reload) break; | |
106 } | |
107 if (reload) { | |
108 CONFIG *newc = new_conf(); | |
109 if (newc) { | |
110 // replace the global config pointer | |
111 pthread_mutex_lock(&config_mutex); | |
112 config = newc; | |
113 pthread_mutex_unlock(&config_mutex); | |
114 } | |
115 else { | |
116 // failed to load new config | |
117 my_syslog("failed to load new configuration"); | |
118 system("echo 'failed to load new /etc/wflogs-daemon.conf' | mail -s 'error in /etc/wflogs-daemon.conf' root"); | |
119 // update the load time on the current config to prevent complaining every 3 minutes | |
120 dc.load_time = time(NULL); | |
121 } | |
122 } | |
123 } | |
124 return NULL; | |
125 } | |
126 | |
127 | |
128 //////////////////////////////////////////////// | |
129 // The signal handler function for child process terminations, | |
130 // called when a child terminates. | |
131 // | |
132 void sigchld(int sig) | |
133 { | |
134 int status; | |
135 /* Wait for any child without blocking */ | |
136 while (waitpid(-1, &status, WNOHANG) > 0) { | |
137 // ignore child exit status, we only do this to cleanup zombies | |
138 } | |
139 } | |
140 | |
141 | |
142 //////////////////////////////////////////////// | |
143 // The termination signal handler function, called to | |
144 // request termination of this process. | |
145 // | |
146 void sigterm(int sig) | |
147 { | |
148 loader_run = false; | |
149 signal(sig, SIG_DFL); // quit on repeated signals | |
150 } | |
151 | |
152 | |
153 void usage(char *prog); | |
154 void usage(char *prog) | |
155 { | |
156 fprintf(stderr, "Usage: %s [-d [level]] [-c]\n", prog); | |
157 fprintf(stderr, "-c will load and dump the config to stdout\n"); | |
158 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n"); | |
159 } | |
160 | |
161 | |
162 void worker(); | |
163 void worker() | |
164 { | |
165 time_t t = time(NULL); | |
166 CONFIG *c; | |
167 pthread_mutex_lock(&config_mutex); | |
168 c = config; | |
169 c->reference_count++; | |
170 pthread_mutex_unlock(&config_mutex); | |
171 while (loader_run) { | |
172 if (c != config) { | |
173 pthread_mutex_lock(&config_mutex); | |
174 CONFIG *old = c; old->reference_count--; | |
175 c = config; c->reference_count++; | |
176 pthread_mutex_unlock(&config_mutex); | |
177 if (!old->reference_count) { | |
178 if (debug_syslog) { | |
179 char buf[maxlen]; | |
180 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation); | |
181 my_syslog(buf); | |
182 } | |
183 delete old; // destructor does all the work | |
184 } | |
185 } | |
186 if (!c->read()) { | |
187 c->sleep(2, t); | |
188 } | |
189 } | |
190 // worker shutting down | |
191 c->free_all(); | |
192 } | |
193 | |
194 | |
195 int main(int argc, char *argv[]) | |
196 { | |
197 token_init(); | |
198 bool check = false; | |
199 int c; | |
200 const char *args = "d:ch"; | |
201 extern char *optarg; | |
202 | |
203 // Process command line options | |
204 while ((c = getopt(argc, argv, args)) != -1) { | |
205 switch (c) { | |
206 case 'c': | |
207 check = true; | |
208 break; | |
209 | |
210 case 'd': | |
211 if (optarg == NULL || *optarg == '\0') debug_syslog = 1; | |
212 else debug_syslog = atoi(optarg); | |
213 break; | |
214 | |
215 case 'h': | |
216 default: | |
217 usage(argv[0]); | |
218 exit(EX_USAGE); | |
219 } | |
220 } | |
221 | |
222 if (check) { | |
223 use_syslog = false; | |
224 debug_syslog = 10; | |
225 config = new_conf(); | |
226 if (config) { | |
227 config->dump(); | |
228 delete config; | |
229 clear_strings(); // for valgrind checking | |
230 return 0; | |
231 } | |
232 else { | |
233 return 1; // config failed to load | |
234 } | |
235 } | |
236 | |
237 // switch to background mode | |
238 if (daemon(1,0) < 0) { | |
239 fprintf(stderr, "daemon() call failed\n"); | |
240 exit(EX_UNAVAILABLE); | |
241 } | |
242 | |
243 // write the pid | |
244 const char *pidpath = "/var/run/wflogs-daemon.pid"; | |
245 unlink(pidpath); | |
246 FILE *f = fopen(pidpath, "w"); | |
247 if (f) { | |
248 #ifdef linux | |
249 // from a comment in the DCC source code: | |
250 // Linux threads are broken. Signals given the | |
251 // original process are delivered to only the | |
252 // thread that happens to have that PID. The | |
253 // sendmail libmilter thread that needs to hear | |
254 // SIGINT and other signals does not, and that breaks | |
255 // scripts that need to stop milters. | |
256 // However, signaling the process group works. | |
257 fprintf(f, "-%d\n", (u_int)getpgrp()); | |
258 #else | |
259 fprintf(f, "%d\n", (u_int)getpid()); | |
260 #endif | |
261 fclose(f); | |
262 } | |
263 | |
264 // setup signal handler for termination signals | |
265 signal(SIGHUP, sigterm); | |
266 signal(SIGTERM, sigterm); | |
267 signal(SIGINT, sigterm); | |
268 | |
269 // initialize the thread sync objects | |
270 pthread_mutex_init(&config_mutex, 0); | |
271 pthread_mutex_init(&syslog_mutex, 0); | |
272 | |
273 // load the initial config | |
274 config = new_conf(); | |
275 if (!config) { | |
276 my_syslog("failed to load initial configuration, quitting"); | |
277 exit(1); | |
278 } | |
279 | |
280 // setup sigchld handler to prevent zombies | |
281 struct sigaction act; | |
282 act.sa_handler = sigchld; // Assign sig_chld as our SIGCHLD handler | |
283 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example | |
284 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated | |
285 if (sigaction(SIGCHLD, &act, NULL) < 0) { | |
286 my_syslog("failed to setup SIGCHLD handler"); | |
287 exit(1); | |
288 } | |
289 | |
290 // only create threads after the fork() in daemon | |
291 pthread_t tid; | |
292 if (pthread_create(&tid, 0, config_loader, 0)) | |
293 my_syslog("failed to create config loader thread"); | |
294 if (pthread_detach(tid)) | |
295 my_syslog("failed to detach config loader thread"); | |
296 | |
297 worker(); | |
298 if (config) delete config; // for valgrind checking | |
299 clear_strings(); // for valgrind checking | |
300 | |
301 return EXIT_SUCCESS; | |
302 } |