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