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>
24 #include <asterisk/md5.h>
29 static char expected_key[] =
30 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
31 0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
34 int (*load_module)(void);
35 int (*unload_module)(void);
36 int (*usecount)(void);
37 char *(*description)(void);
45 static int printdigest(unsigned char *d)
50 snprintf(buf, sizeof(buf), "Unexpected signature:");
52 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
56 ast_log(LOG_DEBUG, buf);
60 static int key_matches(char *key1, char *key2)
65 match &= (key1[x] == key2[x]);
70 static int verify_key(char *key)
75 MD5Update(&c, key, strlen(key));
77 if (key_matches(expected_key, digest))
83 static struct loadupdate {
85 struct loadupdate *next;
88 static pthread_mutex_t modlock = PTHREAD_MUTEX_INITIALIZER;
90 static struct module *module_list=NULL;
92 int ast_unload_resource(char *resource_name, int force)
94 struct module *m, *ml = NULL;
96 if (pthread_mutex_lock(&modlock))
97 ast_log(LOG_WARNING, "Failed to lock\n");
100 if (!strcasecmp(m->resource, resource_name)) {
101 if ((res = m->usecount()) > 0) {
103 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
105 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
106 pthread_mutex_unlock(&modlock);
110 res = m->unload_module();
112 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
113 if (force <= AST_FORCE_FIRM) {
114 pthread_mutex_unlock(&modlock);
117 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
122 module_list = m->next;
129 pthread_mutex_unlock(&modlock);
130 ast_update_use_count();
134 int ast_load_resource(char *resource_name)
144 struct ast_config *cfg;
145 /* Keep the module file parsing silent */
148 cfg = ast_load(AST_MODULE_CONFIG);
151 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
153 flags |= RTLD_GLOBAL;
157 if (pthread_mutex_lock(&modlock))
158 ast_log(LOG_WARNING, "Failed to lock\n");
161 if (!strcasecmp(m->resource, resource_name)) {
162 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
163 pthread_mutex_unlock(&modlock);
168 m = malloc(sizeof(struct module));
170 ast_log(LOG_WARNING, "Out of memory\n");
171 pthread_mutex_unlock(&modlock);
174 strncpy(m->resource, resource_name, sizeof(m->resource));
175 if (resource_name[0] == '/') {
176 strncpy(fn, resource_name, sizeof(fn));
178 snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
180 m->lib = dlopen(fn, RTLD_NOW | flags);
182 ast_log(LOG_WARNING, "%s\n", dlerror());
184 pthread_mutex_unlock(&modlock);
187 m->load_module = dlsym(m->lib, "load_module");
188 if (!m->load_module) {
189 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
192 m->unload_module = dlsym(m->lib, "unload_module");
193 if (!m->unload_module) {
194 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
197 m->usecount = dlsym(m->lib, "usecount");
199 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
202 m->description = dlsym(m->lib, "description");
203 if (!m->description) {
204 ast_log(LOG_WARNING, "No description in module %s\n", fn);
207 m->key = dlsym(m->lib, "key");
209 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
212 m->reload = dlsym(m->lib, "reload");
213 if (m->key && !(key = m->key())) {
214 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
218 if (key && verify_key(key)) {
219 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
223 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
226 pthread_mutex_unlock(&modlock);
231 ast_verbose( " => (%s)\n", m->description());
232 if (option_console && !option_verbose)
236 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
238 m->next = module_list;
241 pthread_mutex_unlock(&modlock);
242 if ((res = m->load_module())) {
243 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
244 ast_unload_resource(resource_name, 0);
247 ast_update_use_count();
251 static int ast_resource_exists(char *resource)
254 if (pthread_mutex_lock(&modlock))
255 ast_log(LOG_WARNING, "Failed to lock\n");
258 if (!strcasecmp(resource, m->resource))
262 pthread_mutex_unlock(&modlock);
271 struct ast_config *cfg;
272 struct ast_variable *v;
274 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
275 cfg = ast_load(AST_MODULE_CONFIG);
277 /* Load explicitly defined modules */
278 v = ast_variable_browse(cfg, "modules");
280 if (!strcasecmp(v->name, "load")) {
281 if (option_debug && !option_verbose)
282 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
283 if (option_verbose) {
284 ast_verbose( " [%s]", v->value);
287 if (ast_load_resource(v->value)) {
288 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
297 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
298 /* Load all modules */
301 mods = opendir(AST_MODULE_DIR);
303 while((d = readdir(mods))) {
304 /* Must end in .so to load it. */
305 if ((strlen(d->d_name) > 3) &&
306 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
307 !ast_resource_exists(d->d_name)) {
308 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
309 an inefficient way to do it, but oh well. */
311 v = ast_variable_browse(cfg, "modules");
313 if (!strcasecmp(v->name, "noload") &&
314 !strcasecmp(v->value, d->d_name))
319 if (option_verbose) {
320 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
327 if (option_debug && !option_verbose)
328 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
329 if (option_verbose) {
330 ast_verbose( VERBOSE_PREFIX_1 "[%s]", d->d_name);
333 if (ast_load_resource(d->d_name)) {
334 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
343 ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
350 void ast_update_use_count(void)
352 /* Notify any module monitors that the use count for a
353 resource has changed */
354 struct loadupdate *m;
355 if (pthread_mutex_lock(&modlock))
356 ast_log(LOG_WARNING, "Failed to lock\n");
362 pthread_mutex_unlock(&modlock);
366 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
370 if (pthread_mutex_trylock(&modlock))
374 modentry(m->resource, m->description(), m->usecount());
378 pthread_mutex_unlock(&modlock);
382 int ast_loader_register(int (*v)(void))
384 struct loadupdate *tmp;
385 /* XXX Should be more flexible here, taking > 1 verboser XXX */
386 if ((tmp = malloc(sizeof (struct loadupdate)))) {
388 if (pthread_mutex_lock(&modlock))
389 ast_log(LOG_WARNING, "Failed to lock\n");
390 tmp->next = updaters;
392 pthread_mutex_unlock(&modlock);
398 int ast_loader_unregister(int (*v)(void))
401 struct loadupdate *tmp, *tmpl=NULL;
402 if (pthread_mutex_lock(&modlock))
403 ast_log(LOG_WARNING, "Failed to lock\n");
406 if (tmp->updater == v) {
408 tmpl->next = tmp->next;
410 updaters = tmp->next;
418 pthread_mutex_unlock(&modlock);