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