Index: cel/cel_sqlite3_custom.c =================================================================== --- cel/cel_sqlite3_custom.c (revision 202372) +++ cel/cel_sqlite3_custom.c (working copy) @@ -64,7 +64,6 @@ static sqlite3 *db = NULL; static char table[80]; -/*! XXX \bug Handling of this var is crash prone on reloads */ static char *columns; static struct ast_event_sub *event_sub = NULL; @@ -75,7 +74,7 @@ static AST_LIST_HEAD_STATIC(sql_values, values); -static int free_config(void); +static void free_config(void); static int load_column_config(const char *tmp) { @@ -168,12 +167,12 @@ return 0; } + ast_mutex_lock(&lock); + if (reload) { free_config(); } - ast_mutex_lock(&lock); - if (!(mappingvar = ast_variable_browse(cfg, "master"))) { /* Nothing configured */ ast_mutex_unlock(&lock); @@ -213,12 +212,10 @@ return 0; } -static int free_config(void) +static void free_config(void) { struct values *value; - ast_mutex_lock(&lock); - if (db) { sqlite3_close(db); db = NULL; @@ -232,10 +229,6 @@ while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) { ast_free(value); } - - ast_mutex_unlock(&lock); - - return 0; } static void sqlite3_log(const struct ast_event *event, void *userdata) @@ -301,12 +294,12 @@ static int unload_module(void) { - free_config(); - if (event_sub) { event_sub = ast_event_unsubscribe(event_sub); } + free_config(); + return 0; } @@ -317,24 +310,18 @@ int res; char *sql; - if (!load_config(0)) { - event_sub = ast_event_subscribe(AST_EVENT_CEL, sqlite3_log, "CEL sqlite3 custom backend", NULL, AST_EVENT_IE_END); - if (!event_sub) { - ast_log(LOG_ERROR, "Unable to register custom SQLite3 CEL handling\n"); - free_config(); - return AST_MODULE_LOAD_DECLINE; - } - } else { + if (load_config(0)) { return AST_MODULE_LOAD_DECLINE; } + ast_mutex_lock(&lock); + /* is the database there? */ snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR); res = sqlite3_open(filename, &db); if (res != SQLITE_OK) { ast_log(LOG_ERROR, "Could not open database %s.\n", filename); - free_config(); - return AST_MODULE_LOAD_DECLINE; + goto declined; } /* is the table there? */ @@ -349,12 +336,25 @@ if (res != SQLITE_OK) { ast_log(LOG_WARNING, "Unable to create table '%s': %s.\n", table, error); sqlite3_free(error); - free_config(); - return AST_MODULE_LOAD_DECLINE; + goto declined; } } + event_sub = ast_event_subscribe(AST_EVENT_CEL, sqlite3_log, "CEL sqlite3 custom backend", NULL, AST_EVENT_IE_END); + if (!event_sub) { + ast_log(LOG_ERROR, "Unable to register custom SQLite3 CEL handling\n"); + goto declined; + } + + ast_mutex_unlock(&lock); + return AST_MODULE_LOAD_SUCCESS; + + declined: + free_config(); + ast_mutex_unlock(&lock); + + return AST_MODULE_LOAD_DECLINE; } static int reload(void)