strongswan/src/charon/daemon.c

402 lines
10 KiB
C
Raw Normal View History

2005-11-03 07:18:16 +00:00
/**
* @file daemon.c
*
2005-12-06 12:54:34 +00:00
* @brief Implementation of daemon_t and main of IKEv2-Daemon.
2005-11-03 07:18:16 +00:00
*
*/
/*
* 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.
*/
2005-11-03 08:56:04 +00:00
#include <stdio.h>
2005-11-16 12:06:34 +00:00
#include <signal.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <execinfo.h>
#include <string.h>
2005-11-16 12:06:34 +00:00
#include "daemon.h"
#include <types.h>
#include <config/connections/local_connection_store.h>
#include <config/credentials/local_credential_store.h>
#include <config/policies/local_policy_store.h>
2005-11-03 17:26:49 +00:00
2005-11-03 08:56:04 +00:00
2005-11-29 10:25:07 +00:00
typedef struct private_daemon_t private_daemon_t;
2005-11-16 12:06:34 +00:00
/**
2005-12-06 12:54:34 +00:00
* Private additions to daemon_t, contains threads and internal functions.
2005-11-16 12:06:34 +00:00
*/
2005-11-29 10:25:07 +00:00
struct private_daemon_t {
/**
2005-12-06 12:54:34 +00:00
* Public members of daemon_t.
2005-11-29 10:25:07 +00:00
*/
daemon_t public;
/**
2005-12-06 12:54:34 +00:00
* A logger_t object assigned for daemon things.
2005-11-29 10:25:07 +00:00
*/
logger_t *logger;
2005-11-16 12:06:34 +00:00
2005-11-29 10:25:07 +00:00
/**
2005-12-06 12:54:34 +00:00
* Signal set used for signal handling.
2005-11-29 10:25:07 +00:00
*/
sigset_t signal_set;
2005-11-29 11:27:25 +00:00
/**
2005-12-06 12:54:34 +00:00
* The thread_id of main-thread.
2005-11-29 11:27:25 +00:00
*/
pthread_t main_thread_id;
2005-11-29 11:27:25 +00:00
/**
2005-12-06 12:54:34 +00:00
* Main loop function.
*
* @param this calling object
2005-11-29 11:27:25 +00:00
*/
2005-11-29 10:25:07 +00:00
void (*run) (private_daemon_t *this);
2005-11-29 11:27:25 +00:00
/**
2005-12-06 12:54:34 +00:00
* Initialize the daemon.
*
* @param this calling object
2005-11-29 11:27:25 +00:00
*/
2005-11-29 10:25:07 +00:00
void (*initialize) (private_daemon_t *this);
2005-11-29 11:27:25 +00:00
/**
2005-12-06 12:54:34 +00:00
* Destroy the daemon.
*
* @param this calling object
2005-11-29 11:27:25 +00:00
*/
void (*destroy) (private_daemon_t *this);
2005-11-29 10:25:07 +00:00
};
2005-11-16 12:06:34 +00:00
2005-11-29 10:25:07 +00:00
/**
2005-12-06 12:54:34 +00:00
* One and only instance of the daemon.
2005-11-29 10:25:07 +00:00
*/
daemon_t *charon;
2005-11-16 12:06:34 +00:00
/**
2005-12-06 12:54:34 +00:00
* Implementation of private_daemon_t.run.
2005-11-16 12:06:34 +00:00
*/
2005-11-29 10:25:07 +00:00
static void run(private_daemon_t *this)
{
/* reselect signals for this thread */
sigemptyset(&(this->signal_set));
sigaddset(&(this->signal_set), SIGINT);
sigaddset(&(this->signal_set), SIGHUP);
sigaddset(&(this->signal_set), SIGTERM);
pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
2005-11-29 10:25:07 +00:00
while(TRUE)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
int signal_number;
int error;
error = sigwait(&(this->signal_set), &signal_number);
if(error)
{
this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
return;
}
switch (signal_number)
{
2005-11-16 12:06:34 +00:00
case SIGHUP:
{
2005-11-29 10:25:07 +00:00
this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
2005-11-16 12:06:34 +00:00
break;
}
case SIGINT:
{
2005-12-06 12:54:34 +00:00
this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
2005-11-16 12:06:34 +00:00
return;
}
case SIGTERM:
2005-12-06 12:54:34 +00:00
this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
2005-11-16 12:06:34 +00:00
return;
default:
{
2005-11-29 10:25:07 +00:00
this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
2005-11-16 12:06:34 +00:00
break;
}
}
}
}
/**
2005-12-06 12:54:34 +00:00
* Implementation of daemon_t.kill.
2005-11-16 12:06:34 +00:00
*/
2005-11-29 11:27:25 +00:00
static void kill_daemon(private_daemon_t *this, char *reason)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
/* we send SIGTERM, so the daemon can cleanly shut down */
2005-12-06 12:54:34 +00:00
this->logger->log(this->logger, CONTROL, "Killing daemon: %s", reason);
if (this->main_thread_id == pthread_self())
2005-11-29 11:27:25 +00:00
{
/* initialization failed, terminate daemon */
this->destroy(this);
unlink(PID_FILE);
2005-11-29 11:27:25 +00:00
exit(-1);
}
else
{
this->logger->log(this->logger, CONTROL, "sending SIGTERM to ourself", reason);
kill(0, SIGTERM);
/* thread must die, since he produced a ciritcal failure and can't continue */
pthread_exit(NULL);
}
2005-11-29 10:25:07 +00:00
}
2005-11-16 12:06:34 +00:00
/**
2005-12-06 12:54:34 +00:00
* Implementation of private_daemon_t.initialize.
2005-11-16 12:06:34 +00:00
*/
2005-11-29 11:27:25 +00:00
static void initialize(private_daemon_t *this)
2005-11-16 12:06:34 +00:00
{
local_credential_store_t* cred_store;
this->public.configuration = configuration_create();
2005-11-29 11:27:25 +00:00
this->public.socket = socket_create(IKEV2_UDP_PORT);
this->public.ike_sa_manager = ike_sa_manager_create();
this->public.job_queue = job_queue_create();
this->public.event_queue = event_queue_create();
this->public.send_queue = send_queue_create();
this->public.connections = (connection_store_t*)local_connection_store_create();
this->public.policies = (policy_store_t*)local_policy_store_create();
this->public.credentials = (credential_store_t*)(cred_store = local_credential_store_create());
/* load keys & certs */
2006-05-30 07:37:48 +00:00
cred_store->load_ca_certificates(cred_store, CA_CERTIFICATE_DIR);
cred_store->load_private_keys(cred_store, SECRETS_FILE, PRIVATE_KEY_DIR);
2005-11-29 10:25:07 +00:00
/* start building threads, we are multi-threaded NOW */
this->public.stroke = stroke_create();
2005-11-29 11:27:25 +00:00
this->public.sender = sender_create();
this->public.receiver = receiver_create();
this->public.scheduler = scheduler_create();
2006-02-09 16:25:02 +00:00
this->public.kernel_interface = kernel_interface_create();
this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
2005-11-16 12:06:34 +00:00
}
/**
2005-11-29 10:25:07 +00:00
* Destory all initiated objects
2005-11-16 12:06:34 +00:00
*/
2005-11-29 11:27:25 +00:00
static void destroy(private_daemon_t *this)
2005-11-16 12:06:34 +00:00
{
/* destruction is a non trivial task, we need to follow
* a strict order to prevent threading issues!
* Kill active threads first, except the sender, as
* the killed IKE_SA want to send delete messages.
*/
2005-11-29 11:27:25 +00:00
if (this->public.receiver != NULL)
{ /* we don't want to receive anything... */
2005-11-29 11:27:25 +00:00
this->public.receiver->destroy(this->public.receiver);
2005-11-16 12:06:34 +00:00
}
if (this->public.stroke != NULL)
{ /* ignore all incoming user requests */
this->public.stroke->destroy(this->public.stroke);
}
2005-11-29 11:27:25 +00:00
if (this->public.scheduler != NULL)
{ /* stop scheduing jobs */
2005-11-29 11:27:25 +00:00
this->public.scheduler->destroy(this->public.scheduler);
2005-11-16 12:06:34 +00:00
}
2005-11-29 11:27:25 +00:00
if (this->public.thread_pool != NULL)
{ /* stop processing jobs */
2005-11-29 11:27:25 +00:00
this->public.thread_pool->destroy(this->public.thread_pool);
2005-11-16 12:06:34 +00:00
}
if (this->public.ike_sa_manager != NULL)
{ /* shut down manager with all IKE SAs */
this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
}
if (this->public.kernel_interface != NULL)
{ /* all child SAs should be down now, so kill kernel interface */
this->public.kernel_interface->destroy(this->public.kernel_interface);
}
/* destroy other infrastructure */
2005-11-29 11:27:25 +00:00
if (this->public.job_queue != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 11:27:25 +00:00
this->public.job_queue->destroy(this->public.job_queue);
2005-11-16 12:06:34 +00:00
}
2005-11-29 11:27:25 +00:00
if (this->public.event_queue != NULL)
2005-11-17 11:24:03 +00:00
{
2005-11-29 11:27:25 +00:00
this->public.event_queue->destroy(this->public.event_queue);
2005-11-17 11:24:03 +00:00
}
if (this->public.configuration != NULL)
2005-11-16 12:06:34 +00:00
{
this->public.configuration->destroy(this->public.configuration);
2005-11-16 12:06:34 +00:00
}
if (this->public.credentials != NULL)
{
this->public.credentials->destroy(this->public.credentials);
}
if (this->public.connections != NULL)
{
this->public.connections->destroy(this->public.connections);
}
if (this->public.policies != NULL)
{
this->public.policies->destroy(this->public.policies);
}
/* we hope the sender could send the outstanding deletes, but
* we shut down here at any cost */
if (this->public.sender != NULL)
{
this->public.sender->destroy(this->public.sender);
}
if (this->public.send_queue != NULL)
{
this->public.send_queue->destroy(this->public.send_queue);
}
if (this->public.socket != NULL)
{
this->public.socket->destroy(this->public.socket);
}
free(this);
2005-11-16 12:06:34 +00:00
}
void signal_handler(int signal)
{
void *array[20];
size_t size;
char **strings;
size_t i;
logger_t *logger;
size = backtrace(array, 20);
strings = backtrace_symbols(array, size);
logger = logger_manager->get_logger(logger_manager, DAEMON);
logger->log(logger, ERROR, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size);
2005-11-16 12:06:34 +00:00
for (i = 0; i < size; i++)
{
logger->log(logger, ERROR, " %s", strings[i]);
}
free (strings);
logger->log(logger, ERROR, "Killing ourself hard after SIGSEGV");
kill(getpid(), SIGKILL);
}
2005-11-29 10:25:07 +00:00
2005-11-16 12:06:34 +00:00
/**
2005-11-29 10:25:07 +00:00
* @brief Create the daemon.
2005-11-16 12:06:34 +00:00
*
2005-11-29 10:25:07 +00:00
* @return created daemon_t
2005-11-16 12:06:34 +00:00
*/
private_daemon_t *daemon_create(void)
{
private_daemon_t *this = malloc_thing(private_daemon_t);
struct sigaction action;
2005-11-29 10:25:07 +00:00
/* assign methods */
this->run = run;
2005-11-29 11:27:25 +00:00
this->destroy = destroy;
this->initialize = initialize;
this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
2005-11-29 10:25:07 +00:00
/* NULL members for clean destruction */
this->public.socket = NULL;
this->public.ike_sa_manager = NULL;
this->public.job_queue = NULL;
this->public.event_queue = NULL;
this->public.send_queue = NULL;
this->public.configuration = NULL;
this->public.credentials = NULL;
this->public.connections = NULL;
this->public.policies = NULL;
2005-11-29 10:25:07 +00:00
this->public.sender= NULL;
this->public.receiver = NULL;
this->public.scheduler = NULL;
2006-02-09 16:25:02 +00:00
this->public.kernel_interface = NULL;
2005-11-29 10:25:07 +00:00
this->public.thread_pool = NULL;
this->public.stroke = NULL;
2005-11-29 10:25:07 +00:00
this->main_thread_id = pthread_self();
2005-11-29 11:27:25 +00:00
/* setup signal handling for all threads */
2005-11-29 10:25:07 +00:00
sigemptyset(&(this->signal_set));
sigaddset(&(this->signal_set), SIGSEGV);
2005-11-29 10:25:07 +00:00
sigaddset(&(this->signal_set), SIGINT);
sigaddset(&(this->signal_set), SIGHUP);
sigaddset(&(this->signal_set), SIGTERM);
pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
/* setup SIGSEGV handler for all threads */
action.sa_handler = signal_handler;
action.sa_mask = this->signal_set;
action.sa_flags = 0;
if (sigaction(SIGSEGV, &action, NULL) == -1)
{
this->logger->log(this->logger, ERROR, "signal handler setup for SIGSEGV failed");
}
2005-11-29 10:25:07 +00:00
return this;
2005-11-16 12:06:34 +00:00
}
/**
2005-12-06 12:54:34 +00:00
* Main function, manages the daemon.
2005-11-16 12:06:34 +00:00
*/
2005-11-29 10:25:07 +00:00
int main(int argc, char *argv[])
{
2005-11-29 10:25:07 +00:00
private_daemon_t *private_charon;
FILE *pid_file;
struct stat stb;
int i;
/* trivial argument parsing */
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--use-syslog") == 0)
{
logger_manager->set_output(logger_manager, ALL_LOGGERS, NULL);
}
}
2005-11-29 10:25:07 +00:00
private_charon = daemon_create();
charon = (daemon_t*)private_charon;
private_charon->logger = logger_manager->get_logger(logger_manager, DAEMON);
2006-05-31 05:48:32 +00:00
private_charon->logger->log(private_charon->logger, CONTROL,
"Starting Charon (strongSwan Version %s)", VERSION);
/* initialize daemon */
private_charon->initialize(private_charon);
/* check/setup PID file */
if (stat(PID_FILE, &stb) == 0)
{
private_charon->logger->log(private_charon->logger, ERROR,
"charon already running (\""PID_FILE"\" exists)");
private_charon->destroy(private_charon);
exit(-1);
}
pid_file = fopen(PID_FILE, "w");
if (pid_file)
{
fprintf(pid_file, "%d\n", getpid());
fclose(pid_file);
}
2005-11-29 10:25:07 +00:00
/* run daemon */
2005-11-29 10:25:07 +00:00
private_charon->run(private_charon);
/* normal termination, cleanup and exit */
2005-11-29 11:27:25 +00:00
private_charon->destroy(private_charon);
unlink(PID_FILE);
2005-11-16 12:06:34 +00:00
return 0;
2005-11-03 08:22:10 +00:00
}
2005-11-29 10:25:07 +00:00