2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * Kevin P. Fleming <kpfleming@digium.com>
8 * Luigi Rizzo <rizzo@icir.org>
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.
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.
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>
32 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
41 #include "asterisk/linkedlists.h"
42 #include "asterisk/module.h"
43 #include "asterisk/options.h"
44 #include "asterisk/config.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/term.h"
48 #include "asterisk/manager.h"
49 #include "asterisk/cdr.h"
50 #include "asterisk/enum.h"
51 #include "asterisk/rtp.h"
52 #include "asterisk/http.h"
53 #include "asterisk/lock.h"
56 #include "asterisk/dlfcn-compat.h"
61 #include "asterisk/md5.h"
62 #include "asterisk/utils.h"
68 struct ast_module_user
{
69 struct ast_channel
*chan
;
70 AST_LIST_ENTRY(ast_module_user
) entry
;
73 AST_LIST_HEAD(module_user_list
, ast_module_user
);
75 static unsigned char expected_key
[] =
76 { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
77 0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
79 static char buildopt_sum
[33] = AST_BUILDOPT_SUM
;
81 static unsigned int embedding
= 1; /* we always start out by registering embedded modules,
82 since they are here before we dlopen() any
86 const struct ast_module_info
*info
;
87 void *lib
; /* the shared lib, or NULL if embedded */
88 int usecount
; /* the number of 'users' currently in this module */
89 struct module_user_list users
; /* the list of users in the module */
91 unsigned int running
:1;
92 unsigned int declined
:1;
94 AST_LIST_ENTRY(ast_module
) entry
;
98 static AST_LIST_HEAD_STATIC(module_list
, ast_module
);
101 int (*updater
)(void);
102 AST_LIST_ENTRY(loadupdate
) entry
;
105 static AST_LIST_HEAD_STATIC(updaters
, loadupdate
);
107 AST_MUTEX_DEFINE_STATIC(reloadlock
);
109 /* when dynamic modules are being loaded, ast_module_register() will
110 need to know what filename the module was loaded from while it
113 struct ast_module
*resource_being_loaded
;
115 /* XXX: should we check for duplicate resource names here? */
117 void ast_module_register(const struct ast_module_info
*info
)
119 struct ast_module
*mod
;
122 if (!(mod
= ast_calloc(1, sizeof(*mod
) + strlen(info
->name
) + 1)))
124 strcpy(mod
->resource
, info
->name
);
126 mod
= resource_being_loaded
;
130 AST_LIST_HEAD_INIT(&mod
->users
);
132 /* during startup, before the loader has been initialized,
133 there are no threads, so there is no need to take the lock
134 on this list to manipulate it. it is also possible that it
135 might be unsafe to use the list lock at that point... so
136 let's avoid it altogether
139 AST_LIST_LOCK(&module_list
);
141 /* it is paramount that the new entry be placed at the tail of
142 the list, otherwise the code that uses dlopen() to load
143 dynamic modules won't be able to find out if the module it
144 just opened was registered or failed to load
146 AST_LIST_INSERT_TAIL(&module_list
, mod
, entry
);
149 AST_LIST_UNLOCK(&module_list
);
151 /* give the module a copy of its own handle, for later use in registrations and the like */
152 *((struct ast_module
**) &(info
->self
)) = mod
;
155 void ast_module_unregister(const struct ast_module_info
*info
)
157 struct ast_module
*mod
= NULL
;
159 /* it is assumed that the users list in the module structure
160 will already be empty, or we cannot have gotten to this
163 AST_LIST_LOCK(&module_list
);
164 AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list
, mod
, entry
) {
165 if (mod
->info
== info
) {
166 AST_LIST_REMOVE_CURRENT(&module_list
, entry
);
170 AST_LIST_TRAVERSE_SAFE_END
;
171 AST_LIST_UNLOCK(&module_list
);
174 AST_LIST_HEAD_DESTROY(&mod
->users
);
179 struct ast_module_user
*__ast_module_user_add(struct ast_module
*mod
,
180 struct ast_channel
*chan
)
182 struct ast_module_user
*u
= ast_calloc(1, sizeof(*u
));
189 AST_LIST_LOCK(&mod
->users
);
190 AST_LIST_INSERT_HEAD(&mod
->users
, u
, entry
);
191 AST_LIST_UNLOCK(&mod
->users
);
193 ast_atomic_fetchadd_int(&mod
->usecount
, +1);
195 ast_update_use_count();
200 void __ast_module_user_remove(struct ast_module
*mod
, struct ast_module_user
*u
)
202 AST_LIST_LOCK(&mod
->users
);
203 AST_LIST_REMOVE(&mod
->users
, u
, entry
);
204 AST_LIST_UNLOCK(&mod
->users
);
205 ast_atomic_fetchadd_int(&mod
->usecount
, -1);
208 ast_update_use_count();
211 void __ast_module_user_hangup_all(struct ast_module
*mod
)
213 struct ast_module_user
*u
;
215 AST_LIST_LOCK(&mod
->users
);
216 while ((u
= AST_LIST_REMOVE_HEAD(&mod
->users
, entry
))) {
217 ast_softhangup(u
->chan
, AST_SOFTHANGUP_APPUNLOAD
);
218 ast_atomic_fetchadd_int(&mod
->usecount
, -1);
221 AST_LIST_UNLOCK(&mod
->users
);
223 ast_update_use_count();
227 * In addition to modules, the reload command handles some extra keywords
228 * which are listed here together with the corresponding handlers.
229 * This table is also used by the command completion code.
231 static struct reload_classes
{
233 int (*reload_fn
)(void);
234 } reload_classes
[] = { /* list in alpha order, longest match first for cli completion */
235 { "cdr", ast_cdr_engine_reload
},
236 { "dnsmgr", dnsmgr_reload
},
237 { "extconfig", read_config_maps
},
238 { "enum", ast_enum_reload
},
239 { "manager", reload_manager
},
240 { "rtp", ast_rtp_reload
},
241 { "http", ast_http_reload
},
242 { "logger", logger_reload
},
246 static int printdigest(const unsigned char *d
)
249 char buf
[256]; /* large enough so we don't have to worry */
251 for (pos
= 0, x
= 0; x
< 16; x
++)
252 pos
+= sprintf(buf
+ pos
, " %02x", *d
++);
254 ast_log(LOG_DEBUG
, "Unexpected signature:%s\n", buf
);
259 static int key_matches(const unsigned char *key1
, const unsigned char *key2
)
263 for (x
= 0; x
< 16; x
++) {
264 if (key1
[x
] != key2
[x
])
271 static int verify_key(const unsigned char *key
)
274 unsigned char digest
[16];
277 MD5Update(&c
, key
, strlen((char *)key
));
278 MD5Final(digest
, &c
);
280 if (key_matches(expected_key
, digest
))
288 static int resource_name_match(const char *name1_in
, const char *name2_in
)
290 char *name1
= (char *) name1_in
;
291 char *name2
= (char *) name2_in
;
293 /* trim off any .so extensions */
294 if (!strcasecmp(name1
+ strlen(name1
) - 3, ".so")) {
295 name1
= ast_strdupa(name1
);
296 name1
[strlen(name1
) - 3] = '\0';
298 if (!strcasecmp(name2
+ strlen(name2
) - 3, ".so")) {
299 name2
= ast_strdupa(name2
);
300 name2
[strlen(name2
) - 3] = '\0';
303 return strcasecmp(name1
, name2
);
306 static struct ast_module
*find_resource(const char *resource
, int do_lock
)
308 struct ast_module
*cur
;
311 AST_LIST_LOCK(&module_list
);
313 AST_LIST_TRAVERSE(&module_list
, cur
, entry
) {
314 if (!resource_name_match(resource
, cur
->resource
))
319 AST_LIST_UNLOCK(&module_list
);
324 #ifdef LOADABLE_MODULES
325 static void unload_dynamic_module(struct ast_module
*mod
)
327 void *lib
= mod
->lib
;
329 /* WARNING: the structure pointed to by mod is going to
330 disappear when this operation succeeds, so we can't
334 while (!dlclose(lib
));
337 static struct ast_module
*load_dynamic_module(const char *resource_in
, unsigned int global_symbols_only
)
341 struct ast_module
*mod
;
342 char *resource
= (char *) resource_in
;
343 unsigned int wants_global
;
345 if (strcasecmp(resource
+ strlen(resource
) - 3, ".so")) {
346 resource
= alloca(strlen(resource_in
) + 3);
347 strcpy(resource
, resource_in
);
348 strcat(resource
, ".so");
351 snprintf(fn
, sizeof(fn
), "%s/%s", ast_config_AST_MODULE_DIR
, resource
);
353 /* make a first load of the module in 'quiet' mode... don't try to resolve
354 any symbols, and don't export any symbols. this will allow us to peek into
355 the module's info block (if available) to see what flags it has set */
357 if (!(resource_being_loaded
= ast_calloc(1, sizeof(*resource_being_loaded
) + strlen(resource
) + 1)))
360 strcpy(resource_being_loaded
->resource
, resource
);
362 if (!(lib
= dlopen(fn
, RTLD_LAZY
| RTLD_LOCAL
))) {
363 ast_log(LOG_WARNING
, "Error loading module '%s': %s\n", resource_in
, dlerror());
364 free(resource_being_loaded
);
368 /* the dlopen() succeeded, let's find out if the module
370 /* note that this will only work properly as long as
371 ast_module_register() (which is called by the module's
372 constructor) places the new module at the tail of the
375 if (resource_being_loaded
!= (mod
= AST_LIST_LAST(&module_list
))) {
376 ast_log(LOG_WARNING
, "Module '%s' did not register itself during load\n", resource_in
);
377 /* no, it did not, so close it and return */
378 while (!dlclose(lib
));
379 /* note that the module's destructor will call ast_module_unregister(),
380 which will free the structure we allocated in resource_being_loaded */
384 wants_global
= ast_test_flag(mod
->info
, AST_MODFLAG_GLOBAL_SYMBOLS
);
386 /* if we are being asked only to load modules that provide global symbols,
387 and this one does not, then close it and return */
388 if (global_symbols_only
&& !wants_global
) {
389 while (!dlclose(lib
));
393 /* if the system supports RTLD_NOLOAD, we can just 'promote' the flags
394 on the already-opened library to what we want... if not, we have to
395 close it and start over
397 #if defined(HAVE_RTLD_NOLOAD) && !defined(__Darwin__)
398 if (!dlopen(fn
, RTLD_NOLOAD
| (wants_global
? RTLD_LAZY
| RTLD_GLOBAL
: RTLD_NOW
| RTLD_LOCAL
))) {
399 ast_log(LOG_WARNING
, "Unable to promote flags on module '%s': %s\n", resource_in
, dlerror());
400 while (!dlclose(lib
));
401 free(resource_being_loaded
);
405 while (!dlclose(lib
));
406 resource_being_loaded
= NULL
;
408 /* start the load process again */
410 if (!(resource_being_loaded
= ast_calloc(1, sizeof(*resource_being_loaded
) + strlen(resource
) + 1)))
413 strcpy(resource_being_loaded
->resource
, resource
);
415 if (!(lib
= dlopen(fn
, wants_global
? RTLD_LAZY
| RTLD_GLOBAL
: RTLD_NOW
| RTLD_LOCAL
))) {
416 ast_log(LOG_WARNING
, "Error loading module '%s': %s\n", resource_in
, dlerror());
417 free(resource_being_loaded
);
421 /* since the module was successfully opened, and it registered itself
422 the previous time we did that, we're going to assume it worked this
426 AST_LIST_LAST(&module_list
)->lib
= lib
;
427 resource_being_loaded
= NULL
;
429 return AST_LIST_LAST(&module_list
);
433 void ast_module_shutdown(void)
435 struct ast_module
*mod
;
436 AST_LIST_HEAD_NOLOCK_STATIC(local_module_list
, ast_module
);
438 /* We have to call the unload() callbacks in reverse order that the modules
439 * exist in the module list so it is the reverse order of how they were
442 AST_LIST_LOCK(&module_list
);
443 while ((mod
= AST_LIST_REMOVE_HEAD(&module_list
, entry
)))
444 AST_LIST_INSERT_HEAD(&local_module_list
, mod
, entry
);
445 AST_LIST_UNLOCK(&module_list
);
447 while ((mod
= AST_LIST_REMOVE_HEAD(&local_module_list
, entry
))) {
448 if (mod
->info
->unload
)
450 /* Since this should only be called when shutting down "gracefully",
451 * all channels should be down before we get to this point, meaning
452 * there will be no module users left. */
453 AST_LIST_HEAD_DESTROY(&mod
->users
);
458 int ast_unload_resource(const char *resource_name
, enum ast_module_unload_mode force
)
460 struct ast_module
*mod
;
464 AST_LIST_LOCK(&module_list
);
466 if (!(mod
= find_resource(resource_name
, 0))) {
467 AST_LIST_UNLOCK(&module_list
);
471 if (!(mod
->flags
.running
|| mod
->flags
.declined
))
475 ast_log(LOG_WARNING
, "Unloading embedded modules is not supported.\n");
479 if (!error
&& (mod
->usecount
> 0)) {
481 ast_log(LOG_WARNING
, "Warning: Forcing removal of module '%s' with use count %d\n",
482 resource_name
, mod
->usecount
);
484 ast_log(LOG_WARNING
, "Soft unload failed, '%s' has use count %d\n", resource_name
,
491 __ast_module_user_hangup_all(mod
);
492 res
= mod
->info
->unload();
495 ast_log(LOG_WARNING
, "Firm unload failed for %s\n", resource_name
);
496 if (force
<= AST_FORCE_FIRM
)
499 ast_log(LOG_WARNING
, "** Dangerous **: Unloading resource anyway, at user request\n");
504 mod
->flags
.running
= mod
->flags
.declined
= 0;
506 AST_LIST_UNLOCK(&module_list
);
508 #ifdef LOADABLE_MODULES
510 unload_dynamic_module(mod
);
514 ast_update_use_count();
519 char *ast_module_helper(const char *line
, const char *word
, int pos
, int state
, int rpos
, int needsreload
)
521 struct ast_module
*cur
;
522 int i
, which
=0, l
= strlen(word
);
528 AST_LIST_LOCK(&module_list
);
529 AST_LIST_TRAVERSE(&module_list
, cur
, entry
) {
530 if (!strncasecmp(word
, cur
->resource
, l
) &&
531 (cur
->info
->reload
|| !needsreload
) &&
533 ret
= strdup(cur
->resource
);
537 AST_LIST_UNLOCK(&module_list
);
540 for (i
=0; !ret
&& reload_classes
[i
].name
; i
++) {
541 if (!strncasecmp(word
, reload_classes
[i
].name
, l
) && ++which
> state
)
542 ret
= strdup(reload_classes
[i
].name
);
549 int ast_module_reload(const char *name
)
551 struct ast_module
*cur
;
552 int res
= 0; /* return value. 0 = not found, others, see below */
555 if (ast_mutex_trylock(&reloadlock
)) {
556 ast_verbose("The previous reload command didn't finish yet\n");
557 return -1; /* reload already in progress */
559 ast_lastreloadtime
= time(NULL
);
561 /* Call "predefined" reload here first */
562 for (i
= 0; reload_classes
[i
].name
; i
++) {
563 if (!name
|| !strcasecmp(name
, reload_classes
[i
].name
)) {
564 reload_classes
[i
].reload_fn(); /* XXX should check error ? */
565 res
= 2; /* found and reloaded */
570 ast_mutex_unlock(&reloadlock
);
574 AST_LIST_LOCK(&module_list
);
575 AST_LIST_TRAVERSE(&module_list
, cur
, entry
) {
576 const struct ast_module_info
*info
= cur
->info
;
578 if (name
&& resource_name_match(name
, cur
->resource
))
581 if (!cur
->flags
.running
|| cur
->flags
.declined
) {
584 ast_log(LOG_NOTICE
, "The module '%s' was not properly initialized. "
585 "Before reloading the module, you must run \"module load %s\" "
586 "and fix whatever is preventing the module from being initialized.\n",
588 res
= 2; /* Don't report that the module was not found */
592 if (!info
->reload
) { /* cannot be reloaded */
593 if (res
< 1) /* store result if possible */
594 res
= 1; /* 1 = no reload() method */
599 if (option_verbose
> 2)
600 ast_verbose(VERBOSE_PREFIX_3
"Reloading module '%s' (%s)\n", cur
->resource
, info
->description
);
603 AST_LIST_UNLOCK(&module_list
);
605 ast_mutex_unlock(&reloadlock
);
610 static unsigned int inspect_module(const struct ast_module
*mod
)
612 if (!mod
->info
->description
) {
613 ast_log(LOG_WARNING
, "Module '%s' does not provide a description.\n", mod
->resource
);
617 if (!mod
->info
->key
) {
618 ast_log(LOG_WARNING
, "Module '%s' does not provide a license key.\n", mod
->resource
);
622 if (verify_key((unsigned char *) mod
->info
->key
)) {
623 ast_log(LOG_WARNING
, "Module '%s' did not provide a valid license key.\n", mod
->resource
);
627 if (!ast_test_flag(mod
->info
, AST_MODFLAG_BUILDSUM
)) {
628 ast_log(LOG_WARNING
, "Module '%s' was not compiled against a recent version of Asterisk and may cause instability.\n", mod
->resource
);
629 } else if (!ast_strlen_zero(mod
->info
->buildopt_sum
) &&
630 strcmp(buildopt_sum
, mod
->info
->buildopt_sum
)) {
631 ast_log(LOG_WARNING
, "Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod
->resource
);
632 ast_log(LOG_WARNING
, "Module '%s' will not be initialized as it may cause instability.\n", mod
->resource
);
639 static enum ast_module_load_result
load_resource(const char *resource_name
, unsigned int global_symbols_only
)
641 struct ast_module
*mod
;
642 enum ast_module_load_result res
= AST_MODULE_LOAD_SUCCESS
;
645 if ((mod
= find_resource(resource_name
, 0))) {
646 if (mod
->flags
.running
) {
647 ast_log(LOG_WARNING
, "Module '%s' already exists.\n", resource_name
);
648 return AST_MODULE_LOAD_DECLINE
;
650 if (global_symbols_only
&& !ast_test_flag(mod
->info
, AST_MODFLAG_GLOBAL_SYMBOLS
))
651 return AST_MODULE_LOAD_SKIP
;
653 #ifdef LOADABLE_MODULES
654 if (!(mod
= load_dynamic_module(resource_name
, global_symbols_only
))) {
655 /* don't generate a warning message during load_modules() */
656 if (!global_symbols_only
) {
657 ast_log(LOG_WARNING
, "Module '%s' could not be loaded.\n", resource_name
);
658 return AST_MODULE_LOAD_DECLINE
;
660 return AST_MODULE_LOAD_SKIP
;
664 ast_log(LOG_WARNING
, "Module '%s' could not be loaded.\n", resource_name
);
665 return AST_MODULE_LOAD_DECLINE
;
669 if (inspect_module(mod
)) {
670 ast_log(LOG_WARNING
, "Module '%s' could not be loaded.\n", resource_name
);
671 #ifdef LOADABLE_MODULES
672 unload_dynamic_module(mod
);
674 return AST_MODULE_LOAD_DECLINE
;
677 mod
->flags
.declined
= 0;
680 res
= mod
->info
->load();
683 case AST_MODULE_LOAD_SUCCESS
:
684 if (!ast_fully_booted
) {
686 ast_verbose("%s => (%s)\n", resource_name
, term_color(tmp
, mod
->info
->description
, COLOR_BROWN
, COLOR_BLACK
, sizeof(tmp
)));
687 if (ast_opt_console
&& !option_verbose
)
691 ast_verbose(VERBOSE_PREFIX_1
"Loaded %s => (%s)\n", resource_name
, mod
->info
->description
);
694 mod
->flags
.running
= 1;
696 ast_update_use_count();
698 case AST_MODULE_LOAD_DECLINE
:
699 mod
->flags
.declined
= 1;
701 case AST_MODULE_LOAD_FAILURE
:
703 case AST_MODULE_LOAD_SKIP
:
704 /* modules should never return this value */
711 int ast_load_resource(const char *resource_name
)
713 AST_LIST_LOCK(&module_list
);
714 load_resource(resource_name
, 0);
715 AST_LIST_UNLOCK(&module_list
);
720 struct load_order_entry
{
722 AST_LIST_ENTRY(load_order_entry
) entry
;
725 AST_LIST_HEAD_NOLOCK(load_order
, load_order_entry
);
727 static struct load_order_entry
*add_to_load_order(const char *resource
, struct load_order
*load_order
)
729 struct load_order_entry
*order
;
731 AST_LIST_TRAVERSE(load_order
, order
, entry
) {
732 if (!resource_name_match(order
->resource
, resource
))
736 if (!(order
= ast_calloc(1, sizeof(*order
))))
739 order
->resource
= ast_strdup(resource
);
740 AST_LIST_INSERT_TAIL(load_order
, order
, entry
);
745 int load_modules(unsigned int preload_only
)
747 struct ast_config
*cfg
;
748 struct ast_module
*mod
;
749 struct load_order_entry
*order
;
750 struct ast_variable
*v
;
751 unsigned int load_count
;
752 struct load_order load_order
;
754 #ifdef LOADABLE_MODULES
755 struct dirent
*dirent
;
759 /* all embedded modules have registered themselves by now */
763 ast_verbose("Asterisk Dynamic Loader Starting:\n");
765 AST_LIST_HEAD_INIT_NOLOCK(&load_order
);
767 AST_LIST_LOCK(&module_list
);
769 if (!(cfg
= ast_config_load(AST_MODULE_CONFIG
))) {
770 ast_log(LOG_WARNING
, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG
);
774 /* first, find all the modules we have been explicitly requested to load */
775 for (v
= ast_variable_browse(cfg
, "modules"); v
; v
= v
->next
) {
776 if (!strcasecmp(v
->name
, preload_only
? "preload" : "load"))
777 add_to_load_order(v
->value
, &load_order
);
780 /* check if 'autoload' is on */
781 if (!preload_only
&& ast_true(ast_variable_retrieve(cfg
, "modules", "autoload"))) {
782 /* if so, first add all the embedded modules that are not already running to the load order */
783 AST_LIST_TRAVERSE(&module_list
, mod
, entry
) {
784 /* if it's not embedded, skip it */
788 if (mod
->flags
.running
)
791 order
= add_to_load_order(mod
->resource
, &load_order
);
794 #ifdef LOADABLE_MODULES
795 /* if we are allowed to load dynamic modules, scan the directory for
796 for all available modules and add them as well */
797 if ((dir
= opendir(ast_config_AST_MODULE_DIR
))) {
798 while ((dirent
= readdir(dir
))) {
799 int ld
= strlen(dirent
->d_name
);
801 /* Must end in .so to load it. */
806 if (strcasecmp(dirent
->d_name
+ ld
- 3, ".so"))
809 /* if there is already a module by this name in the module_list,
811 if (find_resource(dirent
->d_name
, 0))
814 add_to_load_order(dirent
->d_name
, &load_order
);
820 ast_log(LOG_WARNING
, "Unable to open modules directory '%s'.\n",
821 ast_config_AST_MODULE_DIR
);
826 /* now scan the config for any modules we are prohibited from loading and
827 remove them from the load order */
828 for (v
= ast_variable_browse(cfg
, "modules"); v
; v
= v
->next
) {
829 if (strcasecmp(v
->name
, "noload"))
832 AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order
, order
, entry
) {
833 if (!resource_name_match(order
->resource
, v
->value
)) {
834 AST_LIST_REMOVE_CURRENT(&load_order
, entry
);
835 free(order
->resource
);
839 AST_LIST_TRAVERSE_SAFE_END
;
842 /* we are done with the config now, all the information we need is in the
844 ast_config_destroy(cfg
);
847 AST_LIST_TRAVERSE(&load_order
, order
, entry
)
851 ast_log(LOG_NOTICE
, "%d modules will be loaded.\n", load_count
);
853 /* first, load only modules that provide global symbols */
854 AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order
, order
, entry
) {
855 switch (load_resource(order
->resource
, 1)) {
856 case AST_MODULE_LOAD_SUCCESS
:
857 case AST_MODULE_LOAD_DECLINE
:
858 AST_LIST_REMOVE_CURRENT(&load_order
, entry
);
859 free(order
->resource
);
862 case AST_MODULE_LOAD_FAILURE
:
865 case AST_MODULE_LOAD_SKIP
:
866 /* try again later */
870 AST_LIST_TRAVERSE_SAFE_END
;
872 /* now load everything else */
873 AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order
, order
, entry
) {
874 switch (load_resource(order
->resource
, 0)) {
875 case AST_MODULE_LOAD_SUCCESS
:
876 case AST_MODULE_LOAD_DECLINE
:
877 AST_LIST_REMOVE_CURRENT(&load_order
, entry
);
878 free(order
->resource
);
881 case AST_MODULE_LOAD_FAILURE
:
884 case AST_MODULE_LOAD_SKIP
:
885 /* should not happen */
889 AST_LIST_TRAVERSE_SAFE_END
;
892 while ((order
= AST_LIST_REMOVE_HEAD(&load_order
, entry
))) {
893 free(order
->resource
);
897 AST_LIST_UNLOCK(&module_list
);
902 void ast_update_use_count(void)
904 /* Notify any module monitors that the use count for a
905 resource has changed */
906 struct loadupdate
*m
;
908 AST_LIST_LOCK(&updaters
);
909 AST_LIST_TRAVERSE(&updaters
, m
, entry
)
911 AST_LIST_UNLOCK(&updaters
);
914 int ast_update_module_list(int (*modentry
)(const char *module
, const char *description
, int usecnt
, const char *like
),
917 struct ast_module
*cur
;
919 int total_mod_loaded
= 0;
921 if (AST_LIST_TRYLOCK(&module_list
))
924 AST_LIST_TRAVERSE(&module_list
, cur
, entry
) {
925 total_mod_loaded
+= modentry(cur
->resource
, cur
->info
->description
, cur
->usecount
, like
);
929 AST_LIST_UNLOCK(&module_list
);
931 return total_mod_loaded
;
934 int ast_loader_register(int (*v
)(void))
936 struct loadupdate
*tmp
;
938 if (!(tmp
= ast_malloc(sizeof(*tmp
))))
942 AST_LIST_LOCK(&updaters
);
943 AST_LIST_INSERT_HEAD(&updaters
, tmp
, entry
);
944 AST_LIST_UNLOCK(&updaters
);
949 int ast_loader_unregister(int (*v
)(void))
951 struct loadupdate
*cur
;
953 AST_LIST_LOCK(&updaters
);
954 AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters
, cur
, entry
) {
955 if (cur
->updater
== v
) {
956 AST_LIST_REMOVE_CURRENT(&updaters
, entry
);
960 AST_LIST_TRAVERSE_SAFE_END
;
961 AST_LIST_UNLOCK(&updaters
);
966 struct ast_module
*ast_module_ref(struct ast_module
*mod
)
968 ast_atomic_fetchadd_int(&mod
->usecount
, +1);
969 ast_update_use_count();
974 void ast_module_unref(struct ast_module
*mod
)
976 ast_atomic_fetchadd_int(&mod
->usecount
, -1);
977 ast_update_use_count();