Prevent CDR backends from unregistering while billing data is in flight
[asterisk/asterisk.git] / cdr / cdr_custom.c
old mode 100755 (executable)
new mode 100644 (file)
index 0658c8e..51235db
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2009, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
  * at the top of the source tree.
  */
 
-/*! \file
- *
+/*!
+ * \file
  * \brief Custom Comma Separated Value CDR records.
- * 
+ *
+ * \author Mark Spencer <markster@digium.com>
+ *
  * \arg See also \ref AstCDR
  *
  * Logs in LOG_DIR/cdr_custom
  * \ingroup cdr_drivers
  */
 
-#include <sys/types.h>
+/*! \li \ref cdr_custom.c uses the configuration file \ref cdr_custom.conf
+ * \addtogroup configuration_file Configuration Files
+ */
+
+/*!
+ * \page cdr_custom.conf cdr_custom.conf
+ * \verbinclude cdr_custom.conf.sample
+ */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include <time.h>
+
+#include "asterisk/paths.h"    /* use ast_config_AST_LOG_DIR */
 #include "asterisk/channel.h"
 #include "asterisk/cdr.h"
 #include "asterisk/module.h"
 #include "asterisk/config.h"
 #include "asterisk/pbx.h"
-#include "asterisk/logger.h"
 #include "asterisk/utils.h"
+#include "asterisk/lock.h"
+#include "asterisk/threadstorage.h"
+#include "asterisk/strings.h"
 
 #define CUSTOM_LOG_DIR "/cdr_custom"
+#define CONFIG         "cdr_custom.conf"
 
-#define DATE_FORMAT "%Y-%m-%d %T"
-
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <time.h>
-
-AST_MUTEX_DEFINE_STATIC(lock);
+AST_THREADSTORAGE(custom_buf);
 
-static char *desc = "Customizable Comma Separated Values CDR Backend";
+static const char name[] = "cdr-custom";
 
-static char *name = "cdr-custom";
+struct cdr_custom_config {
+       AST_DECLARE_STRING_FIELDS(
+               AST_STRING_FIELD(filename);
+               AST_STRING_FIELD(format);
+               );
+       ast_mutex_t lock;
+       AST_RWLIST_ENTRY(cdr_custom_config) list;
+};
 
-static FILE *mf = NULL;
+static AST_RWLIST_HEAD_STATIC(sinks, cdr_custom_config);
 
-static char master[AST_CONFIG_MAX_PATH];
-static char format[1024]="";
+static void free_config(void)
+{
+       struct cdr_custom_config *sink;
+       while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
+               ast_mutex_destroy(&sink->lock);
+               ast_free(sink);
+       }
+}
 
-static int load_config(int reload) 
+static int load_config(void)
 {
        struct ast_config *cfg;
        struct ast_variable *var;
-       int res = -1;
-
-       strcpy(format, "");
-       strcpy(master, "");
-       if((cfg = ast_config_load("cdr_custom.conf"))) {
-               var = ast_variable_browse(cfg, "mappings");
-               while(var) {
-                       ast_mutex_lock(&lock);
-                       if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
-                               if (strlen(var->value) > (sizeof(format) - 2))
-                                       ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
-                               strncpy(format, var->value, sizeof(format) - 2);
-                               strcat(format,"\n");
-                               snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
-                               ast_mutex_unlock(&lock);
-                       } else
-                               ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
-                       if (var->next)
-                               ast_log(LOG_NOTICE, "Sorry, only one mapping is supported at this time, mapping '%s' will be ignored at line %d.\n", var->next->name, var->next->lineno); 
-                       var = var->next;
+       struct ast_flags config_flags = { 0 };
+       int res = 0;
+
+       cfg = ast_config_load(CONFIG, config_flags);
+       if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
+               ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
+               return -1;
+       }
+
+       var = ast_variable_browse(cfg, "mappings");
+       while (var) {
+               if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
+                       struct cdr_custom_config *sink = ast_calloc_with_stringfields(1, struct cdr_custom_config, 1024);
+
+                       if (!sink) {
+                               ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
+                               res = -2;
+                               break;
+                       }
+
+                       ast_string_field_build(sink, format, "%s\n", var->value);
+                       ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
+                       ast_mutex_init(&sink->lock);
+
+                       AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
+               } else {
+                       ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
                }
-               ast_config_destroy(cfg);
-               res = 0;
-       } else {
-               if (reload)
-                       ast_log(LOG_WARNING, "Failed to reload configuration file.\n");
-               else
-                       ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
+               var = var->next;
        }
-       
+       ast_config_destroy(cfg);
+
        return res;
 }
 
-
-
 static int custom_log(struct ast_cdr *cdr)
 {
-       /* Make sure we have a big enough buf */
-       char buf[2048];
-       struct ast_channel dummy;
-
-       /* Abort if no master file is specified */
-       if (ast_strlen_zero(master))
-               return 0;
-
-       memset(buf, 0 , sizeof(buf));
-       /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
-       memset(&dummy, 0, sizeof(dummy));
-       dummy.cdr = cdr;
-       pbx_substitute_variables_helper(&dummy, format, buf, sizeof(buf) - 1);
-
-       /* because of the absolutely unconditional need for the
-          highest reliability possible in writing billing records,
-          we open write and close the log file each time */
-       mf = fopen(master, "a");
-       if (!mf) {
-               ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
+       struct ast_channel *dummy;
+       struct ast_str *str;
+       struct cdr_custom_config *config;
+
+       /* Batching saves memory management here.  Otherwise, it's the same as doing an allocation and free each time. */
+       if (!(str = ast_str_thread_get(&custom_buf, 16))) {
+               return -1;
        }
-       if (mf) {
-               fputs(buf, mf);
-               fflush(mf); /* be particularly anal here */
-               fclose(mf);
-               mf = NULL;
+
+       dummy = ast_dummy_channel_alloc();
+       if (!dummy) {
+               ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
+               return -1;
        }
-       return 0;
-}
 
-char *description(void)
-{
-       return desc;
-}
+       /* We need to dup here since the cdr actually belongs to the other channel,
+          so when we release this channel we don't want the CDR getting cleaned
+          up prematurely. */
+       ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
+
+       AST_RWLIST_RDLOCK(&sinks);
+
+       AST_LIST_TRAVERSE(&sinks, config, list) {
+               FILE *out;
+
+               ast_str_substitute_variables(&str, 0, dummy, config->format);
+
+               /* Even though we have a lock on the list, we could be being chased by
+                  another thread and this lock ensures that we won't step on anyone's
+                  toes.  Once each CDR backend gets it's own thread, this lock can be
+                  removed. */
+               ast_mutex_lock(&config->lock);
+
+               /* Because of the absolutely unconditional need for the
+                  highest reliability possible in writing billing records,
+                  we open write and close the log file each time */
+               if ((out = fopen(config->filename, "a"))) {
+                       fputs(ast_str_buffer(str), out);
+                       fflush(out); /* be particularly anal here */
+                       fclose(out);
+               } else {
+                       ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
+               }
+
+               ast_mutex_unlock(&config->lock);
+       }
+
+       AST_RWLIST_UNLOCK(&sinks);
+
+       ast_channel_unref(dummy);
 
-int unload_module(void)
-{
-       if (mf)
-               fclose(mf);
-       ast_cdr_unregister(name);
        return 0;
 }
 
-int load_module(void)
+static int unload_module(void)
 {
-       int res = 0;
+       if (ast_cdr_unregister(name)) {
+               return -1;
+       }
 
-       if (!load_config(0)) {
-               res = ast_cdr_register(name, desc, custom_log);
-               if (res)
-                       ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
-               if (mf)
-                       fclose(mf);
+       if (AST_RWLIST_WRLOCK(&sinks)) {
+               ast_cdr_register(name, ast_module_info->description, custom_log);
+               ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
+               return -1;
        }
-       return res;
-}
 
-int reload(void)
-{
-       return load_config(1);
+       free_config();
+       AST_RWLIST_UNLOCK(&sinks);
+       return 0;
 }
 
-int usecount(void)
+static enum ast_module_load_result load_module(void)
 {
-       return 0;
+       if (AST_RWLIST_WRLOCK(&sinks)) {
+               ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
+               return AST_MODULE_LOAD_FAILURE;
+       }
+
+       load_config();
+       AST_RWLIST_UNLOCK(&sinks);
+       ast_cdr_register(name, ast_module_info->description, custom_log);
+       return AST_MODULE_LOAD_SUCCESS;
 }
 
-char *key()
+static int reload(void)
 {
-       return ASTERISK_GPL_KEY;
+       if (AST_RWLIST_WRLOCK(&sinks)) {
+               ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
+               return AST_MODULE_LOAD_FAILURE;
+       }
+
+       free_config();
+       load_config();
+       AST_RWLIST_UNLOCK(&sinks);
+       return AST_MODULE_LOAD_SUCCESS;
 }
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CDR Backend",
+               .load = load_module,
+               .unload = unload_module,
+               .reload = reload,
+               .load_pri = AST_MODPRI_CDR_DRIVER,
+              );
+