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