aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2005-11-29 11:27:25 +0000
committerMartin Willi <martin@strongswan.org>2005-11-29 11:27:25 +0000
commite85220af90ac154e9badeece3aaaab30973f648f (patch)
treee2c0add009b46d5197216ba735fb513fcf5a03be
parent2326633550ee256c0dfda5dc2111811dc0877a58 (diff)
downloadstrongswan-e85220af90ac154e9badeece3aaaab30973f648f.tar.bz2
strongswan-e85220af90ac154e9badeece3aaaab30973f648f.tar.xz
- fixed kill behavior
-rw-r--r--Source/charon/daemon.c132
-rw-r--r--Source/charon/daemon.h6
-rw-r--r--Source/charon/network/socket.c6
-rw-r--r--Source/charon/testcases/testcases.c6
4 files changed, 88 insertions, 62 deletions
diff --git a/Source/charon/daemon.c b/Source/charon/daemon.c
index a32948fe9..7fe3d5b28 100644
--- a/Source/charon/daemon.c
+++ b/Source/charon/daemon.c
@@ -23,6 +23,7 @@
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
+#include <unistd.h>
#include "daemon.h"
@@ -56,22 +57,37 @@ struct private_daemon_t {
*/
sigset_t signal_set;
+ /**
+ * pid of main-thread
+ */
+ pid_t main_thread_pid;
+
+ /**
+ * main loop
+ */
void (*run) (private_daemon_t *this);
- void (*destroy) (private_daemon_t *this, char *reason);
+
+ /**
+ * a routine to add jobs for testing
+ */
void (*build_test_jobs) (private_daemon_t *this);
+
+ /**
+ * initializing daemon
+ */
void (*initialize) (private_daemon_t *this);
- void (*cleanup) (private_daemon_t *this);
+
+ /**
+ * destroy the daemon
+ */
+ void (*destroy) (private_daemon_t *this);
};
-
-
/**
* instance of the daemon
*/
daemon_t *charon;
-
-
/**
* Loop of the main thread, waits for signals
*/
@@ -115,96 +131,104 @@ static void run(private_daemon_t *this)
/**
* Initialize the destruction of the daemon
*/
-static void destroy(private_daemon_t *this, char *reason)
+static void kill_daemon(private_daemon_t *this, char *reason)
{
/* 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);
+ if (this->main_thread_pid == getpid())
+ {
+ /* initialization failed, terminate daemon */
+ this->destroy(this);
+ 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);
+ }
}
/**
* build some jobs to test daemon functionality
*/
-static void build_test_jobs(daemon_t *this)
+static void build_test_jobs(private_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);
+ this->public.job_queue->add(this->public.job_queue, (job_t*)initiate_job);
}
}
-
/**
* Initialize global objects and threads
*/
-static void initialize(daemon_t *this)
+static void initialize(private_daemon_t *this)
{
- 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->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.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);
+ this->public.sender = sender_create();
+ this->public.receiver = receiver_create();
+ this->public.scheduler = scheduler_create();
+ this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
}
/**
* Destory all initiated objects
*/
-static void cleanup(daemon_t *this)
+static void destroy(private_daemon_t *this)
{
- if (this->receiver != NULL)
+ if (this->public.receiver != NULL)
{
- this->receiver->destroy(this->receiver);
+ this->public.receiver->destroy(this->public.receiver);
}
- if (this->scheduler != NULL)
+ if (this->public.scheduler != NULL)
{
- this->scheduler->destroy(this->scheduler);
+ this->public.scheduler->destroy(this->public.scheduler);
}
- if (this->sender != NULL)
+ if (this->public.sender != NULL)
{
- this->sender->destroy(this->sender);
+ this->public.sender->destroy(this->public.sender);
}
- if (this->thread_pool != NULL)
+ if (this->public.thread_pool != NULL)
{
- this->thread_pool->destroy(this->thread_pool);
+ this->public.thread_pool->destroy(this->public.thread_pool);
}
- if (this->job_queue != NULL)
+ if (this->public.job_queue != NULL)
{
- this->job_queue->destroy(this->job_queue);
+ this->public.job_queue->destroy(this->public.job_queue);
}
- if (this->event_queue != NULL)
+ if (this->public.event_queue != NULL)
{
- this->event_queue->destroy(this->event_queue);
+ this->public.event_queue->destroy(this->public.event_queue);
}
- if (this->send_queue != NULL)
+ if (this->public.send_queue != NULL)
{
- this->send_queue->destroy(this->send_queue);
+ this->public.send_queue->destroy(this->public.send_queue);
}
- if (this->socket != NULL)
+ if (this->public.socket != NULL)
{
- this->socket->destroy(this->socket);
+ this->public.socket->destroy(this->public.socket);
}
- if (this->ike_sa_manager != NULL)
+ if (this->public.ike_sa_manager != NULL)
{
- this->ike_sa_manager->destroy(this->ike_sa_manager);
+ this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
}
- if (this->configuration_manager != NULL)
+ if (this->public.configuration_manager != NULL)
{
- this->configuration_manager->destroy(this->configuration_manager);
+ this->public.configuration_manager->destroy(this->public.configuration_manager);
}
- this->logger_manager->destroy(this->logger_manager);
+ this->public.logger_manager->destroy(this->public.logger_manager);
allocator_free(this);
}
@@ -221,10 +245,10 @@ private_daemon_t *daemon_create()
/* 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;
+ this->destroy = destroy;
+ this->build_test_jobs = build_test_jobs;
+ this->initialize = initialize;
+ this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
/* first build a logger */
this->public.logger_manager = logger_manager_create(DEFAULT_LOGLEVEL);
@@ -242,6 +266,8 @@ private_daemon_t *daemon_create()
this->public.scheduler = NULL;
this->public.thread_pool = NULL;
+ this->main_thread_pid = getpid();
+
/* setup signal handling */
sigemptyset(&(this->signal_set));
sigaddset(&(this->signal_set), SIGINT);
@@ -268,12 +294,12 @@ int main(int argc, char *argv[])
private_charon->run(private_charon);
- private_charon->cleanup(private_charon);
+ private_charon->destroy(private_charon);
#ifdef LEAK_DETECTIVE
report_memory_leaks(void);
#endif
- return 0;
+ exit(0);
}
diff --git a/Source/charon/daemon.h b/Source/charon/daemon.h
index 872c7fa27..651ed0370 100644
--- a/Source/charon/daemon.h
+++ b/Source/charon/daemon.h
@@ -48,7 +48,7 @@
* There are several other threads, this defines
* only the number of threads in thread_pool_t.
*/
-#define NUMBER_OF_WORKING_THREADS 1
+#define NUMBER_OF_WORKING_THREADS 4
/**
* Port on which the daemon will
@@ -121,10 +121,10 @@ struct daemon_t {
/**
* @brief shut down the daemon
*
- * @param this the daemon to kill
+ * @param this the daemon to kill
* @param reason describition why it will be killed
*/
- void (*destroy) (daemon_t *this, char *reason);
+ void (*kill) (daemon_t *this, char *reason);
};
/**
diff --git a/Source/charon/network/socket.c b/Source/charon/network/socket.c
index c1c66a08f..538d1ca3d 100644
--- a/Source/charon/network/socket.c
+++ b/Source/charon/network/socket.c
@@ -154,7 +154,7 @@ socket_t *socket_create(u_int16_t port)
this->public.destroy = (void(*)(socket_t*))destroy;
this->logger = charon->logger_manager->create_logger(charon->logger_manager, SOCKET, NULL);
-
+
/* create default ipv4 socket */
this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
if (this->socket_fd < 0)
@@ -162,7 +162,7 @@ socket_t *socket_create(u_int16_t port)
this->logger->log(this->logger, ERROR, "unable to open socket: %s", strerror(errno));
charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
allocator_free(this);
- return NULL;
+ charon->kill(charon, "socket could not be opened");
}
/* bind socket to all interfaces */
@@ -174,7 +174,7 @@ socket_t *socket_create(u_int16_t port)
this->logger->log(this->logger, ERROR, "unable to bind socket to port %d: %s", port, strerror(errno));
charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
allocator_free(this);
- return NULL;
+ charon->kill(charon, "socket could not be opened");
}
return (socket_t*)this;
diff --git a/Source/charon/testcases/testcases.c b/Source/charon/testcases/testcases.c
index 65af490c7..4bf978825 100644
--- a/Source/charon/testcases/testcases.c
+++ b/Source/charon/testcases/testcases.c
@@ -104,7 +104,7 @@ test_t hmac_signer_test2 = {test_hmac_sha1_signer, "HMAC SHA1 signer test"};
daemon_t* charon;
-static void daemon_destroy(daemon_t *this, char* none)
+static void daemon_kill(daemon_t *this, char* none)
{
this->logger_manager->destroy(this->logger_manager);
this->socket->destroy(this->socket);
@@ -126,7 +126,7 @@ daemon_t *daemon_create()
charon = allocator_alloc_thing(daemon_t);
/* assign methods */
- charon->destroy = daemon_destroy;
+ charon->kill = daemon_kill;
charon->logger_manager = logger_manager_create(0);
charon->socket = socket_create(4600);
@@ -207,7 +207,7 @@ int main()
tester->destroy(tester);
- charon->destroy(charon, NULL);
+ charon->kill(charon, NULL);
#ifdef LEAK_DETECTIVE
/* Leaks are reported on stderr */