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