Version 0.1.2 from FTP
[asterisk/asterisk.git] / loader.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Module Loader
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <dirent.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <asterisk/module.h>
20 #include <asterisk/options.h>
21 #include <asterisk/config.h>
22 #include <asterisk/logger.h>
23 #include <dlfcn.h>
24 #define __USE_GNU
25 #include <pthread.h>
26 #include "asterisk.h"
27
28 struct module {
29         int (*load_module)(void);
30         int (*unload_module)(void);
31         int (*usecount)(void);
32         char *(*description)(void);
33         void *lib;
34         char resource[256];
35         struct module *next;
36 };
37
38 static struct loadupdate {
39         int (*updater)(void);
40         struct loadupdate *next;
41 } *updaters = NULL;
42
43 static pthread_mutex_t modlock = PTHREAD_MUTEX_INITIALIZER;
44
45 static struct module *module_list=NULL;
46
47 int ast_unload_resource(char *resource_name, int force)
48 {
49         struct module *m, *ml = NULL;
50         int res = -1;
51         if (pthread_mutex_lock(&modlock))
52                 ast_log(LOG_WARNING, "Failed to lock\n");
53         m = module_list;
54         while(m) {
55                 if (!strcasecmp(m->resource, resource_name)) {
56                         if ((res = m->usecount()) > 0)  {
57                                 if (force) 
58                                         ast_log(LOG_WARNING, "Warning:  Forcing removal of module %s with use count %d\n", resource_name, res);
59                                 else {
60                                         ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
61                                         pthread_mutex_unlock(&modlock);
62                                         return -1;
63                                 }
64                         }
65                         res = m->unload_module();
66                         if (res) {
67                                 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
68                                 if (force <= AST_FORCE_FIRM) {
69                                         pthread_mutex_unlock(&modlock);
70                                         return -1;
71                                 } else
72                                         ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
73                         }
74                         if (ml)
75                                 ml->next = m->next;
76                         else
77                                 module_list = m->next;
78                         dlclose(m->lib);
79                         free(m);
80                 }
81                 ml = m;
82                 m = m->next;
83         }
84         pthread_mutex_unlock(&modlock);
85         ast_update_use_count();
86         return res;
87 }
88
89 int ast_load_resource(char *resource_name)
90 {
91         static char fn[256];
92         int errors=0;
93         int res;
94         struct module *m;
95         int flags=0;
96         char *val;
97         int o;
98         struct ast_config *cfg;
99         /* Keep the module file parsing silent */
100         o = option_verbose;
101         option_verbose = 0;
102         cfg = ast_load(AST_MODULE_CONFIG);
103         option_verbose = o;
104         if (cfg) {
105                 if ((val = ast_variable_retrieve(cfg, "global", resource_name))
106                                 && ast_true(val))
107                         flags |= RTLD_GLOBAL;
108                 ast_destroy(cfg);
109         }
110         
111         if (pthread_mutex_lock(&modlock))
112                 ast_log(LOG_WARNING, "Failed to lock\n");
113         m = module_list;
114         while(m) {
115                 if (!strcasecmp(m->resource, resource_name)) {
116                         ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
117                         pthread_mutex_unlock(&modlock);
118                         return -1;
119                 }
120                 m = m->next;
121         }
122         m = malloc(sizeof(struct module));      
123         if (!m) {
124                 ast_log(LOG_WARNING, "Out of memory\n");
125                 pthread_mutex_unlock(&modlock);
126                 return -1;
127         }
128         strncpy(m->resource, resource_name, sizeof(m->resource));
129         if (resource_name[0] == '/') {
130                 strncpy(fn, resource_name, sizeof(fn));
131         } else {
132                 snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
133         }
134         m->lib = dlopen(fn, RTLD_NOW | flags);
135         if (!m->lib) {
136                 ast_log(LOG_WARNING, "%s\n", dlerror());
137                 free(m);
138                 pthread_mutex_unlock(&modlock);
139                 return -1;
140         }
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);
144                 errors++;
145         }
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);
149                 errors++;
150         }
151         m->usecount = dlsym(m->lib, "usecount");
152         if (!m->usecount) {
153                 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
154                 errors++;
155         }
156         m->description = dlsym(m->lib, "description");
157         if (!m->description) {
158                 ast_log(LOG_WARNING, "No description in module %s\n", fn);
159                 errors++;
160         }
161         if (errors) {
162                 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
163                 dlclose(m->lib);
164                 free(m);
165                 pthread_mutex_unlock(&modlock);
166                 return -1;
167         }
168         if (!fully_booted) {
169                 if (option_verbose) 
170                         ast_verbose( " => (%s)\n", m->description());
171                 if (option_console && !option_verbose)
172                         ast_verbose( ".");
173         } else {
174                 if (option_verbose)
175                         ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
176         }
177         m->next = module_list;
178         module_list = m;
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);
183                 return -1;
184         }
185         ast_update_use_count();
186         return 0;
187 }       
188
189 static int ast_resource_exists(char *resource)
190 {
191         struct module *m;
192         if (pthread_mutex_lock(&modlock))
193                 ast_log(LOG_WARNING, "Failed to lock\n");
194         m = module_list;
195         while(m) {
196                 if (!strcasecmp(resource, m->resource))
197                         break;
198                 m = m->next;
199         }
200         pthread_mutex_unlock(&modlock);
201         if (m)
202                 return -1;
203         else
204                 return 0;
205 }
206
207 int load_modules()
208 {
209         struct ast_config *cfg;
210         struct ast_variable *v;
211         if (option_verbose) 
212                 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
213         cfg = ast_load(AST_MODULE_CONFIG);
214         if (cfg) {
215                 /* Load explicitly defined modules */
216                 v = ast_variable_browse(cfg, "modules");
217                 while(v) {
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);
223                                         fflush(stdout);
224                                 }
225                                 if (ast_load_resource(v->value)) {
226                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
227                                         if (cfg)
228                                                 ast_destroy(cfg);
229                                         return -1;
230                                 }
231                         }
232                         v=v->next;
233                 }
234         }
235         if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
236                 /* Load all modules */
237                 DIR *mods;
238                 struct dirent *d;
239                 mods = opendir(AST_MODULE_DIR);
240                 if (mods) {
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. */
248                                         if (cfg) {
249                                                 v = ast_variable_browse(cfg, "modules");
250                                                 while(v) {
251                                                         if (!strcasecmp(v->name, "noload") &&
252                                                             !strcasecmp(v->value, d->d_name)) 
253                                                                 break;
254                                                         v = v->next;
255                                                 }
256                                                 if (v) {
257                                                         if (option_verbose) {
258                                                                 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
259                                                                 fflush(stdout);
260                                                         }
261                                                         continue;
262                                                 }
263                                                 
264                                         }
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);
269                                                 fflush(stdout);
270                                         }
271                                         if (ast_load_resource(d->d_name)) {
272                                                 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
273                                                 if (cfg)
274                                                         ast_destroy(cfg);
275                                                 return -1;
276                                         }
277                                 }
278                         };
279                 } else {
280                         if (!option_quiet)
281                                 ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
282                 }
283         } 
284         ast_destroy(cfg);
285         return 0;
286 }
287
288 void ast_update_use_count(void)
289 {
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");
295         m = updaters;
296         while(m) {
297                 m->updater();
298                 m = m->next;
299         }
300         pthread_mutex_unlock(&modlock);
301         
302 }
303
304 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
305 {
306         struct module *m;
307         int unlock = -1;
308         if (pthread_mutex_trylock(&modlock))
309                 unlock = 0;
310         m = module_list;
311         while(m) {
312                 modentry(m->resource, m->description(), m->usecount());
313                 m = m->next;
314         }
315         if (unlock)
316                 pthread_mutex_unlock(&modlock);
317         return 0;
318 }
319
320 int ast_loader_register(int (*v)(void)) 
321 {
322         struct loadupdate *tmp;
323         /* XXX Should be more flexible here, taking > 1 verboser XXX */
324         if ((tmp = malloc(sizeof (struct loadupdate)))) {
325                 tmp->updater = v;
326                 if (pthread_mutex_lock(&modlock))
327                         ast_log(LOG_WARNING, "Failed to lock\n");
328                 tmp->next = updaters;
329                 updaters = tmp;
330                 pthread_mutex_unlock(&modlock);
331                 return 0;
332         }
333         return -1;
334 }
335
336 int ast_loader_unregister(int (*v)(void))
337 {
338         int res = -1;
339         struct loadupdate *tmp, *tmpl=NULL;
340         if (pthread_mutex_lock(&modlock))
341                 ast_log(LOG_WARNING, "Failed to lock\n");
342         tmp = updaters;
343         while(tmp) {
344                 if (tmp->updater == v)  {
345                         if (tmpl)
346                                 tmpl->next = tmp->next;
347                         else
348                                 updaters = tmp->next;
349                         break;
350                 }
351                 tmpl = tmp;
352                 tmp = tmp->next;
353         }
354         if (tmp)
355                 res = 0;
356         pthread_mutex_unlock(&modlock);
357         return res;
358 }