Wed Jan 8 2020 09:50:18

Asterisk developer's documentation


res_config_sqlite.c File Reference

res_config_sqlite module. More...

#include "asterisk.h"
#include <sqlite.h>
#include "asterisk/logger.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/cdr.h"
#include "asterisk/cli.h"
#include "asterisk/lock.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/linkedlists.h"

Go to the source code of this file.

Data Structures

struct  sqlite_cache_tables::_columns
 
struct  cfg_entry_args
 
struct  rt_cfg_entry_args
 
struct  rt_multi_cfg_entry_args
 
struct  sqlite_cache_columns
 
struct  sqlite_cache_tables
 
struct  sqlite_tables
 

Macros

#define MACRO_BEGIN   do {
 
#define MACRO_END   } while (0)
 
#define release_table(a)   AST_RWLIST_UNLOCK(&((a)->columns))
 
#define RES_CONFIG_SQLITE_BEGIN
 
#define RES_CONFIG_SQLITE_CONF_FILE   "res_config_sqlite.conf"
 
#define RES_CONFIG_SQLITE_DESCRIPTION   "Resource Module for SQLite 2"
 
#define RES_CONFIG_SQLITE_DRIVER   "sqlite"
 
#define RES_CONFIG_SQLITE_END(error)
 
#define RES_CONFIG_SQLITE_MAX_LOOPS   10
 
#define RES_CONFIG_SQLITE_NAME   "res_config_sqlite"
 
#define SET_VAR(config, to, from)
 
#define sql_get_config_table
 
#define sql_table_structure   "SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name='%s'"
 

Enumerations

enum  {
  RES_CONFIG_SQLITE_CONFIG_ID, RES_CONFIG_SQLITE_CONFIG_CAT_METRIC, RES_CONFIG_SQLITE_CONFIG_VAR_METRIC, RES_CONFIG_SQLITE_CONFIG_COMMENTED,
  RES_CONFIG_SQLITE_CONFIG_FILENAME, RES_CONFIG_SQLITE_CONFIG_CATEGORY, RES_CONFIG_SQLITE_CONFIG_VAR_NAME, RES_CONFIG_SQLITE_CONFIG_VAR_VAL,
  RES_CONFIG_SQLITE_CONFIG_COLUMNS
}
 

Functions

static void __init_sql_buf (void)
 
static void __init_where_buf (void)
 
static void __reg_module (void)
 
static void __unreg_module (void)
 
static int add_cfg_entry (void *arg, int argc, char **argv, char **columnNames)
 SQLite callback function for static configuration. More...
 
static int add_rt_cfg_entry (void *arg, int argc, char **argv, char **columnNames)
 SQLite callback function for RealTime configuration. More...
 
static int add_rt_multi_cfg_entry (void *arg, int argc, char **argv, char **columnNames)
 SQLite callback function for RealTime configuration. More...
 
static int cdr_handler (struct ast_cdr *cdr)
 Asterisk callback function for CDR support. More...
 
static int check_vars (void)
 
static struct ast_configconfig_handler (const char *database, const char *table, const char *file, struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl, const char *who_asked)
 Asterisk callback function for static configuration. More...
 
static struct sqlite_cache_tablesfind_table (const char *tablename)
 
static int find_table_cb (void *vtblptr, int argc, char **argv, char **columnNames)
 
static void free_table (struct sqlite_cache_tables *tblptr)
 
static size_t get_params (va_list ap, const char ***params_ptr, const char ***vals_ptr, int warn)
 Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values. More...
 
static char * handle_cli_show_sqlite_status (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 Asterisk callback function for the CLI status command. More...
 
static char * handle_cli_sqlite_show_tables (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int load_config (void)
 Load the configuration file. More...
 
static int load_module (void)
 
static int realtime_destroy_handler (const char *database, const char *table, const char *keyfield, const char *entity, va_list ap)
 Asterisk callback function for RealTime configuration (destroys variable). More...
 
static struct ast_variablerealtime_handler (const char *database, const char *table, va_list ap)
 Asterisk callback function for RealTime configuration. More...
 
static struct ast_configrealtime_multi_handler (const char *database, const char *table, va_list ap)
 Asterisk callback function for RealTime configuration. More...
 
static int realtime_require_handler (const char *database, const char *table, va_list ap)
 
static int realtime_store_handler (const char *database, const char *table, va_list ap)
 Asterisk callback function for RealTime configuration (variable create/store). More...
 
static int realtime_unload_handler (const char *unused, const char *tablename)
 
static int realtime_update2_handler (const char *database, const char *table, va_list ap)
 
static int realtime_update_handler (const char *database, const char *table, const char *keyfield, const char *entity, va_list ap)
 Asterisk callback function for RealTime configuration (variable update). More...
 
static int set_var (char **var, const char *name, const char *value)
 Allocate a variable. More...
 
static void unload_config (void)
 Free resources related to configuration. More...
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Realtime SQLite configuration" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static int cdr_registered
 
static char * cdr_table
 
static struct ast_cli_entry cli_status []
 
static int cli_status_registered
 
static char * config_table
 
static sqlite * db
 
static char * dbfile
 
static ast_mutex_t mutex = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 }
 
static struct ast_threadstorage sql_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_sql_buf , .custom_init = NULL , }
 
static char * sql_create_cdr_table
 
static struct ast_config_engine sqlite_engine
 
static struct sqlite_tables sqlite_tables = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, 1 } , }
 
static int use_cdr
 
static struct ast_threadstorage where_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_where_buf , .custom_init = NULL , }
 

Detailed Description

res_config_sqlite module.

Definition in file res_config_sqlite.c.

Macro Definition Documentation

#define MACRO_BEGIN   do {

Definition at line 94 of file res_config_sqlite.c.

#define MACRO_END   } while (0)

Definition at line 95 of file res_config_sqlite.c.

#define release_table (   a)    AST_RWLIST_UNLOCK(&((a)->columns))

Definition at line 711 of file res_config_sqlite.c.

Referenced by cdr_handler().

#define RES_CONFIG_SQLITE_BEGIN
Value:
MACRO_BEGIN \
int __i; \
for (__i = 0; __i < RES_CONFIG_SQLITE_MAX_LOOPS; __i++) {
for(;;)
Definition: ast_expr2.c:2460
#define RES_CONFIG_SQLITE_MAX_LOOPS

Macro used before executing a query.

See Also
RES_CONFIG_SQLITE_MAX_LOOPS.

Definition at line 153 of file res_config_sqlite.c.

Referenced by cdr_handler(), config_handler(), load_module(), realtime_destroy_handler(), realtime_handler(), realtime_multi_handler(), realtime_store_handler(), realtime_update2_handler(), and realtime_update_handler().

#define RES_CONFIG_SQLITE_CONF_FILE   "res_config_sqlite.conf"

Definition at line 100 of file res_config_sqlite.c.

Referenced by load_config().

#define RES_CONFIG_SQLITE_DESCRIPTION   "Resource Module for SQLite 2"

Definition at line 99 of file res_config_sqlite.c.

Referenced by load_module().

#define RES_CONFIG_SQLITE_DRIVER   "sqlite"

Definition at line 98 of file res_config_sqlite.c.

#define RES_CONFIG_SQLITE_END (   error)
Value:
if (error != SQLITE_BUSY) \
break; \
usleep(1000); \
} \
#define MACRO_END

Macro used after executing a query.

See Also
RES_CONFIG_SQLITE_MAX_LOOPS.

Definition at line 164 of file res_config_sqlite.c.

Referenced by cdr_handler(), config_handler(), load_module(), realtime_destroy_handler(), realtime_handler(), realtime_multi_handler(), realtime_store_handler(), realtime_update2_handler(), and realtime_update_handler().

#define RES_CONFIG_SQLITE_MAX_LOOPS   10

Maximum number of loops before giving up executing a query. Calls to sqlite_xxx() functions which can return SQLITE_BUSY are enclosed by RES_CONFIG_SQLITE_BEGIN and RES_CONFIG_SQLITE_END, e.g.

char *errormsg;
int error;
RES_CONFIG_SQLITE_BEGIN
 error = sqlite_exec(db, query, NULL, NULL, &errormsg);
RES_CONFIG_SQLITE_END(error)
if (error)
 ...;

Definition at line 146 of file res_config_sqlite.c.

#define RES_CONFIG_SQLITE_NAME   "res_config_sqlite"

Definition at line 97 of file res_config_sqlite.c.

Referenced by load_module(), and unload_module().

#define SET_VAR (   config,
  to,
  from 
)

Definition at line 114 of file res_config_sqlite.c.

Referenced by load_config().

#define sql_get_config_table
Value:
"SELECT *" \
" FROM '%q'" \
" WHERE filename = '%q' AND commented = 0" \
" ORDER BY cat_metric ASC, var_metric ASC;"

SQL query format to fetch the static configuration of a file. Rows must be sorted by category.

See Also
add_cfg_entry()

Definition at line 569 of file res_config_sqlite.c.

Referenced by config_handler().

#define sql_table_structure   "SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name='%s'"

SQL query format to describe the table structure

Definition at line 561 of file res_config_sqlite.c.

Referenced by find_table().

Enumeration Type Documentation

anonymous enum
Enumerator
RES_CONFIG_SQLITE_CONFIG_ID 
RES_CONFIG_SQLITE_CONFIG_CAT_METRIC 
RES_CONFIG_SQLITE_CONFIG_VAR_METRIC 
RES_CONFIG_SQLITE_CONFIG_COMMENTED 
RES_CONFIG_SQLITE_CONFIG_FILENAME 
RES_CONFIG_SQLITE_CONFIG_CATEGORY 
RES_CONFIG_SQLITE_CONFIG_VAR_NAME 
RES_CONFIG_SQLITE_CONFIG_VAR_VAL 
RES_CONFIG_SQLITE_CONFIG_COLUMNS 

Definition at line 102 of file res_config_sqlite.c.

Function Documentation

static void __init_sql_buf ( void  )
static

Definition at line 127 of file res_config_sqlite.c.

157 {
static void __init_where_buf ( void  )
static

Definition at line 128 of file res_config_sqlite.c.

157 {
static void __reg_module ( void  )
static

Definition at line 1877 of file res_config_sqlite.c.

static void __unreg_module ( void  )
static

Definition at line 1877 of file res_config_sqlite.c.

static int add_cfg_entry ( void *  arg,
int  argc,
char **  argv,
char **  columnNames 
)
static

SQLite callback function for static configuration.

This function is passed to the SQLite engine as a callback function to parse a row and store it in a struct ast_config object. It relies on resulting rows being sorted by category.

Parameters
arga pointer to a struct cfg_entry_args object
argcnumber of columns
argvvalues in the row
columnNamesnames and types of the columns
Return values
0on success
1if an error occurred
See Also
cfg_entry_args
sql_get_config_table
config_handler()

Definition at line 858 of file res_config_sqlite.c.

References args, ast_category_append(), ast_category_destroy(), ast_category_new(), ast_config_internal_load(), ast_free, ast_log(), ast_strdup, ast_variable_append(), ast_variable_new(), cfg_entry_args::cat, cfg_entry_args::cat_name, cfg_entry_args::cfg, cfg_entry_args::flags, LOG_WARNING, RES_CONFIG_SQLITE_CONFIG_CATEGORY, RES_CONFIG_SQLITE_CONFIG_COLUMNS, RES_CONFIG_SQLITE_CONFIG_VAR_NAME, RES_CONFIG_SQLITE_CONFIG_VAR_VAL, var, and cfg_entry_args::who_asked.

Referenced by config_handler().

859 {
860  struct cfg_entry_args *args;
861  struct ast_variable *var;
862 
863  if (argc != RES_CONFIG_SQLITE_CONFIG_COLUMNS) {
864  ast_log(LOG_WARNING, "Corrupt table\n");
865  return 1;
866  }
867 
868  args = arg;
869 
870  if (!strcmp(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], "#include")) {
871  struct ast_config *cfg;
872  char *val;
873 
875  cfg = ast_config_internal_load(val, args->cfg, args->flags, "", args->who_asked);
876 
877  if (!cfg) {
878  ast_log(LOG_WARNING, "Unable to include %s\n", val);
879  return 1;
880  } else {
881  args->cfg = cfg;
882  return 0;
883  }
884  }
885 
886  if (!args->cat_name || strcmp(args->cat_name, argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY])) {
887  args->cat = ast_category_new(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY], "", 99999);
888 
889  if (!args->cat) {
890  ast_log(LOG_WARNING, "Unable to allocate category\n");
891  return 1;
892  }
893 
894  ast_free(args->cat_name);
895  args->cat_name = ast_strdup(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
896 
897  if (!args->cat_name) {
898  ast_category_destroy(args->cat);
899  return 1;
900  }
901 
902  ast_category_append(args->cfg, args->cat);
903  }
904 
905  var = ast_variable_new(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL], "");
906 
907  if (!var) {
908  ast_log(LOG_WARNING, "Unable to allocate variable\n");
909  return 1;
910  }
911 
912  ast_variable_append(args->cat, var);
913 
914  return 0;
915 }
struct ast_category * cat
struct ast_config * cfg
#define ast_strdup(a)
Definition: astmm.h:109
Definition: ast_expr2.c:325
struct ast_flags flags
#define LOG_WARNING
Definition: logger.h:144
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
struct ast_category * ast_category_new(const char *name, const char *in_file, int lineno)
Create a category structure.
Definition: config.c:673
void ast_category_destroy(struct ast_category *cat)
Definition: config.c:762
char * cat_name
static struct @350 args
void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
Definition: config.c:483
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
void ast_category_append(struct ast_config *config, struct ast_category *cat)
Definition: config.c:719
struct ast_config * ast_config_internal_load(const char *configfile, struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl_file, const char *who_asked)
Definition: config.c:2459
const char * who_asked
struct ast_variable * ast_variable_new(const char *name, const char *value, const char *filename)
Definition: config.c:278
static int add_rt_cfg_entry ( void *  arg,
int  argc,
char **  argv,
char **  columnNames 
)
static

SQLite callback function for RealTime configuration.

This function is passed to the SQLite engine as a callback function to parse a row and store it in a linked list of struct ast_variable objects.

Parameters
arga pointer to a struct rt_cfg_entry_args object
argcnumber of columns
argvvalues in the row
columnNamesnames and types of the columns
Return values
0on success.
1if an error occurred.
See Also
rt_cfg_entry_args
realtime_handler()

Definition at line 1006 of file res_config_sqlite.c.

References args, ast_variable_new(), rt_cfg_entry_args::last, ast_variable::next, rt_cfg_entry_args::var, and var.

Referenced by realtime_handler().

1007 {
1008  struct rt_cfg_entry_args *args;
1009  struct ast_variable *var;
1010  int i;
1011 
1012  args = arg;
1013 
1014  for (i = 0; i < argc; i++) {
1015  if (!argv[i])
1016  continue;
1017 
1018  if (!(var = ast_variable_new(columnNames[i], argv[i], "")))
1019  return 1;
1020 
1021  if (!args->var)
1022  args->var = var;
1023 
1024  if (!args->last)
1025  args->last = var;
1026  else {
1027  args->last->next = var;
1028  args->last = var;
1029  }
1030  }
1031 
1032  return 0;
1033 }
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
struct ast_variable * var
struct ast_variable * last
static struct @350 args
struct ast_variable * next
Definition: config.h:82
struct ast_variable * ast_variable_new(const char *name, const char *value, const char *filename)
Definition: config.c:278
static int add_rt_multi_cfg_entry ( void *  arg,
int  argc,
char **  argv,
char **  columnNames 
)
static

SQLite callback function for RealTime configuration.

This function performs the same actions as add_rt_cfg_entry() except that the rt_multi_cfg_entry_args structure is designed to store categories in addition to variables.

Parameters
arga pointer to a struct rt_multi_cfg_entry_args object
argcnumber of columns
argvvalues in the row
columnNamesnames and types of the columns
Return values
0on success.
1if an error occurred.
See Also
rt_multi_cfg_entry_args
realtime_multi_handler()

Definition at line 1125 of file res_config_sqlite.c.

References args, ast_category_append(), ast_category_new(), ast_log(), ast_variable_append(), ast_variable_new(), rt_multi_cfg_entry_args::cfg, rt_multi_cfg_entry_args::initfield, LOG_ERROR, LOG_WARNING, and var.

Referenced by realtime_multi_handler().

1126 {
1127  struct rt_multi_cfg_entry_args *args;
1128  struct ast_category *cat;
1129  struct ast_variable *var;
1130  char *cat_name;
1131  size_t i;
1132 
1133  args = arg;
1134  cat_name = NULL;
1135 
1136  /*
1137  * cat_name should always be set here, since initfield is forged from
1138  * params[0] in realtime_multi_handler(), which is a search parameter
1139  * of the SQL query.
1140  */
1141  for (i = 0; i < argc; i++) {
1142  if (!strcmp(args->initfield, columnNames[i]))
1143  cat_name = argv[i];
1144  }
1145 
1146  if (!cat_name) {
1147  ast_log(LOG_ERROR, "Bogus SQL results, cat_name is NULL !\n");
1148  return 1;
1149  }
1150 
1151  if (!(cat = ast_category_new(cat_name, "", 99999))) {
1152  ast_log(LOG_WARNING, "Unable to allocate category\n");
1153  return 1;
1154  }
1155 
1156  ast_category_append(args->cfg, cat);
1157 
1158  for (i = 0; i < argc; i++) {
1159  if (!argv[i]) {
1160  continue;
1161  }
1162 
1163  if (!(var = ast_variable_new(columnNames[i], argv[i], ""))) {
1164  ast_log(LOG_WARNING, "Unable to allocate variable\n");
1165  return 1;
1166  }
1167 
1168  ast_variable_append(cat, var);
1169  }
1170 
1171  return 0;
1172 }
char * initfield
#define LOG_WARNING
Definition: logger.h:144
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
struct ast_category * ast_category_new(const char *name, const char *in_file, int lineno)
Create a category structure.
Definition: config.c:673
struct ast_config * cfg
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
Definition: config.c:483
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
void ast_category_append(struct ast_config *config, struct ast_category *cat)
Definition: config.c:719
struct ast_variable * ast_variable_new(const char *name, const char *value, const char *filename)
Definition: config.c:278
static int cdr_handler ( struct ast_cdr cdr)
static

Asterisk callback function for CDR support.

Parameters
cdrthe CDR entry Asterisk sends us.

Asterisk will call this function each time a CDR entry must be logged if CDR support is enabled.

Return values
0on success
1if an error occurred

Definition at line 792 of file res_config_sqlite.c.

References ast_cdr_getvar(), ast_debug, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, AST_RWLIST_TRAVERSE, ast_str_append(), ast_str_buffer(), ast_str_create(), ast_str_set(), sqlite_cache_tables::columns, find_table(), first, sqlite_cache_columns::isint, LOG_ERROR, LOG_WARNING, mutex, sqlite_cache_columns::name, release_table, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

Referenced by load_module().

793 {
794  char *errormsg = NULL, *tmp, workspace[500];
795  int error, scannum;
797  struct sqlite_cache_columns *col;
798  struct ast_str *sql1 = ast_str_create(160), *sql2 = ast_str_create(16);
799  int first = 1;
800 
801  if (!tbl) {
802  ast_log(LOG_WARNING, "No such table: %s\n", cdr_table);
803  return -1;
804  }
805 
806  ast_str_set(&sql1, 0, "INSERT INTO %s (", cdr_table);
807  ast_str_set(&sql2, 0, ") VALUES (");
808 
809  AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
810  if (col->isint) {
811  ast_cdr_getvar(cdr, col->name, &tmp, workspace, sizeof(workspace), 0, 1);
812  if (!tmp) {
813  continue;
814  }
815  if (sscanf(tmp, "%30d", &scannum) == 1) {
816  ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
817  ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", scannum);
818  }
819  } else {
820  ast_cdr_getvar(cdr, col->name, &tmp, workspace, sizeof(workspace), 0, 0);
821  if (!tmp) {
822  continue;
823  }
824  ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
825  tmp = sqlite_mprintf("%Q", tmp);
826  ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", tmp);
827  sqlite_freemem(tmp);
828  }
829  first = 0;
830  }
831  release_table(tbl);
832 
833  ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
834  ast_free(sql2);
835 
836  ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql1));
837 
839 
841  error = sqlite_exec(db, ast_str_buffer(sql1), NULL, NULL, &errormsg);
842  RES_CONFIG_SQLITE_END(error)
843 
845 
846  ast_free(sql1);
847 
848  if (error) {
849  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
850  sqlite_freemem(errormsg);
851  return 1;
852  }
853  sqlite_freemem(errormsg);
854 
855  return 0;
856 }
static struct sqlite_cache_tables * find_table(const char *tablename)
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:144
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
#define release_table(a)
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:900
struct ast_str * ast_str_create(size_t init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:420
#define ast_mutex_lock(a)
Definition: lock.h:155
void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
Definition: cdr.c:264
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:874
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct sqlite_cache_tables::_columns columns
#define LOG_ERROR
Definition: logger.h:155
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
struct sla_ringing_trunk * first
Definition: app_meetme.c:965
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
static char * cdr_table
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int check_vars ( void  )
static

Definition at line 728 of file res_config_sqlite.c.

References ast_log(), and LOG_ERROR.

Referenced by load_config().

729 {
730  if (!dbfile) {
731  ast_log(LOG_ERROR, "Required parameter undefined: dbfile\n");
732  return 1;
733  }
734 
735  use_cdr = (cdr_table != NULL);
736 
737  return 0;
738 }
static int use_cdr
static char * dbfile
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static char * cdr_table
static struct ast_config * config_handler ( const char *  database,
const char *  table,
const char *  file,
struct ast_config cfg,
struct ast_flags  flags,
const char *  suggested_incl,
const char *  who_asked 
)
static

Asterisk callback function for static configuration.

Asterisk will call this function when it loads its static configuration, which usually happens at startup and reload.

Parameters
databasethe database to use (ignored)
tablethe table to use
filethe file to load from the database
cfgthe struct ast_config object to use when storing variables
flagsOptional flags. Not used.
suggested_inclsuggest include.
who_asked
Return values
cfgobject
NULLif an error occurred
See Also
add_cfg_entry()

Definition at line 917 of file res_config_sqlite.c.

References add_cfg_entry(), ast_debug, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, cfg_entry_args::cat, cfg_entry_args::cat_name, cfg_entry_args::cfg, config_table, cfg_entry_args::flags, LOG_ERROR, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, sql_get_config_table, and cfg_entry_args::who_asked.

919 {
920  struct cfg_entry_args args;
921  char *query, *errormsg = NULL;
922  int error;
923 
924  if (!config_table) {
925  if (!table) {
926  ast_log(LOG_ERROR, "Table name unspecified\n");
927  return NULL;
928  }
929  } else
931 
932  query = sqlite_mprintf(sql_get_config_table, table, file);
933 
934  if (!query) {
935  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
936  return NULL;
937  }
938 
939  ast_debug(1, "SQL query: %s\n", query);
940  args.cfg = cfg;
941  args.cat = NULL;
942  args.cat_name = NULL;
943  args.flags = flags;
944  args.who_asked = who_asked;
945 
947 
949  error = sqlite_exec(db, query, add_cfg_entry, &args, &errormsg);
950  RES_CONFIG_SQLITE_END(error)
951 
953 
955  sqlite_freemem(query);
956 
957  if (error) {
958  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
959  sqlite_freemem(errormsg);
960  return NULL;
961  }
962  sqlite_freemem(errormsg);
963 
964  return cfg;
965 }
struct ast_config * cfg
#define RES_CONFIG_SQLITE_BEGIN
struct ast_flags flags
#define LOG_WARNING
Definition: logger.h:144
#define sql_get_config_table
#define ast_mutex_lock(a)
Definition: lock.h:155
static char * table
Definition: cdr_odbc.c:50
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static char * config_table
static int add_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
SQLite callback function for static configuration.
#define LOG_ERROR
Definition: logger.h:155
char * cat_name
static struct @350 args
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define RES_CONFIG_SQLITE_END(error)
const char * who_asked
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static struct sqlite_cache_tables* find_table ( const char *  tablename)
static

Definition at line 644 of file res_config_sqlite.c.

References ast_asprintf, ast_calloc, ast_debug, ast_free, AST_LIST_EMPTY, ast_log(), ast_mutex_lock, ast_mutex_unlock, AST_RWLIST_HEAD_INIT, AST_RWLIST_INSERT_TAIL, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, sqlite_cache_tables::columns, find_table_cb(), free_table(), LOG_ERROR, LOG_WARNING, mutex, sqlite_cache_tables::name, and sql_table_structure.

Referenced by cdr_handler(), and realtime_require_handler().

645 {
646  struct sqlite_cache_tables *tblptr;
647  int i, err;
648  char *sql, *errstr = NULL;
649 
651 
652  for (i = 0; i < 2; i++) {
653  AST_RWLIST_TRAVERSE(&sqlite_tables, tblptr, list) {
654  if (strcmp(tblptr->name, tablename) == 0) {
655  break;
656  }
657  }
658  if (tblptr) {
659  AST_RWLIST_RDLOCK(&(tblptr->columns));
661  return tblptr;
662  }
663 
664  if (i == 0) {
667  }
668  }
669 
670  /* Table structure not cached; build the structure now */
671  if (ast_asprintf(&sql, sql_table_structure, tablename) < 0) {
672  sql = NULL;
673  }
674  if (!(tblptr = ast_calloc(1, sizeof(*tblptr) + strlen(tablename) + 1))) {
676  ast_log(LOG_ERROR, "Memory error. Cannot cache table '%s'\n", tablename);
677  ast_free(sql);
678  return NULL;
679  }
680  tblptr->name = (char *)tblptr + sizeof(*tblptr);
681  strcpy(tblptr->name, tablename); /* SAFE */
682  AST_RWLIST_HEAD_INIT(&(tblptr->columns));
683 
684  ast_debug(1, "About to query table structure: %s\n", sql);
685 
687  if ((err = sqlite_exec(db, sql, find_table_cb, tblptr, &errstr))) {
689  ast_log(LOG_WARNING, "SQLite error %d: %s\n", err, errstr);
690  ast_free(errstr);
691  free_table(tblptr);
693  ast_free(sql);
694  return NULL;
695  }
697  ast_free(sql);
698 
699  if (AST_LIST_EMPTY(&(tblptr->columns))) {
700  free_table(tblptr);
702  return NULL;
703  }
704 
705  AST_RWLIST_INSERT_TAIL(&sqlite_tables, tblptr, list);
706  AST_RWLIST_RDLOCK(&(tblptr->columns));
708  return tblptr;
709 }
static int find_table_cb(void *vtblptr, int argc, char **argv, char **columnNames)
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static void free_table(struct sqlite_cache_tables *tblptr)
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:449
#define ast_mutex_lock(a)
Definition: lock.h:155
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
Definition: linkedlists.h:624
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_asprintf(a, b, c...)
Definition: astmm.h:121
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define sql_table_structure
struct sqlite_cache_tables::_columns columns
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:726
#define ast_calloc(a, b)
Definition: astmm.h:82
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int find_table_cb ( void *  vtblptr,
int  argc,
char **  argv,
char **  columnNames 
)
static

Definition at line 589 of file res_config_sqlite.c.

References AST_APP_ARG, ast_calloc, ast_debug, AST_DECLARE_APP_ARGS, AST_LIST_INSERT_TAIL, AST_RWLIST_TRAVERSE, ast_skip_blanks(), AST_STANDARD_APP_ARGS, ast_strdupa, sqlite_cache_tables::columns, sqlite_cache_columns::isint, sqlite_cache_columns::name, strcasestr(), and sqlite_cache_columns::type.

Referenced by find_table().

590 {
591  struct sqlite_cache_tables *tblptr = vtblptr;
592  char *sql = ast_strdupa(argv[0]), *start, *end, *type, *remainder;
593  int i;
595  AST_APP_ARG(ld)[100]; /* This means we support up to 100 columns per table */
596  );
597  struct sqlite_cache_columns *col;
598 
599  /* This is really fun. We get to parse an SQL statement to figure out
600  * what columns are in the table.
601  */
602  if ((start = strchr(sql, '(')) && (end = strrchr(sql, ')'))) {
603  start++;
604  *end = '\0';
605  } else {
606  /* Abort */
607  return -1;
608  }
609 
610  AST_STANDARD_APP_ARGS(fie, start);
611  for (i = 0; i < fie.argc; i++) {
612  fie.ld[i] = ast_skip_blanks(fie.ld[i]);
613  ast_debug(5, "Found field: %s\n", fie.ld[i]);
614  if (strncasecmp(fie.ld[i], "PRIMARY KEY", 11) == 0 && (start = strchr(fie.ld[i], '(')) && (end = strchr(fie.ld[i], ')'))) {
615  *end = '\0';
616  AST_RWLIST_TRAVERSE(&(tblptr->columns), col, list) {
617  if (strcasecmp(start + 1, col->name) == 0 && strcasestr(col->type, "INTEGER")) {
618  col->isint = 1;
619  }
620  }
621  continue;
622  }
623  /* type delimiter could be any space character */
624  for (type = fie.ld[i]; *type > 32; type++);
625  *type++ = '\0';
626  type = ast_skip_blanks(type);
627  for (remainder = type; *remainder > 32; remainder++);
628  *remainder = '\0';
629  if (!(col = ast_calloc(1, sizeof(*col) + strlen(fie.ld[i]) + strlen(type) + 2))) {
630  return -1;
631  }
632  col->name = (char *)col + sizeof(*col);
633  col->type = (char *)col + sizeof(*col) + strlen(fie.ld[i]) + 1;
634  strcpy(col->name, fie.ld[i]); /* SAFE */
635  strcpy(col->type, type); /* SAFE */
636  if (strcasestr(col->type, "INTEGER") && strcasestr(col->type, "PRIMARY KEY")) {
637  col->isint = 1;
638  }
639  AST_LIST_INSERT_TAIL(&(tblptr->columns), col, list);
640  }
641  return 0;
642 }
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct sqlite_cache_tables::_columns columns
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:97
static const char type[]
Definition: chan_nbs.c:57
#define ast_calloc(a, b)
Definition: astmm.h:82
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
char * strcasestr(const char *, const char *)
static void free_table ( struct sqlite_cache_tables tblptr)
static

Definition at line 575 of file res_config_sqlite.c.

References ast_free, AST_RWLIST_HEAD_DESTROY, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and sqlite_cache_tables::columns.

Referenced by find_table(), realtime_unload_handler(), and unload_config().

576 {
577  struct sqlite_cache_columns *col;
578 
579  /* Obtain a write lock to ensure there are no read locks outstanding */
580  AST_RWLIST_WRLOCK(&(tblptr->columns));
581  while ((col = AST_RWLIST_REMOVE_HEAD(&(tblptr->columns), list))) {
582  ast_free(col);
583  }
584  AST_RWLIST_UNLOCK(&(tblptr->columns));
585  AST_RWLIST_HEAD_DESTROY(&(tblptr->columns));
586  ast_free(tblptr);
587 }
#define AST_RWLIST_HEAD_DESTROY(head)
Destroys an rwlist head structure.
Definition: linkedlists.h:652
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
struct sqlite_cache_tables::_columns columns
#define ast_free(a)
Definition: astmm.h:97
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:829
static size_t get_params ( va_list  ap,
const char ***  params_ptr,
const char ***  vals_ptr,
int  warn 
)
static

Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values.

ap must have the following format : param1 val1 param2 val2 param3 val3 ... arguments will be extracted to create 2 arrays:

  • params : param1 param2 param3 ...
  • vals : val1 val2 val3 ...

The address of these arrays are stored in params_ptr and vals_ptr. It is the responsibility of the caller to release the memory of these arrays. It is considered an error that va_list has a null or odd number of strings.

Parameters
apthe va_list object to parse
params_ptrwhere the address of the params array is stored
vals_ptrwhere the address of the vals array is stored
warn
Return values
thenumber of elements in the arrays (which have the same size).
0if an error occurred.

Definition at line 967 of file res_config_sqlite.c.

References ast_free, ast_log(), ast_realloc, and LOG_WARNING.

Referenced by auth_http_callback(), generic_http_callback(), realtime_destroy_handler(), realtime_handler(), realtime_multi_handler(), realtime_store_handler(), and realtime_update_handler().

968 {
969  const char **tmp, *param, *val, **params, **vals;
970  size_t params_count;
971 
972  params = NULL;
973  vals = NULL;
974  params_count = 0;
975 
976  while ((param = va_arg(ap, const char *)) && (val = va_arg(ap, const char *))) {
977  if (!(tmp = ast_realloc(params, (params_count + 1) * sizeof(char *)))) {
978  ast_free(params);
979  ast_free(vals);
980  return 0;
981  }
982  params = tmp;
983 
984  if (!(tmp = ast_realloc(vals, (params_count + 1) * sizeof(char *)))) {
985  ast_free(params);
986  ast_free(vals);
987  return 0;
988  }
989  vals = tmp;
990 
991  params[params_count] = param;
992  vals[params_count] = val;
993  params_count++;
994  }
995 
996  if (params_count > 0) {
997  *params_ptr = params;
998  *vals_ptr = vals;
999  } else if (warn) {
1000  ast_log(LOG_WARNING, "1 parameter and 1 value at least required\n");
1001  }
1002 
1003  return params_count;
1004 }
Definition: ast_expr2.c:325
#define LOG_WARNING
Definition: logger.h:144
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
#define ast_realloc(a, b)
Definition: astmm.h:103
static char * handle_cli_show_sqlite_status ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Asterisk callback function for the CLI status command.

Parameters
eCLI command
cmd
aCLI argument list
Returns
RESULT_SUCCESS

Definition at line 1671 of file res_config_sqlite.c.

References ast_cli_args::argc, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

1672 {
1673  switch (cmd) {
1674  case CLI_INIT:
1675  e->command = "sqlite show status";
1676  e->usage =
1677  "Usage: sqlite show status\n"
1678  " Show status information about the SQLite 2 driver\n";
1679  return NULL;
1680  case CLI_GENERATE:
1681  return NULL;
1682  }
1683 
1684  if (a->argc != 3)
1685  return CLI_SHOWUSAGE;
1686 
1687  ast_cli(a->fd, "SQLite database path: %s\n", dbfile);
1688  ast_cli(a->fd, "config_table: ");
1689 
1690  if (!config_table)
1691  ast_cli(a->fd, "unspecified, must be present in extconfig.conf\n");
1692  else
1693  ast_cli(a->fd, "%s\n", config_table);
1694 
1695  ast_cli(a->fd, "cdr_table: ");
1696 
1697  if (!cdr_table)
1698  ast_cli(a->fd, "unspecified, CDR support disabled\n");
1699  else
1700  ast_cli(a->fd, "%s\n", cdr_table);
1701 
1702  return CLI_SUCCESS;
1703 }
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
static char * dbfile
const int fd
Definition: cli.h:153
static char * config_table
#define CLI_SHOWUSAGE
Definition: cli.h:44
char * command
Definition: cli.h:180
static char * cdr_table
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
static char * handle_cli_sqlite_show_tables ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 1705 of file res_config_sqlite.c.

References ast_cli_args::argc, ast_cli(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, sqlite_cache_tables::columns, ast_cli_entry::command, ast_cli_args::fd, sqlite_cache_columns::name, sqlite_cache_tables::name, sqlite_cache_columns::type, and ast_cli_entry::usage.

1706 {
1707  struct sqlite_cache_tables *tbl;
1708  struct sqlite_cache_columns *col;
1709  int found = 0;
1710 
1711  switch (cmd) {
1712  case CLI_INIT:
1713  e->command = "sqlite show tables";
1714  e->usage =
1715  "Usage: sqlite show tables\n"
1716  " Show table information about the SQLite 2 driver\n";
1717  return NULL;
1718  case CLI_GENERATE:
1719  return NULL;
1720  }
1721 
1722  if (a->argc != 3)
1723  return CLI_SHOWUSAGE;
1724 
1726  AST_RWLIST_TRAVERSE(&sqlite_tables, tbl, list) {
1727  found++;
1728  ast_cli(a->fd, "Table %s:\n", tbl->name);
1729  AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
1730  fprintf(stderr, "%s\n", col->name);
1731  ast_cli(a->fd, " %20.20s %-30.30s\n", col->name, col->type);
1732  }
1733  }
1735 
1736  if (!found) {
1737  ast_cli(a->fd, "No tables currently in cache\n");
1738  }
1739 
1740  return CLI_SUCCESS;
1741 }
const int argc
Definition: cli.h:154
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
const int fd
Definition: cli.h:153
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct sqlite_cache_tables::_columns columns
#define CLI_SHOWUSAGE
Definition: cli.h:44
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
static int load_config ( void  )
static

Load the configuration file.

See Also
unload_config()

This function sets dbfile, config_table, and cdr_table. It calls check_vars() before returning, and unload_config() if an error occurred.

Return values
0on success
1if an error occurred

Definition at line 740 of file res_config_sqlite.c.

References ast_config_destroy(), ast_config_load, ast_log(), ast_variable_browse(), check_vars(), config, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, LOG_ERROR, LOG_WARNING, ast_variable::name, ast_variable::next, RES_CONFIG_SQLITE_CONF_FILE, SET_VAR, unload_config(), and var.

Referenced by load_module().

741 {
742  struct ast_config *config;
743  struct ast_variable *var;
744  int error;
745  struct ast_flags config_flags = { 0 };
746 
747  config = ast_config_load(RES_CONFIG_SQLITE_CONF_FILE, config_flags);
748 
749  if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
750  ast_log(LOG_ERROR, "Unable to load " RES_CONFIG_SQLITE_CONF_FILE "\n");
751  return 1;
752  }
753 
754  for (var = ast_variable_browse(config, "general"); var; var = var->next) {
755  if (!strcasecmp(var->name, "dbfile"))
756  SET_VAR(config, dbfile, var);
757  else if (!strcasecmp(var->name, "config_table"))
758  SET_VAR(config, config_table, var);
759  else if (!strcasecmp(var->name, "cdr_table")) {
760  SET_VAR(config, cdr_table, var);
761  } else
762  ast_log(LOG_WARNING, "Unknown parameter : %s\n", var->name);
763  }
764 
765  ast_config_destroy(config);
766  error = check_vars();
767 
768  if (error) {
769  unload_config();
770  return 1;
771  }
772 
773  return 0;
774 }
#define RES_CONFIG_SQLITE_CONF_FILE
static const char config[]
Definition: cdr_csv.c:57
#define LOG_WARNING
Definition: logger.h:144
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
static void unload_config(void)
Free resources related to configuration.
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
static char * dbfile
#define CONFIG_STATUS_FILEMISSING
Definition: config.h:50
static char * config_table
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
const char * name
Definition: config.h:77
static int check_vars(void)
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static char * cdr_table
Structure used to handle boolean flags.
Definition: utils.h:200
#define SET_VAR(config, to, from)
struct ast_variable * next
Definition: config.h:82
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
static int load_module ( void  )
static

Definition at line 1761 of file res_config_sqlite.c.

References ARRAY_LEN, ast_cdr_register(), ast_cli_register_multiple(), ast_config_engine_register(), ast_debug, ast_log(), AST_MODULE_LOAD_DECLINE, cdr_handler(), load_config(), LOG_ERROR, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_DESCRIPTION, RES_CONFIG_SQLITE_END, RES_CONFIG_SQLITE_NAME, S_OR, sql_create_cdr_table, and unload_module().

1762 {
1763  char *errormsg = NULL;
1764  int error;
1765 
1766  db = NULL;
1767  cdr_registered = 0;
1769  dbfile = NULL;
1770  config_table = NULL;
1771  cdr_table = NULL;
1772  error = load_config();
1773 
1774  if (error)
1775  return AST_MODULE_LOAD_DECLINE;
1776 
1777  if (!(db = sqlite_open(dbfile, 0660, &errormsg))) {
1778  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1779  sqlite_freemem(errormsg);
1780  unload_module();
1781  return 1;
1782  }
1783 
1784  sqlite_freemem(errormsg);
1785  errormsg = NULL;
1787 
1788  if (use_cdr) {
1789  char *query;
1790 
1791 /* \cond DOXYGEN_CAN_PARSE_THIS */
1792 #undef QUERY
1793 #define QUERY "SELECT COUNT(id) FROM %Q;"
1794 /* \endcond */
1795 
1796  query = sqlite_mprintf(QUERY, cdr_table);
1797 
1798  if (!query) {
1799  ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
1800  unload_module();
1801  return 1;
1802  }
1803 
1804  ast_debug(1, "SQL query: %s\n", query);
1805 
1807  error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1808  RES_CONFIG_SQLITE_END(error)
1809 
1810  sqlite_freemem(query);
1811 
1812  if (error) {
1813  /*
1814  * Unexpected error.
1815  */
1816  if (error != SQLITE_ERROR) {
1817  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1818  sqlite_freemem(errormsg);
1819  unload_module();
1820  return 1;
1821  }
1822 
1823  sqlite_freemem(errormsg);
1824  errormsg = NULL;
1825  query = sqlite_mprintf(sql_create_cdr_table, cdr_table);
1826 
1827  if (!query) {
1828  ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
1829  unload_module();
1830  return 1;
1831  }
1832 
1833  ast_debug(1, "SQL query: %s\n", query);
1834 
1836  error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1837  RES_CONFIG_SQLITE_END(error)
1838 
1839  sqlite_freemem(query);
1840 
1841  if (error) {
1842  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1843  sqlite_freemem(errormsg);
1844  unload_module();
1845  return 1;
1846  }
1847  }
1848  sqlite_freemem(errormsg);
1849  errormsg = NULL;
1850 
1852 
1853  if (error) {
1854  unload_module();
1855  return 1;
1856  }
1857 
1858  cdr_registered = 1;
1859  }
1860 
1862 
1863  if (error) {
1864  unload_module();
1865  return 1;
1866  }
1867 
1869 
1870  return 0;
1871 }
#define RES_CONFIG_SQLITE_NAME
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static int unload_module(void)
#define RES_CONFIG_SQLITE_BEGIN
static int use_cdr
static struct ast_config_engine sqlite_engine
static char * dbfile
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:130
static int cli_status_registered
static char * config_table
static struct ast_cli_entry cli_status[]
static int cdr_registered
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_config_engine_register(struct ast_config_engine *newconfig)
Register config engine.
Definition: config.c:2378
static char * cdr_table
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
static char * sql_create_cdr_table
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define RES_CONFIG_SQLITE_DESCRIPTION
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
static int load_config(void)
Load the configuration file.
#define RES_CONFIG_SQLITE_END(error)
static int cdr_handler(struct ast_cdr *cdr)
Asterisk callback function for CDR support.
static sqlite * db
static int realtime_destroy_handler ( const char *  database,
const char *  table,
const char *  keyfield,
const char *  entity,
va_list  ap 
)
static

Asterisk callback function for RealTime configuration (destroys variable).

Asterisk will call this function each time a variable has been destroyed internally and must be removed from the backend engine. keyfield and entity are used to find the row to delete, e.g. DELETE FROM table WHERE keyfield = 'entity';. ap is a list of parameters and values with the same format as the other realtime functions.

Parameters
databasethe database to use (ignored)
tablethe table to use
keyfieldthe column of the matching cell
entitythe value of the matching cell
aplist of additional parameters for cell matching
Return values
thenumber of affected rows.
-1if an error occurred.

Definition at line 1543 of file res_config_sqlite.c.

References ast_debug, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, get_params(), LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

1545 {
1546  char *query, *errormsg = NULL, *tmp_str;
1547  const char **params = NULL, **vals = NULL;
1548  size_t params_count;
1549  int error, rows_num;
1550  size_t i;
1551 
1552  if (!table) {
1553  ast_log(LOG_WARNING, "Table name unspecified\n");
1554  return -1;
1555  }
1556 
1557  params_count = get_params(ap, &params, &vals, 0);
1558 
1559 /* \cond DOXYGEN_CAN_PARSE_THIS */
1560 #undef QUERY
1561 #define QUERY "DELETE FROM '%q' WHERE"
1562 /* \endcond */
1563 
1564  if (!(query = sqlite_mprintf(QUERY, table))) {
1565  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1566  ast_free(params);
1567  ast_free(vals);
1568  return -1;
1569  }
1570 
1571  for (i = 0; i < params_count; i++) {
1572  tmp_str = sqlite_mprintf("%s %q = '%q' AND", query, params[i], vals[i]);
1573  sqlite_freemem(query);
1574 
1575  if (!tmp_str) {
1576  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1577  ast_free(params);
1578  ast_free(vals);
1579  return -1;
1580  }
1581 
1582  query = tmp_str;
1583  }
1584 
1585  ast_free(params);
1586  ast_free(vals);
1587  if (!(tmp_str = sqlite_mprintf("%s %q = '%q';", query, keyfield, entity))) {
1588  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1589  sqlite_freemem(query);
1590  return -1;
1591  }
1592  sqlite_freemem(query);
1593  query = tmp_str;
1594  ast_debug(1, "SQL query: %s\n", query);
1595 
1597 
1599  error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1600  RES_CONFIG_SQLITE_END(error)
1601 
1602  if (!error) {
1603  rows_num = sqlite_changes(db);
1604  } else {
1605  rows_num = -1;
1606  }
1607 
1609 
1610  sqlite_freemem(query);
1611 
1612  if (error) {
1613  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1614  }
1615  sqlite_freemem(errormsg);
1616 
1617  return rows_num;
1618 }
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:144
static int entity
Definition: isdn_lib.c:259
#define ast_mutex_lock(a)
Definition: lock.h:155
static char * table
Definition: cdr_odbc.c:50
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
static size_t get_params(va_list ap, const char ***params_ptr, const char ***vals_ptr, int warn)
Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values...
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static struct ast_variable * realtime_handler ( const char *  database,
const char *  table,
va_list  ap 
)
static

Asterisk callback function for RealTime configuration.

Asterisk will call this function each time it requires a variable through the RealTime architecture. ap is a list of parameters and values used to find a specific row, e.g one parameter "name" and one value "123" so that the SQL query becomes SELECT * FROM table WHERE name = '123';.

Parameters
databasethe database to use (ignored)
tablethe table to use
aplist of parameters and values to match
Return values
alinked list of struct ast_variable objects
NULLif an error occurred
See Also
add_rt_cfg_entry()

Definition at line 1035 of file res_config_sqlite.c.

References add_rt_cfg_entry(), ast_debug, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_variables_destroy(), get_params(), rt_cfg_entry_args::last, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, and rt_cfg_entry_args::var.

1036 {
1037  char *query, *errormsg = NULL, *op, *tmp_str;
1038  struct rt_cfg_entry_args args;
1039  const char **params, **vals;
1040  size_t params_count;
1041  int error;
1042 
1043  if (!table) {
1044  ast_log(LOG_WARNING, "Table name unspecified\n");
1045  return NULL;
1046  }
1047 
1048  params_count = get_params(ap, &params, &vals, 1);
1049 
1050  if (params_count == 0)
1051  return NULL;
1052 
1053  op = (strchr(params[0], ' ') == NULL) ? " =" : "";
1054 
1055 /* \cond DOXYGEN_CAN_PARSE_THIS */
1056 #undef QUERY
1057 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
1058 /* \endcond */
1059 
1060  query = sqlite_mprintf(QUERY, table, (config_table && !strcmp(config_table, table)) ? " commented = 0 AND" : "", params[0], op, vals[0]);
1061 
1062  if (!query) {
1063  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1064  ast_free(params);
1065  ast_free(vals);
1066  return NULL;
1067  }
1068 
1069  if (params_count > 1) {
1070  size_t i;
1071 
1072  for (i = 1; i < params_count; i++) {
1073  op = (strchr(params[i], ' ') == NULL) ? " =" : "";
1074  tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op, vals[i]);
1075  sqlite_freemem(query);
1076 
1077  if (!tmp_str) {
1078  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1079  ast_free(params);
1080  ast_free(vals);
1081  return NULL;
1082  }
1083 
1084  query = tmp_str;
1085  }
1086  }
1087 
1088  ast_free(params);
1089  ast_free(vals);
1090 
1091  tmp_str = sqlite_mprintf("%s LIMIT 1;", query);
1092  sqlite_freemem(query);
1093 
1094  if (!tmp_str) {
1095  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1096  return NULL;
1097  }
1098 
1099  query = tmp_str;
1100  ast_debug(1, "SQL query: %s\n", query);
1101  args.var = NULL;
1102  args.last = NULL;
1103 
1105 
1107  error = sqlite_exec(db, query, add_rt_cfg_entry, &args, &errormsg);
1108  RES_CONFIG_SQLITE_END(error)
1109 
1111 
1112  sqlite_freemem(query);
1113 
1114  if (error) {
1115  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1116  sqlite_freemem(errormsg);
1118  return NULL;
1119  }
1120  sqlite_freemem(errormsg);
1121 
1122  return args.var;
1123 }
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:144
static int add_rt_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
SQLite callback function for RealTime configuration.
#define ast_mutex_lock(a)
Definition: lock.h:155
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: config.c:586
static char * table
Definition: cdr_odbc.c:50
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static char * config_table
static struct @350 args
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
static size_t get_params(va_list ap, const char ***params_ptr, const char ***vals_ptr, int warn)
Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values...
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static struct ast_config * realtime_multi_handler ( const char *  database,
const char *  table,
va_list  ap 
)
static

Asterisk callback function for RealTime configuration.

This function performs the same actions as realtime_handler() except that it can store variables per category, and can return several categories.

Parameters
databasethe database to use (ignored)
tablethe table to use
aplist of parameters and values to match
Return values
astruct ast_config object storing categories and variables.
NULLif an error occurred.
See Also
add_rt_multi_cfg_entry()

Definition at line 1174 of file res_config_sqlite.c.

References add_rt_multi_cfg_entry(), ast_config_destroy(), ast_config_new(), ast_debug, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_strdup, rt_multi_cfg_entry_args::cfg, get_params(), rt_multi_cfg_entry_args::initfield, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

1176 {
1177  char *query, *errormsg = NULL, *op, *tmp_str, *initfield;
1179  const char **params, **vals;
1180  struct ast_config *cfg;
1181  size_t params_count;
1182  int error;
1183 
1184  if (!table) {
1185  ast_log(LOG_WARNING, "Table name unspecified\n");
1186  return NULL;
1187  }
1188 
1189  if (!(cfg = ast_config_new())) {
1190  ast_log(LOG_WARNING, "Unable to allocate configuration structure\n");
1191  return NULL;
1192  }
1193 
1194  if (!(params_count = get_params(ap, &params, &vals, 1))) {
1195  ast_config_destroy(cfg);
1196  return NULL;
1197  }
1198 
1199  if (!(initfield = ast_strdup(params[0]))) {
1200  ast_config_destroy(cfg);
1201  ast_free(params);
1202  ast_free(vals);
1203  return NULL;
1204  }
1205 
1206  tmp_str = strchr(initfield, ' ');
1207 
1208  if (tmp_str)
1209  *tmp_str = '\0';
1210 
1211  op = (!strchr(params[0], ' ')) ? " =" : "";
1212 
1213  /*
1214  * Asterisk sends us an already escaped string when searching for
1215  * "exten LIKE" (uh!). Handle it separately.
1216  */
1217  tmp_str = (!strcmp(vals[0], "\\_%")) ? "_%" : (char *)vals[0];
1218 
1219 /* \cond DOXYGEN_CAN_PARSE_THIS */
1220 #undef QUERY
1221 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
1222 /* \endcond */
1223 
1224  if (!(query = sqlite_mprintf(QUERY, table, (config_table && !strcmp(config_table, table)) ? " commented = 0 AND" : "", params[0], op, tmp_str))) {
1225  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1226  ast_config_destroy(cfg);
1227  ast_free(params);
1228  ast_free(vals);
1229  ast_free(initfield);
1230  return NULL;
1231  }
1232 
1233  if (params_count > 1) {
1234  size_t i;
1235 
1236  for (i = 1; i < params_count; i++) {
1237  op = (!strchr(params[i], ' ')) ? " =" : "";
1238  tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, params[i], op, vals[i]);
1239  sqlite_freemem(query);
1240 
1241  if (!tmp_str) {
1242  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1243  ast_config_destroy(cfg);
1244  ast_free(params);
1245  ast_free(vals);
1246  ast_free(initfield);
1247  return NULL;
1248  }
1249 
1250  query = tmp_str;
1251  }
1252  }
1253 
1254  ast_free(params);
1255  ast_free(vals);
1256 
1257  if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
1258  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1259  sqlite_freemem(query);
1260  ast_config_destroy(cfg);
1261  ast_free(initfield);
1262  return NULL;
1263  }
1264 
1265  sqlite_freemem(query);
1266  query = tmp_str;
1267  ast_debug(1, "SQL query: %s\n", query);
1268  args.cfg = cfg;
1269  args.initfield = initfield;
1270 
1272 
1274  error = sqlite_exec(db, query, add_rt_multi_cfg_entry, &args, &errormsg);
1275  RES_CONFIG_SQLITE_END(error)
1276 
1278 
1279  sqlite_freemem(query);
1280  ast_free(initfield);
1281 
1282  if (error) {
1283  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1284  sqlite_freemem(errormsg);
1285  ast_config_destroy(cfg);
1286  return NULL;
1287  }
1288  sqlite_freemem(errormsg);
1289 
1290  return cfg;
1291 }
#define ast_strdup(a)
Definition: astmm.h:109
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:144
#define ast_mutex_lock(a)
Definition: lock.h:155
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
static char * table
Definition: cdr_odbc.c:50
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static char * config_table
static struct @350 args
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
struct ast_config * ast_config_new(void)
Create a new base configuration structure.
Definition: config.c:888
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
static size_t get_params(va_list ap, const char ***params_ptr, const char ***vals_ptr, int warn)
Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values...
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
SQLite callback function for RealTime configuration.
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int realtime_require_handler ( const char *  database,
const char *  table,
va_list  ap 
)
static

Definition at line 1620 of file res_config_sqlite.c.

References ast_log(), ast_rq_is_int(), AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, sqlite_cache_tables::columns, find_table(), sqlite_cache_columns::isint, LOG_WARNING, sqlite_cache_columns::name, and sqlite_cache_columns::type.

1621 {
1622  struct sqlite_cache_tables *tbl = find_table(tablename);
1623  struct sqlite_cache_columns *col;
1624  char *elm;
1625  int type, res = 0;
1626 
1627  if (!tbl) {
1628  return -1;
1629  }
1630 
1631  while ((elm = va_arg(ap, char *))) {
1632  type = va_arg(ap, require_type);
1633  va_arg(ap, int);
1634  /* Check if the field matches the criteria */
1635  AST_RWLIST_TRAVERSE(&tbl->columns, col, list) {
1636  if (strcmp(col->name, elm) == 0) {
1637  /* SQLite only has two types - the 32-bit integer field that
1638  * is the key column, and everything else (everything else
1639  * being a string).
1640  */
1641  if (col->isint && !ast_rq_is_int(type)) {
1642  ast_log(LOG_WARNING, "Realtime table %s: column '%s' is an integer field, but Asterisk requires that it not be!\n", tablename, col->name);
1643  res = -1;
1644  }
1645  break;
1646  }
1647  }
1648  if (!col) {
1649  ast_log(LOG_WARNING, "Realtime table %s requires column '%s', but that column does not exist!\n", tablename, elm);
1650  }
1651  }
1652  AST_RWLIST_UNLOCK(&(tbl->columns));
1653  return res;
1654 }
static struct sqlite_cache_tables * find_table(const char *tablename)
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct sqlite_cache_tables::_columns columns
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static const char type[]
Definition: chan_nbs.c:57
int ast_rq_is_int(require_type type)
Check if require type is an integer type.
Definition: config.h:768
require_type
Types used in ast_realtime_require_field.
Definition: config.h:57
static int realtime_store_handler ( const char *  database,
const char *  table,
va_list  ap 
)
static

Asterisk callback function for RealTime configuration (variable create/store).

Asterisk will call this function each time a variable has been created internally and must be stored in the backend engine. are used to find the row to update, e.g. ap is a list of parameters and values with the same format as the other realtime functions.

Parameters
databasethe database to use (ignored)
tablethe table to use
aplist of parameters and new values to insert into the database
Return values
therowid of inserted row.
-1if an error occurred.

Definition at line 1449 of file res_config_sqlite.c.

References ast_debug, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, get_params(), LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

1450 {
1451  char *errormsg = NULL, *tmp_str, *tmp_keys = NULL, *tmp_keys2 = NULL, *tmp_vals = NULL, *tmp_vals2 = NULL;
1452  const char **params, **vals;
1453  size_t params_count;
1454  int error, rows_id;
1455  size_t i;
1456 
1457  if (!table) {
1458  ast_log(LOG_WARNING, "Table name unspecified\n");
1459  return -1;
1460  }
1461 
1462  if (!(params_count = get_params(ap, &params, &vals, 1)))
1463  return -1;
1464 
1465 /* \cond DOXYGEN_CAN_PARSE_THIS */
1466 #undef QUERY
1467 #define QUERY "INSERT into '%q' (%s) VALUES (%s);"
1468 /* \endcond */
1469 
1470  for (i = 0; i < params_count; i++) {
1471  if ( tmp_keys2 ) {
1472  tmp_keys = sqlite_mprintf("%s, %q", tmp_keys2, params[i]);
1473  sqlite_freemem(tmp_keys2);
1474  } else {
1475  tmp_keys = sqlite_mprintf("%q", params[i]);
1476  }
1477  if (!tmp_keys) {
1478  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1479  sqlite_freemem(tmp_vals);
1480  ast_free(params);
1481  ast_free(vals);
1482  return -1;
1483  }
1484 
1485  if ( tmp_vals2 ) {
1486  tmp_vals = sqlite_mprintf("%s, '%q'", tmp_vals2, vals[i]);
1487  sqlite_freemem(tmp_vals2);
1488  } else {
1489  tmp_vals = sqlite_mprintf("'%q'", vals[i]);
1490  }
1491  if (!tmp_vals) {
1492  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1493  sqlite_freemem(tmp_keys);
1494  ast_free(params);
1495  ast_free(vals);
1496  return -1;
1497  }
1498 
1499 
1500  tmp_keys2 = tmp_keys;
1501  tmp_vals2 = tmp_vals;
1502  }
1503 
1504  ast_free(params);
1505  ast_free(vals);
1506 
1507  if (!(tmp_str = sqlite_mprintf(QUERY, table, tmp_keys, tmp_vals))) {
1508  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1509  sqlite_freemem(tmp_keys);
1510  sqlite_freemem(tmp_vals);
1511  return -1;
1512  }
1513 
1514  sqlite_freemem(tmp_keys);
1515  sqlite_freemem(tmp_vals);
1516 
1517  ast_debug(1, "SQL query: %s\n", tmp_str);
1518 
1520 
1522  error = sqlite_exec(db, tmp_str, NULL, NULL, &errormsg);
1523  RES_CONFIG_SQLITE_END(error)
1524 
1525  if (!error) {
1526  rows_id = sqlite_last_insert_rowid(db);
1527  } else {
1528  rows_id = -1;
1529  }
1530 
1532 
1533  sqlite_freemem(tmp_str);
1534 
1535  if (error) {
1536  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1537  }
1538  sqlite_freemem(errormsg);
1539 
1540  return rows_id;
1541 }
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:144
#define ast_mutex_lock(a)
Definition: lock.h:155
static char * table
Definition: cdr_odbc.c:50
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
static size_t get_params(va_list ap, const char ***params_ptr, const char ***vals_ptr, int warn)
Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values...
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int realtime_unload_handler ( const char *  unused,
const char *  tablename 
)
static

Definition at line 1656 of file res_config_sqlite.c.

References AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, free_table(), and sqlite_cache_tables::name.

1657 {
1658  struct sqlite_cache_tables *tbl;
1661  if (!strcasecmp(tbl->name, tablename)) {
1663  free_table(tbl);
1664  }
1665  }
1668  return 0;
1669 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static void free_table(struct sqlite_cache_tables *tblptr)
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:565
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:542
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:602
static int realtime_update2_handler ( const char *  database,
const char *  table,
va_list  ap 
)
static

Definition at line 1375 of file res_config_sqlite.c.

References ast_debug, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_str_append(), ast_str_buffer(), ast_str_set(), ast_str_thread_get(), first, LOG_ERROR, LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, sql_buf, value, and where_buf.

1377 {
1378  char *errormsg = NULL, *tmp1, *tmp2;
1379  int error, rows_num, first = 1;
1380  struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
1381  struct ast_str *where = ast_str_thread_get(&where_buf, 100);
1382  const char *param, *value;
1383 
1384  if (!table) {
1385  ast_log(LOG_WARNING, "Table name unspecified\n");
1386  return -1;
1387  }
1388 
1389  if (!sql) {
1390  return -1;
1391  }
1392 
1393  ast_str_set(&sql, 0, "UPDATE %s SET", table);
1394  ast_str_set(&where, 0, " WHERE");
1395 
1396  while ((param = va_arg(ap, const char *))) {
1397  value = va_arg(ap, const char *);
1398  ast_str_append(&where, 0, "%s %s = %s",
1399  first ? "" : " AND",
1400  tmp1 = sqlite_mprintf("%q", param),
1401  tmp2 = sqlite_mprintf("%Q", value));
1402  sqlite_freemem(tmp1);
1403  sqlite_freemem(tmp2);
1404  first = 0;
1405  }
1406 
1407  if (first) {
1408  ast_log(LOG_ERROR, "No criteria specified on update to '%s@%s'!\n", table, database);
1409  return -1;
1410  }
1411 
1412  first = 1;
1413  while ((param = va_arg(ap, const char *))) {
1414  value = va_arg(ap, const char *);
1415  ast_str_append(&sql, 0, "%s %s = %s",
1416  first ? "" : ",",
1417  tmp1 = sqlite_mprintf("%q", param),
1418  tmp2 = sqlite_mprintf("%Q", value));
1419  sqlite_freemem(tmp1);
1420  sqlite_freemem(tmp2);
1421  first = 0;
1422  }
1423 
1424  ast_str_append(&sql, 0, " %s", ast_str_buffer(where));
1425  ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql));
1426 
1428 
1430  error = sqlite_exec(db, ast_str_buffer(sql), NULL, NULL, &errormsg);
1431  RES_CONFIG_SQLITE_END(error)
1432 
1433  if (!error) {
1434  rows_num = sqlite_changes(db);
1435  } else {
1436  rows_num = -1;
1437  }
1438 
1440 
1441  if (error) {
1442  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1443  }
1444  sqlite_freemem(errormsg);
1445 
1446  return rows_num;
1447 }
static struct ast_threadstorage where_buf
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:144
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
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:900
static struct ast_threadstorage sql_buf
#define ast_mutex_lock(a)
Definition: lock.h:155
int value
Definition: syslog.c:39
static char * table
Definition: cdr_odbc.c:50
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:874
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define LOG_ERROR
Definition: logger.h:155
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
struct sla_ringing_trunk * first
Definition: app_meetme.c:965
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define RES_CONFIG_SQLITE_END(error)
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:669
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int realtime_update_handler ( const char *  database,
const char *  table,
const char *  keyfield,
const char *  entity,
va_list  ap 
)
static

Asterisk callback function for RealTime configuration (variable update).

Asterisk will call this function each time a variable has been modified internally and must be updated in the backend engine. keyfield and entity are used to find the row to update, e.g. UPDATE table SET ... WHERE keyfield = 'entity';. ap is a list of parameters and values with the same format as the other realtime functions.

Parameters
databasethe database to use (ignored)
tablethe table to use
keyfieldthe column of the matching cell
entitythe value of the matching cell
aplist of parameters and new values to update in the database
Return values
thenumber of affected rows.
-1if an error occurred.

Definition at line 1293 of file res_config_sqlite.c.

References ast_debug, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, get_params(), LOG_WARNING, mutex, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, and S_OR.

1295 {
1296  char *query, *errormsg = NULL, *tmp_str;
1297  const char **params, **vals;
1298  size_t params_count;
1299  int error, rows_num;
1300 
1301  if (!table) {
1302  ast_log(LOG_WARNING, "Table name unspecified\n");
1303  return -1;
1304  }
1305 
1306  if (!(params_count = get_params(ap, &params, &vals, 1)))
1307  return -1;
1308 
1309 /* \cond DOXYGEN_CAN_PARSE_THIS */
1310 #undef QUERY
1311 #define QUERY "UPDATE '%q' SET %q = '%q'"
1312 /* \endcond */
1313 
1314  if (!(query = sqlite_mprintf(QUERY, table, params[0], vals[0]))) {
1315  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1316  ast_free(params);
1317  ast_free(vals);
1318  return -1;
1319  }
1320 
1321  if (params_count > 1) {
1322  size_t i;
1323 
1324  for (i = 1; i < params_count; i++) {
1325  tmp_str = sqlite_mprintf("%s, %q = '%q'", query, params[i], vals[i]);
1326  sqlite_freemem(query);
1327 
1328  if (!tmp_str) {
1329  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1330  ast_free(params);
1331  ast_free(vals);
1332  return -1;
1333  }
1334 
1335  query = tmp_str;
1336  }
1337  }
1338 
1339  ast_free(params);
1340  ast_free(vals);
1341 
1342  if (!(tmp_str = sqlite_mprintf("%s WHERE %q = '%q';", query, keyfield, entity))) {
1343  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1344  sqlite_freemem(query);
1345  return -1;
1346  }
1347 
1348  sqlite_freemem(query);
1349  query = tmp_str;
1350  ast_debug(1, "SQL query: %s\n", query);
1351 
1353 
1355  error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1356  RES_CONFIG_SQLITE_END(error)
1357 
1358  if (!error)
1359  rows_num = sqlite_changes(db);
1360  else
1361  rows_num = -1;
1362 
1364 
1365  sqlite_freemem(query);
1366 
1367  if (error) {
1368  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1369  }
1370  sqlite_freemem(errormsg);
1371 
1372  return rows_num;
1373 }
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:144
static int entity
Definition: isdn_lib.c:259
#define ast_mutex_lock(a)
Definition: lock.h:155
static char * table
Definition: cdr_odbc.c:50
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
static size_t get_params(va_list ap, const char ***params_ptr, const char ***vals_ptr, int warn)
Helper function to parse a va_list object into 2 dynamic arrays of strings, parameters and values...
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define RES_CONFIG_SQLITE_END(error)
else
Definition: ast_expr2.c:1975
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int set_var ( char **  var,
const char *  name,
const char *  value 
)
static

Allocate a variable.

Parameters
varthe address of the variable to set (it will be allocated)
namethe name of the variable (for error handling)
valuethe value to store in var
Return values
0on success
1if an allocation error occurred

Definition at line 713 of file res_config_sqlite.c.

References ast_free, ast_log(), ast_strdup, and LOG_WARNING.

714 {
715  if (*var)
716  ast_free(*var);
717 
718  *var = ast_strdup(value);
719 
720  if (!*var) {
721  ast_log(LOG_WARNING, "Unable to allocate variable %s\n", name);
722  return 1;
723  }
724 
725  return 0;
726 }
#define ast_strdup(a)
Definition: astmm.h:109
#define LOG_WARNING
Definition: logger.h:144
#define var
Definition: ast_expr2f.c:606
int value
Definition: syslog.c:39
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static const char name[]
#define ast_free(a)
Definition: astmm.h:97
static void unload_config ( void  )
static

Free resources related to configuration.

See Also
load_config()

Definition at line 776 of file res_config_sqlite.c.

References ast_free, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and free_table().

Referenced by load_config(), and unload_module().

777 {
778  struct sqlite_cache_tables *tbl;
779  ast_free(dbfile);
780  dbfile = NULL;
782  config_table = NULL;
784  cdr_table = NULL;
786  while ((tbl = AST_RWLIST_REMOVE_HEAD(&sqlite_tables, list))) {
787  free_table(tbl);
788  }
790 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static void free_table(struct sqlite_cache_tables *tblptr)
static char * dbfile
static char * config_table
#define ast_free(a)
Definition: astmm.h:97
static char * cdr_table
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:829
static int unload_module ( void  )
static

Definition at line 1743 of file res_config_sqlite.c.

References ARRAY_LEN, ast_cdr_unregister(), ast_cli_unregister_multiple(), ast_config_engine_deregister(), RES_CONFIG_SQLITE_NAME, and unload_config().

Referenced by load_module().

1744 {
1747 
1748  if (cdr_registered)
1750 
1752 
1753  if (db)
1754  sqlite_close(db);
1755 
1756  unload_config();
1757 
1758  return 0;
1759 }
#define RES_CONFIG_SQLITE_NAME
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
static void unload_config(void)
Free resources related to configuration.
static struct ast_config_engine sqlite_engine
static int cli_status_registered
static struct ast_cli_entry cli_status[]
static int cdr_registered
void ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:165
static sqlite * db
int ast_config_engine_deregister(struct ast_config_engine *del)
Deregister config engine.
Definition: config.c:2397

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Realtime SQLite configuration" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, }
static

Definition at line 1877 of file res_config_sqlite.c.

Definition at line 1877 of file res_config_sqlite.c.

int cdr_registered
static

Set to 1 if the CDR callback function was registered.

Definition at line 468 of file res_config_sqlite.c.

char* cdr_table
static

The name of the table used to store CDR entries.

Definition at line 480 of file res_config_sqlite.c.

struct ast_cli_entry cli_status[]
static
Initial value:
= {
AST_CLI_DEFINE(handle_cli_show_sqlite_status, "Show status information about the SQLite 2 driver"),
AST_CLI_DEFINE(handle_cli_sqlite_show_tables, "Cached table information about the SQLite 2 driver"),
}
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
static char * handle_cli_show_sqlite_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Asterisk callback function for the CLI status command.
static char * handle_cli_sqlite_show_tables(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)

Structure containing details and callback functions for the CLI status command.

Definition at line 509 of file res_config_sqlite.c.

int cli_status_registered
static

Set to 1 if the CLI status command callback function was registered.

Definition at line 471 of file res_config_sqlite.c.

char* config_table
static

The name of the static configuration table.

Definition at line 477 of file res_config_sqlite.c.

Referenced by config_handler().

sqlite* db
static

The SQLite database object.

Definition at line 462 of file res_config_sqlite.c.

char* dbfile
static

The path of the database file.

Definition at line 474 of file res_config_sqlite.c.

ast_mutex_t mutex = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 }
static
struct ast_threadstorage sql_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_sql_buf , .custom_init = NULL , }
static

Definition at line 127 of file res_config_sqlite.c.

Referenced by realtime_update2_handler().

char* sql_create_cdr_table
static

SQL query format to create the CDR table if non existent.

Definition at line 534 of file res_config_sqlite.c.

Referenced by load_module().

struct ast_config_engine sqlite_engine
static

The structure specifying all callback functions used by Asterisk for static and RealTime configuration.

Definition at line 486 of file res_config_sqlite.c.

struct sqlite_tables sqlite_tables = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, 1 } , }
static
int use_cdr
static

Set to 1 if CDR support is enabled.

Definition at line 465 of file res_config_sqlite.c.

struct ast_threadstorage where_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_where_buf , .custom_init = NULL , }
static

Definition at line 128 of file res_config_sqlite.c.

Referenced by realtime_update2_handler().