strongswan/Source/charon/daemon.c

280 lines
6.4 KiB
C
Raw Normal View History

2005-11-03 07:18:16 +00:00
/**
* @file daemon.c
*
* @brief Main of IKEv2-Daemon
*
*/
/*
* 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 "daemon.h"
2005-11-11 10:29:32 +00:00
#include <types.h>
#include <utils/allocator.h>
2005-11-24 16:57:16 +00:00
#include <queues/jobs/initiate_ike_sa_job.h>
2005-11-03 17:26:49 +00:00
2005-11-03 08:56:04 +00:00
2005-11-16 12:06:34 +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-11-29 10:25:07 +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 {
/**
* public members of daemon_t
*/
daemon_t public;
/**
* logger_t object assigned for daemon things
*/
logger_t *logger;
2005-11-16 12:06:34 +00:00
2005-11-29 10:25:07 +00:00
/**
* Signal set used for signal handling
*/
sigset_t signal_set;
void (*run) (private_daemon_t *this);
void (*destroy) (private_daemon_t *this, char *reason);
void (*build_test_jobs) (private_daemon_t *this);
void (*initialize) (private_daemon_t *this);
void (*cleanup) (private_daemon_t *this);
};
2005-11-16 12:06:34 +00:00
2005-11-03 17:26:49 +00:00
2005-11-29 10:25:07 +00:00
/**
* instance of the daemon
*/
daemon_t *charon;
2005-11-16 12:22:57 +00:00
2005-11-16 12:06:34 +00:00
/**
2005-11-29 10:25:07 +00:00
* Loop of the main thread, waits for signals
2005-11-16 12:06:34 +00:00
*/
2005-11-29 10:25:07 +00:00
static void run(private_daemon_t *this)
{
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-11-29 10:25:07 +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-11-29 10:25:07 +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-11-29 10:25:07 +00:00
* Initialize the destruction of the daemon
2005-11-16 12:06:34 +00:00
*/
2005-11-29 10:25:07 +00:00
static void destroy(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 */
this->logger->log(this->logger, ERROR, "Killing daemon: %s", reason);
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-16 12:06:34 +00:00
2005-11-29 10:25:07 +00:00
/**
* build some jobs to test daemon functionality
*/
static void build_test_jobs(daemon_t *this)
{
int i;
for(i = 0; i<1; i++)
{
initiate_ike_sa_job_t *initiate_job;
initiate_job = initiate_ike_sa_job_create("localhost");
this->job_queue->add(this->job_queue, (job_t*)initiate_job);
}
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
2005-11-16 12:06:34 +00:00
/**
2005-11-29 10:25:07 +00:00
* Initialize global objects and threads
2005-11-16 12:06:34 +00:00
*/
2005-11-29 10:25:07 +00:00
static void initialize(daemon_t *this)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->socket = socket_create(IKEV2_UDP_PORT);
this->ike_sa_manager = ike_sa_manager_create();
this->job_queue = job_queue_create();
this->event_queue = event_queue_create();
this->send_queue = send_queue_create();
this->configuration_manager = configuration_manager_create();
this->sender = sender_create();
this->receiver = receiver_create();
this->scheduler = scheduler_create();
this->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 10:25:07 +00:00
static void cleanup(daemon_t *this)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
if (this->receiver != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->receiver->destroy(this->receiver);
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
if (this->scheduler != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->scheduler->destroy(this->scheduler);
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
if (this->sender != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->sender->destroy(this->sender);
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
if (this->thread_pool != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->thread_pool->destroy(this->thread_pool);
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
if (this->job_queue != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->job_queue->destroy(this->job_queue);
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
if (this->event_queue != NULL)
2005-11-17 11:24:03 +00:00
{
2005-11-29 10:25:07 +00:00
this->event_queue->destroy(this->event_queue);
2005-11-17 11:24:03 +00:00
}
2005-11-29 10:25:07 +00:00
if (this->send_queue != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->send_queue->destroy(this->send_queue);
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
if (this->socket != NULL)
2005-11-26 15:48:45 +00:00
{
2005-11-29 10:25:07 +00:00
this->socket->destroy(this->socket);
}
if (this->ike_sa_manager != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->ike_sa_manager->destroy(this->ike_sa_manager);
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
if (this->configuration_manager != NULL)
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
this->configuration_manager->destroy(this->configuration_manager);
2005-11-16 12:06:34 +00:00
}
2005-11-29 10:25:07 +00:00
this->logger_manager->destroy(this->logger_manager);
allocator_free(this);
2005-11-16 12:06:34 +00:00
}
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
*/
2005-11-29 10:25:07 +00:00
private_daemon_t *daemon_create()
2005-11-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
/* assign methods */
this->run = run;
this->public.destroy = (void (*) (daemon_t*,char*))destroy;
this->build_test_jobs = (void (*) (private_daemon_t*)) build_test_jobs;
this->initialize = (void (*) (private_daemon_t*))initialize;
this->cleanup = (void (*) (private_daemon_t*))cleanup;
/* first build a logger */
this->public.logger_manager = logger_manager_create(DEFAULT_LOGLEVEL);
this->logger = (this->public.logger_manager)->create_logger(this->public.logger_manager, DAEMON, NULL);
/* 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_manager = NULL;
this->public.sender= NULL;
this->public.receiver = NULL;
this->public.scheduler = NULL;
this->public.thread_pool = NULL;
/* setup signal handling */
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);
return this;
2005-11-16 12:06:34 +00:00
}
/**
2005-11-29 10:25:07 +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-16 12:06:34 +00:00
{
2005-11-29 10:25:07 +00:00
private_daemon_t *private_charon;
2005-11-29 10:25:07 +00:00
private_charon = daemon_create();
charon = (daemon_t*)private_charon;
private_charon->initialize(private_charon);
private_charon->build_test_jobs(private_charon);
private_charon->run(private_charon);
private_charon->cleanup(private_charon);
2005-11-16 12:22:57 +00:00
2005-11-03 14:50:02 +00:00
#ifdef LEAK_DETECTIVE
2005-11-16 12:06:34 +00:00
report_memory_leaks(void);
2005-11-03 14:50:02 +00:00
#endif
2005-11-16 12:06:34 +00:00
2005-11-29 10:25:07 +00:00
return 0;
2005-11-03 08:22:10 +00:00
}
2005-11-29 10:25:07 +00:00