2006-08-21 02:11:39 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
|
|
|
* Kevin P. Fleming <kpfleming@digium.com>
|
|
|
|
* Luigi Rizzo <rizzo@icir.org>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Module Loader
|
2006-09-15 05:00:27 +00:00
|
|
|
* \author Mark Spencer <markster@digium.com>
|
2006-08-21 02:11:39 +00:00
|
|
|
* \author Kevin P. Fleming <kpfleming@digium.com>
|
|
|
|
* \author Luigi Rizzo <rizzo@icir.org>
|
|
|
|
* - See ModMngMnt
|
|
|
|
*/
|
|
|
|
|
2012-06-15 16:20:16 +00:00
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2007-11-20 22:18:21 +00:00
|
|
|
#include "asterisk/_private.h"
|
2007-11-20 23:16:15 +00:00
|
|
|
#include "asterisk/paths.h" /* use ast_config_AST_MODULE_DIR */
|
2006-08-21 02:11:39 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "asterisk/linkedlists.h"
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/config.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/term.h"
|
2012-07-11 18:33:36 +00:00
|
|
|
#include "asterisk/acl.h"
|
2006-08-21 02:11:39 +00:00
|
|
|
#include "asterisk/manager.h"
|
|
|
|
#include "asterisk/cdr.h"
|
|
|
|
#include "asterisk/enum.h"
|
|
|
|
#include "asterisk/http.h"
|
|
|
|
#include "asterisk/lock.h"
|
2008-01-23 23:09:11 +00:00
|
|
|
#include "asterisk/features.h"
|
2008-03-05 16:23:44 +00:00
|
|
|
#include "asterisk/dsp.h"
|
2008-03-26 18:39:06 +00:00
|
|
|
#include "asterisk/udptl.h"
|
2009-06-09 16:22:04 +00:00
|
|
|
#include "asterisk/heap.h"
|
2010-01-27 18:29:49 +00:00
|
|
|
#include "asterisk/app.h"
|
2012-08-13 20:36:51 +00:00
|
|
|
#include "asterisk/test.h"
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
#include "asterisk/md5.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
|
2012-07-11 02:06:05 +00:00
|
|
|
/*** DOCUMENTATION
|
|
|
|
***/
|
|
|
|
|
2007-10-22 14:56:05 +00:00
|
|
|
#ifndef RTLD_NOW
|
2006-08-21 02:11:39 +00:00
|
|
|
#define RTLD_NOW 0
|
|
|
|
#endif
|
|
|
|
|
2007-11-17 09:51:45 +00:00
|
|
|
#ifndef RTLD_LOCAL
|
|
|
|
#define RTLD_LOCAL 0
|
|
|
|
#endif
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
struct ast_module_user {
|
|
|
|
struct ast_channel *chan;
|
|
|
|
AST_LIST_ENTRY(ast_module_user) entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
AST_LIST_HEAD(module_user_list, ast_module_user);
|
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
static const unsigned char expected_key[] =
|
2006-08-21 02:11:39 +00:00
|
|
|
{ 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
|
|
|
|
0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
|
|
|
|
|
2007-11-20 19:28:10 +00:00
|
|
|
static char buildopt_sum[33] = AST_BUILDOPT_SUM;
|
2007-11-16 16:56:59 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static unsigned int embedding = 1; /* we always start out by registering embedded modules,
|
|
|
|
since they are here before we dlopen() any
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct ast_module {
|
|
|
|
const struct ast_module_info *info;
|
|
|
|
void *lib; /* the shared lib, or NULL if embedded */
|
|
|
|
int usecount; /* the number of 'users' currently in this module */
|
|
|
|
struct module_user_list users; /* the list of users in the module */
|
2007-05-03 16:43:49 +00:00
|
|
|
struct {
|
After some study, thought, comparing, etc. I've backed out the previous universal mod to make ast_flags a 64 bit thing. Instead, I added a 64-bit version of ast_flags (ast_flags64), and 64-bit versions of the test-flag, set-flag, etc. macros, and an app_parse_options64 routine, and I use these in app_dial alone, to eliminate the 30-option limit it had grown to meet. There is room now for 32 more options and flags. I was heavily tempted to implement some of the other ideas that were presented, but this solution does not intro any new versions of dial, doesn't have a different API, has a minimal/zero impact on code outside of dial, and doesn't seriously (I hope) affect the code structure of dial. It's the best I can think of right now. My goal was NOT to rewrite dial. I leave that to a future, coordinated effort.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75983 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-19 23:24:27 +00:00
|
|
|
unsigned int running:1;
|
|
|
|
unsigned int declined:1;
|
2007-05-03 16:43:49 +00:00
|
|
|
} flags;
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_ENTRY(ast_module) entry;
|
|
|
|
char resource[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
static AST_LIST_HEAD_STATIC(module_list, ast_module);
|
|
|
|
|
2011-10-10 14:16:27 +00:00
|
|
|
const char *ast_module_name(const struct ast_module *mod)
|
|
|
|
{
|
|
|
|
if (!mod || !mod->info) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod->info->name;
|
|
|
|
}
|
|
|
|
|
2007-11-06 17:05:13 +00:00
|
|
|
/*
|
|
|
|
* module_list is cleared by its constructor possibly after
|
|
|
|
* we start accumulating embedded modules, so we need to
|
|
|
|
* use another list (without the lock) to accumulate them.
|
|
|
|
* Then we update the main list when embedding is done.
|
|
|
|
*/
|
|
|
|
static struct module_list embedded_module_list;
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
struct loadupdate {
|
|
|
|
int (*updater)(void);
|
|
|
|
AST_LIST_ENTRY(loadupdate) entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
static AST_LIST_HEAD_STATIC(updaters, loadupdate);
|
|
|
|
|
|
|
|
AST_MUTEX_DEFINE_STATIC(reloadlock);
|
|
|
|
|
Merged revisions 199022 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r199022 | seanbright | 2009-06-04 10:14:57 -0400 (Thu, 04 Jun 2009) | 40 lines
Safely handle AMI connections/reload requests that occur during startup.
During asterisk startup, a lock on the list of modules is obtained by the
primary thread while each module is initialized. Issue 13778 pointed out a
problem with this approach, however. Because the AMI is loaded before other
modules, it is possible for a module reload to be issued by a connected client
(via Action: Command), causing a deadlock.
The resolution for 13778 was to move initialization of the manager to happen
after the other modules had already been lodaded. While this fixed this
particular issue, it caused a problem for users (like FreePBX) who call AMI
scripts via an #exec in a configuration file (See issue 15189).
The solution I have come up with is to defer any reload requests that come in
until after the server is fully booted. When a call comes in to
ast_module_reload (from wherever) before we are fully booted, the request is
added to a queue of pending requests. Once we are done booting up, we then
execute these deferred requests in turn.
Note that I have tried to make this a bit more intelligent in that it will not
queue up more than 1 request for the same module to be reloaded, and if a
general reload request comes in ('module reload') the queue is flushed and we
only issue a single deferred reload for the entire system.
As for how this will impact existing installations - Before 13778, a reload
issued before module initialization was completed would result in a deadlock.
After 13778, you simply couldn't connect to the manager during startup (which
causes problems with #exec-that-calls-AMI configuration files). I believe this
is a good general purpose solution that won't negatively impact existing
installations.
(closes issue #15189)
(closes issue #13778)
Reported by: p_lindheimer
Patches:
06032009_15189_deferred_reloads.diff uploaded by seanbright (license 71)
Tested by: p_lindheimer, seanbright
Review: https://reviewboard.asterisk.org/r/272/
........
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@199051 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-06-04 14:31:24 +00:00
|
|
|
struct reload_queue_item {
|
|
|
|
AST_LIST_ENTRY(reload_queue_item) entry;
|
|
|
|
char module[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int do_full_reload = 0;
|
|
|
|
|
|
|
|
static AST_LIST_HEAD_STATIC(reload_queue, reload_queue_item);
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
/* when dynamic modules are being loaded, ast_module_register() will
|
|
|
|
need to know what filename the module was loaded from while it
|
|
|
|
is being registered
|
|
|
|
*/
|
2009-06-15 17:34:30 +00:00
|
|
|
static struct ast_module *resource_being_loaded;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
/* XXX: should we check for duplicate resource names here? */
|
|
|
|
|
|
|
|
void ast_module_register(const struct ast_module_info *info)
|
|
|
|
{
|
|
|
|
struct ast_module *mod;
|
|
|
|
|
|
|
|
if (embedding) {
|
|
|
|
if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
|
|
|
|
return;
|
|
|
|
strcpy(mod->resource, info->name);
|
|
|
|
} else {
|
|
|
|
mod = resource_being_loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod->info = info;
|
|
|
|
AST_LIST_HEAD_INIT(&mod->users);
|
|
|
|
|
|
|
|
/* during startup, before the loader has been initialized,
|
|
|
|
there are no threads, so there is no need to take the lock
|
|
|
|
on this list to manipulate it. it is also possible that it
|
|
|
|
might be unsafe to use the list lock at that point... so
|
|
|
|
let's avoid it altogether
|
|
|
|
*/
|
2007-11-06 17:05:13 +00:00
|
|
|
if (embedding) {
|
|
|
|
AST_LIST_INSERT_TAIL(&embedded_module_list, mod, entry);
|
|
|
|
} else {
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_LOCK(&module_list);
|
2007-11-06 17:05:13 +00:00
|
|
|
/* it is paramount that the new entry be placed at the tail of
|
|
|
|
the list, otherwise the code that uses dlopen() to load
|
|
|
|
dynamic modules won't be able to find out if the module it
|
|
|
|
just opened was registered or failed to load
|
|
|
|
*/
|
|
|
|
AST_LIST_INSERT_TAIL(&module_list, mod, entry);
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_UNLOCK(&module_list);
|
2007-11-06 17:05:13 +00:00
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
/* give the module a copy of its own handle, for later use in registrations and the like */
|
|
|
|
*((struct ast_module **) &(info->self)) = mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_module_unregister(const struct ast_module_info *info)
|
|
|
|
{
|
|
|
|
struct ast_module *mod = NULL;
|
|
|
|
|
|
|
|
/* it is assumed that the users list in the module structure
|
|
|
|
will already be empty, or we cannot have gotten to this
|
|
|
|
point
|
|
|
|
*/
|
|
|
|
AST_LIST_LOCK(&module_list);
|
|
|
|
AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
|
|
|
|
if (mod->info == info) {
|
2007-11-08 05:28:47 +00:00
|
|
|
AST_LIST_REMOVE_CURRENT(entry);
|
2006-08-21 02:11:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AST_LIST_TRAVERSE_SAFE_END;
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
|
|
|
|
|
|
|
if (mod) {
|
|
|
|
AST_LIST_HEAD_DESTROY(&mod->users);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(mod);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-05 21:42:49 +00:00
|
|
|
struct ast_module_user *__ast_module_user_add(struct ast_module *mod, struct ast_channel *chan)
|
2006-08-21 02:11:39 +00:00
|
|
|
{
|
2012-11-05 21:42:49 +00:00
|
|
|
struct ast_module_user *u;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2012-11-05 21:42:49 +00:00
|
|
|
u = ast_calloc(1, sizeof(*u));
|
|
|
|
if (!u) {
|
2006-08-21 02:11:39 +00:00
|
|
|
return NULL;
|
2012-11-05 21:42:49 +00:00
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
u->chan = chan;
|
|
|
|
|
|
|
|
AST_LIST_LOCK(&mod->users);
|
|
|
|
AST_LIST_INSERT_HEAD(&mod->users, u, entry);
|
|
|
|
AST_LIST_UNLOCK(&mod->users);
|
|
|
|
|
|
|
|
ast_atomic_fetchadd_int(&mod->usecount, +1);
|
|
|
|
|
|
|
|
ast_update_use_count();
|
|
|
|
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ast_module_user_remove(struct ast_module *mod, struct ast_module_user *u)
|
|
|
|
{
|
2012-11-05 21:42:49 +00:00
|
|
|
if (!u) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-17 23:10:42 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_LOCK(&mod->users);
|
2012-12-17 23:10:42 +00:00
|
|
|
u = AST_LIST_REMOVE(&mod->users, u, entry);
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_UNLOCK(&mod->users);
|
2012-12-17 23:10:42 +00:00
|
|
|
if (!u) {
|
|
|
|
/*
|
|
|
|
* Was not in the list. Either a bad pointer or
|
|
|
|
* __ast_module_user_hangup_all() has been called.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_atomic_fetchadd_int(&mod->usecount, -1);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(u);
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
ast_update_use_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ast_module_user_hangup_all(struct ast_module *mod)
|
|
|
|
{
|
|
|
|
struct ast_module_user *u;
|
|
|
|
|
|
|
|
AST_LIST_LOCK(&mod->users);
|
|
|
|
while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
|
2012-02-22 21:22:43 +00:00
|
|
|
if (u->chan) {
|
|
|
|
ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
|
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_atomic_fetchadd_int(&mod->usecount, -1);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(u);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
AST_LIST_UNLOCK(&mod->users);
|
|
|
|
|
2008-03-04 23:04:29 +00:00
|
|
|
ast_update_use_count();
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! \note
|
|
|
|
* In addition to modules, the reload command handles some extra keywords
|
|
|
|
* which are listed here together with the corresponding handlers.
|
|
|
|
* This table is also used by the command completion code.
|
|
|
|
*/
|
|
|
|
static struct reload_classes {
|
|
|
|
const char *name;
|
|
|
|
int (*reload_fn)(void);
|
|
|
|
} reload_classes[] = { /* list in alpha order, longest match first for cli completion */
|
|
|
|
{ "cdr", ast_cdr_engine_reload },
|
|
|
|
{ "dnsmgr", dnsmgr_reload },
|
|
|
|
{ "extconfig", read_config_maps },
|
|
|
|
{ "enum", ast_enum_reload },
|
2012-07-11 18:33:36 +00:00
|
|
|
{ "acl", ast_named_acl_reload },
|
2006-08-21 02:11:39 +00:00
|
|
|
{ "manager", reload_manager },
|
|
|
|
{ "http", ast_http_reload },
|
2007-12-06 14:23:12 +00:00
|
|
|
{ "logger", logger_reload },
|
2008-01-23 23:09:11 +00:00
|
|
|
{ "features", ast_features_reload },
|
2008-03-05 16:23:44 +00:00
|
|
|
{ "dsp", ast_dsp_reload},
|
2008-03-26 18:39:06 +00:00
|
|
|
{ "udptl", ast_udptl_reload },
|
2009-02-17 20:41:24 +00:00
|
|
|
{ "indications", ast_indications_reload },
|
2009-06-26 15:28:53 +00:00
|
|
|
{ "cel", ast_cel_engine_reload },
|
Fix transcode_via_sln option with SIP calls and improve PLC usage.
From reviewboard:
The problem here is a bit complex, so try to bear with me...
It was noticed by a Digium customer that generic PLC (as configured in
codecs.conf) did not appear to actually be having any sort of benefit when
packet loss was introduced on an RTP stream. I reproduced this issue myself
by streaming a file across an RTP stream and dropping approx. 5% of the
RTP packets. I saw no real difference between when PLC was enabled or disabled
when using wireshark to analyze the RTP streams.
After analyzing what was going on, it became clear that one of the problems
faced was that when running my tests, the translation paths were being set
up in such a way that PLC could not possibly work as expected. To illustrate,
if packets are lost on channel A's read stream, then we expect that PLC will
be applied to channel B's write stream. The problem is that generic PLC can
only be done when there is a translation path that moves from some codec to
SLINEAR. When I would run my tests, I found that every single time, read
and write translation paths would be set up on channel A instead of channel
B. There appeared to be no real way to predict which channel the translation
paths would be set up on.
This is where Kevin swooped in to let me know about the transcode_via_sln
option in asterisk.conf. It is supposed to work by placing a read translation
path on both channels from the channel's rawreadformat to SLINEAR. It also
will place a write translation path on both channels from SLINEAR to the
channel's rawwriteformat. Using this option allows one to predictably set up
translation paths on all channels. There are two problems with this, though.
First and foremost, the transcode_via_sln option did not appear to be working
properly when I was placing a SIP call between two endpoints which did not
share any common formats. Second, even if this option were to work, for PLC
to be applied, there had to be a write translation path that would go from
some format to SLINEAR. It would not work properly if the starting format
of translation was SLINEAR.
The one-line change presented in this review request in chan_sip.c fixed the
first issue for me. The problem was that in sip_request_call, the
jointcapability of the outbound channel was being set to the format passed to
sip_request_call. This is nativeformats of the inbound channel. Because of this,
when ast_channel_make_compatible was called by app_dial, both channels already
had compatibly read and write formats. Thus, no translation path was set up at
the time. My change is to set the jointcapability of the sip_pvt created during
sip_request_call to the intersection of the inbound channel's nativeformats and
the configured peer capability that we determined during the earlier call to
create_addr. Doing this got the translation paths set up as expected when using
transcode_via_sln.
The changes presented in channel.c fixed the second issue for me. First and
foremost, when Asterisk is started, we'll read codecs.conf to see the value of
the genericplc option. If this option is set, and ast_write is called for a
frame with no data, then we will attempt to fill in the missing samples for
the frame. The implementation uses a channel datastore for maintaining the
PLC state and for creating a buffer to store PLC samples in. Even when we
receive a frame with data, we'll call plc_rx so that the PLC state will have
knowledge of the previous voice frame, which it can use as a basis for when
it comes time to actually do a PLC fill-in.
So, reviewers, now I ask for your help. First off, there's the one line change
in chan_sip that I have put in. Is it right? By my logic it seems correct, but
I'm sure someone can tell me why it is not going to work. This is probably the
change I'm least concerned about, though. What concerns me much more is the
set of changes in channel.c. First off, am I even doing it right? When I run
tests, I can clearly see that when PLC is activated, I see a significant increase
in RTP traffic where I would expect it to be. However, in my humble opinion, the
audio sounds kind of crappy whenever the PLC fill-in is done. It sounds worse to
me than when no PLC is used at all. I need someone to review the logic I have used
to be sure that I'm not misusing anything. As far as I can see my pointer arithmetic
is correct, and my use of AST_FRIENDLY_OFFSET should be correct as well, but I'm
sure someone can point out somewhere where I've done something incorrectly.
As I was writing this review request up, I decided to give the code a test run under
valgrind, and I find that for some reason, calls to plc_rx are causing some invalid
reads. Apparently I'm reading past the end of a buffer somehow. I'll have to dig around
a bit to see why that is the case. If it's obvious to someone reviewing, speak up!
Finally, I have one other proposal that is not reflected in my code review. Since
without transcode_via_sln set, one cannot predict or control where a translation
path will be up, it seems to me that the current practice of using PLC only when
transcoding to SLINEAR is not useful. I recommend that once it has been determined
that the method used in this code review is correct and works as expected, then
the code in translate.c that invokes PLC should be removed.
Review: https://reviewboard.asterisk.org/r/622/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@264452 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-05-19 21:29:08 +00:00
|
|
|
{ "plc", ast_plc_reload },
|
2006-08-21 02:11:39 +00:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int printdigest(const unsigned char *d)
|
|
|
|
{
|
|
|
|
int x, pos;
|
|
|
|
char buf[256]; /* large enough so we don't have to worry */
|
|
|
|
|
|
|
|
for (pos = 0, x = 0; x < 16; x++)
|
|
|
|
pos += sprintf(buf + pos, " %02x", *d++);
|
|
|
|
|
2007-06-24 18:51:41 +00:00
|
|
|
ast_debug(1, "Unexpected signature:%s\n", buf);
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int key_matches(const unsigned char *key1, const unsigned char *key2)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
|
|
|
for (x = 0; x < 16; x++) {
|
|
|
|
if (key1[x] != key2[x])
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int verify_key(const unsigned char *key)
|
|
|
|
{
|
|
|
|
struct MD5Context c;
|
|
|
|
unsigned char digest[16];
|
|
|
|
|
|
|
|
MD5Init(&c);
|
|
|
|
MD5Update(&c, key, strlen((char *)key));
|
|
|
|
MD5Final(digest, &c);
|
|
|
|
|
|
|
|
if (key_matches(expected_key, digest))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
printdigest(digest);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int resource_name_match(const char *name1_in, const char *name2_in)
|
|
|
|
{
|
|
|
|
char *name1 = (char *) name1_in;
|
|
|
|
char *name2 = (char *) name2_in;
|
|
|
|
|
|
|
|
/* trim off any .so extensions */
|
|
|
|
if (!strcasecmp(name1 + strlen(name1) - 3, ".so")) {
|
|
|
|
name1 = ast_strdupa(name1);
|
|
|
|
name1[strlen(name1) - 3] = '\0';
|
|
|
|
}
|
|
|
|
if (!strcasecmp(name2 + strlen(name2) - 3, ".so")) {
|
|
|
|
name2 = ast_strdupa(name2);
|
|
|
|
name2[strlen(name2) - 3] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return strcasecmp(name1, name2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_module *find_resource(const char *resource, int do_lock)
|
|
|
|
{
|
|
|
|
struct ast_module *cur;
|
|
|
|
|
|
|
|
if (do_lock)
|
|
|
|
AST_LIST_LOCK(&module_list);
|
|
|
|
|
|
|
|
AST_LIST_TRAVERSE(&module_list, cur, entry) {
|
|
|
|
if (!resource_name_match(resource, cur->resource))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_lock)
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
|
|
|
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2007-10-22 13:36:13 +00:00
|
|
|
#ifdef LOADABLE_MODULES
|
2006-08-21 02:11:39 +00:00
|
|
|
static void unload_dynamic_module(struct ast_module *mod)
|
|
|
|
{
|
2006-08-26 19:45:16 +00:00
|
|
|
void *lib = mod->lib;
|
|
|
|
|
|
|
|
/* WARNING: the structure pointed to by mod is going to
|
|
|
|
disappear when this operation succeeds, so we can't
|
|
|
|
dereference it */
|
|
|
|
|
|
|
|
if (lib)
|
|
|
|
while (!dlclose(lib));
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 16:53:19 +00:00
|
|
|
static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required);
|
|
|
|
|
|
|
|
static struct ast_module *load_dynamic_module(const char *resource_in, unsigned int global_symbols_only, struct ast_heap *resource_heap)
|
2006-08-21 02:11:39 +00:00
|
|
|
{
|
2008-01-02 09:16:17 +00:00
|
|
|
char fn[PATH_MAX] = "";
|
2007-03-07 20:05:05 +00:00
|
|
|
void *lib = NULL;
|
2006-08-21 02:11:39 +00:00
|
|
|
struct ast_module *mod;
|
2006-08-21 14:42:03 +00:00
|
|
|
unsigned int wants_global;
|
2008-01-02 09:16:17 +00:00
|
|
|
int space; /* room needed for the descriptor */
|
|
|
|
int missing_so = 0;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2008-01-02 09:16:17 +00:00
|
|
|
space = sizeof(*resource_being_loaded) + strlen(resource_in) + 1;
|
|
|
|
if (strcasecmp(resource_in + strlen(resource_in) - 3, ".so")) {
|
|
|
|
missing_so = 1;
|
|
|
|
space += 3; /* room for the extra ".so" */
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
2008-01-02 09:16:17 +00:00
|
|
|
snprintf(fn, sizeof(fn), "%s/%s%s", ast_config_AST_MODULE_DIR, resource_in, missing_so ? ".so" : "");
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2006-08-21 14:42:03 +00:00
|
|
|
/* make a first load of the module in 'quiet' mode... don't try to resolve
|
|
|
|
any symbols, and don't export any symbols. this will allow us to peek into
|
|
|
|
the module's info block (if available) to see what flags it has set */
|
|
|
|
|
2008-01-02 09:16:17 +00:00
|
|
|
resource_being_loaded = ast_calloc(1, space);
|
|
|
|
if (!resource_being_loaded)
|
2006-08-21 02:11:39 +00:00
|
|
|
return NULL;
|
2008-01-02 09:16:17 +00:00
|
|
|
strcpy(resource_being_loaded->resource, resource_in);
|
|
|
|
if (missing_so)
|
|
|
|
strcat(resource_being_loaded->resource, ".so");
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2006-08-21 14:42:03 +00:00
|
|
|
if (!(lib = dlopen(fn, RTLD_LAZY | RTLD_LOCAL))) {
|
2006-10-04 00:26:21 +00:00
|
|
|
ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(resource_being_loaded);
|
2006-08-21 02:11:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the dlopen() succeeded, let's find out if the module
|
|
|
|
registered itself */
|
|
|
|
/* note that this will only work properly as long as
|
|
|
|
ast_module_register() (which is called by the module's
|
|
|
|
constructor) places the new module at the tail of the
|
|
|
|
module_list
|
|
|
|
*/
|
|
|
|
if (resource_being_loaded != (mod = AST_LIST_LAST(&module_list))) {
|
2006-10-04 00:26:21 +00:00
|
|
|
ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);
|
2006-08-21 02:11:39 +00:00
|
|
|
/* no, it did not, so close it and return */
|
2006-08-23 19:28:13 +00:00
|
|
|
while (!dlclose(lib));
|
2006-08-21 14:42:03 +00:00
|
|
|
/* note that the module's destructor will call ast_module_unregister(),
|
|
|
|
which will free the structure we allocated in resource_being_loaded */
|
2006-08-21 02:11:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-21 14:42:03 +00:00
|
|
|
wants_global = ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS);
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
/* if we are being asked only to load modules that provide global symbols,
|
|
|
|
and this one does not, then close it and return */
|
2006-08-23 19:28:13 +00:00
|
|
|
if (global_symbols_only && !wants_global) {
|
|
|
|
while (!dlclose(lib));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-02 05:27:53 +00:00
|
|
|
/* This section is a workaround for a gcc 4.1 bug that has already been
|
|
|
|
* fixed in later versions. Unfortunately, some distributions, such as
|
|
|
|
* RHEL/CentOS 5, distribute gcc 4.1, so we're stuck with having to deal
|
|
|
|
* with this issue. This basically ensures that optional_api modules are
|
|
|
|
* loaded before any module which requires their functionality. */
|
|
|
|
#if !defined(HAVE_ATTRIBUTE_weak_import) && !defined(HAVE_ATTRIBUTE_weakref)
|
|
|
|
if (!ast_strlen_zero(mod->info->nonoptreq)) {
|
|
|
|
/* Force any required dependencies to load */
|
|
|
|
char *each, *required_resource = ast_strdupa(mod->info->nonoptreq);
|
|
|
|
while ((each = strsep(&required_resource, ","))) {
|
2012-09-27 16:53:19 +00:00
|
|
|
struct ast_module *dependency;
|
2010-09-02 05:27:53 +00:00
|
|
|
each = ast_strip(each);
|
2012-09-27 16:53:19 +00:00
|
|
|
dependency = find_resource(each, 0);
|
2010-09-02 05:27:53 +00:00
|
|
|
/* Is it already loaded? */
|
2012-09-27 16:53:19 +00:00
|
|
|
if (!dependency) {
|
|
|
|
load_resource(each, global_symbols_only, resource_heap, 1);
|
2010-09-02 05:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-08-23 19:28:13 +00:00
|
|
|
while (!dlclose(lib));
|
|
|
|
resource_being_loaded = NULL;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2006-08-21 14:42:03 +00:00
|
|
|
/* start the load process again */
|
2008-01-02 09:16:17 +00:00
|
|
|
resource_being_loaded = ast_calloc(1, space);
|
|
|
|
if (!resource_being_loaded)
|
2006-08-21 14:42:03 +00:00
|
|
|
return NULL;
|
2008-01-02 09:16:17 +00:00
|
|
|
strcpy(resource_being_loaded->resource, resource_in);
|
|
|
|
if (missing_so)
|
|
|
|
strcat(resource_being_loaded->resource, ".so");
|
2006-08-21 14:42:03 +00:00
|
|
|
|
|
|
|
if (!(lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {
|
2006-10-04 00:26:21 +00:00
|
|
|
ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(resource_being_loaded);
|
2006-08-21 14:42:03 +00:00
|
|
|
return NULL;
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 14:42:03 +00:00
|
|
|
/* since the module was successfully opened, and it registered itself
|
|
|
|
the previous time we did that, we're going to assume it worked this
|
|
|
|
time too :) */
|
2006-08-23 19:28:13 +00:00
|
|
|
|
2006-08-21 14:42:03 +00:00
|
|
|
AST_LIST_LAST(&module_list)->lib = lib;
|
|
|
|
resource_being_loaded = NULL;
|
|
|
|
|
|
|
|
return AST_LIST_LAST(&module_list);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-05 15:54:36 +00:00
|
|
|
void ast_module_shutdown(void)
|
|
|
|
{
|
|
|
|
struct ast_module *mod;
|
2009-11-09 07:37:52 +00:00
|
|
|
int somethingchanged = 1, final = 0;
|
2007-06-05 15:54:36 +00:00
|
|
|
|
|
|
|
AST_LIST_LOCK(&module_list);
|
|
|
|
|
2009-11-09 07:37:52 +00:00
|
|
|
/*!\note Some resources, like timers, are started up dynamically, and thus
|
|
|
|
* may be still in use, even if all channels are dead. We must therefore
|
|
|
|
* check the usecount before asking modules to unload. */
|
|
|
|
do {
|
|
|
|
if (!somethingchanged) {
|
|
|
|
/*!\note If we go through the entire list without changing
|
|
|
|
* anything, ignore the usecounts and unload, then exit. */
|
|
|
|
final = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset flag before traversing the list */
|
|
|
|
somethingchanged = 0;
|
|
|
|
|
|
|
|
AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
|
|
|
|
if (!final && mod->usecount) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AST_LIST_REMOVE_CURRENT(entry);
|
2010-07-09 17:50:45 +00:00
|
|
|
if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
|
2009-11-09 07:37:52 +00:00
|
|
|
mod->info->unload();
|
|
|
|
}
|
|
|
|
AST_LIST_HEAD_DESTROY(&mod->users);
|
|
|
|
free(mod);
|
|
|
|
somethingchanged = 1;
|
|
|
|
}
|
|
|
|
AST_LIST_TRAVERSE_SAFE_END;
|
|
|
|
} while (somethingchanged && !final);
|
|
|
|
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
2007-06-05 15:54:36 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
|
|
|
|
{
|
|
|
|
struct ast_module *mod;
|
|
|
|
int res = -1;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
AST_LIST_LOCK(&module_list);
|
|
|
|
|
2006-08-22 23:06:13 +00:00
|
|
|
if (!(mod = find_resource(resource_name, 0))) {
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
2008-02-19 21:54:09 +00:00
|
|
|
ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
|
2010-05-12 19:59:16 +00:00
|
|
|
return -1;
|
2006-08-22 23:06:13 +00:00
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2010-07-09 18:24:03 +00:00
|
|
|
if (!mod->flags.running || mod->flags.declined) {
|
|
|
|
ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
|
2006-08-21 02:11:39 +00:00
|
|
|
error = 1;
|
2010-07-09 18:24:03 +00:00
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
if (!error && (mod->usecount > 0)) {
|
2006-09-15 05:00:27 +00:00
|
|
|
if (force)
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_log(LOG_WARNING, "Warning: Forcing removal of module '%s' with use count %d\n",
|
|
|
|
resource_name, mod->usecount);
|
|
|
|
else {
|
|
|
|
ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
|
|
|
|
mod->usecount);
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error) {
|
2012-12-17 23:10:42 +00:00
|
|
|
/* Request any channels attached to the module to hangup. */
|
2006-08-21 02:11:39 +00:00
|
|
|
__ast_module_user_hangup_all(mod);
|
|
|
|
|
2012-12-17 23:10:42 +00:00
|
|
|
res = mod->info->unload();
|
2006-08-21 02:11:39 +00:00
|
|
|
if (res) {
|
|
|
|
ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
|
2012-12-17 23:10:42 +00:00
|
|
|
if (force <= AST_FORCE_FIRM) {
|
2006-08-21 02:11:39 +00:00
|
|
|
error = 1;
|
2012-12-17 23:10:42 +00:00
|
|
|
} else {
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
|
2012-12-17 23:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
/*
|
|
|
|
* Request hangup on any channels that managed to get attached
|
|
|
|
* while we called the module unload function.
|
|
|
|
*/
|
|
|
|
__ast_module_user_hangup_all(mod);
|
|
|
|
sched_yield();
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error)
|
2007-05-03 16:43:49 +00:00
|
|
|
mod->flags.running = mod->flags.declined = 0;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
|
|
|
|
2008-01-02 09:16:17 +00:00
|
|
|
if (!error && !mod->lib && mod->info && mod->info->restore_globals)
|
2007-03-19 22:32:40 +00:00
|
|
|
mod->info->restore_globals();
|
|
|
|
|
2007-10-22 13:36:13 +00:00
|
|
|
#ifdef LOADABLE_MODULES
|
2012-08-13 20:36:51 +00:00
|
|
|
if (!error) {
|
2006-08-21 02:11:39 +00:00
|
|
|
unload_dynamic_module(mod);
|
2012-08-13 20:36:51 +00:00
|
|
|
ast_test_suite_event_notify("MODULE_UNLOAD", "Message: %s", resource_name);
|
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!error)
|
|
|
|
ast_update_use_count();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload)
|
|
|
|
{
|
|
|
|
struct ast_module *cur;
|
|
|
|
int i, which=0, l = strlen(word);
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if (pos != rpos)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
AST_LIST_LOCK(&module_list);
|
|
|
|
AST_LIST_TRAVERSE(&module_list, cur, entry) {
|
|
|
|
if (!strncasecmp(word, cur->resource, l) &&
|
|
|
|
(cur->info->reload || !needsreload) &&
|
|
|
|
++which > state) {
|
2007-06-14 23:01:01 +00:00
|
|
|
ret = ast_strdup(cur->resource);
|
2006-08-21 02:11:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
for (i=0; !ret && reload_classes[i].name; i++) {
|
|
|
|
if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
|
2007-06-14 23:01:01 +00:00
|
|
|
ret = ast_strdup(reload_classes[i].name);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
Merged revisions 199022 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r199022 | seanbright | 2009-06-04 10:14:57 -0400 (Thu, 04 Jun 2009) | 40 lines
Safely handle AMI connections/reload requests that occur during startup.
During asterisk startup, a lock on the list of modules is obtained by the
primary thread while each module is initialized. Issue 13778 pointed out a
problem with this approach, however. Because the AMI is loaded before other
modules, it is possible for a module reload to be issued by a connected client
(via Action: Command), causing a deadlock.
The resolution for 13778 was to move initialization of the manager to happen
after the other modules had already been lodaded. While this fixed this
particular issue, it caused a problem for users (like FreePBX) who call AMI
scripts via an #exec in a configuration file (See issue 15189).
The solution I have come up with is to defer any reload requests that come in
until after the server is fully booted. When a call comes in to
ast_module_reload (from wherever) before we are fully booted, the request is
added to a queue of pending requests. Once we are done booting up, we then
execute these deferred requests in turn.
Note that I have tried to make this a bit more intelligent in that it will not
queue up more than 1 request for the same module to be reloaded, and if a
general reload request comes in ('module reload') the queue is flushed and we
only issue a single deferred reload for the entire system.
As for how this will impact existing installations - Before 13778, a reload
issued before module initialization was completed would result in a deadlock.
After 13778, you simply couldn't connect to the manager during startup (which
causes problems with #exec-that-calls-AMI configuration files). I believe this
is a good general purpose solution that won't negatively impact existing
installations.
(closes issue #15189)
(closes issue #13778)
Reported by: p_lindheimer
Patches:
06032009_15189_deferred_reloads.diff uploaded by seanbright (license 71)
Tested by: p_lindheimer, seanbright
Review: https://reviewboard.asterisk.org/r/272/
........
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@199051 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-06-04 14:31:24 +00:00
|
|
|
void ast_process_pending_reloads(void)
|
|
|
|
{
|
|
|
|
struct reload_queue_item *item;
|
|
|
|
|
|
|
|
if (!ast_fully_booted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_LIST_LOCK(&reload_queue);
|
|
|
|
|
|
|
|
if (do_full_reload) {
|
|
|
|
do_full_reload = 0;
|
|
|
|
AST_LIST_UNLOCK(&reload_queue);
|
|
|
|
ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
|
|
|
|
ast_module_reload(NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
|
|
|
|
ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
|
|
|
|
ast_module_reload(item->module);
|
|
|
|
ast_free(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_LIST_UNLOCK(&reload_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void queue_reload_request(const char *module)
|
|
|
|
{
|
|
|
|
struct reload_queue_item *item;
|
|
|
|
|
|
|
|
AST_LIST_LOCK(&reload_queue);
|
|
|
|
|
|
|
|
if (do_full_reload) {
|
|
|
|
AST_LIST_UNLOCK(&reload_queue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast_strlen_zero(module)) {
|
|
|
|
/* A full reload request (when module is NULL) wipes out any previous
|
|
|
|
reload requests and causes the queue to ignore future ones */
|
|
|
|
while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
|
|
|
|
ast_free(item);
|
|
|
|
}
|
|
|
|
do_full_reload = 1;
|
|
|
|
} else {
|
|
|
|
/* No reason to add the same module twice */
|
|
|
|
AST_LIST_TRAVERSE(&reload_queue, item, entry) {
|
|
|
|
if (!strcasecmp(item->module, module)) {
|
|
|
|
AST_LIST_UNLOCK(&reload_queue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
|
|
|
|
if (!item) {
|
|
|
|
ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
|
|
|
|
AST_LIST_UNLOCK(&reload_queue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy(item->module, module);
|
|
|
|
AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
|
|
|
|
}
|
|
|
|
AST_LIST_UNLOCK(&reload_queue);
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
int ast_module_reload(const char *name)
|
|
|
|
{
|
|
|
|
struct ast_module *cur;
|
|
|
|
int res = 0; /* return value. 0 = not found, others, see below */
|
|
|
|
int i;
|
|
|
|
|
Merged revisions 199022 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r199022 | seanbright | 2009-06-04 10:14:57 -0400 (Thu, 04 Jun 2009) | 40 lines
Safely handle AMI connections/reload requests that occur during startup.
During asterisk startup, a lock on the list of modules is obtained by the
primary thread while each module is initialized. Issue 13778 pointed out a
problem with this approach, however. Because the AMI is loaded before other
modules, it is possible for a module reload to be issued by a connected client
(via Action: Command), causing a deadlock.
The resolution for 13778 was to move initialization of the manager to happen
after the other modules had already been lodaded. While this fixed this
particular issue, it caused a problem for users (like FreePBX) who call AMI
scripts via an #exec in a configuration file (See issue 15189).
The solution I have come up with is to defer any reload requests that come in
until after the server is fully booted. When a call comes in to
ast_module_reload (from wherever) before we are fully booted, the request is
added to a queue of pending requests. Once we are done booting up, we then
execute these deferred requests in turn.
Note that I have tried to make this a bit more intelligent in that it will not
queue up more than 1 request for the same module to be reloaded, and if a
general reload request comes in ('module reload') the queue is flushed and we
only issue a single deferred reload for the entire system.
As for how this will impact existing installations - Before 13778, a reload
issued before module initialization was completed would result in a deadlock.
After 13778, you simply couldn't connect to the manager during startup (which
causes problems with #exec-that-calls-AMI configuration files). I believe this
is a good general purpose solution that won't negatively impact existing
installations.
(closes issue #15189)
(closes issue #13778)
Reported by: p_lindheimer
Patches:
06032009_15189_deferred_reloads.diff uploaded by seanbright (license 71)
Tested by: p_lindheimer, seanbright
Review: https://reviewboard.asterisk.org/r/272/
........
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@199051 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-06-04 14:31:24 +00:00
|
|
|
/* If we aren't fully booted, we just pretend we reloaded but we queue this
|
|
|
|
up to run once we are booted up. */
|
|
|
|
if (!ast_fully_booted) {
|
|
|
|
queue_reload_request(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
if (ast_mutex_trylock(&reloadlock)) {
|
|
|
|
ast_verbose("The previous reload command didn't finish yet\n");
|
|
|
|
return -1; /* reload already in progress */
|
|
|
|
}
|
2007-07-18 19:47:20 +00:00
|
|
|
ast_lastreloadtime = ast_tvnow();
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2010-01-27 18:29:49 +00:00
|
|
|
if (ast_opt_lock_confdir) {
|
|
|
|
int try;
|
|
|
|
int res;
|
|
|
|
for (try = 1, res = AST_LOCK_TIMEOUT; try < 6 && (res == AST_LOCK_TIMEOUT); try++) {
|
|
|
|
res = ast_lock_path(ast_config_AST_CONFIG_DIR);
|
|
|
|
if (res == AST_LOCK_TIMEOUT) {
|
|
|
|
ast_log(LOG_WARNING, "Failed to grab lock on %s, try %d\n", ast_config_AST_CONFIG_DIR, try);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (res != AST_LOCK_SUCCESS) {
|
|
|
|
ast_verbose("Cannot grab lock on %s\n", ast_config_AST_CONFIG_DIR);
|
|
|
|
ast_mutex_unlock(&reloadlock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
/* Call "predefined" reload here first */
|
|
|
|
for (i = 0; reload_classes[i].name; i++) {
|
|
|
|
if (!name || !strcasecmp(name, reload_classes[i].name)) {
|
2012-08-17 16:01:32 +00:00
|
|
|
if (!reload_classes[i].reload_fn()) {
|
|
|
|
ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", name);
|
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
res = 2; /* found and reloaded */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-08 13:50:33 +00:00
|
|
|
if (name && res) {
|
2010-01-27 18:29:49 +00:00
|
|
|
if (ast_opt_lock_confdir) {
|
|
|
|
ast_unlock_path(ast_config_AST_CONFIG_DIR);
|
|
|
|
}
|
2007-02-08 13:50:33 +00:00
|
|
|
ast_mutex_unlock(&reloadlock);
|
2006-08-21 02:11:39 +00:00
|
|
|
return res;
|
2007-02-08 13:50:33 +00:00
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
AST_LIST_LOCK(&module_list);
|
|
|
|
AST_LIST_TRAVERSE(&module_list, cur, entry) {
|
|
|
|
const struct ast_module_info *info = cur->info;
|
|
|
|
|
|
|
|
if (name && resource_name_match(name, cur->resource))
|
|
|
|
continue;
|
|
|
|
|
2008-02-15 19:50:47 +00:00
|
|
|
if (!cur->flags.running || cur->flags.declined) {
|
|
|
|
if (!name)
|
|
|
|
continue;
|
|
|
|
ast_log(LOG_NOTICE, "The module '%s' was not properly initialized. "
|
|
|
|
"Before reloading the module, you must run \"module load %s\" "
|
|
|
|
"and fix whatever is preventing the module from being initialized.\n",
|
|
|
|
name, name);
|
|
|
|
res = 2; /* Don't report that the module was not found */
|
|
|
|
break;
|
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
if (!info->reload) { /* cannot be reloaded */
|
2012-08-16 22:45:33 +00:00
|
|
|
/* Nothing to reload, so reload is successful */
|
|
|
|
ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", cur->resource);
|
2006-08-21 02:11:39 +00:00
|
|
|
if (res < 1) /* store result if possible */
|
|
|
|
res = 1; /* 1 = no reload() method */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = 2;
|
2007-07-26 15:49:18 +00:00
|
|
|
ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
|
2012-08-16 22:45:33 +00:00
|
|
|
if (!info->reload()) {
|
|
|
|
ast_test_suite_event_notify("MODULE_RELOAD", "Message: %s", cur->resource);
|
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
|
|
|
|
2010-01-27 18:29:49 +00:00
|
|
|
if (ast_opt_lock_confdir) {
|
|
|
|
ast_unlock_path(ast_config_AST_CONFIG_DIR);
|
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_mutex_unlock(&reloadlock);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int inspect_module(const struct ast_module *mod)
|
|
|
|
{
|
|
|
|
if (!mod->info->description) {
|
|
|
|
ast_log(LOG_WARNING, "Module '%s' does not provide a description.\n", mod->resource);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mod->info->key) {
|
|
|
|
ast_log(LOG_WARNING, "Module '%s' does not provide a license key.\n", mod->resource);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verify_key((unsigned char *) mod->info->key)) {
|
|
|
|
ast_log(LOG_WARNING, "Module '%s' did not provide a valid license key.\n", mod->resource);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-20 19:28:10 +00:00
|
|
|
if (!ast_strlen_zero(mod->info->buildopt_sum) &&
|
|
|
|
strcmp(buildopt_sum, mod->info->buildopt_sum)) {
|
2007-11-16 16:56:59 +00:00
|
|
|
ast_log(LOG_WARNING, "Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod->resource);
|
|
|
|
ast_log(LOG_WARNING, "Module '%s' will not be initialized as it may cause instability.\n", mod->resource);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-09 16:22:04 +00:00
|
|
|
static enum ast_module_load_result start_resource(struct ast_module *mod)
|
|
|
|
{
|
|
|
|
char tmp[256];
|
|
|
|
enum ast_module_load_result res;
|
|
|
|
|
2012-09-27 16:53:19 +00:00
|
|
|
if (mod->flags.running) {
|
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-06-09 16:22:04 +00:00
|
|
|
if (!mod->info->load) {
|
|
|
|
return AST_MODULE_LOAD_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = mod->info->load();
|
|
|
|
|
|
|
|
switch (res) {
|
|
|
|
case AST_MODULE_LOAD_SUCCESS:
|
|
|
|
if (!ast_fully_booted) {
|
|
|
|
ast_verb(1, "%s => (%s)\n", mod->resource, term_color(tmp, mod->info->description, COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
|
2012-02-14 20:27:16 +00:00
|
|
|
if (ast_opt_console && !option_verbose) {
|
|
|
|
/* This never looks good on anything but the root console, so
|
|
|
|
* it's best not to try to funnel it through the logger. */
|
|
|
|
fprintf(stdout, ".");
|
|
|
|
}
|
2009-06-09 16:22:04 +00:00
|
|
|
} else {
|
|
|
|
ast_verb(1, "Loaded %s => (%s)\n", mod->resource, mod->info->description);
|
|
|
|
}
|
|
|
|
|
|
|
|
mod->flags.running = 1;
|
|
|
|
|
|
|
|
ast_update_use_count();
|
|
|
|
break;
|
|
|
|
case AST_MODULE_LOAD_DECLINE:
|
|
|
|
mod->flags.declined = 1;
|
|
|
|
break;
|
|
|
|
case AST_MODULE_LOAD_FAILURE:
|
2009-06-22 15:33:35 +00:00
|
|
|
case AST_MODULE_LOAD_SKIP: /* modules should never return this value */
|
|
|
|
case AST_MODULE_LOAD_PRIORITY:
|
2009-06-09 16:22:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! loads a resource based upon resource_name. If global_symbols_only is set
|
|
|
|
* only modules with global symbols will be loaded.
|
|
|
|
*
|
|
|
|
* If the ast_heap is provided (not NULL) the module is found and added to the
|
|
|
|
* heap without running the module's load() function. By doing this, modules
|
2012-03-22 19:51:16 +00:00
|
|
|
* added to the resource_heap can be initialized later in order by priority.
|
2009-06-09 16:22:04 +00:00
|
|
|
*
|
|
|
|
* If the ast_heap is not provided, the module's load function will be executed
|
|
|
|
* immediately */
|
2009-11-13 08:52:28 +00:00
|
|
|
static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required)
|
2006-08-21 02:11:39 +00:00
|
|
|
{
|
|
|
|
struct ast_module *mod;
|
|
|
|
enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;
|
|
|
|
|
|
|
|
if ((mod = find_resource(resource_name, 0))) {
|
2007-05-03 16:43:49 +00:00
|
|
|
if (mod->flags.running) {
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
|
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
|
|
|
if (global_symbols_only && !ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS))
|
|
|
|
return AST_MODULE_LOAD_SKIP;
|
|
|
|
} else {
|
2007-10-22 13:36:13 +00:00
|
|
|
#ifdef LOADABLE_MODULES
|
2012-09-27 16:53:19 +00:00
|
|
|
if (!(mod = load_dynamic_module(resource_name, global_symbols_only, resource_heap))) {
|
2006-08-21 02:11:39 +00:00
|
|
|
/* don't generate a warning message during load_modules() */
|
|
|
|
if (!global_symbols_only) {
|
|
|
|
ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
|
2009-11-13 08:52:28 +00:00
|
|
|
return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
|
2006-08-21 02:11:39 +00:00
|
|
|
} else {
|
2009-11-13 08:52:28 +00:00
|
|
|
return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SKIP;
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-22 21:57:16 +00:00
|
|
|
#else
|
2011-02-09 19:17:01 +00:00
|
|
|
ast_log(LOG_WARNING, "Module support is not available. Module '%s' could not be loaded.\n", resource_name);
|
2009-11-13 08:52:28 +00:00
|
|
|
return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
|
2006-08-21 02:11:39 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inspect_module(mod)) {
|
|
|
|
ast_log(LOG_WARNING, "Module '%s' could not be loaded.\n", resource_name);
|
2007-10-22 13:36:13 +00:00
|
|
|
#ifdef LOADABLE_MODULES
|
2006-08-21 02:11:39 +00:00
|
|
|
unload_dynamic_module(mod);
|
|
|
|
#endif
|
2009-11-13 08:52:28 +00:00
|
|
|
return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 21:11:09 +00:00
|
|
|
if (!mod->lib && mod->info->backup_globals && mod->info->backup_globals()) {
|
2007-02-22 02:36:00 +00:00
|
|
|
ast_log(LOG_WARNING, "Module '%s' was unable to backup its global data.\n", resource_name);
|
2009-11-13 08:52:28 +00:00
|
|
|
return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
|
2007-02-22 02:36:00 +00:00
|
|
|
}
|
|
|
|
|
2007-05-03 16:43:49 +00:00
|
|
|
mod->flags.declined = 0;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2009-06-09 16:22:04 +00:00
|
|
|
if (resource_heap) {
|
|
|
|
ast_heap_push(resource_heap, mod);
|
2009-06-22 15:33:35 +00:00
|
|
|
res = AST_MODULE_LOAD_PRIORITY;
|
2009-06-09 16:22:04 +00:00
|
|
|
} else {
|
|
|
|
res = start_resource(mod);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
2011-07-27 15:26:31 +00:00
|
|
|
/* Now make sure that the list is sorted */
|
|
|
|
AST_LIST_LOCK(&module_list);
|
|
|
|
AST_LIST_REMOVE(&module_list, mod, entry);
|
|
|
|
AST_LIST_INSERT_SORTALPHA(&module_list, mod, entry, resource);
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_load_resource(const char *resource_name)
|
|
|
|
{
|
2008-06-05 15:58:11 +00:00
|
|
|
int res;
|
2008-03-04 23:04:29 +00:00
|
|
|
AST_LIST_LOCK(&module_list);
|
2009-11-13 08:52:28 +00:00
|
|
|
res = load_resource(resource_name, 0, NULL, 0);
|
2012-08-16 22:45:33 +00:00
|
|
|
if (!res) {
|
|
|
|
ast_test_suite_event_notify("MODULE_LOAD", "Message: %s", resource_name);
|
|
|
|
}
|
2008-03-04 23:04:29 +00:00
|
|
|
AST_LIST_UNLOCK(&module_list);
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2008-06-05 15:58:11 +00:00
|
|
|
return res;
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct load_order_entry {
|
|
|
|
char *resource;
|
2009-11-13 08:52:28 +00:00
|
|
|
int required;
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_ENTRY(load_order_entry) entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
AST_LIST_HEAD_NOLOCK(load_order, load_order_entry);
|
|
|
|
|
2009-11-13 08:52:28 +00:00
|
|
|
static struct load_order_entry *add_to_load_order(const char *resource, struct load_order *load_order, int required)
|
2006-08-21 02:11:39 +00:00
|
|
|
{
|
|
|
|
struct load_order_entry *order;
|
|
|
|
|
|
|
|
AST_LIST_TRAVERSE(load_order, order, entry) {
|
2009-11-13 08:52:28 +00:00
|
|
|
if (!resource_name_match(order->resource, resource)) {
|
2012-03-22 19:51:16 +00:00
|
|
|
/* Make sure we have the proper setting for the required field
|
2009-11-13 08:52:28 +00:00
|
|
|
(we might have both load= and required= lines in modules.conf) */
|
2009-11-13 10:53:14 +00:00
|
|
|
order->required |= required;
|
2006-08-21 02:11:39 +00:00
|
|
|
return NULL;
|
2009-11-13 08:52:28 +00:00
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(order = ast_calloc(1, sizeof(*order))))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
order->resource = ast_strdup(resource);
|
2009-11-13 08:52:28 +00:00
|
|
|
order->required = required;
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_INSERT_TAIL(load_order, order, entry);
|
|
|
|
|
|
|
|
return order;
|
|
|
|
}
|
2008-06-12 17:27:55 +00:00
|
|
|
|
2009-06-09 16:22:04 +00:00
|
|
|
static int mod_load_cmp(void *a, void *b)
|
|
|
|
{
|
|
|
|
struct ast_module *a_mod = (struct ast_module *) a;
|
|
|
|
struct ast_module *b_mod = (struct ast_module *) b;
|
|
|
|
int res = -1;
|
2010-07-20 19:35:02 +00:00
|
|
|
/* if load_pri is not set, default is 128. Lower is better*/
|
|
|
|
unsigned char a_pri = ast_test_flag(a_mod->info, AST_MODFLAG_LOAD_ORDER) ? a_mod->info->load_pri : 128;
|
|
|
|
unsigned char b_pri = ast_test_flag(b_mod->info, AST_MODFLAG_LOAD_ORDER) ? b_mod->info->load_pri : 128;
|
2009-06-09 16:22:04 +00:00
|
|
|
if (a_pri == b_pri) {
|
|
|
|
res = 0;
|
|
|
|
} else if (a_pri < b_pri) {
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-03-22 19:51:16 +00:00
|
|
|
/*! loads modules in order by load_pri, updates mod_count
|
2009-11-13 08:52:28 +00:00
|
|
|
\return -1 on failure to load module, -2 on failure to load required module, otherwise 0
|
|
|
|
*/
|
2009-06-09 16:22:04 +00:00
|
|
|
static int load_resource_list(struct load_order *load_order, unsigned int global_symbols, int *mod_count)
|
|
|
|
{
|
|
|
|
struct ast_heap *resource_heap;
|
|
|
|
struct load_order_entry *order;
|
|
|
|
struct ast_module *mod;
|
|
|
|
int count = 0;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if(!(resource_heap = ast_heap_create(8, mod_load_cmp, -1))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first, add find and add modules to heap */
|
|
|
|
AST_LIST_TRAVERSE_SAFE_BEGIN(load_order, order, entry) {
|
2009-11-13 08:52:28 +00:00
|
|
|
switch (load_resource(order->resource, global_symbols, resource_heap, order->required)) {
|
2009-06-09 16:22:04 +00:00
|
|
|
case AST_MODULE_LOAD_SUCCESS:
|
|
|
|
case AST_MODULE_LOAD_DECLINE:
|
|
|
|
AST_LIST_REMOVE_CURRENT(entry);
|
|
|
|
ast_free(order->resource);
|
|
|
|
ast_free(order);
|
|
|
|
break;
|
|
|
|
case AST_MODULE_LOAD_FAILURE:
|
2009-11-13 08:52:28 +00:00
|
|
|
ast_log(LOG_ERROR, "*** Failed to load module %s - %s\n", order->resource, order->required ? "Required" : "Not required");
|
|
|
|
fprintf(stderr, "*** Failed to load module %s - %s\n", order->resource, order->required ? "Required" : "Not required");
|
|
|
|
res = order->required ? -2 : -1;
|
2009-06-09 16:22:04 +00:00
|
|
|
goto done;
|
|
|
|
case AST_MODULE_LOAD_SKIP:
|
|
|
|
break;
|
2009-06-22 15:33:35 +00:00
|
|
|
case AST_MODULE_LOAD_PRIORITY:
|
|
|
|
AST_LIST_REMOVE_CURRENT(entry);
|
|
|
|
break;
|
2009-06-09 16:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AST_LIST_TRAVERSE_SAFE_END;
|
|
|
|
|
|
|
|
/* second remove modules from heap sorted by priority */
|
|
|
|
while ((mod = ast_heap_pop(resource_heap))) {
|
|
|
|
switch (start_resource(mod)) {
|
|
|
|
case AST_MODULE_LOAD_SUCCESS:
|
|
|
|
count++;
|
|
|
|
case AST_MODULE_LOAD_DECLINE:
|
|
|
|
break;
|
|
|
|
case AST_MODULE_LOAD_FAILURE:
|
|
|
|
res = -1;
|
|
|
|
goto done;
|
|
|
|
case AST_MODULE_LOAD_SKIP:
|
2009-06-22 15:33:35 +00:00
|
|
|
case AST_MODULE_LOAD_PRIORITY:
|
2009-06-09 16:22:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (mod_count) {
|
|
|
|
*mod_count += count;
|
|
|
|
}
|
|
|
|
ast_heap_destroy(resource_heap);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-08-21 22:23:26 +00:00
|
|
|
int load_modules(unsigned int preload_only)
|
2006-08-21 02:11:39 +00:00
|
|
|
{
|
|
|
|
struct ast_config *cfg;
|
|
|
|
struct ast_module *mod;
|
|
|
|
struct load_order_entry *order;
|
|
|
|
struct ast_variable *v;
|
|
|
|
unsigned int load_count;
|
|
|
|
struct load_order load_order;
|
|
|
|
int res = 0;
|
2007-08-16 21:09:46 +00:00
|
|
|
struct ast_flags config_flags = { 0 };
|
2007-11-27 21:04:29 +00:00
|
|
|
int modulecount = 0;
|
2008-06-12 17:27:55 +00:00
|
|
|
|
2007-10-22 13:36:13 +00:00
|
|
|
#ifdef LOADABLE_MODULES
|
2006-08-21 03:06:41 +00:00
|
|
|
struct dirent *dirent;
|
|
|
|
DIR *dir;
|
|
|
|
#endif
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
/* all embedded modules have registered themselves by now */
|
|
|
|
embedding = 0;
|
|
|
|
|
2007-07-26 15:49:18 +00:00
|
|
|
ast_verb(1, "Asterisk Dynamic Loader Starting:\n");
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2007-05-03 16:43:49 +00:00
|
|
|
AST_LIST_HEAD_INIT_NOLOCK(&load_order);
|
|
|
|
|
|
|
|
AST_LIST_LOCK(&module_list);
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2007-11-06 17:05:13 +00:00
|
|
|
if (embedded_module_list.first) {
|
|
|
|
module_list.first = embedded_module_list.first;
|
|
|
|
module_list.last = embedded_module_list.last;
|
|
|
|
embedded_module_list.first = NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-12 23:30:03 +00:00
|
|
|
cfg = ast_config_load2(AST_MODULE_CONFIG, "" /* core, can't reload */, config_flags);
|
|
|
|
if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
|
2007-05-03 16:43:49 +00:00
|
|
|
goto done;
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 23:06:13 +00:00
|
|
|
/* first, find all the modules we have been explicitly requested to load */
|
|
|
|
for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
|
2008-06-12 17:27:55 +00:00
|
|
|
if (!strcasecmp(v->name, preload_only ? "preload" : "load")) {
|
2009-11-13 08:52:28 +00:00
|
|
|
add_to_load_order(v->value, &load_order, 0);
|
|
|
|
}
|
|
|
|
if (!strcasecmp(v->name, preload_only ? "preload-require" : "require")) {
|
|
|
|
/* Add the module to the list and make sure it's required */
|
|
|
|
add_to_load_order(v->value, &load_order, 1);
|
|
|
|
ast_debug(2, "Adding module to required list: %s (%s)\n", v->value, v->name);
|
2008-06-12 17:27:55 +00:00
|
|
|
}
|
2009-11-13 08:52:28 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if 'autoload' is on */
|
2006-08-21 22:23:26 +00:00
|
|
|
if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
|
2007-05-03 16:43:49 +00:00
|
|
|
/* if so, first add all the embedded modules that are not already running to the load order */
|
|
|
|
AST_LIST_TRAVERSE(&module_list, mod, entry) {
|
|
|
|
/* if it's not embedded, skip it */
|
|
|
|
if (mod->lib)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mod->flags.running)
|
|
|
|
continue;
|
|
|
|
|
2012-06-11 15:23:30 +00:00
|
|
|
add_to_load_order(mod->resource, &load_order, 0);
|
2007-05-03 16:43:49 +00:00
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2007-10-22 13:36:13 +00:00
|
|
|
#ifdef LOADABLE_MODULES
|
2006-08-21 02:11:39 +00:00
|
|
|
/* if we are allowed to load dynamic modules, scan the directory for
|
|
|
|
for all available modules and add them as well */
|
2012-06-11 15:23:30 +00:00
|
|
|
if ((dir = opendir(ast_config_AST_MODULE_DIR))) {
|
2006-08-21 02:11:39 +00:00
|
|
|
while ((dirent = readdir(dir))) {
|
|
|
|
int ld = strlen(dirent->d_name);
|
2006-09-15 05:00:27 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
/* Must end in .so to load it. */
|
|
|
|
|
|
|
|
if (ld < 4)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcasecmp(dirent->d_name + ld - 3, ".so"))
|
2007-05-03 16:43:49 +00:00
|
|
|
continue;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2007-05-03 16:43:49 +00:00
|
|
|
/* if there is already a module by this name in the module_list,
|
|
|
|
skip this file */
|
|
|
|
if (find_resource(dirent->d_name, 0))
|
|
|
|
continue;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
2009-11-13 08:52:28 +00:00
|
|
|
add_to_load_order(dirent->d_name, &load_order, 0);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
} else {
|
|
|
|
if (!ast_opt_quiet)
|
|
|
|
ast_log(LOG_WARNING, "Unable to open modules directory '%s'.\n",
|
|
|
|
ast_config_AST_MODULE_DIR);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now scan the config for any modules we are prohibited from loading and
|
|
|
|
remove them from the load order */
|
|
|
|
for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
|
|
|
|
if (strcasecmp(v->name, "noload"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
AST_LIST_TRAVERSE_SAFE_BEGIN(&load_order, order, entry) {
|
2008-07-28 16:42:00 +00:00
|
|
|
if (!resource_name_match(order->resource, v->value)) {
|
2007-11-08 05:28:47 +00:00
|
|
|
AST_LIST_REMOVE_CURRENT(entry);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(order->resource);
|
|
|
|
ast_free(order);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AST_LIST_TRAVERSE_SAFE_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we are done with the config now, all the information we need is in the
|
|
|
|
load_order list */
|
|
|
|
ast_config_destroy(cfg);
|
|
|
|
|
|
|
|
load_count = 0;
|
|
|
|
AST_LIST_TRAVERSE(&load_order, order, entry)
|
|
|
|
load_count++;
|
|
|
|
|
2006-08-22 23:06:13 +00:00
|
|
|
if (load_count)
|
|
|
|
ast_log(LOG_NOTICE, "%d modules will be loaded.\n", load_count);
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
/* first, load only modules that provide global symbols */
|
2009-06-09 16:22:04 +00:00
|
|
|
if ((res = load_resource_list(&load_order, 1, &modulecount)) < 0) {
|
|
|
|
goto done;
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now load everything else */
|
2009-06-09 16:22:04 +00:00
|
|
|
if ((res = load_resource_list(&load_order, 0, &modulecount)) < 0) {
|
|
|
|
goto done;
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(order->resource);
|
|
|
|
ast_free(order);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
2007-05-03 16:43:49 +00:00
|
|
|
AST_LIST_UNLOCK(&module_list);
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2007-11-27 21:04:29 +00:00
|
|
|
/* Tell manager clients that are aggressive at logging in that we're done
|
|
|
|
loading modules. If there's a DNS problem in chan_sip, we might not
|
|
|
|
even reach this */
|
2012-07-10 22:26:27 +00:00
|
|
|
/*** DOCUMENTATION
|
|
|
|
<managerEventInstance>
|
|
|
|
<synopsis>Raised when all dynamic modules have finished their initial loading.</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="ModuleSelection">
|
|
|
|
<enumlist>
|
|
|
|
<enum name="Preload"/>
|
|
|
|
<enum name="All"/>
|
|
|
|
</enumlist>
|
|
|
|
</parameter>
|
|
|
|
</syntax>
|
|
|
|
</managerEventInstance>
|
|
|
|
***/
|
2007-11-27 21:04:29 +00:00
|
|
|
manager_event(EVENT_FLAG_SYSTEM, "ModuleLoadReport", "ModuleLoadStatus: Done\r\nModuleSelection: %s\r\nModuleCount: %d\r\n", preload_only ? "Preload" : "All", modulecount);
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_update_use_count(void)
|
|
|
|
{
|
2006-09-15 05:00:27 +00:00
|
|
|
/* Notify any module monitors that the use count for a
|
2006-08-21 02:11:39 +00:00
|
|
|
resource has changed */
|
|
|
|
struct loadupdate *m;
|
|
|
|
|
2008-02-27 17:12:08 +00:00
|
|
|
AST_LIST_LOCK(&updaters);
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_TRAVERSE(&updaters, m, entry)
|
|
|
|
m->updater();
|
2008-02-27 17:12:08 +00:00
|
|
|
AST_LIST_UNLOCK(&updaters);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
2013-02-19 17:17:10 +00:00
|
|
|
int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *status, const char *like),
|
2006-08-21 02:11:39 +00:00
|
|
|
const char *like)
|
|
|
|
{
|
|
|
|
struct ast_module *cur;
|
|
|
|
int unlock = -1;
|
|
|
|
int total_mod_loaded = 0;
|
|
|
|
|
|
|
|
if (AST_LIST_TRYLOCK(&module_list))
|
|
|
|
unlock = 0;
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_TRAVERSE(&module_list, cur, entry) {
|
2013-02-19 17:17:10 +00:00
|
|
|
total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount,
|
|
|
|
cur->flags.running ? "Running" : "Not Running", like);
|
2006-08-21 02:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlock)
|
|
|
|
AST_LIST_UNLOCK(&module_list);
|
|
|
|
|
|
|
|
return total_mod_loaded;
|
|
|
|
}
|
|
|
|
|
2006-10-31 08:08:56 +00:00
|
|
|
/*! \brief Check if module exists */
|
2007-05-07 19:03:53 +00:00
|
|
|
int ast_module_check(const char *name)
|
2006-10-30 21:48:41 +00:00
|
|
|
{
|
|
|
|
struct ast_module *cur;
|
|
|
|
|
|
|
|
if (ast_strlen_zero(name))
|
|
|
|
return 0; /* FALSE */
|
|
|
|
|
2006-10-31 08:08:56 +00:00
|
|
|
cur = find_resource(name, 1);
|
|
|
|
|
|
|
|
return (cur != NULL);
|
2006-10-30 21:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-15 05:00:27 +00:00
|
|
|
int ast_loader_register(int (*v)(void))
|
2006-08-21 02:11:39 +00:00
|
|
|
{
|
2006-09-15 05:00:27 +00:00
|
|
|
struct loadupdate *tmp;
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
if (!(tmp = ast_malloc(sizeof(*tmp))))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tmp->updater = v;
|
2008-02-27 17:12:08 +00:00
|
|
|
AST_LIST_LOCK(&updaters);
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
|
2008-02-27 17:12:08 +00:00
|
|
|
AST_LIST_UNLOCK(&updaters);
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_loader_unregister(int (*v)(void))
|
|
|
|
{
|
|
|
|
struct loadupdate *cur;
|
|
|
|
|
2008-02-27 17:12:08 +00:00
|
|
|
AST_LIST_LOCK(&updaters);
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
|
|
|
|
if (cur->updater == v) {
|
2007-11-08 05:28:47 +00:00
|
|
|
AST_LIST_REMOVE_CURRENT(entry);
|
2006-08-21 02:11:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AST_LIST_TRAVERSE_SAFE_END;
|
2008-02-27 17:12:08 +00:00
|
|
|
AST_LIST_UNLOCK(&updaters);
|
2006-08-21 02:11:39 +00:00
|
|
|
|
|
|
|
return cur ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_module *ast_module_ref(struct ast_module *mod)
|
|
|
|
{
|
2010-02-17 07:01:13 +00:00
|
|
|
if (!mod) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_atomic_fetchadd_int(&mod->usecount, +1);
|
|
|
|
ast_update_use_count();
|
|
|
|
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_module_unref(struct ast_module *mod)
|
|
|
|
{
|
2010-02-17 07:01:13 +00:00
|
|
|
if (!mod) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_atomic_fetchadd_int(&mod->usecount, -1);
|
|
|
|
ast_update_use_count();
|
|
|
|
}
|