2 * xmlmodule.c : basic API for dynamic module loading added 2.6.17
4 * See Copyright for the status of this software.
6 * joelwreed@comcast.net
8 * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
15 #include <libxml/xmlmemory.h>
16 #include <libxml/xmlerror.h>
17 #include <libxml/xmlmodule.h>
18 #include <libxml/globals.h>
20 #ifdef LIBXML_MODULES_ENABLED
27 static void *xmlModulePlatformOpen(const char *name
);
28 static int xmlModulePlatformClose(void *handle
);
29 static int xmlModulePlatformSymbol(void *handle
, const char *name
, void **result
);
31 /************************************************************************
33 * module memory error handler *
35 ************************************************************************/
39 * @extra: extra information
41 * Handle an out of memory condition
44 xmlModuleErrMemory(xmlModulePtr module
, const char *extra
)
46 const char *name
= NULL
;
49 name
= (const char *) module
->name
;
52 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_MODULE
,
53 XML_ERR_NO_MEMORY
, XML_ERR_FATAL
, NULL
, 0, extra
,
55 "Memory allocation failed : %s\n", extra
);
60 * @name: the module name
61 * @options: a set of xmlModuleOption
63 * Opens a module/shared library given its name or path
64 * TODO: options are not yet implemented.
66 * Returns a handle for the module or NULL in case of error
69 xmlModuleOpen(const char *name
, int options ATTRIBUTE_UNUSED
)
73 module
= (xmlModulePtr
) xmlMalloc(sizeof(xmlModule
));
75 xmlModuleErrMemory(NULL
, "creating module");
79 memset(module
, 0, sizeof(xmlModule
));
81 module
->handle
= xmlModulePlatformOpen(name
);
83 if (module
->handle
== NULL
) {
85 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_MODULE
,
86 XML_MODULE_OPEN
, XML_ERR_FATAL
, NULL
, 0, 0,
87 name
, NULL
, 0, 0, "failed to open %s\n", name
);
91 module
->name
= xmlStrdup((const xmlChar
*) name
);
98 * @name: the name of the symbol
99 * @symbol: the resulting symbol address
101 * Lookup for a symbol address in the given module
103 * Returns 0 if the symbol was found, or -1 in case of error
106 xmlModuleSymbol(xmlModulePtr module
, const char *name
, void **symbol
)
110 if ((NULL
== module
) || (symbol
== NULL
)) {
111 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_MODULE
,
112 XML_MODULE_OPEN
, XML_ERR_FATAL
, NULL
, 0, 0,
113 NULL
, NULL
, 0, 0, "null parameter\n");
117 rc
= xmlModulePlatformSymbol(module
->handle
, name
, symbol
);
120 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_MODULE
,
121 XML_MODULE_OPEN
, XML_ERR_FATAL
, NULL
, 0, 0,
123 "failed to find symbol: %s\n",
124 (name
== NULL
? "NULL" : name
));
133 * @module: the module handle
135 * The close operations unload the associated module and free the
136 * data associated to the module.
138 * Returns 0 in case of success, -1 in case of argument error and -2
139 * if the module could not be closed/unloaded.
142 xmlModuleClose(xmlModulePtr module
)
146 if (NULL
== module
) {
147 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_MODULE
,
148 XML_MODULE_CLOSE
, XML_ERR_FATAL
, NULL
, 0, 0,
149 NULL
, NULL
, 0, 0, "null module pointer\n");
153 rc
= xmlModulePlatformClose(module
->handle
);
156 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_MODULE
,
157 XML_MODULE_CLOSE
, XML_ERR_FATAL
, NULL
, 0, 0,
158 (const char *) module
->name
, NULL
, 0, 0,
159 "failed to close: %s\n", module
->name
);
163 rc
= xmlModuleFree(module
);
169 * @module: the module handle
171 * The free operations free the data associated to the module
172 * but does not unload the associated shared library which may still
175 * Returns 0 in case of success, -1 in case of argument error
178 xmlModuleFree(xmlModulePtr module
)
180 if (NULL
== module
) {
181 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_MODULE
,
182 XML_MODULE_CLOSE
, XML_ERR_FATAL
, NULL
, 0, NULL
,
183 NULL
, NULL
, 0, 0, "null module pointer\n");
187 xmlFree(module
->name
);
193 #if defined(HAVE_DLOPEN) && !defined(_WIN32)
198 #ifndef RTLD_GLOBAL /* For Tru64 UNIX 4.0 */
199 #define RTLD_GLOBAL 0
203 * xmlModulePlatformOpen:
204 * @name: path to the module
206 * returns a handle on success, and zero on error.
210 xmlModulePlatformOpen(const char *name
)
212 return dlopen(name
, RTLD_GLOBAL
| RTLD_NOW
);
216 * xmlModulePlatformClose:
217 * @handle: handle to the module
219 * returns 0 on success, and non-zero on error.
223 xmlModulePlatformClose(void *handle
)
225 return dlclose(handle
);
229 * xmlModulePlatformSymbol:
230 * http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html
231 * returns 0 on success and the loaded symbol in result, and -1 on error.
235 xmlModulePlatformSymbol(void *handle
, const char *name
, void **symbol
)
237 *symbol
= dlsym(handle
, name
);
238 if (dlerror() != NULL
) {
244 #else /* ! HAVE_DLOPEN */
246 #ifdef HAVE_SHLLOAD /* HAVE_SHLLOAD */
251 * xmlModulePlatformOpen:
252 * returns a handle on success, and zero on error.
256 xmlModulePlatformOpen(const char *name
)
258 return shl_load(name
, BIND_IMMEDIATE
, 0L);
262 * xmlModulePlatformClose:
263 * returns 0 on success, and non-zero on error.
267 xmlModulePlatformClose(void *handle
)
269 return shl_unload(handle
);
273 * xmlModulePlatformSymbol:
274 * http://docs.hp.com/en/B2355-90683/shl_load.3X.html
275 * returns 0 on success and the loaded symbol in result, and -1 on error.
279 xmlModulePlatformSymbol(void *handle
, const char *name
, void **symbol
)
284 rc
= shl_findsym(&handle
, name
, TYPE_UNDEFINED
, symbol
);
288 #endif /* HAVE_SHLLOAD */
289 #endif /* ! HAVE_DLOPEN */
296 * xmlModulePlatformOpen:
297 * returns a handle on success, and zero on error.
301 xmlModulePlatformOpen(const char *name
)
303 return LoadLibraryA(name
);
307 * xmlModulePlatformClose:
308 * returns 0 on success, and non-zero on error.
312 xmlModulePlatformClose(void *handle
)
316 rc
= FreeLibrary(handle
);
321 * xmlModulePlatformSymbol:
322 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp
323 * returns 0 on success and the loaded symbol in result, and -1 on error.
327 xmlModulePlatformSymbol(void *handle
, const char *name
, void **symbol
)
329 *symbol
= GetProcAddress(handle
, name
);
330 return (NULL
== *symbol
) ? -1 : 0;
337 #include <kernel/image.h>
340 * xmlModulePlatformOpen:
341 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
342 * returns a handle on success, and zero on error.
346 xmlModulePlatformOpen(const char *name
)
348 return (void *) load_add_on(name
);
352 * xmlModulePlatformClose:
353 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
354 * returns 0 on success, and non-zero on error.
358 xmlModulePlatformClose(void *handle
)
362 rc
= unload_add_on((image_id
) handle
);
371 * xmlModulePlatformSymbol:
372 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
373 * returns 0 on success and the loaded symbol in result, and -1 on error.
377 xmlModulePlatformSymbol(void *handle
, const char *name
, void **symbol
)
381 rc
= get_image_symbol((image_id
) handle
, name
, B_SYMBOL_TYPE_ANY
, symbol
);
383 return (rc
== B_OK
) ? 0 : -1;
386 #endif /* HAVE_BEOS */
393 * xmlModulePlatformOpen:
394 * os2 api info: http://www.edm2.com/os2api/Dos/DosLoadModule.html
395 * returns a handle on success, and zero on error.
399 xmlModulePlatformOpen(const char *name
)
405 rc
= DosLoadModule(errbuf
, sizeof(errbuf
) - 1, name
, &handle
);
414 * xmlModulePlatformClose:
415 * os2 api info: http://www.edm2.com/os2api/Dos/DosFreeModule.html
416 * returns 0 on success, and non-zero on error.
420 xmlModulePlatformClose(void *handle
)
422 return DosFreeModule(handle
);
426 * xmlModulePlatformSymbol:
427 * os2 api info: http://www.edm2.com/os2api/Dos/DosQueryProcAddr.html
428 * returns 0 on success and the loaded symbol in result, and -1 on error.
432 xmlModulePlatformSymbol(void *handle
, const char *name
, void **symbol
)
436 rc
= DosQueryProcAddr(handle
, 0, name
, symbol
);
438 return (rc
== NO_ERROR
) ? 0 : -1;
441 #endif /* HAVE_OS2 */
443 #define bottom_xmlmodule
444 #include "elfgcchack.h"
445 #endif /* LIBXML_MODULES_ENABLED */