Fri Jan 18 19:59:13 2013

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_entry * ast_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 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 }

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 MAX_DB_FIELD   256

Typedef Documentation

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

Definition at line 112 of file db.c.


Function Documentation

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

Delete entry in astdb.

Definition at line 361 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(), manager_dbdel(), mkintf(), process_clearcache(), reload_queue_members(), and update_registry().

00362 {
00363    char fullkey[MAX_DB_FIELD];
00364    DBT key;
00365    int res, fullkeylen;
00366 
00367    ast_mutex_lock(&dblock);
00368    if (dbinit()) {
00369       ast_mutex_unlock(&dblock);
00370       return -1;
00371    }
00372    
00373    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00374    memset(&key, 0, sizeof(key));
00375    key.data = fullkey;
00376    key.size = fullkeylen + 1;
00377    
00378    res = astdb->del(astdb, &key, 0);
00379    db_sync();
00380    
00381    ast_mutex_unlock(&dblock);
00382 
00383    if (res) {
00384       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00385    }
00386    return res;
00387 }

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:
-1 An error occurred
>= 0 Number of records deleted

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

00238 {
00239    char prefix[MAX_DB_FIELD];
00240 
00241    if (family) {
00242       if (keytree) {
00243          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00244       } else {
00245          snprintf(prefix, sizeof(prefix), "/%s", family);
00246       }
00247    } else if (keytree) {
00248       return -1;
00249    } else {
00250       prefix[0] = '\0';
00251    }
00252 
00253    return process_db_keys(db_deltree_cb, NULL, prefix, 1);
00254 }

void ast_db_freetree ( struct ast_db_entry *  dbe  ) 

Free structure created by ast_db_gettree().

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

00653 {
00654    struct ast_db_entry *last;
00655    while (dbe) {
00656       last = dbe;
00657       dbe = dbe->next;
00658       ast_free(last);
00659    }
00660 }

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:
-1 An error occurred
0 Success

Definition at line 354 of file db.c.

References db_get_common().

Referenced by reload_queue_members().

00355 {
00356    *out = NULL;
00357 
00358    return db_get_common(family, keys, out, -1);
00359 }

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

00628 {
00629    char prefix[MAX_DB_FIELD];
00630    struct ast_db_entry *ret = NULL;
00631 
00632    if (!ast_strlen_zero(family)) {
00633       if (!ast_strlen_zero(keytree)) {
00634          /* Family and key tree */
00635          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00636       } else {
00637          /* Family only */
00638          snprintf(prefix, sizeof(prefix), "/%s", family);
00639       }
00640    } else {
00641       prefix[0] = '\0';
00642    }
00643 
00644    if (process_db_keys(db_gettree_cb, &ret, prefix, 0) < 0) {
00645       ast_log(LOG_WARNING, "Database unavailable\n");
00646       return NULL;
00647    }
00648 
00649    return ret;
00650 }

int ast_db_put ( const char *  family,
const char *  keys,
const char *  value 
)

Store value addressed by family/key.

Definition at line 256 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(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), save_secret(), and update_registry().

00257 {
00258    char fullkey[MAX_DB_FIELD];
00259    DBT key, data;
00260    int res, fullkeylen;
00261 
00262    ast_mutex_lock(&dblock);
00263    if (dbinit()) {
00264       ast_mutex_unlock(&dblock);
00265       return -1;
00266    }
00267 
00268    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00269    memset(&key, 0, sizeof(key));
00270    memset(&data, 0, sizeof(data));
00271    key.data = fullkey;
00272    key.size = fullkeylen + 1;
00273    data.data = (char *) value;
00274    data.size = strlen(value) + 1;
00275    res = astdb->put(astdb, &key, &data, 0);
00276    db_sync();
00277    ast_mutex_unlock(&dblock);
00278    if (res)
00279       ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
00280 
00281    return res;
00282 }

int astdb_init ( void   ) 

Provided by db.c

Definition at line 825 of file db.c.

References ARRAY_LEN, ast_cli_register_multiple(), ast_cond_init, ast_manager_register_xml, ast_pthread_create_background, db_sync_thread(), dbinit(), EVENT_FLAG_REPORTING, EVENT_FLAG_SYSTEM, manager_dbdel(), manager_dbdeltree(), manager_dbget(), and manager_dbput().

Referenced by main().

00826 {
00827    pthread_t dont_care;
00828 
00829    ast_cond_init(&dbcond, NULL);
00830    if (ast_pthread_create_background(&dont_care, NULL, db_sync_thread, NULL)) {
00831       return -1;
00832    }
00833 
00834    /* Ignore check_return warning from Coverity for dbinit below */
00835    dbinit();
00836 
00837    ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
00838    ast_manager_register_xml("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);
00839    ast_manager_register_xml("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);
00840    ast_manager_register_xml("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel);
00841    ast_manager_register_xml("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree);
00842    return 0;
00843 }

static int db_deltree_cb ( DBT *  key,
DBT *  value,
const char *  filter,
void *  data 
) [static]

Definition at line 226 of file db.c.

References dbt_data2str_full(), and keymatch().

Referenced by ast_db_deltree().

00227 {
00228    int res = 0;
00229 
00230    if (keymatch(dbt_data2str_full(key, "<bad key>"), filter)) {
00231       astdb->del(astdb, key, 0);
00232       res = 1;
00233    }
00234    return res;
00235 }

static int db_get_common ( const char *  family,
const char *  keys,
char **  buffer,
int  bufferlen 
) [static]

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

00299 {
00300    char fullkey[MAX_DB_FIELD] = "";
00301    DBT key, data;
00302    int res, fullkeylen;
00303 
00304    ast_mutex_lock(&dblock);
00305    if (dbinit()) {
00306       ast_mutex_unlock(&dblock);
00307       return -1;
00308    }
00309 
00310    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00311    memset(&key, 0, sizeof(key));
00312    memset(&data, 0, sizeof(data));
00313    key.data = fullkey;
00314    key.size = fullkeylen + 1;
00315 
00316    res = astdb->get(astdb, &key, &data, 0);
00317 
00318    /* Be sure to NULL terminate our data either way */
00319    if (res) {
00320       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00321    } else {
00322       if (data.size) {
00323          ((char *)data.data)[data.size - 1] = '\0';
00324 
00325          if (bufferlen == -1) {
00326             *buffer = ast_strdup(data.data);
00327          } else {
00328             /* Make sure that we don't write too much to the dst pointer or we don't
00329              * read too much from the source pointer */
00330             ast_copy_string(*buffer, data.data, bufferlen > data.size ? data.size : bufferlen);
00331          }
00332       } else {
00333          ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
00334       }
00335    }
00336 
00337    /* Data is not fully isolated for concurrency, so the lock must be extended
00338     * to after the copy to the output buffer. */
00339    ast_mutex_unlock(&dblock);
00340 
00341    return res;
00342 }

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

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

00608 {
00609    struct ast_db_entry **ret = data;
00610    struct ast_db_entry *cur;
00611    const char *key_s = dbt_data2str_full(key, "<bad key>");
00612    const char *value_s = dbt_data2str_full(value, "<bad value>");
00613    size_t key_slen = strlen(key_s) + 1, value_slen = strlen(value_s) + 1;
00614 
00615    if (keymatch(key_s, filter) && (cur = ast_malloc(sizeof(*cur) + key_slen + value_slen))) {
00616       cur->next = *ret;
00617       cur->key = cur->data + value_slen;
00618       strcpy(cur->data, value_s);
00619       strcpy(cur->key, key_s);
00620       *ret = cur;
00621       return 1;
00622    }
00623 
00624    return 0;
00625 }

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

Definition at line 506 of file db.c.

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

Referenced by handle_cli_database_show().

00507 {
00508    struct ast_cli_args *a = data;
00509    const char *key_s = dbt_data2str_full(key, "<bad key>");
00510    const char *value_s = dbt_data2str_full(value, "<bad value>");
00511 
00512    if (keymatch(key_s, filter)) {
00513       ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
00514       return 1;
00515    }
00516 
00517    return 0;
00518 }

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

Definition at line 561 of file db.c.

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

Referenced by handle_cli_database_showkey().

00562 {
00563    struct ast_cli_args *a = data;
00564    const char *key_s = dbt_data2str_full(key, "<bad key>");
00565    const char *value_s = dbt_data2str_full(value, "<bad value>");
00566 
00567    if (subkeymatch(key_s, filter)) {
00568       ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
00569       return 1;
00570    }
00571 
00572    return 0;
00573 }

static void db_sync ( void   )  [static]

Definition at line 796 of file db.c.

References ast_cond_signal.

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

00797 {
00798    ast_cond_signal(&dbcond);
00799 }

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

Definition at line 811 of file db.c.

References ast_cond_wait, ast_mutex_lock, ast_mutex_unlock, and dblock.

Referenced by astdb_init().

00812 {
00813    ast_mutex_lock(&dblock);
00814    for (;;) {
00815       ast_cond_wait(&dbcond, &dblock);
00816       ast_mutex_unlock(&dblock);
00817       sleep(1);
00818       ast_mutex_lock(&dblock);
00819       astdb->sync(astdb, 0);
00820    }
00821 
00822    return NULL;
00823 }

static int dbinit ( void   )  [static]

Definition at line 116 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_put(), astdb_init(), db_get_common(), load_module(), and process_db_keys().

00117 {
00118    if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
00119       ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
00120       return -1;
00121    }
00122    return 0;
00123 }

static const char* dbt_data2str ( DBT *  dbt  )  [static]

Definition at line 153 of file db.c.

Referenced by dbt_data2str_full().

00154 {
00155    char *data = "";
00156 
00157    if (dbt->size) {
00158       data = dbt->data;
00159       data[dbt->size - 1] = '\0';
00160    }
00161 
00162    return data;
00163 }

static const char* dbt_data2str_full ( DBT *  dbt,
const char *  def 
) [inline, static]

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

00166 {
00167    return S_OR(dbt_data2str(dbt), def);
00168 }

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

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

00445 {
00446    int res;
00447 
00448    switch (cmd) {
00449    case CLI_INIT:
00450       e->command = "database del";
00451       e->usage =
00452          "Usage: database del <family> <key>\n"
00453          "       Deletes an entry in the Asterisk database for a given\n"
00454          "       family and key.\n";
00455       return NULL;
00456    case CLI_GENERATE:
00457       return NULL;
00458    }
00459 
00460    if (a->argc != 4)
00461       return CLI_SHOWUSAGE;
00462    res = ast_db_del(a->argv[2], a->argv[3]);
00463    if (res) {
00464       ast_cli(a->fd, "Database entry does not exist.\n");
00465    } else {
00466       ast_cli(a->fd, "Database entry removed.\n");
00467    }
00468    return CLI_SUCCESS;
00469 }

static char* handle_cli_database_deltree ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

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

00472 {
00473    int num_deleted;
00474 
00475    switch (cmd) {
00476    case CLI_INIT:
00477       e->command = "database deltree";
00478       e->usage =
00479          "Usage: database deltree <family> [keytree]\n"
00480          "   OR: database deltree <family>[/keytree]\n"
00481          "       Deletes a family or specific keytree within a family\n"
00482          "       in the Asterisk database.  The two arguments may be\n"
00483          "       separated by either a space or a slash.\n";
00484       return NULL;
00485    case CLI_GENERATE:
00486       return NULL;
00487    }
00488 
00489    if ((a->argc < 3) || (a->argc > 4))
00490       return CLI_SHOWUSAGE;
00491    if (a->argc == 4) {
00492       num_deleted = ast_db_deltree(a->argv[2], a->argv[3]);
00493    } else {
00494       num_deleted = ast_db_deltree(a->argv[2], NULL);
00495    }
00496    if (num_deleted < 0) {
00497       ast_cli(a->fd, "Database unavailable.\n");
00498    } else if (num_deleted == 0) {
00499       ast_cli(a->fd, "Database entries do not exist.\n");
00500    } else {
00501       ast_cli(a->fd, "%d database entries removed.\n",num_deleted);
00502    }
00503    return CLI_SUCCESS;
00504 }

static char* handle_cli_database_get ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

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

00417 {
00418    int res;
00419    char tmp[MAX_DB_FIELD];
00420 
00421    switch (cmd) {
00422    case CLI_INIT:
00423       e->command = "database get";
00424       e->usage =
00425          "Usage: database get <family> <key>\n"
00426          "       Retrieves an entry in the Asterisk database for a given\n"
00427          "       family and key.\n";
00428       return NULL;
00429    case CLI_GENERATE:
00430       return NULL;
00431    }
00432 
00433    if (a->argc != 4)
00434       return CLI_SHOWUSAGE;
00435    res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
00436    if (res) {
00437       ast_cli(a->fd, "Database entry not found.\n");
00438    } else {
00439       ast_cli(a->fd, "Value: %s\n", tmp);
00440    }
00441    return CLI_SUCCESS;
00442 }

static char* handle_cli_database_put ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

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

00390 {
00391    int res;
00392 
00393    switch (cmd) {
00394    case CLI_INIT:
00395       e->command = "database put";
00396       e->usage =
00397          "Usage: database put <family> <key> <value>\n"
00398          "       Adds or updates an entry in the Asterisk database for\n"
00399          "       a given family, key, and value.\n";
00400       return NULL;
00401    case CLI_GENERATE:
00402       return NULL;
00403    }
00404 
00405    if (a->argc != 5)
00406       return CLI_SHOWUSAGE;
00407    res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
00408    if (res)  {
00409       ast_cli(a->fd, "Failed to update entry\n");
00410    } else {
00411       ast_cli(a->fd, "Updated database successfully\n");
00412    }
00413    return CLI_SUCCESS;
00414 }

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

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

00521 {
00522    char prefix[MAX_DB_FIELD];
00523    int counter = 0;
00524 
00525    switch (cmd) {
00526    case CLI_INIT:
00527       e->command = "database show";
00528       e->usage =
00529          "Usage: database show [family [keytree]]\n"
00530          "   OR: database show [family[/keytree]]\n"
00531          "       Shows Asterisk database contents, optionally restricted\n"
00532          "       to a given family, or family and keytree. The two arguments\n"
00533          "       may be separated either by a space or by a slash.\n";
00534       return NULL;
00535    case CLI_GENERATE:
00536       return NULL;
00537    }
00538 
00539    if (a->argc == 4) {
00540       /* Family and key tree */
00541       snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
00542    } else if (a->argc == 3) {
00543       /* Family only */
00544       snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
00545    } else if (a->argc == 2) {
00546       /* Neither */
00547       prefix[0] = '\0';
00548    } else {
00549       return CLI_SHOWUSAGE;
00550    }
00551 
00552    if((counter = process_db_keys(db_show_cb, a, prefix, 0)) < 0) {
00553       ast_cli(a->fd, "Database unavailable\n");
00554       return CLI_SUCCESS;
00555    }
00556 
00557    ast_cli(a->fd, "%d results found.\n", counter);
00558    return CLI_SUCCESS;
00559 }

static char* handle_cli_database_showkey ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

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

00576 {
00577    char suffix[MAX_DB_FIELD];
00578    int counter = 0;
00579 
00580    switch (cmd) {
00581    case CLI_INIT:
00582       e->command = "database showkey";
00583       e->usage =
00584          "Usage: database showkey <keytree>\n"
00585          "       Shows Asterisk database contents, restricted to a given key.\n";
00586       return NULL;
00587    case CLI_GENERATE:
00588       return NULL;
00589    }
00590 
00591    if (a->argc == 3) {
00592       /* Key only */
00593       snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
00594    } else {
00595       return CLI_SHOWUSAGE;
00596    }
00597 
00598    if ((counter = process_db_keys(db_showkey_cb, a, suffix, 0)) < 0) {
00599       ast_cli(a->fd, "Database unavailable\n");
00600       return CLI_SUCCESS;
00601    }
00602 
00603    ast_cli(a->fd, "%d results found.\n", counter);
00604    return CLI_SUCCESS;
00605 }

static int keymatch ( const char *  key,
const char *  prefix 
) [inline, static]

Definition at line 126 of file db.c.

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

00127 {
00128    int preflen = strlen(prefix);
00129    if (!preflen)
00130       return 1;
00131    if (!strcasecmp(key, prefix))
00132       return 1;
00133    if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
00134       if (key[preflen] == '/')
00135          return 1;
00136    }
00137    return 0;
00138 }

static int manager_dbdel ( struct mansession *  s,
const struct message *  m 
) [static]

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

00738 {
00739    const char *family = astman_get_header(m, "Family");
00740    const char *key = astman_get_header(m, "Key");
00741    int res;
00742 
00743    if (ast_strlen_zero(family)) {
00744       astman_send_error(s, m, "No family specified.");
00745       return 0;
00746    }
00747 
00748    if (ast_strlen_zero(key)) {
00749       astman_send_error(s, m, "No key specified.");
00750       return 0;
00751    }
00752 
00753    res = ast_db_del(family, key);
00754    if (res)
00755       astman_send_error(s, m, "Database entry not found");
00756    else
00757       astman_send_ack(s, m, "Key deleted successfully");
00758 
00759    return 0;
00760 }

static int manager_dbdeltree ( struct mansession *  s,
const struct message *  m 
) [static]

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

00763 {
00764    const char *family = astman_get_header(m, "Family");
00765    const char *key = astman_get_header(m, "Key");
00766    int num_deleted;
00767 
00768    if (ast_strlen_zero(family)) {
00769       astman_send_error(s, m, "No family specified.");
00770       return 0;
00771    }
00772 
00773    if (!ast_strlen_zero(key)) {
00774       num_deleted = ast_db_deltree(family, key);
00775    } else {
00776       num_deleted = ast_db_deltree(family, NULL);
00777    }
00778 
00779    if (num_deleted < 0) {
00780       astman_send_error(s, m, "Database unavailable");
00781    } else if (num_deleted == 0) {
00782       astman_send_error(s, m, "Database entry not found");
00783    } else {
00784       astman_send_ack(s, m, "Key tree deleted successfully");
00785    }
00786 
00787    return 0;
00788 }

static int manager_dbget ( struct mansession *  s,
const struct message *  m 
) [static]

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

00697 {
00698    const char *id = astman_get_header(m,"ActionID");
00699    char idText[256] = "";
00700    const char *family = astman_get_header(m, "Family");
00701    const char *key = astman_get_header(m, "Key");
00702    char tmp[MAX_DB_FIELD];
00703    int res;
00704 
00705    if (ast_strlen_zero(family)) {
00706       astman_send_error(s, m, "No family specified.");
00707       return 0;
00708    }
00709    if (ast_strlen_zero(key)) {
00710       astman_send_error(s, m, "No key specified.");
00711       return 0;
00712    }
00713 
00714    if (!ast_strlen_zero(id))
00715       snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
00716 
00717    res = ast_db_get(family, key, tmp, sizeof(tmp));
00718    if (res) {
00719       astman_send_error(s, m, "Database entry not found");
00720    } else {
00721       astman_send_ack(s, m, "Result will follow");
00722       astman_append(s, "Event: DBGetResponse\r\n"
00723             "Family: %s\r\n"
00724             "Key: %s\r\n"
00725             "Val: %s\r\n"
00726             "%s"
00727             "\r\n",
00728             family, key, tmp, idText);
00729       astman_append(s, "Event: DBGetComplete\r\n"
00730             "%s"
00731             "\r\n",
00732             idText);
00733    }
00734    return 0;
00735 }

static int manager_dbput ( struct mansession *  s,
const struct message *  m 
) [static]

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

00672 {
00673    const char *family = astman_get_header(m, "Family");
00674    const char *key = astman_get_header(m, "Key");
00675    const char *val = astman_get_header(m, "Val");
00676    int res;
00677 
00678    if (ast_strlen_zero(family)) {
00679       astman_send_error(s, m, "No family specified");
00680       return 0;
00681    }
00682    if (ast_strlen_zero(key)) {
00683       astman_send_error(s, m, "No key specified");
00684       return 0;
00685    }
00686 
00687    res = ast_db_put(family, key, S_OR(val, ""));
00688    if (res) {
00689       astman_send_error(s, m, "Failed to update entry");
00690    } else {
00691       astman_send_ack(s, m, "Updated database successfully");
00692    }
00693    return 0;
00694 }

static int process_db_keys ( process_keys_cb  cb,
void *  data,
const char *  filter,
int  sync 
) [static]

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

00181 {
00182    DBT key = { 0, }, value = { 0, }, last_key = { 0, };
00183    int counter = 0;
00184    int res, last = 0;
00185    char last_key_s[MAX_DB_FIELD];
00186 
00187    ast_mutex_lock(&dblock);
00188    if (dbinit()) {
00189       ast_mutex_unlock(&dblock);
00190       return -1;
00191    }
00192 
00193    /* Somehow, the database can become corrupted such that astdb->seq will continue looping through
00194     * the database indefinitely. The pointer to last_key.data ends up getting re-used by the BDB lib
00195     * so this specifically queries for the last entry, makes a copy of the key, and then uses it as
00196     * a sentinel to avoid repeatedly looping over the list. */
00197 
00198    if (astdb->seq(astdb, &last_key, &value, R_LAST)) {
00199       /* Empty database */
00200       ast_mutex_unlock(&dblock);
00201       return 0;
00202    }
00203 
00204    memcpy(last_key_s, last_key.data, MIN(last_key.size - 1, sizeof(last_key_s)));
00205    last_key_s[last_key.size - 1] = '\0';
00206    for (res = astdb->seq(astdb, &key, &value, R_FIRST);
00207          !res;
00208          res = astdb->seq(astdb, &key, &value, R_NEXT)) {
00209       /* The callback might delete the key, so we have to check it before calling */
00210       last = !strcmp(dbt_data2str_full(&key, "<bad key>"), last_key_s);
00211       counter += cb(&key, &value, filter, data);
00212       if (last) {
00213          break;
00214       }
00215    }
00216 
00217    if (sync) {
00218       db_sync();
00219    }
00220 
00221    ast_mutex_unlock(&dblock);
00222 
00223    return counter;
00224 }

static int subkeymatch ( const char *  key,
const char *  suffix 
) [inline, static]

Definition at line 140 of file db.c.

Referenced by db_showkey_cb().

00141 {
00142    int suffixlen = strlen(suffix);
00143    if (suffixlen) {
00144       const char *subkey = key + strlen(key) - suffixlen;
00145       if (subkey < key)
00146          return 0;
00147       if (!strcasecmp(subkey, suffix))
00148          return 1;
00149    }
00150    return 0;
00151 }


Variable Documentation

DB* astdb [static]

Definition at line 109 of file db.c.

struct ast_cli_entry cli_database[] [static]

Definition at line 662 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]

Definition at line 110 of file db.c.

Referenced by ast_db_del(), ast_db_put(), db_get_common(), db_sync_thread(), and process_db_keys().


Generated on 18 Jan 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1