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>
26 #include <asterisk/enum.h>
27 #include <asterisk/rtp.h>
29 #include <asterisk/dlfcn-compat.h>
33 #include <asterisk/md5.h>
42 static char expected_key[] =
43 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
44 0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
47 int (*load_module)(void);
48 int (*unload_module)(void);
49 int (*usecount)(void);
50 char *(*description)(void);
58 static int printdigest(unsigned char *d)
63 snprintf(buf, sizeof(buf), "Unexpected signature:");
65 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
69 ast_log(LOG_DEBUG, buf);
73 static int key_matches(char *key1, char *key2)
78 match &= (key1[x] == key2[x]);
83 static int verify_key(char *key)
88 MD5Update(&c, key, strlen(key));
90 if (key_matches(expected_key, digest))
96 static struct loadupdate {
98 struct loadupdate *next;
101 static ast_mutex_t modlock = AST_MUTEX_INITIALIZER;
102 static ast_mutex_t reloadlock = AST_MUTEX_INITIALIZER;
104 static struct module *module_list=NULL;
106 int ast_unload_resource(char *resource_name, int force)
108 struct module *m, *ml = NULL;
110 if (ast_mutex_lock(&modlock))
111 ast_log(LOG_WARNING, "Failed to lock\n");
114 if (!strcasecmp(m->resource, resource_name)) {
115 if ((res = m->usecount()) > 0) {
117 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
119 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
120 ast_mutex_unlock(&modlock);
124 res = m->unload_module();
126 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
127 if (force <= AST_FORCE_FIRM) {
128 ast_mutex_unlock(&modlock);
131 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
136 module_list = m->next;
144 ast_mutex_unlock(&modlock);
145 ast_update_use_count();
149 void ast_module_reload(void)
153 /* We'll do the logger and manager the favor of calling its reload here first */
155 if (ast_mutex_trylock(&reloadlock)) {
156 ast_verbose("The previous reload command didn't finish yet\n");
162 time(&ast_lastreloadtime);
164 ast_mutex_lock(&modlock);
168 if (option_verbose > 2)
169 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
174 ast_mutex_unlock(&modlock);
175 ast_mutex_unlock(&reloadlock);
178 int ast_load_resource(char *resource_name)
190 struct ast_config *cfg;
192 /* Keep the module file parsing silent */
194 if (strncasecmp(resource_name, "res_", 4)) {
196 cfg = ast_load(AST_MODULE_CONFIG);
200 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
202 flags |= RTLD_GLOBAL;
207 /* Resource modules are always loaded global and lazy */
209 flags = (RTLD_GLOBAL | RTLD_LAZY);
215 if (ast_mutex_lock(&modlock))
216 ast_log(LOG_WARNING, "Failed to lock\n");
219 if (!strcasecmp(m->resource, resource_name)) {
220 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
221 ast_mutex_unlock(&modlock);
226 m = malloc(sizeof(struct module));
228 ast_log(LOG_WARNING, "Out of memory\n");
229 ast_mutex_unlock(&modlock);
232 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
233 if (resource_name[0] == '/') {
234 strncpy(fn, resource_name, sizeof(fn)-1);
236 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
238 m->lib = dlopen(fn, flags);
240 ast_log(LOG_WARNING, "%s\n", dlerror());
242 ast_mutex_unlock(&modlock);
245 m->load_module = dlsym(m->lib, "load_module");
246 if (m->load_module == NULL)
247 m->load_module = dlsym(m->lib, "_load_module");
248 if (!m->load_module) {
249 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
252 m->unload_module = dlsym(m->lib, "unload_module");
253 if (m->unload_module == NULL)
254 m->unload_module = dlsym(m->lib, "_unload_module");
255 if (!m->unload_module) {
256 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
259 m->usecount = dlsym(m->lib, "usecount");
260 if (m->usecount == NULL)
261 m->usecount = dlsym(m->lib, "_usecount");
263 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
266 m->description = dlsym(m->lib, "description");
267 if (m->description == NULL)
268 m->description = dlsym(m->lib, "_description");
269 if (!m->description) {
270 ast_log(LOG_WARNING, "No description in module %s\n", fn);
273 m->key = dlsym(m->lib, "key");
275 m->key = dlsym(m->lib, "_key");
277 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
280 m->reload = dlsym(m->lib, "reload");
281 if (m->reload == NULL)
282 m->reload = dlsym(m->lib, "_reload");
283 if (m->key && !(key = m->key())) {
284 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
288 if (key && verify_key(key)) {
289 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
293 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
296 ast_mutex_unlock(&modlock);
301 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
302 if (option_console && !option_verbose)
306 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
308 m->next = module_list;
311 ast_mutex_unlock(&modlock);
312 if ((res = m->load_module())) {
313 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
314 ast_unload_resource(resource_name, 0);
317 ast_update_use_count();
321 static int ast_resource_exists(char *resource)
324 if (ast_mutex_lock(&modlock))
325 ast_log(LOG_WARNING, "Failed to lock\n");
328 if (!strcasecmp(resource, m->resource))
332 ast_mutex_unlock(&modlock);
341 struct ast_config *cfg;
342 struct ast_variable *v;
345 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
346 cfg = ast_load(AST_MODULE_CONFIG);
348 /* Load explicitly defined modules */
349 v = ast_variable_browse(cfg, "modules");
351 if (!strcasecmp(v->name, "load")) {
352 if (option_debug && !option_verbose)
353 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
354 if (option_verbose) {
355 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
358 if (ast_load_resource(v->value)) {
359 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
368 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
369 /* Load all modules */
373 /* Make two passes. First, load any resource modules, then load the others. */
375 mods = opendir((char *)ast_config_AST_MODULE_DIR);
377 while((d = readdir(mods))) {
378 /* Must end in .so to load it. */
379 if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) &&
380 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
381 !ast_resource_exists(d->d_name)) {
382 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
383 an inefficient way to do it, but oh well. */
385 v = ast_variable_browse(cfg, "modules");
387 if (!strcasecmp(v->name, "noload") &&
388 !strcasecmp(v->value, d->d_name))
393 if (option_verbose) {
394 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
401 if (option_debug && !option_verbose)
402 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
403 if (option_verbose) {
404 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
407 if (ast_load_resource(d->d_name)) {
408 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
418 ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
426 void ast_update_use_count(void)
428 /* Notify any module monitors that the use count for a
429 resource has changed */
430 struct loadupdate *m;
431 if (ast_mutex_lock(&modlock))
432 ast_log(LOG_WARNING, "Failed to lock\n");
438 ast_mutex_unlock(&modlock);
442 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
446 if (ast_mutex_trylock(&modlock))
450 modentry(m->resource, m->description(), m->usecount());
454 ast_mutex_unlock(&modlock);
458 int ast_loader_register(int (*v)(void))
460 struct loadupdate *tmp;
461 /* XXX Should be more flexible here, taking > 1 verboser XXX */
462 if ((tmp = malloc(sizeof (struct loadupdate)))) {
464 if (ast_mutex_lock(&modlock))
465 ast_log(LOG_WARNING, "Failed to lock\n");
466 tmp->next = updaters;
468 ast_mutex_unlock(&modlock);
474 int ast_loader_unregister(int (*v)(void))
477 struct loadupdate *tmp, *tmpl=NULL;
478 if (ast_mutex_lock(&modlock))
479 ast_log(LOG_WARNING, "Failed to lock\n");
482 if (tmp->updater == v) {
484 tmpl->next = tmp->next;
486 updaters = tmp->next;
494 ast_mutex_unlock(&modlock);