1 /*
2 modules-load.c : irssi
3
4 Copyright (C) 1999-2001 Timo Sirainen
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "module.h"
22 #include "modules.h"
23 #include "modules-load.h"
24 #include "signals.h"
25
26 #include "settings.h"
27 #include "commands.h"
28 #include "misc.h"
29
30 #ifdef HAVE_GMODULE
31
32 /* Returns the module name without path, "lib" prefix or ".so" suffix */
module_get_name(const char * path,int * start,int * end)33 static char *module_get_name(const char *path, int *start, int *end)
34 {
35 const char *name;
36 char *module_name, *ptr;
37
38 name = NULL;
39 if (*path == '~' || g_path_is_absolute(path)) {
40 name = strrchr(path, G_DIR_SEPARATOR);
41 if (name != NULL) name++;
42 }
43
44 if (name == NULL)
45 name = path;
46
47 if (strncmp(name, "lib", 3) == 0)
48 name += 3;
49
50 module_name = g_strdup(name);
51 ptr = strchr(module_name, '.');
52 if (ptr != NULL) *ptr = '\0';
53
54 *start = (int) (name-path);
55 *end = *start + (ptr == NULL ? strlen(name) :
56 (int) (ptr-module_name));
57
58 return module_name;
59 }
60
61 /* Returns the root module name for given submodule (eg. perl_core -> perl) */
module_get_root(const char * name,char ** prefixes)62 static char *module_get_root(const char *name, char **prefixes)
63 {
64 int len;
65
66 /* skip any of the prefixes.. */
67 if (prefixes != NULL) {
68 while (*prefixes != NULL) {
69 len = strlen(*prefixes);
70 if (strncmp(name, *prefixes, len) == 0 &&
71 name[len] == '_') {
72 name += len+1;
73 break;
74 }
75 prefixes++;
76 }
77 }
78
79 /* skip the _core part */
80 len = strlen(name);
81 if (len > 5 && g_strcmp0(name+len-5, "_core") == 0)
82 return g_strndup(name, len-5);
83
84 return g_strdup(name);
85 }
86
87 /* Returns the sub module name for given submodule (eg. perl_core -> core) */
module_get_sub(const char * name,const char * root)88 static char *module_get_sub(const char *name, const char *root)
89 {
90 int rootlen, namelen;
91
92 namelen = strlen(name);
93 rootlen = strlen(root);
94 g_return_val_if_fail(namelen >= rootlen, g_strdup(name));
95
96 if (strncmp(name, root, rootlen) == 0 &&
97 g_strcmp0(name+rootlen, "_core") == 0)
98 return g_strdup("core");
99
100 if (namelen > rootlen && name[namelen-rootlen-1] == '_' &&
101 g_strcmp0(name+namelen-rootlen, root) == 0)
102 return g_strndup(name, namelen-rootlen-1);
103
104 return g_strdup(name);
105 }
106
module_open(const char * name,int * found)107 static GModule *module_open(const char *name, int *found)
108 {
109 struct stat statbuf;
110 GModule *module;
111 char *path, *str;
112
113 if (g_path_is_absolute(name) || *name == '~' ||
114 (*name == '.' && name[1] == G_DIR_SEPARATOR))
115 path = g_strdup(name);
116 else {
117 /* first try from home dir */
118 str = g_strdup_printf("%s/modules", get_irssi_dir());
119 path = g_module_build_path(str, name);
120 g_free(str);
121
122 if (stat(path, &statbuf) == 0) {
123 module = g_module_open(path, (GModuleFlags) 0);
124 g_free(path);
125 *found = TRUE;
126 return module;
127 }
128
129 /* module not found from home dir, try global module dir */
130 g_free(path);
131 path = g_module_build_path(MODULEDIR, name);
132 }
133
134 *found = stat(path, &statbuf) == 0;
135 module = g_module_open(path, (GModuleFlags) 0);
136 g_free(path);
137 return module;
138 }
139
module_get_func(const char * rootmodule,const char * submodule,const char * function)140 static char *module_get_func(const char *rootmodule, const char *submodule,
141 const char *function)
142 {
143 if (g_strcmp0(submodule, "core") == 0)
144 return g_strconcat(rootmodule, "_core_", function, NULL);
145
146 if (g_strcmp0(rootmodule, submodule) == 0)
147 return g_strconcat(rootmodule, "_", function, NULL);
148
149 return g_strconcat(submodule, "_", rootmodule, "_", function, NULL);
150 }
151
152 #define module_error(error, text, rootmodule, submodule) \
153 signal_emit("module error", 4, GINT_TO_POINTER(error), text, \
154 rootmodule, submodule)
155
156 /* Returns 1 if ok, 0 if error in module and
157 -1 if module wasn't found */
module_load_name(const char * path,const char * rootmodule,const char * submodule,int silent)158 static int module_load_name(const char *path, const char *rootmodule,
159 const char *submodule, int silent)
160 {
161 void (*module_init) (void);
162 void (*module_deinit) (void);
163 void (*module_version) (int *);
164 GModule *gmodule;
165 MODULE_REC *module;
166 MODULE_FILE_REC *rec;
167 gpointer value_version = NULL;
168 gpointer value1, value2 = NULL;
169 char *versionfunc, *initfunc, *deinitfunc;
170 int module_abi_version = 0;
171 int found;
172
173 gmodule = module_open(path, &found);
174 if (gmodule == NULL) {
175 if (!silent || found) {
176 module_error(MODULE_ERROR_LOAD, g_module_error(),
177 rootmodule, submodule);
178 }
179 return found ? 0 : -1;
180 }
181
182 /* get the module's irssi abi version and bail out on mismatch */
183 versionfunc = module_get_func(rootmodule, submodule, "abicheck");
184 if (!g_module_symbol(gmodule, versionfunc, &value_version)) {
185 g_free(versionfunc);
186 module_error(MODULE_ERROR_VERSION_MISMATCH, "0",
187 rootmodule, submodule);
188 g_module_close(gmodule);
189 return 0;
190 }
191 g_free(versionfunc);
192 module_version = value_version;
193 module_version(&module_abi_version);
194 if (module_abi_version != IRSSI_ABI_VERSION) {
195 char *module_abi_versionstr = g_strdup_printf("%d", module_abi_version);
196 module_error(MODULE_ERROR_VERSION_MISMATCH, module_abi_versionstr,
197 rootmodule, submodule);
198 g_free(module_abi_versionstr);
199 g_module_close(gmodule);
200 return 0;
201 }
202
203 /* get the module's init() and deinit() functions */
204 initfunc = module_get_func(rootmodule, submodule, "init");
205 deinitfunc = module_get_func(rootmodule, submodule, "deinit");
206 found = g_module_symbol(gmodule, initfunc, &value1) &&
207 g_module_symbol(gmodule, deinitfunc, &value2);
208 g_free(initfunc);
209 g_free(deinitfunc);
210
211 if (!found) {
212 module_error(MODULE_ERROR_INVALID, NULL,
213 rootmodule, submodule);
214 g_module_close(gmodule);
215 return 0;
216 }
217
218 module_init = value1;
219 module_deinit = value2;
220
221 /* Call the module's init() function - it should register itself
222 with module_register() function, abort if it doesn't. */
223 module_init();
224
225 module = module_find(rootmodule);
226 rec = module == NULL ? NULL :
227 g_strcmp0(rootmodule, submodule) == 0 ?
228 module_file_find(module, "core") :
229 module_file_find(module, submodule);
230 if (rec == NULL) {
231 rec = module_register_full(rootmodule, submodule, NULL);
232 rec->gmodule = gmodule;
233 module_file_unload(rec);
234
235 module_error(MODULE_ERROR_INVALID, NULL,
236 rootmodule, submodule);
237 return 0;
238 }
239
240 rec->module_deinit = module_deinit;
241 rec->gmodule = gmodule;
242 rec->initialized = TRUE;
243
244 settings_check_module(rec->defined_module_name);
245
246 signal_emit("module loaded", 2, rec->root, rec);
247 return 1;
248 }
249
module_load_prefixes(const char * path,const char * module,int start,int end,char ** prefixes)250 static int module_load_prefixes(const char *path, const char *module,
251 int start, int end, char **prefixes)
252 {
253 GString *realpath;
254 int status, ok;
255
256 /* load module_core */
257 realpath = g_string_new(path);
258 g_string_insert(realpath, end, "_core");
259
260 /* Don't print the error message the first time, since the module
261 may not have the core part at all. */
262 status = module_load_name(realpath->str, module, "core", TRUE);
263 ok = status > 0;
264
265 if (prefixes != NULL) {
266 /* load all the "prefix modules", like the fe-common, irc,
267 etc. part of the module */
268 while (*prefixes != NULL) {
269 g_string_assign(realpath, path);
270 g_string_insert_c(realpath, start, '_');
271 g_string_insert(realpath, start, *prefixes);
272
273 status = module_load_name(realpath->str, module,
274 *prefixes, TRUE);
275 if (status > 0)
276 ok = TRUE;
277
278 prefixes++;
279 }
280 }
281
282 if (!ok) {
283 /* error loading module, print the error message */
284 g_string_assign(realpath, path);
285 g_string_insert(realpath, end, "_core");
286 module_load_name(realpath->str, module, "core", FALSE);
287 }
288
289 g_string_free(realpath, TRUE);
290 return ok;
291 }
292
module_load_full(const char * path,const char * rootmodule,const char * submodule,int start,int end,char ** prefixes)293 static int module_load_full(const char *path, const char *rootmodule,
294 const char *submodule, int start, int end,
295 char **prefixes)
296 {
297 MODULE_REC *module;
298 int status, try_prefixes;
299
300 if (!g_module_supported())
301 return FALSE;
302
303 module = module_find(rootmodule);
304 if (module != NULL && (g_strcmp0(submodule, rootmodule) == 0 ||
305 module_file_find(module, submodule) != NULL)) {
306 /* module is already loaded */
307 module_error(MODULE_ERROR_ALREADY_LOADED, NULL,
308 rootmodule, submodule);
309 return FALSE;
310 }
311
312 /* check if the given module exists.. */
313 try_prefixes = g_strcmp0(rootmodule, submodule) == 0;
314 status = module_load_name(path, rootmodule, submodule, try_prefixes);
315 if (status == -1 && try_prefixes) {
316 /* nope, try loading the module_core,
317 fe_module, etc. */
318 status = module_load_prefixes(path, rootmodule,
319 start, end, prefixes);
320 }
321
322 return status > 0;
323 }
324
325 /* Load module - automatically tries to load also the related non-core
326 modules given in `prefixes' (like irc, fe, fe_text, ..) */
module_load(const char * path,char ** prefixes)327 int module_load(const char *path, char **prefixes)
328 {
329 char *exppath, *name, *submodule, *rootmodule;
330 int start, end, ret;
331
332 g_return_val_if_fail(path != NULL, FALSE);
333
334 exppath = convert_home(path);
335
336 name = module_get_name(exppath, &start, &end);
337 rootmodule = module_get_root(name, prefixes);
338 submodule = module_get_sub(name, rootmodule);
339 g_free(name);
340
341 ret = module_load_full(exppath, rootmodule, submodule,
342 start, end, prefixes);
343
344 g_free(rootmodule);
345 g_free(submodule);
346 g_free(exppath);
347 return ret;
348 }
349
350 /* Load a sub module. */
module_load_sub(const char * path,const char * submodule,char ** prefixes)351 int module_load_sub(const char *path, const char *submodule, char **prefixes)
352 {
353 GString *full_path;
354 char *exppath, *name, *rootmodule;
355 int start, end, ret;
356
357 g_return_val_if_fail(path != NULL, FALSE);
358 g_return_val_if_fail(submodule != NULL, FALSE);
359
360 exppath = convert_home(path);
361
362 name = module_get_name(exppath, &start, &end);
363 rootmodule = module_get_root(name, prefixes);
364 g_free(name);
365
366 full_path = g_string_new(exppath);
367 if (g_strcmp0(submodule, "core") == 0)
368 g_string_insert(full_path, end, "_core");
369 else {
370 g_string_insert_c(full_path, start, '_');
371 g_string_insert(full_path, start, submodule);
372 }
373
374 ret = module_load_full(full_path->str, rootmodule, submodule,
375 start, end, NULL);
376
377 g_string_free(full_path, TRUE);
378 g_free(rootmodule);
379 g_free(exppath);
380 return ret;
381 }
382
module_file_deinit_gmodule(MODULE_FILE_REC * file)383 static void module_file_deinit_gmodule(MODULE_FILE_REC *file)
384 {
385 /* call the module's deinit() function */
386 if (file->module_deinit != NULL)
387 file->module_deinit();
388
389 if (file->defined_module_name != NULL) {
390 settings_remove_module(file->defined_module_name);
391 commands_remove_module(file->defined_module_name);
392 signals_remove_module(file->defined_module_name);
393 }
394
395 g_module_close(file->gmodule);
396 }
397
398 #else /* !HAVE_GMODULE - modules are not supported */
399
module_load(const char * path,char ** prefixes)400 int module_load(const char *path, char **prefixes)
401 {
402 return FALSE;
403 }
404
405 #endif
406
module_file_unload(MODULE_FILE_REC * file)407 void module_file_unload(MODULE_FILE_REC *file)
408 {
409 MODULE_REC *root;
410
411 root = file->root;
412 root->files = g_slist_remove(root->files, file);
413
414 if (file->initialized)
415 signal_emit("module unloaded", 2, file->root, file);
416
417 #ifdef HAVE_GMODULE
418 if (file->gmodule != NULL)
419 module_file_deinit_gmodule(file);
420 #endif
421
422 g_free(file->name);
423 g_free(file->defined_module_name);
424 g_free(file);
425
426 if (root->files == NULL && g_slist_find(modules, root) != NULL)
427 module_unload(root);
428 }
429
module_unload(MODULE_REC * module)430 void module_unload(MODULE_REC *module)
431 {
432 g_return_if_fail(module != NULL);
433
434 modules = g_slist_remove(modules, module);
435
436 signal_emit("module unloaded", 1, module);
437
438 while (module->files != NULL)
439 module_file_unload(module->files->data);
440
441 g_free(module->name);
442 g_free(module);
443 }
444