officially deprecate the 'roundrobin' queue strategy in favor of 'rrmemory'
[asterisk-bristuff.git] / db.c
blob93d23d0322f6a64066a39c9f5f89471a169b1508
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
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/time.h>
35 #include <signal.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <dirent.h>
40 #include "asterisk.h"
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"
57 #ifdef __CYGWIN__
58 #define dbopen __dbopen
59 #endif
61 static DB *astdb;
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");
68 return -1;
70 return 0;
74 static inline int keymatch(const char *key, const char *prefix)
76 int preflen = strlen(prefix);
77 if (!preflen)
78 return 1;
79 if (!strcasecmp(key, prefix))
80 return 1;
81 if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
82 if (key[preflen] == '/')
83 return 1;
85 return 0;
88 static inline int subkeymatch(const char *key, const char *suffix)
90 int suffixlen = strlen(suffix);
91 if (suffixlen) {
92 const char *subkey = key + strlen(key) - suffixlen;
93 if (subkey < key)
94 return 0;
95 if (!strcasecmp(subkey, suffix))
96 return 1;
98 return 0;
101 int ast_db_deltree(const char *family, const char *keytree)
103 char prefix[256];
104 DBT key, data;
105 char *keys;
106 int res;
107 int pass;
109 if (family) {
110 if (keytree) {
111 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
112 } else {
113 snprintf(prefix, sizeof(prefix), "/%s", family);
115 } else if (keytree) {
116 return -1;
117 } else {
118 prefix[0] = '\0';
121 ast_mutex_lock(&dblock);
122 if (dbinit()) {
123 ast_mutex_unlock(&dblock);
124 return -1;
127 memset(&key, 0, sizeof(key));
128 memset(&data, 0, sizeof(data));
129 pass = 0;
130 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
131 if (key.size) {
132 keys = key.data;
133 keys[key.size - 1] = '\0';
134 } else {
135 keys = "<bad key>";
137 if (keymatch(keys, prefix)) {
138 astdb->del(astdb, &key, 0);
141 astdb->sync(astdb, 0);
142 ast_mutex_unlock(&dblock);
143 return 0;
146 int ast_db_put(const char *family, const char *keys, char *value)
148 char fullkey[256];
149 DBT key, data;
150 int res, fullkeylen;
152 ast_mutex_lock(&dblock);
153 if (dbinit()) {
154 ast_mutex_unlock(&dblock);
155 return -1;
158 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
159 memset(&key, 0, sizeof(key));
160 memset(&data, 0, sizeof(data));
161 key.data = fullkey;
162 key.size = fullkeylen + 1;
163 data.data = value;
164 data.size = strlen(value) + 1;
165 res = astdb->put(astdb, &key, &data, 0);
166 astdb->sync(astdb, 0);
167 ast_mutex_unlock(&dblock);
168 if (res)
169 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
170 return res;
173 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
175 char fullkey[256] = "";
176 DBT key, data;
177 int res, fullkeylen;
179 ast_mutex_lock(&dblock);
180 if (dbinit()) {
181 ast_mutex_unlock(&dblock);
182 return -1;
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);
189 key.data = fullkey;
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 */
197 if (res) {
198 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
199 } else {
200 #if 0
201 printf("Got value of size %d\n", data.size);
202 #endif
203 if (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';
208 } else {
209 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
212 return res;
215 int ast_db_del(const char *family, const char *keys)
217 char fullkey[256];
218 DBT key;
219 int res, fullkeylen;
221 ast_mutex_lock(&dblock);
222 if (dbinit()) {
223 ast_mutex_unlock(&dblock);
224 return -1;
227 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
228 memset(&key, 0, sizeof(key));
229 key.data = fullkey;
230 key.size = fullkeylen + 1;
232 res = astdb->del(astdb, &key, 0);
233 astdb->sync(astdb, 0);
235 ast_mutex_unlock(&dblock);
237 if (res)
238 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
239 return res;
242 static int database_put(int fd, int argc, char *argv[])
244 int res;
245 if (argc != 5)
246 return RESULT_SHOWUSAGE;
247 res = ast_db_put(argv[2], argv[3], argv[4]);
248 if (res) {
249 ast_cli(fd, "Failed to update entry\n");
250 } else {
251 ast_cli(fd, "Updated database successfully\n");
253 return RESULT_SUCCESS;
256 static int database_get(int fd, int argc, char *argv[])
258 int res;
259 char tmp[256];
260 if (argc != 4)
261 return RESULT_SHOWUSAGE;
262 res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
263 if (res) {
264 ast_cli(fd, "Database entry not found.\n");
265 } else {
266 ast_cli(fd, "Value: %s\n", tmp);
268 return RESULT_SUCCESS;
271 static int database_del(int fd, int argc, char *argv[])
273 int res;
274 if (argc != 4)
275 return RESULT_SHOWUSAGE;
276 res = ast_db_del(argv[2], argv[3]);
277 if (res) {
278 ast_cli(fd, "Database entry does not exist.\n");
279 } else {
280 ast_cli(fd, "Database entry removed.\n");
282 return RESULT_SUCCESS;
285 static int database_deltree(int fd, int argc, char *argv[])
287 int res;
288 if ((argc < 3) || (argc > 4))
289 return RESULT_SHOWUSAGE;
290 if (argc == 4) {
291 res = ast_db_deltree(argv[2], argv[3]);
292 } else {
293 res = ast_db_deltree(argv[2], NULL);
295 if (res) {
296 ast_cli(fd, "Database entries do not exist.\n");
297 } else {
298 ast_cli(fd, "Database entries removed.\n");
300 return RESULT_SUCCESS;
303 static int database_show(int fd, int argc, char *argv[])
305 char prefix[256];
306 DBT key, data;
307 char *keys, *values;
308 int res;
309 int pass;
311 if (argc == 4) {
312 /* Family and key tree */
313 snprintf(prefix, sizeof(prefix), "/%s/%s", argv[2], argv[3]);
314 } else if (argc == 3) {
315 /* Family only */
316 snprintf(prefix, sizeof(prefix), "/%s", argv[2]);
317 } else if (argc == 2) {
318 /* Neither */
319 prefix[0] = '\0';
320 } else {
321 return RESULT_SHOWUSAGE;
323 ast_mutex_lock(&dblock);
324 if (dbinit()) {
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));
331 pass = 0;
332 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
333 if (key.size) {
334 keys = key.data;
335 keys[key.size - 1] = '\0';
336 } else {
337 keys = "<bad key>";
339 if (data.size) {
340 values = data.data;
341 values[data.size - 1]='\0';
342 } else {
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[])
355 char suffix[256];
356 DBT key, data;
357 char *keys, *values;
358 int res;
359 int pass;
361 if (argc == 3) {
362 /* Key only */
363 snprintf(suffix, sizeof(suffix), "/%s", argv[2]);
364 } else {
365 return RESULT_SHOWUSAGE;
367 ast_mutex_lock(&dblock);
368 if (dbinit()) {
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));
375 pass = 0;
376 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
377 if (key.size) {
378 keys = key.data;
379 keys[key.size - 1] = '\0';
380 } else {
381 keys = "<bad key>";
383 if (data.size) {
384 values = data.data;
385 values[data.size - 1]='\0';
386 } else {
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)
399 char prefix[256];
400 DBT key, data;
401 char *keys, *values;
402 int values_len;
403 int res;
404 int pass;
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);
412 } else {
413 /* Family only */
414 snprintf(prefix, sizeof(prefix), "/%s", family);
416 } else {
417 prefix[0] = '\0';
419 ast_mutex_lock(&dblock);
420 if (dbinit()) {
421 ast_mutex_unlock(&dblock);
422 ast_log(LOG_WARNING, "Database unavailable\n");
423 return NULL;
425 memset(&key, 0, sizeof(key));
426 memset(&data, 0, sizeof(data));
427 pass = 0;
428 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
429 if (key.size) {
430 keys = key.data;
431 keys[key.size - 1] = '\0';
432 } else {
433 keys = "<bad key>";
435 if (data.size) {
436 values = data.data;
437 values[data.size - 1] = '\0';
438 } else {
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))) {
443 cur->next = NULL;
444 cur->key = cur->data + values_len;
445 strcpy(cur->data, values);
446 strcpy(cur->key, keys);
447 if (last) {
448 last->next = cur;
449 } else {
450 ret = cur;
452 last = cur;
455 ast_mutex_unlock(&dblock);
456 return ret;
459 void ast_db_freetree(struct ast_db_entry *dbe)
461 struct ast_db_entry *last;
462 while (dbe) {
463 last = dbe;
464 dbe = dbe->next;
465 free(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"
486 "family and key.\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"
491 "family and key.\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");
521 int res;
523 if (ast_strlen_zero(family)) {
524 astman_send_error(s, m, "No family specified");
525 return 0;
527 if (ast_strlen_zero(key)) {
528 astman_send_error(s, m, "No key specified");
529 return 0;
531 if (ast_strlen_zero(val)) {
532 astman_send_error(s, m, "No val specified");
533 return 0;
536 res = ast_db_put(family, key, val);
537 if (res) {
538 astman_send_error(s, m, "Failed to update entry");
539 } else {
540 astman_send_ack(s, m, "Updated database successfully");
542 return 0;
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");
551 char tmp[256];
552 int res;
554 if (ast_strlen_zero(family)) {
555 astman_send_error(s, m, "No family specified.");
556 return 0;
558 if (ast_strlen_zero(key)) {
559 astman_send_error(s, m, "No key specified.");
560 return 0;
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));
567 if (res) {
568 astman_send_error(s, m, "Database entry not found");
569 } else {
570 astman_send_ack(s, m, "Result will follow");
571 astman_append(s, "Event: DBGetResponse\r\n"
572 "Family: %s\r\n"
573 "Key: %s\r\n"
574 "Val: %s\r\n"
575 "%s"
576 "\r\n",
577 family, key, tmp, idText);
579 return 0;
582 int astdb_init(void)
584 dbinit();
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");
593 return 0;