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