2008-11-21 12:16:08 +00:00
|
|
|
/*
|
|
|
|
* driver.c: Helpers for loading drivers
|
|
|
|
*
|
2011-04-11 22:25:25 +00:00
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2008-11-21 12:16:08 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "driver.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2010-11-16 14:54:17 +00:00
|
|
|
#include "configmake.h"
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2011-01-21 17:42:07 +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 ? */
|
|
|
|
|
2010-03-09 18:22:22 +00:00
|
|
|
# include <dlfcn.h>
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2012-04-02 17:51:11 +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)
|
2012-04-02 17:51:11 +00:00
|
|
|
virDriverModuleInitialize(NULL);
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2011-02-16 23:37:57 +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;
|
|
|
|
}
|
|
|
|
|
2012-04-02 16:23:59 +00:00
|
|
|
handle = dlopen(modfile, RTLD_NOW | RTLD_GLOBAL);
|
2008-11-21 12:16:08 +00:00
|
|
|
if (!handle) {
|
2010-05-20 06:15:46 +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) {
|
2010-05-20 06:15:46 +00:00
|
|
|
VIR_ERROR(_("Missing module registration symbol %s"), regfunc);
|
2008-11-21 12:16:08 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*regsym)() < 0) {
|
2010-05-20 06:15:46 +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
|