annotate src/syslog2iptables.cpp @ 5:276c4edc8521

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