1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/**
* @file daemon.c
*
* @brief Main of IKEv2-Daemon
*
*/
/*
* Copyright (C) 2005 Jan Hutter, Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include "daemon.h"
#include <types.h>
#include <sa/ike_sa_manager.h>
#include <threads/sender.h>
#include <threads/receiver.h>
#include <threads/scheduler.h>
#include <threads/thread_pool.h>
#include <network/socket.h>
#include <utils/allocator.h>
#include <utils/logger_manager.h>
#include <queues/event_queue.h>
#include <queues/job_queue.h>
#include <queues/send_queue.h>
#include <queues/jobs/initiate_ike_sa_job.h>
/* function declaration (defined and described after main function) */
static status_t initialize_globals();
static void destroy_globals();
static status_t start_threads();
static void end_threads();
static void main_loop();
static void register_signals();
static void destroy_and_exit(int);
/** Global job-queue */
job_queue_t *global_job_queue = NULL;
/** Global event-queue */
event_queue_t *global_event_queue = NULL;
/** Global send-queue */
send_queue_t *global_send_queue = NULL;
/** Global socket */
socket_t *global_socket = NULL;
/** Global logger manager */
logger_manager_t *global_logger_manager = NULL;
/** Global ike_sa-manager */
ike_sa_manager_t *global_ike_sa_manager = NULL;
/** Global configuration-manager */
configuration_manager_t *global_configuration_manager = NULL;
/**
* logger_t object assigned for daemon things
*/
static logger_t *logger = NULL;
/**
* Sender-Thread
*/
static sender_t *sender_thread = NULL;
/**
* Receiver-Thread
*/
static receiver_t *receiver_thread = NULL;
/**
* Scheduler-Thread
*/
static scheduler_t *scheduler_thread = NULL;
/**
* Thread pool holding the worker threads
*/
static thread_pool_t *thread_pool = NULL;
/**
* Signal set used for signal handling
*/
sigset_t signal_set;
int main()
{
/* set signal handler */
register_signals();
/* logger_manager is created first */
global_logger_manager = logger_manager_create(FULL);
if (global_logger_manager == NULL)
{
printf("could not create logger manager");
return -1;
}
/* a own logger for the daemon is created */
logger = global_logger_manager->create_logger(global_logger_manager,DAEMON,NULL);
if (logger == NULL)
{
printf("could not create logger object");
destroy_globals();
return -1;
}
/* initialize all global objects */
if (initialize_globals() != SUCCESS)
{
destroy_globals();
return -1;
}
logger->log(logger,CONTROL,"starting %s", DAEMON_NAME);
/* now its time to create all the different threads :-) */
if (start_threads() != SUCCESS)
{
/* ugh, not good */
logger->log(logger,CONTROL,"Fatal error: Needed Threads could not be started");
destroy_and_exit(-1);
}
int i;
for(i = 0; i<1; i++)
{
initiate_ike_sa_job_t *initiate_job;
initiate_job = initiate_ike_sa_job_create("localhost");
global_job_queue->add(global_job_queue, (job_t*)initiate_job);
}
logger->log(logger,CONTROL|MORE,"going to wait for exit signal");
/* go and handle signals*/
main_loop();
destroy_and_exit(0);
/* never reached */
return -1;
}
/**
* Main Loop.
* Waits for registered signals and acts dependently
*/
static void main_loop()
{
while(1)
{
int signal_number;
int error;
error = sigwait(&signal_set, &signal_number);
if(error)
{
/* do error code */
logger->log(logger,CONTROL,"Error %d when waiting for signal",error);
return;
}
switch (signal_number)
{
case SIGHUP:
{
logger->log(logger,CONTROL,"Signal of type SIGHUP received. Do nothing");
break;
}
case SIGINT:
{
logger->log(logger,CONTROL,"Signal of type SIGINT received. Exit main loop.");
return;
}
case SIGTERM:
{
logger->log(logger,CONTROL,"Signal of type SIGTERM received. Exit main loop.");
return;
}
default:
{
logger->log(logger,CONTROL,"Unknown signal %d received. Do nothing",signal_number);
break;
}
}
}
}
/**
* Registers signals SIGINT, SIGHUP and SIGTERM.
* Signals are handled in main_loop()
*/
static void register_signals()
{
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGINT);
sigaddset(&signal_set, SIGHUP);
sigaddset(&signal_set, SIGTERM);
pthread_sigmask(SIG_BLOCK, &signal_set, 0);
}
/**
* Initializes global objects
*
* @return
* - SUCCESS
* - FAILED
*/
static status_t initialize_globals()
{
/* initialize global object */
global_socket = socket_create(IKEV2_UDP_PORT);
global_ike_sa_manager = ike_sa_manager_create();
global_job_queue = job_queue_create();
global_event_queue = event_queue_create();
global_send_queue = send_queue_create();
global_configuration_manager = configuration_manager_create();
if ( (global_socket == NULL) ||
(global_job_queue == NULL) ||
(global_event_queue == NULL) ||
(global_send_queue == NULL) ||
(global_configuration_manager == NULL) ||
(global_ike_sa_manager == NULL))
{
return FAILED;
}
return SUCCESS;
}
/**
* Destroy global objects
*/
static void destroy_globals()
{
if (global_job_queue != NULL)
{
logger->log(logger,CONTROL|MOST,"destroy global_job_queue");
global_job_queue->destroy(global_job_queue);
}
if (global_event_queue != NULL)
{
logger->log(logger,CONTROL|MOST,"destroy global_event_queue");
global_event_queue->destroy(global_event_queue);
}
if (global_send_queue != NULL)
{
logger->log(logger,CONTROL|MOST,"destroy global_send_queue");
global_send_queue->destroy(global_send_queue);
}
if (global_socket != NULL)
{
logger->log(logger,CONTROL|MOST,"destroy global_socket");
global_socket->destroy(global_socket);
}
if (global_ike_sa_manager != NULL)
{
logger->log(logger,CONTROL|MOST,"destroy global_ike_sa_manager");
global_ike_sa_manager->destroy(global_ike_sa_manager);
}
if (global_configuration_manager != NULL)
{
logger->log(logger,CONTROL|MOST,"destroy global_configuration_manager");
global_configuration_manager->destroy(global_configuration_manager);
}
}
/**
* Creates all needed Threads
*
* @return
* - SUCCESS
* - FAILED
*/
static status_t start_threads()
{
sender_thread = sender_create();
if (sender_thread == NULL)
{
return FAILED;
}
receiver_thread = receiver_create();
if (receiver_thread == NULL)
{
return FAILED;
}
scheduler_thread = scheduler_create();
if (scheduler_thread == NULL)
{
return FAILED;
}
thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
if (thread_pool == NULL)
{
return FAILED;
}
return SUCCESS;
}
/**
* Ends all Threads
*
*/
static void end_threads()
{
if (receiver_thread != NULL)
{
receiver_thread->destroy(receiver_thread);
}
if (scheduler_thread != NULL)
{
scheduler_thread->destroy(scheduler_thread);
}
if (sender_thread != NULL)
{
sender_thread->destroy(sender_thread);
}
if (thread_pool != NULL)
{
thread_pool->destroy(thread_pool);
}
}
/**
* Destroys initialized objects, kills all threads and exits
*
* @param exit_code Code to exit with
*/
static void destroy_and_exit(int exit_code)
{
logger->log(logger,CONTROL,"going to exit daemon");
end_threads();
/* all globals can be destroyed now */
destroy_globals();
/* logger is destroyed */
logger->log(logger,CONTROL|MORE,"destroy logger");
logger->log(logger,CONTROL|MORE,"destroy logger_manager");
global_logger_manager->destroy_logger(global_logger_manager,logger);
global_logger_manager->destroy(global_logger_manager);
#ifdef LEAK_DETECTIVE
/* Leaks are reported in log file */
report_memory_leaks(void);
#endif
exit(exit_code);
}
|