2 * driver.c: Helpers for loading drivers
4 * Copyright (C) 2006-2011 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
31 #include "virmodule.h"
32 #include "virthread.h"
33 #include "configmake.h"
35 VIR_LOG_INIT("driver");
37 #define VIR_FROM_THIS VIR_FROM_NONE
39 /* XXX re-implement this for other OS, or use libtools helper lib ? */
40 #define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
45 virDriverLoadModule(const char *name
,
52 VIR_DEBUG("Module load %s", name
);
54 if (!(modfile
= virFileFindResourceFull(name
,
57 abs_top_builddir
"/src/.libs",
59 "LIBVIRT_DRIVER_DIR")))
62 ret
= virModuleLoad(modfile
, regfunc
, required
);
70 /* XXX unload modules, but we can't until we can unregister libvirt drivers */
72 virThreadLocal connectInterface
;
73 virThreadLocal connectNetwork
;
74 virThreadLocal connectNWFilter
;
75 virThreadLocal connectNodeDev
;
76 virThreadLocal connectSecret
;
77 virThreadLocal connectStorage
;
80 virConnectCacheOnceInit(void)
82 if (virThreadLocalInit(&connectInterface
, NULL
) < 0)
84 if (virThreadLocalInit(&connectNetwork
, NULL
) < 0)
86 if (virThreadLocalInit(&connectNWFilter
, NULL
) < 0)
88 if (virThreadLocalInit(&connectNodeDev
, NULL
) < 0)
90 if (virThreadLocalInit(&connectSecret
, NULL
) < 0)
92 if (virThreadLocalInit(&connectStorage
, NULL
) < 0)
97 VIR_ONCE_GLOBAL_INIT(virConnectCache
);
99 virConnectPtr
virGetConnectInterface(void)
103 if (virConnectCacheInitialize() < 0)
106 conn
= virThreadLocalGet(&connectInterface
);
108 VIR_DEBUG("Return cached interface connection %p", conn
);
111 conn
= virConnectOpen(geteuid() == 0 ? "interface:///system" : "interface:///session");
112 VIR_DEBUG("Opened new interface connection %p", conn
);
117 virConnectPtr
virGetConnectNetwork(void)
121 if (virConnectCacheInitialize() < 0)
124 conn
= virThreadLocalGet(&connectNetwork
);
126 VIR_DEBUG("Return cached network connection %p", conn
);
129 conn
= virConnectOpen(geteuid() == 0 ? "network:///system" : "network:///session");
130 VIR_DEBUG("Opened new network connection %p", conn
);
135 virConnectPtr
virGetConnectNWFilter(void)
139 if (virConnectCacheInitialize() < 0)
142 conn
= virThreadLocalGet(&connectNWFilter
);
144 VIR_DEBUG("Return cached nwfilter connection %p", conn
);
147 conn
= virConnectOpen(geteuid() == 0 ? "nwfilter:///system" : "nwfilter:///session");
148 VIR_DEBUG("Opened new nwfilter connection %p", conn
);
153 virConnectPtr
virGetConnectNodeDev(void)
157 if (virConnectCacheInitialize() < 0)
160 conn
= virThreadLocalGet(&connectNodeDev
);
162 VIR_DEBUG("Return cached nodedev connection %p", conn
);
165 conn
= virConnectOpen(geteuid() == 0 ? "nodedev:///system" : "nodedev:///session");
166 VIR_DEBUG("Opened new nodedev connection %p", conn
);
171 virConnectPtr
virGetConnectSecret(void)
175 if (virConnectCacheInitialize() < 0)
178 conn
= virThreadLocalGet(&connectSecret
);
180 VIR_DEBUG("Return cached secret connection %p", conn
);
183 conn
= virConnectOpen(geteuid() == 0 ? "secret:///system" : "secret:///session");
184 VIR_DEBUG("Opened new secret connection %p", conn
);
189 virConnectPtr
virGetConnectStorage(void)
193 if (virConnectCacheInitialize() < 0)
196 conn
= virThreadLocalGet(&connectStorage
);
198 VIR_DEBUG("Return cached storage connection %p", conn
);
201 conn
= virConnectOpen(geteuid() == 0 ? "storage:///system" : "storage:///session");
202 VIR_DEBUG("Opened new storage connection %p", conn
);
209 virSetConnectInterface(virConnectPtr conn
)
211 if (virConnectCacheInitialize() < 0)
214 VIR_DEBUG("Override interface connection with %p", conn
);
215 return virThreadLocalSet(&connectInterface
, conn
);
220 virSetConnectNetwork(virConnectPtr conn
)
222 if (virConnectCacheInitialize() < 0)
225 VIR_DEBUG("Override network connection with %p", conn
);
226 return virThreadLocalSet(&connectNetwork
, conn
);
231 virSetConnectNWFilter(virConnectPtr conn
)
233 if (virConnectCacheInitialize() < 0)
236 VIR_DEBUG("Override nwfilter connection with %p", conn
);
237 return virThreadLocalSet(&connectNWFilter
, conn
);
242 virSetConnectNodeDev(virConnectPtr conn
)
244 if (virConnectCacheInitialize() < 0)
247 VIR_DEBUG("Override nodedev connection with %p", conn
);
248 return virThreadLocalSet(&connectNodeDev
, conn
);
253 virSetConnectSecret(virConnectPtr conn
)
255 if (virConnectCacheInitialize() < 0)
258 VIR_DEBUG("Override secret connection with %p", conn
);
259 return virThreadLocalSet(&connectSecret
, conn
);
264 virSetConnectStorage(virConnectPtr conn
)
266 if (virConnectCacheInitialize() < 0)
269 VIR_DEBUG("Override storage connection with %p", conn
);
270 return virThreadLocalSet(&connectStorage
, conn
);