From 4d4b6ca9fcf0b86afc7f5fa54b27dbb4c26932ae Mon Sep 17 00:00:00 2001 From: "Alan T. DeKok" Date: Tue, 25 Jul 2017 09:52:01 -0400 Subject: [PATCH] we always append to the file --- src/include/exfile.h | 2 +- src/main/exfile.c | 25 ++++++++++++------------- src/modules/rlm_detail/rlm_detail.c | 2 +- src/modules/rlm_linelog/rlm_linelog.c | 2 +- src/modules/rlm_sql/sql.c | 2 +- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/include/exfile.h b/src/include/exfile.h index c511100603a3..750eb4dd448f 100644 --- a/src/include/exfile.h +++ b/src/include/exfile.h @@ -36,7 +36,7 @@ extern "C" { typedef struct exfile_t exfile_t; exfile_t *exfile_init(TALLOC_CTX *ctx, uint32_t entries, uint32_t idle, bool locking); -int exfile_open(exfile_t *lf, char const *filename, mode_t permissions, bool append); +int exfile_open(exfile_t *lf, char const *filename, mode_t permissions); int exfile_close(exfile_t *lf, int fd); #ifdef __cplusplus diff --git a/src/main/exfile.c b/src/main/exfile.c index 245dee18a9e9..f92d22934cce 100644 --- a/src/main/exfile.c +++ b/src/main/exfile.c @@ -205,10 +205,9 @@ static int exfile_open_mkdir(exfile_t *ef, char const *filename, mode_t permissi * @param ef The logfile context returned from exfile_init(). * @param filename the file to open. * @param permissions to use. - * @param append If true seek to the end of the file. * @return an FD used to write to the file, or -1 on error. */ -int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool append) +int exfile_open(exfile_t *ef, char const *filename, mode_t permissions) { int i, found, tries, unused, oldest; uint32_t hash; @@ -224,8 +223,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app found = exfile_open_mkdir(ef, filename, permissions); if (found < 0) return -1; - if (append) (void) lseek(found, 0, SEEK_END); - + (void) lseek(found, 0, SEEK_END); return found; } @@ -316,10 +314,15 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app reopen: /* - * Open the file and try to lock it. + * Open the file. */ ef->entries[i].fd = exfile_open_mkdir(ef, filename, permissions); - if (ef->entries[i].fd < 0) goto error; + if (ef->entries[i].fd < 0) { + error: + exfile_cleanup_entry(&ef->entries[i]); + PTHREAD_MUTEX_UNLOCK(&(ef->mutex)); + return -1; + } /* * Try to lock it. If we can't lock it, it's because @@ -336,11 +339,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app */ if (lseek(ef->entries[i].fd, 0, SEEK_SET) < 0) { fr_strerror_printf("Failed to seek in file %s: %s", filename, strerror(errno)); - - error: - exfile_cleanup_entry(&ef->entries[i]); - PTHREAD_MUTEX_UNLOCK(&(ef->mutex)); - return -1; + goto error; } /* @@ -357,7 +356,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app /* * Close the file and re-open it. It may * have been deleted. If it was deleted, - * then it should now be unlocked. + * then the new file should now be unlocked. */ close(ef->entries[i].fd); ef->entries[i].fd = open(filename, O_WRONLY | O_CREAT, permissions); @@ -391,7 +390,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app * If we're appending, eek to the end of the file before * returning the FD to the caller. */ - if (append) lseek(ef->entries[i].fd, 0, SEEK_END); + (void) lseek(ef->entries[i].fd, 0, SEEK_END); /* * Return holding the mutex for the entry. diff --git a/src/modules/rlm_detail/rlm_detail.c b/src/modules/rlm_detail/rlm_detail.c index caa01749c06f..20843a4fe63f 100644 --- a/src/modules/rlm_detail/rlm_detail.c +++ b/src/modules/rlm_detail/rlm_detail.c @@ -388,7 +388,7 @@ static rlm_rcode_t CC_HINT(nonnull) detail_do(void *instance, REQUEST *request, #endif #endif - outfd = exfile_open(inst->ef, buffer, inst->perm, true); + outfd = exfile_open(inst->ef, buffer, inst->perm); if (outfd < 0) { RERROR("Couldn't open file %s: %s", buffer, fr_strerror()); return RLM_MODULE_FAIL; diff --git a/src/modules/rlm_linelog/rlm_linelog.c b/src/modules/rlm_linelog/rlm_linelog.c index 3d4219ca9ec2..3c15bf04b309 100644 --- a/src/modules/rlm_linelog/rlm_linelog.c +++ b/src/modules/rlm_linelog/rlm_linelog.c @@ -248,7 +248,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_do_linelog(void *instance, REQUEST *requ return RLM_MODULE_FAIL; } - fd = exfile_open(inst->ef, path, inst->permissions, true); + fd = exfile_open(inst->ef, path, inst->permissions); if (fd < 0) { ERROR("rlm_linelog: Failed to open %s: %s", path, fr_syserror(errno)); return RLM_MODULE_FAIL; diff --git a/src/modules/rlm_sql/sql.c b/src/modules/rlm_sql/sql.c index 05d0bc245643..42759a756319 100644 --- a/src/modules/rlm_sql/sql.c +++ b/src/modules/rlm_sql/sql.c @@ -487,7 +487,7 @@ void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request, return; } - fd = exfile_open(inst->ef, filename, 0640, true); + fd = exfile_open(inst->ef, filename, 0640); if (fd < 0) { ERROR("rlm_sql (%s): Couldn't open logfile '%s': %s", inst->name, expanded, fr_syserror(errno));