Merged revisions 286112 via svnmerge from
[asterisk/asterisk.git] / main / db.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief ASTdb Management
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  *
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 
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/_private.h"
35 #include "asterisk/paths.h"     /* use ast_config_AST_DB */
36 #include <sys/time.h>
37 #include <signal.h>
38 #include <dirent.h>
39
40 #include "asterisk/channel.h"
41 #include "asterisk/file.h"
42 #include "asterisk/app.h"
43 #include "asterisk/dsp.h"
44 #include "asterisk/astdb.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/manager.h"
49 #include "db1-ast/include/db.h"
50
51 /*** DOCUMENTATION
52         <manager name="DBGet" language="en_US">
53                 <synopsis>
54                         Get DB Entry.
55                 </synopsis>
56                 <syntax>
57                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
58                         <parameter name="Family" required="true" />
59                         <parameter name="Key" required="true" />
60                 </syntax>
61                 <description>
62                 </description>
63         </manager>
64         <manager name="DBPut" language="en_US">
65                 <synopsis>
66                         Put DB entry.
67                 </synopsis>
68                 <syntax>
69                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
70                         <parameter name="Family" required="true" />
71                         <parameter name="Key" required="true" />
72                         <parameter name="Val" />
73                 </syntax>
74                 <description>
75                 </description>
76         </manager>
77         <manager name="DBDel" language="en_US">
78                 <synopsis>
79                         Delete DB entry.
80                 </synopsis>
81                 <syntax>
82                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
83                         <parameter name="Family" required="true" />
84                         <parameter name="Key" required="true" />
85                 </syntax>
86                 <description>
87                 </description>
88         </manager>
89         <manager name="DBDelTree" language="en_US">
90                 <synopsis>
91                         Delete DB Tree.
92                 </synopsis>
93                 <syntax>
94                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
95                         <parameter name="Family" required="true" />
96                         <parameter name="Key" />
97                 </syntax>
98                 <description>
99                 </description>
100         </manager>
101  ***/
102
103 static DB *astdb;
104 AST_MUTEX_DEFINE_STATIC(dblock);
105 static ast_cond_t dbcond;
106
107 static void db_sync(void);
108
109 static int dbinit(void) 
110 {
111         if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
112                 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", ast_config_AST_DB, strerror(errno));
113                 return -1;
114         }
115         return 0;
116 }
117
118
119 static inline int keymatch(const char *key, const char *prefix)
120 {
121         int preflen = strlen(prefix);
122         if (!preflen)
123                 return 1;
124         if (!strcasecmp(key, prefix))
125                 return 1;
126         if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
127                 if (key[preflen] == '/')
128                         return 1;
129         }
130         return 0;
131 }
132
133 static inline int subkeymatch(const char *key, const char *suffix)
134 {
135         int suffixlen = strlen(suffix);
136         if (suffixlen) {
137                 const char *subkey = key + strlen(key) - suffixlen;
138                 if (subkey < key)
139                         return 0;
140                 if (!strcasecmp(subkey, suffix))
141                         return 1;
142         }
143         return 0;
144 }
145
146 int ast_db_deltree(const char *family, const char *keytree)
147 {
148         char prefix[256];
149         DBT key, data;
150         char *keys;
151         int res;
152         int pass;
153         int counter = 0;
154         
155         if (family) {
156                 if (keytree) {
157                         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
158                 } else {
159                         snprintf(prefix, sizeof(prefix), "/%s", family);
160                 }
161         } else if (keytree) {
162                 return -1;
163         } else {
164                 prefix[0] = '\0';
165         }
166         
167         ast_mutex_lock(&dblock);
168         if (dbinit()) {
169                 ast_mutex_unlock(&dblock);
170                 return -1;
171         }
172         
173         memset(&key, 0, sizeof(key));
174         memset(&data, 0, sizeof(data));
175         pass = 0;
176         while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
177                 if (key.size) {
178                         keys = key.data;
179                         keys[key.size - 1] = '\0';
180                 } else {
181                         keys = "<bad key>";
182                 }
183                 if (keymatch(keys, prefix)) {
184                         astdb->del(astdb, &key, 0);
185                         counter++;
186                 }
187         }
188         db_sync();
189         ast_mutex_unlock(&dblock);
190         return counter;
191 }
192
193 int ast_db_put(const char *family, const char *keys, const char *value)
194 {
195         char fullkey[256];
196         DBT key, data;
197         int res, fullkeylen;
198
199         ast_mutex_lock(&dblock);
200         if (dbinit()) {
201                 ast_mutex_unlock(&dblock);
202                 return -1;
203         }
204
205         fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
206         memset(&key, 0, sizeof(key));
207         memset(&data, 0, sizeof(data));
208         key.data = fullkey;
209         key.size = fullkeylen + 1;
210         data.data = (char *) value;
211         data.size = strlen(value) + 1;
212         res = astdb->put(astdb, &key, &data, 0);
213         db_sync();
214         ast_mutex_unlock(&dblock);
215         if (res)
216                 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
217         return res;
218 }
219
220 int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
221 {
222         char fullkey[256] = "";
223         DBT key, data;
224         int res, fullkeylen;
225
226         ast_mutex_lock(&dblock);
227         if (dbinit()) {
228                 ast_mutex_unlock(&dblock);
229                 return -1;
230         }
231
232         fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
233         memset(&key, 0, sizeof(key));
234         memset(&data, 0, sizeof(data));
235         memset(value, 0, valuelen);
236         key.data = fullkey;
237         key.size = fullkeylen + 1;
238
239         res = astdb->get(astdb, &key, &data, 0);
240
241         /* Be sure to NULL terminate our data either way */
242         if (res) {
243                 ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
244         } else {
245 #if 0
246                 printf("Got value of size %d\n", data.size);
247 #endif
248                 if (data.size) {
249                         ((char *)data.data)[data.size - 1] = '\0';
250                         /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
251                         ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
252                 } else {
253                         ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
254                 }
255         }
256
257         /* Data is not fully isolated for concurrency, so the lock must be extended
258          * to after the copy to the output buffer. */
259         ast_mutex_unlock(&dblock);
260
261         return res;
262 }
263
264 int ast_db_del(const char *family, const char *keys)
265 {
266         char fullkey[256];
267         DBT key;
268         int res, fullkeylen;
269
270         ast_mutex_lock(&dblock);
271         if (dbinit()) {
272                 ast_mutex_unlock(&dblock);
273                 return -1;
274         }
275         
276         fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
277         memset(&key, 0, sizeof(key));
278         key.data = fullkey;
279         key.size = fullkeylen + 1;
280         
281         res = astdb->del(astdb, &key, 0);
282         db_sync();
283         
284         ast_mutex_unlock(&dblock);
285
286         if (res) {
287                 ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
288         }
289         return res;
290 }
291
292 static char *handle_cli_database_put(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
293 {
294         int res;
295
296         switch (cmd) {
297         case CLI_INIT:
298                 e->command = "database put";
299                 e->usage =
300                         "Usage: database put <family> <key> <value>\n"
301                         "       Adds or updates an entry in the Asterisk database for\n"
302                         "       a given family, key, and value.\n";
303                 return NULL;
304         case CLI_GENERATE:
305                 return NULL;
306         }
307
308         if (a->argc != 5)
309                 return CLI_SHOWUSAGE;
310         res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
311         if (res)  {
312                 ast_cli(a->fd, "Failed to update entry\n");
313         } else {
314                 ast_cli(a->fd, "Updated database successfully\n");
315         }
316         return CLI_SUCCESS;
317 }
318
319 static char *handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
320 {
321         int res;
322         char tmp[256];
323
324         switch (cmd) {
325         case CLI_INIT:
326                 e->command = "database get";
327                 e->usage =
328                         "Usage: database get <family> <key>\n"
329                         "       Retrieves an entry in the Asterisk database for a given\n"
330                         "       family and key.\n";
331                 return NULL;
332         case CLI_GENERATE:
333                 return NULL;
334         }
335
336         if (a->argc != 4)
337                 return CLI_SHOWUSAGE;
338         res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
339         if (res) {
340                 ast_cli(a->fd, "Database entry not found.\n");
341         } else {
342                 ast_cli(a->fd, "Value: %s\n", tmp);
343         }
344         return CLI_SUCCESS;
345 }
346
347 static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
348 {
349         int res;
350
351         switch (cmd) {
352         case CLI_INIT:
353                 e->command = "database del";
354                 e->usage =
355                         "Usage: database del <family> <key>\n"
356                         "       Deletes an entry in the Asterisk database for a given\n"
357                         "       family and key.\n";
358                 return NULL;
359         case CLI_GENERATE:
360                 return NULL;
361         }
362
363         if (a->argc != 4)
364                 return CLI_SHOWUSAGE;
365         res = ast_db_del(a->argv[2], a->argv[3]);
366         if (res) {
367                 ast_cli(a->fd, "Database entry does not exist.\n");
368         } else {
369                 ast_cli(a->fd, "Database entry removed.\n");
370         }
371         return CLI_SUCCESS;
372 }
373
374 static char *handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
375 {
376         int res;
377
378         switch (cmd) {
379         case CLI_INIT:
380                 e->command = "database deltree";
381                 e->usage =
382                         "Usage: database deltree <family> [keytree]\n"
383                         "       Deletes a family or specific keytree within a family\n"
384                         "       in the Asterisk database.\n";
385                 return NULL;
386         case CLI_GENERATE:
387                 return NULL;
388         }
389
390         if ((a->argc < 3) || (a->argc > 4))
391                 return CLI_SHOWUSAGE;
392         if (a->argc == 4) {
393                 res = ast_db_deltree(a->argv[2], a->argv[3]);
394         } else {
395                 res = ast_db_deltree(a->argv[2], NULL);
396         }
397         if (res < 0) {
398                 ast_cli(a->fd, "Database entries do not exist.\n");
399         } else {
400                 ast_cli(a->fd, "%d database entries removed.\n",res);
401         }
402         return CLI_SUCCESS;
403 }
404
405 static char *handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
406 {
407         char prefix[256];
408         DBT key, data;
409         char *keys, *values;
410         int res;
411         int pass;
412         int counter = 0;
413
414         switch (cmd) {
415         case CLI_INIT:
416                 e->command = "database show";
417                 e->usage =
418                         "Usage: database show [family [keytree]]\n"
419                         "       Shows Asterisk database contents, optionally restricted\n"
420                         "       to a given family, or family and keytree.\n";
421                 return NULL;
422         case CLI_GENERATE:
423                 return NULL;
424         }
425
426         if (a->argc == 4) {
427                 /* Family and key tree */
428                 snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
429         } else if (a->argc == 3) {
430                 /* Family only */
431                 snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
432         } else if (a->argc == 2) {
433                 /* Neither */
434                 prefix[0] = '\0';
435         } else {
436                 return CLI_SHOWUSAGE;
437         }
438         ast_mutex_lock(&dblock);
439         if (dbinit()) {
440                 ast_mutex_unlock(&dblock);
441                 ast_cli(a->fd, "Database unavailable\n");
442                 return CLI_SUCCESS;     
443         }
444         memset(&key, 0, sizeof(key));
445         memset(&data, 0, sizeof(data));
446         pass = 0;
447         while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
448                 if (key.size) {
449                         keys = key.data;
450                         keys[key.size - 1] = '\0';
451                 } else {
452                         keys = "<bad key>";
453                 }
454                 if (data.size) {
455                         values = data.data;
456                         values[data.size - 1]='\0';
457                 } else {
458                         values = "<bad value>";
459                 }
460                 if (keymatch(keys, prefix)) {
461                         ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
462                         counter++;
463                 }
464         }
465         ast_mutex_unlock(&dblock);
466         ast_cli(a->fd, "%d results found.\n", counter);
467         return CLI_SUCCESS;     
468 }
469
470 static char *handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
471 {
472         char suffix[256];
473         DBT key, data;
474         char *keys, *values;
475         int res;
476         int pass;
477         int counter = 0;
478
479         switch (cmd) {
480         case CLI_INIT:
481                 e->command = "database showkey";
482                 e->usage =
483                         "Usage: database showkey <keytree>\n"
484                         "       Shows Asterisk database contents, restricted to a given key.\n";
485                 return NULL;
486         case CLI_GENERATE:
487                 return NULL;
488         }
489
490         if (a->argc == 3) {
491                 /* Key only */
492                 snprintf(suffix, sizeof(suffix), "/%s", a->argv[2]);
493         } else {
494                 return CLI_SHOWUSAGE;
495         }
496         ast_mutex_lock(&dblock);
497         if (dbinit()) {
498                 ast_mutex_unlock(&dblock);
499                 ast_cli(a->fd, "Database unavailable\n");
500                 return CLI_SUCCESS;     
501         }
502         memset(&key, 0, sizeof(key));
503         memset(&data, 0, sizeof(data));
504         pass = 0;
505         while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
506                 if (key.size) {
507                         keys = key.data;
508                         keys[key.size - 1] = '\0';
509                 } else {
510                         keys = "<bad key>";
511                 }
512                 if (data.size) {
513                         values = data.data;
514                         values[data.size - 1]='\0';
515                 } else {
516                         values = "<bad value>";
517                 }
518                 if (subkeymatch(keys, suffix)) {
519                         ast_cli(a->fd, "%-50s: %-25s\n", keys, values);
520                         counter++;
521                 }
522         }
523         ast_mutex_unlock(&dblock);
524         ast_cli(a->fd, "%d results found.\n", counter);
525         return CLI_SUCCESS;     
526 }
527
528 struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
529 {
530         char prefix[256];
531         DBT key, data;
532         char *keys, *values;
533         int values_len;
534         int res;
535         int pass;
536         struct ast_db_entry *last = NULL;
537         struct ast_db_entry *cur, *ret=NULL;
538
539         if (!ast_strlen_zero(family)) {
540                 if (!ast_strlen_zero(keytree)) {
541                         /* Family and key tree */
542                         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
543                 } else {
544                         /* Family only */
545                         snprintf(prefix, sizeof(prefix), "/%s", family);
546                 }
547         } else {
548                 prefix[0] = '\0';
549         }
550         ast_mutex_lock(&dblock);
551         if (dbinit()) {
552                 ast_mutex_unlock(&dblock);
553                 ast_log(LOG_WARNING, "Database unavailable\n");
554                 return NULL;    
555         }
556         memset(&key, 0, sizeof(key));
557         memset(&data, 0, sizeof(data));
558         pass = 0;
559         while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
560                 if (key.size) {
561                         keys = key.data;
562                         keys[key.size - 1] = '\0';
563                 } else {
564                         keys = "<bad key>";
565                 }
566                 if (data.size) {
567                         values = data.data;
568                         values[data.size - 1] = '\0';
569                 } else {
570                         values = "<bad value>";
571                 }
572                 values_len = strlen(values) + 1;
573                 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
574                         cur->next = NULL;
575                         cur->key = cur->data + values_len;
576                         strcpy(cur->data, values);
577                         strcpy(cur->key, keys);
578                         if (last) {
579                                 last->next = cur;
580                         } else {
581                                 ret = cur;
582                         }
583                         last = cur;
584                 }
585         }
586         ast_mutex_unlock(&dblock);
587         return ret;     
588 }
589
590 void ast_db_freetree(struct ast_db_entry *dbe)
591 {
592         struct ast_db_entry *last;
593         while (dbe) {
594                 last = dbe;
595                 dbe = dbe->next;
596                 ast_free(last);
597         }
598 }
599
600 static struct ast_cli_entry cli_database[] = {
601         AST_CLI_DEFINE(handle_cli_database_show,    "Shows database contents"),
602         AST_CLI_DEFINE(handle_cli_database_showkey, "Shows database contents"),
603         AST_CLI_DEFINE(handle_cli_database_get,     "Gets database value"),
604         AST_CLI_DEFINE(handle_cli_database_put,     "Adds/updates database value"),
605         AST_CLI_DEFINE(handle_cli_database_del,     "Removes database key/value"),
606         AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database keytree/values")
607 };
608
609 static int manager_dbput(struct mansession *s, const struct message *m)
610 {
611         const char *family = astman_get_header(m, "Family");
612         const char *key = astman_get_header(m, "Key");
613         const char *val = astman_get_header(m, "Val");
614         int res;
615
616         if (ast_strlen_zero(family)) {
617                 astman_send_error(s, m, "No family specified");
618                 return 0;
619         }
620         if (ast_strlen_zero(key)) {
621                 astman_send_error(s, m, "No key specified");
622                 return 0;
623         }
624
625         res = ast_db_put(family, key, S_OR(val, ""));
626         if (res) {
627                 astman_send_error(s, m, "Failed to update entry");
628         } else {
629                 astman_send_ack(s, m, "Updated database successfully");
630         }
631         return 0;
632 }
633
634 static int manager_dbget(struct mansession *s, const struct message *m)
635 {
636         const char *id = astman_get_header(m,"ActionID");
637         char idText[256] = "";
638         const char *family = astman_get_header(m, "Family");
639         const char *key = astman_get_header(m, "Key");
640         char tmp[256];
641         int res;
642
643         if (ast_strlen_zero(family)) {
644                 astman_send_error(s, m, "No family specified.");
645                 return 0;
646         }
647         if (ast_strlen_zero(key)) {
648                 astman_send_error(s, m, "No key specified.");
649                 return 0;
650         }
651
652         if (!ast_strlen_zero(id))
653                 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
654
655         res = ast_db_get(family, key, tmp, sizeof(tmp));
656         if (res) {
657                 astman_send_error(s, m, "Database entry not found");
658         } else {
659                 astman_send_ack(s, m, "Result will follow");
660                 astman_append(s, "Event: DBGetResponse\r\n"
661                                 "Family: %s\r\n"
662                                 "Key: %s\r\n"
663                                 "Val: %s\r\n"
664                                 "%s"
665                                 "\r\n",
666                                 family, key, tmp, idText);
667                 astman_append(s, "Event: DBGetComplete\r\n"
668                                 "%s"
669                                 "\r\n",
670                                 idText);
671         }
672         return 0;
673 }
674
675 static int manager_dbdel(struct mansession *s, const struct message *m)
676 {
677         const char *family = astman_get_header(m, "Family");
678         const char *key = astman_get_header(m, "Key");
679         int res;
680
681         if (ast_strlen_zero(family)) {
682                 astman_send_error(s, m, "No family specified.");
683                 return 0;
684         }
685
686         if (ast_strlen_zero(key)) {
687                 astman_send_error(s, m, "No key specified.");
688                 return 0;
689         }
690
691         res = ast_db_del(family, key);
692         if (res)
693                 astman_send_error(s, m, "Database entry not found");
694         else
695                 astman_send_ack(s, m, "Key deleted successfully");
696
697         return 0;
698 }
699
700 static int manager_dbdeltree(struct mansession *s, const struct message *m)
701 {
702         const char *family = astman_get_header(m, "Family");
703         const char *key = astman_get_header(m, "Key");
704         int res;
705
706         if (ast_strlen_zero(family)) {
707                 astman_send_error(s, m, "No family specified.");
708                 return 0;
709         }
710
711         if (!ast_strlen_zero(key))
712                 res = ast_db_deltree(family, key);
713         else
714                 res = ast_db_deltree(family, NULL);
715
716         if (res < 0)
717                 astman_send_error(s, m, "Database entry not found");
718         else
719                 astman_send_ack(s, m, "Key tree deleted successfully");
720         
721         return 0;
722 }
723
724 /*!
725  * \internal
726  * \brief Signal the astdb sync thread to do its thing.
727  *
728  * \note dblock is assumed to be held when calling this function.
729  */
730 static void db_sync(void)
731 {
732         ast_cond_signal(&dbcond);
733 }
734
735 /*!
736  * \internal
737  * \brief astdb sync thread
738  *
739  * This thread is in charge of syncing astdb to disk after a change.
740  * By pushing it off to this thread to take care of, this I/O bound operation
741  * will not block other threads from performing other critical processing.
742  * If changes happen rapidly, this thread will also ensure that the sync
743  * operations are rate limited.
744  */
745 static void *db_sync_thread(void *data)
746 {
747         ast_mutex_lock(&dblock);
748         for (;;) {
749                 ast_cond_wait(&dbcond, &dblock);
750                 ast_mutex_unlock(&dblock);
751                 sleep(1);
752                 ast_mutex_lock(&dblock);
753                 astdb->sync(astdb, 0);
754         }
755
756         return NULL;
757 }
758
759 int astdb_init(void)
760 {
761         pthread_t dont_care;
762
763         ast_cond_init(&dbcond, NULL);
764         if (ast_pthread_create_background(&dont_care, NULL, db_sync_thread, NULL)) {
765                 return -1;
766         }
767
768         dbinit();
769         ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
770         ast_manager_register_xml("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);
771         ast_manager_register_xml("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);
772         ast_manager_register_xml("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel);
773         ast_manager_register_xml("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree);
774         return 0;
775 }