1999-10-28 20:38:23 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- A telephony toolkit for Linux.
|
|
|
|
*
|
|
|
|
* Module Loader
|
|
|
|
*
|
2005-01-21 07:06:25 +00:00
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
1999-10-28 20:38:23 +00:00
|
|
|
*
|
2004-09-02 20:45:24 +00:00
|
|
|
* Mark Spencer <markster@digium.com>
|
1999-10-28 20:38:23 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-04-22 13:11:34 +00:00
|
|
|
|
2005-06-06 20:27:51 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
2005-06-06 22:12:19 +00:00
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
2005-06-06 20:27:51 +00:00
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/options.h"
|
|
|
|
#include "asterisk/config.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/term.h"
|
|
|
|
#include "asterisk/manager.h"
|
2005-06-03 01:42:31 +00:00
|
|
|
#include "asterisk/cdr.h"
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/enum.h"
|
|
|
|
#include "asterisk/rtp.h"
|
|
|
|
#include "asterisk/lock.h"
|
2003-10-26 18:50:49 +00:00
|
|
|
#ifdef __APPLE__
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/dlfcn-compat.h"
|
2003-10-26 18:50:49 +00:00
|
|
|
#else
|
1999-10-28 20:38:23 +00:00
|
|
|
#include <dlfcn.h>
|
2003-10-26 18:50:49 +00:00
|
|
|
#endif
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/md5.h"
|
1999-10-28 20:38:23 +00:00
|
|
|
|
2003-04-23 19:13:35 +00:00
|
|
|
#ifndef RTLD_NOW
|
|
|
|
#define RTLD_NOW 0
|
|
|
|
#endif
|
|
|
|
|
2005-07-25 15:32:06 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(modlock);
|
|
|
|
AST_MUTEX_DEFINE_STATIC(reloadlock);
|
|
|
|
|
|
|
|
static struct module *module_list=NULL;
|
|
|
|
static int modlistver = 0;
|
|
|
|
|
2005-08-05 16:29:30 +00:00
|
|
|
static unsigned char expected_key[] =
|
2001-03-07 00:52:22 +00:00
|
|
|
{ 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
|
|
|
|
0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
|
|
|
|
|
1999-10-28 20:38:23 +00:00
|
|
|
struct module {
|
|
|
|
int (*load_module)(void);
|
|
|
|
int (*unload_module)(void);
|
|
|
|
int (*usecount)(void);
|
|
|
|
char *(*description)(void);
|
2001-03-07 00:52:22 +00:00
|
|
|
char *(*key)(void);
|
|
|
|
int (*reload)(void);
|
1999-10-28 20:38:23 +00:00
|
|
|
void *lib;
|
|
|
|
char resource[256];
|
|
|
|
struct module *next;
|
|
|
|
};
|
|
|
|
|
2005-07-25 15:32:06 +00:00
|
|
|
static struct loadupdate {
|
|
|
|
int (*updater)(void);
|
|
|
|
struct loadupdate *next;
|
|
|
|
} *updaters = NULL;
|
|
|
|
|
2001-03-07 00:52:22 +00:00
|
|
|
static int printdigest(unsigned char *d)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
char buf[256];
|
|
|
|
char buf2[16];
|
|
|
|
snprintf(buf, sizeof(buf), "Unexpected signature:");
|
2005-07-25 15:32:06 +00:00
|
|
|
for (x=0; x<16; x++) {
|
2001-03-07 00:52:22 +00:00
|
|
|
snprintf(buf2, sizeof(buf2), " %02x", *(d++));
|
|
|
|
strcat(buf, buf2);
|
|
|
|
}
|
|
|
|
strcat(buf, "\n");
|
2004-12-27 22:41:58 +00:00
|
|
|
ast_log(LOG_DEBUG, "%s", buf);
|
2001-03-07 00:52:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-05 16:29:30 +00:00
|
|
|
static int key_matches(unsigned char *key1, unsigned char *key2)
|
2001-03-07 00:52:22 +00:00
|
|
|
{
|
|
|
|
int match = 1;
|
|
|
|
int x;
|
2005-07-25 15:32:06 +00:00
|
|
|
for (x=0; x<16; x++) {
|
2001-03-07 00:52:22 +00:00
|
|
|
match &= (key1[x] == key2[x]);
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2005-08-05 16:29:30 +00:00
|
|
|
static int verify_key(unsigned char *key)
|
2001-03-07 00:52:22 +00:00
|
|
|
{
|
|
|
|
struct MD5Context c;
|
2005-08-05 16:29:30 +00:00
|
|
|
unsigned char digest[16];
|
2001-03-07 00:52:22 +00:00
|
|
|
MD5Init(&c);
|
2005-08-05 16:29:30 +00:00
|
|
|
MD5Update(&c, key, strlen((char *)key));
|
2001-03-07 00:52:22 +00:00
|
|
|
MD5Final(digest, &c);
|
|
|
|
if (key_matches(expected_key, digest))
|
|
|
|
return 0;
|
|
|
|
printdigest(digest);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-16 22:59:30 +00:00
|
|
|
int ast_unload_resource(const char *resource_name, int force)
|
1999-10-28 20:38:23 +00:00
|
|
|
{
|
|
|
|
struct module *m, *ml = NULL;
|
|
|
|
int res = -1;
|
2003-08-13 15:25:16 +00:00
|
|
|
if (ast_mutex_lock(&modlock))
|
1999-10-28 20:38:23 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to lock\n");
|
|
|
|
m = module_list;
|
|
|
|
while(m) {
|
|
|
|
if (!strcasecmp(m->resource, resource_name)) {
|
|
|
|
if ((res = m->usecount()) > 0) {
|
|
|
|
if (force)
|
|
|
|
ast_log(LOG_WARNING, "Warning: Forcing removal of module %s with use count %d\n", resource_name, res);
|
|
|
|
else {
|
|
|
|
ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = m->unload_module();
|
|
|
|
if (res) {
|
|
|
|
ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
|
|
|
|
if (force <= AST_FORCE_FIRM) {
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
|
|
|
|
}
|
|
|
|
if (ml)
|
|
|
|
ml->next = m->next;
|
|
|
|
else
|
|
|
|
module_list = m->next;
|
|
|
|
dlclose(m->lib);
|
|
|
|
free(m);
|
2004-02-19 01:04:21 +00:00
|
|
|
break;
|
1999-10-28 20:38:23 +00:00
|
|
|
}
|
|
|
|
ml = m;
|
|
|
|
m = m->next;
|
|
|
|
}
|
2004-11-17 13:40:47 +00:00
|
|
|
modlistver = rand();
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
ast_update_use_count();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-10-16 21:14:05 +00:00
|
|
|
char *ast_module_helper(char *line, char *word, int pos, int state, int rpos, int needsreload)
|
2001-04-13 04:06:31 +00:00
|
|
|
{
|
|
|
|
struct module *m;
|
2004-10-16 21:14:05 +00:00
|
|
|
int which=0;
|
|
|
|
char *ret;
|
2005-07-25 15:32:06 +00:00
|
|
|
|
2004-10-16 21:14:05 +00:00
|
|
|
if (pos != rpos)
|
|
|
|
return NULL;
|
|
|
|
ast_mutex_lock(&modlock);
|
|
|
|
m = module_list;
|
|
|
|
while(m) {
|
|
|
|
if (!strncasecmp(word, m->resource, strlen(word)) && (m->reload || !needsreload)) {
|
|
|
|
if (++which > state)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
if (m) {
|
|
|
|
ret = strdup(m->resource);
|
|
|
|
} else {
|
|
|
|
ret = NULL;
|
2005-01-25 06:10:20 +00:00
|
|
|
if (!strncasecmp(word, "extconfig", strlen(word))) {
|
2004-10-16 21:14:05 +00:00
|
|
|
if (++which > state)
|
2005-01-25 06:10:20 +00:00
|
|
|
ret = strdup("extconfig");
|
2004-10-16 21:14:05 +00:00
|
|
|
} else if (!strncasecmp(word, "manager", strlen(word))) {
|
|
|
|
if (++which > state)
|
|
|
|
ret = strdup("manager");
|
|
|
|
} else if (!strncasecmp(word, "enum", strlen(word))) {
|
|
|
|
if (++which > state)
|
|
|
|
ret = strdup("enum");
|
|
|
|
} else if (!strncasecmp(word, "rtp", strlen(word))) {
|
|
|
|
if (++which > state)
|
|
|
|
ret = strdup("rtp");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ast_mutex_unlock(&modlock);
|
|
|
|
return ret;
|
|
|
|
}
|
2002-02-11 04:25:04 +00:00
|
|
|
|
2004-10-16 21:14:05 +00:00
|
|
|
int ast_module_reload(const char *name)
|
|
|
|
{
|
|
|
|
struct module *m;
|
|
|
|
int reloaded = 0;
|
2004-11-17 13:40:47 +00:00
|
|
|
int oldversion;
|
|
|
|
int (*reload)(void);
|
2002-07-11 16:49:29 +00:00
|
|
|
/* We'll do the logger and manager the favor of calling its reload here first */
|
2003-11-19 21:19:18 +00:00
|
|
|
|
|
|
|
if (ast_mutex_trylock(&reloadlock)) {
|
|
|
|
ast_verbose("The previous reload command didn't finish yet\n");
|
2004-10-16 21:14:05 +00:00
|
|
|
return -1;
|
2003-11-19 21:19:18 +00:00
|
|
|
}
|
2005-01-25 06:10:20 +00:00
|
|
|
if (!name || !strcasecmp(name, "extconfig")) {
|
|
|
|
read_config_maps();
|
2004-10-16 21:14:05 +00:00
|
|
|
reloaded = 2;
|
|
|
|
}
|
|
|
|
if (!name || !strcasecmp(name, "manager")) {
|
2004-09-03 14:02:12 +00:00
|
|
|
reload_manager();
|
2004-10-16 21:14:05 +00:00
|
|
|
reloaded = 2;
|
|
|
|
}
|
2005-06-03 01:42:31 +00:00
|
|
|
if (!name || !strcasecmp(name, "cdr")) {
|
|
|
|
ast_cdr_engine_reload();
|
|
|
|
reloaded = 2;
|
|
|
|
}
|
2004-10-16 21:14:05 +00:00
|
|
|
if (!name || !strcasecmp(name, "enum")) {
|
2004-09-03 14:02:12 +00:00
|
|
|
ast_enum_reload();
|
2004-10-16 21:14:05 +00:00
|
|
|
reloaded = 2;
|
|
|
|
}
|
|
|
|
if (!name || !strcasecmp(name, "rtp")) {
|
2004-09-03 14:02:12 +00:00
|
|
|
ast_rtp_reload();
|
2004-10-16 21:14:05 +00:00
|
|
|
reloaded = 2;
|
|
|
|
}
|
2005-04-06 21:12:32 +00:00
|
|
|
if (!name || !strcasecmp(name, "dnsmgr")) {
|
|
|
|
dnsmgr_reload();
|
|
|
|
reloaded = 2;
|
|
|
|
}
|
2003-05-02 15:37:34 +00:00
|
|
|
time(&ast_lastreloadtime);
|
2002-02-11 04:25:04 +00:00
|
|
|
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&modlock);
|
2004-11-17 13:40:47 +00:00
|
|
|
oldversion = modlistver;
|
2001-04-13 04:06:31 +00:00
|
|
|
m = module_list;
|
|
|
|
while(m) {
|
2004-09-02 20:45:24 +00:00
|
|
|
if (!name || !strcasecmp(name, m->resource)) {
|
2004-10-16 21:14:05 +00:00
|
|
|
if (reloaded < 1)
|
|
|
|
reloaded = 1;
|
2004-11-17 13:40:47 +00:00
|
|
|
reload = m->reload;
|
|
|
|
ast_mutex_unlock(&modlock);
|
|
|
|
if (reload) {
|
2004-10-16 21:14:05 +00:00
|
|
|
reloaded = 2;
|
2004-09-02 20:45:24 +00:00
|
|
|
if (option_verbose > 2)
|
|
|
|
ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
|
2004-11-17 13:40:47 +00:00
|
|
|
reload();
|
2004-09-02 20:45:24 +00:00
|
|
|
}
|
2004-11-17 13:40:47 +00:00
|
|
|
ast_mutex_lock(&modlock);
|
|
|
|
if (oldversion != modlistver)
|
|
|
|
break;
|
2001-04-13 04:06:31 +00:00
|
|
|
}
|
|
|
|
m = m->next;
|
|
|
|
}
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
2003-11-19 21:19:18 +00:00
|
|
|
ast_mutex_unlock(&reloadlock);
|
2004-10-16 21:14:05 +00:00
|
|
|
return reloaded;
|
2001-04-13 04:06:31 +00:00
|
|
|
}
|
|
|
|
|
2005-01-16 22:59:30 +00:00
|
|
|
static int __load_resource(const char *resource_name, const struct ast_config *cfg)
|
1999-10-28 20:38:23 +00:00
|
|
|
{
|
|
|
|
static char fn[256];
|
|
|
|
int errors=0;
|
|
|
|
int res;
|
1999-12-16 22:47:49 +00:00
|
|
|
struct module *m;
|
2002-05-14 03:17:45 +00:00
|
|
|
int flags=RTLD_NOW;
|
2003-09-08 16:48:07 +00:00
|
|
|
#ifdef RTLD_GLOBAL
|
2000-01-02 20:59:00 +00:00
|
|
|
char *val;
|
2003-09-08 16:48:07 +00:00
|
|
|
#endif
|
2005-08-05 16:29:30 +00:00
|
|
|
unsigned char *key;
|
2002-05-14 03:17:45 +00:00
|
|
|
char tmp[80];
|
2005-01-16 22:59:30 +00:00
|
|
|
|
2001-11-02 01:22:37 +00:00
|
|
|
if (strncasecmp(resource_name, "res_", 4)) {
|
2003-04-23 19:13:35 +00:00
|
|
|
#ifdef RTLD_GLOBAL
|
2005-01-16 22:59:30 +00:00
|
|
|
if (cfg) {
|
2001-11-02 01:22:37 +00:00
|
|
|
if ((val = ast_variable_retrieve(cfg, "global", resource_name))
|
|
|
|
&& ast_true(val))
|
|
|
|
flags |= RTLD_GLOBAL;
|
|
|
|
}
|
2005-01-16 22:59:30 +00:00
|
|
|
#endif
|
2001-11-02 01:22:37 +00:00
|
|
|
} else {
|
2002-05-14 03:17:45 +00:00
|
|
|
/* Resource modules are always loaded global and lazy */
|
2003-04-23 19:13:35 +00:00
|
|
|
#ifdef RTLD_GLOBAL
|
2002-05-14 03:17:45 +00:00
|
|
|
flags = (RTLD_GLOBAL | RTLD_LAZY);
|
2003-04-23 19:13:35 +00:00
|
|
|
#else
|
|
|
|
flags = RTLD_LAZY;
|
|
|
|
#endif
|
2000-01-02 20:59:00 +00:00
|
|
|
}
|
|
|
|
|
2003-08-13 15:25:16 +00:00
|
|
|
if (ast_mutex_lock(&modlock))
|
1999-12-16 22:47:49 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to lock\n");
|
|
|
|
m = module_list;
|
|
|
|
while(m) {
|
|
|
|
if (!strcasecmp(m->resource, resource_name)) {
|
|
|
|
ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-12-16 22:47:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
m = malloc(sizeof(struct module));
|
1999-10-28 20:38:23 +00:00
|
|
|
if (!m) {
|
|
|
|
ast_log(LOG_WARNING, "Out of memory\n");
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-11-02 01:22:37 +00:00
|
|
|
strncpy(m->resource, resource_name, sizeof(m->resource)-1);
|
1999-10-28 20:38:23 +00:00
|
|
|
if (resource_name[0] == '/') {
|
2001-11-02 01:22:37 +00:00
|
|
|
strncpy(fn, resource_name, sizeof(fn)-1);
|
1999-10-28 20:38:23 +00:00
|
|
|
} else {
|
2003-01-30 15:03:20 +00:00
|
|
|
snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
|
1999-10-28 20:38:23 +00:00
|
|
|
}
|
2002-05-14 03:17:45 +00:00
|
|
|
m->lib = dlopen(fn, flags);
|
1999-10-28 20:38:23 +00:00
|
|
|
if (!m->lib) {
|
|
|
|
ast_log(LOG_WARNING, "%s\n", dlerror());
|
|
|
|
free(m);
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
m->load_module = dlsym(m->lib, "load_module");
|
2003-04-23 19:13:35 +00:00
|
|
|
if (m->load_module == NULL)
|
|
|
|
m->load_module = dlsym(m->lib, "_load_module");
|
1999-10-28 20:38:23 +00:00
|
|
|
if (!m->load_module) {
|
|
|
|
ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
m->unload_module = dlsym(m->lib, "unload_module");
|
2003-04-23 19:13:35 +00:00
|
|
|
if (m->unload_module == NULL)
|
|
|
|
m->unload_module = dlsym(m->lib, "_unload_module");
|
1999-10-28 20:38:23 +00:00
|
|
|
if (!m->unload_module) {
|
|
|
|
ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
m->usecount = dlsym(m->lib, "usecount");
|
2003-04-23 19:13:35 +00:00
|
|
|
if (m->usecount == NULL)
|
|
|
|
m->usecount = dlsym(m->lib, "_usecount");
|
1999-10-28 20:38:23 +00:00
|
|
|
if (!m->usecount) {
|
|
|
|
ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
m->description = dlsym(m->lib, "description");
|
2003-04-23 19:13:35 +00:00
|
|
|
if (m->description == NULL)
|
|
|
|
m->description = dlsym(m->lib, "_description");
|
1999-10-28 20:38:23 +00:00
|
|
|
if (!m->description) {
|
|
|
|
ast_log(LOG_WARNING, "No description in module %s\n", fn);
|
|
|
|
errors++;
|
|
|
|
}
|
2001-03-07 00:52:22 +00:00
|
|
|
m->key = dlsym(m->lib, "key");
|
2003-04-23 19:13:35 +00:00
|
|
|
if (m->key == NULL)
|
|
|
|
m->key = dlsym(m->lib, "_key");
|
2001-03-07 00:52:22 +00:00
|
|
|
if (!m->key) {
|
|
|
|
ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
|
|
|
|
errors++;
|
|
|
|
}
|
2005-06-06 18:31:29 +00:00
|
|
|
|
2001-03-07 00:52:22 +00:00
|
|
|
m->reload = dlsym(m->lib, "reload");
|
2003-04-23 19:13:35 +00:00
|
|
|
if (m->reload == NULL)
|
|
|
|
m->reload = dlsym(m->lib, "_reload");
|
2005-06-06 18:31:29 +00:00
|
|
|
|
2005-08-05 16:29:30 +00:00
|
|
|
if (!m->key || !(key = (unsigned char *) m->key())) {
|
2001-03-07 00:52:22 +00:00
|
|
|
ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
|
|
|
|
key = NULL;
|
2004-04-29 04:13:06 +00:00
|
|
|
errors++;
|
|
|
|
}
|
2001-03-07 00:52:22 +00:00
|
|
|
if (key && verify_key(key)) {
|
|
|
|
ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
|
|
|
|
errors++;
|
|
|
|
}
|
1999-10-28 20:38:23 +00:00
|
|
|
if (errors) {
|
2005-07-07 22:32:20 +00:00
|
|
|
ast_log(LOG_WARNING, "%d error%s loading module %s, aborted\n", errors, (errors != 1) ? "s" : "", fn);
|
1999-10-28 20:38:23 +00:00
|
|
|
dlclose(m->lib);
|
|
|
|
free(m);
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-01-02 20:59:00 +00:00
|
|
|
if (!fully_booted) {
|
|
|
|
if (option_verbose)
|
2002-05-14 03:17:45 +00:00
|
|
|
ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
|
2000-01-02 20:59:00 +00:00
|
|
|
if (option_console && !option_verbose)
|
|
|
|
ast_verbose( ".");
|
|
|
|
} else {
|
|
|
|
if (option_verbose)
|
|
|
|
ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
|
|
|
|
}
|
2004-04-14 21:10:00 +00:00
|
|
|
|
2004-10-14 04:38:29 +00:00
|
|
|
/* add module 'm' to end of module_list chain
|
|
|
|
so reload commands will be issued in same order modules were loaded */
|
2004-04-14 21:10:00 +00:00
|
|
|
m->next = NULL;
|
|
|
|
if (module_list == NULL) {
|
2004-10-14 04:38:29 +00:00
|
|
|
/* empty list so far, add at front */
|
2004-04-14 21:10:00 +00:00
|
|
|
module_list = m;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct module *i;
|
2004-10-14 04:38:29 +00:00
|
|
|
/* find end of chain, and add there */
|
2004-04-14 21:10:00 +00:00
|
|
|
for (i = module_list; i->next; i = i->next)
|
|
|
|
;
|
|
|
|
i->next = m;
|
|
|
|
}
|
2001-03-07 00:52:22 +00:00
|
|
|
|
2004-11-17 13:40:47 +00:00
|
|
|
modlistver = rand();
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
if ((res = m->load_module())) {
|
2000-01-02 20:59:00 +00:00
|
|
|
ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
|
1999-12-16 22:47:49 +00:00
|
|
|
ast_unload_resource(resource_name, 0);
|
1999-10-28 20:38:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-01-02 20:59:00 +00:00
|
|
|
ast_update_use_count();
|
1999-10-28 20:38:23 +00:00
|
|
|
return 0;
|
2005-01-16 22:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_load_resource(const char *resource_name)
|
|
|
|
{
|
|
|
|
int o;
|
|
|
|
struct ast_config *cfg = NULL;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* Keep the module file parsing silent */
|
|
|
|
o = option_verbose;
|
|
|
|
option_verbose = 0;
|
2005-01-25 06:10:20 +00:00
|
|
|
cfg = ast_config_load(AST_MODULE_CONFIG);
|
2005-01-16 22:59:30 +00:00
|
|
|
option_verbose = o;
|
|
|
|
res = __load_resource(resource_name, cfg);
|
|
|
|
if (cfg)
|
2005-01-25 06:10:20 +00:00
|
|
|
ast_config_destroy(cfg);
|
2005-01-16 22:59:30 +00:00
|
|
|
return res;
|
1999-10-28 20:38:23 +00:00
|
|
|
}
|
|
|
|
|
1999-12-16 22:47:49 +00:00
|
|
|
static int ast_resource_exists(char *resource)
|
1999-10-28 20:38:23 +00:00
|
|
|
{
|
|
|
|
struct module *m;
|
2003-08-13 15:25:16 +00:00
|
|
|
if (ast_mutex_lock(&modlock))
|
1999-10-28 20:38:23 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to lock\n");
|
|
|
|
m = module_list;
|
|
|
|
while(m) {
|
|
|
|
if (!strcasecmp(resource, m->resource))
|
|
|
|
break;
|
|
|
|
m = m->next;
|
|
|
|
}
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
if (m)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-13 22:44:33 +00:00
|
|
|
static const char *loadorder[] =
|
|
|
|
{
|
|
|
|
"res_",
|
|
|
|
"chan_",
|
|
|
|
"pbx_",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2005-07-05 22:11:43 +00:00
|
|
|
int load_modules(const int preload_only)
|
1999-10-28 20:38:23 +00:00
|
|
|
{
|
|
|
|
struct ast_config *cfg;
|
|
|
|
struct ast_variable *v;
|
2002-05-14 03:17:45 +00:00
|
|
|
char tmp[80];
|
2005-07-05 22:11:43 +00:00
|
|
|
|
|
|
|
if (option_verbose) {
|
|
|
|
if (preload_only)
|
|
|
|
ast_verbose("Asterisk Dynamic Loader loading preload modules:\n");
|
|
|
|
else
|
|
|
|
ast_verbose("Asterisk Dynamic Loader Starting:\n");
|
|
|
|
}
|
|
|
|
|
2005-01-25 06:10:20 +00:00
|
|
|
cfg = ast_config_load(AST_MODULE_CONFIG);
|
1999-10-28 20:38:23 +00:00
|
|
|
if (cfg) {
|
2005-07-05 22:11:43 +00:00
|
|
|
int doload;
|
|
|
|
|
1999-10-28 20:38:23 +00:00
|
|
|
/* Load explicitly defined modules */
|
2005-07-05 22:11:43 +00:00
|
|
|
for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
|
|
|
|
doload = 0;
|
|
|
|
|
|
|
|
if (preload_only)
|
|
|
|
doload = !strcasecmp(v->name, "preload");
|
|
|
|
else
|
|
|
|
doload = !strcasecmp(v->name, "load");
|
|
|
|
|
|
|
|
if (doload) {
|
1999-10-28 20:38:23 +00:00
|
|
|
if (option_debug && !option_verbose)
|
|
|
|
ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
|
|
|
|
if (option_verbose) {
|
2005-07-05 22:11:43 +00:00
|
|
|
ast_verbose(VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
|
1999-10-28 20:38:23 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
2005-01-16 22:59:30 +00:00
|
|
|
if (__load_resource(v->value, cfg)) {
|
1999-10-28 20:38:23 +00:00
|
|
|
ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
|
2005-07-05 22:11:43 +00:00
|
|
|
ast_config_destroy(cfg);
|
1999-10-28 20:38:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-05 22:11:43 +00:00
|
|
|
|
|
|
|
if (preload_only) {
|
|
|
|
ast_config_destroy(cfg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-10-28 20:38:23 +00:00
|
|
|
if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
|
|
|
|
/* Load all modules */
|
|
|
|
DIR *mods;
|
|
|
|
struct dirent *d;
|
2001-11-02 01:22:37 +00:00
|
|
|
int x;
|
2005-07-25 15:32:06 +00:00
|
|
|
|
2004-11-13 22:44:33 +00:00
|
|
|
/* Loop through each order */
|
2005-07-25 15:32:06 +00:00
|
|
|
for (x=0; x<sizeof(loadorder) / sizeof(loadorder[0]); x++) {
|
2003-01-30 15:03:20 +00:00
|
|
|
mods = opendir((char *)ast_config_AST_MODULE_DIR);
|
2001-11-02 01:22:37 +00:00
|
|
|
if (mods) {
|
|
|
|
while((d = readdir(mods))) {
|
|
|
|
/* Must end in .so to load it. */
|
2004-11-13 22:44:33 +00:00
|
|
|
if ((strlen(d->d_name) > 3) &&
|
|
|
|
(!loadorder[x] || !strncasecmp(d->d_name, loadorder[x], strlen(loadorder[x]))) &&
|
2001-11-02 01:22:37 +00:00
|
|
|
!strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
|
|
|
|
!ast_resource_exists(d->d_name)) {
|
|
|
|
/* It's a shared library -- Just be sure we're allowed to load it -- kinda
|
|
|
|
an inefficient way to do it, but oh well. */
|
|
|
|
if (cfg) {
|
|
|
|
v = ast_variable_browse(cfg, "modules");
|
|
|
|
while(v) {
|
|
|
|
if (!strcasecmp(v->name, "noload") &&
|
|
|
|
!strcasecmp(v->value, d->d_name))
|
|
|
|
break;
|
|
|
|
v = v->next;
|
|
|
|
}
|
|
|
|
if (v) {
|
|
|
|
if (option_verbose) {
|
|
|
|
ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
continue;
|
1999-10-28 20:38:23 +00:00
|
|
|
}
|
2001-11-02 01:22:37 +00:00
|
|
|
|
|
|
|
}
|
2005-07-25 15:32:06 +00:00
|
|
|
if (option_debug && !option_verbose)
|
2001-11-02 01:22:37 +00:00
|
|
|
ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
|
|
|
|
if (option_verbose) {
|
2002-05-14 03:17:45 +00:00
|
|
|
ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
|
2001-11-02 01:22:37 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
2005-01-16 22:59:30 +00:00
|
|
|
if (__load_resource(d->d_name, cfg)) {
|
2001-11-02 01:22:37 +00:00
|
|
|
ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
|
|
|
|
if (cfg)
|
2005-01-25 06:10:20 +00:00
|
|
|
ast_config_destroy(cfg);
|
2001-11-02 01:22:37 +00:00
|
|
|
return -1;
|
1999-10-28 20:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-02 01:22:37 +00:00
|
|
|
closedir(mods);
|
|
|
|
} else {
|
|
|
|
if (!option_quiet)
|
2003-01-30 15:03:20 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
|
2001-11-02 01:22:37 +00:00
|
|
|
}
|
1999-10-28 20:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-25 06:10:20 +00:00
|
|
|
ast_config_destroy(cfg);
|
1999-10-28 20:38:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_update_use_count(void)
|
|
|
|
{
|
|
|
|
/* Notify any module monitors that the use count for a
|
|
|
|
resource has changed */
|
|
|
|
struct loadupdate *m;
|
2003-08-13 15:25:16 +00:00
|
|
|
if (ast_mutex_lock(&modlock))
|
1999-10-28 20:38:23 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to lock\n");
|
|
|
|
m = updaters;
|
|
|
|
while(m) {
|
|
|
|
m->updater();
|
|
|
|
m = m->next;
|
|
|
|
}
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-06-06 20:27:51 +00:00
|
|
|
int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
|
2005-06-06 18:31:29 +00:00
|
|
|
const char *like)
|
1999-10-28 20:38:23 +00:00
|
|
|
{
|
|
|
|
struct module *m;
|
|
|
|
int unlock = -1;
|
2004-10-08 18:20:49 +00:00
|
|
|
int total_mod_loaded = 0;
|
2005-06-06 18:31:29 +00:00
|
|
|
|
2003-08-13 15:25:16 +00:00
|
|
|
if (ast_mutex_trylock(&modlock))
|
1999-10-28 20:38:23 +00:00
|
|
|
unlock = 0;
|
|
|
|
m = module_list;
|
2005-06-06 18:31:29 +00:00
|
|
|
while (m) {
|
2005-06-06 20:27:51 +00:00
|
|
|
total_mod_loaded += modentry(m->resource, m->description(), m->usecount(), like);
|
1999-10-28 20:38:23 +00:00
|
|
|
m = m->next;
|
|
|
|
}
|
|
|
|
if (unlock)
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
2005-06-06 18:31:29 +00:00
|
|
|
|
2004-10-08 18:20:49 +00:00
|
|
|
return total_mod_loaded;
|
1999-10-28 20:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_loader_register(int (*v)(void))
|
|
|
|
{
|
|
|
|
struct loadupdate *tmp;
|
|
|
|
/* XXX Should be more flexible here, taking > 1 verboser XXX */
|
|
|
|
if ((tmp = malloc(sizeof (struct loadupdate)))) {
|
|
|
|
tmp->updater = v;
|
2003-08-13 15:25:16 +00:00
|
|
|
if (ast_mutex_lock(&modlock))
|
1999-10-28 20:38:23 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to lock\n");
|
|
|
|
tmp->next = updaters;
|
|
|
|
updaters = tmp;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_loader_unregister(int (*v)(void))
|
|
|
|
{
|
|
|
|
int res = -1;
|
|
|
|
struct loadupdate *tmp, *tmpl=NULL;
|
2003-08-13 15:25:16 +00:00
|
|
|
if (ast_mutex_lock(&modlock))
|
1999-10-28 20:38:23 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to lock\n");
|
|
|
|
tmp = updaters;
|
|
|
|
while(tmp) {
|
|
|
|
if (tmp->updater == v) {
|
|
|
|
if (tmpl)
|
|
|
|
tmpl->next = tmp->next;
|
|
|
|
else
|
|
|
|
updaters = tmp->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmpl = tmp;
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
if (tmp)
|
|
|
|
res = 0;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&modlock);
|
1999-10-28 20:38:23 +00:00
|
|
|
return res;
|
|
|
|
}
|