2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Module Loader
23 * \author Mark Spencer <markster@digium.com>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/module.h"
37 #include "asterisk/options.h"
38 #include "asterisk/config.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/term.h"
42 #include "asterisk/manager.h"
43 #include "asterisk/cdr.h"
44 #include "asterisk/enum.h"
45 #include "asterisk/rtp.h"
46 #include "asterisk/lock.h"
48 #include "asterisk/dlfcn-compat.h"
52 #include "asterisk/md5.h"
58 AST_MUTEX_DEFINE_STATIC(modlock);
59 AST_MUTEX_DEFINE_STATIC(reloadlock);
61 static struct module *module_list=NULL;
62 static int modlistver = 0;
64 static unsigned char expected_key[] =
65 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
66 0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
69 int (*load_module)(void);
70 int (*unload_module)(void);
71 int (*usecount)(void);
72 char *(*description)(void);
80 static struct loadupdate {
82 struct loadupdate *next;
85 static int printdigest(unsigned char *d)
90 snprintf(buf, sizeof(buf), "Unexpected signature:");
91 for (x=0; x<16; x++) {
92 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
96 ast_log(LOG_DEBUG, "%s", buf);
100 static int key_matches(unsigned char *key1, unsigned char *key2)
104 for (x=0; x<16; x++) {
105 match &= (key1[x] == key2[x]);
110 static int verify_key(unsigned char *key)
113 unsigned char digest[16];
115 MD5Update(&c, key, strlen((char *)key));
116 MD5Final(digest, &c);
117 if (key_matches(expected_key, digest))
123 int ast_unload_resource(const char *resource_name, int force)
125 struct module *m, *ml = NULL;
127 if (ast_mutex_lock(&modlock))
128 ast_log(LOG_WARNING, "Failed to lock\n");
131 if (!strcasecmp(m->resource, resource_name)) {
132 if ((res = m->usecount()) > 0) {
134 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
136 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
137 ast_mutex_unlock(&modlock);
141 res = m->unload_module();
143 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
144 if (force <= AST_FORCE_FIRM) {
145 ast_mutex_unlock(&modlock);
148 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
153 module_list = m->next;
162 ast_mutex_unlock(&modlock);
163 ast_update_use_count();
167 char *ast_module_helper(char *line, char *word, int pos, int state, int rpos, int needsreload)
175 ast_mutex_lock(&modlock);
178 if (!strncasecmp(word, m->resource, strlen(word)) && (m->reload || !needsreload)) {
185 ret = strdup(m->resource);
188 if (!strncasecmp(word, "extconfig", strlen(word))) {
190 ret = strdup("extconfig");
191 } else if (!strncasecmp(word, "manager", strlen(word))) {
193 ret = strdup("manager");
194 } else if (!strncasecmp(word, "enum", strlen(word))) {
196 ret = strdup("enum");
197 } else if (!strncasecmp(word, "rtp", strlen(word))) {
203 ast_mutex_unlock(&modlock);
207 int ast_module_reload(const char *name)
213 /* We'll do the logger and manager the favor of calling its reload here first */
215 if (ast_mutex_trylock(&reloadlock)) {
216 ast_verbose("The previous reload command didn't finish yet\n");
219 if (!name || !strcasecmp(name, "extconfig")) {
223 if (!name || !strcasecmp(name, "manager")) {
227 if (!name || !strcasecmp(name, "cdr")) {
228 ast_cdr_engine_reload();
231 if (!name || !strcasecmp(name, "enum")) {
235 if (!name || !strcasecmp(name, "rtp")) {
239 if (!name || !strcasecmp(name, "dnsmgr")) {
243 time(&ast_lastreloadtime);
245 ast_mutex_lock(&modlock);
246 oldversion = modlistver;
249 if (!name || !strcasecmp(name, m->resource)) {
253 ast_mutex_unlock(&modlock);
256 if (option_verbose > 2)
257 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
260 ast_mutex_lock(&modlock);
261 if (oldversion != modlistver)
266 ast_mutex_unlock(&modlock);
267 ast_mutex_unlock(&reloadlock);
271 static int __load_resource(const char *resource_name, const struct ast_config *cfg)
284 if (strncasecmp(resource_name, "res_", 4)) {
287 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
289 flags |= RTLD_GLOBAL;
293 /* Resource modules are always loaded global and lazy */
295 flags = (RTLD_GLOBAL | RTLD_LAZY);
301 if (ast_mutex_lock(&modlock))
302 ast_log(LOG_WARNING, "Failed to lock\n");
305 if (!strcasecmp(m->resource, resource_name)) {
306 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
307 ast_mutex_unlock(&modlock);
312 m = malloc(sizeof(struct module));
314 ast_log(LOG_WARNING, "Out of memory\n");
315 ast_mutex_unlock(&modlock);
318 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
319 if (resource_name[0] == '/') {
320 strncpy(fn, resource_name, sizeof(fn)-1);
322 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
324 m->lib = dlopen(fn, flags);
326 ast_log(LOG_WARNING, "%s\n", dlerror());
328 ast_mutex_unlock(&modlock);
331 m->load_module = dlsym(m->lib, "load_module");
332 if (m->load_module == NULL)
333 m->load_module = dlsym(m->lib, "_load_module");
334 if (!m->load_module) {
335 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
338 m->unload_module = dlsym(m->lib, "unload_module");
339 if (m->unload_module == NULL)
340 m->unload_module = dlsym(m->lib, "_unload_module");
341 if (!m->unload_module) {
342 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
345 m->usecount = dlsym(m->lib, "usecount");
346 if (m->usecount == NULL)
347 m->usecount = dlsym(m->lib, "_usecount");
349 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
352 m->description = dlsym(m->lib, "description");
353 if (m->description == NULL)
354 m->description = dlsym(m->lib, "_description");
355 if (!m->description) {
356 ast_log(LOG_WARNING, "No description in module %s\n", fn);
359 m->key = dlsym(m->lib, "key");
361 m->key = dlsym(m->lib, "_key");
363 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
367 m->reload = dlsym(m->lib, "reload");
368 if (m->reload == NULL)
369 m->reload = dlsym(m->lib, "_reload");
371 if (!m->key || !(key = (unsigned char *) m->key())) {
372 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
376 if (key && verify_key(key)) {
377 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
381 ast_log(LOG_WARNING, "%d error%s loading module %s, aborted\n", errors, (errors != 1) ? "s" : "", fn);
384 ast_mutex_unlock(&modlock);
387 if (!ast_fully_booted) {
389 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
390 if (ast_opt_console && !option_verbose)
394 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
397 /* add module 'm' to end of module_list chain
398 so reload commands will be issued in same order modules were loaded */
400 if (module_list == NULL) {
401 /* empty list so far, add at front */
406 /* find end of chain, and add there */
407 for (i = module_list; i->next; i = i->next)
413 ast_mutex_unlock(&modlock);
414 if ((res = m->load_module())) {
415 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
416 ast_unload_resource(resource_name, 0);
419 ast_update_use_count();
423 int ast_load_resource(const char *resource_name)
426 struct ast_config *cfg = NULL;
429 /* Keep the module file parsing silent */
432 cfg = ast_config_load(AST_MODULE_CONFIG);
434 res = __load_resource(resource_name, cfg);
436 ast_config_destroy(cfg);
440 static int ast_resource_exists(char *resource)
443 if (ast_mutex_lock(&modlock))
444 ast_log(LOG_WARNING, "Failed to lock\n");
447 if (!strcasecmp(resource, m->resource))
451 ast_mutex_unlock(&modlock);
458 static const char *loadorder[] =
466 int load_modules(const int preload_only)
468 struct ast_config *cfg;
469 struct ast_variable *v;
472 if (option_verbose) {
474 ast_verbose("Asterisk Dynamic Loader loading preload modules:\n");
476 ast_verbose("Asterisk Dynamic Loader Starting:\n");
479 cfg = ast_config_load(AST_MODULE_CONFIG);
483 /* Load explicitly defined modules */
484 for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
488 doload = !strcasecmp(v->name, "preload");
490 doload = !strcasecmp(v->name, "load");
493 if (option_debug && !option_verbose)
494 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
495 if (option_verbose) {
496 ast_verbose(VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
499 if (__load_resource(v->value, cfg)) {
500 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
501 ast_config_destroy(cfg);
509 ast_config_destroy(cfg);
513 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
514 /* Load all modules */
519 /* Loop through each order */
520 for (x=0; x<sizeof(loadorder) / sizeof(loadorder[0]); x++) {
521 mods = opendir((char *)ast_config_AST_MODULE_DIR);
523 while((d = readdir(mods))) {
524 /* Must end in .so to load it. */
525 if ((strlen(d->d_name) > 3) &&
526 (!loadorder[x] || !strncasecmp(d->d_name, loadorder[x], strlen(loadorder[x]))) &&
527 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
528 !ast_resource_exists(d->d_name)) {
529 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
530 an inefficient way to do it, but oh well. */
532 v = ast_variable_browse(cfg, "modules");
534 if (!strcasecmp(v->name, "noload") &&
535 !strcasecmp(v->value, d->d_name))
540 if (option_verbose) {
541 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
548 if (option_debug && !option_verbose)
549 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
550 if (option_verbose) {
551 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
554 if (__load_resource(d->d_name, cfg)) {
555 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
557 ast_config_destroy(cfg);
565 ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
569 ast_config_destroy(cfg);
573 void ast_update_use_count(void)
575 /* Notify any module monitors that the use count for a
576 resource has changed */
577 struct loadupdate *m;
578 if (ast_mutex_lock(&modlock))
579 ast_log(LOG_WARNING, "Failed to lock\n");
585 ast_mutex_unlock(&modlock);
589 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
594 int total_mod_loaded = 0;
596 if (ast_mutex_trylock(&modlock))
600 total_mod_loaded += modentry(m->resource, m->description(), m->usecount(), like);
604 ast_mutex_unlock(&modlock);
606 return total_mod_loaded;
609 int ast_loader_register(int (*v)(void))
611 struct loadupdate *tmp;
612 /* XXX Should be more flexible here, taking > 1 verboser XXX */
613 if ((tmp = malloc(sizeof (struct loadupdate)))) {
615 if (ast_mutex_lock(&modlock))
616 ast_log(LOG_WARNING, "Failed to lock\n");
617 tmp->next = updaters;
619 ast_mutex_unlock(&modlock);
625 int ast_loader_unregister(int (*v)(void))
628 struct loadupdate *tmp, *tmpl=NULL;
629 if (ast_mutex_lock(&modlock))
630 ast_log(LOG_WARNING, "Failed to lock\n");
633 if (tmp->updater == v) {
635 tmpl->next = tmp->next;
637 updaters = tmp->next;
645 ast_mutex_unlock(&modlock);