db75809d283e4f8f673419c3b9b61b11f532113d
[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
27 #include "daemon.h"
28
29 #include <types.h>
30 #include <utils/allocator.h>
31 #include <queues/jobs/initiate_ike_sa_job.h>
32 #include <config/static_configuration.h>
33 #include <config/starter_configuration.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 * A routine to add job for testing.
71 *
72 * @param this calling object
73 * @param configuration_name name of configuration to use for initialization
74 */
75 void (*build_test_job) (private_daemon_t *this,char *configuration_name);
76
77 /**
78 * Initialize the daemon.
79 *
80 * @param this calling object
81 */
82 void (*initialize) (private_daemon_t *this);
83
84 /**
85 * Destroy the daemon.
86 *
87 * @param this calling object
88 */
89 void (*destroy) (private_daemon_t *this);
90 };
91
92 /**
93 * One and only instance of the daemon.
94 */
95 daemon_t *charon;
96
97 /**
98 * Implementation of private_daemon_t.run.
99 */
100 static void run(private_daemon_t *this)
101 {
102 while(TRUE)
103 {
104 int signal_number;
105 int error;
106
107 error = sigwait(&(this->signal_set), &signal_number);
108 if(error)
109 {
110 this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
111 return;
112 }
113 switch (signal_number)
114 {
115 case SIGHUP:
116 {
117 this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
118 break;
119 }
120 case SIGINT:
121 {
122 this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
123 return;
124 }
125 case SIGTERM:
126 this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
127 return;
128 default:
129 {
130 this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
131 break;
132 }
133 }
134 }
135 }
136
137 /**
138 * Implementation of daemon_t.kill.
139 */
140 static void kill_daemon(private_daemon_t *this, char *reason)
141 {
142 /* we send SIGTERM, so the daemon can cleanly shut down */
143 this->logger->log(this->logger, CONTROL, "Killing daemon: %s", reason);
144 if (this->main_thread_id == pthread_self())
145 {
146 /* initialization failed, terminate daemon */
147 this->destroy(this);
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.build_test_job.
161 */
162 static void build_test_job(private_daemon_t *this, char *configuration_name)
163 {
164 initiate_ike_sa_job_t *initiate_job;
165
166 /* configuration_name = "localhost-rsa"; */
167 /* configuration_name = "localhost-shared"; */
168 /* configuration_name = "localhost-bad_dh_group"; */
169
170
171 initiate_job = initiate_ike_sa_job_create(configuration_name);
172
173 this->public.event_queue->add_relative(this->public.event_queue, (job_t*)initiate_job, 2000);
174
175 }
176
177 /**
178 * Implementation of private_daemon_t.initialize.
179 */
180 static void initialize(private_daemon_t *this)
181 {
182 this->public.socket = socket_create(IKEV2_UDP_PORT);
183 this->public.ike_sa_manager = ike_sa_manager_create();
184 this->public.job_queue = job_queue_create();
185 this->public.event_queue = event_queue_create();
186 this->public.send_queue = send_queue_create();
187 this->public.configuration = (configuration_t*)static_configuration_create();
188
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 if (this->public.ike_sa_manager != NULL)
202 {
203 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
204 }
205 if (this->public.kernel_interface != NULL)
206 {
207 this->public.kernel_interface->destroy(this->public.kernel_interface);
208 }
209 if (this->public.receiver != NULL)
210 {
211 this->public.receiver->destroy(this->public.receiver);
212 }
213 if (this->public.scheduler != NULL)
214 {
215 this->public.scheduler->destroy(this->public.scheduler);
216 }
217 if (this->public.sender != NULL)
218 {
219 this->public.sender->destroy(this->public.sender);
220 }
221 if (this->public.thread_pool != NULL)
222 {
223 this->public.thread_pool->destroy(this->public.thread_pool);
224 }
225 if (this->public.job_queue != NULL)
226 {
227 this->public.job_queue->destroy(this->public.job_queue);
228 }
229 if (this->public.event_queue != NULL)
230 {
231 this->public.event_queue->destroy(this->public.event_queue);
232 }
233 if (this->public.send_queue != NULL)
234 {
235 this->public.send_queue->destroy(this->public.send_queue);
236 }
237 if (this->public.socket != NULL)
238 {
239 this->public.socket->destroy(this->public.socket);
240 }
241 if (this->public.configuration != NULL)
242 {
243 this->public.configuration->destroy(this->public.configuration);
244 }
245
246 this->public.logger_manager->destroy(this->public.logger_manager);
247 allocator_free(this);
248 }
249
250
251
252 /**
253 * @brief Create the daemon.
254 *
255 * @return created daemon_t
256 */
257 private_daemon_t *daemon_create()
258 {
259 private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
260
261 /* assign methods */
262 this->run = run;
263 this->destroy = destroy;
264 this->build_test_job = build_test_job;
265 this->initialize = initialize;
266 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
267
268 /* first build a logger */
269 this->public.logger_manager = logger_manager_create(DEFAULT_LOGLEVEL);
270 this->logger = (this->public.logger_manager)->create_logger(this->public.logger_manager, DAEMON, NULL);
271
272 /* NULL members for clean destruction */
273 this->public.socket = NULL;
274 this->public.ike_sa_manager = NULL;
275 this->public.job_queue = NULL;
276 this->public.event_queue = NULL;
277 this->public.send_queue = NULL;
278 this->public.configuration = NULL;
279 this->public.sender= NULL;
280 this->public.receiver = NULL;
281 this->public.scheduler = NULL;
282 this->public.kernel_interface = NULL;
283 this->public.thread_pool = NULL;
284
285 this->main_thread_id = pthread_self();
286
287 /* setup signal handling */
288 sigemptyset(&(this->signal_set));
289 sigaddset(&(this->signal_set), SIGINT);
290 sigaddset(&(this->signal_set), SIGHUP);
291 sigaddset(&(this->signal_set), SIGTERM);
292 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
293
294 return this;
295 }
296
297 /**
298 * Main function, manages the daemon.
299 */
300 int main(int argc, char *argv[])
301 {
302 private_daemon_t *private_charon;
303
304 /* allocation needs initialization, before any allocs are done */
305 allocator_init();
306
307 private_charon = daemon_create();
308 charon = (daemon_t*)private_charon;
309
310 private_charon->initialize(private_charon);
311
312 if (argc == 2)
313 {
314 private_charon->build_test_job(private_charon,argv[1]);
315 }
316
317
318 private_charon->run(private_charon);
319
320 private_charon->destroy(private_charon);
321
322 #ifdef LEAK_DETECTIVE
323 report_memory_leaks(void);
324 #endif
325
326 exit(0);
327 }
328