#include "sqliteInt.h"
#include "os.h"
#if OS_UNIX
#ifndef SQLITE_DISABLE_LFS
# define _LARGE_FILE 1
# ifndef _FILE_OFFSET_BITS
# define _FILE_OFFSET_BITS 64
# endif
# define _LARGEFILE_SOURCE 1
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#ifdef SQLITE_ENABLE_LOCKING_STYLE
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/mount.h>
#endif
#ifndef THREADSAFE
# define THREADSAFE 1
#endif
#if THREADSAFE
# include <pthread.h>
# define SQLITE_UNIX_THREADS 1
#endif
#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
#endif
typedef struct unixFile unixFile;
struct unixFile {
IoMethod const *pMethod;
struct openCnt *pOpen;
struct lockInfo *pLock;
#ifdef SQLITE_ENABLE_LOCKING_STYLE
void *lockingContext;
#endif
int h;
unsigned char locktype;
unsigned char isOpen;
unsigned char fullSync;
int dirfd;
i64 offset;
#ifdef SQLITE_UNIX_THREADS
pthread_t tid;
#endif
};
#ifdef SQLITE_CRASH_TEST
extern int sqlite3CrashTestEnable;
extern int sqlite3CrashOpenReadWrite(const char*, OsFile**, int*);
extern int sqlite3CrashOpenExclusive(const char*, OsFile**, int);
extern int sqlite3CrashOpenReadOnly(const char*, OsFile**, int);
# define CRASH_TEST_OVERRIDE(X,A,B,C) \
if(sqlite3CrashTestEnable){ return X(A,B,C); }
#else
# define CRASH_TEST_OVERRIDE(X,A,B,C)
#endif
#include "os_common.h"
#ifndef SQLITE_OMIT_DISKIO
#ifndef O_LARGEFILE
# define O_LARGEFILE 0
#endif
#ifdef SQLITE_DISABLE_LFS
# undef O_LARGEFILE
# define O_LARGEFILE 0
#endif
#ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
#endif
#ifndef O_BINARY
# define O_BINARY 0
#endif
#ifdef __DJGPP__
# define fcntl(A,B,C) 0
#endif
#ifdef SQLITE_UNIX_THREADS
#define threadid pthread_self()
#else
#define threadid 0
#endif
#if defined(SQLITE_UNIX_THREADS)
# define SET_THREADID(X) (X)->tid = pthread_self()
# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
!pthread_equal((X)->tid, pthread_self()))
#else
# define SET_THREADID(X)
# define CHECK_THREADID(X) 0
#endif
struct lockKey {
dev_t dev;
ino_t ino;
#ifdef SQLITE_UNIX_THREADS
pthread_t tid;
#endif
};
struct lockInfo {
struct lockKey key;
int cnt;
int locktype;
int nRef;
};
struct openKey {
dev_t dev;
ino_t ino;
};
struct openCnt {
struct openKey key;
int nRef;
int nLock;
int nPending;
int *aPending;
};
static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0,
sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0,
sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
#ifdef SQLITE_ENABLE_LOCKING_STYLE
typedef enum {
posixLockingStyle = 0,
afpLockingStyle,
flockLockingStyle,
dotlockLockingStyle,
noLockingStyle,
unsupportedLockingStyle
} sqlite3LockingStyle;
#endif
#ifdef SQLITE_UNIX_THREADS
#ifndef SQLITE_THREAD_OVERRIDE_LOCK
# define SQLITE_THREAD_OVERRIDE_LOCK -1
#endif
#ifdef SQLITE_TEST
int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
#else
static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
#endif
struct threadTestData {
int fd;
struct flock lock;
int result;
};
#ifdef SQLITE_LOCK_TRACE
static int lockTrace(int fd, int op, struct flock *p){
char *zOpName, *zType;
int s;
int savedErrno;
if( op==F_GETLK ){
zOpName = "GETLK";
}else if( op==F_SETLK ){
zOpName = "SETLK";
}else{
s = fcntl(fd, op, p);
sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
return s;
}
if( p->l_type==F_RDLCK ){
zType = "RDLCK";
}else if( p->l_type==F_WRLCK ){
zType = "WRLCK";
}else if( p->l_type==F_UNLCK ){
zType = "UNLCK";
}else{
assert( 0 );
}
assert( p->l_whence==SEEK_SET );
s = fcntl(fd, op, p);
savedErrno = errno;
sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
(int)p->l_pid, s);
if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
struct flock l2;
l2 = *p;
fcntl(fd, F_GETLK, &l2);
if( l2.l_type==F_RDLCK ){
zType = "RDLCK";
}else if( l2.l_type==F_WRLCK ){
zType = "WRLCK";
}else if( l2.l_type==F_UNLCK ){
zType = "UNLCK";
}else{
assert( 0 );
}
sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
}
errno = savedErrno;
return s;
}
#define fcntl lockTrace
#endif
static void *threadLockingTest(void *pArg){
struct threadTestData *pData = (struct threadTestData*)pArg;
pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
return pArg;
}
static void testThreadLockingBehavior(int fd_orig){
int fd;
struct threadTestData d[2];
pthread_t t[2];
fd = dup(fd_orig);
if( fd<0 ) return;
memset(d, 0, sizeof(d));
d[0].fd = fd;
d[0].lock.l_type = F_RDLCK;
d[0].lock.l_len = 1;
d[0].lock.l_start = 0;
d[0].lock.l_whence = SEEK_SET;
d[1] = d[0];
d[1].lock.l_type = F_WRLCK;
pthread_create(&t[0], 0, threadLockingTest, &d[0]);
pthread_create(&t[1], 0, threadLockingTest, &d[1]);
pthread_join(t[0], 0);
pthread_join(t[1], 0);
close(fd);
threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
}
#endif
static void releaseLockInfo(struct lockInfo *pLock){
assert( sqlite3OsInMutex(1) );
if (pLock == NULL)
return;
pLock->nRef--;
if( pLock->nRef==0 ){
sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
sqlite3ThreadSafeFree(pLock);
}
}
static void releaseOpenCnt(struct openCnt *pOpen){
assert( sqlite3OsInMutex(1) );
if (pOpen == NULL)
return;
pOpen->nRef--;
if( pOpen->nRef==0 ){
sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
free(pOpen->aPending);
sqlite3ThreadSafeFree(pOpen);
}
}
#ifdef SQLITE_ENABLE_LOCKING_STYLE
static sqlite3LockingStyle sqlite3TestLockingStyle(const char *filePath,
int fd) {
struct flock lockInfo;
lockInfo.l_len = 1;
lockInfo.l_start = 0;
lockInfo.l_whence = SEEK_SET;
lockInfo.l_type = F_RDLCK;
if (fcntl(fd, F_GETLK, &lockInfo) != -1) {
return posixLockingStyle;
}
return dotlockLockingStyle;
}
static sqlite3LockingStyle sqlite3DetectLockingStyle(const char *filePath,
int fd) {
#ifdef SQLITE_FIXED_LOCKING_STYLE
return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE;
#else
struct statfs fsInfo;
if (statfs(filePath, &fsInfo) == -1)
return sqlite3TestLockingStyle(filePath, fd);
if (fsInfo.f_flags & MNT_RDONLY)
return noLockingStyle;
if( (!strcmp(fsInfo.f_fstypename, "hfs")) ||
(!strcmp(fsInfo.f_fstypename, "ufs")) )
return posixLockingStyle;
if(!strcmp(fsInfo.f_fstypename, "afpfs"))
return afpLockingStyle;
if(!strcmp(fsInfo.f_fstypename, "nfs"))
return sqlite3TestLockingStyle(filePath, fd);
if(!strcmp(fsInfo.f_fstypename, "smbfs"))
return flockLockingStyle;
if(!strcmp(fsInfo.f_fstypename, "msdos"))
return dotlockLockingStyle;
if(!strcmp(fsInfo.f_fstypename, "webdav"))
return unsupportedLockingStyle;
return sqlite3TestLockingStyle(filePath, fd);
#endif // SQLITE_FIXED_LOCKING_STYLE
}
#endif
static int findLockInfo(
int fd,
struct lockInfo **ppLock,
struct openCnt **ppOpen
){
int rc;
struct lockKey key1;
struct openKey key2;
struct stat statbuf;
struct lockInfo *pLock;
struct openCnt *pOpen;
rc = fstat(fd, &statbuf);
if( rc!=0 ) return 1;
assert( sqlite3OsInMutex(1) );
memset(&key1, 0, sizeof(key1));
key1.dev = statbuf.st_dev;
key1.ino = statbuf.st_ino;
#ifdef SQLITE_UNIX_THREADS
if( threadsOverrideEachOthersLocks<0 ){
testThreadLockingBehavior(fd);
}
key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
#endif
memset(&key2, 0, sizeof(key2));
key2.dev = statbuf.st_dev;
key2.ino = statbuf.st_ino;
pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
if( pLock==0 ){
struct lockInfo *pOld;
pLock = sqlite3ThreadSafeMalloc( sizeof(*pLock) );
if( pLock==0 ){
rc = 1;
goto exit_findlockinfo;
}
pLock->key = key1;
pLock->nRef = 1;
pLock->cnt = 0;
pLock->locktype = 0;
pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
if( pOld!=0 ){
assert( pOld==pLock );
sqlite3ThreadSafeFree(pLock);
rc = 1;
goto exit_findlockinfo;
}
}else{
pLock->nRef++;
}
*ppLock = pLock;
if( ppOpen!=0 ){
pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
if( pOpen==0 ){
struct openCnt *pOld;
pOpen = sqlite3ThreadSafeMalloc( sizeof(*pOpen) );
if( pOpen==0 ){
releaseLockInfo(pLock);
rc = 1;
goto exit_findlockinfo;
}
pOpen->key = key2;
pOpen->nRef = 1;
pOpen->nLock = 0;
pOpen->nPending = 0;
pOpen->aPending = 0;
pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
if( pOld!=0 ){
assert( pOld==pOpen );
sqlite3ThreadSafeFree(pOpen);
releaseLockInfo(pLock);
rc = 1;
goto exit_findlockinfo;
}
}else{
pOpen->nRef++;
}
*ppOpen = pOpen;
}
exit_findlockinfo:
return rc;
}
#ifdef SQLITE_DEBUG
static const char *locktypeName(int locktype){
switch( locktype ){
case NO_LOCK: return "NONE";
case SHARED_LOCK: return "SHARED";
case RESERVED_LOCK: return "RESERVED";
case PENDING_LOCK: return "PENDING";
case EXCLUSIVE_LOCK: return "EXCLUSIVE";
}
return "ERROR";
}
#endif
#ifdef SQLITE_UNIX_THREADS
static int transferOwnership(unixFile *pFile){
int rc;
pthread_t hSelf;
if( threadsOverrideEachOthersLocks ){
return SQLITE_OK;
}
hSelf = pthread_self();
if( pthread_equal(pFile->tid, hSelf) ){
OSTRACE1("No-transfer, same thread\n");
return SQLITE_OK;
}
if( pFile->locktype!=NO_LOCK ){
return SQLITE_MISUSE;
}
OSTRACE4("Transfer ownership of %d from %d to %d\n",
pFile->h, pFile->tid, hSelf);
pFile->tid = hSelf;
if (pFile->pLock != NULL) {
releaseLockInfo(pFile->pLock);
rc = findLockInfo(pFile->h, &pFile->pLock, 0);
OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
locktypeName(pFile->locktype),
locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
return rc;
} else {
return SQLITE_OK;
}
}
#else
# define transferOwnership(X) SQLITE_OK
#endif
int sqlite3UnixDelete(const char *zFilename){
SimulateIOError(return SQLITE_IOERR_DELETE);
unlink(zFilename);
return SQLITE_OK;
}
int sqlite3UnixFileExists(const char *zFilename){
return access(zFilename, 0)==0;
}
static int allocateUnixFile(
int h,
OsFile **pId,
const char *zFilename,
int delFlag
);
int sqlite3UnixOpenReadWrite(
const char *zFilename,
OsFile **pId,
int *pReadonly
){
int h;
CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadWrite, zFilename, pId, pReadonly);
assert( 0==*pId );
h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY,
SQLITE_DEFAULT_FILE_PERMISSIONS);
if( h<0 ){
#ifdef EISDIR
if( errno==EISDIR ){
return SQLITE_CANTOPEN;
}
#endif
h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
if( h<0 ){
return SQLITE_CANTOPEN;
}
*pReadonly = 1;
}else{
*pReadonly = 0;
}
return allocateUnixFile(h, pId, zFilename, 0);
}
int sqlite3UnixOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){
int h;
CRASH_TEST_OVERRIDE(sqlite3CrashOpenExclusive, zFilename, pId, delFlag);
assert( 0==*pId );
h = open(zFilename,
O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY,
delFlag ? 0600 : SQLITE_DEFAULT_FILE_PERMISSIONS);
if( h<0 ){
return SQLITE_CANTOPEN;
}
return allocateUnixFile(h, pId, zFilename, delFlag);
}
int sqlite3UnixOpenReadOnly(const char *zFilename, OsFile **pId){
int h;
CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadOnly, zFilename, pId, 0);
assert( 0==*pId );
h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
if( h<0 ){
return SQLITE_CANTOPEN;
}
return allocateUnixFile(h, pId, zFilename, 0);
}
static int unixOpenDirectory(
OsFile *id,
const char *zDirname
){
unixFile *pFile = (unixFile*)id;
assert( pFile!=0 );
SET_THREADID(pFile);
assert( pFile->dirfd<0 );
pFile->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0);
if( pFile->dirfd<0 ){
return SQLITE_CANTOPEN;
}
OSTRACE3("OPENDIR %-3d %s\n", pFile->dirfd, zDirname);
return SQLITE_OK;
}
int sqlite3UnixTempFileName(char *zBuf){
static const char *azDirs[] = {
0,
"/var/tmp",
"/usr/tmp",
"/tmp",
".",
};
static const unsigned char zChars[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
int i, j;
struct stat buf;
const char *zDir = ".";
azDirs[0] = sqlite3_temp_directory;
for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
if( azDirs[i]==0 ) continue;
if( stat(azDirs[i], &buf) ) continue;
if( !S_ISDIR(buf.st_mode) ) continue;
if( access(azDirs[i], 07) ) continue;
zDir = azDirs[i];
break;
}
do{
sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
j = strlen(zBuf);
sqlite3Randomness(15, &zBuf[j]);
for(i=0; i<15; i++, j++){
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
}
zBuf[j] = 0;
}while( access(zBuf,0)==0 );
return SQLITE_OK;
}
int sqlite3UnixIsDirWritable(char *zBuf){
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
struct stat buf;
if( zBuf==0 ) return 0;
if( zBuf[0]==0 ) return 0;
if( stat(zBuf, &buf) ) return 0;
if( !S_ISDIR(buf.st_mode) ) return 0;
if( access(zBuf, 07) ) return 0;
#endif
return 1;
}
static int seekAndRead(unixFile *id, void *pBuf, int cnt){
int got;
i64 newOffset;
TIMER_START;
#if defined(USE_PREAD)
got = pread(id->h, pBuf, cnt, id->offset);
SimulateIOError( got = -1 );
#elif defined(USE_PREAD64)
got = pread64(id->h, pBuf, cnt, id->offset);
SimulateIOError( got = -1 );
#else
newOffset = lseek(id->h, id->offset, SEEK_SET);
SimulateIOError( newOffset-- );
if( newOffset!=id->offset ){
return -1;
}
got = read(id->h, pBuf, cnt);
#endif
TIMER_END;
OSTRACE5("READ %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
if( got>0 ){
id->offset += got;
}
return got;
}
static int unixRead(OsFile *id, void *pBuf, int amt){
int got;
assert( id );
got = seekAndRead((unixFile*)id, pBuf, amt);
if( got==amt ){
return SQLITE_OK;
}else if( got<0 ){
return SQLITE_IOERR_READ;
}else{
memset(&((char*)pBuf)[got], 0, amt-got);
return SQLITE_IOERR_SHORT_READ;
}
}
static int seekAndWrite(unixFile *id, const void *pBuf, int cnt){
int got;
i64 newOffset;
TIMER_START;
#if defined(USE_PREAD)
got = pwrite(id->h, pBuf, cnt, id->offset);
#elif defined(USE_PREAD64)
got = pwrite64(id->h, pBuf, cnt, id->offset);
#else
newOffset = lseek(id->h, id->offset, SEEK_SET);
if( newOffset!=id->offset ){
return -1;
}
got = write(id->h, pBuf, cnt);
#endif
TIMER_END;
OSTRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
if( got>0 ){
id->offset += got;
}
return got;
}
static int unixWrite(OsFile *id, const void *pBuf, int amt){
int wrote = 0;
assert( id );
assert( amt>0 );
while( amt>0 && (wrote = seekAndWrite((unixFile*)id, pBuf, amt))>0 ){
amt -= wrote;
pBuf = &((char*)pBuf)[wrote];
}
SimulateIOError(( wrote=(-1), amt=1 ));
SimulateDiskfullError(( wrote=0, amt=1 ));
if( amt>0 ){
if( wrote<0 ){
return SQLITE_IOERR_WRITE;
}else{
return SQLITE_FULL;
}
}
return SQLITE_OK;
}
static int unixSeek(OsFile *id, i64 offset){
assert( id );
#ifdef SQLITE_TEST
if( offset ) SimulateDiskfullError(return SQLITE_FULL);
#endif
((unixFile*)id)->offset = offset;
return SQLITE_OK;
}
#ifdef SQLITE_TEST
int sqlite3_sync_count = 0;
int sqlite3_fullsync_count = 0;
#endif
#ifndef HAVE_FDATASYNC
# define fdatasync fsync
#endif
#ifdef F_FULLFSYNC
# define HAVE_FULLFSYNC 1
#else
# define HAVE_FULLFSYNC 0
#endif
static int full_fsync(int fd, int fullSync, int dataOnly){
int rc;
#ifdef SQLITE_TEST
if( fullSync ) sqlite3_fullsync_count++;
sqlite3_sync_count++;
#endif
#ifdef SQLITE_NO_SYNC
rc = SQLITE_OK;
#else
#if HAVE_FULLFSYNC
if( fullSync ){
rc = fcntl(fd, F_FULLFSYNC, 0);
}else{
rc = 1;
}
if( rc ) rc = fsync(fd);
#else
if( dataOnly ){
rc = fdatasync(fd);
}else{
rc = fsync(fd);
}
#endif
#endif
return rc;
}
static int unixSync(OsFile *id, int dataOnly){
int rc;
unixFile *pFile = (unixFile*)id;
assert( pFile );
OSTRACE2("SYNC %-3d\n", pFile->h);
rc = full_fsync(pFile->h, pFile->fullSync, dataOnly);
SimulateIOError( rc=1 );
if( rc ){
return SQLITE_IOERR_FSYNC;
}
if( pFile->dirfd>=0 ){
OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
HAVE_FULLFSYNC, pFile->fullSync);
#ifndef SQLITE_DISABLE_DIRSYNC
if( (!HAVE_FULLFSYNC || !pFile->fullSync) && full_fsync(pFile->dirfd,0,0) ){
}
#endif
close(pFile->dirfd);
pFile->dirfd = -1;
}
return SQLITE_OK;
}
int sqlite3UnixSyncDirectory(const char *zDirname){
#ifdef SQLITE_DISABLE_DIRSYNC
return SQLITE_OK;
#else
int fd;
int r;
fd = open(zDirname, O_RDONLY|O_BINARY, 0);
OSTRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
if( fd<0 ){
return SQLITE_CANTOPEN;
}
r = fsync(fd);
close(fd);
SimulateIOError( r=1 );
if( r ){
return SQLITE_IOERR_DIR_FSYNC;
}else{
return SQLITE_OK;
}
#endif
}
static int unixTruncate(OsFile *id, i64 nByte){
int rc;
assert( id );
rc = ftruncate(((unixFile*)id)->h, nByte);
SimulateIOError( rc=1 );
if( rc ){
return SQLITE_IOERR_TRUNCATE;
}else{
return SQLITE_OK;
}
}
static int unixFileSize(OsFile *id, i64 *pSize){
int rc;
struct stat buf;
assert( id );
rc = fstat(((unixFile*)id)->h, &buf);
SimulateIOError( rc=1 );
if( rc!=0 ){
return SQLITE_IOERR_FSTAT;
}
*pSize = buf.st_size;
return SQLITE_OK;
}
static int unixCheckReservedLock(OsFile *id){
int r = 0;
unixFile *pFile = (unixFile*)id;
assert( pFile );
sqlite3OsEnterMutex();
if( pFile->pLock->locktype>SHARED_LOCK ){
r = 1;
}
if( !r ){
struct flock lock;
lock.l_whence = SEEK_SET;
lock.l_start = RESERVED_BYTE;
lock.l_len = 1;
lock.l_type = F_WRLCK;
fcntl(pFile->h, F_GETLK, &lock);
if( lock.l_type!=F_UNLCK ){
r = 1;
}
}
sqlite3OsLeaveMutex();
OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
return r;
}
static int unixLock(OsFile *id, int locktype){
int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id;
struct lockInfo *pLock = pFile->pLock;
struct flock lock;
int s;
assert( pFile );
OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
locktypeName(locktype), locktypeName(pFile->locktype),
locktypeName(pLock->locktype), pLock->cnt , getpid());
if( pFile->locktype>=locktype ){
OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
locktypeName(locktype));
return SQLITE_OK;
}
assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
assert( locktype!=PENDING_LOCK );
assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
sqlite3OsEnterMutex();
rc = transferOwnership(pFile);
if( rc!=SQLITE_OK ){
sqlite3OsLeaveMutex();
return rc;
}
pLock = pFile->pLock;
if( (pFile->locktype!=pLock->locktype &&
(pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
){
rc = SQLITE_BUSY;
goto end_lock;
}
if( locktype==SHARED_LOCK &&
(pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
assert( locktype==SHARED_LOCK );
assert( pFile->locktype==0 );
assert( pLock->cnt>0 );
pFile->locktype = SHARED_LOCK;
pLock->cnt++;
pFile->pOpen->nLock++;
goto end_lock;
}
lock.l_len = 1L;
lock.l_whence = SEEK_SET;
if( locktype==SHARED_LOCK
|| (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
){
lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
lock.l_start = PENDING_BYTE;
s = fcntl(pFile->h, F_SETLK, &lock);
if( s==(-1) ){
rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
goto end_lock;
}
}
if( locktype==SHARED_LOCK ){
assert( pLock->cnt==0 );
assert( pLock->locktype==0 );
lock.l_start = SHARED_FIRST;
lock.l_len = SHARED_SIZE;
s = fcntl(pFile->h, F_SETLK, &lock);
lock.l_start = PENDING_BYTE;
lock.l_len = 1L;
lock.l_type = F_UNLCK;
if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
rc = SQLITE_IOERR_UNLOCK;
goto end_lock;
}
if( s==(-1) ){
rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
}else{
pFile->locktype = SHARED_LOCK;
pFile->pOpen->nLock++;
pLock->cnt = 1;
}
}else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
rc = SQLITE_BUSY;
}else{
assert( 0!=pFile->locktype );
lock.l_type = F_WRLCK;
switch( locktype ){
case RESERVED_LOCK:
lock.l_start = RESERVED_BYTE;
break;
case EXCLUSIVE_LOCK:
lock.l_start = SHARED_FIRST;
lock.l_len = SHARED_SIZE;
break;
default:
assert(0);
}
s = fcntl(pFile->h, F_SETLK, &lock);
if( s==(-1) ){
rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
}
}
if( rc==SQLITE_OK ){
pFile->locktype = locktype;
pLock->locktype = locktype;
}else if( locktype==EXCLUSIVE_LOCK ){
pFile->locktype = PENDING_LOCK;
pLock->locktype = PENDING_LOCK;
}
end_lock:
sqlite3OsLeaveMutex();
OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
rc==SQLITE_OK ? "ok" : "failed");
return rc;
}
static int unixUnlock(OsFile *id, int locktype){
struct lockInfo *pLock;
struct flock lock;
int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id;
assert( pFile );
OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
assert( locktype<=SHARED_LOCK );
if( pFile->locktype<=locktype ){
return SQLITE_OK;
}
if( CHECK_THREADID(pFile) ){
return SQLITE_MISUSE;
}
sqlite3OsEnterMutex();
pLock = pFile->pLock;
assert( pLock->cnt!=0 );
if( pFile->locktype>SHARED_LOCK ){
assert( pLock->locktype==pFile->locktype );
if( locktype==SHARED_LOCK ){
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = SHARED_FIRST;
lock.l_len = SHARED_SIZE;
if( fcntl(pFile->h, F_SETLK, &lock)==(-1) ){
rc = SQLITE_IOERR_RDLOCK;
}
}
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = PENDING_BYTE;
lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
pLock->locktype = SHARED_LOCK;
}else{
rc = SQLITE_IOERR_UNLOCK;
}
}
if( locktype==NO_LOCK ){
struct openCnt *pOpen;
pLock->cnt--;
if( pLock->cnt==0 ){
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
pLock->locktype = NO_LOCK;
}else{
rc = SQLITE_IOERR_UNLOCK;
}
}
pOpen = pFile->pOpen;
pOpen->nLock--;
assert( pOpen->nLock>=0 );
if( pOpen->nLock==0 && pOpen->nPending>0 ){
int i;
for(i=0; i<pOpen->nPending; i++){
close(pOpen->aPending[i]);
}
free(pOpen->aPending);
pOpen->nPending = 0;
pOpen->aPending = 0;
}
}
sqlite3OsLeaveMutex();
pFile->locktype = locktype;
return rc;
}
static int unixClose(OsFile **pId){
unixFile *id = (unixFile*)*pId;
if( !id ) return SQLITE_OK;
unixUnlock(*pId, NO_LOCK);
if( id->dirfd>=0 ) close(id->dirfd);
id->dirfd = -1;
sqlite3OsEnterMutex();
if( id->pOpen->nLock ){
int *aNew;
struct openCnt *pOpen = id->pOpen;
aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
if( aNew==0 ){
}else{
pOpen->aPending = aNew;
pOpen->aPending[pOpen->nPending] = id->h;
pOpen->nPending++;
}
}else{
close(id->h);
}
releaseLockInfo(id->pLock);
releaseOpenCnt(id->pOpen);
sqlite3OsLeaveMutex();
id->isOpen = 0;
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
#ifdef SQLITE_ENABLE_LOCKING_STYLE
#pragma mark AFP Support
typedef struct afpLockingContext afpLockingContext;
struct afpLockingContext {
unsigned long long sharedLockByte;
char *filePath;
};
struct ByteRangeLockPB2
{
unsigned long long offset;
unsigned long long length;
unsigned long long retRangeStart;
unsigned char unLockFlag;
unsigned char startEndFlag;
int fd;
};
#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
static int _AFPFSSetLock(const char *path, int fd, unsigned long long offset,
unsigned long long length, int setLockFlag)
{
struct ByteRangeLockPB2 pb;
int err;
pb.unLockFlag = setLockFlag ? 0 : 1;
pb.startEndFlag = 0;
pb.offset = offset;
pb.length = length;
pb.fd = fd;
OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
(setLockFlag?"ON":"OFF"), fd, offset, length);
err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
if ( err==-1 ) {
OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
strerror(errno));
return 1; } else {
return 0;
}
}
static int afpUnixCheckReservedLock(OsFile *id){
int r = 0;
unixFile *pFile = (unixFile*)id;
assert( pFile );
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
if( pFile->locktype>SHARED_LOCK ){
r = 1;
}
if ( !r ) {
int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
if (failed) {
r = 1;
} else {
_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
}
}
OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
return r;
}
static int afpUnixLock(OsFile *id, int locktype)
{
int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id;
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
int gotPendingLock = 0;
assert( pFile );
OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
locktypeName(locktype), locktypeName(pFile->locktype), getpid());
if( pFile->locktype>=locktype ){
OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
locktypeName(locktype));
return SQLITE_OK;
}
assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
assert( locktype!=PENDING_LOCK );
assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
sqlite3OsEnterMutex();
rc = transferOwnership(pFile);
if( rc!=SQLITE_OK ){
sqlite3OsLeaveMutex();
return rc;
}
if( locktype==SHARED_LOCK
|| (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
){
int failed = _AFPFSSetLock(context->filePath, pFile->h,
PENDING_BYTE, 1, 1);
if (failed) {
rc = SQLITE_BUSY;
goto afp_end_lock;
}
}
if( locktype==SHARED_LOCK ){
int lk, failed;
int tries = 0;
lk = random();
context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
failed = _AFPFSSetLock(context->filePath, pFile->h,
SHARED_FIRST+context->sharedLockByte, 1, 1);
if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)) {
rc = SQLITE_IOERR_UNLOCK;
goto afp_end_lock;
}
if( failed ){
rc = SQLITE_BUSY;
} else {
pFile->locktype = SHARED_LOCK;
}
}else{
int failed = 0;
assert( 0!=pFile->locktype );
if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
}
if (!failed && locktype == EXCLUSIVE_LOCK) {
if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
context->sharedLockByte, 1, 0)) {
failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
SHARED_SIZE, 1);
if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
context->sharedLockByte, 1, 1)) {
rc = SQLITE_IOERR_RDLOCK;
}
} else {
rc = SQLITE_IOERR_UNLOCK;
}
}
if( failed && rc == SQLITE_OK){
rc = SQLITE_BUSY;
}
}
if( rc==SQLITE_OK ){
pFile->locktype = locktype;
}else if( locktype==EXCLUSIVE_LOCK ){
pFile->locktype = PENDING_LOCK;
}
afp_end_lock:
sqlite3OsLeaveMutex();
OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
rc==SQLITE_OK ? "ok" : "failed");
return rc;
}
static int afpUnixUnlock(OsFile *id, int locktype) {
struct flock lock;
int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id;
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
assert( pFile );
OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
pFile->locktype, getpid());
assert( locktype<=SHARED_LOCK );
if( pFile->locktype<=locktype ){
return SQLITE_OK;
}
if( CHECK_THREADID(pFile) ){
return SQLITE_MISUSE;
}
sqlite3OsEnterMutex();
if( pFile->locktype>SHARED_LOCK ){
if( locktype==SHARED_LOCK ){
int failed = 0;
if (pFile->locktype==EXCLUSIVE_LOCK) {
failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
SHARED_SIZE, 0);
if (!failed) {
if (_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST+
context->sharedLockByte, 1, 1)) {
rc = SQLITE_IOERR_RDLOCK;
}
} else {
rc = SQLITE_IOERR_UNLOCK;
}
}
}
if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)){
rc = SQLITE_IOERR_UNLOCK;
}
}
if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
if (_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0)) {
rc = SQLITE_IOERR_UNLOCK;
}
}
}
if( locktype==NO_LOCK ){
int failed = _AFPFSSetLock(context->filePath, pFile->h,
SHARED_FIRST + context->sharedLockByte, 1, 0);
if (failed) {
rc = SQLITE_IOERR_UNLOCK;
}
}
if (rc == SQLITE_OK)
pFile->locktype = locktype;
sqlite3OsLeaveMutex();
return rc;
}
static int afpUnixClose(OsFile **pId) {
unixFile *id = (unixFile*)*pId;
if( !id ) return SQLITE_OK;
afpUnixUnlock(*pId, NO_LOCK);
if (id->lockingContext != NULL) {
if (((afpLockingContext *)id->lockingContext)->filePath != NULL)
sqlite3ThreadSafeFree(((afpLockingContext*)id->lockingContext)->filePath);
sqlite3ThreadSafeFree(id->lockingContext);
}
if( id->dirfd>=0 ) close(id->dirfd);
id->dirfd = -1;
close(id->h);
id->isOpen = 0;
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
#pragma mark flock() style locking
typedef void flockLockingContext;
static int flockUnixCheckReservedLock(OsFile *id) {
unixFile *pFile = (unixFile*)id;
if (pFile->locktype == RESERVED_LOCK) {
return 1; } else {
int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
if (!rc) {
flock(pFile->h, LOCK_UN);
return 0; }
return 1; }
}
static int flockUnixLock(OsFile *id, int locktype) {
unixFile *pFile = (unixFile*)id;
if (pFile->locktype > NO_LOCK) {
pFile->locktype = locktype;
return SQLITE_OK;
}
int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
if (rc) {
return SQLITE_BUSY;
} else {
pFile->locktype = locktype;
return SQLITE_OK;
}
}
static int flockUnixUnlock(OsFile *id, int locktype) {
unixFile *pFile = (unixFile*)id;
assert( locktype<=SHARED_LOCK );
if( pFile->locktype==locktype ){
return SQLITE_OK;
}
if (locktype==SHARED_LOCK) {
pFile->locktype = locktype;
return SQLITE_OK;
}
int rc = flock(pFile->h, LOCK_UN);
if (rc)
return SQLITE_IOERR_UNLOCK;
else {
pFile->locktype = NO_LOCK;
return SQLITE_OK;
}
}
static int flockUnixClose(OsFile **pId) {
unixFile *id = (unixFile*)*pId;
if( !id ) return SQLITE_OK;
flockUnixUnlock(*pId, NO_LOCK);
if( id->dirfd>=0 ) close(id->dirfd);
id->dirfd = -1;
sqlite3OsEnterMutex();
close(id->h);
sqlite3OsLeaveMutex();
id->isOpen = 0;
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
#pragma mark Old-School .lock file based locking
typedef struct dotlockLockingContext dotlockLockingContext;
struct dotlockLockingContext {
char *lockPath;
};
static int dotlockUnixCheckReservedLock(OsFile *id) {
unixFile *pFile = (unixFile*)id;
dotlockLockingContext *context =
(dotlockLockingContext *) pFile->lockingContext;
if (pFile->locktype == RESERVED_LOCK) {
return 1; } else {
struct stat statBuf;
if (lstat(context->lockPath,&statBuf) == 0)
return 1;
else
return 0;
}
}
static int dotlockUnixLock(OsFile *id, int locktype) {
unixFile *pFile = (unixFile*)id;
dotlockLockingContext *context =
(dotlockLockingContext *) pFile->lockingContext;
if (pFile->locktype > NO_LOCK) {
pFile->locktype = locktype;
utimes(context->lockPath,NULL);
return SQLITE_OK;
}
struct stat statBuf;
if (lstat(context->lockPath,&statBuf) == 0){
return SQLITE_BUSY; }
int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600);
if (fd < 0) {
return SQLITE_BUSY;
}
close(fd);
pFile->locktype = locktype;
return SQLITE_OK;
}
static int dotlockUnixUnlock(OsFile *id, int locktype) {
unixFile *pFile = (unixFile*)id;
dotlockLockingContext *context =
(dotlockLockingContext *) pFile->lockingContext;
assert( locktype<=SHARED_LOCK );
if( pFile->locktype==locktype ){
return SQLITE_OK;
}
if (locktype==SHARED_LOCK) {
pFile->locktype = locktype;
return SQLITE_OK;
}
unlink(context->lockPath);
pFile->locktype = NO_LOCK;
return SQLITE_OK;
}
static int dotlockUnixClose(OsFile **pId) {
unixFile *id = (unixFile*)*pId;
if( !id ) return SQLITE_OK;
dotlockUnixUnlock(*pId, NO_LOCK);
if (id->lockingContext != NULL) {
if (((dotlockLockingContext *)id->lockingContext)->lockPath != NULL)
sqlite3ThreadSafeFree( ( (dotlockLockingContext *)
id->lockingContext)->lockPath);
sqlite3ThreadSafeFree(id->lockingContext);
}
if( id->dirfd>=0 ) close(id->dirfd);
id->dirfd = -1;
sqlite3OsEnterMutex();
close(id->h);
sqlite3OsLeaveMutex();
id->isOpen = 0;
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
#pragma mark No locking
typedef void nolockLockingContext;
static int nolockUnixCheckReservedLock(OsFile *id) {
return 0;
}
static int nolockUnixLock(OsFile *id, int locktype) {
return SQLITE_OK;
}
static int nolockUnixUnlock(OsFile *id, int locktype) {
return SQLITE_OK;
}
static int nolockUnixClose(OsFile **pId) {
unixFile *id = (unixFile*)*pId;
if( !id ) return SQLITE_OK;
if( id->dirfd>=0 ) close(id->dirfd);
id->dirfd = -1;
sqlite3OsEnterMutex();
close(id->h);
sqlite3OsLeaveMutex();
id->isOpen = 0;
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
#endif
char *sqlite3UnixFullPathname(const char *zRelative){
char *zFull = 0;
if( zRelative[0]=='/' ){
sqlite3SetString(&zFull, zRelative, (char*)0);
}else{
char *zBuf = sqliteMalloc(5000);
if( zBuf==0 ){
return 0;
}
zBuf[0] = 0;
sqlite3SetString(&zFull, getcwd(zBuf, 5000), "/", zRelative,
(char*)0);
sqliteFree(zBuf);
}
#if 0
if( zFull ){
int i, j;
for(i=j=0; zFull[i]; i++){
if( zFull[i]=='/' ){
if( zFull[i+1]=='/' ) continue;
if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
i += 1;
continue;
}
if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
while( j>0 && zFull[j-1]!='/' ){ j--; }
i += 3;
continue;
}
}
zFull[j++] = zFull[i];
}
zFull[j] = 0;
}
#endif
return zFull;
}
static void unixSetFullSync(OsFile *id, int v){
((unixFile*)id)->fullSync = v;
}
static int unixFileHandle(OsFile *id){
return ((unixFile*)id)->h;
}
static int unixLockState(OsFile *id){
return ((unixFile*)id)->locktype;
}
static int unixSectorSize(OsFile *id){
return SQLITE_DEFAULT_SECTOR_SIZE;
}
static const IoMethod sqlite3UnixIoMethod = {
unixClose,
unixOpenDirectory,
unixRead,
unixWrite,
unixSeek,
unixTruncate,
unixSync,
unixSetFullSync,
unixFileHandle,
unixFileSize,
unixLock,
unixUnlock,
unixLockState,
unixCheckReservedLock,
unixSectorSize,
};
#ifdef SQLITE_ENABLE_LOCKING_STYLE
static const IoMethod sqlite3AFPLockingUnixIoMethod = {
afpUnixClose,
unixOpenDirectory,
unixRead,
unixWrite,
unixSeek,
unixTruncate,
unixSync,
unixSetFullSync,
unixFileHandle,
unixFileSize,
afpUnixLock,
afpUnixUnlock,
unixLockState,
afpUnixCheckReservedLock,
unixSectorSize,
};
static const IoMethod sqlite3FlockLockingUnixIoMethod = {
flockUnixClose,
unixOpenDirectory,
unixRead,
unixWrite,
unixSeek,
unixTruncate,
unixSync,
unixSetFullSync,
unixFileHandle,
unixFileSize,
flockUnixLock,
flockUnixUnlock,
unixLockState,
flockUnixCheckReservedLock,
unixSectorSize,
};
static const IoMethod sqlite3DotlockLockingUnixIoMethod = {
dotlockUnixClose,
unixOpenDirectory,
unixRead,
unixWrite,
unixSeek,
unixTruncate,
unixSync,
unixSetFullSync,
unixFileHandle,
unixFileSize,
dotlockUnixLock,
dotlockUnixUnlock,
unixLockState,
dotlockUnixCheckReservedLock,
unixSectorSize,
};
static const IoMethod sqlite3NolockLockingUnixIoMethod = {
nolockUnixClose,
unixOpenDirectory,
unixRead,
unixWrite,
unixSeek,
unixTruncate,
unixSync,
unixSetFullSync,
unixFileHandle,
unixFileSize,
nolockUnixLock,
nolockUnixUnlock,
unixLockState,
nolockUnixCheckReservedLock,
unixSectorSize,
};
#endif
#ifdef SQLITE_ENABLE_LOCKING_STYLE
static int allocateUnixFile(
int h,
OsFile **pId,
const char *zFilename,
int delFlag
){
sqlite3LockingStyle lockingStyle;
unixFile *pNew;
unixFile f;
int rc;
memset(&f, 0, sizeof(f));
lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
if ( lockingStyle == posixLockingStyle ) {
sqlite3OsEnterMutex();
rc = findLockInfo(h, &f.pLock, &f.pOpen);
sqlite3OsLeaveMutex();
if( rc ){
close(h);
unlink(zFilename);
return SQLITE_NOMEM;
}
} else {
f.pLock = NULL;
f.pOpen = NULL;
}
if( delFlag ){
unlink(zFilename);
}
f.dirfd = -1;
f.h = h;
SET_THREADID(&f);
pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
if( pNew==0 ){
close(h);
sqlite3OsEnterMutex();
releaseLockInfo(f.pLock);
releaseOpenCnt(f.pOpen);
sqlite3OsLeaveMutex();
*pId = 0;
return SQLITE_NOMEM;
}else{
*pNew = f;
switch(lockingStyle) {
case afpLockingStyle:
pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
pNew->lockingContext =
sqlite3ThreadSafeMalloc(sizeof(afpLockingContext));
((afpLockingContext *)pNew->lockingContext)->filePath =
sqlite3ThreadSafeMalloc(strlen(zFilename) + 1);
strcpy(((afpLockingContext *)pNew->lockingContext)->filePath,
zFilename);
srandomdev();
break;
case flockLockingStyle:
pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
break;
case dotlockLockingStyle:
pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
pNew->lockingContext = sqlite3ThreadSafeMalloc(
sizeof(dotlockLockingContext));
((dotlockLockingContext *)pNew->lockingContext)->lockPath =
sqlite3ThreadSafeMalloc(strlen(zFilename) + strlen(".lock") + 1);
sprintf(((dotlockLockingContext *)pNew->lockingContext)->lockPath,
"%s.lock", zFilename);
break;
case posixLockingStyle:
pNew->pMethod = &sqlite3UnixIoMethod;
break;
case noLockingStyle:
case unsupportedLockingStyle:
default:
pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
}
*pId = (OsFile*)pNew;
OpenCounter(+1);
return SQLITE_OK;
}
}
#else
static int allocateUnixFile(
int h,
OsFile **pId,
const char *zFilename,
int delFlag
){
unixFile *pNew;
unixFile f;
int rc;
memset(&f, 0, sizeof(f));
sqlite3OsEnterMutex();
rc = findLockInfo(h, &f.pLock, &f.pOpen);
sqlite3OsLeaveMutex();
if( delFlag ){
unlink(zFilename);
}
if( rc ){
close(h);
return SQLITE_NOMEM;
}
OSTRACE3("OPEN %-3d %s\n", h, zFilename);
f.dirfd = -1;
f.h = h;
SET_THREADID(&f);
pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
if( pNew==0 ){
close(h);
sqlite3OsEnterMutex();
releaseLockInfo(f.pLock);
releaseOpenCnt(f.pOpen);
sqlite3OsLeaveMutex();
*pId = 0;
return SQLITE_NOMEM;
}else{
*pNew = f;
pNew->pMethod = &sqlite3UnixIoMethod;
*pId = (OsFile*)pNew;
OpenCounter(+1);
return SQLITE_OK;
}
}
#endif
#endif
#ifndef SQLITE_OMIT_LOAD_EXTENSION
#include <dlfcn.h>
void *sqlite3UnixDlopen(const char *zFilename){
return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
}
void *sqlite3UnixDlsym(void *pHandle, const char *zSymbol){
return dlsym(pHandle, zSymbol);
}
int sqlite3UnixDlclose(void *pHandle){
return dlclose(pHandle);
}
#endif
int sqlite3UnixRandomSeed(char *zBuf){
memset(zBuf, 0, 256);
#if !defined(SQLITE_TEST)
{
int pid, fd;
fd = open("/dev/urandom", O_RDONLY);
if( fd<0 ){
time_t t;
time(&t);
memcpy(zBuf, &t, sizeof(t));
pid = getpid();
memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
}else{
read(fd, zBuf, 256);
close(fd);
}
}
#endif
return SQLITE_OK;
}
int sqlite3UnixSleep(int ms){
#if defined(HAVE_USLEEP) && HAVE_USLEEP
usleep(ms*1000);
return ms;
#else
sleep((ms+999)/1000);
return 1000*((ms+999)/1000);
#endif
}
static int inMutex = 0;
#ifdef SQLITE_UNIX_THREADS
static pthread_t mutexOwner;
static int mutexOwnerValid = 0;
static pthread_mutex_t mutexMain = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutexAux = PTHREAD_MUTEX_INITIALIZER;
#endif
void sqlite3UnixEnterMutex(){
#ifdef SQLITE_UNIX_THREADS
pthread_mutex_lock(&mutexAux);
if( !mutexOwnerValid || !pthread_equal(mutexOwner, pthread_self()) ){
pthread_mutex_unlock(&mutexAux);
pthread_mutex_lock(&mutexMain);
assert( inMutex==0 );
assert( !mutexOwnerValid );
pthread_mutex_lock(&mutexAux);
mutexOwner = pthread_self();
mutexOwnerValid = 1;
}
inMutex++;
pthread_mutex_unlock(&mutexAux);
#else
inMutex++;
#endif
}
void sqlite3UnixLeaveMutex(){
assert( inMutex>0 );
#ifdef SQLITE_UNIX_THREADS
pthread_mutex_lock(&mutexAux);
inMutex--;
assert( pthread_equal(mutexOwner, pthread_self()) );
if( inMutex==0 ){
assert( mutexOwnerValid );
mutexOwnerValid = 0;
pthread_mutex_unlock(&mutexMain);
}
pthread_mutex_unlock(&mutexAux);
#else
inMutex--;
#endif
}
int sqlite3UnixInMutex(int thisThrd){
#ifdef SQLITE_UNIX_THREADS
int rc;
pthread_mutex_lock(&mutexAux);
rc = inMutex>0 && (thisThrd==0 || pthread_equal(mutexOwner,pthread_self()));
pthread_mutex_unlock(&mutexAux);
return rc;
#else
return inMutex>0;
#endif
}
#ifdef SQLITE_TEST
int sqlite3_tsd_count = 0;
# ifdef SQLITE_UNIX_THREADS
static pthread_mutex_t tsd_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
# define TSD_COUNTER(N) \
pthread_mutex_lock(&tsd_counter_mutex); \
sqlite3_tsd_count += N; \
pthread_mutex_unlock(&tsd_counter_mutex);
# else
# define TSD_COUNTER(N) sqlite3_tsd_count += N
# endif
#else
# define TSD_COUNTER(N)
#endif
ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){
static const ThreadData zeroData = {0};
#ifdef SQLITE_UNIX_THREADS
static pthread_key_t key;
static int keyInit = 0;
ThreadData *pTsd;
if( !keyInit ){
sqlite3OsEnterMutex();
if( !keyInit ){
int rc;
rc = pthread_key_create(&key, 0);
if( rc ){
sqlite3OsLeaveMutex();
return 0;
}
keyInit = 1;
}
sqlite3OsLeaveMutex();
}
pTsd = pthread_getspecific(key);
if( allocateFlag>0 ){
if( pTsd==0 ){
if( !sqlite3TestMallocFail() ){
pTsd = sqlite3OsMalloc(sizeof(zeroData));
}
#ifdef SQLITE_MEMDEBUG
sqlite3_isFail = 0;
#endif
if( pTsd ){
*pTsd = zeroData;
pthread_setspecific(key, pTsd);
TSD_COUNTER(+1);
}
}
}else if( pTsd!=0 && allocateFlag<0
&& memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
sqlite3OsFree(pTsd);
pthread_setspecific(key, 0);
TSD_COUNTER(-1);
pTsd = 0;
}
return pTsd;
#else
static ThreadData *pTsd = 0;
if( allocateFlag>0 ){
if( pTsd==0 ){
if( !sqlite3TestMallocFail() ){
pTsd = sqlite3OsMalloc( sizeof(zeroData) );
}
#ifdef SQLITE_MEMDEBUG
sqlite3_isFail = 0;
#endif
if( pTsd ){
*pTsd = zeroData;
TSD_COUNTER(+1);
}
}
}else if( pTsd!=0 && allocateFlag<0
&& memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
sqlite3OsFree(pTsd);
TSD_COUNTER(-1);
pTsd = 0;
}
return pTsd;
#endif
}
#ifdef SQLITE_TEST
int sqlite3_current_time = 0;
#endif
int sqlite3UnixCurrentTime(double *prNow){
#ifdef NO_GETTOD
time_t t;
time(&t);
*prNow = t/86400.0 + 2440587.5;
#else
struct timeval sNow;
gettimeofday(&sNow, 0);
*prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
#endif
#ifdef SQLITE_TEST
if( sqlite3_current_time ){
*prNow = sqlite3_current_time/86400.0 + 2440587.5;
}
#endif
return 0;
}
#endif