2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Steve Murphy <murf@digium.com> borrowed code from cdr,
7 * Mark Spencer <markster@digium.com> and others.
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Custom SQLite3 CEL records.
24 * \author Adapted by Steve Murphy <murf@digium.com> from
25 * Alejandro Rios <alejandro.rios@avatar.com.co> and
26 * Russell Bryant <russell@digium.com> from
27 * cdr_mysql_custom by Edward Eastman <ed@dm3.co.uk>,
28 * and cdr_sqlite by Holger Schurig <hs4233@mail.mn-solutions.de>
31 * \arg See also \ref AstCEL
34 * \ingroup cel_drivers
38 <depend>sqlite3</depend>
39 <support_level>extended</support_level>
44 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
48 #include "asterisk/paths.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/cel.h"
51 #include "asterisk/module.h"
52 #include "asterisk/config.h"
53 #include "asterisk/pbx.h"
54 #include "asterisk/logger.h"
55 #include "asterisk/utils.h"
56 #include "asterisk/cli.h"
57 #include "asterisk/options.h"
58 #include "asterisk/stringfields.h"
60 #define SQLITE_BACKEND_NAME "CEL sqlite3 custom backend"
62 AST_MUTEX_DEFINE_STATIC(lock);
64 static const char config_file[] = "cel_sqlite3_custom.conf";
66 static const char name[] = "cel_sqlite3_custom";
67 static sqlite3 *db = NULL;
69 static char table[80];
71 * \bug Handling of this var is crash prone on reloads
77 AST_LIST_ENTRY(values) list;
80 static AST_LIST_HEAD_STATIC(sql_values, values);
82 static void free_config(void);
84 static int load_column_config(const char *tmp)
87 char *cols = NULL, *save = NULL;
89 struct ast_str *column_string = NULL;
91 if (ast_strlen_zero(tmp)) {
92 ast_log(LOG_WARNING, "Column names not specified. Module not loaded.\n");
95 if (!(column_string = ast_str_create(1024))) {
96 ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
99 if (!(save = cols = ast_strdup(tmp))) {
100 ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
101 ast_free(column_string);
104 while ((col = strsep(&cols, ","))) {
105 col = ast_strip(col);
106 escaped = sqlite3_mprintf("%q", col);
108 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s.'\n", col, table);
109 ast_free(column_string);
113 ast_str_append(&column_string, 0, "%s%s", ast_str_strlen(column_string) ? "," : "", escaped);
114 sqlite3_free(escaped);
116 if (!(columns = ast_strdup(ast_str_buffer(column_string)))) {
117 ast_log(LOG_ERROR, "Out of memory copying columns string for table '%s.'\n", table);
118 ast_free(column_string);
122 ast_free(column_string);
128 static int load_values_config(const char *tmp)
131 char *vals = NULL, *save = NULL;
132 struct values *value = NULL;
134 if (ast_strlen_zero(tmp)) {
135 ast_log(LOG_WARNING, "Values not specified. Module not loaded.\n");
138 if (!(save = vals = ast_strdup(tmp))) {
139 ast_log(LOG_ERROR, "Out of memory creating temporary buffer for value '%s'\n", tmp);
142 while ((val = strsep(&vals, ","))) {
143 /* Strip the single quotes off if they are there */
144 val = ast_strip_quoted(val, "'", "'");
145 value = ast_calloc(sizeof(char), sizeof(*value) + strlen(val) + 1);
147 ast_log(LOG_ERROR, "Out of memory creating entry for value '%s'\n", val);
151 value->expression = (char *) value + sizeof(*value);
152 ast_copy_string(value->expression, val, strlen(val) + 1);
153 AST_LIST_INSERT_TAIL(&sql_values, value, list);
160 static int load_config(int reload)
162 struct ast_config *cfg;
163 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
164 struct ast_variable *mappingvar;
167 if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
168 ast_log(LOG_WARNING, "Failed to %sload configuration file. %s\n",
169 reload ? "re" : "", reload ? "" : "Module not activated.");
171 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
179 if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
180 /* Nothing configured */
181 ast_config_destroy(cfg);
185 /* Mapping must have a table name */
186 if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
187 ast_copy_string(table, tmp, sizeof(table));
189 ast_log(LOG_WARNING, "Table name not specified. Assuming cel.\n");
190 strcpy(table, "cel");
194 if (load_column_config(ast_variable_retrieve(cfg, "master", "columns"))) {
195 ast_config_destroy(cfg);
201 if (load_values_config(ast_variable_retrieve(cfg, "master", "values"))) {
202 ast_config_destroy(cfg);
207 ast_verb(3, "Logging CEL records to table '%s' in 'master.db'\n", table);
209 ast_config_destroy(cfg);
214 static void free_config(void)
216 struct values *value;
228 while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) {
233 static void write_cel(struct ast_event *event)
240 /* Should not have loaded, but be failsafe. */
244 ast_mutex_lock(&lock);
246 { /* Make it obvious that only sql should be used outside of this block */
248 char subst_buf[2048];
249 struct values *value;
250 struct ast_channel *dummy;
251 struct ast_str *value_string = ast_str_create(1024);
253 dummy = ast_cel_fabricate_channel_from_event(event);
255 ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
256 ast_free(value_string);
257 ast_mutex_unlock(&lock);
260 AST_LIST_TRAVERSE(&sql_values, value, list) {
261 pbx_substitute_variables_helper(dummy, value->expression, subst_buf, sizeof(subst_buf) - 1);
262 escaped = sqlite3_mprintf("%q", subst_buf);
263 ast_str_append(&value_string, 0, "%s'%s'", ast_str_strlen(value_string) ? "," : "", escaped);
264 sqlite3_free(escaped);
266 sql = sqlite3_mprintf("INSERT INTO %q (%s) VALUES (%s)", table, columns, ast_str_buffer(value_string));
267 ast_debug(1, "About to log: %s\n", sql);
268 dummy = ast_channel_unref(dummy);
269 ast_free(value_string);
272 /* XXX This seems awful arbitrary... */
273 for (count = 0; count < 5; count++) {
274 int res = sqlite3_exec(db, sql, NULL, NULL, &error);
275 if (res != SQLITE_BUSY && res != SQLITE_LOCKED) {
281 ast_mutex_unlock(&lock);
284 ast_log(LOG_ERROR, "%s. SQL: %s.\n", error, sql);
295 static int unload_module(void)
297 ast_cel_backend_unregister(SQLITE_BACKEND_NAME);
304 static int load_module(void)
307 char filename[PATH_MAX];
311 if (load_config(0)) {
312 return AST_MODULE_LOAD_DECLINE;
315 /* is the database there? */
316 snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR);
317 res = sqlite3_open(filename, &db);
318 if (res != SQLITE_OK) {
319 ast_log(LOG_ERROR, "Could not open database %s.\n", filename);
321 return AST_MODULE_LOAD_DECLINE;
324 /* is the table there? */
325 sql = sqlite3_mprintf("SELECT COUNT(*) FROM %q;", table);
326 res = sqlite3_exec(db, sql, NULL, NULL, NULL);
328 if (res != SQLITE_OK) {
329 /* We don't use %q for the column list here since we already escaped when building it */
330 sql = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY, %s)", table, columns);
331 res = sqlite3_exec(db, sql, NULL, NULL, &error);
333 if (res != SQLITE_OK) {
334 ast_log(LOG_WARNING, "Unable to create table '%s': %s.\n", table, error);
337 return AST_MODULE_LOAD_DECLINE;
341 if (ast_cel_backend_register(SQLITE_BACKEND_NAME, write_cel)) {
342 ast_log(LOG_ERROR, "Unable to register custom SQLite3 CEL handling\n");
344 return AST_MODULE_LOAD_DECLINE;
347 return AST_MODULE_LOAD_SUCCESS;
350 static int reload(void)
354 ast_mutex_lock(&lock);
355 res = load_config(1);
356 ast_mutex_unlock(&lock);
361 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite3 Custom CEL Module",
363 .unload = unload_module,
365 .load_pri = AST_MODPRI_CDR_DRIVER,