]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Call detach on shallowest parent first
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 3 Mar 2022 00:04:20 +0000 (18:04 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 3 Mar 2022 02:36:00 +0000 (20:36 -0600)
src/lib/server/dl_module.c
src/lib/server/dl_module.h

index 4f8db617463e8dd675eeb636c3f678348628878c..5b5bb3314acbf98d71c6ddd3da60640adbfa556a 100644 (file)
@@ -247,18 +247,38 @@ void *dl_module_parent_data_by_child_data(void const *data)
        return dl_inst->parent->data;
 }
 
+/** Detach the shallowest parent first
+ *
+ */
+static void dl_module_detach_parent(dl_module_inst_t *dl_inst)
+{
+       if (dl_inst->detached) return;
+
+       if (dl_inst->parent) dl_module_detach_parent(UNCONST(dl_module_inst_t *, dl_inst->parent));
+
+       if (dl_inst->module->common->detach) {
+               dl_inst->module->common->detach(&(module_detach_ctx_t){ .inst = dl_inst });
+               dl_inst->detached = true;
+       }
+}
+
 static int _dl_module_instance_data_free(void *data)
 {
-        dl_module_inst_t const *dl_inst = dl_module_instance_by_data(data);
+       dl_module_inst_t *dl_inst = UNCONST(dl_module_inst_t *, dl_module_instance_by_data(data));
 
-        if (!dl_inst) {
-                ERROR("Failed resolving data %p, to dl_module_inst_t, refusing to free", data);
-                return -1;
-        }
+       if (!dl_inst) {
+               ERROR("Failed resolving data %p, to dl_module_inst_t, refusing to free", data);
+               return -1;
+       }
 
-        if (dl_inst->module->common->detach) dl_inst->module->common->detach(&(module_detach_ctx_t){ .inst = dl_inst });
+       /*
+        *      Ensure the shallowest parent module
+        *      gets detached first so that it can
+        *      still reach its children.
+        */
+       dl_module_detach_parent(dl_inst);
 
-        return 0;
+       return 0;
 }
 
 /** Allocate module instance data, and parse the module's configuration
index c67ecaa8015a15d4c0e5c0832d4ba568f64564d6..1849bf050c42b7579430a29e0eb91b2c5a6d8e6b 100644 (file)
@@ -164,6 +164,7 @@ struct dl_module_instance_s {
        void                            * _CONST data;          //!< Module instance's parsed configuration.
        CONF_SECTION                    * _CONST conf;          //!< Module's instance configuration.
        dl_module_inst_t const          * _CONST parent;        //!< Parent module's instance (if any).
+       bool                            _CONST detached;        //!< Whether the detach function has been called.
 };
 
 extern fr_table_num_sorted_t const dl_module_type_prefix[];