2009-04-17 16:09:07 +00:00
|
|
|
/** @file vbox_XPCOMCGlue.c
|
|
|
|
* Glue code for dynamically linking to VBoxXPCOMC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of a free software library; you can redistribute
|
|
|
|
* it and/or modify it under the terms of the GNU Lesser General
|
|
|
|
* Public License version 2.1 as published by the Free Software
|
2013-05-15 20:30:23 +00:00
|
|
|
* Foundation and shipped in the "COPYING.LESSER" file with this library.
|
2009-04-17 16:09:07 +00:00
|
|
|
* The library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY of any kind.
|
|
|
|
*
|
|
|
|
* Sun LGPL Disclaimer: For the avoidance of doubt, except that if
|
|
|
|
* any license choice other than GPL or LGPL is available it will
|
|
|
|
* apply instead, Sun elects to use only the Lesser General Public
|
|
|
|
* License version 2.1 (LGPLv2) at this time for any software where
|
|
|
|
* a choice of LGPL license versions is made available with the
|
|
|
|
* language indicating that LGPLv2 or any later version may be used,
|
|
|
|
* or where a choice of which version of the LGPL is applied is
|
|
|
|
* otherwise unspecified.
|
|
|
|
*
|
|
|
|
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
|
|
|
* Clara, CA 95054 USA or visit http://www.sun.com if you need
|
|
|
|
* additional information or have any questions.
|
|
|
|
*/
|
|
|
|
|
2010-06-29 18:01:45 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
2009-04-17 16:09:07 +00:00
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
#include "vbox_XPCOMCGlue.h"
|
2010-10-22 19:25:03 +00:00
|
|
|
#include "internal.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "virfile.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2010-10-22 19:25:03 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_VBOX
|
2009-04-17 16:09:07 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("vbox.vbox_XPCOMCGlue");
|
2009-04-17 16:09:07 +00:00
|
|
|
|
2013-05-30 20:07:46 +00:00
|
|
|
#if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__) || \
|
|
|
|
defined(__FreeBSD__) || defined(__OpenBSD__) || \
|
|
|
|
defined(__FreeBSD_kernel__)
|
2016-02-09 11:05:02 +00:00
|
|
|
# define DYNLIB_NAME "VBoxXPCOMC.so"
|
2009-04-17 16:09:07 +00:00
|
|
|
#elif defined(__APPLE__)
|
2016-02-09 11:05:02 +00:00
|
|
|
# define DYNLIB_NAME "VBoxXPCOMC.dylib"
|
2009-04-17 16:09:07 +00:00
|
|
|
#elif defined(_MSC_VER) || defined(__OS2__)
|
2016-02-09 11:05:02 +00:00
|
|
|
# define DYNLIB_NAME "VBoxXPCOMC.dll"
|
2009-04-17 16:09:07 +00:00
|
|
|
#else
|
|
|
|
# error "Port me"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/** The dlopen handle for VBoxXPCOMC. */
|
2014-10-28 18:38:04 +00:00
|
|
|
static void *hVBoxXPCOMC;
|
2009-05-06 13:23:44 +00:00
|
|
|
/** Pointer to the VBoxXPCOMC function table. */
|
2014-10-28 18:38:04 +00:00
|
|
|
static PCVBOXXPCOM pVBoxFuncs_v2_2;
|
2009-04-17 16:09:07 +00:00
|
|
|
/** Pointer to VBoxGetXPCOMCFunctions for the loaded VBoxXPCOMC so/dylib/dll. */
|
|
|
|
PFNVBOXGETXPCOMCFUNCTIONS g_pfnGetFunctions = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try load VBoxXPCOMC.so/dylib/dll from the specified location and resolve all
|
|
|
|
* the symbols we need.
|
|
|
|
*
|
2010-10-22 19:25:03 +00:00
|
|
|
* @returns 0 on success, -1 on failure and 1 if VBoxXPCOMC was not found.
|
|
|
|
* @param dir The directory where to try load VBoxXPCOMC from. Can
|
|
|
|
* be NULL.
|
|
|
|
* @param setAppHome Whether to set the VBOX_APP_HOME env.var. or not.
|
|
|
|
* @param ignoreMissing Whether to ignore missing library or not.
|
2010-12-14 22:07:57 +00:00
|
|
|
* @param version Version number of the loaded API.
|
2009-04-17 16:09:07 +00:00
|
|
|
*/
|
2010-12-14 22:07:57 +00:00
|
|
|
static int
|
|
|
|
tryLoadOne(const char *dir, bool setAppHome, bool ignoreMissing,
|
|
|
|
unsigned int *version)
|
2009-04-17 16:09:07 +00:00
|
|
|
{
|
2010-10-22 19:25:03 +00:00
|
|
|
int result = -1;
|
|
|
|
char *name = NULL;
|
|
|
|
PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;
|
|
|
|
|
|
|
|
if (dir != NULL) {
|
2013-07-04 10:17:38 +00:00
|
|
|
if (virAsprintf(&name, "%s/%s", dir, DYNLIB_NAME) < 0)
|
2010-10-22 19:25:03 +00:00
|
|
|
return -1;
|
2009-04-17 16:09:07 +00:00
|
|
|
|
2010-10-22 19:25:03 +00:00
|
|
|
if (!virFileExists(name)) {
|
2014-11-13 14:21:37 +00:00
|
|
|
if (!ignoreMissing)
|
2011-06-04 20:40:57 +00:00
|
|
|
VIR_ERROR(_("Library '%s' doesn't exist"), name);
|
2010-10-22 19:25:03 +00:00
|
|
|
|
2010-11-24 11:49:40 +00:00
|
|
|
VIR_FREE(name);
|
2010-10-22 19:25:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-20 11:49:46 +00:00
|
|
|
name = g_strdup(DYNLIB_NAME);
|
2009-04-17 16:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try load it by that name, setting the VBOX_APP_HOME first (for now).
|
|
|
|
* Then resolve and call the function table getter.
|
|
|
|
*/
|
2010-10-22 19:25:03 +00:00
|
|
|
if (setAppHome) {
|
|
|
|
if (dir != NULL) {
|
|
|
|
setenv("VBOX_APP_HOME", dir, 1 /* always override */);
|
|
|
|
} else {
|
2009-04-17 16:09:07 +00:00
|
|
|
unsetenv("VBOX_APP_HOME");
|
2010-10-22 19:25:03 +00:00
|
|
|
}
|
2009-04-17 16:09:07 +00:00
|
|
|
}
|
2009-05-06 13:23:44 +00:00
|
|
|
|
2010-12-14 22:07:57 +00:00
|
|
|
hVBoxXPCOMC = dlopen(name, RTLD_NOW | RTLD_LOCAL);
|
2010-10-22 19:25:03 +00:00
|
|
|
|
2010-12-14 22:07:57 +00:00
|
|
|
if (hVBoxXPCOMC == NULL) {
|
2010-10-29 14:31:29 +00:00
|
|
|
/*
|
|
|
|
* FIXME: Don't warn in this case as it currently breaks make check
|
|
|
|
* on systems without VirtualBox.
|
|
|
|
*/
|
2014-11-13 14:21:37 +00:00
|
|
|
if (dir != NULL)
|
2010-10-29 14:31:29 +00:00
|
|
|
VIR_WARN("Could not dlopen '%s': %s", name, dlerror());
|
|
|
|
|
2010-10-22 19:25:03 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfnGetFunctions = (PFNVBOXGETXPCOMCFUNCTIONS)
|
2010-12-14 22:07:57 +00:00
|
|
|
dlsym(hVBoxXPCOMC, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME);
|
2010-10-22 19:25:03 +00:00
|
|
|
|
|
|
|
if (pfnGetFunctions == NULL) {
|
|
|
|
VIR_ERROR(_("Could not dlsym %s from '%s': %s"),
|
|
|
|
VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, name, dlerror());
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-12-14 22:07:57 +00:00
|
|
|
pVBoxFuncs_v2_2 = pfnGetFunctions(VBOX_XPCOMC_VERSION);
|
2010-10-22 19:25:03 +00:00
|
|
|
|
2010-12-14 22:07:57 +00:00
|
|
|
if (pVBoxFuncs_v2_2 == NULL) {
|
2010-10-22 19:25:03 +00:00
|
|
|
VIR_ERROR(_("Calling %s from '%s' failed"),
|
|
|
|
VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-12-14 22:07:57 +00:00
|
|
|
*version = pVBoxFuncs_v2_2->pfnGetVersion();
|
2010-10-22 19:25:03 +00:00
|
|
|
g_pfnGetFunctions = pfnGetFunctions;
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
if (dir != NULL) {
|
|
|
|
VIR_DEBUG("Found %s in '%s'", DYNLIB_NAME, dir);
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Found %s in dynamic linker search path", DYNLIB_NAME);
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:54:41 +00:00
|
|
|
cleanup:
|
2010-12-14 22:07:57 +00:00
|
|
|
if (hVBoxXPCOMC != NULL && result < 0) {
|
|
|
|
dlclose(hVBoxXPCOMC);
|
|
|
|
hVBoxXPCOMC = NULL;
|
2009-04-17 16:09:07 +00:00
|
|
|
}
|
2010-10-22 19:25:03 +00:00
|
|
|
|
|
|
|
VIR_FREE(name);
|
|
|
|
|
|
|
|
return result;
|
2009-04-17 16:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to locate and load VBoxXPCOMC.so/dylib/dll, resolving all the related
|
|
|
|
* function pointers.
|
|
|
|
*
|
|
|
|
* @returns 0 on success, -1 on failure.
|
|
|
|
*/
|
2010-12-14 22:07:57 +00:00
|
|
|
int
|
|
|
|
VBoxCGlueInit(unsigned int *version)
|
2009-04-17 16:09:07 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/vbox/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2010-10-22 19:25:03 +00:00
|
|
|
static const char *knownDirs[] = {
|
|
|
|
"/usr/lib/virtualbox",
|
|
|
|
"/usr/lib/virtualbox-ose",
|
|
|
|
"/usr/lib64/virtualbox",
|
|
|
|
"/usr/lib64/virtualbox-ose",
|
|
|
|
"/usr/lib/VirtualBox",
|
|
|
|
"/opt/virtualbox",
|
|
|
|
"/opt/VirtualBox",
|
|
|
|
"/opt/virtualbox/i386",
|
|
|
|
"/opt/VirtualBox/i386",
|
|
|
|
"/opt/virtualbox/amd64",
|
|
|
|
"/opt/VirtualBox/amd64",
|
|
|
|
"/usr/local/lib/virtualbox",
|
|
|
|
"/usr/local/lib/VirtualBox",
|
|
|
|
"/Applications/VirtualBox.app/Contents/MacOS"
|
|
|
|
};
|
2019-08-01 12:35:56 +00:00
|
|
|
const char *home = getenv("VBOX_APP_HOME");
|
2010-10-22 19:25:03 +00:00
|
|
|
|
|
|
|
/* If the user specifies the location, try only that. */
|
|
|
|
if (home != NULL) {
|
2014-11-13 14:21:37 +00:00
|
|
|
if (tryLoadOne(home, false, false, version) < 0)
|
2010-10-22 19:25:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-04-17 16:09:07 +00:00
|
|
|
|
2010-10-22 19:25:03 +00:00
|
|
|
/* Try the additionally configured location. */
|
|
|
|
if (VBOX_XPCOMC_DIR[0] != '\0') {
|
2014-11-13 14:21:37 +00:00
|
|
|
if (tryLoadOne(VBOX_XPCOMC_DIR, true, true, version) >= 0)
|
2010-10-22 19:25:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-06-29 18:01:45 +00:00
|
|
|
|
2010-10-22 19:25:03 +00:00
|
|
|
/* Try the known locations. */
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(knownDirs); ++i) {
|
2014-11-13 14:21:37 +00:00
|
|
|
if (tryLoadOne(knownDirs[i], true, true, version) >= 0)
|
2010-10-22 19:25:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-04-17 16:09:07 +00:00
|
|
|
|
2010-10-22 19:25:03 +00:00
|
|
|
/* Finally try the dynamic linker search path. */
|
2014-11-13 14:21:37 +00:00
|
|
|
if (tryLoadOne(NULL, false, true, version) >= 0)
|
2009-04-17 16:09:07 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* No luck, return failure. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminate the C glue library.
|
|
|
|
*/
|
2010-12-14 22:07:57 +00:00
|
|
|
void
|
|
|
|
VBoxCGlueTerm(void)
|
2009-04-17 16:09:07 +00:00
|
|
|
{
|
2010-12-14 22:07:57 +00:00
|
|
|
if (hVBoxXPCOMC != NULL) {
|
2009-04-17 16:09:07 +00:00
|
|
|
#if 0 /* VBoxRT.so doesn't like being reloaded. See @bugref{3725}. */
|
|
|
|
dlclose(g_hVBoxXPCOMC);
|
|
|
|
#endif
|
2010-12-14 22:07:57 +00:00
|
|
|
hVBoxXPCOMC = NULL;
|
2009-04-17 16:09:07 +00:00
|
|
|
}
|
2010-10-22 19:25:03 +00:00
|
|
|
|
2010-12-14 22:07:57 +00:00
|
|
|
pVBoxFuncs_v2_2 = NULL;
|
2009-04-17 16:09:07 +00:00
|
|
|
g_pfnGetFunctions = NULL;
|
|
|
|
}
|
2010-12-16 22:05:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In XPCOM an array is represented by 1) a pointer to an array of pointers
|
|
|
|
* that point to the items and 2) an unsigned int representing the number of
|
|
|
|
* items in the array. When the items aren't needed anymore they are released
|
|
|
|
* or freed according to their type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef nsresult (*ArrayGetter)(void *self, PRUint32 *count, void ***items);
|
2010-12-27 22:35:30 +00:00
|
|
|
typedef nsresult (*ArrayGetterWithPtrArg)(void *self, void *arg, PRUint32 *count, void ***items);
|
|
|
|
typedef nsresult (*ArrayGetterWithUintArg)(void *self, PRUint32 arg, PRUint32 *count, void ***items);
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
vboxArrayGetHelper(vboxArray *array, nsresult nsrc, void **items, PRUint32 count)
|
|
|
|
{
|
|
|
|
array->items = NULL;
|
|
|
|
array->count = 0;
|
|
|
|
|
2014-11-13 14:21:37 +00:00
|
|
|
if (NS_FAILED(nsrc))
|
2010-12-27 22:35:30 +00:00
|
|
|
return nsrc;
|
|
|
|
|
|
|
|
array->items = items;
|
|
|
|
array->count = count;
|
|
|
|
|
|
|
|
return nsrc;
|
|
|
|
}
|
2010-12-16 22:05:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Call the getter with self as first argument and fill the array with the
|
|
|
|
* returned items.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
vboxArrayGet(vboxArray *array, void *self, void *getter)
|
|
|
|
{
|
|
|
|
nsresult nsrc;
|
|
|
|
void **items = NULL;
|
|
|
|
PRUint32 count = 0;
|
|
|
|
|
|
|
|
nsrc = ((ArrayGetter)getter)(self, &count, &items);
|
|
|
|
|
2010-12-27 22:35:30 +00:00
|
|
|
return vboxArrayGetHelper(array, nsrc, items, count);
|
2010-12-16 22:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call the getter with self as first argument and arg as second argument
|
|
|
|
* and fill the array with the returned items.
|
|
|
|
*/
|
|
|
|
nsresult
|
2010-12-27 22:35:30 +00:00
|
|
|
vboxArrayGetWithPtrArg(vboxArray *array, void *self, void *getter, void *arg)
|
2010-12-16 22:05:48 +00:00
|
|
|
{
|
|
|
|
nsresult nsrc;
|
|
|
|
void **items = NULL;
|
|
|
|
PRUint32 count = 0;
|
|
|
|
|
2010-12-27 22:35:30 +00:00
|
|
|
nsrc = ((ArrayGetterWithPtrArg)getter)(self, arg, &count, &items);
|
2010-12-16 22:05:48 +00:00
|
|
|
|
2010-12-27 22:35:30 +00:00
|
|
|
return vboxArrayGetHelper(array, nsrc, items, count);
|
|
|
|
}
|
2010-12-16 22:05:48 +00:00
|
|
|
|
2010-12-27 22:35:30 +00:00
|
|
|
/*
|
|
|
|
* Call the getter with self as first argument and arg as second argument
|
|
|
|
* and fill the array with the returned items.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
vboxArrayGetWithUintArg(vboxArray *array, void *self, void *getter, PRUint32 arg)
|
|
|
|
{
|
|
|
|
nsresult nsrc;
|
|
|
|
void **items = NULL;
|
|
|
|
PRUint32 count = 0;
|
2010-12-16 22:05:48 +00:00
|
|
|
|
2010-12-27 22:35:30 +00:00
|
|
|
nsrc = ((ArrayGetterWithUintArg)getter)(self, arg, &count, &items);
|
2010-12-16 22:05:48 +00:00
|
|
|
|
2010-12-27 22:35:30 +00:00
|
|
|
return vboxArrayGetHelper(array, nsrc, items, count);
|
2010-12-16 22:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release all items in the array and reset it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vboxArrayRelease(vboxArray *array)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/vbox/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2010-12-16 22:05:48 +00:00
|
|
|
nsISupports *supports;
|
|
|
|
|
2014-11-13 14:21:37 +00:00
|
|
|
if (array->items == NULL)
|
2010-12-16 22:05:48 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < array->count; ++i) {
|
|
|
|
supports = array->items[i];
|
|
|
|
|
2014-11-13 14:21:37 +00:00
|
|
|
if (supports != NULL)
|
2010-12-16 22:05:48 +00:00
|
|
|
supports->vtbl->Release(supports);
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:15:40 +00:00
|
|
|
pVBoxFuncs_v2_2->pfnComUnallocMem(array->items);
|
|
|
|
|
2010-12-16 22:05:48 +00:00
|
|
|
array->items = NULL;
|
|
|
|
array->count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unalloc all items in the array and reset it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vboxArrayUnalloc(vboxArray *array)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/vbox/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2010-12-16 22:05:48 +00:00
|
|
|
void *item;
|
|
|
|
|
2014-11-13 14:21:37 +00:00
|
|
|
if (array->items == NULL)
|
2010-12-16 22:05:48 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < array->count; ++i) {
|
|
|
|
item = array->items[i];
|
|
|
|
|
2014-11-13 14:21:37 +00:00
|
|
|
if (item != NULL)
|
2010-12-16 22:05:48 +00:00
|
|
|
pVBoxFuncs_v2_2->pfnComUnallocMem(item);
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:15:40 +00:00
|
|
|
pVBoxFuncs_v2_2->pfnComUnallocMem(array->items);
|
|
|
|
|
2010-12-16 22:05:48 +00:00
|
|
|
array->items = NULL;
|
|
|
|
array->count = 0;
|
|
|
|
}
|