| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#include "asterisk.h" |
| 49 |
|
| 50 |
#include "asterisk/file.h" |
| 51 |
#include "asterisk/channel.h" |
| 52 |
#include "asterisk/config.h" |
| 53 |
#include "asterisk/pbx.h" |
| 54 |
#include "asterisk/module.h" |
| 55 |
#include "asterisk/cli.h" |
| 56 |
#include "asterisk/lock.h" |
| 57 |
#include "asterisk/res_odbc.h" |
| 58 |
#include "asterisk/time.h" |
| 59 |
#include "asterisk/astobj2.h" |
| 60 |
#include "asterisk/app.h" |
| 61 |
#include "asterisk/strings.h" |
| 62 |
#include "asterisk/threadstorage.h" |
| 63 |
|
| 64 |
struct odbc_class |
| 65 |
{ |
| 66 |
AST_LIST_ENTRY(odbc_class) list; |
| 67 |
char name[80]; |
| 68 |
char dsn[80]; |
| 69 |
char *username; |
| 70 |
char *password; |
| 71 |
char *sanitysql; |
| 72 |
SQLHENV env; |
| 73 |
unsigned int delme:1; |
| 74 |
unsigned int backslash_is_escape:1; |
| 75 |
unsigned int forcecommit:1; |
| 76 |
unsigned int cache_is_queue:1; |
| 77 |
unsigned int isolation; |
| 78 |
unsigned int conntimeout; |
| 79 |
unsigned int maxconnections; |
| 80 |
|
| 81 |
struct timeval negative_connection_cache; |
| 82 |
|
| 83 |
struct timeval last_negative_connect; |
| 84 |
|
| 85 |
AST_LIST_HEAD_NOLOCK(, odbc_obj) connections; |
| 86 |
|
| 87 |
ast_mutex_t lock; |
| 88 |
|
| 89 |
ast_cond_t cond; |
| 90 |
|
| 91 |
size_t connection_cnt; |
| 92 |
|
| 93 |
unsigned int logging; |
| 94 |
|
| 95 |
int prepares_executed; |
| 96 |
|
| 97 |
int queries_executed; |
| 98 |
|
| 99 |
long longest_query_execution_time; |
| 100 |
|
| 101 |
char *sql_text; |
| 102 |
|
| 103 |
unsigned int slowquerylimit; |
| 104 |
|
| 105 |
unsigned int max_cache_size; |
| 106 |
|
| 107 |
unsigned int cur_cache; |
| 108 |
}; |
| 109 |
|
| 110 |
static struct ao2_container *class_container; |
| 111 |
|
| 112 |
static AST_RWLIST_HEAD_STATIC(odbc_tables, odbc_cache_tables); |
| 113 |
|
| 114 |
static odbc_status odbc_obj_connect(struct odbc_obj *obj); |
| 115 |
static odbc_status odbc_obj_disconnect(struct odbc_obj *obj); |
| 116 |
static void odbc_register_class(struct odbc_class *class, int connect); |
| 117 |
|
| 118 |
AST_THREADSTORAGE(errors_buf); |
| 119 |
|
| 120 |
struct odbc_txn_frame { |
| 121 |
AST_LIST_ENTRY(odbc_txn_frame) list; |
| 122 |
struct ast_channel *owner; |
| 123 |
struct odbc_obj *obj; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
unsigned int active:1; |
| 132 |
unsigned int forcecommit:1; |
| 133 |
unsigned int isolation; |
| 134 |
char name[0]; |
| 135 |
}; |
| 136 |
|
| 137 |
const char *ast_odbc_isolation2text(int iso) |
| 138 |
{ |
| 139 |
if (iso == SQL_TXN_READ_COMMITTED) { |
| 140 |
return "read_committed"; |
| 141 |
} else if (iso == SQL_TXN_READ_UNCOMMITTED) { |
| 142 |
return "read_uncommitted"; |
| 143 |
} else if (iso == SQL_TXN_SERIALIZABLE) { |
| 144 |
return "serializable"; |
| 145 |
} else if (iso == SQL_TXN_REPEATABLE_READ) { |
| 146 |
return "repeatable_read"; |
| 147 |
} else { |
| 148 |
return "unknown"; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
int ast_odbc_text2isolation(const char *txt) |
| 153 |
{ |
| 154 |
if (strncasecmp(txt, "read_", 5) == 0) { |
| 155 |
if (strncasecmp(txt + 5, "c", 1) == 0) { |
| 156 |
return SQL_TXN_READ_COMMITTED; |
| 157 |
} else if (strncasecmp(txt + 5, "u", 1) == 0) { |
| 158 |
return SQL_TXN_READ_UNCOMMITTED; |
| 159 |
} else { |
| 160 |
return 0; |
| 161 |
} |
| 162 |
} else if (strncasecmp(txt, "ser", 3) == 0) { |
| 163 |
return SQL_TXN_SERIALIZABLE; |
| 164 |
} else if (strncasecmp(txt, "rep", 3) == 0) { |
| 165 |
return SQL_TXN_REPEATABLE_READ; |
| 166 |
} else { |
| 167 |
return 0; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static void odbc_class_destructor(void *data) |
| 172 |
{ |
| 173 |
struct odbc_class *class = data; |
| 174 |
struct odbc_obj *obj; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if (class->username) { |
| 180 |
ast_free(class->username); |
| 181 |
} |
| 182 |
if (class->password) { |
| 183 |
ast_free(class->password); |
| 184 |
} |
| 185 |
if (class->sanitysql) { |
| 186 |
ast_free(class->sanitysql); |
| 187 |
} |
| 188 |
|
| 189 |
while ((obj = AST_LIST_REMOVE_HEAD(&class->connections, list))) { |
| 190 |
ao2_ref(obj, -1); |
| 191 |
} |
| 192 |
|
| 193 |
SQLFreeHandle(SQL_HANDLE_ENV, class->env); |
| 194 |
ast_mutex_destroy(&class->lock); |
| 195 |
ast_cond_destroy(&class->cond); |
| 196 |
ast_free(class->sql_text); |
| 197 |
} |
| 198 |
|
| 199 |
static void odbc_obj_destructor(void *data) |
| 200 |
{ |
| 201 |
struct odbc_obj *obj = data; |
| 202 |
|
| 203 |
odbc_obj_disconnect(obj); |
| 204 |
} |
| 205 |
|
| 206 |
static void destroy_table_cache(struct odbc_cache_tables *table) |
| 207 |
{ |
| 208 |
struct odbc_cache_columns *col; |
| 209 |
|
| 210 |
ast_debug(1, "Destroying table cache for %s\n", table->table); |
| 211 |
|
| 212 |
AST_RWLIST_WRLOCK(&table->columns); |
| 213 |
while ((col = AST_RWLIST_REMOVE_HEAD(&table->columns, list))) { |
| 214 |
ast_free(col); |
| 215 |
} |
| 216 |
AST_RWLIST_UNLOCK(&table->columns); |
| 217 |
AST_RWLIST_HEAD_DESTROY(&table->columns); |
| 218 |
|
| 219 |
ast_free(table); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
struct odbc_cache_tables *ast_odbc_find_table(const char *database, const char *tablename) |
| 238 |
{ |
| 239 |
struct odbc_cache_tables *tableptr; |
| 240 |
struct odbc_cache_columns *entry; |
| 241 |
char columnname[80]; |
| 242 |
SQLLEN sqlptr; |
| 243 |
SQLHSTMT stmt = NULL; |
| 244 |
int res = 0, error = 0; |
| 245 |
struct odbc_obj *obj; |
| 246 |
|
| 247 |
AST_RWLIST_RDLOCK(&odbc_tables); |
| 248 |
AST_RWLIST_TRAVERSE(&odbc_tables, tableptr, list) { |
| 249 |
if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) { |
| 250 |
break; |
| 251 |
} |
| 252 |
} |
| 253 |
if (tableptr) { |
| 254 |
AST_RWLIST_RDLOCK(&tableptr->columns); |
| 255 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 256 |
return tableptr; |
| 257 |
} |
| 258 |
|
| 259 |
if (!(obj = ast_odbc_request_obj(database, 0))) { |
| 260 |
ast_log(LOG_WARNING, "Unable to retrieve database handle for table description '%s@%s'\n", tablename, database); |
| 261 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
do { |
| 267 |
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); |
| 268 |
if (!SQL_SUCCEEDED(res)) { |
| 269 |
ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", database); |
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)tablename, SQL_NTS, (unsigned char *)"%", SQL_NTS); |
| 274 |
if (!SQL_SUCCEEDED(res)) { |
| 275 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 276 |
ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.\n", database); |
| 277 |
break; |
| 278 |
} |
| 279 |
|
| 280 |
if (!(tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + strlen(database) + 1 + strlen(tablename) + 1))) { |
| 281 |
ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", tablename, database); |
| 282 |
break; |
| 283 |
} |
| 284 |
|
| 285 |
tableptr->connection = (char *)tableptr + sizeof(*tableptr); |
| 286 |
tableptr->table = (char *)tableptr + sizeof(*tableptr) + strlen(database) + 1; |
| 287 |
strcpy(tableptr->connection, database); |
| 288 |
strcpy(tableptr->table, tablename); |
| 289 |
AST_RWLIST_HEAD_INIT(&(tableptr->columns)); |
| 290 |
|
| 291 |
while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) { |
| 292 |
SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr); |
| 293 |
|
| 294 |
if (!(entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1))) { |
| 295 |
ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, tablename, database); |
| 296 |
error = 1; |
| 297 |
break; |
| 298 |
} |
| 299 |
entry->name = (char *)entry + sizeof(*entry); |
| 300 |
strcpy(entry->name, columnname); |
| 301 |
|
| 302 |
SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL); |
| 303 |
SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL); |
| 304 |
SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL); |
| 305 |
SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL); |
| 306 |
SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL); |
| 307 |
SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
if (entry->octetlen == 0) { |
| 313 |
entry->octetlen = entry->size; |
| 314 |
} |
| 315 |
|
| 316 |
ast_debug(3, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix); |
| 317 |
|
| 318 |
AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list); |
| 319 |
} |
| 320 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 321 |
|
| 322 |
AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list); |
| 323 |
AST_RWLIST_RDLOCK(&(tableptr->columns)); |
| 324 |
break; |
| 325 |
} while (1); |
| 326 |
|
| 327 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 328 |
|
| 329 |
if (error) { |
| 330 |
destroy_table_cache(tableptr); |
| 331 |
tableptr = NULL; |
| 332 |
} |
| 333 |
ast_odbc_release_obj(obj); |
| 334 |
return tableptr; |
| 335 |
} |
| 336 |
|
| 337 |
struct odbc_cache_columns *ast_odbc_find_column(struct odbc_cache_tables *table, const char *colname) |
| 338 |
{ |
| 339 |
struct odbc_cache_columns *col; |
| 340 |
AST_RWLIST_TRAVERSE(&table->columns, col, list) { |
| 341 |
if (strcasecmp(col->name, colname) == 0) { |
| 342 |
return col; |
| 343 |
} |
| 344 |
} |
| 345 |
return NULL; |
| 346 |
} |
| 347 |
|
| 348 |
int ast_odbc_clear_cache(const char *database, const char *tablename) |
| 349 |
{ |
| 350 |
struct odbc_cache_tables *tableptr; |
| 351 |
|
| 352 |
AST_RWLIST_WRLOCK(&odbc_tables); |
| 353 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&odbc_tables, tableptr, list) { |
| 354 |
if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) { |
| 355 |
AST_LIST_REMOVE_CURRENT(list); |
| 356 |
destroy_table_cache(tableptr); |
| 357 |
break; |
| 358 |
} |
| 359 |
} |
| 360 |
AST_RWLIST_TRAVERSE_SAFE_END |
| 361 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 362 |
return tableptr ? 0 : -1; |
| 363 |
} |
| 364 |
|
| 365 |
SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data) |
| 366 |
{ |
| 367 |
struct timeval start; |
| 368 |
SQLHSTMT stmt; |
| 369 |
|
| 370 |
if (obj->parent->logging) { |
| 371 |
start = ast_tvnow(); |
| 372 |
} |
| 373 |
|
| 374 |
stmt = exec_cb(obj, data); |
| 375 |
|
| 376 |
if (obj->parent->logging) { |
| 377 |
long execution_time = ast_tvdiff_ms(ast_tvnow(), start); |
| 378 |
|
| 379 |
if (obj->parent->slowquerylimit && execution_time > obj->parent->slowquerylimit) { |
| 380 |
ast_log(LOG_WARNING, "SQL query '%s' took %ld milliseconds to execute on class '%s', this may indicate a database problem\n", |
| 381 |
obj->sql_text, execution_time, obj->parent->name); |
| 382 |
} |
| 383 |
|
| 384 |
ast_mutex_lock(&obj->parent->lock); |
| 385 |
if (execution_time > obj->parent->longest_query_execution_time || !obj->parent->sql_text) { |
| 386 |
obj->parent->longest_query_execution_time = execution_time; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
ast_free(obj->parent->sql_text); |
| 394 |
obj->parent->sql_text = obj->sql_text; |
| 395 |
obj->sql_text = NULL; |
| 396 |
} |
| 397 |
ast_mutex_unlock(&obj->parent->lock); |
| 398 |
} |
| 399 |
|
| 400 |
return stmt; |
| 401 |
} |
| 402 |
|
| 403 |
SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data) |
| 404 |
{ |
| 405 |
struct timeval start; |
| 406 |
int res = 0; |
| 407 |
SQLHSTMT stmt; |
| 408 |
|
| 409 |
if (obj->parent->logging) { |
| 410 |
start = ast_tvnow(); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
stmt = prepare_cb(obj, data); |
| 419 |
if (!stmt) { |
| 420 |
return NULL; |
| 421 |
} |
| 422 |
|
| 423 |
res = SQLExecute(stmt); |
| 424 |
if (!SQL_SUCCEEDED(res) && (res != SQL_NO_DATA)) { |
| 425 |
if (res == SQL_ERROR) { |
| 426 |
ast_odbc_print_errors(SQL_HANDLE_STMT, stmt, "SQL Execute"); |
| 427 |
} |
| 428 |
|
| 429 |
ast_log(LOG_WARNING, "SQL Execute error %d!\n", res); |
| 430 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 431 |
stmt = NULL; |
| 432 |
} else if (obj->parent->logging) { |
| 433 |
long execution_time = ast_tvdiff_ms(ast_tvnow(), start); |
| 434 |
|
| 435 |
if (obj->parent->slowquerylimit && execution_time > obj->parent->slowquerylimit) { |
| 436 |
ast_log(LOG_WARNING, "SQL query '%s' took %ld milliseconds to execute on class '%s', this may indicate a database problem\n", |
| 437 |
obj->sql_text, execution_time, obj->parent->name); |
| 438 |
} |
| 439 |
|
| 440 |
ast_mutex_lock(&obj->parent->lock); |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
if (execution_time > obj->parent->longest_query_execution_time || !obj->parent->sql_text) { |
| 446 |
obj->parent->longest_query_execution_time = execution_time; |
| 447 |
ast_free(obj->parent->sql_text); |
| 448 |
obj->parent->sql_text = obj->sql_text; |
| 449 |
obj->sql_text = NULL; |
| 450 |
} |
| 451 |
ast_mutex_unlock(&obj->parent->lock); |
| 452 |
|
| 453 |
ast_atomic_fetchadd_int(&obj->parent->queries_executed, +1); |
| 454 |
} |
| 455 |
|
| 456 |
return stmt; |
| 457 |
} |
| 458 |
|
| 459 |
int ast_odbc_prepare(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql) |
| 460 |
{ |
| 461 |
if (obj->parent->logging) { |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
ast_free(obj->sql_text); |
| 467 |
obj->sql_text = ast_strdup(sql); |
| 468 |
ast_atomic_fetchadd_int(&obj->parent->prepares_executed, +1); |
| 469 |
} |
| 470 |
|
| 471 |
return SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS); |
| 472 |
} |
| 473 |
|
| 474 |
SQLRETURN ast_odbc_execute_sql(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql) |
| 475 |
{ |
| 476 |
if (obj->parent->logging) { |
| 477 |
ast_free(obj->sql_text); |
| 478 |
obj->sql_text = ast_strdup(sql); |
| 479 |
ast_atomic_fetchadd_int(&obj->parent->queries_executed, +1); |
| 480 |
} |
| 481 |
|
| 482 |
return SQLExecDirect(stmt, (unsigned char *)sql, SQL_NTS); |
| 483 |
} |
| 484 |
|
| 485 |
int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt) |
| 486 |
{ |
| 487 |
int res = 0; |
| 488 |
|
| 489 |
res = SQLExecute(stmt); |
| 490 |
if (!SQL_SUCCEEDED(res) && (res != SQL_NO_DATA)) { |
| 491 |
if (res == SQL_ERROR) { |
| 492 |
ast_odbc_print_errors(SQL_HANDLE_STMT, stmt, "SQL Execute"); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
if (obj->parent->logging) { |
| 497 |
ast_atomic_fetchadd_int(&obj->parent->queries_executed, +1); |
| 498 |
} |
| 499 |
|
| 500 |
return res; |
| 501 |
} |
| 502 |
|
| 503 |
SQLRETURN ast_odbc_ast_str_SQLGetData(struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind) |
| 504 |
{ |
| 505 |
SQLRETURN res; |
| 506 |
|
| 507 |
if (pmaxlen == 0) { |
| 508 |
if (SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) { |
| 509 |
ast_str_make_space(buf, *StrLen_or_Ind + 1); |
| 510 |
} |
| 511 |
} else if (pmaxlen > 0) { |
| 512 |
ast_str_make_space(buf, pmaxlen); |
| 513 |
} |
| 514 |
res = SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), ast_str_size(*buf), StrLen_or_Ind); |
| 515 |
ast_str_update(*buf); |
| 516 |
|
| 517 |
return res; |
| 518 |
} |
| 519 |
|
| 520 |
struct ast_str *ast_odbc_print_errors(SQLSMALLINT handle_type, SQLHANDLE handle, const char *operation) |
| 521 |
{ |
| 522 |
struct ast_str *errors = ast_str_thread_get(&errors_buf, 16); |
| 523 |
SQLINTEGER nativeerror = 0; |
| 524 |
SQLSMALLINT diagbytes = 0; |
| 525 |
SQLSMALLINT i; |
| 526 |
unsigned char state[10]; |
| 527 |
unsigned char diagnostic[256]; |
| 528 |
|
| 529 |
ast_str_reset(errors); |
| 530 |
i = 0; |
| 531 |
while (SQLGetDiagRec(handle_type, handle, ++i, state, &nativeerror, |
| 532 |
diagnostic, sizeof(diagnostic), &diagbytes) == SQL_SUCCESS) { |
| 533 |
ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state); |
| 534 |
ast_log(LOG_WARNING, "%s returned an error: %s: %s\n", operation, state, diagnostic); |
| 535 |
|
| 536 |
if (i > 10) { |
| 537 |
ast_log(LOG_WARNING, "There are more than 10 diagnostic records! Ignore the rest.\n"); |
| 538 |
break; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
return errors; |
| 543 |
} |
| 544 |
|
| 545 |
unsigned int ast_odbc_class_get_isolation(struct odbc_class *class) |
| 546 |
{ |
| 547 |
return class->isolation; |
| 548 |
} |
| 549 |
|
| 550 |
unsigned int ast_odbc_class_get_forcecommit(struct odbc_class *class) |
| 551 |
{ |
| 552 |
return class->forcecommit; |
| 553 |
} |
| 554 |
|
| 555 |
const char *ast_odbc_class_get_name(struct odbc_class *class) |
| 556 |
{ |
| 557 |
return class->name; |
| 558 |
} |
| 559 |
|
| 560 |
static int load_odbc_config(void) |
| 561 |
{ |
| 562 |
static char *cfg = "res_odbc.conf"; |
| 563 |
struct ast_config *config; |
| 564 |
struct ast_variable *v; |
| 565 |
char *cat; |
| 566 |
const char *dsn, *username, *password, *sanitysql; |
| 567 |
int enabled, bse, conntimeout, forcecommit, isolation, maxconnections, logging, slowquerylimit; |
| 568 |
struct timeval ncache = { 0, 0 }; |
| 569 |
int preconnect = 0, res = 0, cache_is_queue = 0; |
| 570 |
struct ast_flags config_flags = { 0 }; |
| 571 |
unsigned int max_cache_size; |
| 572 |
|
| 573 |
struct odbc_class *new; |
| 574 |
|
| 575 |
config = ast_config_load(cfg, config_flags); |
| 576 |
if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) { |
| 577 |
ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n"); |
| 578 |
return -1; |
| 579 |
} |
| 580 |
for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) { |
| 581 |
if (!strcasecmp(cat, "ENV")) { |
| 582 |
for (v = ast_variable_browse(config, cat); v; v = v->next) { |
| 583 |
setenv(v->name, v->value, 1); |
| 584 |
ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value); |
| 585 |
} |
| 586 |
} else { |
| 587 |
|
| 588 |
dsn = username = password = sanitysql = NULL; |
| 589 |
enabled = 1; |
| 590 |
preconnect = 0; |
| 591 |
bse = 1; |
| 592 |
conntimeout = 10; |
| 593 |
forcecommit = 0; |
| 594 |
isolation = SQL_TXN_READ_COMMITTED; |
| 595 |
maxconnections = 1; |
| 596 |
logging = 0; |
| 597 |
slowquerylimit = 5000; |
| 598 |
cache_is_queue = 0; |
| 599 |
max_cache_size = UINT_MAX; |
| 600 |
for (v = ast_variable_browse(config, cat); v; v = v->next) { |
| 601 |
if (!strcasecmp(v->name, "pooling") || |
| 602 |
!strncasecmp(v->name, "share", 5) || |
| 603 |
!strcasecmp(v->name, "limit") || |
| 604 |
!strcasecmp(v->name, "idlecheck")) { |
| 605 |
ast_log(LOG_WARNING, "The 'pooling', 'shared_connections', 'limit', and 'idlecheck' options were replaced by 'max_connections'. See res_odbc.conf.sample.\n"); |
| 606 |
} else if (!strcasecmp(v->name, "enabled")) { |
| 607 |
enabled = ast_true(v->value); |
| 608 |
} else if (!strcasecmp(v->name, "pre-connect")) { |
| 609 |
preconnect = ast_true(v->value); |
| 610 |
} else if (!strcasecmp(v->name, "dsn")) { |
| 611 |
dsn = v->value; |
| 612 |
} else if (!strcasecmp(v->name, "username")) { |
| 613 |
username = v->value; |
| 614 |
} else if (!strcasecmp(v->name, "password")) { |
| 615 |
password = v->value; |
| 616 |
} else if (!strcasecmp(v->name, "sanitysql")) { |
| 617 |
sanitysql = v->value; |
| 618 |
} else if (!strcasecmp(v->name, "backslash_is_escape")) { |
| 619 |
bse = ast_true(v->value); |
| 620 |
} else if (!strcasecmp(v->name, "connect_timeout")) { |
| 621 |
if (sscanf(v->value, "%d", &conntimeout) != 1 || conntimeout < 1) { |
| 622 |
ast_log(LOG_WARNING, "connect_timeout must be a positive integer\n"); |
| 623 |
conntimeout = 10; |
| 624 |
} |
| 625 |
} else if (!strcasecmp(v->name, "negative_connection_cache")) { |
| 626 |
double dncache; |
| 627 |
if (sscanf(v->value, "%lf", &dncache) != 1 || dncache < 0) { |
| 628 |
ast_log(LOG_WARNING, "negative_connection_cache must be a non-negative integer\n"); |
| 629 |
|
| 630 |
ncache.tv_sec = 300; |
| 631 |
ncache.tv_usec = 0; |
| 632 |
} else { |
| 633 |
ncache.tv_sec = (int)dncache; |
| 634 |
ncache.tv_usec = (dncache - ncache.tv_sec) * 1000000; |
| 635 |
} |
| 636 |
} else if (!strcasecmp(v->name, "forcecommit")) { |
| 637 |
forcecommit = ast_true(v->value); |
| 638 |
} else if (!strcasecmp(v->name, "isolation")) { |
| 639 |
if ((isolation = ast_odbc_text2isolation(v->value)) == 0) { |
| 640 |
ast_log(LOG_ERROR, "Unrecognized value for 'isolation': '%s' in section '%s'\n", v->value, cat); |
| 641 |
isolation = SQL_TXN_READ_COMMITTED; |
| 642 |
} |
| 643 |
} else if (!strcasecmp(v->name, "max_connections")) { |
| 644 |
if (sscanf(v->value, "%30d", &maxconnections) != 1 || maxconnections < 1) { |
| 645 |
ast_log(LOG_WARNING, "max_connections must be a positive integer\n"); |
| 646 |
maxconnections = 1; |
| 647 |
} |
| 648 |
} else if (!strcasecmp(v->name, "logging")) { |
| 649 |
logging = ast_true(v->value); |
| 650 |
} else if (!strcasecmp(v->name, "slow_query_limit")) { |
| 651 |
if (sscanf(v->value, "%30d", &slowquerylimit) != 1) { |
| 652 |
ast_log(LOG_WARNING, "slow_query_limit must be a positive integer\n"); |
| 653 |
slowquerylimit = 5000; |
| 654 |
} |
| 655 |
} else if (!strcasecmp(v->name, "cache_type")) { |
| 656 |
cache_is_queue = !strcasecmp(v->value, "rr") || |
| 657 |
!strcasecmp(v->value, "roundrobin") || |
| 658 |
!strcasecmp(v->value, "queue"); |
| 659 |
} else if (!strcasecmp(v->name, "cache_size")) { |
| 660 |
if (!strcasecmp(v->value, "-1")) { |
| 661 |
max_cache_size = UINT_MAX; |
| 662 |
} else if (sscanf(v->value, "%u", &max_cache_size) != 1) { |
| 663 |
ast_log(LOG_WARNING, "cache_size must be a non-negative integer or -1 (infinite)\n"); |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
if (enabled && !ast_strlen_zero(dsn)) { |
| 669 |
new = ao2_alloc(sizeof(*new), odbc_class_destructor); |
| 670 |
|
| 671 |
if (!new) { |
| 672 |
res = -1; |
| 673 |
break; |
| 674 |
} |
| 675 |
|
| 676 |
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env); |
| 677 |
res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0); |
| 678 |
|
| 679 |
if (!SQL_SUCCEEDED(res)) { |
| 680 |
ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n"); |
| 681 |
ao2_ref(new, -1); |
| 682 |
return res; |
| 683 |
} |
| 684 |
|
| 685 |
new->backslash_is_escape = bse ? 1 : 0; |
| 686 |
new->forcecommit = forcecommit ? 1 : 0; |
| 687 |
new->isolation = isolation; |
| 688 |
new->conntimeout = conntimeout; |
| 689 |
new->negative_connection_cache = ncache; |
| 690 |
new->maxconnections = maxconnections; |
| 691 |
new->logging = logging; |
| 692 |
new->slowquerylimit = slowquerylimit; |
| 693 |
new->cache_is_queue = cache_is_queue; |
| 694 |
new->max_cache_size = max_cache_size; |
| 695 |
new->cur_cache = 0; |
| 696 |
|
| 697 |
if (cat) |
| 698 |
ast_copy_string(new->name, cat, sizeof(new->name)); |
| 699 |
if (dsn) |
| 700 |
ast_copy_string(new->dsn, dsn, sizeof(new->dsn)); |
| 701 |
if (username && !(new->username = ast_strdup(username))) { |
| 702 |
ao2_ref(new, -1); |
| 703 |
break; |
| 704 |
} |
| 705 |
if (password && !(new->password = ast_strdup(password))) { |
| 706 |
ao2_ref(new, -1); |
| 707 |
break; |
| 708 |
} |
| 709 |
if (sanitysql && !(new->sanitysql = ast_strdup(sanitysql))) { |
| 710 |
ao2_ref(new, -1); |
| 711 |
break; |
| 712 |
} |
| 713 |
|
| 714 |
ast_mutex_init(&new->lock); |
| 715 |
ast_cond_init(&new->cond, NULL); |
| 716 |
|
| 717 |
odbc_register_class(new, preconnect); |
| 718 |
ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn); |
| 719 |
ao2_ref(new, -1); |
| 720 |
new = NULL; |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
ast_config_destroy(config); |
| 725 |
return res; |
| 726 |
} |
| 727 |
|
| 728 |
static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 729 |
{ |
| 730 |
struct ao2_iterator aoi; |
| 731 |
struct odbc_class *class; |
| 732 |
int length = 0; |
| 733 |
int which = 0; |
| 734 |
char *ret = NULL; |
| 735 |
|
| 736 |
switch (cmd) { |
| 737 |
case CLI_INIT: |
| 738 |
e->command = "odbc show"; |
| 739 |
e->usage = |
| 740 |
"Usage: odbc show [class]\n" |
| 741 |
" List settings of a particular ODBC class or,\n" |
| 742 |
" if not specified, all classes.\n"; |
| 743 |
return NULL; |
| 744 |
case CLI_GENERATE: |
| 745 |
if (a->pos != 2) |
| 746 |
return NULL; |
| 747 |
length = strlen(a->word); |
| 748 |
aoi = ao2_iterator_init(class_container, 0); |
| 749 |
while ((class = ao2_iterator_next(&aoi))) { |
| 750 |
if (!strncasecmp(a->word, class->name, length) && ++which > a->n) { |
| 751 |
ret = ast_strdup(class->name); |
| 752 |
} |
| 753 |
ao2_ref(class, -1); |
| 754 |
if (ret) { |
| 755 |
break; |
| 756 |
} |
| 757 |
} |
| 758 |
ao2_iterator_destroy(&aoi); |
| 759 |
if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) { |
| 760 |
ret = ast_strdup("all"); |
| 761 |
} |
| 762 |
return ret; |
| 763 |
} |
| 764 |
|
| 765 |
ast_cli(a->fd, "\nODBC DSN Settings\n"); |
| 766 |
ast_cli(a->fd, "-----------------\n\n"); |
| 767 |
aoi = ao2_iterator_init(class_container, 0); |
| 768 |
while ((class = ao2_iterator_next(&aoi))) { |
| 769 |
if ((a->argc == 2) || (a->argc == 3 && !strcmp(a->argv[2], "all")) || (!strcmp(a->argv[2], class->name))) { |
| 770 |
char timestr[80]; |
| 771 |
struct ast_tm tm; |
| 772 |
|
| 773 |
ast_cli(a->fd, " Name: %s\n DSN: %s\n", class->name, class->dsn); |
| 774 |
|
| 775 |
if (class->last_negative_connect.tv_sec > 0) { |
| 776 |
ast_localtime(&class->last_negative_connect, &tm, NULL); |
| 777 |
ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %T", &tm); |
| 778 |
ast_cli(a->fd, " Last fail connection attempt: %s\n", timestr); |
| 779 |
} |
| 780 |
|
| 781 |
ast_cli(a->fd, " Number of active connections: %zd (out of %d)\n", class->connection_cnt, class->maxconnections); |
| 782 |
ast_cli(a->fd, " Cache Type: %s\n", class->cache_is_queue ? "round-robin queue" : "stack (last release, first re-use)"); |
| 783 |
ast_cli(a->fd, " Cache Usage: %u cached out of %u\n", class->cur_cache, |
| 784 |
class->max_cache_size < class->maxconnections ? class->max_cache_size : class->maxconnections); |
| 785 |
ast_cli(a->fd, " Logging: %s\n", class->logging ? "Enabled" : "Disabled"); |
| 786 |
if (class->logging) { |
| 787 |
ast_cli(a->fd, " Number of prepares executed: %d\n", class->prepares_executed); |
| 788 |
ast_cli(a->fd, " Number of queries executed: %d\n", class->queries_executed); |
| 789 |
ast_mutex_lock(&class->lock); |
| 790 |
if (class->sql_text) { |
| 791 |
ast_cli(a->fd, " Longest running SQL query: %s (%ld milliseconds)\n", class->sql_text, class->longest_query_execution_time); |
| 792 |
} |
| 793 |
ast_mutex_unlock(&class->lock); |
| 794 |
} |
| 795 |
ast_cli(a->fd, "\n"); |
| 796 |
} |
| 797 |
ao2_ref(class, -1); |
| 798 |
} |
| 799 |
ao2_iterator_destroy(&aoi); |
| 800 |
|
| 801 |
return CLI_SUCCESS; |
| 802 |
} |
| 803 |
|
| 804 |
static struct ast_cli_entry cli_odbc[] = { |
| 805 |
AST_CLI_DEFINE(handle_cli_odbc_show, "List ODBC DSN(s)") |
| 806 |
}; |
| 807 |
|
| 808 |
static void odbc_register_class(struct odbc_class *class, int preconnect) |
| 809 |
{ |
| 810 |
struct odbc_obj *obj; |
| 811 |
|
| 812 |
ao2_link(class_container, class); |
| 813 |
|
| 814 |
|
| 815 |
if (!preconnect) { |
| 816 |
return; |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
obj = ast_odbc_request_obj(class->name, 0); |
| 821 |
if (obj) { |
| 822 |
ast_odbc_release_obj(obj); |
| 823 |
} |
| 824 |
|
| 825 |
return; |
| 826 |
} |
| 827 |
|
| 828 |
void ast_odbc_release_obj(struct odbc_obj *obj) |
| 829 |
{ |
| 830 |
struct odbc_class *class = obj->parent; |
| 831 |
|
| 832 |
ast_debug(2, "Releasing ODBC handle %p into pool\n", obj); |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
obj->parent = NULL; |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
ast_free(obj->sql_text); |
| 847 |
obj->sql_text = NULL; |
| 848 |
|
| 849 |
ast_mutex_lock(&class->lock); |
| 850 |
if (class->cache_is_queue) { |
| 851 |
AST_LIST_INSERT_TAIL(&class->connections, obj, list); |
| 852 |
} else { |
| 853 |
AST_LIST_INSERT_HEAD(&class->connections, obj, list); |
| 854 |
} |
| 855 |
|
| 856 |
if (class->cur_cache >= class->max_cache_size) { |
| 857 |
|
| 858 |
if (class->cache_is_queue) { |
| 859 |
|
| 860 |
obj = AST_LIST_REMOVE_HEAD(&class->connections, list); |
| 861 |
} else { |
| 862 |
|
| 863 |
obj = AST_LIST_LAST(&class->connections); |
| 864 |
AST_LIST_REMOVE(&class->connections, obj, list); |
| 865 |
} |
| 866 |
--class->connection_cnt; |
| 867 |
ast_mutex_unlock(&class->lock); |
| 868 |
|
| 869 |
ast_debug(2, "ODBC Pool '%s' exceeded cache size, dropping '%p', connection count is %zd (%u cached)\n", |
| 870 |
class->name, obj, class->connection_cnt, class->cur_cache); |
| 871 |
|
| 872 |
ao2_ref(obj, -1); |
| 873 |
|
| 874 |
ast_mutex_lock(&class->lock); |
| 875 |
} else { |
| 876 |
++class->cur_cache; |
| 877 |
} |
| 878 |
ast_cond_signal(&class->cond); |
| 879 |
ast_mutex_unlock(&class->lock); |
| 880 |
|
| 881 |
ao2_ref(class, -1); |
| 882 |
} |
| 883 |
|
| 884 |
int ast_odbc_backslash_is_escape(struct odbc_obj *obj) |
| 885 |
{ |
| 886 |
return obj->parent->backslash_is_escape; |
| 887 |
} |
| 888 |
|
| 889 |
static int aoro2_class_cb(void *obj, void *arg, int flags) |
| 890 |
{ |
| 891 |
struct odbc_class *class = obj; |
| 892 |
char *name = arg; |
| 893 |
if (!strcmp(class->name, name) && !class->delme) { |
| 894 |
return CMP_MATCH | CMP_STOP; |
| 895 |
} |
| 896 |
return 0; |
| 897 |
} |
| 898 |
|
| 899 |
unsigned int ast_odbc_get_max_connections(const char *name) |
| 900 |
{ |
| 901 |
struct odbc_class *class; |
| 902 |
unsigned int max_connections; |
| 903 |
|
| 904 |
class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name); |
| 905 |
if (!class) { |
| 906 |
return 0; |
| 907 |
} |
| 908 |
|
| 909 |
max_connections = class->maxconnections; |
| 910 |
ao2_ref(class, -1); |
| 911 |
|
| 912 |
return max_connections; |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
static int connection_dead(struct odbc_obj *connection, struct odbc_class *class) |
| 924 |
{ |
| 925 |
char *test_sql = "select 1"; |
| 926 |
SQLINTEGER dead; |
| 927 |
SQLRETURN res; |
| 928 |
SQLHSTMT stmt; |
| 929 |
|
| 930 |
res = SQLGetConnectAttr(connection->con, SQL_ATTR_CONNECTION_DEAD, &dead, 0, 0); |
| 931 |
if (SQL_SUCCEEDED(res)) { |
| 932 |
return dead == SQL_CD_TRUE ? 1 : 0; |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
res = SQLAllocHandle(SQL_HANDLE_STMT, connection->con, &stmt); |
| 939 |
if (!SQL_SUCCEEDED(res)) { |
| 940 |
return 1; |
| 941 |
} |
| 942 |
|
| 943 |
if (!ast_strlen_zero(class->sanitysql)) { |
| 944 |
test_sql = class->sanitysql; |
| 945 |
} |
| 946 |
|
| 947 |
res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS); |
| 948 |
if (!SQL_SUCCEEDED(res)) { |
| 949 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 950 |
return 1; |
| 951 |
} |
| 952 |
|
| 953 |
res = SQLExecute(stmt); |
| 954 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 955 |
|
| 956 |
return SQL_SUCCEEDED(res) ? 0 : 1; |
| 957 |
} |
| 958 |
|
| 959 |
struct odbc_obj *_ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno) |
| 960 |
{ |
| 961 |
struct odbc_obj *obj = NULL; |
| 962 |
struct odbc_class *class; |
| 963 |
|
| 964 |
if (!(class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name))) { |
| 965 |
ast_debug(1, "Class '%s' not found!\n", name); |
| 966 |
return NULL; |
| 967 |
} |
| 968 |
|
| 969 |
while (!obj) { |
| 970 |
ast_mutex_lock(&class->lock); |
| 971 |
|
| 972 |
obj = AST_LIST_REMOVE_HEAD(&class->connections, list); |
| 973 |
if (obj) { |
| 974 |
--class->cur_cache; |
| 975 |
} |
| 976 |
|
| 977 |
ast_mutex_unlock(&class->lock); |
| 978 |
|
| 979 |
if (!obj) { |
| 980 |
ast_mutex_lock(&class->lock); |
| 981 |
|
| 982 |
if (class->connection_cnt < class->maxconnections) { |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor); |
| 988 |
if (!obj) { |
| 989 |
ast_mutex_unlock(&class->lock); |
| 990 |
break; |
| 991 |
} |
| 992 |
|
| 993 |
obj->parent = ao2_bump(class); |
| 994 |
|
| 995 |
class->connection_cnt++; |
| 996 |
|
| 997 |
ast_mutex_unlock(&class->lock); |
| 998 |
|
| 999 |
if (odbc_obj_connect(obj) == ODBC_FAIL) { |
| 1000 |
ast_mutex_lock(&class->lock); |
| 1001 |
class->connection_cnt--; |
| 1002 |
ast_cond_signal(&class->cond); |
| 1003 |
ast_mutex_unlock(&class->lock); |
| 1004 |
ao2_ref(obj->parent, -1); |
| 1005 |
ao2_ref(obj, -1); |
| 1006 |
obj = NULL; |
| 1007 |
break; |
| 1008 |
} |
| 1009 |
|
| 1010 |
ast_mutex_lock(&class->lock); |
| 1011 |
|
| 1012 |
ast_debug(2, "Created ODBC handle %p on class '%s', new count is %zd\n", obj, |
| 1013 |
name, class->connection_cnt); |
| 1014 |
|
| 1015 |
} else { |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
ast_cond_wait(&class->cond, &class->lock); |
| 1021 |
} |
| 1022 |
|
| 1023 |
ast_mutex_unlock(&class->lock); |
| 1024 |
|
| 1025 |
} else if (connection_dead(obj, class)) { |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
ast_mutex_lock(&class->lock); |
| 1030 |
|
| 1031 |
class->connection_cnt--; |
| 1032 |
|
| 1033 |
|
| 1034 |
ast_debug(2, "ODBC handle %p dead - removing from class '%s', new count is %zd\n", |
| 1035 |
obj, name, class->connection_cnt); |
| 1036 |
|
| 1037 |
ast_mutex_unlock(&class->lock); |
| 1038 |
|
| 1039 |
ao2_ref(obj, -1); |
| 1040 |
obj = NULL; |
| 1041 |
} else { |
| 1042 |
|
| 1043 |
|
| 1044 |
obj->parent = ao2_bump(class); |
| 1045 |
ast_debug(2, "Reusing ODBC handle %p from class '%s'\n", obj, name); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
ao2_ref(class, -1); |
| 1050 |
|
| 1051 |
return obj; |
| 1052 |
} |
| 1053 |
|
| 1054 |
struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno) |
| 1055 |
{ |
| 1056 |
struct ast_flags flags = { check ? RES_ODBC_SANITY_CHECK : 0 }; |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
return _ast_odbc_request_obj2(name, flags, file, function, lineno); |
| 1063 |
} |
| 1064 |
|
| 1065 |
static odbc_status odbc_obj_disconnect(struct odbc_obj *obj) |
| 1066 |
{ |
| 1067 |
int res; |
| 1068 |
SQLINTEGER err; |
| 1069 |
short int mlen; |
| 1070 |
unsigned char msg[200], state[10]; |
| 1071 |
SQLHDBC con; |
| 1072 |
|
| 1073 |
|
| 1074 |
if (!obj->con) { |
| 1075 |
return ODBC_SUCCESS; |
| 1076 |
} |
| 1077 |
|
| 1078 |
con = obj->con; |
| 1079 |
obj->con = NULL; |
| 1080 |
res = SQLDisconnect(con); |
| 1081 |
|
| 1082 |
if ((res = SQLFreeHandle(SQL_HANDLE_DBC, con)) == SQL_SUCCESS) { |
| 1083 |
ast_debug(3, "Database handle %p (connection %p) deallocated\n", obj, con); |
| 1084 |
} else { |
| 1085 |
SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen); |
| 1086 |
ast_log(LOG_WARNING, "Unable to deallocate database handle %p? %d errno=%d %s\n", con, res, (int)err, msg); |
| 1087 |
} |
| 1088 |
|
| 1089 |
return ODBC_SUCCESS; |
| 1090 |
} |
| 1091 |
|
| 1092 |
static odbc_status odbc_obj_connect(struct odbc_obj *obj) |
| 1093 |
{ |
| 1094 |
int res; |
| 1095 |
SQLINTEGER err; |
| 1096 |
short int mlen; |
| 1097 |
unsigned char msg[200], state[10]; |
| 1098 |
#ifdef NEEDTRACE |
| 1099 |
SQLINTEGER enable = 1; |
| 1100 |
char *tracefile = "/tmp/odbc.trace"; |
| 1101 |
#endif |
| 1102 |
SQLHDBC con; |
| 1103 |
long int negative_cache_expiration; |
| 1104 |
|
| 1105 |
ast_assert(obj->con == NULL); |
| 1106 |
ast_debug(3, "Connecting %s(%p)\n", obj->parent->name, obj); |
| 1107 |
|
| 1108 |
|
| 1109 |
negative_cache_expiration = obj->parent->last_negative_connect.tv_sec + obj->parent->negative_connection_cache.tv_sec; |
| 1110 |
if (time(NULL) < negative_cache_expiration) { |
| 1111 |
char secs[AST_TIME_T_LEN]; |
| 1112 |
ast_time_t_to_string(negative_cache_expiration - time(NULL), secs, sizeof(secs)); |
| 1113 |
ast_log(LOG_WARNING, "Not connecting to %s. Negative connection cache for %s seconds\n", obj->parent->name, secs); |
| 1114 |
return ODBC_FAIL; |
| 1115 |
} |
| 1116 |
|
| 1117 |
res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &con); |
| 1118 |
|
| 1119 |
if (!SQL_SUCCEEDED(res)) { |
| 1120 |
ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res); |
| 1121 |
obj->parent->last_negative_connect = ast_tvnow(); |
| 1122 |
return ODBC_FAIL; |
| 1123 |
} |
| 1124 |
SQLSetConnectAttr(con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)(long) obj->parent->conntimeout, 0); |
| 1125 |
SQLSetConnectAttr(con, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER *)(long) obj->parent->conntimeout, 0); |
| 1126 |
#ifdef NEEDTRACE |
| 1127 |
SQLSetConnectAttr(con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER); |
| 1128 |
SQLSetConnectAttr(con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile)); |
| 1129 |
#endif |
| 1130 |
|
| 1131 |
res = SQLConnect(con, |
| 1132 |
(SQLCHAR *) obj->parent->dsn, SQL_NTS, |
| 1133 |
(SQLCHAR *) obj->parent->username, SQL_NTS, |
| 1134 |
(SQLCHAR *) obj->parent->password, SQL_NTS); |
| 1135 |
|
| 1136 |
if (!SQL_SUCCEEDED(res)) { |
| 1137 |
SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen); |
| 1138 |
obj->parent->last_negative_connect = ast_tvnow(); |
| 1139 |
ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg); |
| 1140 |
if ((res = SQLFreeHandle(SQL_HANDLE_DBC, con)) != SQL_SUCCESS) { |
| 1141 |
SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen); |
| 1142 |
ast_log(LOG_WARNING, "Unable to deallocate database handle %p? %d errno=%d %s\n", con, res, (int)err, msg); |
| 1143 |
} |
| 1144 |
return ODBC_FAIL; |
| 1145 |
} else { |
| 1146 |
ast_debug(3, "res_odbc: Connected to %s [%s (%p)]\n", obj->parent->name, obj->parent->dsn, obj); |
| 1147 |
} |
| 1148 |
|
| 1149 |
obj->con = con; |
| 1150 |
return ODBC_SUCCESS; |
| 1151 |
} |
| 1152 |
|
| 1153 |
static int reload(void) |
| 1154 |
{ |
| 1155 |
struct odbc_cache_tables *table; |
| 1156 |
struct odbc_class *class; |
| 1157 |
struct ao2_iterator aoi = ao2_iterator_init(class_container, 0); |
| 1158 |
|
| 1159 |
|
| 1160 |
while ((class = ao2_iterator_next(&aoi))) { |
| 1161 |
class->delme = 1; |
| 1162 |
ao2_ref(class, -1); |
| 1163 |
} |
| 1164 |
ao2_iterator_destroy(&aoi); |
| 1165 |
|
| 1166 |
load_odbc_config(); |
| 1167 |
|
| 1168 |
aoi = ao2_iterator_init(class_container, 0); |
| 1169 |
while ((class = ao2_iterator_next(&aoi))) { |
| 1170 |
if (class->delme) { |
| 1171 |
ao2_unlink(class_container, class); |
| 1172 |
} |
| 1173 |
ao2_ref(class, -1); |
| 1174 |
} |
| 1175 |
ao2_iterator_destroy(&aoi); |
| 1176 |
|
| 1177 |
|
| 1178 |
AST_RWLIST_WRLOCK(&odbc_tables); |
| 1179 |
while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) { |
| 1180 |
destroy_table_cache(table); |
| 1181 |
} |
| 1182 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 1183 |
|
| 1184 |
return 0; |
| 1185 |
} |
| 1186 |
|
| 1187 |
static int unload_module(void) |
| 1188 |
{ |
| 1189 |
ao2_cleanup(class_container); |
| 1190 |
ast_cli_unregister_multiple(cli_odbc, ARRAY_LEN(cli_odbc)); |
| 1191 |
|
| 1192 |
return 0; |
| 1193 |
} |
| 1194 |
|
| 1195 |
static int load_module(void) |
| 1196 |
{ |
| 1197 |
class_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, ao2_match_by_addr); |
| 1198 |
if (!class_container) { |
| 1199 |
return AST_MODULE_LOAD_DECLINE; |
| 1200 |
} |
| 1201 |
|
| 1202 |
if (load_odbc_config() == -1) { |
| 1203 |
return AST_MODULE_LOAD_DECLINE; |
| 1204 |
} |
| 1205 |
|
| 1206 |
ast_module_shutdown_ref(ast_module_info->self); |
| 1207 |
ast_cli_register_multiple(cli_odbc, ARRAY_LEN(cli_odbc)); |
| 1208 |
|
| 1209 |
return AST_MODULE_LOAD_SUCCESS; |
| 1210 |
} |
| 1211 |
|
| 1212 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "ODBC resource", |
| 1213 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1214 |
.load = load_module, |
| 1215 |
.unload = unload_module, |
| 1216 |
.reload = reload, |
| 1217 |
.load_pri = AST_MODPRI_REALTIME_DEPEND, |
| 1218 |
.requires = "res_odbc_transaction", |
| 1219 |
); |