Don't keep repeating the warning over and over when the end of the call is reached...
[asterisk-bristuff.git] / main / db.c
blobed6fbc683c4a86c4529e881ad44cba4e7a4567bd
1 /*
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.
19 /*! \file
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
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <dirent.h>
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"
56 #ifdef __CYGWIN__
57 #define dbopen __dbopen
58 #endif
60 static DB *astdb;
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");
67 return -1;
69 return 0;
73 static inline int keymatch(const char *key, const char *prefix)
75 int preflen = strlen(prefix);
76 if (!preflen)
77 return 1;
78 if (!strcasecmp(key, prefix))
79 return 1;
80 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
81 if (key[preflen] == '/')
82 return 1;
84 return 0;
87 static inline int subkeymatch(const char *key, const char *suffix)
89 int suffixlen = strlen(suffix);
90 if (suffixlen) {
91 const char *subkey = key + strlen(key) - suffixlen;
92 if (subkey < key)
93 return 0;
94 if (!strcasecmp(subkey, suffix))
95 return 1;
97 return 0;
100 int ast_db_deltree(const char *family, const char *keytree)
102 char prefix[256];
103 DBT key, data;
104 char *keys;
105 int res;
106 int pass;
108 if (family) {
109 if (keytree) {
110 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
111 } else {
112 snprintf(prefix, sizeof(prefix), "/%s", family);
114 } else if (keytree) {
115 return -1;
116 } else {
117 prefix[0] = '\0';
120 ast_mutex_lock(&dblock);
121 if (dbinit()) {
122 ast_mutex_unlock(&dblock);
123 return -1;
126 memset(&key, 0, sizeof(key));
127 memset(&data, 0, sizeof(data));
128 pass = 0;
129 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
130 if (key.size) {
131 keys = key.data;
132 keys[key.size - 1] = '\0';
133 } else {
134 keys = "<bad key>";
136 if (keymatch(keys, prefix)) {
137 astdb->del(astdb, &key, 0);
140 astdb->sync(astdb, 0);
141 ast_mutex_unlock(&dblock);
142 return 0;
145 int ast_db_put(const char *family, const char *keys, char *value)
147 char fullkey[256];
148 DBT key, data;
149 int res, fullkeylen;
151 ast_mutex_lock(&dblock);
152 if (dbinit()) {
153 ast_mutex_unlock(&dblock);
154 return -1;
157 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
158 memset(&key, 0, sizeof(key));
159 memset(&data, 0, sizeof(data));
160 key.data = fullkey;
161 key.size = fullkeylen + 1;
162 data.data = value;
163 data.size = strlen(value) + 1;
164 res = astdb->put(astdb, &key, &data, 0);
165 astdb->sync(astdb, 0);
166 ast_mutex_unlock(&dblock);
167 if (res)
168 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
169 return res;
172 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
174 char fullkey[256] = "";
175 DBT key, data;
176 int res, fullkeylen;
178 ast_mutex_lock(&dblock);
179 if (dbinit()) {
180 ast_mutex_unlock(&dblock);
181 return -1;
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);
188 key.data = fullkey;
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 */
196 if (res) {
197 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
198 } else {
199 #if 0
200 printf("Got value of size %d\n", data.size);
201 #endif
202 if (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);
206 } else {
207 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
210 return res;
213 int ast_db_del(const char *family, const char *keys)
215 char fullkey[256];
216 DBT key;
217 int res, fullkeylen;
219 ast_mutex_lock(&dblock);
220 if (dbinit()) {
221 ast_mutex_unlock(&dblock);
222 return -1;
225 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
226 memset(&key, 0, sizeof(key));
227 key.data = fullkey;
228 key.size = fullkeylen + 1;
230 res = astdb->del(astdb, &key, 0);
231 astdb->sync(astdb, 0);
233 ast_mutex_unlock(&dblock);
235 if (res)
236 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
237 return res;
240 static int database_put(int fd, int argc, char *argv[])
242 int res;
243 if (argc != 5)
244 return RESULT_SHOWUSAGE;
245 res = ast_db_put(argv[2], argv[3], argv[4]);
246 if (res) {
247 ast_cli(fd, "Failed to update entry\n");
248 } else {
249 ast_cli(fd, "Updated database successfully\n");
251 return RESULT_SUCCESS;
254 static int database_get(int fd, int argc, char *argv[])
256 int res;
257 char tmp[256];
258 if (argc != 4)
259 return RESULT_SHOWUSAGE;
260 res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
261 if (res) {
262 ast_cli(fd, "Database entry not found.\n");
263 } else {
264 ast_cli(fd, "Value: %s\n", tmp);
266 return RESULT_SUCCESS;
269 static int database_del(int fd, int argc, char *argv[])
271 int res;
272 if (argc != 4)
273 return RESULT_SHOWUSAGE;
274 res = ast_db_del(argv[2], argv[3]);
275 if (res) {
276 ast_cli(fd, "Database entry does not exist.\n");
277 } else {
278 ast_cli(fd, "Database entry removed.\n");
280 return RESULT_SUCCESS;
283 static int database_deltree(int fd, int argc, char *argv[])
285 int res;
286 if ((argc < 3) || (argc > 4))
287 return RESULT_SHOWUSAGE;
288 if (argc == 4) {
289 res = ast_db_deltree(argv[2], argv[3]);
290 } else {
291 res = ast_db_deltree(argv[2], NULL);
293 if (res) {
294 ast_cli(fd, "Database entries do not exist.\n");
295 } else {
296 ast_cli(fd, "Database entries removed.\n");
298 return RESULT_SUCCESS;
301 static int database_show(int fd, int argc, char *argv[])
303 char prefix[256];
304 DBT key, data;
305 char *keys, *values;
306 int res;
307 int pass;
309 if (argc == 4) {
310 /* Family and key tree */
311 snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]);
312 } else if (argc == 3) {
313 /* Family only */
314 snprintf(prefix, sizeof(prefix), "/%s", argv[2]);
315 } else if (argc == 2) {
316 /* Neither */
317 prefix[0] = '\0';
318 } else {
319 return RESULT_SHOWUSAGE;
321 ast_mutex_lock(&dblock);
322 if (dbinit()) {
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));
329 pass = 0;
330 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
331 if (key.size) {
332 keys = key.data;
333 keys[key.size - 1] = '\0';
334 } else {
335 keys = "<bad key>";
337 if (data.size) {
338 values = data.data;
339 values[data.size - 1]='\0';
340 } else {
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[])
353 char suffix[256];
354 DBT key, data;
355 char *keys, *values;
356 int res;
357 int pass;
359 if (argc == 3) {
360 /* Key only */
361 snprintf(suffix, sizeof(suffix), "/%s", argv[2]);
362 } else {
363 return RESULT_SHOWUSAGE;
365 ast_mutex_lock(&dblock);
366 if (dbinit()) {
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));
373 pass = 0;
374 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
375 if (key.size) {
376 keys = key.data;
377 keys[key.size - 1] = '\0';
378 } else {
379 keys = "<bad key>";
381 if (data.size) {
382 values = data.data;
383 values[data.size - 1]='\0';
384 } else {
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)
397 char prefix[256];
398 DBT key, data;
399 char *keys, *values;
400 int values_len;
401 int res;
402 int pass;
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);
410 } else {
411 /* Family only */
412 snprintf(prefix, sizeof(prefix), "/%s", family);
414 } else {
415 prefix[0] = '\0';
417 ast_mutex_lock(&dblock);
418 if (dbinit()) {
419 ast_mutex_unlock(&dblock);
420 ast_log(LOG_WARNING, "Database unavailable\n");
421 return NULL;
423 memset(&key, 0, sizeof(key));
424 memset(&data, 0, sizeof(data));
425 pass = 0;
426 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
427 if (key.size) {
428 keys = key.data;
429 keys[key.size - 1] = '\0';
430 } else {
431 keys = "<bad key>";
433 if (data.size) {
434 values = data.data;
435 values[data.size - 1] = '\0';
436 } else {
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))) {
441 cur->next = NULL;
442 cur->key = cur->data + values_len;
443 strcpy(cur->data, values);
444 strcpy(cur->key, keys);
445 if (last) {
446 last->next = cur;
447 } else {
448 ret = cur;
450 last = cur;
453 ast_mutex_unlock(&dblock);
454 return ret;
457 void ast_db_freetree(struct ast_db_entry *dbe)
459 struct ast_db_entry *last;
460 while (dbe) {
461 last = dbe;
462 dbe = dbe->next;
463 free(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"
484 "family and key.\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"
489 "family and key.\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");
527 int res;
529 if (ast_strlen_zero(family)) {
530 astman_send_error(s, m, "No family specified");
531 return 0;
533 if (ast_strlen_zero(key)) {
534 astman_send_error(s, m, "No key specified");
535 return 0;
537 if (ast_strlen_zero(val)) {
538 astman_send_error(s, m, "No val specified");
539 return 0;
542 res = ast_db_put(family, key, val);
543 if (res) {
544 astman_send_error(s, m, "Failed to update entry");
545 } else {
546 astman_send_ack(s, m, "Updated database successfully");
548 return 0;
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");
557 char tmp[256];
558 int res;
560 if (ast_strlen_zero(family)) {
561 astman_send_error(s, m, "No family specified.");
562 return 0;
564 if (ast_strlen_zero(key)) {
565 astman_send_error(s, m, "No key specified.");
566 return 0;
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));
573 if (res) {
574 astman_send_error(s, m, "Database entry not found");
575 } else {
576 astman_send_ack(s, m, "Result will follow");
577 astman_append(s, "Event: DBGetResponse\r\n"
578 "Family: %s\r\n"
579 "Key: %s\r\n"
580 "Val: %s\r\n"
581 "%s"
582 "\r\n",
583 family, key, tmp, idText);
585 return 0;
588 int astdb_init(void)
590 dbinit();
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");
594 return 0;