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