annotate src/wflogs-daemon.cpp @ 11:1b7813f588ba stable-1-0-1

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