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 #ifdef SLAPD_EXTERNAL_EXTENSIONS
19 { "extension", load_extension },
24 typedef struct module_loaded_t {
25 struct module_loaded_t *next;
29 module_loaded_t *module_list = NULL;
31 int module_unload (module_loaded_t *module);
33 int module_init (void)
36 const char *error = lt_dlerror();
37 Debug(LDAP_DEBUG_ANY, "lt_dlinit failed: %s\n", error, 0, 0);
43 int module_kill (void)
45 /* unload all modules before shutdown */
46 while (module_list != NULL) {
47 module_unload(module_list);
51 const char *error = lt_dlerror();
52 Debug(LDAP_DEBUG_ANY, "lt_dlexit failed: %s\n", error, 0, 0);
58 int module_load(const char* file_name, int argc, char *argv[])
60 module_loaded_t *module = NULL;
63 int (*initialize) LDAP_P((int argc, char *argv[]));
65 module = (module_loaded_t *)ch_calloc(1, sizeof(module_loaded_t));
67 Debug(LDAP_DEBUG_ANY, "module_load failed: (%s) out of memory\n", file_name,
73 * The result of lt_dlerror(), when called, must be cached prior
74 * to calling Debug. This is because Debug is a macro that expands
75 * into multiple function calls.
77 if ((module->lib = lt_dlopen(file_name)) == NULL) {
79 Debug(LDAP_DEBUG_ANY, "lt_dlopen failed: (%s) %s\n", file_name,
85 Debug(LDAP_DEBUG_CONFIG, "loaded module %s\n", file_name, 0, 0);
87 if ((initialize = lt_dlsym(module->lib, "init_module")) == NULL) {
88 Debug(LDAP_DEBUG_CONFIG, "module %s: no init_module() function found\n",
90 lt_dlclose(module->lib);
95 /* The imported init_module() routine passes back the type of
96 * module (i.e., which part of slapd it should be hooked into)
97 * or -1 for error. If it passes back 0, then you get the
98 * old behavior (i.e., the library is loaded and not hooked
101 * It might be better if the conf file could specify the type
102 * of module. That way, a single module could support multiple
103 * type of hooks. This could be done by using something like:
105 * moduleload extension /usr/local/openldap/whatever.so
107 * then we'd search through module_regtable for a matching
108 * module type, and hook in there.
110 rc = initialize(argc, argv);
112 Debug(LDAP_DEBUG_CONFIG, "module %s: init_module() failed\n",
114 lt_dlclose(module->lib);
119 if (rc >= (sizeof(module_regtable) / sizeof(struct module_regtable_t))
120 || module_regtable[rc].proc == NULL)
122 Debug(LDAP_DEBUG_CONFIG, "module %s: unknown registration type (%d)\n",
124 module_unload(module);
128 rc = (module_regtable[rc].proc)(module, file_name);
130 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module could not be registered\n",
131 file_name, module_regtable[rc].type, 0);
132 module_unload(module);
136 module->next = module_list;
137 module_list = module;
139 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module registered\n",
140 file_name, module_regtable[rc].type, 0);
144 int module_path(const char *path)
146 return lt_dlsetsearchpath( path );
149 void *module_resolve (const void *module, const char *name)
151 if (module == NULL || name == NULL)
153 return(lt_dlsym(((module_loaded_t *)module)->lib, name));
156 int module_unload (module_loaded_t *module)
158 module_loaded_t *mod;
159 int (*terminate) LDAP_P((void));
161 if (module != NULL) {
162 /* remove module from tracking list */
163 if (module_list == module) {
164 module_list = module->next;
166 for (mod = module_list; mod; mod = mod->next) {
167 if (mod->next == module) {
168 mod->next = module->next;
174 /* call module's terminate routine, if present */
175 if (terminate = lt_dlsym(module->lib, "term_module")) {
179 /* close the library and free the memory */
180 lt_dlclose(module->lib);
186 int load_null (const void *module, const char *file_name)
191 #endif /* SLAPD_MODULES */