driver.c 2.6 KB
Newer Older
1 2 3
/*
 * driver.c: Helpers for loading drivers
 *
4
 * Copyright (C) 2006-2010 Red Hat, Inc.
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
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */


#include <config.h>

#include <unistd.h>

#include "driver.h"
#include "memory.h"
#include "logging.h"
30
#include "util.h"
31
#include "configmake.h"
32

33
#define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
34

35 36 37 38
/* Make sure ... INTERNAL_CALL can not be set by the caller */
verify((VIR_SECRET_GET_VALUE_INTERNAL_CALL &
        VIR_SECRET_GET_VALUE_FLAGS_MASK) == 0);

39 40 41 42
#ifdef WITH_DRIVER_MODULES

/* XXX re-implment this for other OS, or use libtools helper lib ? */

43
# include <dlfcn.h>
44 45 46 47 48 49 50 51 52 53 54 55

void *
virDriverLoadModule(const char *name)
{
    const char *moddir = getenv("LIBVIRT_DRIVER_DIR");
    char *modfile = NULL, *regfunc = NULL;
    void *handle = NULL;
    int (*regsym)(void);

    if (moddir == NULL)
        moddir = DEFAULT_DRIVER_DIR;

56
    VIR_DEBUG("Module load %s", name);
57

58
    if (virAsprintf(&modfile, "%s/libvirt_driver_%s.so", moddir, name) < 0)
59 60 61
        return NULL;

    if (access(modfile, R_OK) < 0) {
62
        VIR_WARN("Module %s not accessible", modfile);
63 64 65 66 67
        goto cleanup;
    }

    handle = dlopen(modfile, RTLD_NOW | RTLD_LOCAL);
    if (!handle) {
68
        VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror());
69 70 71
        goto cleanup;
    }

72
    if (virAsprintf(&regfunc, "%sRegister", name) < 0) {
73 74 75 76 77
        goto cleanup;
    }

    regsym = dlsym(handle, regfunc);
    if (!regsym) {
78
        VIR_ERROR(_("Missing module registration symbol %s"), regfunc);
79 80 81 82
        goto cleanup;
    }

    if ((*regsym)() < 0) {
83
        VIR_ERROR(_("Failed module registration %s"), regfunc);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        goto cleanup;
    }

    VIR_FREE(modfile);
    VIR_FREE(regfunc);
    return handle;

cleanup:
    VIR_FREE(modfile);
    VIR_FREE(regfunc);
    if (handle)
        dlclose(handle);
    return NULL;
}


/* XXX unload modules, but we can't until we can unregister libvirt drivers */

#endif