4 * @brief Implementation of daemon_t and 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
27 #include <sys/types.h>
35 #include <config/connections/local_connection_store.h>
36 #include <config/credentials/local_credential_store.h>
37 #include <config/policies/local_policy_store.h>
40 typedef struct private_daemon_t private_daemon_t
;
43 * Private additions to daemon_t, contains threads and internal functions.
45 struct private_daemon_t
{
47 * Public members of daemon_t.
52 * A logger_t object assigned for daemon things.
57 * Signal set used for signal handling.
62 * The thread_id of main-thread.
64 pthread_t main_thread_id
;
69 * @param this calling object
71 void (*run
) (private_daemon_t
*this);
74 * Initialize the daemon.
76 * @param this calling object
78 void (*initialize
) (private_daemon_t
*this);
83 * @param this calling object
85 void (*destroy
) (private_daemon_t
*this);
89 * One and only instance of the daemon.
94 * Implementation of private_daemon_t.run.
96 static void run(private_daemon_t
*this)
98 /* reselect signals for this thread */
99 sigemptyset(&(this->signal_set
));
100 sigaddset(&(this->signal_set
), SIGINT
);
101 sigaddset(&(this->signal_set
), SIGHUP
);
102 sigaddset(&(this->signal_set
), SIGTERM
);
103 pthread_sigmask(SIG_BLOCK
, &(this->signal_set
), 0);
110 error
= sigwait(&(this->signal_set
), &signal_number
);
113 this->logger
->log(this->logger
, ERROR
, "Error %d when waiting for signal", error
);
116 switch (signal_number
)
120 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGHUP received. Do nothing");
125 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGINT received. Exit main loop");
129 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGTERM received. Exit main loop");
133 this->logger
->log(this->logger
, CONTROL
, "Unknown signal %d received. Do nothing", signal_number
);
141 * Implementation of daemon_t.kill.
143 static void kill_daemon(private_daemon_t
*this, char *reason
)
145 /* we send SIGTERM, so the daemon can cleanly shut down */
146 this->logger
->log(this->logger
, CONTROL
, "Killing daemon: %s", reason
);
147 if (this->main_thread_id
== pthread_self())
149 /* initialization failed, terminate daemon */
156 this->logger
->log(this->logger
, CONTROL
, "sending SIGTERM to ourself", reason
);
158 /* thread must die, since he produced a ciritcal failure and can't continue */
164 * Implementation of private_daemon_t.initialize.
166 static void initialize(private_daemon_t
*this)
168 local_credential_store_t
* cred_store
;
170 this->public.configuration
= configuration_create();
171 this->public.socket
= socket_create(IKEV2_UDP_PORT
);
172 this->public.ike_sa_manager
= ike_sa_manager_create();
173 this->public.job_queue
= job_queue_create();
174 this->public.event_queue
= event_queue_create();
175 this->public.send_queue
= send_queue_create();
176 this->public.connections
= (connection_store_t
*)local_connection_store_create();
177 this->public.policies
= (policy_store_t
*)local_policy_store_create();
178 this->public.credentials
= (credential_store_t
*)(cred_store
= local_credential_store_create());
180 /* load keys & certs */
181 cred_store
->load_certificates(cred_store
, CERTIFICATE_DIR
);
182 cred_store
->load_private_keys(cred_store
, SECRETS_FILE
, PRIVATE_KEY_DIR
);
185 /* start building threads, we are multi-threaded NOW */
186 this->public.stroke
= stroke_create();
187 this->public.sender
= sender_create();
188 this->public.receiver
= receiver_create();
189 this->public.scheduler
= scheduler_create();
190 this->public.kernel_interface
= kernel_interface_create();
191 this->public.thread_pool
= thread_pool_create(NUMBER_OF_WORKING_THREADS
);
195 * Destory all initiated objects
197 static void destroy(private_daemon_t
*this)
199 if (this->public.ike_sa_manager
!= NULL
)
201 this->public.ike_sa_manager
->destroy(this->public.ike_sa_manager
);
203 if (this->public.kernel_interface
!= NULL
)
205 this->public.kernel_interface
->destroy(this->public.kernel_interface
);
207 if (this->public.receiver
!= NULL
)
209 this->public.receiver
->destroy(this->public.receiver
);
211 if (this->public.scheduler
!= NULL
)
213 this->public.scheduler
->destroy(this->public.scheduler
);
215 if (this->public.sender
!= NULL
)
217 this->public.sender
->destroy(this->public.sender
);
219 if (this->public.thread_pool
!= NULL
)
221 this->public.thread_pool
->destroy(this->public.thread_pool
);
223 if (this->public.job_queue
!= NULL
)
225 this->public.job_queue
->destroy(this->public.job_queue
);
227 if (this->public.event_queue
!= NULL
)
229 this->public.event_queue
->destroy(this->public.event_queue
);
231 if (this->public.send_queue
!= NULL
)
233 this->public.send_queue
->destroy(this->public.send_queue
);
235 if (this->public.socket
!= NULL
)
237 this->public.socket
->destroy(this->public.socket
);
239 if (this->public.configuration
!= NULL
)
241 this->public.configuration
->destroy(this->public.configuration
);
243 if (this->public.credentials
!= NULL
)
245 this->public.credentials
->destroy(this->public.credentials
);
247 if (this->public.connections
!= NULL
)
249 this->public.connections
->destroy(this->public.connections
);
251 if (this->public.policies
!= NULL
)
253 this->public.policies
->destroy(this->public.policies
);
255 if (this->public.stroke
!= NULL
)
257 this->public.stroke
->destroy(this->public.stroke
);
262 void signal_handler(int signal
)
270 size
= backtrace(array
, 20);
271 strings
= backtrace_symbols(array
, size
);
272 logger
= logger_manager
->get_logger(logger_manager
, DAEMON
);
274 logger
->log(logger
, ERROR
, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size
);
276 for (i
= 0; i
< size
; i
++)
278 logger
->log(logger
, ERROR
, " %s", strings
[i
]);
281 logger
->log(logger
, ERROR
, "Killing ourself hard after SIGSEGV");
282 kill(getpid(), SIGKILL
);
286 * @brief Create the daemon.
288 * @return created daemon_t
290 private_daemon_t
*daemon_create()
292 private_daemon_t
*this = malloc_thing(private_daemon_t
);
293 struct sigaction action
;
297 this->destroy
= destroy
;
298 this->initialize
= initialize
;
299 this->public.kill
= (void (*) (daemon_t
*,char*))kill_daemon
;
301 /* NULL members for clean destruction */
302 this->public.socket
= NULL
;
303 this->public.ike_sa_manager
= NULL
;
304 this->public.job_queue
= NULL
;
305 this->public.event_queue
= NULL
;
306 this->public.send_queue
= NULL
;
307 this->public.configuration
= NULL
;
308 this->public.credentials
= NULL
;
309 this->public.connections
= NULL
;
310 this->public.policies
= NULL
;
311 this->public.sender
= NULL
;
312 this->public.receiver
= NULL
;
313 this->public.scheduler
= NULL
;
314 this->public.kernel_interface
= NULL
;
315 this->public.thread_pool
= NULL
;
316 this->public.stroke
= NULL
;
318 this->main_thread_id
= pthread_self();
320 /* setup signal handling for all threads */
321 sigemptyset(&(this->signal_set
));
322 sigaddset(&(this->signal_set
), SIGSEGV
);
323 sigaddset(&(this->signal_set
), SIGINT
);
324 sigaddset(&(this->signal_set
), SIGHUP
);
325 sigaddset(&(this->signal_set
), SIGTERM
);
326 pthread_sigmask(SIG_BLOCK
, &(this->signal_set
), 0);
328 /* setup SIGSEGV handler for all threads */
329 action
.sa_handler
= signal_handler
;
330 action
.sa_mask
= this->signal_set
;
332 if (sigaction(SIGSEGV
, &action
, NULL
) == -1)
334 this->logger
->log(this->logger
, ERROR
, "signal handler setup for SIGSEGV failed");
340 * Main function, manages the daemon.
342 int main(int argc
, char *argv
[])
344 private_daemon_t
*private_charon
;
349 /* trivial argument parsing */
350 for (i
= 1; i
< argc
; i
++)
352 if (strcmp(argv
[i
], "--use-syslog") == 0)
354 logger_manager
->set_output(logger_manager
, ALL_LOGGERS
, NULL
);
357 private_charon
= daemon_create();
358 charon
= (daemon_t
*)private_charon
;
360 private_charon
->logger
= logger_manager
->get_logger(logger_manager
, DAEMON
);
362 /* initialize daemon */
363 private_charon
->initialize(private_charon
);
365 /* check/setup PID file */
366 if (stat(PID_FILE
, &stb
) == 0)
368 private_charon
->logger
->log(private_charon
->logger
, ERROR
,
369 "charon already running (\""PID_FILE
"\" exists)");
370 private_charon
->destroy(private_charon
);
373 pid_file
= fopen(PID_FILE
, "w");
376 fprintf(pid_file
, "%d\n", getpid());
381 private_charon
->run(private_charon
);
383 /* normal termination, cleanup and exit */
384 private_charon
->destroy(private_charon
);