| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
|
| 21 |
#include "asterisk/res_odbc.h" |
| 22 |
#include "asterisk/res_odbc_transaction.h" |
| 23 |
#include "asterisk/channel.h" |
| 24 |
#include "asterisk/pbx.h" |
| 25 |
#include "asterisk/app.h" |
| 26 |
#include "asterisk/module.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
struct odbc_txn_frame { |
| 104 |
AST_LIST_ENTRY(odbc_txn_frame) list; |
| 105 |
struct odbc_obj *obj; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
unsigned int active:1; |
| 114 |
unsigned int forcecommit:1; |
| 115 |
unsigned int isolation; |
| 116 |
char name[0]; |
| 117 |
}; |
| 118 |
|
| 119 |
static struct odbc_txn_frame *release_transaction(struct odbc_txn_frame *tx); |
| 120 |
|
| 121 |
static void odbc_txn_free(void *vdata) |
| 122 |
{ |
| 123 |
struct odbc_txn_frame *tx; |
| 124 |
AST_LIST_HEAD(, odbc_txn_frame) *oldlist = vdata; |
| 125 |
|
| 126 |
ast_debug(2, "odbc_txn_free(%p) called\n", vdata); |
| 127 |
|
| 128 |
AST_LIST_LOCK(oldlist); |
| 129 |
while ((tx = AST_LIST_REMOVE_HEAD(oldlist, list))) { |
| 130 |
release_transaction(tx); |
| 131 |
} |
| 132 |
AST_LIST_UNLOCK(oldlist); |
| 133 |
AST_LIST_HEAD_DESTROY(oldlist); |
| 134 |
ast_free(oldlist); |
| 135 |
} |
| 136 |
|
| 137 |
static const struct ast_datastore_info txn_info = { |
| 138 |
.type = "ODBC_Transaction", |
| 139 |
.destroy = odbc_txn_free, |
| 140 |
}; |
| 141 |
|
| 142 |
static struct odbc_txn_frame *create_transaction(struct ast_channel *chan, const char *name, const char *dsn) |
| 143 |
{ |
| 144 |
struct ast_datastore *txn_store; |
| 145 |
AST_LIST_HEAD(, odbc_txn_frame) *oldlist; |
| 146 |
struct odbc_txn_frame *txn = NULL; |
| 147 |
struct odbc_txn_frame *otxn; |
| 148 |
|
| 149 |
if (ast_strlen_zero(dsn)) { |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
ast_channel_lock(chan); |
| 154 |
if ((txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) { |
| 155 |
oldlist = txn_store->data; |
| 156 |
} else { |
| 157 |
if (!(txn_store = ast_datastore_alloc(&txn_info, NULL))) { |
| 158 |
ast_log(LOG_ERROR, "Unable to allocate a new datastore. Cannot create a new transaction.\n"); |
| 159 |
ast_channel_unlock(chan); |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
|
| 163 |
if (!(oldlist = ast_calloc(1, sizeof(*oldlist)))) { |
| 164 |
ast_log(LOG_ERROR, "Unable to allocate datastore list head. Cannot create a new transaction.\n"); |
| 165 |
ast_datastore_free(txn_store); |
| 166 |
ast_channel_unlock(chan); |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
txn_store->data = oldlist; |
| 171 |
AST_LIST_HEAD_INIT(oldlist); |
| 172 |
ast_channel_datastore_add(chan, txn_store); |
| 173 |
} |
| 174 |
ast_channel_unlock(chan); |
| 175 |
|
| 176 |
txn = ast_calloc(1, sizeof(*txn) + strlen(name) + 1); |
| 177 |
if (!txn) { |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
strcpy(txn->name, name); |
| 182 |
txn->obj = ast_odbc_request_obj(dsn, 0); |
| 183 |
if (!txn->obj) { |
| 184 |
ast_free(txn); |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
txn->isolation = ast_odbc_class_get_isolation(txn->obj->parent); |
| 188 |
txn->forcecommit = ast_odbc_class_get_forcecommit(txn->obj->parent); |
| 189 |
txn->active = 1; |
| 190 |
|
| 191 |
if (SQLSetConnectAttr(txn->obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) { |
| 192 |
ast_odbc_print_errors(SQL_HANDLE_DBC, txn->obj->con, "SetConnectAttr (Autocommit)"); |
| 193 |
ast_odbc_release_obj(txn->obj); |
| 194 |
ast_free(txn); |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
if (SQLSetConnectAttr(txn->obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)txn->isolation, 0) == SQL_ERROR) { |
| 200 |
ast_odbc_print_errors(SQL_HANDLE_DBC, txn->obj->con, "SetConnectAttr"); |
| 201 |
ast_odbc_release_obj(txn->obj); |
| 202 |
ast_free(txn); |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
AST_LIST_LOCK(oldlist); |
| 208 |
AST_LIST_TRAVERSE(oldlist, otxn, list) { |
| 209 |
otxn->active = 0; |
| 210 |
} |
| 211 |
AST_LIST_INSERT_TAIL(oldlist, txn, list); |
| 212 |
AST_LIST_UNLOCK(oldlist); |
| 213 |
|
| 214 |
return txn; |
| 215 |
} |
| 216 |
|
| 217 |
static struct odbc_txn_frame *find_transaction(struct ast_channel *chan, const char *name, int active) |
| 218 |
{ |
| 219 |
struct ast_datastore *txn_store; |
| 220 |
AST_LIST_HEAD(, odbc_txn_frame) *oldlist; |
| 221 |
struct odbc_txn_frame *txn = NULL; |
| 222 |
|
| 223 |
if (!chan || (!active && !name)) { |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
ast_channel_lock(chan); |
| 228 |
txn_store = ast_channel_datastore_find(chan, &txn_info, NULL); |
| 229 |
ast_channel_unlock(chan); |
| 230 |
|
| 231 |
if (!txn_store) { |
| 232 |
|
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
oldlist = txn_store->data; |
| 237 |
AST_LIST_LOCK(oldlist); |
| 238 |
|
| 239 |
AST_LIST_TRAVERSE(oldlist, txn, list) { |
| 240 |
if (active) { |
| 241 |
if (txn->active) { |
| 242 |
break; |
| 243 |
} |
| 244 |
} else if (!strcasecmp(txn->name, name)) { |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
AST_LIST_UNLOCK(oldlist); |
| 249 |
|
| 250 |
return txn; |
| 251 |
} |
| 252 |
|
| 253 |
static struct odbc_txn_frame *release_transaction(struct odbc_txn_frame *tx) |
| 254 |
{ |
| 255 |
if (!tx) { |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
|
| 259 |
ast_debug(2, "release_transaction(%p) called (tx->obj = %p\n", tx, tx->obj); |
| 260 |
|
| 261 |
ast_debug(1, "called on a transactional handle with %s\n", tx->forcecommit ? "COMMIT" : "ROLLBACK"); |
| 262 |
if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, tx->forcecommit ? SQL_COMMIT : SQL_ROLLBACK) == SQL_ERROR) { |
| 263 |
ast_odbc_print_errors(SQL_HANDLE_DBC, tx->obj->con, "SQLEndTran"); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
if (SQLSetConnectAttr(tx->obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_ON, 0) == SQL_ERROR) { |
| 273 |
ast_odbc_print_errors(SQL_HANDLE_DBC, tx->obj->con, "SQLSetAttr"); |
| 274 |
} |
| 275 |
|
| 276 |
ast_odbc_release_obj(tx->obj); |
| 277 |
ast_free(tx); |
| 278 |
return NULL; |
| 279 |
} |
| 280 |
|
| 281 |
static int commit_exec(struct ast_channel *chan, const char *data) |
| 282 |
{ |
| 283 |
struct odbc_txn_frame *tx; |
| 284 |
|
| 285 |
if (ast_strlen_zero(data)) { |
| 286 |
tx = find_transaction(chan, NULL, 1); |
| 287 |
} else { |
| 288 |
tx = find_transaction(chan, data, 0); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
pbx_builtin_setvar_helper(chan, "COMMIT_RESULT", "OK"); |
| 293 |
|
| 294 |
if (tx) { |
| 295 |
if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, SQL_COMMIT) == SQL_ERROR) { |
| 296 |
struct ast_str *errors = ast_odbc_print_errors(SQL_HANDLE_DBC, tx->obj->con, "SQLEndTran"); |
| 297 |
pbx_builtin_setvar_helper(chan, "COMMIT_RESULT", ast_str_buffer(errors)); |
| 298 |
} |
| 299 |
} |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
static int rollback_exec(struct ast_channel *chan, const char *data) |
| 304 |
{ |
| 305 |
struct odbc_txn_frame *tx; |
| 306 |
|
| 307 |
if (ast_strlen_zero(data)) { |
| 308 |
tx = find_transaction(chan, NULL, 1); |
| 309 |
} else { |
| 310 |
tx = find_transaction(chan, data, 0); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
pbx_builtin_setvar_helper(chan, "ROLLBACK_RESULT", "OK"); |
| 315 |
|
| 316 |
if (tx) { |
| 317 |
if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, SQL_ROLLBACK) == SQL_ERROR) { |
| 318 |
struct ast_str *errors = ast_odbc_print_errors(SQL_HANDLE_DBC, tx->obj->con, "SQLEndTran"); |
| 319 |
pbx_builtin_setvar_helper(chan, "ROLLBACK_RESULT", ast_str_buffer(errors)); |
| 320 |
} |
| 321 |
} |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static int acf_transaction_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 326 |
{ |
| 327 |
AST_DECLARE_APP_ARGS(args, |
| 328 |
AST_APP_ARG(property); |
| 329 |
AST_APP_ARG(opt); |
| 330 |
); |
| 331 |
struct odbc_txn_frame *tx; |
| 332 |
|
| 333 |
AST_STANDARD_APP_ARGS(args, data); |
| 334 |
if (strcasecmp(args.property, "transaction") == 0) { |
| 335 |
if ((tx = find_transaction(chan, NULL, 1))) { |
| 336 |
ast_copy_string(buf, tx->name, len); |
| 337 |
return 0; |
| 338 |
} |
| 339 |
} else if (strcasecmp(args.property, "isolation") == 0) { |
| 340 |
if (!ast_strlen_zero(args.opt)) { |
| 341 |
tx = find_transaction(chan, args.opt, 0); |
| 342 |
} else { |
| 343 |
tx = find_transaction(chan, NULL, 1); |
| 344 |
} |
| 345 |
if (tx) { |
| 346 |
ast_copy_string(buf, ast_odbc_isolation2text(tx->isolation), len); |
| 347 |
return 0; |
| 348 |
} |
| 349 |
} else if (strcasecmp(args.property, "forcecommit") == 0) { |
| 350 |
if (!ast_strlen_zero(args.opt)) { |
| 351 |
tx = find_transaction(chan, args.opt, 0); |
| 352 |
} else { |
| 353 |
tx = find_transaction(chan, NULL, 1); |
| 354 |
} |
| 355 |
if (tx) { |
| 356 |
ast_copy_string(buf, tx->forcecommit ? "1" : "0", len); |
| 357 |
return 0; |
| 358 |
} |
| 359 |
} |
| 360 |
return -1; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
static int mark_transaction_active(struct ast_channel *chan, struct odbc_txn_frame *tx) |
| 369 |
{ |
| 370 |
struct ast_datastore *txn_store; |
| 371 |
AST_LIST_HEAD(, odbc_txn_frame) *oldlist; |
| 372 |
struct odbc_txn_frame *active = NULL, *txn; |
| 373 |
|
| 374 |
if (!chan) { |
| 375 |
return -1; |
| 376 |
} |
| 377 |
|
| 378 |
ast_channel_lock(chan); |
| 379 |
if (!(txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) { |
| 380 |
ast_channel_unlock(chan); |
| 381 |
return -1; |
| 382 |
} |
| 383 |
|
| 384 |
oldlist = txn_store->data; |
| 385 |
AST_LIST_LOCK(oldlist); |
| 386 |
AST_LIST_TRAVERSE(oldlist, txn, list) { |
| 387 |
if (txn == tx) { |
| 388 |
txn->active = 1; |
| 389 |
active = txn; |
| 390 |
} else { |
| 391 |
txn->active = 0; |
| 392 |
} |
| 393 |
} |
| 394 |
AST_LIST_UNLOCK(oldlist); |
| 395 |
ast_channel_unlock(chan); |
| 396 |
return active ? 0 : -1; |
| 397 |
} |
| 398 |
|
| 399 |
static int acf_transaction_write(struct ast_channel *chan, const char *cmd, char *s, const char *value) |
| 400 |
{ |
| 401 |
AST_DECLARE_APP_ARGS(args, |
| 402 |
AST_APP_ARG(property); |
| 403 |
AST_APP_ARG(opt); |
| 404 |
); |
| 405 |
struct odbc_txn_frame *tx; |
| 406 |
|
| 407 |
AST_STANDARD_APP_ARGS(args, s); |
| 408 |
if (strcasecmp(args.property, "transaction") == 0) { |
| 409 |
|
| 410 |
if ((tx = find_transaction(chan, value, 0))) { |
| 411 |
mark_transaction_active(chan, tx); |
| 412 |
} else if (!create_transaction(chan, value, args.opt)) { |
| 413 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE"); |
| 414 |
return -1; |
| 415 |
} |
| 416 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK"); |
| 417 |
return 0; |
| 418 |
} else if (strcasecmp(args.property, "forcecommit") == 0) { |
| 419 |
|
| 420 |
if (ast_strlen_zero(args.opt)) { |
| 421 |
tx = find_transaction(chan, NULL, 1); |
| 422 |
} else { |
| 423 |
tx = find_transaction(chan, args.opt, 0); |
| 424 |
} |
| 425 |
if (!tx) { |
| 426 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE"); |
| 427 |
return -1; |
| 428 |
} |
| 429 |
if (ast_true(value)) { |
| 430 |
tx->forcecommit = 1; |
| 431 |
} else if (ast_false(value)) { |
| 432 |
tx->forcecommit = 0; |
| 433 |
} else { |
| 434 |
ast_log(LOG_ERROR, "Invalid value for forcecommit: '%s'\n", S_OR(value, "")); |
| 435 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_VALUE"); |
| 436 |
return -1; |
| 437 |
} |
| 438 |
|
| 439 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK"); |
| 440 |
return 0; |
| 441 |
} else if (strcasecmp(args.property, "isolation") == 0) { |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
int isolation = ast_odbc_text2isolation(value); |
| 449 |
if (ast_strlen_zero(args.opt)) { |
| 450 |
tx = find_transaction(chan, NULL, 1); |
| 451 |
} else { |
| 452 |
tx = find_transaction(chan, args.opt, 0); |
| 453 |
} |
| 454 |
if (!tx) { |
| 455 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE"); |
| 456 |
return -1; |
| 457 |
} |
| 458 |
if (isolation == 0) { |
| 459 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_VALUE"); |
| 460 |
ast_log(LOG_ERROR, "Invalid isolation specification: '%s'\n", S_OR(value, "")); |
| 461 |
} else if (SQLSetConnectAttr(tx->obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)isolation, 0) == SQL_ERROR) { |
| 462 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "SQL_ERROR"); |
| 463 |
ast_odbc_print_errors(SQL_HANDLE_DBC, tx->obj->con, "SetConnectAttr (Txn isolation)"); |
| 464 |
} else { |
| 465 |
pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK"); |
| 466 |
tx->isolation = isolation; |
| 467 |
} |
| 468 |
return 0; |
| 469 |
} else { |
| 470 |
ast_log(LOG_ERROR, "Unknown property: '%s'\n", args.property); |
| 471 |
return -1; |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
struct odbc_obj *ast_odbc_retrieve_transaction_obj(struct ast_channel *chan, const char *objname) |
| 476 |
{ |
| 477 |
struct ast_datastore *txn_store; |
| 478 |
AST_LIST_HEAD(, odbc_txn_frame) *oldlist; |
| 479 |
struct odbc_txn_frame *txn = NULL; |
| 480 |
|
| 481 |
if (!chan || !objname) { |
| 482 |
|
| 483 |
return NULL; |
| 484 |
} |
| 485 |
|
| 486 |
ast_channel_lock(chan); |
| 487 |
if ((txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) { |
| 488 |
oldlist = txn_store->data; |
| 489 |
} else { |
| 490 |
ast_channel_unlock(chan); |
| 491 |
return NULL; |
| 492 |
} |
| 493 |
|
| 494 |
AST_LIST_LOCK(oldlist); |
| 495 |
ast_channel_unlock(chan); |
| 496 |
|
| 497 |
AST_LIST_TRAVERSE(oldlist, txn, list) { |
| 498 |
if (txn->obj && txn->obj->parent && !strcmp(ast_odbc_class_get_name(txn->obj->parent), objname)) { |
| 499 |
AST_LIST_UNLOCK(oldlist); |
| 500 |
return txn->obj; |
| 501 |
} |
| 502 |
} |
| 503 |
AST_LIST_UNLOCK(oldlist); |
| 504 |
return NULL; |
| 505 |
} |
| 506 |
|
| 507 |
static struct ast_custom_function odbc_function = { |
| 508 |
.name = "ODBC", |
| 509 |
.read = acf_transaction_read, |
| 510 |
.write = acf_transaction_write, |
| 511 |
}; |
| 512 |
|
| 513 |
static const char * const app_commit = "ODBC_Commit"; |
| 514 |
static const char * const app_rollback = "ODBC_Rollback"; |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
static int unload_module(void) |
| 522 |
{ |
| 523 |
return -1; |
| 524 |
} |
| 525 |
|
| 526 |
static int load_module(void) |
| 527 |
{ |
| 528 |
ast_register_application_xml(app_commit, commit_exec); |
| 529 |
ast_register_application_xml(app_rollback, rollback_exec); |
| 530 |
ast_custom_function_register(&odbc_function); |
| 531 |
return 0; |
| 532 |
} |
| 533 |
|
| 534 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "ODBC transaction resource", |
| 535 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 536 |
.load = load_module, |
| 537 |
.unload = unload_module, |
| 538 |
.load_pri = AST_MODPRI_REALTIME_DEPEND, |
| 539 |
); |