2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Module Loader
23 * \author Mark Spencer <markster@digium.com>
27 #define MOD_LOADER /* prevent some module-specific stuff from being compiled */
30 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
38 #include "asterisk/linkedlists.h"
39 #include "asterisk/module.h"
40 #include "asterisk/options.h"
41 #include "asterisk/config.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/term.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/enum.h"
48 #include "asterisk/rtp.h"
49 #include "asterisk/http.h"
50 #include "asterisk/lock.h"
52 #include "asterisk/dlfcn-compat.h"
56 #include "asterisk/md5.h"
57 #include "asterisk/utils.h"
63 static int modlistver
= 0; /* increase whenever the list changes, to protect reload */
65 static unsigned char expected_key
[] =
66 { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
67 0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
70 * Modules can be in a number of different states, as below:
71 * MS_FAILED attempt to load failed. This is final.
72 * MS_NEW just added to the list, symbols unresolved.
73 * MS_RESOLVED all symbols resolved, but supplier modules not active yet.
74 * MS_CANLOAD all symbols resolved and suppliers are all active
75 * (or we are in a cyclic dependency and we are breaking a loop)
76 * MS_ACTIVE load() returned successfully.
78 enum st_t
{ /* possible states of a module */
79 MS_FAILED
= 0, /*!< cannot load */
80 MS_NEW
= 1, /*!< nothing known */
81 MS_RESOLVED
= 2, /*!< all required resolved */
82 MS_CANLOAD
= 3, /*!< as above, plus cyclic depend.*/
83 MS_ACTIVE
= 4, /*!< all done */
87 * All module symbols are in module_symbols.
88 * Modules are then linked in a list of struct module,
89 * whereas updaters are in a list of struct loadupdate.
91 * Both lists (basically, the entire loader) are protected by
92 * the lock in module_list.
94 * A second lock, reloadlock, is used to prevent concurrent reloads
97 AST_LIST_ENTRY(module
) next
;
98 struct module_symbols
*cb
;
99 void *lib
; /* the shared lib */
103 int export_refcount
; /* how many users of exported symbols */
107 AST_LIST_ENTRY(loadupdate
) next
;
108 int (*updater
)(void);
111 static AST_LIST_HEAD_STATIC(module_list
, module
);
112 static AST_LIST_HEAD_STATIC(updaters
, loadupdate
);
113 AST_MUTEX_DEFINE_STATIC(reloadlock
);
116 * helper localuser routines.
117 * All of these routines are extremely expensive, so the use of
118 * macros is totally unnecessary from the point of view of performance:
119 * the extra function call will be totally negligible in all cases.
122 struct localuser
*ast_localuser_add(struct module_symbols
*me
,
123 struct ast_channel
*chan
)
125 struct localuser
*u
= ast_calloc(1, sizeof(*u
));
129 ast_mutex_lock(&me
->lock
);
130 u
->next
= me
->lu_head
;
132 ast_mutex_unlock(&me
->lock
);
133 ast_atomic_fetchadd_int(&me
->usecnt
, +1);
134 ast_update_use_count();
138 void ast_localuser_remove(struct module_symbols
*me
, struct localuser
*u
)
140 struct localuser
*x
, *prev
= NULL
;
141 ast_mutex_lock(&me
->lock
);
142 /* unlink from the list */
143 for (x
= me
->lu_head
; x
; prev
= x
, x
= x
->next
) {
146 prev
->next
= x
->next
;
148 me
->lu_head
= x
->next
;
152 ast_mutex_unlock(&me
->lock
);
153 ast_atomic_fetchadd_int(&me
->usecnt
, -1);
155 ast_update_use_count();
158 void ast_hangup_localusers(struct module_symbols
*me
)
160 struct localuser
*u
, *next
;
161 ast_mutex_lock(&me
->lock
);
162 for (u
= me
->lu_head
; u
; u
= next
) {
164 ast_softhangup(u
->chan
, AST_SOFTHANGUP_APPUNLOAD
);
165 ast_atomic_fetchadd_int(&me
->usecnt
, -1);
168 ast_mutex_unlock(&me
->lock
);
169 ast_update_use_count();
172 /*--- new-style loader routines ---*/
175 * For backward compatibility, we have 3 types of loadable modules:
177 * MOD_0 these are the 'old style' modules, which export a number
178 * of callbacks, and their full interface, as globally visible
179 * symbols. The module needs to be loaded with RTLD_LAZY and
180 * RTLD_GLOBAL to make symbols visible to other modules, and
181 * to avoid load failures due to cross dependencies.
183 * MOD_1 The generic callbacks are all into a structure, mod_data.
185 * MOD_2 this is the 'new style' format for modules. The module must
186 * explictly declare which simbols are exported and which
187 * symbols from other modules are used, and the code in this
188 * loader will implement appropriate checks to load the modules
189 * in the correct order. Also this allows to load modules
190 * with RTLD_NOW and RTLD_LOCAL so there is no chance of run-time
191 * bugs due to unresolved symbols or name conflicts.
195 * helper routine to print the symbolic name associated to a state
197 static const char *st_name(enum st_t state
)
199 /* try to resolve required symbols */
202 #define ST(x) case x: st = # x; break;
216 * Fetch/release an exported symbol - modify export_refcount by delta
217 * \param delta 1 to fetch a symbol, -1 to release it.
218 * \return on success, return symbol value.
219 * \note Note, modules in MS_FAIL will never match in a 'get' request.
220 * If src is non-NULL, on exit *src points to the source module.
222 * Must be called with the lock held.
224 static void *module_symbol_helper(const char *name
,
225 int delta
, struct module
**src
)
230 AST_LIST_TRAVERSE(&module_list
, m
, next
) {
231 struct symbol_entry
*es
;
232 if (delta
> 0 && m
->state
== MS_FAILED
)
233 continue; /* cannot 'get' a symbol from a failed module */
234 for (es
= m
->cb
->exported_symbols
; ret
== NULL
&& es
&& es
->name
; es
++) {
235 if (!strcmp(es
->name
, name
)) {
237 m
->export_refcount
+= delta
;
247 ast_log(LOG_WARNING
, "symbol %s not found\n", name
);
251 static void *release_module_symbol(const char *name
)
253 return module_symbol_helper(name
, -1, NULL
);
256 static void *get_module_symbol(const char *name
, struct module
**src
)
258 return module_symbol_helper(name
, +1, src
);
262 * \brief Release refcounts to all imported symbols,
263 * and change module state to MS_FAILED.
265 static void release_module(struct module
*m
)
267 struct symbol_entry
*s
;
269 for (s
= m
->cb
->required_symbols
; s
&& s
->name
!= NULL
; s
++) {
270 if (s
->value
!= NULL
) {
271 release_module_symbol(s
->name
);
275 m
->state
= MS_FAILED
;
278 /*! \brief check that no NULL symbols are exported - the algorithms rely on that. */
279 static int check_exported(struct module
*m
)
281 struct symbol_entry
*es
= m
->cb
->exported_symbols
;
286 ast_log(LOG_WARNING
, "module %s exports the following symbols\n",
288 for (; es
->name
; es
++) {
289 void **p
= es
->value
;
292 ast_log(LOG_WARNING
, "\taddr %p size %8d %s\n",
293 es
->value
, es
->size
, es
->name
);
294 for (i
= 0; i
< es
->size
/ sizeof(void *); i
++, p
++) {
296 ast_log(LOG_WARNING
, "\t *** null field at offset %d\n", i
);
305 * \brief Resolve symbols and change state accordingly.
306 * \return Return 1 if state changed, 0 otherwise.
307 * \note If MS_FAILED, MS_ACTIVE or MS_CANLOAD there is nothing to do.
308 * If a symbol cannot be resolved (no supplier or supplier in MS_FAIL),
309 * move to MS_FAIL and release all symbols;
310 * If all suppliers are MS_ACTIVE, move to MS_CANLOAD
311 * otherwise move to MS_RESOLVED.
313 static int resolve(struct module
*m
)
315 struct symbol_entry
*s
;
317 if (m
->state
== MS_FAILED
|| m
->state
== MS_ACTIVE
|| m
->state
== MS_CANLOAD
)
318 return 0; /* already decided what to do */
319 /* now it's either MS_NEW or MS_RESOLVED.
320 * Be optimistic and put it in MS_CANLOAD, then try to
321 * resolve and verify symbols, and downgrade as appropriate.
323 m
->state
= MS_CANLOAD
;
324 for (s
= m
->cb
->required_symbols
; s
&& s
->name
!= NULL
; s
++) {
325 void **p
= (void **)(s
->value
);
327 if (*p
== NULL
) /* symbol not resolved yet */
328 *p
= get_module_symbol(s
->name
, &s
->src
);
329 if (*p
== NULL
|| s
->src
->state
== MS_FAILED
) { /* fail */
331 "Unresolved symbol %s for module %s\n",
332 s
->name
, m
->resource
);
333 release_module(m
); /* and set to MS_FAILED */
336 if (s
->src
->state
!= MS_ACTIVE
)
337 m
->state
= MS_RESOLVED
; /* downgrade */
343 * \brief Fixup references and load modules according to their dependency order.
344 * Called when new modules are added to the list.
345 * The algorithm is as follows:
346 * - all modules MS_FAILED are changed to MS_NEW, in case something
347 * happened that could help them.
348 * - first try to resolve symbols. If successful, change the
349 * module's state to MS_RESOLVED otherwise to MS_FAILED
350 * - repeat on all modules until there is progress:
351 * - if it is MS_ACTIVE or MS_FAILED, continue (no progress)
352 * - if one has all required modules in MS_ACTIVE, try to load it.
353 * If successful it becomes MS_ACTIVE itself, otherwise
354 * MS_FAILED and releases all symbols.
355 * In any case, we have progress.
356 * - if one of the dependencies is MS_FAILED, release and set to
357 * MS_FAILED here too. We have progress.
358 * - if we have no progress there is a cyclic dependency.
359 * Take first and change to MS_CANLOAD, i.e. as if all required are
360 * MS_ACTIVE. we have progress, so repeat.
362 * - must be called with lock held
363 * - recursive calls simply return success.
365 static int fixup(const char *caller
)
368 int total
= 0, new = 0, cycle
= 0;
369 static int in_fixup
= 0; /* disable recursive calls */
374 AST_LIST_TRAVERSE(&module_list
, m
, next
) {
376 if (m
->state
== MS_FAILED
)
378 if (m
->state
== MS_NEW
)
380 /* print some debugging info for new modules */
381 if (m
->state
== MS_NEW
&&
382 (m
->cb
->exported_symbols
|| m
->cb
->required_symbols
))
384 "module %-30s exports %p requires %p state %s(%d)\n",
385 m
->resource
, m
->cb
->exported_symbols
,
386 m
->cb
->required_symbols
,
387 st_name(m
->state
), m
->state
);
389 ast_log(LOG_DEBUG
, "---- fixup (%s): %d modules, %d new ---\n",
392 int again
= 0; /* set if we need another round */
394 ast_log(LOG_DEBUG
, "---- fixup: cycle %d ---\n", cycle
);
395 AST_LIST_TRAVERSE(&module_list
, m
, next
) {
397 again
= 1; /* something changed */
398 if (m
->state
!= MS_CANLOAD
) /* for now, done with this module */
400 /* try to run the load routine */
401 if (m
->cb
->load_module(m
)) { /* error */
402 ast_log(LOG_WARNING
, "load_module %s fail\n",
404 release_module(m
); /* and set to MS_FAIL */
406 ast_log(LOG_WARNING
, "load_module %s success\n",
408 m
->state
= MS_ACTIVE
;
410 again
= 1; /* something has changed */
412 /* Modules in MS_RESOLVED mean a possible cyclic dependency.
413 * Break the indecision by setting one to CANLOAD, and repeat.
415 AST_LIST_TRAVERSE(&module_list
, m
, next
) {
416 if (m
->state
== MS_RESOLVED
) {
417 m
->state
= MS_CANLOAD
;
422 if (!again
) /* we are done */
425 ast_log(LOG_DEBUG
, "---- fixup complete ---\n");
430 /* test routines to see which modules depend on global symbols
431 * exported by other modules.
433 static void check_symbols(void)
436 DIR *mods
= opendir(ast_config_AST_MODULE_DIR
);
440 ast_log(LOG_WARNING
, "module dir <%s>\n", ast_config_AST_MODULE_DIR
);
443 while((d
= readdir(mods
))) {
444 int ld
= strlen(d
->d_name
);
445 /* Must end in .so to load it. */
446 if (ld
<= 3 || strcasecmp(d
->d_name
+ ld
- 3, ".so"))
448 snprintf(buf
, sizeof(buf
), "%s/%s", ast_config_AST_MODULE_DIR
, d
->d_name
);
449 lib
= dlopen(buf
, RTLD_NOW
| RTLD_LOCAL
);
451 ast_log(LOG_WARNING
, "(notice only) module %s error %s\n", d
->d_name
, dlerror());
456 /*--- end new-style routines ---*/
459 * In addition to modules, the reload command handles some extra keywords
460 * which are listed here together with the corresponding handlers.
461 * This table is also used by the command completion code.
463 static struct reload_classes_t
{
465 int (*reload_fn
)(void);
466 } reload_classes
[] = { /* list in alpha order, longest match first */
467 { "cdr", ast_cdr_engine_reload
},
468 { "dnsmgr", dnsmgr_reload
},
469 { "extconfig", read_config_maps
},
470 { "enum", ast_enum_reload
},
471 { "manager", reload_manager
},
472 { "rtp", ast_rtp_reload
},
473 { "http", ast_http_reload
},
477 static int printdigest(const unsigned char *d
)
480 char buf
[256]; /* large enough so we don't have to worry */
482 for (pos
= 0, x
=0; x
<16; x
++)
483 pos
+= sprintf(buf
+ pos
, " %02x", *d
++);
484 ast_log(LOG_DEBUG
, "Unexpected signature:%s\n", buf
);
488 static int key_matches(const unsigned char *key1
, const unsigned char *key2
)
491 for (x
=0; x
<16; x
++) {
492 if (key1
[x
] != key2
[x
]) /* mismatch - fail now. */
498 static int verify_key(const unsigned char *key
)
501 unsigned char digest
[16];
503 MD5Update(&c
, key
, strlen((char *)key
));
504 MD5Final(digest
, &c
);
505 if (key_matches(expected_key
, digest
))
511 int ast_unload_resource(const char *resource_name
, enum unload_mode force
)
516 if (AST_LIST_LOCK(&module_list
)) /* XXX should fail here ? */
517 ast_log(LOG_WARNING
, "Failed to lock\n");
518 AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list
, cur
, next
) {
519 struct module_symbols
*m
= cur
->cb
;
521 if (strcasecmp(cur
->resource
, resource_name
)) /* not us */
523 if (m
->usecnt
> 0 || m
->flags
& NO_UNLOAD
) {
525 ast_log(LOG_WARNING
, "Warning: Forcing removal of module %s with use count %d\n", resource_name
, res
);
527 ast_log(LOG_WARNING
, "Soft unload failed, '%s' has use count %d\n", resource_name
, res
);
532 ast_hangup_localusers(m
);
533 res
= m
->unload_module(m
);
535 ast_log(LOG_WARNING
, "Firm unload failed for %s\n", resource_name
);
536 if (force
<= AST_FORCE_FIRM
) {
540 ast_log(LOG_WARNING
, "** Dangerous **: Unloading resource anyway, at user request\n");
542 release_module(cur
); /* XXX */
543 AST_LIST_REMOVE_CURRENT(&module_list
, next
);
548 AST_LIST_TRAVERSE_SAFE_END
;
551 AST_LIST_UNLOCK(&module_list
);
552 if (!error
) /* XXX maybe within the lock ? */
553 ast_update_use_count();
557 char *ast_module_helper(const char *line
, const char *word
, int pos
, int state
, int rpos
, int needsreload
)
560 int i
, which
=0, l
= strlen(word
);
565 AST_LIST_LOCK(&module_list
);
566 AST_LIST_TRAVERSE(&module_list
, cur
, next
) {
567 if (!strncasecmp(word
, cur
->resource
, l
) && (cur
->cb
->reload
|| !needsreload
) &&
569 ret
= strdup(cur
->resource
);
573 AST_LIST_UNLOCK(&module_list
);
575 for (i
=0; !ret
&& reload_classes
[i
].name
; i
++) {
576 if (!strncasecmp(word
, reload_classes
[i
].name
, l
) && ++which
> state
)
577 ret
= strdup(reload_classes
[i
].name
);
583 int ast_module_reload(const char *name
)
586 int res
= 0; /* return value. 0 = not found, others, see below */
589 if (ast_mutex_trylock(&reloadlock
)) {
590 ast_verbose("The previous reload command didn't finish yet\n");
591 return -1; /* reload already in progress */
593 /* Call "predefined" reload here first */
594 for (i
= 0; reload_classes
[i
].name
; i
++) {
595 if (!name
|| !strcasecmp(name
, reload_classes
[i
].name
)) {
596 reload_classes
[i
].reload_fn(); /* XXX should check error ? */
597 res
= 2; /* found and reloaded */
600 ast_lastreloadtime
= time(NULL
);
602 AST_LIST_LOCK(&module_list
);
603 oldversion
= modlistver
;
604 AST_LIST_TRAVERSE(&module_list
, cur
, next
) {
605 struct module_symbols
*m
= cur
->cb
;
606 if (name
&& strcasecmp(name
, cur
->resource
)) /* not ours */
608 if (!m
->reload
) { /* cannot be reloaded */
609 if (res
< 1) /* store result if possible */
610 res
= 1; /* 1 = no reload() method */
613 /* drop the lock and try a reload. if successful, break */
614 AST_LIST_UNLOCK(&module_list
);
616 if (option_verbose
> 2)
617 ast_verbose(VERBOSE_PREFIX_3
"Reloading module '%s' (%s)\n", cur
->resource
, m
->description());
619 AST_LIST_LOCK(&module_list
);
620 if (oldversion
!= modlistver
) /* something changed, abort */
623 AST_LIST_UNLOCK(&module_list
);
624 ast_mutex_unlock(&reloadlock
);
628 static int resource_exists(const char *resource
, int do_lock
)
631 if (do_lock
&& AST_LIST_LOCK(&module_list
))
632 ast_log(LOG_WARNING
, "Failed to lock\n");
633 AST_LIST_TRAVERSE(&module_list
, cur
, next
) {
634 if (!strcasecmp(resource
, cur
->resource
))
638 AST_LIST_UNLOCK(&module_list
);
642 /* lookup a symbol with or without leading '_', accept either form in input */
643 static void *find_symbol(struct module
*m
, const char *name
, int verbose
)
650 n1
= alloca(strlen(name
) + 2); /* room for leading '_' and final '\0' */
653 s
= dlsym(m
->lib
, n1
+1); /* try without '_' */
655 s
= dlsym(m
->lib
, n1
);
656 if (verbose
&& s
== NULL
)
657 ast_log(LOG_WARNING
, "No symbol '%s' in module '%s\n",
662 /* XXX cfg is only used for !res_* and #ifdef RTLD_GLOBAL */
663 static struct module
* __load_resource(const char *resource_name
,
664 const struct ast_config
*cfg
)
670 struct module_symbols
*m
= NULL
;
671 int flags
= RTLD_NOW
;
676 #define RTLD_GLOBAL 0 /* so it is a No-op */
678 if (strncasecmp(resource_name
, "res_", 4) && cfg
) {
679 char *val
= ast_variable_retrieve(cfg
, "global", resource_name
);
680 if (val
&& ast_true(val
))
681 flags
|= RTLD_GLOBAL
;
683 /* Resource modules are always loaded global and lazy */
684 flags
= (RTLD_GLOBAL
| RTLD_LAZY
);
687 if (AST_LIST_LOCK(&module_list
))
688 ast_log(LOG_WARNING
, "Failed to lock\n");
689 if (resource_exists(resource_name
, 0)) {
690 ast_log(LOG_WARNING
, "Module '%s' already exists\n", resource_name
);
691 AST_LIST_UNLOCK(&module_list
);
694 if (!(cur
= ast_calloc(1, sizeof(*cur
)))) {
695 AST_LIST_UNLOCK(&module_list
);
698 ast_copy_string(cur
->resource
, resource_name
, sizeof(cur
->resource
));
699 if (resource_name
[0] == '/')
700 ast_copy_string(fn
, resource_name
, sizeof(fn
));
702 snprintf(fn
, sizeof(fn
), "%s/%s", ast_config_AST_MODULE_DIR
, resource_name
);
704 /* open in a sane way */
705 cur
->lib
= dlopen(fn
, RTLD_NOW
| RTLD_LOCAL
);
707 ((m
= find_symbol(cur
, "mod_data", 0)) == NULL
|| (m
->flags
& MOD_MASK
) == MOD_0
)) {
708 /* old-style module, close and reload with standard flags */
713 if (cur
->lib
== NULL
) /* try reopen with the old style */
714 cur
->lib
= dlopen(fn
, flags
);
717 ast_log(LOG_WARNING
, "%s\n", dlerror());
719 AST_LIST_UNLOCK(&module_list
);
722 if (m
== NULL
) /* MOD_0 modules may still have a mod_data entry */
723 m
= find_symbol(cur
, "mod_data", 0);
724 if (m
!= NULL
) { /* new style module */
725 cur
->cb
= m
; /* use the mod_data from the module itself */
726 errors
= check_exported(cur
);
728 ast_log(LOG_WARNING
, "missing mod_data for %s\n",
734 if (!m
->unload_module
&& !(m
->flags
& NO_UNLOAD
) )
740 if (!m
->key
|| !(key
= (unsigned char *) m
->key())) {
741 ast_log(LOG_WARNING
, "Key routine returned NULL in module %s\n", fn
);
745 if (key
&& verify_key(key
)) {
746 ast_log(LOG_WARNING
, "Unexpected key returned by module %s\n", fn
);
750 ast_log(LOG_WARNING
, "%d error%s loading module %s, aborted\n", errors
, (errors
!= 1) ? "s" : "", fn
);
753 AST_LIST_UNLOCK(&module_list
);
756 /* init mutex and usecount */
757 ast_mutex_init(&cur
->cb
->lock
);
758 cur
->cb
->lu_head
= NULL
;
760 if (!ast_fully_booted
) {
762 ast_verbose( " => (%s)\n", term_color(tmp
, m
->description(), COLOR_BROWN
, COLOR_BLACK
, sizeof(tmp
)));
763 if (ast_opt_console
&& !option_verbose
)
767 ast_verbose(VERBOSE_PREFIX_1
"Loaded %s => (%s)\n", fn
, m
->description());
770 AST_LIST_INSERT_TAIL(&module_list
, cur
, next
);
771 /* add module to end of module_list chain
772 so reload commands will be issued in same order modules were loaded */
775 if ( (m
->flags
& MOD_MASK
) == MOD_2
) {
776 ast_log(LOG_WARNING
, "new-style module %s, deferring load()\n",
780 cur
->state
= MS_CANLOAD
;
781 /* XXX make sure the usecount is 1 before releasing the lock */
782 AST_LIST_UNLOCK(&module_list
);
784 if (cur
->state
== MS_CANLOAD
&& (res
= m
->load_module(m
))) {
785 ast_log(LOG_WARNING
, "%s: load_module failed, returning %d\n", resource_name
, res
);
786 ast_unload_resource(resource_name
, 0);
789 cur
->state
= MS_ACTIVE
;
790 ast_update_use_count();
795 * \brief load a single module (API call).
796 * (recursive calls from load_module() succeed.
797 * \return Returns 0 on success, -1 on error.
799 int ast_load_resource(const char *resource_name
)
801 int o
= option_verbose
;
802 struct ast_config
*cfg
= NULL
;
805 option_verbose
= 0; /* Keep the module file parsing silent */
806 cfg
= ast_config_load(AST_MODULE_CONFIG
);
807 option_verbose
= o
; /* restore verbosity */
808 m
= __load_resource(resource_name
, cfg
);
810 ast_config_destroy(cfg
);
816 * load a single module (API call).
817 * (recursive calls from load_module() succeed.
819 int ast_load_resource(const char *resource_name
)
824 ast_mutex_lock(&modlock
);
825 m
= __load_resource(resource_name
, 0);
826 fixup(resource_name
);
827 ret
= (m
->state
== MS_FAILED
) ? -1 : 0;
828 ast_mutex_unlock(&modlock
);
833 /*! \brief if enabled, log and output on console the module's name, and try load it */
834 static int print_and_load(const char *s
, struct ast_config
*cfg
)
838 if (option_debug
&& !option_verbose
)
839 ast_log(LOG_DEBUG
, "Loading module %s\n", s
);
840 if (option_verbose
) {
841 ast_verbose(VERBOSE_PREFIX_1
"[%s]",
842 term_color(tmp
, s
, COLOR_BRWHITE
, 0, sizeof(tmp
)));
845 if (__load_resource(s
, cfg
))
846 return 0; /* success */
847 ast_log(LOG_WARNING
, "Loading module %s failed!\n", s
);
851 static const char *loadorder
[] =
859 int load_modules(const int preload_only
)
861 struct ast_config
*cfg
;
864 if (option_verbose
) {
865 ast_verbose(preload_only
?
866 "Asterisk Dynamic Loader loading preload modules:\n" :
867 "Asterisk Dynamic Loader Starting:\n");
873 cfg
= ast_config_load(AST_MODULE_CONFIG
);
876 const char *cmd
= preload_only
? "preload" : "load";
877 struct ast_variable
*v
;
878 /* Load explicitly defined modules */
879 for (v
= ast_variable_browse(cfg
, "modules"); v
; v
= v
->next
) {
880 if (strcasecmp(v
->name
, cmd
)) /* not what we are looking for */
882 if (print_and_load(v
->value
, cfg
)) { /* XXX really fatal ? */
883 ast_config_destroy(cfg
);
892 if (cfg
&& !ast_true(ast_variable_retrieve(cfg
, "modules", "autoload")))
896 * Load all modules. To help resolving dependencies, we load modules
897 * in the order defined by loadorder[], with the final step for
898 * all modules with other prefixes.
899 * (XXX the new loader does not need this).
902 for (x
=0; x
<sizeof(loadorder
) / sizeof(loadorder
[0]); x
++) {
904 DIR *mods
= opendir(ast_config_AST_MODULE_DIR
);
905 const char *base
= loadorder
[x
];
906 int lx
= base
? strlen(base
) : 0;
910 ast_log(LOG_WARNING
, "Unable to open modules directory %s.\n",
911 ast_config_AST_MODULE_DIR
);
912 break; /* suffices to try once! */
914 while((d
= readdir(mods
))) {
915 int ld
= strlen(d
->d_name
);
916 /* Must end in .so to load it. */
917 if (ld
> 3 && (!base
|| !strncasecmp(d
->d_name
, base
, lx
)) &&
918 !strcasecmp(d
->d_name
+ ld
- 3, ".so") &&
919 !resource_exists(d
->d_name
, 1)) {
920 /* It's a shared library, check if we are allowed to load it
921 * (very inefficient, but oh well).
924 struct ast_variable
*v
;
925 for (v
= ast_variable_browse(cfg
, "modules"); v
; v
= v
->next
) {
926 if (!strcasecmp(v
->name
, "noload") &&
927 !strcasecmp(v
->value
, d
->d_name
))
931 if (option_verbose
) {
932 ast_verbose( VERBOSE_PREFIX_1
"[skipping %s]\n",
940 if (print_and_load(d
->d_name
, cfg
)) {
941 ast_config_destroy(cfg
);
949 fixup("load_modules");
950 ast_config_destroy(cfg
);
954 #include <errno.h> /* for errno... */
956 void ast_update_use_count(void)
958 /* Notify any module monitors that the use count for a
959 resource has changed */
960 struct loadupdate
*m
;
961 if (AST_LIST_LOCK(&module_list
))
962 ast_log(LOG_WARNING
, "Failed to lock, errno %d\n", errno
);
963 AST_LIST_TRAVERSE(&updaters
, m
, next
)
965 AST_LIST_UNLOCK(&module_list
);
968 int ast_update_module_list(int (*modentry
)(const char *module
, const char *description
, int usecnt
, const char *like
),
973 int total_mod_loaded
= 0;
975 if (ast_mutex_trylock(&module_list
.lock
))
977 AST_LIST_TRAVERSE(&module_list
, cur
, next
) {
978 total_mod_loaded
+= modentry(cur
->resource
, cur
->cb
->description(), cur
->cb
->usecnt
, like
);
981 AST_LIST_UNLOCK(&module_list
);
983 return total_mod_loaded
;
986 int ast_loader_register(int (*v
)(void))
988 /* XXX Should be more flexible here, taking > 1 verboser XXX */
989 struct loadupdate
*tmp
;
990 if (!(tmp
= ast_malloc(sizeof(*tmp
))))
993 if (AST_LIST_LOCK(&module_list
))
994 ast_log(LOG_WARNING
, "Failed to lock\n");
995 AST_LIST_INSERT_HEAD(&updaters
, tmp
, next
);
996 AST_LIST_UNLOCK(&module_list
);
1000 int ast_loader_unregister(int (*v
)(void))
1002 struct loadupdate
*cur
;
1004 if (AST_LIST_LOCK(&module_list
))
1005 ast_log(LOG_WARNING
, "Failed to lock\n");
1006 AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters
, cur
, next
) {
1007 if (cur
->updater
== v
) {
1008 AST_LIST_REMOVE_CURRENT(&updaters
, next
);
1012 AST_LIST_TRAVERSE_SAFE_END
;
1013 AST_LIST_UNLOCK(&module_list
);
1014 return cur
? 0 : -1;