driver.c 2.8 KB
Newer Older
1 2 3
/*
 * driver.c: Helpers for loading drivers
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27
 *
 */


#include <config.h>

#include <unistd.h>

#include "driver.h"
28
#include "viralloc.h"
29
#include "virlog.h"
30
#include "virutil.h"
31
#include "configmake.h"
32
#include "virstring.h"
33

34 35
VIR_LOG_INIT("driver");

36
#define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
37 38 39 40 41

#ifdef WITH_DRIVER_MODULES

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

42
# include <dlfcn.h>
43

44 45 46 47 48
static const char *moddir = NULL;

void
virDriverModuleInitialize(const char *defmoddir)
{
49
    const char *custommoddir = virGetEnvBlockSUID("LIBVIRT_DRIVER_DIR");
50 51 52 53 54 55 56 57 58
    if (custommoddir)
        moddir = custommoddir;
    else if (defmoddir)
        moddir = defmoddir;
    else
        moddir = DEFAULT_DRIVER_DIR;
    VIR_DEBUG("Module dir %s", moddir);
}

59 60 61 62 63 64 65 66
void *
virDriverLoadModule(const char *name)
{
    char *modfile = NULL, *regfunc = NULL;
    void *handle = NULL;
    int (*regsym)(void);

    if (moddir == NULL)
67
        virDriverModuleInitialize(NULL);
68

69
    VIR_DEBUG("Module load %s", name);
70

71
    if (virAsprintfQuiet(&modfile, "%s/libvirt_driver_%s.so", moddir, name) < 0)
72 73 74
        return NULL;

    if (access(modfile, R_OK) < 0) {
75
        VIR_WARN("Module %s not accessible", modfile);
76 77 78
        goto cleanup;
    }

79 80
    virUpdateSelfLastChanged(modfile);

81
    handle = dlopen(modfile, RTLD_NOW | RTLD_GLOBAL);
82
    if (!handle) {
83
        VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror());
84 85 86
        goto cleanup;
    }

87
    if (virAsprintfQuiet(&regfunc, "%sRegister", name) < 0) {
88 89 90 91 92
        goto cleanup;
    }

    regsym = dlsym(handle, regfunc);
    if (!regsym) {
93
        VIR_ERROR(_("Missing module registration symbol %s"), regfunc);
94 95 96 97
        goto cleanup;
    }

    if ((*regsym)() < 0) {
98
        VIR_ERROR(_("Failed module registration %s"), regfunc);
99 100 101 102 103 104 105
        goto cleanup;
    }

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

106
 cleanup:
107 108 109 110 111 112 113 114 115 116 117
    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