Add The Status Of A Module To The Output Of "CLI> module show"
[asterisk/asterisk.git] / main / loader.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  * Kevin P. Fleming <kpfleming@digium.com>
8  * Luigi Rizzo <rizzo@icir.org>
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief Module Loader
24  * \author Mark Spencer <markster@digium.com>
25  * \author Kevin P. Fleming <kpfleming@digium.com>
26  * \author Luigi Rizzo <rizzo@icir.org>
27  * - See ModMngMnt
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/_private.h"
39 #include "asterisk/paths.h"     /* use ast_config_AST_MODULE_DIR */
40 #include <dirent.h>
41
42 #include "asterisk/linkedlists.h"
43 #include "asterisk/module.h"
44 #include "asterisk/config.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/term.h"
47 #include "asterisk/acl.h"
48 #include "asterisk/manager.h"
49 #include "asterisk/cdr.h"
50 #include "asterisk/enum.h"
51 #include "asterisk/http.h"
52 #include "asterisk/lock.h"
53 #include "asterisk/features.h"
54 #include "asterisk/dsp.h"
55 #include "asterisk/udptl.h"
56 #include "asterisk/heap.h"
57 #include "asterisk/app.h"
58 #include "asterisk/test.h"
59
60 #include <dlfcn.h>
61
62 #include "asterisk/md5.h"
63 #include "asterisk/utils.h"
64
65 /*** DOCUMENTATION
66  ***/
67
68 #ifndef RTLD_NOW
69 #define RTLD_NOW 0
70 #endif
71
72 #ifndef RTLD_LOCAL
73 #define RTLD_LOCAL 0
74 #endif
75
76 struct ast_module_user {
77         struct ast_channel *chan;
78         AST_LIST_ENTRY(ast_module_user) entry;
79 };
80
81 AST_LIST_HEAD(module_user_list, ast_module_user);
82
83 static const unsigned char expected_key[] =
84 { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
85   0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
86
87 static char buildopt_sum[33] = AST_BUILDOPT_SUM;
88
89 static unsigned int embedding = 1; /* we always start out by registering embedded modules,
90                                       since they are here before we dlopen() any
91                                    */
92
93 struct ast_module {
94         const struct ast_module_info *info;
95         void *lib;                                      /* the shared lib, or NULL if embedded */
96         int usecount;                                   /* the number of 'users' currently in this module */
97         struct module_user_list users;                  /* the list of users in the module */
98         struct {
99                 unsigned int running:1;
100                 unsigned int declined:1;
101         } flags;
102         AST_LIST_ENTRY(ast_module) entry;
103         char resource[0];
104 };
105
106 static AST_LIST_HEAD_STATIC(module_list, ast_module);
107
108 const char *ast_module_name(const struct ast_module *mod)
109 {
110         if (!mod || !mod->info) {
111                 return NULL;
112         }
113
114         return mod->info->name;
115 }
116
117 /*
118  * module_list is cleared by its constructor possibly after
119  * we start accumulating embedded modules, so we need to
120  * use another list (without the lock) to accumulate them.
121  * Then we update the main list when embedding is done.
122  */
123 static struct module_list embedded_module_list;
124
125 struct loadupdate {
126         int (*updater)(void);
127         AST_LIST_ENTRY(loadupdate) entry;
128 };
129
130 static AST_LIST_HEAD_STATIC(updaters, loadupdate);
131
132 AST_MUTEX_DEFINE_STATIC(reloadlock);
133
134 struct reload_queue_item {
135         AST_LIST_ENTRY(reload_queue_item) entry;
136         char module[0];
137 };
138
139 static int do_full_reload = 0;
140
141 static AST_LIST_HEAD_STATIC(reload_queue, reload_queue_item);
142
143 /* when dynamic modules are being loaded, ast_module_register() will
144    need to know what filename the module was loaded from while it
145    is being registered
146 */
147 static struct ast_module *resource_being_loaded;
148
149 /* XXX: should we check for duplicate resource names here? */
150
151 void ast_module_register(const struct ast_module_info *info)
152 {
153         struct ast_module *mod;
154
155         if (embedding) {
156                 if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
157                         return;
158                 strcpy(mod->resource, info->name);
159         } else {
160                 mod = resource_being_loaded;
161         }
162
163         mod->info = info;
164         AST_LIST_HEAD_INIT(&mod->users);
165
166         /* during startup, before the loader has been initialized,
167            there are no threads, so there is no need to take the lock
168            on this list to manipulate it. it is also possible that it
169            might be unsafe to use the list lock at that point... so
170            let's avoid it altogether
171         */
172         if (embedding) {
173                 AST_LIST_INSERT_TAIL(&embedded_module_list, mod, entry);
174         } else {
175                 AST_LIST_LOCK(&module_list);
176                 /* it is paramount that the new entry be placed at the tail of
177                    the list, otherwise the code that uses dlopen() to load
178                    dynamic modules won't be able to find out if the module it
179                    just opened was registered or failed to load
180                 */
181                 AST_LIST_INSERT_TAIL(&module_list, mod, entry);
182                 AST_LIST_UNLOCK(&module_list);
183         }
184
185         /* give the module a copy of its own handle, for later use in registrations and the like */
186         *((struct ast_module **) &(info->self)) = mod;
187 }
188
189 void ast_module_unregister(const struct ast_module_info *info)
190 {
191         struct ast_module *mod = NULL;
192
193         /* it is assumed that the users list in the module structure
194            will already be empty, or we cannot have gotten to this
195            point
196         */
197         AST_LIST_LOCK(&module_list);
198         AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
199                 if (mod->info == info) {
200                         AST_LIST_REMOVE_CURRENT(entry);
201                         break;
202                 }
203         }
204         AST_LIST_TRAVERSE_SAFE_END;
205         AST_LIST_UNLOCK(&module_list);
206
207         if (mod) {
208                 AST_LIST_HEAD_DESTROY(&mod->users);
209                 ast_free(mod);
210         }
211 }
212
213 struct ast_module_user *__ast_module_user_add(struct ast_module *mod, struct ast_channel *chan)
214 {
215         struct ast_module_user *u;
216
217         u = ast_calloc(1, sizeof(*u));
218         if (!u) {
219                 return NULL;
220         }
221
222         u->chan = chan;
223
224         AST_LIST_LOCK(&mod->users);
225         AST_LIST_INSERT_HEAD(&mod->users, u, entry);
226         AST_LIST_UNLOCK(&mod->users);
227
228         ast_atomic_fetchadd_int(&mod->usecount, +1);
229
230         ast_update_use_count();
231
232         return u;
233 }
234
235 void __ast_module_user_remove(struct ast_module *mod, struct ast_module_user *u)
236 {
237         if (!u) {
238                 return;
239         }
240
241         AST_LIST_LOCK(&mod->users);
242         u = AST_LIST_REMOVE(&mod->users, u, entry);
243         AST_LIST_UNLOCK(&mod->users);
244         if (!u) {
245                 /*
246                  * Was not in the list.  Either a bad pointer or
247                  * __ast_module_user_hangup_all() has been called.
248                  */
249                 return;
250         }
251
252         ast_atomic_fetchadd_int(&mod->usecount, -1);
253         ast_free(u);
254
255         ast_update_use_count();
256 }
257
258 void __ast_module_user_hangup_all(struct ast_module *mod)
259 {
260         struct ast_module_user *u;
261
262         AST_LIST_LOCK(&mod->users);
263         while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
264                 if (u->chan) {
265                         ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
266                 }
267                 ast_atomic_fetchadd_int(&mod->usecount, -1);
268                 ast_free(u);
269         }
270         AST_LIST_UNLOCK(&mod->users);
271
272         ast_update_use_count();
273 }
274
275 /*! \note
276  * In addition to modules, the reload command handles some extra keywords
277  * which are listed here together with the corresponding handlers.
278  * This table is also used by the command completion code.
279  */
280 static struct reload_classes {
281         const char *name;
282         int (*reload_fn)(void);
283 } reload_classes[] = {  /* list in alpha order, longest match first for cli completion */
284         { "cdr",        ast_cdr_engine_reload },
285         { "dnsmgr",     dnsmgr_reload },
286         { "extconfig",  read_config_maps },
287         { "enum",       ast_enum_reload },
288         { "acl",        ast_named_acl_reload },
289         { "manager",    reload_manager },
290         { "http",       ast_http_reload },
291         { "logger",     logger_reload },
292         { "features",   ast_features_reload },
293         { "dsp",        ast_dsp_reload},
294         { "udptl",      ast_udptl_reload },
295         { "indications", ast_indications_reload },
296         { "cel",        ast_cel_engine_reload },
297         { "plc",        ast_plc_reload },
298         { NULL,         NULL }
299 };
300
301 static int printdigest(const unsigned char *d)
302 {
303         int x, pos;
304         char buf[256]; /* large enough so we don't have to worry */
305
306         for (pos = 0, x = 0; x < 16; x++)
307                 pos += sprintf(buf + pos, " %02x", *d++);
308
309         ast_debug(1, "Unexpected signature:%s\n", buf);
310
311         return 0;
312 }
313
314 static int key_matches(const unsigned char *key1, const unsigned char *key2)
315 {
316         int x;
317
318         for (x = 0; x < 16; x++) {
319                 if (key1[x] != key2[x])
320                         return 0;
321         }
322
323         return 1;
324 }
325
326 static int verify_key(const unsigned char *key)
327 {
328         struct MD5Context c;
329         unsigned char digest[16];
330
331         MD5Init(&c);
332         MD5Update(&c, key, strlen((char *)key));
333         MD5Final(digest, &c);
334
335         if (key_matches(expected_key, digest))
336                 return 0;
337
338         printdigest(digest);
339
340         return -1;
341 }
342
343 static int resource_name_match(const char *name1_in, const char *name2_in)
344 {
345         char *name1 = (char *) name1_in;
346         char *name2 = (char *) name2_in;
347
348         /* trim off any .so extensions */
349         if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
350                 name1 = ast_strdupa(name1);
351                 name1[strlen(name1) - 3] = '\0';
352         }
353         if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
354                 name2 = ast_strdupa(name2);
355                 name2[strlen(name2) - 3] = '\0';
356         }
357
358         return strcasecmp(name1, name2);
359 }
360
361 static struct ast_module *find_resource(const char *resource, int do_lock)
362 {
363         struct ast_module *cur;
364
365         if (do_lock)
366                 AST_LIST_LOCK(&module_list);
367
368         AST_LIST_TRAVERSE(&module_list, cur, entry) {
369                 if (!resource_name_match(resource, cur->resource))
370                         break;
371         }
372
373         if (do_lock)
374                 AST_LIST_UNLOCK(&module_list);
375
376         return cur;
377 }
378
379 #ifdef LOADABLE_MODULES
380 static void unload_dynamic_module(struct ast_module *mod)
381 {
382         void *lib = mod->lib;
383
384         /* WARNING: the structure pointed to by mod is going to
385            disappear when this operation succeeds, so we can't
386            dereference it */
387
388         if (lib)
389                 while (!dlclose(lib));
390 }
391
392 static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required);
393
394 static struct ast_module *load_dynamic_module(const char *resource_in, unsigned int global_symbols_only, struct ast_heap *resource_heap)
395 {
396         char fn[PATH_MAX] = "";
397         void *lib = NULL;
398         struct ast_module *mod;
399         unsigned int wants_global;
400         int space;      /* room needed for the descriptor */
401         int missing_so = 0;
402
403         space = sizeof(*resource_being_loaded) + strlen(resource_in) + 1;
404         if (strcasecmp(resource_in + strlen(resource_in) - 3, ".so")) {
405                 missing_so = 1;
406                 space += 3;     /* room for the extra ".so" */
407         }
408
409         snprintf(fn, sizeof(fn), "%s/%s%s", ast_config_AST_MODULE_DIR, resource_in, missing_so ? ".so" : "");
410
411         /* make a first load of the module in 'quiet' mode... don't try to resolve
412            any symbols, and don't export any symbols. this will allow us to peek into
413            the module's info block (if available) to see what flags it has set */
414
415         resource_being_loaded = ast_calloc(1, space);
416         if (!resource_being_loaded)
417                 return NULL;
418         strcpy(resource_being_loaded->resource, resource_in);
419         if (missing_so)
420                 strcat(resource_being_loaded->resource, ".so");
421
422         if (!(lib = dlopen(fn, RTLD_LAZY | RTLD_LOCAL))) {
423                 ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
424                 ast_free(resource_being_loaded);
425                 return NULL;
426         }
427
428         /* the dlopen() succeeded, let's find out if the module
429            registered itself */
430         /* note that this will only work properly as long as
431            ast_module_register() (which is called by the module's
432            constructor) places the new module at the tail of the
433            module_list
434         */
435         if (resource_being_loaded != (mod = AST_LIST_LAST(&module_list))) {
436                 ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);
437                 /* no, it did not, so close it and return */
438                 while (!dlclose(lib));
439                 /* note that the module's destructor will call ast_module_unregister(),
440                    which will free the structure we allocated in resource_being_loaded */
441                 return NULL;
442         }
443
444         wants_global = ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS);
445
446         /* if we are being asked only to load modules that provide global symbols,
447            and this one does not, then close it and return */
448         if (global_symbols_only && !wants_global) {
449                 while (!dlclose(lib));
450                 return NULL;
451         }
452
453         /* This section is a workaround for a gcc 4.1 bug that has already been
454          * fixed in later versions.  Unfortunately, some distributions, such as
455          * RHEL/CentOS 5, distribute gcc 4.1, so we're stuck with having to deal
456          * with this issue.  This basically ensures that optional_api modules are
457          * loaded before any module which requires their functionality. */
458 #if !defined(HAVE_ATTRIBUTE_weak_import) && !defined(HAVE_ATTRIBUTE_weakref)
459         if (!ast_strlen_zero(mod->info->nonoptreq)) {
460                 /* Force any required dependencies to load */
461                 char *each, *required_resource = ast_strdupa(mod->info->nonoptreq);
462                 while ((each = strsep(&required_resource, ","))) {
463                         struct ast_module *dependency;
464                         each = ast_strip(each);
465                         dependency = find_resource(each, 0);
466                         /* Is it already loaded? */
467                         if (!dependency) {
468                                 load_resource(each, global_symbols_only, resource_heap, 1);
469                         }
470                 }
471         }
472 #endif
473
474         while (!dlclose(lib));
475         resource_being_loaded = NULL;
476
477         /* start the load process again */
478         resource_being_loaded = ast_calloc(1, space);
479         if (!resource_being_loaded)
480                 return NULL;
481         strcpy(resource_being_loaded->resource, resource_in);
482         if (missing_so)
483                 strcat(resource_being_loaded->resource, ".so");
484
485         if (!(lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {
486                 ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
487                 ast_free(resource_being_loaded);
488                 return NULL;
489         }
490
491         /* since the module was successfully opened, and it registered itself
492            the previous time we did that, we're going to assume it worked this
493            time too :) */
494
495         AST_LIST_LAST(&module_list)->lib = lib;
496         resource_being_loaded = NULL;
497
498         return AST_LIST_LAST(&module_list);
499 }
500 #endif
501
502 void ast_module_shutdown(void)
503 {
504         struct ast_module *mod;
505         int somethingchanged = 1, final = 0;
506
507         AST_LIST_LOCK(&module_list);
508
509         /*!\note Some resources, like timers, are started up dynamically, and thus
510          * may be still in use, even if all channels are dead.  We must therefore
511          * check the usecount before asking modules to unload. */
512         do {
513                 if (!somethingchanged) {
514                         /*!\note If we go through the entire list without changing
515                          * anything, ignore the usecounts and unload, then exit. */
516                         final = 1;
517                 }
518
519                 /* Reset flag before traversing the list */
520                 somethingchanged = 0;
521
522                 AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
523                         if (!final && mod->usecount) {
524                                 continue;
525                         }
526                         AST_LIST_REMOVE_CURRENT(entry);
527                         if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
528                                 mod->info->unload();
529                         }
530                         AST_LIST_HEAD_DESTROY(&mod->users);
531                         free(mod);
532                         somethingchanged = 1;
533                 }
534                 AST_LIST_TRAVERSE_SAFE_END;
535         } while (somethingchanged && !final);
536
537         AST_LIST_UNLOCK(&module_list);
538 }
539
540 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
541 {
542         struct ast_module *mod;
543         int res = -1;
544         int error = 0;
545
546         AST_LIST_LOCK(&module_list);
547
548         if (!(mod = find_resource(resource_name, 0))) {
549                 AST_LIST_UNLOCK(&module_list);
550                 ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
551                 return -1;
552         }
553
554         if (!mod->flags.running || mod->flags.declined) {
555                 ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
556                 error = 1;
557         }
558
559         if (!error && (mod->usecount > 0)) {
560                 if (force)
561                         ast_log(LOG_WARNING, "Warning:  Forcing removal of module '%s' with use count %d\n",
562                                 resource_name, mod->usecount);
563                 else {
564                         ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
565                                 mod->usecount);
566                         error = 1;
567                 }
568         }
569
570         if (!error) {
571                 /* Request any channels attached to the module to hangup. */
572                 __ast_module_user_hangup_all(mod);
573
574                 res = mod->info->unload();
575                 if (res) {
576                         ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
577                         if (force <= AST_FORCE_FIRM) {
578                                 error = 1;
579                         } else {
580                                 ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
581                         }
582                 }
583
584                 if (!error) {
585                         /*
586                          * Request hangup on any channels that managed to get attached
587                          * while we called the module unload function.
588                          */
589                         __ast_module_user_hangup_all(mod);
590                         sched_yield();
591                 }
592         }
593
594         if (!error)
595                 mod->flags.running = mod->flags.declined = 0;
596
597         AST_LIST_UNLOCK(&module_list);
598
599         if (!error && !mod->lib && mod->info && mod->info->restore_globals)
600                 mod->info->restore_globals();
601
602 #ifdef LOADABLE_MODULES
603         if (!error) {
604                 unload_dynamic_module(mod);
605                 ast_test_suite_event_notify("MODULE_UNLOAD", "Message: %s", resource_name);
606         }
607 #endif
608
609         if (!error)
610                 ast_update_use_count();
611
612         return res;
613 }
614
615 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload)
616 {
617         struct ast_module *cur;
618         int i, which=0, l = strlen(word);
619         char *ret = NULL;
620
621         if (pos != rpos)
622                 return NULL;
623
624         AST_LIST_LOCK(&module_list);
625         AST_LIST_TRAVERSE(&module_list, cur, entry) {
626                 if (!strncasecmp(word, cur->resource, l) &&
627                     (cur->info->reload || !needsreload) &&
628                     ++which > state) {
629                         ret = ast_strdup(cur->resource);
630                         break;
631                 }
632         }
633         AST_LIST_UNLOCK(&module_list);
634
635         if (!ret) {
636                 for (i=0; !ret && reload_classes[i].name; i++) {
637                         if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
638                                 ret = ast_strdup(reload_classes[i].name);
639                 }
640         }
641
642         return ret;
643 }
644
645 void ast_process_pending_reloads(void)
646 {
647         struct reload_queue_item *item;
648
649         if (!ast_fully_booted) {
650                 return;
651         }
652
653         AST_LIST_LOCK(&reload_queue);
654
655         if (do_full_reload) {
656                 do_full_reload = 0;
657                 AST_LIST_UNLOCK(&reload_queue);
658                 ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
659                 ast_module_reload(NULL);
660                 return;
661         }
662
663         while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
664                 ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
665                 ast_module_reload(item->module);
666                 ast_free(item);
667         }
668
669         AST_LIST_UNLOCK(&reload_queue);
670 }
671
672 static void queue_reload_request(const char *module)
673 {
674         struct reload_queue_item *item;
675
676         AST_LIST_LOCK(&reload_queue);
677
678         if (do_full_reload) {
679                 AST_LIST_UNLOCK(&reload_queue);
680                 return;
681         }
682
683         if (ast_strlen_zero(module)) {
684                 /* A full reload request (when module is NULL) wipes out any previous
685                    reload requests and causes the queue to ignore future ones */
686                 while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
687                         ast_free(item);
688                 }
689                 do_full_reload = 1;
690         } else {
691                 /* No reason to add the same module twice */
692                 AST_LIST_TRAVERSE(&reload_queue, item, entry) {
693                         if (!strcasecmp(item->module, module)) {
694                                 AST_LIST_UNLOCK(&reload_queue);
695                                 return;
696                         }
697                 }
698                 item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
699                 if (!item) {
700                         ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
701                         AST_LIST_UNLOCK(&reload_queue);
702                         return;
703                 }
704                 strcpy(item->module, module);
705                 AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
706         }
707         AST_LIST_UNLOCK(&reload_queue);
708 }
709
710 int ast_module_reload(const char *name)
711 {
712         struct ast_module *cur;
713         int res = 0; /* return value. 0 = not found, others, see below */
714         int i;
715
716         /* If we aren't fully booted, we just pretend we reloaded but we queue this
717            up to run once we are booted up. */
718         if (!ast_fully_booted) {
719                 queue_reload_request(name);
720                 return 0;
721         }
722
723         if (ast_mutex_trylock(&reloadlock)) {
724                 ast_verbose("The previous reload command didn't finish yet\n");
725                 return -1;      /* reload already in progress */
726         }
727         ast_lastreloadtime = ast_tvnow();
728
729         if (ast_opt_lock_confdir) {
730                 int try;
731                 int res;
732                 for (try = 1, res = AST_LOCK_TIMEOUT; try < 6 && (res == AST_LOCK_TIMEOUT); try++) {
733                         res = ast_lock_path(ast_config_AST_CONFIG_DIR);
734                         if (res == AST_LOCK_TIMEOUT) {
735                                 ast_log(LOG_WARNING, "Failed to grab lock on %s, try %d\n", ast_config_AST_CONFIG_DIR, try);
736                         }
737                 }
738                 if (res != AST_LOCK_SUCCESS) {
739                         ast_verbose("Cannot grab lock on %s\n", ast_config_AST_CONFIG_DIR);
740                         ast_mutex_unlock(&reloadlock);
741                         return -1;
742                 }
743         }
744
745         /* Call "predefined" reload here first */
746         for (i = 0; reload_classes[i].name; i++) {
747                 if (!name || !strcasecmp(name, reload_classes[i].name)) {
748                         if (!reload_classes[i].reload_fn()) {
749                                 ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", name);
750                         }
751                         res = 2;        /* found and reloaded */
752                 }
753         }
754
755         if (name && res) {
756                 if (ast_opt_lock_confdir) {
757                         ast_unlock_path(ast_config_AST_CONFIG_DIR);
758                 }
759                 ast_mutex_unlock(&reloadlock);
760                 return res;
761         }
762
763         AST_LIST_LOCK(&module_list);
764         AST_LIST_TRAVERSE(&module_list, cur, entry) {
765                 const struct ast_module_info *info = cur->info;
766
767                 if (name && resource_name_match(name, cur->resource))
768                         continue;
769
770                 if (!cur->flags.running || cur->flags.declined) {
771                         if (!name)
772                                 continue;
773                         ast_log(LOG_NOTICE, "The module '%s' was not properly initialized.  "
774                                 "Before reloading the module, you must run \"module load %s\" "
775                                 "and fix whatever is preventing the module from being initialized.\n",
776                                 name, name);
777                         res = 2; /* Don't report that the module was not found */
778                         break;
779                 }
780
781                 if (!info->reload) {    /* cannot be reloaded */
782                         /* Nothing to reload, so reload is successful */
783                         ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", cur->resource);
784                         if (res < 1)    /* store result if possible */
785                                 res = 1;        /* 1 = no reload() method */
786                         continue;
787                 }
788
789                 res = 2;
790                 ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
791                 if (!info->reload()) {
792                         ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", cur->resource);
793                 }
794         }
795         AST_LIST_UNLOCK(&module_list);
796
797         if (ast_opt_lock_confdir) {
798                 ast_unlock_path(ast_config_AST_CONFIG_DIR);
799         }
800         ast_mutex_unlock(&reloadlock);
801
802         return res;
803 }
804
805 static unsigned int inspect_module(const struct ast_module *mod)
806 {
807         if (!mod->info->description) {
808                 ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
809                 return 1;
810         }
811
812         if (!mod->info->key) {
813                 ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
814                 return 1;
815         }
816
817         if (verify_key((unsigned char *) mod->info->key)) {
818                 ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
819                 return 1;
820         }
821
822         if (!ast_strlen_zero(mod->info->buildopt_sum) &&
823             strcmp(buildopt_sum, mod->info->buildopt_sum)) {
824                 ast_log(LOG_WARNING, "Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod->resource);
825                 ast_log(LOG_WARNING, "Module '%s' will not be initialized as it may cause instability.\n", mod->resource);
826                 return 1;
827         }
828
829         return 0;
830 }
831
832 static enum ast_module_load_result start_resource(struct ast_module *mod)
833 {
834         char tmp[256];
835         enum ast_module_load_result res;
836
837         if (mod->flags.running) {
838                 return AST_MODULE_LOAD_SUCCESS;
839         }
840
841         if (!mod->info->load) {
842                 return AST_MODULE_LOAD_FAILURE;
843         }
844
845         res = mod->info->load();
846
847         switch (res) {
848         case AST_MODULE_LOAD_SUCCESS:
849                 if (!ast_fully_booted) {
850                         ast_verb(1, "%s => (%s)\n", mod->resource, term_color(tmp, mod->info->description, COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
851                         if (ast_opt_console && !option_verbose) {
852                                 /* This never looks good on anything but the root console, so
853                                  * it's best not to try to funnel it through the logger. */
854                                 fprintf(stdout, ".");
855                         }
856                 } else {
857                         ast_verb(1, "Loaded %s => (%s)\n", mod->resource, mod->info->description);
858                 }
859
860                 mod->flags.running = 1;
861
862                 ast_update_use_count();
863                 break;
864         case AST_MODULE_LOAD_DECLINE:
865                 mod->flags.declined = 1;
866                 break;
867         case AST_MODULE_LOAD_FAILURE:
868         case AST_MODULE_LOAD_SKIP: /* modules should never return this value */
869         case AST_MODULE_LOAD_PRIORITY:
870                 break;
871         }
872
873         return res;
874 }
875
876 /*! loads a resource based upon resource_name. If global_symbols_only is set
877  *  only modules with global symbols will be loaded.
878  *
879  *  If the ast_heap is provided (not NULL) the module is found and added to the
880  *  heap without running the module's load() function.  By doing this, modules
881  *  added to the resource_heap can be initialized later in order by priority.
882  *
883  *  If the ast_heap is not provided, the module's load function will be executed
884  *  immediately */
885 static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required)
886 {
887         struct ast_module *mod;
888         enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;
889
890         if ((mod = find_resource(resource_name, 0))) {
891                 if (mod->flags.running) {
892                         ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
893                         return AST_MODULE_LOAD_DECLINE;
894                 }
895                 if (global_symbols_only && !ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS))
896                         return AST_MODULE_LOAD_SKIP;
897         } else {
898 #ifdef LOADABLE_MODULES
899                 if (!(mod = load_dynamic_module(resource_name, global_symbols_only, resource_heap))) {
900                         /* don't generate a warning message during load_modules() */
901                         if (!global_symbols_only) {
902                                 ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
903                                 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
904                         } else {
905                                 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SKIP;
906                         }
907                 }
908 #else
909                 ast_log(LOG_WARNING, "Module support is not available. Module '%s' could not be loaded.\n", resource_name);
910                 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
911 #endif
912         }
913
914         if (inspect_module(mod)) {
915                 ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
916 #ifdef LOADABLE_MODULES
917                 unload_dynamic_module(mod);
918 #endif
919                 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
920         }
921
922         if (!mod->lib && mod->info->backup_globals && mod->info->backup_globals()) {
923                 ast_log(LOG_WARNING, "Module '%s' was unable to backup its global data.\n", resource_name);
924                 return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
925         }
926
927         mod->flags.declined = 0;
928
929         if (resource_heap) {
930                 ast_heap_push(resource_heap, mod);
931                 res = AST_MODULE_LOAD_PRIORITY;
932         } else {
933                 res = start_resource(mod);
934         }
935
936         /* Now make sure that the list is sorted */
937         AST_LIST_LOCK(&module_list);
938         AST_LIST_REMOVE(&module_list, mod, entry);
939         AST_LIST_INSERT_SORTALPHA(&module_list, mod, entry, resource);
940         AST_LIST_UNLOCK(&module_list);
941
942         return res;
943 }
944
945 int ast_load_resource(const char *resource_name)
946 {
947         int res;
948         AST_LIST_LOCK(&module_list);
949         res = load_resource(resource_name, 0, NULL, 0);
950         if (!res) {
951                 ast_test_suite_event_notify("MODULE_LOAD", "Message: %s", resource_name);
952         }
953         AST_LIST_UNLOCK(&module_list);
954
955         return res;
956 }
957
958 struct load_order_entry {
959         char *resource;
960         int required;
961         AST_LIST_ENTRY(load_order_entry) entry;
962 };
963
964 AST_LIST_HEAD_NOLOCK(load_order, load_order_entry);
965
966 static struct load_order_entry *add_to_load_order(const char *resource, struct load_order *load_order, int required)
967 {
968         struct load_order_entry *order;
969
970         AST_LIST_TRAVERSE(load_order, order, entry) {
971                 if (!resource_name_match(order->resource, resource)) {
972                         /* Make sure we have the proper setting for the required field
973                            (we might have both load= and required= lines in modules.conf) */
974                         order->required |= required;
975                         return NULL;
976                 }
977         }
978
979         if (!(order = ast_calloc(1, sizeof(*order))))
980                 return NULL;
981
982         order->resource = ast_strdup(resource);
983         order->required = required;
984         AST_LIST_INSERT_TAIL(load_order, order, entry);
985
986         return order;
987 }
988
989 static int mod_load_cmp(void *a, void *b)
990 {
991         struct ast_module *a_mod = (struct ast_module *) a;
992         struct ast_module *b_mod = (struct ast_module *) b;
993         int res = -1;
994         /* if load_pri is not set, default is 128.  Lower is better*/
995         unsigned char a_pri = ast_test_flag(a_mod->info, AST_MODFLAG_LOAD_ORDER) ? a_mod->info->load_pri : 128;
996         unsigned char b_pri = ast_test_flag(b_mod->info, AST_MODFLAG_LOAD_ORDER) ? b_mod->info->load_pri : 128;
997         if (a_pri == b_pri) {
998                 res = 0;
999         } else if (a_pri < b_pri) {
1000                 res = 1;
1001         }
1002         return res;
1003 }
1004
1005 /*! loads modules in order by load_pri, updates mod_count
1006         \return -1 on failure to load module, -2 on failure to load required module, otherwise 0
1007 */
1008 static int load_resource_list(struct load_order *load_order, unsigned int global_symbols, int *mod_count)
1009 {
1010         struct ast_heap *resource_heap;
1011         struct load_order_entry *order;
1012         struct ast_module *mod;
1013         int count = 0;
1014         int res = 0;
1015
1016         if(!(resource_heap = ast_heap_create(8, mod_load_cmp, -1))) {
1017                 return -1;
1018         }
1019
1020         /* first, add find and add modules to heap */
1021         AST_LIST_TRAVERSE_SAFE_BEGIN(load_order, order, entry) {
1022                 switch (load_resource(order->resource, global_symbols, resource_heap, order->required)) {
1023                 case AST_MODULE_LOAD_SUCCESS:
1024                 case AST_MODULE_LOAD_DECLINE:
1025                         AST_LIST_REMOVE_CURRENT(entry);
1026                         ast_free(order->resource);
1027                         ast_free(order);
1028                         break;
1029                 case AST_MODULE_LOAD_FAILURE:
1030                         ast_log(LOG_ERROR, "*** Failed to load module %s - %s\n", order->resource, order->required ? "Required" : "Not required");
1031                         fprintf(stderr, "*** Failed to load module %s - %s\n", order->resource, order->required ? "Required" : "Not required");
1032                         res = order->required ? -2 : -1;
1033                         goto done;
1034                 case AST_MODULE_LOAD_SKIP:
1035                         break;
1036                 case AST_MODULE_LOAD_PRIORITY:
1037                         AST_LIST_REMOVE_CURRENT(entry);
1038                         break;
1039                 }
1040         }
1041         AST_LIST_TRAVERSE_SAFE_END;
1042
1043         /* second remove modules from heap sorted by priority */
1044         while ((mod = ast_heap_pop(resource_heap))) {
1045                 switch (start_resource(mod)) {
1046                 case AST_MODULE_LOAD_SUCCESS:
1047                         count++;
1048                 case AST_MODULE_LOAD_DECLINE:
1049                         break;
1050                 case AST_MODULE_LOAD_FAILURE:
1051                         res = -1;
1052                         goto done;
1053                 case AST_MODULE_LOAD_SKIP:
1054                 case AST_MODULE_LOAD_PRIORITY:
1055                         break;
1056                 }
1057         }
1058
1059 done:
1060         if (mod_count) {
1061                 *mod_count += count;
1062         }
1063         ast_heap_destroy(resource_heap);
1064
1065         return res;
1066 }
1067
1068 int load_modules(unsigned int preload_only)
1069 {
1070         struct ast_config *cfg;
1071         struct ast_module *mod;
1072         struct load_order_entry *order;
1073         struct ast_variable *v;
1074         unsigned int load_count;
1075         struct load_order load_order;
1076         int res = 0;
1077         struct ast_flags config_flags = { 0 };
1078         int modulecount = 0;
1079
1080 #ifdef LOADABLE_MODULES
1081         struct dirent *dirent;
1082         DIR *dir;
1083 #endif
1084
1085         /* all embedded modules have registered themselves by now */
1086         embedding = 0;
1087
1088         ast_verb(1, "Asterisk Dynamic Loader Starting:\n");
1089
1090         AST_LIST_HEAD_INIT_NOLOCK(&load_order);
1091
1092         AST_LIST_LOCK(&module_list);
1093
1094         if (embedded_module_list.first) {
1095                 module_list.first = embedded_module_list.first;
1096                 module_list.last = embedded_module_list.last;
1097                 embedded_module_list.first = NULL;
1098         }
1099
1100         cfg = ast_config_load2(AST_MODULE_CONFIG, "" /* core, can't reload */, config_flags);
1101         if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
1102                 ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
1103                 goto done;
1104         }
1105
1106         /* first, find all the modules we have been explicitly requested to load */
1107         for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
1108                 if (!strcasecmp(v->name, preload_only ? "preload" : "load")) {
1109                         add_to_load_order(v->value, &load_order, 0);
1110                 }
1111                 if (!strcasecmp(v->name, preload_only ? "preload-require" : "require")) {
1112                         /* Add the module to the list and make sure it's required */
1113                         add_to_load_order(v->value, &load_order, 1);
1114                         ast_debug(2, "Adding module to required list: %s (%s)\n", v->value, v->name);
1115                 }
1116
1117         }
1118
1119         /* check if 'autoload' is on */
1120         if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
1121                 /* if so, first add all the embedded modules that are not already running to the load order */
1122                 AST_LIST_TRAVERSE(&module_list, mod, entry) {
1123                         /* if it's not embedded, skip it */
1124                         if (mod->lib)
1125                                 continue;
1126
1127                         if (mod->flags.running)
1128                                 continue;
1129
1130                         add_to_load_order(mod->resource, &load_order, 0);
1131                 }
1132
1133 #ifdef LOADABLE_MODULES
1134                 /* if we are allowed to load dynamic modules, scan the directory for
1135                    for all available modules and add them as well */
1136                 if ((dir = opendir(ast_config_AST_MODULE_DIR))) {
1137                         while ((dirent = readdir(dir))) {
1138                                 int ld = strlen(dirent->d_name);
1139
1140                                 /* Must end in .so to load it.  */
1141
1142                                 if (ld < 4)
1143                                         continue;
1144
1145                                 if (strcasecmp(dirent->d_name + ld - 3, ".so"))
1146                                         continue;
1147
1148                                 /* if there is already a module by this name in the module_list,
1149                                    skip this file */
1150                                 if (find_resource(dirent->d_name, 0))
1151                                         continue;
1152
1153                                 add_to_load_order(dirent->d_name, &load_order, 0);
1154                         }
1155
1156                         closedir(dir);
1157                 } else {
1158                         if (!ast_opt_quiet)
1159                                 ast_log(LOG_WARNING, "Unable to open modules directory '%s'.\n",
1160                                         ast_config_AST_MODULE_DIR);
1161                 }
1162 #endif
1163         }
1164
1165         /* now scan the config for any modules we are prohibited from loading and
1166            remove them from the load order */
1167         for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
1168                 if (strcasecmp(v->name, "noload"))
1169                         continue;
1170
1171                 AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
1172                         if (!resource_name_match(order->resource, v->value)) {
1173                                 AST_LIST_REMOVE_CURRENT(entry);
1174                                 ast_free(order->resource);
1175                                 ast_free(order);
1176                         }
1177                 }
1178                 AST_LIST_TRAVERSE_SAFE_END;
1179         }
1180
1181         /* we are done with the config now, all the information we need is in the
1182            load_order list */
1183         ast_config_destroy(cfg);
1184
1185         load_count = 0;
1186         AST_LIST_TRAVERSE(&load_order, order, entry)
1187                 load_count++;
1188
1189         if (load_count)
1190                 ast_log(LOG_NOTICE, "%d modules will be loaded.\n", load_count);
1191
1192         /* first, load only modules that provide global symbols */
1193         if ((res = load_resource_list(&load_order, 1, &modulecount)) < 0) {
1194                 goto done;
1195         }
1196
1197         /* now load everything else */
1198         if ((res = load_resource_list(&load_order, 0, &modulecount)) < 0) {
1199                 goto done;
1200         }
1201
1202 done:
1203         while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
1204                 ast_free(order->resource);
1205                 ast_free(order);
1206         }
1207
1208         AST_LIST_UNLOCK(&module_list);
1209
1210         /* Tell manager clients that are aggressive at logging in that we're done
1211            loading modules. If there's a DNS problem in chan_sip, we might not
1212            even reach this */
1213         /*** DOCUMENTATION
1214                 <managerEventInstance>
1215                         <synopsis>Raised when all dynamic modules have finished their initial loading.</synopsis>
1216                         <syntax>
1217                                 <parameter name="ModuleSelection">
1218                                         <enumlist>
1219                                                 <enum name="Preload"/>
1220                                                 <enum name="All"/>
1221                                         </enumlist>
1222                                 </parameter>
1223                         </syntax>
1224                 </managerEventInstance>
1225         ***/
1226         manager_event(EVENT_FLAG_SYSTEM, "ModuleLoadReport", "ModuleLoadStatus: Done\r\nModuleSelection: %s\r\nModuleCount: %d\r\n", preload_only ? "Preload" : "All", modulecount);
1227
1228         return res;
1229 }
1230
1231 void ast_update_use_count(void)
1232 {
1233         /* Notify any module monitors that the use count for a
1234            resource has changed */
1235         struct loadupdate *m;
1236
1237         AST_LIST_LOCK(&updaters);
1238         AST_LIST_TRAVERSE(&updaters, m, entry)
1239                 m->updater();
1240         AST_LIST_UNLOCK(&updaters);
1241 }
1242
1243 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *status, const char *like),
1244                            const char *like)
1245 {
1246         struct ast_module *cur;
1247         int unlock = -1;
1248         int total_mod_loaded = 0;
1249
1250         if (AST_LIST_TRYLOCK(&module_list))
1251                 unlock = 0;
1252
1253         AST_LIST_TRAVERSE(&module_list, cur, entry) {
1254                 total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount,
1255                                                 cur->flags.running ? "Running" : "Not Running", like);
1256         }
1257
1258         if (unlock)
1259                 AST_LIST_UNLOCK(&module_list);
1260
1261         return total_mod_loaded;
1262 }
1263
1264 /*! \brief Check if module exists */
1265 int ast_module_check(const char *name)
1266 {
1267         struct ast_module *cur;
1268
1269         if (ast_strlen_zero(name))
1270                 return 0;       /* FALSE */
1271
1272         cur = find_resource(name, 1);
1273
1274         return (cur != NULL);
1275 }
1276
1277
1278 int ast_loader_register(int (*v)(void))
1279 {
1280         struct loadupdate *tmp;
1281
1282         if (!(tmp = ast_malloc(sizeof(*tmp))))
1283                 return -1;
1284
1285         tmp->updater = v;
1286         AST_LIST_LOCK(&updaters);
1287         AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
1288         AST_LIST_UNLOCK(&updaters);
1289
1290         return 0;
1291 }
1292
1293 int ast_loader_unregister(int (*v)(void))
1294 {
1295         struct loadupdate *cur;
1296
1297         AST_LIST_LOCK(&updaters);
1298         AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
1299                 if (cur->updater == v)  {
1300                         AST_LIST_REMOVE_CURRENT(entry);
1301                         break;
1302                 }
1303         }
1304         AST_LIST_TRAVERSE_SAFE_END;
1305         AST_LIST_UNLOCK(&updaters);
1306
1307         return cur ? 0 : -1;
1308 }
1309
1310 struct ast_module *ast_module_ref(struct ast_module *mod)
1311 {
1312         if (!mod) {
1313                 return NULL;
1314         }
1315
1316         ast_atomic_fetchadd_int(&mod->usecount, +1);
1317         ast_update_use_count();
1318
1319         return mod;
1320 }
1321
1322 void ast_module_unref(struct ast_module *mod)
1323 {
1324         if (!mod) {
1325                 return;
1326         }
1327
1328         ast_atomic_fetchadd_int(&mod->usecount, -1);
1329         ast_update_use_count();
1330 }