Improve documentation by making all of the colors used readable,
[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 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/_private.h"
39 #include "asterisk/paths.h"     /* use ast_config_AST_DB */
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <signal.h>
45 #include <dirent.h>
46 #include <sqlite3.h>
47
48 #include "asterisk/channel.h"
49 #include "asterisk/file.h"
50 #include "asterisk/app.h"
51 #include "asterisk/dsp.h"
52 #include "asterisk/astdb.h"
53 #include "asterisk/cli.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/manager.h"
56
57 /*** DOCUMENTATION
58         <manager name="DBGet" language="en_US">
59                 <synopsis>
60                         Get DB Entry.
61                 </synopsis>
62                 <syntax>
63                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
64                         <parameter name="Family" required="true" />
65                         <parameter name="Key" required="true" />
66                 </syntax>
67                 <description>
68                 </description>
69         </manager>
70         <manager name="DBPut" language="en_US">
71                 <synopsis>
72                         Put DB entry.
73                 </synopsis>
74                 <syntax>
75                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
76                         <parameter name="Family" required="true" />
77                         <parameter name="Key" required="true" />
78                         <parameter name="Val" />
79                 </syntax>
80                 <description>
81                 </description>
82         </manager>
83         <manager name="DBDel" language="en_US">
84                 <synopsis>
85                         Delete DB entry.
86                 </synopsis>
87                 <syntax>
88                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
89                         <parameter name="Family" required="true" />
90                         <parameter name="Key" required="true" />
91                 </syntax>
92                 <description>
93                 </description>
94         </manager>
95         <manager name="DBDelTree" language="en_US">
96                 <synopsis>
97                         Delete DB Tree.
98                 </synopsis>
99                 <syntax>
100                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
101                         <parameter name="Family" required="true" />
102                         <parameter name="Key" />
103                 </syntax>
104                 <description>
105                 </description>
106         </manager>
107  ***/
108
109 #define MAX_DB_FIELD 256
110 AST_MUTEX_DEFINE_STATIC(dblock);
111 static ast_cond_t dbcond;
112 static sqlite3 *astdb;
113 static pthread_t syncthread;
114 static int doexit;
115
116 static void db_sync(void);
117
118 #define DEFINE_SQL_STATEMENT(stmt,sql) static sqlite3_stmt *stmt; \
119         const char stmt##_sql[] = sql;
120
121 DEFINE_SQL_STATEMENT(put_stmt, "INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)")
122 DEFINE_SQL_STATEMENT(get_stmt, "SELECT value FROM astdb WHERE key=?")
123 DEFINE_SQL_STATEMENT(del_stmt, "DELETE FROM astdb WHERE key=?")
124 DEFINE_SQL_STATEMENT(deltree_stmt, "DELETE FROM astdb WHERE key || '/' LIKE ? || '/' || '%'")
125 DEFINE_SQL_STATEMENT(deltree_all_stmt, "DELETE FROM astdb")
126 DEFINE_SQL_STATEMENT(gettree_stmt, "SELECT key, value FROM astdb WHERE key || '/' LIKE ? || '/' || '%' ORDER BY key")
127 DEFINE_SQL_STATEMENT(gettree_all_stmt, "SELECT key, value FROM astdb ORDER BY key")
128 DEFINE_SQL_STATEMENT(showkey_stmt, "SELECT key, value FROM astdb WHERE key LIKE '%' || '/' || ? ORDER BY key")
129 DEFINE_SQL_STATEMENT(create_astdb_stmt, "CREATE TABLE IF NOT EXISTS astdb(key VARCHAR(256), value VARCHAR(256), PRIMARY KEY(key))")
130
131 static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len)
132 {
133         ast_mutex_lock(&dblock);
134         if (sqlite3_prepare(astdb, sql, len, stmt, NULL) != SQLITE_OK) {
135                 ast_log(LOG_WARNING, "Couldn't prepare statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
136                 ast_mutex_unlock(&dblock);
137                 return -1;
138         }
139         ast_mutex_unlock(&dblock);
140
141         return 0;
142 }
143
144 /*! \internal
145  * \brief Clean up the prepared SQLite3 statement
146  * \note dblock should already be locked prior to calling this method
147  */
148 static int clean_stmt(sqlite3_stmt *stmt, const char *sql)
149 {
150         if (sqlite3_finalize(stmt) != SQLITE_OK) {
151                 ast_log(LOG_WARNING, "Couldn't finalize statement '%s': %s\n", sql, sqlite3_errmsg(astdb));
152                 return -1;
153         }
154         return 0;
155 }
156
157 /*! \internal
158  * \brief Clean up all prepared SQLite3 statements
159  * \note dblock should already be locked prior to calling this method
160  */
161 static void clean_statements(void)
162 {
163         clean_stmt(get_stmt, get_stmt_sql);
164         clean_stmt(del_stmt, del_stmt_sql);
165         clean_stmt(deltree_stmt, deltree_stmt_sql);
166         clean_stmt(deltree_all_stmt, deltree_all_stmt_sql);
167         clean_stmt(gettree_stmt, gettree_stmt_sql);
168         clean_stmt(gettree_all_stmt, gettree_all_stmt_sql);
169         clean_stmt(showkey_stmt, showkey_stmt_sql);
170         clean_stmt(put_stmt, put_stmt_sql);
171         clean_stmt(create_astdb_stmt, create_astdb_stmt_sql);
172 }
173
174 static int init_statements(void)
175 {
176         /* Don't initialize create_astdb_statment here as the astdb table needs to exist
177          * brefore these statments can be initialized */
178         return init_stmt(&get_stmt, get_stmt_sql, sizeof(get_stmt_sql))
179         || init_stmt(&del_stmt, del_stmt_sql, sizeof(del_stmt_sql))
180         || init_stmt(&deltree_stmt, deltree_stmt_sql, sizeof(deltree_stmt_sql))
181         || init_stmt(&deltree_all_stmt, deltree_all_stmt_sql, sizeof(deltree_all_stmt_sql))
182         || init_stmt(&gettree_stmt, gettree_stmt_sql, sizeof(gettree_stmt_sql))
183         || init_stmt(&gettree_all_stmt, gettree_all_stmt_sql, sizeof(gettree_all_stmt_sql))
184         || init_stmt(&showkey_stmt, showkey_stmt_sql, sizeof(showkey_stmt_sql))
185         || init_stmt(&put_stmt, put_stmt_sql, sizeof(put_stmt_sql));
186 }
187
188 static int convert_bdb_to_sqlite3(void)
189 {
190         char *cmd;
191         int res;
192
193         ast_asprintf(&cmd, "%s/astdb2sqlite3 '%s'\n", ast_config_AST_SBIN_DIR, ast_config_AST_DB);
194         res = ast_safe_system(cmd);
195         ast_free(cmd);
196
197         return res;
198 }
199
200 static int db_create_astdb(void)
201 {
202         int res = 0;
203
204         if (!create_astdb_stmt) {
205                 init_stmt(&create_astdb_stmt, create_astdb_stmt_sql, sizeof(create_astdb_stmt_sql));
206         }
207
208         ast_mutex_lock(&dblock);
209         if (sqlite3_step(create_astdb_stmt) != SQLITE_DONE) {
210                 ast_log(LOG_WARNING, "Couldn't create astdb table: %s\n", sqlite3_errmsg(astdb));
211                 res = -1;
212         }
213         sqlite3_reset(create_astdb_stmt);
214         db_sync();
215         ast_mutex_unlock(&dblock);
216
217         return res;
218 }
219
220 static int db_open(void)
221 {
222         char *dbname;
223         struct stat dont_care;
224
225         if (!(dbname = ast_alloca(strlen(ast_config_AST_DB) + sizeof(".sqlite3")))) {
226                 return -1;
227         }
228         strcpy(dbname, ast_config_AST_DB);
229         strcat(dbname, ".sqlite3");
230
231         if (stat(dbname, &dont_care) && !stat(ast_config_AST_DB, &dont_care)) {
232                 if (convert_bdb_to_sqlite3()) {
233                         ast_log(LOG_ERROR, "*** Database conversion failed!\n");
234                         ast_log(LOG_ERROR, "*** Asterisk now uses SQLite3 for its internal\n");
235                         ast_log(LOG_ERROR, "*** database. Conversion from the old astdb\n");
236                         ast_log(LOG_ERROR, "*** failed. Most likely the astdb2sqlite3 utility\n");
237                         ast_log(LOG_ERROR, "*** was not selected for build. To convert the\n");
238                         ast_log(LOG_ERROR, "*** old astdb, please delete '%s'\n", dbname);
239                         ast_log(LOG_ERROR, "*** and re-run 'make menuselect' and select astdb2sqlite3\n");
240                         ast_log(LOG_ERROR, "*** in the Utilities section, then 'make && make install'.\n");
241                         ast_log(LOG_ERROR, "*** It is also imperative that the user under which\n");
242                         ast_log(LOG_ERROR, "*** Asterisk runs have write permission to the directory\n");
243                         ast_log(LOG_ERROR, "*** where the database resides.\n");
244                         sleep(5);
245                 } else {
246                         ast_log(LOG_NOTICE, "Database conversion succeeded!\n");
247                 }
248         }
249
250         ast_mutex_lock(&dblock);
251         if (sqlite3_open(dbname, &astdb) != SQLITE_OK) {
252                 ast_log(LOG_WARNING, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(astdb));
253                 sqlite3_close(astdb);
254                 ast_mutex_unlock(&dblock);
255                 return -1;
256         }
257         ast_mutex_unlock(&dblock);
258
259         return 0;
260 }
261
262 static int db_init(void)
263 {
264         if (astdb) {
265                 return 0;
266         }
267
268         if (db_open() || db_create_astdb() || init_statements()) {
269                 return -1;
270         }
271
272         return 0;
273 }
274
275 /* We purposely don't lock around the sqlite3 call because the transaction
276  * calls will be called with the database lock held. For any other use, make
277  * sure to take the dblock yourself. */
278 static int db_execute_sql(const char *sql, int (*callback)(void *, int, char **, char **), void *arg)
279 {
280         char *errmsg = NULL;
281         int res =0;
282
283         sqlite3_exec(astdb, sql, callback, arg, &errmsg);
284         if (errmsg) {
285                 ast_log(LOG_WARNING, "Error executing SQL: %s\n", errmsg);
286                 sqlite3_free(errmsg);
287                 res = -1;
288         }
289
290         return res;
291 }
292
293 static int ast_db_begin_transaction(void)
294 {
295         return db_execute_sql("BEGIN TRANSACTION", NULL, NULL);
296 }
297
298 static int ast_db_commit_transaction(void)
299 {
300         return db_execute_sql("COMMIT", NULL, NULL);
301 }
302
303 static int ast_db_rollback_transaction(void)
304 {
305         return db_execute_sql("ROLLBACK", NULL, NULL);
306 }
307
308 int ast_db_put(const char *family, const char *key, const char *value)
309 {
310         char fullkey[MAX_DB_FIELD];
311         size_t fullkey_len;
312         int res = 0;
313
314         if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
315                 ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
316                 return -1;
317         }
318
319         fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
320
321         ast_mutex_lock(&dblock);
322         if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
323                 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
324                 res = -1;
325         } else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
326                 ast_log(LOG_WARNING, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
327                 res = -1;
328         } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
329                 ast_log(LOG_WARNING, "Couldn't execute statment: %s\n", sqlite3_errmsg(astdb));
330                 res = -1;
331         }
332
333         sqlite3_reset(put_stmt);
334         db_sync();
335         ast_mutex_unlock(&dblock);
336
337         return res;
338 }
339
340 /*!
341  * \internal
342  * \brief Get key value specified by family/key.
343  *
344  * Gets the value associated with the specified \a family and \a key, and
345  * stores it, either into the fixed sized buffer specified by \a buffer
346  * and \a bufferlen, or as a heap allocated string if \a bufferlen is -1.
347  *
348  * \note If \a bufferlen is -1, \a buffer points to heap allocated memory
349  *       and must be freed by calling ast_free().
350  *
351  * \retval -1 An error occurred
352  * \retval 0 Success
353  */
354 static int db_get_common(const char *family, const char *key, char **buffer, int bufferlen)
355 {
356         const unsigned char *result;
357         char fullkey[MAX_DB_FIELD];
358         size_t fullkey_len;
359         int res = 0;
360
361         if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
362                 ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
363                 return -1;
364         }
365
366         fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
367
368         ast_mutex_lock(&dblock);
369         if (sqlite3_bind_text(get_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
370                 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
371                 res = -1;
372         } else if (sqlite3_step(get_stmt) != SQLITE_ROW) {
373                 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
374                 res = -1;
375         } else if (!(result = sqlite3_column_text(get_stmt, 0))) {
376                 ast_log(LOG_WARNING, "Couldn't get value\n");
377                 res = -1;
378         } else {
379                 const char *value = (const char *) result;
380
381                 if (bufferlen == -1) {
382                         *buffer = ast_strdup(value);
383                 } else {
384                         ast_copy_string(*buffer, value, bufferlen);
385                 }
386         }
387         sqlite3_reset(get_stmt);
388         ast_mutex_unlock(&dblock);
389
390         return res;
391 }
392
393 int ast_db_get(const char *family, const char *key, char *value, int valuelen)
394 {
395         ast_assert(value != NULL);
396
397         /* Make sure we initialize */
398         value[0] = 0;
399
400         return db_get_common(family, key, &value, valuelen);
401 }
402
403 int ast_db_get_allocated(const char *family, const char *key, char **out)
404 {
405         *out = NULL;
406
407         return db_get_common(family, key, out, -1);
408 }
409
410 int ast_db_del(const char *family, const char *key)
411 {
412         char fullkey[MAX_DB_FIELD];
413         size_t fullkey_len;
414         int res = 0;
415
416         if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
417                 ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
418                 return -1;
419         }
420
421         fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
422
423         ast_mutex_lock(&dblock);
424         if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
425                 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
426                 res = -1;
427         } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
428                 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
429                 res = -1;
430         }
431         sqlite3_reset(del_stmt);
432         db_sync();
433         ast_mutex_unlock(&dblock);
434
435         return res;
436 }
437
438 int ast_db_deltree(const char *family, const char *keytree)
439 {
440         sqlite3_stmt *stmt = deltree_stmt;
441         char prefix[MAX_DB_FIELD];
442         int res = 0;
443
444         if (!ast_strlen_zero(family)) {
445                 if (!ast_strlen_zero(keytree)) {
446                         /* Family and key tree */
447                         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
448                 } else {
449                         /* Family only */
450                         snprintf(prefix, sizeof(prefix), "/%s", family);
451                 }
452         } else {
453                 prefix[0] = '\0';
454                 stmt = deltree_all_stmt;
455         }
456
457         ast_mutex_lock(&dblock);
458         if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
459                 ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
460                 res = -1;
461         } else if (sqlite3_step(stmt) != SQLITE_DONE) {
462                 ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
463                 res = -1;
464         }
465         res = sqlite3_changes(astdb);
466         sqlite3_reset(stmt);
467         db_sync();
468         ast_mutex_unlock(&dblock);
469
470         return res;
471 }
472
473 struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
474 {
475         char prefix[MAX_DB_FIELD];
476         sqlite3_stmt *stmt = gettree_stmt;
477         struct ast_db_entry *cur, *last = NULL, *ret = NULL;
478
479         if (!ast_strlen_zero(family)) {
480                 if (!ast_strlen_zero(keytree)) {
481                         /* Family and key tree */
482                         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
483                 } else {
484                         /* Family only */
485                         snprintf(prefix, sizeof(prefix), "/%s", family);
486                 }
487         } else {
488                 prefix[0] = '\0';
489                 stmt = gettree_all_stmt;
490         }
491
492         ast_mutex_lock(&dblock);
493         if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
494                 ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
495                 sqlite3_reset(stmt);
496                 ast_mutex_unlock(&dblock);
497                 return NULL;
498         }
499
500         while (sqlite3_step(stmt) == SQLITE_ROW) {
501                 const char *key_s, *value_s;
502                 if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
503                         break;
504                 }
505                 if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
506                         break;
507                 }
508                 if (!(cur = ast_malloc(sizeof(*cur) + strlen(key_s) + strlen(value_s) + 2))) {
509                         break;
510                 }
511                 cur->next = NULL;
512                 cur->key = cur->data + strlen(value_s) + 1;
513                 strcpy(cur->data, value_s);
514                 strcpy(cur->key, key_s);
515                 if (last) {
516                         last->next = cur;
517                 } else {
518                         ret = cur;
519                 }
520                 last = cur;
521         }
522         sqlite3_reset(stmt);
523         ast_mutex_unlock(&dblock);
524
525         return ret;
526 }
527
528 void ast_db_freetree(struct ast_db_entry *dbe)
529 {
530         struct ast_db_entry *last;
531         while (dbe) {
532                 last = dbe;
533                 dbe = dbe->next;
534                 ast_free(last);
535         }
536 }
537
538 static char *handle_cli_database_put(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
539 {
540         int res;
541
542         switch (cmd) {
543         case CLI_INIT:
544                 e->command = "database put";
545                 e->usage =
546                         "Usage: database put <family> <key> <value>\n"
547                         "       Adds or updates an entry in the Asterisk database for\n"
548                         "       a given family, key, and value.\n";
549                 return NULL;
550         case CLI_GENERATE:
551                 return NULL;
552         }
553
554         if (a->argc != 5)
555                 return CLI_SHOWUSAGE;
556         res = ast_db_put(a->argv[2], a->argv[3], a->argv[4]);
557         if (res)  {
558                 ast_cli(a->fd, "Failed to update entry\n");
559         } else {
560                 ast_cli(a->fd, "Updated database successfully\n");
561         }
562         return CLI_SUCCESS;
563 }
564
565 static char *handle_cli_database_get(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
566 {
567         int res;
568         char tmp[MAX_DB_FIELD];
569
570         switch (cmd) {
571         case CLI_INIT:
572                 e->command = "database get";
573                 e->usage =
574                         "Usage: database get <family> <key>\n"
575                         "       Retrieves an entry in the Asterisk database for a given\n"
576                         "       family and key.\n";
577                 return NULL;
578         case CLI_GENERATE:
579                 return NULL;
580         }
581
582         if (a->argc != 4)
583                 return CLI_SHOWUSAGE;
584         res = ast_db_get(a->argv[2], a->argv[3], tmp, sizeof(tmp));
585         if (res) {
586                 ast_cli(a->fd, "Database entry not found.\n");
587         } else {
588                 ast_cli(a->fd, "Value: %s\n", tmp);
589         }
590         return CLI_SUCCESS;
591 }
592
593 static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
594 {
595         int res;
596
597         switch (cmd) {
598         case CLI_INIT:
599                 e->command = "database del";
600                 e->usage =
601                         "Usage: database del <family> <key>\n"
602                         "       Deletes an entry in the Asterisk database for a given\n"
603                         "       family and key.\n";
604                 return NULL;
605         case CLI_GENERATE:
606                 return NULL;
607         }
608
609         if (a->argc != 4)
610                 return CLI_SHOWUSAGE;
611         res = ast_db_del(a->argv[2], a->argv[3]);
612         if (res) {
613                 ast_cli(a->fd, "Database entry does not exist.\n");
614         } else {
615                 ast_cli(a->fd, "Database entry removed.\n");
616         }
617         return CLI_SUCCESS;
618 }
619
620 static char *handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
621 {
622         int num_deleted;
623
624         switch (cmd) {
625         case CLI_INIT:
626                 e->command = "database deltree";
627                 e->usage =
628                         "Usage: database deltree <family> [keytree]\n"
629                         "   OR: database deltree <family>[/keytree]\n"
630                         "       Deletes a family or specific keytree within a family\n"
631                         "       in the Asterisk database.  The two arguments may be\n"
632                         "       separated by either a space or a slash.\n";
633                 return NULL;
634         case CLI_GENERATE:
635                 return NULL;
636         }
637
638         if ((a->argc < 3) || (a->argc > 4))
639                 return CLI_SHOWUSAGE;
640         if (a->argc == 4) {
641                 num_deleted = ast_db_deltree(a->argv[2], a->argv[3]);
642         } else {
643                 num_deleted = ast_db_deltree(a->argv[2], NULL);
644         }
645         if (num_deleted < 0) {
646                 ast_cli(a->fd, "Database unavailable.\n");
647         } else if (num_deleted == 0) {
648                 ast_cli(a->fd, "Database entries do not exist.\n");
649         } else {
650                 ast_cli(a->fd, "%d database entries removed.\n",num_deleted);
651         }
652         return CLI_SUCCESS;
653 }
654
655 static char *handle_cli_database_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
656 {
657         char prefix[MAX_DB_FIELD];
658         int counter = 0;
659         sqlite3_stmt *stmt = gettree_stmt;
660
661         switch (cmd) {
662         case CLI_INIT:
663                 e->command = "database show";
664                 e->usage =
665                         "Usage: database show [family [keytree]]\n"
666                         "   OR: database show [family[/keytree]]\n"
667                         "       Shows Asterisk database contents, optionally restricted\n"
668                         "       to a given family, or family and keytree. The two arguments\n"
669                         "       may be separated either by a space or by a slash.\n";
670                 return NULL;
671         case CLI_GENERATE:
672                 return NULL;
673         }
674
675         if (a->argc == 4) {
676                 /* Family and key tree */
677                 snprintf(prefix, sizeof(prefix), "/%s/%s", a->argv[2], a->argv[3]);
678         } else if (a->argc == 3) {
679                 /* Family only */
680                 snprintf(prefix, sizeof(prefix), "/%s", a->argv[2]);
681         } else if (a->argc == 2) {
682                 /* Neither */
683                 prefix[0] = '\0';
684                 stmt = gettree_all_stmt;
685
686         } else {
687                 return CLI_SHOWUSAGE;
688         }
689
690         ast_mutex_lock(&dblock);
691         if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
692                 ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
693                 sqlite3_reset(stmt);
694                 ast_mutex_unlock(&dblock);
695                 return NULL;
696         }
697
698         while (sqlite3_step(stmt) == SQLITE_ROW) {
699                 const char *key_s, *value_s;
700                 if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
701                         ast_log(LOG_WARNING, "Skipping invalid key!\n");
702                         continue;
703                 }
704                 if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
705                         ast_log(LOG_WARNING, "Skipping invalid value!\n");
706                         continue;
707                 }
708                 ++counter;
709                 ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
710         }
711
712         sqlite3_reset(stmt);
713         ast_mutex_unlock(&dblock);
714
715         ast_cli(a->fd, "%d results found.\n", counter);
716         return CLI_SUCCESS;
717 }
718
719 static char *handle_cli_database_showkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
720 {
721         int counter = 0;
722
723         switch (cmd) {
724         case CLI_INIT:
725                 e->command = "database showkey";
726                 e->usage =
727                         "Usage: database showkey <keytree>\n"
728                         "       Shows Asterisk database contents, restricted to a given key.\n";
729                 return NULL;
730         case CLI_GENERATE:
731                 return NULL;
732         }
733
734         if (a->argc != 3) {
735                 return CLI_SHOWUSAGE;
736         }
737
738         ast_mutex_lock(&dblock);
739         if (!ast_strlen_zero(a->argv[2]) && (sqlite3_bind_text(showkey_stmt, 1, a->argv[2], -1, SQLITE_STATIC) != SQLITE_OK)) {
740                 ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", a->argv[2], sqlite3_errmsg(astdb));
741                 sqlite3_reset(showkey_stmt);
742                 ast_mutex_unlock(&dblock);
743                 return NULL;
744         }
745
746         while (sqlite3_step(showkey_stmt) == SQLITE_ROW) {
747                 const char *key_s, *value_s;
748                 if (!(key_s = (const char *) sqlite3_column_text(showkey_stmt, 0))) {
749                         break;
750                 }
751                 if (!(value_s = (const char *) sqlite3_column_text(showkey_stmt, 1))) {
752                         break;
753                 }
754                 ++counter;
755                 ast_cli(a->fd, "%-50s: %-25s\n", key_s, value_s);
756         }
757         sqlite3_reset(showkey_stmt);
758         ast_mutex_unlock(&dblock);
759
760         ast_cli(a->fd, "%d results found.\n", counter);
761         return CLI_SUCCESS;
762 }
763
764 static int display_results(void *arg, int columns, char **values, char **colnames)
765 {
766         struct ast_cli_args *a = arg;
767         size_t x;
768
769         for (x = 0; x < columns; x++) {
770                 ast_cli(a->fd, "%-5s: %-50s\n", colnames[x], values[x]);
771         }
772         ast_cli(a->fd, "\n");
773
774         return 0;
775 }
776
777 static char *handle_cli_database_query(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
778 {
779
780         switch (cmd) {
781         case CLI_INIT:
782                 e->command = "database query";
783                 e->usage =
784                         "Usage: database query \"<SQL Statement>\"\n"
785                         "       Run a user-specified SQL query on the database. Be careful.\n";
786                 return NULL;
787         case CLI_GENERATE:
788                 return NULL;
789         }
790
791         if (a->argc != 3) {
792                 return CLI_SHOWUSAGE;
793         }
794
795         ast_mutex_lock(&dblock);
796         db_execute_sql(a->argv[2], display_results, a);
797         db_sync(); /* Go ahead and sync the db in case they write */
798         ast_mutex_unlock(&dblock);
799
800         return CLI_SUCCESS;
801 }
802
803 static struct ast_cli_entry cli_database[] = {
804         AST_CLI_DEFINE(handle_cli_database_show,    "Shows database contents"),
805         AST_CLI_DEFINE(handle_cli_database_showkey, "Shows database contents"),
806         AST_CLI_DEFINE(handle_cli_database_get,     "Gets database value"),
807         AST_CLI_DEFINE(handle_cli_database_put,     "Adds/updates database value"),
808         AST_CLI_DEFINE(handle_cli_database_del,     "Removes database key/value"),
809         AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database keytree/values"),
810         AST_CLI_DEFINE(handle_cli_database_query,   "Run a user-specified query on the astdb"),
811 };
812
813 static int manager_dbput(struct mansession *s, const struct message *m)
814 {
815         const char *family = astman_get_header(m, "Family");
816         const char *key = astman_get_header(m, "Key");
817         const char *val = astman_get_header(m, "Val");
818         int res;
819
820         if (ast_strlen_zero(family)) {
821                 astman_send_error(s, m, "No family specified");
822                 return 0;
823         }
824         if (ast_strlen_zero(key)) {
825                 astman_send_error(s, m, "No key specified");
826                 return 0;
827         }
828
829         res = ast_db_put(family, key, S_OR(val, ""));
830         if (res) {
831                 astman_send_error(s, m, "Failed to update entry");
832         } else {
833                 astman_send_ack(s, m, "Updated database successfully");
834         }
835         return 0;
836 }
837
838 static int manager_dbget(struct mansession *s, const struct message *m)
839 {
840         const char *id = astman_get_header(m,"ActionID");
841         char idText[256] = "";
842         const char *family = astman_get_header(m, "Family");
843         const char *key = astman_get_header(m, "Key");
844         char tmp[MAX_DB_FIELD];
845         int res;
846
847         if (ast_strlen_zero(family)) {
848                 astman_send_error(s, m, "No family specified.");
849                 return 0;
850         }
851         if (ast_strlen_zero(key)) {
852                 astman_send_error(s, m, "No key specified.");
853                 return 0;
854         }
855
856         if (!ast_strlen_zero(id))
857                 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
858
859         res = ast_db_get(family, key, tmp, sizeof(tmp));
860         if (res) {
861                 astman_send_error(s, m, "Database entry not found");
862         } else {
863                 astman_send_ack(s, m, "Result will follow");
864                 astman_append(s, "Event: DBGetResponse\r\n"
865                                 "Family: %s\r\n"
866                                 "Key: %s\r\n"
867                                 "Val: %s\r\n"
868                                 "%s"
869                                 "\r\n",
870                                 family, key, tmp, idText);
871                 astman_append(s, "Event: DBGetComplete\r\n"
872                                 "%s"
873                                 "\r\n",
874                                 idText);
875         }
876         return 0;
877 }
878
879 static int manager_dbdel(struct mansession *s, const struct message *m)
880 {
881         const char *family = astman_get_header(m, "Family");
882         const char *key = astman_get_header(m, "Key");
883         int res;
884
885         if (ast_strlen_zero(family)) {
886                 astman_send_error(s, m, "No family specified.");
887                 return 0;
888         }
889
890         if (ast_strlen_zero(key)) {
891                 astman_send_error(s, m, "No key specified.");
892                 return 0;
893         }
894
895         res = ast_db_del(family, key);
896         if (res)
897                 astman_send_error(s, m, "Database entry not found");
898         else
899                 astman_send_ack(s, m, "Key deleted successfully");
900
901         return 0;
902 }
903
904 static int manager_dbdeltree(struct mansession *s, const struct message *m)
905 {
906         const char *family = astman_get_header(m, "Family");
907         const char *key = astman_get_header(m, "Key");
908         int num_deleted;
909
910         if (ast_strlen_zero(family)) {
911                 astman_send_error(s, m, "No family specified.");
912                 return 0;
913         }
914
915         if (!ast_strlen_zero(key)) {
916                 num_deleted = ast_db_deltree(family, key);
917         } else {
918                 num_deleted = ast_db_deltree(family, NULL);
919         }
920
921         if (num_deleted < 0) {
922                 astman_send_error(s, m, "Database unavailable");
923         } else if (num_deleted == 0) {
924                 astman_send_error(s, m, "Database entry not found");
925         } else {
926                 astman_send_ack(s, m, "Key tree deleted successfully");
927         }
928
929         return 0;
930 }
931
932 /*!
933  * \internal
934  * \brief Signal the astdb sync thread to do its thing.
935  *
936  * \note dblock is assumed to be held when calling this function.
937  */
938 static void db_sync(void)
939 {
940         ast_cond_signal(&dbcond);
941 }
942
943 /*!
944  * \internal
945  * \brief astdb sync thread
946  *
947  * This thread is in charge of syncing astdb to disk after a change.
948  * By pushing it off to this thread to take care of, this I/O bound operation
949  * will not block other threads from performing other critical processing.
950  * If changes happen rapidly, this thread will also ensure that the sync
951  * operations are rate limited.
952  */
953 static void *db_sync_thread(void *data)
954 {
955         ast_mutex_lock(&dblock);
956         ast_db_begin_transaction();
957         for (;;) {
958                 /* We're ok with spurious wakeups, so we don't worry about a predicate */
959                 ast_cond_wait(&dbcond, &dblock);
960                 if (ast_db_commit_transaction()) {
961                         ast_db_rollback_transaction();
962                 }
963                 if (doexit) {
964                         ast_mutex_unlock(&dblock);
965                         break;
966                 }
967                 ast_db_begin_transaction();
968                 ast_mutex_unlock(&dblock);
969                 sleep(1);
970                 ast_mutex_lock(&dblock);
971                 /* Unfortunately, it is possible for signaling to happen
972                  * when we're not waiting: in the bit when we're unlocked
973                  * above. Do the do-exit check here again. (We could do
974                  * it once, but that would impose a forced delay of 1
975                  * second always.) */
976                 if (doexit) {
977                         ast_mutex_unlock(&dblock);
978                         break;
979                 }
980         }
981
982         return NULL;
983 }
984
985 /*! \internal \brief Clean up resources on Asterisk shutdown */
986 static void astdb_atexit(void)
987 {
988         ast_cli_unregister_multiple(cli_database, ARRAY_LEN(cli_database));
989         ast_manager_unregister("DBGet");
990         ast_manager_unregister("DBPut");
991         ast_manager_unregister("DBDel");
992         ast_manager_unregister("DBDelTree");
993
994         /* Set doexit to 1 to kill thread. db_sync must be called with
995          * mutex held. */
996         doexit = 1;
997         ast_mutex_lock(&dblock);
998         db_sync();
999         ast_mutex_unlock(&dblock);
1000
1001         pthread_join(syncthread, NULL);
1002         ast_mutex_lock(&dblock);
1003         clean_statements();
1004         if (sqlite3_close(astdb) == SQLITE_OK) {
1005                 astdb = NULL;
1006         }
1007         ast_mutex_unlock(&dblock);
1008 }
1009
1010 int astdb_init(void)
1011 {
1012         if (db_init()) {
1013                 return -1;
1014         }
1015
1016         ast_cond_init(&dbcond, NULL);
1017         if (ast_pthread_create_background(&syncthread, NULL, db_sync_thread, NULL)) {
1018                 return -1;
1019         }
1020
1021         ast_register_atexit(astdb_atexit);
1022         ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
1023         ast_manager_register_xml_core("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);
1024         ast_manager_register_xml_core("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);
1025         ast_manager_register_xml_core("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel);
1026         ast_manager_register_xml_core("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree);
1027         return 0;
1028 }