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