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>
27 #include <asterisk/md5.h>
33 static char expected_key[] =
34 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
35 0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
38 int (*load_module)(void);
39 int (*unload_module)(void);
40 int (*usecount)(void);
41 char *(*description)(void);
49 static int printdigest(unsigned char *d)
54 snprintf(buf, sizeof(buf), "Unexpected signature:");
56 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
60 ast_log(LOG_DEBUG, buf);
64 static int key_matches(char *key1, char *key2)
69 match &= (key1[x] == key2[x]);
74 static int verify_key(char *key)
79 MD5Update(&c, key, strlen(key));
81 if (key_matches(expected_key, digest))
87 static struct loadupdate {
89 struct loadupdate *next;
92 static pthread_mutex_t modlock = AST_MUTEX_INITIALIZER;
94 static struct module *module_list=NULL;
96 int ast_unload_resource(char *resource_name, int force)
98 struct module *m, *ml = NULL;
100 if (ast_pthread_mutex_lock(&modlock))
101 ast_log(LOG_WARNING, "Failed to lock\n");
104 if (!strcasecmp(m->resource, resource_name)) {
105 if ((res = m->usecount()) > 0) {
107 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
109 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
110 ast_pthread_mutex_unlock(&modlock);
114 res = m->unload_module();
116 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
117 if (force <= AST_FORCE_FIRM) {
118 ast_pthread_mutex_unlock(&modlock);
121 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
126 module_list = m->next;
133 ast_pthread_mutex_unlock(&modlock);
134 ast_update_use_count();
138 void ast_module_reload(void)
142 /* We'll do the logger and manager the favor of calling its reload here first */
145 ast_pthread_mutex_lock(&modlock);
149 if (option_verbose > 2)
150 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
155 ast_pthread_mutex_unlock(&modlock);
158 int ast_load_resource(char *resource_name)
168 struct ast_config *cfg;
170 /* Keep the module file parsing silent */
172 if (strncasecmp(resource_name, "res_", 4)) {
174 cfg = ast_load(AST_MODULE_CONFIG);
177 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
179 flags |= RTLD_GLOBAL;
183 /* Resource modules are always loaded global and lazy */
184 flags = (RTLD_GLOBAL | RTLD_LAZY);
187 if (ast_pthread_mutex_lock(&modlock))
188 ast_log(LOG_WARNING, "Failed to lock\n");
191 if (!strcasecmp(m->resource, resource_name)) {
192 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
193 ast_pthread_mutex_unlock(&modlock);
198 m = malloc(sizeof(struct module));
200 ast_log(LOG_WARNING, "Out of memory\n");
201 ast_pthread_mutex_unlock(&modlock);
204 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
205 if (resource_name[0] == '/') {
206 strncpy(fn, resource_name, sizeof(fn)-1);
208 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
210 m->lib = dlopen(fn, flags);
212 ast_log(LOG_WARNING, "%s\n", dlerror());
214 ast_pthread_mutex_unlock(&modlock);
217 m->load_module = dlsym(m->lib, "load_module");
218 if (!m->load_module) {
219 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
222 m->unload_module = dlsym(m->lib, "unload_module");
223 if (!m->unload_module) {
224 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
227 m->usecount = dlsym(m->lib, "usecount");
229 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
232 m->description = dlsym(m->lib, "description");
233 if (!m->description) {
234 ast_log(LOG_WARNING, "No description in module %s\n", fn);
237 m->key = dlsym(m->lib, "key");
239 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
242 m->reload = dlsym(m->lib, "reload");
243 if (m->key && !(key = m->key())) {
244 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
248 if (key && verify_key(key)) {
249 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
253 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
256 ast_pthread_mutex_unlock(&modlock);
261 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
262 if (option_console && !option_verbose)
266 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
268 m->next = module_list;
271 ast_pthread_mutex_unlock(&modlock);
272 if ((res = m->load_module())) {
273 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
274 ast_unload_resource(resource_name, 0);
277 ast_update_use_count();
281 static int ast_resource_exists(char *resource)
284 if (ast_pthread_mutex_lock(&modlock))
285 ast_log(LOG_WARNING, "Failed to lock\n");
288 if (!strcasecmp(resource, m->resource))
292 ast_pthread_mutex_unlock(&modlock);
301 struct ast_config *cfg;
302 struct ast_variable *v;
305 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
306 cfg = ast_load(AST_MODULE_CONFIG);
308 /* Load explicitly defined modules */
309 v = ast_variable_browse(cfg, "modules");
311 if (!strcasecmp(v->name, "load")) {
312 if (option_debug && !option_verbose)
313 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
314 if (option_verbose) {
315 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
318 if (ast_load_resource(v->value)) {
319 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
328 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
329 /* Load all modules */
333 /* Make two passes. First, load any resource modules, then load the others. */
335 mods = opendir((char *)ast_config_AST_MODULE_DIR);
337 while((d = readdir(mods))) {
338 /* Must end in .so to load it. */
339 if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) &&
340 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
341 !ast_resource_exists(d->d_name)) {
342 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
343 an inefficient way to do it, but oh well. */
345 v = ast_variable_browse(cfg, "modules");
347 if (!strcasecmp(v->name, "noload") &&
348 !strcasecmp(v->value, d->d_name))
353 if (option_verbose) {
354 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
361 if (option_debug && !option_verbose)
362 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
363 if (option_verbose) {
364 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
367 if (ast_load_resource(d->d_name)) {
368 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
378 ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
386 void ast_update_use_count(void)
388 /* Notify any module monitors that the use count for a
389 resource has changed */
390 struct loadupdate *m;
391 if (ast_pthread_mutex_lock(&modlock))
392 ast_log(LOG_WARNING, "Failed to lock\n");
398 ast_pthread_mutex_unlock(&modlock);
402 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
406 if (pthread_mutex_trylock(&modlock))
410 modentry(m->resource, m->description(), m->usecount());
414 ast_pthread_mutex_unlock(&modlock);
418 int ast_loader_register(int (*v)(void))
420 struct loadupdate *tmp;
421 /* XXX Should be more flexible here, taking > 1 verboser XXX */
422 if ((tmp = malloc(sizeof (struct loadupdate)))) {
424 if (ast_pthread_mutex_lock(&modlock))
425 ast_log(LOG_WARNING, "Failed to lock\n");
426 tmp->next = updaters;
428 ast_pthread_mutex_unlock(&modlock);
434 int ast_loader_unregister(int (*v)(void))
437 struct loadupdate *tmp, *tmpl=NULL;
438 if (ast_pthread_mutex_lock(&modlock))
439 ast_log(LOG_WARNING, "Failed to lock\n");
442 if (tmp->updater == v) {
444 tmpl->next = tmp->next;
446 updaters = tmp->next;
454 ast_pthread_mutex_unlock(&modlock);