--- cdr_csv.c.orig 2007-07-31 17:13:45.000000000 -0400 +++ cdr_csv.c 2007-07-31 15:56:03.000000000 -0400 @@ -48,6 +48,7 @@ #include "asterisk/options.h" #include "asterisk/logger.h" #include "asterisk/utils.h" +#include "asterisk/lock.h" #define CSV_LOG_DIR "/cdr-csv" #define CSV_MASTER "/Master.csv" @@ -94,6 +95,7 @@ static FILE *mf = NULL; +static ast_mutex_t filelock; /* lock to keep records from getting garbled */ static int load_config(void) { @@ -286,15 +288,11 @@ /* 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(csvmaster, "a"); - if (!mf) { - ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno)); - } if (mf) { + ast_mutex_lock(&filelock); fputs(buf, mf); + ast_mutex_unlock(&filelock); fflush(mf); /* be particularly anal here */ - fclose(mf); - mf = NULL; } if (!ast_strlen_zero(cdr->accountcode)) { if (writefile(buf, cdr->accountcode)) @@ -316,8 +314,21 @@ { int res; + ast_mutex_init(&filelock); + if(!load_config()) return AST_MODULE_LOAD_DECLINE; + + char csvmaster[PATH_MAX]; + snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER); + + mf = fopen(csvmaster, "a"); + + if (!mf) { + /* unable to open the file */ + ast_log(LOG_ERROR, "Unable to open master file %s : %s\n", csvmaster, strerror(errno)); + return AST_MODULE_LOAD_DECLINE; + } res = ast_cdr_register(name, ast_module_info->description, csv_log); if (res) { @@ -332,6 +343,23 @@ { load_config(); + char csvmaster[PATH_MAX]; + snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER); + + if(mf) { /* close the file if it is open */ + fclose(mf); + mf = NULL; + } + if(!mf) { /* reopen the file if it is closed */ + mf = fopen(csvmaster, "a"); + + if (!mf) { + /* unable to open the file */ + ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno)); + return AST_MODULE_LOAD_DECLINE; + } + } + return 0; }