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"
|
2018-04-25 14:35:27 +00:00
|
|
|
#include "virmodule.h"
|
2018-02-13 10:08:00 +00:00
|
|
|
#include "virthread.h"
|
2010-11-16 14:54:17 +00:00
|
|
|
#include "configmake.h"
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("driver");
|
|
|
|
|
2018-04-19 15:21:38 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2016-09-21 19:17:30 +00:00
|
|
|
/* XXX re-implement this for other OS, or use libtools helper lib ? */
|
2017-07-26 19:31:47 +00:00
|
|
|
#define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2017-08-02 10:21:12 +00:00
|
|
|
|
2017-01-18 15:44:20 +00:00
|
|
|
|
2017-01-26 13:57:41 +00:00
|
|
|
int
|
|
|
|
virDriverLoadModule(const char *name,
|
2018-04-19 15:50:56 +00:00
|
|
|
const char *regfunc,
|
|
|
|
bool required)
|
2008-11-21 12:16:08 +00:00
|
|
|
{
|
2017-01-18 15:44:20 +00:00
|
|
|
char *modfile = NULL;
|
2017-01-26 13:57:41 +00:00
|
|
|
int ret;
|
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
|
|
|
|
2014-04-24 15:45:49 +00:00
|
|
|
if (!(modfile = virFileFindResourceFull(name,
|
|
|
|
"libvirt_driver_",
|
|
|
|
".so",
|
2019-03-12 14:11:47 +00:00
|
|
|
abs_top_builddir "/src/.libs",
|
2015-04-01 10:38:42 +00:00
|
|
|
DEFAULT_DRIVER_DIR,
|
2014-04-24 15:45:49 +00:00
|
|
|
"LIBVIRT_DRIVER_DIR")))
|
2018-04-19 14:25:01 +00:00
|
|
|
return -1;
|
2008-11-21 12:16:08 +00:00
|
|
|
|
2018-04-25 14:35:27 +00:00
|
|
|
ret = virModuleLoad(modfile, regfunc, required);
|
2008-11-21 12:16:08 +00:00
|
|
|
|
|
|
|
VIR_FREE(modfile);
|
2017-01-26 13:57:41 +00:00
|
|
|
|
|
|
|
return ret;
|
2008-11-21 12:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* XXX unload modules, but we can't until we can unregister libvirt drivers */
|
2018-01-31 18:21:52 +00:00
|
|
|
|
2018-02-13 10:08:00 +00:00
|
|
|
virThreadLocal connectInterface;
|
|
|
|
virThreadLocal connectNetwork;
|
|
|
|
virThreadLocal connectNWFilter;
|
|
|
|
virThreadLocal connectNodeDev;
|
|
|
|
virThreadLocal connectSecret;
|
|
|
|
virThreadLocal connectStorage;
|
|
|
|
|
|
|
|
static int
|
|
|
|
virConnectCacheOnceInit(void)
|
|
|
|
{
|
|
|
|
if (virThreadLocalInit(&connectInterface, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virThreadLocalInit(&connectNetwork, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virThreadLocalInit(&connectNWFilter, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virThreadLocalInit(&connectNodeDev, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virThreadLocalInit(&connectSecret, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virThreadLocalInit(&connectStorage, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(virConnectCache);
|
|
|
|
|
2018-01-31 18:21:52 +00:00
|
|
|
virConnectPtr virGetConnectInterface(void)
|
|
|
|
{
|
2018-02-13 10:08:00 +00:00
|
|
|
virConnectPtr conn;
|
|
|
|
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
conn = virThreadLocalGet(&connectInterface);
|
|
|
|
if (conn) {
|
|
|
|
VIR_DEBUG("Return cached interface connection %p", conn);
|
|
|
|
virObjectRef(conn);
|
|
|
|
} else {
|
|
|
|
conn = virConnectOpen(geteuid() == 0 ? "interface:///system" : "interface:///session");
|
|
|
|
VIR_DEBUG("Opened new interface connection %p", conn);
|
|
|
|
}
|
|
|
|
return conn;
|
2018-01-31 18:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virConnectPtr virGetConnectNetwork(void)
|
|
|
|
{
|
2018-02-13 10:08:00 +00:00
|
|
|
virConnectPtr conn;
|
|
|
|
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
conn = virThreadLocalGet(&connectNetwork);
|
|
|
|
if (conn) {
|
|
|
|
VIR_DEBUG("Return cached network connection %p", conn);
|
|
|
|
virObjectRef(conn);
|
|
|
|
} else {
|
|
|
|
conn = virConnectOpen(geteuid() == 0 ? "network:///system" : "network:///session");
|
|
|
|
VIR_DEBUG("Opened new network connection %p", conn);
|
|
|
|
}
|
|
|
|
return conn;
|
2018-01-31 18:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virConnectPtr virGetConnectNWFilter(void)
|
|
|
|
{
|
2018-02-13 10:08:00 +00:00
|
|
|
virConnectPtr conn;
|
|
|
|
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
conn = virThreadLocalGet(&connectNWFilter);
|
|
|
|
if (conn) {
|
|
|
|
VIR_DEBUG("Return cached nwfilter connection %p", conn);
|
|
|
|
virObjectRef(conn);
|
|
|
|
} else {
|
|
|
|
conn = virConnectOpen(geteuid() == 0 ? "nwfilter:///system" : "nwfilter:///session");
|
|
|
|
VIR_DEBUG("Opened new nwfilter connection %p", conn);
|
|
|
|
}
|
|
|
|
return conn;
|
2018-01-31 18:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virConnectPtr virGetConnectNodeDev(void)
|
|
|
|
{
|
2018-02-13 10:08:00 +00:00
|
|
|
virConnectPtr conn;
|
|
|
|
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
conn = virThreadLocalGet(&connectNodeDev);
|
|
|
|
if (conn) {
|
|
|
|
VIR_DEBUG("Return cached nodedev connection %p", conn);
|
|
|
|
virObjectRef(conn);
|
|
|
|
} else {
|
|
|
|
conn = virConnectOpen(geteuid() == 0 ? "nodedev:///system" : "nodedev:///session");
|
|
|
|
VIR_DEBUG("Opened new nodedev connection %p", conn);
|
|
|
|
}
|
|
|
|
return conn;
|
2018-01-31 18:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virConnectPtr virGetConnectSecret(void)
|
|
|
|
{
|
2018-02-13 10:08:00 +00:00
|
|
|
virConnectPtr conn;
|
|
|
|
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
conn = virThreadLocalGet(&connectSecret);
|
|
|
|
if (conn) {
|
|
|
|
VIR_DEBUG("Return cached secret connection %p", conn);
|
|
|
|
virObjectRef(conn);
|
|
|
|
} else {
|
|
|
|
conn = virConnectOpen(geteuid() == 0 ? "secret:///system" : "secret:///session");
|
|
|
|
VIR_DEBUG("Opened new secret connection %p", conn);
|
|
|
|
}
|
|
|
|
return conn;
|
2018-01-31 18:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virConnectPtr virGetConnectStorage(void)
|
|
|
|
{
|
2018-02-13 10:08:00 +00:00
|
|
|
virConnectPtr conn;
|
|
|
|
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
conn = virThreadLocalGet(&connectStorage);
|
|
|
|
if (conn) {
|
|
|
|
VIR_DEBUG("Return cached storage connection %p", conn);
|
|
|
|
virObjectRef(conn);
|
|
|
|
} else {
|
|
|
|
conn = virConnectOpen(geteuid() == 0 ? "storage:///system" : "storage:///session");
|
|
|
|
VIR_DEBUG("Opened new storage connection %p", conn);
|
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSetConnectInterface(virConnectPtr conn)
|
|
|
|
{
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Override interface connection with %p", conn);
|
|
|
|
return virThreadLocalSet(&connectInterface, conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSetConnectNetwork(virConnectPtr conn)
|
|
|
|
{
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Override network connection with %p", conn);
|
|
|
|
return virThreadLocalSet(&connectNetwork, conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSetConnectNWFilter(virConnectPtr conn)
|
|
|
|
{
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Override nwfilter connection with %p", conn);
|
|
|
|
return virThreadLocalSet(&connectNWFilter, conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSetConnectNodeDev(virConnectPtr conn)
|
|
|
|
{
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Override nodedev connection with %p", conn);
|
|
|
|
return virThreadLocalSet(&connectNodeDev, conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSetConnectSecret(virConnectPtr conn)
|
|
|
|
{
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Override secret connection with %p", conn);
|
|
|
|
return virThreadLocalSet(&connectSecret, conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSetConnectStorage(virConnectPtr conn)
|
|
|
|
{
|
|
|
|
if (virConnectCacheInitialize() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Override storage connection with %p", conn);
|
|
|
|
return virThreadLocalSet(&connectStorage, conn);
|
2018-01-31 18:21:52 +00:00
|
|
|
}
|