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