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