Fri Apr 29 2011 07:57:42

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.

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 a whole family (for some reason also called "tree".
void ast_db_freetree (struct ast_db_entry *dbe)
 Free in-memory data.
int ast_db_get (const char *family, const char *keys, char *value, int valuelen)
 Get key value specified by family/key.
struct ast_db_entryast_db_gettree (const char *family, const char *keytree)
 Get a whole family.
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 db_sync (void)
static void * db_sync_thread (void *data)
static int dbinit (void)
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 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 = { { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } , 1, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP }

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.


Function Documentation

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

Delete entry in astdb.

Definition at line 264 of file db.c.

References ast_debug, ast_mutex_lock, ast_mutex_unlock, db_sync(), dbinit(), and dblock.

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[256];
   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 a whole family (for some reason also called "tree".

Definition at line 146 of file db.c.

References ast_mutex_lock, ast_mutex_unlock, db_sync(), dbinit(), dblock, keymatch(), keys, pass, and prefix.

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

{
   char prefix[256];
   DBT key, data;
   char *keys;
   int res;
   int pass;
   int counter = 0;
   
   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';
   }
   
   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }
   
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   pass = 0;
   while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
      if (key.size) {
         keys = key.data;
         keys[key.size - 1] = '\0';
      } else {
         keys = "<bad key>";
      }
      if (keymatch(keys, prefix)) {
         astdb->del(astdb, &key, 0);
         counter++;
      }
   }
   db_sync();
   ast_mutex_unlock(&dblock);
   return counter;
}
void ast_db_freetree ( struct ast_db_entry dbe)

Free in-memory data.

Definition at line 590 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 
)

Get key value specified by family/key.

Definition at line 220 of file db.c.

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

Referenced by ast_privacy_check(), auth_exec(), blacklist_read(), cache_lookup_internal(), check_access(), create_addr(), custom_devstate_callback(), database_increment(), destroy_all_channels(), function_db_delete(), function_db_exists(), function_db_read(), handle_cli_database_get(), handle_dbget(), iax_provision_version(), load_password(), manager_dbget(), mkintf(), populate_addr(), reg_source_db(), and reload_queue_members().

{
   char fullkey[256] = "";
   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));
   memset(value, 0, valuelen);
   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 0
      printf("Got value of size %d\n", data.size);
#endif
      if (data.size) {
         ((char *)data.data)[data.size - 1] = '\0';
         /* 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(value, data.data, (valuelen > data.size) ? data.size : valuelen);
      } 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;
}
struct ast_db_entry* ast_db_gettree ( const char *  family,
const char *  keytree 
) [read]

Get a whole family.

Definition at line 528 of file db.c.

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

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

{
   char prefix[256];
   DBT key, data;
   char *keys, *values;
   int values_len;
   int res;
   int pass;
   struct ast_db_entry *last = NULL;
   struct ast_db_entry *cur, *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';
   }
   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      ast_log(LOG_WARNING, "Database unavailable\n");
      return NULL;   
   }
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   pass = 0;
   while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
      if (key.size) {
         keys = key.data;
         keys[key.size - 1] = '\0';
      } else {
         keys = "<bad key>";
      }
      if (data.size) {
         values = data.data;
         values[data.size - 1] = '\0';
      } else {
         values = "<bad value>";
      }
      values_len = strlen(values) + 1;
      if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
         cur->next = NULL;
         cur->key = cur->data + values_len;
         strcpy(cur->data, values);
         strcpy(cur->key, keys);
         if (last) {
            last->next = cur;
         } else {
            ret = cur;
         }
         last = cur;
      }
   }
   ast_mutex_unlock(&dblock);
   return ret; 
}
int ast_db_put ( const char *  family,
const char *  keys,
const char *  value 
)

Store value addressed by family/key.

Definition at line 193 of file db.c.

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

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[256];
   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 db_sync ( void  ) [static]

Definition at line 730 of file db.c.

References ast_cond_signal.

Referenced by ast_db_del(), ast_db_deltree(), and ast_db_put().

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

Definition at line 745 of file db.c.

References 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);
      ast_mutex_unlock(&dblock);
      sleep(1);
      ast_mutex_lock(&dblock);
      astdb->sync(astdb, 0);
   }

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

Definition at line 109 of file db.c.

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

Referenced by ast_db_del(), ast_db_deltree(), ast_db_get(), ast_db_gettree(), ast_db_put(), astdb_init(), handle_cli_database_show(), handle_cli_database_showkey(), and load_module().

{
   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 char* handle_cli_database_del ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 347 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 374 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 res;

   switch (cmd) {
   case CLI_INIT:
      e->command = "database deltree";
      e->usage =
         "Usage: database deltree <family> [keytree]\n"
         "       Deletes a family or specific keytree within a family\n"
         "       in the Asterisk database.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if ((a->argc < 3) || (a->argc > 4))
      return CLI_SHOWUSAGE;
   if (a->argc == 4) {
      res = ast_db_deltree(a->argv[2], a->argv[3]);
   } else {
      res = ast_db_deltree(a->argv[2], NULL);
   }
   if (res < 0) {
      ast_cli(a->fd, "Database entries do not exist.\n");
   } else {
      ast_cli(a->fd, "%d database entries removed.\n",res);
   }
   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 319 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, and ast_cli_entry::usage.

{
   int res;
   char tmp[256];

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

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_mutex_lock, ast_mutex_unlock, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, dbinit(), dblock, ast_cli_args::fd, keymatch(), keys, pass, prefix, and ast_cli_entry::usage.

{
   char prefix[256];
   DBT key, data;
   char *keys, *values;
   int res;
   int pass;
   int counter = 0;

   switch (cmd) {
   case CLI_INIT:
      e->command = "database show";
      e->usage =
         "Usage: database show [family [keytree]]\n"
         "       Shows Asterisk database contents, optionally restricted\n"
         "       to a given family, or family and keytree.\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;
   }
   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      ast_cli(a->fd, "Database unavailable\n");
      return CLI_SUCCESS;  
   }
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   pass = 0;
   while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
      if (key.size) {
         keys = key.data;
         keys[key.size - 1] = '\0';
      } else {
         keys = "<bad key>";
      }
      if (data.size) {
         values = data.data;
         values[data.size - 1]='\0';
      } else {
         values = "<bad value>";
      }
      if (keymatch(keys, prefix)) {
         ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
         counter++;
      }
   }
   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 470 of file db.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_mutex_lock, ast_mutex_unlock, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, dbinit(), dblock, ast_cli_args::fd, keys, pass, subkeymatch(), and ast_cli_entry::usage.

{
   char suffix[256];
   DBT key, data;
   char *keys, *values;
   int res;
   int pass;
   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;
   }
   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      ast_cli(a->fd, "Database unavailable\n");
      return CLI_SUCCESS;  
   }
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   pass = 0;
   while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
      if (key.size) {
         keys = key.data;
         keys[key.size - 1] = '\0';
      } else {
         keys = "<bad key>";
      }
      if (data.size) {
         values = data.data;
         values[data.size - 1]='\0';
      } else {
         values = "<bad value>";
      }
      if (subkeymatch(keys, suffix)) {
         ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
         counter++;
      }
   }
   ast_mutex_unlock(&dblock);
   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 119 of file db.c.

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

{
   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 675 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 700 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 res;

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

   if (!ast_strlen_zero(key))
      res = ast_db_deltree(family, key);
   else
      res = ast_db_deltree(family, NULL);

   if (res < 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 634 of file db.c.

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

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[256];
   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 609 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 subkeymatch ( const char *  key,
const char *  suffix 
) [inline, static]

Definition at line 133 of file db.c.

Referenced by handle_cli_database_showkey().

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

struct ast_cli_entry cli_database[] [static]

Definition at line 600 of file db.c.

ast_cond_t dbcond [static]

Definition at line 105 of file db.c.

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