fixed compilation error
[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/connections/local_connection_store.h>
37 #include <config/credentials/local_credential_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 local_credential_store_t* cred_store;
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_t*)(cred_store = local_credential_store_create());
181
182 /* load keys & certs */
183 cred_store->load_ca_certificates(cred_store, CA_CERTIFICATE_DIR);
184 cred_store->load_private_keys(cred_store, SECRETS_FILE, PRIVATE_KEY_DIR);
185
186
187 /* start building threads, we are multi-threaded NOW */
188 this->public.stroke = stroke_create();
189 this->public.sender = sender_create();
190 this->public.receiver = receiver_create();
191 this->public.scheduler = scheduler_create();
192 this->public.kernel_interface = kernel_interface_create();
193 this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
194 }
195
196 /**
197 * Destory all initiated objects
198 */
199 static void destroy(private_daemon_t *this)
200 {
201 /* destruction is a non trivial task, we need to follow
202 * a strict order to prevent threading issues!
203 * Kill active threads first, except the sender, as
204 * the killed IKE_SA want to send delete messages.
205 */
206 if (this->public.receiver != NULL)
207 { /* we don't want to receive anything... */
208 this->public.receiver->destroy(this->public.receiver);
209 }
210 if (this->public.stroke != NULL)
211 { /* ignore all incoming user requests */
212 this->public.stroke->destroy(this->public.stroke);
213 }
214 if (this->public.scheduler != NULL)
215 { /* stop scheduing jobs */
216 this->public.scheduler->destroy(this->public.scheduler);
217 }
218 if (this->public.thread_pool != NULL)
219 { /* stop processing jobs */
220 this->public.thread_pool->destroy(this->public.thread_pool);
221 }
222 if (this->public.ike_sa_manager != NULL)
223 { /* shut down manager with all IKE SAs */
224 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
225 }
226 if (this->public.kernel_interface != NULL)
227 { /* all child SAs should be down now, so kill kernel interface */
228 this->public.kernel_interface->destroy(this->public.kernel_interface);
229 }
230 /* destroy other infrastructure */
231 if (this->public.job_queue != NULL)
232 {
233 this->public.job_queue->destroy(this->public.job_queue);
234 }
235 if (this->public.event_queue != NULL)
236 {
237 this->public.event_queue->destroy(this->public.event_queue);
238 }
239 if (this->public.configuration != NULL)
240 {
241 this->public.configuration->destroy(this->public.configuration);
242 }
243 if (this->public.credentials != NULL)
244 {
245 this->public.credentials->destroy(this->public.credentials);
246 }
247 if (this->public.connections != NULL)
248 {
249 this->public.connections->destroy(this->public.connections);
250 }
251 if (this->public.policies != NULL)
252 {
253 this->public.policies->destroy(this->public.policies);
254 }
255 /* we hope the sender could send the outstanding deletes, but
256 * we shut down here at any cost */
257 if (this->public.sender != NULL)
258 {
259 this->public.sender->destroy(this->public.sender);
260 }
261 if (this->public.send_queue != NULL)
262 {
263 this->public.send_queue->destroy(this->public.send_queue);
264 }
265 if (this->public.socket != NULL)
266 {
267 this->public.socket->destroy(this->public.socket);
268 }
269 free(this);
270 }
271
272 void signal_handler(int signal)
273 {
274 void *array[20];
275 size_t size;
276 char **strings;
277 size_t i;
278 logger_t *logger;
279
280 size = backtrace(array, 20);
281 strings = backtrace_symbols(array, size);
282 logger = logger_manager->get_logger(logger_manager, DAEMON);
283
284 logger->log(logger, ERROR, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size);
285
286 for (i = 0; i < size; i++)
287 {
288 logger->log(logger, ERROR, " %s", strings[i]);
289 }
290 free (strings);
291 logger->log(logger, ERROR, "Killing ourself hard after SIGSEGV");
292 kill(getpid(), SIGKILL);
293 }
294
295 /**
296 * @brief Create the daemon.
297 *
298 * @return created daemon_t
299 */
300 private_daemon_t *daemon_create(void)
301 {
302 private_daemon_t *this = malloc_thing(private_daemon_t);
303 struct sigaction action;
304
305 /* assign methods */
306 this->run = run;
307 this->destroy = destroy;
308 this->initialize = initialize;
309 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
310
311 /* NULL members for clean destruction */
312 this->public.socket = NULL;
313 this->public.ike_sa_manager = NULL;
314 this->public.job_queue = NULL;
315 this->public.event_queue = NULL;
316 this->public.send_queue = NULL;
317 this->public.configuration = NULL;
318 this->public.credentials = NULL;
319 this->public.connections = NULL;
320 this->public.policies = NULL;
321 this->public.sender= NULL;
322 this->public.receiver = NULL;
323 this->public.scheduler = NULL;
324 this->public.kernel_interface = NULL;
325 this->public.thread_pool = NULL;
326 this->public.stroke = NULL;
327
328 this->main_thread_id = pthread_self();
329
330 /* setup signal handling for all threads */
331 sigemptyset(&(this->signal_set));
332 sigaddset(&(this->signal_set), SIGSEGV);
333 sigaddset(&(this->signal_set), SIGINT);
334 sigaddset(&(this->signal_set), SIGHUP);
335 sigaddset(&(this->signal_set), SIGTERM);
336 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
337
338 /* setup SIGSEGV handler for all threads */
339 action.sa_handler = signal_handler;
340 action.sa_mask = this->signal_set;
341 action.sa_flags = 0;
342 if (sigaction(SIGSEGV, &action, NULL) == -1)
343 {
344 this->logger->log(this->logger, ERROR, "signal handler setup for SIGSEGV failed");
345 }
346 return this;
347 }
348
349 static void usage(const char *msg)
350 {
351 if (msg != NULL && *msg != '\0')
352 fprintf(stderr, "%s\n", msg);
353 fprintf(stderr, "Usage: charon"
354 " [--help]"
355 " [--version]"
356 " [--use-syslog]"
357 " [--strictcrlpolicy]"
358 "\n"
359 );
360 exit(msg == NULL? 0 : 1);
361 }
362
363
364 /**
365 * Main function, manages the daemon.
366 */
367 int main(int argc, char *argv[])
368 {
369 bool strict_crl_policy = FALSE;
370
371 private_daemon_t *private_charon;
372 FILE *pid_file;
373 struct stat stb;
374
375 /* handle arguments */
376 for (;;)
377 {
378 static const struct option long_opts[] = {
379 { "help", no_argument, NULL, 'h' },
380 { "version", no_argument, NULL, 'v' },
381 { "use-syslog", no_argument, NULL, 'l' },
382 { "strictcrlpolicy", no_argument, NULL, 'r' },
383 { 0,0,0,0 }
384 };
385
386 int c = getopt_long(argc, argv, "", long_opts, NULL);
387
388 /* Note: "breaking" from case terminates loop */
389 switch (c)
390 {
391 case EOF: /* end of flags */
392 break;
393 case 'h':
394 usage(NULL);
395 break; /* not actually reached */
396 case 'v':
397 printf("Linux strongSwan %s\n", VERSION);
398 exit(0);
399 case 'l':
400 logger_manager->set_output(logger_manager, ALL_LOGGERS, NULL);
401 continue;
402 case 'r':
403 strict_crl_policy = TRUE;
404 continue;
405 default:
406 usage("");
407 break; /* not actually reached */
408 }
409 break;
410 }
411
412 private_charon = daemon_create();
413 charon = (daemon_t*)private_charon;
414
415 private_charon->logger = logger_manager->get_logger(logger_manager, DAEMON);
416
417 private_charon->logger->log(private_charon->logger, CONTROL,
418 "Starting Charon (strongSwan Version %s)", VERSION);
419
420 /* initialize daemon */
421 private_charon->initialize(private_charon, strict_crl_policy);
422
423 /* check/setup PID file */
424 if (stat(PID_FILE, &stb) == 0)
425 {
426 private_charon->logger->log(private_charon->logger, ERROR,
427 "charon already running (\""PID_FILE"\" exists)");
428 private_charon->destroy(private_charon);
429 exit(-1);
430 }
431 pid_file = fopen(PID_FILE, "w");
432 if (pid_file)
433 {
434 fprintf(pid_file, "%d\n", getpid());
435 fclose(pid_file);
436 }
437
438 /* run daemon */
439 private_charon->run(private_charon);
440
441 /* normal termination, cleanup and exit */
442 private_charon->destroy(private_charon);
443 unlink(PID_FILE);
444
445 return 0;
446 }
447
448