Sat Apr 26 2014 22:02:46

Asterisk developer's documentation


db.c File Reference

ASTdb Management. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/paths.h"
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <dirent.h>
#include <sqlite3.h>
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/app.h"
#include "asterisk/dsp.h"
#include "asterisk/astdb.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/manager.h"
Include dependency graph for db.c:

Go to the source code of this file.

Defines

#define DEFINE_SQL_STATEMENT(stmt, sql)
#define MAX_DB_FIELD   256

Functions

static int ast_db_begin_transaction (void)
static int ast_db_commit_transaction (void)
int ast_db_del (const char *family, const char *key)
 Delete entry in astdb.
int ast_db_deltree (const char *family, const char *keytree)
 Delete one or more entries in astdb If both parameters are NULL, the entire database will be purged. If only keytree is NULL, all entries within the family will be purged. It is an error for keytree to have a value when family is NULL.
void ast_db_freetree (struct ast_db_entry *dbe)
 Free structure created by ast_db_gettree()
int ast_db_get (const char *family, const char *key, char *value, int valuelen)
 Get key value specified by family/key.
int ast_db_get_allocated (const char *family, const char *key, char **out)
 Get key value specified by family/key as a heap allocated string.
struct ast_db_entryast_db_gettree (const char *family, const char *keytree)
 Get a list of values within the astdb tree If family is specified, only those keys will be returned. If keytree is specified, subkeys are expected to exist (separated from the key with a slash). If subkeys do not exist and keytree is specified, the tree will consist of either a single entry or NULL will be returned.
int ast_db_put (const char *family, const char *key, const char *value)
 Store value addressed by family/key.
static int ast_db_rollback_transaction (void)
static void astdb_atexit (void)
int astdb_init (void)
static void clean_statements (void)
static int clean_stmt (sqlite3_stmt **stmt, const char *sql)
static int convert_bdb_to_sqlite3 (void)
static int db_create_astdb (void)
static int db_execute_sql (const char *sql, int(*callback)(void *, int, char **, char **), void *arg)
static int db_get_common (const char *family, const char *key, char **buffer, int bufferlen)
static int db_init (void)
static int db_open (void)
static void db_sync (void)
static void * db_sync_thread (void *data)
 DEFINE_SQL_STATEMENT (put_stmt,"INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)")
static int display_results (void *arg, int columns, char **values, char **colnames)
static char * handle_cli_database_del (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_deltree (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_get (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_put (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_query (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_showkey (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int init_statements (void)
static int manager_dbdel (struct mansession *s, const struct message *m)
static int manager_dbdeltree (struct mansession *s, const struct message *m)
static int manager_dbget (struct mansession *s, const struct message *m)
static int manager_dbput (struct mansession *s, const struct message *m)

Variables

static sqlite3 * astdb
static struct ast_cli_entry cli_database []
static ast_cond_t dbcond
static ast_mutex_t dblock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 }
static int doexit
static int dosync
static pthread_t syncthread

Detailed Description

ASTdb Management.

Author:
Mark Spencer <markster@digium.com>
Note:
DB3 is licensed under Sleepycat Public License and is thus incompatible with GPL. To avoid having to make another exception (and complicate licensing even further) we elect to use DB1 which is BSD licensed

Definition in file db.c.


Define Documentation

#define DEFINE_SQL_STATEMENT (   stmt,
  sql 
)
Value:
static sqlite3_stmt *stmt; \
   const char stmt##_sql[] = sql;

Definition at line 119 of file db.c.


Function Documentation

static int ast_db_begin_transaction ( void  ) [static]

Definition at line 296 of file db.c.

References db_execute_sql().

Referenced by db_sync_thread().

{
   return db_execute_sql("BEGIN TRANSACTION", NULL, NULL);
}
static int ast_db_commit_transaction ( void  ) [static]

Definition at line 301 of file db.c.

References db_execute_sql().

Referenced by db_sync_thread().

{
   return db_execute_sql("COMMIT", NULL, NULL);
}
int ast_db_del ( const char *  family,
const char *  key 
)

Delete entry in astdb.

Definition at line 413 of file db.c.

References ast_debug, ast_log(), ast_mutex_lock, ast_mutex_unlock, db_sync(), dblock, LOG_WARNING, and MAX_DB_FIELD.

Referenced by __expire_registry(), ast_privacy_set(), auth_exec(), cache_lookup_internal(), del_exec(), destroy_all_channels(), destroy_association(), dialgroup_refreshdb(), dump_queue_members(), function_db_delete(), handle_cli_database_del(), handle_dbdel(), handle_pri_service_generic(), manager_dbdel(), mkintf(), pri_dchannel(), process_clearcache(), reload_queue_members(), and update_registry().

{
   char fullkey[MAX_DB_FIELD];
   size_t fullkey_len;
   int res = 0;

   if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
      ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
      return -1;
   }

   fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);

   ast_mutex_lock(&dblock);
   if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
      ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
      res = -1;
   }
   sqlite3_reset(del_stmt);
   db_sync();
   ast_mutex_unlock(&dblock);

   return res;
}
int ast_db_deltree ( const char *  family,
const char *  keytree 
)

Delete one or more entries in astdb If both parameters are NULL, the entire database will be purged. If only keytree is NULL, all entries within the family will be purged. It is an error for keytree to have a value when family is NULL.

Return values:
-1An error occurred
>=0 Number of records deleted

Definition at line 441 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), db_sync(), dblock, LOG_WARNING, MAX_DB_FIELD, and prefix.

Referenced by ast_privacy_reset(), deltree_exec(), dundi_flush(), handle_cli_database_deltree(), handle_dbdeltree(), iax_provision_reload(), and manager_dbdeltree().

{
   sqlite3_stmt *stmt = deltree_stmt;
   char prefix[MAX_DB_FIELD];
   int res = 0;

   if (!ast_strlen_zero(family)) {
      if (!ast_strlen_zero(keytree)) {
         /* Family and key tree */
         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
      } else {
         /* Family only */
         snprintf(prefix, sizeof(prefix), "/%s", family);
      }
   } else {
      prefix[0] = '\0';
      stmt = deltree_all_stmt;
   }

   ast_mutex_lock(&dblock);
   if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
      ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_step(stmt) != SQLITE_DONE) {
      ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   }
   res = sqlite3_changes(astdb);
   sqlite3_reset(stmt);
   db_sync();
   ast_mutex_unlock(&dblock);

   return res;
}
void ast_db_freetree ( struct ast_db_entry dbe)

Free structure created by ast_db_gettree()

Definition at line 531 of file db.c.

References ast_free, last, and ast_db_entry::next.

Referenced by dundi_show_cache(), dundi_show_hints(), function_db_keys(), handle_cli_devstate_list(), handle_cli_presencestate_list(), load_module(), process_clearcache(), and reload_queue_members().

{
   struct ast_db_entry *last;
   while (dbe) {
      last = dbe;
      dbe = dbe->next;
      ast_free(last);
   }
}
int ast_db_get ( const char *  family,
const char *  key,
char *  value,
int  valuelen 
)
int ast_db_get_allocated ( const char *  family,
const char *  key,
char **  out 
)

Get key value specified by family/key as a heap allocated string.

Given a family and key, sets out to a pointer to a heap allocated string. In the event of an error, out will be set to NULL. The string must be freed by calling ast_free().

Return values:
-1An error occurred
0Success

Definition at line 406 of file db.c.

References db_get_common().

Referenced by reload_queue_members().

{
   *out = NULL;

   return db_get_common(family, key, out, -1);
}
struct ast_db_entry* ast_db_gettree ( const char *  family,
const char *  keytree 
) [read]

Get a list of values within the astdb tree If family is specified, only those keys will be returned. If keytree is specified, subkeys are expected to exist (separated from the key with a slash). If subkeys do not exist and keytree is specified, the tree will consist of either a single entry or NULL will be returned.

Resulting tree should be freed by passing the return value to ast_db_freetree() when usage is concluded.

Definition at line 476 of file db.c.

References ast_log(), ast_malloc, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), ast_db_entry::data, dblock, ast_db_entry::key, last, LOG_WARNING, MAX_DB_FIELD, ast_db_entry::next, and prefix.

Referenced by dundi_show_cache(), dundi_show_hints(), function_db_keys(), handle_cli_devstate_list(), handle_cli_presencestate_list(), load_module(), process_clearcache(), and reload_queue_members().

{
   char prefix[MAX_DB_FIELD];
   sqlite3_stmt *stmt = gettree_stmt;
   struct ast_db_entry *cur, *last = NULL, *ret = NULL;

   if (!ast_strlen_zero(family)) {
      if (!ast_strlen_zero(keytree)) {
         /* Family and key tree */
         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
      } else {
         /* Family only */
         snprintf(prefix, sizeof(prefix), "/%s", family);
      }
   } else {
      prefix[0] = '\0';
      stmt = gettree_all_stmt;
   }

   ast_mutex_lock(&dblock);
   if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
      ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
      sqlite3_reset(stmt);
      ast_mutex_unlock(&dblock);
      return NULL;
   }

   while (sqlite3_step(stmt) == SQLITE_ROW) {
      const char *key_s, *value_s;
      if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
         break;
      }
      if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
         break;
      }
      if (!(cur = ast_malloc(sizeof(*cur) + strlen(key_s) + strlen(value_s) + 2))) {
         break;
      }
      cur->next = NULL;
      cur->key = cur->data + strlen(value_s) + 1;
      strcpy(cur->data, value_s);
      strcpy(cur->key, key_s);
      if (last) {
         last->next = cur;
      } else {
         ret = cur;
      }
      last = cur;
   }
   sqlite3_reset(stmt);
   ast_mutex_unlock(&dblock);

   return ret;
}
int ast_db_put ( const char *  family,
const char *  key,
const char *  value 
)

Store value addressed by family/key.

Definition at line 311 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, db_sync(), dblock, LOG_WARNING, and MAX_DB_FIELD.

Referenced by __analog_ss_thread(), ast_privacy_set(), cache_save(), cache_save_hint(), database_increment(), devstate_write(), dialgroup_refreshdb(), dump_queue_members(), function_db_write(), handle_cli_database_put(), handle_cli_devstate_change(), handle_cli_presencestate_change(), handle_command_response(), handle_dbput(), handle_pri_service_generic(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), presence_write(), pri_dchannel(), save_secret(), and update_registry().

{
   char fullkey[MAX_DB_FIELD];
   size_t fullkey_len;
   int res = 0;

   if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
      ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
      return -1;
   }

   fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);

   ast_mutex_lock(&dblock);
   if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
      ast_log(LOG_WARNING, "Couldn't execute statment: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   }

   sqlite3_reset(put_stmt);
   db_sync();
   ast_mutex_unlock(&dblock);

   return res;
}
static int ast_db_rollback_transaction ( void  ) [static]

Definition at line 306 of file db.c.

References db_execute_sql().

Referenced by db_sync_thread().

{
   return db_execute_sql("ROLLBACK", NULL, NULL);
}
static void astdb_atexit ( void  ) [static]

Definition at line 987 of file db.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_manager_unregister(), ast_mutex_lock, ast_mutex_unlock, clean_statements(), db_sync(), and dblock.

Referenced by astdb_init().

{
   ast_cli_unregister_multiple(cli_database, ARRAY_LEN(cli_database));
   ast_manager_unregister("DBGet");
   ast_manager_unregister("DBPut");
   ast_manager_unregister("DBDel");
   ast_manager_unregister("DBDelTree");

   /* Set doexit to 1 to kill thread. db_sync must be called with
    * mutex held. */
   ast_mutex_lock(&dblock);
   doexit = 1;
   db_sync();
   ast_mutex_unlock(&dblock);

   pthread_join(syncthread, NULL);
   ast_mutex_lock(&dblock);
   clean_statements();
   if (sqlite3_close(astdb) == SQLITE_OK) {
      astdb = NULL;
   }
   ast_mutex_unlock(&dblock);
}
static void clean_statements ( void  ) [static]

Definition at line 164 of file db.c.

References clean_stmt().

Referenced by astdb_atexit().

{
   clean_stmt(&get_stmt, get_stmt_sql);
   clean_stmt(&del_stmt, del_stmt_sql);
   clean_stmt(&deltree_stmt, deltree_stmt_sql);
   clean_stmt(&deltree_all_stmt, deltree_all_stmt_sql);
   clean_stmt(&gettree_stmt, gettree_stmt_sql);
   clean_stmt(&gettree_all_stmt, gettree_all_stmt_sql);
   clean_stmt(&showkey_stmt, showkey_stmt_sql);
   clean_stmt(&put_stmt, put_stmt_sql);
   clean_stmt(&create_astdb_stmt, create_astdb_stmt_sql);
}
static int clean_stmt ( sqlite3_stmt **  stmt,
const char *  sql 
) [static]

Definition at line 149 of file db.c.

References ast_log(), and LOG_WARNING.

Referenced by clean_statements().

{
   if (sqlite3_finalize(*stmt) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't finalize statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
      *stmt = NULL;
      return -1;
   }
   *stmt = NULL;
   return 0;
}
static int convert_bdb_to_sqlite3 ( void  ) [static]

Definition at line 191 of file db.c.

References ast_asprintf, ast_config_AST_DB, ast_config_AST_SBIN_DIR, ast_free, and ast_safe_system().

Referenced by db_open().

{
   char *cmd;
   int res;

   ast_asprintf(&cmd, "%s/astdb2sqlite3 '%s'\n", ast_config_AST_SBIN_DIR, ast_config_AST_DB);
   res = ast_safe_system(cmd);
   ast_free(cmd);

   return res;
}
static int db_create_astdb ( void  ) [static]

Definition at line 203 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, db_sync(), dblock, and LOG_WARNING.

Referenced by db_init().

{
   int res = 0;

   if (!create_astdb_stmt) {
      init_stmt(&create_astdb_stmt, create_astdb_stmt_sql, sizeof(create_astdb_stmt_sql));
   }

   ast_mutex_lock(&dblock);
   if (sqlite3_step(create_astdb_stmt) != SQLITE_DONE) {
      ast_log(LOG_WARNING, "Couldn't create astdb table: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   }
   sqlite3_reset(create_astdb_stmt);
   db_sync();
   ast_mutex_unlock(&dblock);

   return res;
}
static int db_execute_sql ( const char *  sql,
int(*)(void *, int, char **, char **)  callback,
void *  arg 
) [static]

Definition at line 281 of file db.c.

References ast_log(), and LOG_WARNING.

Referenced by ast_db_begin_transaction(), ast_db_commit_transaction(), ast_db_rollback_transaction(), and handle_cli_database_query().

{
   char *errmsg = NULL;
   int res =0;

   sqlite3_exec(astdb, sql, callback, arg, &errmsg);
   if (errmsg) {
      ast_log(LOG_WARNING, "Error executing SQL: %s\n", errmsg);
      sqlite3_free(errmsg);
      res = -1;
   }

   return res;
}
static int db_get_common ( const char *  family,
const char *  key,
char **  buffer,
int  bufferlen 
) [static]

Definition at line 357 of file db.c.

References ast_copy_string(), ast_debug, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_strdup, dblock, LOG_WARNING, MAX_DB_FIELD, and value.

Referenced by ast_db_get(), and ast_db_get_allocated().

{
   const unsigned char *result;
   char fullkey[MAX_DB_FIELD];
   size_t fullkey_len;
   int res = 0;

   if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
      ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
      return -1;
   }

   fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);

   ast_mutex_lock(&dblock);
   if (sqlite3_bind_text(get_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_step(get_stmt) != SQLITE_ROW) {
      ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
      res = -1;
   } else if (!(result = sqlite3_column_text(get_stmt, 0))) {
      ast_log(LOG_WARNING, "Couldn't get value\n");
      res = -1;
   } else {
      const char *value = (const char *) result;

      if (bufferlen == -1) {
         *buffer = ast_strdup(value);
      } else {
         ast_copy_string(*buffer, value, bufferlen);
      }
   }
   sqlite3_reset(get_stmt);
   ast_mutex_unlock(&dblock);

   return res;
}
static int db_init ( void  ) [static]

Definition at line 265 of file db.c.

References db_create_astdb(), db_open(), and init_statements().

Referenced by astdb_init().

{
   if (astdb) {
      return 0;
   }

   if (db_open() || db_create_astdb() || init_statements()) {
      return -1;
   }

   return 0;
}
static int db_open ( void  ) [static]

Definition at line 223 of file db.c.

References ast_alloca, ast_config_AST_DB, ast_log(), ast_mutex_lock, ast_mutex_unlock, convert_bdb_to_sqlite3(), dblock, dbname, LOG_ERROR, LOG_NOTICE, and LOG_WARNING.

Referenced by db_init().

{
   char *dbname;
   struct stat dont_care;

   if (!(dbname = ast_alloca(strlen(ast_config_AST_DB) + sizeof(".sqlite3")))) {
      return -1;
   }
   strcpy(dbname, ast_config_AST_DB);
   strcat(dbname, ".sqlite3");

   if (stat(dbname, &dont_care) && !stat(ast_config_AST_DB, &dont_care)) {
      if (convert_bdb_to_sqlite3()) {
         ast_log(LOG_ERROR, "*** Database conversion failed!\n");
         ast_log(LOG_ERROR, "*** Asterisk now uses SQLite3 for its internal\n");
         ast_log(LOG_ERROR, "*** database. Conversion from the old astdb\n");
         ast_log(LOG_ERROR, "*** failed. Most likely the astdb2sqlite3 utility\n");
         ast_log(LOG_ERROR, "*** was not selected for build. To convert the\n");
         ast_log(LOG_ERROR, "*** old astdb, please delete '%s'\n", dbname);
         ast_log(LOG_ERROR, "*** and re-run 'make menuselect' and select astdb2sqlite3\n");
         ast_log(LOG_ERROR, "*** in the Utilities section, then 'make && make install'.\n");
         ast_log(LOG_ERROR, "*** It is also imperative that the user under which\n");
         ast_log(LOG_ERROR, "*** Asterisk runs have write permission to the directory\n");
         ast_log(LOG_ERROR, "*** where the database resides.\n");
         sleep(5);
      } else {
         ast_log(LOG_NOTICE, "Database conversion succeeded!\n");
      }
   }

   ast_mutex_lock(&dblock);
   if (sqlite3_open(dbname, &astdb) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(astdb));
      sqlite3_close(astdb);
      ast_mutex_unlock(&dblock);
      return -1;
   }
   ast_mutex_unlock(&dblock);

   return 0;
}
static void db_sync ( void  ) [static]
static void* db_sync_thread ( void *  data) [static]

Definition at line 957 of file db.c.

References ast_cond_wait, ast_db_begin_transaction(), ast_db_commit_transaction(), ast_db_rollback_transaction(), ast_mutex_lock, ast_mutex_unlock, and dblock.

Referenced by astdb_init().

{
   ast_mutex_lock(&dblock);
   ast_db_begin_transaction();
   for (;;) {
      /* If dosync is set, db_sync() was called during sleep(1), 
       * and the pending transaction should be committed. 
       * Otherwise, block until db_sync() is called.
       */
      while (!dosync) {
         ast_cond_wait(&dbcond, &dblock);
      }
      dosync = 0;
      if (ast_db_commit_transaction()) {
         ast_db_rollback_transaction();
      }
      if (doexit) {
         ast_mutex_unlock(&dblock);
         break;
      }
      ast_db_begin_transaction();
      ast_mutex_unlock(&dblock);
      sleep(1);
      ast_mutex_lock(&dblock);
   }

   return NULL;
}
DEFINE_SQL_STATEMENT ( put_stmt  ,
"INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)"   
)

Definition at line 122 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, dblock, and LOG_WARNING.

{
   ast_mutex_lock(&dblock);
   if (sqlite3_prepare(astdb, sql, len, stmt, NULL) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't prepare statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
      ast_mutex_unlock(&dblock);
      return -1;
   }
   ast_mutex_unlock(&dblock);

   return 0;
}
static int display_results ( void *  arg,
int  columns,
char **  values,
char **  colnames 
) [static]

Definition at line 767 of file db.c.

References ast_cli(), columns, and ast_cli_args::fd.

Referenced by handle_cli_database_query().

{
   struct ast_cli_args *a = arg;
   size_t x;

   for (x = 0; x < columns; x++) {
      ast_cli(a->fd, "%-5s: %-50s\n", colnames[x], values[x]);
   }
   ast_cli(a->fd, "\n");

   return 0;
}
static char* handle_cli_database_del ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 596 of file db.c.

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

{
   int res;

   switch (cmd) {
   case CLI_INIT:
      e->command = "database del";
      e->usage =
         "Usage: database del <family> <key>\n"
         "       Deletes an entry in the Asterisk database for a given\n"
         "       family and key.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc != 4)
      return CLI_SHOWUSAGE;
   res = ast_db_del(a->argv[2], a->argv[3]);
   if (res) {
      ast_cli(a->fd, "Database entry does not exist.\n");
   } else {
      ast_cli(a->fd, "Database entry removed.\n");
   }
   return CLI_SUCCESS;
}
static char* handle_cli_database_deltree ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 623 of file db.c.

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

{
   int num_deleted;

   switch (cmd) {
   case CLI_INIT:
      e->command = "database deltree";
      e->usage =
         "Usage: database deltree <family> [keytree]\n"
         "   OR: database deltree <family>[/keytree]\n"
         "       Deletes a family or specific keytree within a family\n"
         "       in the Asterisk database.  The two arguments may be\n"
         "       separated by either a space or a slash.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if ((a->argc < 3) || (a->argc > 4))
      return CLI_SHOWUSAGE;
   if (a->argc == 4) {
      num_deleted = ast_db_deltree(a->argv[2], a->argv[3]);
   } else {
      num_deleted = ast_db_deltree(a->argv[2], NULL);
   }
   if (num_deleted < 0) {
      ast_cli(a->fd, "Database unavailable.\n");
   } else if (num_deleted == 0) {
      ast_cli(a->fd, "Database entries do not exist.\n");
   } else {
      ast_cli(a->fd, "%d database entries removed.\n",num_deleted);
   }
   return CLI_SUCCESS;
}
static char* handle_cli_database_get ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 568 of file db.c.

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

{
   int res;
   char tmp[MAX_DB_FIELD];

   switch (cmd) {
   case CLI_INIT:
      e->command = "database get";
      e->usage =
         "Usage: database get <family> <key>\n"
         "       Retrieves an entry in the Asterisk database for a given\n"
         "       family and key.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc != 4)
      return CLI_SHOWUSAGE;
   res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
   if (res) {
      ast_cli(a->fd, "Database entry not found.\n");
   } else {
      ast_cli(a->fd, "Value: %s\n", tmp);
   }
   return CLI_SUCCESS;
}
static char* handle_cli_database_put ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 541 of file db.c.

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

{
   int res;

   switch (cmd) {
   case CLI_INIT:
      e->command = "database put";
      e->usage =
         "Usage: database put <family> <key> <value>\n"
         "       Adds or updates an entry in the Asterisk database for\n"
         "       a given family, key, and value.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc != 5)
      return CLI_SHOWUSAGE;
   res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
   if (res)  {
      ast_cli(a->fd, "Failed to update entry\n");
   } else {
      ast_cli(a->fd, "Updated database successfully\n");
   }
   return CLI_SUCCESS;
}
static char* handle_cli_database_query ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 780 of file db.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_mutex_lock, ast_mutex_unlock, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, db_execute_sql(), db_sync(), dblock, display_results(), and ast_cli_entry::usage.

{

   switch (cmd) {
   case CLI_INIT:
      e->command = "database query";
      e->usage =
         "Usage: database query \"<SQL Statement>\"\n"
         "       Run a user-specified SQL query on the database. Be careful.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc != 3) {
      return CLI_SHOWUSAGE;
   }

   ast_mutex_lock(&dblock);
   db_execute_sql(a->argv[2], display_results, a);
   db_sync(); /* Go ahead and sync the db in case they write */
   ast_mutex_unlock(&dblock);

   return CLI_SUCCESS;
}
static char* handle_cli_database_show ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 658 of file db.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, dblock, ast_cli_args::fd, LOG_WARNING, MAX_DB_FIELD, prefix, and ast_cli_entry::usage.

{
   char prefix[MAX_DB_FIELD];
   int counter = 0;
   sqlite3_stmt *stmt = gettree_stmt;

   switch (cmd) {
   case CLI_INIT:
      e->command = "database show";
      e->usage =
         "Usage: database show [family [keytree]]\n"
         "   OR: database show [family[/keytree]]\n"
         "       Shows Asterisk database contents, optionally restricted\n"
         "       to a given family, or family and keytree. The two arguments\n"
         "       may be separated either by a space or by a slash.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc == 4) {
      /* Family and key tree */
      snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
   } else if (a->argc == 3) {
      /* Family only */
      snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
   } else if (a->argc == 2) {
      /* Neither */
      prefix[0] = '\0';
      stmt = gettree_all_stmt;

   } else {
      return CLI_SHOWUSAGE;
   }

   ast_mutex_lock(&dblock);
   if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
      ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
      sqlite3_reset(stmt);
      ast_mutex_unlock(&dblock);
      return NULL;
   }

   while (sqlite3_step(stmt) == SQLITE_ROW) {
      const char *key_s, *value_s;
      if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
         ast_log(LOG_WARNING, "Skipping invalid key!\n");
         continue;
      }
      if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
         ast_log(LOG_WARNING, "Skipping invalid value!\n");
         continue;
      }
      ++counter;
      ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
   }

   sqlite3_reset(stmt);
   ast_mutex_unlock(&dblock);

   ast_cli(a->fd, "%d results found.\n", counter);
   return CLI_SUCCESS;
}
static char* handle_cli_database_showkey ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 722 of file db.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, dblock, ast_cli_args::fd, LOG_WARNING, and ast_cli_entry::usage.

{
   int counter = 0;

   switch (cmd) {
   case CLI_INIT:
      e->command = "database showkey";
      e->usage =
         "Usage: database showkey <keytree>\n"
         "       Shows Asterisk database contents, restricted to a given key.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc != 3) {
      return CLI_SHOWUSAGE;
   }

   ast_mutex_lock(&dblock);
   if (!ast_strlen_zero(a->argv[2]) && (sqlite3_bind_text(showkey_stmt, 1, a->argv[2], -1, SQLITE_STATIC) != SQLITE_OK)) {
      ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", a->argv[2], sqlite3_errmsg(astdb));
      sqlite3_reset(showkey_stmt);
      ast_mutex_unlock(&dblock);
      return NULL;
   }

   while (sqlite3_step(showkey_stmt) == SQLITE_ROW) {
      const char *key_s, *value_s;
      if (!(key_s = (const char *) sqlite3_column_text(showkey_stmt, 0))) {
         break;
      }
      if (!(value_s = (const char *) sqlite3_column_text(showkey_stmt, 1))) {
         break;
      }
      ++counter;
      ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
   }
   sqlite3_reset(showkey_stmt);
   ast_mutex_unlock(&dblock);

   ast_cli(a->fd, "%d results found.\n", counter);
   return CLI_SUCCESS;
}
static int init_statements ( void  ) [static]

Definition at line 177 of file db.c.

Referenced by db_init().

{
   /* Don't initialize create_astdb_statment here as the astdb table needs to exist
    * brefore these statments can be initialized */
   return init_stmt(&get_stmt, get_stmt_sql, sizeof(get_stmt_sql))
   || init_stmt(&del_stmt, del_stmt_sql, sizeof(del_stmt_sql))
   || init_stmt(&deltree_stmt, deltree_stmt_sql, sizeof(deltree_stmt_sql))
   || init_stmt(&deltree_all_stmt, deltree_all_stmt_sql, sizeof(deltree_all_stmt_sql))
   || init_stmt(&gettree_stmt, gettree_stmt_sql, sizeof(gettree_stmt_sql))
   || init_stmt(&gettree_all_stmt, gettree_all_stmt_sql, sizeof(gettree_all_stmt_sql))
   || init_stmt(&showkey_stmt, showkey_stmt_sql, sizeof(showkey_stmt_sql))
   || init_stmt(&put_stmt, put_stmt_sql, sizeof(put_stmt_sql));
}
static int manager_dbdel ( struct mansession s,
const struct message m 
) [static]

Definition at line 882 of file db.c.

References ast_db_del(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), and astman_send_error().

Referenced by astdb_init().

{
   const char *family = astman_get_header(m, "Family");
   const char *key = astman_get_header(m, "Key");
   int res;

   if (ast_strlen_zero(family)) {
      astman_send_error(s, m, "No family specified.");
      return 0;
   }

   if (ast_strlen_zero(key)) {
      astman_send_error(s, m, "No key specified.");
      return 0;
   }

   res = ast_db_del(family, key);
   if (res)
      astman_send_error(s, m, "Database entry not found");
   else
      astman_send_ack(s, m, "Key deleted successfully");

   return 0;
}
static int manager_dbdeltree ( struct mansession s,
const struct message m 
) [static]

Definition at line 907 of file db.c.

References ast_db_deltree(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), and astman_send_error().

Referenced by astdb_init().

{
   const char *family = astman_get_header(m, "Family");
   const char *key = astman_get_header(m, "Key");
   int num_deleted;

   if (ast_strlen_zero(family)) {
      astman_send_error(s, m, "No family specified.");
      return 0;
   }

   if (!ast_strlen_zero(key)) {
      num_deleted = ast_db_deltree(family, key);
   } else {
      num_deleted = ast_db_deltree(family, NULL);
   }

   if (num_deleted < 0) {
      astman_send_error(s, m, "Database unavailable");
   } else if (num_deleted == 0) {
      astman_send_error(s, m, "Database entry not found");
   } else {
      astman_send_ack(s, m, "Key tree deleted successfully");
   }

   return 0;
}
static int manager_dbget ( struct mansession s,
const struct message m 
) [static]

Definition at line 841 of file db.c.

References ast_db_get(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_ack(), astman_send_error(), and MAX_DB_FIELD.

Referenced by astdb_init().

{
   const char *id = astman_get_header(m,"ActionID");
   char idText[256] = "";
   const char *family = astman_get_header(m, "Family");
   const char *key = astman_get_header(m, "Key");
   char tmp[MAX_DB_FIELD];
   int res;

   if (ast_strlen_zero(family)) {
      astman_send_error(s, m, "No family specified.");
      return 0;
   }
   if (ast_strlen_zero(key)) {
      astman_send_error(s, m, "No key specified.");
      return 0;
   }

   if (!ast_strlen_zero(id))
      snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);

   res = ast_db_get(family, key, tmp, sizeof(tmp));
   if (res) {
      astman_send_error(s, m, "Database entry not found");
   } else {
      astman_send_ack(s, m, "Result will follow");
      astman_append(s, "Event: DBGetResponse\r\n"
            "Family: %s\r\n"
            "Key: %s\r\n"
            "Val: %s\r\n"
            "%s"
            "\r\n",
            family, key, tmp, idText);
      astman_append(s, "Event: DBGetComplete\r\n"
            "%s"
            "\r\n",
            idText);
   }
   return 0;
}
static int manager_dbput ( struct mansession s,
const struct message m 
) [static]

Definition at line 816 of file db.c.

References ast_db_put(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), and S_OR.

Referenced by astdb_init().

{
   const char *family = astman_get_header(m, "Family");
   const char *key = astman_get_header(m, "Key");
   const char *val = astman_get_header(m, "Val");
   int res;

   if (ast_strlen_zero(family)) {
      astman_send_error(s, m, "No family specified");
      return 0;
   }
   if (ast_strlen_zero(key)) {
      astman_send_error(s, m, "No key specified");
      return 0;
   }

   res = ast_db_put(family, key, S_OR(val, ""));
   if (res) {
      astman_send_error(s, m, "Failed to update entry");
   } else {
      astman_send_ack(s, m, "Updated database successfully");
   }
   return 0;
}

Variable Documentation

sqlite3* astdb [static]

Definition at line 112 of file db.c.

struct ast_cli_entry cli_database[] [static]

Definition at line 806 of file db.c.

ast_cond_t dbcond [static]

Definition at line 111 of file db.c.

int doexit [static]

Definition at line 114 of file db.c.

Referenced by ast_monitor_change_fname().

int dosync [static]

Definition at line 115 of file db.c.

pthread_t syncthread [static]

Definition at line 113 of file db.c.