* Mutex for configured loggers
*/
mutex_t *mutex;
+
+ /**
+ * Integrity check failed?
+ */
+ bool integrity_failed;
+
+ /**
+ * Number of times we have been initialized
+ */
+ refcount_t ref;
};
/**
},
.loggers = linked_list_create(),
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
+ .ref = 1,
);
charon = &this->public;
this->public.caps = capabilities_create();
*/
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;
}
*/
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());
!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;
}
* 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
*/