Index: res/res_odbc.c =================================================================== RCS file: /usr/cvsroot/asterisk/res/res_odbc.c,v retrieving revision 1.17 diff -u -u -r1.17 res_odbc.c --- res/res_odbc.c 10 Jul 2005 23:21:39 -0000 1.17 +++ res/res_odbc.c 29 Aug 2005 17:16:38 -0000 @@ -37,6 +37,8 @@ }; static struct odbc_list ODBC_REGISTRY[MAX_ODBC_HANDLES]; +#define reset_param(old, new) if(old) { free(old) ; if (!(old = strdup(new))) return -1;} +static int reload_odbc_obj(char *name, char *dsn, char *username, char *password, int sanitycheck); static void odbc_destroy(void) @@ -91,7 +93,7 @@ { int res = 0; res = SQLExecute(stmt); - if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + if (obj->sanitycheck && (res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Execute error! Attempting a reconnect...\n"); ast_mutex_lock(&obj->lock); obj->up = 0; @@ -110,7 +112,7 @@ int res = 0; res = SQLExecDirect (stmt, sql, SQL_NTS); - if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + if (obj->sanitycheck && (res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Execute error! Attempting a reconnect...\n"); ast_mutex_lock(&obj->lock); obj->up = 0; @@ -163,14 +165,14 @@ return obj->up; } -static int load_odbc_config(void) +static int load_odbc_config(int reload) { static char *cfg = "res_odbc.conf"; struct ast_config *config; struct ast_variable *v; char *cat, *dsn, *username, *password; - int enabled; - int connect = 0; + int enabled = 0; + int connect = 0, sanitycheck = 0; char *env_var; odbc_obj *obj; @@ -188,28 +190,35 @@ free(env_var); } } - - cat = ast_category_browse(config, cat); + cat = ast_category_browse(config, cat); } dsn = username = password = NULL; - enabled = 1; - connect = 0; + enabled = 1; + connect = 0; + sanitycheck = 0; for (v = ast_variable_browse(config, cat); v; v = v->next) { + connect = 0; + sanitycheck = 0; + if (!strcmp(v->name, "enabled")) enabled = ast_true(v->value); - if (!strcmp(v->name, "pre-connect")) + else if (!strcmp(v->name, "pre-connect")) connect = ast_true(v->value); - if (!strcmp(v->name, "dsn")) + else if (!strcmp(v->name, "dsn")) dsn = v->value; - if (!strcmp(v->name, "username")) + else if (!strcmp(v->name, "username")) username = v->value; - if (!strcmp(v->name, "password")) - password = v->value; + else if (!strcmp(v->name, "sanitycheck")) + sanitycheck = atoi(v->value); } - if (enabled && dsn) { - obj = new_odbc_obj(cat, dsn, username, password); + if(reload) { + if(reload_odbc_obj(cat, dsn, username, password, sanitycheck)) { + ast_log(LOG_ERROR,"Reload Failed.\n"); + } + } else if (enabled && dsn) { + obj = new_odbc_obj(cat, dsn, username, password, sanitycheck); if (obj) { register_odbc_obj(cat, obj); ast_log(LOG_NOTICE, "registered database handle '%s' dsn->[%s]\n", cat, obj->dsn); @@ -219,7 +228,6 @@ } else { ast_log(LOG_WARNING, "Addition of obj %s failed.\n", cat); } - } } ast_config_destroy(config); @@ -230,8 +238,9 @@ int odbc_dump_fd(int fd, odbc_obj *obj) { /* make sure the connection is up before we lie to our master.*/ - odbc_sanity_check(obj); - ast_cli(fd, "Name: %s\nDSN: %s\nConnected: %s\n\n", obj->name, obj->dsn, obj->up ? "yes" : "no"); + if(obj->sanitycheck) + odbc_sanity_check(obj); + ast_cli(fd, "Name: %s\nDSN: %s\nConnected: %s\nSanity Check: %d\n", obj->name, obj->dsn, obj->up ? "yes" : "no", obj->sanitycheck); return 0; } @@ -253,19 +262,24 @@ int x = 0; if (!strcmp(argv[1], "show")) { + ast_cli(fd, "\n"); if (!argv[2] || (argv[2] && !strcmp(argv[2], "all"))) { for (x = 0; x < MAX_ODBC_HANDLES; x++) { if (!ODBC_REGISTRY[x].used) break; - if (ODBC_REGISTRY[x].obj) + if (ODBC_REGISTRY[x].obj) { odbc_dump_fd(fd, ODBC_REGISTRY[x].obj); + ast_cli(fd, "\n"); + } } } else { obj = odbc_read(ODBC_REGISTRY, argv[2]); - if (obj) + if (obj) { odbc_dump_fd(fd, obj); + } } } + ast_cli(fd, "\n"); return 0; } @@ -339,47 +353,59 @@ { odbc_obj *obj = NULL; if((obj = (odbc_obj *) odbc_read(ODBC_REGISTRY, name))) { - if(check) + if(check || obj->sanitycheck > 1) odbc_sanity_check(obj); } return obj; } -odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password) + +static int reload_odbc_obj(char *name, char *dsn, char *username, char *password, int sanitycheck) +{ + odbc_obj *obj; + + if((obj = fetch_odbc_obj(name, 1))) { + if(!sanitycheck) + sanitycheck = 1; + + ast_mutex_lock(&obj->lock); + reset_param(obj->name, name); + reset_param(obj->dsn, dsn); + reset_param(obj->username, username); + reset_param(obj->password, password); + obj->sanitycheck = sanitycheck; + ast_mutex_unlock(&obj->lock); + return 0; + } else { + ast_log(LOG_ERROR,"Cannot Find Object %s\n", name); + } + return -1; +} + +odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password, int sanitycheck) { - static odbc_obj *new; + odbc_obj *new; + new = malloc(sizeof(odbc_obj)); if (!new) return NULL; memset(new, 0, sizeof(odbc_obj)); new->env = SQL_NULL_HANDLE; - - new->name = malloc(strlen(name) + 1); - if (new->name == NULL) + if(!sanitycheck) + sanitycheck = 1; + if (name && !(new->name = strdup(name))) return NULL; - - new->dsn = malloc(strlen(dsn) + 1); - if (new->dsn == NULL) + if (dsn && !(new->dsn = strdup(dsn))) + return NULL; + if (username && !(new->username = strdup(username))) + return NULL; + if (password && !(new->password = strdup(password))) return NULL; - if (username) { - new->username = malloc(strlen(username) + 1); - if (new->username == NULL) - return NULL; - strcpy(new->username, username); - } - - if (password) { - new->password = malloc(strlen(password) + 1); - if (new->password == NULL) - return NULL; - strcpy(new->password, password); - } - - strcpy(new->name, name); - strcpy(new->dsn, dsn); new->up = 0; + if(sanitycheck >= 0) + new->sanitycheck = sanitycheck; ast_mutex_init(&new->lock); return new; } @@ -508,10 +534,14 @@ return 0; } +int reload(void) { + return load_odbc_config(1); +} + int load_module(void) { odbc_init(); - load_odbc_config(); + load_odbc_config(0); ast_cli_register(&odbc_disconnect_struct); ast_cli_register(&odbc_connect_struct); ast_cli_register(&odbc_show_struct);