]> git.proxmox.com Git - mirror_qemu.git/blame - util/module.c
module: rename module_load_one to module_load
[mirror_qemu.git] / util / module.c
CommitLineData
0bfe3ca5
AL
1/*
2 * QEMU Module Infrastructure
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
0bfe3ca5
AL
14 */
15
aafd7584 16#include "qemu/osdep.h"
aa0d1f44 17#ifdef CONFIG_MODULES
e26110cf 18#include <gmodule.h>
aa0d1f44 19#endif
1de7afc9
PB
20#include "qemu/queue.h"
21#include "qemu/module.h"
1b934064 22#include "qemu/cutils.h"
5111edaf 23#include "qemu/config-file.h"
bd83c861
CE
24#ifdef CONFIG_MODULE_UPGRADES
25#include "qemu-version.h"
26#endif
819b8b13 27#include "trace.h"
0bfe3ca5
AL
28
29typedef struct ModuleEntry
30{
0bfe3ca5 31 void (*init)(void);
72cf2d4f 32 QTAILQ_ENTRY(ModuleEntry) node;
e26110cf 33 module_init_type type;
0bfe3ca5
AL
34} ModuleEntry;
35
72cf2d4f 36typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
0bfe3ca5 37
f7897430 38static ModuleTypeList init_type_list[MODULE_INIT_MAX];
46a07579 39static bool modules_init_done[MODULE_INIT_MAX];
0bfe3ca5 40
e26110cf
FZ
41static ModuleTypeList dso_init_list;
42
43static void init_lists(void)
0bfe3ca5 44{
f7897430
AL
45 static int inited;
46 int i;
0bfe3ca5 47
f7897430
AL
48 if (inited) {
49 return;
0bfe3ca5
AL
50 }
51
f7897430 52 for (i = 0; i < MODULE_INIT_MAX; i++) {
72cf2d4f 53 QTAILQ_INIT(&init_type_list[i]);
f7897430 54 }
0bfe3ca5 55
e26110cf
FZ
56 QTAILQ_INIT(&dso_init_list);
57
f7897430
AL
58 inited = 1;
59}
0bfe3ca5 60
0bfe3ca5 61
f7897430
AL
62static ModuleTypeList *find_type(module_init_type type)
63{
e26110cf 64 init_lists();
f7897430 65
9be38598 66 return &init_type_list[type];
0bfe3ca5
AL
67}
68
69void register_module_init(void (*fn)(void), module_init_type type)
70{
71 ModuleEntry *e;
72 ModuleTypeList *l;
73
7267c094 74 e = g_malloc0(sizeof(*e));
0bfe3ca5 75 e->init = fn;
e26110cf 76 e->type = type;
0bfe3ca5 77
f7897430 78 l = find_type(type);
0bfe3ca5 79
72cf2d4f 80 QTAILQ_INSERT_TAIL(l, e, node);
0bfe3ca5
AL
81}
82
e26110cf
FZ
83void register_dso_module_init(void (*fn)(void), module_init_type type)
84{
85 ModuleEntry *e;
86
87 init_lists();
88
89 e = g_malloc0(sizeof(*e));
90 e->init = fn;
91 e->type = type;
92
93 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
94}
95
0bfe3ca5
AL
96void module_call_init(module_init_type type)
97{
98 ModuleTypeList *l;
99 ModuleEntry *e;
100
46a07579
AB
101 if (modules_init_done[type]) {
102 return;
103 }
104
f7897430 105 l = find_type(type);
0bfe3ca5 106
72cf2d4f 107 QTAILQ_FOREACH(e, l, node) {
0bfe3ca5
AL
108 e->init();
109 }
46a07579
AB
110
111 modules_init_done[type] = true;
0bfe3ca5 112}
e26110cf
FZ
113
114#ifdef CONFIG_MODULES
5ebbfecc
GH
115
116static const QemuModinfo module_info_stub[] = { {
117 /* end of list */
118} };
119static const QemuModinfo *module_info = module_info_stub;
d7795d3c 120static const char *module_arch;
5ebbfecc
GH
121
122void module_init_info(const QemuModinfo *info)
123{
124 module_info = info;
125}
126
d7795d3c
GH
127void module_allow_arch(const char *arch)
128{
129 module_arch = arch;
130}
131
132static bool module_check_arch(const QemuModinfo *modinfo)
133{
134 if (modinfo->arch) {
135 if (!module_arch) {
136 /* no arch set -> ignore all */
137 return false;
138 }
139 if (strcmp(module_arch, modinfo->arch) != 0) {
140 /* mismatch */
141 return false;
142 }
143 }
144 return true;
145}
146
2106106d 147static int module_load_file(const char *fname, bool export_symbols)
e26110cf
FZ
148{
149 GModule *g_module;
150 void (*sym)(void);
859aef02 151 const char *dsosuf = CONFIG_HOST_DSOSUF;
e26110cf
FZ
152 int len = strlen(fname);
153 int suf_len = strlen(dsosuf);
154 ModuleEntry *e, *next;
6f13fa7a 155 int ret, flags;
e26110cf
FZ
156
157 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
158 /* wrong suffix */
159 ret = -EINVAL;
160 goto out;
161 }
162 if (access(fname, F_OK)) {
163 ret = -ENOENT;
164 goto out;
165 }
166
167 assert(QTAILQ_EMPTY(&dso_init_list));
168
546323bd 169 flags = 0;
6f13fa7a
GH
170 if (!export_symbols) {
171 flags |= G_MODULE_BIND_LOCAL;
172 }
173 g_module = g_module_open(fname, flags);
e26110cf 174 if (!g_module) {
2106106d
CF
175 fprintf(stderr, "Failed to open module: %s\n",
176 g_module_error());
e26110cf
FZ
177 ret = -EINVAL;
178 goto out;
179 }
180 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
181 fprintf(stderr, "Failed to initialize module: %s\n",
182 fname);
183 /* Print some info if this is a QEMU module (but from different build),
184 * this will make debugging user problems easier. */
185 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
186 fprintf(stderr,
187 "Note: only modules from the same build can be loaded.\n");
188 }
189 g_module_close(g_module);
190 ret = -EINVAL;
191 } else {
192 QTAILQ_FOREACH(e, &dso_init_list, node) {
88d88798 193 e->init();
e26110cf
FZ
194 register_module_init(e->init, e->type);
195 }
196 ret = 0;
197 }
198
819b8b13 199 trace_module_load_module(fname);
e26110cf
FZ
200 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
201 QTAILQ_REMOVE(&dso_init_list, e, node);
202 g_free(e);
203 }
204out:
205 return ret;
206}
207#endif
208
dbc0e805 209bool module_load(const char *prefix, const char *lib_name)
e26110cf 210{
81d8ccb1
MAL
211 bool success = false;
212
e26110cf
FZ
213#ifdef CONFIG_MODULES
214 char *fname = NULL;
bd83c861
CE
215#ifdef CONFIG_MODULE_UPGRADES
216 char *version_dir;
217#endif
900610e6 218 const char *search_dir;
267514b3 219 char *dirs[5];
dffa41b4 220 char *module_name;
900610e6 221 int i = 0, n_dirs = 0;
e897b9a7 222 int ret;
6f13fa7a 223 bool export_symbols = false;
dffa41b4 224 static GHashTable *loaded_modules;
e897b9a7
GH
225 const QemuModinfo *modinfo;
226 const char **sl;
e26110cf
FZ
227
228 if (!g_module_supported()) {
229 fprintf(stderr, "Module is not supported by system.\n");
81d8ccb1 230 return false;
e26110cf
FZ
231 }
232
dffa41b4
FZ
233 if (!loaded_modules) {
234 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
235 }
236
237 module_name = g_strdup_printf("%s%s", prefix, lib_name);
238
64e16fbb 239 if (g_hash_table_contains(loaded_modules, module_name)) {
dffa41b4 240 g_free(module_name);
81d8ccb1 241 return true;
dffa41b4 242 }
64e16fbb 243 g_hash_table_add(loaded_modules, module_name);
dffa41b4 244
e897b9a7 245 for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
d7795d3c
GH
246 if (modinfo->arch) {
247 if (strcmp(modinfo->name, module_name) == 0) {
248 if (!module_check_arch(modinfo)) {
249 return false;
250 }
251 }
252 }
e897b9a7
GH
253 if (modinfo->deps) {
254 if (strcmp(modinfo->name, module_name) == 0) {
255 /* we depend on other module(s) */
256 for (sl = modinfo->deps; *sl != NULL; sl++) {
dbc0e805 257 module_load("", *sl);
e897b9a7
GH
258 }
259 } else {
260 for (sl = modinfo->deps; *sl != NULL; sl++) {
261 if (strcmp(module_name, *sl) == 0) {
262 /* another module depends on us */
263 export_symbols = true;
264 }
265 }
266 }
267 }
268 }
269
900610e6 270 search_dir = getenv("QEMU_MODULE_DIR");
271 if (search_dir != NULL) {
272 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
273 }
1b934064 274 dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR);
bd83c861
CE
275
276#ifdef CONFIG_MODULE_UPGRADES
277 version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
278 G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
279 '_');
280 dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
281#endif
282
900610e6 283 assert(n_dirs <= ARRAY_SIZE(dirs));
284
900610e6 285 for (i = 0; i < n_dirs; i++) {
dffa41b4 286 fname = g_strdup_printf("%s/%s%s",
859aef02 287 dirs[i], module_name, CONFIG_HOST_DSOSUF);
2106106d 288 ret = module_load_file(fname, export_symbols);
88d88798
MM
289 g_free(fname);
290 fname = NULL;
291 /* Try loading until loaded a module file */
292 if (!ret) {
81d8ccb1 293 success = true;
88d88798 294 break;
e26110cf 295 }
e26110cf
FZ
296 }
297
81d8ccb1
MAL
298 if (!success) {
299 g_hash_table_remove(loaded_modules, module_name);
638be478 300 g_free(module_name);
81d8ccb1
MAL
301 }
302
900610e6 303 for (i = 0; i < n_dirs; i++) {
e26110cf
FZ
304 g_free(dirs[i]);
305 }
306
307#endif
81d8ccb1 308 return success;
e26110cf 309}
28457744 310
9f4a0f09 311#ifdef CONFIG_MODULES
28457744
GH
312
313static bool module_loaded_qom_all;
314
dbc0e805 315void module_load_qom(const char *type)
28457744 316{
9f4a0f09
GH
317 const QemuModinfo *modinfo;
318 const char **sl;
28457744 319
d87350b0
GH
320 if (!type) {
321 return;
322 }
9f4a0f09 323
819b8b13 324 trace_module_lookup_object_type(type);
9f4a0f09
GH
325 for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
326 if (!modinfo->objs) {
327 continue;
328 }
ab0cfc3d
GH
329 if (!module_check_arch(modinfo)) {
330 continue;
331 }
9f4a0f09
GH
332 for (sl = modinfo->objs; *sl != NULL; sl++) {
333 if (strcmp(type, *sl) == 0) {
dbc0e805 334 module_load("", modinfo->name);
9f4a0f09 335 }
28457744
GH
336 }
337 }
338}
339
340void module_load_qom_all(void)
341{
9f4a0f09 342 const QemuModinfo *modinfo;
28457744
GH
343
344 if (module_loaded_qom_all) {
345 return;
346 }
9f4a0f09
GH
347
348 for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
349 if (!modinfo->objs) {
28457744
GH
350 continue;
351 }
ab0cfc3d
GH
352 if (!module_check_arch(modinfo)) {
353 continue;
354 }
dbc0e805 355 module_load("", modinfo->name);
28457744
GH
356 }
357 module_loaded_qom_all = true;
358}
9f4a0f09 359
5111edaf
GH
360void qemu_load_module_for_opts(const char *group)
361{
362 const QemuModinfo *modinfo;
363 const char **sl;
364
365 for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
366 if (!modinfo->opts) {
367 continue;
368 }
369 for (sl = modinfo->opts; *sl != NULL; sl++) {
370 if (strcmp(group, *sl) == 0) {
dbc0e805 371 module_load("", modinfo->name);
5111edaf
GH
372 }
373 }
374 }
375}
376
9f4a0f09
GH
377#else
378
d7795d3c 379void module_allow_arch(const char *arch) {}
5111edaf 380void qemu_load_module_for_opts(const char *group) {}
dbc0e805 381void module_load_qom(const char *type) {}
9f4a0f09
GH
382void module_load_qom_all(void) {}
383
384#endif