- code cleaned up
[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 int i;
162 for(i = 0; i<1; i++)
163 {
164 initiate_ike_sa_job_t *initiate_job;
165 initiate_job = initiate_ike_sa_job_create("localhost");
166 this->public.event_queue->add_relative(this->public.event_queue, (job_t*)initiate_job, i * 5000);
167 }
168 }
169
170 /**
171 * Implementation of private_daemon_t.initialize.
172 */
173 static void initialize(private_daemon_t *this)
174 {
175 this->public.socket = socket_create(IKEV2_UDP_PORT);
176 this->public.ike_sa_manager = ike_sa_manager_create();
177 this->public.job_queue = job_queue_create();
178 this->public.event_queue = event_queue_create();
179 this->public.send_queue = send_queue_create();
180 this->public.configuration_manager = configuration_manager_create(RETRANSMIT_TIMEOUT,MAX_RETRANSMIT_COUNT, HALF_OPEN_IKE_SA_TIMEOUT);
181
182 this->public.sender = sender_create();
183 this->public.receiver = receiver_create();
184 this->public.scheduler = scheduler_create();
185 this->public.prime_pool = prime_pool_create(PRIME_PRE_COMPUTATION_LIMIT);
186 this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
187 }
188
189 /**
190 * Destory all initiated objects
191 */
192 static void destroy(private_daemon_t *this)
193 {
194 if (this->public.receiver != NULL)
195 {
196 this->public.receiver->destroy(this->public.receiver);
197 }
198 if (this->public.scheduler != NULL)
199 {
200 this->public.scheduler->destroy(this->public.scheduler);
201 }
202 if (this->public.sender != NULL)
203 {
204 this->public.sender->destroy(this->public.sender);
205 }
206 if (this->public.thread_pool != NULL)
207 {
208 this->public.thread_pool->destroy(this->public.thread_pool);
209 }
210 if (this->public.prime_pool != NULL)
211 {
212 this->public.prime_pool->destroy(this->public.prime_pool);
213 }
214 if (this->public.job_queue != NULL)
215 {
216 this->public.job_queue->destroy(this->public.job_queue);
217 }
218 if (this->public.event_queue != NULL)
219 {
220 this->public.event_queue->destroy(this->public.event_queue);
221 }
222 if (this->public.send_queue != NULL)
223 {
224 this->public.send_queue->destroy(this->public.send_queue);
225 }
226 if (this->public.socket != NULL)
227 {
228 this->public.socket->destroy(this->public.socket);
229 }
230 if (this->public.ike_sa_manager != NULL)
231 {
232 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
233 }
234 if (this->public.configuration_manager != NULL)
235 {
236 this->public.configuration_manager->destroy(this->public.configuration_manager);
237 }
238
239 this->public.logger_manager->destroy(this->public.logger_manager);
240 allocator_free(this);
241 }
242
243
244
245 /**
246 * @brief Create the daemon.
247 *
248 * @return created daemon_t
249 */
250 private_daemon_t *daemon_create()
251 {
252 private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
253
254 /* assign methods */
255 this->run = run;
256 this->destroy = destroy;
257 this->build_test_jobs = build_test_jobs;
258 this->initialize = initialize;
259 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
260
261 /* first build a logger */
262 this->public.logger_manager = logger_manager_create(DEFAULT_LOGLEVEL);
263 this->logger = (this->public.logger_manager)->create_logger(this->public.logger_manager, DAEMON, NULL);
264
265 /* NULL members for clean destruction */
266 this->public.socket = NULL;
267 this->public.ike_sa_manager = NULL;
268 this->public.job_queue = NULL;
269 this->public.event_queue = NULL;
270 this->public.send_queue = NULL;
271 this->public.configuration_manager = NULL;
272 this->public.sender= NULL;
273 this->public.receiver = NULL;
274 this->public.scheduler = NULL;
275 this->public.thread_pool = NULL;
276
277 this->main_thread_id = pthread_self();
278
279 /* setup signal handling */
280 sigemptyset(&(this->signal_set));
281 sigaddset(&(this->signal_set), SIGINT);
282 sigaddset(&(this->signal_set), SIGHUP);
283 sigaddset(&(this->signal_set), SIGTERM);
284 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
285
286 return this;
287 }
288
289 /**
290 * Main function, manages the daemon.
291 */
292 int main(int argc, char *argv[])
293 {
294 private_daemon_t *private_charon;
295
296 /* allocation needs initialization, before any allocs are done */
297 allocator_init();
298
299 private_charon = daemon_create();
300 charon = (daemon_t*)private_charon;
301
302 private_charon->initialize(private_charon);
303
304 private_charon->build_test_jobs(private_charon);
305
306 private_charon->run(private_charon);
307
308 private_charon->destroy(private_charon);
309
310 #ifdef LEAK_DETECTIVE
311 report_memory_leaks(void);
312 #endif
313
314 exit(0);
315 }
316