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;
143 ast_mutex_unlock(&modlock);
144 ast_update_use_count();
148 void ast_module_reload(void)
152 /* We'll do the logger and manager the favor of calling its reload here first */
154 if (ast_mutex_trylock(&reloadlock)) {
155 ast_verbose("The previous reload command didn't finish yet\n");
161 time(&ast_lastreloadtime);
163 ast_mutex_lock(&modlock);
167 if (option_verbose > 2)
168 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
173 ast_mutex_unlock(&modlock);
174 ast_mutex_unlock(&reloadlock);
177 int ast_load_resource(char *resource_name)
189 struct ast_config *cfg;
191 /* Keep the module file parsing silent */
193 if (strncasecmp(resource_name, "res_", 4)) {
195 cfg = ast_load(AST_MODULE_CONFIG);
199 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
201 flags |= RTLD_GLOBAL;
206 /* Resource modules are always loaded global and lazy */
208 flags = (RTLD_GLOBAL | RTLD_LAZY);
214 if (ast_mutex_lock(&modlock))
215 ast_log(LOG_WARNING, "Failed to lock\n");
218 if (!strcasecmp(m->resource, resource_name)) {
219 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
220 ast_mutex_unlock(&modlock);
225 m = malloc(sizeof(struct module));
227 ast_log(LOG_WARNING, "Out of memory\n");
228 ast_mutex_unlock(&modlock);
231 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
232 if (resource_name[0] == '/') {
233 strncpy(fn, resource_name, sizeof(fn)-1);
235 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
237 m->lib = dlopen(fn, flags);
239 ast_log(LOG_WARNING, "%s\n", dlerror());
241 ast_mutex_unlock(&modlock);
244 m->load_module = dlsym(m->lib, "load_module");
245 if (m->load_module == NULL)
246 m->load_module = dlsym(m->lib, "_load_module");
247 if (!m->load_module) {
248 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
251 m->unload_module = dlsym(m->lib, "unload_module");
252 if (m->unload_module == NULL)
253 m->unload_module = dlsym(m->lib, "_unload_module");
254 if (!m->unload_module) {
255 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
258 m->usecount = dlsym(m->lib, "usecount");
259 if (m->usecount == NULL)
260 m->usecount = dlsym(m->lib, "_usecount");
262 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
265 m->description = dlsym(m->lib, "description");
266 if (m->description == NULL)
267 m->description = dlsym(m->lib, "_description");
268 if (!m->description) {
269 ast_log(LOG_WARNING, "No description in module %s\n", fn);
272 m->key = dlsym(m->lib, "key");
274 m->key = dlsym(m->lib, "_key");
276 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
279 m->reload = dlsym(m->lib, "reload");
280 if (m->reload == NULL)
281 m->reload = dlsym(m->lib, "_reload");
282 if (m->key && !(key = m->key())) {
283 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
287 if (key && verify_key(key)) {
288 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
292 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
295 ast_mutex_unlock(&modlock);
300 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
301 if (option_console && !option_verbose)
305 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
307 m->next = module_list;
310 ast_mutex_unlock(&modlock);
311 if ((res = m->load_module())) {
312 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
313 ast_unload_resource(resource_name, 0);
316 ast_update_use_count();
320 static int ast_resource_exists(char *resource)
323 if (ast_mutex_lock(&modlock))
324 ast_log(LOG_WARNING, "Failed to lock\n");
327 if (!strcasecmp(resource, m->resource))
331 ast_mutex_unlock(&modlock);
340 struct ast_config *cfg;
341 struct ast_variable *v;
344 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
345 cfg = ast_load(AST_MODULE_CONFIG);
347 /* Load explicitly defined modules */
348 v = ast_variable_browse(cfg, "modules");
350 if (!strcasecmp(v->name, "load")) {
351 if (option_debug && !option_verbose)
352 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
353 if (option_verbose) {
354 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
357 if (ast_load_resource(v->value)) {
358 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
367 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
368 /* Load all modules */
372 /* Make two passes. First, load any resource modules, then load the others. */
374 mods = opendir((char *)ast_config_AST_MODULE_DIR);
376 while((d = readdir(mods))) {
377 /* Must end in .so to load it. */
378 if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) &&
379 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
380 !ast_resource_exists(d->d_name)) {
381 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
382 an inefficient way to do it, but oh well. */
384 v = ast_variable_browse(cfg, "modules");
386 if (!strcasecmp(v->name, "noload") &&
387 !strcasecmp(v->value, d->d_name))
392 if (option_verbose) {
393 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
400 if (option_debug && !option_verbose)
401 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
402 if (option_verbose) {
403 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
406 if (ast_load_resource(d->d_name)) {
407 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
417 ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
425 void ast_update_use_count(void)
427 /* Notify any module monitors that the use count for a
428 resource has changed */
429 struct loadupdate *m;
430 if (ast_mutex_lock(&modlock))
431 ast_log(LOG_WARNING, "Failed to lock\n");
437 ast_mutex_unlock(&modlock);
441 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
445 if (ast_mutex_trylock(&modlock))
449 modentry(m->resource, m->description(), m->usecount());
453 ast_mutex_unlock(&modlock);
457 int ast_loader_register(int (*v)(void))
459 struct loadupdate *tmp;
460 /* XXX Should be more flexible here, taking > 1 verboser XXX */
461 if ((tmp = malloc(sizeof (struct loadupdate)))) {
463 if (ast_mutex_lock(&modlock))
464 ast_log(LOG_WARNING, "Failed to lock\n");
465 tmp->next = updaters;
467 ast_mutex_unlock(&modlock);
473 int ast_loader_unregister(int (*v)(void))
476 struct loadupdate *tmp, *tmpl=NULL;
477 if (ast_mutex_lock(&modlock))
478 ast_log(LOG_WARNING, "Failed to lock\n");
481 if (tmp->updater == v) {
483 tmpl->next = tmp->next;
485 updaters = tmp->next;
493 ast_mutex_unlock(&modlock);