2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
24 typedef int (*MODULE_INIT_FN)(
27 typedef int (*MODULE_LOAD_FN)(
29 const char *filename);
30 typedef int (*MODULE_TERM_FN)(void);
33 struct module_regtable_t {
36 } module_regtable[] = {
37 { "null", load_null_module },
38 #ifdef SLAPD_EXTERNAL_EXTENSIONS
39 { "extension", load_extop_module },
44 typedef struct module_loaded_t {
45 struct module_loaded_t *next;
49 module_loaded_t *module_list = NULL;
51 static int module_unload (module_loaded_t *module);
54 static char ebuf[BUFSIZ];
57 int module_init (void)
60 const char *error = lt_dlerror();
62 strcpy( ebuf, error );
66 Debug(LDAP_DEBUG_ANY, "lt_dlinit failed: %s\n", error, 0, 0);
73 int module_kill (void)
75 /* unload all modules before shutdown */
76 while (module_list != NULL) {
77 module_unload(module_list);
81 const char *error = lt_dlerror();
83 strcpy( ebuf, error );
87 Debug(LDAP_DEBUG_ANY, "lt_dlexit failed: %s\n", error, 0, 0);
94 int module_load(const char* file_name, int argc, char *argv[])
96 module_loaded_t *module = NULL;
99 MODULE_INIT_FN initialize;
103 #define file file_name
106 module = (module_loaded_t *)ch_calloc(1, sizeof(module_loaded_t));
107 if (module == NULL) {
108 Debug(LDAP_DEBUG_ANY, "module_load failed: (%s) out of memory\n", file_name,
115 strcpy( file, file_name );
119 * The result of lt_dlerror(), when called, must be cached prior
120 * to calling Debug. This is because Debug is a macro that expands
121 * into multiple function calls.
123 if ((module->lib = lt_dlopenext(file)) == NULL) {
124 error = lt_dlerror();
126 strcpy( ebuf, error );
130 Debug(LDAP_DEBUG_ANY, "lt_dlopenext failed: (%s) %s\n", file_name,
137 Debug(LDAP_DEBUG_CONFIG, "loaded module %s\n", file_name, 0, 0);
141 #pragma convlit(suspend)
143 if ((initialize = lt_dlsym(module->lib, "init_module")) == NULL) {
145 #pragma convlit(resume)
147 Debug(LDAP_DEBUG_CONFIG, "module %s: no init_module() function found\n",
150 lt_dlclose(module->lib);
155 /* The imported init_module() routine passes back the type of
156 * module (i.e., which part of slapd it should be hooked into)
157 * or -1 for error. If it passes back 0, then you get the
158 * old behavior (i.e., the library is loaded and not hooked
161 * It might be better if the conf file could specify the type
162 * of module. That way, a single module could support multiple
163 * type of hooks. This could be done by using something like:
165 * moduleload extension /usr/local/openldap/whatever.so
167 * then we'd search through module_regtable for a matching
168 * module type, and hook in there.
170 rc = initialize(argc, argv);
172 Debug(LDAP_DEBUG_CONFIG, "module %s: init_module() failed\n",
175 lt_dlclose(module->lib);
180 if (rc >= (int)(sizeof(module_regtable) / sizeof(struct module_regtable_t))
181 || module_regtable[rc].proc == NULL)
183 Debug(LDAP_DEBUG_CONFIG, "module %s: unknown registration type (%d)\n",
186 module_unload(module);
190 rc = (module_regtable[rc].proc)(module, file_name);
192 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module could not be registered\n",
193 file_name, module_regtable[rc].type, 0);
195 module_unload(module);
199 module->next = module_list;
200 module_list = module;
202 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module registered\n",
203 file_name, module_regtable[rc].type, 0);
208 int module_path(const char *path)
215 return lt_dlsetsearchpath( path );
218 void *module_resolve (const void *module, const char *name)
225 if (module == NULL || name == NULL)
227 return(lt_dlsym(((module_loaded_t *)module)->lib, name));
230 static int module_unload (module_loaded_t *module)
232 module_loaded_t *mod;
233 MODULE_TERM_FN terminate;
235 if (module != NULL) {
236 /* remove module from tracking list */
237 if (module_list == module) {
238 module_list = module->next;
240 for (mod = module_list; mod; mod = mod->next) {
241 if (mod->next == module) {
242 mod->next = module->next;
248 /* call module's terminate routine, if present */
250 #pragma convlit(suspend)
252 if ((terminate = lt_dlsym(module->lib, "term_module"))) {
254 #pragma convlit(resume)
259 /* close the library and free the memory */
260 lt_dlclose(module->lib);
266 int load_null_module (const void *module, const char *file_name)
271 #ifdef SLAPD_EXTERNAL_EXTENSIONS
275 const char *file_name
278 SLAP_EXTOP_MAIN_FN *ext_main;
279 SLAP_EXTOP_GETOID_FN *ext_getoid;
283 ext_main = (SLAP_EXTOP_MAIN_FN *)module_resolve(module, "ext_main");
284 if (ext_main == NULL) {
288 ext_getoid = module_resolve(module, "ext_getoid");
289 if (ext_getoid == NULL) {
293 rc = (ext_getoid)(0, &oid, 256);
297 if (oid.bv_val == NULL || oid.bv_len == 0) {
301 rc = load_extop( &oid, ext_main );
304 #endif /* SLAPD_EXTERNAL_EXTENSIONS */
305 #endif /* SLAPD_MODULES */