xmlmodule.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * xmlmodule.c : basic API for dynamic module loading added 2.6.17
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * joelwreed@comcast.net
  7. *
  8. * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
  9. */
  10. #define IN_LIBXML
  11. #include "libxml.h"
  12. #include <string.h>
  13. #include <libxml/xmlmemory.h>
  14. #include <libxml/xmlerror.h>
  15. #include <libxml/xmlmodule.h>
  16. #include <libxml/globals.h>
  17. #ifdef LIBXML_MODULES_ENABLED
  18. struct _xmlModule {
  19. unsigned char *name;
  20. void *handle;
  21. };
  22. static void *xmlModulePlatformOpen(const char *name);
  23. static int xmlModulePlatformClose(void *handle);
  24. static int xmlModulePlatformSymbol(void *handle, const char *name, void **result);
  25. /************************************************************************
  26. * *
  27. * module memory error handler *
  28. * *
  29. ************************************************************************/
  30. /**
  31. * xmlModuleErrMemory:
  32. * @extra: extra information
  33. *
  34. * Handle an out of memory condition
  35. */
  36. static void
  37. xmlModuleErrMemory(xmlModulePtr module, const char *extra)
  38. {
  39. const char *name = NULL;
  40. if (module != NULL) {
  41. name = (const char *) module->name;
  42. }
  43. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
  44. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
  45. name, NULL, 0, 0,
  46. "Memory allocation failed : %s\n", extra);
  47. }
  48. /**
  49. * xmlModuleOpen:
  50. * @name: the module name
  51. * @options: a set of xmlModuleOption
  52. *
  53. * Opens a module/shared library given its name or path
  54. * TODO: options are not yet implemented.
  55. *
  56. * Returns a handle for the module or NULL in case of error
  57. */
  58. xmlModulePtr
  59. xmlModuleOpen(const char *name, int options ATTRIBUTE_UNUSED)
  60. {
  61. xmlModulePtr module;
  62. module = (xmlModulePtr) xmlMalloc(sizeof(xmlModule));
  63. if (module == NULL) {
  64. xmlModuleErrMemory(NULL, "creating module");
  65. return (NULL);
  66. }
  67. memset(module, 0, sizeof(xmlModule));
  68. module->handle = xmlModulePlatformOpen(name);
  69. if (module->handle == NULL) {
  70. xmlFree(module);
  71. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
  72. XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
  73. name, NULL, 0, 0, "failed to open %s\n", name);
  74. return(NULL);
  75. }
  76. module->name = xmlStrdup((const xmlChar *) name);
  77. return (module);
  78. }
  79. /**
  80. * xmlModuleSymbol:
  81. * @module: the module
  82. * @name: the name of the symbol
  83. * @symbol: the resulting symbol address
  84. *
  85. * Lookup for a symbol address in the given module
  86. *
  87. * Returns 0 if the symbol was found, or -1 in case of error
  88. */
  89. int
  90. xmlModuleSymbol(xmlModulePtr module, const char *name, void **symbol)
  91. {
  92. int rc = -1;
  93. if ((NULL == module) || (symbol == NULL)) {
  94. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
  95. XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
  96. NULL, NULL, 0, 0, "null parameter\n");
  97. return rc;
  98. }
  99. rc = xmlModulePlatformSymbol(module->handle, name, symbol);
  100. if (rc == -1) {
  101. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
  102. XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
  103. name, NULL, 0, 0,
  104. "failed to find symbol: %s\n",
  105. (name == NULL ? "NULL" : name));
  106. return rc;
  107. }
  108. return rc;
  109. }
  110. /**
  111. * xmlModuleClose:
  112. * @module: the module handle
  113. *
  114. * The close operations unload the associated module and free the
  115. * data associated to the module.
  116. *
  117. * Returns 0 in case of success, -1 in case of argument error and -2
  118. * if the module could not be closed/unloaded.
  119. */
  120. int
  121. xmlModuleClose(xmlModulePtr module)
  122. {
  123. int rc;
  124. if (NULL == module) {
  125. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
  126. XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, 0,
  127. NULL, NULL, 0, 0, "null module pointer\n");
  128. return -1;
  129. }
  130. rc = xmlModulePlatformClose(module->handle);
  131. if (rc != 0) {
  132. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
  133. XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, 0,
  134. (const char *) module->name, NULL, 0, 0,
  135. "failed to close: %s\n", module->name);
  136. return -2;
  137. }
  138. rc = xmlModuleFree(module);
  139. return (rc);
  140. }
  141. /**
  142. * xmlModuleFree:
  143. * @module: the module handle
  144. *
  145. * The free operations free the data associated to the module
  146. * but does not unload the associated shared library which may still
  147. * be in use.
  148. *
  149. * Returns 0 in case of success, -1 in case of argument error
  150. */
  151. int
  152. xmlModuleFree(xmlModulePtr module)
  153. {
  154. if (NULL == module) {
  155. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
  156. XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, NULL,
  157. NULL, NULL, 0, 0, "null module pointer\n");
  158. return -1;
  159. }
  160. xmlFree(module->name);
  161. xmlFree(module);
  162. return (0);
  163. }
  164. #if defined(HAVE_DLOPEN) && !defined(_WIN32)
  165. #ifdef HAVE_DLFCN_H
  166. #include <dlfcn.h>
  167. #endif
  168. #ifndef RTLD_GLOBAL /* For Tru64 UNIX 4.0 */
  169. #define RTLD_GLOBAL 0
  170. #endif
  171. /**
  172. * xmlModulePlatformOpen:
  173. * @name: path to the module
  174. *
  175. * returns a handle on success, and zero on error.
  176. */
  177. static void *
  178. xmlModulePlatformOpen(const char *name)
  179. {
  180. return dlopen(name, RTLD_GLOBAL | RTLD_NOW);
  181. }
  182. /*
  183. * xmlModulePlatformClose:
  184. * @handle: handle to the module
  185. *
  186. * returns 0 on success, and non-zero on error.
  187. */
  188. static int
  189. xmlModulePlatformClose(void *handle)
  190. {
  191. return dlclose(handle);
  192. }
  193. /*
  194. * xmlModulePlatformSymbol:
  195. * http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html
  196. * returns 0 on success and the loaded symbol in result, and -1 on error.
  197. */
  198. static int
  199. xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
  200. {
  201. *symbol = dlsym(handle, name);
  202. if (dlerror() != NULL) {
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. #else /* ! HAVE_DLOPEN */
  208. #ifdef HAVE_SHLLOAD /* HAVE_SHLLOAD */
  209. #ifdef HAVE_DL_H
  210. #include <dl.h>
  211. #endif
  212. /*
  213. * xmlModulePlatformOpen:
  214. * returns a handle on success, and zero on error.
  215. */
  216. static void *
  217. xmlModulePlatformOpen(const char *name)
  218. {
  219. return shl_load(name, BIND_IMMEDIATE, 0L);
  220. }
  221. /*
  222. * xmlModulePlatformClose:
  223. * returns 0 on success, and non-zero on error.
  224. */
  225. static int
  226. xmlModulePlatformClose(void *handle)
  227. {
  228. return shl_unload(handle);
  229. }
  230. /*
  231. * xmlModulePlatformSymbol:
  232. * http://docs.hp.com/en/B2355-90683/shl_load.3X.html
  233. * returns 0 on success and the loaded symbol in result, and -1 on error.
  234. */
  235. static int
  236. xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
  237. {
  238. int rc;
  239. errno = 0;
  240. rc = shl_findsym(&handle, name, TYPE_UNDEFINED, symbol);
  241. return rc;
  242. }
  243. #endif /* HAVE_SHLLOAD */
  244. #endif /* ! HAVE_DLOPEN */
  245. #ifdef _WIN32
  246. #include <windows.h>
  247. /*
  248. * xmlModulePlatformOpen:
  249. * returns a handle on success, and zero on error.
  250. */
  251. static void *
  252. xmlModulePlatformOpen(const char *name)
  253. {
  254. return LoadLibrary(name);
  255. }
  256. /*
  257. * xmlModulePlatformClose:
  258. * returns 0 on success, and non-zero on error.
  259. */
  260. static int
  261. xmlModulePlatformClose(void *handle)
  262. {
  263. int rc;
  264. rc = FreeLibrary(handle);
  265. return (0 == rc);
  266. }
  267. /*
  268. * xmlModulePlatformSymbol:
  269. * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp
  270. * returns 0 on success and the loaded symbol in result, and -1 on error.
  271. */
  272. static int
  273. xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
  274. {
  275. *symbol = GetProcAddress(handle, name);
  276. return (NULL == *symbol) ? -1 : 0;
  277. }
  278. #endif /* _WIN32 */
  279. #ifdef HAVE_BEOS
  280. #include <kernel/image.h>
  281. /*
  282. * xmlModulePlatformOpen:
  283. * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
  284. * returns a handle on success, and zero on error.
  285. */
  286. static void *
  287. xmlModulePlatformOpen(const char *name)
  288. {
  289. return (void *) load_add_on(name);
  290. }
  291. /*
  292. * xmlModulePlatformClose:
  293. * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
  294. * returns 0 on success, and non-zero on error.
  295. */
  296. static int
  297. xmlModulePlatformClose(void *handle)
  298. {
  299. status_t rc;
  300. rc = unload_add_on((image_id) handle);
  301. if (rc == B_OK)
  302. return 0;
  303. else
  304. return -1;
  305. }
  306. /*
  307. * xmlModulePlatformSymbol:
  308. * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
  309. * returns 0 on success and the loaded symbol in result, and -1 on error.
  310. */
  311. static int
  312. xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
  313. {
  314. status_t rc;
  315. rc = get_image_symbol((image_id) handle, name, B_SYMBOL_TYPE_ANY, symbol);
  316. return (rc == B_OK) ? 0 : -1;
  317. }
  318. #endif /* HAVE_BEOS */
  319. #ifdef HAVE_OS2
  320. #include <os2.h>
  321. /*
  322. * xmlModulePlatformOpen:
  323. * os2 api info: http://www.edm2.com/os2api/Dos/DosLoadModule.html
  324. * returns a handle on success, and zero on error.
  325. */
  326. static void *
  327. xmlModulePlatformOpen(const char *name)
  328. {
  329. char errbuf[256];
  330. void *handle;
  331. int rc;
  332. rc = DosLoadModule(errbuf, sizeof(errbuf) - 1, name, &handle);
  333. if (rc)
  334. return 0;
  335. else
  336. return (handle);
  337. }
  338. /*
  339. * xmlModulePlatformClose:
  340. * os2 api info: http://www.edm2.com/os2api/Dos/DosFreeModule.html
  341. * returns 0 on success, and non-zero on error.
  342. */
  343. static int
  344. xmlModulePlatformClose(void *handle)
  345. {
  346. return DosFreeModule(handle);
  347. }
  348. /*
  349. * xmlModulePlatformSymbol:
  350. * os2 api info: http://www.edm2.com/os2api/Dos/DosQueryProcAddr.html
  351. * returns 0 on success and the loaded symbol in result, and -1 on error.
  352. */
  353. static int
  354. xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
  355. {
  356. int rc;
  357. rc = DosQueryProcAddr(handle, 0, name, symbol);
  358. return (rc == NO_ERROR) ? 0 : -1;
  359. }
  360. #endif /* HAVE_OS2 */
  361. #define bottom_xmlmodule
  362. #include "elfgcchack.h"
  363. #endif /* LIBXML_MODULES_ENABLED */