4 * @brief Main of IKEv2-Daemon
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
32 #include "ike_sa_manager.h"
35 #include "scheduler.h"
36 #include "thread_pool.h"
37 #include "utils/allocator.h"
38 #include "utils/logger_manager.h"
39 #include "queues/event_queue.h"
40 #include "queues/job_queue.h"
41 #include "queues/send_queue.h"
44 /* function declaration (defined and described after main function) */
46 static status_t
initialize_globals();
47 static void destroy_globals();
48 static status_t
start_threads();
49 static void end_threads();
50 static void main_loop();
51 static void register_signals();
52 static void destroy_and_exit(int);
54 /** Global job-queue */
55 job_queue_t
*global_job_queue
= NULL
;
56 /** Global event-queue */
57 event_queue_t
*global_event_queue
= NULL
;
58 /** Global send-queue */
59 send_queue_t
*global_send_queue
= NULL
;
61 socket_t
*global_socket
= NULL
;
62 /** Global logger manager */
63 logger_manager_t
*global_logger_manager
= NULL
;
64 /** Global ike_sa-manager */
65 ike_sa_manager_t
*global_ike_sa_manager
= NULL
;
66 /** Global configuration-manager */
67 configuration_manager_t
*global_configuration_manager
= NULL
;
70 * logger_t object assigned for daemon things
72 static logger_t
*logger
= NULL
;
77 static sender_t
*sender_thread
= NULL
;
81 static receiver_t
*receiver_thread
= NULL
;
85 static scheduler_t
*scheduler_thread
= NULL
;
87 * Thread pool holding the worker threads
89 static thread_pool_t
*thread_pool
= NULL
;
92 * Signal set used for signal handling
99 /* set signal handler */
102 /* logger_manager is created first */
103 global_logger_manager
= logger_manager_create(FULL
);
104 if (global_logger_manager
== NULL
)
106 printf("could not create logger manager");
110 /* a own logger for the daemon is created */
111 logger
= global_logger_manager
->create_logger(global_logger_manager
,DAEMON
,NULL
);
114 printf("could not create logger object");
119 /* initialize all global objects */
120 if (initialize_globals() != SUCCESS
)
125 /* a own logger for the daemon is created */
126 logger
= global_logger_manager
->create_logger(global_logger_manager
,DAEMON
,NULL
);
129 printf("could not create logger object");
134 logger
->log(logger
,CONTROL
,"start daemon %s", DAEMON_NAME
);
135 /* now its time to create all the different threads :-) */
136 if (start_threads() != SUCCESS
)
139 logger
->log(logger
,CONTROL
,"Fatal error: Needed Threads could not be started");
140 destroy_and_exit(-1);
144 for(i
= 0; i
<10; i
++)
146 initiate_ike_sa_job_t
*initiate_job
;
148 initiate_job
= initiate_ike_sa_job_create("pinflb31");
149 global_event_queue
->add_relative(global_event_queue
, (job_t
*)initiate_job
, i
* 1000);
153 logger
->log(logger
,CONTROL
|MORE
,"going to wait for exit signal");
154 /* go and handle signals*/
165 * Waits for registered signals and acts dependently
167 static void main_loop()
174 error
= sigwait(&signal_set
, &signal_number
);
179 logger
->log(logger
,CONTROL
,"Error %d when waiting for signal",error
);
182 switch (signal_number
)
186 logger
->log(logger
,CONTROL
,"Signal of type SIGHUP received. Do nothing");
191 logger
->log(logger
,CONTROL
,"Signal of type SIGINT received. Exit main loop.");
196 logger
->log(logger
,CONTROL
,"Signal of type SIGTERM received. Exit main loop.");
201 logger
->log(logger
,CONTROL
,"Unknown signal %d received. Do nothing",signal_number
);
209 * Registers signals SIGINT, SIGHUP and SIGTERM.
210 * Signals are handled in main_loop()
212 static void register_signals()
214 sigemptyset(&signal_set
);
215 sigaddset(&signal_set
, SIGINT
);
216 sigaddset(&signal_set
, SIGHUP
);
217 sigaddset(&signal_set
, SIGTERM
);
218 pthread_sigmask(SIG_BLOCK
, &signal_set
, 0);
223 * Initializes global objects
229 static status_t
initialize_globals()
231 /* initialize global object */
232 global_socket
= socket_create(IKEV2_UDP_PORT
);
233 global_ike_sa_manager
= ike_sa_manager_create();
234 global_job_queue
= job_queue_create();
235 global_event_queue
= event_queue_create();
236 global_send_queue
= send_queue_create();
237 global_configuration_manager
= configuration_manager_create();
239 if ( (global_socket
== NULL
) ||
240 (global_job_queue
== NULL
) ||
241 (global_event_queue
== NULL
) ||
242 (global_send_queue
== NULL
) ||
243 (global_configuration_manager
== NULL
) ||
244 (global_ike_sa_manager
== NULL
))
253 * Destroy global objects
255 static void destroy_globals()
257 if (global_ike_sa_manager
!= NULL
)
259 global_job_queue
->destroy(global_job_queue
);
261 if (global_event_queue
!= NULL
)
263 global_event_queue
->destroy(global_event_queue
);
265 if (global_send_queue
!= NULL
)
267 global_send_queue
->destroy(global_send_queue
);
269 if (global_socket
!= NULL
)
271 global_socket
->destroy(global_socket
);
273 if (global_ike_sa_manager
!= NULL
)
275 global_ike_sa_manager
->destroy(global_ike_sa_manager
);
277 if (global_ike_sa_manager
!= NULL
)
279 global_configuration_manager
->destroy(global_configuration_manager
);
284 * Creates all needed Threads
290 static status_t
start_threads()
292 sender_thread
= sender_create();
293 if (sender_thread
== NULL
)
297 scheduler_thread
= scheduler_create();
298 if (scheduler_thread
== NULL
)
302 thread_pool
= thread_pool_create(NUMBER_OF_WORKING_THREADS
);
303 if (thread_pool
== NULL
)
307 receiver_thread
= receiver_create();
308 if (receiver_thread
== NULL
)
321 static void end_threads()
323 if (receiver_thread
!= NULL
)
325 receiver_thread
->destroy(receiver_thread
);
327 if (scheduler_thread
!= NULL
)
329 scheduler_thread
->destroy(scheduler_thread
);
331 if (sender_thread
!= NULL
)
333 sender_thread
->destroy(sender_thread
);
335 if (thread_pool
!= NULL
)
337 thread_pool
->destroy(thread_pool
);
343 * Destroys initialized objects, kills all threads and exits
345 * @param exit_code Code to exit with
347 static void destroy_and_exit(int exit_code
)
349 logger
->log(logger
,CONTROL
,"going to exit daemon");
353 /* all globals can be destroyed now */
356 /* logger is destroyed */
357 logger
->log(logger
,CONTROL
|MORE
,"destroy logger");
358 global_logger_manager
->destroy_logger(global_logger_manager
,logger
);
359 logger
->log(logger
,CONTROL
|MORE
,"destroy logger_manager");
360 logger
->log(logger
,CONTROL
|MORE
,"------------------------------------");
361 if (global_logger_manager
!= NULL
)
363 global_logger_manager
->destroy(global_logger_manager
);
366 #ifdef LEAK_DETECTIVE
367 /* Leaks are reported in log file */
368 report_memory_leaks(void);