2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief ASTdb Management
23 * \author Mark Spencer <markster@digium.com>
25 * \note DB3 is licensed under Sleepycat Public License and is thus incompatible
26 * with GPL. To avoid having to make another exception (and complicate
27 * licensing even further) we elect to use DB1 which is BSD licensed
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/channel.h"
45 #include "asterisk/file.h"
46 #include "asterisk/app.h"
47 #include "asterisk/dsp.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/options.h"
50 #include "asterisk/astdb.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/utils.h"
53 #include "asterisk/lock.h"
54 #include "asterisk/manager.h"
55 #include "db1-ast/include/db.h"
58 #define dbopen __dbopen
62 AST_MUTEX_DEFINE_STATIC(dblock);
64 static int dbinit(void)
66 if (!astdb && !(astdb = dbopen((char *)ast_config_AST_DB, O_CREAT | O_RDWR, 0664, DB_BTREE, NULL))) {
67 ast_log(LOG_WARNING, "Unable to open Asterisk database\n");
74 static inline int keymatch(const char *key, const char *prefix)
76 int preflen = strlen(prefix);
79 if (!strcasecmp(key, prefix))
81 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
82 if (key[preflen] == '/')
88 static inline int subkeymatch(const char *key, const char *suffix)
90 int suffixlen = strlen(suffix);
92 const char *subkey = key + strlen(key) - suffixlen;
95 if (!strcasecmp(subkey, suffix))
101 int ast_db_deltree(const char *family, const char *keytree)
111 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
113 snprintf(prefix, sizeof(prefix), "/%s", family);
115 } else if (keytree) {
121 ast_mutex_lock(&dblock);
123 ast_mutex_unlock(&dblock);
127 memset(&key, 0, sizeof(key));
128 memset(&data, 0, sizeof(data));
130 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
133 keys[key.size - 1] = '\0';
137 if (keymatch(keys, prefix)) {
138 astdb->del(astdb, &key, 0);
141 astdb->sync(astdb, 0);
142 ast_mutex_unlock(&dblock);
146 int ast_db_put(const char *family, const char *keys, char *value)
152 ast_mutex_lock(&dblock);
154 ast_mutex_unlock(&dblock);
158 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
159 memset(&key, 0, sizeof(key));
160 memset(&data, 0, sizeof(data));
162 key.size = fullkeylen + 1;
164 data.size = strlen(value) + 1;
165 res = astdb->put(astdb, &key, &data, 0);
166 astdb->sync(astdb, 0);
167 ast_mutex_unlock(&dblock);
169 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
173 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
175 char fullkey[256] = "";
179 ast_mutex_lock(&dblock);
181 ast_mutex_unlock(&dblock);
185 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
186 memset(&key, 0, sizeof(key));
187 memset(&data, 0, sizeof(data));
188 memset(value, 0, valuelen);
190 key.size = fullkeylen + 1;
192 res = astdb->get(astdb, &key, &data, 0);
194 ast_mutex_unlock(&dblock);
196 /* Be sure to NULL terminate our data either way */
198 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
201 printf("Got value of size %d\n", data.size);
204 ((char *)data.data)[data.size - 1] = '\0';
205 /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
206 strncpy(value, data.data, (valuelen > data.size) ? data.size : valuelen);
207 value[valuelen - 1] = '\0';
209 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
215 int ast_db_del(const char *family, const char *keys)
221 ast_mutex_lock(&dblock);
223 ast_mutex_unlock(&dblock);
227 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
228 memset(&key, 0, sizeof(key));
230 key.size = fullkeylen + 1;
232 res = astdb->del(astdb, &key, 0);
233 astdb->sync(astdb, 0);
235 ast_mutex_unlock(&dblock);
238 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
242 static int database_put(int fd, int argc, char *argv[])
246 return RESULT_SHOWUSAGE;
247 res = ast_db_put(argv[2], argv[3], argv[4]);
249 ast_cli(fd, "Failed to update entry\n");
251 ast_cli(fd, "Updated database successfully\n");
253 return RESULT_SUCCESS;
256 static int database_get(int fd, int argc, char *argv[])
261 return RESULT_SHOWUSAGE;
262 res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
264 ast_cli(fd, "Database entry not found.\n");
266 ast_cli(fd, "Value: %s\n", tmp);
268 return RESULT_SUCCESS;
271 static int database_del(int fd, int argc, char *argv[])
275 return RESULT_SHOWUSAGE;
276 res = ast_db_del(argv[2], argv[3]);
278 ast_cli(fd, "Database entry does not exist.\n");
280 ast_cli(fd, "Database entry removed.\n");
282 return RESULT_SUCCESS;
285 static int database_deltree(int fd, int argc, char *argv[])
288 if ((argc < 3) || (argc > 4))
289 return RESULT_SHOWUSAGE;
291 res = ast_db_deltree(argv[2], argv[3]);
293 res = ast_db_deltree(argv[2], NULL);
296 ast_cli(fd, "Database entries do not exist.\n");
298 ast_cli(fd, "Database entries removed.\n");
300 return RESULT_SUCCESS;
303 static int database_show(int fd, int argc, char *argv[])
312 /* Family and key tree */
313 snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]);
314 } else if (argc == 3) {
316 snprintf(prefix, sizeof(prefix), "/%s", argv[2]);
317 } else if (argc == 2) {
321 return RESULT_SHOWUSAGE;
323 ast_mutex_lock(&dblock);
325 ast_mutex_unlock(&dblock);
326 ast_cli(fd, "Database unavailable\n");
327 return RESULT_SUCCESS;
329 memset(&key, 0, sizeof(key));
330 memset(&data, 0, sizeof(data));
332 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
335 keys[key.size - 1] = '\0';
341 values[data.size - 1]='\0';
343 values = "<bad value>";
345 if (keymatch(keys, prefix)) {
346 ast_cli(fd, "%-50s: %-25s\n", keys, values);
349 ast_mutex_unlock(&dblock);
350 return RESULT_SUCCESS;
353 static int database_showkey(int fd, int argc, char *argv[])
363 snprintf(suffix, sizeof(suffix), "/%s", argv[2]);
365 return RESULT_SHOWUSAGE;
367 ast_mutex_lock(&dblock);
369 ast_mutex_unlock(&dblock);
370 ast_cli(fd, "Database unavailable\n");
371 return RESULT_SUCCESS;
373 memset(&key, 0, sizeof(key));
374 memset(&data, 0, sizeof(data));
376 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
379 keys[key.size - 1] = '\0';
385 values[data.size - 1]='\0';
387 values = "<bad value>";
389 if (subkeymatch(keys, suffix)) {
390 ast_cli(fd, "%-50s: %-25s\n", keys, values);
393 ast_mutex_unlock(&dblock);
394 return RESULT_SUCCESS;
397 struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
405 struct ast_db_entry *last = NULL;
406 struct ast_db_entry *cur, *ret=NULL;
408 if (!ast_strlen_zero(family)) {
409 if (!ast_strlen_zero(keytree)) {
410 /* Family and key tree */
411 snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
414 snprintf(prefix, sizeof(prefix), "/%s", family);
419 ast_mutex_lock(&dblock);
421 ast_mutex_unlock(&dblock);
422 ast_log(LOG_WARNING, "Database unavailable\n");
425 memset(&key, 0, sizeof(key));
426 memset(&data, 0, sizeof(data));
428 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
431 keys[key.size - 1] = '\0';
437 values[data.size - 1] = '\0';
439 values = "<bad value>";
441 values_len = strlen(values) + 1;
442 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
444 cur->key = cur->data + values_len;
445 strcpy(cur->data, values);
446 strcpy(cur->key, keys);
455 ast_mutex_unlock(&dblock);
459 void ast_db_freetree(struct ast_db_entry *dbe)
461 struct ast_db_entry *last;
469 static char database_show_usage[] =
470 "Usage: database show [family [keytree]]\n"
471 " Shows Asterisk database contents, optionally restricted\n"
472 "to a given family, or family and keytree.\n";
474 static char database_showkey_usage[] =
475 "Usage: database showkey <keytree>\n"
476 " Shows Asterisk database contents, restricted to a given key.\n";
478 static char database_put_usage[] =
479 "Usage: database put <family> <key> <value>\n"
480 " Adds or updates an entry in the Asterisk database for\n"
481 "a given family, key, and value.\n";
483 static char database_get_usage[] =
484 "Usage: database get <family> <key>\n"
485 " Retrieves an entry in the Asterisk database for a given\n"
488 static char database_del_usage[] =
489 "Usage: database del <family> <key>\n"
490 " Deletes an entry in the Asterisk database for a given\n"
493 static char database_deltree_usage[] =
494 "Usage: database deltree <family> [keytree]\n"
495 " Deletes a family or specific keytree within a family\n"
496 "in the Asterisk database.\n";
498 struct ast_cli_entry cli_database_show =
499 { { "database", "show", NULL }, database_show, "Shows database contents", database_show_usage };
501 struct ast_cli_entry cli_database_showkey =
502 { { "database", "showkey", NULL }, database_showkey, "Shows database contents", database_showkey_usage };
504 struct ast_cli_entry cli_database_get =
505 { { "database", "get", NULL }, database_get, "Gets database value", database_get_usage };
507 struct ast_cli_entry cli_database_put =
508 { { "database", "put", NULL }, database_put, "Adds/updates database value", database_put_usage };
510 struct ast_cli_entry cli_database_del =
511 { { "database", "del", NULL }, database_del, "Removes database key/value", database_del_usage };
513 struct ast_cli_entry cli_database_deltree =
514 { { "database", "deltree", NULL }, database_deltree, "Removes database keytree/values", database_deltree_usage };
516 static int manager_dbput(struct mansession *s, struct message *m)
518 char *family = astman_get_header(m, "Family");
519 char *key = astman_get_header(m, "Key");
520 char *val = astman_get_header(m, "Val");
523 if (ast_strlen_zero(family)) {
524 astman_send_error(s, m, "No family specified");
527 if (ast_strlen_zero(key)) {
528 astman_send_error(s, m, "No key specified");
531 if (ast_strlen_zero(val)) {
532 astman_send_error(s, m, "No val specified");
536 res = ast_db_put(family, key, val);
538 astman_send_error(s, m, "Failed to update entry");
540 astman_send_ack(s, m, "Updated database successfully");
545 static int manager_dbget(struct mansession *s, struct message *m)
547 char *id = astman_get_header(m,"ActionID");
548 char idText[256] = "";
549 char *family = astman_get_header(m, "Family");
550 char *key = astman_get_header(m, "Key");
554 if (ast_strlen_zero(family)) {
555 astman_send_error(s, m, "No family specified.");
558 if (ast_strlen_zero(key)) {
559 astman_send_error(s, m, "No key specified.");
563 if (!ast_strlen_zero(id))
564 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
566 res = ast_db_get(family, key, tmp, sizeof(tmp));
568 astman_send_error(s, m, "Database entry not found");
570 astman_send_ack(s, m, "Result will follow");
571 ast_cli(s->fd, "Event: DBGetResponse\r\n"
577 family, key, tmp, idText);
585 ast_cli_register(&cli_database_show);
586 ast_cli_register(&cli_database_showkey);
587 ast_cli_register(&cli_database_get);
588 ast_cli_register(&cli_database_put);
589 ast_cli_register(&cli_database_del);
590 ast_cli_register(&cli_database_deltree);
591 ast_manager_register("DBGet", EVENT_FLAG_SYSTEM, manager_dbget, "Get DB Entry");
592 ast_manager_register("DBPut", EVENT_FLAG_SYSTEM, manager_dbput, "Put DB Entry");