Update chanelog
[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         if (!name || !strcasecmp(name, "astconfig"))
161                 read_ast_cust_config();
162         if (!name || !strcasecmp(name, "manager"))
163                 reload_manager();
164         if (!name || !strcasecmp(name, "enum"))
165                 ast_enum_reload();
166         if (!name || !strcasecmp(name, "rtp"))
167                 ast_rtp_reload();
168         time(&ast_lastreloadtime);
169
170         ast_mutex_lock(&modlock);
171         m = module_list;
172         while(m) {
173                 if (!name || !strcasecmp(name, m->resource)) {
174                         if (m->reload) {
175                                 if (option_verbose > 2) 
176                                         ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
177                                 m->reload();
178                         }
179                 }
180                 m = m->next;
181         }
182         ast_mutex_unlock(&modlock);
183         ast_mutex_unlock(&reloadlock);
184 }
185
186 int ast_load_resource(char *resource_name)
187 {
188         static char fn[256];
189         int errors=0;
190         int res;
191         struct module *m;
192         int flags=RTLD_NOW;
193 #ifdef RTLD_GLOBAL
194         char *val;
195 #endif
196         char *key;
197         int o;
198         struct ast_config *cfg;
199         char tmp[80];
200         /* Keep the module file parsing silent */
201         o = option_verbose;
202         if (strncasecmp(resource_name, "res_", 4)) {
203                 option_verbose = 0;
204                 cfg = ast_load(AST_MODULE_CONFIG);
205                 option_verbose = o;
206                 if (cfg) {
207 #ifdef RTLD_GLOBAL
208                         if ((val = ast_variable_retrieve(cfg, "global", resource_name))
209                                         && ast_true(val))
210                                 flags |= RTLD_GLOBAL;
211 #endif
212                         ast_destroy(cfg);
213                 }
214         } else {
215                 /* Resource modules are always loaded global and lazy */
216 #ifdef RTLD_GLOBAL
217                 flags = (RTLD_GLOBAL | RTLD_LAZY);
218 #else
219                 flags = RTLD_LAZY;
220 #endif
221         }
222         
223         if (ast_mutex_lock(&modlock))
224                 ast_log(LOG_WARNING, "Failed to lock\n");
225         m = module_list;
226         while(m) {
227                 if (!strcasecmp(m->resource, resource_name)) {
228                         ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
229                         ast_mutex_unlock(&modlock);
230                         return -1;
231                 }
232                 m = m->next;
233         }
234         m = malloc(sizeof(struct module));      
235         if (!m) {
236                 ast_log(LOG_WARNING, "Out of memory\n");
237                 ast_mutex_unlock(&modlock);
238                 return -1;
239         }
240         strncpy(m->resource, resource_name, sizeof(m->resource)-1);
241         if (resource_name[0] == '/') {
242                 strncpy(fn, resource_name, sizeof(fn)-1);
243         } else {
244                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
245         }
246         m->lib = dlopen(fn, flags);
247         if (!m->lib) {
248                 ast_log(LOG_WARNING, "%s\n", dlerror());
249                 free(m);
250                 ast_mutex_unlock(&modlock);
251                 return -1;
252         }
253         m->load_module = dlsym(m->lib, "load_module");
254         if (m->load_module == NULL)
255                 m->load_module = dlsym(m->lib, "_load_module");
256         if (!m->load_module) {
257                 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
258                 errors++;
259         }
260         m->unload_module = dlsym(m->lib, "unload_module");
261         if (m->unload_module == NULL)
262                 m->unload_module = dlsym(m->lib, "_unload_module");
263         if (!m->unload_module) {
264                 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
265                 errors++;
266         }
267         m->usecount = dlsym(m->lib, "usecount");
268         if (m->usecount == NULL)
269                 m->usecount = dlsym(m->lib, "_usecount");
270         if (!m->usecount) {
271                 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
272                 errors++;
273         }
274         m->description = dlsym(m->lib, "description");
275         if (m->description == NULL)
276                 m->description = dlsym(m->lib, "_description");
277         if (!m->description) {
278                 ast_log(LOG_WARNING, "No description in module %s\n", fn);
279                 errors++;
280         }
281         m->key = dlsym(m->lib, "key");
282         if (m->key == NULL)
283                 m->key = dlsym(m->lib, "_key");
284         if (!m->key) {
285                 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
286                 errors++;
287         }
288         m->reload = dlsym(m->lib, "reload");
289         if (m->reload == NULL)
290                 m->reload = dlsym(m->lib, "_reload");
291         if (!m->key || !(key = m->key())) {
292                 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
293                 key = NULL;
294                 errors++;
295         }
296         if (key && verify_key(key)) {
297                 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
298                 errors++;
299         }
300         if (errors) {
301                 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
302                 dlclose(m->lib);
303                 free(m);
304                 ast_mutex_unlock(&modlock);
305                 return -1;
306         }
307         if (!fully_booted) {
308                 if (option_verbose) 
309                         ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
310                 if (option_console && !option_verbose)
311                         ast_verbose( ".");
312         } else {
313                 if (option_verbose)
314                         ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
315         }
316
317         // add module 'm' to end of module_list chain
318         // so reload commands will be issued in same order modules were loaded
319         m->next = NULL;
320         if (module_list == NULL) {
321                 // empty list so far, add at front
322                 module_list = m;
323         }
324         else {
325                 struct module *i;
326                 // find end of chain, and add there
327                 for (i = module_list; i->next; i = i->next)
328                         ;
329                 i->next = m;
330         }
331         
332         ast_mutex_unlock(&modlock);
333         if ((res = m->load_module())) {
334                 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
335                 ast_unload_resource(resource_name, 0);
336                 return -1;
337         }
338         ast_update_use_count();
339         return 0;
340 }       
341
342 static int ast_resource_exists(char *resource)
343 {
344         struct module *m;
345         if (ast_mutex_lock(&modlock))
346                 ast_log(LOG_WARNING, "Failed to lock\n");
347         m = module_list;
348         while(m) {
349                 if (!strcasecmp(resource, m->resource))
350                         break;
351                 m = m->next;
352         }
353         ast_mutex_unlock(&modlock);
354         if (m)
355                 return -1;
356         else
357                 return 0;
358 }
359
360 int load_modules()
361 {
362         struct ast_config *cfg;
363         struct ast_variable *v;
364         char tmp[80];
365         if (option_verbose) 
366                 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
367         cfg = ast_load(AST_MODULE_CONFIG);
368         if (cfg) {
369                 /* Load explicitly defined modules */
370                 v = ast_variable_browse(cfg, "modules");
371                 while(v) {
372                         if (!strcasecmp(v->name, "load")) {
373                                 if (option_debug && !option_verbose)
374                                         ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
375                                 if (option_verbose) {
376                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
377                                         fflush(stdout);
378                                 }
379                                 if (ast_load_resource(v->value)) {
380                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
381                                         if (cfg)
382                                                 ast_destroy(cfg);
383                                         return -1;
384                                 }
385                         }
386                         v=v->next;
387                 }
388         }
389         if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
390                 /* Load all modules */
391                 DIR *mods;
392                 struct dirent *d;
393                 int x;
394                 /* Make two passes.  First, load any resource modules, then load the others. */
395                 for (x=0;x<2;x++) {
396                         mods = opendir((char *)ast_config_AST_MODULE_DIR);
397                         if (mods) {
398                                 while((d = readdir(mods))) {
399                                         /* Must end in .so to load it.  */
400                                         if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) && 
401                                             !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
402                                                 !ast_resource_exists(d->d_name)) {
403                                                 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
404                                                    an inefficient way to do it, but oh well. */
405                                                 if (cfg) {
406                                                         v = ast_variable_browse(cfg, "modules");
407                                                         while(v) {
408                                                                 if (!strcasecmp(v->name, "noload") &&
409                                                                     !strcasecmp(v->value, d->d_name)) 
410                                                                         break;
411                                                                 v = v->next;
412                                                         }
413                                                         if (v) {
414                                                                 if (option_verbose) {
415                                                                         ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
416                                                                         fflush(stdout);
417                                                                 }
418                                                                 continue;
419                                                         }
420                                                         
421                                                 }
422                                             if (option_debug && !option_verbose)
423                                                         ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
424                                                 if (option_verbose) {
425                                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
426                                                         fflush(stdout);
427                                                 }
428                                                 if (ast_load_resource(d->d_name)) {
429                                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
430                                                         if (cfg)
431                                                                 ast_destroy(cfg);
432                                                         return -1;
433                                                 }
434                                         }
435                                 }
436                                 closedir(mods);
437                         } else {
438                                 if (!option_quiet)
439                                         ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
440                         }
441                 }
442         } 
443         ast_destroy(cfg);
444         return 0;
445 }
446
447 void ast_update_use_count(void)
448 {
449         /* Notify any module monitors that the use count for a 
450            resource has changed */
451         struct loadupdate *m;
452         if (ast_mutex_lock(&modlock))
453                 ast_log(LOG_WARNING, "Failed to lock\n");
454         m = updaters;
455         while(m) {
456                 m->updater();
457                 m = m->next;
458         }
459         ast_mutex_unlock(&modlock);
460         
461 }
462
463 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
464 {
465         struct module *m;
466         int unlock = -1;
467         if (ast_mutex_trylock(&modlock))
468                 unlock = 0;
469         m = module_list;
470         while(m) {
471                 modentry(m->resource, m->description(), m->usecount());
472                 m = m->next;
473         }
474         if (unlock)
475                 ast_mutex_unlock(&modlock);
476         return 0;
477 }
478
479 int ast_loader_register(int (*v)(void)) 
480 {
481         struct loadupdate *tmp;
482         /* XXX Should be more flexible here, taking > 1 verboser XXX */
483         if ((tmp = malloc(sizeof (struct loadupdate)))) {
484                 tmp->updater = v;
485                 if (ast_mutex_lock(&modlock))
486                         ast_log(LOG_WARNING, "Failed to lock\n");
487                 tmp->next = updaters;
488                 updaters = tmp;
489                 ast_mutex_unlock(&modlock);
490                 return 0;
491         }
492         return -1;
493 }
494
495 int ast_loader_unregister(int (*v)(void))
496 {
497         int res = -1;
498         struct loadupdate *tmp, *tmpl=NULL;
499         if (ast_mutex_lock(&modlock))
500                 ast_log(LOG_WARNING, "Failed to lock\n");
501         tmp = updaters;
502         while(tmp) {
503                 if (tmp->updater == v)  {
504                         if (tmpl)
505                                 tmpl->next = tmp->next;
506                         else
507                                 updaters = tmp->next;
508                         break;
509                 }
510                 tmpl = tmp;
511                 tmp = tmp->next;
512         }
513         if (tmp)
514                 res = 0;
515         ast_mutex_unlock(&modlock);
516         return res;
517 }