]> Git Repo - qemu.git/blame - util/module.c
gitlab-ci: Add cross-compiling build tests
[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 <[email protected]>
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"
bd83c861
CE
22#ifdef CONFIG_MODULE_UPGRADES
23#include "qemu-version.h"
24#endif
0bfe3ca5
AL
25
26typedef struct ModuleEntry
27{
0bfe3ca5 28 void (*init)(void);
72cf2d4f 29 QTAILQ_ENTRY(ModuleEntry) node;
e26110cf 30 module_init_type type;
0bfe3ca5
AL
31} ModuleEntry;
32
72cf2d4f 33typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
0bfe3ca5 34
f7897430 35static ModuleTypeList init_type_list[MODULE_INIT_MAX];
46a07579 36static bool modules_init_done[MODULE_INIT_MAX];
0bfe3ca5 37
e26110cf
FZ
38static ModuleTypeList dso_init_list;
39
40static void init_lists(void)
0bfe3ca5 41{
f7897430
AL
42 static int inited;
43 int i;
0bfe3ca5 44
f7897430
AL
45 if (inited) {
46 return;
0bfe3ca5
AL
47 }
48
f7897430 49 for (i = 0; i < MODULE_INIT_MAX; i++) {
72cf2d4f 50 QTAILQ_INIT(&init_type_list[i]);
f7897430 51 }
0bfe3ca5 52
e26110cf
FZ
53 QTAILQ_INIT(&dso_init_list);
54
f7897430
AL
55 inited = 1;
56}
0bfe3ca5 57
0bfe3ca5 58
f7897430
AL
59static ModuleTypeList *find_type(module_init_type type)
60{
e26110cf 61 init_lists();
f7897430 62
9be38598 63 return &init_type_list[type];
0bfe3ca5
AL
64}
65
66void register_module_init(void (*fn)(void), module_init_type type)
67{
68 ModuleEntry *e;
69 ModuleTypeList *l;
70
7267c094 71 e = g_malloc0(sizeof(*e));
0bfe3ca5 72 e->init = fn;
e26110cf 73 e->type = type;
0bfe3ca5 74
f7897430 75 l = find_type(type);
0bfe3ca5 76
72cf2d4f 77 QTAILQ_INSERT_TAIL(l, e, node);
0bfe3ca5
AL
78}
79
e26110cf
FZ
80void register_dso_module_init(void (*fn)(void), module_init_type type)
81{
82 ModuleEntry *e;
83
84 init_lists();
85
86 e = g_malloc0(sizeof(*e));
87 e->init = fn;
88 e->type = type;
89
90 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
91}
92
0bfe3ca5
AL
93void module_call_init(module_init_type type)
94{
95 ModuleTypeList *l;
96 ModuleEntry *e;
97
46a07579
AB
98 if (modules_init_done[type]) {
99 return;
100 }
101
f7897430 102 l = find_type(type);
0bfe3ca5 103
72cf2d4f 104 QTAILQ_FOREACH(e, l, node) {
0bfe3ca5
AL
105 e->init();
106 }
46a07579
AB
107
108 modules_init_done[type] = true;
0bfe3ca5 109}
e26110cf
FZ
110
111#ifdef CONFIG_MODULES
112static int module_load_file(const char *fname)
113{
114 GModule *g_module;
115 void (*sym)(void);
859aef02 116 const char *dsosuf = CONFIG_HOST_DSOSUF;
e26110cf
FZ
117 int len = strlen(fname);
118 int suf_len = strlen(dsosuf);
119 ModuleEntry *e, *next;
120 int ret;
121
122 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
123 /* wrong suffix */
124 ret = -EINVAL;
125 goto out;
126 }
127 if (access(fname, F_OK)) {
128 ret = -ENOENT;
129 goto out;
130 }
131
132 assert(QTAILQ_EMPTY(&dso_init_list));
133
134 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
135 if (!g_module) {
136 fprintf(stderr, "Failed to open module: %s\n",
137 g_module_error());
138 ret = -EINVAL;
139 goto out;
140 }
141 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
142 fprintf(stderr, "Failed to initialize module: %s\n",
143 fname);
144 /* Print some info if this is a QEMU module (but from different build),
145 * this will make debugging user problems easier. */
146 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
147 fprintf(stderr,
148 "Note: only modules from the same build can be loaded.\n");
149 }
150 g_module_close(g_module);
151 ret = -EINVAL;
152 } else {
153 QTAILQ_FOREACH(e, &dso_init_list, node) {
88d88798 154 e->init();
e26110cf
FZ
155 register_module_init(e->init, e->type);
156 }
157 ret = 0;
158 }
159
160 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
161 QTAILQ_REMOVE(&dso_init_list, e, node);
162 g_free(e);
163 }
164out:
165 return ret;
166}
167#endif
168
81d8ccb1 169bool module_load_one(const char *prefix, const char *lib_name)
e26110cf 170{
81d8ccb1
MAL
171 bool success = false;
172
e26110cf
FZ
173#ifdef CONFIG_MODULES
174 char *fname = NULL;
e26110cf 175 char *exec_dir;
bd83c861
CE
176#ifdef CONFIG_MODULE_UPGRADES
177 char *version_dir;
178#endif
900610e6 179 const char *search_dir;
267514b3 180 char *dirs[5];
dffa41b4 181 char *module_name;
900610e6 182 int i = 0, n_dirs = 0;
e26110cf 183 int ret;
dffa41b4 184 static GHashTable *loaded_modules;
e26110cf
FZ
185
186 if (!g_module_supported()) {
187 fprintf(stderr, "Module is not supported by system.\n");
81d8ccb1 188 return false;
e26110cf
FZ
189 }
190
dffa41b4
FZ
191 if (!loaded_modules) {
192 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
193 }
194
195 module_name = g_strdup_printf("%s%s", prefix, lib_name);
196
90629122 197 if (!g_hash_table_add(loaded_modules, module_name)) {
dffa41b4 198 g_free(module_name);
81d8ccb1 199 return true;
dffa41b4 200 }
dffa41b4 201
e26110cf 202 exec_dir = qemu_get_exec_dir();
900610e6 203 search_dir = getenv("QEMU_MODULE_DIR");
204 if (search_dir != NULL) {
205 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
206 }
207 dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
900610e6 208 dirs[n_dirs++] = g_strdup_printf("%s", exec_dir ? : "");
bd83c861
CE
209
210#ifdef CONFIG_MODULE_UPGRADES
211 version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
212 G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
213 '_');
214 dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
215#endif
216
900610e6 217 assert(n_dirs <= ARRAY_SIZE(dirs));
218
e26110cf
FZ
219 g_free(exec_dir);
220 exec_dir = NULL;
221
900610e6 222 for (i = 0; i < n_dirs; i++) {
dffa41b4 223 fname = g_strdup_printf("%s/%s%s",
859aef02 224 dirs[i], module_name, CONFIG_HOST_DSOSUF);
88d88798
MM
225 ret = module_load_file(fname);
226 g_free(fname);
227 fname = NULL;
228 /* Try loading until loaded a module file */
229 if (!ret) {
81d8ccb1 230 success = true;
88d88798 231 break;
e26110cf 232 }
e26110cf
FZ
233 }
234
81d8ccb1
MAL
235 if (!success) {
236 g_hash_table_remove(loaded_modules, module_name);
638be478 237 g_free(module_name);
81d8ccb1
MAL
238 }
239
900610e6 240 for (i = 0; i < n_dirs; i++) {
e26110cf
FZ
241 g_free(dirs[i]);
242 }
243
244#endif
81d8ccb1 245 return success;
e26110cf 246}
28457744
GH
247
248/*
249 * Building devices and other qom objects modular is mostly useful in
250 * case they have dependencies to external shared libraries, so we can
251 * cut down the core qemu library dependencies. Which is the case for
252 * only a very few devices & objects.
253 *
254 * So with the expectation that this will be rather the exception than
255 * to rule and the list will not gain that many entries go with a
256 * simple manually maintained list for now.
257 */
258static struct {
259 const char *type;
260 const char *prefix;
261 const char *module;
262} const qom_modules[] = {
8887312b
GH
263 { "ccid-card-passthru", "hw-", "usb-smartcard" },
264 { "ccid-card-emulated", "hw-", "usb-smartcard" },
aa9c8573 265 { "usb-redir", "hw-", "usb-redirect" },
d39e93d4
GH
266 { "qxl-vga", "hw-", "display-qxl" },
267 { "qxl", "hw-", "display-qxl" },
ef138c77 268 { "chardev-braille", "chardev-", "baum" },
28457744
GH
269};
270
271static bool module_loaded_qom_all;
272
273void module_load_qom_one(const char *type)
274{
275 int i;
276
d87350b0
GH
277 if (!type) {
278 return;
279 }
28457744
GH
280 if (module_loaded_qom_all) {
281 return;
282 }
283 for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
284 if (strcmp(qom_modules[i].type, type) == 0) {
285 module_load_one(qom_modules[i].prefix,
286 qom_modules[i].module);
287 return;
288 }
289 }
290}
291
292void module_load_qom_all(void)
293{
294 int i;
295
296 if (module_loaded_qom_all) {
297 return;
298 }
299 for (i = 0; i < ARRAY_SIZE(qom_modules); i++) {
300 if (i > 0 && (strcmp(qom_modules[i - 1].module,
301 qom_modules[i].module) == 0 &&
302 strcmp(qom_modules[i - 1].prefix,
303 qom_modules[i].prefix) == 0)) {
304 /* one module implementing multiple types -> load only once */
305 continue;
306 }
307 module_load_one(qom_modules[i].prefix, qom_modules[i].module);
308 }
309 module_loaded_qom_all = true;
310}
This page took 0.740528 seconds and 4 git commands to generate.