Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
Macros | Functions | Variables
db.c File Reference

ASTdb Management. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/paths.h"
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <dirent.h>
#include <sqlite3.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/manager.h"
Include dependency graph for db.c:

Go to the source code of this file.

Macros

#define DEFINE_SQL_STATEMENT(stmt, sql)
 

Functions

static int ast_db_begin_transaction (void)
 
static int ast_db_commit_transaction (void)
 
int ast_db_del (const char *family, const char *key)
 Delete entry in astdb. More...
 
int ast_db_del2 (const char *family, const char *key)
 Same as ast_db_del, but with more stringent error checking. More...
 
int ast_db_deltree (const char *family, const char *keytree)
 Delete one or more entries in astdb. More...
 
int ast_db_exists (const char *family, const char *key)
 Check if family/key exitsts. 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 *key, char *value, int valuelen)
 Get key value specified by family/key. More...
 
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. More...
 
struct ast_db_entryast_db_gettree (const char *family, const char *keytree)
 Get a list of values within the astdb tree. More...
 
struct ast_db_entryast_db_gettree_by_prefix (const char *family, const char *key_prefix)
 Get a list of values with the given key prefix. More...
 
int ast_db_put (const char *family, const char *key, const char *value)
 Store value addressed by family/key. More...
 
static int ast_db_rollback_transaction (void)
 
static void astdb_atexit (void)
 
int astdb_init (void)
 
static void clean_statements (void)
 
static int clean_stmt (sqlite3_stmt **stmt, const char *sql)
 
static char * create_prefix (const char *family, const char *keytree, int *prefix_len)
 
static int db_create_astdb (void)
 
static int db_execute_sql (const char *sql, int(*callback)(void *, int, char **, char **), void *arg)
 
static int db_get_common (const char *family, const char *key, char **buffer, int bufferlen)
 
static struct ast_db_entrydb_gettree_common (sqlite3_stmt *stmt)
 
static int db_init (void)
 
static int db_open (void)
 
static void db_sync (void)
 
static void * db_sync_thread (void *data)
 
 DEFINE_SQL_STATEMENT (put_stmt, "INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)")
 
static int display_results (void *arg, int columns, char **values, char **colnames)
 
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_query (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 init_statements (void)
 
static int manager_db_tree_get (struct mansession *s, const struct message *m)
 
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)
 

Variables

static sqlite3 * 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, 0} }
 
static int doexit
 
static int dosync
 
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

◆ DEFINE_SQL_STATEMENT

#define DEFINE_SQL_STATEMENT (   stmt,
  sql 
)
Value:
static sqlite3_stmt *stmt; \
const char stmt##_sql[] = sql;

Definition at line 145 of file db.c.

Function Documentation

◆ ast_db_begin_transaction()

static int ast_db_begin_transaction ( void  )
static

Definition at line 320 of file db.c.

321{
322 return db_execute_sql("BEGIN TRANSACTION", NULL, NULL);
323}
static int db_execute_sql(const char *sql, int(*callback)(void *, int, char **, char **), void *arg)
Definition: db.c:306
#define NULL
Definition: resample.c:96

References db_execute_sql(), and NULL.

Referenced by db_sync_thread().

◆ ast_db_commit_transaction()

static int ast_db_commit_transaction ( void  )
static

Definition at line 325 of file db.c.

326{
327 return db_execute_sql("COMMIT", NULL, NULL);
328}

References db_execute_sql(), and NULL.

Referenced by db_sync_thread().

◆ ast_db_del()

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

Delete entry in astdb.

Definition at line 472 of file db.c.

473{
474 char *fullkey;
475 int fullkey_len;
476 int res = 0;
477
478 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
479 if (fullkey_len < 0) {
480 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
481 family, key);
482 return -1;
483 }
484
486 if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
487 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
488 res = -1;
489 } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
490 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
491 res = -1;
492 }
493 sqlite3_reset(del_stmt);
494 db_sync();
496 ast_free(fullkey);
497
498 return res;
499}
#define ast_free(a)
Definition: astmm.h:180
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ast_log
Definition: astobj2.c:42
static void db_sync(void)
Definition: db.c:1176
static sqlite3 * astdb
Definition: db.c:138
static ast_mutex_t dblock
Definition: db.c:136
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
#define ast_mutex_unlock(a)
Definition: lock.h:194
#define ast_mutex_lock(a)
Definition: lock.h:193

References ast_asprintf, ast_debug, ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, and LOG_WARNING.

Referenced by __expire_registry(), add_cert_key_to_astdb(), ast_privacy_set(), AST_TEST_DEFINE(), auth_exec(), cache_lookup_internal(), cleanup_cert_from_astdb_and_fs(), destroy_all_channels(), dialgroup_refreshdb(), do_register_expire(), dump_queue_members(), function_db_delete(), handle_dbdel(), media_cache_item_del_from_astdb(), media_cache_remove_from_astdb(), mkintf(), process_clearcache(), reload_queue_members(), sorcery_astdb_delete(), stasis_app_device_state_delete(), and update_registry().

◆ ast_db_del2()

int ast_db_del2 ( const char *  family,
const char *  key 
)

Same as ast_db_del, but with more stringent error checking.

Unlike ast_db_del, if the key does not exist in the first place, an error is emitted and -1 is returned.

Return values
-1An error occured (including key not found to begin with)
0Successfully deleted

Definition at line 501 of file db.c.

502{
503 char *fullkey;
504 char tmp[1];
505 int fullkey_len;
506 int mres, res = 0;
507
508 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
509 if (fullkey_len < 0) {
510 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
511 family, key);
512 return -1;
513 }
514
516 if (ast_db_get(family, key, tmp, sizeof(tmp))) {
517 ast_log(LOG_WARNING, "AstDB key %s does not exist\n", fullkey);
518 res = -1;
519 } else if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
520 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
521 res = -1;
522 } else if ((mres = sqlite3_step(del_stmt) != SQLITE_DONE)) {
523 ast_log(LOG_WARNING, "AstDB error (%s): %s\n", fullkey, sqlite3_errstr(mres));
524 res = -1;
525 }
526 sqlite3_reset(del_stmt);
527 db_sync();
529 ast_free(fullkey);
530
531 return res;
532}
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: db.c:421

References ast_asprintf, ast_db_get(), ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, and LOG_WARNING.

Referenced by handle_cli_database_del(), and manager_dbdel().

◆ ast_db_deltree()

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

560{
561 sqlite3_stmt *stmt = deltree_stmt;
562 char *prefix = NULL;
563 int prefix_len = 0;
564 int res = 0;
565
566 if (ast_strlen_zero(family) && !ast_strlen_zero(keytree)) {
567 ast_log(LOG_WARNING, "Key tree '%s' specified without family\n", keytree);
568 return -1;
569 }
570
571 prefix = create_prefix(family, keytree, &prefix_len);
572 if (!prefix) {
573 return -1;
574 }
575 if (prefix_len == 0) {
576 stmt = deltree_all_stmt;
577 }
578
580 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
581 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
582 res = -1;
583 } else if (sqlite3_step(stmt) != SQLITE_DONE) {
584 ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
585 res = -1;
586 }
587 res = sqlite3_changes(astdb);
588 sqlite3_reset(stmt);
589 db_sync();
592
593 return res;
594}
static char * create_prefix(const char *family, const char *keytree, int *prefix_len)
Definition: db.c:534
static char prefix[MAX_PREFIX]
Definition: http.c:144
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65

References ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), astdb, create_prefix(), db_sync(), dblock, LOG_WARNING, NULL, and prefix.

Referenced by ast_privacy_reset(), AST_TEST_DEFINE(), cleanup_cert_from_astdb_and_fs(), deinitialize_sorcery(), deltree_exec(), dundi_flush(), handle_cli_database_deltree(), handle_dbdeltree(), iax_provision_reload(), manager_dbdeltree(), media_cache_item_del_from_astdb(), and media_cache_remove_from_astdb().

◆ ast_db_exists()

int ast_db_exists ( const char *  family,
const char *  key 
)

Check if family/key exitsts.

Parameters
family
key
Return values
1if family/key exists
0if family/key does not exist or an error occurred

Definition at line 438 of file db.c.

439{
440 int result;
441 char *fullkey;
442 int fullkey_len;
443 int res = 0;
444
445 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
446 if (fullkey_len < 0) {
447 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
448 family, key);
449 return -1;
450 }
451
453 res = sqlite3_bind_text(exists_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC);
454 if (res != SQLITE_OK) {
455 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %d:%s\n", res, sqlite3_errmsg(astdb));
456 res = 0;
457 } else if (sqlite3_step(exists_stmt) != SQLITE_ROW) {
458 res = 0;
459 } else if (!(result = sqlite3_column_int(exists_stmt, 0))) {
460 res = 0;
461 } else {
462 res = result;
463 }
464 sqlite3_reset(exists_stmt);
466 ast_free(fullkey);
467
468 return res;
469}
static PGresult * result
Definition: cel_pgsql.c:84

References ast_asprintf, ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, dblock, LOG_WARNING, and result.

Referenced by cleanup_cert_from_astdb_and_fs(), and retrieve_cert_from_cache().

◆ ast_db_freetree()

void ast_db_freetree ( struct ast_db_entry dbe)

◆ ast_db_get()

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

Get key value specified by family/key.

Definition at line 421 of file db.c.

422{
424
425 /* Make sure we initialize */
426 value[0] = 0;
427
428 return db_get_common(family, key, &value, valuelen);
429}
static int db_get_common(const char *family, const char *key, char **buffer, int bufferlen)
Definition: db.c:381
int value
Definition: syslog.c:37
#define ast_assert(a)
Definition: utils.h:739

References ast_assert, db_get_common(), NULL, and value.

Referenced by ast_db_del2(), ast_pbx_uuid_get(), ast_privacy_check(), AST_TEST_DEFINE(), auth_exec(), blacklist_read(), cache_lookup_internal(), check_access(), create_addr(), custom_devstate_callback(), custom_presence_callback(), database_increment(), destroy_all_channels(), function_db_delete(), function_db_exists(), function_db_read(), handle_dbget(), iax_provision_version(), load_password(), mkintf(), populate_addr(), refresh_all_favorite(), reg_source_db(), retrieve_cert_from_cache(), sorcery_astdb_delete(), sorcery_astdb_update(), and stasis_device_state_cb().

◆ ast_db_get_allocated()

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

432{
433 *out = NULL;
434
435 return db_get_common(family, key, out, -1);
436}
FILE * out
Definition: utils/frame.c:33

References db_get_common(), NULL, and out.

Referenced by AST_TEST_DEFINE(), handle_cli_database_get(), manager_dbget(), media_cache_item_del_from_astdb(), reload_queue_members(), and sorcery_astdb_retrieve_id().

◆ ast_db_gettree()

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

636{
637 char *prefix = NULL;
638 int prefix_len = 0;
639 sqlite3_stmt *stmt = gettree_stmt;
640 struct ast_db_entry *ret;
641
642 prefix = create_prefix(family, keytree, &prefix_len);
643 if (!prefix) {
644 return NULL;
645 }
646 if (prefix_len == 0) {
647 stmt = gettree_all_stmt;
648 }
649
651 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
652 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
653 sqlite3_reset(stmt);
656 return NULL;
657 }
658
659 ret = db_gettree_common(stmt);
660 sqlite3_reset(stmt);
663
664 return ret;
665}
static struct ast_db_entry * db_gettree_common(sqlite3_stmt *stmt)
Definition: db.c:596

References ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, create_prefix(), db_gettree_common(), dblock, LOG_WARNING, NULL, and prefix.

Referenced by AST_TEST_DEFINE(), dundi_show_cache(), dundi_show_hints(), function_db_keycount(), function_db_keys(), handle_cli_devstate_list(), handle_cli_presencestate_list(), load_module(), media_cache_item_populate_from_astdb(), media_cache_populate_from_astdb(), populate_cache(), process_clearcache(), reload_queue_members(), sorcery_astdb_retrieve_fields_common(), sorcery_astdb_retrieve_regex(), and stasis_app_device_states_to_json().

◆ ast_db_gettree_by_prefix()

struct ast_db_entry * ast_db_gettree_by_prefix ( const char *  family,
const char *  key_prefix 
)

Get a list of values with the given key prefix.

Parameters
familyThe family to search under
key_prefixThe key prefix to search under
Return values
NULLAn error occurred

Definition at line 667 of file db.c.

668{
669 char *prefix = NULL;
670 int prefix_len = 0;
671 struct ast_db_entry *ret;
672
673 prefix = create_prefix(family, key_prefix, &prefix_len);
674 if (!prefix) {
675 return NULL;
676 }
677
679 if (sqlite3_bind_text(gettree_prefix_stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK) {
680 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
681 sqlite3_reset(gettree_prefix_stmt);
684 return NULL;
685 }
686
687 ret = db_gettree_common(gettree_prefix_stmt);
688 sqlite3_reset(gettree_prefix_stmt);
691
692 return ret;
693}

References ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, create_prefix(), db_gettree_common(), dblock, LOG_WARNING, NULL, and prefix.

Referenced by sorcery_astdb_retrieve_prefix().

◆ ast_db_put()

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

Store value addressed by family/key.

Definition at line 335 of file db.c.

336{
337 char *fullkey;
338 int fullkey_len;
339 int res = 0;
340
341 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
342 if (fullkey_len < 0) {
343 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
344 family, key);
345 return -1;
346 }
347
349 if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
350 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
351 res = -1;
352 } else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
353 ast_log(LOG_WARNING, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
354 res = -1;
355 } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
356 ast_log(LOG_WARNING, "Couldn't execute statement: %s\n", sqlite3_errmsg(astdb));
357 res = -1;
358 }
359 sqlite3_reset(put_stmt);
360 db_sync();
362 ast_free(fullkey);
363
364 return res;
365}

References ast_asprintf, ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, LOG_WARNING, and value.

Referenced by __analog_ss_thread(), add_cert_expiration_to_astdb(), add_cert_key_to_astdb(), ast_privacy_set(), AST_TEST_DEFINE(), asterisk_daemon(), 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_cli_presencestate_change(), handle_command_response(), handle_dbput(), iax_provision_build(), manager_dbput(), media_cache_item_sync_to_astdb(), metadata_sync_to_astdb(), presence_write(), save_secret(), sorcery_astdb_create(), stasis_app_device_state_update(), and update_registry().

◆ ast_db_rollback_transaction()

static int ast_db_rollback_transaction ( void  )
static

Definition at line 330 of file db.c.

331{
332 return db_execute_sql("ROLLBACK", NULL, NULL);
333}

References db_execute_sql(), and NULL.

Referenced by db_sync_thread().

◆ astdb_atexit()

static void astdb_atexit ( void  )
static

Definition at line 1225 of file db.c.

1226{
1228 ast_manager_unregister("DBGet");
1229 ast_manager_unregister("DBGetTree");
1230 ast_manager_unregister("DBPut");
1231 ast_manager_unregister("DBDel");
1232 ast_manager_unregister("DBDelTree");
1233
1234 /* Set doexit to 1 to kill thread. db_sync must be called with
1235 * mutex held. */
1237 doexit = 1;
1238 db_sync();
1240
1241 pthread_join(syncthread, NULL);
1244 if (sqlite3_close(astdb) == SQLITE_OK) {
1245 astdb = NULL;
1246 }
1248}
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static int doexit
Definition: db.c:140
static void clean_statements(void)
Definition: db.c:205
static struct ast_cli_entry cli_database[]
Definition: db.c:974
static pthread_t syncthread
Definition: db.c:139
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:7697
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_manager_unregister(), ast_mutex_lock, ast_mutex_unlock, astdb, clean_statements(), cli_database, db_sync(), dblock, doexit, NULL, and syncthread.

Referenced by astdb_init().

◆ astdb_init()

int astdb_init ( void  )

Provided by db.c

Definition at line 1250 of file db.c.

1251{
1253
1254 if (db_init()) {
1255 return -1;
1256 }
1257
1259 return -1;
1260 }
1261
1269 return 0;
1270}
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: clicompat.c:13
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static int manager_dbdel(struct mansession *s, const struct message *m)
Definition: db.c:1117
static int manager_db_tree_get(struct mansession *s, const struct message *m)
Definition: db.c:1051
static int manager_dbdeltree(struct mansession *s, const struct message *m)
Definition: db.c:1142
static int manager_dbget(struct mansession *s, const struct message *m)
Definition: db.c:1009
static int db_init(void)
Definition: db.c:290
static int manager_dbput(struct mansession *s, const struct message *m)
Definition: db.c:984
static void * db_sync_thread(void *data)
Definition: db.c:1192
static ast_cond_t dbcond
Definition: db.c:137
static void astdb_atexit(void)
Definition: db.c:1225
#define ast_cond_init(cond, attr)
Definition: lock.h:205
#define EVENT_FLAG_REPORTING
Definition: manager.h:84
#define ast_manager_register_xml_core(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:203
#define EVENT_FLAG_SYSTEM
Definition: manager.h:75
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:592

References ARRAY_LEN, ast_cli_register_multiple, ast_cond_init, ast_manager_register_xml_core, ast_pthread_create_background, ast_register_atexit(), astdb_atexit(), cli_database, db_init(), db_sync_thread(), dbcond, EVENT_FLAG_REPORTING, EVENT_FLAG_SYSTEM, manager_db_tree_get(), manager_dbdel(), manager_dbdeltree(), manager_dbget(), manager_dbput(), NULL, and syncthread.

Referenced by asterisk_daemon().

◆ clean_statements()

static void clean_statements ( void  )
static

Definition at line 205 of file db.c.

206{
207 clean_stmt(&get_stmt, get_stmt_sql);
208 clean_stmt(&exists_stmt, exists_stmt_sql);
209 clean_stmt(&del_stmt, del_stmt_sql);
210 clean_stmt(&deltree_stmt, deltree_stmt_sql);
211 clean_stmt(&deltree_all_stmt, deltree_all_stmt_sql);
212 clean_stmt(&gettree_stmt, gettree_stmt_sql);
213 clean_stmt(&gettree_all_stmt, gettree_all_stmt_sql);
214 clean_stmt(&gettree_prefix_stmt, gettree_prefix_stmt_sql);
215 clean_stmt(&showkey_stmt, showkey_stmt_sql);
216 clean_stmt(&put_stmt, put_stmt_sql);
217 clean_stmt(&create_astdb_stmt, create_astdb_stmt_sql);
218}
static int clean_stmt(sqlite3_stmt **stmt, const char *sql)
Definition: db.c:190

References clean_stmt().

Referenced by astdb_atexit().

◆ clean_stmt()

static int clean_stmt ( sqlite3_stmt **  stmt,
const char *  sql 
)
static

Definition at line 190 of file db.c.

191{
192 if (sqlite3_finalize(*stmt) != SQLITE_OK) {
193 ast_log(LOG_WARNING, "Couldn't finalize statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
194 *stmt = NULL;
195 return -1;
196 }
197 *stmt = NULL;
198 return 0;
199}

References ast_log, astdb, LOG_WARNING, and NULL.

Referenced by clean_statements().

◆ create_prefix()

static char * create_prefix ( const char *  family,
const char *  keytree,
int *  prefix_len 
)
static

Definition at line 534 of file db.c.

536{
537 char *prefix = NULL;
538 *prefix_len = 0;
539
540 if (!ast_strlen_zero(family)) {
541 if (!ast_strlen_zero(keytree)) {
542 /* Family and key tree */
543 *prefix_len = ast_asprintf(&prefix, "/%s/%s", family, keytree);
544 } else {
545 /* Family only */
546 *prefix_len = ast_asprintf(&prefix, "/%s", family);
547 }
548 if (*prefix_len < 0) {
549 ast_log(LOG_WARNING, "Unable to allocate memory for family/keytree '/%s%s%s'\n",
550 S_OR(family, ""), S_COR(keytree, "/", ""), S_OR(keytree, ""));
551 return NULL;
552 }
553 } else {
554 prefix = ast_strdup("");
555 }
556 return prefix;
557}
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:87

References ast_asprintf, ast_log, ast_strdup, ast_strlen_zero(), LOG_WARNING, NULL, prefix, S_COR, and S_OR.

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

◆ db_create_astdb()

static int db_create_astdb ( void  )
static

Definition at line 236 of file db.c.

237{
238 int res = 0;
239
240 if (!create_astdb_stmt) {
241 init_stmt(&create_astdb_stmt, create_astdb_stmt_sql, sizeof(create_astdb_stmt_sql));
242 }
243
245 if (sqlite3_step(create_astdb_stmt) != SQLITE_DONE) {
246 ast_log(LOG_WARNING, "Couldn't create astdb table: %s\n", sqlite3_errmsg(astdb));
247 res = -1;
248 }
249 sqlite3_reset(create_astdb_stmt);
250 db_sync();
252
253 return res;
254}

References ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, and LOG_WARNING.

Referenced by db_init().

◆ db_execute_sql()

static int db_execute_sql ( const char *  sql,
int(*)(void *, int, char **, char **)  callback,
void *  arg 
)
static

Definition at line 306 of file db.c.

307{
308 char *errmsg = NULL;
309 int res =0;
310
311 if (sqlite3_exec(astdb, sql, callback, arg, &errmsg) != SQLITE_OK) {
312 ast_log(LOG_WARNING, "Error executing SQL (%s): %s\n", sql, errmsg);
313 sqlite3_free(errmsg);
314 res = -1;
315 }
316
317 return res;
318}

References ast_log, astdb, LOG_WARNING, and NULL.

Referenced by ast_db_begin_transaction(), ast_db_commit_transaction(), ast_db_rollback_transaction(), and handle_cli_database_query().

◆ db_get_common()

static int db_get_common ( const char *  family,
const char *  key,
char **  buffer,
int  bufferlen 
)
static

Definition at line 381 of file db.c.

382{
383 const unsigned char *result;
384 char *fullkey;
385 int fullkey_len;
386 int res = 0;
387
388 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
389 if (fullkey_len < 0) {
390 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
391 family, key);
392 return -1;
393 }
394
396 if (sqlite3_bind_text(get_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
397 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
398 res = -1;
399 } else if (sqlite3_step(get_stmt) != SQLITE_ROW) {
400 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
401 res = -1;
402 } else if (!(result = sqlite3_column_text(get_stmt, 0))) {
403 ast_log(LOG_WARNING, "Couldn't get value\n");
404 res = -1;
405 } else {
406 const char *value = (const char *) result;
407
408 if (bufferlen == -1) {
409 *buffer = ast_strdup(value);
410 } else {
411 ast_copy_string(*buffer, value, bufferlen);
412 }
413 }
414 sqlite3_reset(get_stmt);
416 ast_free(fullkey);
417
418 return res;
419}
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425

References ast_asprintf, ast_copy_string(), ast_debug, ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, ast_strdup, astdb, dblock, LOG_WARNING, result, and value.

Referenced by ast_db_get(), and ast_db_get_allocated().

◆ db_gettree_common()

static struct ast_db_entry * db_gettree_common ( sqlite3_stmt *  stmt)
static

Definition at line 596 of file db.c.

597{
598 struct ast_db_entry *head = NULL, *prev = NULL, *cur;
599
600 while (sqlite3_step(stmt) == SQLITE_ROW) {
601 const char *key, *value;
602 size_t key_len, value_len;
603
604 key = (const char *) sqlite3_column_text(stmt, 0);
605 value = (const char *) sqlite3_column_text(stmt, 1);
606
607 if (!key || !value) {
608 break;
609 }
610
611 key_len = strlen(key);
612 value_len = strlen(value);
613
614 cur = ast_malloc(sizeof(*cur) + key_len + value_len + 2);
615 if (!cur) {
616 break;
617 }
618
619 cur->next = NULL;
620 cur->key = cur->data + value_len + 1;
621 memcpy(cur->data, value, value_len + 1);
622 memcpy(cur->key, key, key_len + 1);
623
624 if (prev) {
625 prev->next = cur;
626 } else {
627 head = cur;
628 }
629 prev = cur;
630 }
631
632 return head;
633}
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
char * key
Definition: astdb.h:33

References ast_malloc, ast_db_entry::key, NULL, and value.

Referenced by ast_db_gettree(), and ast_db_gettree_by_prefix().

◆ db_init()

static int db_init ( void  )
static

Definition at line 290 of file db.c.

291{
292 if (astdb) {
293 return 0;
294 }
295
296 if (db_open() || db_create_astdb() || init_statements()) {
297 return -1;
298 }
299
300 return 0;
301}
static int db_create_astdb(void)
Definition: db.c:236
static int db_open(void)
Definition: db.c:256
static int init_statements(void)
Definition: db.c:220

References astdb, db_create_astdb(), db_open(), and init_statements().

Referenced by astdb_init().

◆ db_open()

static int db_open ( void  )
static

Definition at line 256 of file db.c.

257{
258 struct stat dont_care;
259 char dbname[strlen(ast_config_AST_DB) + sizeof(".sqlite3")];
260
261 strcpy(dbname, ast_config_AST_DB);
262 strcat(dbname, ".sqlite3");
263
264 if (stat(dbname, &dont_care) && !stat(ast_config_AST_DB, &dont_care)) {
265 ast_log(LOG_ERROR, "When Asterisk 10.0.0 was released, the format of Asterisk's internal\n");
266 ast_log(LOG_ERROR, "database was changed from Berkeley DB to SQLite3. Part of that change\n");
267 ast_log(LOG_ERROR, "involved the creation of a conversion utility - astdb2sqlite3 - that\n");
268 ast_log(LOG_ERROR, "was shipped with Asterisk. This conversion utility performed a\n");
269 ast_log(LOG_ERROR, "one-time migration from the old format to the new one.\n");
270 ast_log(LOG_ERROR, "\n");
271 ast_log(LOG_ERROR, "Starting with Asterisk 23.0.0, astdb2sqlite3 no longer ships as part\n");
272 ast_log(LOG_ERROR, "of the Asterisk distribution. If you are upgrading from a version of\n");
273 ast_log(LOG_ERROR, "Asterisk that still uses the Berkeley DB implementation, you will need\n");
274 ast_log(LOG_ERROR, "to acquire astdb2sqlite3 from an earlier release of Asterisk.\n");
275 }
276
278 if (sqlite3_open(dbname, &astdb) != SQLITE_OK) {
279 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(astdb));
280 sqlite3_close(astdb);
282 return -1;
283 }
284
286
287 return 0;
288}
#define LOG_ERROR
const char * ast_config_AST_DB
Definition: options.c:165
static char dbname[MAX_DB_OPTION_SIZE]

References ast_config_AST_DB, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, dblock, dbname, LOG_ERROR, and LOG_WARNING.

Referenced by db_init().

◆ db_sync()

static void db_sync ( void  )
static

Definition at line 1176 of file db.c.

1177{
1178 dosync = 1;
1180}
static int dosync
Definition: db.c:141
#define ast_cond_signal(cond)
Definition: lock.h:207

References ast_cond_signal, dbcond, and dosync.

Referenced by ast_db_del(), ast_db_del2(), ast_db_deltree(), ast_db_put(), astdb_atexit(), db_create_astdb(), and handle_cli_database_query().

◆ db_sync_thread()

static void * db_sync_thread ( void *  data)
static

Definition at line 1192 of file db.c.

1193{
1196 for (;;) {
1197 /* If dosync is set, db_sync() was called during sleep(1),
1198 * and the pending transaction should be committed.
1199 * Otherwise, block until db_sync() is called.
1200 */
1201 while (!dosync) {
1203 }
1204 dosync = 0;
1207 }
1208 if (doexit) {
1210 break;
1211 }
1214 sleep(1);
1216 }
1217
1218 return NULL;
1219}
static int ast_db_begin_transaction(void)
Definition: db.c:320
static int ast_db_commit_transaction(void)
Definition: db.c:325
static int ast_db_rollback_transaction(void)
Definition: db.c:330
#define ast_cond_wait(cond, mutex)
Definition: lock.h:209

References ast_cond_wait, ast_db_begin_transaction(), ast_db_commit_transaction(), ast_db_rollback_transaction(), ast_mutex_lock, ast_mutex_unlock, dbcond, dblock, doexit, dosync, and NULL.

Referenced by astdb_init().

◆ DEFINE_SQL_STATEMENT()

DEFINE_SQL_STATEMENT ( put_stmt  ,
"INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)"   
)

Definition at line 148 of file db.c.

174{
176 if (sqlite3_prepare(astdb, sql, len, stmt, NULL) != SQLITE_OK) {
177 ast_log(LOG_WARNING, "Couldn't prepare statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
179 return -1;
180 }
182
183 return 0;
184}
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

References ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, dblock, len(), LOG_WARNING, and NULL.

◆ display_results()

static int display_results ( void *  arg,
int  columns,
char **  values,
char **  colnames 
)
static

Definition at line 935 of file db.c.

936{
937 struct ast_cli_args *a = arg;
938 size_t x;
939
940 for (x = 0; x < columns; x++) {
941 ast_cli(a->fd, "%-5s: %-50s\n", colnames[x], values[x]);
942 }
943 ast_cli(a->fd, "\n");
944
945 return 0;
946}
static char * columns
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
static struct test_val a

References a, ast_cli(), and columns.

Referenced by handle_cli_database_query().

◆ handle_cli_database_del()

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

Definition at line 762 of file db.c.

763{
764 int res;
765
766 switch (cmd) {
767 case CLI_INIT:
768 e->command = "database del";
769 e->usage =
770 "Usage: database del <family> <key>\n"
771 " Deletes an entry in the Asterisk database for a given\n"
772 " family and key.\n";
773 return NULL;
774 case CLI_GENERATE:
775 return NULL;
776 }
777
778 if (a->argc != 4)
779 return CLI_SHOWUSAGE;
780 res = ast_db_del2(a->argv[2], a->argv[3]);
781 if (res) {
782 ast_cli(a->fd, "Database entry could not be removed.\n");
783 } else {
784 ast_cli(a->fd, "Database entry removed.\n");
785 }
786 return CLI_SUCCESS;
787}
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
int ast_db_del2(const char *family, const char *key)
Same as ast_db_del, but with more stringent error checking.
Definition: db.c:501
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177

References a, ast_cli(), ast_db_del2(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, and ast_cli_entry::usage.

◆ handle_cli_database_deltree()

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

Definition at line 789 of file db.c.

790{
791 int num_deleted;
792
793 switch (cmd) {
794 case CLI_INIT:
795 e->command = "database deltree";
796 e->usage =
797 "Usage: database deltree <family> [keytree]\n"
798 " OR: database deltree <family>[/keytree]\n"
799 " Deletes a family or specific keytree within a family\n"
800 " in the Asterisk database. The two arguments may be\n"
801 " separated by either a space or a slash.\n";
802 return NULL;
803 case CLI_GENERATE:
804 return NULL;
805 }
806
807 if ((a->argc < 3) || (a->argc > 4))
808 return CLI_SHOWUSAGE;
809 if (a->argc == 4) {
810 num_deleted = ast_db_deltree(a->argv[2], a->argv[3]);
811 } else {
812 num_deleted = ast_db_deltree(a->argv[2], NULL);
813 }
814 if (num_deleted < 0) {
815 ast_cli(a->fd, "Database unavailable.\n");
816 } else if (num_deleted == 0) {
817 ast_cli(a->fd, "Database entries do not exist.\n");
818 } else {
819 ast_cli(a->fd, "%d database entries removed.\n",num_deleted);
820 }
821 return CLI_SUCCESS;
822}
int ast_db_deltree(const char *family, const char *keytree)
Delete one or more entries in astdb.
Definition: db.c:559

References a, ast_cli(), ast_db_deltree(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, and ast_cli_entry::usage.

◆ handle_cli_database_get()

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

Definition at line 732 of file db.c.

733{
734 int res;
735 char *tmp = NULL;
736
737 switch (cmd) {
738 case CLI_INIT:
739 e->command = "database get";
740 e->usage =
741 "Usage: database get <family> <key>\n"
742 " Retrieves an entry in the Asterisk database for a given\n"
743 " family and key.\n";
744 return NULL;
745 case CLI_GENERATE:
746 return NULL;
747 }
748
749 if (a->argc != 4)
750 return CLI_SHOWUSAGE;
751 res = ast_db_get_allocated(a->argv[2], a->argv[3], &tmp);
752 if (res) {
753 ast_cli(a->fd, "Database entry not found.\n");
754 } else {
755 ast_cli(a->fd, "Value: %s\n", tmp);
756 ast_free(tmp);
757 }
758
759 return CLI_SUCCESS;
760}
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.
Definition: db.c:431

References a, ast_cli(), ast_db_get_allocated(), ast_free, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, and ast_cli_entry::usage.

◆ handle_cli_database_put()

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

Definition at line 705 of file db.c.

706{
707 int res;
708
709 switch (cmd) {
710 case CLI_INIT:
711 e->command = "database put";
712 e->usage =
713 "Usage: database put <family> <key> <value>\n"
714 " Adds or updates an entry in the Asterisk database for\n"
715 " a given family, key, and value.\n";
716 return NULL;
717 case CLI_GENERATE:
718 return NULL;
719 }
720
721 if (a->argc != 5)
722 return CLI_SHOWUSAGE;
723 res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
724 if (res) {
725 ast_cli(a->fd, "Failed to update entry\n");
726 } else {
727 ast_cli(a->fd, "Updated database successfully\n");
728 }
729 return CLI_SUCCESS;
730}
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: db.c:335

References a, ast_cli(), ast_db_put(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, and ast_cli_entry::usage.

◆ handle_cli_database_query()

static char * handle_cli_database_query ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 948 of file db.c.

949{
950
951 switch (cmd) {
952 case CLI_INIT:
953 e->command = "database query";
954 e->usage =
955 "Usage: database query \"<SQL Statement>\"\n"
956 " Run a user-specified SQL query on the database. Be careful.\n";
957 return NULL;
958 case CLI_GENERATE:
959 return NULL;
960 }
961
962 if (a->argc != 3) {
963 return CLI_SHOWUSAGE;
964 }
965
967 db_execute_sql(a->argv[2], display_results, a);
968 db_sync(); /* Go ahead and sync the db in case they write */
970
971 return CLI_SUCCESS;
972}
static int display_results(void *arg, int columns, char **values, char **colnames)
Definition: db.c:935

References a, ast_mutex_lock, ast_mutex_unlock, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, db_execute_sql(), db_sync(), dblock, display_results(), NULL, and ast_cli_entry::usage.

◆ handle_cli_database_show()

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

Definition at line 824 of file db.c.

825{
826 char *prefix = NULL;
827 int prefix_len = 0;
828 const char *family = a->argc > 2 ? a->argv[2] : "";
829 const char *keytree = a->argc > 3 ? a->argv[3] : "";
830 int counter = 0;
831 sqlite3_stmt *stmt = gettree_stmt;
832
833 switch (cmd) {
834 case CLI_INIT:
835 e->command = "database show";
836 e->usage =
837 "Usage: database show [family [keytree]]\n"
838 " OR: database show [family[/keytree]]\n"
839 " Shows Asterisk database contents, optionally restricted\n"
840 " to a given family, or family and keytree. The two arguments\n"
841 " may be separated either by a space or by a slash.\n";
842 return NULL;
843 case CLI_GENERATE:
844 return NULL;
845 }
846
847 if (a->argc > 4) {
848 return CLI_SHOWUSAGE;
849 }
850
851 prefix = create_prefix(family, keytree, &prefix_len);
852 if (!prefix) {
853 return NULL;
854 }
855 if (prefix_len == 0) {
856 stmt = gettree_all_stmt;
857 }
858
860 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
861 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
862 sqlite3_reset(stmt);
865 return NULL;
866 }
867
868 while (sqlite3_step(stmt) == SQLITE_ROW) {
869 const char *key_s, *value_s;
870 if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
871 ast_log(LOG_WARNING, "Skipping invalid key!\n");
872 continue;
873 }
874 if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
875 ast_log(LOG_WARNING, "Skipping invalid value!\n");
876 continue;
877 }
878 ++counter;
879 ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
880 }
881
882 sqlite3_reset(stmt);
885
886 ast_cli(a->fd, "%d results found.\n", counter);
887 return CLI_SUCCESS;
888}

References a, ast_cli(), ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, create_prefix(), dblock, LOG_WARNING, NULL, prefix, and ast_cli_entry::usage.

◆ handle_cli_database_showkey()

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

Definition at line 890 of file db.c.

891{
892 int counter = 0;
893
894 switch (cmd) {
895 case CLI_INIT:
896 e->command = "database showkey";
897 e->usage =
898 "Usage: database showkey <keytree>\n"
899 " Shows Asterisk database contents, restricted to a given key.\n";
900 return NULL;
901 case CLI_GENERATE:
902 return NULL;
903 }
904
905 if (a->argc != 3) {
906 return CLI_SHOWUSAGE;
907 }
908
910 if (!ast_strlen_zero(a->argv[2]) && (sqlite3_bind_text(showkey_stmt, 1, a->argv[2], -1, SQLITE_STATIC) != SQLITE_OK)) {
911 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", a->argv[2], sqlite3_errmsg(astdb));
912 sqlite3_reset(showkey_stmt);
914 return NULL;
915 }
916
917 while (sqlite3_step(showkey_stmt) == SQLITE_ROW) {
918 const char *key_s, *value_s;
919 if (!(key_s = (const char *) sqlite3_column_text(showkey_stmt, 0))) {
920 break;
921 }
922 if (!(value_s = (const char *) sqlite3_column_text(showkey_stmt, 1))) {
923 break;
924 }
925 ++counter;
926 ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
927 }
928 sqlite3_reset(showkey_stmt);
930
931 ast_cli(a->fd, "%d results found.\n", counter);
932 return CLI_SUCCESS;
933}

References a, ast_cli(), ast_log, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), astdb, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, dblock, LOG_WARNING, NULL, and ast_cli_entry::usage.

◆ init_statements()

static int init_statements ( void  )
static

Definition at line 220 of file db.c.

221{
222 /* Don't initialize create_astdb_statement here as the astdb table needs to exist
223 * before these statements can be initialized */
224 return init_stmt(&get_stmt, get_stmt_sql, sizeof(get_stmt_sql))
225 || init_stmt(&exists_stmt, exists_stmt_sql, sizeof(exists_stmt_sql))
226 || init_stmt(&del_stmt, del_stmt_sql, sizeof(del_stmt_sql))
227 || init_stmt(&deltree_stmt, deltree_stmt_sql, sizeof(deltree_stmt_sql))
228 || init_stmt(&deltree_all_stmt, deltree_all_stmt_sql, sizeof(deltree_all_stmt_sql))
229 || init_stmt(&gettree_stmt, gettree_stmt_sql, sizeof(gettree_stmt_sql))
230 || init_stmt(&gettree_all_stmt, gettree_all_stmt_sql, sizeof(gettree_all_stmt_sql))
231 || init_stmt(&gettree_prefix_stmt, gettree_prefix_stmt_sql, sizeof(gettree_prefix_stmt_sql))
232 || init_stmt(&showkey_stmt, showkey_stmt_sql, sizeof(showkey_stmt_sql))
233 || init_stmt(&put_stmt, put_stmt_sql, sizeof(put_stmt_sql));
234}

Referenced by db_init().

◆ manager_db_tree_get()

static int manager_db_tree_get ( struct mansession s,
const struct message m 
)
static

Definition at line 1051 of file db.c.

1052{
1053 char *prefix;
1054 int prefix_len = 0;
1055 char idText[256];
1056 const char *id = astman_get_header(m,"ActionID");
1057 const char *family = astman_get_header(m, "Family");
1058 const char *key = astman_get_header(m, "Key");
1059 sqlite3_stmt *stmt = gettree_stmt;
1060 int count = 0;
1061
1062 prefix = create_prefix(family, key, &prefix_len);
1063 if (!prefix) {
1064 astman_send_error(s, m, "Unable to allocate memory for Family/Key");
1065 return 0;
1066 }
1067 if (prefix_len == 0) {
1068 stmt = gettree_all_stmt;
1069 }
1070
1071 idText[0] = '\0';
1072 if (!ast_strlen_zero(id)) {
1073 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
1074 }
1075
1077 if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
1078 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
1079 sqlite3_reset(stmt);
1082 astman_send_error(s, m, "Unable to search database");
1083 return 0;
1084 }
1085
1086 astman_send_listack(s, m, "Result will follow", "start");
1087
1088 while (sqlite3_step(stmt) == SQLITE_ROW) {
1089 const char *key_s, *value_s;
1090 if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
1091 ast_log(LOG_WARNING, "Skipping invalid key!\n");
1092 continue;
1093 }
1094 if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
1095 ast_log(LOG_WARNING, "Skipping invalid value!\n");
1096 continue;
1097 }
1098 astman_append(s, "Event: DBGetTreeResponse\r\n"
1099 "Key: %s\r\n"
1100 "Val: %s\r\n"
1101 "%s"
1102 "\r\n",
1103 key_s, value_s, idText);
1104 count++;
1105 }
1106
1107 sqlite3_reset(stmt);
1110
1111 astman_send_list_complete_start(s, m, "DBGetTreeComplete", count);
1113
1114 return 0;
1115}
void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag)
Send ack in manager transaction to begin a list.
Definition: manager.c:2028
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:1986
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
Start the list complete event.
Definition: manager.c:2064
const char * astman_get_header(const struct message *m, char *var)
Get header from manager transaction.
Definition: manager.c:1647
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
Definition: manager.c:2072
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:1907

References ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), astdb, astman_append(), astman_get_header(), astman_send_error(), astman_send_list_complete_end(), astman_send_list_complete_start(), astman_send_listack(), create_prefix(), dblock, LOG_WARNING, and prefix.

Referenced by astdb_init().

◆ manager_dbdel()

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

Definition at line 1117 of file db.c.

1118{
1119 const char *family = astman_get_header(m, "Family");
1120 const char *key = astman_get_header(m, "Key");
1121 int res;
1122
1123 if (ast_strlen_zero(family)) {
1124 astman_send_error(s, m, "No family specified.");
1125 return 0;
1126 }
1127
1128 if (ast_strlen_zero(key)) {
1129 astman_send_error(s, m, "No key specified.");
1130 return 0;
1131 }
1132
1133 res = ast_db_del2(family, key);
1134 if (res)
1135 astman_send_error(s, m, "Database entry could not be deleted");
1136 else
1137 astman_send_ack(s, m, "Key deleted successfully");
1138
1139 return 0;
1140}
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:2018

References ast_db_del2(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), and astman_send_error().

Referenced by astdb_init().

◆ manager_dbdeltree()

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

Definition at line 1142 of file db.c.

1143{
1144 const char *family = astman_get_header(m, "Family");
1145 const char *key = astman_get_header(m, "Key");
1146 int num_deleted;
1147
1148 if (ast_strlen_zero(family)) {
1149 astman_send_error(s, m, "No family specified.");
1150 return 0;
1151 }
1152
1153 if (!ast_strlen_zero(key)) {
1154 num_deleted = ast_db_deltree(family, key);
1155 } else {
1156 num_deleted = ast_db_deltree(family, NULL);
1157 }
1158
1159 if (num_deleted < 0) {
1160 astman_send_error(s, m, "Database unavailable");
1161 } else if (num_deleted == 0) {
1162 astman_send_error(s, m, "Database entry not found");
1163 } else {
1164 astman_send_ack(s, m, "Key tree deleted successfully");
1165 }
1166
1167 return 0;
1168}

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

Referenced by astdb_init().

◆ manager_dbget()

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

Definition at line 1009 of file db.c.

1010{
1011 const char *id = astman_get_header(m,"ActionID");
1012 char idText[256];
1013 const char *family = astman_get_header(m, "Family");
1014 const char *key = astman_get_header(m, "Key");
1015 char *tmp = NULL;
1016 int res;
1017
1018 if (ast_strlen_zero(family)) {
1019 astman_send_error(s, m, "No family specified.");
1020 return 0;
1021 }
1022 if (ast_strlen_zero(key)) {
1023 astman_send_error(s, m, "No key specified.");
1024 return 0;
1025 }
1026
1027 idText[0] = '\0';
1028 if (!ast_strlen_zero(id))
1029 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
1030
1031 res = ast_db_get_allocated(family, key, &tmp);
1032 if (res) {
1033 astman_send_error(s, m, "Database entry not found");
1034 } else {
1035 astman_send_listack(s, m, "Result will follow", "start");
1036
1037 astman_append(s, "Event: DBGetResponse\r\n"
1038 "Family: %s\r\n"
1039 "Key: %s\r\n"
1040 "Val: %s\r\n"
1041 "%s"
1042 "\r\n",
1043 family, key, tmp, idText);
1044 ast_free(tmp);
1045 astman_send_list_complete_start(s, m, "DBGetComplete", 1);
1047 }
1048 return 0;
1049}

References ast_db_get_allocated(), ast_free, ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), astman_send_list_complete_end(), astman_send_list_complete_start(), astman_send_listack(), and NULL.

Referenced by astdb_init().

◆ manager_dbput()

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

Definition at line 984 of file db.c.

985{
986 const char *family = astman_get_header(m, "Family");
987 const char *key = astman_get_header(m, "Key");
988 const char *val = astman_get_header(m, "Val");
989 int res;
990
991 if (ast_strlen_zero(family)) {
992 astman_send_error(s, m, "No family specified");
993 return 0;
994 }
995 if (ast_strlen_zero(key)) {
996 astman_send_error(s, m, "No key specified");
997 return 0;
998 }
999
1000 res = ast_db_put(family, key, S_OR(val, ""));
1001 if (res) {
1002 astman_send_error(s, m, "Failed to update entry");
1003 } else {
1004 astman_send_ack(s, m, "Updated database successfully");
1005 }
1006 return 0;
1007}
Definition: ast_expr2.c:325

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

Referenced by astdb_init().

Variable Documentation

◆ astdb

sqlite3* astdb
static

◆ cli_database

struct ast_cli_entry cli_database[]
static

Definition at line 974 of file db.c.

Referenced by astdb_atexit(), and astdb_init().

◆ dbcond

ast_cond_t dbcond
static

Definition at line 137 of file db.c.

Referenced by astdb_init(), db_sync(), and db_sync_thread().

◆ dblock

ast_mutex_t dblock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
static

◆ doexit

int doexit
static

Definition at line 140 of file db.c.

Referenced by astdb_atexit(), and db_sync_thread().

◆ dosync

int dosync
static

Definition at line 141 of file db.c.

Referenced by db_sync(), and db_sync_thread().

◆ syncthread

pthread_t syncthread
static

Definition at line 139 of file db.c.

Referenced by astdb_atexit(), and astdb_init().