Mon Aug 31 12:30:34 2015

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"

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

#define MAX_DB_FIELD   256

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

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

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

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

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

00657 {
00658    struct ast_db_entry *last;
00659    while (dbe) {
00660       last = dbe;
00661       dbe = dbe->next;
00662       ast_free(last);
00663    }
00664 }

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

References db_get_common().

Referenced by reload_queue_members().

00359 {
00360    *out = NULL;
00361 
00362    return db_get_common(family, keys, out, -1);
00363 }

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

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

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

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

int astdb_init ( void   ) 
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().

00855 {
00856    ast_cli_unregister_multiple(cli_database, ARRAY_LEN(cli_database));
00857    ast_manager_unregister("DBGet");
00858    ast_manager_unregister("DBPut");
00859    ast_manager_unregister("DBDel");
00860    ast_manager_unregister("DBDelTree");
00861 
00862    ast_mutex_lock(&dblock);
00863    doexit = 1;
00864    db_sync();
00865    ast_mutex_unlock(&dblock);
00866 
00867    pthread_join(syncthread, NULL);
00868 
00869 #if defined(DEBUG_FD_LEAKS) && defined(close)
00870 /* DEBUG_FD_LEAKS causes conflicting define of close() in asterisk.h */
00871 #undef close
00872 #endif
00873 
00874    if (astdb) {
00875       astdb->close(astdb);
00876       astdb = NULL;
00877    }
00878 }

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

00231 {
00232    int res = 0;
00233 
00234    if (keymatch(dbt_data2str_full(key, "<bad key>"), filter)) {
00235       astdb->del(astdb, key, 0);
00236       res = 1;
00237    }
00238    return res;
00239 }

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

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

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

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

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

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

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

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

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

00801 {
00802    ast_cond_signal(&dbcond);
00803 }

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

00816 {
00817    ast_mutex_lock(&dblock);
00818    for (;;) {
00819       ast_cond_wait(&dbcond, &dblock);
00820       if (doexit) {
00821          /*
00822           * We were likely awakened just to exit.  Sync anyway just in
00823           * case.
00824           */
00825          if (astdb) {
00826             astdb->sync(astdb, 0);
00827          }
00828          ast_mutex_unlock(&dblock);
00829          break;
00830       }
00831 
00832       ast_mutex_unlock(&dblock);
00833       /*
00834        * Sleep so if we have a bunch of db puts in a row, they won't
00835        * get written one at a time to the db but in a batch.
00836        */
00837       sleep(1);
00838       ast_mutex_lock(&dblock);
00839 
00840       /* The db should be successfully opened to get here. */
00841       ast_assert(astdb != NULL);
00842       astdb->sync(astdb, 0);
00843 
00844       if (doexit) {
00845          /* We were asked to exit while sleeping. */
00846          ast_mutex_unlock(&dblock);
00847          break;
00848       }
00849    }
00850 
00851    return NULL;
00852 }

static int dbinit ( void   )  [static]

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

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

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

Definition at line 157 of file db.c.

Referenced by dbt_data2str_full().

00158 {
00159    char *data = "";
00160 
00161    if (dbt->size) {
00162       data = dbt->data;
00163       data[dbt->size - 1] = '\0';
00164    }
00165 
00166    return data;
00167 }

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

00170 {
00171    return S_OR(dbt_data2str(dbt), def);
00172 }

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Definition at line 144 of file db.c.

Referenced by db_showkey_cb().

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


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.


Generated on 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1