Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Enumerations | Functions | Variables
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"
Include dependency graph for res_config_sqlite.c:

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...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
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 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)
 Load the module. More...
 
static int realtime_destroy_handler (const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields)
 Asterisk callback function for RealTime configuration (destroys variable). More...
 
static struct ast_variablerealtime_handler (const char *database, const char *table, const struct ast_variable *fields)
 Asterisk callback function for RealTime configuration. More...
 
static struct ast_configrealtime_multi_handler (const char *database, const char *table, const struct ast_variable *fields)
 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, const struct ast_variable *fields)
 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, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
 
static int realtime_update_handler (const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields)
 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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_DEPRECATED, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, .requires = "extconfig", }
 
static const 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, 0} }
 
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, 0} } , }
 
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

◆ MACRO_BEGIN

#define MACRO_BEGIN   do {

Definition at line 102 of file res_config_sqlite.c.

◆ MACRO_END

#define MACRO_END   } while (0)

Definition at line 103 of file res_config_sqlite.c.

◆ release_table

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

Definition at line 693 of file res_config_sqlite.c.

Referenced by cdr_handler().

◆ RES_CONFIG_SQLITE_BEGIN

#define RES_CONFIG_SQLITE_BEGIN
Value:
MACRO_BEGIN \
int __i; \
\
for (__i = 0; __i < RES_CONFIG_SQLITE_MAX_LOOPS; __i++) {
#define RES_CONFIG_SQLITE_MAX_LOOPS

Macro used before executing a query.

See also
RES_CONFIG_SQLITE_MAX_LOOPS.

Definition at line 161 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().

◆ RES_CONFIG_SQLITE_CONF_FILE

#define RES_CONFIG_SQLITE_CONF_FILE   "res_config_sqlite.conf"

Definition at line 108 of file res_config_sqlite.c.

Referenced by load_config().

◆ RES_CONFIG_SQLITE_DESCRIPTION

#define RES_CONFIG_SQLITE_DESCRIPTION   "Resource Module for SQLite 2"

Definition at line 107 of file res_config_sqlite.c.

Referenced by load_module().

◆ RES_CONFIG_SQLITE_DRIVER

#define RES_CONFIG_SQLITE_DRIVER   "sqlite"

Definition at line 106 of file res_config_sqlite.c.

◆ RES_CONFIG_SQLITE_END

#define RES_CONFIG_SQLITE_END (   error)
Value:
if (error != SQLITE_BUSY) \
break; \
usleep(1000); \
} \
MACRO_END;
int error(const char *format,...)
Definition: utils/frame.c:999

Macro used after executing a query.

See also
RES_CONFIG_SQLITE_MAX_LOOPS.

Definition at line 172 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().

◆ RES_CONFIG_SQLITE_MAX_LOOPS

#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 154 of file res_config_sqlite.c.

◆ RES_CONFIG_SQLITE_NAME

#define RES_CONFIG_SQLITE_NAME   "res_config_sqlite"

Definition at line 105 of file res_config_sqlite.c.

Referenced by load_module(), and unload_module().

◆ SET_VAR

#define SET_VAR (   config,
  to,
  from 
)

Definition at line 122 of file res_config_sqlite.c.

Referenced by load_config().

◆ sql_get_config_table

#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 551 of file res_config_sqlite.c.

Referenced by config_handler().

◆ sql_table_structure

#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 543 of file res_config_sqlite.c.

Referenced by find_table().

Enumeration Type Documentation

◆ anonymous enum

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 110 of file res_config_sqlite.c.

Function Documentation

◆ __init_sql_buf()

static void __init_sql_buf ( void  )
static

Definition at line 135 of file res_config_sqlite.c.

165 {

◆ __init_where_buf()

static void __init_where_buf ( void  )
static

Definition at line 136 of file res_config_sqlite.c.

165 {

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 1787 of file res_config_sqlite.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 1787 of file res_config_sqlite.c.

◆ add_cfg_entry()

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 848 of file res_config_sqlite.c.

References args, ast_category_append(), ast_category_destroy(), ast_category_new_dynamic, 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().

849 {
850  struct cfg_entry_args *args;
851  struct ast_variable *var;
852 
853  if (argc != RES_CONFIG_SQLITE_CONFIG_COLUMNS) {
854  ast_log(LOG_WARNING, "Corrupt table\n");
855  return 1;
856  }
857 
858  args = arg;
859 
860  if (!strcmp(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], "#include")) {
861  struct ast_config *cfg;
862  char *val;
863 
865  cfg = ast_config_internal_load(val, args->cfg, args->flags, "", args->who_asked);
866 
867  if (!cfg) {
868  ast_log(LOG_WARNING, "Unable to include %s\n", val);
869  return 1;
870  } else {
871  args->cfg = cfg;
872  return 0;
873  }
874  }
875 
876  if (!args->cat_name || strcmp(args->cat_name, argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY])) {
877  args->cat = ast_category_new_dynamic(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
878  if (!args->cat) {
879  return 1;
880  }
881 
882  ast_free(args->cat_name);
883  args->cat_name = ast_strdup(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
884 
885  if (!args->cat_name) {
886  ast_category_destroy(args->cat);
887  return 1;
888  }
889 
890  ast_category_append(args->cfg, args->cat);
891  }
892 
893  var = ast_variable_new(argv[RES_CONFIG_SQLITE_CONFIG_VAR_NAME], argv[RES_CONFIG_SQLITE_CONFIG_VAR_VAL], "");
894 
895  if (!var) {
896  ast_log(LOG_WARNING, "Unable to allocate variable\n");
897  return 1;
898  }
899 
900  ast_variable_append(args->cat, var);
901 
902  return 0;
903 }
Definition: ast_expr2.c:325
struct ast_flags flags
#define LOG_WARNING
Definition: logger.h:274
Structure for variables, used for configurations and for channel variables.
#define var
Definition: ast_expr2f.c:614
struct ast_config * cfg
char * cat_name
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
const char * args
void ast_category_destroy(struct ast_category *cat)
Definition: extconf.c:2847
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: main/config.c:3112
struct ast_category * cat
#define ast_log
Definition: astobj2.c:42
#define ast_variable_new(name, value, filename)
const char * who_asked
#define ast_free(a)
Definition: astmm.h:182
#define ast_category_new_dynamic(name)
Create a category that is not backed by a file.
void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
Definition: extconf.c:1178
void ast_category_append(struct ast_config *config, struct ast_category *cat)
Appends a category to a config.
Definition: extconf.c:2835

◆ add_rt_cfg_entry()

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 955 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().

956 {
957  struct rt_cfg_entry_args *args;
958  struct ast_variable *var;
959  int i;
960 
961  args = arg;
962 
963  for (i = 0; i < argc; i++) {
964  if (!argv[i])
965  continue;
966 
967  if (!(var = ast_variable_new(columnNames[i], argv[i], "")))
968  return 1;
969 
970  if (!args->var)
971  args->var = var;
972 
973  if (!args->last)
974  args->last = var;
975  else {
976  args->last->next = var;
977  args->last = var;
978  }
979  }
980 
981  return 0;
982 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
#define var
Definition: ast_expr2f.c:614
struct ast_variable * var
const char * args
#define ast_variable_new(name, value, filename)
struct ast_variable * last

◆ add_rt_multi_cfg_entry()

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 1061 of file res_config_sqlite.c.

References args, ast_category_append(), ast_category_new_dynamic, ast_log, ast_variable_append(), ast_variable_new, cfg_entry_args::cat, cfg_entry_args::cat_name, rt_multi_cfg_entry_args::cfg, rt_multi_cfg_entry_args::initfield, LOG_ERROR, LOG_WARNING, NULL, and var.

Referenced by realtime_multi_handler().

1062 {
1063  struct rt_multi_cfg_entry_args *args;
1064  struct ast_category *cat;
1065  struct ast_variable *var;
1066  char *cat_name;
1067  size_t i;
1068 
1069  args = arg;
1070  cat_name = NULL;
1071 
1072  /*
1073  * cat_name should always be set here, since initfield is forged from
1074  * params[0] in realtime_multi_handler(), which is a search parameter
1075  * of the SQL query.
1076  */
1077  for (i = 0; i < argc; i++) {
1078  if (!strcmp(args->initfield, columnNames[i]))
1079  cat_name = argv[i];
1080  }
1081 
1082  if (!cat_name) {
1083  ast_log(LOG_ERROR, "Bogus SQL results, cat_name is NULL !\n");
1084  return 1;
1085  }
1086 
1087  cat = ast_category_new_dynamic(cat_name);
1088  if (!cat) {
1089  return 1;
1090  }
1091 
1092  ast_category_append(args->cfg, cat);
1093 
1094  for (i = 0; i < argc; i++) {
1095  if (!argv[i]) {
1096  continue;
1097  }
1098 
1099  if (!(var = ast_variable_new(columnNames[i], argv[i], ""))) {
1100  ast_log(LOG_WARNING, "Unable to allocate variable\n");
1101  return 1;
1102  }
1103 
1104  ast_variable_append(cat, var);
1105  }
1106 
1107  return 0;
1108 }
char * initfield
#define LOG_WARNING
Definition: logger.h:274
Structure for variables, used for configurations and for channel variables.
#define var
Definition: ast_expr2f.c:614
const char * args
#define NULL
Definition: resample.c:96
struct ast_config * cfg
#define ast_log
Definition: astobj2.c:42
#define ast_variable_new(name, value, filename)
#define LOG_ERROR
Definition: logger.h:285
#define ast_category_new_dynamic(name)
Create a category that is not backed by a file.
void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
Definition: extconf.c:1178
void ast_category_append(struct ast_config *config, struct ast_category *cat)
Appends a category to a config.
Definition: extconf.c:2835

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 1787 of file res_config_sqlite.c.

◆ cdr_handler()

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 774 of file res_config_sqlite.c.

References ast_cdr_format_var(), 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(), cdr_table, sqlite_cache_tables::columns, db, error(), find_table(), first, sqlite_cache_columns::isint, LOG_ERROR, LOG_WARNING, mutex, sqlite_cache_columns::name, NULL, release_table, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, and tmp().

Referenced by load_module().

775 {
776  char *errormsg = NULL, *tmp, workspace[500];
777  int error, scannum;
779  struct sqlite_cache_columns *col;
780  struct ast_str *sql1 = ast_str_create(160), *sql2 = ast_str_create(16);
781  int first = 1;
782 
783  if (!sql1 || !sql2) {
784  ast_free(sql1);
785  ast_free(sql2);
786  return -1;
787  }
788 
789  if (!tbl) {
790  ast_log(LOG_WARNING, "No such table: %s\n", cdr_table);
791  ast_free(sql1);
792  ast_free(sql2);
793  return -1;
794  }
795 
796  ast_str_set(&sql1, 0, "INSERT INTO %s (", cdr_table);
797  ast_str_set(&sql2, 0, ") VALUES (");
798 
799  AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
800  if (col->isint) {
801  ast_cdr_format_var(cdr, col->name, &tmp, workspace, sizeof(workspace), 1);
802  if (!tmp) {
803  continue;
804  }
805  if (sscanf(tmp, "%30d", &scannum) == 1) {
806  ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
807  ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", scannum);
808  }
809  } else {
810  ast_cdr_format_var(cdr, col->name, &tmp, workspace, sizeof(workspace), 0);
811  if (!tmp) {
812  continue;
813  }
814  ast_str_append(&sql1, 0, "%s%s", first ? "" : ",", col->name);
815  tmp = sqlite_mprintf("%Q", tmp);
816  ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", tmp);
817  sqlite_freemem(tmp);
818  }
819  first = 0;
820  }
821  release_table(tbl);
822 
823  ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
824  ast_free(sql2);
825 
826  ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql1));
827 
829 
831  error = sqlite_exec(db, ast_str_buffer(sql1), NULL, NULL, &errormsg);
832  RES_CONFIG_SQLITE_END(error)
833 
835 
836  ast_free(sql1);
837 
838  if (error) {
839  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
840  sqlite_freemem(errormsg);
841  return 1;
842  }
843  sqlite_freemem(errormsg);
844 
845  return 0;
846 }
static struct sqlite_cache_tables * find_table(const char *tablename)
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:274
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define release_table(a)
static int tmp()
Definition: bt_open.c:389
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:1091
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int raw)
Format a CDR variable from an already posted CDR.
Definition: cdr.c:3050
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:1065
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct sqlite_cache_tables::_columns columns
#define LOG_ERROR
Definition: logger.h:285
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
struct sla_ringing_trunk * first
Definition: app_meetme.c:1092
#define ast_free(a)
Definition: astmm.h:182
static char * 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:79
int error(const char *format,...)
Definition: utils/frame.c:999
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ check_vars()

static int check_vars ( void  )
static

Definition at line 710 of file res_config_sqlite.c.

References ast_log, cdr_table, dbfile, LOG_ERROR, NULL, and use_cdr.

Referenced by load_config().

711 {
712  if (!dbfile) {
713  ast_log(LOG_ERROR, "Required parameter undefined: dbfile\n");
714  return 1;
715  }
716 
717  use_cdr = (cdr_table != NULL);
718 
719  return 0;
720 }
static int use_cdr
#define NULL
Definition: resample.c:96
static char * dbfile
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
static char * cdr_table

◆ config_handler()

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 905 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, db, error(), cfg_entry_args::flags, LOG_ERROR, LOG_WARNING, mutex, NULL, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, sql_get_config_table, and cfg_entry_args::who_asked.

907 {
908  struct cfg_entry_args args;
909  char *query, *errormsg = NULL;
910  int error;
911 
912  if (!config_table) {
913  if (!table) {
914  ast_log(LOG_ERROR, "Table name unspecified\n");
915  return NULL;
916  }
917  } else
919 
920  query = sqlite_mprintf(sql_get_config_table, table, file);
921 
922  if (!query) {
923  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
924  return NULL;
925  }
926 
927  ast_debug(1, "SQL query: %s\n", query);
928  args.cfg = cfg;
929  args.cat = NULL;
930  args.cat_name = NULL;
931  args.flags = flags;
932  args.who_asked = who_asked;
933 
935 
937  error = sqlite_exec(db, query, add_cfg_entry, &args, &errormsg);
938  RES_CONFIG_SQLITE_END(error)
939 
941 
942  ast_free(args.cat_name);
943  sqlite_freemem(query);
944 
945  if (error) {
946  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
947  sqlite_freemem(errormsg);
948  return NULL;
949  }
950  sqlite_freemem(errormsg);
951 
952  return cfg;
953 }
#define RES_CONFIG_SQLITE_BEGIN
struct ast_flags flags
#define LOG_WARNING
Definition: logger.h:274
#define sql_get_config_table
struct ast_config * cfg
#define ast_mutex_lock(a)
Definition: lock.h:187
const char * args
#define NULL
Definition: resample.c:96
static char * table
Definition: cdr_odbc.c:58
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static char * config_table
const char * who_asked
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:285
#define ast_free(a)
Definition: astmm.h:182
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
int error(const char *format,...)
Definition: utils/frame.c:999
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ find_table()

static struct sqlite_cache_tables* find_table ( const char *  tablename)
static

Definition at line 626 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, db, find_table_cb(), free_table(), LOG_ERROR, LOG_WARNING, mutex, sqlite_cache_tables::name, NULL, and sql_table_structure.

Referenced by cdr_handler(), and realtime_require_handler().

627 {
628  struct sqlite_cache_tables *tblptr;
629  int i, err;
630  char *sql, *errstr = NULL;
631 
633 
634  for (i = 0; i < 2; i++) {
635  AST_RWLIST_TRAVERSE(&sqlite_tables, tblptr, list) {
636  if (strcmp(tblptr->name, tablename) == 0) {
637  break;
638  }
639  }
640  if (tblptr) {
641  AST_RWLIST_RDLOCK(&(tblptr->columns));
643  return tblptr;
644  }
645 
646  if (i == 0) {
649  }
650  }
651 
652  /* Table structure not cached; build the structure now */
653  if (ast_asprintf(&sql, sql_table_structure, tablename) < 0) {
654  sql = NULL;
655  }
656  if (!(tblptr = ast_calloc(1, sizeof(*tblptr) + strlen(tablename) + 1))) {
658  ast_log(LOG_ERROR, "Memory error. Cannot cache table '%s'\n", tablename);
659  ast_free(sql);
660  return NULL;
661  }
662  tblptr->name = (char *)tblptr + sizeof(*tblptr);
663  strcpy(tblptr->name, tablename); /* SAFE */
664  AST_RWLIST_HEAD_INIT(&(tblptr->columns));
665 
666  ast_debug(1, "About to query table structure: %s\n", sql);
667 
669  if ((err = sqlite_exec(db, sql, find_table_cb, tblptr, &errstr))) {
671  ast_log(LOG_WARNING, "SQLite error %d: %s\n", err, errstr);
672  ast_free(errstr);
673  free_table(tblptr);
675  ast_free(sql);
676  return NULL;
677  }
679  ast_free(sql);
680 
681  if (AST_LIST_EMPTY(&(tblptr->columns))) {
682  free_table(tblptr);
684  return NULL;
685  }
686 
687  AST_RWLIST_INSERT_TAIL(&sqlite_tables, tblptr, list);
688  AST_RWLIST_RDLOCK(&(tblptr->columns));
690  return tblptr;
691 }
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:274
#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:187
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
Definition: linkedlists.h:638
#define NULL
Definition: resample.c:96
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:269
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define sql_table_structure
struct sqlite_cache_tables::_columns columns
#define LOG_ERROR
Definition: logger.h:285
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:740
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ find_table_cb()

static int find_table_cb ( void *  vtblptr,
int  argc,
char **  argv,
char **  columnNames 
)
static

Definition at line 571 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, end, sqlite_cache_columns::isint, sqlite_cache_columns::name, strcasestr(), type, and sqlite_cache_columns::type.

Referenced by find_table().

572 {
573  struct sqlite_cache_tables *tblptr = vtblptr;
574  char *sql = ast_strdupa(argv[0]), *start, *end, *type, *remainder;
575  int i;
577  AST_APP_ARG(ld)[100]; /* This means we support up to 100 columns per table */
578  );
579  struct sqlite_cache_columns *col;
580 
581  /* This is really fun. We get to parse an SQL statement to figure out
582  * what columns are in the table.
583  */
584  if ((start = strchr(sql, '(')) && (end = strrchr(sql, ')'))) {
585  start++;
586  *end = '\0';
587  } else {
588  /* Abort */
589  return -1;
590  }
591 
592  AST_STANDARD_APP_ARGS(fie, start);
593  for (i = 0; i < fie.argc; i++) {
594  fie.ld[i] = ast_skip_blanks(fie.ld[i]);
595  ast_debug(5, "Found field: %s\n", fie.ld[i]);
596  if (strncasecmp(fie.ld[i], "PRIMARY KEY", 11) == 0 && (start = strchr(fie.ld[i], '(')) && (end = strchr(fie.ld[i], ')'))) {
597  *end = '\0';
598  AST_RWLIST_TRAVERSE(&(tblptr->columns), col, list) {
599  if (strcasecmp(start + 1, col->name) == 0 && strcasestr(col->type, "INTEGER")) {
600  col->isint = 1;
601  }
602  }
603  continue;
604  }
605  /* type delimiter could be any space character */
606  for (type = fie.ld[i]; *type > 32; type++);
607  *type++ = '\0';
608  type = ast_skip_blanks(type);
609  for (remainder = type; *remainder > 32; remainder++);
610  *remainder = '\0';
611  if (!(col = ast_calloc(1, sizeof(*col) + strlen(fie.ld[i]) + strlen(type) + 2))) {
612  return -1;
613  }
614  col->name = (char *)col + sizeof(*col);
615  col->type = (char *)col + sizeof(*col) + strlen(fie.ld[i]) + 1;
616  strcpy(col->name, fie.ld[i]); /* SAFE */
617  strcpy(col->type, type); /* SAFE */
618  if (strcasestr(col->type, "INTEGER") && strcasestr(col->type, "PRIMARY KEY")) {
619  col->isint = 1;
620  }
621  AST_LIST_INSERT_TAIL(&(tblptr->columns), col, list);
622  }
623  return 0;
624 }
static const char type[]
Definition: chan_ooh323.c:109
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
char * end
Definition: eagi_proxy.c:73
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
struct sqlite_cache_tables::_columns columns
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:730
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:157
char * strcasestr(const char *, const char *)
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ free_table()

static void free_table ( struct sqlite_cache_tables tblptr)
static

Definition at line 557 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().

558 {
559  struct sqlite_cache_columns *col;
560 
561  /* Obtain a write lock to ensure there are no read locks outstanding */
562  AST_RWLIST_WRLOCK(&(tblptr->columns));
563  while ((col = AST_RWLIST_REMOVE_HEAD(&(tblptr->columns), list))) {
564  ast_free(col);
565  }
566  AST_RWLIST_UNLOCK(&(tblptr->columns));
567  AST_RWLIST_HEAD_DESTROY(&(tblptr->columns));
568  ast_free(tblptr);
569 }
#define AST_RWLIST_HEAD_DESTROY(head)
Destroys an rwlist head structure.
Definition: linkedlists.h:666
#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:182
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:843

◆ handle_cli_show_sqlite_status()

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 1561 of file res_config_sqlite.c.

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

1562 {
1563  switch (cmd) {
1564  case CLI_INIT:
1565  e->command = "sqlite show status";
1566  e->usage =
1567  "Usage: sqlite show status\n"
1568  " Show status information about the SQLite 2 driver\n";
1569  return NULL;
1570  case CLI_GENERATE:
1571  return NULL;
1572  }
1573 
1574  if (a->argc != 3)
1575  return CLI_SHOWUSAGE;
1576 
1577  ast_cli(a->fd, "SQLite database path: %s\n", dbfile);
1578  ast_cli(a->fd, "config_table: ");
1579 
1580  if (!config_table)
1581  ast_cli(a->fd, "unspecified, must be present in extconfig.conf\n");
1582  else
1583  ast_cli(a->fd, "%s\n", config_table);
1584 
1585  ast_cli(a->fd, "cdr_table: ");
1586 
1587  if (!cdr_table)
1588  ast_cli(a->fd, "unspecified, CDR support disabled\n");
1589  else
1590  ast_cli(a->fd, "%s\n", cdr_table);
1591 
1592  return CLI_SUCCESS;
1593 }
const int argc
Definition: cli.h:160
Definition: cli.h:152
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
static char * dbfile
const int fd
Definition: cli.h:159
static char * config_table
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
static char * cdr_table
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44

◆ handle_cli_sqlite_show_tables()

static char * handle_cli_sqlite_show_tables ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 1595 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, NULL, sqlite_cache_columns::type, and ast_cli_entry::usage.

1596 {
1597  struct sqlite_cache_tables *tbl;
1598  struct sqlite_cache_columns *col;
1599  int found = 0;
1600 
1601  switch (cmd) {
1602  case CLI_INIT:
1603  e->command = "sqlite show tables";
1604  e->usage =
1605  "Usage: sqlite show tables\n"
1606  " Show table information about the SQLite 2 driver\n";
1607  return NULL;
1608  case CLI_GENERATE:
1609  return NULL;
1610  }
1611 
1612  if (a->argc != 3)
1613  return CLI_SHOWUSAGE;
1614 
1616  AST_RWLIST_TRAVERSE(&sqlite_tables, tbl, list) {
1617  found++;
1618  ast_cli(a->fd, "Table %s:\n", tbl->name);
1619  AST_RWLIST_TRAVERSE(&(tbl->columns), col, list) {
1620  fprintf(stderr, "%s\n", col->name);
1621  ast_cli(a->fd, " %20.20s %-30.30s\n", col->name, col->type);
1622  }
1623  }
1625 
1626  if (!found) {
1627  ast_cli(a->fd, "No tables currently in cache\n");
1628  }
1629 
1630  return CLI_SUCCESS;
1631 }
const int argc
Definition: cli.h:160
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Definition: cli.h:152
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
const int fd
Definition: cli.h:159
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct sqlite_cache_tables::_columns columns
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44

◆ load_config()

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 722 of file res_config_sqlite.c.

References ast_config_destroy(), ast_config_load, ast_log, ast_variable_browse(), cdr_table, check_vars(), config, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, config_table, dbfile, error(), 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().

723 {
724  struct ast_config *config;
725  struct ast_variable *var;
726  int error;
727  struct ast_flags config_flags = { 0 };
728 
729  config = ast_config_load(RES_CONFIG_SQLITE_CONF_FILE, config_flags);
730 
731  if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
732  ast_log(LOG_ERROR, "Unable to load " RES_CONFIG_SQLITE_CONF_FILE "\n");
733  return 1;
734  }
735 
736  for (var = ast_variable_browse(config, "general"); var; var = var->next) {
737  if (!strcasecmp(var->name, "dbfile"))
738  SET_VAR(config, dbfile, var);
739  else if (!strcasecmp(var->name, "config_table"))
740  SET_VAR(config, config_table, var);
741  else if (!strcasecmp(var->name, "cdr_table")) {
742  SET_VAR(config, cdr_table, var);
743  } else
744  ast_log(LOG_WARNING, "Unknown parameter : %s\n", var->name);
745  }
746 
747  ast_config_destroy(config);
748  error = check_vars();
749 
750  if (error) {
751  unload_config();
752  return 1;
753  }
754 
755  return 0;
756 }
#define RES_CONFIG_SQLITE_CONF_FILE
struct ast_variable * next
char * config
Definition: conf2ael.c:66
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1216
#define LOG_WARNING
Definition: logger.h:274
#define CONFIG_STATUS_FILEINVALID
static void unload_config(void)
Free resources related to configuration.
Structure for variables, used for configurations and for channel variables.
#define var
Definition: ast_expr2f.c:614
static char * dbfile
#define ast_log
Definition: astobj2.c:42
#define ast_config_load(filename, flags)
Load a config file.
static char * config_table
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
static int check_vars(void)
#define LOG_ERROR
Definition: logger.h:285
static char * cdr_table
Structure used to handle boolean flags.
Definition: utils.h:199
#define CONFIG_STATUS_FILEMISSING
#define SET_VAR(config, to, from)
int error(const char *format,...)
Definition: utils/frame.c:999

◆ load_module()

static int load_module ( void  )
static

Load the module.

Module loading including tests for configuration or dependencies. This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE, or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails tests return AST_MODULE_LOAD_FAILURE. If the module can not load the configuration file or other non-critical problem return AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.

Definition at line 1663 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_MODFLAG_LOAD_ORDER, AST_MODPRI_REALTIME_DRIVER, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_DEPRECATED, ASTERISK_GPL_KEY, cdr_handler(), cdr_registered, cdr_table, cli_status_registered, config_table, db, dbfile, error(), load_config(), LOG_ERROR, NULL, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_DESCRIPTION, RES_CONFIG_SQLITE_END, RES_CONFIG_SQLITE_NAME, S_OR, sql_create_cdr_table, unload_module(), and use_cdr.

1664 {
1665  char *errormsg = NULL;
1666  int error;
1667 
1668  db = NULL;
1669  cdr_registered = 0;
1671  dbfile = NULL;
1672  config_table = NULL;
1673  cdr_table = NULL;
1674  error = load_config();
1675 
1676  if (error)
1677  return AST_MODULE_LOAD_DECLINE;
1678 
1679  if (!(db = sqlite_open(dbfile, 0660, &errormsg))) {
1680  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1681  sqlite_freemem(errormsg);
1682  unload_module();
1683  return AST_MODULE_LOAD_DECLINE;
1684  }
1685 
1686  sqlite_freemem(errormsg);
1687  errormsg = NULL;
1689 
1690  if (use_cdr) {
1691  char *query;
1692 
1693 /* \cond DOXYGEN_CAN_PARSE_THIS */
1694 #undef QUERY
1695 #define QUERY "SELECT COUNT(id) FROM %Q;"
1696 /* \endcond */
1697 
1698  query = sqlite_mprintf(QUERY, cdr_table);
1699 
1700  if (!query) {
1701  ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
1702  unload_module();
1703  return AST_MODULE_LOAD_DECLINE;
1704  }
1705 
1706  ast_debug(1, "SQL query: %s\n", query);
1707 
1709  error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1710  RES_CONFIG_SQLITE_END(error)
1711 
1712  sqlite_freemem(query);
1713 
1714  if (error) {
1715  /*
1716  * Unexpected error.
1717  */
1718  if (error != SQLITE_ERROR) {
1719  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1720  sqlite_freemem(errormsg);
1721  unload_module();
1722  return AST_MODULE_LOAD_DECLINE;
1723  }
1724 
1725  sqlite_freemem(errormsg);
1726  errormsg = NULL;
1727  query = sqlite_mprintf(sql_create_cdr_table, cdr_table);
1728 
1729  if (!query) {
1730  ast_log(LOG_ERROR, "Unable to allocate SQL query\n");
1731  unload_module();
1732  return AST_MODULE_LOAD_DECLINE;
1733  }
1734 
1735  ast_debug(1, "SQL query: %s\n", query);
1736 
1738  error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1739  RES_CONFIG_SQLITE_END(error)
1740 
1741  sqlite_freemem(query);
1742 
1743  if (error) {
1744  ast_log(LOG_ERROR, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1745  sqlite_freemem(errormsg);
1746  unload_module();
1747  return AST_MODULE_LOAD_DECLINE;
1748  }
1749  }
1750  sqlite_freemem(errormsg);
1751  errormsg = NULL;
1752 
1754 
1755  if (error) {
1756  unload_module();
1757  return AST_MODULE_LOAD_DECLINE;
1758  }
1759 
1760  cdr_registered = 1;
1761  }
1762 
1764 
1765  if (error) {
1766  unload_module();
1767  return AST_MODULE_LOAD_DECLINE;
1768  }
1769 
1771 
1772  return AST_MODULE_LOAD_SUCCESS;
1773 }
#define RES_CONFIG_SQLITE_NAME
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static int unload_module(void)
#define RES_CONFIG_SQLITE_BEGIN
int ast_config_engine_register(struct ast_config_engine *newconfig)
Register config engine.
Definition: main/config.c:2990
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static int use_cdr
#define NULL
Definition: resample.c:96
static struct ast_config_engine sqlite_engine
static char * dbfile
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:2943
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:285
static char * cdr_table
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
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:79
#define RES_CONFIG_SQLITE_DESCRIPTION
static int load_config(void)
Load the configuration file.
int error(const char *format,...)
Definition: utils/frame.c:999
#define RES_CONFIG_SQLITE_END(error)
static int cdr_handler(struct ast_cdr *cdr)
Asterisk callback function for CDR support.
static sqlite * db

◆ realtime_destroy_handler()

static int realtime_destroy_handler ( const char *  database,
const char *  table,
const char *  keyfield,
const char *  entity,
const struct ast_variable fields 
)
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
fieldslist of additional parameters for cell matching
Return values
thenumber of affected rows.
-1if an error occurred.

Definition at line 1443 of file res_config_sqlite.c.

References ast_debug, ast_log, ast_mutex_lock, ast_mutex_unlock, db, error(), LOG_WARNING, mutex, ast_variable::name, ast_variable::next, NULL, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, and ast_variable::value.

1445 {
1446  char *query, *errormsg = NULL, *tmp_str;
1447  const struct ast_variable *field;
1448  int error, rows_num;
1449 
1450  if (!table) {
1451  ast_log(LOG_WARNING, "Table name unspecified\n");
1452  return -1;
1453  }
1454 
1455 /* \cond DOXYGEN_CAN_PARSE_THIS */
1456 #undef QUERY
1457 #define QUERY "DELETE FROM '%q' WHERE"
1458 /* \endcond */
1459 
1460  if (!(query = sqlite_mprintf(QUERY, table))) {
1461  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1462  return -1;
1463  }
1464 
1465  for (field = fields; field; field = field->next) {
1466  tmp_str = sqlite_mprintf("%s %q = '%q' AND", query, field->name, field->value);
1467  sqlite_freemem(query);
1468 
1469  if (!tmp_str) {
1470  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1471  return -1;
1472  }
1473 
1474  query = tmp_str;
1475  }
1476 
1477  if (!(tmp_str = sqlite_mprintf("%s %q = '%q';", query, keyfield, entity))) {
1478  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1479  sqlite_freemem(query);
1480  return -1;
1481  }
1482  sqlite_freemem(query);
1483  query = tmp_str;
1484  ast_debug(1, "SQL query: %s\n", query);
1485 
1487 
1489  error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1490  RES_CONFIG_SQLITE_END(error)
1491 
1492  if (!error) {
1493  rows_num = sqlite_changes(db);
1494  } else {
1495  rows_num = -1;
1496  }
1497 
1499 
1500  sqlite_freemem(query);
1501 
1502  if (error) {
1503  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1504  }
1505  sqlite_freemem(errormsg);
1506 
1507  return rows_num;
1508 }
struct ast_variable * next
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:274
static int entity
Definition: isdn_lib.c:259
Structure for variables, used for configurations and for channel variables.
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
static char * table
Definition: cdr_odbc.c:58
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
int error(const char *format,...)
Definition: utils/frame.c:999
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ realtime_handler()

static struct ast_variable * realtime_handler ( const char *  database,
const char *  table,
const struct ast_variable fields 
)
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
fieldslist 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 984 of file res_config_sqlite.c.

References add_rt_cfg_entry(), ast_debug, ast_log, ast_mutex_lock, ast_mutex_unlock, ast_variables_destroy(), config_table, db, error(), rt_cfg_entry_args::last, LOG_WARNING, mutex, ast_variable::name, ast_variable::next, NULL, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, ast_variable::value, and rt_cfg_entry_args::var.

985 {
986  char *query, *errormsg = NULL, *op, *tmp_str;
987  struct rt_cfg_entry_args args;
988  const struct ast_variable *field = fields;
989  int error;
990 
991  if (!table) {
992  ast_log(LOG_WARNING, "Table name unspecified\n");
993  return NULL;
994  }
995 
996  if (!fields) {
997  return NULL;
998  }
999 
1000  op = (strchr(field->name, ' ') == NULL) ? " =" : "";
1001 
1002 /* \cond DOXYGEN_CAN_PARSE_THIS */
1003 #undef QUERY
1004 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
1005 /* \endcond */
1006 
1007  query = sqlite_mprintf(QUERY, table, (config_table && !strcmp(config_table, table)) ? " commented = 0 AND" : "", field->name, op, field->value);
1008 
1009  if (!query) {
1010  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1011  return NULL;
1012  }
1013 
1014  while ((field = field->next)) {
1015  op = (strchr(field->name, ' ') == NULL) ? " =" : "";
1016  tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, field->name, op, field->value);
1017  sqlite_freemem(query);
1018 
1019  if (!tmp_str) {
1020  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1021  return NULL;
1022  }
1023 
1024  query = tmp_str;
1025  }
1026 
1027  tmp_str = sqlite_mprintf("%s LIMIT 1;", query);
1028  sqlite_freemem(query);
1029 
1030  if (!tmp_str) {
1031  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1032  return NULL;
1033  }
1034 
1035  query = tmp_str;
1036  ast_debug(1, "SQL query: %s\n", query);
1037  args.var = NULL;
1038  args.last = NULL;
1039 
1041 
1043  error = sqlite_exec(db, query, add_rt_cfg_entry, &args, &errormsg);
1044  RES_CONFIG_SQLITE_END(error)
1045 
1047 
1048  sqlite_freemem(query);
1049 
1050  if (error) {
1051  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1052  sqlite_freemem(errormsg);
1054  return NULL;
1055  }
1056  sqlite_freemem(errormsg);
1057 
1058  return args.var;
1059 }
struct ast_variable * next
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:274
Structure for variables, used for configurations and for channel variables.
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:187
const char * args
#define NULL
Definition: resample.c:96
static char * table
Definition: cdr_odbc.c:58
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static char * config_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:79
int error(const char *format,...)
Definition: utils/frame.c:999
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ realtime_multi_handler()

static struct ast_config * realtime_multi_handler ( const char *  database,
const char *  table,
const struct ast_variable fields 
)
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
fieldslist 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 1110 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, cfg_entry_args::cfg, rt_multi_cfg_entry_args::cfg, config_table, db, error(), rt_multi_cfg_entry_args::initfield, LOG_WARNING, mutex, ast_variable::name, ast_variable::next, NULL, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, and ast_variable::value.

1112 {
1113  char *query, *errormsg = NULL, *op, *tmp_str, *initfield;
1115  const struct ast_variable *field = fields;
1116  struct ast_config *cfg;
1117  int error;
1118 
1119  if (!table) {
1120  ast_log(LOG_WARNING, "Table name unspecified\n");
1121  return NULL;
1122  }
1123 
1124  if (!fields) {
1125  return NULL;
1126  }
1127 
1128  if (!(cfg = ast_config_new())) {
1129  ast_log(LOG_WARNING, "Unable to allocate configuration structure\n");
1130  return NULL;
1131  }
1132 
1133  if (!(initfield = ast_strdup(field->name))) {
1134  ast_config_destroy(cfg);
1135  return NULL;
1136  }
1137 
1138  tmp_str = strchr(initfield, ' ');
1139 
1140  if (tmp_str)
1141  *tmp_str = '\0';
1142 
1143  op = (!strchr(field->name, ' ')) ? " =" : "";
1144 
1145  /*
1146  * Asterisk sends us an already escaped string when searching for
1147  * "exten LIKE" (uh!). Handle it separately.
1148  */
1149  tmp_str = (!strcmp(field->value, "\\_%")) ? "_%" : (char *)field->value;
1150 
1151 /* \cond DOXYGEN_CAN_PARSE_THIS */
1152 #undef QUERY
1153 #define QUERY "SELECT * FROM '%q' WHERE%s %q%s '%q'"
1154 /* \endcond */
1155 
1156  if (!(query = sqlite_mprintf(QUERY, table, (config_table && !strcmp(config_table, table)) ? " commented = 0 AND" : "", field->name, op, tmp_str))) {
1157  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1158  ast_config_destroy(cfg);
1159  ast_free(initfield);
1160  return NULL;
1161  }
1162 
1163  while ((field = field->next)) {
1164  op = (!strchr(field->name, ' ')) ? " =" : "";
1165  tmp_str = sqlite_mprintf("%s AND %q%s '%q'", query, field->name, op, field->value);
1166  sqlite_freemem(query);
1167 
1168  if (!tmp_str) {
1169  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1170  ast_config_destroy(cfg);
1171  ast_free(initfield);
1172  return NULL;
1173  }
1174 
1175  query = tmp_str;
1176  }
1177 
1178  if (!(tmp_str = sqlite_mprintf("%s ORDER BY %q;", query, initfield))) {
1179  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1180  sqlite_freemem(query);
1181  ast_config_destroy(cfg);
1182  ast_free(initfield);
1183  return NULL;
1184  }
1185 
1186  sqlite_freemem(query);
1187  query = tmp_str;
1188  ast_debug(1, "SQL query: %s\n", query);
1189  args.cfg = cfg;
1190  args.initfield = initfield;
1191 
1193 
1195  error = sqlite_exec(db, query, add_rt_multi_cfg_entry, &args, &errormsg);
1196  RES_CONFIG_SQLITE_END(error)
1197 
1199 
1200  sqlite_freemem(query);
1201  ast_free(initfield);
1202 
1203  if (error) {
1204  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1205  sqlite_freemem(errormsg);
1206  ast_config_destroy(cfg);
1207  return NULL;
1208  }
1209  sqlite_freemem(errormsg);
1210 
1211  return cfg;
1212 }
struct ast_variable * next
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:274
Structure for variables, used for configurations and for channel variables.
#define ast_mutex_lock(a)
Definition: lock.h:187
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
const char * args
#define NULL
Definition: resample.c:96
static char * table
Definition: cdr_odbc.c:58
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static char * config_table
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
struct ast_config * ast_config_new(void)
Create a new base configuration structure.
Definition: extconf.c:3276
#define ast_free(a)
Definition: astmm.h:182
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
static int add_rt_multi_cfg_entry(void *arg, int argc, char **argv, char **columnNames)
SQLite callback function for RealTime configuration.
int error(const char *format,...)
Definition: utils/frame.c:999
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ realtime_require_handler()

static int realtime_require_handler ( const char *  database,
const char *  table,
va_list  ap 
)
static

Definition at line 1510 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 type.

1511 {
1512  struct sqlite_cache_tables *tbl = find_table(tablename);
1513  struct sqlite_cache_columns *col;
1514  char *elm;
1515  int type, res = 0;
1516 
1517  if (!tbl) {
1518  return -1;
1519  }
1520 
1521  while ((elm = va_arg(ap, char *))) {
1522  type = va_arg(ap, require_type);
1523  va_arg(ap, int);
1524  /* Check if the field matches the criteria */
1525  AST_RWLIST_TRAVERSE(&tbl->columns, col, list) {
1526  if (strcmp(col->name, elm) == 0) {
1527  /* SQLite only has two types - the 32-bit integer field that
1528  * is the key column, and everything else (everything else
1529  * being a string).
1530  */
1531  if (col->isint && !ast_rq_is_int(type)) {
1532  ast_log(LOG_WARNING, "Realtime table %s: column '%s' is an integer field, but Asterisk requires that it not be!\n", tablename, col->name);
1533  res = -1;
1534  }
1535  break;
1536  }
1537  }
1538  if (!col) {
1539  ast_log(LOG_WARNING, "Realtime table %s requires column '%s', but that column does not exist!\n", tablename, elm);
1540  }
1541  }
1542  AST_RWLIST_UNLOCK(&(tbl->columns));
1543  return res;
1544 }
require_type
Types used in ast_realtime_require_field.
static const char type[]
Definition: chan_ooh323.c:109
static struct sqlite_cache_tables * find_table(const char *tablename)
#define LOG_WARNING
Definition: logger.h:274
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define ast_log
Definition: astobj2.c:42
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct sqlite_cache_tables::_columns columns
int ast_rq_is_int(require_type type)
Check if require type is an integer type.

◆ realtime_store_handler()

static int realtime_store_handler ( const char *  database,
const char *  table,
const struct ast_variable fields 
)
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
fieldslist of parameters and new values to insert into the database
Return values
therowid of inserted row.
-1if an error occurred.

Definition at line 1357 of file res_config_sqlite.c.

References ast_debug, ast_log, ast_mutex_lock, ast_mutex_unlock, db, error(), LOG_WARNING, mutex, ast_variable::name, ast_variable::next, NULL, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, and ast_variable::value.

1358 {
1359  char *errormsg = NULL, *tmp_str, *tmp_keys = NULL, *tmp_keys2 = NULL, *tmp_vals = NULL, *tmp_vals2 = NULL;
1360  const struct ast_variable *field = fields;
1361  int error, rows_id;
1362 
1363  if (!table) {
1364  ast_log(LOG_WARNING, "Table name unspecified\n");
1365  return -1;
1366  }
1367 
1368  if (!fields) {
1369  return -1;
1370  }
1371 
1372 /* \cond DOXYGEN_CAN_PARSE_THIS */
1373 #undef QUERY
1374 #define QUERY "INSERT into '%q' (%s) VALUES (%s);"
1375 /* \endcond */
1376 
1377  do {
1378  if ( tmp_keys2 ) {
1379  tmp_keys = sqlite_mprintf("%s, %q", tmp_keys2, field->name);
1380  sqlite_freemem(tmp_keys2);
1381  } else {
1382  tmp_keys = sqlite_mprintf("%q", field->name);
1383  }
1384  if (!tmp_keys) {
1385  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1386  sqlite_freemem(tmp_vals);
1387  return -1;
1388  }
1389 
1390  if ( tmp_vals2 ) {
1391  tmp_vals = sqlite_mprintf("%s, '%q'", tmp_vals2, field->value);
1392  sqlite_freemem(tmp_vals2);
1393  } else {
1394  tmp_vals = sqlite_mprintf("'%q'", field->value);
1395  }
1396  if (!tmp_vals) {
1397  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1398  sqlite_freemem(tmp_keys);
1399  return -1;
1400  }
1401 
1402 
1403  tmp_keys2 = tmp_keys;
1404  tmp_vals2 = tmp_vals;
1405  } while ((field = field->next));
1406 
1407  if (!(tmp_str = sqlite_mprintf(QUERY, table, tmp_keys, tmp_vals))) {
1408  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1409  sqlite_freemem(tmp_keys);
1410  sqlite_freemem(tmp_vals);
1411  return -1;
1412  }
1413 
1414  sqlite_freemem(tmp_keys);
1415  sqlite_freemem(tmp_vals);
1416 
1417  ast_debug(1, "SQL query: %s\n", tmp_str);
1418 
1420 
1422  error = sqlite_exec(db, tmp_str, NULL, NULL, &errormsg);
1423  RES_CONFIG_SQLITE_END(error)
1424 
1425  if (!error) {
1426  rows_id = sqlite_last_insert_rowid(db);
1427  } else {
1428  rows_id = -1;
1429  }
1430 
1432 
1433  sqlite_freemem(tmp_str);
1434 
1435  if (error) {
1436  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1437  }
1438  sqlite_freemem(errormsg);
1439 
1440  return rows_id;
1441 }
struct ast_variable * next
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:274
Structure for variables, used for configurations and for channel variables.
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
static char * table
Definition: cdr_odbc.c:58
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
int error(const char *format,...)
Definition: utils/frame.c:999
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ realtime_unload_handler()

static int realtime_unload_handler ( const char *  unused,
const char *  tablename 
)
static

Definition at line 1546 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.

1547 {
1548  struct sqlite_cache_tables *tbl;
1551  if (!strcasecmp(tbl->name, tablename)) {
1553  free_table(tbl);
1554  }
1555  }
1558  return 0;
1559 }
#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:569
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:544
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:616

◆ realtime_update2_handler()

static int realtime_update2_handler ( const char *  database,
const char *  table,
const struct ast_variable lookup_fields,
const struct ast_variable update_fields 
)
static

Definition at line 1285 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(), db, error(), first, LOG_ERROR, LOG_WARNING, mutex, ast_variable::name, ast_variable::next, NULL, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, sql_buf, ast_variable::value, and where_buf.

1287 {
1288  char *errormsg = NULL, *tmp1, *tmp2;
1289  int error, rows_num, first = 1;
1290  struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
1291  struct ast_str *where = ast_str_thread_get(&where_buf, 100);
1292  const struct ast_variable *field;
1293 
1294  if (!table) {
1295  ast_log(LOG_WARNING, "Table name unspecified\n");
1296  return -1;
1297  }
1298 
1299  if (!sql) {
1300  return -1;
1301  }
1302 
1303  ast_str_set(&sql, 0, "UPDATE %s SET", table);
1304  ast_str_set(&where, 0, " WHERE");
1305 
1306  for (field = lookup_fields; field; field = field->next) {
1307  ast_str_append(&where, 0, "%s %s = %s",
1308  first ? "" : " AND",
1309  tmp1 = sqlite_mprintf("%q", field->name),
1310  tmp2 = sqlite_mprintf("%Q", field->value));
1311  sqlite_freemem(tmp1);
1312  sqlite_freemem(tmp2);
1313  first = 0;
1314  }
1315 
1316  if (first) {
1317  ast_log(LOG_ERROR, "No criteria specified on update to '%s@%s'!\n", table, database);
1318  return -1;
1319  }
1320 
1321  first = 1;
1322  for (field = update_fields; field; field = field->next) {
1323  ast_str_append(&sql, 0, "%s %s = %s",
1324  first ? "" : ",",
1325  tmp1 = sqlite_mprintf("%q", field->name),
1326  tmp2 = sqlite_mprintf("%Q", field->value));
1327  sqlite_freemem(tmp1);
1328  sqlite_freemem(tmp2);
1329  first = 0;
1330  }
1331 
1332  ast_str_append(&sql, 0, " %s", ast_str_buffer(where));
1333  ast_debug(1, "SQL query: %s\n", ast_str_buffer(sql));
1334 
1336 
1338  error = sqlite_exec(db, ast_str_buffer(sql), NULL, NULL, &errormsg);
1339  RES_CONFIG_SQLITE_END(error)
1340 
1341  if (!error) {
1342  rows_num = sqlite_changes(db);
1343  } else {
1344  rows_num = -1;
1345  }
1346 
1348 
1349  if (error) {
1350  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1351  }
1352  sqlite_freemem(errormsg);
1353 
1354  return rows_num;
1355 }
struct ast_variable * next
static struct ast_threadstorage where_buf
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:274
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
Structure for variables, used for configurations and for channel variables.
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:1091
static struct ast_threadstorage sql_buf
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
static char * table
Definition: cdr_odbc.c:58
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:1065
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
struct sla_ringing_trunk * first
Definition: app_meetme.c:1092
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
int error(const char *format,...)
Definition: utils/frame.c:999
#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:861
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ realtime_update_handler()

static int realtime_update_handler ( const char *  database,
const char *  table,
const char *  keyfield,
const char *  entity,
const struct ast_variable fields 
)
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
fieldslist of parameters and new values to update in the database
Return values
thenumber of affected rows.
-1if an error occurred.

Definition at line 1214 of file res_config_sqlite.c.

References ast_debug, ast_log, ast_mutex_lock, ast_mutex_unlock, db, error(), LOG_WARNING, mutex, ast_variable::name, ast_variable::next, NULL, RES_CONFIG_SQLITE_BEGIN, RES_CONFIG_SQLITE_END, S_OR, and ast_variable::value.

1216 {
1217  char *query, *errormsg = NULL, *tmp_str;
1218  const struct ast_variable *field = fields;
1219  int error, rows_num;
1220 
1221  if (!table) {
1222  ast_log(LOG_WARNING, "Table name unspecified\n");
1223  return -1;
1224  }
1225 
1226  if (!field) {
1227  return -1;
1228  }
1229 
1230 /* \cond DOXYGEN_CAN_PARSE_THIS */
1231 #undef QUERY
1232 #define QUERY "UPDATE '%q' SET %q = '%q'"
1233 /* \endcond */
1234 
1235  if (!(query = sqlite_mprintf(QUERY, table, field->name, field->value))) {
1236  ast_log(LOG_WARNING, "Unable to allocate SQL query\n");
1237  return -1;
1238  }
1239 
1240  while ((field = field->next)) {
1241  tmp_str = sqlite_mprintf("%s, %q = '%q'", query, field->name, field->value);
1242  sqlite_freemem(query);
1243 
1244  if (!tmp_str) {
1245  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1246  return -1;
1247  }
1248 
1249  query = tmp_str;
1250  }
1251 
1252  if (!(tmp_str = sqlite_mprintf("%s WHERE %q = '%q';", query, keyfield, entity))) {
1253  ast_log(LOG_WARNING, "Unable to reallocate SQL query\n");
1254  sqlite_freemem(query);
1255  return -1;
1256  }
1257 
1258  sqlite_freemem(query);
1259  query = tmp_str;
1260  ast_debug(1, "SQL query: %s\n", query);
1261 
1263 
1265  error = sqlite_exec(db, query, NULL, NULL, &errormsg);
1266  RES_CONFIG_SQLITE_END(error)
1267 
1268  if (!error)
1269  rows_num = sqlite_changes(db);
1270  else
1271  rows_num = -1;
1272 
1274 
1275  sqlite_freemem(query);
1276 
1277  if (error) {
1278  ast_log(LOG_WARNING, "%s\n", S_OR(errormsg, sqlite_error_string(error)));
1279  }
1280  sqlite_freemem(errormsg);
1281 
1282  return rows_num;
1283 }
struct ast_variable * next
#define RES_CONFIG_SQLITE_BEGIN
#define LOG_WARNING
Definition: logger.h:274
static int entity
Definition: isdn_lib.c:259
Structure for variables, used for configurations and for channel variables.
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
static char * table
Definition: cdr_odbc.c:58
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
int error(const char *format,...)
Definition: utils/frame.c:999
#define RES_CONFIG_SQLITE_END(error)
static ast_mutex_t mutex
static sqlite * db
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ set_var()

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 695 of file res_config_sqlite.c.

References ast_free, ast_log, ast_strdup, and LOG_WARNING.

696 {
697  if (*var)
698  ast_free(*var);
699 
700  *var = ast_strdup(value);
701 
702  if (!*var) {
703  ast_log(LOG_WARNING, "Unable to allocate variable %s\n", name);
704  return 1;
705  }
706 
707  return 0;
708 }
#define LOG_WARNING
Definition: logger.h:274
#define var
Definition: ast_expr2f.c:614
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
int value
Definition: syslog.c:37
#define ast_log
Definition: astobj2.c:42
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182

◆ unload_config()

static void unload_config ( void  )
static

Free resources related to configuration.

See also
load_config()

Definition at line 758 of file res_config_sqlite.c.

References ast_free, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, cdr_table, config_table, dbfile, free_table(), and NULL.

Referenced by load_config(), and unload_module().

759 {
760  struct sqlite_cache_tables *tbl;
761  ast_free(dbfile);
762  dbfile = NULL;
764  config_table = NULL;
766  cdr_table = NULL;
768  while ((tbl = AST_RWLIST_REMOVE_HEAD(&sqlite_tables, list))) {
769  free_table(tbl);
770  }
772 }
#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 NULL
Definition: resample.c:96
static char * dbfile
static char * config_table
#define ast_free(a)
Definition: astmm.h:182
static char * cdr_table
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:843

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 1633 of file res_config_sqlite.c.

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

Referenced by load_module().

1634 {
1636  return -1;
1637  }
1638 
1639  if (cli_status_registered) {
1641  }
1642 
1644 
1645  if (db)
1646  sqlite_close(db);
1647 
1648  unload_config();
1649 
1650  return 0;
1651 }
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:2988
#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: clicompat.c:30
static void unload_config(void)
Free resources related to configuration.
int ast_config_engine_deregister(struct ast_config_engine *del)
Deregister config engine.
Definition: main/config.c:3006
static struct ast_config_engine sqlite_engine
static int cli_status_registered
static struct ast_cli_entry cli_status[]
static int cdr_registered
static sqlite * db

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_DEPRECATED, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, .requires = "extconfig", }
static

Definition at line 1787 of file res_config_sqlite.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 1787 of file res_config_sqlite.c.

◆ cdr_registered

int cdr_registered
static

Set to 1 if the CDR callback function was registered.

Definition at line 450 of file res_config_sqlite.c.

Referenced by load_module(), and unload_module().

◆ cdr_table

char* cdr_table
static

The name of the table used to store CDR entries.

Definition at line 462 of file res_config_sqlite.c.

Referenced by cdr_handler(), check_vars(), handle_cli_show_sqlite_status(), load_config(), load_module(), and unload_config().

◆ cli_status

struct ast_cli_entry cli_status[]
static
Initial value:
= {
{ .handler = handle_cli_show_sqlite_status , .summary = "Show status information about the SQLite 2 driver" ,},
{ .handler = handle_cli_sqlite_show_tables , .summary = "Cached table information about the SQLite 2 driver" ,},
}
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 491 of file res_config_sqlite.c.

◆ cli_status_registered

int cli_status_registered
static

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

Definition at line 453 of file res_config_sqlite.c.

Referenced by load_module(), and unload_module().

◆ config_table

char* config_table
static

The name of the static configuration table.

Definition at line 459 of file res_config_sqlite.c.

Referenced by config_handler(), handle_cli_show_sqlite_status(), load_config(), load_module(), realtime_handler(), realtime_multi_handler(), and unload_config().

◆ db

sqlite* db
static

◆ dbfile

char* dbfile
static

The path of the database file.

Definition at line 456 of file res_config_sqlite.c.

Referenced by check_vars(), handle_cli_show_sqlite_status(), load_config(), load_module(), and unload_config().

◆ mutex

ast_mutex_t mutex = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
static

◆ sql_buf

struct ast_threadstorage sql_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_sql_buf , .custom_init = NULL , }
static

Definition at line 135 of file res_config_sqlite.c.

Referenced by realtime_update2_handler().

◆ sql_create_cdr_table

char* sql_create_cdr_table
static

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

Definition at line 516 of file res_config_sqlite.c.

Referenced by load_module().

◆ sqlite_engine

struct ast_config_engine sqlite_engine
static

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

Definition at line 468 of file res_config_sqlite.c.

◆ sqlite_tables

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

◆ use_cdr

int use_cdr
static

Set to 1 if CDR support is enabled.

Definition at line 447 of file res_config_sqlite.c.

Referenced by check_vars(), and load_module().

◆ where_buf

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

Definition at line 136 of file res_config_sqlite.c.

Referenced by realtime_update2_handler().