ast_calloc() already reports OOM conditions
[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 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                                                 res = -1;
260                                                 break;
261                                         }
262
263                                         if (cat)
264                                                 ast_copy_string(new->name, cat, sizeof(new->name));
265                                         if (dsn)
266                                                 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
267                                         if (username)
268                                                 ast_copy_string(new->username, username, sizeof(new->username));
269                                         if (password)
270                                                 ast_copy_string(new->password, password, sizeof(new->password));
271
272                                         SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
273                                         res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
274
275                                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
276                                                 ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
277                                                 SQLFreeHandle(SQL_HANDLE_ENV, new->env);
278                                                 return res;
279                                         }
280
281                                         if (pooling) {
282                                                 new->haspool = pooling;
283                                                 if (limit) {
284                                                         new->limit = limit;
285                                                 } else {
286                                                         ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
287                                                         new->limit = 5;
288                                                 }
289                                         }
290
291                                         odbc_register_class(new, connect);
292                                         ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
293                                 }
294                         }
295                 }
296                 ast_config_destroy(config);
297         }
298         return res;
299 }
300
301 static int odbc_show_command(int fd, int argc, char **argv)
302 {
303         struct odbc_class *class;
304         struct odbc_obj *current;
305
306         if (!strcmp(argv[1], "show")) {
307                 AST_LIST_LOCK(&odbc_list);
308                 AST_LIST_TRAVERSE(&odbc_list, class, list) {
309                         if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all")) || (!strcmp(argv[2], class->name))) {
310                                 int count = 0;
311                                 ast_cli(fd, "Name: %s\nDSN: %s\n", class->name, class->dsn);
312
313                                 if (class->haspool) {
314                                         ast_cli(fd, "Pooled: yes\nLimit: %d\nConnections in use: %d\n", class->limit, class->count);
315
316                                         AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
317                                                 ast_cli(fd, "  Connection %d: %s", ++count, current->up && odbc_sanity_check(current) ? "connected" : "disconnected");
318                                         }
319                                 } else {
320                                         /* Should only ever be one of these */
321                                         AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
322                                                 ast_cli(fd, "Pooled: no\nConnected: %s\n", current->up && odbc_sanity_check(current) ? "yes" : "no");
323                                         }
324                                 }
325
326                                 ast_cli(fd, "\n");
327                         }
328                 }
329                 AST_LIST_UNLOCK(&odbc_list);
330         }
331         return 0;
332 }
333
334 static char show_usage[] =
335 "Usage: odbc show [<class>]\n"
336 "       List settings of a particular ODBC class.\n"
337 "       or, if not specified, all classes.\n";
338
339 static struct ast_cli_entry odbc_show_struct =
340         { { "odbc", "show", NULL }, odbc_show_command, "Show ODBC DSN(s)", show_usage };
341
342 static int odbc_register_class(struct odbc_class *class, int connect)
343 {
344         struct odbc_obj *obj;
345         if (class) {
346                 AST_LIST_LOCK(&odbc_list);
347                 AST_LIST_INSERT_HEAD(&odbc_list, class, list);
348                 AST_LIST_UNLOCK(&odbc_list);
349
350                 if (connect) {
351                         /* Request and release builds a connection */
352                         obj = odbc_request_obj(class->name, 0);
353                         odbc_release_obj(obj);
354                 }
355
356                 return 0;
357         } else {
358                 ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
359                 return -1;
360         }
361 }
362
363 void odbc_release_obj(struct odbc_obj *obj)
364 {
365         /* For pooled connections, this frees the connection to be
366          * reused.  For non-pooled connections, it does nothing. */
367         obj->used = 0;
368 }
369
370 struct odbc_obj *odbc_request_obj(const char *name, int check)
371 {
372         struct odbc_obj *obj = NULL;
373         struct odbc_class *class;
374
375         AST_LIST_LOCK(&odbc_list);
376         AST_LIST_TRAVERSE(&odbc_list, class, list) {
377                 if (!strcmp(class->name, name))
378                         break;
379         }
380         AST_LIST_UNLOCK(&odbc_list);
381
382         if (!class)
383                 return NULL;
384
385         AST_LIST_LOCK(&class->odbc_obj);
386         if (class->haspool) {
387                 /* Recycle connections before building another */
388                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
389                         if (! obj->used) {
390                                 obj->used = 1;
391                                 break;
392                         }
393                 }
394
395                 if (!obj && (class->count < class->limit)) {
396                         class->count++;
397                         obj = ast_calloc(1, sizeof(*obj));
398                         if (!obj) {
399                                 AST_LIST_UNLOCK(&class->odbc_obj);
400                                 return NULL;
401                         }
402                         ast_mutex_init(&obj->lock);
403                         obj->parent = class;
404                         odbc_obj_connect(obj);
405                         AST_LIST_INSERT_TAIL(&class->odbc_obj, obj, list);
406                 }
407         } else {
408                 /* Non-pooled connection: multiple modules can use the same connection. */
409                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
410                         /* Non-pooled connection: if there is an entry, return it */
411                         break;
412                 }
413
414                 if (!obj) {
415                         /* No entry: build one */
416                         obj = ast_calloc(1, sizeof(*obj));
417                         if (!obj) {
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                                                 res = -1;
590                                                 break;
591                                         }
592
593                                         if (cat)
594                                                 ast_copy_string(new->name, cat, sizeof(new->name));
595                                         if (dsn)
596                                                 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
597                                         if (username)
598                                                 ast_copy_string(new->username, username, sizeof(new->username));
599                                         if (password)
600                                                 ast_copy_string(new->password, password, sizeof(new->password));
601
602                                         if (!class) {
603                                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
604                                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
605
606                                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
607                                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
608                                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
609                                                         AST_LIST_UNLOCK(&odbc_list);
610                                                         return res;
611                                                 }
612                                         }
613
614                                         if (pooling) {
615                                                 new->haspool = pooling;
616                                                 if (limit) {
617                                                         new->limit = limit;
618                                                 } else {
619                                                         ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
620                                                         new->limit = 5;
621                                                 }
622                                         }
623
624                                         if (class) {
625                                                 ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn);
626                                         } else {
627                                                 odbc_register_class(new, connect);
628                                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
629                                         }
630                                 }
631                         }
632                 }
633                 ast_config_destroy(config);
634         }
635
636         /* Purge classes that we know can go away (pooled with 0, only) */
637         AST_LIST_TRAVERSE_SAFE_BEGIN(&odbc_list, class, list) {
638                 if (class->delme && class->haspool && class->count == 0) {
639                         AST_LIST_TRAVERSE_SAFE_BEGIN(&(class->odbc_obj), current, list) {
640                                 AST_LIST_REMOVE_CURRENT(&(class->odbc_obj), list);
641                                 odbc_obj_disconnect(current);
642                                 ast_mutex_destroy(&current->lock);
643                                 free(current);
644                         }
645                         AST_LIST_TRAVERSE_SAFE_END;
646
647                         AST_LIST_REMOVE_CURRENT(&odbc_list, list);
648                         free(class);
649                 }
650         }
651         AST_LIST_TRAVERSE_SAFE_END;
652         AST_LIST_UNLOCK(&odbc_list);
653
654         return 0;
655 }
656
657 static int unload_module(void *mod)
658 {
659         /* Prohibit unloading */
660         return -1;
661 }
662
663 static int load_module(void *mod)
664 {
665         load_odbc_config();
666         ast_cli_register(&odbc_show_struct);
667         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
668         return 0;
669 }
670
671 static const char *description(void)
672 {
673         return "ODBC Resource";
674 }
675
676 static const char *key(void)
677 {
678         return ASTERISK_GPL_KEY;
679 }
680
681 STD_MOD(MOD_0, reload, NULL, NULL);