eccb5352 |
1 | |
2 | /* module.c : Implementation of the module loading/unloading mechanism. |
3 | * |
4 | */ |
5 | |
3d218f2a |
6 | #include <lttv/module.h> |
dc877563 |
7 | |
8 | |
9 | struct _LttvModule |
10 | { |
11 | GModule *module; |
12 | guint ref_count; |
13 | guint load_count; |
14 | GPtrArray *dependents; |
15 | }; |
16 | |
eccb5352 |
17 | |
18 | /* Table of loaded modules and paths where to search for modules */ |
19 | |
20 | static GHashTable *modules = NULL; |
21 | |
eccb5352 |
22 | static GPtrArray *modulesPaths = NULL; |
23 | |
dc877563 |
24 | static void lttv_module_unload_all(); |
25 | |
26 | |
27 | void lttv_module_init(int argc, char **argv) |
28 | { |
eccb5352 |
29 | modules = g_hash_table_new(g_str_hash, g_str_equal); |
eccb5352 |
30 | modulesPaths = g_ptr_array_new(); |
31 | } |
32 | |
dc877563 |
33 | |
34 | void lttv_module_destroy() |
35 | { |
eccb5352 |
36 | int i; |
37 | |
38 | /* Unload all modules */ |
39 | lttv_module_unload_all(); |
40 | |
41 | /* Free the modules paths pointer array as well as the elements */ |
dc877563 |
42 | for(i = 0; i < modulesPaths->len ; i++) { |
eccb5352 |
43 | g_free(modulesPaths->pdata[i]); |
44 | } |
45 | g_ptr_array_free(modulesPaths,TRUE) ; |
eccb5352 |
46 | modulesPaths = NULL; |
eccb5352 |
47 | |
48 | /* destroy the hash table */ |
49 | g_hash_table_destroy(modules) ; |
50 | modules = NULL; |
51 | } |
52 | |
dc877563 |
53 | |
eccb5352 |
54 | /* Add a new pathname to the modules loading search path */ |
55 | |
dc877563 |
56 | void lttv_module_path_add(const char *name) |
57 | { |
eccb5352 |
58 | g_ptr_array_add(modulesPaths,(char*)g_strdup(name)); |
59 | } |
60 | |
61 | |
ffd54a90 |
62 | static LttvModule * |
dc877563 |
63 | module_load(const char *name, int argc, char **argv) |
64 | { |
65 | GModule *gm; |
eccb5352 |
66 | |
dc877563 |
67 | LttvModule *m; |
eccb5352 |
68 | |
69 | int i; |
70 | |
71 | char *pathname; |
ffd54a90 |
72 | |
73 | const char *module_name; |
eccb5352 |
74 | |
dc877563 |
75 | LttvModuleInit init_function; |
eccb5352 |
76 | |
dc877563 |
77 | /* Try to find the module along all the user specified paths */ |
eccb5352 |
78 | |
79 | for(i = 0 ; i < modulesPaths->len ; i++) { |
80 | pathname = g_module_build_path(modulesPaths->pdata[i],name); |
558aa013 |
81 | gm = g_module_open(pathname,G_MODULE_BIND_LAZY); |
82 | g_critical("module : %s", pathname); |
83 | g_critical("erreur : %s", g_module_error()); |
dc877563 |
84 | g_free(pathname); |
eccb5352 |
85 | |
dc877563 |
86 | if(gm != NULL) break; |
87 | } |
88 | |
89 | /* Try the default system path */ |
90 | |
91 | if(gm == NULL) { |
92 | pathname = g_module_build_path(NULL,name); |
558aa013 |
93 | gm = g_module_open(pathname,G_MODULE_BIND_LAZY); |
94 | g_critical("module : %s", pathname); |
eccb5352 |
95 | g_free(pathname); |
96 | } |
eccb5352 |
97 | |
dc877563 |
98 | /* Module cannot be found */ |
558aa013 |
99 | if(gm==NULL) |
100 | g_critical("module est null"); |
dc877563 |
101 | if(gm == NULL) return NULL; |
102 | |
103 | /* Check if the module was already opened using the hopefully canonical name |
104 | returned by g_module_name. */ |
105 | |
ffd54a90 |
106 | module_name = g_module_name(gm); |
eccb5352 |
107 | |
ffd54a90 |
108 | m = g_hash_table_lookup(modules, module_name); |
eccb5352 |
109 | |
dc877563 |
110 | if(m == NULL) { |
eccb5352 |
111 | |
dc877563 |
112 | /* Module loaded for the first time. Insert it in the table and call the |
113 | init function if any. */ |
eccb5352 |
114 | |
ffd54a90 |
115 | m = g_new(LttvModule, 1); |
dc877563 |
116 | m->module = gm; |
117 | m->ref_count = 0; |
118 | m->load_count = 0; |
119 | m->dependents = g_ptr_array_new(); |
ffd54a90 |
120 | g_hash_table_insert(modules, (gpointer)module_name, m); |
dc877563 |
121 | |
122 | if(!g_module_symbol(gm, "init", (gpointer)&init_function)) { |
123 | g_warning("module %s (%s) has no init function", name, pathname); |
124 | } |
c432246e |
125 | else init_function(m, argc, argv); |
eccb5352 |
126 | } |
127 | else { |
dc877563 |
128 | |
129 | /* Module was already opened, check that it really is the same and |
130 | undo the extra g_module_open */ |
131 | |
132 | if(m->module != gm) g_error("Two gmodules with the same pathname"); |
133 | g_module_close(gm); |
eccb5352 |
134 | } |
135 | |
dc877563 |
136 | m->ref_count++; |
137 | return m; |
eccb5352 |
138 | } |
139 | |
eccb5352 |
140 | |
ffd54a90 |
141 | LttvModule * |
dc877563 |
142 | lttv_module_load(const char *name, int argc, char **argv) |
143 | { |
144 | LttvModule *m = module_load(name, argc, argv); |
dc877563 |
145 | if(m != NULL) m->load_count++; |
146 | return m; |
147 | } |
eccb5352 |
148 | |
eccb5352 |
149 | |
dc877563 |
150 | LttvModule * |
151 | lttv_module_require(LttvModule *m, const char *name, int argc, char **argv) |
152 | { |
153 | LttvModule *module; |
eccb5352 |
154 | |
dc877563 |
155 | module = module_load(name, argc, argv); |
156 | if(module != NULL) g_ptr_array_add(m->dependents, module); |
157 | return module; |
eccb5352 |
158 | } |
159 | |
160 | |
dc877563 |
161 | static void module_unload(LttvModule *m) |
162 | { |
163 | LttvModuleDestroy destroy_function; |
eccb5352 |
164 | |
dc877563 |
165 | char *pathname; |
eccb5352 |
166 | |
ffd54a90 |
167 | guint i, len; |
eccb5352 |
168 | |
dc877563 |
169 | /* Decrement the reference count */ |
eccb5352 |
170 | |
dc877563 |
171 | m->ref_count--; |
172 | if(m->ref_count > 0) return; |
eccb5352 |
173 | |
dc877563 |
174 | /* We really have to unload the module, first unload its dependents */ |
eccb5352 |
175 | |
dc877563 |
176 | len = m->dependents->len; |
eccb5352 |
177 | |
dc877563 |
178 | for(i = 0 ; i < len ; i++) { |
179 | module_unload(m->dependents->pdata[i]); |
eccb5352 |
180 | } |
eccb5352 |
181 | |
dc877563 |
182 | if(len != m->dependents->len) g_error("dependents list modified"); |
183 | |
184 | /* Unload the module itself */ |
185 | |
186 | if(!g_module_symbol(m->module, "destroy", (gpointer)&destroy_function)) { |
187 | g_warning("module (%s) has no destroy function", pathname); |
eccb5352 |
188 | } |
dc877563 |
189 | else destroy_function(); |
eccb5352 |
190 | |
dc877563 |
191 | g_hash_table_remove(modules, g_module_name(m->module)); |
192 | g_ptr_array_free(m->dependents, TRUE); |
193 | g_module_close(m->module); |
194 | g_free(m); |
195 | } |
eccb5352 |
196 | |
eccb5352 |
197 | |
dc877563 |
198 | void lttv_module_unload(LttvModule *m) |
199 | { |
200 | if(m->load_count <= 0) { |
201 | g_error("more unload than load (%s)", g_module_name(m->module)); |
202 | return; |
eccb5352 |
203 | } |
dc877563 |
204 | m->load_count--; |
205 | module_unload(m); |
206 | } |
207 | |
eccb5352 |
208 | |
dc877563 |
209 | static void |
210 | list_modules(gpointer key, gpointer value, gpointer user_data) |
211 | { |
212 | g_ptr_array_add((GPtrArray *)user_data, value); |
eccb5352 |
213 | } |
214 | |
dc877563 |
215 | |
216 | LttvModule ** |
217 | lttv_module_list(guint *nb) |
218 | { |
219 | GPtrArray *list = g_ptr_array_new(); |
220 | |
221 | LttvModule **array; |
222 | |
223 | g_hash_table_foreach(modules, list_modules, list); |
224 | *nb = list->len; |
225 | array = (LttvModule **)list->pdata; |
226 | g_ptr_array_free(list, FALSE); |
227 | return array; |
228 | } |
229 | |
230 | |
231 | LttvModule ** |
232 | lttv_module_info(LttvModule *m, const char **name, |
233 | guint *ref_count, guint *load_count, guint *nb_dependents) |
234 | { |
235 | guint i, len = m->dependents->len; |
236 | |
237 | LttvModule **array = g_new(LttvModule *, len); |
238 | |
239 | *name = g_module_name(m->module); |
240 | *ref_count = m->ref_count; |
241 | *load_count = m->load_count; |
242 | *nb_dependents = len; |
243 | for(i = 0 ; i < len ; i++) array[i] = m->dependents->pdata[i]; |
244 | return array; |
245 | } |
246 | |
247 | |
248 | static void |
249 | list_independent(gpointer key, gpointer value, gpointer user_data) |
250 | { |
251 | LttvModule *m = (LttvModule *)value; |
252 | |
253 | if(m->load_count > 0) g_ptr_array_add((GPtrArray *)user_data, m); |
254 | } |
255 | |
256 | |
257 | void |
258 | lttv_module_unload_all() |
259 | { |
260 | guint i; |
261 | |
262 | LttvModule *m; |
263 | |
264 | GPtrArray *independent_modules = g_ptr_array_new(); |
265 | |
266 | g_hash_table_foreach(modules, list_independent, independent_modules); |
267 | |
268 | for(i = 0 ; i < independent_modules->len ; i++) { |
ffd54a90 |
269 | m = (LttvModule *)independent_modules->pdata[i]; |
dc877563 |
270 | while(m->load_count > 0) lttv_module_unload(m); |
eccb5352 |
271 | } |
dc877563 |
272 | |
273 | g_ptr_array_free(independent_modules, TRUE); |
274 | if(g_hash_table_size(modules) != 0) g_warning("cannot unload all modules"); |
eccb5352 |
275 | } |