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 <utils/allocator.h>
33 #include <queues/jobs/initiate_ike_sa_job.h>
38 typedef struct private_daemon_t private_daemon_t
;
41 * Private additions to daemon_t, contains
42 * threads and internal functions.
44 struct private_daemon_t
{
46 * public members of daemon_t
51 * logger_t object assigned for daemon things
56 * Signal set used for signal handling
63 pid_t main_thread_pid
;
68 void (*run
) (private_daemon_t
*this);
71 * a routine to add jobs for testing
73 void (*build_test_jobs
) (private_daemon_t
*this);
78 void (*initialize
) (private_daemon_t
*this);
83 void (*destroy
) (private_daemon_t
*this);
87 * instance of the daemon
92 * Loop of the main thread, waits for signals
94 static void run(private_daemon_t
*this)
101 error
= sigwait(&(this->signal_set
), &signal_number
);
104 this->logger
->log(this->logger
, ERROR
, "Error %d when waiting for signal", error
);
107 switch (signal_number
)
111 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGHUP received. Do nothing");
116 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGINT received. Exit main loop.");
120 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGTERM received. Exit main loop.");
124 this->logger
->log(this->logger
, CONTROL
, "Unknown signal %d received. Do nothing", signal_number
);
132 * Initialize the destruction of the daemon
134 static void kill_daemon(private_daemon_t
*this, char *reason
)
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())
140 /* initialization failed, terminate daemon */
146 this->logger
->log(this->logger
, CONTROL
, "sending SIGTERM to ourself", reason
);
148 /* thread must die, since he produced a ciritcal failure and can't continue */
154 * build some jobs to test daemon functionality
156 static void build_test_jobs(private_daemon_t
*this)
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
);
168 * Initialize global objects and threads
170 static void initialize(private_daemon_t
*this)
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();
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
);
186 * Destory all initiated objects
188 static void destroy(private_daemon_t
*this)
190 if (this->public.receiver
!= NULL
)
192 this->public.receiver
->destroy(this->public.receiver
);
194 if (this->public.scheduler
!= NULL
)
196 this->public.scheduler
->destroy(this->public.scheduler
);
198 if (this->public.sender
!= NULL
)
200 this->public.sender
->destroy(this->public.sender
);
202 if (this->public.thread_pool
!= NULL
)
204 this->public.thread_pool
->destroy(this->public.thread_pool
);
206 if (this->public.job_queue
!= NULL
)
208 this->public.job_queue
->destroy(this->public.job_queue
);
210 if (this->public.event_queue
!= NULL
)
212 this->public.event_queue
->destroy(this->public.event_queue
);
214 if (this->public.send_queue
!= NULL
)
216 this->public.send_queue
->destroy(this->public.send_queue
);
218 if (this->public.socket
!= NULL
)
220 this->public.socket
->destroy(this->public.socket
);
222 if (this->public.ike_sa_manager
!= NULL
)
224 this->public.ike_sa_manager
->destroy(this->public.ike_sa_manager
);
226 if (this->public.configuration_manager
!= NULL
)
228 this->public.configuration_manager
->destroy(this->public.configuration_manager
);
231 this->public.logger_manager
->destroy(this->public.logger_manager
);
232 allocator_free(this);
238 * @brief Create the daemon.
240 * @return created daemon_t
242 private_daemon_t
*daemon_create()
244 private_daemon_t
*this = allocator_alloc_thing(private_daemon_t
);
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
;
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
);
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
;
269 this->main_thread_pid
= getpid();
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);
282 * Main function, manages the daemon
284 int main(int argc
, char *argv
[])
286 private_daemon_t
*private_charon
;
288 private_charon
= daemon_create();
289 charon
= (daemon_t
*)private_charon
;
291 private_charon
->initialize(private_charon
);
293 private_charon
->build_test_jobs(private_charon
);
295 private_charon
->run(private_charon
);
297 private_charon
->destroy(private_charon
);
299 #ifdef LEAK_DETECTIVE
300 report_memory_leaks(void);