driver.c 3.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 "virfile.h"
30
#include "virlog.h"
31
#include "configmake.h"
32

33 34
VIR_LOG_INIT("driver");

35 36 37

#ifdef WITH_DRIVER_MODULES

N
Nitesh Konkar 已提交
38
/* XXX re-implement this for other OS, or use libtools helper lib ? */
39

40
# include <dlfcn.h>
J
Ján Tomko 已提交
41
# define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

static void *
virDriverLoadModuleFile(const char *file)
{
    void *handle = NULL;

    VIR_DEBUG("Load module file '%s'", file);

    if (access(file, R_OK) < 0) {
        VIR_INFO("Module %s not accessible", file);
        return NULL;
    }

    virUpdateSelfLastChanged(file);

    if (!(handle = dlopen(file, RTLD_NOW | RTLD_GLOBAL)))
        VIR_ERROR(_("failed to load module %s %s"), file, dlerror());

    return handle;
}


static void *
virDriverLoadModuleFunc(void *handle,
                        const char *funcname)
{
    void *regsym;

    VIR_DEBUG("Lookup function '%s'", funcname);

    if (!(regsym = dlsym(handle, funcname)))
        VIR_ERROR(_("Missing module registration symbol %s"), funcname);

    return regsym;
}


/**
 * virDriverLoadModuleFull:
 * @path: filename of module to load
 * @regfunc: name of the function that registers the module
 * @handle: Returns handle of the loaded library if not NULL
 *
 * Loads a loadable module named @path and calls the
 * registration function @regfunc. If @handle is not NULL the handle is returned
 * in the variable. Otherwise the handle is leaked so that the module stays
 * loaded forever.
 *
 * The module is automatically looked up in the appropriate place (git or
 * installed directory).
 *
 * Returns 0 on success, 1 if the module was not found and -1 on any error.
 */
int
virDriverLoadModuleFull(const char *path,
                        const char *regfunc,
                        void **handle)
{
    void *rethandle = NULL;
    int (*regsym)(void);
    int ret = -1;

    if (!(rethandle = virDriverLoadModuleFile(path))) {
        ret = 1;
        goto cleanup;
    }

    if (!(regsym = virDriverLoadModuleFunc(rethandle, regfunc)))
        goto cleanup;

    if ((*regsym)() < 0) {
        VIR_ERROR(_("Failed module registration %s"), regfunc);
        goto cleanup;
    }

    if (handle)
        VIR_STEAL_PTR(*handle, rethandle);
    else
        rethandle = NULL;

    ret = 0;

 cleanup:
    if (rethandle)
        dlclose(rethandle);
    return ret;
}


132 133 134
int
virDriverLoadModule(const char *name,
                    const char *regfunc)
135
{
136
    char *modfile = NULL;
137
    int ret;
138

139
    VIR_DEBUG("Module load %s", name);
140

141 142 143
    if (!(modfile = virFileFindResourceFull(name,
                                            "libvirt_driver_",
                                            ".so",
144
                                            abs_topbuilddir "/src/.libs",
J
Ján Tomko 已提交
145
                                            DEFAULT_DRIVER_DIR,
146
                                            "LIBVIRT_DRIVER_DIR")))
147
        return 1;
148

149
    ret = virDriverLoadModuleFull(modfile, regfunc, NULL);
150 151

    VIR_FREE(modfile);
152 153

    return ret;
154 155 156 157 158 159
}


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

#endif