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"
|
2014-04-24 15:45:49 +00:00
|
|
|
#include "virfile.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2013-10-09 10:18:15 +00:00
|
|
|
#include "virutil.h"
|
2010-11-16 14:54:17 +00:00
|
|
|
#include "configmake.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("driver");
|
|
|
|
|
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
|
|
|
|
|
|
|
void *
|
|
|
|
virDriverLoadModule(const char *name)
|
|
|
|
{
|
|
|
|
char *modfile = NULL, *regfunc = NULL;
|
|
|
|
void *handle = NULL;
|
|
|
|
int (*regsym)(void);
|
|
|
|
|
2011-02-16 23:37:57 +00:00
|
|
|
VIR_DEBUG("Module load %s", name);
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2014-04-24 15:45:49 +00:00
|
|
|
if (!(modfile = virFileFindResourceFull(name,
|
|
|
|
"libvirt_driver_",
|
|
|
|
".so",
|
|
|
|
"src/.libs",
|
|
|
|
LIBDIR "/libvirt/connection-driver",
|
|
|
|
"LIBVIRT_DRIVER_DIR")))
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:20:50 +00:00
|
|
|
virUpdateSelfLastChanged(modfile);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-07 15:10:28 +00:00
|
|
|
if (virAsprintfQuiet(®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;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2008-11-21 12:16:08 +00:00
|
|
|
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
|