Asterisk - The Open Source Telephony Project  GIT-master-4a4f1a5
res_odbc.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2012, 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  * \author Tilghman Lesher <tilghman@digium.com>
29  *
30  * \arg See also: \ref cdr_odbc.c
31  */
32 
33 /*! \li \ref res_odbc.c uses the configuration file \ref res_odbc.conf
34  * \addtogroup configuration_file Configuration Files
35  */
36 
37 /*!
38  * \page res_odbc.conf res_odbc.conf
39  * \verbinclude res_odbc.conf.sample
40  */
41 
42 /*** MODULEINFO
43  <depend>generic_odbc</depend>
44  <depend>res_odbc_transaction</depend>
45  <support_level>core</support_level>
46  ***/
47 
48 #include "asterisk.h"
49 
50 #include "asterisk/file.h"
51 #include "asterisk/channel.h"
52 #include "asterisk/config.h"
53 #include "asterisk/pbx.h"
54 #include "asterisk/module.h"
55 #include "asterisk/cli.h"
56 #include "asterisk/lock.h"
57 #include "asterisk/res_odbc.h"
58 #include "asterisk/time.h"
59 #include "asterisk/astobj2.h"
60 #include "asterisk/app.h"
61 #include "asterisk/strings.h"
62 #include "asterisk/threadstorage.h"
63 
64 struct odbc_class
65 {
67  char name[80];
68  char dsn[80];
69  char *username;
70  char *password;
71  char *sanitysql;
72  SQLHENV env;
73  unsigned int delme:1; /*!< Purge the class */
74  unsigned int backslash_is_escape:1; /*!< On this database, the backslash is a native escape sequence */
75  unsigned int forcecommit:1; /*!< Should uncommitted transactions be auto-committed on handle release? */
76  unsigned int isolation; /*!< Flags for how the DB should deal with data in other, uncommitted transactions */
77  unsigned int conntimeout; /*!< Maximum time the connection process should take */
78  unsigned int maxconnections; /*!< Maximum number of allowed connections */
79  /*! When a connection fails, cache that failure for how long? */
80  struct timeval negative_connection_cache;
81  /*! When a connection fails, when did that last occur? */
82  struct timeval last_negative_connect;
83  /*! A pool of available connections */
85  /*! Lock to protect the connections */
87  /*! Condition to notify any pending connection requesters */
89  /*! The total number of current connections */
91  /*! Whether logging is enabled on this class or not */
92  unsigned int logging;
93  /*! The number of prepares executed on this class (total from all connections */
95  /*! The number of queries executed on this class (total from all connections) */
97  /*! The longest execution time for a query executed on this class */
99  /*! The SQL query that took the longest to execute */
100  char *sql_text;
101  /*! Slow query limit (in milliseconds) */
102  unsigned int slowquerylimit;
103 };
104 
106 
108 
109 static odbc_status odbc_obj_connect(struct odbc_obj *obj);
110 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj);
111 static void odbc_register_class(struct odbc_class *class, int connect);
112 
114 
118  struct odbc_obj *obj; /*!< Database handle within which transacted statements are run */
119  /*!\brief Is this record the current active transaction within the channel?
120  * Note that the active flag is really only necessary for statements which
121  * are triggered from the dialplan, as there isn't a direct correlation
122  * between multiple statements. Applications wishing to use transactions
123  * may simply perform each statement on the same odbc_obj, which keeps the
124  * transaction persistent.
125  */
126  unsigned int active:1;
127  unsigned int forcecommit:1; /*!< Should uncommitted transactions be auto-committed on handle release? */
128  unsigned int isolation; /*!< Flags for how the DB should deal with data in other, uncommitted transactions */
129  char name[0]; /*!< Name of this transaction ID */
130 };
131 
132 const char *ast_odbc_isolation2text(int iso)
133 {
134  if (iso == SQL_TXN_READ_COMMITTED) {
135  return "read_committed";
136  } else if (iso == SQL_TXN_READ_UNCOMMITTED) {
137  return "read_uncommitted";
138  } else if (iso == SQL_TXN_SERIALIZABLE) {
139  return "serializable";
140  } else if (iso == SQL_TXN_REPEATABLE_READ) {
141  return "repeatable_read";
142  } else {
143  return "unknown";
144  }
145 }
146 
147 int ast_odbc_text2isolation(const char *txt)
148 {
149  if (strncasecmp(txt, "read_", 5) == 0) {
150  if (strncasecmp(txt + 5, "c", 1) == 0) {
151  return SQL_TXN_READ_COMMITTED;
152  } else if (strncasecmp(txt + 5, "u", 1) == 0) {
153  return SQL_TXN_READ_UNCOMMITTED;
154  } else {
155  return 0;
156  }
157  } else if (strncasecmp(txt, "ser", 3) == 0) {
158  return SQL_TXN_SERIALIZABLE;
159  } else if (strncasecmp(txt, "rep", 3) == 0) {
160  return SQL_TXN_REPEATABLE_READ;
161  } else {
162  return 0;
163  }
164 }
165 
166 static void odbc_class_destructor(void *data)
167 {
168  struct odbc_class *class = data;
169  struct odbc_obj *obj;
170 
171  /* Due to refcounts, we can safely assume that any objects with a reference
172  * to us will prevent our destruction, so we don't need to worry about them.
173  */
174  if (class->username) {
175  ast_free(class->username);
176  }
177  if (class->password) {
178  ast_free(class->password);
179  }
180  if (class->sanitysql) {
181  ast_free(class->sanitysql);
182  }
183 
184  while ((obj = AST_LIST_REMOVE_HEAD(&class->connections, list))) {
185  ao2_ref(obj, -1);
186  }
187 
188  SQLFreeHandle(SQL_HANDLE_ENV, class->env);
189  ast_mutex_destroy(&class->lock);
190  ast_cond_destroy(&class->cond);
191  ast_free(class->sql_text);
192 }
193 
194 static void odbc_obj_destructor(void *data)
195 {
196  struct odbc_obj *obj = data;
197 
198  odbc_obj_disconnect(obj);
199 }
200 
202 {
203  struct odbc_cache_columns *col;
204 
205  ast_debug(1, "Destroying table cache for %s\n", table->table);
206 
207  AST_RWLIST_WRLOCK(&table->columns);
208  while ((col = AST_RWLIST_REMOVE_HEAD(&table->columns, list))) {
209  ast_free(col);
210  }
211  AST_RWLIST_UNLOCK(&table->columns);
212  AST_RWLIST_HEAD_DESTROY(&table->columns);
213 
214  ast_free(table);
215 }
216 
217 /*!
218  * XXX This creates a connection and disconnects it. In some situations, the caller of
219  * this function has its own connection and could donate it to this function instead of
220  * needing to create another one.
221  *
222  * XXX The automatic readlock of the columns is awkward. It's done because it's possible for
223  * multiple threads to have references to the table, and the table is not refcounted. Possible
224  * changes here would be
225  * * Eliminate the table cache entirely. The use of ast_odbc_find_table() is generally
226  * questionable. The only real good use right now is from ast_realtime_require_field() in
227  * order to make sure the DB has the expected columns in it. Since that is only used sparingly,
228  * the need to cache tables is questionable. Instead, the table structure can be fetched from
229  * the DB directly each time, resulting in a single owner of the data.
230  * * Make odbc_cache_tables a refcounted object.
231  */
232 struct odbc_cache_tables *ast_odbc_find_table(const char *database, const char *tablename)
233 {
234  struct odbc_cache_tables *tableptr;
235  struct odbc_cache_columns *entry;
236  char columnname[80];
237  SQLLEN sqlptr;
238  SQLHSTMT stmt = NULL;
239  int res = 0, error = 0;
240  struct odbc_obj *obj;
241 
243  AST_RWLIST_TRAVERSE(&odbc_tables, tableptr, list) {
244  if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
245  break;
246  }
247  }
248  if (tableptr) {
249  AST_RWLIST_RDLOCK(&tableptr->columns);
251  return tableptr;
252  }
253 
254  if (!(obj = ast_odbc_request_obj(database, 0))) {
255  ast_log(LOG_WARNING, "Unable to retrieve database handle for table description '%s@%s'\n", tablename, database);
257  return NULL;
258  }
259 
260  /* Table structure not already cached; build it now. */
261  do {
262  res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
263  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
264  ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", database);
265  break;
266  }
267 
268  res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)tablename, SQL_NTS, (unsigned char *)"%", SQL_NTS);
269  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
270  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
271  ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.\n", database);
272  break;
273  }
274 
275  if (!(tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + strlen(database) + 1 + strlen(tablename) + 1))) {
276  ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", tablename, database);
277  break;
278  }
279 
280  tableptr->connection = (char *)tableptr + sizeof(*tableptr);
281  tableptr->table = (char *)tableptr + sizeof(*tableptr) + strlen(database) + 1;
282  strcpy(tableptr->connection, database); /* SAFE */
283  strcpy(tableptr->table, tablename); /* SAFE */
284  AST_RWLIST_HEAD_INIT(&(tableptr->columns));
285 
286  while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
287  SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
288 
289  if (!(entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1))) {
290  ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, tablename, database);
291  error = 1;
292  break;
293  }
294  entry->name = (char *)entry + sizeof(*entry);
295  strcpy(entry->name, columnname);
296 
297  SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
298  SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
299  SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
300  SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
301  SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
302  SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
303 
304  /* Specification states that the octenlen should be the maximum number of bytes
305  * returned in a char or binary column, but it seems that some drivers just set
306  * it to NULL. (Bad Postgres! No biscuit!) */
307  if (entry->octetlen == 0) {
308  entry->octetlen = entry->size;
309  }
310 
311  ast_debug(3, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix);
312  /* Insert column info into column list */
313  AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
314  }
315  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
316 
318  AST_RWLIST_RDLOCK(&(tableptr->columns));
319  break;
320  } while (1);
321 
323 
324  if (error) {
325  destroy_table_cache(tableptr);
326  tableptr = NULL;
327  }
329  return tableptr;
330 }
331 
332 struct odbc_cache_columns *ast_odbc_find_column(struct odbc_cache_tables *table, const char *colname)
333 {
334  struct odbc_cache_columns *col;
335  AST_RWLIST_TRAVERSE(&table->columns, col, list) {
336  if (strcasecmp(col->name, colname) == 0) {
337  return col;
338  }
339  }
340  return NULL;
341 }
342 
343 int ast_odbc_clear_cache(const char *database, const char *tablename)
344 {
345  struct odbc_cache_tables *tableptr;
346 
348  AST_RWLIST_TRAVERSE_SAFE_BEGIN(&odbc_tables, tableptr, list) {
349  if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
351  destroy_table_cache(tableptr);
352  break;
353  }
354  }
357  return tableptr ? 0 : -1;
358 }
359 
360 SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data)
361 {
362  struct timeval start;
363  SQLHSTMT stmt;
364 
365  if (obj->parent->logging) {
366  start = ast_tvnow();
367  }
368 
369  stmt = exec_cb(obj, data);
370 
371  if (obj->parent->logging) {
372  long execution_time = ast_tvdiff_ms(ast_tvnow(), start);
373 
374  if (obj->parent->slowquerylimit && execution_time > obj->parent->slowquerylimit) {
375  ast_log(LOG_WARNING, "SQL query '%s' took %ld milliseconds to execute on class '%s', this may indicate a database problem\n",
376  obj->sql_text, execution_time, obj->parent->name);
377  }
378 
379  ast_mutex_lock(&obj->parent->lock);
380  if (execution_time > obj->parent->longest_query_execution_time || !obj->parent->sql_text) {
381  obj->parent->longest_query_execution_time = execution_time;
382  /* Due to the callback nature of the res_odbc API it's not possible to ensure that
383  * the SQL text is removed from the connection in all cases, so only if it becomes the
384  * new longest executing query do we steal the SQL text. In other cases what will happen
385  * is that the SQL text will be freed if the connection is released back to the class or
386  * if a new query is done on the connection.
387  */
388  ast_free(obj->parent->sql_text);
389  obj->parent->sql_text = obj->sql_text;
390  obj->sql_text = NULL;
391  }
392  ast_mutex_unlock(&obj->parent->lock);
393  }
394 
395  return stmt;
396 }
397 
398 SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
399 {
400  struct timeval start;
401  int res = 0;
402  SQLHSTMT stmt;
403 
404  if (obj->parent->logging) {
405  start = ast_tvnow();
406  }
407 
408  /* This prepare callback may do more than just prepare -- it may also
409  * bind parameters, bind results, etc. The real key, here, is that
410  * when we disconnect, all handles become invalid for most databases.
411  * We must therefore redo everything when we establish a new
412  * connection. */
413  stmt = prepare_cb(obj, data);
414  if (!stmt) {
415  return NULL;
416  }
417 
418  res = SQLExecute(stmt);
419  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
420  if (res == SQL_ERROR) {
421  ast_odbc_print_errors(SQL_HANDLE_STMT, stmt, "SQL Execute");
422  }
423 
424  ast_log(LOG_WARNING, "SQL Execute error %d!\n", res);
425  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
426  stmt = NULL;
427  } else if (obj->parent->logging) {
428  long execution_time = ast_tvdiff_ms(ast_tvnow(), start);
429 
430  if (obj->parent->slowquerylimit && execution_time > obj->parent->slowquerylimit) {
431  ast_log(LOG_WARNING, "SQL query '%s' took %ld milliseconds to execute on class '%s', this may indicate a database problem\n",
432  obj->sql_text, execution_time, obj->parent->name);
433  }
434 
435  ast_mutex_lock(&obj->parent->lock);
436 
437  /* If this takes the record on longest query execution time, update the parent class
438  * with the information.
439  */
440  if (execution_time > obj->parent->longest_query_execution_time || !obj->parent->sql_text) {
441  obj->parent->longest_query_execution_time = execution_time;
442  ast_free(obj->parent->sql_text);
443  obj->parent->sql_text = obj->sql_text;
444  obj->sql_text = NULL;
445  }
446  ast_mutex_unlock(&obj->parent->lock);
447 
449  }
450 
451  return stmt;
452 }
453 
454 int ast_odbc_prepare(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
455 {
456  if (obj->parent->logging) {
457  /* It is possible for this connection to be reused without being
458  * released back to the class, so we free what may already exist
459  * and place the new SQL in.
460  */
461  ast_free(obj->sql_text);
462  obj->sql_text = ast_strdup(sql);
464  }
465 
466  return SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
467 }
468 
469 SQLRETURN ast_odbc_execute_sql(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
470 {
471  if (obj->parent->logging) {
472  ast_free(obj->sql_text);
473  obj->sql_text = ast_strdup(sql);
475  }
476 
477  return SQLExecDirect(stmt, (unsigned char *)sql, SQL_NTS);
478 }
479 
480 int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt)
481 {
482  int res = 0;
483 
484  res = SQLExecute(stmt);
485  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
486  if (res == SQL_ERROR) {
487  ast_odbc_print_errors(SQL_HANDLE_STMT, stmt, "SQL Execute");
488  }
489  }
490 
491  if (obj->parent->logging) {
493  }
494 
495  return res;
496 }
497 
498 SQLRETURN ast_odbc_ast_str_SQLGetData(struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind)
499 {
500  SQLRETURN res;
501 
502  if (pmaxlen == 0) {
503  if (SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) {
504  ast_str_make_space(buf, *StrLen_or_Ind + 1);
505  }
506  } else if (pmaxlen > 0) {
507  ast_str_make_space(buf, pmaxlen);
508  }
509  res = SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), ast_str_size(*buf), StrLen_or_Ind);
511 
512  return res;
513 }
514 
515 struct ast_str *ast_odbc_print_errors(SQLSMALLINT handle_type, SQLHANDLE handle, const char *operation)
516 {
517  struct ast_str *errors = ast_str_thread_get(&errors_buf, 16);
518  SQLINTEGER nativeerror = 0;
519  SQLSMALLINT diagbytes = 0;
520  SQLSMALLINT i;
521  unsigned char state[10];
522  unsigned char diagnostic[256];
523 
524  ast_str_reset(errors);
525  i = 0;
526  while (SQLGetDiagRec(handle_type, handle, ++i, state, &nativeerror,
527  diagnostic, sizeof(diagnostic), &diagbytes) == SQL_SUCCESS) {
528  ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state);
529  ast_log(LOG_WARNING, "%s returned an error: %s: %s\n", operation, state, diagnostic);
530  /* XXX Why is this here? */
531  if (i > 10) {
532  ast_log(LOG_WARNING, "There are more than 10 diagnostic records! Ignore the rest.\n");
533  break;
534  }
535  }
536 
537  return errors;
538 }
539 
540 unsigned int ast_odbc_class_get_isolation(struct odbc_class *class)
541 {
542  return class->isolation;
543 }
544 
545 unsigned int ast_odbc_class_get_forcecommit(struct odbc_class *class)
546 {
547  return class->forcecommit;
548 }
549 
550 const char *ast_odbc_class_get_name(struct odbc_class *class)
551 {
552  return class->name;
553 }
554 
555 static int load_odbc_config(void)
556 {
557  static char *cfg = "res_odbc.conf";
558  struct ast_config *config;
559  struct ast_variable *v;
560  char *cat;
561  const char *dsn, *username, *password, *sanitysql;
562  int enabled, bse, conntimeout, forcecommit, isolation, maxconnections, logging, slowquerylimit;
563  struct timeval ncache = { 0, 0 };
564  int preconnect = 0, res = 0;
565  struct ast_flags config_flags = { 0 };
566 
567  struct odbc_class *new;
568 
569  config = ast_config_load(cfg, config_flags);
571  ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
572  return -1;
573  }
574  for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
575  if (!strcasecmp(cat, "ENV")) {
576  for (v = ast_variable_browse(config, cat); v; v = v->next) {
577  setenv(v->name, v->value, 1);
578  ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
579  }
580  } else {
581  /* Reset all to defaults for each class of odbc connections */
583  enabled = 1;
584  preconnect = 0;
585  bse = 1;
586  conntimeout = 10;
587  forcecommit = 0;
588  isolation = SQL_TXN_READ_COMMITTED;
589  maxconnections = 1;
590  logging = 0;
591  slowquerylimit = 5000;
592  for (v = ast_variable_browse(config, cat); v; v = v->next) {
593  if (!strcasecmp(v->name, "pooling") ||
594  !strncasecmp(v->name, "share", 5) ||
595  !strcasecmp(v->name, "limit") ||
596  !strcasecmp(v->name, "idlecheck")) {
597  ast_log(LOG_WARNING, "The 'pooling', 'shared_connections', 'limit', and 'idlecheck' options were replaced by 'max_connections'. See res_odbc.conf.sample.\n");
598  } else if (!strcasecmp(v->name, "enabled")) {
599  enabled = ast_true(v->value);
600  } else if (!strcasecmp(v->name, "pre-connect")) {
601  preconnect = ast_true(v->value);
602  } else if (!strcasecmp(v->name, "dsn")) {
603  dsn = v->value;
604  } else if (!strcasecmp(v->name, "username")) {
605  username = v->value;
606  } else if (!strcasecmp(v->name, "password")) {
607  password = v->value;
608  } else if (!strcasecmp(v->name, "sanitysql")) {
609  sanitysql = v->value;
610  } else if (!strcasecmp(v->name, "backslash_is_escape")) {
611  bse = ast_true(v->value);
612  } else if (!strcasecmp(v->name, "connect_timeout")) {
613  if (sscanf(v->value, "%d", &conntimeout) != 1 || conntimeout < 1) {
614  ast_log(LOG_WARNING, "connect_timeout must be a positive integer\n");
615  conntimeout = 10;
616  }
617  } else if (!strcasecmp(v->name, "negative_connection_cache")) {
618  double dncache;
619  if (sscanf(v->value, "%lf", &dncache) != 1 || dncache < 0) {
620  ast_log(LOG_WARNING, "negative_connection_cache must be a non-negative integer\n");
621  /* 5 minutes sounds like a reasonable default */
622  ncache.tv_sec = 300;
623  ncache.tv_usec = 0;
624  } else {
625  ncache.tv_sec = (int)dncache;
626  ncache.tv_usec = (dncache - ncache.tv_sec) * 1000000;
627  }
628  } else if (!strcasecmp(v->name, "forcecommit")) {
629  forcecommit = ast_true(v->value);
630  } else if (!strcasecmp(v->name, "isolation")) {
631  if ((isolation = ast_odbc_text2isolation(v->value)) == 0) {
632  ast_log(LOG_ERROR, "Unrecognized value for 'isolation': '%s' in section '%s'\n", v->value, cat);
633  isolation = SQL_TXN_READ_COMMITTED;
634  }
635  } else if (!strcasecmp(v->name, "max_connections")) {
636  if (sscanf(v->value, "%30d", &maxconnections) != 1 || maxconnections < 1) {
637  ast_log(LOG_WARNING, "max_connections must be a positive integer\n");
638  maxconnections = 1;
639  }
640  } else if (!strcasecmp(v->name, "logging")) {
641  logging = ast_true(v->value);
642  } else if (!strcasecmp(v->name, "slow_query_limit")) {
643  if (sscanf(v->value, "%30d", &slowquerylimit) != 1) {
644  ast_log(LOG_WARNING, "slow_query_limit must be a positive integer\n");
645  slowquerylimit = 5000;
646  }
647  }
648  }
649 
650  if (enabled && !ast_strlen_zero(dsn)) {
651  new = ao2_alloc(sizeof(*new), odbc_class_destructor);
652 
653  if (!new) {
654  res = -1;
655  break;
656  }
657 
658  SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
659  res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
660 
661  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
662  ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
663  ao2_ref(new, -1);
664  return res;
665  }
666 
667  new->backslash_is_escape = bse ? 1 : 0;
668  new->forcecommit = forcecommit ? 1 : 0;
669  new->isolation = isolation;
670  new->conntimeout = conntimeout;
671  new->negative_connection_cache = ncache;
672  new->maxconnections = maxconnections;
673  new->logging = logging;
674  new->slowquerylimit = slowquerylimit;
675 
676  if (cat)
677  ast_copy_string(new->name, cat, sizeof(new->name));
678  if (dsn)
679  ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
680  if (username && !(new->username = ast_strdup(username))) {
681  ao2_ref(new, -1);
682  break;
683  }
684  if (password && !(new->password = ast_strdup(password))) {
685  ao2_ref(new, -1);
686  break;
687  }
688  if (sanitysql && !(new->sanitysql = ast_strdup(sanitysql))) {
689  ao2_ref(new, -1);
690  break;
691  }
692 
693  ast_mutex_init(&new->lock);
694  ast_cond_init(&new->cond, NULL);
695 
696  odbc_register_class(new, preconnect);
697  ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
698  ao2_ref(new, -1);
699  new = NULL;
700  }
701  }
702  }
704  return res;
705 }
706 
707 static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
708 {
709  struct ao2_iterator aoi;
710  struct odbc_class *class;
711  int length = 0;
712  int which = 0;
713  char *ret = NULL;
714 
715  switch (cmd) {
716  case CLI_INIT:
717  e->command = "odbc show";
718  e->usage =
719  "Usage: odbc show [class]\n"
720  " List settings of a particular ODBC class or,\n"
721  " if not specified, all classes.\n";
722  return NULL;
723  case CLI_GENERATE:
724  if (a->pos != 2)
725  return NULL;
726  length = strlen(a->word);
728  while ((class = ao2_iterator_next(&aoi))) {
729  if (!strncasecmp(a->word, class->name, length) && ++which > a->n) {
730  ret = ast_strdup(class->name);
731  }
732  ao2_ref(class, -1);
733  if (ret) {
734  break;
735  }
736  }
737  ao2_iterator_destroy(&aoi);
738  if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) {
739  ret = ast_strdup("all");
740  }
741  return ret;
742  }
743 
744  ast_cli(a->fd, "\nODBC DSN Settings\n");
745  ast_cli(a->fd, "-----------------\n\n");
747  while ((class = ao2_iterator_next(&aoi))) {
748  if ((a->argc == 2) || (a->argc == 3 && !strcmp(a->argv[2], "all")) || (!strcmp(a->argv[2], class->name))) {
749  char timestr[80];
750  struct ast_tm tm;
751 
752  ast_cli(a->fd, " Name: %s\n DSN: %s\n", class->name, class->dsn);
753 
754  if (class->last_negative_connect.tv_sec > 0) {
755  ast_localtime(&class->last_negative_connect, &tm, NULL);
756  ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %T", &tm);
757  ast_cli(a->fd, " Last fail connection attempt: %s\n", timestr);
758  }
759 
760  ast_cli(a->fd, " Number of active connections: %zd (out of %d)\n", class->connection_cnt, class->maxconnections);
761  ast_cli(a->fd, " Logging: %s\n", class->logging ? "Enabled" : "Disabled");
762  if (class->logging) {
763  ast_cli(a->fd, " Number of prepares executed: %d\n", class->prepares_executed);
764  ast_cli(a->fd, " Number of queries executed: %d\n", class->queries_executed);
765  ast_mutex_lock(&class->lock);
766  if (class->sql_text) {
767  ast_cli(a->fd, " Longest running SQL query: %s (%ld milliseconds)\n", class->sql_text, class->longest_query_execution_time);
768  }
769  ast_mutex_unlock(&class->lock);
770  }
771  ast_cli(a->fd, "\n");
772  }
773  ao2_ref(class, -1);
774  }
775  ao2_iterator_destroy(&aoi);
776 
777  return CLI_SUCCESS;
778 }
779 
780 static struct ast_cli_entry cli_odbc[] = {
781  AST_CLI_DEFINE(handle_cli_odbc_show, "List ODBC DSN(s)")
782 };
783 
784 static void odbc_register_class(struct odbc_class *class, int preconnect)
785 {
786  struct odbc_obj *obj;
787 
788  ao2_link(class_container, class);
789  /* I still have a reference in the caller, so a deref is NOT missing here. */
790 
791  if (!preconnect) {
792  return;
793  }
794 
795  /* Request and release builds a connection */
796  obj = ast_odbc_request_obj(class->name, 0);
797  if (obj) {
799  }
800 
801  return;
802 }
803 
805 {
806  struct odbc_class *class = obj->parent;
807 
808  ast_debug(2, "Releasing ODBC handle %p into pool\n", obj);
809 
810  /* The odbc_obj only holds a reference to the class when it is
811  * actively being used. This guarantees no circular reference
812  * between odbc_class and odbc_obj. Since it is being released
813  * we also release our class reference. If a reload occurred before
814  * the class will go away automatically once all odbc_obj are
815  * released back.
816  */
817  obj->parent = NULL;
818 
819  /* Free the SQL text so that the next user of this connection has
820  * a fresh start.
821  */
822  ast_free(obj->sql_text);
823  obj->sql_text = NULL;
824 
825  ast_mutex_lock(&class->lock);
826  AST_LIST_INSERT_HEAD(&class->connections, obj, list);
827  ast_cond_signal(&class->cond);
828  ast_mutex_unlock(&class->lock);
829 
830  ao2_ref(class, -1);
831 }
832 
834 {
835  return obj->parent->backslash_is_escape;
836 }
837 
838 static int aoro2_class_cb(void *obj, void *arg, int flags)
839 {
840  struct odbc_class *class = obj;
841  char *name = arg;
842  if (!strcmp(class->name, name) && !class->delme) {
843  return CMP_MATCH | CMP_STOP;
844  }
845  return 0;
846 }
847 
848 unsigned int ast_odbc_get_max_connections(const char *name)
849 {
850  struct odbc_class *class;
851  unsigned int max_connections;
852 
853  class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name);
854  if (!class) {
855  return 0;
856  }
857 
858  max_connections = class->maxconnections;
859  ao2_ref(class, -1);
860 
861  return max_connections;
862 }
863 
864 /*
865  * \brief Determine if the connection has died.
866  *
867  * \param connection The connection to check
868  * \param class The ODBC class
869  * \retval 1 Yep, it's dead
870  * \retval 0 It's alive and well
871  */
872 static int connection_dead(struct odbc_obj *connection, struct odbc_class *class)
873 {
874  char *test_sql = "select 1";
875  SQLINTEGER dead;
876  SQLRETURN res;
877  SQLHSTMT stmt;
878 
879  res = SQLGetConnectAttr(connection->con, SQL_ATTR_CONNECTION_DEAD, &dead, 0, 0);
880  if (SQL_SUCCEEDED(res)) {
881  return dead == SQL_CD_TRUE ? 1 : 0;
882  }
883 
884  /* If the Driver doesn't support SQL_ATTR_CONNECTION_DEAD do a
885  * probing query instead
886  */
887  res = SQLAllocHandle(SQL_HANDLE_STMT, connection->con, &stmt);
888  if (!SQL_SUCCEEDED(res)) {
889  return 1;
890  }
891 
892  if (!ast_strlen_zero(class->sanitysql)) {
893  test_sql = class->sanitysql;
894  }
895 
896  res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
897  if (!SQL_SUCCEEDED(res)) {
898  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
899  return 1;
900  }
901 
902  res = SQLExecute(stmt);
903  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
904 
905  return SQL_SUCCEEDED(res) ? 0 : 1;
906 }
907 
908 struct odbc_obj *_ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno)
909 {
910  struct odbc_obj *obj = NULL;
911  struct odbc_class *class;
912 
913  if (!(class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name))) {
914  ast_debug(1, "Class '%s' not found!\n", name);
915  return NULL;
916  }
917 
918  ast_mutex_lock(&class->lock);
919 
920  while (!obj) {
921  obj = AST_LIST_REMOVE_HEAD(&class->connections, list);
922 
923  if (!obj) {
924  if (class->connection_cnt < class->maxconnections) {
925  /* If no connection is immediately available establish a new
926  * one if allowed. If we try and fail we give up completely as
927  * we could go into an infinite loop otherwise.
928  */
929  obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
930  if (!obj) {
931  break;
932  }
933 
934  obj->parent = ao2_bump(class);
935  if (odbc_obj_connect(obj) == ODBC_FAIL) {
936  ao2_ref(obj->parent, -1);
937  ao2_ref(obj, -1);
938  obj = NULL;
939  break;
940  }
941 
942  class->connection_cnt++;
943  ast_debug(2, "Created ODBC handle %p on class '%s', new count is %zd\n", obj,
944  name, class->connection_cnt);
945  } else {
946  /* Otherwise if we're not allowed to create a new one we
947  * wait for another thread to give up the connection they
948  * own.
949  */
950  ast_cond_wait(&class->cond, &class->lock);
951  }
952  } else if (connection_dead(obj, class)) {
953  /* If the connection is dead try to grab another functional one from the
954  * pool instead of trying to resurrect this one.
955  */
956  ao2_ref(obj, -1);
957  obj = NULL;
958  class->connection_cnt--;
959  ast_debug(2, "ODBC handle %p dead - removing from class '%s', new count is %zd\n",
960  obj, name, class->connection_cnt);
961  } else {
962  /* We successfully grabbed a connection from the pool and all is well!
963  */
964  obj->parent = ao2_bump(class);
965  ast_debug(2, "Reusing ODBC handle %p from class '%s'\n", obj, name);
966  }
967  }
968 
969  ast_mutex_unlock(&class->lock);
970  ao2_ref(class, -1);
971 
972  return obj;
973 }
974 
975 struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno)
976 {
977  struct ast_flags flags = { check ? RES_ODBC_SANITY_CHECK : 0 };
978  /* XXX New flow means that the "check" parameter doesn't do anything. We're requesting
979  * a connection from ODBC. We'll either get a new one, which obviously is already connected, or
980  * we'll get one from the ODBC connection pool. In that case, it will ensure to only give us a
981  * live connection
982  */
983  return _ast_odbc_request_obj2(name, flags, file, function, lineno);
984 }
985 
987 {
988  int res;
989  SQLINTEGER err;
990  short int mlen;
991  unsigned char msg[200], state[10];
992  SQLHDBC con;
993 
994  /* Nothing to disconnect */
995  if (!obj->con) {
996  return ODBC_SUCCESS;
997  }
998 
999  con = obj->con;
1000  obj->con = NULL;
1001  res = SQLDisconnect(con);
1002 
1003  if ((res = SQLFreeHandle(SQL_HANDLE_DBC, con)) == SQL_SUCCESS) {
1004  ast_debug(3, "Database handle %p (connection %p) deallocated\n", obj, con);
1005  } else {
1006  SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen);
1007  ast_log(LOG_WARNING, "Unable to deallocate database handle %p? %d errno=%d %s\n", con, res, (int)err, msg);
1008  }
1009 
1010  return ODBC_SUCCESS;
1011 }
1012 
1014 {
1015  int res;
1016  SQLINTEGER err;
1017  short int mlen;
1018  unsigned char msg[200], state[10];
1019 #ifdef NEEDTRACE
1020  SQLINTEGER enable = 1;
1021  char *tracefile = "/tmp/odbc.trace";
1022 #endif
1023  SQLHDBC con;
1024  long int negative_cache_expiration;
1025 
1026  ast_assert(obj->con == NULL);
1027  ast_debug(3, "Connecting %s(%p)\n", obj->parent->name, obj);
1028 
1029  /* Dont connect while server is marked as unreachable via negative_connection_cache */
1030  negative_cache_expiration = obj->parent->last_negative_connect.tv_sec + obj->parent->negative_connection_cache.tv_sec;
1031  if (time(NULL) < negative_cache_expiration) {
1032  ast_log(LOG_WARNING, "Not connecting to %s. Negative connection cache for %ld seconds\n", obj->parent->name, negative_cache_expiration - time(NULL));
1033  return ODBC_FAIL;
1034  }
1035 
1036  res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &con);
1037 
1038  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
1039  ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
1041  return ODBC_FAIL;
1042  }
1043  SQLSetConnectAttr(con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)(long) obj->parent->conntimeout, 0);
1044  SQLSetConnectAttr(con, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER *)(long) obj->parent->conntimeout, 0);
1045 #ifdef NEEDTRACE
1046  SQLSetConnectAttr(con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
1047  SQLSetConnectAttr(con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
1048 #endif
1049 
1050  res = SQLConnect(con,
1051  (SQLCHAR *) obj->parent->dsn, SQL_NTS,
1052  (SQLCHAR *) obj->parent->username, SQL_NTS,
1053  (SQLCHAR *) obj->parent->password, SQL_NTS);
1054 
1055  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
1056  SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen);
1058  ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
1059  if ((res = SQLFreeHandle(SQL_HANDLE_DBC, con)) != SQL_SUCCESS) {
1060  SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen);
1061  ast_log(LOG_WARNING, "Unable to deallocate database handle %p? %d errno=%d %s\n", con, res, (int)err, msg);
1062  }
1063  return ODBC_FAIL;
1064  } else {
1065  ast_debug(3, "res_odbc: Connected to %s [%s (%p)]\n", obj->parent->name, obj->parent->dsn, obj);
1066  }
1067 
1068  obj->con = con;
1069  return ODBC_SUCCESS;
1070 }
1071 
1072 static int reload(void)
1073 {
1074  struct odbc_cache_tables *table;
1075  struct odbc_class *class;
1077 
1078  /* First, mark all to be purged */
1079  while ((class = ao2_iterator_next(&aoi))) {
1080  class->delme = 1;
1081  ao2_ref(class, -1);
1082  }
1083  ao2_iterator_destroy(&aoi);
1084 
1085  load_odbc_config();
1086 
1088  while ((class = ao2_iterator_next(&aoi))) {
1089  if (class->delme) {
1090  ao2_unlink(class_container, class);
1091  }
1092  ao2_ref(class, -1);
1093  }
1094  ao2_iterator_destroy(&aoi);
1095 
1096  /* Empty the cache; it will get rebuilt the next time the tables are needed. */
1098  while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
1100  }
1102 
1103  return 0;
1104 }
1105 
1106 static int unload_module(void)
1107 {
1110 
1111  return 0;
1112 }
1113 
1114 static int load_module(void)
1115 {
1117  if (!class_container) {
1118  return AST_MODULE_LOAD_DECLINE;
1119  }
1120 
1121  if (load_odbc_config() == -1) {
1122  return AST_MODULE_LOAD_DECLINE;
1123  }
1124 
1127 
1128  return AST_MODULE_LOAD_SUCCESS;
1129 }
1130 
1132  .support_level = AST_MODULE_SUPPORT_CORE,
1133  .load = load_module,
1134  .unload = unload_module,
1135  .reload = reload,
1136  .load_pri = AST_MODPRI_REALTIME_DEPEND,
1137  .requires = "res_odbc_transaction",
1138 );
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:182
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
#define ast_log
Definition: astobj2.c:42
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
#define ao2_link(container, obj)
Definition: astobj2.h:1549
@ CMP_MATCH
Definition: astobj2.h:1031
@ CMP_STOP
Definition: astobj2.h:1032
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:365
int ao2_match_by_addr(void *obj, void *arg, int flags)
A common ao2_callback is one that matches by address.
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ao2_unlink(container, obj)
Definition: astobj2.h:1598
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_bump(obj)
Definition: astobj2.h:491
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Definition: astobj2.h:1335
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
static char * dsn
Definition: cdr_odbc.c:58
static char * table
Definition: cdr_odbc.c:58
static const char config[]
Definition: chan_ooh323.c:111
General Asterisk PBX channel definitions.
Standard Command Line Interface.
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static int enabled
Definition: dnsmgr.c:91
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Generic File Format Support. Should be included by clients of the file handling routines....
static const char name[]
Definition: format_mp3.c:68
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
int setenv(const char *name, const char *value, int overwrite)
Configuration File Parser.
#define ast_config_load(filename, flags)
Load a config file.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3328
#define CONFIG_STATUS_FILEMISSING
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define CONFIG_STATUS_FILEINVALID
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1216
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:545
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
Definition: linkedlists.h:639
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
Definition: linkedlists.h:333
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:844
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:617
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:711
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:741
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
#define AST_RWLIST_HEAD_DESTROY(head)
Destroys an rwlist head structure.
Definition: linkedlists.h:667
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
Asterisk locking-related definitions:
#define ast_cond_destroy(cond)
Definition: lock.h:200
#define ast_cond_wait(cond, mutex)
Definition: lock.h:203
#define ast_cond_init(cond, attr)
Definition: lock.h:199
#define ast_mutex_init(pmutex)
Definition: lock.h:184
#define ast_mutex_unlock(a)
Definition: lock.h:188
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
pthread_cond_t ast_cond_t
Definition: lock.h:176
#define ast_mutex_destroy(a)
Definition: lock.h:186
#define ast_mutex_lock(a)
Definition: lock.h:187
#define ast_cond_signal(cond)
Definition: lock.h:201
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:453
#define LOG_ERROR
Definition: logger.h:286
#define LOG_NOTICE
Definition: logger.h:264
#define LOG_WARNING
Definition: logger.h:275
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition: module.h:316
#define ast_module_shutdown_ref(mod)
Prevent unload of the module before shutdown.
Definition: module.h:464
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_REALTIME_DEPEND
Definition: module.h:321
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Core PBX routines and definitions.
AST_LIST_HEAD_NOLOCK(contactliststruct, contact)
static struct ast_threadstorage errors_buf
Definition: res_odbc.c:113
static struct ast_cli_entry cli_odbc[]
Definition: res_odbc.c:780
static void odbc_register_class(struct odbc_class *class, int connect)
Definition: res_odbc.c:784
static int load_odbc_config(void)
Definition: res_odbc.c:555
static int aoro2_class_cb(void *obj, void *arg, int flags)
Definition: res_odbc.c:838
static void odbc_obj_destructor(void *data)
Definition: res_odbc.c:194
static odbc_status odbc_obj_connect(struct odbc_obj *obj)
Definition: res_odbc.c:1013
const char * ast_odbc_class_get_name(struct odbc_class *class)
Get the name of an ODBC class.
Definition: res_odbc.c:550
int ast_odbc_prepare(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
Prepares a SQL query on a statement.
Definition: res_odbc.c:454
unsigned int ast_odbc_class_get_isolation(struct odbc_class *class)
Get the transaction isolation setting for an ODBC class.
Definition: res_odbc.c:540
void ast_odbc_release_obj(struct odbc_obj *obj)
Releases an ODBC object previously allocated by ast_odbc_request_obj()
Definition: res_odbc.c:804
static char * handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_odbc.c:707
unsigned int ast_odbc_get_max_connections(const char *name)
Return the current configured maximum number of connections for a class.
Definition: res_odbc.c:848
int ast_odbc_clear_cache(const char *database, const char *tablename)
Remove a cache entry from memory This function may be called to clear entries created and cached by t...
Definition: res_odbc.c:343
static void odbc_class_destructor(void *data)
Definition: res_odbc.c:166
static struct ao2_container * class_container
Definition: res_odbc.c:105
static void destroy_table_cache(struct odbc_cache_tables *table)
Definition: res_odbc.c:201
int ast_odbc_backslash_is_escape(struct odbc_obj *obj)
Checks if the database natively supports backslash as an escape character.
Definition: res_odbc.c:833
struct odbc_cache_tables * ast_odbc_find_table(const char *database, const char *tablename)
Find or create an entry describing the table specified.
Definition: res_odbc.c:232
const char * ast_odbc_isolation2text(int iso)
Convert from numeric transaction isolation values to their textual counterparts.
Definition: res_odbc.c:132
SQLRETURN ast_odbc_ast_str_SQLGetData(struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind)
Wrapper for SQLGetData to use with dynamic strings.
Definition: res_odbc.c:498
SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT(*exec_cb)(struct odbc_obj *obj, void *data), void *data)
Executes an non prepared statement and returns the resulting statement handle.
Definition: res_odbc.c:360
unsigned int ast_odbc_class_get_forcecommit(struct odbc_class *class)
Get the transaction forcecommit setting for an ODBC class.
Definition: res_odbc.c:545
static int load_module(void)
Definition: res_odbc.c:1114
SQLRETURN ast_odbc_execute_sql(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
Execute a unprepared SQL query.
Definition: res_odbc.c:469
static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
Definition: res_odbc.c:986
struct odbc_obj * _ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno)
Definition: res_odbc.c:975
static int unload_module(void)
Definition: res_odbc.c:1106
static int reload(void)
Definition: res_odbc.c:1072
SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT(*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
Prepares, executes, and returns the resulting statement handle.
Definition: res_odbc.c:398
int ast_odbc_text2isolation(const char *txt)
Convert from textual transaction isolation values to their numeric constants.
Definition: res_odbc.c:147
static int connection_dead(struct odbc_obj *connection, struct odbc_class *class)
Definition: res_odbc.c:872
struct odbc_obj * _ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno)
Definition: res_odbc.c:908
struct ast_str * ast_odbc_print_errors(SQLSMALLINT handle_type, SQLHANDLE handle, const char *operation)
Shortcut for printing errors to logs after a failed SQL operation.
Definition: res_odbc.c:515
int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt)
Executes a prepared statement handle.
Definition: res_odbc.c:480
struct odbc_cache_columns * ast_odbc_find_column(struct odbc_cache_tables *table, const char *colname)
Find a column entry within a cached table structure.
Definition: res_odbc.c:332
ODBC resource manager.
odbc_status
Definition: res_odbc.h:36
@ ODBC_FAIL
Definition: res_odbc.h:36
@ ODBC_SUCCESS
Definition: res_odbc.h:36
@ RES_ODBC_SANITY_CHECK
Definition: res_odbc.h:40
#define ast_odbc_request_obj(name, check)
Get a ODBC connection object.
Definition: res_odbc.h:120
#define NULL
Definition: resample.c:96
String manipulation functions.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1104
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:727
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: main/utils.c:1951
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:666
#define ast_str_make_space(buf, new_len)
Definition: strings.h:793
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
Definition: strings.h:676
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:701
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:712
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:874
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
Main Channel structure associated with a channel.
descriptor for a cli entry.
Definition: cli.h:171
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
struct ast_module * self
Definition: module.h:342
Structure for mutex and tracking information.
Definition: lock.h:135
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:597
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Definition: func_odbc.c:150
Definition: search.h:40
These structures are used for adaptive capabilities.
Definition: res_odbc.h:59
char * connection
Definition: res_odbc.h:71
struct odbc_cache_tables::_columns columns
unsigned int slowquerylimit
Definition: res_odbc.c:102
char dsn[80]
Definition: res_odbc.c:68
struct odbc_class::@444 connections
ast_cond_t cond
Definition: res_odbc.c:88
char * sql_text
Definition: res_odbc.c:100
unsigned int delme
Definition: res_odbc.c:73
int queries_executed
Definition: res_odbc.c:96
char name[80]
Definition: res_odbc.c:67
size_t connection_cnt
Definition: res_odbc.c:90
unsigned int logging
Definition: res_odbc.c:92
char * password
Definition: res_odbc.c:70
unsigned int maxconnections
Definition: res_odbc.c:78
int prepares_executed
Definition: res_odbc.c:94
struct timeval negative_connection_cache
Definition: res_odbc.c:80
unsigned int isolation
Definition: res_odbc.c:76
unsigned int backslash_is_escape
Definition: res_odbc.c:74
char * username
Definition: res_odbc.c:69
SQLHENV env
Definition: res_odbc.c:72
long longest_query_execution_time
Definition: res_odbc.c:98
unsigned int conntimeout
Definition: res_odbc.c:77
struct odbc_class::@443 list
struct timeval last_negative_connect
Definition: res_odbc.c:82
unsigned int forcecommit
Definition: res_odbc.c:75
ast_mutex_t lock
Definition: res_odbc.c:86
char * sanitysql
Definition: res_odbc.c:71
ODBC container.
Definition: res_odbc.h:46
char * sql_text
Definition: res_odbc.h:54
struct odbc_class * parent
Definition: res_odbc.h:48
struct odbc_obj::@281 list
SQLHDBC con
Definition: res_odbc.h:47
char name[0]
Definition: res_odbc.c:129
unsigned int isolation
Definition: res_odbc.c:128
struct ast_channel * owner
Definition: res_odbc.c:117
struct odbc_txn_frame::@445 list
unsigned int active
Is this record the current active transaction within the channel? Note that the active flag is really...
Definition: res_odbc.c:126
unsigned int forcecommit
Definition: res_odbc.c:127
struct odbc_obj * obj
Definition: res_odbc.c:118
const char * name
static struct test_val a
Definitions to aid in the use of thread local storage.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:86
Time-related functions and macros.
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int error(const char *format,...)
Definition: utils/frame.c:999
#define ast_assert(a)
Definition: utils.h:710
#define ARRAY_LEN(a)
Definition: utils.h:639