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