examples: Move $(STATIC_BINARIES) to $(AM_LDFLAGS)
[libvirt/ericb.git] / src / driver.c
blob5e8f68f6df517adaa9a44730c68037203f74d26c
1 /*
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/>.
23 #include <config.h>
25 #include <unistd.h>
27 #include "driver.h"
28 #include "viralloc.h"
29 #include "virfile.h"
30 #include "virlog.h"
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"
44 int
45 virDriverLoadModule(const char *name,
46 const char *regfunc,
47 bool required)
49 char *modfile = NULL;
50 int ret;
52 VIR_DEBUG("Module load %s", name);
54 if (!(modfile = virFileFindResourceFull(name,
55 "libvirt_driver_",
56 ".so",
57 abs_top_builddir "/src/.libs",
58 DEFAULT_DRIVER_DIR,
59 "LIBVIRT_DRIVER_DIR")))
60 return -1;
62 ret = virModuleLoad(modfile, regfunc, required);
64 VIR_FREE(modfile);
66 return ret;
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;
79 static int
80 virConnectCacheOnceInit(void)
82 if (virThreadLocalInit(&connectInterface, NULL) < 0)
83 return -1;
84 if (virThreadLocalInit(&connectNetwork, NULL) < 0)
85 return -1;
86 if (virThreadLocalInit(&connectNWFilter, NULL) < 0)
87 return -1;
88 if (virThreadLocalInit(&connectNodeDev, NULL) < 0)
89 return -1;
90 if (virThreadLocalInit(&connectSecret, NULL) < 0)
91 return -1;
92 if (virThreadLocalInit(&connectStorage, NULL) < 0)
93 return -1;
94 return 0;
97 VIR_ONCE_GLOBAL_INIT(virConnectCache);
99 virConnectPtr virGetConnectInterface(void)
101 virConnectPtr conn;
103 if (virConnectCacheInitialize() < 0)
104 return NULL;
106 conn = virThreadLocalGet(&connectInterface);
107 if (conn) {
108 VIR_DEBUG("Return cached interface connection %p", conn);
109 virObjectRef(conn);
110 } else {
111 conn = virConnectOpen(geteuid() == 0 ? "interface:///system" : "interface:///session");
112 VIR_DEBUG("Opened new interface connection %p", conn);
114 return conn;
117 virConnectPtr virGetConnectNetwork(void)
119 virConnectPtr conn;
121 if (virConnectCacheInitialize() < 0)
122 return NULL;
124 conn = virThreadLocalGet(&connectNetwork);
125 if (conn) {
126 VIR_DEBUG("Return cached network connection %p", conn);
127 virObjectRef(conn);
128 } else {
129 conn = virConnectOpen(geteuid() == 0 ? "network:///system" : "network:///session");
130 VIR_DEBUG("Opened new network connection %p", conn);
132 return conn;
135 virConnectPtr virGetConnectNWFilter(void)
137 virConnectPtr conn;
139 if (virConnectCacheInitialize() < 0)
140 return NULL;
142 conn = virThreadLocalGet(&connectNWFilter);
143 if (conn) {
144 VIR_DEBUG("Return cached nwfilter connection %p", conn);
145 virObjectRef(conn);
146 } else {
147 conn = virConnectOpen(geteuid() == 0 ? "nwfilter:///system" : "nwfilter:///session");
148 VIR_DEBUG("Opened new nwfilter connection %p", conn);
150 return conn;
153 virConnectPtr virGetConnectNodeDev(void)
155 virConnectPtr conn;
157 if (virConnectCacheInitialize() < 0)
158 return NULL;
160 conn = virThreadLocalGet(&connectNodeDev);
161 if (conn) {
162 VIR_DEBUG("Return cached nodedev connection %p", conn);
163 virObjectRef(conn);
164 } else {
165 conn = virConnectOpen(geteuid() == 0 ? "nodedev:///system" : "nodedev:///session");
166 VIR_DEBUG("Opened new nodedev connection %p", conn);
168 return conn;
171 virConnectPtr virGetConnectSecret(void)
173 virConnectPtr conn;
175 if (virConnectCacheInitialize() < 0)
176 return NULL;
178 conn = virThreadLocalGet(&connectSecret);
179 if (conn) {
180 VIR_DEBUG("Return cached secret connection %p", conn);
181 virObjectRef(conn);
182 } else {
183 conn = virConnectOpen(geteuid() == 0 ? "secret:///system" : "secret:///session");
184 VIR_DEBUG("Opened new secret connection %p", conn);
186 return conn;
189 virConnectPtr virGetConnectStorage(void)
191 virConnectPtr conn;
193 if (virConnectCacheInitialize() < 0)
194 return NULL;
196 conn = virThreadLocalGet(&connectStorage);
197 if (conn) {
198 VIR_DEBUG("Return cached storage connection %p", conn);
199 virObjectRef(conn);
200 } else {
201 conn = virConnectOpen(geteuid() == 0 ? "storage:///system" : "storage:///session");
202 VIR_DEBUG("Opened new storage connection %p", conn);
204 return conn;
209 virSetConnectInterface(virConnectPtr conn)
211 if (virConnectCacheInitialize() < 0)
212 return -1;
214 VIR_DEBUG("Override interface connection with %p", conn);
215 return virThreadLocalSet(&connectInterface, conn);
220 virSetConnectNetwork(virConnectPtr conn)
222 if (virConnectCacheInitialize() < 0)
223 return -1;
225 VIR_DEBUG("Override network connection with %p", conn);
226 return virThreadLocalSet(&connectNetwork, conn);
231 virSetConnectNWFilter(virConnectPtr conn)
233 if (virConnectCacheInitialize() < 0)
234 return -1;
236 VIR_DEBUG("Override nwfilter connection with %p", conn);
237 return virThreadLocalSet(&connectNWFilter, conn);
242 virSetConnectNodeDev(virConnectPtr conn)
244 if (virConnectCacheInitialize() < 0)
245 return -1;
247 VIR_DEBUG("Override nodedev connection with %p", conn);
248 return virThreadLocalSet(&connectNodeDev, conn);
253 virSetConnectSecret(virConnectPtr conn)
255 if (virConnectCacheInitialize() < 0)
256 return -1;
258 VIR_DEBUG("Override secret connection with %p", conn);
259 return virThreadLocalSet(&connectSecret, conn);
264 virSetConnectStorage(virConnectPtr conn)
266 if (virConnectCacheInitialize() < 0)
267 return -1;
269 VIR_DEBUG("Override storage connection with %p", conn);
270 return virThreadLocalSet(&connectStorage, conn);