Add Q.SIG option to zapata.conf
[asterisk/asterisk.git] / loader.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Module Loader
5  * 
6  * Copyright (C) 1999 - 2005, 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
20 #include "asterisk/module.h"
21 #include "asterisk/options.h"
22 #include "asterisk/config.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, "%s", 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 static int modlistver = 0;
107
108 int ast_unload_resource(const char *resource_name, int force)
109 {
110         struct module *m, *ml = NULL;
111         int res = -1;
112         if (ast_mutex_lock(&modlock))
113                 ast_log(LOG_WARNING, "Failed to lock\n");
114         m = module_list;
115         while(m) {
116                 if (!strcasecmp(m->resource, resource_name)) {
117                         if ((res = m->usecount()) > 0)  {
118                                 if (force) 
119                                         ast_log(LOG_WARNING, "Warning:  Forcing removal of module %s with use count %d\n", resource_name, res);
120                                 else {
121                                         ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
122                                         ast_mutex_unlock(&modlock);
123                                         return -1;
124                                 }
125                         }
126                         res = m->unload_module();
127                         if (res) {
128                                 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
129                                 if (force <= AST_FORCE_FIRM) {
130                                         ast_mutex_unlock(&modlock);
131                                         return -1;
132                                 } else
133                                         ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
134                         }
135                         if (ml)
136                                 ml->next = m->next;
137                         else
138                                 module_list = m->next;
139                         dlclose(m->lib);
140                         free(m);
141                         break;
142                 }
143                 ml = m;
144                 m = m->next;
145         }
146         modlistver = rand();
147         ast_mutex_unlock(&modlock);
148         ast_update_use_count();
149         return res;
150 }
151
152 char *ast_module_helper(char *line, char *word, int pos, int state, int rpos, int needsreload)
153 {
154         struct module *m;
155         int which=0;
156         char *ret;
157         if (pos != rpos)
158                 return NULL;
159         ast_mutex_lock(&modlock);
160         m = module_list;
161         while(m) {
162                 if (!strncasecmp(word, m->resource, strlen(word)) && (m->reload || !needsreload)) {
163                         if (++which > state)
164                                 break;
165                 }
166                 m = m->next;
167         }
168         if (m) {
169                 ret = strdup(m->resource);
170         } else {
171                 ret = NULL;
172                 if (!strncasecmp(word, "extconfig", strlen(word))) {
173                         if (++which > state)
174                                 ret = strdup("extconfig");
175                 } else if (!strncasecmp(word, "manager", strlen(word))) {
176                         if (++which > state)
177                                 ret = strdup("manager");
178                 } else if (!strncasecmp(word, "enum", strlen(word))) {
179                         if (++which > state)
180                                 ret = strdup("enum");
181                 } else if (!strncasecmp(word, "rtp", strlen(word))) {
182                         if (++which > state)
183                                 ret = strdup("rtp");
184                 }
185                         
186         }
187         ast_mutex_unlock(&modlock);
188         return ret;
189 }
190
191 int ast_module_reload(const char *name)
192 {
193         struct module *m;
194         int reloaded = 0;
195         int oldversion;
196         int (*reload)(void);
197         /* We'll do the logger and manager the favor of calling its reload here first */
198
199         if (ast_mutex_trylock(&reloadlock)) {
200                 ast_verbose("The previous reload command didn't finish yet\n");
201                 return -1;
202         }
203         if (!name || !strcasecmp(name, "extconfig")) {
204                 read_config_maps();
205                 reloaded = 2;
206         }
207         if (!name || !strcasecmp(name, "manager")) {
208                 reload_manager();
209                 reloaded = 2;
210         }
211         if (!name || !strcasecmp(name, "enum")) {
212                 ast_enum_reload();
213                 reloaded = 2;
214         }
215         if (!name || !strcasecmp(name, "rtp")) {
216                 ast_rtp_reload();
217                 reloaded = 2;
218         }
219         if (!name || !strcasecmp(name, "dnsmgr")) {
220                 dnsmgr_reload();
221                 reloaded = 2;
222         }
223         time(&ast_lastreloadtime);
224
225         ast_mutex_lock(&modlock);
226         oldversion = modlistver;
227         m = module_list;
228         while(m) {
229                 if (!name || !strcasecmp(name, m->resource)) {
230                         if (reloaded < 1)
231                                 reloaded = 1;
232                         reload = m->reload;
233                         ast_mutex_unlock(&modlock);
234                         if (reload) {
235                                 reloaded = 2;
236                                 if (option_verbose > 2) 
237                                         ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
238                                 reload();
239                         }
240                         ast_mutex_lock(&modlock);
241                         if (oldversion != modlistver)
242                                 break;
243                 }
244                 m = m->next;
245         }
246         ast_mutex_unlock(&modlock);
247         ast_mutex_unlock(&reloadlock);
248         return reloaded;
249 }
250
251 static int __load_resource(const char *resource_name, const struct ast_config *cfg)
252 {
253         static char fn[256];
254         int errors=0;
255         int res;
256         struct module *m;
257         int flags=RTLD_NOW;
258 #ifdef RTLD_GLOBAL
259         char *val;
260 #endif
261         char *key;
262         char tmp[80];
263
264         if (strncasecmp(resource_name, "res_", 4)) {
265 #ifdef RTLD_GLOBAL
266                 if (cfg) {
267                         if ((val = ast_variable_retrieve(cfg, "global", resource_name))
268                                         && ast_true(val))
269                                 flags |= RTLD_GLOBAL;
270                 }
271 #endif
272         } else {
273                 /* Resource modules are always loaded global and lazy */
274 #ifdef RTLD_GLOBAL
275                 flags = (RTLD_GLOBAL | RTLD_LAZY);
276 #else
277                 flags = RTLD_LAZY;
278 #endif
279         }
280         
281         if (ast_mutex_lock(&modlock))
282                 ast_log(LOG_WARNING, "Failed to lock\n");
283         m = module_list;
284         while(m) {
285                 if (!strcasecmp(m->resource, resource_name)) {
286                         ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
287                         ast_mutex_unlock(&modlock);
288                         return -1;
289                 }
290                 m = m->next;
291         }
292         m = malloc(sizeof(struct module));      
293         if (!m) {
294                 ast_log(LOG_WARNING, "Out of memory\n");
295                 ast_mutex_unlock(&modlock);
296                 return -1;
297         }
298         strncpy(m->resource, resource_name, sizeof(m->resource)-1);
299         if (resource_name[0] == '/') {
300                 strncpy(fn, resource_name, sizeof(fn)-1);
301         } else {
302                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
303         }
304         m->lib = dlopen(fn, flags);
305         if (!m->lib) {
306                 ast_log(LOG_WARNING, "%s\n", dlerror());
307                 free(m);
308                 ast_mutex_unlock(&modlock);
309                 return -1;
310         }
311         m->load_module = dlsym(m->lib, "load_module");
312         if (m->load_module == NULL)
313                 m->load_module = dlsym(m->lib, "_load_module");
314         if (!m->load_module) {
315                 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
316                 errors++;
317         }
318         m->unload_module = dlsym(m->lib, "unload_module");
319         if (m->unload_module == NULL)
320                 m->unload_module = dlsym(m->lib, "_unload_module");
321         if (!m->unload_module) {
322                 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
323                 errors++;
324         }
325         m->usecount = dlsym(m->lib, "usecount");
326         if (m->usecount == NULL)
327                 m->usecount = dlsym(m->lib, "_usecount");
328         if (!m->usecount) {
329                 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
330                 errors++;
331         }
332         m->description = dlsym(m->lib, "description");
333         if (m->description == NULL)
334                 m->description = dlsym(m->lib, "_description");
335         if (!m->description) {
336                 ast_log(LOG_WARNING, "No description in module %s\n", fn);
337                 errors++;
338         }
339         m->key = dlsym(m->lib, "key");
340         if (m->key == NULL)
341                 m->key = dlsym(m->lib, "_key");
342         if (!m->key) {
343                 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
344                 errors++;
345         }
346         m->reload = dlsym(m->lib, "reload");
347         if (m->reload == NULL)
348                 m->reload = dlsym(m->lib, "_reload");
349         if (!m->key || !(key = m->key())) {
350                 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
351                 key = NULL;
352                 errors++;
353         }
354         if (key && verify_key(key)) {
355                 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
356                 errors++;
357         }
358         if (errors) {
359                 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
360                 dlclose(m->lib);
361                 free(m);
362                 ast_mutex_unlock(&modlock);
363                 return -1;
364         }
365         if (!fully_booted) {
366                 if (option_verbose) 
367                         ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
368                 if (option_console && !option_verbose)
369                         ast_verbose( ".");
370         } else {
371                 if (option_verbose)
372                         ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
373         }
374
375         /* add module 'm' to end of module_list chain
376            so reload commands will be issued in same order modules were loaded */
377         m->next = NULL;
378         if (module_list == NULL) {
379                 /* empty list so far, add at front */
380                 module_list = m;
381         }
382         else {
383                 struct module *i;
384                 /* find end of chain, and add there */
385                 for (i = module_list; i->next; i = i->next)
386                         ;
387                 i->next = m;
388         }
389         
390         modlistver = rand();
391         ast_mutex_unlock(&modlock);
392         if ((res = m->load_module())) {
393                 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
394                 ast_unload_resource(resource_name, 0);
395                 return -1;
396         }
397         ast_update_use_count();
398         return 0;
399 }
400
401 int ast_load_resource(const char *resource_name)
402 {
403         int o;
404         struct ast_config *cfg = NULL;
405         int res;
406
407         /* Keep the module file parsing silent */
408         o = option_verbose;
409         option_verbose = 0;
410         cfg = ast_config_load(AST_MODULE_CONFIG);
411         option_verbose = o;
412         res = __load_resource(resource_name, cfg);
413         if (cfg)
414                 ast_config_destroy(cfg);
415         return res;
416 }       
417
418 static int ast_resource_exists(char *resource)
419 {
420         struct module *m;
421         if (ast_mutex_lock(&modlock))
422                 ast_log(LOG_WARNING, "Failed to lock\n");
423         m = module_list;
424         while(m) {
425                 if (!strcasecmp(resource, m->resource))
426                         break;
427                 m = m->next;
428         }
429         ast_mutex_unlock(&modlock);
430         if (m)
431                 return -1;
432         else
433                 return 0;
434 }
435
436 static const char *loadorder[] =
437 {
438         "res_",
439         "chan_",
440         "pbx_",
441         NULL,
442 };
443
444 int load_modules()
445 {
446         struct ast_config *cfg;
447         struct ast_variable *v;
448         char tmp[80];
449         if (option_verbose) 
450                 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
451         cfg = ast_config_load(AST_MODULE_CONFIG);
452         if (cfg) {
453                 /* Load explicitly defined modules */
454                 v = ast_variable_browse(cfg, "modules");
455                 while(v) {
456                         if (!strcasecmp(v->name, "load")) {
457                                 if (option_debug && !option_verbose)
458                                         ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
459                                 if (option_verbose) {
460                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
461                                         fflush(stdout);
462                                 }
463                                 if (__load_resource(v->value, cfg)) {
464                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
465                                         if (cfg)
466                                                 ast_config_destroy(cfg);
467                                         return -1;
468                                 }
469                         }
470                         v=v->next;
471                 }
472         }
473         if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
474                 /* Load all modules */
475                 DIR *mods;
476                 struct dirent *d;
477                 int x;
478                 /* Loop through each order */
479                 for (x=0;x<sizeof(loadorder) / sizeof(loadorder[0]);x++) {
480                         mods = opendir((char *)ast_config_AST_MODULE_DIR);
481                         if (mods) {
482                                 while((d = readdir(mods))) {
483                                         /* Must end in .so to load it.  */
484                                         if ((strlen(d->d_name) > 3) && 
485                                             (!loadorder[x] || !strncasecmp(d->d_name, loadorder[x], strlen(loadorder[x]))) && 
486                                             !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
487                                                 !ast_resource_exists(d->d_name)) {
488                                                 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
489                                                    an inefficient way to do it, but oh well. */
490                                                 if (cfg) {
491                                                         v = ast_variable_browse(cfg, "modules");
492                                                         while(v) {
493                                                                 if (!strcasecmp(v->name, "noload") &&
494                                                                     !strcasecmp(v->value, d->d_name)) 
495                                                                         break;
496                                                                 v = v->next;
497                                                         }
498                                                         if (v) {
499                                                                 if (option_verbose) {
500                                                                         ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
501                                                                         fflush(stdout);
502                                                                 }
503                                                                 continue;
504                                                         }
505                                                         
506                                                 }
507                                             if (option_debug && !option_verbose)
508                                                         ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
509                                                 if (option_verbose) {
510                                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
511                                                         fflush(stdout);
512                                                 }
513                                                 if (__load_resource(d->d_name, cfg)) {
514                                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
515                                                         if (cfg)
516                                                                 ast_config_destroy(cfg);
517                                                         return -1;
518                                                 }
519                                         }
520                                 }
521                                 closedir(mods);
522                         } else {
523                                 if (!option_quiet)
524                                         ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
525                         }
526                 }
527         } 
528         ast_config_destroy(cfg);
529         return 0;
530 }
531
532 void ast_update_use_count(void)
533 {
534         /* Notify any module monitors that the use count for a 
535            resource has changed */
536         struct loadupdate *m;
537         if (ast_mutex_lock(&modlock))
538                 ast_log(LOG_WARNING, "Failed to lock\n");
539         m = updaters;
540         while(m) {
541                 m->updater();
542                 m = m->next;
543         }
544         ast_mutex_unlock(&modlock);
545         
546 }
547
548 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt, char *like), char *like)
549 {
550         struct module *m;
551         int unlock = -1;
552         int total_mod_loaded = 0;
553         if (ast_mutex_trylock(&modlock))
554                 unlock = 0;
555         m = module_list;
556         while(m) {
557                 total_mod_loaded += modentry(m->resource, m->description(), m->usecount(), like);
558                 m = m->next;
559         }
560         if (unlock)
561                 ast_mutex_unlock(&modlock);
562         return total_mod_loaded;
563 }
564
565 int ast_loader_register(int (*v)(void)) 
566 {
567         struct loadupdate *tmp;
568         /* XXX Should be more flexible here, taking > 1 verboser XXX */
569         if ((tmp = malloc(sizeof (struct loadupdate)))) {
570                 tmp->updater = v;
571                 if (ast_mutex_lock(&modlock))
572                         ast_log(LOG_WARNING, "Failed to lock\n");
573                 tmp->next = updaters;
574                 updaters = tmp;
575                 ast_mutex_unlock(&modlock);
576                 return 0;
577         }
578         return -1;
579 }
580
581 int ast_loader_unregister(int (*v)(void))
582 {
583         int res = -1;
584         struct loadupdate *tmp, *tmpl=NULL;
585         if (ast_mutex_lock(&modlock))
586                 ast_log(LOG_WARNING, "Failed to lock\n");
587         tmp = updaters;
588         while(tmp) {
589                 if (tmp->updater == v)  {
590                         if (tmpl)
591                                 tmpl->next = tmp->next;
592                         else
593                                 updaters = tmp->next;
594                         break;
595                 }
596                 tmpl = tmp;
597                 tmp = tmp->next;
598         }
599         if (tmp)
600                 res = 0;
601         ast_mutex_unlock(&modlock);
602         return res;
603 }