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