2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999-2004, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
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/config_pvt.h>
23 #include <asterisk/logger.h>
24 #include <asterisk/channel.h>
25 #include <asterisk/term.h>
26 #include <asterisk/manager.h>
27 #include <asterisk/enum.h>
28 #include <asterisk/rtp.h>
29 #include <asterisk/lock.h>
31 #include <asterisk/dlfcn-compat.h>
35 #include <asterisk/md5.h>
43 static char expected_key[] =
44 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
45 0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
48 int (*load_module)(void);
49 int (*unload_module)(void);
50 int (*usecount)(void);
51 char *(*description)(void);
59 static int printdigest(unsigned char *d)
64 snprintf(buf, sizeof(buf), "Unexpected signature:");
66 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
70 ast_log(LOG_DEBUG, buf);
74 static int key_matches(char *key1, char *key2)
79 match &= (key1[x] == key2[x]);
84 static int verify_key(char *key)
89 MD5Update(&c, key, strlen(key));
91 if (key_matches(expected_key, digest))
97 static struct loadupdate {
99 struct loadupdate *next;
102 AST_MUTEX_DEFINE_STATIC(modlock);
103 AST_MUTEX_DEFINE_STATIC(reloadlock);
105 static struct module *module_list=NULL;
106 static int modlistver = 0;
108 int ast_unload_resource(char *resource_name, int force)
110 struct module *m, *ml = NULL;
112 if (ast_mutex_lock(&modlock))
113 ast_log(LOG_WARNING, "Failed to lock\n");
116 if (!strcasecmp(m->resource, resource_name)) {
117 if ((res = m->usecount()) > 0) {
119 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
121 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
122 ast_mutex_unlock(&modlock);
126 res = m->unload_module();
128 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
129 if (force <= AST_FORCE_FIRM) {
130 ast_mutex_unlock(&modlock);
133 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
138 module_list = m->next;
147 ast_mutex_unlock(&modlock);
148 ast_update_use_count();
152 char *ast_module_helper(char *line, char *word, int pos, int state, int rpos, int needsreload)
159 ast_mutex_lock(&modlock);
162 if (!strncasecmp(word, m->resource, strlen(word)) && (m->reload || !needsreload)) {
169 ret = strdup(m->resource);
172 if (!strncasecmp(word, "astconfig", strlen(word))) {
174 ret = strdup("astconfig");
175 } else if (!strncasecmp(word, "manager", strlen(word))) {
177 ret = strdup("manager");
178 } else if (!strncasecmp(word, "enum", strlen(word))) {
180 ret = strdup("enum");
181 } else if (!strncasecmp(word, "rtp", strlen(word))) {
187 ast_mutex_unlock(&modlock);
191 int ast_module_reload(const char *name)
197 /* We'll do the logger and manager the favor of calling its reload here first */
199 if (ast_mutex_trylock(&reloadlock)) {
200 ast_verbose("The previous reload command didn't finish yet\n");
203 if (!name || !strcasecmp(name, "astconfig")) {
204 read_ast_cust_config();
207 if (!name || !strcasecmp(name, "manager")) {
211 if (!name || !strcasecmp(name, "enum")) {
215 if (!name || !strcasecmp(name, "rtp")) {
219 time(&ast_lastreloadtime);
221 ast_mutex_lock(&modlock);
222 oldversion = modlistver;
225 if (!name || !strcasecmp(name, m->resource)) {
229 ast_mutex_unlock(&modlock);
232 if (option_verbose > 2)
233 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
236 ast_mutex_lock(&modlock);
237 if (oldversion != modlistver)
242 ast_mutex_unlock(&modlock);
243 ast_mutex_unlock(&reloadlock);
247 int ast_load_resource(char *resource_name)
259 struct ast_config *cfg;
261 /* Keep the module file parsing silent */
263 if (strncasecmp(resource_name, "res_", 4)) {
265 cfg = ast_load(AST_MODULE_CONFIG);
269 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
271 flags |= RTLD_GLOBAL;
276 /* Resource modules are always loaded global and lazy */
278 flags = (RTLD_GLOBAL | RTLD_LAZY);
284 if (ast_mutex_lock(&modlock))
285 ast_log(LOG_WARNING, "Failed to lock\n");
288 if (!strcasecmp(m->resource, resource_name)) {
289 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
290 ast_mutex_unlock(&modlock);
295 m = malloc(sizeof(struct module));
297 ast_log(LOG_WARNING, "Out of memory\n");
298 ast_mutex_unlock(&modlock);
301 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
302 if (resource_name[0] == '/') {
303 strncpy(fn, resource_name, sizeof(fn)-1);
305 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
307 m->lib = dlopen(fn, flags);
309 ast_log(LOG_WARNING, "%s\n", dlerror());
311 ast_mutex_unlock(&modlock);
314 m->load_module = dlsym(m->lib, "load_module");
315 if (m->load_module == NULL)
316 m->load_module = dlsym(m->lib, "_load_module");
317 if (!m->load_module) {
318 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
321 m->unload_module = dlsym(m->lib, "unload_module");
322 if (m->unload_module == NULL)
323 m->unload_module = dlsym(m->lib, "_unload_module");
324 if (!m->unload_module) {
325 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
328 m->usecount = dlsym(m->lib, "usecount");
329 if (m->usecount == NULL)
330 m->usecount = dlsym(m->lib, "_usecount");
332 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
335 m->description = dlsym(m->lib, "description");
336 if (m->description == NULL)
337 m->description = dlsym(m->lib, "_description");
338 if (!m->description) {
339 ast_log(LOG_WARNING, "No description in module %s\n", fn);
342 m->key = dlsym(m->lib, "key");
344 m->key = dlsym(m->lib, "_key");
346 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
349 m->reload = dlsym(m->lib, "reload");
350 if (m->reload == NULL)
351 m->reload = dlsym(m->lib, "_reload");
352 if (!m->key || !(key = m->key())) {
353 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
357 if (key && verify_key(key)) {
358 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
362 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
365 ast_mutex_unlock(&modlock);
370 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
371 if (option_console && !option_verbose)
375 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
378 /* add module 'm' to end of module_list chain
379 so reload commands will be issued in same order modules were loaded */
381 if (module_list == NULL) {
382 /* empty list so far, add at front */
387 /* find end of chain, and add there */
388 for (i = module_list; i->next; i = i->next)
394 ast_mutex_unlock(&modlock);
395 if ((res = m->load_module())) {
396 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
397 ast_unload_resource(resource_name, 0);
400 ast_update_use_count();
404 static int ast_resource_exists(char *resource)
407 if (ast_mutex_lock(&modlock))
408 ast_log(LOG_WARNING, "Failed to lock\n");
411 if (!strcasecmp(resource, m->resource))
415 ast_mutex_unlock(&modlock);
422 static const char *loadorder[] =
432 struct ast_config *cfg;
433 struct ast_variable *v;
436 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
437 cfg = ast_load(AST_MODULE_CONFIG);
439 /* Load explicitly defined modules */
440 v = ast_variable_browse(cfg, "modules");
442 if (!strcasecmp(v->name, "load")) {
443 if (option_debug && !option_verbose)
444 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
445 if (option_verbose) {
446 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
449 if (ast_load_resource(v->value)) {
450 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
459 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
460 /* Load all modules */
464 /* Loop through each order */
465 for (x=0;x<sizeof(loadorder) / sizeof(loadorder[0]);x++) {
466 mods = opendir((char *)ast_config_AST_MODULE_DIR);
468 while((d = readdir(mods))) {
469 /* Must end in .so to load it. */
470 if ((strlen(d->d_name) > 3) &&
471 (!loadorder[x] || !strncasecmp(d->d_name, loadorder[x], strlen(loadorder[x]))) &&
472 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
473 !ast_resource_exists(d->d_name)) {
474 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
475 an inefficient way to do it, but oh well. */
477 v = ast_variable_browse(cfg, "modules");
479 if (!strcasecmp(v->name, "noload") &&
480 !strcasecmp(v->value, d->d_name))
485 if (option_verbose) {
486 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
493 if (option_debug && !option_verbose)
494 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
495 if (option_verbose) {
496 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
499 if (ast_load_resource(d->d_name)) {
500 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
510 ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
518 void ast_update_use_count(void)
520 /* Notify any module monitors that the use count for a
521 resource has changed */
522 struct loadupdate *m;
523 if (ast_mutex_lock(&modlock))
524 ast_log(LOG_WARNING, "Failed to lock\n");
530 ast_mutex_unlock(&modlock);
534 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt, char *like), char *like)
538 int total_mod_loaded = 0;
539 if (ast_mutex_trylock(&modlock))
543 total_mod_loaded += modentry(m->resource, m->description(), m->usecount(), like);
547 ast_mutex_unlock(&modlock);
548 return total_mod_loaded;
551 int ast_loader_register(int (*v)(void))
553 struct loadupdate *tmp;
554 /* XXX Should be more flexible here, taking > 1 verboser XXX */
555 if ((tmp = malloc(sizeof (struct loadupdate)))) {
557 if (ast_mutex_lock(&modlock))
558 ast_log(LOG_WARNING, "Failed to lock\n");
559 tmp->next = updaters;
561 ast_mutex_unlock(&modlock);
567 int ast_loader_unregister(int (*v)(void))
570 struct loadupdate *tmp, *tmpl=NULL;
571 if (ast_mutex_lock(&modlock))
572 ast_log(LOG_WARNING, "Failed to lock\n");
575 if (tmp->updater == v) {
577 tmpl->next = tmp->next;
579 updaters = tmp->next;
587 ast_mutex_unlock(&modlock);