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>
36 #include <config/credentials/credential_store.h>
37 #include <config/connections/local_connection_store.h>
38 #include <config/policies/local_policy_store.h>
41 typedef struct private_daemon_t private_daemon_t
;
44 * Private additions to daemon_t, contains threads and internal functions.
46 struct private_daemon_t
{
48 * Public members of daemon_t.
53 * A logger_t object assigned for daemon things.
58 * Signal set used for signal handling.
63 * The thread_id of main-thread.
65 pthread_t main_thread_id
;
70 * @param this calling object
72 void (*run
) (private_daemon_t
*this);
75 * Initialize the daemon.
77 * @param this calling object
78 * @param strict enforce a strict crl policy
80 void (*initialize
) (private_daemon_t
*this, bool strict
);
85 * @param this calling object
87 void (*destroy
) (private_daemon_t
*this);
91 * One and only instance of the daemon.
96 * Implementation of private_daemon_t.run.
98 static void run(private_daemon_t
*this)
100 /* reselect signals for this thread */
101 sigemptyset(&(this->signal_set
));
102 sigaddset(&(this->signal_set
), SIGINT
);
103 sigaddset(&(this->signal_set
), SIGHUP
);
104 sigaddset(&(this->signal_set
), SIGTERM
);
105 pthread_sigmask(SIG_BLOCK
, &(this->signal_set
), 0);
112 error
= sigwait(&(this->signal_set
), &signal_number
);
115 this->logger
->log(this->logger
, ERROR
, "Error %d when waiting for signal", error
);
118 switch (signal_number
)
122 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGHUP received. Do nothing");
127 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGINT received. Exit main loop");
131 this->logger
->log(this->logger
, CONTROL
, "Signal of type SIGTERM received. Exit main loop");
135 this->logger
->log(this->logger
, CONTROL
, "Unknown signal %d received. Do nothing", signal_number
);
143 * Implementation of daemon_t.kill.
145 static void kill_daemon(private_daemon_t
*this, char *reason
)
147 /* we send SIGTERM, so the daemon can cleanly shut down */
148 this->logger
->log(this->logger
, CONTROL
, "Killing daemon: %s", reason
);
149 if (this->main_thread_id
== pthread_self())
151 /* initialization failed, terminate daemon */
158 this->logger
->log(this->logger
, CONTROL
, "sending SIGTERM to ourself", reason
);
160 /* thread must die, since he produced a ciritcal failure and can't continue */
166 * Implementation of private_daemon_t.initialize.
168 static void initialize(private_daemon_t
*this, bool strict
)
170 credential_store_t
* credentials
;
172 this->public.configuration
= configuration_create();
173 this->public.socket
= socket_create(IKEV2_UDP_PORT
);
174 this->public.ike_sa_manager
= ike_sa_manager_create();
175 this->public.job_queue
= job_queue_create();
176 this->public.event_queue
= event_queue_create();
177 this->public.send_queue
= send_queue_create();
178 this->public.connections
= (connection_store_t
*)local_connection_store_create();
179 this->public.policies
= (policy_store_t
*)local_policy_store_create();
180 this->public.credentials
= credential_store_create(strict
);
182 /* load keys, ca certificates and crls */
183 credentials
= this->public.credentials
;
184 credentials
->load_ca_certificates(credentials
, CA_CERTIFICATE_DIR
);
185 credentials
->load_crls(credentials
, CRL_DIR
);
186 credentials
->load_private_keys(credentials
, SECRETS_FILE
, PRIVATE_KEY_DIR
);
189 /* start building threads, we are multi-threaded NOW */
190 this->public.stroke
= stroke_create();
191 this->public.sender
= sender_create();
192 this->public.receiver
= receiver_create();
193 this->public.scheduler
= scheduler_create();
194 this->public.kernel_interface
= kernel_interface_create();
195 this->public.thread_pool
= thread_pool_create(NUMBER_OF_WORKING_THREADS
);
199 * Destory all initiated objects
201 static void destroy(private_daemon_t
*this)
203 /* destruction is a non trivial task, we need to follow
204 * a strict order to prevent threading issues!
205 * Kill active threads first, except the sender, as
206 * the killed IKE_SA want to send delete messages.
208 if (this->public.receiver
!= NULL
)
209 { /* we don't want to receive anything... */
210 this->public.receiver
->destroy(this->public.receiver
);
212 if (this->public.stroke
!= NULL
)
213 { /* ignore all incoming user requests */
214 this->public.stroke
->destroy(this->public.stroke
);
216 if (this->public.scheduler
!= NULL
)
217 { /* stop scheduing jobs */
218 this->public.scheduler
->destroy(this->public.scheduler
);
220 if (this->public.thread_pool
!= NULL
)
221 { /* stop processing jobs */
222 this->public.thread_pool
->destroy(this->public.thread_pool
);
224 if (this->public.ike_sa_manager
!= NULL
)
225 { /* shut down manager with all IKE SAs */
226 this->public.ike_sa_manager
->destroy(this->public.ike_sa_manager
);
228 if (this->public.kernel_interface
!= NULL
)
229 { /* all child SAs should be down now, so kill kernel interface */
230 this->public.kernel_interface
->destroy(this->public.kernel_interface
);
232 /* destroy other infrastructure */
233 if (this->public.job_queue
!= NULL
)
235 this->public.job_queue
->destroy(this->public.job_queue
);
237 if (this->public.event_queue
!= NULL
)
239 this->public.event_queue
->destroy(this->public.event_queue
);
241 if (this->public.configuration
!= NULL
)
243 this->public.configuration
->destroy(this->public.configuration
);
245 if (this->public.credentials
!= NULL
)
247 this->public.credentials
->destroy(this->public.credentials
);
249 if (this->public.connections
!= NULL
)
251 this->public.connections
->destroy(this->public.connections
);
253 if (this->public.policies
!= NULL
)
255 this->public.policies
->destroy(this->public.policies
);
257 /* we hope the sender could send the outstanding deletes, but
258 * we shut down here at any cost */
259 if (this->public.sender
!= NULL
)
261 this->public.sender
->destroy(this->public.sender
);
263 if (this->public.send_queue
!= NULL
)
265 this->public.send_queue
->destroy(this->public.send_queue
);
267 if (this->public.socket
!= NULL
)
269 this->public.socket
->destroy(this->public.socket
);
274 void signal_handler(int signal
)
282 size
= backtrace(array
, 20);
283 strings
= backtrace_symbols(array
, size
);
284 logger
= logger_manager
->get_logger(logger_manager
, DAEMON
);
286 logger
->log(logger
, ERROR
, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size
);
288 for (i
= 0; i
< size
; i
++)
290 logger
->log(logger
, ERROR
, " %s", strings
[i
]);
293 logger
->log(logger
, ERROR
, "Killing ourself hard after SIGSEGV");
294 kill(getpid(), SIGKILL
);
298 * @brief Create the daemon.
300 * @return created daemon_t
302 private_daemon_t
*daemon_create(void)
304 private_daemon_t
*this = malloc_thing(private_daemon_t
);
305 struct sigaction action
;
309 this->destroy
= destroy
;
310 this->initialize
= initialize
;
311 this->public.kill
= (void (*) (daemon_t
*,char*))kill_daemon
;
313 /* NULL members for clean destruction */
314 this->public.socket
= NULL
;
315 this->public.ike_sa_manager
= NULL
;
316 this->public.job_queue
= NULL
;
317 this->public.event_queue
= NULL
;
318 this->public.send_queue
= NULL
;
319 this->public.configuration
= NULL
;
320 this->public.credentials
= NULL
;
321 this->public.connections
= NULL
;
322 this->public.policies
= NULL
;
323 this->public.sender
= NULL
;
324 this->public.receiver
= NULL
;
325 this->public.scheduler
= NULL
;
326 this->public.kernel_interface
= NULL
;
327 this->public.thread_pool
= NULL
;
328 this->public.stroke
= NULL
;
330 this->main_thread_id
= pthread_self();
332 /* setup signal handling for all threads */
333 sigemptyset(&(this->signal_set
));
334 sigaddset(&(this->signal_set
), SIGSEGV
);
335 sigaddset(&(this->signal_set
), SIGINT
);
336 sigaddset(&(this->signal_set
), SIGHUP
);
337 sigaddset(&(this->signal_set
), SIGTERM
);
338 pthread_sigmask(SIG_BLOCK
, &(this->signal_set
), 0);
340 /* setup SIGSEGV handler for all threads */
341 action
.sa_handler
= signal_handler
;
342 action
.sa_mask
= this->signal_set
;
344 if (sigaction(SIGSEGV
, &action
, NULL
) == -1)
346 this->logger
->log(this->logger
, ERROR
, "signal handler setup for SIGSEGV failed");
351 static void usage(const char *msg
)
353 if (msg
!= NULL
&& *msg
!= '\0')
354 fprintf(stderr
, "%s\n", msg
);
355 fprintf(stderr
, "Usage: charon"
359 " [--strictcrlpolicy]"
362 exit(msg
== NULL?
0 : 1);
367 * Main function, manages the daemon.
369 int main(int argc
, char *argv
[])
371 bool strict_crl_policy
= FALSE
;
373 private_daemon_t
*private_charon
;
377 /* handle arguments */
380 static const struct option long_opts
[] = {
381 { "help", no_argument
, NULL
, 'h' },
382 { "version", no_argument
, NULL
, 'v' },
383 { "use-syslog", no_argument
, NULL
, 'l' },
384 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
388 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
390 /* Note: "breaking" from case terminates loop */
393 case EOF
: /* end of flags */
397 break; /* not actually reached */
399 printf("Linux strongSwan %s\n", VERSION
);
402 logger_manager
->set_output(logger_manager
, ALL_LOGGERS
, NULL
);
405 strict_crl_policy
= TRUE
;
409 break; /* not actually reached */
414 private_charon
= daemon_create();
415 charon
= (daemon_t
*)private_charon
;
417 private_charon
->logger
= logger_manager
->get_logger(logger_manager
, DAEMON
);
419 private_charon
->logger
->log(private_charon
->logger
, CONTROL
,
420 "Starting Charon (strongSwan Version %s)", VERSION
);
422 /* initialize daemon */
423 private_charon
->initialize(private_charon
, strict_crl_policy
);
425 /* check/setup PID file */
426 if (stat(PID_FILE
, &stb
) == 0)
428 private_charon
->logger
->log(private_charon
->logger
, ERROR
,
429 "charon already running (\""PID_FILE
"\" exists)");
430 private_charon
->destroy(private_charon
);
433 pid_file
= fopen(PID_FILE
, "w");
436 fprintf(pid_file
, "%d\n", getpid());
441 private_charon
->run(private_charon
);
443 /* normal termination, cleanup and exit */
444 private_charon
->destroy(private_charon
);