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