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