mail-index-lock.c revision d5498f4d64a97d04a1b608920f23eb2ad2353d05
/* Copyright (c) 2003-2012 Dovecot authors, see the included COPYING file */
/*
Locking should never fail or timeout. Exclusive locks must be kept as short
time as possible. Shared locks can be long living, so if we can't get
exclusive lock directly, we'll recreate the index. That means the shared
lock holders can keep using the old file.
lock_id is used to figure out if acquired lock is still valid. When index
file is reopened, the lock_id can become invalid. It doesn't matter however,
as no-one's going to modify the old file anymore.
lock_id also tells us if we're referring to a shared or an exclusive lock.
This allows us to drop back to shared locking once all exclusive locks
are dropped. Shared locks have even numbers, exclusive locks have odd numbers.
The number is increased by two every time the lock is dropped or index file
is reopened.
*/
#include "lib.h"
#include "nfs-workarounds.h"
#include "mail-index-private.h"
#define MAIL_INDEX_SHARED_LOCK_TIMEOUT 120
int lock_type, unsigned int timeout_secs,
{
if (fd == -1) {
return 1;
}
}
unsigned int timeout_secs, unsigned int *lock_id_r)
{
int ret;
/* file is already locked */
return 1;
}
/* FIXME: exclusive locking will rewrite the index file every
time. shouldn't really be needed.. reading doesn't require
locks then, though */
return 1;
}
if (ret <= 0)
return ret;
return 1;
}
{
return;
/* Assume flock() is emulated with fcntl(), because that's how most
OSes work nowadays. */
if (locked &&
} else {
}
}
{
unsigned int timeout_secs;
int ret;
if (ret > 0) {
return 0;
}
if (ret < 0)
return -1;
"Timeout (%us) while waiting for shared lock for index file %s",
return -1;
}
{
*_lock_id = 0;
/* shared lock */
/* unlocking some older generation of the index file.
we've already closed the file so just ignore this. */
return;
}
if (index->shared_lock_count == 0) {
if (!MAIL_INDEX_IS_IN_MEMORY(index))
}
}
}
{
return TRUE;
}
return FALSE;
}