(no commit message)
[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_certificates(cred_store, 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 if (this->public.ike_sa_manager != NULL)
200 {
201 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
202 }
203 if (this->public.kernel_interface != NULL)
204 {
205 this->public.kernel_interface->destroy(this->public.kernel_interface);
206 }
207 if (this->public.receiver != NULL)
208 {
209 this->public.receiver->destroy(this->public.receiver);
210 }
211 if (this->public.scheduler != NULL)
212 {
213 this->public.scheduler->destroy(this->public.scheduler);
214 }
215 if (this->public.sender != NULL)
216 {
217 this->public.sender->destroy(this->public.sender);
218 }
219 if (this->public.thread_pool != NULL)
220 {
221 this->public.thread_pool->destroy(this->public.thread_pool);
222 }
223 if (this->public.job_queue != NULL)
224 {
225 this->public.job_queue->destroy(this->public.job_queue);
226 }
227 if (this->public.event_queue != NULL)
228 {
229 this->public.event_queue->destroy(this->public.event_queue);
230 }
231 if (this->public.send_queue != NULL)
232 {
233 this->public.send_queue->destroy(this->public.send_queue);
234 }
235 if (this->public.socket != NULL)
236 {
237 this->public.socket->destroy(this->public.socket);
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 if (this->public.stroke != NULL)
256 {
257 this->public.stroke->destroy(this->public.stroke);
258 }
259 free(this);
260 }
261
262 void signal_handler(int signal)
263 {
264 void *array[20];
265 size_t size;
266 char **strings;
267 size_t i;
268 logger_t *logger;
269
270 size = backtrace(array, 20);
271 strings = backtrace_symbols(array, size);
272 logger = logger_manager->get_logger(logger_manager, DAEMON);
273
274 logger->log(logger, ERROR, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size);
275
276 for (i = 0; i < size; i++)
277 {
278 logger->log(logger, ERROR, " %s", strings[i]);
279 }
280 free (strings);
281 logger->log(logger, ERROR, "Killing ourself hard after SIGSEGV");
282 kill(getpid(), SIGKILL);
283 }
284
285 /**
286 * @brief Create the daemon.
287 *
288 * @return created daemon_t
289 */
290 private_daemon_t *daemon_create(void)
291 {
292 private_daemon_t *this = malloc_thing(private_daemon_t);
293 struct sigaction action;
294
295 /* assign methods */
296 this->run = run;
297 this->destroy = destroy;
298 this->initialize = initialize;
299 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
300
301 /* NULL members for clean destruction */
302 this->public.socket = NULL;
303 this->public.ike_sa_manager = NULL;
304 this->public.job_queue = NULL;
305 this->public.event_queue = NULL;
306 this->public.send_queue = NULL;
307 this->public.configuration = NULL;
308 this->public.credentials = NULL;
309 this->public.connections = NULL;
310 this->public.policies = NULL;
311 this->public.sender= NULL;
312 this->public.receiver = NULL;
313 this->public.scheduler = NULL;
314 this->public.kernel_interface = NULL;
315 this->public.thread_pool = NULL;
316 this->public.stroke = NULL;
317
318 this->main_thread_id = pthread_self();
319
320 /* setup signal handling for all threads */
321 sigemptyset(&(this->signal_set));
322 sigaddset(&(this->signal_set), SIGSEGV);
323 sigaddset(&(this->signal_set), SIGINT);
324 sigaddset(&(this->signal_set), SIGHUP);
325 sigaddset(&(this->signal_set), SIGTERM);
326 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
327
328 /* setup SIGSEGV handler for all threads */
329 action.sa_handler = signal_handler;
330 action.sa_mask = this->signal_set;
331 action.sa_flags = 0;
332 if (sigaction(SIGSEGV, &action, NULL) == -1)
333 {
334 this->logger->log(this->logger, ERROR, "signal handler setup for SIGSEGV failed");
335 }
336 return this;
337 }
338
339 /**
340 * Main function, manages the daemon.
341 */
342 int main(int argc, char *argv[])
343 {
344 private_daemon_t *private_charon;
345 FILE *pid_file;
346 struct stat stb;
347 int i;
348
349 /* trivial argument parsing */
350 for (i = 1; i < argc; i++)
351 {
352 if (strcmp(argv[i], "--use-syslog") == 0)
353 {
354 logger_manager->set_output(logger_manager, ALL_LOGGERS, NULL);
355 }
356 }
357 private_charon = daemon_create();
358 charon = (daemon_t*)private_charon;
359
360 private_charon->logger = logger_manager->get_logger(logger_manager, DAEMON);
361
362 /* initialize daemon */
363 private_charon->initialize(private_charon);
364
365 /* check/setup PID file */
366 if (stat(PID_FILE, &stb) == 0)
367 {
368 private_charon->logger->log(private_charon->logger, ERROR,
369 "charon already running (\""PID_FILE"\" exists)");
370 private_charon->destroy(private_charon);
371 exit(-1);
372 }
373 pid_file = fopen(PID_FILE, "w");
374 if (pid_file)
375 {
376 fprintf(pid_file, "%d\n", getpid());
377 fclose(pid_file);
378 }
379
380 /* run daemon */
381 private_charon->run(private_charon);
382
383 /* normal termination, cleanup and exit */
384 private_charon->destroy(private_charon);
385 unlink(PID_FILE);
386
387 return 0;
388 }
389
390