Skip to content

Commit

Permalink
exfile_open now opens files for read/writing, which is useful if the …
Browse files Browse the repository at this point in the history
…exfile instance may be used to acquire fds for reading too

add 'append' argument, that controls whether exfile_open seeks to the end of the file.
  • Loading branch information
arr2036 committed Dec 8, 2014
1 parent 4b27298 commit c3019bd
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/include/exfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
int exfile_open(exfile_t *lf, char const *filename, mode_t permissions);
int exfile_open(exfile_t *lf, char const *filename, mode_t permissions, bool append);
int exfile_close(exfile_t *lf, int fd);
int exfile_unlock(exfile_t *lf, int fd);

Expand Down
10 changes: 5 additions & 5 deletions src/main/exfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ exfile_t *exfile_init(TALLOC_CTX *ctx, uint32_t max_entries, uint32_t max_idle)
* @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)
int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool append)
{
uint32_t i;
uint32_t hash;
Expand Down Expand Up @@ -202,7 +203,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions)
ef->entries[i].filename = talloc_strdup(ef->entries, filename);
ef->entries[i].fd = -1;

ef->entries[i].fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, permissions);
ef->entries[i].fd = open(filename, O_RDWR | O_APPEND | O_CREAT, permissions);
if (ef->entries[i].fd < 0) {
mode_t dirperm;
char *p, *dir;
Expand Down Expand Up @@ -290,16 +291,15 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions)
* Seek to the end of the file before returning the FD to
* the caller.
*/
lseek(ef->entries[i].fd, 0, SEEK_END);
if (append) lseek(ef->entries[i].fd, 0, SEEK_END);

/*
* Return holding the mutex for the entry.
*/
ef->entries[i].last_used = now;
ef->entries[i].dup = dup(ef->entries[i].fd);
if (ef->entries[i].dup < 0) {
fr_strerror_printf("Failed calling dup(): %s",
strerror(errno));
fr_strerror_printf("Failed calling dup(): %s", strerror(errno));
goto error;
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/rlm_detail/rlm_detail.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ static rlm_rcode_t CC_HINT(nonnull) detail_do(void *instance, REQUEST *request,
#endif
#endif

outfd = exfile_open(inst->ef, buffer, inst->perm);
outfd = exfile_open(inst->ef, buffer, inst->perm, true);
if (outfd < 0) {
RERROR("Couldn't open file %s: %s", buffer, fr_strerror());
return RLM_MODULE_FAIL;
Expand Down
5 changes: 2 additions & 3 deletions src/modules/rlm_linelog/rlm_linelog.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,9 @@ static rlm_rcode_t CC_HINT(nonnull) mod_do_linelog(void *instance, REQUEST *requ
*p = '/';
}

fd = exfile_open(inst->ef, path, inst->permissions);
fd = exfile_open(inst->ef, path, inst->permissions, true);
if (fd == -1) {
ERROR("rlm_linelog: Failed to open %s: %s",
path, fr_syserror(errno));
ERROR("rlm_linelog: Failed to open %s: %s", path, fr_syserror(errno));
return RLM_MODULE_FAIL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/rlm_sql/sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request,
return;
}

fd = exfile_open(inst->ef, filename, 0640);
fd = exfile_open(inst->ef, filename, 0640, true);
if (fd < 0) {
ERROR("rlm_sql (%s): Couldn't open logfile '%s': %s", inst->config->xlat_name,
expanded, fr_syserror(errno));
Expand Down

0 comments on commit c3019bd

Please sign in to comment.