From f9f78a2cbfdb91b7505fbd3360183d17dd79d409 Mon Sep 17 00:00:00 2001 From: "Roland C. Dowdeswell" Date: Thu, 31 May 2012 17:30:29 +0100 Subject: [PATCH] kadm5_log_reinit() needs to obtain its lock before truncating the file. We can't use O_TRUNC on open because (without O_EXLOCK which is not portable) we would be modifying the file without an exclusive lock. So, we drop the use of O_TRUNC and use ftruncate(2) after obtaining the lock via flock(2). --- lib/kadm5/log.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/kadm5/log.c b/lib/kadm5/log.c index 659a9c5e9..5ace4682d 100644 --- a/lib/kadm5/log.c +++ b/lib/kadm5/log.c @@ -124,6 +124,7 @@ kadm5_ret_t kadm5_log_reinit (kadm5_server_context *context) { int fd; + int ret; kadm5_log_context *log_context = &context->log_context; if (log_context->log_fd != -1) { @@ -131,12 +132,18 @@ kadm5_log_reinit (kadm5_server_context *context) close (log_context->log_fd); log_context->log_fd = -1; } - fd = open (log_context->log_file, O_RDWR | O_CREAT | O_TRUNC, 0600); + fd = open (log_context->log_file, O_RDWR | O_CREAT, 0600); if (fd < 0) return errno; if (flock (fd, LOCK_EX) < 0) { + ret = errno; close (fd); - return errno; + return ret; + } + if (ftruncate(fd, 0) < 0) { + ret = errno; + close(fd); + return ret; } log_context->version = 0;