10 int load_null (const void *module, const char *file_name);
11 int load_extension (const void *module, const char *file_name);
13 struct module_regtable_t {
15 int (*proc)(const void *module, const char *file_name);
16 } module_regtable[] = {
17 { "null", load_null },
18 { "extension", load_extension },
22 typedef struct module_loaded_t {
23 struct module_loaded_t *next;
27 module_loaded_t *module_list = NULL;
29 int module_unload (module_loaded_t *module);
31 int module_init (void)
34 const char *error = lt_dlerror();
35 Debug(LDAP_DEBUG_ANY, "lt_dlinit failed: %s\n", error, 0, 0);
41 int module_kill (void)
43 /* unload all modules before shutdown */
44 while (module_list != NULL) {
45 module_unload(module_list);
49 const char *error = lt_dlerror();
50 Debug(LDAP_DEBUG_ANY, "lt_dlexit failed: %s\n", error, 0, 0);
56 int module_load(const char* file_name, int argc, char *argv[])
58 module_loaded_t *module = NULL;
61 int (*initialize) LDAP_P((int argc, char *argv[]));
63 module = (module_loaded_t *)ch_calloc(1, sizeof(module_loaded_t));
65 Debug(LDAP_DEBUG_ANY, "module_load failed: (%s) out of memory\n", file_name,
71 * The result of lt_dlerror(), when called, must be cached prior
72 * to calling Debug. This is because Debug is a macro that expands
73 * into multiple function calls.
75 if ((module->lib = lt_dlopen(file_name)) == NULL) {
77 Debug(LDAP_DEBUG_ANY, "lt_dlopen failed: (%s) %s\n", file_name,
83 Debug(LDAP_DEBUG_CONFIG, "loaded module %s\n", file_name, 0, 0);
85 if ((initialize = lt_dlsym(module->lib, "init_module")) == NULL) {
86 Debug(LDAP_DEBUG_CONFIG, "module %s: no init_module() function found\n",
88 lt_dlclose(module->lib);
93 /* The imported init_module() routine passes back the type of
94 * module (i.e., which part of slapd it should be hooked into)
95 * or -1 for error. If it passes back 0, then you get the
96 * old behavior (i.e., the library is loaded and not hooked
99 * It might be better if the conf file could specify the type
100 * of module. That way, a single module could support multiple
101 * type of hooks. This could be done by using something like:
103 * moduleload extension /usr/local/openldap/whatever.so
105 * then we'd search through module_regtable for a matching
106 * module type, and hook in there.
108 rc = initialize(argc, argv);
110 Debug(LDAP_DEBUG_CONFIG, "module %s: init_module() failed\n",
112 lt_dlclose(module->lib);
117 if (rc >= (sizeof(module_regtable) / sizeof(struct module_regtable_t))
118 || module_regtable[rc].proc == NULL)
120 Debug(LDAP_DEBUG_CONFIG, "module %s: unknown registration type (%d)\n",
122 module_unload(module);
126 rc = (module_regtable[rc].proc)(module, file_name);
128 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module could not be registered\n",
129 file_name, module_regtable[rc].type, 0);
130 module_unload(module);
134 module->next = module_list;
135 module_list = module;
137 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module registered\n",
138 file_name, module_regtable[rc].type, 0);
142 int module_path(const char *path)
144 return lt_dlsetsearchpath( path );
147 void *module_resolve (const void *module, const char *name)
149 if (module == NULL || name == NULL)
151 return(lt_dlsym(((module_loaded_t *)module)->lib, name));
154 int module_unload (module_loaded_t *module)
156 module_loaded_t *mod;
157 int (*terminate) LDAP_P((void));
159 if (module != NULL) {
160 /* remove module from tracking list */
161 if (module_list == module) {
162 module_list = module->next;
164 for (mod = module_list; mod; mod = mod->next) {
165 if (mod->next == module) {
166 mod->next = module->next;
172 /* call module's terminate routine, if present */
173 if (terminate = lt_dlsym(module->lib, "term_module")) {
177 /* close the library and free the memory */
178 lt_dlclose(module->lib);
184 int load_null (const void *module, const char *file_name)
189 #endif /* SLAPD_MODULES */