daemon: Add facility to register custom init/deinit functions

Same as the previous commit but with access to the daemon.
This commit is contained in:
Tobias Brunner
2025-04-10 08:31:09 +02:00
parent a6f4146f45
commit b0a4b7f2dd
2 changed files with 56 additions and 7 deletions
+45 -6
View File
@@ -1,5 +1,5 @@
/*
* 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
@@ -106,9 +106,9 @@ struct private_daemon_t {
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
@@ -193,6 +193,30 @@ void register_custom_logger(char *name,
}
}
#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
*/
@@ -980,6 +1004,7 @@ private_daemon_t *daemon_create()
void libcharon_deinit()
{
private_daemon_t *this = (private_daemon_t*)charon;
int i;
if (!this || !ref_put(&this->ref))
{ /* have more users */
@@ -988,6 +1013,11 @@ void libcharon_deinit()
run_scripts(this, "stop");
for (i = 0; i < init_function_count; ++i)
{
init_functions[i](FALSE);
}
destroy(this);
charon = NULL;
}
@@ -998,12 +1028,13 @@ void libcharon_deinit()
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();
@@ -1019,7 +1050,15 @@ bool libcharon_init()
!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;
}
return !this->integrity_failed;
for (i = 0; i < init_function_count; ++i)
{
if (!init_functions[i](TRUE))
{
this->init_failed = TRUE;
}
}
return !this->init_failed;
}
+11 -1
View File
@@ -1,5 +1,5 @@
/*
* 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
@@ -391,6 +391,16 @@ bool libcharon_init();
*/
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.
*