Merged revisions 283561 via svnmerge from
[asterisk/asterisk.git] / res / res_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * res_odbc.c <ODBC resource manager>
9  * Copyright (C) 2004 - 2005 Anthony Minessale II <anthmct@yahoo.com>
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2. See the LICENSE file
19  * at the top of the source tree.
20  */
21
22 /*! \file
23  *
24  * \brief ODBC resource manager
25  * 
26  * \author Mark Spencer <markster@digium.com>
27  * \author Anthony Minessale II <anthmct@yahoo.com>
28  * \author Tilghman Lesher <tilghman@digium.com>
29  *
30  * \arg See also: \ref cdr_odbc
31  */
32
33 /*** MODULEINFO
34         <depend>generic_odbc</depend>
35         <depend>ltdl</depend>
36  ***/
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41
42 #include "asterisk/file.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/config.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/cli.h"
48 #include "asterisk/lock.h"
49 #include "asterisk/res_odbc.h"
50 #include "asterisk/time.h"
51 #include "asterisk/astobj2.h"
52 #include "asterisk/app.h"
53 #include "asterisk/strings.h"
54 #include "asterisk/threadstorage.h"
55 #include "asterisk/data.h"
56
57 /*** DOCUMENTATION
58         <function name="ODBC" language="en_US">
59                 <synopsis>
60                         Controls ODBC transaction properties.
61                 </synopsis>
62                 <syntax>
63                         <parameter name="property" required="true">
64                                 <enumlist>
65                                         <enum name="transaction">
66                                                 <para>Gets or sets the active transaction ID.  If set, and the transaction ID does not
67                                                 exist and a <replaceable>database name</replaceable> is specified as an argument, it will be created.</para>
68                                         </enum>
69                                         <enum name="forcecommit">
70                                                 <para>Controls whether a transaction will be automatically committed when the channel
71                                                 hangs up.  Defaults to false.  If a <replaceable>transaction ID</replaceable> is specified in the optional argument,
72                                                 the property will be applied to that ID, otherwise to the current active ID.</para>
73                                         </enum>
74                                         <enum name="isolation">
75                                                 <para>Controls the data isolation on uncommitted transactions.  May be one of the
76                                                 following: <literal>read_committed</literal>, <literal>read_uncommitted</literal>,
77                                                 <literal>repeatable_read</literal>, or <literal>serializable</literal>.  Defaults to the
78                                                 database setting in <filename>res_odbc.conf</filename> or <literal>read_committed</literal>
79                                                 if not specified.  If a <replaceable>transaction ID</replaceable> is specified as an optional argument, it will be
80                                                 applied to that ID, otherwise the current active ID.</para>
81                                         </enum>
82                                 </enumlist>
83                         </parameter>
84                         <parameter name="argument" required="false" />
85                 </syntax>
86                 <description>
87                         <para>The ODBC() function allows setting several properties to influence how a connected
88                         database processes transactions.</para>
89                 </description>
90         </function>
91         <application name="ODBC_Commit" language="en_US">
92                 <synopsis>
93                         Commits a currently open database transaction.
94                 </synopsis>
95                 <syntax>
96                         <parameter name="transaction ID" required="no" />
97                 </syntax>
98                 <description>
99                         <para>Commits the database transaction specified by <replaceable>transaction ID</replaceable>
100                         or the current active transaction, if not specified.</para>
101                 </description>
102         </application>
103         <application name="ODBC_Rollback" language="en_US">
104                 <synopsis>
105                         Rollback a currently open database transaction.
106                 </synopsis>
107                 <syntax>
108                         <parameter name="transaction ID" required="no" />
109                 </syntax>
110                 <description>
111                         <para>Rolls back the database transaction specified by <replaceable>transaction ID</replaceable>
112                         or the current active transaction, if not specified.</para>
113                 </description>
114         </application>
115  ***/
116
117 struct odbc_class
118 {
119         AST_LIST_ENTRY(odbc_class) list;
120         char name[80];
121         char dsn[80];
122         char *username;
123         char *password;
124         char *sanitysql;
125         SQLHENV env;
126         unsigned int haspool:1;              /*!< Boolean - TDS databases need this */
127         unsigned int delme:1;                /*!< Purge the class */
128         unsigned int backslash_is_escape:1;  /*!< On this database, the backslash is a native escape sequence */
129         unsigned int forcecommit:1;          /*!< Should uncommitted transactions be auto-committed on handle release? */
130         unsigned int isolation;              /*!< Flags for how the DB should deal with data in other, uncommitted transactions */
131         unsigned int limit;                  /*!< Maximum number of database handles we will allow */
132         int count;                           /*!< Running count of pooled connections */
133         unsigned int idlecheck;              /*!< Recheck the connection if it is idle for this long (in seconds) */
134         unsigned int conntimeout;            /*!< Maximum time the connection process should take */
135         /*! When a connection fails, cache that failure for how long? */
136         struct timeval negative_connection_cache;
137         /*! When a connection fails, when did that last occur? */
138         struct timeval last_negative_connect;
139         /*! List of handles associated with this class */
140         struct ao2_container *obj_container;
141 };
142
143 static struct ao2_container *class_container;
144
145 static AST_RWLIST_HEAD_STATIC(odbc_tables, odbc_cache_tables);
146
147 static odbc_status odbc_obj_connect(struct odbc_obj *obj);
148 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj);
149 static int odbc_register_class(struct odbc_class *class, int connect);
150 static void odbc_txn_free(void *data);
151 static void odbc_release_obj2(struct odbc_obj *obj, struct odbc_txn_frame *tx);
152
153 AST_THREADSTORAGE(errors_buf);
154
155 static struct ast_datastore_info txn_info = {
156         .type = "ODBC_Transaction",
157         .destroy = odbc_txn_free,
158 };
159
160 struct odbc_txn_frame {
161         AST_LIST_ENTRY(odbc_txn_frame) list;
162         struct ast_channel *owner;
163         struct odbc_obj *obj;        /*!< Database handle within which transacted statements are run */
164         /*!\brief Is this record the current active transaction within the channel?
165          * Note that the active flag is really only necessary for statements which
166          * are triggered from the dialplan, as there isn't a direct correlation
167          * between multiple statements.  Applications wishing to use transactions
168          * may simply perform each statement on the same odbc_obj, which keeps the
169          * transaction persistent.
170          */
171         unsigned int active:1;
172         unsigned int forcecommit:1;     /*!< Should uncommitted transactions be auto-committed on handle release? */
173         unsigned int isolation;         /*!< Flags for how the DB should deal with data in other, uncommitted transactions */
174         char name[0];                   /*!< Name of this transaction ID */
175 };
176
177 #define DATA_EXPORT_ODBC_CLASS(MEMBER)                          \
178         MEMBER(odbc_class, name, AST_DATA_STRING)               \
179         MEMBER(odbc_class, dsn, AST_DATA_STRING)                \
180         MEMBER(odbc_class, username, AST_DATA_STRING)           \
181         MEMBER(odbc_class, password, AST_DATA_PASSWORD)         \
182         MEMBER(odbc_class, limit, AST_DATA_INTEGER)             \
183         MEMBER(odbc_class, count, AST_DATA_INTEGER)             \
184         MEMBER(odbc_class, forcecommit, AST_DATA_BOOLEAN)
185
186 AST_DATA_STRUCTURE(odbc_class, DATA_EXPORT_ODBC_CLASS);
187
188 static const char *isolation2text(int iso)
189 {
190         if (iso == SQL_TXN_READ_COMMITTED) {
191                 return "read_committed";
192         } else if (iso == SQL_TXN_READ_UNCOMMITTED) {
193                 return "read_uncommitted";
194         } else if (iso == SQL_TXN_SERIALIZABLE) {
195                 return "serializable";
196         } else if (iso == SQL_TXN_REPEATABLE_READ) {
197                 return "repeatable_read";
198         } else {
199                 return "unknown";
200         }
201 }
202
203 static int text2isolation(const char *txt)
204 {
205         if (strncasecmp(txt, "read_", 5) == 0) {
206                 if (strncasecmp(txt + 5, "c", 1) == 0) {
207                         return SQL_TXN_READ_COMMITTED;
208                 } else if (strncasecmp(txt + 5, "u", 1) == 0) {
209                         return SQL_TXN_READ_UNCOMMITTED;
210                 } else {
211                         return 0;
212                 }
213         } else if (strncasecmp(txt, "ser", 3) == 0) {
214                 return SQL_TXN_SERIALIZABLE;
215         } else if (strncasecmp(txt, "rep", 3) == 0) {
216                 return SQL_TXN_REPEATABLE_READ;
217         } else {
218                 return 0;
219         }
220 }
221
222 static struct odbc_txn_frame *find_transaction(struct ast_channel *chan, struct odbc_obj *obj, const char *name, int active)
223 {
224         struct ast_datastore *txn_store;
225         AST_LIST_HEAD(, odbc_txn_frame) *oldlist;
226         struct odbc_txn_frame *txn = NULL;
227
228         if (!chan && obj && obj->txf && obj->txf->owner) {
229                 chan = obj->txf->owner;
230         } else if (!chan) {
231                 /* No channel == no transaction */
232                 return NULL;
233         }
234
235         ast_channel_lock(chan);
236         if ((txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) {
237                 oldlist = txn_store->data;
238         } else {
239                 /* Need to create a new datastore */
240                 if (!(txn_store = ast_datastore_alloc(&txn_info, NULL))) {
241                         ast_log(LOG_ERROR, "Unable to allocate a new datastore.  Cannot create a new transaction.\n");
242                         ast_channel_unlock(chan);
243                         return NULL;
244                 }
245
246                 if (!(oldlist = ast_calloc(1, sizeof(*oldlist)))) {
247                         ast_log(LOG_ERROR, "Unable to allocate datastore list head.  Cannot create a new transaction.\n");
248                         ast_datastore_free(txn_store);
249                         ast_channel_unlock(chan);
250                         return NULL;
251                 }
252
253                 txn_store->data = oldlist;
254                 AST_LIST_HEAD_INIT(oldlist);
255                 ast_channel_datastore_add(chan, txn_store);
256         }
257
258         AST_LIST_LOCK(oldlist);
259         ast_channel_unlock(chan);
260
261         /* Scanning for an object is *fast*.  Scanning for a name is much slower. */
262         if (obj != NULL || active == 1) {
263                 AST_LIST_TRAVERSE(oldlist, txn, list) {
264                         if (txn->obj == obj || txn->active) {
265                                 AST_LIST_UNLOCK(oldlist);
266                                 return txn;
267                         }
268                 }
269         }
270
271         if (name != NULL) {
272                 AST_LIST_TRAVERSE(oldlist, txn, list) {
273                         if (!strcasecmp(txn->name, name)) {
274                                 AST_LIST_UNLOCK(oldlist);
275                                 return txn;
276                         }
277                 }
278         }
279
280         /* Nothing found, create one */
281         if (name && obj && (txn = ast_calloc(1, sizeof(*txn) + strlen(name) + 1))) {
282                 struct odbc_txn_frame *otxn;
283
284                 strcpy(txn->name, name); /* SAFE */
285                 txn->obj = obj;
286                 txn->isolation = obj->parent->isolation;
287                 txn->forcecommit = obj->parent->forcecommit;
288                 txn->owner = chan;
289                 txn->active = 1;
290
291                 /* On creation, the txn becomes active, and all others inactive */
292                 AST_LIST_TRAVERSE(oldlist, otxn, list) {
293                         otxn->active = 0;
294                 }
295                 AST_LIST_INSERT_TAIL(oldlist, txn, list);
296
297                 obj->txf = txn;
298                 obj->tx = 1;
299         }
300         AST_LIST_UNLOCK(oldlist);
301
302         return txn;
303 }
304
305 static struct odbc_txn_frame *release_transaction(struct odbc_txn_frame *tx)
306 {
307         if (!tx) {
308                 return NULL;
309         }
310
311         ast_debug(2, "release_transaction(%p) called (tx->obj = %p, tx->obj->txf = %p)\n", tx, tx->obj, tx->obj ? tx->obj->txf : NULL);
312
313         /* If we have an owner, disassociate */
314         if (tx->owner) {
315                 struct ast_datastore *txn_store;
316                 AST_LIST_HEAD(, odbc_txn_frame) *oldlist;
317
318                 ast_channel_lock(tx->owner);
319                 if ((txn_store = ast_channel_datastore_find(tx->owner, &txn_info, NULL))) {
320                         oldlist = txn_store->data;
321                         AST_LIST_LOCK(oldlist);
322                         AST_LIST_REMOVE(oldlist, tx, list);
323                         AST_LIST_UNLOCK(oldlist);
324                 }
325                 ast_channel_unlock(tx->owner);
326                 tx->owner = NULL;
327         }
328
329         if (tx->obj) {
330                 /* If we have any uncommitted transactions, they are handled when we release the object */
331                 struct odbc_obj *obj = tx->obj;
332                 /* Prevent recursion during destruction */
333                 tx->obj->txf = NULL;
334                 tx->obj = NULL;
335                 odbc_release_obj2(obj, tx);
336         }
337         ast_free(tx);
338         return NULL;
339 }
340
341 static void odbc_txn_free(void *vdata)
342 {
343         struct odbc_txn_frame *tx;
344         AST_LIST_HEAD(, odbc_txn_frame) *oldlist = vdata;
345
346         ast_debug(2, "odbc_txn_free(%p) called\n", vdata);
347
348         AST_LIST_LOCK(oldlist);
349         while ((tx = AST_LIST_REMOVE_HEAD(oldlist, list))) {
350                 release_transaction(tx);
351         }
352         AST_LIST_UNLOCK(oldlist);
353         AST_LIST_HEAD_DESTROY(oldlist);
354         ast_free(oldlist);
355 }
356
357 static int mark_transaction_active(struct ast_channel *chan, struct odbc_txn_frame *tx)
358 {
359         struct ast_datastore *txn_store;
360         AST_LIST_HEAD(, odbc_txn_frame) *oldlist;
361         struct odbc_txn_frame *active = NULL, *txn;
362
363         if (!chan && tx && tx->owner) {
364                 chan = tx->owner;
365         }
366
367         ast_channel_lock(chan);
368         if (!(txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) {
369                 ast_channel_unlock(chan);
370                 return -1;
371         }
372
373         oldlist = txn_store->data;
374         AST_LIST_LOCK(oldlist);
375         AST_LIST_TRAVERSE(oldlist, txn, list) {
376                 if (txn == tx) {
377                         txn->active = 1;
378                         active = txn;
379                 } else {
380                         txn->active = 0;
381                 }
382         }
383         AST_LIST_UNLOCK(oldlist);
384         ast_channel_unlock(chan);
385         return active ? 0 : -1;
386 }
387
388 static void odbc_class_destructor(void *data)
389 {
390         struct odbc_class *class = data;
391         /* Due to refcounts, we can safely assume that any objects with a reference
392          * to us will prevent our destruction, so we don't need to worry about them.
393          */
394         if (class->username) {
395                 ast_free(class->username);
396         }
397         if (class->password) {
398                 ast_free(class->password);
399         }
400         if (class->sanitysql) {
401                 ast_free(class->sanitysql);
402         }
403         ao2_ref(class->obj_container, -1);
404         SQLFreeHandle(SQL_HANDLE_ENV, class->env);
405 }
406
407 static int null_hash_fn(const void *obj, const int flags)
408 {
409         return 0;
410 }
411
412 static void odbc_obj_destructor(void *data)
413 {
414         struct odbc_obj *obj = data;
415         struct odbc_class *class = obj->parent;
416         obj->parent = NULL;
417         odbc_obj_disconnect(obj);
418         ast_mutex_destroy(&obj->lock);
419         ao2_ref(class, -1);
420 }
421
422 static void destroy_table_cache(struct odbc_cache_tables *table) {
423         struct odbc_cache_columns *col;
424         ast_debug(1, "Destroying table cache for %s\n", table->table);
425         AST_RWLIST_WRLOCK(&table->columns);
426         while ((col = AST_RWLIST_REMOVE_HEAD(&table->columns, list))) {
427                 ast_free(col);
428         }
429         AST_RWLIST_UNLOCK(&table->columns);
430         AST_RWLIST_HEAD_DESTROY(&table->columns);
431         ast_free(table);
432 }
433
434 /*!
435  * \brief Find or create an entry describing the table specified.
436  * \param database Name of an ODBC class on which to query the table
437  * \param tablename Tablename to describe
438  * \retval A structure describing the table layout, or NULL, if the table is not found or another error occurs.
439  * When a structure is returned, the contained columns list will be
440  * rdlock'ed, to ensure that it will be retained in memory.
441  * \since 1.6.1
442  */
443 struct odbc_cache_tables *ast_odbc_find_table(const char *database, const char *tablename)
444 {
445         struct odbc_cache_tables *tableptr;
446         struct odbc_cache_columns *entry;
447         char columnname[80];
448         SQLLEN sqlptr;
449         SQLHSTMT stmt = NULL;
450         int res = 0, error = 0, try = 0;
451         struct odbc_obj *obj = ast_odbc_request_obj(database, 0);
452
453         AST_RWLIST_RDLOCK(&odbc_tables);
454         AST_RWLIST_TRAVERSE(&odbc_tables, tableptr, list) {
455                 if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
456                         break;
457                 }
458         }
459         if (tableptr) {
460                 AST_RWLIST_RDLOCK(&tableptr->columns);
461                 AST_RWLIST_UNLOCK(&odbc_tables);
462                 if (obj) {
463                         ast_odbc_release_obj(obj);
464                 }
465                 return tableptr;
466         }
467
468         if (!obj) {
469                 ast_log(LOG_WARNING, "Unable to retrieve database handle for table description '%s@%s'\n", tablename, database);
470                 AST_RWLIST_UNLOCK(&odbc_tables);
471                 return NULL;
472         }
473
474         /* Table structure not already cached; build it now. */
475         do {
476                 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
477                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
478                         if (try == 0) {
479                                 try = 1;
480                                 ast_odbc_sanity_check(obj);
481                                 continue;
482                         }
483                         ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", database);
484                         break;
485                 }
486
487                 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)tablename, SQL_NTS, (unsigned char *)"%", SQL_NTS);
488                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
489                         if (try == 0) {
490                                 try = 1;
491                                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
492                                 ast_odbc_sanity_check(obj);
493                                 continue;
494                         }
495                         ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.\n", database);
496                         break;
497                 }
498
499                 if (!(tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + strlen(database) + 1 + strlen(tablename) + 1))) {
500                         ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", tablename, database);
501                         break;
502                 }
503
504                 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
505                 tableptr->table = (char *)tableptr + sizeof(*tableptr) + strlen(database) + 1;
506                 strcpy(tableptr->connection, database); /* SAFE */
507                 strcpy(tableptr->table, tablename); /* SAFE */
508                 AST_RWLIST_HEAD_INIT(&(tableptr->columns));
509
510                 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
511                         SQLGetData(stmt,  4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
512
513                         if (!(entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1))) {
514                                 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, tablename, database);
515                                 error = 1;
516                                 break;
517                         }
518                         entry->name = (char *)entry + sizeof(*entry);
519                         strcpy(entry->name, columnname);
520
521                         SQLGetData(stmt,  5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
522                         SQLGetData(stmt,  7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
523                         SQLGetData(stmt,  9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
524                         SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
525                         SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
526                         SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
527
528                         /* Specification states that the octenlen should be the maximum number of bytes
529                          * returned in a char or binary column, but it seems that some drivers just set
530                          * it to NULL. (Bad Postgres! No biscuit!) */
531                         if (entry->octetlen == 0) {
532                                 entry->octetlen = entry->size;
533                         }
534
535                         ast_verb(10, "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);
536                         /* Insert column info into column list */
537                         AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
538                 }
539                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
540
541                 AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
542                 AST_RWLIST_RDLOCK(&(tableptr->columns));
543                 break;
544         } while (1);
545
546         AST_RWLIST_UNLOCK(&odbc_tables);
547
548         if (error) {
549                 destroy_table_cache(tableptr);
550                 tableptr = NULL;
551         }
552         if (obj) {
553                 ast_odbc_release_obj(obj);
554         }
555         return tableptr;
556 }
557
558 struct odbc_cache_columns *ast_odbc_find_column(struct odbc_cache_tables *table, const char *colname)
559 {
560         struct odbc_cache_columns *col;
561         AST_RWLIST_TRAVERSE(&table->columns, col, list) {
562                 if (strcasecmp(col->name, colname) == 0) {
563                         return col;
564                 }
565         }
566         return NULL;
567 }
568
569 int ast_odbc_clear_cache(const char *database, const char *tablename)
570 {
571         struct odbc_cache_tables *tableptr;
572
573         AST_RWLIST_WRLOCK(&odbc_tables);
574         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&odbc_tables, tableptr, list) {
575                 if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
576                         AST_LIST_REMOVE_CURRENT(list);
577                         destroy_table_cache(tableptr);
578                         break;
579                 }
580         }
581         AST_RWLIST_TRAVERSE_SAFE_END
582         AST_RWLIST_UNLOCK(&odbc_tables);
583         return tableptr ? 0 : -1;
584 }
585
586 SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data)
587 {
588         int attempt;
589         SQLHSTMT stmt;
590
591         for (attempt = 0; attempt < 2; attempt++) {
592                 stmt = exec_cb(obj, data);
593
594                 if (stmt) {
595                         break;
596                 } else if (obj->tx) {
597                         ast_log(LOG_WARNING, "Failed to execute, but unable to reconnect, as we're transactional.\n");
598                         break;
599                 } else {
600                         obj->up = 0;
601                         ast_log(LOG_WARNING, "SQL Exec Direct failed.  Attempting a reconnect...\n");
602
603                         odbc_obj_disconnect(obj);
604                         odbc_obj_connect(obj);
605                 }
606         }
607
608         return stmt;
609 }
610
611 SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
612 {
613         int res = 0, i, attempt;
614         SQLINTEGER nativeerror=0, numfields=0;
615         SQLSMALLINT diagbytes=0;
616         unsigned char state[10], diagnostic[256];
617         SQLHSTMT stmt;
618
619         for (attempt = 0; attempt < 2; attempt++) {
620                 /* This prepare callback may do more than just prepare -- it may also
621                  * bind parameters, bind results, etc.  The real key, here, is that
622                  * when we disconnect, all handles become invalid for most databases.
623                  * We must therefore redo everything when we establish a new
624                  * connection. */
625                 stmt = prepare_cb(obj, data);
626
627                 if (stmt) {
628                         res = SQLExecute(stmt);
629                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
630                                 if (res == SQL_ERROR) {
631                                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
632                                         for (i = 0; i < numfields; i++) {
633                                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
634                                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
635                                                 if (i > 10) {
636                                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
637                                                         break;
638                                                 }
639                                         }
640                                 }
641
642                                 if (obj->tx) {
643                                         ast_log(LOG_WARNING, "SQL Execute error, but unable to reconnect, as we're transactional.\n");
644                                         break;
645                                 } else {
646                                         ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
647                                         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
648                                         stmt = NULL;
649
650                                         obj->up = 0;
651                                         /*
652                                          * While this isn't the best way to try to correct an error, this won't automatically
653                                          * fail when the statement handle invalidates.
654                                          */
655                                         ast_odbc_sanity_check(obj);
656                                         continue;
657                                 }
658                         } else {
659                                 obj->last_used = ast_tvnow();
660                         }
661                         break;
662                 } else if (attempt == 0) {
663                         ast_odbc_sanity_check(obj);
664                 }
665         }
666
667         return stmt;
668 }
669
670 int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt)
671 {
672         int res = 0, i;
673         SQLINTEGER nativeerror=0, numfields=0;
674         SQLSMALLINT diagbytes=0;
675         unsigned char state[10], diagnostic[256];
676
677         res = SQLExecute(stmt);
678         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
679                 if (res == SQL_ERROR) {
680                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
681                         for (i = 0; i < numfields; i++) {
682                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
683                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
684                                 if (i > 10) {
685                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
686                                         break;
687                                 }
688                         }
689                 }
690         } else {
691                 obj->last_used = ast_tvnow();
692         }
693
694         return res;
695 }
696
697 SQLRETURN ast_odbc_ast_str_SQLGetData(struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind)
698 {
699         SQLRETURN res;
700
701         if (pmaxlen == 0) {
702                 if (SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) {
703                         ast_str_make_space(buf, *StrLen_or_Ind + 1);
704                 }
705         } else if (pmaxlen > 0) {
706                 ast_str_make_space(buf, pmaxlen);
707         }
708         res = SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), ast_str_size(*buf), StrLen_or_Ind);
709         ast_str_update(*buf);
710
711         return res;
712 }
713
714 int ast_odbc_sanity_check(struct odbc_obj *obj) 
715 {
716         char *test_sql = "select 1";
717         SQLHSTMT stmt;
718         int res = 0;
719
720         if (!ast_strlen_zero(obj->parent->sanitysql))
721                 test_sql = obj->parent->sanitysql;
722
723         if (obj->up) {
724                 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
725                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
726                         obj->up = 0;
727                 } else {
728                         res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
729                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
730                                 obj->up = 0;
731                         } else {
732                                 res = SQLExecute(stmt);
733                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
734                                         obj->up = 0;
735                                 }
736                         }
737                 }
738                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
739         }
740
741         if (!obj->up && !obj->tx) { /* Try to reconnect! */
742                 ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
743                 odbc_obj_disconnect(obj);
744                 odbc_obj_connect(obj);
745         }
746         return obj->up;
747 }
748
749 static int load_odbc_config(void)
750 {
751         static char *cfg = "res_odbc.conf";
752         struct ast_config *config;
753         struct ast_variable *v;
754         char *cat;
755         const char *dsn, *username, *password, *sanitysql;
756         int enabled, pooling, limit, bse, conntimeout, forcecommit, isolation;
757         struct timeval ncache = { 0, 0 };
758         unsigned int idlecheck;
759         int preconnect = 0, res = 0;
760         struct ast_flags config_flags = { 0 };
761
762         struct odbc_class *new;
763
764         config = ast_config_load(cfg, config_flags);
765         if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
766                 ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
767                 return -1;
768         }
769         for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
770                 if (!strcasecmp(cat, "ENV")) {
771                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
772                                 setenv(v->name, v->value, 1);
773                                 ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
774                         }
775                 } else {
776                         /* Reset all to defaults for each class of odbc connections */
777                         dsn = username = password = sanitysql = NULL;
778                         enabled = 1;
779                         preconnect = idlecheck = 0;
780                         pooling = 0;
781                         limit = 0;
782                         bse = 1;
783                         conntimeout = 10;
784                         forcecommit = 0;
785                         isolation = SQL_TXN_READ_COMMITTED;
786                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
787                                 if (!strcasecmp(v->name, "pooling")) {
788                                         if (ast_true(v->value))
789                                                 pooling = 1;
790                                 } else if (!strncasecmp(v->name, "share", 5)) {
791                                         /* "shareconnections" is a little clearer in meaning than "pooling" */
792                                         if (ast_false(v->value))
793                                                 pooling = 1;
794                                 } else if (!strcasecmp(v->name, "limit")) {
795                                         sscanf(v->value, "%30d", &limit);
796                                         if (ast_true(v->value) && !limit) {
797                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat);
798                                                 limit = 1023;
799                                         } else if (ast_false(v->value)) {
800                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
801                                                 enabled = 0;
802                                                 break;
803                                         }
804                                 } else if (!strcasecmp(v->name, "idlecheck")) {
805                                         sscanf(v->value, "%30u", &idlecheck);
806                                 } else if (!strcasecmp(v->name, "enabled")) {
807                                         enabled = ast_true(v->value);
808                                 } else if (!strcasecmp(v->name, "pre-connect")) {
809                                         preconnect = ast_true(v->value);
810                                 } else if (!strcasecmp(v->name, "dsn")) {
811                                         dsn = v->value;
812                                 } else if (!strcasecmp(v->name, "username")) {
813                                         username = v->value;
814                                 } else if (!strcasecmp(v->name, "password")) {
815                                         password = v->value;
816                                 } else if (!strcasecmp(v->name, "sanitysql")) {
817                                         sanitysql = v->value;
818                                 } else if (!strcasecmp(v->name, "backslash_is_escape")) {
819                                         bse = ast_true(v->value);
820                                 } else if (!strcasecmp(v->name, "connect_timeout")) {
821                                         if (sscanf(v->value, "%d", &conntimeout) != 1 || conntimeout < 1) {
822                                                 ast_log(LOG_WARNING, "connect_timeout must be a positive integer\n");
823                                                 conntimeout = 10;
824                                         }
825                                 } else if (!strcasecmp(v->name, "negative_connection_cache")) {
826                                         double dncache;
827                                         if (sscanf(v->value, "%lf", &dncache) != 1 || dncache < 0) {
828                                                 ast_log(LOG_WARNING, "negative_connection_cache must be a non-negative integer\n");
829                                                 /* 5 minutes sounds like a reasonable default */
830                                                 ncache.tv_sec = 300;
831                                                 ncache.tv_usec = 0;
832                                         } else {
833                                                 ncache.tv_sec = (int)dncache;
834                                                 ncache.tv_usec = (dncache - ncache.tv_sec) * 1000000;
835                                         }
836                                 } else if (!strcasecmp(v->name, "forcecommit")) {
837                                         forcecommit = ast_true(v->value);
838                                 } else if (!strcasecmp(v->name, "isolation")) {
839                                         if ((isolation = text2isolation(v->value)) == 0) {
840                                                 ast_log(LOG_ERROR, "Unrecognized value for 'isolation': '%s' in section '%s'\n", v->value, cat);
841                                                 isolation = SQL_TXN_READ_COMMITTED;
842                                         }
843                                 }
844                         }
845
846                         if (enabled && !ast_strlen_zero(dsn)) {
847                                 new = ao2_alloc(sizeof(*new), odbc_class_destructor);
848
849                                 if (!new) {
850                                         res = -1;
851                                         break;
852                                 }
853
854                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
855                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
856
857                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
858                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
859                                         ao2_ref(new, -1);
860                                         return res;
861                                 }
862
863                                 new->obj_container = ao2_container_alloc(1, null_hash_fn, ao2_match_by_addr);
864
865                                 if (pooling) {
866                                         new->haspool = pooling;
867                                         if (limit) {
868                                                 new->limit = limit;
869                                         } else {
870                                                 ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
871                                                 new->limit = 5;
872                                         }
873                                 }
874
875                                 new->backslash_is_escape = bse ? 1 : 0;
876                                 new->forcecommit = forcecommit ? 1 : 0;
877                                 new->isolation = isolation;
878                                 new->idlecheck = idlecheck;
879                                 new->conntimeout = conntimeout;
880                                 new->negative_connection_cache = ncache;
881
882                                 if (cat)
883                                         ast_copy_string(new->name, cat, sizeof(new->name));
884                                 if (dsn)
885                                         ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
886                                 if (username && !(new->username = ast_strdup(username))) {
887                                         ao2_ref(new, -1);
888                                         break;
889                                 }
890                                 if (password && !(new->password = ast_strdup(password))) {
891                                         ao2_ref(new, -1);
892                                         break;
893                                 }
894                                 if (sanitysql && !(new->sanitysql = ast_strdup(sanitysql))) {
895                                         ao2_ref(new, -1);
896                                         break;
897                                 }
898
899                                 odbc_register_class(new, preconnect);
900                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
901                                 ao2_ref(new, -1);
902                                 new = NULL;
903                         }
904                 }
905         }
906         ast_config_destroy(config);
907         return res;
908 }
909
910 static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
911 {
912         struct ao2_iterator aoi = ao2_iterator_init(class_container, 0);
913         struct odbc_class *class;
914         struct odbc_obj *current;
915         int length = 0;
916         int which = 0;
917         char *ret = NULL;
918
919         switch (cmd) {
920         case CLI_INIT:
921                 e->command = "odbc show";
922                 e->usage =
923                                 "Usage: odbc show [class]\n"
924                                 "       List settings of a particular ODBC class or,\n"
925                                 "       if not specified, all classes.\n";
926                 return NULL;
927         case CLI_GENERATE:
928                 if (a->pos != 2)
929                         return NULL;
930                 length = strlen(a->word);
931                 while ((class = ao2_iterator_next(&aoi))) {
932                         if (!strncasecmp(a->word, class->name, length) && ++which > a->n) {
933                                 ret = ast_strdup(class->name);
934                         }
935                         ao2_ref(class, -1);
936                         if (ret) {
937                                 break;
938                         }
939                 }
940                 ao2_iterator_destroy(&aoi);
941                 if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) {
942                         ret = ast_strdup("all");
943                 }
944                 return ret;
945         }
946
947         ast_cli(a->fd, "\nODBC DSN Settings\n");
948         ast_cli(a->fd,   "-----------------\n\n");
949         aoi = ao2_iterator_init(class_container, 0);
950         while ((class = ao2_iterator_next(&aoi))) {
951                 if ((a->argc == 2) || (a->argc == 3 && !strcmp(a->argv[2], "all")) || (!strcmp(a->argv[2], class->name))) {
952                         int count = 0;
953                         char timestr[80];
954                         struct ast_tm tm;
955
956                         ast_localtime(&class->last_negative_connect, &tm, NULL);
957                         ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %T", &tm);
958                         ast_cli(a->fd, "  Name:   %s\n  DSN:    %s\n", class->name, class->dsn);
959                         ast_cli(a->fd, "    Last connection attempt: %s\n", timestr);
960
961                         if (class->haspool) {
962                                 struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
963
964                                 ast_cli(a->fd, "  Pooled: Yes\n  Limit:  %d\n  Connections in use: %d\n", class->limit, class->count);
965
966                                 while ((current = ao2_iterator_next(&aoi2))) {
967                                         ast_mutex_lock(&current->lock);
968 #ifdef DEBUG_THREADS
969                                         ast_cli(a->fd, "    - Connection %d: %s (%s:%d %s)\n", ++count,
970                                                 current->used ? "in use" :
971                                                 current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected",
972                                                 current->file, current->lineno, current->function);
973 #else
974                                         ast_cli(a->fd, "    - Connection %d: %s\n", ++count,
975                                                 current->used ? "in use" :
976                                                 current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
977 #endif
978                                         ast_mutex_unlock(&current->lock);
979                                         ao2_ref(current, -1);
980                                 }
981                                 ao2_iterator_destroy(&aoi2);
982                         } else {
983                                 /* Should only ever be one of these (unless there are transactions) */
984                                 struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
985                                 while ((current = ao2_iterator_next(&aoi2))) {
986                                         ast_cli(a->fd, "  Pooled: No\n  Connected: %s\n", current->used ? "In use" :
987                                                 current->up && ast_odbc_sanity_check(current) ? "Yes" : "No");
988                                         ao2_ref(current, -1);
989                                 }
990                                 ao2_iterator_destroy(&aoi2);
991                         }
992                         ast_cli(a->fd, "\n");
993                 }
994                 ao2_ref(class, -1);
995         }
996         ao2_iterator_destroy(&aoi);
997
998         return CLI_SUCCESS;
999 }
1000
1001 static struct ast_cli_entry cli_odbc[] = {
1002         AST_CLI_DEFINE(handle_cli_odbc_show, "List ODBC DSN(s)")
1003 };
1004
1005 static int odbc_register_class(struct odbc_class *class, int preconnect)
1006 {
1007         struct odbc_obj *obj;
1008         if (class) {
1009                 ao2_link(class_container, class);
1010                 /* I still have a reference in the caller, so a deref is NOT missing here. */
1011
1012                 if (preconnect) {
1013                         /* Request and release builds a connection */
1014                         obj = ast_odbc_request_obj(class->name, 0);
1015                         if (obj) {
1016                                 ast_odbc_release_obj(obj);
1017                         }
1018                 }
1019
1020                 return 0;
1021         } else {
1022                 ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
1023                 return -1;
1024         }
1025 }
1026
1027 static void odbc_release_obj2(struct odbc_obj *obj, struct odbc_txn_frame *tx)
1028 {
1029         SQLINTEGER nativeerror=0, numfields=0;
1030         SQLSMALLINT diagbytes=0, i;
1031         unsigned char state[10], diagnostic[256];
1032
1033         ast_debug(2, "odbc_release_obj2(%p) called (obj->txf = %p)\n", obj, obj->txf);
1034         if (tx) {
1035                 ast_debug(1, "called on a transactional handle with %s\n", tx->forcecommit ? "COMMIT" : "ROLLBACK");
1036                 if (SQLEndTran(SQL_HANDLE_DBC, obj->con, tx->forcecommit ? SQL_COMMIT : SQL_ROLLBACK) == SQL_ERROR) {
1037                         /* Handle possible transaction commit failure */
1038                         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1039                         for (i = 0; i < numfields; i++) {
1040                                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1041                                 ast_log(LOG_WARNING, "SQLEndTran returned an error: %s: %s\n", state, diagnostic);
1042                                 if (!strcmp((char *)state, "25S02") || !strcmp((char *)state, "08007")) {
1043                                         /* These codes mean that a commit failed and a transaction
1044                                          * is still active. We must rollback, or things will get
1045                                          * very, very weird for anybody using the handle next. */
1046                                         SQLEndTran(SQL_HANDLE_DBC, obj->con, SQL_ROLLBACK);
1047                                 }
1048                                 if (i > 10) {
1049                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1050                                         break;
1051                                 }
1052                         }
1053                 }
1054
1055                 /* Transaction is done, reset autocommit */
1056                 if (SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_ON, 0) == SQL_ERROR) {
1057                         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1058                         for (i = 0; i < numfields; i++) {
1059                                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1060                                 ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1061                                 if (i > 10) {
1062                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1063                                         break;
1064                                 }
1065                         }
1066                 }
1067         }
1068
1069 #ifdef DEBUG_THREADS
1070         obj->file[0] = '\0';
1071         obj->function[0] = '\0';
1072         obj->lineno = 0;
1073 #endif
1074
1075         /* For pooled connections, this frees the connection to be
1076          * reused.  For non-pooled connections, it does nothing. */
1077         obj->used = 0;
1078         if (obj->txf) {
1079                 /* Prevent recursion -- transaction is already closed out. */
1080                 obj->txf->obj = NULL;
1081                 obj->txf = release_transaction(obj->txf);
1082         }
1083         ao2_ref(obj, -1);
1084 }
1085
1086 void ast_odbc_release_obj(struct odbc_obj *obj)
1087 {
1088         struct odbc_txn_frame *tx = find_transaction(NULL, obj, NULL, 0);
1089         odbc_release_obj2(obj, tx);
1090 }
1091
1092 int ast_odbc_backslash_is_escape(struct odbc_obj *obj)
1093 {
1094         return obj->parent->backslash_is_escape;
1095 }
1096
1097 static int commit_exec(struct ast_channel *chan, const char *data)
1098 {
1099         struct odbc_txn_frame *tx;
1100         SQLINTEGER nativeerror=0, numfields=0;
1101         SQLSMALLINT diagbytes=0, i;
1102         unsigned char state[10], diagnostic[256];
1103
1104         if (ast_strlen_zero(data)) {
1105                 tx = find_transaction(chan, NULL, NULL, 1);
1106         } else {
1107                 tx = find_transaction(chan, NULL, data, 0);
1108         }
1109
1110         pbx_builtin_setvar_helper(chan, "COMMIT_RESULT", "OK");
1111
1112         if (tx) {
1113                 if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, SQL_COMMIT) == SQL_ERROR) {
1114                         struct ast_str *errors = ast_str_thread_get(&errors_buf, 16);
1115                         ast_str_reset(errors);
1116
1117                         /* Handle possible transaction commit failure */
1118                         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1119                         for (i = 0; i < numfields; i++) {
1120                                 SQLGetDiagRec(SQL_HANDLE_DBC, tx->obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1121                                 ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state);
1122                                 ast_log(LOG_WARNING, "SQLEndTran returned an error: %s: %s\n", state, diagnostic);
1123                                 if (i > 10) {
1124                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1125                                         break;
1126                                 }
1127                         }
1128                         pbx_builtin_setvar_helper(chan, "COMMIT_RESULT", ast_str_buffer(errors));
1129                 }
1130         }
1131         return 0;
1132 }
1133
1134 static int rollback_exec(struct ast_channel *chan, const char *data)
1135 {
1136         struct odbc_txn_frame *tx;
1137         SQLINTEGER nativeerror=0, numfields=0;
1138         SQLSMALLINT diagbytes=0, i;
1139         unsigned char state[10], diagnostic[256];
1140
1141         if (ast_strlen_zero(data)) {
1142                 tx = find_transaction(chan, NULL, NULL, 1);
1143         } else {
1144                 tx = find_transaction(chan, NULL, data, 0);
1145         }
1146
1147         pbx_builtin_setvar_helper(chan, "ROLLBACK_RESULT", "OK");
1148
1149         if (tx) {
1150                 if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, SQL_ROLLBACK) == SQL_ERROR) {
1151                         struct ast_str *errors = ast_str_thread_get(&errors_buf, 16);
1152                         ast_str_reset(errors);
1153
1154                         /* Handle possible transaction commit failure */
1155                         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1156                         for (i = 0; i < numfields; i++) {
1157                                 SQLGetDiagRec(SQL_HANDLE_DBC, tx->obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1158                                 ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state);
1159                                 ast_log(LOG_WARNING, "SQLEndTran returned an error: %s: %s\n", state, diagnostic);
1160                                 if (i > 10) {
1161                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1162                                         break;
1163                                 }
1164                         }
1165                         pbx_builtin_setvar_helper(chan, "ROLLBACK_RESULT", ast_str_buffer(errors));
1166                 }
1167         }
1168         return 0;
1169 }
1170
1171 static int aoro2_class_cb(void *obj, void *arg, int flags)
1172 {
1173         struct odbc_class *class = obj;
1174         char *name = arg;
1175         if (!strcmp(class->name, name) && !class->delme) {
1176                 return CMP_MATCH | CMP_STOP;
1177         }
1178         return 0;
1179 }
1180
1181 #define USE_TX (void *)(long)1
1182 #define NO_TX  (void *)(long)2
1183 #define EOR_TX (void *)(long)3
1184
1185 static int aoro2_obj_cb(void *vobj, void *arg, int flags)
1186 {
1187         struct odbc_obj *obj = vobj;
1188         ast_mutex_lock(&obj->lock);
1189         if ((arg == NO_TX && !obj->tx) || (arg == EOR_TX && !obj->used) || (arg == USE_TX && obj->tx && !obj->used)) {
1190                 obj->used = 1;
1191                 ast_mutex_unlock(&obj->lock);
1192                 return CMP_MATCH | CMP_STOP;
1193         }
1194         ast_mutex_unlock(&obj->lock);
1195         return 0;
1196 }
1197
1198 struct odbc_obj *_ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno)
1199 {
1200         struct odbc_obj *obj = NULL;
1201         struct odbc_class *class;
1202         SQLINTEGER nativeerror=0, numfields=0;
1203         SQLSMALLINT diagbytes=0, i;
1204         unsigned char state[10], diagnostic[256];
1205
1206         if (!(class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name))) {
1207                 ast_debug(1, "Class not found!\n");
1208                 return NULL;
1209         }
1210
1211         ast_assert(ao2_ref(class, 0) > 1);
1212
1213         if (class->haspool) {
1214                 /* Recycle connections before building another */
1215                 obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, EOR_TX);
1216
1217                 if (obj) {
1218                         ast_assert(ao2_ref(obj, 0) > 1);
1219                 }
1220                 if (!obj && (class->count < class->limit) &&
1221                                 (time(NULL) > class->last_negative_connect.tv_sec + class->negative_connection_cache.tv_sec)) {
1222                         obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
1223                         if (!obj) {
1224                                 class->count--;
1225                                 ao2_ref(class, -1);
1226                                 ast_debug(3, "Unable to allocate object\n");
1227                                 return NULL;
1228                         }
1229                         ast_assert(ao2_ref(obj, 0) == 1);
1230                         ast_mutex_init(&obj->lock);
1231                         /* obj inherits the outstanding reference to class */
1232                         obj->parent = class;
1233                         class = NULL;
1234                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
1235                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1236                                 ao2_ref(obj, -1);
1237                                 ast_assert(ao2_ref(class, 0) > 0);
1238                                 obj = NULL;
1239                         } else {
1240                                 obj->used = 1;
1241                                 ao2_link(obj->parent->obj_container, obj);
1242                                 ast_atomic_fetchadd_int(&obj->parent->count, +1);
1243                         }
1244                 } else {
1245                         /* Object is not constructed, so delete outstanding reference to class. */
1246                         ao2_ref(class, -1);
1247                         class = NULL;
1248                 }
1249
1250                 if (obj && ast_test_flag(&flags, RES_ODBC_INDEPENDENT_CONNECTION)) {
1251                         /* Ensure this connection has autocommit turned off. */
1252                         if (SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) {
1253                                 SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1254                                 for (i = 0; i < numfields; i++) {
1255                                         SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1256                                         ast_log(LOG_WARNING, "SQLSetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1257                                         if (i > 10) {
1258                                                 ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1259                                                 break;
1260                                         }
1261                                 }
1262                         }
1263                 }
1264         } else if (ast_test_flag(&flags, RES_ODBC_INDEPENDENT_CONNECTION)) {
1265                 /* Non-pooled connections -- but must use a separate connection handle */
1266                 if (!(obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, USE_TX))) {
1267                         ast_debug(1, "Object not found\n");
1268                         obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
1269                         if (!obj) {
1270                                 ao2_ref(class, -1);
1271                                 ast_debug(3, "Unable to allocate object\n");
1272                                 return NULL;
1273                         }
1274                         ast_mutex_init(&obj->lock);
1275                         /* obj inherits the outstanding reference to class */
1276                         obj->parent = class;
1277                         class = NULL;
1278                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
1279                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1280                                 ao2_ref(obj, -1);
1281                                 obj = NULL;
1282                         } else {
1283                                 obj->used = 1;
1284                                 ao2_link(obj->parent->obj_container, obj);
1285                                 ast_atomic_fetchadd_int(&obj->parent->count, +1);
1286                         }
1287                 }
1288
1289                 if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) {
1290                         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1291                         for (i = 0; i < numfields; i++) {
1292                                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1293                                 ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1294                                 if (i > 10) {
1295                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1296                                         break;
1297                                 }
1298                         }
1299                 }
1300         } else {
1301                 /* Non-pooled connection: multiple modules can use the same connection. */
1302                 if ((obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, NO_TX))) {
1303                         /* Object is not constructed, so delete outstanding reference to class. */
1304                         ast_assert(ao2_ref(class, 0) > 1);
1305                         ao2_ref(class, -1);
1306                         class = NULL;
1307                 } else {
1308                         /* No entry: build one */
1309                         if (!(obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor))) {
1310                                 ast_assert(ao2_ref(class, 0) > 1);
1311                                 ao2_ref(class, -1);
1312                                 ast_debug(3, "Unable to allocate object\n");
1313                                 return NULL;
1314                         }
1315                         ast_mutex_init(&obj->lock);
1316                         /* obj inherits the outstanding reference to class */
1317                         obj->parent = class;
1318                         class = NULL;
1319                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
1320                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1321                                 ao2_ref(obj, -1);
1322                                 obj = NULL;
1323                         } else {
1324                                 ao2_link(obj->parent->obj_container, obj);
1325                                 ast_assert(ao2_ref(obj, 0) > 1);
1326                         }
1327                 }
1328
1329                 if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_ON, 0) == SQL_ERROR) {
1330                         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1331                         for (i = 0; i < numfields; i++) {
1332                                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1333                                 ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1334                                 if (i > 10) {
1335                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1336                                         break;
1337                                 }
1338                         }
1339                 }
1340         }
1341
1342         /* Set the isolation property */
1343         if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)obj->parent->isolation, 0) == SQL_ERROR) {
1344                 SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1345                 for (i = 0; i < numfields; i++) {
1346                         SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1347                         ast_log(LOG_WARNING, "SetConnectAttr (Txn isolation) returned an error: %s: %s\n", state, diagnostic);
1348                         if (i > 10) {
1349                                 ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1350                                 break;
1351                         }
1352                 }
1353         }
1354
1355         if (obj && ast_test_flag(&flags, RES_ODBC_CONNECTED) && !obj->up) {
1356                 /* Check if this connection qualifies for reconnection, with negative connection cache time */
1357                 if (time(NULL) > class->last_negative_connect.tv_sec + class->negative_connection_cache.tv_sec) {
1358                         odbc_obj_connect(obj);
1359                 }
1360         } else if (obj && ast_test_flag(&flags, RES_ODBC_SANITY_CHECK)) {
1361                 ast_odbc_sanity_check(obj);
1362         } else if (obj && obj->parent->idlecheck > 0 && ast_tvdiff_sec(ast_tvnow(), obj->last_used) > obj->parent->idlecheck) {
1363                 odbc_obj_connect(obj);
1364         }
1365
1366 #ifdef DEBUG_THREADS
1367         if (obj) {
1368                 ast_copy_string(obj->file, file, sizeof(obj->file));
1369                 ast_copy_string(obj->function, function, sizeof(obj->function));
1370                 obj->lineno = lineno;
1371         }
1372 #endif
1373         ast_assert(class == NULL);
1374
1375         if (obj) {
1376                 ast_assert(ao2_ref(obj, 0) > 1);
1377         }
1378         return obj;
1379 }
1380
1381 struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno)
1382 {
1383         struct ast_flags flags = { check ? RES_ODBC_SANITY_CHECK : 0 };
1384         return _ast_odbc_request_obj2(name, flags, file, function, lineno);
1385 }
1386
1387 struct odbc_obj *ast_odbc_retrieve_transaction_obj(struct ast_channel *chan, const char *objname)
1388 {
1389         struct ast_datastore *txn_store;
1390         AST_LIST_HEAD(, odbc_txn_frame) *oldlist;
1391         struct odbc_txn_frame *txn = NULL;
1392
1393         if (!chan) {
1394                 /* No channel == no transaction */
1395                 return NULL;
1396         }
1397
1398         ast_channel_lock(chan);
1399         if ((txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) {
1400                 oldlist = txn_store->data;
1401         } else {
1402                 ast_channel_unlock(chan);
1403                 return NULL;
1404         }
1405
1406         AST_LIST_LOCK(oldlist);
1407         ast_channel_unlock(chan);
1408
1409         AST_LIST_TRAVERSE(oldlist, txn, list) {
1410                 if (txn->obj && txn->obj->parent && !strcmp(txn->obj->parent->name, objname)) {
1411                         AST_LIST_UNLOCK(oldlist);
1412                         return txn->obj;
1413                 }
1414         }
1415         AST_LIST_UNLOCK(oldlist);
1416         return NULL;
1417 }
1418
1419 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
1420 {
1421         int res;
1422         SQLINTEGER err;
1423         short int mlen;
1424         unsigned char msg[200], state[10];
1425
1426         /* Nothing to disconnect */
1427         if (!obj->con) {
1428                 return ODBC_SUCCESS;
1429         }
1430
1431         ast_mutex_lock(&obj->lock);
1432
1433         res = SQLDisconnect(obj->con);
1434
1435         if (obj->parent) {
1436                 if (res == SQL_SUCCESS || res == SQL_SUCCESS_WITH_INFO) {
1437                         ast_log(LOG_DEBUG, "Disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
1438                 } else {
1439                         ast_log(LOG_DEBUG, "res_odbc: %s [%s] already disconnected\n", obj->parent->name, obj->parent->dsn);
1440                 }
1441         }
1442
1443         if ((res = SQLFreeHandle(SQL_HANDLE_DBC, obj->con) == SQL_SUCCESS)) {
1444                 obj->con = NULL;
1445                 ast_log(LOG_DEBUG, "Database handle deallocated\n");
1446         } else {
1447                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, state, &err, msg, 100, &mlen);
1448                 ast_log(LOG_WARNING, "Unable to deallocate database handle? %d errno=%d %s\n", res, (int)err, msg);
1449         }
1450
1451         obj->up = 0;
1452         ast_mutex_unlock(&obj->lock);
1453         return ODBC_SUCCESS;
1454 }
1455
1456 static odbc_status odbc_obj_connect(struct odbc_obj *obj)
1457 {
1458         int res;
1459         SQLINTEGER err;
1460         short int mlen;
1461         unsigned char msg[200], state[10];
1462 #ifdef NEEDTRACE
1463         SQLINTEGER enable = 1;
1464         char *tracefile = "/tmp/odbc.trace";
1465 #endif
1466         ast_mutex_lock(&obj->lock);
1467
1468         if (obj->up) {
1469                 odbc_obj_disconnect(obj);
1470                 ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
1471         } else {
1472                 ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
1473         }
1474
1475         res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);
1476
1477         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
1478                 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
1479                 obj->parent->last_negative_connect = ast_tvnow();
1480                 ast_mutex_unlock(&obj->lock);
1481                 return ODBC_FAIL;
1482         }
1483         SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)(long) obj->parent->conntimeout, 0);
1484         SQLSetConnectAttr(obj->con, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER *)(long) obj->parent->conntimeout, 0);
1485 #ifdef NEEDTRACE
1486         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
1487         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
1488 #endif
1489
1490         res = SQLConnect(obj->con,
1491                    (SQLCHAR *) obj->parent->dsn, SQL_NTS,
1492                    (SQLCHAR *) obj->parent->username, SQL_NTS,
1493                    (SQLCHAR *) obj->parent->password, SQL_NTS);
1494
1495         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
1496                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, state, &err, msg, 100, &mlen);
1497                 obj->parent->last_negative_connect = ast_tvnow();
1498                 ast_mutex_unlock(&obj->lock);
1499                 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
1500                 return ODBC_FAIL;
1501         } else {
1502                 ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
1503                 obj->up = 1;
1504                 obj->last_used = ast_tvnow();
1505         }
1506
1507         ast_mutex_unlock(&obj->lock);
1508         return ODBC_SUCCESS;
1509 }
1510
1511 static int acf_transaction_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
1512 {
1513         AST_DECLARE_APP_ARGS(args,
1514                 AST_APP_ARG(property);
1515                 AST_APP_ARG(opt);
1516         );
1517         struct odbc_txn_frame *tx;
1518
1519         AST_STANDARD_APP_ARGS(args, data);
1520         if (strcasecmp(args.property, "transaction") == 0) {
1521                 if ((tx = find_transaction(chan, NULL, NULL, 1))) {
1522                         ast_copy_string(buf, tx->name, len);
1523                         return 0;
1524                 }
1525         } else if (strcasecmp(args.property, "isolation") == 0) {
1526                 if (!ast_strlen_zero(args.opt)) {
1527                         tx = find_transaction(chan, NULL, args.opt, 0);
1528                 } else {
1529                         tx = find_transaction(chan, NULL, NULL, 1);
1530                 }
1531                 if (tx) {
1532                         ast_copy_string(buf, isolation2text(tx->isolation), len);
1533                         return 0;
1534                 }
1535         } else if (strcasecmp(args.property, "forcecommit") == 0) {
1536                 if (!ast_strlen_zero(args.opt)) {
1537                         tx = find_transaction(chan, NULL, args.opt, 0);
1538                 } else {
1539                         tx = find_transaction(chan, NULL, NULL, 1);
1540                 }
1541                 if (tx) {
1542                         ast_copy_string(buf, tx->forcecommit ? "1" : "0", len);
1543                         return 0;
1544                 }
1545         }
1546         return -1;
1547 }
1548
1549 static int acf_transaction_write(struct ast_channel *chan, const char *cmd, char *s, const char *value)
1550 {
1551         AST_DECLARE_APP_ARGS(args,
1552                 AST_APP_ARG(property);
1553                 AST_APP_ARG(opt);
1554         );
1555         struct odbc_txn_frame *tx;
1556         SQLINTEGER nativeerror=0, numfields=0;
1557         SQLSMALLINT diagbytes=0, i;
1558         unsigned char state[10], diagnostic[256];
1559
1560         AST_STANDARD_APP_ARGS(args, s);
1561         if (strcasecmp(args.property, "transaction") == 0) {
1562                 /* Set active transaction */
1563                 struct odbc_obj *obj;
1564                 if ((tx = find_transaction(chan, NULL, value, 0))) {
1565                         mark_transaction_active(chan, tx);
1566                 } else {
1567                         /* No such transaction, create one */
1568                         struct ast_flags flags = { RES_ODBC_INDEPENDENT_CONNECTION };
1569                         if (ast_strlen_zero(args.opt) || !(obj = ast_odbc_request_obj2(args.opt, flags))) {
1570                                 ast_log(LOG_ERROR, "Could not create transaction: invalid database specification '%s'\n", S_OR(args.opt, ""));
1571                                 pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_DB");
1572                                 return -1;
1573                         }
1574                         if (!(tx = find_transaction(chan, obj, value, 0))) {
1575                                 pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
1576                                 return -1;
1577                         }
1578                         obj->tx = 1;
1579                 }
1580                 pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
1581                 return 0;
1582         } else if (strcasecmp(args.property, "forcecommit") == 0) {
1583                 /* Set what happens when an uncommitted transaction ends without explicit Commit or Rollback */
1584                 if (ast_strlen_zero(args.opt)) {
1585                         tx = find_transaction(chan, NULL, NULL, 1);
1586                 } else {
1587                         tx = find_transaction(chan, NULL, args.opt, 0);
1588                 }
1589                 if (!tx) {
1590                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
1591                         return -1;
1592                 }
1593                 if (ast_true(value)) {
1594                         tx->forcecommit = 1;
1595                 } else if (ast_false(value)) {
1596                         tx->forcecommit = 0;
1597                 } else {
1598                         ast_log(LOG_ERROR, "Invalid value for forcecommit: '%s'\n", S_OR(value, ""));
1599                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_VALUE");
1600                         return -1;
1601                 }
1602
1603                 pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
1604                 return 0;
1605         } else if (strcasecmp(args.property, "isolation") == 0) {
1606                 /* How do uncommitted transactions affect reads? */
1607                 int isolation = text2isolation(value);
1608                 if (ast_strlen_zero(args.opt)) {
1609                         tx = find_transaction(chan, NULL, NULL, 1);
1610                 } else {
1611                         tx = find_transaction(chan, NULL, args.opt, 0);
1612                 }
1613                 if (!tx) {
1614                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
1615                         return -1;
1616                 }
1617                 if (isolation == 0) {
1618                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_VALUE");
1619                         ast_log(LOG_ERROR, "Invalid isolation specification: '%s'\n", S_OR(value, ""));
1620                 } else if (SQLSetConnectAttr(tx->obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)isolation, 0) == SQL_ERROR) {
1621                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "SQL_ERROR");
1622                         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1623                         for (i = 0; i < numfields; i++) {
1624                                 SQLGetDiagRec(SQL_HANDLE_DBC, tx->obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1625                                 ast_log(LOG_WARNING, "SetConnectAttr (Txn isolation) returned an error: %s: %s\n", state, diagnostic);
1626                                 if (i > 10) {
1627                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1628                                         break;
1629                                 }
1630                         }
1631                 } else {
1632                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
1633                         tx->isolation = isolation;
1634                 }
1635                 return 0;
1636         } else {
1637                 ast_log(LOG_ERROR, "Unknown property: '%s'\n", args.property);
1638                 return -1;
1639         }
1640 }
1641
1642 static struct ast_custom_function odbc_function = {
1643         .name = "ODBC",
1644         .read = acf_transaction_read,
1645         .write = acf_transaction_write,
1646 };
1647
1648 static const char * const app_commit = "ODBC_Commit";
1649 static const char * const app_rollback = "ODBC_Rollback";
1650
1651 /*!
1652  * \internal
1653  * \brief Implements the channels provider.
1654  */
1655 static int data_odbc_provider_handler(const struct ast_data_search *search,
1656                 struct ast_data *root)
1657 {
1658         struct ao2_iterator aoi, aoi2;
1659         struct odbc_class *class;
1660         struct odbc_obj *current;
1661         struct ast_data *data_odbc_class, *data_odbc_connections, *data_odbc_connection;
1662         struct ast_data *enum_node;
1663         int count;
1664
1665         aoi = ao2_iterator_init(class_container, 0);
1666         while ((class = ao2_iterator_next(&aoi))) {
1667                 data_odbc_class = ast_data_add_node(root, "class");
1668                 if (!data_odbc_class) {
1669                         ao2_ref(class, -1);
1670                         continue;
1671                 }
1672
1673                 ast_data_add_structure(odbc_class, data_odbc_class, class);
1674
1675                 if (!ao2_container_count(class->obj_container)) {
1676                         ao2_ref(class, -1);
1677                         continue;
1678                 }
1679
1680                 data_odbc_connections = ast_data_add_node(data_odbc_class, "connections");
1681                 if (!data_odbc_connections) {
1682                         ao2_ref(class, -1);
1683                         continue;
1684                 }
1685
1686                 ast_data_add_bool(data_odbc_class, "shared", !class->haspool);
1687                 /* isolation */
1688                 enum_node = ast_data_add_node(data_odbc_class, "isolation");
1689                 if (!enum_node) {
1690                         ao2_ref(class, -1);
1691                         continue;
1692                 }
1693                 ast_data_add_int(enum_node, "value", class->isolation);
1694                 ast_data_add_str(enum_node, "text", isolation2text(class->isolation));
1695
1696                 count = 0;
1697                 aoi2 = ao2_iterator_init(class->obj_container, 0);
1698                 while ((current = ao2_iterator_next(&aoi2))) {
1699                         data_odbc_connection = ast_data_add_node(data_odbc_connections, "connection");
1700                         if (!data_odbc_connection) {
1701                                 ao2_ref(current, -1);
1702                                 continue;
1703                         }
1704
1705                         ast_mutex_lock(&current->lock);
1706                         ast_data_add_str(data_odbc_connection, "status", current->used ? "in use" :
1707                                         current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
1708                         ast_data_add_bool(data_odbc_connection, "transactional", current->tx);
1709                         ast_mutex_unlock(&current->lock);
1710
1711                         if (class->haspool) {
1712                                 ast_data_add_int(data_odbc_connection, "number", ++count);
1713                         }
1714
1715                         ao2_ref(current, -1);
1716                 }
1717                 ao2_ref(class, -1);
1718
1719                 if (!ast_data_search_match(search, data_odbc_class)) {
1720                         ast_data_remove_node(root, data_odbc_class);
1721                 }
1722         }
1723         return 0;
1724 }
1725
1726 /*!
1727  * \internal
1728  * \brief /asterisk/res/odbc/listprovider.
1729  */
1730 static const struct ast_data_handler odbc_provider = {
1731         .version = AST_DATA_HANDLER_VERSION,
1732         .get = data_odbc_provider_handler
1733 };
1734
1735 static const struct ast_data_entry odbc_providers[] = {
1736         AST_DATA_ENTRY("/asterisk/res/odbc", &odbc_provider),
1737 };
1738
1739 static int reload(void)
1740 {
1741         struct odbc_cache_tables *table;
1742         struct odbc_class *class;
1743         struct odbc_obj *current;
1744         struct ao2_iterator aoi = ao2_iterator_init(class_container, 0);
1745
1746         /* First, mark all to be purged */
1747         while ((class = ao2_iterator_next(&aoi))) {
1748                 class->delme = 1;
1749                 ao2_ref(class, -1);
1750         }
1751         ao2_iterator_destroy(&aoi);
1752
1753         load_odbc_config();
1754
1755         /* Purge remaining classes */
1756
1757         /* Note on how this works; this is a case of circular references, so we
1758          * explicitly do NOT want to use a callback here (or we wind up in
1759          * recursive hell).
1760          *
1761          * 1. Iterate through all the classes.  Note that the classes will currently
1762          * contain two classes of the same name, one of which is marked delme and
1763          * will be purged when all remaining objects of the class are released, and
1764          * the other, which was created above when we re-parsed the config file.
1765          * 2. On each class, there is a reference held by the master container and
1766          * a reference held by each connection object.  There are two cases for
1767          * destruction of the class, noted below.  However, in all cases, all O-refs
1768          * (references to objects) will first be freed, which will cause the C-refs
1769          * (references to classes) to be decremented (but never to 0, because the
1770          * class container still has a reference).
1771          *    a) If the class has outstanding objects, the C-ref by the class
1772          *    container will then be freed, which leaves only C-refs by any
1773          *    outstanding objects.  When the final outstanding object is released
1774          *    (O-refs held by applications and dialplan functions), it will in turn
1775          *    free the final C-ref, causing class destruction.
1776          *    b) If the class has no outstanding objects, when the class container
1777          *    removes the final C-ref, the class will be destroyed.
1778          */
1779         aoi = ao2_iterator_init(class_container, 0);
1780         while ((class = ao2_iterator_next(&aoi))) { /* C-ref++ (by iterator) */
1781                 if (class->delme) {
1782                         struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
1783                         while ((current = ao2_iterator_next(&aoi2))) { /* O-ref++ (by iterator) */
1784                                 ao2_unlink(class->obj_container, current); /* unlink O-ref from class (reference handled implicitly) */
1785                                 ao2_ref(current, -1); /* O-ref-- (by iterator) */
1786                                 /* At this point, either
1787                                  * a) there's an outstanding O-ref, or
1788                                  * b) the object has already been destroyed.
1789                                  */
1790                         }
1791                         ao2_iterator_destroy(&aoi2);
1792                         ao2_unlink(class_container, class); /* unlink C-ref from container (reference handled implicitly) */
1793                         /* At this point, either
1794                          * a) there's an outstanding O-ref, which holds an outstanding C-ref, or
1795                          * b) the last remaining C-ref is held by the iterator, which will be
1796                          * destroyed in the next step.
1797                          */
1798                 }
1799                 ao2_ref(class, -1); /* C-ref-- (by iterator) */
1800         }
1801         ao2_iterator_destroy(&aoi);
1802
1803         /* Empty the cache; it will get rebuilt the next time the tables are needed. */
1804         AST_RWLIST_WRLOCK(&odbc_tables);
1805         while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
1806                 destroy_table_cache(table);
1807         }
1808         AST_RWLIST_UNLOCK(&odbc_tables);
1809
1810         return 0;
1811 }
1812
1813 static int unload_module(void)
1814 {
1815         /* Prohibit unloading */
1816         return -1;
1817 }
1818
1819 static int load_module(void)
1820 {
1821         if (!(class_container = ao2_container_alloc(1, null_hash_fn, ao2_match_by_addr)))
1822                 return AST_MODULE_LOAD_DECLINE;
1823         if (load_odbc_config() == -1)
1824                 return AST_MODULE_LOAD_DECLINE;
1825         ast_cli_register_multiple(cli_odbc, ARRAY_LEN(cli_odbc));
1826         ast_data_register_multiple(odbc_providers, ARRAY_LEN(odbc_providers));
1827         ast_register_application_xml(app_commit, commit_exec);
1828         ast_register_application_xml(app_rollback, rollback_exec);
1829         ast_custom_function_register(&odbc_function);
1830         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
1831         return 0;
1832 }
1833
1834 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "ODBC resource",
1835                 .load = load_module,
1836                 .unload = unload_module,
1837                 .reload = reload,
1838                 .load_pri = AST_MODPRI_REALTIME_DEPEND,
1839                );