Version 0.3.0 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 <asterisk/channel.h>
24 #include <asterisk/term.h>
25 #include <asterisk/manager.h>
26 #include <dlfcn.h>
27 #include <asterisk/md5.h>
28 #define __USE_GNU
29 #include <pthread.h>
30 #include "asterisk.h"
31 #include "astconf.h"
32
33 static char expected_key[] =
34 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
35   0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
36
37 struct module {
38         int (*load_module)(void);
39         int (*unload_module)(void);
40         int (*usecount)(void);
41         char *(*description)(void);
42         char *(*key)(void);
43         int (*reload)(void);
44         void *lib;
45         char resource[256];
46         struct module *next;
47 };
48
49 static int printdigest(unsigned char *d)
50 {
51         int x;
52         char buf[256];
53         char buf2[16];
54         snprintf(buf, sizeof(buf), "Unexpected signature:");
55         for (x=0;x<16;x++) {
56                 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
57                 strcat(buf, buf2);
58         }
59         strcat(buf, "\n");
60         ast_log(LOG_DEBUG, buf);
61         return 0;
62 }
63
64 static int key_matches(char *key1, char *key2)
65 {
66         int match = 1;
67         int x;
68         for (x=0;x<16;x++) {
69                 match &= (key1[x] == key2[x]);
70         }
71         return match;
72 }
73
74 static int verify_key(char *key)
75 {
76         struct MD5Context c;
77         char digest[16];
78         MD5Init(&c);
79         MD5Update(&c, key, strlen(key));
80         MD5Final(digest, &c);
81         if (key_matches(expected_key, digest))
82                 return 0;
83         printdigest(digest);
84         return -1;
85 }
86
87 static struct loadupdate {
88         int (*updater)(void);
89         struct loadupdate *next;
90 } *updaters = NULL;
91
92 static pthread_mutex_t modlock = AST_MUTEX_INITIALIZER;
93
94 static struct module *module_list=NULL;
95
96 int ast_unload_resource(char *resource_name, int force)
97 {
98         struct module *m, *ml = NULL;
99         int res = -1;
100         if (ast_pthread_mutex_lock(&modlock))
101                 ast_log(LOG_WARNING, "Failed to lock\n");
102         m = module_list;
103         while(m) {
104                 if (!strcasecmp(m->resource, resource_name)) {
105                         if ((res = m->usecount()) > 0)  {
106                                 if (force) 
107                                         ast_log(LOG_WARNING, "Warning:  Forcing removal of module %s with use count %d\n", resource_name, res);
108                                 else {
109                                         ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
110                                         ast_pthread_mutex_unlock(&modlock);
111                                         return -1;
112                                 }
113                         }
114                         res = m->unload_module();
115                         if (res) {
116                                 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
117                                 if (force <= AST_FORCE_FIRM) {
118                                         ast_pthread_mutex_unlock(&modlock);
119                                         return -1;
120                                 } else
121                                         ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
122                         }
123                         if (ml)
124                                 ml->next = m->next;
125                         else
126                                 module_list = m->next;
127                         dlclose(m->lib);
128                         free(m);
129                 }
130                 ml = m;
131                 m = m->next;
132         }
133         ast_pthread_mutex_unlock(&modlock);
134         ast_update_use_count();
135         return res;
136 }
137
138 void ast_module_reload(void)
139 {
140         struct module *m;
141
142         /* We'll do the logger and manager the favor of calling its reload here first */
143         reload_manager();
144
145         ast_pthread_mutex_lock(&modlock);
146         m = module_list;
147         while(m) {
148                 if (m->reload) {
149                         if (option_verbose > 2) 
150                                 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
151                         m->reload();
152                 }
153                 m = m->next;
154         }
155         ast_pthread_mutex_unlock(&modlock);
156 }
157
158 int ast_load_resource(char *resource_name)
159 {
160         static char fn[256];
161         int errors=0;
162         int res;
163         struct module *m;
164         int flags=RTLD_NOW;
165         char *val;
166         char *key;
167         int o;
168         struct ast_config *cfg;
169         char tmp[80];
170         /* Keep the module file parsing silent */
171         o = option_verbose;
172         if (strncasecmp(resource_name, "res_", 4)) {
173                 option_verbose = 0;
174                 cfg = ast_load(AST_MODULE_CONFIG);
175                 option_verbose = o;
176                 if (cfg) {
177                         if ((val = ast_variable_retrieve(cfg, "global", resource_name))
178                                         && ast_true(val))
179                                 flags |= RTLD_GLOBAL;
180                         ast_destroy(cfg);
181                 }
182         } else {
183                 /* Resource modules are always loaded global and lazy */
184                 flags = (RTLD_GLOBAL | RTLD_LAZY);
185         }
186         
187         if (ast_pthread_mutex_lock(&modlock))
188                 ast_log(LOG_WARNING, "Failed to lock\n");
189         m = module_list;
190         while(m) {
191                 if (!strcasecmp(m->resource, resource_name)) {
192                         ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
193                         ast_pthread_mutex_unlock(&modlock);
194                         return -1;
195                 }
196                 m = m->next;
197         }
198         m = malloc(sizeof(struct module));      
199         if (!m) {
200                 ast_log(LOG_WARNING, "Out of memory\n");
201                 ast_pthread_mutex_unlock(&modlock);
202                 return -1;
203         }
204         strncpy(m->resource, resource_name, sizeof(m->resource)-1);
205         if (resource_name[0] == '/') {
206                 strncpy(fn, resource_name, sizeof(fn)-1);
207         } else {
208                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
209         }
210         m->lib = dlopen(fn, flags);
211         if (!m->lib) {
212                 ast_log(LOG_WARNING, "%s\n", dlerror());
213                 free(m);
214                 ast_pthread_mutex_unlock(&modlock);
215                 return -1;
216         }
217         m->load_module = dlsym(m->lib, "load_module");
218         if (!m->load_module) {
219                 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
220                 errors++;
221         }
222         m->unload_module = dlsym(m->lib, "unload_module");
223         if (!m->unload_module) {
224                 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
225                 errors++;
226         }
227         m->usecount = dlsym(m->lib, "usecount");
228         if (!m->usecount) {
229                 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
230                 errors++;
231         }
232         m->description = dlsym(m->lib, "description");
233         if (!m->description) {
234                 ast_log(LOG_WARNING, "No description in module %s\n", fn);
235                 errors++;
236         }
237         m->key = dlsym(m->lib, "key");
238         if (!m->key) {
239                 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
240                 errors++;
241         }
242         m->reload = dlsym(m->lib, "reload");
243         if (m->key && !(key = m->key())) {
244                 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
245                 errors++;
246         } else
247                 key = NULL;
248         if (key && verify_key(key)) {
249                 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
250                 errors++;
251         }
252         if (errors) {
253                 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
254                 dlclose(m->lib);
255                 free(m);
256                 ast_pthread_mutex_unlock(&modlock);
257                 return -1;
258         }
259         if (!fully_booted) {
260                 if (option_verbose) 
261                         ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
262                 if (option_console && !option_verbose)
263                         ast_verbose( ".");
264         } else {
265                 if (option_verbose)
266                         ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
267         }
268         m->next = module_list;
269         
270         module_list = m;
271         ast_pthread_mutex_unlock(&modlock);
272         if ((res = m->load_module())) {
273                 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
274                 ast_unload_resource(resource_name, 0);
275                 return -1;
276         }
277         ast_update_use_count();
278         return 0;
279 }       
280
281 static int ast_resource_exists(char *resource)
282 {
283         struct module *m;
284         if (ast_pthread_mutex_lock(&modlock))
285                 ast_log(LOG_WARNING, "Failed to lock\n");
286         m = module_list;
287         while(m) {
288                 if (!strcasecmp(resource, m->resource))
289                         break;
290                 m = m->next;
291         }
292         ast_pthread_mutex_unlock(&modlock);
293         if (m)
294                 return -1;
295         else
296                 return 0;
297 }
298
299 int load_modules()
300 {
301         struct ast_config *cfg;
302         struct ast_variable *v;
303         char tmp[80];
304         if (option_verbose) 
305                 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
306         cfg = ast_load(AST_MODULE_CONFIG);
307         if (cfg) {
308                 /* Load explicitly defined modules */
309                 v = ast_variable_browse(cfg, "modules");
310                 while(v) {
311                         if (!strcasecmp(v->name, "load")) {
312                                 if (option_debug && !option_verbose)
313                                         ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
314                                 if (option_verbose) {
315                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
316                                         fflush(stdout);
317                                 }
318                                 if (ast_load_resource(v->value)) {
319                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
320                                         if (cfg)
321                                                 ast_destroy(cfg);
322                                         return -1;
323                                 }
324                         }
325                         v=v->next;
326                 }
327         }
328         if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
329                 /* Load all modules */
330                 DIR *mods;
331                 struct dirent *d;
332                 int x;
333                 /* Make two passes.  First, load any resource modules, then load the others. */
334                 for (x=0;x<2;x++) {
335                         mods = opendir((char *)ast_config_AST_MODULE_DIR);
336                         if (mods) {
337                                 while((d = readdir(mods))) {
338                                         /* Must end in .so to load it.  */
339                                         if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) && 
340                                             !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
341                                                 !ast_resource_exists(d->d_name)) {
342                                                 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
343                                                    an inefficient way to do it, but oh well. */
344                                                 if (cfg) {
345                                                         v = ast_variable_browse(cfg, "modules");
346                                                         while(v) {
347                                                                 if (!strcasecmp(v->name, "noload") &&
348                                                                     !strcasecmp(v->value, d->d_name)) 
349                                                                         break;
350                                                                 v = v->next;
351                                                         }
352                                                         if (v) {
353                                                                 if (option_verbose) {
354                                                                         ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
355                                                                         fflush(stdout);
356                                                                 }
357                                                                 continue;
358                                                         }
359                                                         
360                                                 }
361                                             if (option_debug && !option_verbose)
362                                                         ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
363                                                 if (option_verbose) {
364                                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
365                                                         fflush(stdout);
366                                                 }
367                                                 if (ast_load_resource(d->d_name)) {
368                                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
369                                                         if (cfg)
370                                                                 ast_destroy(cfg);
371                                                         return -1;
372                                                 }
373                                         }
374                                 }
375                                 closedir(mods);
376                         } else {
377                                 if (!option_quiet)
378                                         ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
379                         }
380                 }
381         } 
382         ast_destroy(cfg);
383         return 0;
384 }
385
386 void ast_update_use_count(void)
387 {
388         /* Notify any module monitors that the use count for a 
389            resource has changed */
390         struct loadupdate *m;
391         if (ast_pthread_mutex_lock(&modlock))
392                 ast_log(LOG_WARNING, "Failed to lock\n");
393         m = updaters;
394         while(m) {
395                 m->updater();
396                 m = m->next;
397         }
398         ast_pthread_mutex_unlock(&modlock);
399         
400 }
401
402 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
403 {
404         struct module *m;
405         int unlock = -1;
406         if (pthread_mutex_trylock(&modlock))
407                 unlock = 0;
408         m = module_list;
409         while(m) {
410                 modentry(m->resource, m->description(), m->usecount());
411                 m = m->next;
412         }
413         if (unlock)
414                 ast_pthread_mutex_unlock(&modlock);
415         return 0;
416 }
417
418 int ast_loader_register(int (*v)(void)) 
419 {
420         struct loadupdate *tmp;
421         /* XXX Should be more flexible here, taking > 1 verboser XXX */
422         if ((tmp = malloc(sizeof (struct loadupdate)))) {
423                 tmp->updater = v;
424                 if (ast_pthread_mutex_lock(&modlock))
425                         ast_log(LOG_WARNING, "Failed to lock\n");
426                 tmp->next = updaters;
427                 updaters = tmp;
428                 ast_pthread_mutex_unlock(&modlock);
429                 return 0;
430         }
431         return -1;
432 }
433
434 int ast_loader_unregister(int (*v)(void))
435 {
436         int res = -1;
437         struct loadupdate *tmp, *tmpl=NULL;
438         if (ast_pthread_mutex_lock(&modlock))
439                 ast_log(LOG_WARNING, "Failed to lock\n");
440         tmp = updaters;
441         while(tmp) {
442                 if (tmp->updater == v)  {
443                         if (tmpl)
444                                 tmpl->next = tmp->next;
445                         else
446                                 updaters = tmp->next;
447                         break;
448                 }
449                 tmpl = tmp;
450                 tmp = tmp->next;
451         }
452         if (tmp)
453                 res = 0;
454         ast_pthread_mutex_unlock(&modlock);
455         return res;
456 }