Merge the realtime failover branch
[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                         forcecommit = 0;
784                         isolation = SQL_TXN_READ_COMMITTED;
785                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
786                                 if (!strcasecmp(v->name, "pooling")) {
787                                         if (ast_true(v->value))
788                                                 pooling = 1;
789                                 } else if (!strncasecmp(v->name, "share", 5)) {
790                                         /* "shareconnections" is a little clearer in meaning than "pooling" */
791                                         if (ast_false(v->value))
792                                                 pooling = 1;
793                                 } else if (!strcasecmp(v->name, "limit")) {
794                                         sscanf(v->value, "%30d", &limit);
795                                         if (ast_true(v->value) && !limit) {
796                                                 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);
797                                                 limit = 1023;
798                                         } else if (ast_false(v->value)) {
799                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
800                                                 enabled = 0;
801                                                 break;
802                                         }
803                                 } else if (!strcasecmp(v->name, "idlecheck")) {
804                                         sscanf(v->value, "%30u", &idlecheck);
805                                 } else if (!strcasecmp(v->name, "enabled")) {
806                                         enabled = ast_true(v->value);
807                                 } else if (!strcasecmp(v->name, "pre-connect")) {
808                                         preconnect = ast_true(v->value);
809                                 } else if (!strcasecmp(v->name, "dsn")) {
810                                         dsn = v->value;
811                                 } else if (!strcasecmp(v->name, "username")) {
812                                         username = v->value;
813                                 } else if (!strcasecmp(v->name, "password")) {
814                                         password = v->value;
815                                 } else if (!strcasecmp(v->name, "sanitysql")) {
816                                         sanitysql = v->value;
817                                 } else if (!strcasecmp(v->name, "backslash_is_escape")) {
818                                         bse = ast_true(v->value);
819                                 } else if (!strcasecmp(v->name, "connect_timeout")) {
820                                         if (sscanf(v->value, "%d", &conntimeout) != 1 || conntimeout < 1) {
821                                                 ast_log(LOG_WARNING, "connect_timeout must be a positive integer\n");
822                                                 conntimeout = 10;
823                                         }
824                                 } else if (!strcasecmp(v->name, "negative_connection_cache")) {
825                                         double dncache;
826                                         if (sscanf(v->value, "%lf", &dncache) != 1 || dncache < 0) {
827                                                 ast_log(LOG_WARNING, "negative_connection_cache must be a non-negative integer\n");
828                                                 /* 5 minutes sounds like a reasonable default */
829                                                 ncache.tv_sec = 300;
830                                                 ncache.tv_usec = 0;
831                                         } else {
832                                                 ncache.tv_sec = (int)dncache;
833                                                 ncache.tv_usec = (dncache - ncache.tv_sec) * 1000000;
834                                         }
835                                 } else if (!strcasecmp(v->name, "forcecommit")) {
836                                         forcecommit = ast_true(v->value);
837                                 } else if (!strcasecmp(v->name, "isolation")) {
838                                         if ((isolation = text2isolation(v->value)) == 0) {
839                                                 ast_log(LOG_ERROR, "Unrecognized value for 'isolation': '%s' in section '%s'\n", v->value, cat);
840                                                 isolation = SQL_TXN_READ_COMMITTED;
841                                         }
842                                 }
843                         }
844
845                         if (enabled && !ast_strlen_zero(dsn)) {
846                                 new = ao2_alloc(sizeof(*new), odbc_class_destructor);
847
848                                 if (!new) {
849                                         res = -1;
850                                         break;
851                                 }
852
853                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
854                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
855
856                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
857                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
858                                         ao2_ref(new, -1);
859                                         return res;
860                                 }
861
862                                 new->obj_container = ao2_container_alloc(1, null_hash_fn, ao2_match_by_addr);
863
864                                 if (pooling) {
865                                         new->haspool = pooling;
866                                         if (limit) {
867                                                 new->limit = limit;
868                                         } else {
869                                                 ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
870                                                 new->limit = 5;
871                                         }
872                                 }
873
874                                 new->backslash_is_escape = bse ? 1 : 0;
875                                 new->forcecommit = forcecommit ? 1 : 0;
876                                 new->isolation = isolation;
877                                 new->idlecheck = idlecheck;
878                                 new->conntimeout = conntimeout;
879                                 new->negative_connection_cache = ncache;
880
881                                 if (cat)
882                                         ast_copy_string(new->name, cat, sizeof(new->name));
883                                 if (dsn)
884                                         ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
885                                 if (username && !(new->username = ast_strdup(username))) {
886                                         ao2_ref(new, -1);
887                                         break;
888                                 }
889                                 if (password && !(new->password = ast_strdup(password))) {
890                                         ao2_ref(new, -1);
891                                         break;
892                                 }
893                                 if (sanitysql && !(new->sanitysql = ast_strdup(sanitysql))) {
894                                         ao2_ref(new, -1);
895                                         break;
896                                 }
897
898                                 odbc_register_class(new, preconnect);
899                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
900                                 ao2_ref(new, -1);
901                                 new = NULL;
902                         }
903                 }
904         }
905         ast_config_destroy(config);
906         return res;
907 }
908
909 static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
910 {
911         struct ao2_iterator aoi = ao2_iterator_init(class_container, 0);
912         struct odbc_class *class;
913         struct odbc_obj *current;
914         int length = 0;
915         int which = 0;
916         char *ret = NULL;
917
918         switch (cmd) {
919         case CLI_INIT:
920                 e->command = "odbc show";
921                 e->usage =
922                                 "Usage: odbc show [class]\n"
923                                 "       List settings of a particular ODBC class or,\n"
924                                 "       if not specified, all classes.\n";
925                 return NULL;
926         case CLI_GENERATE:
927                 if (a->pos != 2)
928                         return NULL;
929                 length = strlen(a->word);
930                 while ((class = ao2_iterator_next(&aoi))) {
931                         if (!strncasecmp(a->word, class->name, length) && ++which > a->n) {
932                                 ret = ast_strdup(class->name);
933                         }
934                         ao2_ref(class, -1);
935                         if (ret) {
936                                 break;
937                         }
938                 }
939                 ao2_iterator_destroy(&aoi);
940                 if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) {
941                         ret = ast_strdup("all");
942                 }
943                 return ret;
944         }
945
946         ast_cli(a->fd, "\nODBC DSN Settings\n");
947         ast_cli(a->fd,   "-----------------\n\n");
948         aoi = ao2_iterator_init(class_container, 0);
949         while ((class = ao2_iterator_next(&aoi))) {
950                 if ((a->argc == 2) || (a->argc == 3 && !strcmp(a->argv[2], "all")) || (!strcmp(a->argv[2], class->name))) {
951                         int count = 0;
952                         char timestr[80];
953                         struct ast_tm tm;
954
955                         ast_localtime(&class->last_negative_connect, &tm, NULL);
956                         ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %T", &tm);
957                         ast_cli(a->fd, "  Name:   %s\n  DSN:    %s\n", class->name, class->dsn);
958                         ast_cli(a->fd, "    Last connection attempt: %s\n", timestr);
959
960                         if (class->haspool) {
961                                 struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
962
963                                 ast_cli(a->fd, "  Pooled: Yes\n  Limit:  %d\n  Connections in use: %d\n", class->limit, class->count);
964
965                                 while ((current = ao2_iterator_next(&aoi2))) {
966                                         ast_mutex_lock(&current->lock);
967 #ifdef DEBUG_THREADS
968                                         ast_cli(a->fd, "    - Connection %d: %s (%s:%d %s)\n", ++count,
969                                                 current->used ? "in use" :
970                                                 current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected",
971                                                 current->file, current->lineno, current->function);
972 #else
973                                         ast_cli(a->fd, "    - Connection %d: %s\n", ++count,
974                                                 current->used ? "in use" :
975                                                 current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
976 #endif
977                                         ast_mutex_unlock(&current->lock);
978                                         ao2_ref(current, -1);
979                                 }
980                                 ao2_iterator_destroy(&aoi2);
981                         } else {
982                                 /* Should only ever be one of these (unless there are transactions) */
983                                 struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
984                                 while ((current = ao2_iterator_next(&aoi2))) {
985                                         ast_cli(a->fd, "  Pooled: No\n  Connected: %s\n", current->used ? "In use" :
986                                                 current->up && ast_odbc_sanity_check(current) ? "Yes" : "No");
987                                         ao2_ref(current, -1);
988                                 }
989                                 ao2_iterator_destroy(&aoi2);
990                         }
991                         ast_cli(a->fd, "\n");
992                 }
993                 ao2_ref(class, -1);
994         }
995         ao2_iterator_destroy(&aoi);
996
997         return CLI_SUCCESS;
998 }
999
1000 static struct ast_cli_entry cli_odbc[] = {
1001         AST_CLI_DEFINE(handle_cli_odbc_show, "List ODBC DSN(s)")
1002 };
1003
1004 static int odbc_register_class(struct odbc_class *class, int preconnect)
1005 {
1006         struct odbc_obj *obj;
1007         if (class) {
1008                 ao2_link(class_container, class);
1009                 /* I still have a reference in the caller, so a deref is NOT missing here. */
1010
1011                 if (preconnect) {
1012                         /* Request and release builds a connection */
1013                         obj = ast_odbc_request_obj(class->name, 0);
1014                         if (obj) {
1015                                 ast_odbc_release_obj(obj);
1016                         }
1017                 }
1018
1019                 return 0;
1020         } else {
1021                 ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
1022                 return -1;
1023         }
1024 }
1025
1026 static void odbc_release_obj2(struct odbc_obj *obj, struct odbc_txn_frame *tx)
1027 {
1028         SQLINTEGER nativeerror=0, numfields=0;
1029         SQLSMALLINT diagbytes=0, i;
1030         unsigned char state[10], diagnostic[256];
1031
1032         ast_debug(2, "odbc_release_obj2(%p) called (obj->txf = %p)\n", obj, obj->txf);
1033         if (tx) {
1034                 ast_debug(1, "called on a transactional handle with %s\n", tx->forcecommit ? "COMMIT" : "ROLLBACK");
1035                 if (SQLEndTran(SQL_HANDLE_DBC, obj->con, tx->forcecommit ? SQL_COMMIT : SQL_ROLLBACK) == SQL_ERROR) {
1036                         /* Handle possible transaction commit failure */
1037                         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1038                         for (i = 0; i < numfields; i++) {
1039                                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1040                                 ast_log(LOG_WARNING, "SQLEndTran returned an error: %s: %s\n", state, diagnostic);
1041                                 if (!strcmp((char *)state, "25S02") || !strcmp((char *)state, "08007")) {
1042                                         /* These codes mean that a commit failed and a transaction
1043                                          * is still active. We must rollback, or things will get
1044                                          * very, very weird for anybody using the handle next. */
1045                                         SQLEndTran(SQL_HANDLE_DBC, obj->con, SQL_ROLLBACK);
1046                                 }
1047                                 if (i > 10) {
1048                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1049                                         break;
1050                                 }
1051                         }
1052                 }
1053
1054                 /* Transaction is done, reset autocommit */
1055                 if (SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_ON, 0) == SQL_ERROR) {
1056                         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1057                         for (i = 0; i < numfields; i++) {
1058                                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1059                                 ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1060                                 if (i > 10) {
1061                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1062                                         break;
1063                                 }
1064                         }
1065                 }
1066         }
1067
1068 #ifdef DEBUG_THREADS
1069         obj->file[0] = '\0';
1070         obj->function[0] = '\0';
1071         obj->lineno = 0;
1072 #endif
1073
1074         /* For pooled connections, this frees the connection to be
1075          * reused.  For non-pooled connections, it does nothing. */
1076         obj->used = 0;
1077         if (obj->txf) {
1078                 /* Prevent recursion -- transaction is already closed out. */
1079                 obj->txf->obj = NULL;
1080                 obj->txf = release_transaction(obj->txf);
1081         }
1082         ao2_ref(obj, -1);
1083 }
1084
1085 void ast_odbc_release_obj(struct odbc_obj *obj)
1086 {
1087         struct odbc_txn_frame *tx = find_transaction(NULL, obj, NULL, 0);
1088         odbc_release_obj2(obj, tx);
1089 }
1090
1091 int ast_odbc_backslash_is_escape(struct odbc_obj *obj)
1092 {
1093         return obj->parent->backslash_is_escape;
1094 }
1095
1096 static int commit_exec(struct ast_channel *chan, const char *data)
1097 {
1098         struct odbc_txn_frame *tx;
1099         SQLINTEGER nativeerror=0, numfields=0;
1100         SQLSMALLINT diagbytes=0, i;
1101         unsigned char state[10], diagnostic[256];
1102
1103         if (ast_strlen_zero(data)) {
1104                 tx = find_transaction(chan, NULL, NULL, 1);
1105         } else {
1106                 tx = find_transaction(chan, NULL, data, 0);
1107         }
1108
1109         pbx_builtin_setvar_helper(chan, "COMMIT_RESULT", "OK");
1110
1111         if (tx) {
1112                 if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, SQL_COMMIT) == SQL_ERROR) {
1113                         struct ast_str *errors = ast_str_thread_get(&errors_buf, 16);
1114                         ast_str_reset(errors);
1115
1116                         /* Handle possible transaction commit failure */
1117                         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1118                         for (i = 0; i < numfields; i++) {
1119                                 SQLGetDiagRec(SQL_HANDLE_DBC, tx->obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1120                                 ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state);
1121                                 ast_log(LOG_WARNING, "SQLEndTran returned an error: %s: %s\n", state, diagnostic);
1122                                 if (i > 10) {
1123                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1124                                         break;
1125                                 }
1126                         }
1127                         pbx_builtin_setvar_helper(chan, "COMMIT_RESULT", ast_str_buffer(errors));
1128                 }
1129         }
1130         return 0;
1131 }
1132
1133 static int rollback_exec(struct ast_channel *chan, const char *data)
1134 {
1135         struct odbc_txn_frame *tx;
1136         SQLINTEGER nativeerror=0, numfields=0;
1137         SQLSMALLINT diagbytes=0, i;
1138         unsigned char state[10], diagnostic[256];
1139
1140         if (ast_strlen_zero(data)) {
1141                 tx = find_transaction(chan, NULL, NULL, 1);
1142         } else {
1143                 tx = find_transaction(chan, NULL, data, 0);
1144         }
1145
1146         pbx_builtin_setvar_helper(chan, "ROLLBACK_RESULT", "OK");
1147
1148         if (tx) {
1149                 if (SQLEndTran(SQL_HANDLE_DBC, tx->obj->con, SQL_ROLLBACK) == SQL_ERROR) {
1150                         struct ast_str *errors = ast_str_thread_get(&errors_buf, 16);
1151                         ast_str_reset(errors);
1152
1153                         /* Handle possible transaction commit failure */
1154                         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1155                         for (i = 0; i < numfields; i++) {
1156                                 SQLGetDiagRec(SQL_HANDLE_DBC, tx->obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1157                                 ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state);
1158                                 ast_log(LOG_WARNING, "SQLEndTran returned an error: %s: %s\n", state, diagnostic);
1159                                 if (i > 10) {
1160                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1161                                         break;
1162                                 }
1163                         }
1164                         pbx_builtin_setvar_helper(chan, "ROLLBACK_RESULT", ast_str_buffer(errors));
1165                 }
1166         }
1167         return 0;
1168 }
1169
1170 static int aoro2_class_cb(void *obj, void *arg, int flags)
1171 {
1172         struct odbc_class *class = obj;
1173         char *name = arg;
1174         if (!strcmp(class->name, name) && !class->delme) {
1175                 return CMP_MATCH | CMP_STOP;
1176         }
1177         return 0;
1178 }
1179
1180 #define USE_TX (void *)(long)1
1181 #define NO_TX  (void *)(long)2
1182 #define EOR_TX (void *)(long)3
1183
1184 static int aoro2_obj_cb(void *vobj, void *arg, int flags)
1185 {
1186         struct odbc_obj *obj = vobj;
1187         ast_mutex_lock(&obj->lock);
1188         if ((arg == NO_TX && !obj->tx) || (arg == EOR_TX && !obj->used) || (arg == USE_TX && obj->tx && !obj->used)) {
1189                 obj->used = 1;
1190                 ast_mutex_unlock(&obj->lock);
1191                 return CMP_MATCH | CMP_STOP;
1192         }
1193         ast_mutex_unlock(&obj->lock);
1194         return 0;
1195 }
1196
1197 struct odbc_obj *_ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno)
1198 {
1199         struct odbc_obj *obj = NULL;
1200         struct odbc_class *class;
1201         SQLINTEGER nativeerror=0, numfields=0;
1202         SQLSMALLINT diagbytes=0, i;
1203         unsigned char state[10], diagnostic[256];
1204
1205         if (!(class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name))) {
1206                 ast_debug(1, "Class not found!\n");
1207                 return NULL;
1208         }
1209
1210         ast_assert(ao2_ref(class, 0) > 1);
1211
1212         if (class->haspool) {
1213                 /* Recycle connections before building another */
1214                 obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, EOR_TX);
1215
1216                 if (obj) {
1217                         ast_assert(ao2_ref(obj, 0) > 1);
1218                 }
1219                 if (!obj && (class->count < class->limit) &&
1220                                 (time(NULL) > class->last_negative_connect.tv_sec + class->negative_connection_cache.tv_sec)) {
1221                         obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
1222                         if (!obj) {
1223                                 class->count--;
1224                                 ao2_ref(class, -1);
1225                                 ast_debug(3, "Unable to allocate object\n");
1226                                 return NULL;
1227                         }
1228                         ast_assert(ao2_ref(obj, 0) == 1);
1229                         ast_mutex_init(&obj->lock);
1230                         /* obj inherits the outstanding reference to class */
1231                         obj->parent = class;
1232                         class = NULL;
1233                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
1234                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1235                                 ao2_ref(obj, -1);
1236                                 ast_assert(ao2_ref(class, 0) > 0);
1237                                 obj = NULL;
1238                         } else {
1239                                 obj->used = 1;
1240                                 ao2_link(obj->parent->obj_container, obj);
1241                                 ast_atomic_fetchadd_int(&obj->parent->count, +1);
1242                         }
1243                 } else {
1244                         /* Object is not constructed, so delete outstanding reference to class. */
1245                         ao2_ref(class, -1);
1246                         class = NULL;
1247                 }
1248
1249                 if (obj && ast_test_flag(&flags, RES_ODBC_INDEPENDENT_CONNECTION)) {
1250                         /* Ensure this connection has autocommit turned off. */
1251                         if (SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) {
1252                                 SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1253                                 for (i = 0; i < numfields; i++) {
1254                                         SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1255                                         ast_log(LOG_WARNING, "SQLSetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1256                                         if (i > 10) {
1257                                                 ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1258                                                 break;
1259                                         }
1260                                 }
1261                         }
1262                 }
1263         } else if (ast_test_flag(&flags, RES_ODBC_INDEPENDENT_CONNECTION)) {
1264                 /* Non-pooled connections -- but must use a separate connection handle */
1265                 if (!(obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, USE_TX))) {
1266                         ast_debug(1, "Object not found\n");
1267                         obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
1268                         if (!obj) {
1269                                 ao2_ref(class, -1);
1270                                 ast_debug(3, "Unable to allocate object\n");
1271                                 return NULL;
1272                         }
1273                         ast_mutex_init(&obj->lock);
1274                         /* obj inherits the outstanding reference to class */
1275                         obj->parent = class;
1276                         class = NULL;
1277                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
1278                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1279                                 ao2_ref(obj, -1);
1280                                 obj = NULL;
1281                         } else {
1282                                 obj->used = 1;
1283                                 ao2_link(obj->parent->obj_container, obj);
1284                                 ast_atomic_fetchadd_int(&obj->parent->count, +1);
1285                         }
1286                 }
1287
1288                 if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0) == SQL_ERROR) {
1289                         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1290                         for (i = 0; i < numfields; i++) {
1291                                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1292                                 ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1293                                 if (i > 10) {
1294                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1295                                         break;
1296                                 }
1297                         }
1298                 }
1299         } else {
1300                 /* Non-pooled connection: multiple modules can use the same connection. */
1301                 if ((obj = ao2_callback(class->obj_container, 0, aoro2_obj_cb, NO_TX))) {
1302                         /* Object is not constructed, so delete outstanding reference to class. */
1303                         ast_assert(ao2_ref(class, 0) > 1);
1304                         ao2_ref(class, -1);
1305                         class = NULL;
1306                 } else {
1307                         /* No entry: build one */
1308                         if (!(obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor))) {
1309                                 ast_assert(ao2_ref(class, 0) > 1);
1310                                 ao2_ref(class, -1);
1311                                 ast_debug(3, "Unable to allocate object\n");
1312                                 return NULL;
1313                         }
1314                         ast_mutex_init(&obj->lock);
1315                         /* obj inherits the outstanding reference to class */
1316                         obj->parent = class;
1317                         class = NULL;
1318                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
1319                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
1320                                 ao2_ref(obj, -1);
1321                                 obj = NULL;
1322                         } else {
1323                                 ao2_link(obj->parent->obj_container, obj);
1324                                 ast_assert(ao2_ref(obj, 0) > 1);
1325                         }
1326                 }
1327
1328                 if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_ON, 0) == SQL_ERROR) {
1329                         SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1330                         for (i = 0; i < numfields; i++) {
1331                                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1332                                 ast_log(LOG_WARNING, "SetConnectAttr (Autocommit) returned an error: %s: %s\n", state, diagnostic);
1333                                 if (i > 10) {
1334                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1335                                         break;
1336                                 }
1337                         }
1338                 }
1339         }
1340
1341         /* Set the isolation property */
1342         if (obj && SQLSetConnectAttr(obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)obj->parent->isolation, 0) == SQL_ERROR) {
1343                 SQLGetDiagField(SQL_HANDLE_DBC, obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1344                 for (i = 0; i < numfields; i++) {
1345                         SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1346                         ast_log(LOG_WARNING, "SetConnectAttr (Txn isolation) returned an error: %s: %s\n", state, diagnostic);
1347                         if (i > 10) {
1348                                 ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1349                                 break;
1350                         }
1351                 }
1352         }
1353
1354         if (obj && ast_test_flag(&flags, RES_ODBC_CONNECTED) && !obj->up) {
1355                 /* Check if this connection qualifies for reconnection, with negative connection cache time */
1356                 if (time(NULL) > class->last_negative_connect.tv_sec + class->negative_connection_cache.tv_sec) {
1357                         odbc_obj_connect(obj);
1358                 }
1359         } else if (obj && ast_test_flag(&flags, RES_ODBC_SANITY_CHECK)) {
1360                 ast_odbc_sanity_check(obj);
1361         } else if (obj && obj->parent->idlecheck > 0 && ast_tvdiff_sec(ast_tvnow(), obj->last_used) > obj->parent->idlecheck) {
1362                 odbc_obj_connect(obj);
1363         }
1364
1365 #ifdef DEBUG_THREADS
1366         if (obj) {
1367                 ast_copy_string(obj->file, file, sizeof(obj->file));
1368                 ast_copy_string(obj->function, function, sizeof(obj->function));
1369                 obj->lineno = lineno;
1370         }
1371 #endif
1372         ast_assert(class == NULL);
1373
1374         if (obj) {
1375                 ast_assert(ao2_ref(obj, 0) > 1);
1376         }
1377         return obj;
1378 }
1379
1380 struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno)
1381 {
1382         struct ast_flags flags = { check ? RES_ODBC_SANITY_CHECK : 0 };
1383         return _ast_odbc_request_obj2(name, flags, file, function, lineno);
1384 }
1385
1386 struct odbc_obj *ast_odbc_retrieve_transaction_obj(struct ast_channel *chan, const char *objname)
1387 {
1388         struct ast_datastore *txn_store;
1389         AST_LIST_HEAD(, odbc_txn_frame) *oldlist;
1390         struct odbc_txn_frame *txn = NULL;
1391
1392         if (!chan) {
1393                 /* No channel == no transaction */
1394                 return NULL;
1395         }
1396
1397         ast_channel_lock(chan);
1398         if ((txn_store = ast_channel_datastore_find(chan, &txn_info, NULL))) {
1399                 oldlist = txn_store->data;
1400         } else {
1401                 ast_channel_unlock(chan);
1402                 return NULL;
1403         }
1404
1405         AST_LIST_LOCK(oldlist);
1406         ast_channel_unlock(chan);
1407
1408         AST_LIST_TRAVERSE(oldlist, txn, list) {
1409                 if (txn->obj && txn->obj->parent && !strcmp(txn->obj->parent->name, objname)) {
1410                         AST_LIST_UNLOCK(oldlist);
1411                         return txn->obj;
1412                 }
1413         }
1414         AST_LIST_UNLOCK(oldlist);
1415         return NULL;
1416 }
1417
1418 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
1419 {
1420         int res;
1421         SQLINTEGER err;
1422         short int mlen;
1423         unsigned char msg[200], state[10];
1424
1425         /* Nothing to disconnect */
1426         if (!obj->con) {
1427                 return ODBC_SUCCESS;
1428         }
1429
1430         ast_mutex_lock(&obj->lock);
1431
1432         res = SQLDisconnect(obj->con);
1433
1434         if (obj->parent) {
1435                 if (res == SQL_SUCCESS || res == SQL_SUCCESS_WITH_INFO) {
1436                         ast_log(LOG_DEBUG, "Disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
1437                 } else {
1438                         ast_log(LOG_DEBUG, "res_odbc: %s [%s] already disconnected\n", obj->parent->name, obj->parent->dsn);
1439                 }
1440         }
1441
1442         if ((res = SQLFreeHandle(SQL_HANDLE_DBC, obj->con) == SQL_SUCCESS)) {
1443                 obj->con = NULL;
1444                 ast_log(LOG_DEBUG, "Database handle deallocated\n");
1445         } else {
1446                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, state, &err, msg, 100, &mlen);
1447                 ast_log(LOG_WARNING, "Unable to deallocate database handle? %d errno=%d %s\n", res, (int)err, msg);
1448         }
1449
1450         obj->up = 0;
1451         ast_mutex_unlock(&obj->lock);
1452         return ODBC_SUCCESS;
1453 }
1454
1455 static odbc_status odbc_obj_connect(struct odbc_obj *obj)
1456 {
1457         int res;
1458         SQLINTEGER err;
1459         short int mlen;
1460         unsigned char msg[200], state[10];
1461 #ifdef NEEDTRACE
1462         SQLINTEGER enable = 1;
1463         char *tracefile = "/tmp/odbc.trace";
1464 #endif
1465         ast_mutex_lock(&obj->lock);
1466
1467         if (obj->up) {
1468                 odbc_obj_disconnect(obj);
1469                 ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
1470         } else {
1471                 ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
1472         }
1473
1474         res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);
1475
1476         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
1477                 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
1478                 obj->parent->last_negative_connect = ast_tvnow();
1479                 ast_mutex_unlock(&obj->lock);
1480                 return ODBC_FAIL;
1481         }
1482         SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) obj->parent->conntimeout, 0);
1483         SQLSetConnectAttr(obj->con, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER *) obj->parent->conntimeout, 0);
1484 #ifdef NEEDTRACE
1485         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
1486         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
1487 #endif
1488
1489         res = SQLConnect(obj->con,
1490                    (SQLCHAR *) obj->parent->dsn, SQL_NTS,
1491                    (SQLCHAR *) obj->parent->username, SQL_NTS,
1492                    (SQLCHAR *) obj->parent->password, SQL_NTS);
1493
1494         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
1495                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, state, &err, msg, 100, &mlen);
1496                 obj->parent->last_negative_connect = ast_tvnow();
1497                 ast_mutex_unlock(&obj->lock);
1498                 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
1499                 return ODBC_FAIL;
1500         } else {
1501                 ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
1502                 obj->up = 1;
1503                 obj->last_used = ast_tvnow();
1504         }
1505
1506         ast_mutex_unlock(&obj->lock);
1507         return ODBC_SUCCESS;
1508 }
1509
1510 static int acf_transaction_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
1511 {
1512         AST_DECLARE_APP_ARGS(args,
1513                 AST_APP_ARG(property);
1514                 AST_APP_ARG(opt);
1515         );
1516         struct odbc_txn_frame *tx;
1517
1518         AST_STANDARD_APP_ARGS(args, data);
1519         if (strcasecmp(args.property, "transaction") == 0) {
1520                 if ((tx = find_transaction(chan, NULL, NULL, 1))) {
1521                         ast_copy_string(buf, tx->name, len);
1522                         return 0;
1523                 }
1524         } else if (strcasecmp(args.property, "isolation") == 0) {
1525                 if (!ast_strlen_zero(args.opt)) {
1526                         tx = find_transaction(chan, NULL, args.opt, 0);
1527                 } else {
1528                         tx = find_transaction(chan, NULL, NULL, 1);
1529                 }
1530                 if (tx) {
1531                         ast_copy_string(buf, isolation2text(tx->isolation), len);
1532                         return 0;
1533                 }
1534         } else if (strcasecmp(args.property, "forcecommit") == 0) {
1535                 if (!ast_strlen_zero(args.opt)) {
1536                         tx = find_transaction(chan, NULL, args.opt, 0);
1537                 } else {
1538                         tx = find_transaction(chan, NULL, NULL, 1);
1539                 }
1540                 if (tx) {
1541                         ast_copy_string(buf, tx->forcecommit ? "1" : "0", len);
1542                         return 0;
1543                 }
1544         }
1545         return -1;
1546 }
1547
1548 static int acf_transaction_write(struct ast_channel *chan, const char *cmd, char *s, const char *value)
1549 {
1550         AST_DECLARE_APP_ARGS(args,
1551                 AST_APP_ARG(property);
1552                 AST_APP_ARG(opt);
1553         );
1554         struct odbc_txn_frame *tx;
1555         SQLINTEGER nativeerror=0, numfields=0;
1556         SQLSMALLINT diagbytes=0, i;
1557         unsigned char state[10], diagnostic[256];
1558
1559         AST_STANDARD_APP_ARGS(args, s);
1560         if (strcasecmp(args.property, "transaction") == 0) {
1561                 /* Set active transaction */
1562                 struct odbc_obj *obj;
1563                 if ((tx = find_transaction(chan, NULL, value, 0))) {
1564                         mark_transaction_active(chan, tx);
1565                 } else {
1566                         /* No such transaction, create one */
1567                         struct ast_flags flags = { RES_ODBC_INDEPENDENT_CONNECTION };
1568                         if (ast_strlen_zero(args.opt) || !(obj = ast_odbc_request_obj2(args.opt, flags))) {
1569                                 ast_log(LOG_ERROR, "Could not create transaction: invalid database specification '%s'\n", S_OR(args.opt, ""));
1570                                 pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_DB");
1571                                 return -1;
1572                         }
1573                         if (!(tx = find_transaction(chan, obj, value, 0))) {
1574                                 pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
1575                                 return -1;
1576                         }
1577                         obj->tx = 1;
1578                 }
1579                 pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
1580                 return 0;
1581         } else if (strcasecmp(args.property, "forcecommit") == 0) {
1582                 /* Set what happens when an uncommitted transaction ends without explicit Commit or Rollback */
1583                 if (ast_strlen_zero(args.opt)) {
1584                         tx = find_transaction(chan, NULL, NULL, 1);
1585                 } else {
1586                         tx = find_transaction(chan, NULL, args.opt, 0);
1587                 }
1588                 if (!tx) {
1589                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
1590                         return -1;
1591                 }
1592                 if (ast_true(value)) {
1593                         tx->forcecommit = 1;
1594                 } else if (ast_false(value)) {
1595                         tx->forcecommit = 0;
1596                 } else {
1597                         ast_log(LOG_ERROR, "Invalid value for forcecommit: '%s'\n", S_OR(value, ""));
1598                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_VALUE");
1599                         return -1;
1600                 }
1601
1602                 pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
1603                 return 0;
1604         } else if (strcasecmp(args.property, "isolation") == 0) {
1605                 /* How do uncommitted transactions affect reads? */
1606                 int isolation = text2isolation(value);
1607                 if (ast_strlen_zero(args.opt)) {
1608                         tx = find_transaction(chan, NULL, NULL, 1);
1609                 } else {
1610                         tx = find_transaction(chan, NULL, args.opt, 0);
1611                 }
1612                 if (!tx) {
1613                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "FAILED_TO_CREATE");
1614                         return -1;
1615                 }
1616                 if (isolation == 0) {
1617                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "INVALID_VALUE");
1618                         ast_log(LOG_ERROR, "Invalid isolation specification: '%s'\n", S_OR(value, ""));
1619                 } else if (SQLSetConnectAttr(tx->obj->con, SQL_ATTR_TXN_ISOLATION, (void *)(long)isolation, 0) == SQL_ERROR) {
1620                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "SQL_ERROR");
1621                         SQLGetDiagField(SQL_HANDLE_DBC, tx->obj->con, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
1622                         for (i = 0; i < numfields; i++) {
1623                                 SQLGetDiagRec(SQL_HANDLE_DBC, tx->obj->con, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
1624                                 ast_log(LOG_WARNING, "SetConnectAttr (Txn isolation) returned an error: %s: %s\n", state, diagnostic);
1625                                 if (i > 10) {
1626                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
1627                                         break;
1628                                 }
1629                         }
1630                 } else {
1631                         pbx_builtin_setvar_helper(chan, "ODBC_RESULT", "OK");
1632                         tx->isolation = isolation;
1633                 }
1634                 return 0;
1635         } else {
1636                 ast_log(LOG_ERROR, "Unknown property: '%s'\n", args.property);
1637                 return -1;
1638         }
1639 }
1640
1641 static struct ast_custom_function odbc_function = {
1642         .name = "ODBC",
1643         .read = acf_transaction_read,
1644         .write = acf_transaction_write,
1645 };
1646
1647 static const char * const app_commit = "ODBC_Commit";
1648 static const char * const app_rollback = "ODBC_Rollback";
1649
1650 /*!
1651  * \internal
1652  * \brief Implements the channels provider.
1653  */
1654 static int data_odbc_provider_handler(const struct ast_data_search *search,
1655                 struct ast_data *root)
1656 {
1657         struct ao2_iterator aoi, aoi2;
1658         struct odbc_class *class;
1659         struct odbc_obj *current;
1660         struct ast_data *data_odbc_class, *data_odbc_connections, *data_odbc_connection;
1661         struct ast_data *enum_node;
1662         int count;
1663
1664         aoi = ao2_iterator_init(class_container, 0);
1665         while ((class = ao2_iterator_next(&aoi))) {
1666                 data_odbc_class = ast_data_add_node(root, "class");
1667                 if (!data_odbc_class) {
1668                         ao2_ref(class, -1);
1669                         continue;
1670                 }
1671
1672                 ast_data_add_structure(odbc_class, data_odbc_class, class);
1673
1674                 if (!ao2_container_count(class->obj_container)) {
1675                         ao2_ref(class, -1);
1676                         continue;
1677                 }
1678
1679                 data_odbc_connections = ast_data_add_node(data_odbc_class, "connections");
1680                 if (!data_odbc_connections) {
1681                         ao2_ref(class, -1);
1682                         continue;
1683                 }
1684
1685                 ast_data_add_bool(data_odbc_class, "shared", !class->haspool);
1686                 /* isolation */
1687                 enum_node = ast_data_add_node(data_odbc_class, "isolation");
1688                 if (!enum_node) {
1689                         ao2_ref(class, -1);
1690                         continue;
1691                 }
1692                 ast_data_add_int(enum_node, "value", class->isolation);
1693                 ast_data_add_str(enum_node, "text", isolation2text(class->isolation));
1694
1695                 count = 0;
1696                 aoi2 = ao2_iterator_init(class->obj_container, 0);
1697                 while ((current = ao2_iterator_next(&aoi2))) {
1698                         data_odbc_connection = ast_data_add_node(data_odbc_connections, "connection");
1699                         if (!data_odbc_connection) {
1700                                 ao2_ref(current, -1);
1701                                 continue;
1702                         }
1703
1704                         ast_mutex_lock(&current->lock);
1705                         ast_data_add_str(data_odbc_connection, "status", current->used ? "in use" :
1706                                         current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
1707                         ast_data_add_bool(data_odbc_connection, "transactional", current->tx);
1708                         ast_mutex_unlock(&current->lock);
1709
1710                         if (class->haspool) {
1711                                 ast_data_add_int(data_odbc_connection, "number", ++count);
1712                         }
1713
1714                         ao2_ref(current, -1);
1715                 }
1716                 ao2_ref(class, -1);
1717
1718                 if (!ast_data_search_match(search, data_odbc_class)) {
1719                         ast_data_remove_node(root, data_odbc_class);
1720                 }
1721         }
1722         return 0;
1723 }
1724
1725 /*!
1726  * \internal
1727  * \brief /asterisk/res/odbc/listprovider.
1728  */
1729 static const struct ast_data_handler odbc_provider = {
1730         .version = AST_DATA_HANDLER_VERSION,
1731         .get = data_odbc_provider_handler
1732 };
1733
1734 static const struct ast_data_entry odbc_providers[] = {
1735         AST_DATA_ENTRY("/asterisk/res/odbc", &odbc_provider),
1736 };
1737
1738 static int reload(void)
1739 {
1740         struct odbc_cache_tables *table;
1741         struct odbc_class *class;
1742         struct odbc_obj *current;
1743         struct ao2_iterator aoi = ao2_iterator_init(class_container, 0);
1744
1745         /* First, mark all to be purged */
1746         while ((class = ao2_iterator_next(&aoi))) {
1747                 class->delme = 1;
1748                 ao2_ref(class, -1);
1749         }
1750         ao2_iterator_destroy(&aoi);
1751
1752         load_odbc_config();
1753
1754         /* Purge remaining classes */
1755
1756         /* Note on how this works; this is a case of circular references, so we
1757          * explicitly do NOT want to use a callback here (or we wind up in
1758          * recursive hell).
1759          *
1760          * 1. Iterate through all the classes.  Note that the classes will currently
1761          * contain two classes of the same name, one of which is marked delme and
1762          * will be purged when all remaining objects of the class are released, and
1763          * the other, which was created above when we re-parsed the config file.
1764          * 2. On each class, there is a reference held by the master container and
1765          * a reference held by each connection object.  There are two cases for
1766          * destruction of the class, noted below.  However, in all cases, all O-refs
1767          * (references to objects) will first be freed, which will cause the C-refs
1768          * (references to classes) to be decremented (but never to 0, because the
1769          * class container still has a reference).
1770          *    a) If the class has outstanding objects, the C-ref by the class
1771          *    container will then be freed, which leaves only C-refs by any
1772          *    outstanding objects.  When the final outstanding object is released
1773          *    (O-refs held by applications and dialplan functions), it will in turn
1774          *    free the final C-ref, causing class destruction.
1775          *    b) If the class has no outstanding objects, when the class container
1776          *    removes the final C-ref, the class will be destroyed.
1777          */
1778         aoi = ao2_iterator_init(class_container, 0);
1779         while ((class = ao2_iterator_next(&aoi))) { /* C-ref++ (by iterator) */
1780                 if (class->delme) {
1781                         struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
1782                         while ((current = ao2_iterator_next(&aoi2))) { /* O-ref++ (by iterator) */
1783                                 ao2_unlink(class->obj_container, current); /* unlink O-ref from class (reference handled implicitly) */
1784                                 ao2_ref(current, -1); /* O-ref-- (by iterator) */
1785                                 /* At this point, either
1786                                  * a) there's an outstanding O-ref, or
1787                                  * b) the object has already been destroyed.
1788                                  */
1789                         }
1790                         ao2_iterator_destroy(&aoi2);
1791                         ao2_unlink(class_container, class); /* unlink C-ref from container (reference handled implicitly) */
1792                         /* At this point, either
1793                          * a) there's an outstanding O-ref, which holds an outstanding C-ref, or
1794                          * b) the last remaining C-ref is held by the iterator, which will be
1795                          * destroyed in the next step.
1796                          */
1797                 }
1798                 ao2_ref(class, -1); /* C-ref-- (by iterator) */
1799         }
1800         ao2_iterator_destroy(&aoi);
1801
1802         /* Empty the cache; it will get rebuilt the next time the tables are needed. */
1803         AST_RWLIST_WRLOCK(&odbc_tables);
1804         while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
1805                 destroy_table_cache(table);
1806         }
1807         AST_RWLIST_UNLOCK(&odbc_tables);
1808
1809         return 0;
1810 }
1811
1812 static int unload_module(void)
1813 {
1814         /* Prohibit unloading */
1815         return -1;
1816 }
1817
1818 static int load_module(void)
1819 {
1820         if (!(class_container = ao2_container_alloc(1, null_hash_fn, ao2_match_by_addr)))
1821                 return AST_MODULE_LOAD_DECLINE;
1822         if (load_odbc_config() == -1)
1823                 return AST_MODULE_LOAD_DECLINE;
1824         ast_cli_register_multiple(cli_odbc, ARRAY_LEN(cli_odbc));
1825         ast_data_register_multiple(odbc_providers, ARRAY_LEN(odbc_providers));
1826         ast_register_application_xml(app_commit, commit_exec);
1827         ast_register_application_xml(app_rollback, rollback_exec);
1828         ast_custom_function_register(&odbc_function);
1829         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
1830         return 0;
1831 }
1832
1833 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "ODBC resource",
1834                 .load = load_module,
1835                 .unload = unload_module,
1836                 .reload = reload,
1837                 .load_pri = AST_MODPRI_REALTIME_DEPEND,
1838                );