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