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\n");
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 */
197 ast_log(LOG_DEBUG
, "Unable to find key '%s' in family '%s'\n", keys
, family
);
200 printf("Got value of size %d\n", data
.size
);
203 ((char *)data
.data
)[data
.size
- 1] = '\0';
204 /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
205 ast_copy_string(value
, data
.data
, (valuelen
> data
.size
) ? data
.size
: valuelen
);
207 ast_log(LOG_NOTICE
, "Strange, empty value for /%s/%s\n", family
, keys
);
213 int ast_db_del(const char *family
, const char *keys
)
219 ast_mutex_lock(&dblock
);
221 ast_mutex_unlock(&dblock
);
225 fullkeylen
= snprintf(fullkey
, sizeof(fullkey
), "/%s/%s", family
, keys
);
226 memset(&key
, 0, sizeof(key
));
228 key
.size
= fullkeylen
+ 1;
230 res
= astdb
->del(astdb
, &key
, 0);
231 astdb
->sync(astdb
, 0);
233 ast_mutex_unlock(&dblock
);
236 ast_log(LOG_DEBUG
, "Unable to find key '%s' in family '%s'\n", keys
, family
);
240 static int database_put(int fd
, int argc
, char *argv
[])
244 return RESULT_SHOWUSAGE
;
245 res
= ast_db_put(argv
[2], argv
[3], argv
[4]);
247 ast_cli(fd
, "Failed to update entry\n");
249 ast_cli(fd
, "Updated database successfully\n");
251 return RESULT_SUCCESS
;
254 static int database_get(int fd
, int argc
, char *argv
[])
259 return RESULT_SHOWUSAGE
;
260 res
= ast_db_get(argv
[2], argv
[3], tmp
, sizeof(tmp
));
262 ast_cli(fd
, "Database entry not found.\n");
264 ast_cli(fd
, "Value: %s\n", tmp
);
266 return RESULT_SUCCESS
;
269 static int database_del(int fd
, int argc
, char *argv
[])
273 return RESULT_SHOWUSAGE
;
274 res
= ast_db_del(argv
[2], argv
[3]);
276 ast_cli(fd
, "Database entry does not exist.\n");
278 ast_cli(fd
, "Database entry removed.\n");
280 return RESULT_SUCCESS
;
283 static int database_deltree(int fd
, int argc
, char *argv
[])
286 if ((argc
< 3) || (argc
> 4))
287 return RESULT_SHOWUSAGE
;
289 res
= ast_db_deltree(argv
[2], argv
[3]);
291 res
= ast_db_deltree(argv
[2], NULL
);
294 ast_cli(fd
, "Database entries do not exist.\n");
296 ast_cli(fd
, "Database entries removed.\n");
298 return RESULT_SUCCESS
;
301 static int database_show(int fd
, int argc
, char *argv
[])
310 /* Family and key tree */
311 snprintf(prefix
, sizeof(prefix
), "/%s/%s", argv
[2], argv
[3]);
312 } else if (argc
== 3) {
314 snprintf(prefix
, sizeof(prefix
), "/%s", argv
[2]);
315 } else if (argc
== 2) {
319 return RESULT_SHOWUSAGE
;
321 ast_mutex_lock(&dblock
);
323 ast_mutex_unlock(&dblock
);
324 ast_cli(fd
, "Database unavailable\n");
325 return RESULT_SUCCESS
;
327 memset(&key
, 0, sizeof(key
));
328 memset(&data
, 0, sizeof(data
));
330 while (!(res
= astdb
->seq(astdb
, &key
, &data
, pass
++ ? R_NEXT
: R_FIRST
))) {
333 keys
[key
.size
- 1] = '\0';
339 values
[data
.size
- 1]='\0';
341 values
= "<bad value>";
343 if (keymatch(keys
, prefix
)) {
344 ast_cli(fd
, "%-50s: %-25s\n", keys
, values
);
347 ast_mutex_unlock(&dblock
);
348 return RESULT_SUCCESS
;
351 static int database_showkey(int fd
, int argc
, char *argv
[])
361 snprintf(suffix
, sizeof(suffix
), "/%s", argv
[2]);
363 return RESULT_SHOWUSAGE
;
365 ast_mutex_lock(&dblock
);
367 ast_mutex_unlock(&dblock
);
368 ast_cli(fd
, "Database unavailable\n");
369 return RESULT_SUCCESS
;
371 memset(&key
, 0, sizeof(key
));
372 memset(&data
, 0, sizeof(data
));
374 while (!(res
= astdb
->seq(astdb
, &key
, &data
, pass
++ ? R_NEXT
: R_FIRST
))) {
377 keys
[key
.size
- 1] = '\0';
383 values
[data
.size
- 1]='\0';
385 values
= "<bad value>";
387 if (subkeymatch(keys
, suffix
)) {
388 ast_cli(fd
, "%-50s: %-25s\n", keys
, values
);
391 ast_mutex_unlock(&dblock
);
392 return RESULT_SUCCESS
;
395 struct ast_db_entry
*ast_db_gettree(const char *family
, const char *keytree
)
403 struct ast_db_entry
*last
= NULL
;
404 struct ast_db_entry
*cur
, *ret
=NULL
;
406 if (!ast_strlen_zero(family
)) {
407 if (!ast_strlen_zero(keytree
)) {
408 /* Family and key tree */
409 snprintf(prefix
, sizeof(prefix
), "/%s/%s", family
, prefix
);
412 snprintf(prefix
, sizeof(prefix
), "/%s", family
);
417 ast_mutex_lock(&dblock
);
419 ast_mutex_unlock(&dblock
);
420 ast_log(LOG_WARNING
, "Database unavailable\n");
423 memset(&key
, 0, sizeof(key
));
424 memset(&data
, 0, sizeof(data
));
426 while (!(res
= astdb
->seq(astdb
, &key
, &data
, pass
++ ? R_NEXT
: R_FIRST
))) {
429 keys
[key
.size
- 1] = '\0';
435 values
[data
.size
- 1] = '\0';
437 values
= "<bad value>";
439 values_len
= strlen(values
) + 1;
440 if (keymatch(keys
, prefix
) && (cur
= ast_malloc(sizeof(*cur
) + strlen(keys
) + 1 + values_len
))) {
442 cur
->key
= cur
->data
+ values_len
;
443 strcpy(cur
->data
, values
);
444 strcpy(cur
->key
, keys
);
453 ast_mutex_unlock(&dblock
);
457 void ast_db_freetree(struct ast_db_entry
*dbe
)
459 struct ast_db_entry
*last
;
467 static char database_show_usage
[] =
468 "Usage: database show [family [keytree]]\n"
469 " Shows Asterisk database contents, optionally restricted\n"
470 "to a given family, or family and keytree.\n";
472 static char database_showkey_usage
[] =
473 "Usage: database showkey <keytree>\n"
474 " Shows Asterisk database contents, restricted to a given key.\n";
476 static char database_put_usage
[] =
477 "Usage: database put <family> <key> <value>\n"
478 " Adds or updates an entry in the Asterisk database for\n"
479 "a given family, key, and value.\n";
481 static char database_get_usage
[] =
482 "Usage: database get <family> <key>\n"
483 " Retrieves an entry in the Asterisk database for a given\n"
486 static char database_del_usage
[] =
487 "Usage: database del <family> <key>\n"
488 " Deletes an entry in the Asterisk database for a given\n"
491 static char database_deltree_usage
[] =
492 "Usage: database deltree <family> [keytree]\n"
493 " Deletes a family or specific keytree within a family\n"
494 "in the Asterisk database.\n";
496 struct ast_cli_entry cli_database
[] = {
497 { { "database", "show", NULL
},
498 database_show
, "Shows database contents",
499 database_show_usage
},
501 { { "database", "showkey", NULL
},
502 database_showkey
, "Shows database contents",
503 database_showkey_usage
},
505 { { "database", "get", NULL
},
506 database_get
, "Gets database value",
507 database_get_usage
},
509 { { "database", "put", NULL
},
510 database_put
, "Adds/updates database value",
511 database_put_usage
},
513 { { "database", "del", NULL
},
514 database_del
, "Removes database key/value",
515 database_del_usage
},
517 { { "database", "deltree", NULL
},
518 database_deltree
, "Removes database keytree/values",
519 database_deltree_usage
},
522 static int manager_dbput(struct mansession
*s
, struct message
*m
)
524 char *family
= astman_get_header(m
, "Family");
525 char *key
= astman_get_header(m
, "Key");
526 char *val
= astman_get_header(m
, "Val");
529 if (ast_strlen_zero(family
)) {
530 astman_send_error(s
, m
, "No family specified");
533 if (ast_strlen_zero(key
)) {
534 astman_send_error(s
, m
, "No key specified");
537 if (ast_strlen_zero(val
)) {
538 astman_send_error(s
, m
, "No val specified");
542 res
= ast_db_put(family
, key
, val
);
544 astman_send_error(s
, m
, "Failed to update entry");
546 astman_send_ack(s
, m
, "Updated database successfully");
551 static int manager_dbget(struct mansession
*s
, struct message
*m
)
553 char *id
= astman_get_header(m
,"ActionID");
554 char idText
[256] = "";
555 char *family
= astman_get_header(m
, "Family");
556 char *key
= astman_get_header(m
, "Key");
560 if (ast_strlen_zero(family
)) {
561 astman_send_error(s
, m
, "No family specified.");
564 if (ast_strlen_zero(key
)) {
565 astman_send_error(s
, m
, "No key specified.");
569 if (!ast_strlen_zero(id
))
570 snprintf(idText
, sizeof(idText
) ,"ActionID: %s\r\n", id
);
572 res
= ast_db_get(family
, key
, tmp
, sizeof(tmp
));
574 astman_send_error(s
, m
, "Database entry not found");
576 astman_send_ack(s
, m
, "Result will follow");
577 astman_append(s
, "Event: DBGetResponse\r\n"
583 family
, key
, tmp
, idText
);
591 ast_cli_register_multiple(cli_database
, sizeof(cli_database
) / sizeof(struct ast_cli_entry
));
592 ast_manager_register("DBGet", EVENT_FLAG_SYSTEM
, manager_dbget
, "Get DB Entry");
593 ast_manager_register("DBPut", EVENT_FLAG_SYSTEM
, manager_dbput
, "Put DB Entry");