libvirt/src/driver.c

113 lines
2.7 KiB
C
Raw Normal View History

2008-11-21 12:16:08 +00:00
/*
* driver.c: Helpers for loading drivers
*
* Copyright (C) 2006-2011 Red Hat, Inc.
2008-11-21 12:16:08 +00:00
*
* 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, see
* <http://www.gnu.org/licenses/>.
2008-11-21 12:16:08 +00:00
*
*/
#include <config.h>
#include <unistd.h>
#include "driver.h"
#include "memory.h"
#include "logging.h"
#include "util.h"
maint: use gnulib configmake rather than open-coding things * bootstrap.conf (gnulib_modules): Add configmake. * daemon/Makefile.am (libvirtd_CFLAGS): Drop defines provided by gnulib. * src/Makefile.am (INCLUDES): Likewise. * tests/Makefile.am (INCLUDES): Likewise. * tools/Makefile.am (virsh_CFLAGS): Likewise. * daemon/libvirtd.c (qemudInitPaths, usage, main): Update clients. * src/cpu/cpu_map.c (CPUMAPFILE): Likewise. * src/driver.c (DEFAULT_DRIVER_DIR): Likewise. * src/internal.h (_): Likewise. * src/libvirt.c (virInitialize): Likewise. * src/lxc/lxc_conf.h (LXC_CONFIG_DIR, LXC_STATE_DIR, LXC_LOG_DIR): Likewise. * src/lxc/lxc_conf.c (lxcCapsInit, lxcLoadDriverConfig): Likewise. * src/network/bridge_driver.c (NETWORK_PID_DIR) (NETWORK_STATE_DIR, DNSMASQ_STATE_DIR, networkStartup): Likewise. * src/nwfilter/nwfilter_driver.c (nwfilterDriverStartup): Likewise. * src/qemu/qemu_conf.c (qemudLoadDriverConfig): Likewise. * src/qemu/qemu_driver.c (qemudStartup): Likewise. * src/remote/remote_driver.h (LIBVIRTD_PRIV_UNIX_SOCKET) (LIBVIRTD_PRIV_UNIX_SOCKET_RO, LIBVIRTD_CONFIGURATION_FILE) (LIBVIRT_PKI_DIR): Likewise. * src/secret/secret_driver.c (secretDriverStartup): Likewise. * src/security/security_apparmor.c (VIRT_AA_HELPER): Likewise. * src/security/virt-aa-helper.c (main): Likewise. * src/storage/storage_backend_disk.c (PARTHELPER): Likewise. * src/storage/storage_driver.c (storageDriverStartup): Likewise. * src/uml/uml_driver.c (TEMPDIR, umlStartup): Likewise. * src/util/hooks.c (LIBVIRT_HOOK_DIR): Likewise. * tools/virsh.c (main): Likewise. * docs/hooks.html.in: Likewise.
2010-11-16 14:54:17 +00:00
#include "configmake.h"
2008-11-21 12:16:08 +00:00
#define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
2008-11-21 12:16:08 +00:00
#ifdef WITH_DRIVER_MODULES
/* XXX re-implment this for other OS, or use libtools helper lib ? */
# include <dlfcn.h>
2008-11-21 12:16:08 +00:00
static const char *moddir = NULL;
void
virDriverModuleInitialize(const char *defmoddir)
{
const char *custommoddir = getenv("LIBVIRT_DRIVER_DIR");
if (custommoddir)
moddir = custommoddir;
else if (defmoddir)
moddir = defmoddir;
else
moddir = DEFAULT_DRIVER_DIR;
VIR_DEBUG("Module dir %s", moddir);
}
2008-11-21 12:16:08 +00:00
void *
virDriverLoadModule(const char *name)
{
char *modfile = NULL, *regfunc = NULL;
void *handle = NULL;
int (*regsym)(void);
if (moddir == NULL)
virDriverModuleInitialize(NULL);
2008-11-21 12:16:08 +00:00
VIR_DEBUG("Module load %s", name);
2008-11-21 12:16:08 +00:00
2008-12-23 13:03:29 +00:00
if (virAsprintf(&modfile, "%s/libvirt_driver_%s.so", moddir, name) < 0)
2008-11-21 12:16:08 +00:00
return NULL;
if (access(modfile, R_OK) < 0) {
2009-05-08 10:05:56 +00:00
VIR_WARN("Module %s not accessible", modfile);
2008-11-21 12:16:08 +00:00
goto cleanup;
}
handle = dlopen(modfile, RTLD_NOW | RTLD_GLOBAL);
2008-11-21 12:16:08 +00:00
if (!handle) {
VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror());
2008-11-21 12:16:08 +00:00
goto cleanup;
}
2008-12-23 13:03:29 +00:00
if (virAsprintf(&regfunc, "%sRegister", name) < 0) {
2008-11-21 12:16:08 +00:00
goto cleanup;
}
regsym = dlsym(handle, regfunc);
if (!regsym) {
VIR_ERROR(_("Missing module registration symbol %s"), regfunc);
2008-11-21 12:16:08 +00:00
goto cleanup;
}
if ((*regsym)() < 0) {
VIR_ERROR(_("Failed module registration %s"), regfunc);
2008-11-21 12:16:08 +00:00
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