vbox_XPCOMCGlue.c 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/** @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
11
 * Foundation and shipped in the "COPYING.LESSER" file with this library.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * 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.
 */

/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
32 33

#include <config.h>
34 35 36 37 38

#include <stdlib.h>
#include <dlfcn.h>

#include "vbox_XPCOMCGlue.h"
39
#include "internal.h"
40
#include "viralloc.h"
41
#include "virutil.h"
42
#include "virlog.h"
43
#include "virerror.h"
44
#include "virfile.h"
45
#include "virstring.h"
46 47

#define VIR_FROM_THIS VIR_FROM_VBOX
48

49
VIR_LOG_INIT("vbox.vbox_XPCOMCGlue");
50 51 52 53

/*******************************************************************************
*   Defined Constants And Macros                                               *
*******************************************************************************/
54 55 56
#if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__) || \
    defined(__FreeBSD__) || defined(__OpenBSD__) || \
    defined(__FreeBSD_kernel__)
57 58 59 60 61 62 63 64 65 66 67 68 69 70
# define DYNLIB_NAME    "VBoxXPCOMC.so"
#elif defined(__APPLE__)
# define DYNLIB_NAME    "VBoxXPCOMC.dylib"
#elif defined(_MSC_VER) || defined(__OS2__)
# define DYNLIB_NAME    "VBoxXPCOMC.dll"
#else
# error "Port me"
#endif


/*******************************************************************************
*   Global Variables                                                           *
*******************************************************************************/
/** The dlopen handle for VBoxXPCOMC. */
71
static void *hVBoxXPCOMC;
72
/** Pointer to the VBoxXPCOMC function table. */
73
static PCVBOXXPCOM pVBoxFuncs_v2_2;
74 75 76 77 78 79 80 81
/** 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.
 *
82 83 84 85 86
 * @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.
87
 * @param   version       Version number of the loaded API.
88
 */
89 90 91
static int
tryLoadOne(const char *dir, bool setAppHome, bool ignoreMissing,
           unsigned int *version)
92
{
93 94 95 96 97
    int result = -1;
    char *name = NULL;
    PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;

    if (dir != NULL) {
98
        if (virAsprintf(&name, "%s/%s", dir, DYNLIB_NAME) < 0)
99
            return -1;
100

101
        if (!virFileExists(name)) {
102
            if (!ignoreMissing)
M
Matthias Bolte 已提交
103
                VIR_ERROR(_("Library '%s' doesn't exist"), name);
104

105
            VIR_FREE(name);
106 107 108
            return -1;
        }
    } else {
109
        if (VIR_STRDUP(name, DYNLIB_NAME) < 0)
110
            return -1;
111 112 113 114 115 116
    }

    /*
     * Try load it by that name, setting the VBOX_APP_HOME first (for now).
     * Then resolve and call the function table getter.
     */
117 118 119 120
    if (setAppHome) {
        if (dir != NULL) {
            setenv("VBOX_APP_HOME", dir, 1 /* always override */);
        } else {
121
            unsetenv("VBOX_APP_HOME");
122
        }
123
    }
124

125
    hVBoxXPCOMC = dlopen(name, RTLD_NOW | RTLD_LOCAL);
126

127
    if (hVBoxXPCOMC == NULL) {
128 129 130 131
        /*
         * FIXME: Don't warn in this case as it currently breaks make check
         *        on systems without VirtualBox.
         */
132
        if (dir != NULL)
133 134
            VIR_WARN("Could not dlopen '%s': %s", name, dlerror());

135 136 137 138
        goto cleanup;
    }

    pfnGetFunctions = (PFNVBOXGETXPCOMCFUNCTIONS)
139
        dlsym(hVBoxXPCOMC, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME);
140 141 142 143 144 145 146

    if (pfnGetFunctions == NULL) {
        VIR_ERROR(_("Could not dlsym %s from '%s': %s"),
                  VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, name, dlerror());
        goto cleanup;
    }

147
    pVBoxFuncs_v2_2 = pfnGetFunctions(VBOX_XPCOMC_VERSION);
148

149
    if (pVBoxFuncs_v2_2 == NULL) {
150 151 152 153 154
        VIR_ERROR(_("Calling %s from '%s' failed"),
                  VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, name);
        goto cleanup;
    }

155
    *version = pVBoxFuncs_v2_2->pfnGetVersion();
156 157 158 159 160 161 162 163 164
    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);
    }

165
 cleanup:
166 167 168
    if (hVBoxXPCOMC != NULL && result < 0) {
        dlclose(hVBoxXPCOMC);
        hVBoxXPCOMC = NULL;
169
    }
170 171 172 173

    VIR_FREE(name);

    return result;
174 175 176 177 178 179 180 181 182
}


/**
 * Tries to locate and load VBoxXPCOMC.so/dylib/dll, resolving all the related
 * function pointers.
 *
 * @returns 0 on success, -1 on failure.
 */
183 184
int
VBoxCGlueInit(unsigned int *version)
185
{
186
    size_t i;
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    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"
    };
203
    const char *home = virGetEnvBlockSUID("VBOX_APP_HOME");
204 205 206

    /* If the user specifies the location, try only that. */
    if (home != NULL) {
207
        if (tryLoadOne(home, false, false, version) < 0)
208 209
            return -1;
    }
210

211 212
    /* Try the additionally configured location. */
    if (VBOX_XPCOMC_DIR[0] != '\0') {
213
        if (tryLoadOne(VBOX_XPCOMC_DIR, true, true, version) >= 0)
214 215
            return 0;
    }
216

217 218
    /* Try the known locations. */
    for (i = 0; i < ARRAY_CARDINALITY(knownDirs); ++i) {
219
        if (tryLoadOne(knownDirs[i], true, true, version) >= 0)
220 221
            return 0;
    }
222

223
    /* Finally try the dynamic linker search path. */
224
    if (tryLoadOne(NULL, false, true, version) >= 0)
225 226 227 228 229 230 231 232 233 234
        return 0;

    /* No luck, return failure. */
    return -1;
}


/**
 * Terminate the C glue library.
 */
235 236
void
VBoxCGlueTerm(void)
237
{
238
    if (hVBoxXPCOMC != NULL) {
239 240 241
#if 0 /* VBoxRT.so doesn't like being reloaded. See @bugref{3725}. */
        dlclose(g_hVBoxXPCOMC);
#endif
242
        hVBoxXPCOMC = NULL;
243
    }
244

245
    pVBoxFuncs_v2_2 = NULL;
246 247
    g_pfnGetFunctions = NULL;
}
248 249 250 251 252 253 254 255 256 257 258



/*
 * 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);
259 260 261 262 263 264 265 266 267
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;

268
    if (NS_FAILED(nsrc))
269 270 271 272 273 274 275
        return nsrc;

    array->items = items;
    array->count = count;

    return nsrc;
}
276 277 278 279 280 281 282 283 284 285 286 287 288 289

/*
 * 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);

290
    return vboxArrayGetHelper(array, nsrc, items, count);
291 292 293 294 295 296 297
}

/*
 * Call the getter with self as first argument and arg as second argument
 * and fill the array with the returned items.
 */
nsresult
298
vboxArrayGetWithPtrArg(vboxArray *array, void *self, void *getter, void *arg)
299 300 301 302 303
{
    nsresult nsrc;
    void **items = NULL;
    PRUint32 count = 0;

304
    nsrc = ((ArrayGetterWithPtrArg)getter)(self, arg, &count, &items);
305

306 307
    return vboxArrayGetHelper(array, nsrc, items, count);
}
308

309 310 311 312 313 314 315 316 317 318
/*
 * 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;
319

320
    nsrc = ((ArrayGetterWithUintArg)getter)(self, arg, &count, &items);
321

322
    return vboxArrayGetHelper(array, nsrc, items, count);
323 324 325 326 327 328 329 330
}

/*
 * Release all items in the array and reset it.
 */
void
vboxArrayRelease(vboxArray *array)
{
331
    size_t i;
332 333
    nsISupports *supports;

334
    if (array->items == NULL)
335 336 337 338 339
        return;

    for (i = 0; i < array->count; ++i) {
        supports = array->items[i];

340
        if (supports != NULL)
341 342 343
            supports->vtbl->Release(supports);
    }

344 345
    pVBoxFuncs_v2_2->pfnComUnallocMem(array->items);

346 347 348 349 350 351 352 353 354 355
    array->items = NULL;
    array->count = 0;
}

/*
 * Unalloc all items in the array and reset it.
 */
void
vboxArrayUnalloc(vboxArray *array)
{
356
    size_t i;
357 358
    void *item;

359
    if (array->items == NULL)
360 361 362 363 364
        return;

    for (i = 0; i < array->count; ++i) {
        item = array->items[i];

365
        if (item != NULL)
366 367 368
            pVBoxFuncs_v2_2->pfnComUnallocMem(item);
    }

369 370
    pVBoxFuncs_v2_2->pfnComUnallocMem(array->items);

371 372 373
    array->items = NULL;
    array->count = 0;
}