code of message_t cleaned and added more logs
[strongswan.git] / Source / charon / daemon.c
1 /**
2 * @file daemon.c
3 *
4 * @brief 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 #include <unistd.h>
27
28 #include "daemon.h"
29
30
31 #include <types.h>
32 #include <utils/allocator.h>
33 #include <queues/jobs/initiate_ike_sa_job.h>
34
35
36
37
38 typedef struct private_daemon_t private_daemon_t;
39
40 /**
41 * Private additions to daemon_t, contains
42 * threads and internal functions.
43 */
44 struct private_daemon_t {
45 /**
46 * public members of daemon_t
47 */
48 daemon_t public;
49
50 /**
51 * logger_t object assigned for daemon things
52 */
53 logger_t *logger;
54
55 /**
56 * Signal set used for signal handling
57 */
58 sigset_t signal_set;
59
60 /**
61 * pid of main-thread
62 */
63 pid_t main_thread_pid;
64
65 /**
66 * main loop
67 */
68 void (*run) (private_daemon_t *this);
69
70 /**
71 * a routine to add jobs for testing
72 */
73 void (*build_test_jobs) (private_daemon_t *this);
74
75 /**
76 * initializing daemon
77 */
78 void (*initialize) (private_daemon_t *this);
79
80 /**
81 * destroy the daemon
82 */
83 void (*destroy) (private_daemon_t *this);
84 };
85
86 /**
87 * instance of the daemon
88 */
89 daemon_t *charon;
90
91 /**
92 * Loop of the main thread, waits for signals
93 */
94 static void run(private_daemon_t *this)
95 {
96 while(TRUE)
97 {
98 int signal_number;
99 int error;
100
101 error = sigwait(&(this->signal_set), &signal_number);
102 if(error)
103 {
104 this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
105 return;
106 }
107 switch (signal_number)
108 {
109 case SIGHUP:
110 {
111 this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
112 break;
113 }
114 case SIGINT:
115 {
116 this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop.");
117 return;
118 }
119 case SIGTERM:
120 this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop.");
121 return;
122 default:
123 {
124 this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
125 break;
126 }
127 }
128 }
129 }
130
131 /**
132 * Initialize the destruction of the daemon
133 */
134 static void kill_daemon(private_daemon_t *this, char *reason)
135 {
136 /* we send SIGTERM, so the daemon can cleanly shut down */
137 this->logger->log(this->logger, ERROR, "Killing daemon: %s", reason);
138 if (this->main_thread_pid == getpid())
139 {
140 /* initialization failed, terminate daemon */
141 this->destroy(this);
142 exit(-1);
143 }
144 else
145 {
146 this->logger->log(this->logger, CONTROL, "sending SIGTERM to ourself", reason);
147 kill(0, SIGTERM);
148 /* thread must die, since he produced a ciritcal failure and can't continue */
149 pthread_exit(NULL);
150 }
151 }
152
153 /**
154 * build some jobs to test daemon functionality
155 */
156 static void build_test_jobs(private_daemon_t *this)
157 {
158 int i;
159 for(i = 0; i<1; i++)
160 {
161 initiate_ike_sa_job_t *initiate_job;
162 initiate_job = initiate_ike_sa_job_create("localhost");
163 this->public.job_queue->add(this->public.job_queue, (job_t*)initiate_job);
164 }
165 }
166
167 /**
168 * Initialize global objects and threads
169 */
170 static void initialize(private_daemon_t *this)
171 {
172 this->public.socket = socket_create(IKEV2_UDP_PORT);
173 this->public.ike_sa_manager = ike_sa_manager_create();
174 this->public.job_queue = job_queue_create();
175 this->public.event_queue = event_queue_create();
176 this->public.send_queue = send_queue_create();
177 this->public.configuration_manager = configuration_manager_create();
178
179 this->public.sender = sender_create();
180 this->public.receiver = receiver_create();
181 this->public.scheduler = scheduler_create();
182 this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
183 }
184
185 /**
186 * Destory all initiated objects
187 */
188 static void destroy(private_daemon_t *this)
189 {
190 if (this->public.receiver != NULL)
191 {
192 this->public.receiver->destroy(this->public.receiver);
193 }
194 if (this->public.scheduler != NULL)
195 {
196 this->public.scheduler->destroy(this->public.scheduler);
197 }
198 if (this->public.sender != NULL)
199 {
200 this->public.sender->destroy(this->public.sender);
201 }
202 if (this->public.thread_pool != NULL)
203 {
204 this->public.thread_pool->destroy(this->public.thread_pool);
205 }
206 if (this->public.job_queue != NULL)
207 {
208 this->public.job_queue->destroy(this->public.job_queue);
209 }
210 if (this->public.event_queue != NULL)
211 {
212 this->public.event_queue->destroy(this->public.event_queue);
213 }
214 if (this->public.send_queue != NULL)
215 {
216 this->public.send_queue->destroy(this->public.send_queue);
217 }
218 if (this->public.socket != NULL)
219 {
220 this->public.socket->destroy(this->public.socket);
221 }
222 if (this->public.ike_sa_manager != NULL)
223 {
224 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
225 }
226 if (this->public.configuration_manager != NULL)
227 {
228 this->public.configuration_manager->destroy(this->public.configuration_manager);
229 }
230
231 this->public.logger_manager->destroy(this->public.logger_manager);
232 allocator_free(this);
233 }
234
235
236
237 /**
238 * @brief Create the daemon.
239 *
240 * @return created daemon_t
241 */
242 private_daemon_t *daemon_create()
243 {
244 private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
245
246 /* assign methods */
247 this->run = run;
248 this->destroy = destroy;
249 this->build_test_jobs = build_test_jobs;
250 this->initialize = initialize;
251 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
252
253 /* first build a logger */
254 this->public.logger_manager = logger_manager_create(DEFAULT_LOGLEVEL);
255 this->logger = (this->public.logger_manager)->create_logger(this->public.logger_manager, DAEMON, NULL);
256
257 /* NULL members for clean destruction */
258 this->public.socket = NULL;
259 this->public.ike_sa_manager = NULL;
260 this->public.job_queue = NULL;
261 this->public.event_queue = NULL;
262 this->public.send_queue = NULL;
263 this->public.configuration_manager = NULL;
264 this->public.sender= NULL;
265 this->public.receiver = NULL;
266 this->public.scheduler = NULL;
267 this->public.thread_pool = NULL;
268
269 this->main_thread_pid = getpid();
270
271 /* setup signal handling */
272 sigemptyset(&(this->signal_set));
273 sigaddset(&(this->signal_set), SIGINT);
274 sigaddset(&(this->signal_set), SIGHUP);
275 sigaddset(&(this->signal_set), SIGTERM);
276 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
277
278 return this;
279 }
280
281 /**
282 * Main function, manages the daemon
283 */
284 int main(int argc, char *argv[])
285 {
286 private_daemon_t *private_charon;
287
288 private_charon = daemon_create();
289 charon = (daemon_t*)private_charon;
290
291 private_charon->initialize(private_charon);
292
293 private_charon->build_test_jobs(private_charon);
294
295 private_charon->run(private_charon);
296
297 private_charon->destroy(private_charon);
298
299 #ifdef LEAK_DETECTIVE
300 report_memory_leaks(void);
301 #endif
302
303 exit(0);
304 }
305