updated copyright information
[strongswan.git] / src / 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) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #include <stdio.h>
26 #include <signal.h>
27 #include <pthread.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <execinfo.h>
32 #include <string.h>
33 #include <getopt.h>
34
35 #include "daemon.h"
36
37 #include <types.h>
38 #include <config/credentials/local_credential_store.h>
39 #include <config/connections/local_connection_store.h>
40 #include <config/policies/local_policy_store.h>
41
42
43 typedef struct private_daemon_t private_daemon_t;
44
45 /**
46 * Private additions to daemon_t, contains threads and internal functions.
47 */
48 struct private_daemon_t {
49 /**
50 * Public members of daemon_t.
51 */
52 daemon_t public;
53
54 /**
55 * A logger_t object assigned for daemon things.
56 */
57 logger_t *logger;
58
59 /**
60 * Signal set used for signal handling.
61 */
62 sigset_t signal_set;
63
64 /**
65 * The thread_id of main-thread.
66 */
67 pthread_t main_thread_id;
68
69 /**
70 * Main loop function.
71 *
72 * @param this calling object
73 */
74 void (*run) (private_daemon_t *this);
75
76 /**
77 * Initialize the daemon.
78 *
79 * @param this calling object
80 * @param strict enforce a strict crl policy
81 */
82 void (*initialize) (private_daemon_t *this, bool strict);
83
84 /**
85 * Destroy the daemon.
86 *
87 * @param this calling object
88 */
89 void (*destroy) (private_daemon_t *this);
90 };
91
92 /**
93 * One and only instance of the daemon.
94 */
95 daemon_t *charon;
96
97 /**
98 * Implementation of private_daemon_t.run.
99 */
100 static void run(private_daemon_t *this)
101 {
102 /* reselect signals for this thread */
103 sigemptyset(&(this->signal_set));
104 sigaddset(&(this->signal_set), SIGINT);
105 sigaddset(&(this->signal_set), SIGHUP);
106 sigaddset(&(this->signal_set), SIGTERM);
107 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
108
109 while(TRUE)
110 {
111 int signal_number;
112 int error;
113
114 error = sigwait(&(this->signal_set), &signal_number);
115 if(error)
116 {
117 this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
118 return;
119 }
120 switch (signal_number)
121 {
122 case SIGHUP:
123 {
124 this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
125 break;
126 }
127 case SIGINT:
128 {
129 this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
130 return;
131 }
132 case SIGTERM:
133 this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
134 return;
135 default:
136 {
137 this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
138 break;
139 }
140 }
141 }
142 }
143
144 /**
145 * Implementation of daemon_t.kill.
146 */
147 static void kill_daemon(private_daemon_t *this, char *reason)
148 {
149 /* we send SIGTERM, so the daemon can cleanly shut down */
150 this->logger->log(this->logger, CONTROL, "Killing daemon: %s", reason);
151 if (this->main_thread_id == pthread_self())
152 {
153 /* initialization failed, terminate daemon */
154 this->destroy(this);
155 unlink(PID_FILE);
156 exit(-1);
157 }
158 else
159 {
160 this->logger->log(this->logger, CONTROL, "sending SIGTERM to ourself", reason);
161 raise(SIGTERM);
162 /* thread must die, since he produced a ciritcal failure and can't continue */
163 pthread_exit(NULL);
164 }
165 }
166
167 /**
168 * Implementation of private_daemon_t.initialize.
169 */
170 static void initialize(private_daemon_t *this, bool strict)
171 {
172 credential_store_t* credentials;
173
174 this->public.configuration = configuration_create();
175 this->public.socket = socket_create(IKEV2_UDP_PORT, IKEV2_NATT_PORT);
176 this->public.interfaces = interfaces_create(IKEV2_UDP_PORT);
177 this->public.ike_sa_manager = ike_sa_manager_create();
178 this->public.job_queue = job_queue_create();
179 this->public.event_queue = event_queue_create();
180 this->public.send_queue = send_queue_create();
181 this->public.connections = (connection_store_t*)local_connection_store_create();
182 this->public.policies = (policy_store_t*)local_policy_store_create();
183 this->public.credentials = (credential_store_t*)local_credential_store_create(strict);
184
185 /* load keys, ca certificates and crls */
186 credentials = this->public.credentials;
187 credentials->load_ca_certificates(credentials);
188 credentials->load_crls(credentials);
189 credentials->load_private_keys(credentials);
190
191 /* start building threads, we are multi-threaded NOW */
192 this->public.stroke = stroke_create();
193 this->public.sender = sender_create();
194 this->public.receiver = receiver_create();
195 this->public.scheduler = scheduler_create();
196 this->public.kernel_interface = kernel_interface_create();
197 this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
198 }
199
200 /**
201 * Destory all initiated objects
202 */
203 static void destroy(private_daemon_t *this)
204 {
205 /* destruction is a non trivial task, we need to follow
206 * a strict order to prevent threading issues!
207 * Kill active threads first, except the sender, as
208 * the killed IKE_SA want to send delete messages.
209 */
210 if (this->public.receiver != NULL)
211 { /* we don't want to receive anything... */
212 this->public.receiver->destroy(this->public.receiver);
213 }
214 if (this->public.stroke != NULL)
215 { /* ignore all incoming user requests */
216 this->public.stroke->destroy(this->public.stroke);
217 }
218 if (this->public.scheduler != NULL)
219 { /* stop scheduing jobs */
220 this->public.scheduler->destroy(this->public.scheduler);
221 }
222 if (this->public.thread_pool != NULL)
223 { /* stop processing jobs */
224 this->public.thread_pool->destroy(this->public.thread_pool);
225 }
226 if (this->public.ike_sa_manager != NULL)
227 { /* shut down manager with all IKE SAs */
228 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
229 }
230 if (this->public.kernel_interface != NULL)
231 { /* all child SAs should be down now, so kill kernel interface */
232 this->public.kernel_interface->destroy(this->public.kernel_interface);
233 }
234 /* destroy other infrastructure */
235 if (this->public.job_queue != NULL)
236 {
237 this->public.job_queue->destroy(this->public.job_queue);
238 }
239 if (this->public.event_queue != NULL)
240 {
241 this->public.event_queue->destroy(this->public.event_queue);
242 }
243 if (this->public.interfaces != NULL)
244 {
245 this->public.interfaces->destroy(this->public.interfaces);
246 }
247 if (this->public.configuration != NULL)
248 {
249 this->public.configuration->destroy(this->public.configuration);
250 }
251 if (this->public.credentials != NULL)
252 {
253 this->public.credentials->destroy(this->public.credentials);
254 }
255 if (this->public.connections != NULL)
256 {
257 this->public.connections->destroy(this->public.connections);
258 }
259 if (this->public.policies != NULL)
260 {
261 this->public.policies->destroy(this->public.policies);
262 }
263 /* we hope the sender could send the outstanding deletes, but
264 * we shut down here at any cost */
265 if (this->public.sender != NULL)
266 {
267 this->public.sender->destroy(this->public.sender);
268 }
269 if (this->public.send_queue != NULL)
270 {
271 this->public.send_queue->destroy(this->public.send_queue);
272 }
273 if (this->public.socket != NULL)
274 {
275 this->public.socket->destroy(this->public.socket);
276 }
277 free(this);
278 }
279
280 void signal_handler(int signal)
281 {
282 void *array[20];
283 size_t size;
284 char **strings;
285 size_t i;
286 logger_t *logger;
287
288 size = backtrace(array, 20);
289 strings = backtrace_symbols(array, size);
290 logger = logger_manager->get_logger(logger_manager, DAEMON);
291
292 logger->log(logger, ERROR, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size);
293
294 for (i = 0; i < size; i++)
295 {
296 logger->log(logger, ERROR, " %s", strings[i]);
297 }
298 free (strings);
299 logger->log(logger, ERROR, "Killing ourself hard after SIGSEGV");
300 raise(SIGKILL);
301 }
302
303 /**
304 * @brief Create the daemon.
305 *
306 * @return created daemon_t
307 */
308 private_daemon_t *daemon_create(void)
309 {
310 private_daemon_t *this = malloc_thing(private_daemon_t);
311 struct sigaction action;
312
313 /* assign methods */
314 this->run = run;
315 this->destroy = destroy;
316 this->initialize = initialize;
317 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
318
319 /* NULL members for clean destruction */
320 this->public.socket = NULL;
321 this->public.interfaces = NULL;
322 this->public.ike_sa_manager = NULL;
323 this->public.job_queue = NULL;
324 this->public.event_queue = NULL;
325 this->public.send_queue = NULL;
326 this->public.configuration = NULL;
327 this->public.credentials = NULL;
328 this->public.connections = NULL;
329 this->public.policies = NULL;
330 this->public.sender= NULL;
331 this->public.receiver = NULL;
332 this->public.scheduler = NULL;
333 this->public.kernel_interface = NULL;
334 this->public.thread_pool = NULL;
335 this->public.stroke = NULL;
336
337 this->main_thread_id = pthread_self();
338
339 /* setup signal handling for all threads */
340 sigemptyset(&(this->signal_set));
341 sigaddset(&(this->signal_set), SIGSEGV);
342 sigaddset(&(this->signal_set), SIGINT);
343 sigaddset(&(this->signal_set), SIGHUP);
344 sigaddset(&(this->signal_set), SIGTERM);
345 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
346
347 /* setup SIGSEGV handler for all threads */
348 action.sa_handler = signal_handler;
349 action.sa_mask = this->signal_set;
350 action.sa_flags = 0;
351 if (sigaction(SIGSEGV, &action, NULL) == -1)
352 {
353 this->logger->log(this->logger, ERROR, "signal handler setup for SIGSEGV failed");
354 }
355 return this;
356 }
357
358 static void usage(const char *msg)
359 {
360 if (msg != NULL && *msg != '\0')
361 fprintf(stderr, "%s\n", msg);
362 fprintf(stderr, "Usage: charon"
363 " [--help]"
364 " [--version]"
365 " [--use-syslog]"
366 " [--strictcrlpolicy]"
367 "\n"
368 );
369 exit(msg == NULL? 0 : 1);
370 }
371
372
373 /**
374 * Main function, manages the daemon.
375 */
376 int main(int argc, char *argv[])
377 {
378 bool strict_crl_policy = FALSE;
379
380 private_daemon_t *private_charon;
381 FILE *pid_file;
382 struct stat stb;
383
384 /* handle arguments */
385 for (;;)
386 {
387 static const struct option long_opts[] = {
388 { "help", no_argument, NULL, 'h' },
389 { "version", no_argument, NULL, 'v' },
390 { "use-syslog", no_argument, NULL, 'l' },
391 { "strictcrlpolicy", no_argument, NULL, 'r' },
392 { 0,0,0,0 }
393 };
394
395 int c = getopt_long(argc, argv, "", long_opts, NULL);
396
397 /* Note: "breaking" from case terminates loop */
398 switch (c)
399 {
400 case EOF: /* end of flags */
401 break;
402 case 'h':
403 usage(NULL);
404 break; /* not actually reached */
405 case 'v':
406 printf("Linux strongSwan %s\n", VERSION);
407 exit(0);
408 case 'l':
409 logger_manager->set_output(logger_manager, ALL_LOGGERS, NULL);
410 continue;
411 case 'r':
412 strict_crl_policy = TRUE;
413 continue;
414 default:
415 usage("");
416 break; /* not actually reached */
417 }
418 break;
419 }
420
421 private_charon = daemon_create();
422 charon = (daemon_t*)private_charon;
423
424 private_charon->logger = logger_manager->get_logger(logger_manager, DAEMON);
425
426 private_charon->logger->log(private_charon->logger, CONTROL,
427 "Starting Charon (strongSwan Version %s)", VERSION);
428
429 /* initialize daemon */
430 private_charon->initialize(private_charon, strict_crl_policy);
431
432 /* check/setup PID file */
433 if (stat(PID_FILE, &stb) == 0)
434 {
435 private_charon->logger->log(private_charon->logger, ERROR,
436 "charon already running (\""PID_FILE"\" exists)");
437 private_charon->destroy(private_charon);
438 exit(-1);
439 }
440 pid_file = fopen(PID_FILE, "w");
441 if (pid_file)
442 {
443 fprintf(pid_file, "%d\n", getpid());
444 fclose(pid_file);
445 }
446
447 /* run daemon */
448 private_charon->run(private_charon);
449
450 /* normal termination, cleanup and exit */
451 private_charon->destroy(private_charon);
452 unlink(PID_FILE);
453
454 return 0;
455 }
456
457