xref: /qemu/util/module.c (revision 2c7c4cf0)
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 #include "qemu-common.h"
18 #ifdef CONFIG_MODULES
19 #include <gmodule.h>
20 #endif
21 #include "qemu/queue.h"
22 #include "qemu/module.h"
23 
24 typedef struct ModuleEntry
25 {
26     void (*init)(void);
27     QTAILQ_ENTRY(ModuleEntry) node;
28     module_init_type type;
29 } ModuleEntry;
30 
31 typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
32 
33 static ModuleTypeList init_type_list[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     l = find_type(type);
96 
97     QTAILQ_FOREACH(e, l, node) {
98         e->init();
99     }
100 }
101 
102 #ifdef CONFIG_MODULES
103 static 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) {
145             e->init();
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     }
155 out:
156     return ret;
157 }
158 #endif
159 
160 void module_load_one(const char *prefix, const char *lib_name)
161 {
162 #ifdef CONFIG_MODULES
163     char *fname = NULL;
164     char *exec_dir;
165     char *dirs[3];
166     int i = 0;
167     int ret;
168 
169     if (!g_module_supported()) {
170         fprintf(stderr, "Module is not supported by system.\n");
171         return;
172     }
173 
174     exec_dir = qemu_get_exec_dir();
175     dirs[i++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
176     dirs[i++] = g_strdup_printf("%s/..", exec_dir ? : "");
177     dirs[i++] = g_strdup_printf("%s", exec_dir ? : "");
178     assert(i == ARRAY_SIZE(dirs));
179     g_free(exec_dir);
180     exec_dir = NULL;
181 
182     for (i = 0; i < ARRAY_SIZE(dirs); i++) {
183         fname = g_strdup_printf("%s/%s%s%s",
184                 dirs[i], prefix, lib_name, HOST_DSOSUF);
185         ret = module_load_file(fname);
186         g_free(fname);
187         fname = NULL;
188         /* Try loading until loaded a module file */
189         if (!ret) {
190             break;
191         }
192     }
193 
194     for (i = 0; i < ARRAY_SIZE(dirs); i++) {
195         g_free(dirs[i]);
196     }
197 
198 #endif
199 }
200