]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
daemon: Add facility to register custom init/deinit functions
authorTobias Brunner <tobias@strongswan.org>
Fri, 28 Mar 2025 12:51:14 +0000 (13:51 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 10 Apr 2025 06:31:09 +0000 (08:31 +0200)
Same as the previous commit but with access to the daemon.

src/libcharon/daemon.c
src/libcharon/daemon.h

index 05819696ccbbea3d71c48dbb2d8ef9e5c572d7ca..282715ea84924d3d845611fef4663715e5943aec 100644 (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;
+       }
+
+       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;
 }
index 1809960d7f9cc3009ed600ae642e989dcb3e634b..71db76dd9cbad823db4522b3495d1a06b4b2c435 100644 (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.
  *