You need to sign in or sign up before continuing.
driver.c 3.1 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
 *
 */


#include <config.h>

#include <unistd.h>
26
#include <c-ctype.h>
27 28

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

36 37
VIR_LOG_INIT("driver");

38
#define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
39 40 41 42 43

#ifdef WITH_DRIVER_MODULES

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

44
# include <dlfcn.h>
45 46 47 48

void *
virDriverLoadModule(const char *name)
{
49 50
    char *modfile = NULL, *regfunc = NULL, *fixedname = NULL;
    char *tmp;
51 52 53
    void *handle = NULL;
    int (*regsym)(void);

54
    VIR_DEBUG("Module load %s", name);
55

56 57 58
    if (!(modfile = virFileFindResourceFull(name,
                                            "libvirt_driver_",
                                            ".so",
59
                                            abs_topbuilddir "/src/.libs",
60 61
                                            LIBDIR "/libvirt/connection-driver",
                                            "LIBVIRT_DRIVER_DIR")))
62 63 64
        return NULL;

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

69 70
    virUpdateSelfLastChanged(modfile);

71
    handle = dlopen(modfile, RTLD_NOW | RTLD_GLOBAL);
72
    if (!handle) {
73
        VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror());
74 75 76
        goto cleanup;
    }

77 78 79 80 81 82 83 84 85 86 87
    if (VIR_STRDUP_QUIET(fixedname, name) < 0) {
        VIR_ERROR(_("out of memory"));
        goto cleanup;
    }

    /* convert something_like_this into somethingLikeThis */
    while ((tmp = strchr(fixedname, '_'))) {
        memmove(tmp, tmp + 1, strlen(tmp));
        *tmp = c_toupper(*tmp);
    }

88
    if (virAsprintfQuiet(&regfunc, "%sRegister", fixedname) < 0)
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
        goto cleanup;
    }

    VIR_FREE(modfile);
    VIR_FREE(regfunc);
104
    VIR_FREE(fixedname);
105 106
    return handle;

107
 cleanup:
108 109
    VIR_FREE(modfile);
    VIR_FREE(regfunc);
110
    VIR_FREE(fixedname);
111 112 113 114 115 116 117 118 119
    if (handle)
        dlclose(handle);
    return NULL;
}


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

#endif