Version 0.1.10 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         ast_pthread_mutex_lock(&modlock);
139         m = module_list;
140         while(m) {
141                 if (m->reload) {
142                         if (option_verbose > 2) 
143                                 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
144                         m->reload();
145                 }
146                 m = m->next;
147         }
148         ast_pthread_mutex_unlock(&modlock);
149 }
150
151 int ast_load_resource(char *resource_name)
152 {
153         static char fn[256];
154         int errors=0;
155         int res;
156         struct module *m;
157         int flags=0;
158         char *val;
159         char *key;
160         int o;
161         struct ast_config *cfg;
162         /* Keep the module file parsing silent */
163         o = option_verbose;
164         if (strncasecmp(resource_name, "res_", 4)) {
165                 option_verbose = 0;
166                 cfg = ast_load(AST_MODULE_CONFIG);
167                 option_verbose = o;
168                 if (cfg) {
169                         if ((val = ast_variable_retrieve(cfg, "global", resource_name))
170                                         && ast_true(val))
171                                 flags |= RTLD_GLOBAL;
172                         ast_destroy(cfg);
173                 }
174         } else {
175                 /* Resource modules are always loaded global */
176                 flags |= RTLD_GLOBAL;
177         }
178         
179         if (ast_pthread_mutex_lock(&modlock))
180                 ast_log(LOG_WARNING, "Failed to lock\n");
181         m = module_list;
182         while(m) {
183                 if (!strcasecmp(m->resource, resource_name)) {
184                         ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
185                         ast_pthread_mutex_unlock(&modlock);
186                         return -1;
187                 }
188                 m = m->next;
189         }
190         m = malloc(sizeof(struct module));      
191         if (!m) {
192                 ast_log(LOG_WARNING, "Out of memory\n");
193                 ast_pthread_mutex_unlock(&modlock);
194                 return -1;
195         }
196         strncpy(m->resource, resource_name, sizeof(m->resource)-1);
197         if (resource_name[0] == '/') {
198                 strncpy(fn, resource_name, sizeof(fn)-1);
199         } else {
200                 snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
201         }
202         m->lib = dlopen(fn, RTLD_NOW | flags);
203         if (!m->lib) {
204                 ast_log(LOG_WARNING, "%s\n", dlerror());
205                 free(m);
206                 ast_pthread_mutex_unlock(&modlock);
207                 return -1;
208         }
209         m->load_module = dlsym(m->lib, "load_module");
210         if (!m->load_module) {
211                 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
212                 errors++;
213         }
214         m->unload_module = dlsym(m->lib, "unload_module");
215         if (!m->unload_module) {
216                 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
217                 errors++;
218         }
219         m->usecount = dlsym(m->lib, "usecount");
220         if (!m->usecount) {
221                 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
222                 errors++;
223         }
224         m->description = dlsym(m->lib, "description");
225         if (!m->description) {
226                 ast_log(LOG_WARNING, "No description in module %s\n", fn);
227                 errors++;
228         }
229         m->key = dlsym(m->lib, "key");
230         if (!m->key) {
231                 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
232                 errors++;
233         }
234         m->reload = dlsym(m->lib, "reload");
235         if (m->key && !(key = m->key())) {
236                 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
237                 errors++;
238         } else
239                 key = NULL;
240         if (key && verify_key(key)) {
241                 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
242                 errors++;
243         }
244         if (errors) {
245                 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
246                 dlclose(m->lib);
247                 free(m);
248                 ast_pthread_mutex_unlock(&modlock);
249                 return -1;
250         }
251         if (!fully_booted) {
252                 if (option_verbose) 
253                         ast_verbose( " => (%s)\n", m->description());
254                 if (option_console && !option_verbose)
255                         ast_verbose( ".");
256         } else {
257                 if (option_verbose)
258                         ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
259         }
260         m->next = module_list;
261         
262         module_list = m;
263         ast_pthread_mutex_unlock(&modlock);
264         if ((res = m->load_module())) {
265                 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
266                 ast_unload_resource(resource_name, 0);
267                 return -1;
268         }
269         ast_update_use_count();
270         return 0;
271 }       
272
273 static int ast_resource_exists(char *resource)
274 {
275         struct module *m;
276         if (ast_pthread_mutex_lock(&modlock))
277                 ast_log(LOG_WARNING, "Failed to lock\n");
278         m = module_list;
279         while(m) {
280                 if (!strcasecmp(resource, m->resource))
281                         break;
282                 m = m->next;
283         }
284         ast_pthread_mutex_unlock(&modlock);
285         if (m)
286                 return -1;
287         else
288                 return 0;
289 }
290
291 int load_modules()
292 {
293         struct ast_config *cfg;
294         struct ast_variable *v;
295         if (option_verbose) 
296                 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
297         cfg = ast_load(AST_MODULE_CONFIG);
298         if (cfg) {
299                 /* Load explicitly defined modules */
300                 v = ast_variable_browse(cfg, "modules");
301                 while(v) {
302                         if (!strcasecmp(v->name, "load")) {
303                                 if (option_debug && !option_verbose)
304                                         ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
305                                 if (option_verbose) {
306                                         ast_verbose( " [%s]", v->value);
307                                         fflush(stdout);
308                                 }
309                                 if (ast_load_resource(v->value)) {
310                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
311                                         if (cfg)
312                                                 ast_destroy(cfg);
313                                         return -1;
314                                 }
315                         }
316                         v=v->next;
317                 }
318         }
319         if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
320                 /* Load all modules */
321                 DIR *mods;
322                 struct dirent *d;
323                 int x;
324                 /* Make two passes.  First, load any resource modules, then load the others. */
325                 for (x=0;x<2;x++) {
326                         mods = opendir(AST_MODULE_DIR);
327                         if (mods) {
328                                 while((d = readdir(mods))) {
329                                         /* Must end in .so to load it.  */
330                                         if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) && 
331                                             !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
332                                                 !ast_resource_exists(d->d_name)) {
333                                                 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
334                                                    an inefficient way to do it, but oh well. */
335                                                 if (cfg) {
336                                                         v = ast_variable_browse(cfg, "modules");
337                                                         while(v) {
338                                                                 if (!strcasecmp(v->name, "noload") &&
339                                                                     !strcasecmp(v->value, d->d_name)) 
340                                                                         break;
341                                                                 v = v->next;
342                                                         }
343                                                         if (v) {
344                                                                 if (option_verbose) {
345                                                                         ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
346                                                                         fflush(stdout);
347                                                                 }
348                                                                 continue;
349                                                         }
350                                                         
351                                                 }
352                                             if (option_debug && !option_verbose)
353                                                         ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
354                                                 if (option_verbose) {
355                                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", d->d_name);
356                                                         fflush(stdout);
357                                                 }
358                                                 if (ast_load_resource(d->d_name)) {
359                                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
360                                                         if (cfg)
361                                                                 ast_destroy(cfg);
362                                                         return -1;
363                                                 }
364                                         }
365                                 }
366                                 closedir(mods);
367                         } else {
368                                 if (!option_quiet)
369                                         ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
370                         }
371                 }
372         } 
373         ast_destroy(cfg);
374         return 0;
375 }
376
377 void ast_update_use_count(void)
378 {
379         /* Notify any module monitors that the use count for a 
380            resource has changed */
381         struct loadupdate *m;
382         if (ast_pthread_mutex_lock(&modlock))
383                 ast_log(LOG_WARNING, "Failed to lock\n");
384         m = updaters;
385         while(m) {
386                 m->updater();
387                 m = m->next;
388         }
389         ast_pthread_mutex_unlock(&modlock);
390         
391 }
392
393 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
394 {
395         struct module *m;
396         int unlock = -1;
397         if (pthread_mutex_trylock(&modlock))
398                 unlock = 0;
399         m = module_list;
400         while(m) {
401                 modentry(m->resource, m->description(), m->usecount());
402                 m = m->next;
403         }
404         if (unlock)
405                 ast_pthread_mutex_unlock(&modlock);
406         return 0;
407 }
408
409 int ast_loader_register(int (*v)(void)) 
410 {
411         struct loadupdate *tmp;
412         /* XXX Should be more flexible here, taking > 1 verboser XXX */
413         if ((tmp = malloc(sizeof (struct loadupdate)))) {
414                 tmp->updater = v;
415                 if (ast_pthread_mutex_lock(&modlock))
416                         ast_log(LOG_WARNING, "Failed to lock\n");
417                 tmp->next = updaters;
418                 updaters = tmp;
419                 ast_pthread_mutex_unlock(&modlock);
420                 return 0;
421         }
422         return -1;
423 }
424
425 int ast_loader_unregister(int (*v)(void))
426 {
427         int res = -1;
428         struct loadupdate *tmp, *tmpl=NULL;
429         if (ast_pthread_mutex_lock(&modlock))
430                 ast_log(LOG_WARNING, "Failed to lock\n");
431         tmp = updaters;
432         while(tmp) {
433                 if (tmp->updater == v)  {
434                         if (tmpl)
435                                 tmpl->next = tmp->next;
436                         else
437                                 updaters = tmp->next;
438                         break;
439                 }
440                 tmpl = tmp;
441                 tmp = tmp->next;
442         }
443         if (tmp)
444                 res = 0;
445         ast_pthread_mutex_unlock(&modlock);
446         return res;
447 }