d6a549b4b1a124e6e04f55974dd3d69cba0d7053
[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         if (pthread_mutex_lock(&modlock))
96                 ast_log(LOG_WARNING, "Failed to lock\n");
97         m = module_list;
98         while(m) {
99                 if (!strcasecmp(m->resource, resource_name)) {
100                         ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
101                         pthread_mutex_unlock(&modlock);
102                         return -1;
103                 }
104                 m = m->next;
105         }
106         m = malloc(sizeof(struct module));      
107         if (!m) {
108                 ast_log(LOG_WARNING, "Out of memory\n");
109                 pthread_mutex_unlock(&modlock);
110                 return -1;
111         }
112         strncpy(m->resource, resource_name, sizeof(m->resource));
113         if (resource_name[0] == '/') {
114                 strncpy(fn, resource_name, sizeof(fn));
115         } else {
116                 snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
117         }
118         m->lib = dlopen(fn, RTLD_NOW  | RTLD_GLOBAL);
119         if (!m->lib) {
120                 ast_log(LOG_WARNING, "%s\n", dlerror());
121                 free(m);
122                 pthread_mutex_unlock(&modlock);
123                 return -1;
124         }
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);
128                 errors++;
129         }
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);
133                 errors++;
134         }
135         m->usecount = dlsym(m->lib, "usecount");
136         if (!m->usecount) {
137                 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
138                 errors++;
139         }
140         m->description = dlsym(m->lib, "description");
141         if (!m->description) {
142                 ast_log(LOG_WARNING, "No description in module %s\n", fn);
143                 errors++;
144         }
145         if (errors) {
146                 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
147                 dlclose(m->lib);
148                 free(m);
149                 pthread_mutex_unlock(&modlock);
150                 return -1;
151         }
152         if (option_verbose) 
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);
158                 return -1;
159         }
160         m->next = module_list;
161         module_list = m;
162         return 0;
163 }       
164
165 static int ast_resource_exists(char *resource)
166 {
167         struct module *m;
168         if (pthread_mutex_lock(&modlock))
169                 ast_log(LOG_WARNING, "Failed to lock\n");
170         m = module_list;
171         while(m) {
172                 if (!strcasecmp(resource, m->resource))
173                         break;
174                 m = m->next;
175         }
176         pthread_mutex_unlock(&modlock);
177         if (m)
178                 return -1;
179         else
180                 return 0;
181 }
182
183 int load_modules()
184 {
185         struct ast_config *cfg;
186         struct ast_variable *v;
187         if (option_verbose) 
188                 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
189         cfg = ast_load(AST_MODULE_CONFIG);
190         if (cfg) {
191                 /* Load explicitly defined modules */
192                 v = ast_variable_browse(cfg, "modules");
193                 while(v) {
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);
199                                         fflush(stdout);
200                                 }
201                                 if (ast_load_resource(v->value)) {
202                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
203                                         if (cfg)
204                                                 ast_destroy(cfg);
205                                         return -1;
206                                 }
207                         }
208                         v=v->next;
209                 }
210         }
211         if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
212                 /* Load all modules */
213                 DIR *mods;
214                 struct dirent *d;
215                 mods = opendir(AST_MODULE_DIR);
216                 if (mods) {
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. */
224                                         if (cfg) {
225                                                 v = ast_variable_browse(cfg, "modules");
226                                                 while(v) {
227                                                         if (!strcasecmp(v->name, "noload") &&
228                                                             !strcasecmp(v->value, d->d_name)) 
229                                                                 break;
230                                                         v = v->next;
231                                                 }
232                                                 if (v) {
233                                                         if (option_verbose) {
234                                                                 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
235                                                                 fflush(stdout);
236                                                         }
237                                                         continue;
238                                                 }
239                                                 
240                                         }
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);
245                                                 fflush(stdout);
246                                         }
247                                         if (ast_load_resource(d->d_name)) {
248                                                 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
249                                                 if (cfg)
250                                                         ast_destroy(cfg);
251                                                 return -1;
252                                         }
253                                 }
254                         };
255                 } else {
256                         if (!option_quiet)
257                                 ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
258                 }
259         } 
260         ast_destroy(cfg);
261         return 0;
262 }
263
264 void ast_update_use_count(void)
265 {
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");
271         m = updaters;
272         while(m) {
273                 m->updater();
274                 m = m->next;
275         }
276         pthread_mutex_unlock(&modlock);
277         
278 }
279
280 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
281 {
282         struct module *m;
283         int unlock = -1;
284         if (pthread_mutex_trylock(&modlock))
285                 unlock = 0;
286         m = module_list;
287         while(m) {
288                 modentry(m->resource, m->description(), m->usecount());
289                 m = m->next;
290         }
291         if (unlock)
292                 pthread_mutex_unlock(&modlock);
293         return 0;
294 }
295
296 int ast_loader_register(int (*v)(void)) 
297 {
298         struct loadupdate *tmp;
299         /* XXX Should be more flexible here, taking > 1 verboser XXX */
300         if ((tmp = malloc(sizeof (struct loadupdate)))) {
301                 tmp->updater = v;
302                 if (pthread_mutex_lock(&modlock))
303                         ast_log(LOG_WARNING, "Failed to lock\n");
304                 tmp->next = updaters;
305                 updaters = tmp;
306                 pthread_mutex_unlock(&modlock);
307                 return 0;
308         }
309         return -1;
310 }
311
312 int ast_loader_unregister(int (*v)(void))
313 {
314         int res = -1;
315         struct loadupdate *tmp, *tmpl=NULL;
316         if (pthread_mutex_lock(&modlock))
317                 ast_log(LOG_WARNING, "Failed to lock\n");
318         tmp = updaters;
319         while(tmp) {
320                 if (tmp->updater == v)  {
321                         if (tmpl)
322                                 tmpl->next = tmp->next;
323                         else
324                                 updaters = tmp->next;
325                         break;
326                 }
327                 tmpl = tmp;
328                 tmp = tmp->next;
329         }
330         if (tmp)
331                 res = 0;
332         pthread_mutex_unlock(&modlock);
333         return res;
334 }