#include "ap_config.h"
#if defined(HAVE_FCNTL_SERIALIZED_ACCEPT) && !defined(USE_FLOCK_SERIALIZED_ACCEPT)
#define USE_FCNTL 1
#include <fcntl.h>
#elif defined(HAVE_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) && !defined(NETWARE)
#include <sys/file.h>
#endif
#ifndef LOCK_UN
#undef USE_FLOCK
#define USE_FCNTL 1
#include <fcntl.h>
#endif
#endif
#ifdef AIX
#undef USE_FLOCK
#define USE_FCNTL 1
#include <fcntl.h>
#endif
#ifdef WIN32
#undef USE_FCNTL
#define USE_LOCKING
#include <sys/locking.h>
#endif
#ifdef NETWARE
#undef USE_FCNTL
#define USE_SEM_LOCKING
#include <nwsemaph.h>
LONG
locking_sem = 0;
#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
#ifdef USE_SEM_LOCKING
if
((locking_sem != 0) && (TimedWaitOnLocalSemaphore (locking_sem, 10000) != 0))
rc = -1;
else
rc = 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
#ifdef USE_SEM_LOCKING
if
(locking_sem)
SignalLocalSemaphore (locking_sem);
rc = 1;
#endif
return
rc;
}