VBoxCAPIGlue.c revision 0d625252f8e48f8ec3e2f5a0d30ced35b886fb75
/* $Revision$ */
/** @file
* Glue code for dynamically linking to VBoxCAPI.
*/
/*
* Copyright (C) 2008-2015 Oracle Corporation
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "VBoxCAPIGlue.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#ifndef WIN32
# include <dlfcn.h>
# include <pthread.h>
#else /* WIN32 */
# include <Windows.h>
#endif /* WIN32 */
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
# define DYNLIB_NAME "VBoxXPCOMC.so"
# define DYNLIB_NAME "VBoxXPCOMC.dylib"
# define DYNLIB_NAME "VBoxXPCOMC.dll"
# define DYNLIB_NAME "VBoxCAPI.dll"
#else
# error "Port me"
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifndef WIN32
void *g_hVBoxCAPI = NULL;
#else /* WIN32 */
#endif /* WIN32 */
/** The last load error. */
/** Pointer to the VBOXCAPI function table. */
typedef void FNDUMMY(void);
/** Just a dummy global structure containing a bunch of
* function pointers to code which is wanted in the link. */
{
#ifndef WIN32
/* The following link dependency is for helping gdb as it gets hideously
* confused if the application doesn't drag in pthreads, but uses it. */
#endif /* !WIN32 */
};
/**
* Wrapper for setting g_szVBoxErrMsg. Can be an empty stub.
*
* @param fAlways When 0 the g_szVBoxErrMsg is only set if empty.
* @param pszFormat The format string.
* @param ... The arguments.
*/
{
if ( fAlways
|| !g_szVBoxErrMsg[0])
{
}
}
/**
* the symbols we need. Tries both the new style and legacy name.
*
* @returns 0 on success, -1 on failure.
* @param pszHome The directory where to try load VBoxCAPI/VBoxXPCOMC
* from. Can be NULL.
* @param fSetAppHome Whether to set the VBOX_APP_HOME env.var. or not
* (boolean).
*/
{
char szName[4096];
/*
* Construct the full name.
*/
if (cbBufNeeded > sizeof(szName))
{
(unsigned)cbBufNeeded);
return -1;
}
if (cchHome)
{
cchHome++;
}
/*
* Try load it by that name, setting the VBOX_APP_HOME first (for now).
* Then resolve and call the function table getter.
*/
if (fSetAppHome)
{
#ifndef WIN32
if (pszHome)
else
unsetenv("VBOX_APP_HOME");
#endif /* !WIN32 */
}
#ifndef WIN32
#else /* WIN32 */
#endif /* WIN32 */
if (g_hVBoxCAPI)
{
#ifndef WIN32
if (!pfnGetFunctions)
# endif /* VBOX_GET_XPCOM_FUNCTIONS_SYMBOL_NAME */
#else /* WIN32 */
#endif /* WIN32 */
if (pfnGetFunctions)
{
if (g_pVBoxFuncs)
{
{
return 0;
}
g_pVBoxFuncs = NULL;
}
else
{
/* bail out */
}
}
else
{
#ifndef WIN32
#else /* WIN32 */
#endif /* WIN32 */
}
#ifndef WIN32
#else /* WIN32 */
#endif /* WIN32 */
g_hVBoxCAPI = NULL;
}
else
{
#ifndef WIN32
#else /* WIN32 */
#endif /* WIN32 */
}
return -1;
}
/**
* Tries to locate and load VBoxCAPI.so/dylib/dll, resolving all the related
* function pointers.
*
* @returns 0 on success, -1 on failure.
*
* @remark This should be considered moved into a separate glue library since
* its its going to be pretty much the same for any user of VBoxCAPI
* and it will just cause trouble to have duplicate versions of this
* source code all around the place.
*/
int VBoxCGlueInit(void)
{
const char *pszHome;
/*
* If the user specifies the location, try only that.
*/
if (pszHome)
return tryLoadLibrary(pszHome, 0);
/*
* Try the known standard locations.
*/
#if defined(__gnu__linux__) || defined(__linux__)
return 0;
return 0;
return 0;
return 0;
return 0;
#elif defined(__FreeBSD__)
return 0;
return 0;
if (pszHome)
{
char szPath[4096];
return 0;
}
return 0;
#else
# error "port me"
#endif
/*
* Finally try the dynamic linker search path.
*/
return 0;
/* No luck, return failure. */
return -1;
}
/**
* Terminate the C glue library.
*/
void VBoxCGlueTerm(void)
{
if (g_hVBoxCAPI)
{
#if 0 /* VBoxRT.so doesn't like being reloaded. See @bugref{3725}. */
#ifndef WIN32
#else
#endif
#endif
g_hVBoxCAPI = NULL;
}
g_pVBoxFuncs = NULL;
}