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>
26 #include <asterisk/md5.h>
31 static char expected_key[] =
32 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
33 0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
36 int (*load_module)(void);
37 int (*unload_module)(void);
38 int (*usecount)(void);
39 char *(*description)(void);
47 static int printdigest(unsigned char *d)
52 snprintf(buf, sizeof(buf), "Unexpected signature:");
54 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
58 ast_log(LOG_DEBUG, buf);
62 static int key_matches(char *key1, char *key2)
67 match &= (key1[x] == key2[x]);
72 static int verify_key(char *key)
77 MD5Update(&c, key, strlen(key));
79 if (key_matches(expected_key, digest))
85 static struct loadupdate {
87 struct loadupdate *next;
90 static pthread_mutex_t modlock = AST_MUTEX_INITIALIZER;
92 static struct module *module_list=NULL;
94 int ast_unload_resource(char *resource_name, int force)
96 struct module *m, *ml = NULL;
98 if (ast_pthread_mutex_lock(&modlock))
99 ast_log(LOG_WARNING, "Failed to lock\n");
102 if (!strcasecmp(m->resource, resource_name)) {
103 if ((res = m->usecount()) > 0) {
105 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
107 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
108 ast_pthread_mutex_unlock(&modlock);
112 res = m->unload_module();
114 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
115 if (force <= AST_FORCE_FIRM) {
116 ast_pthread_mutex_unlock(&modlock);
119 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
124 module_list = m->next;
131 ast_pthread_mutex_unlock(&modlock);
132 ast_update_use_count();
136 void ast_module_reload(void)
140 /* We'll do the logger the favor of calling its reload here first */
143 ast_pthread_mutex_lock(&modlock);
147 if (option_verbose > 2)
148 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
153 ast_pthread_mutex_unlock(&modlock);
156 int ast_load_resource(char *resource_name)
166 struct ast_config *cfg;
168 /* Keep the module file parsing silent */
170 if (strncasecmp(resource_name, "res_", 4)) {
172 cfg = ast_load(AST_MODULE_CONFIG);
175 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
177 flags |= RTLD_GLOBAL;
181 /* Resource modules are always loaded global and lazy */
182 flags = (RTLD_GLOBAL | RTLD_LAZY);
185 if (ast_pthread_mutex_lock(&modlock))
186 ast_log(LOG_WARNING, "Failed to lock\n");
189 if (!strcasecmp(m->resource, resource_name)) {
190 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
191 ast_pthread_mutex_unlock(&modlock);
196 m = malloc(sizeof(struct module));
198 ast_log(LOG_WARNING, "Out of memory\n");
199 ast_pthread_mutex_unlock(&modlock);
202 strncpy(m->resource, resource_name, sizeof(m->resource)-1);
203 if (resource_name[0] == '/') {
204 strncpy(fn, resource_name, sizeof(fn)-1);
206 snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
208 m->lib = dlopen(fn, flags);
210 ast_log(LOG_WARNING, "%s\n", dlerror());
212 ast_pthread_mutex_unlock(&modlock);
215 m->load_module = dlsym(m->lib, "load_module");
216 if (!m->load_module) {
217 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
220 m->unload_module = dlsym(m->lib, "unload_module");
221 if (!m->unload_module) {
222 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
225 m->usecount = dlsym(m->lib, "usecount");
227 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
230 m->description = dlsym(m->lib, "description");
231 if (!m->description) {
232 ast_log(LOG_WARNING, "No description in module %s\n", fn);
235 m->key = dlsym(m->lib, "key");
237 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
240 m->reload = dlsym(m->lib, "reload");
241 if (m->key && !(key = m->key())) {
242 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
246 if (key && verify_key(key)) {
247 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
251 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
254 ast_pthread_mutex_unlock(&modlock);
259 ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
260 if (option_console && !option_verbose)
264 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
266 m->next = module_list;
269 ast_pthread_mutex_unlock(&modlock);
270 if ((res = m->load_module())) {
271 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
272 ast_unload_resource(resource_name, 0);
275 ast_update_use_count();
279 static int ast_resource_exists(char *resource)
282 if (ast_pthread_mutex_lock(&modlock))
283 ast_log(LOG_WARNING, "Failed to lock\n");
286 if (!strcasecmp(resource, m->resource))
290 ast_pthread_mutex_unlock(&modlock);
299 struct ast_config *cfg;
300 struct ast_variable *v;
303 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
304 cfg = ast_load(AST_MODULE_CONFIG);
306 /* Load explicitly defined modules */
307 v = ast_variable_browse(cfg, "modules");
309 if (!strcasecmp(v->name, "load")) {
310 if (option_debug && !option_verbose)
311 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
312 if (option_verbose) {
313 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
316 if (ast_load_resource(v->value)) {
317 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
326 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
327 /* Load all modules */
331 /* Make two passes. First, load any resource modules, then load the others. */
333 mods = opendir(AST_MODULE_DIR);
335 while((d = readdir(mods))) {
336 /* Must end in .so to load it. */
337 if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) &&
338 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
339 !ast_resource_exists(d->d_name)) {
340 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
341 an inefficient way to do it, but oh well. */
343 v = ast_variable_browse(cfg, "modules");
345 if (!strcasecmp(v->name, "noload") &&
346 !strcasecmp(v->value, d->d_name))
351 if (option_verbose) {
352 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
359 if (option_debug && !option_verbose)
360 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
361 if (option_verbose) {
362 ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
365 if (ast_load_resource(d->d_name)) {
366 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
376 ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
384 void ast_update_use_count(void)
386 /* Notify any module monitors that the use count for a
387 resource has changed */
388 struct loadupdate *m;
389 if (ast_pthread_mutex_lock(&modlock))
390 ast_log(LOG_WARNING, "Failed to lock\n");
396 ast_pthread_mutex_unlock(&modlock);
400 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
404 if (pthread_mutex_trylock(&modlock))
408 modentry(m->resource, m->description(), m->usecount());
412 ast_pthread_mutex_unlock(&modlock);
416 int ast_loader_register(int (*v)(void))
418 struct loadupdate *tmp;
419 /* XXX Should be more flexible here, taking > 1 verboser XXX */
420 if ((tmp = malloc(sizeof (struct loadupdate)))) {
422 if (ast_pthread_mutex_lock(&modlock))
423 ast_log(LOG_WARNING, "Failed to lock\n");
424 tmp->next = updaters;
426 ast_pthread_mutex_unlock(&modlock);
432 int ast_loader_unregister(int (*v)(void))
435 struct loadupdate *tmp, *tmpl=NULL;
436 if (ast_pthread_mutex_lock(&modlock))
437 ast_log(LOG_WARNING, "Failed to lock\n");
440 if (tmp->updater == v) {
442 tmpl->next = tmp->next;
444 updaters = tmp->next;
452 ast_pthread_mutex_unlock(&modlock);