Asterisk - The Open Source Telephony Project GIT-master-f36a736
Macros | Functions | Variables
main/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 main/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 int convert_bdb_to_sqlite3 (void)
 
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 main/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 128 of file main/db.c.

Function Documentation

◆ ast_db_begin_transaction()

static int ast_db_begin_transaction ( void  )
static

Definition at line 326 of file main/db.c.

327{
328 return db_execute_sql("BEGIN TRANSACTION", NULL, NULL);
329}
static int db_execute_sql(const char *sql, int(*callback)(void *, int, char **, char **), void *arg)
Definition: main/db.c:312
#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 331 of file main/db.c.

332{
333 return db_execute_sql("COMMIT", NULL, NULL);
334}

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 478 of file main/db.c.

479{
480 char *fullkey;
481 int fullkey_len;
482 int res = 0;
483
484 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
485 if (fullkey_len < 0) {
486 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
487 family, key);
488 return -1;
489 }
490
492 if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
493 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
494 res = -1;
495 } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
496 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
497 res = -1;
498 }
499 sqlite3_reset(del_stmt);
500 db_sync();
502 ast_free(fullkey);
503
504 return res;
505}
#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
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
#define ast_mutex_unlock(a)
Definition: lock.h:190
#define ast_mutex_lock(a)
Definition: lock.h:189
static void db_sync(void)
Definition: main/db.c:1182
static sqlite3 * astdb
Definition: main/db.c:121
static ast_mutex_t dblock
Definition: main/db.c:119

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 507 of file main/db.c.

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

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

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 565 of file main/db.c.

566{
567 sqlite3_stmt *stmt = deltree_stmt;
568 char *prefix = NULL;
569 int prefix_len = 0;
570 int res = 0;
571
572 if (ast_strlen_zero(family) && !ast_strlen_zero(keytree)) {
573 ast_log(LOG_WARNING, "Key tree '%s' specified without family\n", keytree);
574 return -1;
575 }
576
577 prefix = create_prefix(family, keytree, &prefix_len);
578 if (!prefix) {
579 return -1;
580 }
581 if (prefix_len == 0) {
582 stmt = deltree_all_stmt;
583 }
584
586 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
587 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
588 res = -1;
589 } else if (sqlite3_step(stmt) != SQLITE_DONE) {
590 ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
591 res = -1;
592 }
593 res = sqlite3_changes(astdb);
594 sqlite3_reset(stmt);
595 db_sync();
598
599 return res;
600}
static char prefix[MAX_PREFIX]
Definition: http.c:144
static char * create_prefix(const char *family, const char *keytree, int *prefix_len)
Definition: main/db.c:540
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 444 of file main/db.c.

445{
446 int result;
447 char *fullkey;
448 int fullkey_len;
449 int res = 0;
450
451 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
452 if (fullkey_len < 0) {
453 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
454 family, key);
455 return -1;
456 }
457
459 res = sqlite3_bind_text(exists_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC);
460 if (res != SQLITE_OK) {
461 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %d:%s\n", res, sqlite3_errmsg(astdb));
462 res = 0;
463 } else if (sqlite3_step(exists_stmt) != SQLITE_ROW) {
464 res = 0;
465 } else if (!(result = sqlite3_column_int(exists_stmt, 0))) {
466 res = 0;
467 } else {
468 res = result;
469 }
470 sqlite3_reset(exists_stmt);
472 ast_free(fullkey);
473
474 return res;
475}
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 427 of file main/db.c.

428{
430
431 /* Make sure we initialize */
432 value[0] = 0;
433
434 return db_get_common(family, key, &value, valuelen);
435}
static int db_get_common(const char *family, const char *key, char **buffer, int bufferlen)
Definition: main/db.c:387
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 437 of file main/db.c.

438{
439 *out = NULL;
440
441 return db_get_common(family, key, out, -1);
442}
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 641 of file main/db.c.

642{
643 char *prefix = NULL;
644 int prefix_len = 0;
645 sqlite3_stmt *stmt = gettree_stmt;
646 struct ast_db_entry *ret;
647
648 prefix = create_prefix(family, keytree, &prefix_len);
649 if (!prefix) {
650 return NULL;
651 }
652 if (prefix_len == 0) {
653 stmt = gettree_all_stmt;
654 }
655
657 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
658 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
659 sqlite3_reset(stmt);
662 return NULL;
663 }
664
665 ret = db_gettree_common(stmt);
666 sqlite3_reset(stmt);
669
670 return ret;
671}
static struct ast_db_entry * db_gettree_common(sqlite3_stmt *stmt)
Definition: main/db.c:602

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 673 of file main/db.c.

674{
675 char *prefix = NULL;
676 int prefix_len = 0;
677 struct ast_db_entry *ret;
678
679 prefix = create_prefix(family, key_prefix, &prefix_len);
680 if (!prefix) {
681 return NULL;
682 }
683
685 if (sqlite3_bind_text(gettree_prefix_stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK) {
686 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
687 sqlite3_reset(gettree_prefix_stmt);
690 return NULL;
691 }
692
693 ret = db_gettree_common(gettree_prefix_stmt);
694 sqlite3_reset(gettree_prefix_stmt);
697
698 return ret;
699}

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 341 of file main/db.c.

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

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 336 of file main/db.c.

337{
338 return db_execute_sql("ROLLBACK", NULL, NULL);
339}

References db_execute_sql(), and NULL.

Referenced by db_sync_thread().

◆ astdb_atexit()

static void astdb_atexit ( void  )
static

Definition at line 1231 of file main/db.c.

1232{
1234 ast_manager_unregister("DBGet");
1235 ast_manager_unregister("DBGetTree");
1236 ast_manager_unregister("DBPut");
1237 ast_manager_unregister("DBDel");
1238 ast_manager_unregister("DBDelTree");
1239
1240 /* Set doexit to 1 to kill thread. db_sync must be called with
1241 * mutex held. */
1243 doexit = 1;
1244 db_sync();
1246
1247 pthread_join(syncthread, NULL);
1250 if (sqlite3_close(astdb) == SQLITE_OK) {
1251 astdb = NULL;
1252 }
1254}
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:7608
static int doexit
Definition: main/db.c:123
static void clean_statements(void)
Definition: main/db.c:188
static struct ast_cli_entry cli_database[]
Definition: main/db.c:980
static pthread_t syncthread
Definition: main/db.c:122
#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 1256 of file main/db.c.

1257{
1259
1260 if (db_init()) {
1261 return -1;
1262 }
1263
1265 return -1;
1266 }
1267
1275 return 0;
1276}
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
#define ast_cond_init(cond, attr)
Definition: lock.h:201
static int manager_dbdel(struct mansession *s, const struct message *m)
Definition: main/db.c:1123
static int manager_db_tree_get(struct mansession *s, const struct message *m)
Definition: main/db.c:1057
static int manager_dbdeltree(struct mansession *s, const struct message *m)
Definition: main/db.c:1148
static int manager_dbget(struct mansession *s, const struct message *m)
Definition: main/db.c:1015
static int db_init(void)
Definition: main/db.c:296
static int manager_dbput(struct mansession *s, const struct message *m)
Definition: main/db.c:990
static void * db_sync_thread(void *data)
Definition: main/db.c:1198
static ast_cond_t dbcond
Definition: main/db.c:120
static void astdb_atexit(void)
Definition: main/db.c:1231
#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:202
#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 188 of file main/db.c.

189{
190 clean_stmt(&get_stmt, get_stmt_sql);
191 clean_stmt(&exists_stmt, exists_stmt_sql);
192 clean_stmt(&del_stmt, del_stmt_sql);
193 clean_stmt(&deltree_stmt, deltree_stmt_sql);
194 clean_stmt(&deltree_all_stmt, deltree_all_stmt_sql);
195 clean_stmt(&gettree_stmt, gettree_stmt_sql);
196 clean_stmt(&gettree_all_stmt, gettree_all_stmt_sql);
197 clean_stmt(&gettree_prefix_stmt, gettree_prefix_stmt_sql);
198 clean_stmt(&showkey_stmt, showkey_stmt_sql);
199 clean_stmt(&put_stmt, put_stmt_sql);
200 clean_stmt(&create_astdb_stmt, create_astdb_stmt_sql);
201}
static int clean_stmt(sqlite3_stmt **stmt, const char *sql)
Definition: main/db.c:173

References clean_stmt().

Referenced by astdb_atexit().

◆ clean_stmt()

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

Definition at line 173 of file main/db.c.

174{
175 if (sqlite3_finalize(*stmt) != SQLITE_OK) {
176 ast_log(LOG_WARNING, "Couldn't finalize statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
177 *stmt = NULL;
178 return -1;
179 }
180 *stmt = NULL;
181 return 0;
182}

References ast_log, astdb, LOG_WARNING, and NULL.

Referenced by clean_statements().

◆ convert_bdb_to_sqlite3()

static int convert_bdb_to_sqlite3 ( void  )
static

Definition at line 219 of file main/db.c.

220{
221 char *cmd;
222 int res;
223
224 res = ast_asprintf(&cmd, "%s/astdb2sqlite3 '%s'\n", ast_config_AST_SBIN_DIR, ast_config_AST_DB);
225 if (0 <= res) {
226 res = ast_safe_system(cmd);
227 ast_free(cmd);
228 }
229
230 return res;
231}
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
Definition: extconf.c:829
const char * ast_config_AST_SBIN_DIR
Definition: options.c:163
const char * ast_config_AST_DB
Definition: options.c:165

References ast_asprintf, ast_config_AST_DB, ast_config_AST_SBIN_DIR, ast_free, and ast_safe_system().

Referenced by db_open().

◆ create_prefix()

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

Definition at line 540 of file main/db.c.

542{
543 char *prefix = NULL;
544 *prefix_len = 0;
545
546 if (!ast_strlen_zero(family)) {
547 if (!ast_strlen_zero(keytree)) {
548 /* Family and key tree */
549 *prefix_len = ast_asprintf(&prefix, "/%s/%s", family, keytree);
550 } else {
551 /* Family only */
552 *prefix_len = ast_asprintf(&prefix, "/%s", family);
553 }
554 if (*prefix_len < 0) {
555 ast_log(LOG_WARNING, "Unable to allocate memory for family/keytree '/%s%s%s'\n",
556 S_OR(family, ""), S_COR(keytree, "/", ""), S_OR(keytree, ""));
557 return NULL;
558 }
559 } else {
560 prefix = ast_strdup("");
561 }
562 return prefix;
563}
#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 233 of file main/db.c.

234{
235 int res = 0;
236
237 if (!create_astdb_stmt) {
238 init_stmt(&create_astdb_stmt, create_astdb_stmt_sql, sizeof(create_astdb_stmt_sql));
239 }
240
242 if (sqlite3_step(create_astdb_stmt) != SQLITE_DONE) {
243 ast_log(LOG_WARNING, "Couldn't create astdb table: %s\n", sqlite3_errmsg(astdb));
244 res = -1;
245 }
246 sqlite3_reset(create_astdb_stmt);
247 db_sync();
249
250 return res;
251}
static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len)

References ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, init_stmt(), 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 312 of file main/db.c.

313{
314 char *errmsg = NULL;
315 int res =0;
316
317 if (sqlite3_exec(astdb, sql, callback, arg, &errmsg) != SQLITE_OK) {
318 ast_log(LOG_WARNING, "Error executing SQL (%s): %s\n", sql, errmsg);
319 sqlite3_free(errmsg);
320 res = -1;
321 }
322
323 return res;
324}

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 387 of file main/db.c.

388{
389 const unsigned char *result;
390 char *fullkey;
391 int fullkey_len;
392 int res = 0;
393
394 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
395 if (fullkey_len < 0) {
396 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
397 family, key);
398 return -1;
399 }
400
402 if (sqlite3_bind_text(get_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
403 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
404 res = -1;
405 } else if (sqlite3_step(get_stmt) != SQLITE_ROW) {
406 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
407 res = -1;
408 } else if (!(result = sqlite3_column_text(get_stmt, 0))) {
409 ast_log(LOG_WARNING, "Couldn't get value\n");
410 res = -1;
411 } else {
412 const char *value = (const char *) result;
413
414 if (bufferlen == -1) {
415 *buffer = ast_strdup(value);
416 } else {
417 ast_copy_string(*buffer, value, bufferlen);
418 }
419 }
420 sqlite3_reset(get_stmt);
422 ast_free(fullkey);
423
424 return res;
425}
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 602 of file main/db.c.

603{
604 struct ast_db_entry *head = NULL, *prev = NULL, *cur;
605
606 while (sqlite3_step(stmt) == SQLITE_ROW) {
607 const char *key, *value;
608 size_t key_len, value_len;
609
610 key = (const char *) sqlite3_column_text(stmt, 0);
611 value = (const char *) sqlite3_column_text(stmt, 1);
612
613 if (!key || !value) {
614 break;
615 }
616
617 key_len = strlen(key);
618 value_len = strlen(value);
619
620 cur = ast_malloc(sizeof(*cur) + key_len + value_len + 2);
621 if (!cur) {
622 break;
623 }
624
625 cur->next = NULL;
626 cur->key = cur->data + value_len + 1;
627 memcpy(cur->data, value, value_len + 1);
628 memcpy(cur->key, key, key_len + 1);
629
630 if (prev) {
631 prev->next = cur;
632 } else {
633 head = cur;
634 }
635 prev = cur;
636 }
637
638 return head;
639}
#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 296 of file main/db.c.

297{
298 if (astdb) {
299 return 0;
300 }
301
302 if (db_open() || db_create_astdb() || init_statements()) {
303 return -1;
304 }
305
306 return 0;
307}
static int db_create_astdb(void)
Definition: main/db.c:233
static int db_open(void)
Definition: main/db.c:253
static int init_statements(void)
Definition: main/db.c:203

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 253 of file main/db.c.

254{
255 char *dbname;
256 struct stat dont_care;
257
258 if (!(dbname = ast_alloca(strlen(ast_config_AST_DB) + sizeof(".sqlite3")))) {
259 return -1;
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)) {
266 ast_log(LOG_ERROR, "*** Database conversion failed!\n");
267 ast_log(LOG_ERROR, "*** Asterisk now uses SQLite3 for its internal\n");
268 ast_log(LOG_ERROR, "*** database. Conversion from the old astdb\n");
269 ast_log(LOG_ERROR, "*** failed. Most likely the astdb2sqlite3 utility\n");
270 ast_log(LOG_ERROR, "*** was not selected for build. To convert the\n");
271 ast_log(LOG_ERROR, "*** old astdb, please delete '%s'\n", dbname);
272 ast_log(LOG_ERROR, "*** and re-run 'make menuselect' and select astdb2sqlite3\n");
273 ast_log(LOG_ERROR, "*** in the Utilities section, then 'make && make install'.\n");
274 ast_log(LOG_ERROR, "*** It is also imperative that the user under which\n");
275 ast_log(LOG_ERROR, "*** Asterisk runs have write permission to the directory\n");
276 ast_log(LOG_ERROR, "*** where the database resides.\n");
277 sleep(5);
278 } else {
279 ast_log(LOG_NOTICE, "Database conversion succeeded!\n");
280 }
281 }
282
284 if (sqlite3_open(dbname, &astdb) != SQLITE_OK) {
285 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(astdb));
286 sqlite3_close(astdb);
288 return -1;
289 }
290
292
293 return 0;
294}
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define LOG_ERROR
#define LOG_NOTICE
static int convert_bdb_to_sqlite3(void)
Definition: main/db.c:219
static char dbname[MAX_DB_OPTION_SIZE]

References ast_alloca, ast_config_AST_DB, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, convert_bdb_to_sqlite3(), dblock, dbname, LOG_ERROR, LOG_NOTICE, and LOG_WARNING.

Referenced by db_init().

◆ db_sync()

static void db_sync ( void  )
static

Definition at line 1182 of file main/db.c.

1183{
1184 dosync = 1;
1186}
#define ast_cond_signal(cond)
Definition: lock.h:203
static int dosync
Definition: main/db.c:124

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 1198 of file main/db.c.

1199{
1202 for (;;) {
1203 /* If dosync is set, db_sync() was called during sleep(1),
1204 * and the pending transaction should be committed.
1205 * Otherwise, block until db_sync() is called.
1206 */
1207 while (!dosync) {
1209 }
1210 dosync = 0;
1213 }
1214 if (doexit) {
1216 break;
1217 }
1220 sleep(1);
1222 }
1223
1224 return NULL;
1225}
#define ast_cond_wait(cond, mutex)
Definition: lock.h:205
static int ast_db_begin_transaction(void)
Definition: main/db.c:326
static int ast_db_commit_transaction(void)
Definition: main/db.c:331
static int ast_db_rollback_transaction(void)
Definition: main/db.c:336

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 131 of file main/db.c.

157{
159 if (sqlite3_prepare(astdb, sql, len, stmt, NULL) != SQLITE_OK) {
160 ast_log(LOG_WARNING, "Couldn't prepare statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
162 return -1;
163 }
165
166 return 0;
167}
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 941 of file main/db.c.

942{
943 struct ast_cli_args *a = arg;
944 size_t x;
945
946 for (x = 0; x < columns; x++) {
947 ast_cli(a->fd, "%-5s: %-50s\n", colnames[x], values[x]);
948 }
949 ast_cli(a->fd, "\n");
950
951 return 0;
952}
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 768 of file main/db.c.

769{
770 int res;
771
772 switch (cmd) {
773 case CLI_INIT:
774 e->command = "database del";
775 e->usage =
776 "Usage: database del <family> <key>\n"
777 " Deletes an entry in the Asterisk database for a given\n"
778 " family and key.\n";
779 return NULL;
780 case CLI_GENERATE:
781 return NULL;
782 }
783
784 if (a->argc != 4)
785 return CLI_SHOWUSAGE;
786 res = ast_db_del2(a->argv[2], a->argv[3]);
787 if (res) {
788 ast_cli(a->fd, "Database entry could not be removed.\n");
789 } else {
790 ast_cli(a->fd, "Database entry removed.\n");
791 }
792 return CLI_SUCCESS;
793}
#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: main/db.c:507
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 795 of file main/db.c.

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

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 738 of file main/db.c.

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

References a, ast_cli(), ast_db_get_allocated(), ast_free, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, tmp(), 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 711 of file main/db.c.

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

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 954 of file main/db.c.

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

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 830 of file main/db.c.

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

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 896 of file main/db.c.

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

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 203 of file main/db.c.

204{
205 /* Don't initialize create_astdb_statement here as the astdb table needs to exist
206 * brefore these statements can be initialized */
207 return init_stmt(&get_stmt, get_stmt_sql, sizeof(get_stmt_sql))
208 || init_stmt(&exists_stmt, exists_stmt_sql, sizeof(exists_stmt_sql))
209 || init_stmt(&del_stmt, del_stmt_sql, sizeof(del_stmt_sql))
210 || init_stmt(&deltree_stmt, deltree_stmt_sql, sizeof(deltree_stmt_sql))
211 || init_stmt(&deltree_all_stmt, deltree_all_stmt_sql, sizeof(deltree_all_stmt_sql))
212 || init_stmt(&gettree_stmt, gettree_stmt_sql, sizeof(gettree_stmt_sql))
213 || init_stmt(&gettree_all_stmt, gettree_all_stmt_sql, sizeof(gettree_all_stmt_sql))
214 || init_stmt(&gettree_prefix_stmt, gettree_prefix_stmt_sql, sizeof(gettree_prefix_stmt_sql))
215 || init_stmt(&showkey_stmt, showkey_stmt_sql, sizeof(showkey_stmt_sql))
216 || init_stmt(&put_stmt, put_stmt_sql, sizeof(put_stmt_sql));
217}

References init_stmt().

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 1057 of file main/db.c.

1058{
1059 char *prefix;
1060 int prefix_len = 0;
1061 char idText[256];
1062 const char *id = astman_get_header(m,"ActionID");
1063 const char *family = astman_get_header(m, "Family");
1064 const char *key = astman_get_header(m, "Key");
1065 sqlite3_stmt *stmt = gettree_stmt;
1066 int count = 0;
1067
1068 prefix = create_prefix(family, key, &prefix_len);
1069 if (!prefix) {
1070 astman_send_error(s, m, "Unable to allocate memory for Family/Key");
1071 return 0;
1072 }
1073 if (prefix_len == 0) {
1074 stmt = gettree_all_stmt;
1075 }
1076
1077 idText[0] = '\0';
1078 if (!ast_strlen_zero(id)) {
1079 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
1080 }
1081
1083 if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
1084 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
1085 sqlite3_reset(stmt);
1088 astman_send_error(s, m, "Unable to search database");
1089 return 0;
1090 }
1091
1092 astman_send_listack(s, m, "Result will follow", "start");
1093
1094 while (sqlite3_step(stmt) == SQLITE_ROW) {
1095 const char *key_s, *value_s;
1096 if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
1097 ast_log(LOG_WARNING, "Skipping invalid key!\n");
1098 continue;
1099 }
1100 if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
1101 ast_log(LOG_WARNING, "Skipping invalid value!\n");
1102 continue;
1103 }
1104 astman_append(s, "Event: DBGetTreeResponse\r\n"
1105 "Key: %s\r\n"
1106 "Val: %s\r\n"
1107 "%s"
1108 "\r\n",
1109 key_s, value_s, idText);
1110 count++;
1111 }
1112
1113 sqlite3_reset(stmt);
1116
1117 astman_send_list_complete_start(s, m, "DBGetTreeComplete", count);
1119
1120 return 0;
1121}
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:2011
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:1969
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:2047
const char * astman_get_header(const struct message *m, char *var)
Get header from manager transaction.
Definition: manager.c:1630
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
Definition: manager.c:2055
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:1890

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 1123 of file main/db.c.

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

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 1148 of file main/db.c.

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

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 1015 of file main/db.c.

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

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(), NULL, and tmp().

Referenced by astdb_init().

◆ manager_dbput()

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

Definition at line 990 of file main/db.c.

991{
992 const char *family = astman_get_header(m, "Family");
993 const char *key = astman_get_header(m, "Key");
994 const char *val = astman_get_header(m, "Val");
995 int res;
996
997 if (ast_strlen_zero(family)) {
998 astman_send_error(s, m, "No family specified");
999 return 0;
1000 }
1001 if (ast_strlen_zero(key)) {
1002 astman_send_error(s, m, "No key specified");
1003 return 0;
1004 }
1005
1006 res = ast_db_put(family, key, S_OR(val, ""));
1007 if (res) {
1008 astman_send_error(s, m, "Failed to update entry");
1009 } else {
1010 astman_send_ack(s, m, "Updated database successfully");
1011 }
1012 return 0;
1013}
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 980 of file main/db.c.

Referenced by astdb_atexit(), and astdb_init().

◆ dbcond

ast_cond_t dbcond
static

Definition at line 120 of file main/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 123 of file main/db.c.

Referenced by astdb_atexit(), and db_sync_thread().

◆ dosync

int dosync
static

Definition at line 124 of file main/db.c.

Referenced by db_sync(), and db_sync_thread().

◆ syncthread

pthread_t syncthread
static

Definition at line 122 of file main/db.c.

Referenced by astdb_atexit(), and astdb_init().