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>
29 int (*load_module)(void);
30 int (*unload_module)(void);
31 int (*usecount)(void);
32 char *(*description)(void);
38 static struct loadupdate {
40 struct loadupdate *next;
43 static pthread_mutex_t modlock = PTHREAD_MUTEX_INITIALIZER;
45 static struct module *module_list=NULL;
47 int ast_unload_resource(char *resource_name, int force)
49 struct module *m, *ml = NULL;
51 if (pthread_mutex_lock(&modlock))
52 ast_log(LOG_WARNING, "Failed to lock\n");
55 if (!strcasecmp(m->resource, resource_name)) {
56 if ((res = m->usecount()) > 0) {
58 ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
60 ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
61 pthread_mutex_unlock(&modlock);
65 res = m->unload_module();
67 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
68 if (force <= AST_FORCE_FIRM) {
69 pthread_mutex_unlock(&modlock);
72 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
77 module_list = m->next;
84 pthread_mutex_unlock(&modlock);
85 ast_update_use_count();
89 int ast_load_resource(char *resource_name)
95 if (pthread_mutex_lock(&modlock))
96 ast_log(LOG_WARNING, "Failed to lock\n");
99 if (!strcasecmp(m->resource, resource_name)) {
100 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
101 pthread_mutex_unlock(&modlock);
106 m = malloc(sizeof(struct module));
108 ast_log(LOG_WARNING, "Out of memory\n");
109 pthread_mutex_unlock(&modlock);
112 strncpy(m->resource, resource_name, sizeof(m->resource));
113 if (resource_name[0] == '/') {
114 strncpy(fn, resource_name, sizeof(fn));
116 snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
118 m->lib = dlopen(fn, RTLD_NOW | RTLD_GLOBAL);
120 ast_log(LOG_WARNING, "%s\n", dlerror());
122 pthread_mutex_unlock(&modlock);
125 m->load_module = dlsym(m->lib, "load_module");
126 if (!m->load_module) {
127 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
130 m->unload_module = dlsym(m->lib, "unload_module");
131 if (!m->unload_module) {
132 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
135 m->usecount = dlsym(m->lib, "usecount");
137 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
140 m->description = dlsym(m->lib, "description");
141 if (!m->description) {
142 ast_log(LOG_WARNING, "No description in module %s\n", fn);
146 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
149 pthread_mutex_unlock(&modlock);
153 ast_verbose( " => (%s)\n", m->description());
154 pthread_mutex_unlock(&modlock);
155 if ((res = m->load_module())) {
156 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, fn, res);
157 ast_unload_resource(resource_name, 0);
160 m->next = module_list;
165 static int ast_resource_exists(char *resource)
168 if (pthread_mutex_lock(&modlock))
169 ast_log(LOG_WARNING, "Failed to lock\n");
172 if (!strcasecmp(resource, m->resource))
176 pthread_mutex_unlock(&modlock);
185 struct ast_config *cfg;
186 struct ast_variable *v;
188 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
189 cfg = ast_load(AST_MODULE_CONFIG);
191 /* Load explicitly defined modules */
192 v = ast_variable_browse(cfg, "modules");
194 if (!strcasecmp(v->name, "load")) {
195 if (option_debug && !option_verbose)
196 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
197 if (option_verbose) {
198 ast_verbose( " [%s]", v->value);
201 if (ast_load_resource(v->value)) {
202 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
211 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
212 /* Load all modules */
215 mods = opendir(AST_MODULE_DIR);
217 while((d = readdir(mods))) {
218 /* Must end in .so to load it. */
219 if ((strlen(d->d_name) > 3) &&
220 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
221 !ast_resource_exists(d->d_name)) {
222 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
223 an inefficient way to do it, but oh well. */
225 v = ast_variable_browse(cfg, "modules");
227 if (!strcasecmp(v->name, "noload") &&
228 !strcasecmp(v->value, d->d_name))
233 if (option_verbose) {
234 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
241 if (option_debug && !option_verbose)
242 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
243 if (option_verbose) {
244 ast_verbose( VERBOSE_PREFIX_1 "[%s]", d->d_name);
247 if (ast_load_resource(d->d_name)) {
248 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
257 ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
264 void ast_update_use_count(void)
266 /* Notify any module monitors that the use count for a
267 resource has changed */
268 struct loadupdate *m;
269 if (pthread_mutex_lock(&modlock))
270 ast_log(LOG_WARNING, "Failed to lock\n");
276 pthread_mutex_unlock(&modlock);
280 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
284 if (pthread_mutex_trylock(&modlock))
288 modentry(m->resource, m->description(), m->usecount());
292 pthread_mutex_unlock(&modlock);
296 int ast_loader_register(int (*v)(void))
298 struct loadupdate *tmp;
299 /* XXX Should be more flexible here, taking > 1 verboser XXX */
300 if ((tmp = malloc(sizeof (struct loadupdate)))) {
302 if (pthread_mutex_lock(&modlock))
303 ast_log(LOG_WARNING, "Failed to lock\n");
304 tmp->next = updaters;
306 pthread_mutex_unlock(&modlock);
312 int ast_loader_unregister(int (*v)(void))
315 struct loadupdate *tmp, *tmpl=NULL;
316 if (pthread_mutex_lock(&modlock))
317 ast_log(LOG_WARNING, "Failed to lock\n");
320 if (tmp->updater == v) {
322 tmpl->next = tmp->next;
324 updaters = tmp->next;
332 pthread_mutex_unlock(&modlock);