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)
98 struct ast_config *cfg;
99 /* Keep the module file parsing silent */
102 cfg = ast_load(AST_MODULE_CONFIG);
105 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
107 flags |= RTLD_GLOBAL;
111 if (pthread_mutex_lock(&modlock))
112 ast_log(LOG_WARNING, "Failed to lock\n");
115 if (!strcasecmp(m->resource, resource_name)) {
116 ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
117 pthread_mutex_unlock(&modlock);
122 m = malloc(sizeof(struct module));
124 ast_log(LOG_WARNING, "Out of memory\n");
125 pthread_mutex_unlock(&modlock);
128 strncpy(m->resource, resource_name, sizeof(m->resource));
129 if (resource_name[0] == '/') {
130 strncpy(fn, resource_name, sizeof(fn));
132 snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
134 m->lib = dlopen(fn, RTLD_NOW | flags);
136 ast_log(LOG_WARNING, "%s\n", dlerror());
138 pthread_mutex_unlock(&modlock);
141 m->load_module = dlsym(m->lib, "load_module");
142 if (!m->load_module) {
143 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
146 m->unload_module = dlsym(m->lib, "unload_module");
147 if (!m->unload_module) {
148 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
151 m->usecount = dlsym(m->lib, "usecount");
153 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
156 m->description = dlsym(m->lib, "description");
157 if (!m->description) {
158 ast_log(LOG_WARNING, "No description in module %s\n", fn);
162 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
165 pthread_mutex_unlock(&modlock);
170 ast_verbose( " => (%s)\n", m->description());
171 if (option_console && !option_verbose)
175 ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
177 m->next = module_list;
179 pthread_mutex_unlock(&modlock);
180 if ((res = m->load_module())) {
181 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
182 ast_unload_resource(resource_name, 0);
185 ast_update_use_count();
189 static int ast_resource_exists(char *resource)
192 if (pthread_mutex_lock(&modlock))
193 ast_log(LOG_WARNING, "Failed to lock\n");
196 if (!strcasecmp(resource, m->resource))
200 pthread_mutex_unlock(&modlock);
209 struct ast_config *cfg;
210 struct ast_variable *v;
212 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
213 cfg = ast_load(AST_MODULE_CONFIG);
215 /* Load explicitly defined modules */
216 v = ast_variable_browse(cfg, "modules");
218 if (!strcasecmp(v->name, "load")) {
219 if (option_debug && !option_verbose)
220 ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
221 if (option_verbose) {
222 ast_verbose( " [%s]", v->value);
225 if (ast_load_resource(v->value)) {
226 ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
235 if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
236 /* Load all modules */
239 mods = opendir(AST_MODULE_DIR);
241 while((d = readdir(mods))) {
242 /* Must end in .so to load it. */
243 if ((strlen(d->d_name) > 3) &&
244 !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
245 !ast_resource_exists(d->d_name)) {
246 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
247 an inefficient way to do it, but oh well. */
249 v = ast_variable_browse(cfg, "modules");
251 if (!strcasecmp(v->name, "noload") &&
252 !strcasecmp(v->value, d->d_name))
257 if (option_verbose) {
258 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
265 if (option_debug && !option_verbose)
266 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
267 if (option_verbose) {
268 ast_verbose( VERBOSE_PREFIX_1 "[%s]", d->d_name);
271 if (ast_load_resource(d->d_name)) {
272 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
281 ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
288 void ast_update_use_count(void)
290 /* Notify any module monitors that the use count for a
291 resource has changed */
292 struct loadupdate *m;
293 if (pthread_mutex_lock(&modlock))
294 ast_log(LOG_WARNING, "Failed to lock\n");
300 pthread_mutex_unlock(&modlock);
304 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
308 if (pthread_mutex_trylock(&modlock))
312 modentry(m->resource, m->description(), m->usecount());
316 pthread_mutex_unlock(&modlock);
320 int ast_loader_register(int (*v)(void))
322 struct loadupdate *tmp;
323 /* XXX Should be more flexible here, taking > 1 verboser XXX */
324 if ((tmp = malloc(sizeof (struct loadupdate)))) {
326 if (pthread_mutex_lock(&modlock))
327 ast_log(LOG_WARNING, "Failed to lock\n");
328 tmp->next = updaters;
330 pthread_mutex_unlock(&modlock);
336 int ast_loader_unregister(int (*v)(void))
339 struct loadupdate *tmp, *tmpl=NULL;
340 if (pthread_mutex_lock(&modlock))
341 ast_log(LOG_WARNING, "Failed to lock\n");
344 if (tmp->updater == v) {
346 tmpl->next = tmp->next;
348 updaters = tmp->next;
356 pthread_mutex_unlock(&modlock);