Fri Jun 19 12:10:32 2009

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.

Functions

int ast_db_del (const char *family, const char *keys)
 Delete entry in astdb.
int ast_db_deltree (const char *family, const char *keytree)
 Delete a whole family (for some reason also called "tree".
void ast_db_freetree (struct ast_db_entry *dbe)
 Free in-memory data.
int ast_db_get (const char *family, const char *keys, char *value, int valuelen)
 Get key value specified by family/key.
ast_db_entryast_db_gettree (const char *family, const char *keytree)
 Get a whole family.
int ast_db_put (const char *family, const char *keys, const char *value)
 Store value addressed by family/key.
int astdb_init (void)
static int dbinit (void)
static char * handle_cli_database_del (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_deltree (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_get (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_put (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_database_showkey (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int keymatch (const char *key, const char *prefix)
static int manager_dbdel (struct mansession *s, const struct message *m)
static int manager_dbdeltree (struct mansession *s, const struct message *m)
static int manager_dbget (struct mansession *s, const struct message *m)
static int manager_dbput (struct mansession *s, const struct message *m)
static int subkeymatch (const char *key, const char *suffix)

Variables

static DB * astdb
ast_cli_entry cli_database []
static ast_mutex_t dblock = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP )


Detailed Description

ASTdb Management.

Author:
Mark Spencer <markster@digium.com>
Note:
DB3 is licensed under Sleepycat Public License and is thus incompatible with GPL. To avoid having to make another exception (and complicate licensing even further) we elect to use DB1 which is BSD licensed

Definition in file db.c.


Function Documentation

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

Delete entry in astdb.

Definition at line 209 of file db.c.

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

Referenced by __expire_registry(), ast_privacy_set(), auth_exec(), cache_lookup_internal(), del_exec(), destroy_association(), dialgroup_refreshdb(), dump_agents(), dump_queue_members(), function_db_delete(), handle_cli_database_del(), handle_dbdel(), manager_dbdel(), process_clearcache(), reload_agents(), reload_queue_members(), and update_registry().

00210 {
00211    char fullkey[256];
00212    DBT key;
00213    int res, fullkeylen;
00214 
00215    ast_mutex_lock(&dblock);
00216    if (dbinit()) {
00217       ast_mutex_unlock(&dblock);
00218       return -1;
00219    }
00220    
00221    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00222    memset(&key, 0, sizeof(key));
00223    key.data = fullkey;
00224    key.size = fullkeylen + 1;
00225    
00226    res = astdb->del(astdb, &key, 0);
00227    astdb->sync(astdb, 0);
00228    
00229    ast_mutex_unlock(&dblock);
00230 
00231    if (res) {
00232       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00233    }
00234    return res;
00235 }

int ast_db_deltree ( const char *  family,
const char *  keytree 
)

Delete a whole family (for some reason also called "tree".

Definition at line 91 of file db.c.

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

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

00092 {
00093    char prefix[256];
00094    DBT key, data;
00095    char *keys;
00096    int res;
00097    int pass;
00098    int counter = 0;
00099    
00100    if (family) {
00101       if (keytree) {
00102          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00103       } else {
00104          snprintf(prefix, sizeof(prefix), "/%s", family);
00105       }
00106    } else if (keytree) {
00107       return -1;
00108    } else {
00109       prefix[0] = '\0';
00110    }
00111    
00112    ast_mutex_lock(&dblock);
00113    if (dbinit()) {
00114       ast_mutex_unlock(&dblock);
00115       return -1;
00116    }
00117    
00118    memset(&key, 0, sizeof(key));
00119    memset(&data, 0, sizeof(data));
00120    pass = 0;
00121    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00122       if (key.size) {
00123          keys = key.data;
00124          keys[key.size - 1] = '\0';
00125       } else {
00126          keys = "<bad key>";
00127       }
00128       if (keymatch(keys, prefix)) {
00129          astdb->del(astdb, &key, 0);
00130          counter++;
00131       }
00132    }
00133    astdb->sync(astdb, 0);
00134    ast_mutex_unlock(&dblock);
00135    return counter;
00136 }

void ast_db_freetree ( struct ast_db_entry dbe  ) 

Free in-memory data.

Definition at line 535 of file db.c.

References ast_free, last, and ast_db_entry::next.

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

00536 {
00537    struct ast_db_entry *last;
00538    while (dbe) {
00539       last = dbe;
00540       dbe = dbe->next;
00541       ast_free(last);
00542    }
00543 }

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

Get key value specified by family/key.

Definition at line 165 of file db.c.

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

Referenced by ast_privacy_check(), auth_exec(), blacklist_read(), cache_lookup_internal(), check_access(), create_addr(), custom_devstate_callback(), database_increment(), 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(), reload_agents(), and reload_queue_members().

00166 {
00167    char fullkey[256] = "";
00168    DBT key, data;
00169    int res, fullkeylen;
00170 
00171    ast_mutex_lock(&dblock);
00172    if (dbinit()) {
00173       ast_mutex_unlock(&dblock);
00174       return -1;
00175    }
00176 
00177    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00178    memset(&key, 0, sizeof(key));
00179    memset(&data, 0, sizeof(data));
00180    memset(value, 0, valuelen);
00181    key.data = fullkey;
00182    key.size = fullkeylen + 1;
00183 
00184    res = astdb->get(astdb, &key, &data, 0);
00185 
00186    /* Be sure to NULL terminate our data either way */
00187    if (res) {
00188       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00189    } else {
00190 #if 0
00191       printf("Got value of size %d\n", data.size);
00192 #endif
00193       if (data.size) {
00194          ((char *)data.data)[data.size - 1] = '\0';
00195          /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
00196          ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
00197       } else {
00198          ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
00199       }
00200    }
00201 
00202    /* Data is not fully isolated for concurrency, so the lock must be extended
00203     * to after the copy to the output buffer. */
00204    ast_mutex_unlock(&dblock);
00205 
00206    return res;
00207 }

struct ast_db_entry* ast_db_gettree ( const char *  family,
const char *  keytree 
)

Get a whole family.

Definition at line 473 of file db.c.

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

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

00474 {
00475    char prefix[256];
00476    DBT key, data;
00477    char *keys, *values;
00478    int values_len;
00479    int res;
00480    int pass;
00481    struct ast_db_entry *last = NULL;
00482    struct ast_db_entry *cur, *ret=NULL;
00483 
00484    if (!ast_strlen_zero(family)) {
00485       if (!ast_strlen_zero(keytree)) {
00486          /* Family and key tree */
00487          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00488       } else {
00489          /* Family only */
00490          snprintf(prefix, sizeof(prefix), "/%s", family);
00491       }
00492    } else {
00493       prefix[0] = '\0';
00494    }
00495    ast_mutex_lock(&dblock);
00496    if (dbinit()) {
00497       ast_mutex_unlock(&dblock);
00498       ast_log(LOG_WARNING, "Database unavailable\n");
00499       return NULL;   
00500    }
00501    memset(&key, 0, sizeof(key));
00502    memset(&data, 0, sizeof(data));
00503    pass = 0;
00504    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00505       if (key.size) {
00506          keys = key.data;
00507          keys[key.size - 1] = '\0';
00508       } else {
00509          keys = "<bad key>";
00510       }
00511       if (data.size) {
00512          values = data.data;
00513          values[data.size - 1] = '\0';
00514       } else {
00515          values = "<bad value>";
00516       }
00517       values_len = strlen(values) + 1;
00518       if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
00519          cur->next = NULL;
00520          cur->key = cur->data + values_len;
00521          strcpy(cur->data, values);
00522          strcpy(cur->key, keys);
00523          if (last) {
00524             last->next = cur;
00525          } else {
00526             ret = cur;
00527          }
00528          last = cur;
00529       }
00530    }
00531    ast_mutex_unlock(&dblock);
00532    return ret; 
00533 }

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

Store value addressed by family/key.

Definition at line 138 of file db.c.

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

Referenced by ast_privacy_set(), cache_save(), cache_save_hint(), database_increment(), devstate_write(), dialgroup_refreshdb(), dump_agents(), 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(), ss_thread(), and update_registry().

00139 {
00140    char fullkey[256];
00141    DBT key, data;
00142    int res, fullkeylen;
00143 
00144    ast_mutex_lock(&dblock);
00145    if (dbinit()) {
00146       ast_mutex_unlock(&dblock);
00147       return -1;
00148    }
00149 
00150    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00151    memset(&key, 0, sizeof(key));
00152    memset(&data, 0, sizeof(data));
00153    key.data = fullkey;
00154    key.size = fullkeylen + 1;
00155    data.data = (char *) value;
00156    data.size = strlen(value) + 1;
00157    res = astdb->put(astdb, &key, &data, 0);
00158    astdb->sync(astdb, 0);
00159    ast_mutex_unlock(&dblock);
00160    if (res)
00161       ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
00162    return res;
00163 }

int astdb_init ( void   ) 

Provided by db.c

Definition at line 665 of file db.c.

References ast_cli_register_multiple(), ast_manager_register, cli_database, dbinit(), EVENT_FLAG_REPORTING, EVENT_FLAG_SYSTEM, manager_dbdel(), manager_dbdeltree(), manager_dbget(), and manager_dbput().

Referenced by main().

00666 {
00667    dbinit();
00668    ast_cli_register_multiple(cli_database, sizeof(cli_database) / sizeof(struct ast_cli_entry));
00669    ast_manager_register("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget, "Get DB Entry");
00670    ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry");
00671    ast_manager_register("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel, "Delete DB Entry");
00672    ast_manager_register("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree, "Delete DB Tree");
00673    return 0;
00674 }

static int dbinit ( void   )  [static]

Definition at line 54 of file db.c.

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

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

00055 {
00056    if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
00057       ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
00058       return -1;
00059    }
00060    return 0;
00061 }

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

Definition at line 292 of file db.c.

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

00293 {
00294    int res;
00295 
00296    switch (cmd) {
00297    case CLI_INIT:
00298       e->command = "database del";
00299       e->usage =
00300          "Usage: database del <family> <key>\n"
00301          "       Deletes an entry in the Asterisk database for a given\n"
00302          "       family and key.\n";
00303       return NULL;
00304    case CLI_GENERATE:
00305       return NULL;
00306    }
00307 
00308    if (a->argc != 4)
00309       return CLI_SHOWUSAGE;
00310    res = ast_db_del(a->argv[2], a->argv[3]);
00311    if (res) {
00312       ast_cli(a->fd, "Database entry does not exist.\n");
00313    } else {
00314       ast_cli(a->fd, "Database entry removed.\n");
00315    }
00316    return CLI_SUCCESS;
00317 }

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

Definition at line 319 of file db.c.

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

00320 {
00321    int res;
00322 
00323    switch (cmd) {
00324    case CLI_INIT:
00325       e->command = "database deltree";
00326       e->usage =
00327          "Usage: database deltree <family> [keytree]\n"
00328          "       Deletes a family or specific keytree within a family\n"
00329          "       in the Asterisk database.\n";
00330       return NULL;
00331    case CLI_GENERATE:
00332       return NULL;
00333    }
00334 
00335    if ((a->argc < 3) || (a->argc > 4))
00336       return CLI_SHOWUSAGE;
00337    if (a->argc == 4) {
00338       res = ast_db_deltree(a->argv[2], a->argv[3]);
00339    } else {
00340       res = ast_db_deltree(a->argv[2], NULL);
00341    }
00342    if (res < 0) {
00343       ast_cli(a->fd, "Database entries do not exist.\n");
00344    } else {
00345       ast_cli(a->fd, "%d database entries removed.\n",res);
00346    }
00347    return CLI_SUCCESS;
00348 }

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

Definition at line 264 of file db.c.

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

00265 {
00266    int res;
00267    char tmp[256];
00268 
00269    switch (cmd) {
00270    case CLI_INIT:
00271       e->command = "database get";
00272       e->usage =
00273          "Usage: database get <family> <key>\n"
00274          "       Retrieves an entry in the Asterisk database for a given\n"
00275          "       family and key.\n";
00276       return NULL;
00277    case CLI_GENERATE:
00278       return NULL;
00279    }
00280 
00281    if (a->argc != 4)
00282       return CLI_SHOWUSAGE;
00283    res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
00284    if (res) {
00285       ast_cli(a->fd, "Database entry not found.\n");
00286    } else {
00287       ast_cli(a->fd, "Value: %s\n", tmp);
00288    }
00289    return CLI_SUCCESS;
00290 }

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

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

00238 {
00239    int res;
00240 
00241    switch (cmd) {
00242    case CLI_INIT:
00243       e->command = "database put";
00244       e->usage =
00245          "Usage: database put <family> <key> <value>\n"
00246          "       Adds or updates an entry in the Asterisk database for\n"
00247          "       a given family, key, and value.\n";
00248       return NULL;
00249    case CLI_GENERATE:
00250       return NULL;
00251    }
00252 
00253    if (a->argc != 5)
00254       return CLI_SHOWUSAGE;
00255    res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
00256    if (res)  {
00257       ast_cli(a->fd, "Failed to update entry\n");
00258    } else {
00259       ast_cli(a->fd, "Updated database successfully\n");
00260    }
00261    return CLI_SUCCESS;
00262 }

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

Definition at line 350 of file db.c.

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

00351 {
00352    char prefix[256];
00353    DBT key, data;
00354    char *keys, *values;
00355    int res;
00356    int pass;
00357    int counter = 0;
00358 
00359    switch (cmd) {
00360    case CLI_INIT:
00361       e->command = "database show";
00362       e->usage =
00363          "Usage: database show [family [keytree]]\n"
00364          "       Shows Asterisk database contents, optionally restricted\n"
00365          "       to a given family, or family and keytree.\n";
00366       return NULL;
00367    case CLI_GENERATE:
00368       return NULL;
00369    }
00370 
00371    if (a->argc == 4) {
00372       /* Family and key tree */
00373       snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
00374    } else if (a->argc == 3) {
00375       /* Family only */
00376       snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
00377    } else if (a->argc == 2) {
00378       /* Neither */
00379       prefix[0] = '\0';
00380    } else {
00381       return CLI_SHOWUSAGE;
00382    }
00383    ast_mutex_lock(&dblock);
00384    if (dbinit()) {
00385       ast_mutex_unlock(&dblock);
00386       ast_cli(a->fd, "Database unavailable\n");
00387       return CLI_SUCCESS;  
00388    }
00389    memset(&key, 0, sizeof(key));
00390    memset(&data, 0, sizeof(data));
00391    pass = 0;
00392    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00393       if (key.size) {
00394          keys = key.data;
00395          keys[key.size - 1] = '\0';
00396       } else {
00397          keys = "<bad key>";
00398       }
00399       if (data.size) {
00400          values = data.data;
00401          values[data.size - 1]='\0';
00402       } else {
00403          values = "<bad value>";
00404       }
00405       if (keymatch(keys, prefix)) {
00406          ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
00407          counter++;
00408       }
00409    }
00410    ast_mutex_unlock(&dblock);
00411    ast_cli(a->fd, "%d results found.\n", counter);
00412    return CLI_SUCCESS;  
00413 }

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

Definition at line 415 of file db.c.

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

00416 {
00417    char suffix[256];
00418    DBT key, data;
00419    char *keys, *values;
00420    int res;
00421    int pass;
00422    int counter = 0;
00423 
00424    switch (cmd) {
00425    case CLI_INIT:
00426       e->command = "database showkey";
00427       e->usage =
00428          "Usage: database showkey <keytree>\n"
00429          "       Shows Asterisk database contents, restricted to a given key.\n";
00430       return NULL;
00431    case CLI_GENERATE:
00432       return NULL;
00433    }
00434 
00435    if (a->argc == 3) {
00436       /* Key only */
00437       snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
00438    } else {
00439       return CLI_SHOWUSAGE;
00440    }
00441    ast_mutex_lock(&dblock);
00442    if (dbinit()) {
00443       ast_mutex_unlock(&dblock);
00444       ast_cli(a->fd, "Database unavailable\n");
00445       return CLI_SUCCESS;  
00446    }
00447    memset(&key, 0, sizeof(key));
00448    memset(&data, 0, sizeof(data));
00449    pass = 0;
00450    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00451       if (key.size) {
00452          keys = key.data;
00453          keys[key.size - 1] = '\0';
00454       } else {
00455          keys = "<bad key>";
00456       }
00457       if (data.size) {
00458          values = data.data;
00459          values[data.size - 1]='\0';
00460       } else {
00461          values = "<bad value>";
00462       }
00463       if (subkeymatch(keys, suffix)) {
00464          ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
00465          counter++;
00466       }
00467    }
00468    ast_mutex_unlock(&dblock);
00469    ast_cli(a->fd, "%d results found.\n", counter);
00470    return CLI_SUCCESS;  
00471 }

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

Definition at line 64 of file db.c.

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

00065 {
00066    int preflen = strlen(prefix);
00067    if (!preflen)
00068       return 1;
00069    if (!strcasecmp(key, prefix))
00070       return 1;
00071    if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
00072       if (key[preflen] == '/')
00073          return 1;
00074    }
00075    return 0;
00076 }

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

Definition at line 616 of file db.c.

References ast_db_del(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), and s.

Referenced by astdb_init().

00617 {
00618    const char *family = astman_get_header(m, "Family");
00619    const char *key = astman_get_header(m, "Key");
00620    int res;
00621 
00622    if (ast_strlen_zero(family)) {
00623       astman_send_error(s, m, "No family specified.");
00624       return 0;
00625    }
00626 
00627    if (ast_strlen_zero(key)) {
00628       astman_send_error(s, m, "No key specified.");
00629       return 0;
00630    }
00631 
00632    res = ast_db_del(family, key);
00633    if (res)
00634       astman_send_error(s, m, "Database entry not found");
00635    else
00636       astman_send_ack(s, m, "Key deleted successfully");
00637 
00638    return 0;
00639 }

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

Definition at line 641 of file db.c.

References ast_db_deltree(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), and s.

Referenced by astdb_init().

00642 {
00643    const char *family = astman_get_header(m, "Family");
00644    const char *key = astman_get_header(m, "Key");
00645    int res;
00646 
00647    if (ast_strlen_zero(family)) {
00648       astman_send_error(s, m, "No family specified.");
00649       return 0;
00650    }
00651 
00652    if (!ast_strlen_zero(key))
00653       res = ast_db_deltree(family, key);
00654    else
00655       res = ast_db_deltree(family, NULL);
00656 
00657    if (res < 0)
00658       astman_send_error(s, m, "Database entry not found");
00659    else
00660       astman_send_ack(s, m, "Key tree deleted successfully");
00661    
00662    return 0;
00663 }

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

Definition at line 579 of file db.c.

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

Referenced by astdb_init().

00580 {
00581    const char *id = astman_get_header(m,"ActionID");
00582    char idText[256] = "";
00583    const char *family = astman_get_header(m, "Family");
00584    const char *key = astman_get_header(m, "Key");
00585    char tmp[256];
00586    int res;
00587 
00588    if (ast_strlen_zero(family)) {
00589       astman_send_error(s, m, "No family specified.");
00590       return 0;
00591    }
00592    if (ast_strlen_zero(key)) {
00593       astman_send_error(s, m, "No key specified.");
00594       return 0;
00595    }
00596 
00597    if (!ast_strlen_zero(id))
00598       snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
00599 
00600    res = ast_db_get(family, key, tmp, sizeof(tmp));
00601    if (res) {
00602       astman_send_error(s, m, "Database entry not found");
00603    } else {
00604       astman_send_ack(s, m, "Result will follow");
00605       astman_append(s, "Event: DBGetResponse\r\n"
00606             "Family: %s\r\n"
00607             "Key: %s\r\n"
00608             "Val: %s\r\n"
00609             "%s"
00610             "\r\n",
00611             family, key, tmp, idText);
00612    }
00613    return 0;
00614 }

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

Definition at line 554 of file db.c.

References ast_db_put(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), s, and S_OR.

Referenced by astdb_init().

00555 {
00556    const char *family = astman_get_header(m, "Family");
00557    const char *key = astman_get_header(m, "Key");
00558    const char *val = astman_get_header(m, "Val");
00559    int res;
00560 
00561    if (ast_strlen_zero(family)) {
00562       astman_send_error(s, m, "No family specified");
00563       return 0;
00564    }
00565    if (ast_strlen_zero(key)) {
00566       astman_send_error(s, m, "No key specified");
00567       return 0;
00568    }
00569 
00570    res = ast_db_put(family, key, S_OR(val, ""));
00571    if (res) {
00572       astman_send_error(s, m, "Failed to update entry");
00573    } else {
00574       astman_send_ack(s, m, "Updated database successfully");
00575    }
00576    return 0;
00577 }

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

Definition at line 78 of file db.c.

Referenced by handle_cli_database_showkey().

00079 {
00080    int suffixlen = strlen(suffix);
00081    if (suffixlen) {
00082       const char *subkey = key + strlen(key) - suffixlen;
00083       if (subkey < key)
00084          return 0;
00085       if (!strcasecmp(subkey, suffix))
00086          return 1;
00087    }
00088    return 0;
00089 }


Variable Documentation

DB* astdb [static]

Definition at line 51 of file db.c.

struct ast_cli_entry cli_database[]

Definition at line 545 of file db.c.

Referenced by astdb_init().

ast_mutex_t dblock = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP ) [static]

Definition at line 52 of file db.c.

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


Generated on Fri Jun 19 12:10:32 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7