Merged revisions 77571 via svnmerge from
[asterisk/asterisk.git] / res / res_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, 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  *
29  * \arg See also: \ref cdr_odbc
30  */
31
32 /*** MODULEINFO
33         <depend>unixodbc</depend>
34         <depend>ltdl</depend>
35  ***/
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45
46 #include "asterisk/file.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/config.h"
50 #include "asterisk/options.h"
51 #include "asterisk/pbx.h"
52 #include "asterisk/module.h"
53 #include "asterisk/cli.h"
54 #include "asterisk/lock.h"
55 #include "asterisk/res_odbc.h"
56
57 struct odbc_class
58 {
59         AST_LIST_ENTRY(odbc_class) list;
60         char name[80];
61         char dsn[80];
62         char username[80];
63         char password[80];
64         char sanitysql[256];
65         SQLHENV env;
66         unsigned int haspool:1;         /* Boolean - TDS databases need this */
67         unsigned int limit:10;          /* Gives a limit of 1023 maximum */
68         unsigned int count:10;          /* Running count of pooled connections */
69         unsigned int delme:1;                   /* Purge the class */
70         AST_LIST_HEAD(, odbc_obj) odbc_obj;
71 };
72
73 AST_LIST_HEAD_STATIC(odbc_list, odbc_class);
74
75 static odbc_status odbc_obj_connect(struct odbc_obj *obj);
76 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj);
77 static int odbc_register_class(struct odbc_class *class, int connect);
78
79
80 SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
81 {
82         int res = 0, i, attempt;
83         SQLINTEGER nativeerror=0, numfields=0;
84         SQLSMALLINT diagbytes=0;
85         unsigned char state[10], diagnostic[256];
86         SQLHSTMT stmt;
87
88         for (attempt = 0; attempt < 2; attempt++) {
89                 /* This prepare callback may do more than just prepare -- it may also
90                  * bind parameters, bind results, etc.  The real key, here, is that
91                  * when we disconnect, all handles become invalid for most databases.
92                  * We must therefore redo everything when we establish a new
93                  * connection. */
94                 stmt = prepare_cb(obj, data);
95
96                 if (stmt) {
97                         res = SQLExecute(stmt);
98                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
99                                 if (res == SQL_ERROR) {
100                                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
101                                         for (i = 0; i < numfields; i++) {
102                                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
103                                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
104                                                 if (i > 10) {
105                                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
106                                                         break;
107                                                 }
108                                         }
109                                 }
110
111                                 ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
112                                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
113                                 stmt = NULL;
114
115                                 obj->up = 0;
116                                 /*
117                                  * While this isn't the best way to try to correct an error, this won't automatically
118                                  * fail when the statement handle invalidates.
119                                  */
120                                 ast_odbc_sanity_check(obj);
121                                 continue;
122                         }
123                         break;
124                 } else if (attempt == 0)
125                         ast_odbc_sanity_check(obj);
126         }
127
128         return stmt;
129 }
130
131 int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt) 
132 {
133         int res = 0, i;
134         SQLINTEGER nativeerror=0, numfields=0;
135         SQLSMALLINT diagbytes=0;
136         unsigned char state[10], diagnostic[256];
137
138         res = SQLExecute(stmt);
139         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
140                 if (res == SQL_ERROR) {
141                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
142                         for (i = 0; i < numfields; i++) {
143                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
144                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
145                                 if (i > 10) {
146                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
147                                         break;
148                                 }
149                         }
150                 }
151 #if 0
152                 /* This is a really bad method of trying to correct a dead connection.  It
153                  * only ever really worked with MySQL.  It will not work with any other
154                  * database, since most databases prepare their statements on the server,
155                  * and if you disconnect, you invalidate the statement handle.  Hence, if
156                  * you disconnect, you're going to fail anyway, whether you try to execute
157                  * a second time or not.
158                  */
159                 ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
160                 ast_mutex_lock(&obj->lock);
161                 obj->up = 0;
162                 ast_mutex_unlock(&obj->lock);
163                 odbc_obj_disconnect(obj);
164                 odbc_obj_connect(obj);
165                 res = SQLExecute(stmt);
166 #endif
167         }
168         
169         return res;
170 }
171
172
173 int ast_odbc_sanity_check(struct odbc_obj *obj) 
174 {
175         char *test_sql = "select 1";
176         SQLHSTMT stmt;
177         int res = 0;
178
179         if (obj->parent->sanitysql)
180                 test_sql = obj->parent->sanitysql;
181
182         if (obj->up) {
183                 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
184                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
185                         obj->up = 0;
186                 } else {
187                         res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
188                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
189                                 obj->up = 0;
190                         } else {
191                                 res = SQLExecute(stmt);
192                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
193                                         obj->up = 0;
194                                 }
195                         }
196                 }
197                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
198         }
199
200         if (!obj->up) { /* Try to reconnect! */
201                 ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
202                 odbc_obj_disconnect(obj);
203                 odbc_obj_connect(obj);
204         }
205         return obj->up;
206 }
207
208 static int load_odbc_config(void)
209 {
210         static char *cfg = "res_odbc.conf";
211         struct ast_config *config;
212         struct ast_variable *v;
213         char *cat, *dsn, *username, *password, *sanitysql;
214         int enabled, pooling, limit;
215         int connect = 0, res = 0;
216
217         struct odbc_class *new;
218
219         config = ast_config_load(cfg);
220         if (!config) {
221                 ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
222                 return -1;
223         }
224         for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
225                 if (!strcasecmp(cat, "ENV")) {
226                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
227                                 setenv(v->name, v->value, 1);
228                                 ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
229                         }
230                 } else {
231                         /* Reset all to defaults for each class of odbc connections */
232                         dsn = username = password = sanitysql = NULL;
233                         enabled = 1;
234                         connect = 0;
235                         pooling = 0;
236                         limit = 0;
237                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
238                                 if (!strcasecmp(v->name, "pooling")) {
239                                         if (ast_true(v->value))
240                                                 pooling = 1;
241                                 } else if (!strcasecmp(v->name, "limit")) {
242                                         sscanf(v->value, "%d", &limit);
243                                         if (ast_true(v->value) && !limit) {
244                                                 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);
245                                                 limit = 1023;
246                                         } else if (ast_false(v->value)) {
247                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
248                                                 enabled = 0;
249                                                 break;
250                                         }
251                                 } else if (!strcasecmp(v->name, "enabled")) {
252                                         enabled = ast_true(v->value);
253                                 } else if (!strcasecmp(v->name, "pre-connect")) {
254                                         connect = ast_true(v->value);
255                                 } else if (!strcasecmp(v->name, "dsn")) {
256                                         dsn = v->value;
257                                 } else if (!strcasecmp(v->name, "username")) {
258                                         username = v->value;
259                                 } else if (!strcasecmp(v->name, "password")) {
260                                         password = v->value;
261                                 } else if (!strcasecmp(v->name, "sanitysql")) {
262                                         sanitysql = v->value;
263                                 }
264                         }
265
266                         if (enabled && !ast_strlen_zero(dsn)) {
267                                 new = ast_calloc(1, sizeof(*new));
268
269                                 if (!new) {
270                                         res = -1;
271                                         break;
272                                 }
273
274                                 if (cat)
275                                         ast_copy_string(new->name, cat, sizeof(new->name));
276                                 if (dsn)
277                                         ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
278                                 if (username)
279                                         ast_copy_string(new->username, username, sizeof(new->username));
280                                 if (password)
281                                         ast_copy_string(new->password, password, sizeof(new->password));
282                                 if (sanitysql)
283                                         ast_copy_string(new->sanitysql, sanitysql, sizeof(new->sanitysql));
284
285                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
286                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
287
288                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
289                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
290                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
291                                         return res;
292                                 }
293
294                                 if (pooling) {
295                                         new->haspool = pooling;
296                                         if (limit) {
297                                                 new->limit = limit;
298                                         } else {
299                                                 ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
300                                                 new->limit = 5;
301                                         }
302                                 }
303
304                                 odbc_register_class(new, connect);
305                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
306                         }
307                 }
308         }
309         ast_config_destroy(config);
310         return res;
311 }
312
313 static int odbc_show_command(int fd, int argc, char **argv)
314 {
315         struct odbc_class *class;
316         struct odbc_obj *current;
317
318         AST_LIST_LOCK(&odbc_list);
319         AST_LIST_TRAVERSE(&odbc_list, class, list) {
320                 if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all")) || (!strcmp(argv[2], class->name))) {
321                         int count = 0;
322                         ast_cli(fd, "Name: %s\nDSN: %s\n", class->name, class->dsn);
323
324                         if (class->haspool) {
325                                 ast_cli(fd, "Pooled: yes\nLimit: %d\nConnections in use: %d\n", class->limit, class->count);
326
327                                 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
328                                         ast_cli(fd, "  Connection %d: %s\n", ++count, current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
329                                 }
330                         } else {
331                                 /* Should only ever be one of these */
332                                 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
333                                         ast_cli(fd, "Pooled: no\nConnected: %s\n", current->up && ast_odbc_sanity_check(current) ? "yes" : "no");
334                                 }
335                         }
336
337                                 ast_cli(fd, "\n");
338                 }
339         }
340         AST_LIST_UNLOCK(&odbc_list);
341
342         return 0;
343 }
344
345 static const char show_usage[] =
346 "Usage: odbc show [<class>]\n"
347 "       List settings of a particular ODBC class.\n"
348 "       or, if not specified, all classes.\n";
349
350 static struct ast_cli_entry cli_odbc[] = {
351         { { "odbc", "show", NULL },
352         odbc_show_command, "List ODBC DSN(s)",
353         show_usage },
354 };
355
356 static int odbc_register_class(struct odbc_class *class, int connect)
357 {
358         struct odbc_obj *obj;
359         if (class) {
360                 AST_LIST_LOCK(&odbc_list);
361                 AST_LIST_INSERT_HEAD(&odbc_list, class, list);
362                 AST_LIST_UNLOCK(&odbc_list);
363
364                 if (connect) {
365                         /* Request and release builds a connection */
366                         obj = ast_odbc_request_obj(class->name, 0);
367                         if (obj)
368                                 ast_odbc_release_obj(obj);
369                 }
370
371                 return 0;
372         } else {
373                 ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
374                 return -1;
375         }
376 }
377
378 void ast_odbc_release_obj(struct odbc_obj *obj)
379 {
380         /* For pooled connections, this frees the connection to be
381          * reused.  For non-pooled connections, it does nothing. */
382         obj->used = 0;
383 }
384
385 struct odbc_obj *ast_odbc_request_obj(const char *name, int check)
386 {
387         struct odbc_obj *obj = NULL;
388         struct odbc_class *class;
389
390         AST_LIST_LOCK(&odbc_list);
391         AST_LIST_TRAVERSE(&odbc_list, class, list) {
392                 if (!strcmp(class->name, name))
393                         break;
394         }
395         AST_LIST_UNLOCK(&odbc_list);
396
397         if (!class)
398                 return NULL;
399
400         AST_LIST_LOCK(&class->odbc_obj);
401         if (class->haspool) {
402                 /* Recycle connections before building another */
403                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
404                         if (! obj->used) {
405                                 obj->used = 1;
406                                 break;
407                         }
408                 }
409
410                 if (!obj && (class->count < class->limit)) {
411                         class->count++;
412                         obj = ast_calloc(1, sizeof(*obj));
413                         if (!obj) {
414                                 AST_LIST_UNLOCK(&class->odbc_obj);
415                                 return NULL;
416                         }
417                         ast_mutex_init(&obj->lock);
418                         obj->parent = class;
419                         odbc_obj_connect(obj);
420                         AST_LIST_INSERT_TAIL(&class->odbc_obj, obj, list);
421                 }
422         } else {
423                 /* Non-pooled connection: multiple modules can use the same connection. */
424                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
425                         /* Non-pooled connection: if there is an entry, return it */
426                         break;
427                 }
428
429                 if (!obj) {
430                         /* No entry: build one */
431                         obj = ast_calloc(1, sizeof(*obj));
432                         if (!obj) {
433                                 AST_LIST_UNLOCK(&class->odbc_obj);
434                                 return NULL;
435                         }
436                         ast_mutex_init(&obj->lock);
437                         obj->parent = class;
438                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
439                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
440                                 ast_mutex_destroy(&obj->lock);
441                                 ast_free(obj);
442                                 obj = NULL;
443                         } else {
444                                 AST_LIST_INSERT_HEAD(&class->odbc_obj, obj, list);
445                         }
446                 }
447         }
448         AST_LIST_UNLOCK(&class->odbc_obj);
449
450         if (obj && check) {
451                 ast_odbc_sanity_check(obj);
452         }
453         return obj;
454 }
455
456 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
457 {
458         int res;
459         ast_mutex_lock(&obj->lock);
460
461         res = SQLDisconnect(obj->con);
462
463         if (res == ODBC_SUCCESS) {
464                 ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
465         } else {
466                 ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
467                 obj->parent->name, obj->parent->dsn);
468         }
469         obj->up = 0;
470         ast_mutex_unlock(&obj->lock);
471         return ODBC_SUCCESS;
472 }
473
474 static odbc_status odbc_obj_connect(struct odbc_obj *obj)
475 {
476         int res;
477         SQLINTEGER err;
478         short int mlen;
479         unsigned char msg[200], stat[10];
480 #ifdef NEEDTRACE
481         SQLINTEGER enable = 1;
482         char *tracefile = "/tmp/odbc.trace";
483 #endif
484         ast_mutex_lock(&obj->lock);
485
486         res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);
487
488         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
489
490                 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
491                 SQLFreeHandle(SQL_HANDLE_ENV, obj->parent->env);
492
493                 ast_mutex_unlock(&obj->lock);
494                 return ODBC_FAIL;
495         }
496         SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
497 #ifdef NEEDTRACE
498         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
499         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
500 #endif
501
502         if (obj->up) {
503                 odbc_obj_disconnect(obj);
504                 ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
505         } else {
506                 ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
507         }
508
509         res = SQLConnect(obj->con,
510                    (SQLCHAR *) obj->parent->dsn, SQL_NTS,
511                    (SQLCHAR *) obj->parent->username, SQL_NTS,
512                    (SQLCHAR *) obj->parent->password, SQL_NTS);
513
514         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
515                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
516                 ast_mutex_unlock(&obj->lock);
517                 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
518                 return ODBC_FAIL;
519         } else {
520                 ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
521                 obj->up = 1;
522         }
523
524         ast_mutex_unlock(&obj->lock);
525         return ODBC_SUCCESS;
526 }
527
528 static int reload(void)
529 {
530         static char *cfg = "res_odbc.conf";
531         struct ast_config *config;
532         struct ast_variable *v;
533         char *cat, *dsn, *username, *password, *sanitysql;
534         int enabled, pooling, limit;
535         int connect = 0, res = 0;
536
537         struct odbc_class *new, *class;
538         struct odbc_obj *current;
539
540         /* First, mark all to be purged */
541         AST_LIST_LOCK(&odbc_list);
542         AST_LIST_TRAVERSE(&odbc_list, class, list) {
543                 class->delme = 1;
544         }
545
546         config = ast_config_load(cfg);
547         if (config) {
548                 for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
549                         if (!strcasecmp(cat, "ENV")) {
550                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
551                                         setenv(v->name, v->value, 1);
552                                         ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
553                                 }
554                         } else {
555                                 /* Reset all to defaults for each class of odbc connections */
556                                 dsn = username = password = sanitysql = NULL;
557                                 enabled = 1;
558                                 connect = 0;
559                                 pooling = 0;
560                                 limit = 0;
561                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
562                                         if (!strcasecmp(v->name, "pooling")) {
563                                                 pooling = 1;
564                                         } else if (!strcasecmp(v->name, "limit")) {
565                                                 sscanf(v->value, "%d", &limit);
566                                                 if (ast_true(v->value) && !limit) {
567                                                         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);
568                                                         limit = 1023;
569                                                 } else if (ast_false(v->value)) {
570                                                         ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
571                                                         enabled = 0;
572                                                         break;
573                                                 }
574                                         } else if (!strcasecmp(v->name, "enabled")) {
575                                                 enabled = ast_true(v->value);
576                                         } else if (!strcasecmp(v->name, "pre-connect")) {
577                                                 connect = ast_true(v->value);
578                                         } else if (!strcasecmp(v->name, "dsn")) {
579                                                 dsn = v->value;
580                                         } else if (!strcasecmp(v->name, "username")) {
581                                                 username = v->value;
582                                         } else if (!strcasecmp(v->name, "password")) {
583                                                 password = v->value;
584                                         } else if (!strcasecmp(v->name, "sanitysql")) {
585                                                 sanitysql = v->value;
586                                         }
587                                 }
588
589                                 if (enabled && !ast_strlen_zero(dsn)) {
590                                         /* First, check the list to see if it already exists */
591                                         AST_LIST_TRAVERSE(&odbc_list, class, list) {
592                                                 if (!strcmp(class->name, cat)) {
593                                                         class->delme = 0;
594                                                         break;
595                                                 }
596                                         }
597
598                                         if (class) {
599                                                 new = class;
600                                         } else {
601                                                 new = ast_calloc(1, sizeof(*new));
602                                         }
603
604                                         if (!new) {
605                                                 res = -1;
606                                                 break;
607                                         }
608
609                                         if (cat)
610                                                 ast_copy_string(new->name, cat, sizeof(new->name));
611                                         if (dsn)
612                                                 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
613                                         if (username)
614                                                 ast_copy_string(new->username, username, sizeof(new->username));
615                                         if (password)
616                                                 ast_copy_string(new->password, password, sizeof(new->password));
617                                         if (sanitysql)
618                                                 ast_copy_string(new->sanitysql, sanitysql, sizeof(new->sanitysql));
619
620                                         if (!class) {
621                                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
622                                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
623
624                                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
625                                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
626                                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
627                                                         AST_LIST_UNLOCK(&odbc_list);
628                                                         return res;
629                                                 }
630                                         }
631
632                                         if (pooling) {
633                                                 new->haspool = pooling;
634                                                 if (limit) {
635                                                         new->limit = limit;
636                                                 } else {
637                                                         ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
638                                                         new->limit = 5;
639                                                 }
640                                         }
641
642                                         if (class) {
643                                                 ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn);
644                                         } else {
645                                                 odbc_register_class(new, connect);
646                                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
647                                         }
648                                 }
649                         }
650                 }
651                 ast_config_destroy(config);
652         }
653
654         /* Purge classes that we know can go away (pooled with 0, only) */
655         AST_LIST_TRAVERSE_SAFE_BEGIN(&odbc_list, class, list) {
656                 if (class->delme && class->haspool && class->count == 0) {
657                         AST_LIST_TRAVERSE_SAFE_BEGIN(&(class->odbc_obj), current, list) {
658                                 AST_LIST_REMOVE_CURRENT(&(class->odbc_obj), list);
659                                 odbc_obj_disconnect(current);
660                                 ast_mutex_destroy(&current->lock);
661                                 ast_free(current);
662                         }
663                         AST_LIST_TRAVERSE_SAFE_END;
664
665                         AST_LIST_REMOVE_CURRENT(&odbc_list, list);
666                         ast_free(class);
667                 }
668         }
669         AST_LIST_TRAVERSE_SAFE_END;
670         AST_LIST_UNLOCK(&odbc_list);
671
672         return 0;
673 }
674
675 static int unload_module(void)
676 {
677         /* Prohibit unloading */
678         return -1;
679 }
680
681 static int load_module(void)
682 {
683         if(load_odbc_config() == -1)
684                 return AST_MODULE_LOAD_DECLINE;
685         ast_cli_register_multiple(cli_odbc, sizeof(cli_odbc) / sizeof(struct ast_cli_entry));
686         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
687         return 0;
688 }
689
690 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "ODBC resource",
691                 .load = load_module,
692                 .unload = unload_module,
693                 .reload = reload,
694                );