- fixed thread pool 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 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.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
186 }
187
188 /**
189 * Destory all initiated objects
190 */
191 static void destroy(private_daemon_t *this)
192 {
193 if (this->public.receiver != NULL)
194 {
195 this->public.receiver->destroy(this->public.receiver);
196 }
197 if (this->public.scheduler != NULL)
198 {
199 this->public.scheduler->destroy(this->public.scheduler);
200 }
201 if (this->public.sender != NULL)
202 {
203 this->public.sender->destroy(this->public.sender);
204 }
205 if (this->public.thread_pool != NULL)
206 {
207 this->public.thread_pool->destroy(this->public.thread_pool);
208 }
209 if (this->public.job_queue != NULL)
210 {
211 this->public.job_queue->destroy(this->public.job_queue);
212 }
213 if (this->public.event_queue != NULL)
214 {
215 this->public.event_queue->destroy(this->public.event_queue);
216 }
217 if (this->public.send_queue != NULL)
218 {
219 this->public.send_queue->destroy(this->public.send_queue);
220 }
221 if (this->public.socket != NULL)
222 {
223 this->public.socket->destroy(this->public.socket);
224 }
225 if (this->public.ike_sa_manager != NULL)
226 {
227 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
228 }
229 if (this->public.configuration_manager != NULL)
230 {
231 this->public.configuration_manager->destroy(this->public.configuration_manager);
232 }
233
234 this->public.logger_manager->destroy(this->public.logger_manager);
235 allocator_free(this);
236 }
237
238
239
240 /**
241 * @brief Create the daemon.
242 *
243 * @return created daemon_t
244 */
245 private_daemon_t *daemon_create()
246 {
247 private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
248
249 /* assign methods */
250 this->run = run;
251 this->destroy = destroy;
252 this->build_test_jobs = build_test_jobs;
253 this->initialize = initialize;
254 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
255
256 /* first build a logger */
257 this->public.logger_manager = logger_manager_create(DEFAULT_LOGLEVEL);
258 this->logger = (this->public.logger_manager)->create_logger(this->public.logger_manager, DAEMON, NULL);
259
260 /* NULL members for clean destruction */
261 this->public.socket = NULL;
262 this->public.ike_sa_manager = NULL;
263 this->public.job_queue = NULL;
264 this->public.event_queue = NULL;
265 this->public.send_queue = NULL;
266 this->public.configuration_manager = NULL;
267 this->public.sender= NULL;
268 this->public.receiver = NULL;
269 this->public.scheduler = NULL;
270 this->public.thread_pool = NULL;
271
272 this->main_thread_id = pthread_self();
273
274 /* setup signal handling */
275 sigemptyset(&(this->signal_set));
276 sigaddset(&(this->signal_set), SIGINT);
277 sigaddset(&(this->signal_set), SIGHUP);
278 sigaddset(&(this->signal_set), SIGTERM);
279 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
280
281 return this;
282 }
283
284 /**
285 * Main function, manages the daemon.
286 */
287 int main(int argc, char *argv[])
288 {
289 private_daemon_t *private_charon;
290
291 /* allocation needs initialization, before any allocs are done */
292 allocator_init();
293
294 private_charon = daemon_create();
295 charon = (daemon_t*)private_charon;
296
297 private_charon->initialize(private_charon);
298
299 private_charon->build_test_jobs(private_charon);
300
301 private_charon->run(private_charon);
302
303 private_charon->destroy(private_charon);
304
305 #ifdef LEAK_DETECTIVE
306 report_memory_leaks(void);
307 #endif
308
309 exit(0);
310 }
311