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

#include <stdlib.h>
#include <dlfcn.h>
37
#include <stdbool.h>
38 39

#include "vbox_XPCOMCGlue.h"
40 41 42 43 44 45 46
#include "internal.h"
#include "memory.h"
#include "util.h"
#include "logging.h"
#include "virterror_internal.h"

#define VIR_FROM_THIS VIR_FROM_VBOX
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


/*******************************************************************************
*   Defined Constants And Macros                                               *
*******************************************************************************/
#if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__) || defined(__FreeBSD__)
# 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. */
67
static void *hVBoxXPCOMC = NULL;
68
/** Pointer to the VBoxXPCOMC function table. */
69
static PCVBOXXPCOM pVBoxFuncs_v2_2 = NULL;
70 71 72 73 74 75 76 77
/** 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.
 *
78 79 80 81 82
 * @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.
83
 * @param   version       Version number of the loaded API.
84
 */
85 86 87
static int
tryLoadOne(const char *dir, bool setAppHome, bool ignoreMissing,
           unsigned int *version)
88
{
89 90 91 92 93 94 95 96 97
    int result = -1;
    char *name = NULL;
    PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;

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

99 100 101 102 103
        if (!virFileExists(name)) {
            if (!ignoreMissing) {
                VIR_ERROR(_("Libaray '%s' doesn't exist"), name);
            }

104
            VIR_FREE(name);
105 106 107 108 109 110 111 112 113
            return -1;
        }
    } else {
        name = strdup(DYNLIB_NAME);

        if (name == NULL) {
            virReportOOMError();
            return -1;
        }
114 115 116 117 118 119
    }

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

128
    hVBoxXPCOMC = dlopen(name, RTLD_NOW | RTLD_LOCAL);
129

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

139 140 141 142
        goto cleanup;
    }

    pfnGetFunctions = (PFNVBOXGETXPCOMCFUNCTIONS)
143
        dlsym(hVBoxXPCOMC, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME);
144 145 146 147 148 149 150

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

151
    pVBoxFuncs_v2_2 = pfnGetFunctions(VBOX_XPCOMC_VERSION);
152

153
    if (pVBoxFuncs_v2_2 == NULL) {
154 155 156 157 158
        VIR_ERROR(_("Calling %s from '%s' failed"),
                  VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, name);
        goto cleanup;
    }

159
    *version = pVBoxFuncs_v2_2->pfnGetVersion();
160 161 162 163 164 165 166 167 168 169
    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);
    }

cleanup:
170 171 172
    if (hVBoxXPCOMC != NULL && result < 0) {
        dlclose(hVBoxXPCOMC);
        hVBoxXPCOMC = NULL;
173
    }
174 175 176 177

    VIR_FREE(name);

    return result;
178 179 180 181 182 183 184 185 186
}


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

    /* If the user specifies the location, try only that. */
    if (home != NULL) {
211
        if (tryLoadOne(home, false, false, version) < 0) {
212 213 214
            return -1;
        }
    }
215

216 217
    /* Try the additionally configured location. */
    if (VBOX_XPCOMC_DIR[0] != '\0') {
218
        if (tryLoadOne(VBOX_XPCOMC_DIR, true, true, version) >= 0) {
219 220 221
            return 0;
        }
    }
222

223 224
    /* Try the known locations. */
    for (i = 0; i < ARRAY_CARDINALITY(knownDirs); ++i) {
225
        if (tryLoadOne(knownDirs[i], true, true, version) >= 0) {
226 227 228
            return 0;
        }
    }
229

230
    /* Finally try the dynamic linker search path. */
231
    if (tryLoadOne(NULL, false, true, version) >= 0) {
232
        return 0;
233
    }
234 235 236 237 238 239 240 241 242

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


/**
 * Terminate the C glue library.
 */
243 244
void
VBoxCGlueTerm(void)
245
{
246
    if (hVBoxXPCOMC != NULL) {
247 248 249
#if 0 /* VBoxRT.so doesn't like being reloaded. See @bugref{3725}. */
        dlclose(g_hVBoxXPCOMC);
#endif
250
        hVBoxXPCOMC = NULL;
251
    }
252

253
    pVBoxFuncs_v2_2 = NULL;
254 255
    g_pfnGetFunctions = NULL;
}
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370



/*
 * 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);
typedef nsresult (*ArrayGetterWithArg)(void *self, void *arg, PRUint32 *count, void ***items);

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

    array->items = NULL;
    array->count = 0;

    nsrc = ((ArrayGetter)getter)(self, &count, &items);

    if (NS_FAILED(nsrc)) {
        return nsrc;
    }

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

    return nsrc;
}

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

    array->items = NULL;
    array->count = 0;

    nsrc = ((ArrayGetterWithArg)getter)(self, arg, &count, &items);

    if (NS_FAILED(nsrc)) {
        return nsrc;
    }

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

    return nsrc;
}

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

    if (array->items == NULL) {
        return;
    }

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

        if (supports != NULL) {
            supports->vtbl->Release(supports);
        }
    }

    array->items = NULL;
    array->count = 0;
}


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

    if (array->items == NULL) {
        return;
    }

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

        if (item != NULL) {
            pVBoxFuncs_v2_2->pfnComUnallocMem(item);
        }
    }

    array->items = NULL;
    array->count = 0;
}