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