+struct module_regtable_t {
+ char *type;
+ MODULE_LOAD_FN proc;
+} module_regtable[] = {
+ { "null", load_null_module },
+#ifdef SLAPD_EXTERNAL_EXTENSIONS
+ { "extension", load_extop_module },
+#endif
+ { NULL, NULL }
+};
+
+typedef struct module_loaded_t {
+ struct module_loaded_t *next;
+ lt_dlhandle lib;
+} module_loaded_t;
+
+module_loaded_t *module_list = NULL;
+
+static int module_unload (module_loaded_t *module);
+
+int module_init (void)
+{
+ if (lt_dlinit()) {
+ const char *error = lt_dlerror();
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "module", LDAP_LEVEL_CRIT,
+ "module_init: lt_ldinit failed: %s\n", error ));
+#else
+ Debug(LDAP_DEBUG_ANY, "lt_dlinit failed: %s\n", error, 0, 0);
+#endif
+
+ return -1;
+ }
+ return 0;
+}
+
+int module_kill (void)
+{
+ /* unload all modules before shutdown */
+ while (module_list != NULL) {
+ module_unload(module_list);
+ }
+
+ if (lt_dlexit()) {
+ const char *error = lt_dlerror();
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "module", LDAP_LEVEL_CRIT,
+ "module_kill: lt_dlexit failed: %s\n", error ));
+#else
+ Debug(LDAP_DEBUG_ANY, "lt_dlexit failed: %s\n", error, 0, 0);
+#endif
+
+ return -1;
+ }
+ return 0;
+}
+
+int module_load(const char* file_name, int argc, char *argv[])
+{
+ module_loaded_t *module = NULL;
+ const char *error;
+ int rc;
+ MODULE_INIT_FN initialize;
+
+ module = (module_loaded_t *)ch_calloc(1, sizeof(module_loaded_t));
+ if (module == NULL) {
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "module", LDAP_LEVEL_CRIT,
+ "module_load: (%s) out of memory.\n", file_name ));
+#else
+ Debug(LDAP_DEBUG_ANY, "module_load failed: (%s) out of memory\n", file_name,
+ 0, 0);
+#endif
+
+ return -1;
+ }
+
+ /*
+ * The result of lt_dlerror(), when called, must be cached prior
+ * to calling Debug. This is because Debug is a macro that expands
+ * into multiple function calls.
+ */
+ if ((module->lib = lt_dlopen(file_name)) == NULL) {
+ error = lt_dlerror();
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "module", LDAP_LEVEL_CRIT,
+ "module_load: lt_dlopen failed: (%s) %s.\n",
+ file_name, error ));
+#else
+ Debug(LDAP_DEBUG_ANY, "lt_dlopen failed: (%s) %s\n", file_name,
+ error, 0);
+#endif
+
+ ch_free(module);
+ return -1;
+ }
+
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "module", LDAP_LEVEL_INFO,
+ "module_load: loaded module %s\n", file_name ));
+#else
+ Debug(LDAP_DEBUG_CONFIG, "loaded module %s\n", file_name, 0, 0);
+#endif
+