annotate src/syslog2iptables.cpp @ 17:fd00aa28682c

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