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