Clean up dnsmgr.conf parsing

Review: https://reviewboard.asterisk.org/r/1432/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@335555 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Paul Belanger
2011-09-13 14:22:58 +00:00
parent 2a25779d47
commit 61b369ac76
2 changed files with 39 additions and 39 deletions

View File

@@ -33,6 +33,7 @@ Configuration Files:
with what is expected across modules. with what is expected across modules.
- cdr.conf: [general] section - cdr.conf: [general] section
- dnsmgr.conf
SIP SIP
=== ===

View File

@@ -109,8 +109,9 @@ struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *r
void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry) void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
{ {
if (!entry) if (!entry) {
return; return;
}
AST_RWLIST_WRLOCK(&entry_list); AST_RWLIST_WRLOCK(&entry_list);
AST_RWLIST_REMOVE(&entry_list, entry, list); AST_RWLIST_REMOVE(&entry_list, entry, list);
@@ -230,16 +231,18 @@ static int refresh_list(const void *data)
/* if a refresh or reload is already in progress, exit now */ /* if a refresh or reload is already in progress, exit now */
if (ast_mutex_trylock(&refresh_lock)) { if (ast_mutex_trylock(&refresh_lock)) {
if (info->verbose) if (info->verbose) {
ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n"); ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
}
return -1; return -1;
} }
ast_verb(3, "Refreshing DNS lookups.\n"); ast_verb(3, "Refreshing DNS lookups.\n");
AST_RWLIST_RDLOCK(info->entries); AST_RWLIST_RDLOCK(info->entries);
AST_RWLIST_TRAVERSE(info->entries, entry, list) { AST_RWLIST_TRAVERSE(info->entries, entry, list) {
if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) {
continue; continue;
}
dnsmgr_refresh(entry, info->verbose); dnsmgr_refresh(entry, info->verbose);
} }
@@ -273,8 +276,9 @@ static char *handle_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_
case CLI_GENERATE: case CLI_GENERATE:
return NULL; return NULL;
} }
if (a->argc > 2) if (a->argc > 2) {
return CLI_SHOWUSAGE; return CLI_SHOWUSAGE;
}
do_reload(0); do_reload(0);
return CLI_SUCCESS; return CLI_SUCCESS;
@@ -339,8 +343,9 @@ static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_
return NULL; return NULL;
} }
if (a->argc > 2) if (a->argc > 2) {
return CLI_SHOWUSAGE; return CLI_SHOWUSAGE;
}
ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled"); ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval); ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
@@ -377,16 +382,15 @@ int dnsmgr_reload(void)
static int do_reload(int loading) static int do_reload(int loading)
{ {
struct ast_config *config; struct ast_config *config;
struct ast_variable *v;
struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED }; struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
const char *interval_value;
const char *enabled_value;
int interval; int interval;
int was_enabled; int was_enabled;
int res = -1; int res = 0;
config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags); config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags);
if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) { if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) {
return 0; return res;
} }
/* ensure that no refresh cycles run while the reload is in progress */ /* ensure that no refresh cycles run while the reload is in progress */
@@ -399,23 +403,24 @@ static int do_reload(int loading)
AST_SCHED_DEL(sched, refresh_sched); AST_SCHED_DEL(sched, refresh_sched);
if (config) { for (v = ast_variable_browse(config, "general"); v; v = v->next) {
if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) { if (!strcasecmp(v->name, "enable")) {
enabled = ast_true(enabled_value); enabled = ast_true(v->value);
} } else if (!strcasecmp(v->name, "refreshinterval")) {
if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) { if (sscanf(v->value, "%30d", &interval) < 1) {
if (sscanf(interval_value, "%30d", &interval) < 1) ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value); } else if (interval < 0) {
else if (interval < 0)
ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval); ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
else } else {
refresh_interval = interval; refresh_interval = interval;
} }
ast_config_destroy(config);
} }
}
ast_config_destroy(config);
if (enabled && refresh_interval) if (enabled && refresh_interval) {
ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval); ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
}
/* if this reload enabled the manager, create the background thread /* if this reload enabled the manager, create the background thread
if it does not exist */ if it does not exist */
@@ -427,20 +432,14 @@ static int do_reload(int loading)
} }
/* make a background refresh happen right away */ /* make a background refresh happen right away */
refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1); refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
res = 0; /* if this reload disabled the manager and there is a background thread, kill it */
} } else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
/* if this reload disabled the manager and there is a background thread,
kill it */
else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
/* wake up the thread so it will exit */ /* wake up the thread so it will exit */
pthread_cancel(refresh_thread); pthread_cancel(refresh_thread);
pthread_kill(refresh_thread, SIGURG); pthread_kill(refresh_thread, SIGURG);
pthread_join(refresh_thread, NULL); pthread_join(refresh_thread, NULL);
refresh_thread = AST_PTHREADT_NULL; refresh_thread = AST_PTHREADT_NULL;
res = 0;
} }
else
res = 0;
ast_mutex_unlock(&refresh_lock); ast_mutex_unlock(&refresh_lock);
manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: DNSmgr\r\nStatus: %s\r/nMessage: DNSmgr reload Requested\r\n", enabled ? "Enabled" : "Disabled"); manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: DNSmgr\r\nStatus: %s\r/nMessage: DNSmgr reload Requested\r\n", enabled ? "Enabled" : "Disabled");