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"
22 #ifdef CONFIG_MODULE_UPGRADES
23 #include "qemu-version.h"
26 typedef struct ModuleEntry
29 QTAILQ_ENTRY(ModuleEntry) node;
30 module_init_type type;
33 typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
35 static ModuleTypeList init_type_list[MODULE_INIT_MAX];
36 static bool modules_init_done[MODULE_INIT_MAX];
38 static ModuleTypeList dso_init_list;
40 static void init_lists(void)
49 for (i = 0; i < MODULE_INIT_MAX; i++) {
50 QTAILQ_INIT(&init_type_list[i]);
53 QTAILQ_INIT(&dso_init_list);
59 static ModuleTypeList *find_type(module_init_type type)
63 return &init_type_list[type];
66 void register_module_init(void (*fn)(void), module_init_type type)
71 e = g_malloc0(sizeof(*e));
77 QTAILQ_INSERT_TAIL(l, e, node);
80 void register_dso_module_init(void (*fn)(void), module_init_type type)
86 e = g_malloc0(sizeof(*e));
90 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
93 void module_call_init(module_init_type type)
98 if (modules_init_done[type]) {
104 QTAILQ_FOREACH(e, l, node) {
108 modules_init_done[type] = true;
111 #ifdef CONFIG_MODULES
112 static int module_load_file(const char *fname)
116 const char *dsosuf = HOST_DSOSUF;
117 int len = strlen(fname);
118 int suf_len = strlen(dsosuf);
119 ModuleEntry *e, *next;
122 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
127 if (access(fname, F_OK)) {
132 assert(QTAILQ_EMPTY(&dso_init_list));
134 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
136 fprintf(stderr, "Failed to open module: %s\n",
141 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
142 fprintf(stderr, "Failed to initialize module: %s\n",
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)) {
148 "Note: only modules from the same build can be loaded.\n");
150 g_module_close(g_module);
153 QTAILQ_FOREACH(e, &dso_init_list, node) {
155 register_module_init(e->init, e->type);
160 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
161 QTAILQ_REMOVE(&dso_init_list, e, node);
169 bool module_load_one(const char *prefix, const char *lib_name)
171 bool success = false;
173 #ifdef CONFIG_MODULES
176 #ifdef CONFIG_MODULE_UPGRADES
179 const char *search_dir;
182 int i = 0, n_dirs = 0;
184 static GHashTable *loaded_modules;
186 if (!g_module_supported()) {
187 fprintf(stderr, "Module is not supported by system.\n");
191 if (!loaded_modules) {
192 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
195 module_name = g_strdup_printf("%s%s", prefix, lib_name);
197 if (!g_hash_table_add(loaded_modules, module_name)) {
202 exec_dir = qemu_get_exec_dir();
203 search_dir = getenv("QEMU_MODULE_DIR");
204 if (search_dir != NULL) {
205 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
207 dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
208 dirs[n_dirs++] = g_strdup_printf("%s/..", exec_dir ? : "");
209 dirs[n_dirs++] = g_strdup_printf("%s", exec_dir ? : "");
211 #ifdef CONFIG_MODULE_UPGRADES
212 version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
213 G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "+-.~",
215 dirs[n_dirs++] = g_strdup_printf("/var/run/qemu/%s", version_dir);
218 assert(n_dirs <= ARRAY_SIZE(dirs));
223 for (i = 0; i < n_dirs; i++) {
224 fname = g_strdup_printf("%s/%s%s",
225 dirs[i], module_name, HOST_DSOSUF);
226 ret = module_load_file(fname);
229 /* Try loading until loaded a module file */
237 g_hash_table_remove(loaded_modules, module_name);
241 for (i = 0; i < n_dirs; i++) {