Mon Apr 28 2014 10:06:40

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 <signal.h>
#include <dirent.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/lock.h"
#include "asterisk/manager.h"
#include "db1-ast/include/db.h"
Include dependency graph for db.c:

Go to the source code of this file.

Defines

#define MAX_DB_FIELD   256

Typedefs

typedef int(* process_keys_cb )(DBT *key, DBT *value, const char *filter, void *data)

Functions

int ast_db_del (const char *family, const char *keys)
 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 *keys, char *value, int valuelen)
 Get key value specified by family/key.
int ast_db_get_allocated (const char *family, const char *keys, 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 *keys, const char *value)
 Store value addressed by family/key.
int astdb_init (void)
static void astdb_shutdown (void)
static int db_deltree_cb (DBT *key, DBT *value, const char *filter, void *data)
static int db_get_common (const char *family, const char *keys, char **buffer, int bufferlen)
static int db_gettree_cb (DBT *key, DBT *value, const char *filter, void *data)
static int db_show_cb (DBT *key, DBT *value, const char *filter, void *data)
static int db_showkey_cb (DBT *key, DBT *value, const char *filter, void *data)
static void db_sync (void)
static void * db_sync_thread (void *data)
static int dbinit (void)
static const char * dbt_data2str (DBT *dbt)
static const char * dbt_data2str_full (DBT *dbt, const char *def)
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_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 keymatch (const char *key, const char *prefix)
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)
static int process_db_keys (process_keys_cb cb, void *data, const char *filter, int sync)
static int subkeymatch (const char *key, const char *suffix)

Variables

static DB * 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 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


Typedef Documentation

typedef int(* process_keys_cb)(DBT *key, DBT *value, const char *filter, void *data)

Definition at line 114 of file db.c.


Function Documentation

int ast_db_del ( const char *  family,
const char *  keys 
)

Delete entry in astdb.

Definition at line 365 of file db.c.

References ast_debug, ast_mutex_lock, ast_mutex_unlock, db_sync(), dbinit(), dblock, 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];
   DBT key;
   int res, fullkeylen;

   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }
   
   fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
   memset(&key, 0, sizeof(key));
   key.data = fullkey;
   key.size = fullkeylen + 1;
   
   res = astdb->del(astdb, &key, 0);
   db_sync();
   
   ast_mutex_unlock(&dblock);

   if (res) {
      ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
   }
   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 241 of file db.c.

References db_deltree_cb(), MAX_DB_FIELD, prefix, and process_db_keys().

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

{
   char prefix[MAX_DB_FIELD];

   if (family) {
      if (keytree) {
         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
      } else {
         snprintf(prefix, sizeof(prefix), "/%s", family);
      }
   } else if (keytree) {
      return -1;
   } else {
      prefix[0] = '\0';
   }

   return process_db_keys(db_deltree_cb, NULL, prefix, 1);
}
void ast_db_freetree ( struct ast_db_entry dbe)

Free structure created by ast_db_gettree()

Definition at line 656 of file db.c.

References ast_free, last, and ast_db_entry::next.

Referenced by handle_cli_devstate_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 *  keys,
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 358 of file db.c.

References db_get_common().

Referenced by reload_queue_members().

{
   *out = NULL;

   return db_get_common(family, keys, 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 631 of file db.c.

References ast_log(), ast_strlen_zero(), db_gettree_cb(), LOG_WARNING, MAX_DB_FIELD, prefix, and process_db_keys().

Referenced by handle_cli_devstate_list(), load_module(), process_clearcache(), and reload_queue_members().

{
   char prefix[MAX_DB_FIELD];
   struct ast_db_entry *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';
   }

   if (process_db_keys(db_gettree_cb, &ret, prefix, 0) < 0) {
      ast_log(LOG_WARNING, "Database unavailable\n");
      return NULL;
   }

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

Store value addressed by family/key.

Definition at line 260 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, db_sync(), dbinit(), 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_command_response(), handle_dbput(), handle_pri_service_generic(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), pri_dchannel(), save_secret(), and update_registry().

{
   char fullkey[MAX_DB_FIELD];
   DBT key, data;
   int res, fullkeylen;

   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }

   fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   key.data = fullkey;
   key.size = fullkeylen + 1;
   data.data = (char *) value;
   data.size = strlen(value) + 1;
   res = astdb->put(astdb, &key, &data, 0);
   db_sync();
   ast_mutex_unlock(&dblock);
   if (res)
      ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);

   return res;
}
static void astdb_shutdown ( void  ) [static]

Definition at line 854 of file db.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_manager_unregister(), ast_mutex_lock, ast_mutex_unlock, 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");

   ast_mutex_lock(&dblock);
   doexit = 1;
   db_sync();
   ast_mutex_unlock(&dblock);

   pthread_join(syncthread, NULL);

#if defined(DEBUG_FD_LEAKS) && defined(close)
/* DEBUG_FD_LEAKS causes conflicting define of close() in asterisk.h */
#undef close
#endif

   if (astdb) {
      astdb->close(astdb);
      astdb = NULL;
   }
}
static int db_deltree_cb ( DBT *  key,
DBT *  value,
const char *  filter,
void *  data 
) [static]

Definition at line 230 of file db.c.

References dbt_data2str_full(), and keymatch().

Referenced by ast_db_deltree().

{
   int res = 0;

   if (keymatch(dbt_data2str_full(key, "<bad key>"), filter)) {
      astdb->del(astdb, key, 0);
      res = 1;
   }
   return res;
}
static int db_get_common ( const char *  family,
const char *  keys,
char **  buffer,
int  bufferlen 
) [static]

Definition at line 302 of file db.c.

References ast_copy_string(), ast_debug, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_strdup, dbinit(), dblock, LOG_NOTICE, and MAX_DB_FIELD.

Referenced by ast_db_get(), and ast_db_get_allocated().

{
   char fullkey[MAX_DB_FIELD] = "";
   DBT key, data;
   int res, fullkeylen;

   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }

   fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   key.data = fullkey;
   key.size = fullkeylen + 1;

   res = astdb->get(astdb, &key, &data, 0);

   /* Be sure to NULL terminate our data either way */
   if (res) {
      ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
   } else {
      if (data.size) {
         ((char *)data.data)[data.size - 1] = '\0';

         if (bufferlen == -1) {
            *buffer = ast_strdup(data.data);
         } else {
            /* Make sure that we don't write too much to the dst pointer or we don't
             * read too much from the source pointer */
            ast_copy_string(*buffer, data.data, bufferlen > data.size ? data.size : bufferlen);
         }
      } else {
         ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
      }
   }

   /* Data is not fully isolated for concurrency, so the lock must be extended
    * to after the copy to the output buffer. */
   ast_mutex_unlock(&dblock);

   return res;
}
static int db_gettree_cb ( DBT *  key,
DBT *  value,
const char *  filter,
void *  data 
) [static]

Definition at line 611 of file db.c.

References ast_malloc, ast_db_entry::data, dbt_data2str_full(), ast_db_entry::key, keymatch(), and ast_db_entry::next.

Referenced by ast_db_gettree().

{
   struct ast_db_entry **ret = data;
   struct ast_db_entry *cur;
   const char *key_s = dbt_data2str_full(key, "<bad key>");
   const char *value_s = dbt_data2str_full(value, "<bad value>");
   size_t key_slen = strlen(key_s) + 1, value_slen = strlen(value_s) + 1;

   if (keymatch(key_s, filter) && (cur = ast_malloc(sizeof(*cur) + key_slen + value_slen))) {
      cur->next = *ret;
      cur->key = cur->data + value_slen;
      strcpy(cur->data, value_s);
      strcpy(cur->key, key_s);
      *ret = cur;
      return 1;
   }

   return 0;
}
static int db_show_cb ( DBT *  key,
DBT *  value,
const char *  filter,
void *  data 
) [static]

Definition at line 510 of file db.c.

References ast_cli(), dbt_data2str_full(), ast_cli_args::fd, and keymatch().

Referenced by handle_cli_database_show().

{
   struct ast_cli_args *a = data;
   const char *key_s = dbt_data2str_full(key, "<bad key>");
   const char *value_s = dbt_data2str_full(value, "<bad value>");

   if (keymatch(key_s, filter)) {
      ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
      return 1;
   }

   return 0;
}
static int db_showkey_cb ( DBT *  key,
DBT *  value,
const char *  filter,
void *  data 
) [static]

Definition at line 565 of file db.c.

References ast_cli(), dbt_data2str_full(), ast_cli_args::fd, and subkeymatch().

Referenced by handle_cli_database_showkey().

{
   struct ast_cli_args *a = data;
   const char *key_s = dbt_data2str_full(key, "<bad key>");
   const char *value_s = dbt_data2str_full(value, "<bad value>");

   if (subkeymatch(key_s, filter)) {
      ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
      return 1;
   }

   return 0;
}
static void db_sync ( void  ) [static]

Definition at line 800 of file db.c.

References ast_cond_signal.

Referenced by ast_db_del(), ast_db_put(), astdb_shutdown(), and process_db_keys().

static void* db_sync_thread ( void *  data) [static]

Definition at line 815 of file db.c.

References ast_assert, ast_cond_wait, ast_mutex_lock, ast_mutex_unlock, and dblock.

Referenced by astdb_init().

{
   ast_mutex_lock(&dblock);
   for (;;) {
      ast_cond_wait(&dbcond, &dblock);
      if (doexit) {
         /*
          * We were likely awakened just to exit.  Sync anyway just in
          * case.
          */
         if (astdb) {
            astdb->sync(astdb, 0);
         }
         ast_mutex_unlock(&dblock);
         break;
      }

      ast_mutex_unlock(&dblock);
      /*
       * Sleep so if we have a bunch of db puts in a row, they won't
       * get written one at a time to the db but in a batch.
       */
      sleep(1);
      ast_mutex_lock(&dblock);

      /* The db should be successfully opened to get here. */
      ast_assert(astdb != NULL);
      astdb->sync(astdb, 0);

      if (doexit) {
         /* We were asked to exit while sleeping. */
         ast_mutex_unlock(&dblock);
         break;
      }
   }

   return NULL;
}
static int dbinit ( void  ) [static]

Definition at line 118 of file db.c.

References ast_config_AST_DB, ast_log(), errno, and LOG_WARNING.

Referenced by ast_db_del(), ast_db_put(), astdb_init(), db_get_common(), load_module(), and process_db_keys().

{
   if (doexit) {
      return -1;
   }
   if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
      ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
      return -1;
   }
   return 0;
}
static const char* dbt_data2str ( DBT *  dbt) [static]

Definition at line 157 of file db.c.

Referenced by dbt_data2str_full().

{
   char *data = "";

   if (dbt->size) {
      data = dbt->data;
      data[dbt->size - 1] = '\0';
   }

   return data;
}
static const char* dbt_data2str_full ( DBT *  dbt,
const char *  def 
) [inline, static]

Definition at line 169 of file db.c.

References dbt_data2str(), and S_OR.

Referenced by db_deltree_cb(), db_gettree_cb(), db_show_cb(), db_showkey_cb(), and process_db_keys().

{
   return S_OR(dbt_data2str(dbt), def);
}
static char* handle_cli_database_del ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 448 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 475 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 420 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 393 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_show ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 524 of file db.c.

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

{
   char prefix[MAX_DB_FIELD];
   int counter = 0;

   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';
   } else {
      return CLI_SHOWUSAGE;
   }

   if((counter = process_db_keys(db_show_cb, a, prefix, 0)) < 0) {
      ast_cli(a->fd, "Database unavailable\n");
      return CLI_SUCCESS;
   }

   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 579 of file db.c.

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

{
   char suffix[MAX_DB_FIELD];
   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) {
      /* Key only */
      snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
   } else {
      return CLI_SHOWUSAGE;
   }

   if ((counter = process_db_keys(db_showkey_cb, a, suffix, 0)) < 0) {
      ast_cli(a->fd, "Database unavailable\n");
      return CLI_SUCCESS;
   }

   ast_cli(a->fd, "%d results found.\n", counter);
   return CLI_SUCCESS;
}
static int keymatch ( const char *  key,
const char *  prefix 
) [inline, static]

Definition at line 130 of file db.c.

Referenced by db_deltree_cb(), db_gettree_cb(), and db_show_cb().

{
   int preflen = strlen(prefix);
   if (!preflen)
      return 1;
   if (!strcasecmp(key, prefix))
      return 1;
   if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
      if (key[preflen] == '/')
         return 1;
   }
   return 0;
}
static int manager_dbdel ( struct mansession s,
const struct message m 
) [static]

Definition at line 741 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 766 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 700 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 675 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;
}
static int process_db_keys ( process_keys_cb  cb,
void *  data,
const char *  filter,
int  sync 
) [static]

Definition at line 184 of file db.c.

References ast_mutex_lock, ast_mutex_unlock, db_sync(), dbinit(), dblock, dbt_data2str_full(), last, MAX_DB_FIELD, and MIN.

Referenced by ast_db_deltree(), ast_db_gettree(), handle_cli_database_show(), and handle_cli_database_showkey().

{
   DBT key = { 0, }, value = { 0, }, last_key = { 0, };
   int counter = 0;
   int res, last = 0;
   char last_key_s[MAX_DB_FIELD];

   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }

   /* Somehow, the database can become corrupted such that astdb->seq will continue looping through
    * the database indefinitely. The pointer to last_key.data ends up getting re-used by the BDB lib
    * so this specifically queries for the last entry, makes a copy of the key, and then uses it as
    * a sentinel to avoid repeatedly looping over the list. */

   if (astdb->seq(astdb, &last_key, &value, R_LAST)) {
      /* Empty database */
      ast_mutex_unlock(&dblock);
      return 0;
   }

   memcpy(last_key_s, last_key.data, MIN(last_key.size - 1, sizeof(last_key_s)));
   last_key_s[last_key.size - 1] = '\0';
   for (res = astdb->seq(astdb, &key, &value, R_FIRST);
         !res;
         res = astdb->seq(astdb, &key, &value, R_NEXT)) {
      /* The callback might delete the key, so we have to check it before calling */
      last = !strcmp(dbt_data2str_full(&key, "<bad key>"), last_key_s);
      counter += cb(&key, &value, filter, data);
      if (last) {
         break;
      }
   }

   if (sync) {
      db_sync();
   }

   ast_mutex_unlock(&dblock);

   return counter;
}
static int subkeymatch ( const char *  key,
const char *  suffix 
) [inline, static]

Definition at line 144 of file db.c.

Referenced by db_showkey_cb().

{
   int suffixlen = strlen(suffix);
   if (suffixlen) {
      const char *subkey = key + strlen(key) - suffixlen;
      if (subkey < key)
         return 0;
      if (!strcasecmp(subkey, suffix))
         return 1;
   }
   return 0;
}

Variable Documentation

DB* astdb [static]

Definition at line 109 of file db.c.

struct ast_cli_entry cli_database[] [static]

Definition at line 666 of file db.c.

ast_cond_t dbcond [static]

Definition at line 111 of file db.c.

ast_mutex_t dblock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 } [static]
int doexit [static]

Definition at line 113 of file db.c.

Referenced by ast_monitor_change_fname().

pthread_t syncthread [static]

Definition at line 112 of file db.c.