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