dev-hid: convert init to realize
[qemu/ar7.git] / util / module.c
blob4bd4a94d87f290d374a456c28d9f802fc426c9bd
1 /*
2 * QEMU Module Infrastructure
4 * Copyright IBM, Corp. 2009
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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 <stdlib.h>
17 #include "qemu-common.h"
18 #ifdef CONFIG_MODULES
19 #include <gmodule.h>
20 #endif
21 #include "qemu/queue.h"
22 #include "qemu/module.h"
24 typedef struct ModuleEntry
26 void (*init)(void);
27 QTAILQ_ENTRY(ModuleEntry) node;
28 module_init_type type;
29 } ModuleEntry;
31 typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
33 static ModuleTypeList init_type_list[MODULE_INIT_MAX];
35 static ModuleTypeList dso_init_list;
37 static void init_lists(void)
39 static int inited;
40 int i;
42 if (inited) {
43 return;
46 for (i = 0; i < MODULE_INIT_MAX; i++) {
47 QTAILQ_INIT(&init_type_list[i]);
50 QTAILQ_INIT(&dso_init_list);
52 inited = 1;
56 static ModuleTypeList *find_type(module_init_type type)
58 ModuleTypeList *l;
60 init_lists();
62 l = &init_type_list[type];
64 return l;
67 void register_module_init(void (*fn)(void), module_init_type type)
69 ModuleEntry *e;
70 ModuleTypeList *l;
72 e = g_malloc0(sizeof(*e));
73 e->init = fn;
74 e->type = type;
76 l = find_type(type);
78 QTAILQ_INSERT_TAIL(l, e, node);
81 void register_dso_module_init(void (*fn)(void), module_init_type type)
83 ModuleEntry *e;
85 init_lists();
87 e = g_malloc0(sizeof(*e));
88 e->init = fn;
89 e->type = type;
91 QTAILQ_INSERT_TAIL(&dso_init_list, e, node);
94 static void module_load(module_init_type type);
96 void module_call_init(module_init_type type)
98 ModuleTypeList *l;
99 ModuleEntry *e;
101 module_load(type);
102 l = find_type(type);
104 QTAILQ_FOREACH(e, l, node) {
105 e->init();
109 #ifdef CONFIG_MODULES
110 static int module_load_file(const char *fname)
112 GModule *g_module;
113 void (*sym)(void);
114 const char *dsosuf = HOST_DSOSUF;
115 int len = strlen(fname);
116 int suf_len = strlen(dsosuf);
117 ModuleEntry *e, *next;
118 int ret;
120 if (len <= suf_len || strcmp(&fname[len - suf_len], dsosuf)) {
121 /* wrong suffix */
122 ret = -EINVAL;
123 goto out;
125 if (access(fname, F_OK)) {
126 ret = -ENOENT;
127 goto out;
130 assert(QTAILQ_EMPTY(&dso_init_list));
132 g_module = g_module_open(fname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
133 if (!g_module) {
134 fprintf(stderr, "Failed to open module: %s\n",
135 g_module_error());
136 ret = -EINVAL;
137 goto out;
139 if (!g_module_symbol(g_module, DSO_STAMP_FUN_STR, (gpointer *)&sym)) {
140 fprintf(stderr, "Failed to initialize module: %s\n",
141 fname);
142 /* Print some info if this is a QEMU module (but from different build),
143 * this will make debugging user problems easier. */
144 if (g_module_symbol(g_module, "qemu_module_dummy", (gpointer *)&sym)) {
145 fprintf(stderr,
146 "Note: only modules from the same build can be loaded.\n");
148 g_module_close(g_module);
149 ret = -EINVAL;
150 } else {
151 QTAILQ_FOREACH(e, &dso_init_list, node) {
152 register_module_init(e->init, e->type);
154 ret = 0;
157 QTAILQ_FOREACH_SAFE(e, &dso_init_list, node, next) {
158 QTAILQ_REMOVE(&dso_init_list, e, node);
159 g_free(e);
161 out:
162 return ret;
164 #endif
166 static void module_load(module_init_type type)
168 #ifdef CONFIG_MODULES
169 char *fname = NULL;
170 const char **mp;
171 static const char *block_modules[] = {
172 CONFIG_BLOCK_MODULES
174 char *exec_dir;
175 char *dirs[3];
176 int i = 0;
177 int ret;
179 if (!g_module_supported()) {
180 fprintf(stderr, "Module is not supported by system.\n");
181 return;
184 switch (type) {
185 case MODULE_INIT_BLOCK:
186 mp = block_modules;
187 break;
188 default:
189 /* no other types have dynamic modules for now*/
190 return;
193 exec_dir = qemu_get_exec_dir();
194 dirs[i++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
195 dirs[i++] = g_strdup_printf("%s/..", exec_dir ? : "");
196 dirs[i++] = g_strdup_printf("%s", exec_dir ? : "");
197 assert(i == ARRAY_SIZE(dirs));
198 g_free(exec_dir);
199 exec_dir = NULL;
201 for ( ; *mp; mp++) {
202 for (i = 0; i < ARRAY_SIZE(dirs); i++) {
203 fname = g_strdup_printf("%s/%s%s", dirs[i], *mp, HOST_DSOSUF);
204 ret = module_load_file(fname);
205 g_free(fname);
206 fname = NULL;
207 /* Try loading until loaded a module file */
208 if (!ret) {
209 break;
214 for (i = 0; i < ARRAY_SIZE(dirs); i++) {
215 g_free(dirs[i]);
218 #endif