Version 0.1.1 from FTP
[asterisk/asterisk.git] / loader.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Module Loader
5  * 
6  * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
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 = malloc(sizeof(struct module));
95         if (!m) {
96                 ast_log(LOG_WARNING, "Out of memory\n");
97                 return -1;
98         }
99         strncpy(m->resource, resource_name, sizeof(m->resource));
100         if (resource_name[0] == '/') {
101                 strncpy(fn, resource_name, sizeof(fn));
102         } else {
103                 snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
104         }
105         m->lib = dlopen(fn, RTLD_NOW  | RTLD_GLOBAL);
106         if (!m->lib) {
107                 ast_log(LOG_WARNING, "%s\n", dlerror());
108                 free(m);
109                 return -1;
110         }
111         m->load_module = dlsym(m->lib, "load_module");
112         if (!m->load_module) {
113                 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
114                 errors++;
115         }
116         m->unload_module = dlsym(m->lib, "unload_module");
117         if (!m->unload_module) {
118                 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
119                 errors++;
120         }
121         m->usecount = dlsym(m->lib, "usecount");
122         if (!m->usecount) {
123                 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
124                 errors++;
125         }
126         m->description = dlsym(m->lib, "description");
127         if (!m->description) {
128                 ast_log(LOG_WARNING, "No description in module %s\n", fn);
129                 errors++;
130         }
131         if (errors) {
132                 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
133                 dlclose(m->lib);
134                 free(m);
135                 return -1;
136         }
137         if (option_verbose) 
138                 ast_verbose( " => (%s)\n", m->description());
139         if ((res = m->load_module())) {
140                 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, fn, res);
141                 free(m);
142                 return -1;
143         }
144         if (pthread_mutex_lock(&modlock))
145                 ast_log(LOG_WARNING, "Failed to lock\n");
146         m->next = module_list;
147         module_list = m;
148         pthread_mutex_unlock(&modlock);
149         ast_update_use_count();
150         return 0;
151 }       
152
153 int ast_resource_exists(char *resource)
154 {
155         struct module *m;
156         if (pthread_mutex_lock(&modlock))
157                 ast_log(LOG_WARNING, "Failed to lock\n");
158         m = module_list;
159         while(m) {
160                 if (!strcasecmp(resource, m->resource))
161                         break;
162                 m = m->next;
163         }
164         pthread_mutex_unlock(&modlock);
165         if (m)
166                 return -1;
167         else
168                 return 0;
169 }
170
171 int load_modules()
172 {
173         struct ast_config *cfg;
174         struct ast_variable *v;
175         if (option_verbose) 
176                 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
177         cfg = ast_load(AST_MODULE_CONFIG);
178         if (cfg) {
179                 /* Load explicitly defined modules */
180                 v = ast_variable_browse(cfg, "modules");
181                 while(v) {
182                         if (!strcasecmp(v->name, "load")) {
183                                 if (option_debug && !option_verbose)
184                                         ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
185                                 if (option_verbose) {
186                                         ast_verbose( " [%s]", v->value);
187                                         fflush(stdout);
188                                 }
189                                 if (ast_load_resource(v->value)) {
190                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
191                                         if (cfg)
192                                                 ast_destroy(cfg);
193                                         return -1;
194                                 }
195                         }
196                         v=v->next;
197                 }
198         }
199         if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
200                 /* Load all modules */
201                 DIR *mods;
202                 struct dirent *d;
203                 mods = opendir(AST_MODULE_DIR);
204                 if (mods) {
205                         while((d = readdir(mods))) {
206                                 /* Must end in .so to load it.  */
207                                 if ((strlen(d->d_name) > 3) && 
208                                     !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
209                                         !ast_resource_exists(d->d_name)) {
210                                         /* It's a shared library -- Just be sure we're allowed to load it -- kinda
211                                            an inefficient way to do it, but oh well. */
212                                         if (cfg) {
213                                                 v = ast_variable_browse(cfg, "modules");
214                                                 while(v) {
215                                                         if (!strcasecmp(v->name, "noload") &&
216                                                             !strcasecmp(v->value, d->d_name)) 
217                                                                 break;
218                                                         v = v->next;
219                                                 }
220                                                 if (v) {
221                                                         if (option_verbose) {
222                                                                 ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
223                                                                 fflush(stdout);
224                                                         }
225                                                         continue;
226                                                 }
227                                                 
228                                         }
229                                     if (option_debug && !option_verbose)
230                                                 ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
231                                         if (option_verbose) {
232                                                 ast_verbose( VERBOSE_PREFIX_1 "[%s]", d->d_name);
233                                                 fflush(stdout);
234                                         }
235                                         if (ast_load_resource(d->d_name)) {
236                                                 ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
237                                                 if (cfg)
238                                                         ast_destroy(cfg);
239                                                 return -1;
240                                         }
241                                 }
242                         };
243                 } else {
244                         if (!option_quiet)
245                                 ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
246                 }
247         } 
248         ast_destroy(cfg);
249         return 0;
250 }
251
252 void ast_update_use_count(void)
253 {
254         /* Notify any module monitors that the use count for a 
255            resource has changed */
256         struct loadupdate *m;
257         if (pthread_mutex_lock(&modlock))
258                 ast_log(LOG_WARNING, "Failed to lock\n");
259         m = updaters;
260         while(m) {
261                 m->updater();
262                 m = m->next;
263         }
264         pthread_mutex_unlock(&modlock);
265         
266 }
267
268 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
269 {
270         struct module *m;
271         int unlock = -1;
272         if (pthread_mutex_trylock(&modlock))
273                 unlock = 0;
274         m = module_list;
275         while(m) {
276                 modentry(m->resource, m->description(), m->usecount());
277                 m = m->next;
278         }
279         if (unlock)
280                 pthread_mutex_unlock(&modlock);
281         return 0;
282 }
283
284 int ast_loader_register(int (*v)(void)) 
285 {
286         struct loadupdate *tmp;
287         /* XXX Should be more flexible here, taking > 1 verboser XXX */
288         if ((tmp = malloc(sizeof (struct loadupdate)))) {
289                 tmp->updater = v;
290                 if (pthread_mutex_lock(&modlock))
291                         ast_log(LOG_WARNING, "Failed to lock\n");
292                 tmp->next = updaters;
293                 updaters = tmp;
294                 pthread_mutex_unlock(&modlock);
295                 return 0;
296         }
297         return -1;
298 }
299
300 int ast_loader_unregister(int (*v)(void))
301 {
302         int res = -1;
303         struct loadupdate *tmp, *tmpl=NULL;
304         if (pthread_mutex_lock(&modlock))
305                 ast_log(LOG_WARNING, "Failed to lock\n");
306         tmp = updaters;
307         while(tmp) {
308                 if (tmp->updater == v)  {
309                         if (tmpl)
310                                 tmpl->next = tmp->next;
311                         else
312                                 updaters = tmp->next;
313                         break;
314                 }
315                 tmpl = tmp;
316                 tmp = tmp->next;
317         }
318         if (tmp)
319                 res = 0;
320         pthread_mutex_unlock(&modlock);
321         return res;
322 }