Blame xmlmodule.c

Packit 423ecb
/*
Packit 423ecb
 * xmlmodule.c : basic API for dynamic module loading added 2.6.17
Packit 423ecb
 *
Packit 423ecb
 * See Copyright for the status of this software.
Packit 423ecb
 *
Packit 423ecb
 * joelwreed@comcast.net
Packit 423ecb
 *
Packit 423ecb
 * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
/* In order RTLD_GLOBAL and RTLD_NOW to be defined on zOS */
Packit 423ecb
#if defined(__MVS__)
Packit 423ecb
#define _UNIX03_SOURCE
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#define IN_LIBXML
Packit 423ecb
#include "libxml.h"
Packit 423ecb
Packit 423ecb
#include <string.h>
Packit 423ecb
#include <libxml/xmlmemory.h>
Packit 423ecb
#include <libxml/xmlerror.h>
Packit 423ecb
#include <libxml/xmlmodule.h>
Packit 423ecb
#include <libxml/globals.h>
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_MODULES_ENABLED
Packit 423ecb
Packit 423ecb
struct _xmlModule {
Packit 423ecb
    unsigned char *name;
Packit 423ecb
    void *handle;
Packit 423ecb
};
Packit 423ecb
Packit 423ecb
static void *xmlModulePlatformOpen(const char *name);
Packit 423ecb
static int xmlModulePlatformClose(void *handle);
Packit 423ecb
static int xmlModulePlatformSymbol(void *handle, const char *name, void **result);
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		module memory error handler				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlModuleErrMemory:
Packit 423ecb
 * @extra:  extra information
Packit 423ecb
 *
Packit 423ecb
 * Handle an out of memory condition
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlModuleErrMemory(xmlModulePtr module, const char *extra)
Packit 423ecb
{
Packit 423ecb
    const char *name = NULL;
Packit 423ecb
Packit 423ecb
    if (module != NULL) {
Packit 423ecb
        name = (const char *) module->name;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
Packit 423ecb
                    XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
Packit 423ecb
                    name, NULL, 0, 0,
Packit 423ecb
                    "Memory allocation failed : %s\n", extra);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlModuleOpen:
Packit 423ecb
 * @name: the module name
Packit 423ecb
 * @options: a set of xmlModuleOption
Packit 423ecb
 *
Packit 423ecb
 * Opens a module/shared library given its name or path
Packit 423ecb
 * NOTE: that due to portability issues, behaviour can only be
Packit 423ecb
 * guaranteed with @name using ASCII. We canot guarantee that
Packit 423ecb
 * an UTF-8 string would work, which is why name is a const char *
Packit 423ecb
 * and not a const xmlChar * .
Packit 423ecb
 * TODO: options are not yet implemented.
Packit 423ecb
 *
Packit 423ecb
 * Returns a handle for the module or NULL in case of error
Packit 423ecb
 */
Packit 423ecb
xmlModulePtr
Packit 423ecb
xmlModuleOpen(const char *name, int options ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlModulePtr module;
Packit 423ecb
Packit 423ecb
    module = (xmlModulePtr) xmlMalloc(sizeof(xmlModule));
Packit 423ecb
    if (module == NULL) {
Packit 423ecb
        xmlModuleErrMemory(NULL, "creating module");
Packit 423ecb
        return (NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    memset(module, 0, sizeof(xmlModule));
Packit 423ecb
Packit 423ecb
    module->handle = xmlModulePlatformOpen(name);
Packit 423ecb
Packit 423ecb
    if (module->handle == NULL) {
Packit 423ecb
        xmlFree(module);
Packit 423ecb
        __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
Packit 423ecb
                        XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
Packit 423ecb
                        name, NULL, 0, 0, "failed to open %s\n", name);
Packit 423ecb
        return(NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    module->name = xmlStrdup((const xmlChar *) name);
Packit 423ecb
    return (module);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlModuleSymbol:
Packit 423ecb
 * @module: the module
Packit 423ecb
 * @name: the name of the symbol
Packit 423ecb
 * @symbol: the resulting symbol address
Packit 423ecb
 *
Packit 423ecb
 * Lookup for a symbol address in the given module
Packit 423ecb
 * NOTE: that due to portability issues, behaviour can only be
Packit 423ecb
 * guaranteed with @name using ASCII. We canot guarantee that
Packit 423ecb
 * an UTF-8 string would work, which is why name is a const char *
Packit 423ecb
 * and not a const xmlChar * .
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if the symbol was found, or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlModuleSymbol(xmlModulePtr module, const char *name, void **symbol)
Packit 423ecb
{
Packit 423ecb
    int rc = -1;
Packit 423ecb
Packit 423ecb
    if ((NULL == module) || (symbol == NULL) || (name == NULL)) {
Packit 423ecb
        __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
Packit 423ecb
                        XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
Packit 423ecb
                        NULL, NULL, 0, 0, "null parameter\n");
Packit 423ecb
        return rc;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    rc = xmlModulePlatformSymbol(module->handle, name, symbol);
Packit 423ecb
Packit 423ecb
    if (rc == -1) {
Packit 423ecb
        __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
Packit 423ecb
                        XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
Packit 423ecb
                        name, NULL, 0, 0,
Packit 423ecb
                        "failed to find symbol: %s\n",
Packit 423ecb
			(name == NULL ? "NULL" : name));
Packit 423ecb
        return rc;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    return rc;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlModuleClose:
Packit 423ecb
 * @module: the module handle
Packit 423ecb
 *
Packit 423ecb
 * The close operations unload the associated module and free the
Packit 423ecb
 * data associated to the module.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, -1 in case of argument error and -2
Packit 423ecb
 *         if the module could not be closed/unloaded.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlModuleClose(xmlModulePtr module)
Packit 423ecb
{
Packit 423ecb
    int rc;
Packit 423ecb
Packit 423ecb
    if (NULL == module) {
Packit 423ecb
        __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
Packit 423ecb
                        XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, 0,
Packit 423ecb
                        NULL, NULL, 0, 0, "null module pointer\n");
Packit 423ecb
        return -1;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    rc = xmlModulePlatformClose(module->handle);
Packit 423ecb
Packit 423ecb
    if (rc != 0) {
Packit 423ecb
        __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
Packit 423ecb
                        XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, 0,
Packit 423ecb
                        (const char *) module->name, NULL, 0, 0,
Packit 423ecb
                        "failed to close: %s\n", module->name);
Packit 423ecb
        return -2;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    rc = xmlModuleFree(module);
Packit 423ecb
    return (rc);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlModuleFree:
Packit 423ecb
 * @module: the module handle
Packit 423ecb
 *
Packit 423ecb
 * The free operations free the data associated to the module
Packit 423ecb
 * but does not unload the associated shared library which may still
Packit 423ecb
 * be in use.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, -1 in case of argument error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlModuleFree(xmlModulePtr module)
Packit 423ecb
{
Packit 423ecb
    if (NULL == module) {
Packit 423ecb
        __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
Packit 423ecb
                        XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, NULL,
Packit 423ecb
                        NULL, NULL, 0, 0, "null module pointer\n");
Packit 423ecb
        return -1;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    xmlFree(module->name);
Packit 423ecb
    xmlFree(module);
Packit 423ecb
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#if defined(HAVE_DLOPEN) && !defined(_WIN32)
Packit 423ecb
#ifdef HAVE_DLFCN_H
Packit 423ecb
#include <dlfcn.h>
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#ifndef RTLD_GLOBAL            /* For Tru64 UNIX 4.0 */
Packit 423ecb
#define RTLD_GLOBAL 0
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlModulePlatformOpen:
Packit 423ecb
 * @name: path to the module
Packit 423ecb
 *
Packit 423ecb
 * returns a handle on success, and zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static void *
Packit 423ecb
xmlModulePlatformOpen(const char *name)
Packit 423ecb
{
Packit 423ecb
    return dlopen(name, RTLD_GLOBAL | RTLD_NOW);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformClose:
Packit 423ecb
 * @handle: handle to the module
Packit 423ecb
 *
Packit 423ecb
 * returns 0 on success, and non-zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformClose(void *handle)
Packit 423ecb
{
Packit 423ecb
    return dlclose(handle);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformSymbol:
Packit 423ecb
 * http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html
Packit 423ecb
 * returns 0 on success and the loaded symbol in result, and -1 on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
Packit 423ecb
{
Packit 423ecb
    *symbol = dlsym(handle, name);
Packit 423ecb
    if (dlerror() != NULL) {
Packit 423ecb
	return -1;
Packit 423ecb
    }
Packit 423ecb
    return 0;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#else /* ! HAVE_DLOPEN */
Packit 423ecb
Packit 423ecb
#ifdef HAVE_SHLLOAD             /* HAVE_SHLLOAD */
Packit 423ecb
#ifdef HAVE_DL_H
Packit 423ecb
#include <dl.h>
Packit 423ecb
#endif
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformOpen:
Packit 423ecb
 * returns a handle on success, and zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static void *
Packit 423ecb
xmlModulePlatformOpen(const char *name)
Packit 423ecb
{
Packit 423ecb
    return shl_load(name, BIND_IMMEDIATE, 0L);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformClose:
Packit 423ecb
 * returns 0 on success, and non-zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformClose(void *handle)
Packit 423ecb
{
Packit 423ecb
    return shl_unload(handle);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformSymbol:
Packit 423ecb
 * http://docs.hp.com/en/B2355-90683/shl_load.3X.html
Packit 423ecb
 * returns 0 on success and the loaded symbol in result, and -1 on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
Packit 423ecb
{
Packit 423ecb
    int rc;
Packit 423ecb
Packit 423ecb
    errno = 0;
Packit 423ecb
    rc = shl_findsym(&handle, name, TYPE_UNDEFINED, symbol);
Packit 423ecb
    return rc;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#endif /* HAVE_SHLLOAD */
Packit 423ecb
#endif /* ! HAVE_DLOPEN */
Packit 423ecb
Packit 423ecb
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit 423ecb
Packit 423ecb
#define WIN32_LEAN_AND_MEAN
Packit 423ecb
#include <windows.h>
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformOpen:
Packit 423ecb
 * returns a handle on success, and zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static void *
Packit 423ecb
xmlModulePlatformOpen(const char *name)
Packit 423ecb
{
Packit 423ecb
    return LoadLibraryA(name);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformClose:
Packit 423ecb
 * returns 0 on success, and non-zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformClose(void *handle)
Packit 423ecb
{
Packit 423ecb
    int rc;
Packit 423ecb
Packit 423ecb
    rc = FreeLibrary(handle);
Packit 423ecb
    return (0 == rc);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformSymbol:
Packit 423ecb
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp
Packit 423ecb
 * returns 0 on success and the loaded symbol in result, and -1 on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
Packit 423ecb
{
Packit 423ecb
#ifdef _WIN32_WCE
Packit 423ecb
    /*
Packit 423ecb
     * GetProcAddressA seems only available on WinCE
Packit 423ecb
     */
Packit 423ecb
    *symbol = GetProcAddressA(handle, name);
Packit 423ecb
#else
Packit 423ecb
    *symbol = GetProcAddress(handle, name);
Packit 423ecb
#endif
Packit 423ecb
    return (NULL == *symbol) ? -1 : 0;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#endif /* _WIN32 */
Packit 423ecb
Packit 423ecb
#ifdef HAVE_BEOS
Packit 423ecb
Packit 423ecb
#include <kernel/image.h>
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformOpen:
Packit 423ecb
 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
Packit 423ecb
 * returns a handle on success, and zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static void *
Packit 423ecb
xmlModulePlatformOpen(const char *name)
Packit 423ecb
{
Packit 423ecb
    return (void *) load_add_on(name);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformClose:
Packit 423ecb
 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
Packit 423ecb
 * returns 0 on success, and non-zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformClose(void *handle)
Packit 423ecb
{
Packit 423ecb
    status_t rc;
Packit 423ecb
Packit 423ecb
    rc = unload_add_on((image_id) handle);
Packit 423ecb
Packit 423ecb
    if (rc == B_OK)
Packit 423ecb
        return 0;
Packit 423ecb
    else
Packit 423ecb
        return -1;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformSymbol:
Packit 423ecb
 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
Packit 423ecb
 * returns 0 on success and the loaded symbol in result, and -1 on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
Packit 423ecb
{
Packit 423ecb
    status_t rc;
Packit 423ecb
Packit 423ecb
    rc = get_image_symbol((image_id) handle, name, B_SYMBOL_TYPE_ANY, symbol);
Packit 423ecb
Packit 423ecb
    return (rc == B_OK) ? 0 : -1;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#endif /* HAVE_BEOS */
Packit 423ecb
Packit 423ecb
#ifdef HAVE_OS2
Packit 423ecb
Packit 423ecb
#include <os2.h>
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformOpen:
Packit 423ecb
 * os2 api info: http://www.edm2.com/os2api/Dos/DosLoadModule.html
Packit 423ecb
 * returns a handle on success, and zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static void *
Packit 423ecb
xmlModulePlatformOpen(const char *name)
Packit 423ecb
{
Packit 423ecb
    char errbuf[256];
Packit 423ecb
    void *handle;
Packit 423ecb
    int rc;
Packit 423ecb
Packit 423ecb
    rc = DosLoadModule(errbuf, sizeof(errbuf) - 1, name, &handle);
Packit 423ecb
Packit 423ecb
    if (rc)
Packit 423ecb
        return 0;
Packit 423ecb
    else
Packit 423ecb
        return (handle);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformClose:
Packit 423ecb
 * os2 api info: http://www.edm2.com/os2api/Dos/DosFreeModule.html
Packit 423ecb
 * returns 0 on success, and non-zero on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformClose(void *handle)
Packit 423ecb
{
Packit 423ecb
    return DosFreeModule(handle);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlModulePlatformSymbol:
Packit 423ecb
 * os2 api info: http://www.edm2.com/os2api/Dos/DosQueryProcAddr.html
Packit 423ecb
 * returns 0 on success and the loaded symbol in result, and -1 on error.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
Packit 423ecb
{
Packit 423ecb
    int rc;
Packit 423ecb
Packit 423ecb
    rc = DosQueryProcAddr(handle, 0, name, symbol);
Packit 423ecb
Packit 423ecb
    return (rc == NO_ERROR) ? 0 : -1;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#endif /* HAVE_OS2 */
Packit 423ecb
Packit 423ecb
#define bottom_xmlmodule
Packit 423ecb
#include "elfgcchack.h"
Packit 423ecb
#endif /* LIBXML_MODULES_ENABLED */