aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/bus
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2008-03-13 14:14:44 +0000
committerMartin Willi <martin@strongswan.org>2008-03-13 14:14:44 +0000
commit552cc11b1f017ce4962fca741f567d098f768574 (patch)
tree2835ae64c435191e04b5a265b1509c40a2e6766a /src/charon/bus
parent2df655134ca29f7a0b7d90ef4783f85eff1ddfd3 (diff)
downloadstrongswan-552cc11b1f017ce4962fca741f567d098f768574.tar.bz2
strongswan-552cc11b1f017ce4962fca741f567d098f768574.tar.xz
merged the modularization branch (credentials) back to trunk
Diffstat (limited to 'src/charon/bus')
-rw-r--r--src/charon/bus/bus.c61
-rw-r--r--src/charon/bus/bus.h66
-rw-r--r--src/charon/bus/listeners/file_logger.c9
-rw-r--r--src/charon/bus/listeners/file_logger.h35
-rw-r--r--src/charon/bus/listeners/sys_logger.c9
-rw-r--r--src/charon/bus/listeners/sys_logger.h35
6 files changed, 88 insertions, 127 deletions
diff --git a/src/charon/bus/bus.c b/src/charon/bus/bus.c
index e53ac43ce..5f813e781 100644
--- a/src/charon/bus/bus.c
+++ b/src/charon/bus/bus.c
@@ -1,10 +1,3 @@
-/**
- * @file bus.c
- *
- * @brief Implementation of bus_t.
- *
- */
-
/*
* Copyright (C) 2006 Martin Willi
* Hochschule fuer Technik Rapperswil
@@ -18,6 +11,8 @@
* 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.
+ *
+ * $Id$
*/
#include "bus.h"
@@ -25,6 +20,7 @@
#include <pthread.h>
#include <daemon.h>
+#include <utils/mutex.h>
ENUM(signal_names, SIG_ANY, SIG_MAX,
/** should not get printed */
@@ -72,9 +68,9 @@ struct private_bus_t {
linked_list_t *listeners;
/**
- * mutex to synchronize active listeners
+ * mutex to synchronize active listeners, recursively
*/
- pthread_mutex_t mutex;
+ mutex_t *mutex;
/**
* Thread local storage for a unique, simple thread ID
@@ -107,7 +103,7 @@ struct entry_t {
/**
* condvar where active listeners wait
*/
- pthread_cond_t cond;
+ condvar_t *condvar;
};
/**
@@ -119,12 +115,21 @@ static entry_t *entry_create(bus_listener_t *listener, bool blocker)
this->listener = listener;
this->blocker = blocker;
- pthread_cond_init(&this->cond, NULL);
+ this->condvar = condvar_create(CONDVAR_DEFAULT);
return this;
}
/**
+ * destroy an entry_t
+ */
+static void entry_destroy(entry_t *entry)
+{
+ entry->condvar->destroy(entry->condvar);
+ free(entry);
+}
+
+/**
* Get a unique thread number for a calling thread. Since
* pthread_self returns large and ugly numbers, use this function
* for logging; these numbers are incremental starting at 1
@@ -151,9 +156,9 @@ static int get_thread_number(private_bus_t *this)
*/
static void add_listener(private_bus_t *this, bus_listener_t *listener)
{
- pthread_mutex_lock(&this->mutex);
+ this->mutex->lock(this->mutex);
this->listeners->insert_last(this->listeners, entry_create(listener, FALSE));
- pthread_mutex_unlock(&this->mutex);
+ this->mutex->unlock(this->mutex);
}
/**
@@ -164,19 +169,19 @@ static void remove_listener(private_bus_t *this, bus_listener_t *listener)
iterator_t *iterator;
entry_t *entry;
- pthread_mutex_lock(&this->mutex);
+ this->mutex->lock(this->mutex);
iterator = this->listeners->create_iterator(this->listeners, TRUE);
while (iterator->iterate(iterator, (void**)&entry))
{
if (entry->listener == listener)
{
iterator->remove(iterator);
- free(entry);
+ entry_destroy(entry);
break;
}
}
iterator->destroy(iterator);
- pthread_mutex_unlock(&this->mutex);
+ this->mutex->unlock(this->mutex);
}
typedef struct cleanup_data_t cleanup_data_t;
@@ -205,7 +210,7 @@ static void listener_cleanup(cleanup_data_t *data)
if (entry == data->entry)
{
iterator->remove(iterator);
- free(entry);
+ entry_destroy(entry);
break;
}
}
@@ -223,21 +228,21 @@ static void listen_(private_bus_t *this, bus_listener_t *listener, job_t *job)
data.this = this;
data.entry = entry_create(listener, TRUE);
- pthread_mutex_lock(&this->mutex);
+ this->mutex->lock(this->mutex);
this->listeners->insert_last(this->listeners, data.entry);
charon->processor->queue_job(charon->processor, job);
- pthread_cleanup_push((void*)pthread_mutex_unlock, &this->mutex);
+ pthread_cleanup_push((void*)this->mutex->unlock, this->mutex);
pthread_cleanup_push((void*)listener_cleanup, &data);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
while (data.entry->blocker)
{
- pthread_cond_wait(&data.entry->cond, &this->mutex);
+ data.entry->condvar->wait(data.entry->condvar, this->mutex);
}
pthread_setcancelstate(old, NULL);
pthread_cleanup_pop(FALSE);
/* unlock mutex */
pthread_cleanup_pop(TRUE);
- free(data.entry);
+ entry_destroy(data.entry);
}
/**
@@ -248,6 +253,7 @@ static void set_sa(private_bus_t *this, ike_sa_t *ike_sa)
pthread_setspecific(this->thread_sa, ike_sa);
}
+
/**
* Implementation of bus_t.vsignal.
*/
@@ -259,7 +265,7 @@ static void vsignal(private_bus_t *this, signal_t signal, level_t level,
ike_sa_t *ike_sa;
long thread;
- pthread_mutex_lock(&this->mutex);
+ this->mutex->lock(this->mutex);
ike_sa = pthread_getspecific(this->thread_sa);
thread = get_thread_number(this);
@@ -275,18 +281,18 @@ static void vsignal(private_bus_t *this, signal_t signal, level_t level,
if (entry->blocker)
{
entry->blocker = FALSE;
- pthread_cond_signal(&entry->cond);
+ entry->condvar->signal(entry->condvar);
}
else
{
- free(entry);
+ entry_destroy(entry);
}
}
va_end(args_copy);
}
iterator->destroy(iterator);
- pthread_mutex_unlock(&this->mutex);
+ this->mutex->unlock(this->mutex);
}
/**
@@ -307,7 +313,8 @@ static void signal_(private_bus_t *this, signal_t signal, level_t level,
*/
static void destroy(private_bus_t *this)
{
- this->listeners->destroy_function(this->listeners, free);
+ this->mutex->destroy(this->mutex);
+ this->listeners->destroy_function(this->listeners, (void*)entry_destroy);
free(this);
}
@@ -327,7 +334,7 @@ bus_t *bus_create()
this->public.destroy = (void(*)(bus_t*)) destroy;
this->listeners = linked_list_create();
- pthread_mutex_init(&this->mutex, NULL);
+ this->mutex = mutex_create(MUTEX_DEFAULT);
pthread_key_create(&this->thread_id, NULL);
pthread_key_create(&this->thread_sa, NULL);
diff --git a/src/charon/bus/bus.h b/src/charon/bus/bus.h
index f71018444..678bf37d4 100644
--- a/src/charon/bus/bus.h
+++ b/src/charon/bus/bus.h
@@ -1,10 +1,3 @@
-/**
- * @file bus.h
- *
- * @brief Interface of bus_t.
- *
- */
-
/*
* Copyright (C) 2006 Martin Willi
* Hochschule fuer Technik Rapperswil
@@ -18,6 +11,13 @@
* 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.
+ *
+ * $Id$
+ */
+
+/**
+ * @defgroup bus bus
+ * @{ @ingroup charon
*/
#ifndef BUS_H_
@@ -36,7 +36,7 @@ typedef struct bus_t bus_t;
/**
- * @brief signals emitted by the daemon.
+ * signals emitted by the daemon.
*
* Signaling is for different purporses. First, it allows debugging via
* "debugging signal messages", sencondly, it allows to follow certain
@@ -52,8 +52,6 @@ typedef struct bus_t bus_t;
* Debug signal betwee a START and a SUCCESS/FAILED belongs to that operation
* if the IKE_SA is the same. The thread may change, as multiple threads
* may be involved in a complex scenario.
- *
- * @ingroup bus
*/
enum signal_t {
/** pseudo signal, representing any other signal */
@@ -157,7 +155,7 @@ enum level_t {
#if DEBUG_LEVEL >= 1
/**
- * @brief Log a debug message via the signal bus.
+ * Log a debug message via the signal bus.
*
* @param signal signal_t signal description
* @param format printf() style format string
@@ -189,7 +187,7 @@ enum level_t {
#endif /* DBG4 */
/**
- * @brief Raise a signal for an occured event.
+ * Raise a signal for an occured event.
*
* @param sig signal_t signal description
* @param format printf() style format string
@@ -198,7 +196,7 @@ enum level_t {
#define SIG(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_0, format, ##__VA_ARGS__)
/**
- * @brief Get the type of a signal.
+ * Get the type of a signal.
*
* A signal may be a debugging signal with a specific context. They have
* a level specific for their context > 0. All audit signals use the
@@ -211,17 +209,15 @@ enum level_t {
/**
- * @brief Interface for registering at the signal bus.
+ * Interface for registering at the signal bus.
*
* To receive signals from the bus, the client implementing the
* bus_listener_t interface registers itself at the signal bus.
- *
- * @ingroup bus
*/
struct bus_listener_t {
/**
- * @brief Send a signal to a bus listener.
+ * Send a signal to a bus listener.
*
* A numerical identification for the thread is included, as the
* associated IKE_SA, if any. Signal specifies the type of
@@ -231,8 +227,10 @@ struct bus_listener_t {
* a "..." parameters to functions is not (cleanly) possible.
* The implementing signal function returns TRUE to stay registered
* to the bus, or FALSE to unregister itself.
+ * You should not call bus_t.signal() inside of a registered listener,
+ * as it WILL call itself recursively. If you do so, make shure to
+ * avoid infinite recursion. Watch your stack!
*
- * @param this listener
* @param singal kind of the signal (up, down, rekeyed, ...)
* @param level verbosity level of the signal
* @param thread ID of the thread raised this signal
@@ -246,40 +244,36 @@ struct bus_listener_t {
};
/**
- * @brief Signal bus which sends signals to registered listeners.
+ * Signal bus which sends signals to registered listeners.
*
* The signal bus is not much more than a multiplexer. A listener interested
* in receiving event signals registers at the bus. Any signals sent to
* are delivered to all registered listeners.
* To deliver signals to threads, the blocking listen() call may be used
* to wait for a signal.
- *
- * @ingroup bus
*/
struct bus_t {
/**
- * @brief Register a listener to the bus.
+ * Register a listener to the bus.
*
* A registered listener receives all signals which are sent to the bus.
* The listener is passive; the thread which emitted the signal
* processes the listener routine.
*
- * @param this bus
* @param listener listener to register.
*/
void (*add_listener) (bus_t *this, bus_listener_t *listener);
/**
- * @brief Unregister a listener from the bus.
+ * Unregister a listener from the bus.
*
- * @param this bus
* @param listener listener to unregister.
*/
void (*remove_listener) (bus_t *this, bus_listener_t *listener);
/**
- * @brief Register a listener and block the calling thread.
+ * Register a listener and block the calling thread.
*
* This call registers a listener and blocks the calling thread until
* its listeners function returns FALSE. This allows to wait for certain
@@ -287,14 +281,13 @@ struct bus_t {
* registered, this allows to listen on events we initiate with the job
* without missing any signals.
*
- * @param this bus
* @param listener listener to register
* @param job job to execute asynchronously when registered, or NULL
*/
void (*listen)(bus_t *this, bus_listener_t *listener, job_t *job);
/**
- * @brief Set the IKE_SA the calling thread is using.
+ * Set the IKE_SA the calling thread is using.
*
* To associate an received signal to an IKE_SA without passing it as
* parameter each time, the thread registers it's used IKE_SA each
@@ -302,13 +295,12 @@ struct bus_t {
* the IKE_SA (by passing NULL). This IKE_SA is stored per-thread, so each
* thread has one IKE_SA registered (or not).
*
- * @param this bus
* @param ike_sa ike_sa to register, or NULL to unregister
*/
void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
/**
- * @brief Send a signal to the bus.
+ * Send a signal to the bus.
*
* The signal specifies the type of the event occured. The format string
* specifies an additional informational or error message with a
@@ -316,7 +308,6 @@ struct bus_t {
* Some useful macros are available to shorten this call.
* @see SIG(), DBG1()
*
- * @param this bus
* @param singal kind of the signal (up, down, rekeyed, ...)
* @param level verbosity level of the signal
* @param format printf() style format string
@@ -325,7 +316,7 @@ struct bus_t {
void (*signal) (bus_t *this, signal_t signal, level_t level, char* format, ...);
/**
- * @brief Send a signal to the bus using va_list arguments.
+ * Send a signal to the bus using va_list arguments.
*
* Same as bus_t.signal(), but uses va_list argument list.
*
@@ -333,7 +324,6 @@ struct bus_t {
* called extensively and therefore shouldn't allocate heap memory or
* do other expensive tasks!
*
- * @param this bus
* @param singal kind of the signal (up, down, rekeyed, ...)
* @param level verbosity level of the signal
* @param format printf() style format string
@@ -342,20 +332,16 @@ struct bus_t {
void (*vsignal) (bus_t *this, signal_t signal, level_t level, char* format, va_list args);
/**
- * @brief Destroy the signal bus.
- *
- * @param this bus to destroy
+ * Destroy the signal bus.
*/
void (*destroy) (bus_t *this);
};
/**
- * @brief Create the signal bus which multiplexes signals to its listeners.
+ * Create the signal bus which multiplexes signals to its listeners.
*
* @return signal bus instance
- *
- * @ingroup bus
*/
bus_t *bus_create();
-#endif /* BUS_H_ */
+#endif /* BUS_H_ @} */
diff --git a/src/charon/bus/listeners/file_logger.c b/src/charon/bus/listeners/file_logger.c
index 14f9f72cf..1a31e316e 100644
--- a/src/charon/bus/listeners/file_logger.c
+++ b/src/charon/bus/listeners/file_logger.c
@@ -1,10 +1,3 @@
-/**
- * @file file_logger.c
- *
- * @brief Implementation of file_logger_t.
- *
- */
-
/*
* Copyright (C) 2006 Martin Willi
* Hochschule fuer Technik Rapperswil
@@ -18,6 +11,8 @@
* 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.
+ *
+ * $Id$
*/
#include <stdio.h>
diff --git a/src/charon/bus/listeners/file_logger.h b/src/charon/bus/listeners/file_logger.h
index d67daba25..6b716c6a7 100644
--- a/src/charon/bus/listeners/file_logger.h
+++ b/src/charon/bus/listeners/file_logger.h
@@ -1,10 +1,3 @@
-/**
- * @file file_logger.h
- *
- * @brief Interface of file_logger_t.
- *
- */
-
/*
* Copyright (C) 2006 Martin Willi
* Hochschule fuer Technik Rapperswil
@@ -18,6 +11,13 @@
* 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.
+ *
+ * $Id$
+ */
+
+/**
+ * @defgroup file_logger file_logger
+ * @{ @ingroup listeners
*/
#ifndef FILE_LOGGER_H_
@@ -28,12 +28,7 @@ typedef struct file_logger_t file_logger_t;
#include <bus/bus.h>
/**
- * @brief Logger to files which implements bus_listener_t.
- *
- * @b Constructors:
- * - file_logger_create()
- *
- * @ingroup listeners
+ * Logger to files which implements bus_listener_t.
*/
struct file_logger_t {
@@ -43,31 +38,25 @@ struct file_logger_t {
bus_listener_t listener;
/**
- * @brief Set the loglevel for a signal type.
+ * Set the loglevel for a signal type.
*
- * @param this stream_logger_t object
* @param singal type of signal
* @param level max level to log (0..4)
*/
void (*set_level) (file_logger_t *this, signal_t signal, level_t level);
/**
- * @brief Destroys a file_logger_t object.
- *
- * @param this file_logger_t object
+ * Destroys a file_logger_t object.
*/
void (*destroy) (file_logger_t *this);
};
/**
- * @brief Constructor to create a file_logger_t object.
+ * Constructor to create a file_logger_t object.
*
* @param out FILE to write to
* @return file_logger_t object
- *
- * @ingroup listeners
*/
file_logger_t *file_logger_create(FILE *out);
-
-#endif /* FILE_LOGGER_H_ */
+#endif /* FILE_LOGGER_H_ @} */
diff --git a/src/charon/bus/listeners/sys_logger.c b/src/charon/bus/listeners/sys_logger.c
index d26d14dc0..876fab8fd 100644
--- a/src/charon/bus/listeners/sys_logger.c
+++ b/src/charon/bus/listeners/sys_logger.c
@@ -1,10 +1,3 @@
-/**
- * @file sys_logger.c
- *
- * @brief Implementation of sys_logger_t.
- *
- */
-
/*
* Copyright (C) 2006 Martin Willi
* Hochschule fuer Technik Rapperswil
@@ -18,6 +11,8 @@
* 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.
+ *
+ * $Id$
*/
#include <stdio.h>
diff --git a/src/charon/bus/listeners/sys_logger.h b/src/charon/bus/listeners/sys_logger.h
index 091217313..1a04c2ad9 100644
--- a/src/charon/bus/listeners/sys_logger.h
+++ b/src/charon/bus/listeners/sys_logger.h
@@ -1,10 +1,3 @@
-/**
- * @file sys_logger.h
- *
- * @brief Interface of sys_logger_t.
- *
- */
-
/*
* Copyright (C) 2006 Martin Willi
* Hochschule fuer Technik Rapperswil
@@ -18,6 +11,13 @@
* 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.
+ *
+ * $Id$
+ */
+
+/**
+ * @defgroup sys_logger sys_logger
+ * @{ @ingroup listeners
*/
#ifndef SYS_LOGGER_H_
@@ -30,12 +30,7 @@ typedef struct sys_logger_t sys_logger_t;
#include <bus/bus.h>
/**
- * @brief Logger for syslog which implements bus_listener_t.
- *
- * @b Constructors:
- * - sys_logger_create()
- *
- * @ingroup listeners
+ * Logger for syslog which implements bus_listener_t.
*/
struct sys_logger_t {
@@ -45,31 +40,25 @@ struct sys_logger_t {
bus_listener_t listener;
/**
- * @brief Set the loglevel for a signal type.
+ * Set the loglevel for a signal type.
*
- * @param this stream_logger_t object
* @param singal type of signal
* @param level max level to log
*/
void (*set_level) (sys_logger_t *this, signal_t signal, level_t level);
/**
- * @brief Destroys a sys_logger_t object.
- *
- * @param this sys_logger_t object
+ * Destroys a sys_logger_t object.
*/
void (*destroy) (sys_logger_t *this);
};
/**
- * @brief Constructor to create a sys_logger_t object.
+ * Constructor to create a sys_logger_t object.
*
* @param facility syslog facility to use
* @return sys_logger_t object
- *
- * @ingroup listeners
*/
sys_logger_t *sys_logger_create(int facility);
-
-#endif /* SYS_LOGGER_H_ */
+#endif /* SYS_LOGGER_H_ @} */