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 read_ast_cust_config();
164 time(&ast_lastreloadtime);
166 ast_mutex_lock(&modlock);
169 if (!name || !strcasecmp(name, m->resource)) {
171 if (option_verbose > 2)
172 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
178 ast_mutex_unlock(&modlock);
179 ast_mutex_unlock(&reloadlock);
182 int ast_load_resource(char *resource_name)
194 struct ast_config *cfg;
196 /* Keep the module file parsing silent */
198 if (strncasecmp(resource_name, "res_", 4)) {
200 cfg = ast_load(AST_MODULE_CONFIG);
204 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
206 flags |= RTLD_GLOBAL;
211 /* Resource modules are always loaded global and lazy */
213 flags = (RTLD_GLOBAL | RTLD_LAZY);
219 if (ast_mutex_lock(&modlock))
220 ast_log(LOG_WARNING, "Failed to lock\n");
223 if (!strcasecmp(m->resource, resource_name)) {
224 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
225 ast_mutex_unlock(&modlock);
230 m = malloc(sizeof(struct module));
232 ast_log(LOG_WARNING, "Out of memory\n");
233 ast_mutex_unlock(&modlock);
236 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
237 if (resource_name[0] == '/') {
238 strncpy(fn, resource_name, sizeof(fn)-1);
240 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
242 m->lib = dlopen(fn, flags);
244 ast_log(LOG_WARNING, "%s\n", dlerror());
246 ast_mutex_unlock(&modlock);
249 m->load_module = dlsym(m->lib, "load_module");
250 if (m->load_module == NULL)
251 m->load_module = dlsym(m->lib, "_load_module");
252 if (!m->load_module) {
253 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
256 m->unload_module = dlsym(m->lib, "unload_module");
257 if (m->unload_module == NULL)
258 m->unload_module = dlsym(m->lib, "_unload_module");
259 if (!m->unload_module) {
260 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
263 m->usecount = dlsym(m->lib, "usecount");
264 if (m->usecount == NULL)
265 m->usecount = dlsym(m->lib, "_usecount");
267 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
270 m->description = dlsym(m->lib, "description");
271 if (m->description == NULL)
272 m->description = dlsym(m->lib, "_description");
273 if (!m->description) {
274 ast_log(LOG_WARNING, "No description in module %s\n", fn);
277 m->key = dlsym(m->lib, "key");
279 m->key = dlsym(m->lib, "_key");
281 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
284 m->reload = dlsym(m->lib, "reload");
285 if (m->reload == NULL)
286 m->reload = dlsym(m->lib, "_reload");
287 if (!m->key || !(key = m->key())) {
288 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
292 if (key && verify_key(key)) {
293 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
297 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
300 ast_mutex_unlock(&modlock);
305 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
306 if (option_console && !option_verbose)
310 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
313 // add module 'm' to end of module_list chain
314 // so reload commands will be issued in same order modules were loaded
316 if (module_list == NULL) {
317 // empty list so far, add at front
322 // find end of chain, and add there
323 for (i = module_list; i->next; i = i->next)
328 ast_mutex_unlock(&modlock);
329 if ((res = m->load_module())) {
330 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
331 ast_unload_resource(resource_name, 0);
334 ast_update_use_count();
338 static int ast_resource_exists(char *resource)
341 if (ast_mutex_lock(&modlock))
342 ast_log(LOG_WARNING, "Failed to lock\n");
345 if (!strcasecmp(resource, m->resource))
349 ast_mutex_unlock(&modlock);
358 struct ast_config *cfg;
359 struct ast_variable *v;
362 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
363 cfg = ast_load(AST_MODULE_CONFIG);
365 /* Load explicitly defined modules */
366 v = ast_variable_browse(cfg, "modules");
368 if (!strcasecmp(v->name, "load")) {
369 if (option_debug && !option_verbose)
370 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
371 if (option_verbose) {
372 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
375 if (ast_load_resource(v->value)) {
376 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
385 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
386 /* Load all modules */
390 /* Make two passes. First, load any resource modules, then load the others. */
392 mods = opendir((char *)ast_config_AST_MODULE_DIR);
394 while((d = readdir(mods))) {
395 /* Must end in .so to load it. */
396 if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) &&
397 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
398 !ast_resource_exists(d->d_name)) {
399 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
400 an inefficient way to do it, but oh well. */
402 v = ast_variable_browse(cfg, "modules");
404 if (!strcasecmp(v->name, "noload") &&
405 !strcasecmp(v->value, d->d_name))
410 if (option_verbose) {
411 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
418 if (option_debug && !option_verbose)
419 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
420 if (option_verbose) {
421 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
424 if (ast_load_resource(d->d_name)) {
425 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
435 ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
443 void ast_update_use_count(void)
445 /* Notify any module monitors that the use count for a
446 resource has changed */
447 struct loadupdate *m;
448 if (ast_mutex_lock(&modlock))
449 ast_log(LOG_WARNING, "Failed to lock\n");
455 ast_mutex_unlock(&modlock);
459 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
463 if (ast_mutex_trylock(&modlock))
467 modentry(m->resource, m->description(), m->usecount());
471 ast_mutex_unlock(&modlock);
475 int ast_loader_register(int (*v)(void))
477 struct loadupdate *tmp;
478 /* XXX Should be more flexible here, taking > 1 verboser XXX */
479 if ((tmp = malloc(sizeof (struct loadupdate)))) {
481 if (ast_mutex_lock(&modlock))
482 ast_log(LOG_WARNING, "Failed to lock\n");
483 tmp->next = updaters;
485 ast_mutex_unlock(&modlock);
491 int ast_loader_unregister(int (*v)(void))
494 struct loadupdate *tmp, *tmpl=NULL;
495 if (ast_mutex_lock(&modlock))
496 ast_log(LOG_WARNING, "Failed to lock\n");
499 if (tmp->updater == v) {
501 tmpl->next = tmp->next;
503 updaters = tmp->next;
511 ast_mutex_unlock(&modlock);