2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
19 #include <asterisk/module.h>
20 #include <asterisk/options.h>
21 #include <asterisk/config.h>
22 #include <asterisk/logger.h>
23 #include <asterisk/channel.h>
24 #include <asterisk/term.h>
25 #include <asterisk/manager.h>
27 #include <asterisk/md5.h>
37 static char expected_key[] =
38 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
39 0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
42 int (*load_module)(void);
43 int (*unload_module)(void);
44 int (*usecount)(void);
45 char *(*description)(void);
53 static int printdigest(unsigned char *d)
58 snprintf(buf, sizeof(buf), "Unexpected signature:");
60 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
64 ast_log(LOG_DEBUG, buf);
68 static int key_matches(char *key1, char *key2)
73 match &= (key1[x] == key2[x]);
78 static int verify_key(char *key)
83 MD5Update(&c, key, strlen(key));
85 if (key_matches(expected_key, digest))
91 static struct loadupdate {
93 struct loadupdate *next;
96 static pthread_mutex_t modlock = AST_MUTEX_INITIALIZER;
98 static struct module *module_list=NULL;
100 int ast_unload_resource(char *resource_name, int force)
102 struct module *m, *ml = NULL;
104 if (ast_pthread_mutex_lock(&modlock))
105 ast_log(LOG_WARNING, "Failed to lock\n");
108 if (!strcasecmp(m->resource, resource_name)) {
109 if ((res = m->usecount()) > 0) {
111 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
113 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
114 ast_pthread_mutex_unlock(&modlock);
118 res = m->unload_module();
120 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
121 if (force <= AST_FORCE_FIRM) {
122 ast_pthread_mutex_unlock(&modlock);
125 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
130 module_list = m->next;
137 ast_pthread_mutex_unlock(&modlock);
138 ast_update_use_count();
142 void ast_module_reload(void)
146 /* We'll do the logger and manager the favor of calling its reload here first */
149 ast_pthread_mutex_lock(&modlock);
153 if (option_verbose > 2)
154 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
159 ast_pthread_mutex_unlock(&modlock);
162 int ast_load_resource(char *resource_name)
172 struct ast_config *cfg;
174 /* Keep the module file parsing silent */
176 if (strncasecmp(resource_name, "res_", 4)) {
178 cfg = ast_load(AST_MODULE_CONFIG);
182 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
184 flags |= RTLD_GLOBAL;
189 /* Resource modules are always loaded global and lazy */
191 flags = (RTLD_GLOBAL | RTLD_LAZY);
197 if (ast_pthread_mutex_lock(&modlock))
198 ast_log(LOG_WARNING, "Failed to lock\n");
201 if (!strcasecmp(m->resource, resource_name)) {
202 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
203 ast_pthread_mutex_unlock(&modlock);
208 m = malloc(sizeof(struct module));
210 ast_log(LOG_WARNING, "Out of memory\n");
211 ast_pthread_mutex_unlock(&modlock);
214 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
215 if (resource_name[0] == '/') {
216 strncpy(fn, resource_name, sizeof(fn)-1);
218 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
220 m->lib = dlopen(fn, flags);
222 ast_log(LOG_WARNING, "%s\n", dlerror());
224 ast_pthread_mutex_unlock(&modlock);
227 m->load_module = dlsym(m->lib, "load_module");
228 if (m->load_module == NULL)
229 m->load_module = dlsym(m->lib, "_load_module");
230 if (!m->load_module) {
231 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
234 m->unload_module = dlsym(m->lib, "unload_module");
235 if (m->unload_module == NULL)
236 m->unload_module = dlsym(m->lib, "_unload_module");
237 if (!m->unload_module) {
238 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
241 m->usecount = dlsym(m->lib, "usecount");
242 if (m->usecount == NULL)
243 m->usecount = dlsym(m->lib, "_usecount");
245 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
248 m->description = dlsym(m->lib, "description");
249 if (m->description == NULL)
250 m->description = dlsym(m->lib, "_description");
251 if (!m->description) {
252 ast_log(LOG_WARNING, "No description in module %s\n", fn);
255 m->key = dlsym(m->lib, "key");
257 m->key = dlsym(m->lib, "_key");
259 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
262 m->reload = dlsym(m->lib, "reload");
263 if (m->reload == NULL)
264 m->reload = dlsym(m->lib, "_reload");
265 if (m->key && !(key = m->key())) {
266 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
270 if (key && verify_key(key)) {
271 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
275 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
278 ast_pthread_mutex_unlock(&modlock);
283 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
284 if (option_console && !option_verbose)
288 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
290 m->next = module_list;
293 ast_pthread_mutex_unlock(&modlock);
294 if ((res = m->load_module())) {
295 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
296 ast_unload_resource(resource_name, 0);
299 ast_update_use_count();
303 static int ast_resource_exists(char *resource)
306 if (ast_pthread_mutex_lock(&modlock))
307 ast_log(LOG_WARNING, "Failed to lock\n");
310 if (!strcasecmp(resource, m->resource))
314 ast_pthread_mutex_unlock(&modlock);
323 struct ast_config *cfg;
324 struct ast_variable *v;
327 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
328 cfg = ast_load(AST_MODULE_CONFIG);
330 /* Load explicitly defined modules */
331 v = ast_variable_browse(cfg, "modules");
333 if (!strcasecmp(v->name, "load")) {
334 if (option_debug && !option_verbose)
335 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
336 if (option_verbose) {
337 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
340 if (ast_load_resource(v->value)) {
341 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
350 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
351 /* Load all modules */
355 /* Make two passes. First, load any resource modules, then load the others. */
357 mods = opendir((char *)ast_config_AST_MODULE_DIR);
359 while((d = readdir(mods))) {
360 /* Must end in .so to load it. */
361 if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) &&
362 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
363 !ast_resource_exists(d->d_name)) {
364 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
365 an inefficient way to do it, but oh well. */
367 v = ast_variable_browse(cfg, "modules");
369 if (!strcasecmp(v->name, "noload") &&
370 !strcasecmp(v->value, d->d_name))
375 if (option_verbose) {
376 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
383 if (option_debug && !option_verbose)
384 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
385 if (option_verbose) {
386 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
389 if (ast_load_resource(d->d_name)) {
390 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
400 ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
408 void ast_update_use_count(void)
410 /* Notify any module monitors that the use count for a
411 resource has changed */
412 struct loadupdate *m;
413 if (ast_pthread_mutex_lock(&modlock))
414 ast_log(LOG_WARNING, "Failed to lock\n");
420 ast_pthread_mutex_unlock(&modlock);
424 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
428 if (pthread_mutex_trylock(&modlock))
432 modentry(m->resource, m->description(), m->usecount());
436 ast_pthread_mutex_unlock(&modlock);
440 int ast_loader_register(int (*v)(void))
442 struct loadupdate *tmp;
443 /* XXX Should be more flexible here, taking > 1 verboser XXX */
444 if ((tmp = malloc(sizeof (struct loadupdate)))) {
446 if (ast_pthread_mutex_lock(&modlock))
447 ast_log(LOG_WARNING, "Failed to lock\n");
448 tmp->next = updaters;
450 ast_pthread_mutex_unlock(&modlock);
456 int ast_loader_unregister(int (*v)(void))
459 struct loadupdate *tmp, *tmpl=NULL;
460 if (ast_pthread_mutex_lock(&modlock))
461 ast_log(LOG_WARNING, "Failed to lock\n");
464 if (tmp->updater == v) {
466 tmpl->next = tmp->next;
468 updaters = tmp->next;
476 ast_pthread_mutex_unlock(&modlock);