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).
This commit is contained in:
Roland C. Dowdeswell
2012-05-31 17:30:29 +01:00
parent 8546c0c7b8
commit f9f78a2cbf

View File

@@ -124,6 +124,7 @@ kadm5_ret_t
kadm5_log_reinit (kadm5_server_context *context) kadm5_log_reinit (kadm5_server_context *context)
{ {
int fd; int fd;
int ret;
kadm5_log_context *log_context = &context->log_context; kadm5_log_context *log_context = &context->log_context;
if (log_context->log_fd != -1) { if (log_context->log_fd != -1) {
@@ -131,12 +132,18 @@ kadm5_log_reinit (kadm5_server_context *context)
close (log_context->log_fd); close (log_context->log_fd);
log_context->log_fd = -1; 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) if (fd < 0)
return errno; return errno;
if (flock (fd, LOCK_EX) < 0) { if (flock (fd, LOCK_EX) < 0) {
ret = errno;
close (fd); close (fd);
return errno; return ret;
}
if (ftruncate(fd, 0) < 0) {
ret = errno;
close(fd);
return ret;
} }
log_context->version = 0; log_context->version = 0;