2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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 );
67 LDAP_LOG( SLAPD, CRIT,
68 "module_init: lt_dlinit failed: %s\n", error, 0, 0 );
70 Debug(LDAP_DEBUG_ANY, "lt_dlinit failed: %s\n", error, 0, 0);
78 int module_kill (void)
80 /* unload all modules before shutdown */
81 while (module_list != NULL) {
82 module_unload(module_list);
86 const char *error = lt_dlerror();
88 strcpy( ebuf, error );
93 LDAP_LOG( SLAPD, CRIT, "module_kill: lt_dlexit failed: %s\n",
96 Debug(LDAP_DEBUG_ANY, "lt_dlexit failed: %s\n", error, 0, 0);
104 int module_load(const char* file_name, int argc, char *argv[])
106 module_loaded_t *module = NULL;
109 MODULE_INIT_FN initialize;
113 #define file file_name
116 module = (module_loaded_t *)ch_calloc(1, sizeof(module_loaded_t));
117 if (module == NULL) {
119 LDAP_LOG( SLAPD, CRIT,
120 "module_load: (%s) out of memory.\n", file_name, 0, 0 );
122 Debug(LDAP_DEBUG_ANY, "module_load failed: (%s) out of memory\n", file_name,
130 strcpy( file, file_name );
134 * The result of lt_dlerror(), when called, must be cached prior
135 * to calling Debug. This is because Debug is a macro that expands
136 * into multiple function calls.
138 if ((module->lib = lt_dlopenext(file)) == NULL) {
139 error = lt_dlerror();
141 strcpy( ebuf, error );
146 LDAP_LOG( SLAPD, CRIT,
147 "module_load: lt_dlopenext failed: (%s) %s.\n",
148 file_name, error, 0 );
150 Debug(LDAP_DEBUG_ANY, "lt_dlopenext failed: (%s) %s\n", file_name,
159 LDAP_LOG( SLAPD, INFO, "module_load: loaded module %s\n", file_name, 0, 0 );
161 Debug(LDAP_DEBUG_CONFIG, "loaded module %s\n", file_name, 0, 0);
166 #pragma convlit(suspend)
168 if ((initialize = lt_dlsym(module->lib, "init_module")) == NULL) {
170 #pragma convlit(resume)
173 LDAP_LOG( SLAPD, ERR,
174 "module_load: module %s : no init_module() function found\n",
177 Debug(LDAP_DEBUG_CONFIG, "module %s: no init_module() function found\n",
181 lt_dlclose(module->lib);
186 /* The imported init_module() routine passes back the type of
187 * module (i.e., which part of slapd it should be hooked into)
188 * or -1 for error. If it passes back 0, then you get the
189 * old behavior (i.e., the library is loaded and not hooked
192 * It might be better if the conf file could specify the type
193 * of module. That way, a single module could support multiple
194 * type of hooks. This could be done by using something like:
196 * moduleload extension /usr/local/openldap/whatever.so
198 * then we'd search through module_regtable for a matching
199 * module type, and hook in there.
201 rc = initialize(argc, argv);
204 LDAP_LOG( SLAPD, ERR,
205 "module_load: module %s init_module() failed\n", file_name, 0, 0);
207 Debug(LDAP_DEBUG_CONFIG, "module %s: init_module() failed\n",
211 lt_dlclose(module->lib);
216 if (rc >= (int)(sizeof(module_regtable) / sizeof(struct module_regtable_t))
217 || module_regtable[rc].proc == NULL)
220 LDAP_LOG( SLAPD, ERR,
221 "module_load: module %s: unknown registration type (%d).\n",
224 Debug(LDAP_DEBUG_CONFIG, "module %s: unknown registration type (%d)\n",
228 module_unload(module);
232 rc = (module_regtable[rc].proc)(module, file_name);
235 LDAP_LOG( SLAPD, ERR,
236 "module_load: module %s:%s could not be registered.\n",
237 file_name, module_regtable[rc].type, 0 );
239 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module could not be registered\n",
240 file_name, module_regtable[rc].type, 0);
243 module_unload(module);
247 module->next = module_list;
248 module_list = module;
251 LDAP_LOG( SLAPD, INFO,
252 "module_load: module %s:%s registered\n", file_name,
253 module_regtable[rc].type, 0 );
255 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module registered\n",
256 file_name, module_regtable[rc].type, 0);
262 int module_path(const char *path)
269 return lt_dlsetsearchpath( path );
272 void *module_resolve (const void *module, const char *name)
279 if (module == NULL || name == NULL)
281 return(lt_dlsym(((module_loaded_t *)module)->lib, name));
284 static int module_unload (module_loaded_t *module)
286 module_loaded_t *mod;
287 MODULE_TERM_FN terminate;
289 if (module != NULL) {
290 /* remove module from tracking list */
291 if (module_list == module) {
292 module_list = module->next;
294 for (mod = module_list; mod; mod = mod->next) {
295 if (mod->next == module) {
296 mod->next = module->next;
302 /* call module's terminate routine, if present */
304 #pragma convlit(suspend)
306 if ((terminate = lt_dlsym(module->lib, "term_module"))) {
308 #pragma convlit(resume)
313 /* close the library and free the memory */
314 lt_dlclose(module->lib);
320 int load_null_module (const void *module, const char *file_name)
325 #ifdef SLAPD_EXTERNAL_EXTENSIONS
329 const char *file_name
332 SLAP_EXTOP_MAIN_FN *ext_main;
333 SLAP_EXTOP_GETOID_FN *ext_getoid;
337 ext_main = (SLAP_EXTOP_MAIN_FN *)module_resolve(module, "ext_main");
338 if (ext_main == NULL) {
342 ext_getoid = module_resolve(module, "ext_getoid");
343 if (ext_getoid == NULL) {
347 rc = (ext_getoid)(0, &oid, 256);
351 if (oid.bv_val == NULL || oid.bv_len == 0) {
355 rc = load_extop( &oid, ext_main );
358 #endif /* SLAPD_EXTERNAL_EXTENSIONS */
359 #endif /* SLAPD_MODULES */