]> git.proxmox.com Git - mirror_qemu.git/blame - util/module.c
Revert "vl: Fix to create migration object before block backends again"
[mirror_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 <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 *
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"
d844a7b6 17#include "qemu-common.h"
aa0d1f44 18#ifdef CONFIG_MODULES
e26110cf 19#include <gmodule.h>
aa0d1f44 20#endif
1de7afc9
PB
21#include "qemu/queue.h"
22#include "qemu/module.h"
0bfe3ca5
AL
23
24typedef struct ModuleEntry
25{
0bfe3ca5 26 void (*init)(void);
72cf2d4f 27 QTAILQ_ENTRY(ModuleEntry) node;
e26110cf 28 module_init_type type;
0bfe3ca5
AL
29} ModuleEntry;
30
72cf2d4f 31typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
0bfe3ca5 32
f7897430 33static ModuleTypeList init_type_list[MODULE_INIT_MAX];
0bfe3ca5 34
e26110cf
FZ
35static ModuleTypeList dso_init_list;
36
37static void init_lists(void)
0bfe3ca5 38{
f7897430
AL
39 static int inited;
40 int i;
0bfe3ca5 41
f7897430
AL
42 if (inited) {
43 return;
0bfe3ca5
AL
44 }
45
f7897430 46 for (i = 0; i < MODULE_INIT_MAX; i++) {
72cf2d4f 47 QTAILQ_INIT(&init_type_list[i]);
f7897430 48 }
0bfe3ca5 49
e26110cf
FZ
50 QTAILQ_INIT(&dso_init_list);
51
f7897430
AL
52 inited = 1;
53}
0bfe3ca5 54
0bfe3ca5 55
f7897430
AL
56static ModuleTypeList *find_type(module_init_type type)
57{
e26110cf 58 init_lists();
f7897430 59
9be38598 60 return &init_type_list[type];
0bfe3ca5
AL
61}
62
63void register_module_init(void (*fn)(void), module_init_type type)
64{
65 ModuleEntry *e;
66 ModuleTypeList *l;
67
7267c094 68 e = g_malloc0(sizeof(*e));
0bfe3ca5 69 e->init = fn;
e26110cf 70 e->type = type;
0bfe3ca5 71
f7897430 72 l = find_type(type);
0bfe3ca5 73
72cf2d4f 74 QTAILQ_INSERT_TAIL(l, e, node);
0bfe3ca5
AL
75}
76
e26110cf
FZ
77void 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
0bfe3ca5
AL
90void module_call_init(module_init_type type)
91{
92 ModuleTypeList *l;
93 ModuleEntry *e;
94
f7897430 95 l = find_type(type);
0bfe3ca5 96
72cf2d4f 97 QTAILQ_FOREACH(e, l, node) {
0bfe3ca5
AL
98 e->init();
99 }
100}
e26110cf
FZ
101
102#ifdef CONFIG_MODULES
103static int module_load_file(const char *fname)
104{
105 GModule *g_module;
106 void (*sym)(void);
107 const char *dsosuf = HOST_DSOSUF;
108 int len = strlen(fname);
109 int suf_len = strlen(dsosuf);
110 ModuleEntry *e, *next;
111 int ret;
112
113 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
114 /* wrong suffix */
115 ret = -EINVAL;
116 goto out;
117 }
118 if (access(fname, F_OK)) {
119 ret = -ENOENT;
120 goto out;
121 }
122
123 assert(QTAILQ_EMPTY(&dso_init_list));
124
125 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
126 if (!g_module) {
127 fprintf(stderr, "Failed to open module: %s\n",
128 g_module_error());
129 ret = -EINVAL;
130 goto out;
131 }
132 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
133 fprintf(stderr, "Failed to initialize module: %s\n",
134 fname);
135 /* Print some info if this is a QEMU module (but from different build),
136 * this will make debugging user problems easier. */
137 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
138 fprintf(stderr,
139 "Note: only modules from the same build can be loaded.\n");
140 }
141 g_module_close(g_module);
142 ret = -EINVAL;
143 } else {
144 QTAILQ_FOREACH(e, &dso_init_list, node) {
88d88798 145 e->init();
e26110cf
FZ
146 register_module_init(e->init, e->type);
147 }
148 ret = 0;
149 }
150
151 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
152 QTAILQ_REMOVE(&dso_init_list, e, node);
153 g_free(e);
154 }
155out:
156 return ret;
157}
158#endif
159
88d88798 160void module_load_one(const char *prefix, const char *lib_name)
e26110cf
FZ
161{
162#ifdef CONFIG_MODULES
163 char *fname = NULL;
e26110cf 164 char *exec_dir;
900610e6 165 const char *search_dir;
166 char *dirs[4];
dffa41b4 167 char *module_name;
900610e6 168 int i = 0, n_dirs = 0;
e26110cf 169 int ret;
dffa41b4 170 static GHashTable *loaded_modules;
e26110cf
FZ
171
172 if (!g_module_supported()) {
173 fprintf(stderr, "Module is not supported by system.\n");
174 return;
175 }
176
dffa41b4
FZ
177 if (!loaded_modules) {
178 loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
179 }
180
181 module_name = g_strdup_printf("%s%s", prefix, lib_name);
182
183 if (g_hash_table_lookup(loaded_modules, module_name)) {
184 g_free(module_name);
185 return;
186 }
187 g_hash_table_insert(loaded_modules, module_name, module_name);
188
e26110cf 189 exec_dir = qemu_get_exec_dir();
900610e6 190 search_dir = getenv("QEMU_MODULE_DIR");
191 if (search_dir != NULL) {
192 dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
193 }
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));
198
e26110cf
FZ
199 g_free(exec_dir);
200 exec_dir = NULL;
201
900610e6 202 for (i = 0; i < n_dirs; i++) {
dffa41b4
FZ
203 fname = g_strdup_printf("%s/%s%s",
204 dirs[i], module_name, HOST_DSOSUF);
88d88798
MM
205 ret = module_load_file(fname);
206 g_free(fname);
207 fname = NULL;
208 /* Try loading until loaded a module file */
209 if (!ret) {
210 break;
e26110cf 211 }
e26110cf
FZ
212 }
213
900610e6 214 for (i = 0; i < n_dirs; i++) {
e26110cf
FZ
215 g_free(dirs[i]);
216 }
217
218#endif
219}