/*
- * Copyright (C) 2006-2017 Tobias Brunner
+ * Copyright (C) 2006-2025 Tobias Brunner
* Copyright (C) 2005-2009 Martin Willi
* Copyright (C) 2006 Daniel Roethlisberger
* Copyright (C) 2005 Jan Hutter
mutex_t *mutex;
/**
- * Integrity check failed?
+ * Initialization (e.g. integrity check) failed?
*/
- bool integrity_failed;
+ bool init_failed;
/**
* Number of times we have been initialized
}
}
+#define MAX_LIBCHARON_INIT_FUNCTIONS 10
+
+/**
+ * Static array for init function registration using __attribute__((constructor))
+ */
+static library_init_t init_functions[MAX_LIBCHARON_INIT_FUNCTIONS];
+static int init_function_count;
+
+/**
+ * Described in header
+ */
+void libcharon_init_register(library_init_t init)
+{
+ if (init_function_count < MAX_LIBCHARON_INIT_FUNCTIONS - 1)
+ {
+ init_functions[init_function_count++] = init;
+ }
+ else
+ {
+ fprintf(stderr, "failed to register init function, please increase "
+ "MAX_LIBCHARON_INIT_FUNCTIONS");
+ }
+}
+
/**
* Types of supported loggers
*/
void libcharon_deinit()
{
private_daemon_t *this = (private_daemon_t*)charon;
+ int i;
if (!this || !ref_put(&this->ref))
{ /* have more users */
run_scripts(this, "stop");
+ for (i = 0; i < init_function_count; ++i)
+ {
+ init_functions[i](FALSE);
+ }
+
destroy(this);
charon = NULL;
}
bool libcharon_init()
{
private_daemon_t *this;
+ int i;
if (charon)
{ /* already initialized, increase refcount */
this = (private_daemon_t*)charon;
ref_get(&this->ref);
- return !this->integrity_failed;
+ return !this->init_failed;
}
this = daemon_create();
!lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
{
dbg(DBG_DMN, 1, "integrity check of libcharon failed");
- this->integrity_failed = TRUE;
+ this->init_failed = TRUE;
+ }
+
+ for (i = 0; i < init_function_count; ++i)
+ {
+ if (!init_functions[i](TRUE))
+ {
+ this->init_failed = TRUE;
+ }
}
- return !this->integrity_failed;
+ return !this->init_failed;
}
/*
- * Copyright (C) 2006-2017 Tobias Brunner
+ * Copyright (C) 2006-2025 Tobias Brunner
* Copyright (C) 2005-2009 Martin Willi
* Copyright (C) 2006 Daniel Roethlisberger
* Copyright (C) 2005 Jan Hutter
*/
void libcharon_deinit();
+/**
+ * Register a custom init function that's called at the end of libcharon_init()
+ * and the start of libcharon_deinit().
+ *
+ * To be called from __attribute__((constructor)) functions.
+ *
+ * @param init init function
+ */
+void libcharon_init_register(library_init_t init);
+
/**
* Register a custom logger constructor.
*