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