Sat Mar 10 01:55:20 2012

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.
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 int db_deltree_cb (DBT *key, DBT *value, const char *filter, void *data)
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

Definition at line 103 of file db.c.

Referenced by ast_db_del(), ast_db_deltree(), ast_db_get(), ast_db_gettree(), ast_db_put(), handle_cli_database_get(), handle_cli_database_show(), handle_cli_database_showkey(), manager_dbget(), and process_db_keys().


Typedef Documentation

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

Definition at line 108 of file db.c.


Function Documentation

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

Delete entry in astdb.

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

00315 {
00316    char fullkey[MAX_DB_FIELD];
00317    DBT key;
00318    int res, fullkeylen;
00319 
00320    ast_mutex_lock(&dblock);
00321    if (dbinit()) {
00322       ast_mutex_unlock(&dblock);
00323       return -1;
00324    }
00325    
00326    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00327    memset(&key, 0, sizeof(key));
00328    key.data = fullkey;
00329    key.size = fullkeylen + 1;
00330    
00331    res = astdb->del(astdb, &key, 0);
00332    db_sync();
00333    
00334    ast_mutex_unlock(&dblock);
00335 
00336    if (res) {
00337       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00338    }
00339    return res;
00340 }

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

00224 {
00225    char prefix[MAX_DB_FIELD];
00226 
00227    if (family) {
00228       if (keytree) {
00229          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00230       } else {
00231          snprintf(prefix, sizeof(prefix), "/%s", family);
00232       }
00233    } else if (keytree) {
00234       return -1;
00235    } else {
00236       prefix[0] = '\0';
00237    }
00238 
00239    return process_db_keys(db_deltree_cb, NULL, prefix, 1);
00240 }

void ast_db_freetree ( struct ast_db_entry dbe  ) 

Free structure created by ast_db_gettree().

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

00604 {
00605    struct ast_db_entry *last;
00606    while (dbe) {
00607       last = dbe;
00608       dbe = dbe->next;
00609       ast_free(last);
00610    }
00611 }

int ast_db_get ( const char *  family,
const char *  keys,
char *  value,
int  valuelen 
)

Get key value specified by family/key.

Definition at line 270 of file db.c.

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

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(), populate_addr(), reg_source_db(), and reload_queue_members().

00271 {
00272    char fullkey[MAX_DB_FIELD] = "";
00273    DBT key, data;
00274    int res, fullkeylen;
00275 
00276    ast_mutex_lock(&dblock);
00277    if (dbinit()) {
00278       ast_mutex_unlock(&dblock);
00279       return -1;
00280    }
00281 
00282    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00283    memset(&key, 0, sizeof(key));
00284    memset(&data, 0, sizeof(data));
00285    memset(value, 0, valuelen);
00286    key.data = fullkey;
00287    key.size = fullkeylen + 1;
00288 
00289    res = astdb->get(astdb, &key, &data, 0);
00290 
00291    /* Be sure to NULL terminate our data either way */
00292    if (res) {
00293       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00294    } else {
00295 #if 0
00296       printf("Got value of size %d\n", data.size);
00297 #endif
00298       if (data.size) {
00299          ((char *)data.data)[data.size - 1] = '\0';
00300          /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
00301          ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
00302       } else {
00303          ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
00304       }
00305    }
00306 
00307    /* Data is not fully isolated for concurrency, so the lock must be extended
00308     * to after the copy to the output buffer. */
00309    ast_mutex_unlock(&dblock);
00310 
00311    return res;
00312 }

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.

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

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

00579 {
00580    char prefix[MAX_DB_FIELD];
00581    struct ast_db_entry *ret = NULL;
00582 
00583    if (!ast_strlen_zero(family)) {
00584       if (!ast_strlen_zero(keytree)) {
00585          /* Family and key tree */
00586          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00587       } else {
00588          /* Family only */
00589          snprintf(prefix, sizeof(prefix), "/%s", family);
00590       }
00591    } else {
00592       prefix[0] = '\0';
00593    }
00594 
00595    if (process_db_keys(db_gettree_cb, &ret, prefix, 0) < 0) {
00596       ast_log(LOG_WARNING, "Database unavailable\n");
00597       return NULL;
00598    }
00599 
00600    return ret;
00601 }

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

Store value addressed by family/key.

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

00243 {
00244    char fullkey[MAX_DB_FIELD];
00245    DBT key, data;
00246    int res, fullkeylen;
00247 
00248    ast_mutex_lock(&dblock);
00249    if (dbinit()) {
00250       ast_mutex_unlock(&dblock);
00251       return -1;
00252    }
00253 
00254    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00255    memset(&key, 0, sizeof(key));
00256    memset(&data, 0, sizeof(data));
00257    key.data = fullkey;
00258    key.size = fullkeylen + 1;
00259    data.data = (char *) value;
00260    data.size = strlen(value) + 1;
00261    res = astdb->put(astdb, &key, &data, 0);
00262    db_sync();
00263    ast_mutex_unlock(&dblock);
00264    if (res)
00265       ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
00266 
00267    return res;
00268 }

int astdb_init ( void   ) 

Provided by db.c

Definition at line 772 of file db.c.

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

Referenced by main().

00773 {
00774    pthread_t dont_care;
00775 
00776    ast_cond_init(&dbcond, NULL);
00777    if (ast_pthread_create_background(&dont_care, NULL, db_sync_thread, NULL)) {
00778       return -1;
00779    }
00780 
00781    dbinit();
00782    ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
00783    ast_manager_register_xml("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);
00784    ast_manager_register_xml("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);
00785    ast_manager_register_xml("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel);
00786    ast_manager_register_xml("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree);
00787    return 0;
00788 }

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

Definition at line 212 of file db.c.

References dbt_data2str_full(), and keymatch().

Referenced by ast_db_deltree().

00213 {
00214    int res = 0;
00215 
00216    if (keymatch(dbt_data2str_full(key, "<bad key>"), filter)) {
00217       astdb->del(astdb, key, 0);
00218       res = 1;
00219    }
00220    return res;
00221 }

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

Definition at line 558 of file db.c.

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

Referenced by ast_db_gettree().

00559 {
00560    struct ast_db_entry **ret = data;
00561    struct ast_db_entry *cur;
00562    const char *key_s = dbt_data2str_full(key, "<bad key>");
00563    const char *value_s = dbt_data2str_full(value, "<bad value>");
00564    size_t key_slen = strlen(key_s) + 1, value_slen = strlen(value_s) + 1;
00565 
00566    if (keymatch(key_s, filter) && (cur = ast_malloc(sizeof(*cur) + key_slen + value_slen))) {
00567       cur->next = *ret;
00568       cur->key = cur->data + value_slen;
00569       strcpy(cur->data, value_s);
00570       strcpy(cur->key, key_s);
00571       *ret = cur;
00572       return 1;
00573    }
00574 
00575    return 0;
00576 }

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

Definition at line 457 of file db.c.

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

Referenced by handle_cli_database_show().

00458 {
00459    struct ast_cli_args *a = data;
00460    const char *key_s = dbt_data2str_full(key, "<bad key>");
00461    const char *value_s = dbt_data2str_full(value, "<bad value>");
00462 
00463    if (keymatch(key_s, filter)) {
00464       ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
00465       return 1;
00466    }
00467 
00468    return 0;
00469 }

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

Definition at line 512 of file db.c.

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

Referenced by handle_cli_database_showkey().

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

static void db_sync ( void   )  [static]

Definition at line 743 of file db.c.

References ast_cond_signal.

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

00744 {
00745    ast_cond_signal(&dbcond);
00746 }

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

Definition at line 758 of file db.c.

References ast_cond_wait, ast_mutex_lock, ast_mutex_unlock, and dblock.

Referenced by astdb_init().

00759 {
00760    ast_mutex_lock(&dblock);
00761    for (;;) {
00762       ast_cond_wait(&dbcond, &dblock);
00763       ast_mutex_unlock(&dblock);
00764       sleep(1);
00765       ast_mutex_lock(&dblock);
00766       astdb->sync(astdb, 0);
00767    }
00768 
00769    return NULL;
00770 }

static int dbinit ( void   )  [static]

Definition at line 112 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_get(), ast_db_put(), astdb_init(), load_module(), and process_db_keys().

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

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

Definition at line 149 of file db.c.

Referenced by dbt_data2str_full().

00150 {
00151    char *data = "";
00152 
00153    if (dbt->size) {
00154       data = dbt->data;
00155       data[dbt->size - 1] = '\0';
00156    }
00157 
00158    return data;
00159 }

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

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

00162 {
00163    return S_OR(dbt_data2str(dbt), def);
00164 }

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

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

00398 {
00399    int res;
00400 
00401    switch (cmd) {
00402    case CLI_INIT:
00403       e->command = "database del";
00404       e->usage =
00405          "Usage: database del <family> <key>\n"
00406          "       Deletes an entry in the Asterisk database for a given\n"
00407          "       family and key.\n";
00408       return NULL;
00409    case CLI_GENERATE:
00410       return NULL;
00411    }
00412 
00413    if (a->argc != 4)
00414       return CLI_SHOWUSAGE;
00415    res = ast_db_del(a->argv[2], a->argv[3]);
00416    if (res) {
00417       ast_cli(a->fd, "Database entry does not exist.\n");
00418    } else {
00419       ast_cli(a->fd, "Database entry removed.\n");
00420    }
00421    return CLI_SUCCESS;
00422 }

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

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

00425 {
00426    int res;
00427 
00428    switch (cmd) {
00429    case CLI_INIT:
00430       e->command = "database deltree";
00431       e->usage =
00432          "Usage: database deltree <family> [keytree]\n"
00433          "   OR: database deltree <family>[/keytree]\n"
00434          "       Deletes a family or specific keytree within a family\n"
00435          "       in the Asterisk database.  The two arguments may be\n"
00436          "       separated by either a space or a slash.\n";
00437       return NULL;
00438    case CLI_GENERATE:
00439       return NULL;
00440    }
00441 
00442    if ((a->argc < 3) || (a->argc > 4))
00443       return CLI_SHOWUSAGE;
00444    if (a->argc == 4) {
00445       res = ast_db_deltree(a->argv[2], a->argv[3]);
00446    } else {
00447       res = ast_db_deltree(a->argv[2], NULL);
00448    }
00449    if (res < 0) {
00450       ast_cli(a->fd, "Database entries do not exist.\n");
00451    } else {
00452       ast_cli(a->fd, "%d database entries removed.\n",res);
00453    }
00454    return CLI_SUCCESS;
00455 }

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

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

00370 {
00371    int res;
00372    char tmp[MAX_DB_FIELD];
00373 
00374    switch (cmd) {
00375    case CLI_INIT:
00376       e->command = "database get";
00377       e->usage =
00378          "Usage: database get <family> <key>\n"
00379          "       Retrieves an entry in the Asterisk database for a given\n"
00380          "       family and key.\n";
00381       return NULL;
00382    case CLI_GENERATE:
00383       return NULL;
00384    }
00385 
00386    if (a->argc != 4)
00387       return CLI_SHOWUSAGE;
00388    res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
00389    if (res) {
00390       ast_cli(a->fd, "Database entry not found.\n");
00391    } else {
00392       ast_cli(a->fd, "Value: %s\n", tmp);
00393    }
00394    return CLI_SUCCESS;
00395 }

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

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

00343 {
00344    int res;
00345 
00346    switch (cmd) {
00347    case CLI_INIT:
00348       e->command = "database put";
00349       e->usage =
00350          "Usage: database put <family> <key> <value>\n"
00351          "       Adds or updates an entry in the Asterisk database for\n"
00352          "       a given family, key, and value.\n";
00353       return NULL;
00354    case CLI_GENERATE:
00355       return NULL;
00356    }
00357 
00358    if (a->argc != 5)
00359       return CLI_SHOWUSAGE;
00360    res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
00361    if (res)  {
00362       ast_cli(a->fd, "Failed to update entry\n");
00363    } else {
00364       ast_cli(a->fd, "Updated database successfully\n");
00365    }
00366    return CLI_SUCCESS;
00367 }

static char* handle_cli_database_show ( 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(), 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.

00472 {
00473    char prefix[MAX_DB_FIELD];
00474    int counter = 0;
00475 
00476    switch (cmd) {
00477    case CLI_INIT:
00478       e->command = "database show";
00479       e->usage =
00480          "Usage: database show [family [keytree]]\n"
00481          "   OR: database show [family[/keytree]]\n"
00482          "       Shows Asterisk database contents, optionally restricted\n"
00483          "       to a given family, or family and keytree. The two arguments\n"
00484          "       may be separated either by a space or by a slash.\n";
00485       return NULL;
00486    case CLI_GENERATE:
00487       return NULL;
00488    }
00489 
00490    if (a->argc == 4) {
00491       /* Family and key tree */
00492       snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
00493    } else if (a->argc == 3) {
00494       /* Family only */
00495       snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
00496    } else if (a->argc == 2) {
00497       /* Neither */
00498       prefix[0] = '\0';
00499    } else {
00500       return CLI_SHOWUSAGE;
00501    }
00502 
00503    if((counter = process_db_keys(db_show_cb, a, prefix, 0)) < 0) {
00504       ast_cli(a->fd, "Database unavailable\n");
00505       return CLI_SUCCESS;
00506    }
00507 
00508    ast_cli(a->fd, "%d results found.\n", counter);
00509    return CLI_SUCCESS;
00510 }

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

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

00527 {
00528    char suffix[MAX_DB_FIELD];
00529    int counter = 0;
00530 
00531    switch (cmd) {
00532    case CLI_INIT:
00533       e->command = "database showkey";
00534       e->usage =
00535          "Usage: database showkey <keytree>\n"
00536          "       Shows Asterisk database contents, restricted to a given key.\n";
00537       return NULL;
00538    case CLI_GENERATE:
00539       return NULL;
00540    }
00541 
00542    if (a->argc == 3) {
00543       /* Key only */
00544       snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
00545    } else {
00546       return CLI_SHOWUSAGE;
00547    }
00548 
00549    if ((counter = process_db_keys(db_showkey_cb, a, suffix, 0)) < 0) {
00550       ast_cli(a->fd, "Database unavailable\n");
00551       return CLI_SUCCESS;
00552    }
00553 
00554    ast_cli(a->fd, "%d results found.\n", counter);
00555    return CLI_SUCCESS;
00556 }

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

Definition at line 122 of file db.c.

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

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

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

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

00689 {
00690    const char *family = astman_get_header(m, "Family");
00691    const char *key = astman_get_header(m, "Key");
00692    int res;
00693 
00694    if (ast_strlen_zero(family)) {
00695       astman_send_error(s, m, "No family specified.");
00696       return 0;
00697    }
00698 
00699    if (ast_strlen_zero(key)) {
00700       astman_send_error(s, m, "No key specified.");
00701       return 0;
00702    }
00703 
00704    res = ast_db_del(family, key);
00705    if (res)
00706       astman_send_error(s, m, "Database entry not found");
00707    else
00708       astman_send_ack(s, m, "Key deleted successfully");
00709 
00710    return 0;
00711 }

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

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

00714 {
00715    const char *family = astman_get_header(m, "Family");
00716    const char *key = astman_get_header(m, "Key");
00717    int res;
00718 
00719    if (ast_strlen_zero(family)) {
00720       astman_send_error(s, m, "No family specified.");
00721       return 0;
00722    }
00723 
00724    if (!ast_strlen_zero(key))
00725       res = ast_db_deltree(family, key);
00726    else
00727       res = ast_db_deltree(family, NULL);
00728 
00729    if (res < 0)
00730       astman_send_error(s, m, "Database entry not found");
00731    else
00732       astman_send_ack(s, m, "Key tree deleted successfully");
00733    
00734    return 0;
00735 }

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

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

00648 {
00649    const char *id = astman_get_header(m,"ActionID");
00650    char idText[256] = "";
00651    const char *family = astman_get_header(m, "Family");
00652    const char *key = astman_get_header(m, "Key");
00653    char tmp[MAX_DB_FIELD];
00654    int res;
00655 
00656    if (ast_strlen_zero(family)) {
00657       astman_send_error(s, m, "No family specified.");
00658       return 0;
00659    }
00660    if (ast_strlen_zero(key)) {
00661       astman_send_error(s, m, "No key specified.");
00662       return 0;
00663    }
00664 
00665    if (!ast_strlen_zero(id))
00666       snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
00667 
00668    res = ast_db_get(family, key, tmp, sizeof(tmp));
00669    if (res) {
00670       astman_send_error(s, m, "Database entry not found");
00671    } else {
00672       astman_send_ack(s, m, "Result will follow");
00673       astman_append(s, "Event: DBGetResponse\r\n"
00674             "Family: %s\r\n"
00675             "Key: %s\r\n"
00676             "Val: %s\r\n"
00677             "%s"
00678             "\r\n",
00679             family, key, tmp, idText);
00680       astman_append(s, "Event: DBGetComplete\r\n"
00681             "%s"
00682             "\r\n",
00683             idText);
00684    }
00685    return 0;
00686 }

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

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

00623 {
00624    const char *family = astman_get_header(m, "Family");
00625    const char *key = astman_get_header(m, "Key");
00626    const char *val = astman_get_header(m, "Val");
00627    int res;
00628 
00629    if (ast_strlen_zero(family)) {
00630       astman_send_error(s, m, "No family specified");
00631       return 0;
00632    }
00633    if (ast_strlen_zero(key)) {
00634       astman_send_error(s, m, "No key specified");
00635       return 0;
00636    }
00637 
00638    res = ast_db_put(family, key, S_OR(val, ""));
00639    if (res) {
00640       astman_send_error(s, m, "Failed to update entry");
00641    } else {
00642       astman_send_ack(s, m, "Updated database successfully");
00643    }
00644    return 0;
00645 }

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

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

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

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

Definition at line 136 of file db.c.

Referenced by db_showkey_cb().

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


Variable Documentation

DB* astdb [static]

Definition at line 105 of file db.c.

struct ast_cli_entry cli_database[] [static]

Definition at line 613 of file db.c.

Referenced by astdb_init().

ast_cond_t dbcond [static]

Definition at line 107 of file db.c.

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

Definition at line 106 of file db.c.

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


Generated on Sat Mar 10 01:55:20 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7