annotate src/syslog2iptables.cpp @ 9:d76f9ff42487

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