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