38 #include "asterisk/_private.h"
53 #include "db1-ast/include/db.h"
107 #define MAX_DB_FIELD 256
132 int preflen = strlen(prefix);
135 if (!strcasecmp(key, prefix))
137 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
138 if (key[preflen] ==
'/')
144 static inline int subkeymatch(
const char *key,
const char *suffix)
146 int suffixlen = strlen(suffix);
148 const char *subkey = key + strlen(key) - suffixlen;
151 if (!strcasecmp(subkey, suffix))
163 data[dbt->size - 1] =
'\0';
186 DBT key = { 0, }, value = { 0, }, last_key = { 0, };
202 if (astdb->seq(astdb, &last_key, &value, R_LAST)) {
208 memcpy(last_key_s, last_key.data,
MIN(last_key.size - 1,
sizeof(last_key_s)));
209 last_key_s[last_key.size - 1] =
'\0';
210 for (res = astdb->seq(astdb, &key, &value, R_FIRST);
212 res = astdb->seq(astdb, &key, &value, R_NEXT)) {
215 counter += cb(&key, &value, filter, data);
230 static int db_deltree_cb(DBT *key, DBT *value,
const char *filter,
void *data)
235 astdb->del(astdb, key, 0);
247 snprintf(prefix,
sizeof(prefix),
"/%s/%s", family, keytree);
249 snprintf(prefix,
sizeof(prefix),
"/%s", family);
251 }
else if (keytree) {
272 fullkeylen = snprintf(fullkey,
sizeof(fullkey),
"/%s/%s", family, keys);
273 memset(&key, 0,
sizeof(key));
274 memset(&data, 0,
sizeof(data));
276 key.size = fullkeylen + 1;
277 data.data = (
char *) value;
278 data.size = strlen(value) + 1;
279 res = astdb->put(astdb, &key, &data, 0);
283 ast_log(
LOG_WARNING,
"Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
314 fullkeylen = snprintf(fullkey,
sizeof(fullkey),
"/%s/%s", family, keys);
315 memset(&key, 0,
sizeof(key));
316 memset(&data, 0,
sizeof(data));
318 key.size = fullkeylen + 1;
320 res = astdb->get(astdb, &key, &data, 0);
324 ast_debug(1,
"Unable to find key '%s' in family '%s'\n", keys, family);
327 ((
char *)data.data)[data.size - 1] =
'\0';
329 if (bufferlen == -1) {
334 ast_copy_string(*buffer, data.data, bufferlen > data.size ? data.size : bufferlen);
377 fullkeylen = snprintf(fullkey,
sizeof(fullkey),
"/%s/%s", family, keys);
378 memset(&key, 0,
sizeof(key));
380 key.size = fullkeylen + 1;
382 res = astdb->del(astdb, &key, 0);
388 ast_debug(1,
"Unable to find key '%s' in family '%s'\n", keys, family);
401 "Usage: database put <family> <key> <value>\n"
402 " Adds or updates an entry in the Asterisk database for\n"
403 " a given family, key, and value.\n";
413 ast_cli(a->
fd,
"Failed to update entry\n");
415 ast_cli(a->
fd,
"Updated database successfully\n");
429 "Usage: database get <family> <key>\n"
430 " Retrieves an entry in the Asterisk database for a given\n"
431 " family and key.\n";
441 ast_cli(a->
fd,
"Database entry not found.\n");
456 "Usage: database del <family> <key>\n"
457 " Deletes an entry in the Asterisk database for a given\n"
458 " family and key.\n";
468 ast_cli(a->
fd,
"Database entry does not exist.\n");
470 ast_cli(a->
fd,
"Database entry removed.\n");
481 e->
command =
"database deltree";
483 "Usage: database deltree <family> [keytree]\n"
484 " OR: database deltree <family>[/keytree]\n"
485 " Deletes a family or specific keytree within a family\n"
486 " in the Asterisk database. The two arguments may be\n"
487 " separated by either a space or a slash.\n";
500 if (num_deleted < 0) {
501 ast_cli(a->
fd,
"Database unavailable.\n");
502 }
else if (num_deleted == 0) {
503 ast_cli(a->
fd,
"Database entries do not exist.\n");
505 ast_cli(a->
fd,
"%d database entries removed.\n",num_deleted);
510 static int db_show_cb(DBT *key, DBT *value,
const char *filter,
void *data)
517 ast_cli(a->
fd,
"%-50s: %-25s\n", key_s, value_s);
533 "Usage: database show [family [keytree]]\n"
534 " OR: database show [family[/keytree]]\n"
535 " Shows Asterisk database contents, optionally restricted\n"
536 " to a given family, or family and keytree. The two arguments\n"
537 " may be separated either by a space or by a slash.\n";
545 snprintf(prefix,
sizeof(prefix),
"/%s/%s", a->
argv[2], a->
argv[3]);
546 }
else if (a->
argc == 3) {
548 snprintf(prefix,
sizeof(prefix),
"/%s", a->
argv[2]);
549 }
else if (a->
argc == 2) {
557 ast_cli(a->
fd,
"Database unavailable\n");
561 ast_cli(a->
fd,
"%d results found.\n", counter);
565 static int db_showkey_cb(DBT *key, DBT *value,
const char *filter,
void *data)
572 ast_cli(a->
fd,
"%-50s: %-25s\n", key_s, value_s);
586 e->
command =
"database showkey";
588 "Usage: database showkey <keytree>\n"
589 " Shows Asterisk database contents, restricted to a given key.\n";
597 snprintf(suffix,
sizeof(suffix),
"/%s", a->
argv[2]);
603 ast_cli(a->
fd,
"Database unavailable\n");
607 ast_cli(a->
fd,
"%d results found.\n", counter);
611 static int db_gettree_cb(DBT *key, DBT *value,
const char *filter,
void *data)
617 size_t key_slen = strlen(key_s) + 1, value_slen = strlen(value_s) + 1;
619 if (
keymatch(key_s, filter) && (cur =
ast_malloc(
sizeof(*cur) + key_slen + value_slen))) {
621 cur->
key = cur->
data + value_slen;
622 strcpy(cur->
data, value_s);
623 strcpy(cur->
key, key_s);
639 snprintf(prefix,
sizeof(prefix),
"/%s/%s", family, keytree);
642 snprintf(prefix,
sizeof(prefix),
"/%s", family);
703 char idText[256] =
"";
719 snprintf(idText,
sizeof(idText) ,
"ActionID: %s\r\n",
id);
721 res =
ast_db_get(family, key, tmp,
sizeof(tmp));
732 family, key, tmp, idText);
783 if (num_deleted < 0) {
785 }
else if (num_deleted == 0) {
826 astdb->sync(astdb, 0);
842 astdb->sync(astdb, 0);
867 pthread_join(syncthread, NULL);
869 #if defined(DEBUG_FD_LEAKS) && defined(close)
#define AST_CLI_DEFINE(fn, txt,...)
static struct ast_cli_entry cli_database[]
Asterisk locking-related definitions:
void astman_append(struct mansession *s, const char *fmt,...)
Asterisk main include file. File version handling, generic pbx functions.
const char * ast_config_AST_DB
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
static const char * dbt_data2str_full(DBT *dbt, const char *def)
Convenient Signal Processing routines.
descriptor for a cli entry.
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
int(* process_keys_cb)(DBT *key, DBT *value, const char *filter, void *data)
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.
#define ast_cond_wait(cond, mutex)
#define ast_cond_init(cond, attr)
static int subkeymatch(const char *key, const char *suffix)
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
#define ast_mutex_lock(a)
static ast_mutex_t dblock
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
void ast_cli(int fd, const char *fmt,...)
#define ast_cond_signal(cond)
static int manager_dbget(struct mansession *s, const struct message *m)
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
pthread_cond_t ast_cond_t
#define ast_pthread_create_background(a, b, c, d)
static int db_show_cb(DBT *key, DBT *value, const char *filter, void *data)
static pthread_t syncthread
#define EVENT_FLAG_SYSTEM
#define ast_debug(level,...)
Log a DEBUG message.
struct ast_db_entry * next
static int db_gettree_cb(DBT *key, DBT *value, const char *filter, void *data)
static int manager_dbdel(struct mansession *s, const struct message *m)
General Asterisk PBX channel definitions.
Asterisk file paths, configured in asterisk.conf.
#define ast_manager_register_xml(a, b, c)
Register a manager callback using XML documentation to describe the manager.
static int db_get_common(const char *family, const char *keys, char **buffer, int bufferlen)
static force_inline int attribute_pure ast_strlen_zero(const char *s)
struct sla_ringing_trunk * last
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
static char * handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int process_db_keys(process_keys_cb cb, void *data, const char *filter, int sync)
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
static void db_sync(void)
static char * handle_cli_database_put(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 If family is specified, only those keys will be returned...
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
static int db_showkey_cb(DBT *key, DBT *value, const char *filter, void *data)
static const char * dbt_data2str(DBT *dbt)
static void * db_sync_thread(void *data)
static int db_deltree_cb(DBT *key, DBT *value, const char *filter, void *data)
#define EVENT_FLAG_REPORTING
static int manager_dbput(struct mansession *s, const struct message *m)
Standard Command Line Interface.
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
static int manager_dbdeltree(struct mansession *s, const struct message *m)
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
static char * handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
static char * handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
static int keymatch(const char *key, const char *prefix)
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
static char * handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Persistant data storage (akin to *doze registry)
#define AST_MUTEX_DEFINE_STATIC(mutex)
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...
int ast_manager_unregister(char *action)
Unregister a registered manager command.
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
#define ast_mutex_unlock(a)
static char * handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char prefix[MAX_PREFIX]
static void astdb_shutdown(void)