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;
107 int ast_unload_resource(char *resource_name, int force)
109 struct module *m, *ml = NULL;
111 if (ast_mutex_lock(&modlock))
112 ast_log(LOG_WARNING, "Failed to lock\n");
115 if (!strcasecmp(m->resource, resource_name)) {
116 if ((res = m->usecount()) > 0) {
118 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
120 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
121 ast_mutex_unlock(&modlock);
125 res = m->unload_module();
127 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
128 if (force <= AST_FORCE_FIRM) {
129 ast_mutex_unlock(&modlock);
132 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
137 module_list = m->next;
145 ast_mutex_unlock(&modlock);
146 ast_update_use_count();
150 void ast_module_reload(const char *name)
154 /* We'll do the logger and manager the favor of calling its reload here first */
156 if (ast_mutex_trylock(&reloadlock)) {
157 ast_verbose("The previous reload command didn't finish yet\n");
160 if (!name || !strcasecmp(name, "astconfig"))
161 read_ast_cust_config();
162 if (!name || !strcasecmp(name, "manager"))
164 if (!name || !strcasecmp(name, "enum"))
166 if (!name || !strcasecmp(name, "rtp"))
168 time(&ast_lastreloadtime);
170 ast_mutex_lock(&modlock);
173 if (!name || !strcasecmp(name, m->resource)) {
175 if (option_verbose > 2)
176 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
182 ast_mutex_unlock(&modlock);
183 ast_mutex_unlock(&reloadlock);
186 int ast_load_resource(char *resource_name)
198 struct ast_config *cfg;
200 /* Keep the module file parsing silent */
202 if (strncasecmp(resource_name, "res_", 4)) {
204 cfg = ast_load(AST_MODULE_CONFIG);
208 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
210 flags |= RTLD_GLOBAL;
215 /* Resource modules are always loaded global and lazy */
217 flags = (RTLD_GLOBAL | RTLD_LAZY);
223 if (ast_mutex_lock(&modlock))
224 ast_log(LOG_WARNING, "Failed to lock\n");
227 if (!strcasecmp(m->resource, resource_name)) {
228 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
229 ast_mutex_unlock(&modlock);
234 m = malloc(sizeof(struct module));
236 ast_log(LOG_WARNING, "Out of memory\n");
237 ast_mutex_unlock(&modlock);
240 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
241 if (resource_name[0] == '/') {
242 strncpy(fn, resource_name, sizeof(fn)-1);
244 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
246 m->lib = dlopen(fn, flags);
248 ast_log(LOG_WARNING, "%s\n", dlerror());
250 ast_mutex_unlock(&modlock);
253 m->load_module = dlsym(m->lib, "load_module");
254 if (m->load_module == NULL)
255 m->load_module = dlsym(m->lib, "_load_module");
256 if (!m->load_module) {
257 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
260 m->unload_module = dlsym(m->lib, "unload_module");
261 if (m->unload_module == NULL)
262 m->unload_module = dlsym(m->lib, "_unload_module");
263 if (!m->unload_module) {
264 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
267 m->usecount = dlsym(m->lib, "usecount");
268 if (m->usecount == NULL)
269 m->usecount = dlsym(m->lib, "_usecount");
271 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
274 m->description = dlsym(m->lib, "description");
275 if (m->description == NULL)
276 m->description = dlsym(m->lib, "_description");
277 if (!m->description) {
278 ast_log(LOG_WARNING, "No description in module %s\n", fn);
281 m->key = dlsym(m->lib, "key");
283 m->key = dlsym(m->lib, "_key");
285 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
288 m->reload = dlsym(m->lib, "reload");
289 if (m->reload == NULL)
290 m->reload = dlsym(m->lib, "_reload");
291 if (!m->key || !(key = m->key())) {
292 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
296 if (key && verify_key(key)) {
297 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
301 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
304 ast_mutex_unlock(&modlock);
309 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
310 if (option_console && !option_verbose)
314 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
317 // add module 'm' to end of module_list chain
318 // so reload commands will be issued in same order modules were loaded
320 if (module_list == NULL) {
321 // empty list so far, add at front
326 // find end of chain, and add there
327 for (i = module_list; i->next; i = i->next)
332 ast_mutex_unlock(&modlock);
333 if ((res = m->load_module())) {
334 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
335 ast_unload_resource(resource_name, 0);
338 ast_update_use_count();
342 static int ast_resource_exists(char *resource)
345 if (ast_mutex_lock(&modlock))
346 ast_log(LOG_WARNING, "Failed to lock\n");
349 if (!strcasecmp(resource, m->resource))
353 ast_mutex_unlock(&modlock);
362 struct ast_config *cfg;
363 struct ast_variable *v;
366 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
367 cfg = ast_load(AST_MODULE_CONFIG);
369 /* Load explicitly defined modules */
370 v = ast_variable_browse(cfg, "modules");
372 if (!strcasecmp(v->name, "load")) {
373 if (option_debug && !option_verbose)
374 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
375 if (option_verbose) {
376 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
379 if (ast_load_resource(v->value)) {
380 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
389 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
390 /* Load all modules */
394 /* Make two passes. First, load any resource modules, then load the others. */
396 mods = opendir((char *)ast_config_AST_MODULE_DIR);
398 while((d = readdir(mods))) {
399 /* Must end in .so to load it. */
400 if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) &&
401 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
402 !ast_resource_exists(d->d_name)) {
403 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
404 an inefficient way to do it, but oh well. */
406 v = ast_variable_browse(cfg, "modules");
408 if (!strcasecmp(v->name, "noload") &&
409 !strcasecmp(v->value, d->d_name))
414 if (option_verbose) {
415 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
422 if (option_debug && !option_verbose)
423 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
424 if (option_verbose) {
425 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
428 if (ast_load_resource(d->d_name)) {
429 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
439 ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
447 void ast_update_use_count(void)
449 /* Notify any module monitors that the use count for a
450 resource has changed */
451 struct loadupdate *m;
452 if (ast_mutex_lock(&modlock))
453 ast_log(LOG_WARNING, "Failed to lock\n");
459 ast_mutex_unlock(&modlock);
463 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
467 if (ast_mutex_trylock(&modlock))
471 modentry(m->resource, m->description(), m->usecount());
475 ast_mutex_unlock(&modlock);
479 int ast_loader_register(int (*v)(void))
481 struct loadupdate *tmp;
482 /* XXX Should be more flexible here, taking > 1 verboser XXX */
483 if ((tmp = malloc(sizeof (struct loadupdate)))) {
485 if (ast_mutex_lock(&modlock))
486 ast_log(LOG_WARNING, "Failed to lock\n");
487 tmp->next = updaters;
489 ast_mutex_unlock(&modlock);
495 int ast_loader_unregister(int (*v)(void))
498 struct loadupdate *tmp, *tmpl=NULL;
499 if (ast_mutex_lock(&modlock))
500 ast_log(LOG_WARNING, "Failed to lock\n");
503 if (tmp->updater == v) {
505 tmpl->next = tmp->next;
507 updaters = tmp->next;
515 ast_mutex_unlock(&modlock);