]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
ipset-pf-support, move startup and destartup to the front of the module 1098/head
authorW.C.A. Wijngaards <wouter@nlnetlabs.nl>
Wed, 3 Jul 2024 11:53:44 +0000 (13:53 +0200)
committerW.C.A. Wijngaards <wouter@nlnetlabs.nl>
Wed, 3 Jul 2024 11:53:44 +0000 (13:53 +0200)
func block functions, modstack call deinit function names, and detect
module change when no startup functions are needed.

18 files changed:
cachedb/cachedb.c
daemon/daemon.c
daemon/daemon.h
dns64/dns64.c
dynlibmod/dynlibmod.c
edns-subnet/subnetmod.c
ipsecmod/ipsecmod.c
ipset/ipset.c
iterator/iterator.c
libunbound/context.c
libunbound/libunbound.c
pythonmod/pythonmod.c
respip/respip.c
services/modstack.c
services/modstack.h
testcode/unitzonemd.c
util/module.h
validator/validator.c

index aca68d7ed73fd2359f53083b1627fef16543a287..7a07b997696797efc3646f9ee95839d0b6d08877 100644 (file)
@@ -983,7 +983,7 @@ cachedb_get_mem(struct module_env* env, int id)
  */
 static struct module_func_block cachedb_block = {
        "cachedb",
-       &cachedb_init, &cachedb_deinit, NULL, NULL, &cachedb_operate,
+       NULL, NULL, &cachedb_init, &cachedb_deinit, &cachedb_operate,
        &cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
 };
 
index ab3d182c312113bec774790cb89e530c57291b28..d81bec84441114c2c10b5ac4e17725924610bd2e 100644 (file)
@@ -450,7 +450,7 @@ daemon_privileged(struct daemon* daemon)
        daemon->env->cfg = daemon->cfg;
        daemon->env->alloc = &daemon->superalloc;
        daemon->env->worker = NULL;
-       if(!modstack_startup(&daemon->mods, daemon->cfg->module_conf,
+       if(!modstack_call_startup(&daemon->mods, daemon->cfg->module_conf,
                daemon->env)) {
                fatal_exit("failed to startup modules");
        }
@@ -466,11 +466,15 @@ static void daemon_setup_modules(struct daemon* daemon)
        daemon->env->cfg = daemon->cfg;
        daemon->env->alloc = &daemon->superalloc;
        daemon->env->worker = NULL;
+       if(daemon->mods_inited) {
+               modstack_call_deinit(&daemon->mods, daemon->env);
+       }
        daemon->env->need_to_validate = 0; /* set by module init below */
-       if(!modstack_setup(&daemon->mods, daemon->cfg->module_conf,
+       if(!modstack_call_init(&daemon->mods, daemon->cfg->module_conf,
                daemon->env)) {
-               fatal_exit("failed to setup modules");
+               fatal_exit("failed to init modules");
        }
+       daemon->mods_inited = 1;
        log_edns_known_options(VERB_ALGO, daemon->env);
 }
 
@@ -904,8 +908,9 @@ daemon_delete(struct daemon* daemon)
        size_t i;
        if(!daemon)
                return;
-       modstack_desetup(&daemon->mods, daemon->env);
-       modstack_destartup(&daemon->mods, daemon->env);
+       modstack_call_deinit(&daemon->mods, daemon->env);
+       modstack_call_destartup(&daemon->mods, daemon->env);
+       modstack_free(&daemon->mods);
        daemon_remote_delete(daemon->rc);
        for(i = 0; i < daemon->num_ports; i++)
                listening_ports_free(daemon->ports[i]);
index 200149745c1c2426e8735aa8e02c0982982d1f72..a6b6391cc48b6d3409d494a47d3a901155b3c34e 100644 (file)
@@ -115,6 +115,8 @@ struct daemon {
        struct module_env* env;
        /** stack of module callbacks */
        struct module_stack mods;
+       /** The module stack has been inited */
+       int mods_inited;
        /** access control, which client IPs are allowed to connect */
        struct acl_list* acl;
        /** access control, which interfaces are allowed to connect */
index d34eafb9f73f783686e23edfcc959181a59e83c7..3a43698a8dcdfa626a35bb56aa575b322105d21f 100644 (file)
@@ -1044,7 +1044,7 @@ dns64_get_mem(struct module_env* env, int id)
  */
 static struct module_func_block dns64_block = {
        "dns64",
-       &dns64_init, &dns64_deinit, NULL, NULL, &dns64_operate,
+       NULL, NULL, &dns64_init, &dns64_deinit, &dns64_operate,
        &dns64_inform_super, &dns64_clear, &dns64_get_mem
 };
 
index 06c4f08c18215b9accb63b779fe6db09d9bf45be..c941154927ee7cc6815f8c9898ec72d354b993a2 100644 (file)
@@ -297,7 +297,7 @@ inplace_cb_delete_wrapped(struct module_env* env, enum inplace_cb_list_type type
  */
 static struct module_func_block dynlibmod_block = {
    "dynlib",
-   &dynlibmod_init, &dynlibmod_deinit, NULL, NULL, &dynlibmod_operate,
+   NULL, NULL, &dynlibmod_init, &dynlibmod_deinit, &dynlibmod_operate,
    &dynlibmod_inform_super, &dynlibmod_clear, &dynlibmod_get_mem
 };
 
index 8e6db02e7f2a9e71a13ed3b9eac87d4a502781e1..ead720f340063d655e2e28e642bb97f6dba02557 100644 (file)
@@ -996,7 +996,7 @@ subnetmod_get_mem(struct module_env *env, int id)
  */
 static struct module_func_block subnetmod_block = {
        "subnetcache",
-       &subnetmod_init, &subnetmod_deinit, NULL, NULL, &subnetmod_operate,
+       NULL, NULL, &subnetmod_init, &subnetmod_deinit, &subnetmod_operate,
        &subnetmod_inform_super, &subnetmod_clear, &subnetmod_get_mem
 };
 
index 137e22c33752617b569861bae8b526495bef83da..76f9b1965e0878d2383142c1c9eb258cdd55b488 100644 (file)
@@ -615,7 +615,7 @@ ipsecmod_get_mem(struct module_env* env, int id)
  */
 static struct module_func_block ipsecmod_block = {
        "ipsecmod",
-       &ipsecmod_init, &ipsecmod_deinit, NULL, NULL, &ipsecmod_operate,
+       NULL, NULL, &ipsecmod_init, &ipsecmod_deinit, &ipsecmod_operate,
        &ipsecmod_inform_super, &ipsecmod_clear, &ipsecmod_get_mem
 };
 
index d416f34fd806b7fa97523ddc0fdb9bf2543a3769..1ad2c09f46daa4c516cb2b2434ce7dd96d9adaec 100644 (file)
@@ -491,7 +491,7 @@ size_t ipset_get_mem(struct module_env *env, int id) {
  */
 static struct module_func_block ipset_block = {
        "ipset",
-       &ipset_init, &ipset_deinit, &ipset_startup, &ipset_destartup,
+       &ipset_startup, &ipset_destartup, &ipset_init, &ipset_deinit,
        &ipset_operate, &ipset_inform_super, &ipset_clear, &ipset_get_mem
 };
 
index c99d9504aa966808b4ba5d67ca47b0b56a9a66f5..cddb02717a0a59a2906b413e190cb4a90af2a572 100644 (file)
@@ -4489,7 +4489,7 @@ iter_get_mem(struct module_env* env, int id)
  */
 static struct module_func_block iter_block = {
        "iterator",
-       &iter_init, &iter_deinit, NULL, NULL, &iter_operate,
+       NULL, NULL, &iter_init, &iter_deinit, &iter_operate,
        &iter_inform_super, &iter_clear, &iter_get_mem
 };
 
index 5b2d36b2d318cabe3847f8d90c5468efc07856c8..05f57987a40435571c061d05784588ffc8692ebd 100644 (file)
@@ -75,9 +75,9 @@ context_finalize(struct ub_ctx* ctx)
        ctx->pipe_pid = getpid();
        cfg_apply_local_port_policy(cfg, 65536);
        config_apply(cfg);
-       if(!modstack_startup(&ctx->mods, cfg->module_conf, ctx->env))
+       if(!modstack_call_startup(&ctx->mods, cfg->module_conf, ctx->env))
                return UB_INITFAIL;
-       if(!modstack_setup(&ctx->mods, cfg->module_conf, ctx->env))
+       if(!modstack_call_init(&ctx->mods, cfg->module_conf, ctx->env))
                return UB_INITFAIL;
        listen_setup_locks();
        log_edns_known_options(VERB_ALGO, ctx->env);
index 561e41fba8d1f9b1dca9cf2d2fe2b4025cfc5d2b..9c6a3e309717076e3f895e2336c48f818d816d30 100644 (file)
@@ -188,8 +188,9 @@ ub_ctx_create(void)
                int e = errno;
                ub_randfree(ctx->seed_rnd);
                config_delete(ctx->env->cfg);
-               modstack_desetup(&ctx->mods, ctx->env);
-               modstack_destartup(&ctx->mods, ctx->env);
+               modstack_call_deinit(&ctx->mods, ctx->env);
+               modstack_call_destartup(&ctx->mods, ctx->env);
+               modstack_free(&ctx->mods);
                listen_desetup_locks();
                edns_known_options_delete(ctx->env);
                edns_strings_delete(ctx->env->edns_strings);
@@ -203,8 +204,9 @@ ub_ctx_create(void)
                tube_delete(ctx->qq_pipe);
                ub_randfree(ctx->seed_rnd);
                config_delete(ctx->env->cfg);
-               modstack_desetup(&ctx->mods, ctx->env);
-               modstack_destartup(&ctx->mods, ctx->env);
+               modstack_call_deinit(&ctx->mods, ctx->env);
+               modstack_call_destartup(&ctx->mods, ctx->env);
+               modstack_free(&ctx->mods);
                listen_desetup_locks();
                edns_known_options_delete(ctx->env);
                edns_strings_delete(ctx->env->edns_strings);
@@ -362,8 +364,9 @@ ub_ctx_delete(struct ub_ctx* ctx)
        }
        libworker_delete_event(ctx->event_worker);
 
-       modstack_desetup(&ctx->mods, ctx->env);
-       modstack_destartup(&ctx->mods, ctx->env);
+       modstack_call_deinit(&ctx->mods, ctx->env);
+       modstack_call_destartup(&ctx->mods, ctx->env);
+       modstack_free(&ctx->mods);
        a = ctx->alloc_list;
        while(a) {
                na = a->super;
index f397012ac1c2ae4d71e1d378eb540676f1bfd14d..e231ad0791724aa9199ae612a74b03ebb8c53b95 100644 (file)
@@ -777,7 +777,7 @@ size_t pythonmod_get_mem(struct module_env* env, int id)
  */
 static struct module_func_block pythonmod_block = {
    "python",
-   &pythonmod_init, &pythonmod_deinit, NULL, NULL, &pythonmod_operate,
+   NULL, NULL, &pythonmod_init, &pythonmod_deinit, &pythonmod_operate,
    &pythonmod_inform_super, &pythonmod_clear, &pythonmod_get_mem
 };
 
index cfc6a4908697d7760555a480993d9b2bfb544c32..db48f176e460cfd638b70b7eadb718469e3db876 100644 (file)
@@ -1259,7 +1259,7 @@ respip_get_mem(struct module_env* env, int id)
  */
 static struct module_func_block respip_block = {
        "respip",
-       &respip_init, &respip_deinit, NULL, NULL, &respip_operate,
+       NULL, NULL, &respip_init, &respip_deinit, &respip_operate,
        &respip_inform_super, &respip_clear, &respip_get_mem
 };
 
index f32f942c8fc3115b7fbd1d5f224f358b0be53fe7..6c8af0505b69fd70d45b8bbc7d378c96c4efceae 100644 (file)
@@ -95,6 +95,16 @@ modstack_init(struct module_stack* stack)
        stack->mod = NULL;
 }
 
+void
+modstack_free(struct module_stack* stack)
+{
+       if(!stack)
+               return;
+        stack->num = 0;
+        free(stack->mod);
+        stack->mod = NULL;
+}
+
 int
 modstack_config(struct module_stack* stack, const char* module_conf)
 {
@@ -223,7 +233,7 @@ module_func_block* module_factory(const char** str)
 }
 
 int
-modstack_startup(struct module_stack* stack, const char* module_conf,
+modstack_call_startup(struct module_stack* stack, const char* module_conf,
        struct module_env* env)
 {
         int i;
@@ -249,22 +259,33 @@ modstack_startup(struct module_stack* stack, const char* module_conf,
 }
 
 int
-modstack_setup(struct module_stack* stack, const char* module_conf,
+modstack_call_init(struct module_stack* stack, const char* module_conf,
        struct module_env* env)
 {
-        int i;
-       if(stack->num != 0)
-               modstack_desetup(stack, env);
+        int i, changed = 0;
         env->need_to_validate = 0; /* set by module init below */
         for(i=0; i<stack->num; i++) {
                while(*module_conf && isspace(*module_conf))
                        module_conf++;
                 if(strncmp(stack->mod[i]->name, module_conf,
                        strlen(stack->mod[i]->name))) {
-                       log_err("changed module ordering during reload not supported");
-                       return 0;
+                       if(stack->mod[i]->startup || stack->mod[i]->destartup) {
+                               log_err("changed module ordering during reload not supported, for module that needs startup");
+                               return 0;
+                       } else {
+                               changed = 1;
+                       }
                }
                module_conf += strlen(stack->mod[i]->name);
+       }
+       if(changed) {
+               modstack_free(stack);
+               if(!modstack_config(stack, module_conf)) {
+                       return 0;
+               }
+       }
+
+        for(i=0; i<stack->num; i++) {
                 verbose(VERB_OPS, "init module %d: %s",
                         i, stack->mod[i]->name);
                 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
@@ -278,7 +299,7 @@ modstack_setup(struct module_stack* stack, const char* module_conf,
 }
 
 void
-modstack_desetup(struct module_stack* stack, struct module_env* env)
+modstack_call_deinit(struct module_stack* stack, struct module_env* env)
 {
         int i;
         for(i=0; i<stack->num; i++) {
@@ -288,7 +309,7 @@ modstack_desetup(struct module_stack* stack, struct module_env* env)
 }
 
 void
-modstack_destartup(struct module_stack* stack, struct module_env* env)
+modstack_call_destartup(struct module_stack* stack, struct module_env* env)
 {
         int i;
         for(i=0; i<stack->num; i++) {
@@ -297,9 +318,6 @@ modstack_destartup(struct module_stack* stack, struct module_env* env)
                 fptr_ok(fptr_whitelist_mod_destartup(stack->mod[i]->destartup));
                 (*stack->mod[i]->destartup)(env, i);
         }
-        stack->num = 0;
-        free(stack->mod);
-        stack->mod = NULL;
 }
 
 int
index deb8b634b279bdc737bd8ec09f3553396de73af3..5674aefdd01873c9c476201423ce3b7f229365b4 100644 (file)
@@ -61,14 +61,20 @@ struct module_stack {
 void modstack_init(struct module_stack* stack);
 
 /**
- * Initialises modules and assignes ids.
+ * Free the stack of modules
+ * @param stack: stack that frees up memory.
+ */
+void modstack_free(struct module_stack* stack);
+
+/**
+ * Initialises modules and assignes ids. Calls module_startup().
  * @param stack: Expected empty, filled according to module_conf
  * @param module_conf: string what modules to initialize
  * @param env: module environment which is inited by the modules.
  *     environment should have a superalloc, cfg,
  * @return on false a module init failed.
  */
-int modstack_startup(struct module_stack* stack, const char* module_conf,
+int modstack_call_startup(struct module_stack* stack, const char* module_conf,
        struct module_env* env);
 
 /**
@@ -103,22 +109,22 @@ const char** module_list_avail(void);
  *     env.need_to_validate is set by the modules.
  * @return on false a module init failed.
  */
-int modstack_setup(struct module_stack* stack, const char* module_conf,
+int modstack_call_init(struct module_stack* stack, const char* module_conf,
        struct module_env* env);
 
 /**
- * Desetup the modules, deinit.
+ * Deinit the modules.
  * @param stack: made empty.
  * @param env: module env for module deinit() calls.
  */
-void modstack_desetup(struct module_stack* stack, struct module_env* env);
+void modstack_call_deinit(struct module_stack* stack, struct module_env* env);
 
 /**
  * Destartup the modules, close, delete.
  * @param stack: made empty.
  * @param env: module env for module destartup() calls.
  */
-void modstack_destartup(struct module_stack* stack, struct module_env* env);
+void modstack_call_destartup(struct module_stack* stack, struct module_env* env);
 
 /**
  * Find index of module by name.
index 6ae29cdc4b5c190b47a873e96d13bb41f7dea782..81d92c102a1a37e48f34561c12b09fe9cd2883af 100644 (file)
@@ -288,9 +288,9 @@ static void zonemd_verify_test(char* zname, char* zfile, char* tastr,
        if(!env.auth_zones)
                fatal_exit("out of memory");
        memset(&mods, 0, sizeof(mods));
-       if(!modstack_startup(&mods, env.cfg->module_conf, &env))
+       if(!modstack_call_startup(&mods, env.cfg->module_conf, &env))
                fatal_exit("could not modstack_startup");
-       if(!modstack_setup(&mods, env.cfg->module_conf, &env))
+       if(!modstack_call_init(&mods, env.cfg->module_conf, &env))
                fatal_exit("could not modstack_call_init");
        env.mesh = mesh_create(&mods, &env);
        if(!env.mesh)
@@ -329,8 +329,9 @@ static void zonemd_verify_test(char* zname, char* zfile, char* tastr,
 
        /* desetup test harness */
        mesh_delete(env.mesh);
-       modstack_desetup(&mods, &env);
-       modstack_destartup(&mods, &env);
+       modstack_call_deinit(&mods, &env);
+       modstack_call_destartup(&mods, &env);
+       modstack_free(&mods);
        auth_zones_delete(env.auth_zones);
        anchors_delete(env.anchors);
        config_delete(env.cfg);
index 702372cb8b6a2701440e71c766179c3dfc3097a3..5bdb622a2515448f7bb6d255542ddc357b25e244 100644 (file)
@@ -713,42 +713,42 @@ struct module_func_block {
        const char* name;
 
        /**
-        * Initialise the module. Called when restarting or reloading the
-        * daemon.
-        * This is the place to apply settings from the config file.
+        * Set up the module for start. This is called only once at startup.
+        * Privileged operations like opening device files may be done here.
+        * The function ptr can be NULL, if it is not used.
         * @param env: module environment.
         * @param id: module id number.
         * return: 0 on error
         */
-       int (*init)(struct module_env* env, int id);
+       int (*startup)(struct module_env* env, int id);
 
        /**
-        * Deinitialise the module, undo stuff done during init().
-        * Called before reloading the daemon.
+        * Close down the module for stop. This is called only once before
+        * shutdown to free resources allocated during startup().
+        * Closing privileged ports or files must be done here.
+        * The function ptr can be NULL, if it is not used.
         * @param env: module environment.
         * @param id: module id number.
         */
-       void (*deinit)(struct module_env* env, int id);
+       void (*destartup)(struct module_env* env, int id);
 
        /**
-        * Set up the module for start. This is called only once at startup.
-        * Privileged operations like opening device files may be done here.
-        * The function ptr can be NULL, if it is not used.
+        * Initialise the module. Called when restarting or reloading the
+        * daemon.
+        * This is the place to apply settings from the config file.
         * @param env: module environment.
         * @param id: module id number.
         * return: 0 on error
         */
-       int (*startup)(struct module_env* env, int id);
+       int (*init)(struct module_env* env, int id);
 
        /**
-        * Close down the module for stop. This is called only once before
-        * shutdown to free resources allocated during startup().
-        * Closing privileged ports or files must be done here.
-        * The function ptr can be NULL, if it is not used.
+        * Deinitialise the module, undo stuff done during init().
+        * Called before reloading the daemon.
         * @param env: module environment.
         * @param id: module id number.
         */
-       void (*destartup)(struct module_env* env, int id);
+       void (*deinit)(struct module_env* env, int id);
 
        /**
         * accept a new query, or work further on existing query.
index 3f62733c7491d367477848ef8eb4aec73ab61917..d02f66d1fc641227eabea3fec413074575a8a778 100644 (file)
@@ -3344,7 +3344,7 @@ val_get_mem(struct module_env* env, int id)
  */
 static struct module_func_block val_block = {
        "validator",
-       &val_init, &val_deinit, NULL, NULL, &val_operate, &val_inform_super,
+       NULL, NULL, &val_init, &val_deinit, &val_operate, &val_inform_super,
        &val_clear, &val_get_mem
 };