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