diff options
author | Martin Willi <martin@revosec.ch> | 2012-10-25 14:50:30 +0200 |
---|---|---|
committer | Martin Willi <martin@revosec.ch> | 2012-11-14 10:14:37 +0100 |
commit | de4c1def83431a38395511f7e006218b3112b4b1 (patch) | |
tree | c984f37fdc9d84934eff45be3186ebceacbdab36 | |
parent | 8edb6248f817ca6864faaa2d4f10e52c9f22852f (diff) | |
download | strongswan-de4c1def83431a38395511f7e006218b3112b4b1.tar.bz2 strongswan-de4c1def83431a38395511f7e006218b3112b4b1.tar.xz |
libcharon can be initialized more than once
-rw-r--r-- | src/libcharon/daemon.c | 36 | ||||
-rw-r--r-- | src/libcharon/daemon.h | 3 |
2 files changed, 34 insertions, 5 deletions
diff --git a/src/libcharon/daemon.c b/src/libcharon/daemon.c index 9ab857fe8..b27e1776a 100644 --- a/src/libcharon/daemon.c +++ b/src/libcharon/daemon.c @@ -71,6 +71,16 @@ struct private_daemon_t { * Mutex for configured loggers */ mutex_t *mutex; + + /** + * Integrity check failed? + */ + bool integrity_failed; + + /** + * Number of times we have been initialized + */ + refcount_t ref; }; /** @@ -570,6 +580,7 @@ private_daemon_t *daemon_create(const char *name) }, .loggers = linked_list_create(), .mutex = mutex_create(MUTEX_TYPE_DEFAULT), + .ref = 1, ); charon = &this->public; this->public.caps = capabilities_create(); @@ -592,7 +603,14 @@ private_daemon_t *daemon_create(const char *name) */ void libcharon_deinit() { - destroy((private_daemon_t*)charon); + private_daemon_t *this = (private_daemon_t*)charon; + + if (!this || !ref_put(&this->ref)) + { /* have more users */ + return; + } + + destroy(this); charon = NULL; } @@ -601,7 +619,16 @@ void libcharon_deinit() */ bool libcharon_init(const char *name) { - daemon_create(name); + private_daemon_t *this; + + if (charon) + { /* already initialized, increase refcount */ + this = (private_daemon_t*)charon; + ref_get(&this->ref); + return !this->integrity_failed; + } + + this = daemon_create(name); /* for uncritical pseudo random numbers */ srandom(time(NULL) + getpid()); @@ -619,8 +646,7 @@ bool libcharon_init(const char *name) !lib->integrity->check(lib->integrity, "libcharon", libcharon_init)) { dbg(DBG_DMN, 1, "integrity check of libcharon failed"); - return FALSE; + this->integrity_failed = TRUE; } - - return TRUE; + return !this->integrity_failed; } diff --git a/src/libcharon/daemon.h b/src/libcharon/daemon.h index 06aa4ab72..2926d945b 100644 --- a/src/libcharon/daemon.h +++ b/src/libcharon/daemon.h @@ -329,6 +329,9 @@ extern daemon_t *charon; * This function initializes the bus, listeners can be registered before * calling initialize(). * + * libcharon_init() may be called multiple times in a single process, but each + * caller should call libcharon_deinit() for each call to libcharon_init(). + * * @param name name of the binary that uses the library * @return FALSE if integrity check failed */ |