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 * 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. 14 */ 15 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 23 typedef struct ModuleEntry 24 { 25 void (*init)(void); 26 QTAILQ_ENTRY(ModuleEntry) node; 27 module_init_type type; 28 } ModuleEntry; 29 30 typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList; 31 32 static ModuleTypeList init_type_list[MODULE_INIT_MAX]; 33 static bool modules_init_done[MODULE_INIT_MAX]; 34 35 static ModuleTypeList dso_init_list; 36 37 static void init_lists(void) 38 { 39 static int inited; 40 int i; 41 42 if (inited) { 43 return; 44 } 45 46 for (i = 0; i < MODULE_INIT_MAX; i++) { 47 QTAILQ_INIT(&init_type_list[i]); 48 } 49 50 QTAILQ_INIT(&dso_init_list); 51 52 inited = 1; 53 } 54 55 56 static ModuleTypeList *find_type(module_init_type type) 57 { 58 init_lists(); 59 60 return &init_type_list[type]; 61 } 62 63 void register_module_init(void (*fn)(void), module_init_type type) 64 { 65 ModuleEntry *e; 66 ModuleTypeList *l; 67 68 e = g_malloc0(sizeof(*e)); 69 e->init = fn; 70 e->type = type; 71 72 l = find_type(type); 73 74 QTAILQ_INSERT_TAIL(l, e, node); 75 } 76 77 void register_dso_module_init(void (*fn)(void), module_init_type type) 78 { 79 ModuleEntry *e; 80 81 init_lists(); 82 83 e = g_malloc0(sizeof(*e)); 84 e->init = fn; 85 e->type = type; 86 87 QTAILQ_INSERT_TAIL(&dso_init_list, e, node); 88 } 89 90 void module_call_init(module_init_type type) 91 { 92 ModuleTypeList *l; 93 ModuleEntry *e; 94 95 if (modules_init_done[type]) { 96 return; 97 } 98 99 l = find_type(type); 100 101 QTAILQ_FOREACH(e, l, node) { 102 e->init(); 103 } 104 105 modules_init_done[type] = true; 106 } 107 108 #ifdef CONFIG_MODULES 109 static int module_load_file(const char *fname) 110 { 111 GModule *g_module; 112 void (*sym)(void); 113 const char *dsosuf = HOST_DSOSUF; 114 int len = strlen(fname); 115 int suf_len = strlen(dsosuf); 116 ModuleEntry *e, *next; 117 int ret; 118 119 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) { 120 /* wrong suffix */ 121 ret = -EINVAL; 122 goto out; 123 } 124 if (access(fname, F_OK)) { 125 ret = -ENOENT; 126 goto out; 127 } 128 129 assert(QTAILQ_EMPTY(&dso_init_list)); 130 131 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL); 132 if (!g_module) { 133 fprintf(stderr, "Failed to open module: %s\n", 134 g_module_error()); 135 ret = -EINVAL; 136 goto out; 137 } 138 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) { 139 fprintf(stderr, "Failed to initialize module: %s\n", 140 fname); 141 /* Print some info if this is a QEMU module (but from different build), 142 * this will make debugging user problems easier. */ 143 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) { 144 fprintf(stderr, 145 "Note: only modules from the same build can be loaded.\n"); 146 } 147 g_module_close(g_module); 148 ret = -EINVAL; 149 } else { 150 QTAILQ_FOREACH(e, &dso_init_list, node) { 151 e->init(); 152 register_module_init(e->init, e->type); 153 } 154 ret = 0; 155 } 156 157 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) { 158 QTAILQ_REMOVE(&dso_init_list, e, node); 159 g_free(e); 160 } 161 out: 162 return ret; 163 } 164 #endif 165 166 bool module_load_one(const char *prefix, const char *lib_name) 167 { 168 bool success = false; 169 170 #ifdef CONFIG_MODULES 171 char *fname = NULL; 172 char *exec_dir; 173 const char *search_dir; 174 char *dirs[4]; 175 char *module_name; 176 int i = 0, n_dirs = 0; 177 int ret; 178 static GHashTable *loaded_modules; 179 180 if (!g_module_supported()) { 181 fprintf(stderr, "Module is not supported by system.\n"); 182 return false; 183 } 184 185 if (!loaded_modules) { 186 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal); 187 } 188 189 module_name = g_strdup_printf("%s%s", prefix, lib_name); 190 191 if (!g_hash_table_add(loaded_modules, module_name)) { 192 g_free(module_name); 193 return true; 194 } 195 196 exec_dir = qemu_get_exec_dir(); 197 search_dir = getenv("QEMU_MODULE_DIR"); 198 if (search_dir != NULL) { 199 dirs[n_dirs++] = g_strdup_printf("%s", search_dir); 200 } 201 dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR); 202 dirs[n_dirs++] = g_strdup_printf("%s/..", exec_dir ? : ""); 203 dirs[n_dirs++] = g_strdup_printf("%s", exec_dir ? : ""); 204 assert(n_dirs <= ARRAY_SIZE(dirs)); 205 206 g_free(exec_dir); 207 exec_dir = NULL; 208 209 for (i = 0; i < n_dirs; i++) { 210 fname = g_strdup_printf("%s/%s%s", 211 dirs[i], module_name, HOST_DSOSUF); 212 ret = module_load_file(fname); 213 g_free(fname); 214 fname = NULL; 215 /* Try loading until loaded a module file */ 216 if (!ret) { 217 success = true; 218 break; 219 } 220 } 221 222 if (!success) { 223 g_hash_table_remove(loaded_modules, module_name); 224 g_free(module_name); 225 } 226 227 for (i = 0; i < n_dirs; i++) { 228 g_free(dirs[i]); 229 } 230 231 #endif 232 return success; 233 } 234