- fixed log-to-syslog behavior
[strongswan.git] / Source / charon / daemon.c
1 /**
2 * @file daemon.c
3 *
4 * @brief Implementation of daemon_t and main of IKEv2-Daemon.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdio.h>
24 #include <signal.h>
25 #include <pthread.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <execinfo.h>
30 #include <string.h>
31
32 #include "daemon.h"
33
34 #include <types.h>
35
36
37 typedef struct private_daemon_t private_daemon_t;
38
39 /**
40 * Private additions to daemon_t, contains threads and internal functions.
41 */
42 struct private_daemon_t {
43 /**
44 * Public members of daemon_t.
45 */
46 daemon_t public;
47
48 /**
49 * A logger_t object assigned for daemon things.
50 */
51 logger_t *logger;
52
53 /**
54 * Signal set used for signal handling.
55 */
56 sigset_t signal_set;
57
58 /**
59 * The thread_id of main-thread.
60 */
61 pthread_t main_thread_id;
62
63 /**
64 * Main loop function.
65 *
66 * @param this calling object
67 */
68 void (*run) (private_daemon_t *this);
69
70 /**
71 * Initialize the daemon.
72 *
73 * @param this calling object
74 */
75 void (*initialize) (private_daemon_t *this);
76
77 /**
78 * Destroy the daemon.
79 *
80 * @param this calling object
81 */
82 void (*destroy) (private_daemon_t *this);
83 };
84
85 /**
86 * One and only instance of the daemon.
87 */
88 daemon_t *charon;
89
90 /**
91 * Implementation of private_daemon_t.run.
92 */
93 static void run(private_daemon_t *this)
94 {
95 /* reselect signals for this thread */
96 sigemptyset(&(this->signal_set));
97 sigaddset(&(this->signal_set), SIGINT);
98 sigaddset(&(this->signal_set), SIGHUP);
99 sigaddset(&(this->signal_set), SIGTERM);
100 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
101
102 while(TRUE)
103 {
104 int signal_number;
105 int error;
106
107 error = sigwait(&(this->signal_set), &signal_number);
108 if(error)
109 {
110 this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
111 return;
112 }
113 switch (signal_number)
114 {
115 case SIGHUP:
116 {
117 this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
118 break;
119 }
120 case SIGINT:
121 {
122 this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
123 return;
124 }
125 case SIGTERM:
126 this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
127 return;
128 default:
129 {
130 this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
131 break;
132 }
133 }
134 }
135 }
136
137 /**
138 * Implementation of daemon_t.kill.
139 */
140 static void kill_daemon(private_daemon_t *this, char *reason)
141 {
142 /* we send SIGTERM, so the daemon can cleanly shut down */
143 this->logger->log(this->logger, CONTROL, "Killing daemon: %s", reason);
144 if (this->main_thread_id == pthread_self())
145 {
146 /* initialization failed, terminate daemon */
147 this->destroy(this);
148 unlink(PID_FILE);
149 exit(-1);
150 }
151 else
152 {
153 this->logger->log(this->logger, CONTROL, "sending SIGTERM to ourself", reason);
154 kill(0, SIGTERM);
155 /* thread must die, since he produced a ciritcal failure and can't continue */
156 pthread_exit(NULL);
157 }
158 }
159
160 /**
161 * Implementation of private_daemon_t.initialize.
162 */
163 static void initialize(private_daemon_t *this)
164 {
165 this->public.configuration = configuration_create();
166 this->public.socket = socket_create(IKEV2_UDP_PORT);
167 this->public.ike_sa_manager = ike_sa_manager_create();
168 this->public.job_queue = job_queue_create();
169 this->public.event_queue = event_queue_create();
170 this->public.send_queue = send_queue_create();
171 this->public.stroke = stroke_create();
172 this->public.connections = &this->public.stroke->connections;
173 this->public.policies = &this->public.stroke->policies;
174 this->public.credentials = &this->public.stroke->credentials;
175
176 this->public.sender = sender_create();
177 this->public.receiver = receiver_create();
178 this->public.scheduler = scheduler_create();
179 this->public.kernel_interface = kernel_interface_create();
180 this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
181 }
182
183 /**
184 * Destory all initiated objects
185 */
186 static void destroy(private_daemon_t *this)
187 {
188 if (this->public.ike_sa_manager != NULL)
189 {
190 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
191 }
192 if (this->public.kernel_interface != NULL)
193 {
194 this->public.kernel_interface->destroy(this->public.kernel_interface);
195 }
196 if (this->public.receiver != NULL)
197 {
198 this->public.receiver->destroy(this->public.receiver);
199 }
200 if (this->public.scheduler != NULL)
201 {
202 this->public.scheduler->destroy(this->public.scheduler);
203 }
204 if (this->public.sender != NULL)
205 {
206 this->public.sender->destroy(this->public.sender);
207 }
208 if (this->public.thread_pool != NULL)
209 {
210 this->public.thread_pool->destroy(this->public.thread_pool);
211 }
212 if (this->public.job_queue != NULL)
213 {
214 this->public.job_queue->destroy(this->public.job_queue);
215 }
216 if (this->public.event_queue != NULL)
217 {
218 this->public.event_queue->destroy(this->public.event_queue);
219 }
220 if (this->public.send_queue != NULL)
221 {
222 this->public.send_queue->destroy(this->public.send_queue);
223 }
224 if (this->public.socket != NULL)
225 {
226 this->public.socket->destroy(this->public.socket);
227 }
228 if (this->public.configuration != NULL)
229 {
230 this->public.configuration->destroy(this->public.configuration);
231 }
232 if (this->public.credentials != NULL)
233 {
234 this->public.credentials->destroy(this->public.credentials);
235 }
236 if (this->public.connections != NULL)
237 {
238 this->public.connections->destroy(this->public.connections);
239 }
240 if (this->public.policies != NULL)
241 {
242 this->public.policies->destroy(this->public.policies);
243 }
244 if (this->public.stroke != NULL)
245 {
246 this->public.stroke->destroy(this->public.stroke);
247 }
248 free(this);
249 }
250
251 void signal_handler(int signal)
252 {
253 void *array[20];
254 size_t size;
255 char **strings;
256 size_t i;
257 logger_t *logger;
258
259 size = backtrace(array, 20);
260 strings = backtrace_symbols(array, size);
261 logger = logger_manager->get_logger(logger_manager, DAEMON);
262
263 logger->log(logger, ERROR, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size);
264
265 for (i = 0; i < size; i++)
266 {
267 logger->log(logger, ERROR, " %s", strings[i]);
268 }
269 free (strings);
270 logger->log(logger, ERROR, "Killing ourself hard after SIGSEGV");
271 kill(getpid(), SIGKILL);
272 }
273
274 /**
275 * @brief Create the daemon.
276 *
277 * @return created daemon_t
278 */
279 private_daemon_t *daemon_create()
280 {
281 private_daemon_t *this = malloc_thing(private_daemon_t);
282 struct sigaction action;
283
284 /* assign methods */
285 this->run = run;
286 this->destroy = destroy;
287 this->initialize = initialize;
288 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
289
290 /* NULL members for clean destruction */
291 this->public.socket = NULL;
292 this->public.ike_sa_manager = NULL;
293 this->public.job_queue = NULL;
294 this->public.event_queue = NULL;
295 this->public.send_queue = NULL;
296 this->public.configuration = NULL;
297 this->public.credentials = NULL;
298 this->public.connections = NULL;
299 this->public.policies = NULL;
300 this->public.sender= NULL;
301 this->public.receiver = NULL;
302 this->public.scheduler = NULL;
303 this->public.kernel_interface = NULL;
304 this->public.thread_pool = NULL;
305 this->public.stroke = NULL;
306
307 this->main_thread_id = pthread_self();
308
309 /* setup signal handling for all threads */
310 sigemptyset(&(this->signal_set));
311 sigaddset(&(this->signal_set), SIGSEGV);
312 sigaddset(&(this->signal_set), SIGINT);
313 sigaddset(&(this->signal_set), SIGHUP);
314 sigaddset(&(this->signal_set), SIGTERM);
315 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
316
317 /* setup SIGSEGV handler for all threads */
318 action.sa_handler = signal_handler;
319 action.sa_mask = this->signal_set;
320 action.sa_flags = 0;
321 if (sigaction(SIGSEGV, &action, NULL) == -1)
322 {
323 this->logger->log(this->logger, ERROR, "signal handler setup for SIGSEGV failed");
324 }
325 return this;
326 }
327
328 /**
329 * Main function, manages the daemon.
330 */
331 int main(int argc, char *argv[])
332 {
333 private_daemon_t *private_charon;
334 FILE *pid_file;
335 struct stat stb;
336 int i;
337
338 /* trivial argument parsing */
339 for (i = 1; i < argc; i++)
340 {
341 if (strcmp(argv[i], "--use-syslog") == 0)
342 {
343 logger_manager->set_output(logger_manager, ALL_LOGGERS, NULL);
344 }
345 }
346
347 private_charon = daemon_create();
348 charon = (daemon_t*)private_charon;
349
350 private_charon->logger = logger_manager->get_logger(logger_manager, DAEMON);
351
352 /* check/setup PID file */
353 if (stat(PID_FILE, &stb) == 0)
354 {
355 private_charon->logger->log(private_charon->logger, ERROR,
356 "charon already running (\""PID_FILE"\" exists)");
357 private_charon->destroy(private_charon);
358 exit(-1);
359 }
360 pid_file = fopen(PID_FILE, "w");
361 if (pid_file)
362 {
363 fprintf(pid_file, "%d\n", getpid());
364 fclose(pid_file);
365 }
366
367 /* initialize and run daemon */
368 private_charon->initialize(private_charon);
369 private_charon->run(private_charon);
370
371 /* normal termination, cleanup and exit */
372 private_charon->destroy(private_charon);
373 unlink(PID_FILE);
374
375 return 0;
376 }
377
378