#include "ap_config.h"
#if defined(USE_FCNTL_SERIALIZED_ACCEPT)
#define USE_FCNTL 1
#include <fcntl.h>
#endif
#if defined(USE_FLOCK_SERIALIZED_ACCEPT)
#define USE_FLOCK 1
#include <sys/file.h>
#endif
#if !defined(USE_FCNTL) && !defined(USE_FLOCK)
#define USE_FLOCK 1
#if !defined(MPE) && !defined(WIN32)
#include <sys/file.h>
#endif
#ifndef LOCK_UN
#undef USE_FLOCK
#define USE_FCNTL 1
#include <fcntl.h>
#endif
#endif
#ifdef USE_FCNTL
static struct flock lock_it;
static struct flock unlock_it;
#endif
int sdbm_fd_lock(int fd, int readonly)
{
int rc;
#ifdef USE_FCNTL
lock_it.l_whence = SEEK_SET;
lock_it.l_start = 0;
lock_it.l_len = 0;
lock_it.l_type = readonly ? F_RDLCK : F_WRLCK;
lock_it.l_pid = 0;
while ( ((rc = fcntl(fd, F_SETLKW, &lock_it)) < 0)
&& (errno == EINTR) ) {
continue;
}
#endif
#ifdef USE_FLOCK
while ( ((rc = flock(fd, readonly ? LOCK_SH : LOCK_EX)) < 0)
&& (errno == EINTR) ) {
continue;
}
#endif
#ifdef USE_LOCKING
lseek(fd, 0, SEEK_SET);
rc = _locking(fd, _LK_LOCK, 1);
#endif
return rc;
}
int sdbm_fd_unlock(int fd)
{
int rc;
#ifdef USE_FCNTL
unlock_it.l_whence = SEEK_SET;
unlock_it.l_start = 0;
unlock_it.l_len = 0;
unlock_it.l_type = F_UNLCK;
unlock_it.l_pid = 0;
rc = fcntl(fd, F_SETLKW, &unlock_it);
#endif
#ifdef USE_FLOCK
rc = flock(fd, LOCK_UN);
#endif
#ifdef USE_LOCKING
lseek(fd, 0, SEEK_SET);
rc = _locking(fd, _LK_UNLCK, 1);
#endif
return rc;
}