Wed Jan 8 2020 09:50:11

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.

Macros

#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. More...
 
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. More...
 
void ast_db_freetree (struct ast_db_entry *dbe)
 Free structure created by ast_db_gettree() More...
 
int ast_db_get (const char *family, const char *keys, char *value, int valuelen)
 Get key value specified by family/key. More...
 
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. More...
 
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. More...
 
int ast_db_put (const char *family, const char *keys, const char *value)
 Store value addressed by family/key. More...
 
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 marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
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.

Macro Definition Documentation

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

366 {
367  char fullkey[MAX_DB_FIELD];
368  DBT key;
369  int res, fullkeylen;
370 
372  if (dbinit()) {
374  return -1;
375  }
376 
377  fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
378  memset(&key, 0, sizeof(key));
379  key.data = fullkey;
380  key.size = fullkeylen + 1;
381 
382  res = astdb->del(astdb, &key, 0);
383  db_sync();
384 
386 
387  if (res) {
388  ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
389  }
390  return res;
391 }
#define ast_mutex_lock(a)
Definition: lock.h:155
static ast_mutex_t dblock
Definition: db.c:110
static int dbinit(void)
Definition: db.c:118
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static DB * astdb
Definition: db.c:109
static void db_sync(void)
Definition: db.c:800
#define MAX_DB_FIELD
Definition: db.c:107
#define ast_mutex_unlock(a)
Definition: lock.h:156
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
-1An 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().

242 {
243  char prefix[MAX_DB_FIELD];
244 
245  if (family) {
246  if (keytree) {
247  snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
248  } else {
249  snprintf(prefix, sizeof(prefix), "/%s", family);
250  }
251  } else if (keytree) {
252  return -1;
253  } else {
254  prefix[0] = '\0';
255  }
256 
257  return process_db_keys(db_deltree_cb, NULL, prefix, 1);
258 }
static int process_db_keys(process_keys_cb cb, void *data, const char *filter, int sync)
Definition: db.c:184
static int db_deltree_cb(DBT *key, DBT *value, const char *filter, void *data)
Definition: db.c:230
#define MAX_DB_FIELD
Definition: db.c:107
static char prefix[MAX_PREFIX]
Definition: http.c:107
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().

657 {
658  struct ast_db_entry *last;
659  while (dbe) {
660  last = dbe;
661  dbe = dbe->next;
662  ast_free(last);
663  }
664 }
struct ast_db_entry * next
Definition: astdb.h:31
struct sla_ringing_trunk * last
Definition: app_meetme.c:965
Definition: astdb.h:30
#define ast_free(a)
Definition: astmm.h:97
int ast_db_get ( const char *  family,
const char *  keys,
char *  value,
int  valuelen 
)

Get key value specified by family/key.

Definition at line 348 of file db.c.

References ast_assert, and db_get_common().

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

349 {
350  ast_assert(value != NULL);
351 
352  /* Make sure we initialize */
353  value[0] = 0;
354 
355  return db_get_common(family, keys, &value, valuelen);
356 }
#define ast_assert(a)
Definition: utils.h:738
int value
Definition: syslog.c:39
static int db_get_common(const char *family, const char *keys, char **buffer, int bufferlen)
Definition: db.c:302
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
-1An error occurred
0Success

Definition at line 358 of file db.c.

References db_get_common().

Referenced by reload_queue_members().

359 {
360  *out = NULL;
361 
362  return db_get_common(family, keys, out, -1);
363 }
static int db_get_common(const char *family, const char *keys, char **buffer, int bufferlen)
Definition: db.c:302
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 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().

632 {
633  char prefix[MAX_DB_FIELD];
634  struct ast_db_entry *ret = NULL;
635 
636  if (!ast_strlen_zero(family)) {
637  if (!ast_strlen_zero(keytree)) {
638  /* Family and key tree */
639  snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
640  } else {
641  /* Family only */
642  snprintf(prefix, sizeof(prefix), "/%s", family);
643  }
644  } else {
645  prefix[0] = '\0';
646  }
647 
648  if (process_db_keys(db_gettree_cb, &ret, prefix, 0) < 0) {
649  ast_log(LOG_WARNING, "Database unavailable\n");
650  return NULL;
651  }
652 
653  return ret;
654 }
#define LOG_WARNING
Definition: logger.h:144
static int db_gettree_cb(DBT *key, DBT *value, const char *filter, void *data)
Definition: db.c:611
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static int process_db_keys(process_keys_cb cb, void *data, const char *filter, int sync)
Definition: db.c:184
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
Definition: astdb.h:30
#define MAX_DB_FIELD
Definition: db.c:107
static char prefix[MAX_PREFIX]
Definition: http.c:107
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(), handle_pri_service_generic(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), pri_dchannel(), save_secret(), and update_registry().

261 {
262  char fullkey[MAX_DB_FIELD];
263  DBT key, data;
264  int res, fullkeylen;
265 
267  if (dbinit()) {
269  return -1;
270  }
271 
272  fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
273  memset(&key, 0, sizeof(key));
274  memset(&data, 0, sizeof(data));
275  key.data = fullkey;
276  key.size = fullkeylen + 1;
277  data.data = (char *) value;
278  data.size = strlen(value) + 1;
279  res = astdb->put(astdb, &key, &data, 0);
280  db_sync();
282  if (res)
283  ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
284 
285  return res;
286 }
#define LOG_WARNING
Definition: logger.h:144
#define ast_mutex_lock(a)
Definition: lock.h:155
static ast_mutex_t dblock
Definition: db.c:110
int value
Definition: syslog.c:39
static int dbinit(void)
Definition: db.c:118
static DB * astdb
Definition: db.c:109
static void db_sync(void)
Definition: db.c:800
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
char data[0]
Definition: astdb.h:33
#define MAX_DB_FIELD
Definition: db.c:107
#define ast_mutex_unlock(a)
Definition: lock.h:156
char * key
Definition: astdb.h:32
int astdb_init ( void  )

Provided by db.c

Definition at line 880 of file db.c.

References ARRAY_LEN, ast_cli_register_multiple(), ast_cond_init, ast_manager_register_xml, ast_mutex_lock, ast_mutex_unlock, ast_pthread_create_background, ast_register_atexit(), astdb_shutdown(), db_sync_thread(), dbinit(), dblock, EVENT_FLAG_REPORTING, EVENT_FLAG_SYSTEM, manager_dbdel(), manager_dbdeltree(), manager_dbget(), and manager_dbput().

Referenced by main().

881 {
882  ast_cond_init(&dbcond, NULL);
884  return -1;
885  }
886 
888  /* Ignore check_return warning from Coverity for dbinit below */
889  dbinit();
891 
897 
899  return 0;
900 }
static struct ast_cli_entry cli_database[]
Definition: db.c:666
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_cond_init(cond, attr)
Definition: lock.h:167
#define ast_mutex_lock(a)
Definition: lock.h:155
static ast_mutex_t dblock
Definition: db.c:110
static int dbinit(void)
Definition: db.c:118
static ast_cond_t dbcond
Definition: db.c:111
static int manager_dbget(struct mansession *s, const struct message *m)
Definition: db.c:700
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:426
static pthread_t syncthread
Definition: db.c:112
#define EVENT_FLAG_SYSTEM
Definition: manager.h:71
static int manager_dbdel(struct mansession *s, const struct message *m)
Definition: db.c:741
#define ast_manager_register_xml(a, b, c)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:172
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: asterisk.c:998
static void * db_sync_thread(void *data)
Definition: db.c:815
#define EVENT_FLAG_REPORTING
Definition: manager.h:80
static int manager_dbput(struct mansession *s, const struct message *m)
Definition: db.c:675
static int manager_dbdeltree(struct mansession *s, const struct message *m)
Definition: db.c:766
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
#define ast_mutex_unlock(a)
Definition: lock.h:156
static void astdb_shutdown(void)
Definition: db.c:854
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().

855 {
857  ast_manager_unregister("DBGet");
858  ast_manager_unregister("DBPut");
859  ast_manager_unregister("DBDel");
860  ast_manager_unregister("DBDelTree");
861 
863  doexit = 1;
864  db_sync();
866 
867  pthread_join(syncthread, NULL);
868 
869 #if defined(DEBUG_FD_LEAKS) && defined(close)
870 /* DEBUG_FD_LEAKS causes conflicting define of close() in asterisk.h */
871 #undef close
872 #endif
873 
874  if (astdb) {
875  astdb->close(astdb);
876  astdb = NULL;
877  }
878 }
static struct ast_cli_entry cli_database[]
Definition: db.c:666
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
#define ast_mutex_lock(a)
Definition: lock.h:155
static ast_mutex_t dblock
Definition: db.c:110
static pthread_t syncthread
Definition: db.c:112
static DB * astdb
Definition: db.c:109
static int doexit
Definition: db.c:113
static void db_sync(void)
Definition: db.c:800
int ast_manager_unregister(char *action)
Unregister a registered manager command.
Definition: manager.c:5355
#define ast_mutex_unlock(a)
Definition: lock.h:156
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().

231 {
232  int res = 0;
233 
234  if (keymatch(dbt_data2str_full(key, "<bad key>"), filter)) {
235  astdb->del(astdb, key, 0);
236  res = 1;
237  }
238  return res;
239 }
static const char * dbt_data2str_full(DBT *dbt, const char *def)
Definition: db.c:169
static DB * astdb
Definition: db.c:109
static int keymatch(const char *key, const char *prefix)
Definition: db.c:130
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:694
char * key
Definition: astdb.h:32
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().

303 {
304  char fullkey[MAX_DB_FIELD] = "";
305  DBT key, data;
306  int res, fullkeylen;
307 
309  if (dbinit()) {
311  return -1;
312  }
313 
314  fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
315  memset(&key, 0, sizeof(key));
316  memset(&data, 0, sizeof(data));
317  key.data = fullkey;
318  key.size = fullkeylen + 1;
319 
320  res = astdb->get(astdb, &key, &data, 0);
321 
322  /* Be sure to NULL terminate our data either way */
323  if (res) {
324  ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
325  } else {
326  if (data.size) {
327  ((char *)data.data)[data.size - 1] = '\0';
328 
329  if (bufferlen == -1) {
330  *buffer = ast_strdup(data.data);
331  } else {
332  /* Make sure that we don't write too much to the dst pointer or we don't
333  * read too much from the source pointer */
334  ast_copy_string(*buffer, data.data, bufferlen > data.size ? data.size : bufferlen);
335  }
336  } else {
337  ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
338  }
339  }
340 
341  /* Data is not fully isolated for concurrency, so the lock must be extended
342  * to after the copy to the output buffer. */
344 
345  return res;
346 }
#define ast_strdup(a)
Definition: astmm.h:109
#define ast_mutex_lock(a)
Definition: lock.h:155
static ast_mutex_t dblock
Definition: db.c:110
static int dbinit(void)
Definition: db.c:118
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static DB * astdb
Definition: db.c:109
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
char data[0]
Definition: astdb.h:33
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define MAX_DB_FIELD
Definition: db.c:107
#define ast_mutex_unlock(a)
Definition: lock.h:156
char * key
Definition: astdb.h:32
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().

612 {
613  struct ast_db_entry **ret = data;
614  struct ast_db_entry *cur;
615  const char *key_s = dbt_data2str_full(key, "<bad key>");
616  const char *value_s = dbt_data2str_full(value, "<bad value>");
617  size_t key_slen = strlen(key_s) + 1, value_slen = strlen(value_s) + 1;
618 
619  if (keymatch(key_s, filter) && (cur = ast_malloc(sizeof(*cur) + key_slen + value_slen))) {
620  cur->next = *ret;
621  cur->key = cur->data + value_slen;
622  strcpy(cur->data, value_s);
623  strcpy(cur->key, key_s);
624  *ret = cur;
625  return 1;
626  }
627 
628  return 0;
629 }
static const char * dbt_data2str_full(DBT *dbt, const char *def)
Definition: db.c:169
int value
Definition: syslog.c:39
struct ast_db_entry * next
Definition: astdb.h:31
Definition: astdb.h:30
char data[0]
Definition: astdb.h:33
static int keymatch(const char *key, const char *prefix)
Definition: db.c:130
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:694
#define ast_malloc(a)
Definition: astmm.h:91
char * key
Definition: astdb.h:32
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().

511 {
512  struct ast_cli_args *a = data;
513  const char *key_s = dbt_data2str_full(key, "<bad key>");
514  const char *value_s = dbt_data2str_full(value, "<bad value>");
515 
516  if (keymatch(key_s, filter)) {
517  ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
518  return 1;
519  }
520 
521  return 0;
522 }
static const char * dbt_data2str_full(DBT *dbt, const char *def)
Definition: db.c:169
int value
Definition: syslog.c:39
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
const int fd
Definition: cli.h:153
static int keymatch(const char *key, const char *prefix)
Definition: db.c:130
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:694
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().

566 {
567  struct ast_cli_args *a = data;
568  const char *key_s = dbt_data2str_full(key, "<bad key>");
569  const char *value_s = dbt_data2str_full(value, "<bad value>");
570 
571  if (subkeymatch(key_s, filter)) {
572  ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
573  return 1;
574  }
575 
576  return 0;
577 }
static const char * dbt_data2str_full(DBT *dbt, const char *def)
Definition: db.c:169
static int subkeymatch(const char *key, const char *suffix)
Definition: db.c:144
int value
Definition: syslog.c:39
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
const int fd
Definition: cli.h:153
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:694
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().

801 {
803 }
static ast_cond_t dbcond
Definition: db.c:111
#define ast_cond_signal(cond)
Definition: lock.h:169
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().

816 {
818  for (;;) {
820  if (doexit) {
821  /*
822  * We were likely awakened just to exit. Sync anyway just in
823  * case.
824  */
825  if (astdb) {
826  astdb->sync(astdb, 0);
827  }
829  break;
830  }
831 
833  /*
834  * Sleep so if we have a bunch of db puts in a row, they won't
835  * get written one at a time to the db but in a batch.
836  */
837  sleep(1);
839 
840  /* The db should be successfully opened to get here. */
841  ast_assert(astdb != NULL);
842  astdb->sync(astdb, 0);
843 
844  if (doexit) {
845  /* We were asked to exit while sleeping. */
847  break;
848  }
849  }
850 
851  return NULL;
852 }
#define ast_cond_wait(cond, mutex)
Definition: lock.h:171
#define ast_assert(a)
Definition: utils.h:738
#define ast_mutex_lock(a)
Definition: lock.h:155
static ast_mutex_t dblock
Definition: db.c:110
static ast_cond_t dbcond
Definition: db.c:111
static DB * astdb
Definition: db.c:109
static int doexit
Definition: db.c:113
#define ast_mutex_unlock(a)
Definition: lock.h:156
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().

119 {
120  if (doexit) {
121  return -1;
122  }
123  if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
124  ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
125  return -1;
126  }
127  return 0;
128 }
const char * ast_config_AST_DB
Definition: asterisk.c:268
#define LOG_WARNING
Definition: logger.h:144
#define AST_FILE_MODE
Definition: asterisk.h:36
static DB * astdb
Definition: db.c:109
static int doexit
Definition: db.c:113
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
static const char* dbt_data2str ( DBT *  dbt)
static

Definition at line 157 of file db.c.

Referenced by dbt_data2str_full().

158 {
159  char *data = "";
160 
161  if (dbt->size) {
162  data = dbt->data;
163  data[dbt->size - 1] = '\0';
164  }
165 
166  return data;
167 }
static const char* dbt_data2str_full ( DBT *  dbt,
const char *  def 
)
inlinestatic

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

170 {
171  return S_OR(dbt_data2str(dbt), def);
172 }
static const char * dbt_data2str(DBT *dbt)
Definition: db.c:157
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
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.

449 {
450  int res;
451 
452  switch (cmd) {
453  case CLI_INIT:
454  e->command = "database del";
455  e->usage =
456  "Usage: database del <family> <key>\n"
457  " Deletes an entry in the Asterisk database for a given\n"
458  " family and key.\n";
459  return NULL;
460  case CLI_GENERATE:
461  return NULL;
462  }
463 
464  if (a->argc != 4)
465  return CLI_SHOWUSAGE;
466  res = ast_db_del(a->argv[2], a->argv[3]);
467  if (res) {
468  ast_cli(a->fd, "Database entry does not exist.\n");
469  } else {
470  ast_cli(a->fd, "Database entry removed.\n");
471  }
472  return CLI_SUCCESS;
473 }
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
const int fd
Definition: cli.h:153
const char *const * argv
Definition: cli.h:155
#define CLI_SHOWUSAGE
Definition: cli.h:44
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: db.c:365
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.

476 {
477  int num_deleted;
478 
479  switch (cmd) {
480  case CLI_INIT:
481  e->command = "database deltree";
482  e->usage =
483  "Usage: database deltree <family> [keytree]\n"
484  " OR: database deltree <family>[/keytree]\n"
485  " Deletes a family or specific keytree within a family\n"
486  " in the Asterisk database. The two arguments may be\n"
487  " separated by either a space or a slash.\n";
488  return NULL;
489  case CLI_GENERATE:
490  return NULL;
491  }
492 
493  if ((a->argc < 3) || (a->argc > 4))
494  return CLI_SHOWUSAGE;
495  if (a->argc == 4) {
496  num_deleted = ast_db_deltree(a->argv[2], a->argv[3]);
497  } else {
498  num_deleted = ast_db_deltree(a->argv[2], NULL);
499  }
500  if (num_deleted < 0) {
501  ast_cli(a->fd, "Database unavailable.\n");
502  } else if (num_deleted == 0) {
503  ast_cli(a->fd, "Database entries do not exist.\n");
504  } else {
505  ast_cli(a->fd, "%d database entries removed.\n",num_deleted);
506  }
507  return CLI_SUCCESS;
508 }
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
const int fd
Definition: cli.h:153
const char *const * argv
Definition: cli.h:155
#define CLI_SHOWUSAGE
Definition: cli.h:44
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
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...
Definition: db.c:241
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.

421 {
422  int res;
423  char tmp[MAX_DB_FIELD];
424 
425  switch (cmd) {
426  case CLI_INIT:
427  e->command = "database get";
428  e->usage =
429  "Usage: database get <family> <key>\n"
430  " Retrieves an entry in the Asterisk database for a given\n"
431  " family and key.\n";
432  return NULL;
433  case CLI_GENERATE:
434  return NULL;
435  }
436 
437  if (a->argc != 4)
438  return CLI_SHOWUSAGE;
439  res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
440  if (res) {
441  ast_cli(a->fd, "Database entry not found.\n");
442  } else {
443  ast_cli(a->fd, "Value: %s\n", tmp);
444  }
445  return CLI_SUCCESS;
446 }
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
Definition: db.c:348
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
const int fd
Definition: cli.h:153
const char *const * argv
Definition: cli.h:155
#define CLI_SHOWUSAGE
Definition: cli.h:44
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
#define MAX_DB_FIELD
Definition: db.c:107
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.

394 {
395  int res;
396 
397  switch (cmd) {
398  case CLI_INIT:
399  e->command = "database put";
400  e->usage =
401  "Usage: database put <family> <key> <value>\n"
402  " Adds or updates an entry in the Asterisk database for\n"
403  " a given family, key, and value.\n";
404  return NULL;
405  case CLI_GENERATE:
406  return NULL;
407  }
408 
409  if (a->argc != 5)
410  return CLI_SHOWUSAGE;
411  res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
412  if (res) {
413  ast_cli(a->fd, "Failed to update entry\n");
414  } else {
415  ast_cli(a->fd, "Updated database successfully\n");
416  }
417  return CLI_SUCCESS;
418 }
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
const int fd
Definition: cli.h:153
const char *const * argv
Definition: cli.h:155
#define CLI_SHOWUSAGE
Definition: cli.h:44
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: db.c:260
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.

525 {
526  char prefix[MAX_DB_FIELD];
527  int counter = 0;
528 
529  switch (cmd) {
530  case CLI_INIT:
531  e->command = "database show";
532  e->usage =
533  "Usage: database show [family [keytree]]\n"
534  " OR: database show [family[/keytree]]\n"
535  " Shows Asterisk database contents, optionally restricted\n"
536  " to a given family, or family and keytree. The two arguments\n"
537  " may be separated either by a space or by a slash.\n";
538  return NULL;
539  case CLI_GENERATE:
540  return NULL;
541  }
542 
543  if (a->argc == 4) {
544  /* Family and key tree */
545  snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
546  } else if (a->argc == 3) {
547  /* Family only */
548  snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
549  } else if (a->argc == 2) {
550  /* Neither */
551  prefix[0] = '\0';
552  } else {
553  return CLI_SHOWUSAGE;
554  }
555 
556  if((counter = process_db_keys(db_show_cb, a, prefix, 0)) < 0) {
557  ast_cli(a->fd, "Database unavailable\n");
558  return CLI_SUCCESS;
559  }
560 
561  ast_cli(a->fd, "%d results found.\n", counter);
562  return CLI_SUCCESS;
563 }
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
static int db_show_cb(DBT *key, DBT *value, const char *filter, void *data)
Definition: db.c:510
const int fd
Definition: cli.h:153
static int process_db_keys(process_keys_cb cb, void *data, const char *filter, int sync)
Definition: db.c:184
const char *const * argv
Definition: cli.h:155
#define CLI_SHOWUSAGE
Definition: cli.h:44
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
#define MAX_DB_FIELD
Definition: db.c:107
static char prefix[MAX_PREFIX]
Definition: http.c:107
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.

580 {
581  char suffix[MAX_DB_FIELD];
582  int counter = 0;
583 
584  switch (cmd) {
585  case CLI_INIT:
586  e->command = "database showkey";
587  e->usage =
588  "Usage: database showkey <keytree>\n"
589  " Shows Asterisk database contents, restricted to a given key.\n";
590  return NULL;
591  case CLI_GENERATE:
592  return NULL;
593  }
594 
595  if (a->argc == 3) {
596  /* Key only */
597  snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
598  } else {
599  return CLI_SHOWUSAGE;
600  }
601 
602  if ((counter = process_db_keys(db_showkey_cb, a, suffix, 0)) < 0) {
603  ast_cli(a->fd, "Database unavailable\n");
604  return CLI_SUCCESS;
605  }
606 
607  ast_cli(a->fd, "%d results found.\n", counter);
608  return CLI_SUCCESS;
609 }
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
const int fd
Definition: cli.h:153
static int process_db_keys(process_keys_cb cb, void *data, const char *filter, int sync)
Definition: db.c:184
const char *const * argv
Definition: cli.h:155
#define CLI_SHOWUSAGE
Definition: cli.h:44
static int db_showkey_cb(DBT *key, DBT *value, const char *filter, void *data)
Definition: db.c:565
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
#define MAX_DB_FIELD
Definition: db.c:107
static int keymatch ( const char *  key,
const char *  prefix 
)
inlinestatic

Definition at line 130 of file db.c.

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

131 {
132  int preflen = strlen(prefix);
133  if (!preflen)
134  return 1;
135  if (!strcasecmp(key, prefix))
136  return 1;
137  if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
138  if (key[preflen] == '/')
139  return 1;
140  }
141  return 0;
142 }
static char prefix[MAX_PREFIX]
Definition: http.c:107
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().

742 {
743  const char *family = astman_get_header(m, "Family");
744  const char *key = astman_get_header(m, "Key");
745  int res;
746 
747  if (ast_strlen_zero(family)) {
748  astman_send_error(s, m, "No family specified.");
749  return 0;
750  }
751 
752  if (ast_strlen_zero(key)) {
753  astman_send_error(s, m, "No key specified.");
754  return 0;
755  }
756 
757  res = ast_db_del(family, key);
758  if (res)
759  astman_send_error(s, m, "Database entry not found");
760  else
761  astman_send_ack(s, m, "Key deleted successfully");
762 
763  return 0;
764 }
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:2135
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:1860
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: db.c:365
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:2130
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().

767 {
768  const char *family = astman_get_header(m, "Family");
769  const char *key = astman_get_header(m, "Key");
770  int num_deleted;
771 
772  if (ast_strlen_zero(family)) {
773  astman_send_error(s, m, "No family specified.");
774  return 0;
775  }
776 
777  if (!ast_strlen_zero(key)) {
778  num_deleted = ast_db_deltree(family, key);
779  } else {
780  num_deleted = ast_db_deltree(family, NULL);
781  }
782 
783  if (num_deleted < 0) {
784  astman_send_error(s, m, "Database unavailable");
785  } else if (num_deleted == 0) {
786  astman_send_error(s, m, "Database entry not found");
787  } else {
788  astman_send_ack(s, m, "Key tree deleted successfully");
789  }
790 
791  return 0;
792 }
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:2135
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:1860
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:2130
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...
Definition: db.c:241
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().

701 {
702  const char *id = astman_get_header(m,"ActionID");
703  char idText[256] = "";
704  const char *family = astman_get_header(m, "Family");
705  const char *key = astman_get_header(m, "Key");
706  char tmp[MAX_DB_FIELD];
707  int res;
708 
709  if (ast_strlen_zero(family)) {
710  astman_send_error(s, m, "No family specified.");
711  return 0;
712  }
713  if (ast_strlen_zero(key)) {
714  astman_send_error(s, m, "No key specified.");
715  return 0;
716  }
717 
718  if (!ast_strlen_zero(id))
719  snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
720 
721  res = ast_db_get(family, key, tmp, sizeof(tmp));
722  if (res) {
723  astman_send_error(s, m, "Database entry not found");
724  } else {
725  astman_send_ack(s, m, "Result will follow");
726  astman_append(s, "Event: DBGetResponse\r\n"
727  "Family: %s\r\n"
728  "Key: %s\r\n"
729  "Val: %s\r\n"
730  "%s"
731  "\r\n",
732  family, key, tmp, idText);
733  astman_append(s, "Event: DBGetComplete\r\n"
734  "%s"
735  "\r\n",
736  idText);
737  }
738  return 0;
739 }
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:2068
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
Definition: db.c:348
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:2135
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:1860
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define MAX_DB_FIELD
Definition: db.c:107
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:2130
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().

676 {
677  const char *family = astman_get_header(m, "Family");
678  const char *key = astman_get_header(m, "Key");
679  const char *val = astman_get_header(m, "Val");
680  int res;
681 
682  if (ast_strlen_zero(family)) {
683  astman_send_error(s, m, "No family specified");
684  return 0;
685  }
686  if (ast_strlen_zero(key)) {
687  astman_send_error(s, m, "No key specified");
688  return 0;
689  }
690 
691  res = ast_db_put(family, key, S_OR(val, ""));
692  if (res) {
693  astman_send_error(s, m, "Failed to update entry");
694  } else {
695  astman_send_ack(s, m, "Updated database successfully");
696  }
697  return 0;
698 }
Definition: ast_expr2.c:325
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:2135
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:1860
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: db.c:260
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:2130
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().

185 {
186  DBT key = { 0, }, value = { 0, }, last_key = { 0, };
187  int counter = 0;
188  int res, last = 0;
189  char last_key_s[MAX_DB_FIELD];
190 
192  if (dbinit()) {
194  return -1;
195  }
196 
197  /* Somehow, the database can become corrupted such that astdb->seq will continue looping through
198  * the database indefinitely. The pointer to last_key.data ends up getting re-used by the BDB lib
199  * so this specifically queries for the last entry, makes a copy of the key, and then uses it as
200  * a sentinel to avoid repeatedly looping over the list. */
201 
202  if (astdb->seq(astdb, &last_key, &value, R_LAST)) {
203  /* Empty database */
205  return 0;
206  }
207 
208  memcpy(last_key_s, last_key.data, MIN(last_key.size - 1, sizeof(last_key_s)));
209  last_key_s[last_key.size - 1] = '\0';
210  for (res = astdb->seq(astdb, &key, &value, R_FIRST);
211  !res;
212  res = astdb->seq(astdb, &key, &value, R_NEXT)) {
213  /* The callback might delete the key, so we have to check it before calling */
214  last = !strcmp(dbt_data2str_full(&key, "<bad key>"), last_key_s);
215  counter += cb(&key, &value, filter, data);
216  if (last) {
217  break;
218  }
219  }
220 
221  if (sync) {
222  db_sync();
223  }
224 
226 
227  return counter;
228 }
static const char * dbt_data2str_full(DBT *dbt, const char *def)
Definition: db.c:169
#define ast_mutex_lock(a)
Definition: lock.h:155
static ast_mutex_t dblock
Definition: db.c:110
int value
Definition: syslog.c:39
static int dbinit(void)
Definition: db.c:118
#define MIN(a, b)
Definition: utils.h:226
static DB * astdb
Definition: db.c:109
struct sla_ringing_trunk * last
Definition: app_meetme.c:965
static void db_sync(void)
Definition: db.c:800
#define MAX_DB_FIELD
Definition: db.c:107
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:694
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int subkeymatch ( const char *  key,
const char *  suffix 
)
inlinestatic

Definition at line 144 of file db.c.

Referenced by db_showkey_cb().

145 {
146  int suffixlen = strlen(suffix);
147  if (suffixlen) {
148  const char *subkey = key + strlen(key) - suffixlen;
149  if (subkey < key)
150  return 0;
151  if (!strcasecmp(subkey, suffix))
152  return 1;
153  }
154  return 0;
155 }

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.