• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 /* In order RTLD_GLOBAL and RTLD_NOW to be defined on zOS */
12 #if defined(__MVS__)
13 #define _UNIX03_SOURCE
14 #endif
15 
16 #define IN_LIBXML
17 #include "libxml.h"
18 
19 #include <string.h>
20 #include <libxml/xmlmodule.h>
21 #include <libxml/xmlmemory.h>
22 #include <libxml/xmlerror.h>
23 #include <libxml/xmlstring.h>
24 
25 #include "private/error.h"
26 
27 #ifdef LIBXML_MODULES_ENABLED
28 
29 struct _xmlModule {
30     unsigned char *name;
31     void *handle;
32 };
33 
34 static void *xmlModulePlatformOpen(const char *name);
35 static int xmlModulePlatformClose(void *handle);
36 static int xmlModulePlatformSymbol(void *handle, const char *name, void **result);
37 
38 /************************************************************************
39  *									*
40  *		module memory error handler				*
41  *									*
42  ************************************************************************/
43 
44 /**
45  * xmlModuleOpen:
46  * @name: the module name
47  * @options: a set of xmlModuleOption
48  *
49  * Opens a module/shared library given its name or path
50  * NOTE: that due to portability issues, behaviour can only be
51  * guaranteed with @name using ASCII. We cannot guarantee that
52  * an UTF-8 string would work, which is why name is a const char *
53  * and not a const xmlChar * .
54  * TODO: options are not yet implemented.
55  *
56  * Returns a handle for the module or NULL in case of error
57  */
58 xmlModulePtr
xmlModuleOpen(const char * name,int options ATTRIBUTE_UNUSED)59 xmlModuleOpen(const char *name, int options ATTRIBUTE_UNUSED)
60 {
61     xmlModulePtr module;
62 
63     module = (xmlModulePtr) xmlMalloc(sizeof(xmlModule));
64     if (module == NULL)
65         return (NULL);
66 
67     memset(module, 0, sizeof(xmlModule));
68 
69     module->handle = xmlModulePlatformOpen(name);
70 
71     if (module->handle == NULL) {
72         xmlFree(module);
73         return(NULL);
74     }
75 
76     module->name = xmlStrdup((const xmlChar *) name);
77     return (module);
78 }
79 
80 /**
81  * xmlModuleSymbol:
82  * @module: the module
83  * @name: the name of the symbol
84  * @symbol: the resulting symbol address
85  *
86  * Lookup for a symbol address in the given module
87  * NOTE: that due to portability issues, behaviour can only be
88  * guaranteed with @name using ASCII. We cannot guarantee that
89  * an UTF-8 string would work, which is why name is a const char *
90  * and not a const xmlChar * .
91  *
92  * Returns 0 if the symbol was found, or -1 in case of error
93  */
94 int
xmlModuleSymbol(xmlModulePtr module,const char * name,void ** symbol)95 xmlModuleSymbol(xmlModulePtr module, const char *name, void **symbol)
96 {
97     int rc = -1;
98 
99     if ((NULL == module) || (symbol == NULL) || (name == NULL))
100         return rc;
101 
102     rc = xmlModulePlatformSymbol(module->handle, name, symbol);
103 
104     if (rc == -1)
105         return rc;
106 
107     return rc;
108 }
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
xmlModuleClose(xmlModulePtr module)121 xmlModuleClose(xmlModulePtr module)
122 {
123     int rc;
124 
125     if (NULL == module)
126         return -1;
127 
128     rc = xmlModulePlatformClose(module->handle);
129 
130     if (rc != 0)
131         return -2;
132 
133     rc = xmlModuleFree(module);
134     return (rc);
135 }
136 
137 /**
138  * xmlModuleFree:
139  * @module: the module handle
140  *
141  * The free operations free the data associated to the module
142  * but does not unload the associated shared library which may still
143  * be in use.
144  *
145  * Returns 0 in case of success, -1 in case of argument error
146  */
147 int
xmlModuleFree(xmlModulePtr module)148 xmlModuleFree(xmlModulePtr module)
149 {
150     if (NULL == module)
151         return -1;
152 
153     xmlFree(module->name);
154     xmlFree(module);
155 
156     return (0);
157 }
158 
159 #if defined(HAVE_DLOPEN) && !defined(_WIN32)
160 #ifdef HAVE_DLFCN_H
161 #include <dlfcn.h>
162 #endif
163 
164 #ifndef RTLD_GLOBAL            /* For Tru64 UNIX 4.0 */
165 #define RTLD_GLOBAL 0
166 #endif
167 
168 /**
169  * xmlModulePlatformOpen:
170  * @name: path to the module
171  *
172  * returns a handle on success, and zero on error.
173  */
174 
175 static void *
xmlModulePlatformOpen(const char * name)176 xmlModulePlatformOpen(const char *name)
177 {
178     return dlopen(name, RTLD_GLOBAL | RTLD_NOW);
179 }
180 
181 /*
182  * xmlModulePlatformClose:
183  * @handle: handle to the module
184  *
185  * returns 0 on success, and non-zero on error.
186  */
187 
188 static int
xmlModulePlatformClose(void * handle)189 xmlModulePlatformClose(void *handle)
190 {
191     return dlclose(handle);
192 }
193 
194 /*
195  * xmlModulePlatformSymbol:
196  * http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html
197  * returns 0 on success and the loaded symbol in result, and -1 on error.
198  */
199 
200 static int
xmlModulePlatformSymbol(void * handle,const char * name,void ** symbol)201 xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
202 {
203     *symbol = dlsym(handle, name);
204     if (dlerror() != NULL) {
205 	return -1;
206     }
207     return 0;
208 }
209 
210 #else /* ! HAVE_DLOPEN */
211 
212 #ifdef HAVE_SHLLOAD             /* HAVE_SHLLOAD */
213 #ifdef HAVE_DL_H
214 #include <dl.h>
215 #endif
216 /*
217  * xmlModulePlatformOpen:
218  * returns a handle on success, and zero on error.
219  */
220 
221 static void *
xmlModulePlatformOpen(const char * name)222 xmlModulePlatformOpen(const char *name)
223 {
224     return shl_load(name, BIND_IMMEDIATE, 0L);
225 }
226 
227 /*
228  * xmlModulePlatformClose:
229  * returns 0 on success, and non-zero on error.
230  */
231 
232 static int
xmlModulePlatformClose(void * handle)233 xmlModulePlatformClose(void *handle)
234 {
235     return shl_unload(handle);
236 }
237 
238 /*
239  * xmlModulePlatformSymbol:
240  * http://docs.hp.com/en/B2355-90683/shl_load.3X.html
241  * returns 0 on success and the loaded symbol in result, and -1 on error.
242  */
243 
244 static int
xmlModulePlatformSymbol(void * handle,const char * name,void ** symbol)245 xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
246 {
247     int rc;
248 
249     errno = 0;
250     rc = shl_findsym(&handle, name, TYPE_UNDEFINED, symbol);
251     return rc;
252 }
253 
254 #endif /* HAVE_SHLLOAD */
255 #endif /* ! HAVE_DLOPEN */
256 
257 #if defined(_WIN32)
258 
259 #define WIN32_LEAN_AND_MEAN
260 #include <windows.h>
261 
262 /*
263  * xmlModulePlatformOpen:
264  * returns a handle on success, and zero on error.
265  */
266 
267 static void *
xmlModulePlatformOpen(const char * name)268 xmlModulePlatformOpen(const char *name)
269 {
270     return LoadLibraryA(name);
271 }
272 
273 /*
274  * xmlModulePlatformClose:
275  * returns 0 on success, and non-zero on error.
276  */
277 
278 static int
xmlModulePlatformClose(void * handle)279 xmlModulePlatformClose(void *handle)
280 {
281     int rc;
282 
283     rc = FreeLibrary(handle);
284     return (0 == rc);
285 }
286 
287 /*
288  * xmlModulePlatformSymbol:
289  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp
290  * returns 0 on success and the loaded symbol in result, and -1 on error.
291  */
292 
293 static int
xmlModulePlatformSymbol(void * handle,const char * name,void ** symbol)294 xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
295 {
296 XML_IGNORE_FPTR_CAST_WARNINGS
297     *symbol = GetProcAddress(handle, name);
298     return (NULL == *symbol) ? -1 : 0;
299 XML_POP_WARNINGS
300 }
301 
302 #endif /* _WIN32 */
303 
304 #endif /* LIBXML_MODULES_ENABLED */
305