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
32 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
43 #include "asterisk/channel.h"
44 #include "asterisk/file.h"
45 #include "asterisk/app.h"
46 #include "asterisk/dsp.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/options.h"
49 #include "asterisk/astdb.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/lock.h"
53 #include "asterisk/manager.h"
54 #include "db1-ast/include/db.h"
57 #define dbopen __dbopen
61 AST_MUTEX_DEFINE_STATIC(dblock
);
63 static int dbinit(void)
65 if (!astdb
&& !(astdb
= dbopen((char *)ast_config_AST_DB
, O_CREAT
| O_RDWR
, 0664, DB_BTREE
, NULL
))) {
66 ast_log(LOG_WARNING
, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB
, strerror(errno
));
73 static inline int keymatch(const char *key
, const char *prefix
)
75 int preflen
= strlen(prefix
);
78 if (!strcasecmp(key
, prefix
))
80 if ((strlen(key
) > preflen
) && !strncasecmp(key
, prefix
, preflen
)) {
81 if (key
[preflen
] == '/')
87 static inline int subkeymatch(const char *key
, const char *suffix
)
89 int suffixlen
= strlen(suffix
);
91 const char *subkey
= key
+ strlen(key
) - suffixlen
;
94 if (!strcasecmp(subkey
, suffix
))
100 int ast_db_deltree(const char *family
, const char *keytree
)
110 snprintf(prefix
, sizeof(prefix
), "/%s/%s", family
, keytree
);
112 snprintf(prefix
, sizeof(prefix
), "/%s", family
);
114 } else if (keytree
) {
120 ast_mutex_lock(&dblock
);
122 ast_mutex_unlock(&dblock
);
126 memset(&key
, 0, sizeof(key
));
127 memset(&data
, 0, sizeof(data
));
129 while (!(res
= astdb
->seq(astdb
, &key
, &data
, pass
++ ? R_NEXT
: R_FIRST
))) {
132 keys
[key
.size
- 1] = '\0';
136 if (keymatch(keys
, prefix
)) {
137 astdb
->del(astdb
, &key
, 0);
140 astdb
->sync(astdb
, 0);
141 ast_mutex_unlock(&dblock
);
145 int ast_db_put(const char *family
, const char *keys
, char *value
)
151 ast_mutex_lock(&dblock
);
153 ast_mutex_unlock(&dblock
);
157 fullkeylen
= snprintf(fullkey
, sizeof(fullkey
), "/%s/%s", family
, keys
);
158 memset(&key
, 0, sizeof(key
));
159 memset(&data
, 0, sizeof(data
));
161 key
.size
= fullkeylen
+ 1;
163 data
.size
= strlen(value
) + 1;
164 res
= astdb
->put(astdb
, &key
, &data
, 0);
165 astdb
->sync(astdb
, 0);
166 ast_mutex_unlock(&dblock
);
168 ast_log(LOG_WARNING
, "Unable to put value '%s' for key '%s' in family '%s'\n", value
, keys
, family
);
172 int ast_db_get(const char *family
, const char *keys
, char *value
, int valuelen
)
174 char fullkey
[256] = "";
178 ast_mutex_lock(&dblock
);
180 ast_mutex_unlock(&dblock
);
184 fullkeylen
= snprintf(fullkey
, sizeof(fullkey
), "/%s/%s", family
, keys
);
185 memset(&key
, 0, sizeof(key
));
186 memset(&data
, 0, sizeof(data
));
187 memset(value
, 0, valuelen
);
189 key
.size
= fullkeylen
+ 1;
191 res
= astdb
->get(astdb
, &key
, &data
, 0);
193 ast_mutex_unlock(&dblock
);
195 /* 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 ast_copy_string(value
, data
.data
, (valuelen
> data
.size
) ? data
.size
: valuelen
);
208 ast_log(LOG_NOTICE
, "Strange, empty value for /%s/%s\n", family
, keys
);
214 int ast_db_del(const char *family
, const char *keys
)
220 ast_mutex_lock(&dblock
);
222 ast_mutex_unlock(&dblock
);
226 fullkeylen
= snprintf(fullkey
, sizeof(fullkey
), "/%s/%s", family
, keys
);
227 memset(&key
, 0, sizeof(key
));
229 key
.size
= fullkeylen
+ 1;
231 res
= astdb
->del(astdb
, &key
, 0);
232 astdb
->sync(astdb
, 0);
234 ast_mutex_unlock(&dblock
);
236 if (res
&& option_debug
)
237 ast_log(LOG_DEBUG
, "Unable to find key '%s' in family '%s'\n", keys
, family
);
241 static int database_put(int fd
, int argc
, char *argv
[])
245 return RESULT_SHOWUSAGE
;
246 res
= ast_db_put(argv
[2], argv
[3], argv
[4]);
248 ast_cli(fd
, "Failed to update entry\n");
250 ast_cli(fd
, "Updated database successfully\n");
252 return RESULT_SUCCESS
;
255 static int database_get(int fd
, int argc
, char *argv
[])
260 return RESULT_SHOWUSAGE
;
261 res
= ast_db_get(argv
[2], argv
[3], tmp
, sizeof(tmp
));
263 ast_cli(fd
, "Database entry not found.\n");
265 ast_cli(fd
, "Value: %s\n", tmp
);
267 return RESULT_SUCCESS
;
270 static int database_del(int fd
, int argc
, char *argv
[])
274 return RESULT_SHOWUSAGE
;
275 res
= ast_db_del(argv
[2], argv
[3]);
277 ast_cli(fd
, "Database entry does not exist.\n");
279 ast_cli(fd
, "Database entry removed.\n");
281 return RESULT_SUCCESS
;
284 static int database_deltree(int fd
, int argc
, char *argv
[])
287 if ((argc
< 3) || (argc
> 4))
288 return RESULT_SHOWUSAGE
;
290 res
= ast_db_deltree(argv
[2], argv
[3]);
292 res
= ast_db_deltree(argv
[2], NULL
);
295 ast_cli(fd
, "Database entries do not exist.\n");
297 ast_cli(fd
, "Database entries removed.\n");
299 return RESULT_SUCCESS
;
302 static int database_show(int fd
, int argc
, char *argv
[])
311 /* Family and key tree */
312 snprintf(prefix
, sizeof(prefix
), "/%s/%s", argv
[2], argv
[3]);
313 } else if (argc
== 3) {
315 snprintf(prefix
, sizeof(prefix
), "/%s", argv
[2]);
316 } else if (argc
== 2) {
320 return RESULT_SHOWUSAGE
;
322 ast_mutex_lock(&dblock
);
324 ast_mutex_unlock(&dblock
);
325 ast_cli(fd
, "Database unavailable\n");
326 return RESULT_SUCCESS
;
328 memset(&key
, 0, sizeof(key
));
329 memset(&data
, 0, sizeof(data
));
331 while (!(res
= astdb
->seq(astdb
, &key
, &data
, pass
++ ? R_NEXT
: R_FIRST
))) {
334 keys
[key
.size
- 1] = '\0';
340 values
[data
.size
- 1]='\0';
342 values
= "<bad value>";
344 if (keymatch(keys
, prefix
)) {
345 ast_cli(fd
, "%-50s: %-25s\n", keys
, values
);
348 ast_mutex_unlock(&dblock
);
349 return RESULT_SUCCESS
;
352 static int database_showkey(int fd
, int argc
, char *argv
[])
362 snprintf(suffix
, sizeof(suffix
), "/%s", argv
[2]);
364 return RESULT_SHOWUSAGE
;
366 ast_mutex_lock(&dblock
);
368 ast_mutex_unlock(&dblock
);
369 ast_cli(fd
, "Database unavailable\n");
370 return RESULT_SUCCESS
;
372 memset(&key
, 0, sizeof(key
));
373 memset(&data
, 0, sizeof(data
));
375 while (!(res
= astdb
->seq(astdb
, &key
, &data
, pass
++ ? R_NEXT
: R_FIRST
))) {
378 keys
[key
.size
- 1] = '\0';
384 values
[data
.size
- 1]='\0';
386 values
= "<bad value>";
388 if (subkeymatch(keys
, suffix
)) {
389 ast_cli(fd
, "%-50s: %-25s\n", keys
, values
);
392 ast_mutex_unlock(&dblock
);
393 return RESULT_SUCCESS
;
396 struct ast_db_entry
*ast_db_gettree(const char *family
, const char *keytree
)
404 struct ast_db_entry
*last
= NULL
;
405 struct ast_db_entry
*cur
, *ret
=NULL
;
407 if (!ast_strlen_zero(family
)) {
408 if (!ast_strlen_zero(keytree
)) {
409 /* Family and key tree */
410 snprintf(prefix
, sizeof(prefix
), "/%s/%s", family
, prefix
);
413 snprintf(prefix
, sizeof(prefix
), "/%s", family
);
418 ast_mutex_lock(&dblock
);
420 ast_mutex_unlock(&dblock
);
421 ast_log(LOG_WARNING
, "Database unavailable\n");
424 memset(&key
, 0, sizeof(key
));
425 memset(&data
, 0, sizeof(data
));
427 while (!(res
= astdb
->seq(astdb
, &key
, &data
, pass
++ ? R_NEXT
: R_FIRST
))) {
430 keys
[key
.size
- 1] = '\0';
436 values
[data
.size
- 1] = '\0';
438 values
= "<bad value>";
440 values_len
= strlen(values
) + 1;
441 if (keymatch(keys
, prefix
) && (cur
= ast_malloc(sizeof(*cur
) + strlen(keys
) + 1 + values_len
))) {
443 cur
->key
= cur
->data
+ values_len
;
444 strcpy(cur
->data
, values
);
445 strcpy(cur
->key
, keys
);
454 ast_mutex_unlock(&dblock
);
458 void ast_db_freetree(struct ast_db_entry
*dbe
)
460 struct ast_db_entry
*last
;
468 static char database_show_usage
[] =
469 "Usage: database show [family [keytree]]\n"
470 " Shows Asterisk database contents, optionally restricted\n"
471 "to a given family, or family and keytree.\n";
473 static char database_showkey_usage
[] =
474 "Usage: database showkey <keytree>\n"
475 " Shows Asterisk database contents, restricted to a given key.\n";
477 static char database_put_usage
[] =
478 "Usage: database put <family> <key> <value>\n"
479 " Adds or updates an entry in the Asterisk database for\n"
480 "a given family, key, and value.\n";
482 static char database_get_usage
[] =
483 "Usage: database get <family> <key>\n"
484 " Retrieves an entry in the Asterisk database for a given\n"
487 static char database_del_usage
[] =
488 "Usage: database del <family> <key>\n"
489 " Deletes an entry in the Asterisk database for a given\n"
492 static char database_deltree_usage
[] =
493 "Usage: database deltree <family> [keytree]\n"
494 " Deletes a family or specific keytree within a family\n"
495 "in the Asterisk database.\n";
497 struct ast_cli_entry cli_database
[] = {
498 { { "database", "show", NULL
},
499 database_show
, "Shows database contents",
500 database_show_usage
},
502 { { "database", "showkey", NULL
},
503 database_showkey
, "Shows database contents",
504 database_showkey_usage
},
506 { { "database", "get", NULL
},
507 database_get
, "Gets database value",
508 database_get_usage
},
510 { { "database", "put", NULL
},
511 database_put
, "Adds/updates database value",
512 database_put_usage
},
514 { { "database", "del", NULL
},
515 database_del
, "Removes database key/value",
516 database_del_usage
},
518 { { "database", "deltree", NULL
},
519 database_deltree
, "Removes database keytree/values",
520 database_deltree_usage
},
523 static int manager_dbput(struct mansession
*s
, const struct message
*m
)
525 const char *family
= astman_get_header(m
, "Family");
526 const char *key
= astman_get_header(m
, "Key");
527 const char *val
= astman_get_header(m
, "Val");
530 if (ast_strlen_zero(family
)) {
531 astman_send_error(s
, m
, "No family specified");
534 if (ast_strlen_zero(key
)) {
535 astman_send_error(s
, m
, "No key specified");
539 res
= ast_db_put(family
, key
, (char *) S_OR(val
, ""));
541 astman_send_error(s
, m
, "Failed to update entry");
543 astman_send_ack(s
, m
, "Updated database successfully");
548 static int manager_dbget(struct mansession
*s
, const struct message
*m
)
550 const char *id
= astman_get_header(m
,"ActionID");
551 char idText
[256] = "";
552 const char *family
= astman_get_header(m
, "Family");
553 const char *key
= astman_get_header(m
, "Key");
557 if (ast_strlen_zero(family
)) {
558 astman_send_error(s
, m
, "No family specified.");
561 if (ast_strlen_zero(key
)) {
562 astman_send_error(s
, m
, "No key specified.");
566 if (!ast_strlen_zero(id
))
567 snprintf(idText
, sizeof(idText
) ,"ActionID: %s\r\n", id
);
569 res
= ast_db_get(family
, key
, tmp
, sizeof(tmp
));
571 astman_send_error(s
, m
, "Database entry not found");
573 astman_send_ack(s
, m
, "Result will follow");
574 astman_append(s
, "Event: DBGetResponse\r\n"
580 family
, key
, tmp
, idText
);
588 ast_cli_register_multiple(cli_database
, sizeof(cli_database
) / sizeof(struct ast_cli_entry
));
589 ast_manager_register("DBGet", EVENT_FLAG_SYSTEM
, manager_dbget
, "Get DB Entry");
590 ast_manager_register("DBPut", EVENT_FLAG_SYSTEM
, manager_dbput
, "Put DB Entry");