From: Tobias Brunner Date: Fri, 28 Mar 2025 12:49:56 +0000 (+0100) Subject: library: Add facility to register custom init/deinit functions X-Git-Tag: 6.0.2dr1~50 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a6f4146f45ad8fb968b852d6d6be044637d01890;p=thirdparty%2Fstrongswan.git library: Add facility to register custom init/deinit functions These can be linked into the application to do initialization/cleanup without having to modify the source code. --- diff --git a/src/libstrongswan/library.c b/src/libstrongswan/library.c index bfc55d4cc8..a102ae43c8 100644 --- a/src/libstrongswan/library.c +++ b/src/libstrongswan/library.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2018 Tobias Brunner + * Copyright (C) 2009-2025 Tobias Brunner * Copyright (C) 2008 Martin Willi * * Copyright (C) secunet Security Networks AG @@ -95,6 +95,30 @@ void library_add_namespace(char *ns) } } +#define MAX_LIBSTRONGSWAN_INIT_FUNCTIONS 10 + +/** + * Static array for init function registration using __attribute__((constructor)) + */ +static library_init_t init_functions[MAX_LIBSTRONGSWAN_INIT_FUNCTIONS]; +static int init_function_count; + +/** + * Described in header + */ +void library_init_register(library_init_t init) +{ + if (init_function_count < MAX_LIBSTRONGSWAN_INIT_FUNCTIONS - 1) + { + init_functions[init_function_count++] = init; + } + else + { + fprintf(stderr, "failed to register init function, please increase " + "MAX_LIBSTRONGSWAN_INIT_FUNCTIONS"); + } +} + /** * Register plugins if built statically */ @@ -149,6 +173,7 @@ void library_deinit() { private_library_t *this = (private_library_t*)lib; bool detailed; + int i; if (!this || !ref_put(&this->ref)) { /* have more users */ @@ -161,6 +186,11 @@ void library_deinit() /* make sure the cache is clear before unloading plugins */ lib->credmgr->flush_cache(lib->credmgr, CERT_ANY); + for (i = 0; i < init_function_count; ++i) + { + init_functions[i](FALSE); + } + key_exchange_deinit(); this->public.streams->destroy(this->public.streams); @@ -441,5 +471,12 @@ bool library_init(char *settings, const char *namespace) key_exchange_init(); + for (i = 0; i < init_function_count; ++i) + { + if (!init_functions[i](TRUE)) + { + this->init_failed = TRUE; + } + } return !this->init_failed; } diff --git a/src/libstrongswan/library.h b/src/libstrongswan/library.h index 20c30bdac8..078208342f 100644 --- a/src/libstrongswan/library.h +++ b/src/libstrongswan/library.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2018 Tobias Brunner + * Copyright (C) 2010-2025 Tobias Brunner * Copyright (C) 2008 Martin Willi * * Copyright (C) secunet Security Networks AG @@ -285,6 +285,24 @@ bool library_init(char *settings, const char *namespace); */ void library_deinit(); +/** + * Custom function called during init/deinit of a library. + * + * @param init TRUE during init, FALSE during deinit + * @return FALSE if an error occurred and init should fail + */ +typedef bool (*library_init_t)(bool init); + +/** + * Register a custom init function that's called at the end of library_init() + * and the start of library_deinit(). + * + * To be called from __attribute__((constructor)) functions. + * + * @param init init function + */ +void library_init_register(library_init_t init); + /** * Library instance, set after library_init() and before library_deinit() calls. */