2008-11-21 12:16:08 +00:00
|
|
|
/*
|
|
|
|
* driver.c: Helpers for loading drivers
|
|
|
|
*
|
2009-02-17 10:43:26 +00:00
|
|
|
* Copyright (C) 2006-2009 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, 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"
|
2009-02-17 10:43:26 +00:00
|
|
|
#include "util.h"
|
2008-11-21 12:16:08 +00:00
|
|
|
|
|
|
|
#define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/drivers"
|
|
|
|
|
|
|
|
#ifdef WITH_DRIVER_MODULES
|
|
|
|
|
|
|
|
/* XXX re-implment this for other OS, or use libtools helper lib ? */
|
|
|
|
|
2010-03-09 18:22:22 +00:00
|
|
|
# include <dlfcn.h>
|
2008-11-21 12:16:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
DEBUG("Module load %s", name);
|
|
|
|
|
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_LOCAL);
|
|
|
|
if (!handle) {
|
2009-05-08 10:05:56 +00:00
|
|
|
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(®func, "%sRegister", name) < 0) {
|
2008-11-21 12:16:08 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
regsym = dlsym(handle, regfunc);
|
|
|
|
if (!regsym) {
|
2009-05-08 10:05:56 +00:00
|
|
|
VIR_ERROR("Missing module registration symbol %s", regfunc);
|
2008-11-21 12:16:08 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*regsym)() < 0) {
|
2009-05-08 10:05:56 +00:00
|
|
|
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
|