2 * QEMU Module Infrastructure
4 * Copyright IBM, Corp. 2009
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"
20 #include "qemu/queue.h"
21 #include "qemu/module.h"
23 typedef struct ModuleEntry
26 QTAILQ_ENTRY(ModuleEntry) node;
27 module_init_type type;
30 typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
32 static ModuleTypeList init_type_list[MODULE_INIT_MAX];
34 static ModuleTypeList dso_init_list;
36 static void init_lists(void)
45 for (i = 0; i < MODULE_INIT_MAX; i++) {
46 QTAILQ_INIT(&init_type_list[i]);
49 QTAILQ_INIT(&dso_init_list);
55 static ModuleTypeList *find_type(module_init_type type)
59 return &init_type_list[type];
62 void register_module_init(void (*fn)(void), module_init_type type)
67 e = g_malloc0(sizeof(*e));
73 QTAILQ_INSERT_TAIL(l, e, node);
76 void register_dso_module_init(void (*fn)(void), module_init_type type)
82 e = g_malloc0(sizeof(*e));
86 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
89 void module_call_init(module_init_type type)
96 QTAILQ_FOREACH(e, l, node) {
101 #ifdef CONFIG_MODULES
102 static int module_load_file(const char *fname)
106 const char *dsosuf = HOST_DSOSUF;
107 int len = strlen(fname);
108 int suf_len = strlen(dsosuf);
109 ModuleEntry *e, *next;
112 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
117 if (access(fname, F_OK)) {
122 assert(QTAILQ_EMPTY(&dso_init_list));
124 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
126 fprintf(stderr, "Failed to open module: %s\n",
131 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
132 fprintf(stderr, "Failed to initialize module: %s\n",
134 /* Print some info if this is a QEMU module (but from different build),
135 * this will make debugging user problems easier. */
136 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
138 "Note: only modules from the same build can be loaded.\n");
140 g_module_close(g_module);
143 QTAILQ_FOREACH(e, &dso_init_list, node) {
145 register_module_init(e->init, e->type);
150 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
151 QTAILQ_REMOVE(&dso_init_list, e, node);
159 bool module_load_one(const char *prefix, const char *lib_name)
161 bool success = false;
163 #ifdef CONFIG_MODULES
166 const char *search_dir;
169 int i = 0, n_dirs = 0;
171 static GHashTable *loaded_modules;
173 if (!g_module_supported()) {
174 fprintf(stderr, "Module is not supported by system.\n");
178 if (!loaded_modules) {
179 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
182 module_name = g_strdup_printf("%s%s", prefix, lib_name);
184 if (!g_hash_table_add(loaded_modules, module_name)) {
189 exec_dir = qemu_get_exec_dir();
190 search_dir = getenv("QEMU_MODULE_DIR");
191 if (search_dir != NULL) {
192 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
194 dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
195 dirs[n_dirs++] = g_strdup_printf("%s/..", exec_dir ? : "");
196 dirs[n_dirs++] = g_strdup_printf("%s", exec_dir ? : "");
197 assert(n_dirs <= ARRAY_SIZE(dirs));
202 for (i = 0; i < n_dirs; i++) {
203 fname = g_strdup_printf("%s/%s%s",
204 dirs[i], module_name, HOST_DSOSUF);
205 ret = module_load_file(fname);
208 /* Try loading until loaded a module file */
216 g_hash_table_remove(loaded_modules, module_name);
220 for (i = 0; i < n_dirs; i++) {