modules: use modinfo for qom load
[qemu/kevin.git] / util / module.c
blob745ae0fb20ed609f916e8e81171eed8a2ffff609
1 /*
2 * QEMU Module Infrastructure
4 * Copyright IBM, Corp. 2009
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #ifdef CONFIG_MODULES
18 #include <gmodule.h>
19 #endif
20 #include "qemu/queue.h"
21 #include "qemu/module.h"
22 #include "qemu/cutils.h"
23 #ifdef CONFIG_MODULE_UPGRADES
24 #include "qemu-version.h"
25 #endif
27 typedef struct ModuleEntry
29 void (*init)(void);
30 QTAILQ_ENTRY(ModuleEntry) node;
31 module_init_type type;
32 } ModuleEntry;
34 typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
36 static ModuleTypeList init_type_list[MODULE_INIT_MAX];
37 static bool modules_init_done[MODULE_INIT_MAX];
39 static ModuleTypeList dso_init_list;
41 static void init_lists(void)
43 static int inited;
44 int i;
46 if (inited) {
47 return;
50 for (i = 0; i < MODULE_INIT_MAX; i++) {
51 QTAILQ_INIT(&init_type_list[i]);
54 QTAILQ_INIT(&dso_init_list);
56 inited = 1;
60 static ModuleTypeList *find_type(module_init_type type)
62 init_lists();
64 return &init_type_list[type];
67 void register_module_init(void (*fn)(void), module_init_type type)
69 ModuleEntry *e;
70 ModuleTypeList *l;
72 e = g_malloc0(sizeof(*e));
73 e->init = fn;
74 e->type = type;
76 l = find_type(type);
78 QTAILQ_INSERT_TAIL(l, e, node);
81 void register_dso_module_init(void (*fn)(void), module_init_type type)
83 ModuleEntry *e;
85 init_lists();
87 e = g_malloc0(sizeof(*e));
88 e->init = fn;
89 e->type = type;
91 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
94 void module_call_init(module_init_type type)
96 ModuleTypeList *l;
97 ModuleEntry *e;
99 if (modules_init_done[type]) {
100 return;
103 l = find_type(type);
105 QTAILQ_FOREACH(e, l, node) {
106 e->init();
109 modules_init_done[type] = true;
112 #ifdef CONFIG_MODULES
114 static const QemuModinfo module_info_stub[] = { {
115 /* end of list */
116 } };
117 static const QemuModinfo *module_info = module_info_stub;
119 void module_init_info(const QemuModinfo *info)
121 module_info = info;
124 static int module_load_file(const char *fname, bool mayfail, bool export_symbols)
126 GModule *g_module;
127 void (*sym)(void);
128 const char *dsosuf = CONFIG_HOST_DSOSUF;
129 int len = strlen(fname);
130 int suf_len = strlen(dsosuf);
131 ModuleEntry *e, *next;
132 int ret, flags;
134 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
135 /* wrong suffix */
136 ret = -EINVAL;
137 goto out;
139 if (access(fname, F_OK)) {
140 ret = -ENOENT;
141 goto out;
144 assert(QTAILQ_EMPTY(&dso_init_list));
146 flags = 0;
147 if (!export_symbols) {
148 flags |= G_MODULE_BIND_LOCAL;
150 g_module = g_module_open(fname, flags);
151 if (!g_module) {
152 if (!mayfail) {
153 fprintf(stderr, "Failed to open module: %s\n",
154 g_module_error());
156 ret = -EINVAL;
157 goto out;
159 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
160 fprintf(stderr, "Failed to initialize module: %s\n",
161 fname);
162 /* Print some info if this is a QEMU module (but from different build),
163 * this will make debugging user problems easier. */
164 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
165 fprintf(stderr,
166 "Note: only modules from the same build can be loaded.\n");
168 g_module_close(g_module);
169 ret = -EINVAL;
170 } else {
171 QTAILQ_FOREACH(e, &dso_init_list, node) {
172 e->init();
173 register_module_init(e->init, e->type);
175 ret = 0;
178 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
179 QTAILQ_REMOVE(&dso_init_list, e, node);
180 g_free(e);
182 out:
183 return ret;
185 #endif
187 bool module_load_one(const char *prefix, const char *lib_name, bool mayfail)
189 bool success = false;
191 #ifdef CONFIG_MODULES
192 char *fname = NULL;
193 #ifdef CONFIG_MODULE_UPGRADES
194 char *version_dir;
195 #endif
196 const char *search_dir;
197 char *dirs[5];
198 char *module_name;
199 int i = 0, n_dirs = 0;
200 int ret;
201 bool export_symbols = false;
202 static GHashTable *loaded_modules;
203 const QemuModinfo *modinfo;
204 const char **sl;
206 if (!g_module_supported()) {
207 fprintf(stderr, "Module is not supported by system.\n");
208 return false;
211 if (!loaded_modules) {
212 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
215 module_name = g_strdup_printf("%s%s", prefix, lib_name);
217 if (g_hash_table_contains(loaded_modules, module_name)) {
218 g_free(module_name);
219 return true;
221 g_hash_table_add(loaded_modules, module_name);
223 for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
224 if (modinfo->deps) {
225 if (strcmp(modinfo->name, module_name) == 0) {
226 /* we depend on other module(s) */
227 for (sl = modinfo->deps; *sl != NULL; sl++) {
228 module_load_one("", *sl, false);
230 } else {
231 for (sl = modinfo->deps; *sl != NULL; sl++) {
232 if (strcmp(module_name, *sl) == 0) {
233 /* another module depends on us */
234 export_symbols = true;
241 search_dir = getenv("QEMU_MODULE_DIR");
242 if (search_dir != NULL) {
243 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
245 dirs[n_dirs++] = get_relocated_path(CONFIG_QEMU_MODDIR);
246 dirs[n_dirs++] = g_strdup(qemu_get_exec_dir());
248 #ifdef CONFIG_MODULE_UPGRADES
249 version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
250 G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
251 '_');
252 dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
253 #endif
255 assert(n_dirs <= ARRAY_SIZE(dirs));
257 for (i = 0; i < n_dirs; i++) {
258 fname = g_strdup_printf("%s/%s%s",
259 dirs[i], module_name, CONFIG_HOST_DSOSUF);
260 ret = module_load_file(fname, mayfail, export_symbols);
261 g_free(fname);
262 fname = NULL;
263 /* Try loading until loaded a module file */
264 if (!ret) {
265 success = true;
266 break;
270 if (!success) {
271 g_hash_table_remove(loaded_modules, module_name);
272 g_free(module_name);
275 for (i = 0; i < n_dirs; i++) {
276 g_free(dirs[i]);
279 #endif
280 return success;
283 #ifdef CONFIG_MODULES
285 static bool module_loaded_qom_all;
287 void module_load_qom_one(const char *type)
289 const QemuModinfo *modinfo;
290 const char **sl;
292 if (!type) {
293 return;
296 for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
297 if (!modinfo->objs) {
298 continue;
300 for (sl = modinfo->objs; *sl != NULL; sl++) {
301 if (strcmp(type, *sl) == 0) {
302 module_load_one("", modinfo->name, false);
308 void module_load_qom_all(void)
310 const QemuModinfo *modinfo;
312 if (module_loaded_qom_all) {
313 return;
316 for (modinfo = module_info; modinfo->name != NULL; modinfo++) {
317 if (!modinfo->objs) {
318 continue;
320 module_load_one("", modinfo->name, false);
322 module_loaded_qom_all = true;
325 #else
327 void module_load_qom_one(const char *type) {}
328 void module_load_qom_all(void) {}
330 #endif