mbox-lock.c revision 704efd0b34e3611e3decf1d559fe6a93214b0bd0
/* Copyright (C) 2002 Timo Sirainen */
#include "lib.h"
#include "mbox-storage.h"
#include "mbox-file.h"
#include "mbox-lock.h"
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_FLOCK
#endif
/* 0.1 .. 0.2msec */
/* lock methods to use in wanted order */
#define DEFAULT_READ_LOCK_METHODS "fcntl"
#define DEFAULT_WRITE_LOCK_METHODS "dotlock fcntl"
/* lock timeout */
/* assume stale dotlock if mbox file hasn't changed for n seconds */
enum mbox_lock_type {
};
struct mbox_lock_context {
struct index_mailbox *ibox;
int lock_status[MBOX_LOCK_COUNT];
int checked_file;
int lock_type;
int dotlock_last_stale;
};
struct mbox_lock_data {
enum mbox_lock_type type;
const char *name;
};
#ifdef HAVE_FLOCK
#else
# define mbox_lock_flock NULL
#endif
#ifdef HAVE_LOCKF
#else
# define mbox_lock_lockf NULL
#endif
struct mbox_lock_data lock_data[] = {
};
static int lock_settings_initialized = FALSE;
static int lock_timeout, dotlock_change_timeout;
enum mbox_lock_type *locks)
{
enum mbox_lock_type type;
const char *const *lock;
int i, dest;
break;
}
}
i_fatal("%s: Support for lock type %s "
}
for (i = 0; i < dest; i++) {
}
/* @UNSAFE */
}
}
static void mbox_init_lock_settings(void)
{
const char *str;
}
{
return 0;
return -1;
}
}
if (mbox_file_open(ibox) < 0)
return -1;
}
return 0;
}
{
int idx;
/* get next index we wish to try locking */
break;
}
return FALSE;
}
}
return TRUE;
}
{
int ret;
return 1;
ret = -1;
}
return 1;
}
if (ret < 0) {
return -1;
}
if (ret == 0) {
"Timeout while waiting for lock");
return 0;
}
return -1;
return 1;
}
#ifdef HAVE_FLOCK
{
return -1;
return 1;
else
last_notify = 0;
if (errno != EWOULDBLOCK) {
return -1;
}
if (max_wait_time == 0)
return 0;
if (now >= max_wait_time)
return 0;
if (now != last_notify) {
max_wait_time - now);
}
}
return 1;
}
#endif
#ifdef HAVE_LOCKF
{
return -1;
return 1;
else
last_notify = 0;
return -1;
}
if (max_wait_time == 0)
return 0;
if (now >= max_wait_time)
return 0;
if (now != last_notify) {
max_wait_time - now);
}
}
return 1;
}
#endif
{
int wait_type;
return -1;
return 1;
return -1;
}
return 0;
max_wait_time - now);
}
return 1;
}
{
enum mbox_lock_type *lock_types;
enum mbox_lock_type type;
int i, ret = 0, lock_status;
type = lock_types[i];
continue;
if (ret <= 0)
break;
}
return ret;
}
unsigned int *lock_id_r)
{
struct mbox_lock_context ctx;
int ret;
ibox->mbox_locks++;
return 1;
}
if (ret <= 0) {
(void)mbox_unlock_files(&ctx);
if (ret == 0) {
"Timeout while waiting for lock");
}
return ret;
}
ibox->mbox_locks++;
return 1;
}
{
int ret = 0;
ret = -1;
/* make sure we don't keep mmap() between locks */
return ret;
}
{
struct mbox_lock_context ctx;
int i;
if (--ibox->mbox_locks > 0)
return 0;
for (i = 0; i < MBOX_LOCK_COUNT; i++)
return mbox_unlock_files(&ctx);
}