128#define DEFINE_SQL_STATEMENT(stmt,sql) static sqlite3_stmt *stmt; \
129 const char stmt##_sql[] = sql;
156static
int init_stmt(sqlite3_stmt **stmt, const
char *sql,
size_t len)
159 if (sqlite3_prepare(
astdb, sql,
len, stmt,
NULL) != SQLITE_OK) {
175 if (sqlite3_finalize(*stmt) != SQLITE_OK) {
194 clean_stmt(&deltree_all_stmt, deltree_all_stmt_sql);
196 clean_stmt(&gettree_all_stmt, gettree_all_stmt_sql);
197 clean_stmt(&gettree_prefix_stmt, gettree_prefix_stmt_sql);
200 clean_stmt(&create_astdb_stmt, create_astdb_stmt_sql);
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));
237 if (!create_astdb_stmt) {
238 init_stmt(&create_astdb_stmt, create_astdb_stmt_sql,
sizeof(create_astdb_stmt_sql));
242 if (sqlite3_step(create_astdb_stmt) != SQLITE_DONE) {
246 sqlite3_reset(create_astdb_stmt);
256 struct stat dont_care;
262 strcat(
dbname,
".sqlite3");
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");
286 sqlite3_close(
astdb);
312static int db_execute_sql(
const char *sql,
int (*callback)(
void *,
int,
char **,
char **),
void *arg)
317 if (sqlite3_exec(
astdb, sql, callback, arg, &errmsg) != SQLITE_OK) {
319 sqlite3_free(errmsg);
347 fullkey_len =
ast_asprintf(&fullkey,
"/%s/%s", family, key);
348 if (fullkey_len < 0) {
355 if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
358 }
else if (sqlite3_bind_text(put_stmt, 2,
value, -1, SQLITE_STATIC) != SQLITE_OK) {
361 }
else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
365 sqlite3_reset(put_stmt);
387static int db_get_common(
const char *family,
const char *key,
char **buffer,
int bufferlen)
389 const unsigned char *
result;
394 fullkey_len =
ast_asprintf(&fullkey,
"/%s/%s", family, key);
395 if (fullkey_len < 0) {
402 if (sqlite3_bind_text(get_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
405 }
else if (sqlite3_step(get_stmt) != SQLITE_ROW) {
406 ast_debug(1,
"Unable to find key '%s' in family '%s'\n", key, family);
408 }
else if (!(
result = sqlite3_column_text(get_stmt, 0))) {
414 if (bufferlen == -1) {
420 sqlite3_reset(get_stmt);
451 fullkey_len =
ast_asprintf(&fullkey,
"/%s/%s", family, key);
452 if (fullkey_len < 0) {
459 res = sqlite3_bind_text(exists_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC);
460 if (res != SQLITE_OK) {
463 }
else if (sqlite3_step(exists_stmt) != SQLITE_ROW) {
465 }
else if (!(
result = sqlite3_column_int(exists_stmt, 0))) {
470 sqlite3_reset(exists_stmt);
484 fullkey_len =
ast_asprintf(&fullkey,
"/%s/%s", family, key);
485 if (fullkey_len < 0) {
492 if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
495 }
else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
496 ast_debug(1,
"Unable to find key '%s' in family '%s'\n", key, family);
499 sqlite3_reset(del_stmt);
514 fullkey_len =
ast_asprintf(&fullkey,
"/%s/%s", family, key);
515 if (fullkey_len < 0) {
525 }
else if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
528 }
else if ((mres = sqlite3_step(del_stmt) != SQLITE_DONE)) {
532 sqlite3_reset(del_stmt);
554 if (*prefix_len < 0) {
556 S_OR(family,
""),
S_COR(keytree,
"/",
""),
S_OR(keytree,
""));
567 sqlite3_stmt *stmt = deltree_stmt;
581 if (prefix_len == 0) {
582 stmt = deltree_all_stmt;
586 if (prefix_len && (sqlite3_bind_text(stmt, 1,
prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
589 }
else if (sqlite3_step(stmt) != SQLITE_DONE) {
593 res = sqlite3_changes(
astdb);
606 while (sqlite3_step(stmt) == SQLITE_ROW) {
608 size_t key_len, value_len;
610 key = (
const char *) sqlite3_column_text(stmt, 0);
611 value = (
const char *) sqlite3_column_text(stmt, 1);
617 key_len = strlen(
key);
618 value_len = strlen(
value);
620 cur =
ast_malloc(
sizeof(*cur) + key_len + value_len + 2);
626 cur->key = cur->data + value_len + 1;
627 memcpy(cur->data,
value, value_len + 1);
628 memcpy(cur->key,
key, key_len + 1);
645 sqlite3_stmt *stmt = gettree_stmt;
652 if (prefix_len == 0) {
653 stmt = gettree_all_stmt;
657 if (prefix_len && (sqlite3_bind_text(stmt, 1,
prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
685 if (sqlite3_bind_text(gettree_prefix_stmt, 1,
prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK) {
687 sqlite3_reset(gettree_prefix_stmt);
694 sqlite3_reset(gettree_prefix_stmt);
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";
731 ast_cli(
a->fd,
"Failed to update entry\n");
733 ast_cli(
a->fd,
"Updated database successfully\n");
747 "Usage: database get <family> <key>\n"
748 " Retrieves an entry in the Asterisk database for a given\n"
749 " family and key.\n";
759 ast_cli(
a->fd,
"Database entry not found.\n");
776 "Usage: database del <family> <key>\n"
777 " Deletes an entry in the Asterisk database for a given\n"
778 " family and key.\n";
788 ast_cli(
a->fd,
"Database entry could not be removed.\n");
790 ast_cli(
a->fd,
"Database entry removed.\n");
801 e->
command =
"database deltree";
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";
813 if ((
a->argc < 3) || (
a->argc > 4))
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");
825 ast_cli(
a->fd,
"%d database entries removed.\n",num_deleted);
834 const char *family =
a->argc > 2 ?
a->argv[2] :
"";
835 const char *keytree =
a->argc > 3 ?
a->argv[3] :
"";
837 sqlite3_stmt *stmt = gettree_stmt;
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";
861 if (prefix_len == 0) {
862 stmt = gettree_all_stmt;
866 if (prefix_len && (sqlite3_bind_text(stmt, 1,
prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
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))) {
880 if (!(value_s = (
const char *) sqlite3_column_text(stmt, 1))) {
885 ast_cli(
a->fd,
"%-50s: %-25s\n", key_s, value_s);
892 ast_cli(
a->fd,
"%d results found.\n", counter);
902 e->
command =
"database showkey";
904 "Usage: database showkey <keytree>\n"
905 " Shows Asterisk database contents, restricted to a given key.\n";
916 if (!
ast_strlen_zero(
a->argv[2]) && (sqlite3_bind_text(showkey_stmt, 1,
a->argv[2], -1, SQLITE_STATIC) != SQLITE_OK)) {
918 sqlite3_reset(showkey_stmt);
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))) {
928 if (!(value_s = (
const char *) sqlite3_column_text(showkey_stmt, 1))) {
932 ast_cli(
a->fd,
"%-50s: %-25s\n", key_s, value_s);
934 sqlite3_reset(showkey_stmt);
937 ast_cli(
a->fd,
"%d results found.\n", counter);
946 for (x = 0; x <
columns; x++) {
961 "Usage: database query \"<SQL Statement>\"\n"
962 " Run a user-specified SQL query on the database. Be careful.\n";
1035 snprintf(idText,
sizeof(idText) ,
"ActionID: %s\r\n",
id);
1049 family, key,
tmp, idText);
1065 sqlite3_stmt *stmt = gettree_stmt;
1073 if (prefix_len == 0) {
1074 stmt = gettree_all_stmt;
1079 snprintf(idText,
sizeof(idText) ,
"ActionID: %s\r\n",
id);
1085 sqlite3_reset(stmt);
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))) {
1100 if (!(value_s = (
const char *) sqlite3_column_text(stmt, 1))) {
1109 key_s, value_s, idText);
1113 sqlite3_reset(stmt);
1165 if (num_deleted < 0) {
1167 }
else if (num_deleted == 0) {
1250 if (sqlite3_close(
astdb) == SQLITE_OK) {
Prototypes for public functions only of internal interest,.
struct sla_ringing_trunk * last
#define EXISTS(a, b, c, d)
#define DELETE(a, b, c, d)
static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len)
Persistent data storage (akin to *doze registry)
Asterisk main include file. File version handling, generic pbx functions.
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
#define ast_strdup(str)
A wrapper for strdup()
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
#define ast_malloc(len)
A wrapper for malloc()
General Asterisk PBX channel definitions.
Standard Command Line Interface.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
#define AST_CLI_DEFINE(fn, txt,...)
void ast_cli(int fd, const char *fmt,...)
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Convenient Signal Processing routines.
Generic File Format Support. Should be included by clients of the file handling routines....
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag)
Send ack in manager transaction to begin a list.
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
Start the list complete event.
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
const char * astman_get_header(const struct message *m, char *var)
Get header from manager transaction.
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
void astman_append(struct mansession *s, const char *fmt,...)
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
static char prefix[MAX_PREFIX]
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_cond_wait(cond, mutex)
#define ast_cond_init(cond, attr)
#define ast_mutex_unlock(a)
pthread_cond_t ast_cond_t
#define ast_mutex_lock(a)
#define AST_MUTEX_DEFINE_STATIC(mutex)
#define ast_cond_signal(cond)
static char * handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int manager_dbdel(struct mansession *s, const struct message *m)
static void db_sync(void)
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
static void clean_statements(void)
static char * handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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.
static int manager_db_tree_get(struct mansession *s, const struct message *m)
int ast_db_del2(const char *family, const char *key)
Same as ast_db_del, but with more stringent error checking.
static int manager_dbdeltree(struct mansession *s, const struct message *m)
static struct ast_cli_entry cli_database[]
void ast_db_freetree(struct ast_db_entry *dbe)
Free structure created by ast_db_gettree()
static struct ast_db_entry * db_gettree_common(sqlite3_stmt *stmt)
static int manager_dbget(struct mansession *s, const struct message *m)
static char * handle_cli_database_query(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int convert_bdb_to_sqlite3(void)
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.
static int manager_dbput(struct mansession *s, const struct message *m)
static int db_execute_sql(const char *sql, int(*callback)(void *, int, char **, char **), void *arg)
static int display_results(void *arg, int columns, char **values, char **colnames)
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
static int ast_db_begin_transaction(void)
static int db_create_astdb(void)
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
static int ast_db_commit_transaction(void)
static ast_mutex_t dblock
#define DEFINE_SQL_STATEMENT(stmt, sql)
static void * db_sync_thread(void *data)
static char * handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
static char * create_prefix(const char *family, const char *keytree, int *prefix_len)
static pthread_t syncthread
static char * handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static void astdb_atexit(void)
static int init_statements(void)
static char * handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int db_get_common(const char *family, const char *key, char **buffer, int bufferlen)
static int clean_stmt(sqlite3_stmt **stmt, const char *sql)
static int ast_db_rollback_transaction(void)
int ast_db_deltree(const char *family, const char *keytree)
Delete one or more entries in astdb.
static char * handle_cli_database_put(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
int ast_db_exists(const char *family, const char *key)
Check if family/key exitsts.
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define EVENT_FLAG_REPORTING
#define ast_manager_register_xml_core(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
#define EVENT_FLAG_SYSTEM
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_SBIN_DIR
const char * ast_config_AST_DB
static char dbname[MAX_DB_OPTION_SIZE]
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
static force_inline int attribute_pure ast_strlen_zero(const char *s)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
descriptor for a cli entry.
struct ast_db_entry * next
In case you didn't read that giant block of text above the mansession_session struct,...
#define ast_pthread_create_background(a, b, c, d)