blob: dc223d929e758bb328c4a5962fb72ad4d98d1392 [file] [log] [blame]
drhbbd42a62004-05-22 17:41:58 +00001/*
2** 2004 May 22
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This file contains code that is specific to Unix systems.
14*/
drhbbd42a62004-05-22 17:41:58 +000015#include "sqliteInt.h"
drheb206252004-10-01 02:00:31 +000016#include "os.h"
17#if OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000018
drhbfe66312006-10-03 17:40:40 +000019/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */
20
drh9cbe6352005-11-29 03:13:21 +000021/*
22** These #defines should enable >2GB file support on Posix if the
23** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000024** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000025**
26** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
27** on the compiler command line. This is necessary if you are compiling
28** on a recent machine (ex: RedHat 7.2) but you want your code to work
29** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
30** without this option, LFS is enable. But LFS does not exist in the kernel
31** in RedHat 6.0, so the code won't work. Hence, for maximum binary
32** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000033*/
34#ifndef SQLITE_DISABLE_LFS
35# define _LARGE_FILE 1
36# ifndef _FILE_OFFSET_BITS
37# define _FILE_OFFSET_BITS 64
38# endif
39# define _LARGEFILE_SOURCE 1
40#endif
drhbbd42a62004-05-22 17:41:58 +000041
drh9cbe6352005-11-29 03:13:21 +000042/*
43** standard include files.
44*/
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <fcntl.h>
48#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000049#include <time.h>
drh19e2d372005-08-29 23:00:03 +000050#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000051#include <errno.h>
drhbfe66312006-10-03 17:40:40 +000052#ifdef SQLITE_ENABLE_LOCKING_STYLE
53#include <sys/ioctl.h>
54#include <sys/param.h>
55#include <sys/mount.h>
56#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000057
58/*
drhf1a221e2006-01-15 17:27:17 +000059** If we are to be thread-safe, include the pthreads header and define
60** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +000061*/
drh2c547df2007-04-01 18:46:19 +000062#ifndef THREADSAFE
63# define THREADSAFE 1
64#endif
65#if THREADSAFE
drh9cbe6352005-11-29 03:13:21 +000066# include <pthread.h>
67# define SQLITE_UNIX_THREADS 1
68#endif
69
70/*
71** Default permissions when creating a new file
72*/
73#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
74# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
75#endif
76
77
78
79/*
drh054889e2005-11-30 03:20:31 +000080** The unixFile structure is subclass of OsFile specific for the unix
81** protability layer.
drh9cbe6352005-11-29 03:13:21 +000082*/
drh054889e2005-11-30 03:20:31 +000083typedef struct unixFile unixFile;
84struct unixFile {
danielk197762079062007-08-15 17:08:46 +000085 sqlite3_io_methods const *pMethod; /* Always the first entry */
drh9cbe6352005-11-29 03:13:21 +000086 struct openCnt *pOpen; /* Info about all open fd's on this inode */
87 struct lockInfo *pLock; /* Info about locks on this inode */
drhbfe66312006-10-03 17:40:40 +000088#ifdef SQLITE_ENABLE_LOCKING_STYLE
89 void *lockingContext; /* Locking style specific state */
90#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000091 int h; /* The file descriptor */
92 unsigned char locktype; /* The type of lock held on this fd */
93 unsigned char isOpen; /* True if needs to be closed */
94 unsigned char fullSync; /* Use F_FULLSYNC if available */
95 int dirfd; /* File descriptor for the directory */
drhb912b282006-03-23 22:42:20 +000096 i64 offset; /* Seek offset */
drh9cbe6352005-11-29 03:13:21 +000097#ifdef SQLITE_UNIX_THREADS
drhf1a221e2006-01-15 17:27:17 +000098 pthread_t tid; /* The thread that "owns" this OsFile */
drh9cbe6352005-11-29 03:13:21 +000099#endif
100};
101
danielk197762079062007-08-15 17:08:46 +0000102
drh66560ad2006-01-06 14:32:19 +0000103/*
104** Provide the ability to override some OS-layer functions during
105** testing. This is used to simulate OS crashes to verify that
106** commits are atomic even in the event of an OS crash.
107*/
108#ifdef SQLITE_CRASH_TEST
109 extern int sqlite3CrashTestEnable;
danielk197762079062007-08-15 17:08:46 +0000110 int sqlite3CrashFileWrap(sqlite3_file *, const char *, sqlite3_file **);
111 static int CRASH_TEST_OVERRIDE(const char *zFile, sqlite3_file **pId, int rc){
112 if( rc==SQLITE_OK && sqlite3CrashTestEnable ){
113 rc = sqlite3CrashFileWrap(*pId, zFile, pId);
114 }
115 return rc;
116 }
drh66560ad2006-01-06 14:32:19 +0000117#else
danielk197762079062007-08-15 17:08:46 +0000118# define CRASH_TEST_OVERRIDE(A,B,C) C
drh66560ad2006-01-06 14:32:19 +0000119#endif
120
drh0ccebe72005-06-07 22:22:50 +0000121
122/*
drh198bf392006-01-06 21:52:49 +0000123** Include code that is common to all os_*.c files
124*/
125#include "os_common.h"
126
127/*
drh0ccebe72005-06-07 22:22:50 +0000128** Do not include any of the File I/O interface procedures if the
drhf1a221e2006-01-15 17:27:17 +0000129** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
drh0ccebe72005-06-07 22:22:50 +0000130** will be in-memory only)
131*/
132#ifndef SQLITE_OMIT_DISKIO
133
134
135/*
136** Define various macros that are missing from some systems.
137*/
drhbbd42a62004-05-22 17:41:58 +0000138#ifndef O_LARGEFILE
139# define O_LARGEFILE 0
140#endif
141#ifdef SQLITE_DISABLE_LFS
142# undef O_LARGEFILE
143# define O_LARGEFILE 0
144#endif
145#ifndef O_NOFOLLOW
146# define O_NOFOLLOW 0
147#endif
148#ifndef O_BINARY
149# define O_BINARY 0
150#endif
151
152/*
153** The DJGPP compiler environment looks mostly like Unix, but it
154** lacks the fcntl() system call. So redefine fcntl() to be something
155** that always succeeds. This means that locking does not occur under
danielk197726c5d792005-11-25 09:01:23 +0000156** DJGPP. But it's DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000157*/
158#ifdef __DJGPP__
159# define fcntl(A,B,C) 0
160#endif
161
162/*
drh2b4b5962005-06-15 17:47:55 +0000163** The threadid macro resolves to the thread-id or to 0. Used for
164** testing and debugging only.
165*/
166#ifdef SQLITE_UNIX_THREADS
167#define threadid pthread_self()
168#else
169#define threadid 0
170#endif
171
172/*
173** Set or check the OsFile.tid field. This field is set when an OsFile
174** is first opened. All subsequent uses of the OsFile verify that the
175** same thread is operating on the OsFile. Some operating systems do
176** not allow locks to be overridden by other threads and that restriction
177** means that sqlite3* database handles cannot be moved from one thread
178** to another. This logic makes sure a user does not try to do that
179** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000180**
181** Version 3.3.1 (2006-01-15): OsFiles can be moved from one thread to
182** another as long as we are running on a system that supports threads
183** overriding each others locks (which now the most common behavior)
184** or if no locks are held. But the OsFile.pLock field needs to be
185** recomputed because its key includes the thread-id. See the
186** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000187*/
drh029b44b2006-01-15 00:13:15 +0000188#if defined(SQLITE_UNIX_THREADS)
drh9cbe6352005-11-29 03:13:21 +0000189# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000190# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
191 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000192#else
193# define SET_THREADID(X)
194# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000195#endif
196
drhbbd42a62004-05-22 17:41:58 +0000197/*
198** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
199** section 6.5.2.2 lines 483 through 490 specify that when a process
200** sets or clears a lock, that operation overrides any prior locks set
201** by the same process. It does not explicitly say so, but this implies
202** that it overrides locks set by the same process using a different
203** file descriptor. Consider this test case:
204**
205** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
206** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
207**
208** Suppose ./file1 and ./file2 are really the same file (because
209** one is a hard or symbolic link to the other) then if you set
210** an exclusive lock on fd1, then try to get an exclusive lock
211** on fd2, it works. I would have expected the second lock to
212** fail since there was already a lock on the file due to fd1.
213** But not so. Since both locks came from the same process, the
214** second overrides the first, even though they were on different
215** file descriptors opened on different file names.
216**
217** Bummer. If you ask me, this is broken. Badly broken. It means
218** that we cannot use POSIX locks to synchronize file access among
219** competing threads of the same process. POSIX locks will work fine
220** to synchronize access for threads in separate processes, but not
221** threads within the same process.
222**
223** To work around the problem, SQLite has to manage file locks internally
224** on its own. Whenever a new database is opened, we have to find the
225** specific inode of the database file (the inode is determined by the
226** st_dev and st_ino fields of the stat structure that fstat() fills in)
227** and check for locks already existing on that inode. When locks are
228** created or removed, we have to look at our own internal record of the
229** locks to see if another thread has previously set a lock on that same
230** inode.
231**
232** The OsFile structure for POSIX is no longer just an integer file
233** descriptor. It is now a structure that holds the integer file
234** descriptor and a pointer to a structure that describes the internal
235** locks on the corresponding inode. There is one locking structure
236** per inode, so if the same inode is opened twice, both OsFile structures
237** point to the same locking structure. The locking structure keeps
238** a reference count (so we will know when to delete it) and a "cnt"
239** field that tells us its internal lock status. cnt==0 means the
240** file is unlocked. cnt==-1 means the file has an exclusive lock.
241** cnt>0 means there are cnt shared locks on the file.
242**
243** Any attempt to lock or unlock a file first checks the locking
244** structure. The fcntl() system call is only invoked to set a
245** POSIX lock if the internal lock structure transitions between
246** a locked and an unlocked state.
247**
248** 2004-Jan-11:
249** More recent discoveries about POSIX advisory locks. (The more
250** I discover, the more I realize the a POSIX advisory locks are
251** an abomination.)
252**
253** If you close a file descriptor that points to a file that has locks,
254** all locks on that file that are owned by the current process are
255** released. To work around this problem, each OsFile structure contains
256** a pointer to an openCnt structure. There is one openCnt structure
257** per open inode, which means that multiple OsFiles can point to a single
258** openCnt. When an attempt is made to close an OsFile, if there are
259** other OsFiles open on the same inode that are holding locks, the call
260** to close() the file descriptor is deferred until all of the locks clear.
261** The openCnt structure keeps a list of file descriptors that need to
262** be closed and that list is walked (and cleared) when the last lock
263** clears.
264**
265** First, under Linux threads, because each thread has a separate
266** process ID, lock operations in one thread do not override locks
267** to the same file in other threads. Linux threads behave like
268** separate processes in this respect. But, if you close a file
269** descriptor in linux threads, all locks are cleared, even locks
270** on other threads and even though the other threads have different
271** process IDs. Linux threads is inconsistent in this respect.
272** (I'm beginning to think that linux threads is an abomination too.)
273** The consequence of this all is that the hash table for the lockInfo
274** structure has to include the process id as part of its key because
275** locks in different threads are treated as distinct. But the
276** openCnt structure should not include the process id in its
277** key because close() clears lock on all threads, not just the current
278** thread. Were it not for this goofiness in linux threads, we could
279** combine the lockInfo and openCnt structures into a single structure.
drh5fdae772004-06-29 03:29:00 +0000280**
281** 2004-Jun-28:
282** On some versions of linux, threads can override each others locks.
283** On others not. Sometimes you can change the behavior on the same
284** system by setting the LD_ASSUME_KERNEL environment variable. The
285** POSIX standard is silent as to which behavior is correct, as far
286** as I can tell, so other versions of unix might show the same
287** inconsistency. There is no little doubt in my mind that posix
288** advisory locks and linux threads are profoundly broken.
289**
290** To work around the inconsistencies, we have to test at runtime
291** whether or not threads can override each others locks. This test
292** is run once, the first time any lock is attempted. A static
293** variable is set to record the results of this test for future
294** use.
drhbbd42a62004-05-22 17:41:58 +0000295*/
296
297/*
298** An instance of the following structure serves as the key used
drh5fdae772004-06-29 03:29:00 +0000299** to locate a particular lockInfo structure given its inode.
300**
301** If threads cannot override each others locks, then we set the
302** lockKey.tid field to the thread ID. If threads can override
drhf1a221e2006-01-15 17:27:17 +0000303** each others locks then tid is always set to zero. tid is omitted
304** if we compile without threading support.
drhbbd42a62004-05-22 17:41:58 +0000305*/
306struct lockKey {
drh5fdae772004-06-29 03:29:00 +0000307 dev_t dev; /* Device number */
308 ino_t ino; /* Inode number */
309#ifdef SQLITE_UNIX_THREADS
drhd9cb6ac2005-10-20 07:28:17 +0000310 pthread_t tid; /* Thread ID or zero if threads can override each other */
drh5fdae772004-06-29 03:29:00 +0000311#endif
drhbbd42a62004-05-22 17:41:58 +0000312};
313
314/*
315** An instance of the following structure is allocated for each open
316** inode on each thread with a different process ID. (Threads have
317** different process IDs on linux, but not on most other unixes.)
318**
319** A single inode can have multiple file descriptors, so each OsFile
320** structure contains a pointer to an instance of this object and this
321** object keeps a count of the number of OsFiles pointing to it.
322*/
323struct lockInfo {
324 struct lockKey key; /* The lookup key */
drh2ac3ee92004-06-07 16:27:46 +0000325 int cnt; /* Number of SHARED locks held */
danielk19779a1d0ab2004-06-01 14:09:28 +0000326 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
drhbbd42a62004-05-22 17:41:58 +0000327 int nRef; /* Number of pointers to this structure */
328};
329
330/*
331** An instance of the following structure serves as the key used
332** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000333** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000334*/
335struct openKey {
336 dev_t dev; /* Device number */
337 ino_t ino; /* Inode number */
338};
339
340/*
341** An instance of the following structure is allocated for each open
342** inode. This structure keeps track of the number of locks on that
343** inode. If a close is attempted against an inode that is holding
344** locks, the close is deferred until all locks clear by adding the
345** file descriptor to be closed to the pending list.
346*/
347struct openCnt {
348 struct openKey key; /* The lookup key */
349 int nRef; /* Number of pointers to this structure */
350 int nLock; /* Number of outstanding locks */
351 int nPending; /* Number of pending close() operations */
352 int *aPending; /* Malloced space holding fd's awaiting a close() */
353};
354
355/*
drhf1a221e2006-01-15 17:27:17 +0000356** These hash tables map inodes and file descriptors (really, lockKey and
357** openKey structures) into lockInfo and openCnt structures. Access to
358** these hash tables must be protected by a mutex.
drhbbd42a62004-05-22 17:41:58 +0000359*/
danielk1977750b03e2006-02-14 10:48:39 +0000360static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0,
361 sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
362static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0,
363 sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
drh5fdae772004-06-29 03:29:00 +0000364
drhbfe66312006-10-03 17:40:40 +0000365#ifdef SQLITE_ENABLE_LOCKING_STYLE
366/*
367** The locking styles are associated with the different file locking
368** capabilities supported by different file systems.
369**
370** POSIX locking style fully supports shared and exclusive byte-range locks
371** ADP locking only supports exclusive byte-range locks
372** FLOCK only supports a single file-global exclusive lock
373** DOTLOCK isn't a true locking style, it refers to the use of a special
374** file named the same as the database file with a '.lock' extension, this
375** can be used on file systems that do not offer any reliable file locking
376** NO locking means that no locking will be attempted, this is only used for
377** read-only file systems currently
378** UNSUPPORTED means that no locking will be attempted, this is only used for
379** file systems that are known to be unsupported
380*/
381typedef enum {
drhfd131da2007-08-07 17:13:03 +0000382 posixLockingStyle = 0, /* standard posix-advisory locks */
383 afpLockingStyle, /* use afp locks */
384 flockLockingStyle, /* use flock() */
385 dotlockLockingStyle, /* use <file>.lock files */
386 noLockingStyle, /* useful for read-only file system */
387 unsupportedLockingStyle /* indicates unsupported file system */
drhbfe66312006-10-03 17:40:40 +0000388} sqlite3LockingStyle;
389#endif /* SQLITE_ENABLE_LOCKING_STYLE */
390
drh5fdae772004-06-29 03:29:00 +0000391#ifdef SQLITE_UNIX_THREADS
392/*
393** This variable records whether or not threads can override each others
394** locks.
395**
396** 0: No. Threads cannot override each others locks.
397** 1: Yes. Threads can override each others locks.
398** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000399**
drh5062d3a2006-01-31 23:03:35 +0000400** On some systems, we know at compile-time if threads can override each
401** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
402** will be set appropriately. On other systems, we have to check at
403** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
404** undefined.
405**
drhf1a221e2006-01-15 17:27:17 +0000406** This variable normally has file scope only. But during testing, we make
407** it a global so that the test code can change its value in order to verify
408** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000409*/
drh5062d3a2006-01-31 23:03:35 +0000410#ifndef SQLITE_THREAD_OVERRIDE_LOCK
411# define SQLITE_THREAD_OVERRIDE_LOCK -1
412#endif
drh029b44b2006-01-15 00:13:15 +0000413#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000414int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000415#else
drh5062d3a2006-01-31 23:03:35 +0000416static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000417#endif
drh5fdae772004-06-29 03:29:00 +0000418
419/*
420** This structure holds information passed into individual test
421** threads by the testThreadLockingBehavior() routine.
422*/
423struct threadTestData {
424 int fd; /* File to be locked */
425 struct flock lock; /* The locking operation */
426 int result; /* Result of the locking operation */
427};
428
drh2b4b5962005-06-15 17:47:55 +0000429#ifdef SQLITE_LOCK_TRACE
430/*
431** Print out information about all locking operations.
432**
433** This routine is used for troubleshooting locks on multithreaded
434** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
435** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000436** turned off.
drh2b4b5962005-06-15 17:47:55 +0000437*/
438static int lockTrace(int fd, int op, struct flock *p){
439 char *zOpName, *zType;
440 int s;
441 int savedErrno;
442 if( op==F_GETLK ){
443 zOpName = "GETLK";
444 }else if( op==F_SETLK ){
445 zOpName = "SETLK";
446 }else{
447 s = fcntl(fd, op, p);
448 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
449 return s;
450 }
451 if( p->l_type==F_RDLCK ){
452 zType = "RDLCK";
453 }else if( p->l_type==F_WRLCK ){
454 zType = "WRLCK";
455 }else if( p->l_type==F_UNLCK ){
456 zType = "UNLCK";
457 }else{
458 assert( 0 );
459 }
460 assert( p->l_whence==SEEK_SET );
461 s = fcntl(fd, op, p);
462 savedErrno = errno;
463 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
464 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
465 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000466 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000467 struct flock l2;
468 l2 = *p;
469 fcntl(fd, F_GETLK, &l2);
470 if( l2.l_type==F_RDLCK ){
471 zType = "RDLCK";
472 }else if( l2.l_type==F_WRLCK ){
473 zType = "WRLCK";
474 }else if( l2.l_type==F_UNLCK ){
475 zType = "UNLCK";
476 }else{
477 assert( 0 );
478 }
479 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
480 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
481 }
482 errno = savedErrno;
483 return s;
484}
485#define fcntl lockTrace
486#endif /* SQLITE_LOCK_TRACE */
487
drh5fdae772004-06-29 03:29:00 +0000488/*
489** The testThreadLockingBehavior() routine launches two separate
490** threads on this routine. This routine attempts to lock a file
491** descriptor then returns. The success or failure of that attempt
492** allows the testThreadLockingBehavior() procedure to determine
493** whether or not threads can override each others locks.
494*/
495static void *threadLockingTest(void *pArg){
496 struct threadTestData *pData = (struct threadTestData*)pArg;
497 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
498 return pArg;
499}
500
501/*
502** This procedure attempts to determine whether or not threads
503** can override each others locks then sets the
504** threadsOverrideEachOthersLocks variable appropriately.
505*/
danielk19774d5238f2006-01-27 06:32:00 +0000506static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000507 int fd;
508 struct threadTestData d[2];
509 pthread_t t[2];
510
511 fd = dup(fd_orig);
512 if( fd<0 ) return;
513 memset(d, 0, sizeof(d));
514 d[0].fd = fd;
515 d[0].lock.l_type = F_RDLCK;
516 d[0].lock.l_len = 1;
517 d[0].lock.l_start = 0;
518 d[0].lock.l_whence = SEEK_SET;
519 d[1] = d[0];
520 d[1].lock.l_type = F_WRLCK;
521 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
522 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
523 pthread_join(t[0], 0);
524 pthread_join(t[1], 0);
525 close(fd);
526 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
527}
528#endif /* SQLITE_UNIX_THREADS */
529
drhbbd42a62004-05-22 17:41:58 +0000530/*
531** Release a lockInfo structure previously allocated by findLockInfo().
532*/
533static void releaseLockInfo(struct lockInfo *pLock){
drh757b04e2006-01-18 17:25:45 +0000534 assert( sqlite3OsInMutex(1) );
drhbfe66312006-10-03 17:40:40 +0000535 if (pLock == NULL)
536 return;
drhbbd42a62004-05-22 17:41:58 +0000537 pLock->nRef--;
538 if( pLock->nRef==0 ){
539 sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
danielk1977750b03e2006-02-14 10:48:39 +0000540 sqlite3ThreadSafeFree(pLock);
drhbbd42a62004-05-22 17:41:58 +0000541 }
542}
543
544/*
545** Release a openCnt structure previously allocated by findLockInfo().
546*/
547static void releaseOpenCnt(struct openCnt *pOpen){
drh757b04e2006-01-18 17:25:45 +0000548 assert( sqlite3OsInMutex(1) );
drhbfe66312006-10-03 17:40:40 +0000549 if (pOpen == NULL)
550 return;
drhbbd42a62004-05-22 17:41:58 +0000551 pOpen->nRef--;
552 if( pOpen->nRef==0 ){
553 sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
drh64b1bea2006-01-15 02:30:57 +0000554 free(pOpen->aPending);
danielk1977750b03e2006-02-14 10:48:39 +0000555 sqlite3ThreadSafeFree(pOpen);
drhbbd42a62004-05-22 17:41:58 +0000556 }
557}
558
drhbfe66312006-10-03 17:40:40 +0000559#ifdef SQLITE_ENABLE_LOCKING_STYLE
560/*
561** Tests a byte-range locking query to see if byte range locks are
562** supported, if not we fall back to dotlockLockingStyle.
563*/
564static sqlite3LockingStyle sqlite3TestLockingStyle(const char *filePath,
565 int fd) {
566 /* test byte-range lock using fcntl */
567 struct flock lockInfo;
568
569 lockInfo.l_len = 1;
570 lockInfo.l_start = 0;
571 lockInfo.l_whence = SEEK_SET;
572 lockInfo.l_type = F_RDLCK;
573
aswiftae0943b2007-01-31 23:37:07 +0000574 if (fcntl(fd, F_GETLK, &lockInfo) != -1) {
drhbfe66312006-10-03 17:40:40 +0000575 return posixLockingStyle;
576 }
577
578 /* testing for flock can give false positives. So if if the above test
579 ** fails, then we fall back to using dot-lock style locking.
580 */
581 return dotlockLockingStyle;
582}
583
584/*
585** Examines the f_fstypename entry in the statfs structure as returned by
586** stat() for the file system hosting the database file, assigns the
587** appropriate locking style based on it's value. These values and
588** assignments are based on Darwin/OSX behavior and have not been tested on
589** other systems.
590*/
591static sqlite3LockingStyle sqlite3DetectLockingStyle(const char *filePath,
592 int fd) {
593
594#ifdef SQLITE_FIXED_LOCKING_STYLE
595 return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE;
596#else
597 struct statfs fsInfo;
598
599 if (statfs(filePath, &fsInfo) == -1)
600 return sqlite3TestLockingStyle(filePath, fd);
601
602 if (fsInfo.f_flags & MNT_RDONLY)
603 return noLockingStyle;
604
605 if( (!strcmp(fsInfo.f_fstypename, "hfs")) ||
606 (!strcmp(fsInfo.f_fstypename, "ufs")) )
drhfd131da2007-08-07 17:13:03 +0000607 return posixLockingStyle;
drhbfe66312006-10-03 17:40:40 +0000608
609 if(!strcmp(fsInfo.f_fstypename, "afpfs"))
610 return afpLockingStyle;
611
612 if(!strcmp(fsInfo.f_fstypename, "nfs"))
613 return sqlite3TestLockingStyle(filePath, fd);
614
615 if(!strcmp(fsInfo.f_fstypename, "smbfs"))
616 return flockLockingStyle;
617
618 if(!strcmp(fsInfo.f_fstypename, "msdos"))
619 return dotlockLockingStyle;
620
621 if(!strcmp(fsInfo.f_fstypename, "webdav"))
622 return unsupportedLockingStyle;
623
624 return sqlite3TestLockingStyle(filePath, fd);
drh3b62b2f2007-06-08 18:27:03 +0000625#endif /* SQLITE_FIXED_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000626}
627
628#endif /* SQLITE_ENABLE_LOCKING_STYLE */
629
drhbbd42a62004-05-22 17:41:58 +0000630/*
631** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000632** describes that file descriptor. Create new ones if necessary. The
633** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000634**
635** Return the number of errors.
636*/
drh38f82712004-06-18 17:10:16 +0000637static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000638 int fd, /* The file descriptor used in the key */
639 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000640 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000641){
642 int rc;
643 struct lockKey key1;
644 struct openKey key2;
645 struct stat statbuf;
646 struct lockInfo *pLock;
647 struct openCnt *pOpen;
648 rc = fstat(fd, &statbuf);
649 if( rc!=0 ) return 1;
danielk1977441b09a2006-01-05 13:48:29 +0000650
drh757b04e2006-01-18 17:25:45 +0000651 assert( sqlite3OsInMutex(1) );
drhbbd42a62004-05-22 17:41:58 +0000652 memset(&key1, 0, sizeof(key1));
653 key1.dev = statbuf.st_dev;
654 key1.ino = statbuf.st_ino;
drh5fdae772004-06-29 03:29:00 +0000655#ifdef SQLITE_UNIX_THREADS
656 if( threadsOverrideEachOthersLocks<0 ){
657 testThreadLockingBehavior(fd);
658 }
659 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
660#endif
drhbbd42a62004-05-22 17:41:58 +0000661 memset(&key2, 0, sizeof(key2));
662 key2.dev = statbuf.st_dev;
663 key2.ino = statbuf.st_ino;
664 pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
665 if( pLock==0 ){
666 struct lockInfo *pOld;
danielk1977750b03e2006-02-14 10:48:39 +0000667 pLock = sqlite3ThreadSafeMalloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000668 if( pLock==0 ){
669 rc = 1;
670 goto exit_findlockinfo;
671 }
drhbbd42a62004-05-22 17:41:58 +0000672 pLock->key = key1;
673 pLock->nRef = 1;
674 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000675 pLock->locktype = 0;
drhbbd42a62004-05-22 17:41:58 +0000676 pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
677 if( pOld!=0 ){
678 assert( pOld==pLock );
danielk1977750b03e2006-02-14 10:48:39 +0000679 sqlite3ThreadSafeFree(pLock);
danielk1977441b09a2006-01-05 13:48:29 +0000680 rc = 1;
681 goto exit_findlockinfo;
drhbbd42a62004-05-22 17:41:58 +0000682 }
683 }else{
684 pLock->nRef++;
685 }
686 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000687 if( ppOpen!=0 ){
688 pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
drhbbd42a62004-05-22 17:41:58 +0000689 if( pOpen==0 ){
drh029b44b2006-01-15 00:13:15 +0000690 struct openCnt *pOld;
danielk1977750b03e2006-02-14 10:48:39 +0000691 pOpen = sqlite3ThreadSafeMalloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000692 if( pOpen==0 ){
693 releaseLockInfo(pLock);
694 rc = 1;
695 goto exit_findlockinfo;
696 }
697 pOpen->key = key2;
698 pOpen->nRef = 1;
699 pOpen->nLock = 0;
700 pOpen->nPending = 0;
701 pOpen->aPending = 0;
702 pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
703 if( pOld!=0 ){
704 assert( pOld==pOpen );
danielk1977750b03e2006-02-14 10:48:39 +0000705 sqlite3ThreadSafeFree(pOpen);
drh029b44b2006-01-15 00:13:15 +0000706 releaseLockInfo(pLock);
707 rc = 1;
708 goto exit_findlockinfo;
709 }
710 }else{
711 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000712 }
drh029b44b2006-01-15 00:13:15 +0000713 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000714 }
danielk1977441b09a2006-01-05 13:48:29 +0000715
716exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000717 return rc;
drhbbd42a62004-05-22 17:41:58 +0000718}
719
drh64b1bea2006-01-15 02:30:57 +0000720#ifdef SQLITE_DEBUG
721/*
722** Helper function for printing out trace information from debugging
723** binaries. This returns the string represetation of the supplied
724** integer lock-type.
725*/
726static const char *locktypeName(int locktype){
727 switch( locktype ){
728 case NO_LOCK: return "NONE";
729 case SHARED_LOCK: return "SHARED";
730 case RESERVED_LOCK: return "RESERVED";
731 case PENDING_LOCK: return "PENDING";
732 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
733 }
734 return "ERROR";
735}
736#endif
737
drhbbd42a62004-05-22 17:41:58 +0000738/*
drh029b44b2006-01-15 00:13:15 +0000739** If we are currently in a different thread than the thread that the
740** unixFile argument belongs to, then transfer ownership of the unixFile
741** over to the current thread.
742**
743** A unixFile is only owned by a thread on systems where one thread is
744** unable to override locks created by a different thread. RedHat9 is
745** an example of such a system.
746**
747** Ownership transfer is only allowed if the unixFile is currently unlocked.
748** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000749** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +0000750*/
751#ifdef SQLITE_UNIX_THREADS
752static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +0000753 int rc;
drh029b44b2006-01-15 00:13:15 +0000754 pthread_t hSelf;
755 if( threadsOverrideEachOthersLocks ){
756 /* Ownership transfers not needed on this system */
757 return SQLITE_OK;
758 }
759 hSelf = pthread_self();
760 if( pthread_equal(pFile->tid, hSelf) ){
761 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +0000762 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +0000763 return SQLITE_OK;
764 }
765 if( pFile->locktype!=NO_LOCK ){
766 /* We cannot change ownership while we are holding a lock! */
767 return SQLITE_MISUSE;
768 }
drh4f0c5872007-03-26 22:05:01 +0000769 OSTRACE4("Transfer ownership of %d from %d to %d\n",
770 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +0000771 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +0000772 if (pFile->pLock != NULL) {
773 releaseLockInfo(pFile->pLock);
774 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
drh4f0c5872007-03-26 22:05:01 +0000775 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +0000776 locktypeName(pFile->locktype),
777 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
778 return rc;
779 } else {
780 return SQLITE_OK;
781 }
drh029b44b2006-01-15 00:13:15 +0000782}
783#else
drhf1a221e2006-01-15 17:27:17 +0000784 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +0000785# define transferOwnership(X) SQLITE_OK
786#endif
787
788/*
drhbbd42a62004-05-22 17:41:58 +0000789** Delete the named file
790*/
drh66560ad2006-01-06 14:32:19 +0000791int sqlite3UnixDelete(const char *zFilename){
danielk1977979f38e2007-03-27 16:19:51 +0000792 SimulateIOError(return SQLITE_IOERR_DELETE);
drhbbd42a62004-05-22 17:41:58 +0000793 unlink(zFilename);
794 return SQLITE_OK;
795}
796
797/*
798** Return TRUE if the named file exists.
799*/
drh66560ad2006-01-06 14:32:19 +0000800int sqlite3UnixFileExists(const char *zFilename){
drhbbd42a62004-05-22 17:41:58 +0000801 return access(zFilename, 0)==0;
802}
803
drh054889e2005-11-30 03:20:31 +0000804/* Forward declaration */
drhbfe66312006-10-03 17:40:40 +0000805static int allocateUnixFile(
806 int h, /* File descriptor of the open file */
danielk197762079062007-08-15 17:08:46 +0000807 sqlite3_file **pId, /* Write the real file descriptor here */
drhbfe66312006-10-03 17:40:40 +0000808 const char *zFilename, /* Name of the file being opened */
809 int delFlag /* If true, make sure the file deletes on close */
810);
drh9cbe6352005-11-29 03:13:21 +0000811
812/*
drhbbd42a62004-05-22 17:41:58 +0000813** Attempt to open a file for both reading and writing. If that
814** fails, try opening it read-only. If the file does not exist,
815** try to create it.
816**
817** On success, a handle for the open file is written to *id
818** and *pReadonly is set to 0 if the file was opened for reading and
819** writing or 1 if the file was opened read-only. The function returns
820** SQLITE_OK.
821**
822** On failure, the function returns SQLITE_CANTOPEN and leaves
823** *id and *pReadonly unchanged.
824*/
drh66560ad2006-01-06 14:32:19 +0000825int sqlite3UnixOpenReadWrite(
drhbbd42a62004-05-22 17:41:58 +0000826 const char *zFilename,
danielk197762079062007-08-15 17:08:46 +0000827 sqlite3_file **pId,
drhbbd42a62004-05-22 17:41:58 +0000828 int *pReadonly
829){
drhbfe66312006-10-03 17:40:40 +0000830 int h;
831
drh9cbe6352005-11-29 03:13:21 +0000832 assert( 0==*pId );
drhbfe66312006-10-03 17:40:40 +0000833 h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY,
834 SQLITE_DEFAULT_FILE_PERMISSIONS);
835 if( h<0 ){
drh6458e392004-07-20 01:14:13 +0000836#ifdef EISDIR
837 if( errno==EISDIR ){
838 return SQLITE_CANTOPEN;
839 }
840#endif
drhbfe66312006-10-03 17:40:40 +0000841 h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
842 if( h<0 ){
drhbbd42a62004-05-22 17:41:58 +0000843 return SQLITE_CANTOPEN;
844 }
845 *pReadonly = 1;
846 }else{
847 *pReadonly = 0;
848 }
danielk197762079062007-08-15 17:08:46 +0000849
850 return CRASH_TEST_OVERRIDE(
851 zFilename, pId, allocateUnixFile(h, pId, zFilename, 0)
852 );
drhbbd42a62004-05-22 17:41:58 +0000853}
854
855
856/*
857** Attempt to open a new file for exclusive access by this process.
858** The file will be opened for both reading and writing. To avoid
859** a potential security problem, we do not allow the file to have
860** previously existed. Nor do we allow the file to be a symbolic
861** link.
862**
863** If delFlag is true, then make arrangements to automatically delete
864** the file when it is closed.
865**
866** On success, write the file handle into *id and return SQLITE_OK.
867**
868** On failure, return SQLITE_CANTOPEN.
869*/
danielk197762079062007-08-15 17:08:46 +0000870int sqlite3UnixOpenExclusive(
871 const char *zFilename,
872 sqlite3_file **pId,
873 int delFlag
874){
drhbfe66312006-10-03 17:40:40 +0000875 int h;
drh9cbe6352005-11-29 03:13:21 +0000876
877 assert( 0==*pId );
drhbfe66312006-10-03 17:40:40 +0000878 h = open(zFilename,
drhd6459672005-08-13 17:17:01 +0000879 O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY,
drh3f56e6e2007-03-15 01:16:47 +0000880 delFlag ? 0600 : SQLITE_DEFAULT_FILE_PERMISSIONS);
drhbfe66312006-10-03 17:40:40 +0000881 if( h<0 ){
drhbbd42a62004-05-22 17:41:58 +0000882 return SQLITE_CANTOPEN;
883 }
danielk197762079062007-08-15 17:08:46 +0000884 return CRASH_TEST_OVERRIDE(
885 zFilename, pId, allocateUnixFile(h, pId, zFilename, delFlag)
886 );
drhbbd42a62004-05-22 17:41:58 +0000887}
888
889/*
890** Attempt to open a new file for read-only access.
891**
892** On success, write the file handle into *id and return SQLITE_OK.
893**
894** On failure, return SQLITE_CANTOPEN.
895*/
danielk197762079062007-08-15 17:08:46 +0000896int sqlite3UnixOpenReadOnly(const char *zFilename, sqlite3_file **pId){
drhbfe66312006-10-03 17:40:40 +0000897 int h;
898
drh9cbe6352005-11-29 03:13:21 +0000899 assert( 0==*pId );
drhbfe66312006-10-03 17:40:40 +0000900 h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
901 if( h<0 ){
drhbbd42a62004-05-22 17:41:58 +0000902 return SQLITE_CANTOPEN;
903 }
danielk197762079062007-08-15 17:08:46 +0000904 return CRASH_TEST_OVERRIDE(
905 zFilename, pId, allocateUnixFile(h, pId, zFilename, 0)
906 );
drhbbd42a62004-05-22 17:41:58 +0000907}
908
909/*
910** Attempt to open a file descriptor for the directory that contains a
911** file. This file descriptor can be used to fsync() the directory
912** in order to make sure the creation of a new file is actually written
913** to disk.
914**
915** This routine is only meaningful for Unix. It is a no-op under
916** windows since windows does not support hard links.
917**
drhbfe66312006-10-03 17:40:40 +0000918** If FULL_FSYNC is enabled, this function is not longer useful,
919** a FULL_FSYNC sync applies to all pending disk operations.
920**
drh9cbe6352005-11-29 03:13:21 +0000921** On success, a handle for a previously open file at *id is
drhbbd42a62004-05-22 17:41:58 +0000922** updated with the new directory file descriptor and SQLITE_OK is
923** returned.
924**
925** On failure, the function returns SQLITE_CANTOPEN and leaves
926** *id unchanged.
927*/
drh9c06c952005-11-26 00:25:00 +0000928static int unixOpenDirectory(
drh054889e2005-11-30 03:20:31 +0000929 OsFile *id,
930 const char *zDirname
drhbbd42a62004-05-22 17:41:58 +0000931){
drhe78669b2007-06-29 12:04:26 +0000932 int h;
drh054889e2005-11-30 03:20:31 +0000933 unixFile *pFile = (unixFile*)id;
drhbb5f18d2007-04-06 18:23:17 +0000934 assert( pFile!=0 );
drh054889e2005-11-30 03:20:31 +0000935 SET_THREADID(pFile);
936 assert( pFile->dirfd<0 );
drhe78669b2007-06-29 12:04:26 +0000937 pFile->dirfd = h = open(zDirname, O_RDONLY|O_BINARY, 0);
938 if( h<0 ){
drhbbd42a62004-05-22 17:41:58 +0000939 return SQLITE_CANTOPEN;
940 }
drhe78669b2007-06-29 12:04:26 +0000941#ifdef FD_CLOEXEC
942 fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
943#endif
944 OSTRACE3("OPENDIR %-3d %s\n", h, zDirname);
drhbbd42a62004-05-22 17:41:58 +0000945 return SQLITE_OK;
946}
947
948/*
949** Create a temporary file name in zBuf. zBuf must be big enough to
950** hold at least SQLITE_TEMPNAME_SIZE characters.
951*/
drh66560ad2006-01-06 14:32:19 +0000952int sqlite3UnixTempFileName(char *zBuf){
drhbbd42a62004-05-22 17:41:58 +0000953 static const char *azDirs[] = {
drhab3f9fe2004-08-14 17:10:10 +0000954 0,
drhbbd42a62004-05-22 17:41:58 +0000955 "/var/tmp",
956 "/usr/tmp",
957 "/tmp",
958 ".",
959 };
drh57196282004-10-06 15:41:16 +0000960 static const unsigned char zChars[] =
drhbbd42a62004-05-22 17:41:58 +0000961 "abcdefghijklmnopqrstuvwxyz"
962 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
963 "0123456789";
964 int i, j;
965 struct stat buf;
966 const char *zDir = ".";
drheffd02b2004-08-29 23:42:13 +0000967 azDirs[0] = sqlite3_temp_directory;
drhbbd42a62004-05-22 17:41:58 +0000968 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
drhab3f9fe2004-08-14 17:10:10 +0000969 if( azDirs[i]==0 ) continue;
drhbbd42a62004-05-22 17:41:58 +0000970 if( stat(azDirs[i], &buf) ) continue;
971 if( !S_ISDIR(buf.st_mode) ) continue;
972 if( access(azDirs[i], 07) ) continue;
973 zDir = azDirs[i];
974 break;
975 }
976 do{
drh5bb3eb92007-05-04 13:15:55 +0000977 sqlite3_snprintf(SQLITE_TEMPNAME_SIZE, zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
drhbbd42a62004-05-22 17:41:58 +0000978 j = strlen(zBuf);
979 sqlite3Randomness(15, &zBuf[j]);
980 for(i=0; i<15; i++, j++){
981 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
982 }
983 zBuf[j] = 0;
984 }while( access(zBuf,0)==0 );
985 return SQLITE_OK;
986}
987
988/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000989** Check that a given pathname is a directory and is writable
990**
991*/
drh66560ad2006-01-06 14:32:19 +0000992int sqlite3UnixIsDirWritable(char *zBuf){
drh9c06c952005-11-26 00:25:00 +0000993#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000994 struct stat buf;
995 if( zBuf==0 ) return 0;
drh268283b2005-01-08 15:44:25 +0000996 if( zBuf[0]==0 ) return 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000997 if( stat(zBuf, &buf) ) return 0;
998 if( !S_ISDIR(buf.st_mode) ) return 0;
999 if( access(zBuf, 07) ) return 0;
drh9c06c952005-11-26 00:25:00 +00001000#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +00001001 return 1;
1002}
1003
1004/*
drhb912b282006-03-23 22:42:20 +00001005** Seek to the offset in id->offset then read cnt bytes into pBuf.
1006** Return the number of bytes actually read. Update the offset.
1007*/
danielk197762079062007-08-15 17:08:46 +00001008static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001009 int got;
drh8ebf6702007-02-06 11:11:08 +00001010 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001011 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001012#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001013 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001014 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +00001015#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001016 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001017 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +00001018#else
danielk197762079062007-08-15 17:08:46 +00001019 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +00001020 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +00001021 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +00001022 return -1;
1023 }
drhb912b282006-03-23 22:42:20 +00001024 got = read(id->h, pBuf, cnt);
1025#endif
drh15d00c42007-02-27 02:01:14 +00001026 TIMER_END;
drh4f0c5872007-03-26 22:05:01 +00001027 OSTRACE5("READ %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001028 return got;
1029}
1030
1031/*
drhbbd42a62004-05-22 17:41:58 +00001032** Read data from a file into a buffer. Return SQLITE_OK if all
1033** bytes were read successfully and SQLITE_IOERR if anything goes
1034** wrong.
1035*/
danielk197762079062007-08-15 17:08:46 +00001036static int unixRead(
1037 sqlite3_file *id,
1038 void *pBuf,
1039 int amt,
1040 sqlite3_int64 offset
1041){
drhbbd42a62004-05-22 17:41:58 +00001042 int got;
drh9cbe6352005-11-29 03:13:21 +00001043 assert( id );
danielk197762079062007-08-15 17:08:46 +00001044 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +00001045 if( got==amt ){
1046 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +00001047 }else if( got<0 ){
1048 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +00001049 }else{
drhbafda092007-01-03 23:36:22 +00001050 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +00001051 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +00001052 }
1053}
1054
1055/*
drhb912b282006-03-23 22:42:20 +00001056** Seek to the offset in id->offset then read cnt bytes into pBuf.
1057** Return the number of bytes actually read. Update the offset.
1058*/
danielk197762079062007-08-15 17:08:46 +00001059static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001060 int got;
drh8ebf6702007-02-06 11:11:08 +00001061 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001062 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001063#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001064 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +00001065#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001066 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +00001067#else
danielk197762079062007-08-15 17:08:46 +00001068 newOffset = lseek(id->h, offset, SEEK_SET);
1069 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +00001070 return -1;
1071 }
drhb912b282006-03-23 22:42:20 +00001072 got = write(id->h, pBuf, cnt);
1073#endif
drh15d00c42007-02-27 02:01:14 +00001074 TIMER_END;
danielk197762079062007-08-15 17:08:46 +00001075 OSTRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001076 return got;
1077}
1078
1079
1080/*
drhbbd42a62004-05-22 17:41:58 +00001081** Write data from a buffer into a file. Return SQLITE_OK on success
1082** or some other error code on failure.
1083*/
danielk197762079062007-08-15 17:08:46 +00001084static int unixWrite(
1085 sqlite3_file *id,
1086 const void *pBuf,
1087 int amt,
1088 sqlite3_int64 offset
1089){
drhbbd42a62004-05-22 17:41:58 +00001090 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +00001091 assert( id );
drh4c7f9412005-02-03 00:29:47 +00001092 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +00001093 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +00001094 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +00001095 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +00001096 pBuf = &((char*)pBuf)[wrote];
1097 }
drh59685932006-09-14 13:47:11 +00001098 SimulateIOError(( wrote=(-1), amt=1 ));
1099 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +00001100 if( amt>0 ){
drh59685932006-09-14 13:47:11 +00001101 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +00001102 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +00001103 }else{
1104 return SQLITE_FULL;
1105 }
drhbbd42a62004-05-22 17:41:58 +00001106 }
1107 return SQLITE_OK;
1108}
1109
1110/*
1111** Move the read/write pointer in a file.
1112*/
drh9c06c952005-11-26 00:25:00 +00001113static int unixSeek(OsFile *id, i64 offset){
drh9cbe6352005-11-29 03:13:21 +00001114 assert( id );
drhb4746b92005-09-09 01:32:06 +00001115#ifdef SQLITE_TEST
drh59685932006-09-14 13:47:11 +00001116 if( offset ) SimulateDiskfullError(return SQLITE_FULL);
drhb4746b92005-09-09 01:32:06 +00001117#endif
drhb912b282006-03-23 22:42:20 +00001118 ((unixFile*)id)->offset = offset;
drhbbd42a62004-05-22 17:41:58 +00001119 return SQLITE_OK;
1120}
1121
drhb851b2c2005-03-10 14:11:12 +00001122#ifdef SQLITE_TEST
1123/*
1124** Count the number of fullsyncs and normal syncs. This is used to test
1125** that syncs and fullsyncs are occuring at the right times.
1126*/
1127int sqlite3_sync_count = 0;
1128int sqlite3_fullsync_count = 0;
1129#endif
1130
drhf2f23912005-10-05 10:29:36 +00001131/*
1132** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
1133** Otherwise use fsync() in its place.
1134*/
1135#ifndef HAVE_FDATASYNC
1136# define fdatasync fsync
1137#endif
1138
drhac530b12006-02-11 01:25:50 +00001139/*
1140** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
1141** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
1142** only available on Mac OS X. But that could change.
1143*/
1144#ifdef F_FULLFSYNC
1145# define HAVE_FULLFSYNC 1
1146#else
1147# define HAVE_FULLFSYNC 0
1148#endif
1149
drhb851b2c2005-03-10 14:11:12 +00001150
drhbbd42a62004-05-22 17:41:58 +00001151/*
drhdd809b02004-07-17 21:44:57 +00001152** The fsync() system call does not work as advertised on many
1153** unix systems. The following procedure is an attempt to make
1154** it work better.
drh1398ad32005-01-19 23:24:50 +00001155**
1156** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
1157** for testing when we want to run through the test suite quickly.
1158** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
1159** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
1160** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +00001161*/
drheb796a72005-09-08 12:38:41 +00001162static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +00001163 int rc;
drhb851b2c2005-03-10 14:11:12 +00001164
1165 /* Record the number of times that we do a normal fsync() and
1166 ** FULLSYNC. This is used during testing to verify that this procedure
1167 ** gets called with the correct arguments.
1168 */
1169#ifdef SQLITE_TEST
1170 if( fullSync ) sqlite3_fullsync_count++;
1171 sqlite3_sync_count++;
1172#endif
1173
1174 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
1175 ** no-op
1176 */
1177#ifdef SQLITE_NO_SYNC
1178 rc = SQLITE_OK;
1179#else
1180
drhac530b12006-02-11 01:25:50 +00001181#if HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +00001182 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +00001183 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +00001184 }else{
1185 rc = 1;
1186 }
1187 /* If the FULLFSYNC failed, fall back to attempting an fsync().
1188 * It shouldn't be possible for fullfsync to fail on the local
1189 * file system (on OSX), so failure indicates that FULLFSYNC
1190 * isn't supported for this file system. So, attempt an fsync
1191 * and (for now) ignore the overhead of a superfluous fcntl call.
1192 * It'd be better to detect fullfsync support once and avoid
1193 * the fcntl call every time sync is called.
1194 */
1195 if( rc ) rc = fsync(fd);
1196
1197#else
drheb796a72005-09-08 12:38:41 +00001198 if( dataOnly ){
1199 rc = fdatasync(fd);
drhf2f23912005-10-05 10:29:36 +00001200 }else{
drheb796a72005-09-08 12:38:41 +00001201 rc = fsync(fd);
1202 }
aswiftae0943b2007-01-31 23:37:07 +00001203#endif /* HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +00001204#endif /* defined(SQLITE_NO_SYNC) */
1205
drhdd809b02004-07-17 21:44:57 +00001206 return rc;
1207}
1208
1209/*
drhbbd42a62004-05-22 17:41:58 +00001210** Make sure all writes to a particular file are committed to disk.
1211**
drheb796a72005-09-08 12:38:41 +00001212** If dataOnly==0 then both the file itself and its metadata (file
1213** size, access time, etc) are synced. If dataOnly!=0 then only the
1214** file data is synced.
1215**
drhbbd42a62004-05-22 17:41:58 +00001216** Under Unix, also make sure that the directory entry for the file
1217** has been created by fsync-ing the directory that contains the file.
1218** If we do not do this and we encounter a power failure, the directory
1219** entry for the journal might not exist after we reboot. The next
1220** SQLite to access the file will not know that the journal exists (because
1221** the directory entry for the journal was never created) and the transaction
1222** will not roll back - possibly leading to database corruption.
1223*/
danielk197762079062007-08-15 17:08:46 +00001224static int unixSync(sqlite3_file *id, int dataOnly){
drh59685932006-09-14 13:47:11 +00001225 int rc;
drh054889e2005-11-30 03:20:31 +00001226 unixFile *pFile = (unixFile*)id;
1227 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001228 OSTRACE2("SYNC %-3d\n", pFile->h);
drh59685932006-09-14 13:47:11 +00001229 rc = full_fsync(pFile->h, pFile->fullSync, dataOnly);
1230 SimulateIOError( rc=1 );
1231 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001232 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001233 }
drh054889e2005-11-30 03:20:31 +00001234 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001235 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
drhac530b12006-02-11 01:25:50 +00001236 HAVE_FULLFSYNC, pFile->fullSync);
danielk1977d7c03f72005-11-25 10:38:22 +00001237#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001238 /* The directory sync is only attempted if full_fsync is
1239 ** turned off or unavailable. If a full_fsync occurred above,
1240 ** then the directory sync is superfluous.
1241 */
1242 if( (!HAVE_FULLFSYNC || !pFile->fullSync) && full_fsync(pFile->dirfd,0,0) ){
1243 /*
1244 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001245 ** errors when applied to directories on certain file systems.
1246 ** A failed directory sync is not a big deal. So it seems
1247 ** better to ignore the error. Ticket #1657
1248 */
1249 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001250 }
danielk1977d7c03f72005-11-25 10:38:22 +00001251#endif
drh054889e2005-11-30 03:20:31 +00001252 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1253 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001254 }
drha2854222004-06-17 19:04:17 +00001255 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001256}
1257
1258/*
danielk1977962398d2004-06-14 09:35:16 +00001259** Sync the directory zDirname. This is a no-op on operating systems other
1260** than UNIX.
drhb851b2c2005-03-10 14:11:12 +00001261**
1262** This is used to make sure the master journal file has truely been deleted
1263** before making changes to individual journals on a multi-database commit.
drhf30cc942005-03-11 17:52:34 +00001264** The F_FULLFSYNC option is not needed here.
danielk1977962398d2004-06-14 09:35:16 +00001265*/
drh66560ad2006-01-06 14:32:19 +00001266int sqlite3UnixSyncDirectory(const char *zDirname){
danielk1977d7c03f72005-11-25 10:38:22 +00001267#ifdef SQLITE_DISABLE_DIRSYNC
1268 return SQLITE_OK;
1269#else
danielk1977962398d2004-06-14 09:35:16 +00001270 int fd;
1271 int r;
drh8e855772005-05-17 11:25:31 +00001272 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh4f0c5872007-03-26 22:05:01 +00001273 OSTRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
danielk1977962398d2004-06-14 09:35:16 +00001274 if( fd<0 ){
1275 return SQLITE_CANTOPEN;
1276 }
1277 r = fsync(fd);
1278 close(fd);
drh59685932006-09-14 13:47:11 +00001279 SimulateIOError( r=1 );
1280 if( r ){
drh4ac285a2006-09-15 07:28:50 +00001281 return SQLITE_IOERR_DIR_FSYNC;
drh59685932006-09-14 13:47:11 +00001282 }else{
1283 return SQLITE_OK;
1284 }
danielk1977d7c03f72005-11-25 10:38:22 +00001285#endif
danielk1977962398d2004-06-14 09:35:16 +00001286}
1287
1288/*
drhbbd42a62004-05-22 17:41:58 +00001289** Truncate an open file to a specified size
1290*/
danielk197762079062007-08-15 17:08:46 +00001291static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001292 int rc;
drh9cbe6352005-11-29 03:13:21 +00001293 assert( id );
drh63fff5f2007-06-19 10:50:38 +00001294 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001295 SimulateIOError( rc=1 );
1296 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001297 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001298 }else{
1299 return SQLITE_OK;
1300 }
drhbbd42a62004-05-22 17:41:58 +00001301}
1302
1303/*
1304** Determine the current size of a file in bytes
1305*/
danielk197762079062007-08-15 17:08:46 +00001306static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001307 int rc;
drhbbd42a62004-05-22 17:41:58 +00001308 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001309 assert( id );
drh59685932006-09-14 13:47:11 +00001310 rc = fstat(((unixFile*)id)->h, &buf);
1311 SimulateIOError( rc=1 );
1312 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001313 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001314 }
1315 *pSize = buf.st_size;
1316 return SQLITE_OK;
1317}
1318
danielk19779a1d0ab2004-06-01 14:09:28 +00001319/*
danielk197713adf8a2004-06-03 16:08:41 +00001320** This routine checks if there is a RESERVED lock held on the specified
1321** file by this or any other process. If such a lock is held, return
drh2ac3ee92004-06-07 16:27:46 +00001322** non-zero. If the file is unlocked or holds only SHARED locks, then
1323** return zero.
danielk197713adf8a2004-06-03 16:08:41 +00001324*/
danielk197762079062007-08-15 17:08:46 +00001325static int unixCheckReservedLock(sqlite3_file *id){
danielk197713adf8a2004-06-03 16:08:41 +00001326 int r = 0;
drh054889e2005-11-30 03:20:31 +00001327 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001328
drh054889e2005-11-30 03:20:31 +00001329 assert( pFile );
drh66560ad2006-01-06 14:32:19 +00001330 sqlite3OsEnterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001331
1332 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001333 if( pFile->pLock->locktype>SHARED_LOCK ){
danielk197713adf8a2004-06-03 16:08:41 +00001334 r = 1;
1335 }
1336
drh2ac3ee92004-06-07 16:27:46 +00001337 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001338 */
1339 if( !r ){
1340 struct flock lock;
1341 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001342 lock.l_start = RESERVED_BYTE;
1343 lock.l_len = 1;
1344 lock.l_type = F_WRLCK;
drh054889e2005-11-30 03:20:31 +00001345 fcntl(pFile->h, F_GETLK, &lock);
danielk197713adf8a2004-06-03 16:08:41 +00001346 if( lock.l_type!=F_UNLCK ){
1347 r = 1;
1348 }
1349 }
1350
drh66560ad2006-01-06 14:32:19 +00001351 sqlite3OsLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001352 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
danielk197713adf8a2004-06-03 16:08:41 +00001353
1354 return r;
1355}
1356
1357/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001358** Lock the file with the lock specified by parameter locktype - one
1359** of the following:
1360**
drh2ac3ee92004-06-07 16:27:46 +00001361** (1) SHARED_LOCK
1362** (2) RESERVED_LOCK
1363** (3) PENDING_LOCK
1364** (4) EXCLUSIVE_LOCK
1365**
drhb3e04342004-06-08 00:47:47 +00001366** Sometimes when requesting one lock state, additional lock states
1367** are inserted in between. The locking might fail on one of the later
1368** transitions leaving the lock state different from what it started but
1369** still short of its goal. The following chart shows the allowed
1370** transitions and the inserted intermediate states:
1371**
1372** UNLOCKED -> SHARED
1373** SHARED -> RESERVED
1374** SHARED -> (PENDING) -> EXCLUSIVE
1375** RESERVED -> (PENDING) -> EXCLUSIVE
1376** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001377**
drha6abd042004-06-09 17:37:22 +00001378** This routine will only increase a lock. Use the sqlite3OsUnlock()
1379** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001380*/
danielk197762079062007-08-15 17:08:46 +00001381static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001382 /* The following describes the implementation of the various locks and
1383 ** lock transitions in terms of the POSIX advisory shared and exclusive
1384 ** lock primitives (called read-locks and write-locks below, to avoid
1385 ** confusion with SQLite lock names). The algorithms are complicated
1386 ** slightly in order to be compatible with windows systems simultaneously
1387 ** accessing the same database file, in case that is ever required.
1388 **
1389 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1390 ** byte', each single bytes at well known offsets, and the 'shared byte
1391 ** range', a range of 510 bytes at a well known offset.
1392 **
1393 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1394 ** byte'. If this is successful, a random byte from the 'shared byte
1395 ** range' is read-locked and the lock on the 'pending byte' released.
1396 **
danielk197790ba3bd2004-06-25 08:32:25 +00001397 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1398 ** A RESERVED lock is implemented by grabbing a write-lock on the
1399 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001400 **
1401 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001402 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1403 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1404 ** obtained, but existing SHARED locks are allowed to persist. A process
1405 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1406 ** This property is used by the algorithm for rolling back a journal file
1407 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001408 **
danielk197790ba3bd2004-06-25 08:32:25 +00001409 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1410 ** implemented by obtaining a write-lock on the entire 'shared byte
1411 ** range'. Since all other locks require a read-lock on one of the bytes
1412 ** within this range, this ensures that no other locks are held on the
1413 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001414 **
1415 ** The reason a single byte cannot be used instead of the 'shared byte
1416 ** range' is that some versions of windows do not support read-locks. By
1417 ** locking a random byte from a range, concurrent SHARED locks may exist
1418 ** even if the locking primitive used is always a write-lock.
1419 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001420 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001421 unixFile *pFile = (unixFile*)id;
1422 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001423 struct flock lock;
1424 int s;
1425
drh054889e2005-11-30 03:20:31 +00001426 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001427 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001428 locktypeName(locktype), locktypeName(pFile->locktype),
1429 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001430
1431 /* If there is already a lock of this type or more restrictive on the
1432 ** OsFile, do nothing. Don't use the end_lock: exit path, as
drh66560ad2006-01-06 14:32:19 +00001433 ** sqlite3OsEnterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001434 */
drh054889e2005-11-30 03:20:31 +00001435 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001436 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001437 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001438 return SQLITE_OK;
1439 }
1440
drhb3e04342004-06-08 00:47:47 +00001441 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001442 */
drh054889e2005-11-30 03:20:31 +00001443 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001444 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001445 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001446
drh054889e2005-11-30 03:20:31 +00001447 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001448 */
drh66560ad2006-01-06 14:32:19 +00001449 sqlite3OsEnterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001450
drh029b44b2006-01-15 00:13:15 +00001451 /* Make sure the current thread owns the pFile.
1452 */
1453 rc = transferOwnership(pFile);
1454 if( rc!=SQLITE_OK ){
1455 sqlite3OsLeaveMutex();
1456 return rc;
1457 }
drh64b1bea2006-01-15 02:30:57 +00001458 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001459
danielk19779a1d0ab2004-06-01 14:09:28 +00001460 /* If some thread using this PID has a lock via a different OsFile*
1461 ** handle that precludes the requested lock, return BUSY.
1462 */
drh054889e2005-11-30 03:20:31 +00001463 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001464 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001465 ){
1466 rc = SQLITE_BUSY;
1467 goto end_lock;
1468 }
1469
1470 /* If a SHARED lock is requested, and some thread using this PID already
1471 ** has a SHARED or RESERVED lock, then increment reference counts and
1472 ** return SQLITE_OK.
1473 */
1474 if( locktype==SHARED_LOCK &&
1475 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1476 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001477 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001478 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001479 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001480 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001481 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001482 goto end_lock;
1483 }
1484
danielk197713adf8a2004-06-03 16:08:41 +00001485 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001486
danielk19779a1d0ab2004-06-01 14:09:28 +00001487 lock.l_whence = SEEK_SET;
1488
drh3cde3bb2004-06-12 02:17:14 +00001489 /* A PENDING lock is needed before acquiring a SHARED lock and before
1490 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1491 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001492 */
drh3cde3bb2004-06-12 02:17:14 +00001493 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001494 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001495 ){
danielk1977489468c2004-06-28 08:25:47 +00001496 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001497 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001498 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001499 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001500 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1501 goto end_lock;
1502 }
drh3cde3bb2004-06-12 02:17:14 +00001503 }
1504
1505
1506 /* If control gets to this point, then actually go ahead and make
1507 ** operating system calls for the specified lock.
1508 */
1509 if( locktype==SHARED_LOCK ){
1510 assert( pLock->cnt==0 );
1511 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001512
drh2ac3ee92004-06-07 16:27:46 +00001513 /* Now get the read-lock */
1514 lock.l_start = SHARED_FIRST;
1515 lock.l_len = SHARED_SIZE;
drh054889e2005-11-30 03:20:31 +00001516 s = fcntl(pFile->h, F_SETLK, &lock);
drh2ac3ee92004-06-07 16:27:46 +00001517
1518 /* Drop the temporary PENDING lock */
1519 lock.l_start = PENDING_BYTE;
1520 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001521 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001522 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001523 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001524 goto end_lock;
1525 }
drhe2396a12007-03-29 20:19:58 +00001526 if( s==(-1) ){
drhbbd42a62004-05-22 17:41:58 +00001527 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1528 }else{
drh054889e2005-11-30 03:20:31 +00001529 pFile->locktype = SHARED_LOCK;
1530 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001531 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001532 }
drh3cde3bb2004-06-12 02:17:14 +00001533 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1534 /* We are trying for an exclusive lock but another thread in this
1535 ** same process is still holding a shared lock. */
1536 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001537 }else{
drh3cde3bb2004-06-12 02:17:14 +00001538 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001539 ** assumed that there is a SHARED or greater lock on the file
1540 ** already.
1541 */
drh054889e2005-11-30 03:20:31 +00001542 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001543 lock.l_type = F_WRLCK;
1544 switch( locktype ){
1545 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001546 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001547 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001548 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001549 lock.l_start = SHARED_FIRST;
1550 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001551 break;
1552 default:
1553 assert(0);
1554 }
drh054889e2005-11-30 03:20:31 +00001555 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001556 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001557 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1558 }
drhbbd42a62004-05-22 17:41:58 +00001559 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001560
danielk1977ecb2a962004-06-02 06:30:16 +00001561 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001562 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001563 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001564 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001565 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001566 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001567 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001568
1569end_lock:
drh66560ad2006-01-06 14:32:19 +00001570 sqlite3OsLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001571 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001572 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001573 return rc;
1574}
1575
1576/*
drh054889e2005-11-30 03:20:31 +00001577** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001578** must be either NO_LOCK or SHARED_LOCK.
1579**
1580** If the locking level of the file descriptor is already at or below
1581** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001582*/
danielk197762079062007-08-15 17:08:46 +00001583static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001584 struct lockInfo *pLock;
1585 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001586 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001587 unixFile *pFile = (unixFile*)id;
drha6abd042004-06-09 17:37:22 +00001588
drh054889e2005-11-30 03:20:31 +00001589 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001590 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001591 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001592
1593 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001594 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001595 return SQLITE_OK;
1596 }
drhf1a221e2006-01-15 17:27:17 +00001597 if( CHECK_THREADID(pFile) ){
1598 return SQLITE_MISUSE;
1599 }
drh66560ad2006-01-06 14:32:19 +00001600 sqlite3OsEnterMutex();
drh054889e2005-11-30 03:20:31 +00001601 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001602 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001603 if( pFile->locktype>SHARED_LOCK ){
1604 assert( pLock->locktype==pFile->locktype );
drh9c105bb2004-10-02 20:38:28 +00001605 if( locktype==SHARED_LOCK ){
1606 lock.l_type = F_RDLCK;
1607 lock.l_whence = SEEK_SET;
1608 lock.l_start = SHARED_FIRST;
1609 lock.l_len = SHARED_SIZE;
drhe2396a12007-03-29 20:19:58 +00001610 if( fcntl(pFile->h, F_SETLK, &lock)==(-1) ){
drh9c105bb2004-10-02 20:38:28 +00001611 /* This should never happen */
drh4ac285a2006-09-15 07:28:50 +00001612 rc = SQLITE_IOERR_RDLOCK;
drh9c105bb2004-10-02 20:38:28 +00001613 }
1614 }
drhbbd42a62004-05-22 17:41:58 +00001615 lock.l_type = F_UNLCK;
1616 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001617 lock.l_start = PENDING_BYTE;
1618 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drhe2396a12007-03-29 20:19:58 +00001619 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001620 pLock->locktype = SHARED_LOCK;
1621 }else{
drh4ac285a2006-09-15 07:28:50 +00001622 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001623 }
drhbbd42a62004-05-22 17:41:58 +00001624 }
drha6abd042004-06-09 17:37:22 +00001625 if( locktype==NO_LOCK ){
1626 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001627
drha6abd042004-06-09 17:37:22 +00001628 /* Decrement the shared lock counter. Release the lock using an
1629 ** OS call only when all threads in this same process have released
1630 ** the lock.
1631 */
1632 pLock->cnt--;
1633 if( pLock->cnt==0 ){
1634 lock.l_type = F_UNLCK;
1635 lock.l_whence = SEEK_SET;
1636 lock.l_start = lock.l_len = 0L;
drhe2396a12007-03-29 20:19:58 +00001637 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001638 pLock->locktype = NO_LOCK;
1639 }else{
drh4ac285a2006-09-15 07:28:50 +00001640 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001641 }
drha6abd042004-06-09 17:37:22 +00001642 }
1643
drhbbd42a62004-05-22 17:41:58 +00001644 /* Decrement the count of locks against this same file. When the
1645 ** count reaches zero, close any other file descriptors whose close
1646 ** was deferred because of outstanding locks.
1647 */
drh054889e2005-11-30 03:20:31 +00001648 pOpen = pFile->pOpen;
drhbbd42a62004-05-22 17:41:58 +00001649 pOpen->nLock--;
1650 assert( pOpen->nLock>=0 );
1651 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1652 int i;
1653 for(i=0; i<pOpen->nPending; i++){
1654 close(pOpen->aPending[i]);
1655 }
drh64b1bea2006-01-15 02:30:57 +00001656 free(pOpen->aPending);
drhbbd42a62004-05-22 17:41:58 +00001657 pOpen->nPending = 0;
1658 pOpen->aPending = 0;
1659 }
1660 }
drh66560ad2006-01-06 14:32:19 +00001661 sqlite3OsLeaveMutex();
drh054889e2005-11-30 03:20:31 +00001662 pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001663 return rc;
drhbbd42a62004-05-22 17:41:58 +00001664}
1665
1666/*
danielk1977e3026632004-06-22 11:29:02 +00001667** Close a file.
1668*/
danielk197762079062007-08-15 17:08:46 +00001669static int unixClose(sqlite3_file *id){
1670 unixFile *pFile = (unixFile *)id;
1671 if( !pFile ) return SQLITE_OK;
1672 unixUnlock(id, NO_LOCK);
1673 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1674 pFile->dirfd = -1;
drh66560ad2006-01-06 14:32:19 +00001675 sqlite3OsEnterMutex();
danielk1977441b09a2006-01-05 13:48:29 +00001676
danielk197762079062007-08-15 17:08:46 +00001677 if( pFile->pOpen->nLock ){
danielk1977e3026632004-06-22 11:29:02 +00001678 /* If there are outstanding locks, do not actually close the file just
1679 ** yet because that would clear those locks. Instead, add the file
1680 ** descriptor to pOpen->aPending. It will be automatically closed when
1681 ** the last lock is cleared.
1682 */
1683 int *aNew;
danielk197762079062007-08-15 17:08:46 +00001684 struct openCnt *pOpen = pFile->pOpen;
drh64b1bea2006-01-15 02:30:57 +00001685 aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e3026632004-06-22 11:29:02 +00001686 if( aNew==0 ){
1687 /* If a malloc fails, just leak the file descriptor */
1688 }else{
1689 pOpen->aPending = aNew;
danielk197762079062007-08-15 17:08:46 +00001690 pOpen->aPending[pOpen->nPending] = pFile->h;
drhad81e872005-08-21 21:45:01 +00001691 pOpen->nPending++;
danielk1977e3026632004-06-22 11:29:02 +00001692 }
1693 }else{
1694 /* There are no outstanding locks so we can close the file immediately */
danielk197762079062007-08-15 17:08:46 +00001695 close(pFile->h);
danielk1977e3026632004-06-22 11:29:02 +00001696 }
danielk197762079062007-08-15 17:08:46 +00001697 releaseLockInfo(pFile->pLock);
1698 releaseOpenCnt(pFile->pOpen);
danielk1977441b09a2006-01-05 13:48:29 +00001699
drh66560ad2006-01-06 14:32:19 +00001700 sqlite3OsLeaveMutex();
danielk197762079062007-08-15 17:08:46 +00001701 pFile->isOpen = 0;
1702 OSTRACE2("CLOSE %-3d\n", pFile->h);
danielk1977e3026632004-06-22 11:29:02 +00001703 OpenCounter(-1);
danielk1977750b03e2006-02-14 10:48:39 +00001704 sqlite3ThreadSafeFree(id);
drh02afc862006-01-20 18:10:57 +00001705 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001706}
1707
drhbfe66312006-10-03 17:40:40 +00001708
1709#ifdef SQLITE_ENABLE_LOCKING_STYLE
1710#pragma mark AFP Support
1711
1712/*
1713 ** The afpLockingContext structure contains all afp lock specific state
1714 */
1715typedef struct afpLockingContext afpLockingContext;
1716struct afpLockingContext {
1717 unsigned long long sharedLockByte;
1718 char *filePath;
1719};
1720
1721struct ByteRangeLockPB2
1722{
1723 unsigned long long offset; /* offset to first byte to lock */
1724 unsigned long long length; /* nbr of bytes to lock */
1725 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1726 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1727 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1728 int fd; /* file desc to assoc this lock with */
1729};
1730
drhfd131da2007-08-07 17:13:03 +00001731#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001732
1733/* return 0 on success, 1 on failure. To match the behavior of the
1734 normal posix file locking (used in unixLock for example), we should
1735 provide 'richer' return codes - specifically to differentiate between
1736 'file busy' and 'file system error' results */
1737static int _AFPFSSetLock(const char *path, int fd, unsigned long long offset,
1738 unsigned long long length, int setLockFlag)
1739{
drhfd131da2007-08-07 17:13:03 +00001740 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001741 int err;
1742
1743 pb.unLockFlag = setLockFlag ? 0 : 1;
1744 pb.startEndFlag = 0;
1745 pb.offset = offset;
1746 pb.length = length;
1747 pb.fd = fd;
drh4f0c5872007-03-26 22:05:01 +00001748 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
drhbfe66312006-10-03 17:40:40 +00001749 (setLockFlag?"ON":"OFF"), fd, offset, length);
1750 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1751 if ( err==-1 ) {
drh4f0c5872007-03-26 22:05:01 +00001752 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
drhbfe66312006-10-03 17:40:40 +00001753 strerror(errno));
drh3b62b2f2007-06-08 18:27:03 +00001754 return 1; /* error */
drhbfe66312006-10-03 17:40:40 +00001755 } else {
1756 return 0;
1757 }
1758}
1759
1760/*
1761 ** This routine checks if there is a RESERVED lock held on the specified
1762 ** file by this or any other process. If such a lock is held, return
1763 ** non-zero. If the file is unlocked or holds only SHARED locks, then
1764 ** return zero.
1765 */
1766static int afpUnixCheckReservedLock(OsFile *id){
1767 int r = 0;
1768 unixFile *pFile = (unixFile*)id;
1769
1770 assert( pFile );
1771 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1772
1773 /* Check if a thread in this process holds such a lock */
1774 if( pFile->locktype>SHARED_LOCK ){
1775 r = 1;
1776 }
1777
1778 /* Otherwise see if some other process holds it.
1779 */
1780 if ( !r ) {
drh3b62b2f2007-06-08 18:27:03 +00001781 /* lock the byte */
drhbfe66312006-10-03 17:40:40 +00001782 int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1783 if (failed) {
1784 /* if we failed to get the lock then someone else must have it */
1785 r = 1;
1786 } else {
1787 /* if we succeeded in taking the reserved lock, unlock it to restore
1788 ** the original state */
1789 _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
1790 }
1791 }
drh4f0c5872007-03-26 22:05:01 +00001792 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
drhbfe66312006-10-03 17:40:40 +00001793
1794 return r;
1795}
1796
1797/* AFP-style locking following the behavior of unixLock, see the unixLock
1798** function comments for details of lock management. */
1799static int afpUnixLock(OsFile *id, int locktype)
1800{
1801 int rc = SQLITE_OK;
1802 unixFile *pFile = (unixFile*)id;
1803 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1804 int gotPendingLock = 0;
1805
1806 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001807 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001808 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
1809 /* If there is already a lock of this type or more restrictive on the
1810 ** OsFile, do nothing. Don't use the afp_end_lock: exit path, as
1811 ** sqlite3OsEnterMutex() hasn't been called yet.
1812 */
1813 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001814 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001815 locktypeName(locktype));
1816 return SQLITE_OK;
1817 }
1818
1819 /* Make sure the locking sequence is correct
1820 */
1821 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
1822 assert( locktype!=PENDING_LOCK );
1823 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
1824
1825 /* This mutex is needed because pFile->pLock is shared across threads
1826 */
1827 sqlite3OsEnterMutex();
1828
1829 /* Make sure the current thread owns the pFile.
1830 */
1831 rc = transferOwnership(pFile);
1832 if( rc!=SQLITE_OK ){
1833 sqlite3OsLeaveMutex();
1834 return rc;
1835 }
1836
1837 /* A PENDING lock is needed before acquiring a SHARED lock and before
1838 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1839 ** be released.
1840 */
1841 if( locktype==SHARED_LOCK
1842 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
1843 ){
1844 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1845 PENDING_BYTE, 1, 1);
1846 if (failed) {
1847 rc = SQLITE_BUSY;
1848 goto afp_end_lock;
1849 }
1850 }
1851
1852 /* If control gets to this point, then actually go ahead and make
1853 ** operating system calls for the specified lock.
1854 */
1855 if( locktype==SHARED_LOCK ){
1856 int lk, failed;
1857 int tries = 0;
1858
1859 /* Now get the read-lock */
1860 /* note that the quality of the randomness doesn't matter that much */
1861 lk = random();
1862 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
1863 failed = _AFPFSSetLock(context->filePath, pFile->h,
1864 SHARED_FIRST+context->sharedLockByte, 1, 1);
1865
1866 /* Drop the temporary PENDING lock */
1867 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)) {
1868 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1869 goto afp_end_lock;
1870 }
1871
1872 if( failed ){
1873 rc = SQLITE_BUSY;
1874 } else {
1875 pFile->locktype = SHARED_LOCK;
1876 }
1877 }else{
1878 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1879 ** assumed that there is a SHARED or greater lock on the file
1880 ** already.
1881 */
1882 int failed = 0;
1883 assert( 0!=pFile->locktype );
1884 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
1885 /* Acquire a RESERVED lock */
1886 failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1887 }
1888 if (!failed && locktype == EXCLUSIVE_LOCK) {
1889 /* Acquire an EXCLUSIVE lock */
1890
1891 /* Remove the shared lock before trying the range. we'll need to
1892 ** reestablish the shared lock if we can't get the afpUnixUnlock
1893 */
1894 if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1895 context->sharedLockByte, 1, 0)) {
1896 /* now attemmpt to get the exclusive lock range */
1897 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1898 SHARED_SIZE, 1);
1899 if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1900 context->sharedLockByte, 1, 1)) {
1901 rc = SQLITE_IOERR_RDLOCK; /* this should never happen */
1902 }
1903 } else {
1904 /* */
1905 rc = SQLITE_IOERR_UNLOCK; /* this should never happen */
1906 }
1907 }
1908 if( failed && rc == SQLITE_OK){
1909 rc = SQLITE_BUSY;
1910 }
1911 }
1912
1913 if( rc==SQLITE_OK ){
1914 pFile->locktype = locktype;
1915 }else if( locktype==EXCLUSIVE_LOCK ){
1916 pFile->locktype = PENDING_LOCK;
1917 }
1918
1919afp_end_lock:
1920 sqlite3OsLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001921 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00001922 rc==SQLITE_OK ? "ok" : "failed");
1923 return rc;
1924}
1925
1926/*
1927 ** Lower the locking level on file descriptor pFile to locktype. locktype
1928 ** must be either NO_LOCK or SHARED_LOCK.
1929 **
1930 ** If the locking level of the file descriptor is already at or below
1931 ** the requested locking level, this routine is a no-op.
1932 */
1933static int afpUnixUnlock(OsFile *id, int locktype) {
1934 struct flock lock;
1935 int rc = SQLITE_OK;
1936 unixFile *pFile = (unixFile*)id;
1937 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1938
1939 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001940 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00001941 pFile->locktype, getpid());
1942
1943 assert( locktype<=SHARED_LOCK );
1944 if( pFile->locktype<=locktype ){
1945 return SQLITE_OK;
1946 }
1947 if( CHECK_THREADID(pFile) ){
1948 return SQLITE_MISUSE;
1949 }
1950 sqlite3OsEnterMutex();
1951 if( pFile->locktype>SHARED_LOCK ){
1952 if( locktype==SHARED_LOCK ){
1953 int failed = 0;
1954
1955 /* unlock the exclusive range - then re-establish the shared lock */
1956 if (pFile->locktype==EXCLUSIVE_LOCK) {
1957 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1958 SHARED_SIZE, 0);
1959 if (!failed) {
1960 /* successfully removed the exclusive lock */
1961 if (_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST+
1962 context->sharedLockByte, 1, 1)) {
1963 /* failed to re-establish our shared lock */
1964 rc = SQLITE_IOERR_RDLOCK; /* This should never happen */
1965 }
1966 } else {
1967 /* This should never happen - failed to unlock the exclusive range */
1968 rc = SQLITE_IOERR_UNLOCK;
1969 }
1970 }
1971 }
1972 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
1973 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)){
1974 /* failed to release the pending lock */
1975 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1976 }
1977 }
1978 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
1979 if (_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0)) {
1980 /* failed to release the reserved lock */
1981 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1982 }
1983 }
1984 }
1985 if( locktype==NO_LOCK ){
1986 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1987 SHARED_FIRST + context->sharedLockByte, 1, 0);
1988 if (failed) {
1989 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1990 }
1991 }
1992 if (rc == SQLITE_OK)
1993 pFile->locktype = locktype;
1994 sqlite3OsLeaveMutex();
1995 return rc;
1996}
1997
1998/*
1999 ** Close a file & cleanup AFP specific locking context
2000 */
2001static int afpUnixClose(OsFile **pId) {
2002 unixFile *id = (unixFile*)*pId;
2003
2004 if( !id ) return SQLITE_OK;
2005 afpUnixUnlock(*pId, NO_LOCK);
2006 /* free the AFP locking structure */
2007 if (id->lockingContext != NULL) {
2008 if (((afpLockingContext *)id->lockingContext)->filePath != NULL)
2009 sqlite3ThreadSafeFree(((afpLockingContext*)id->lockingContext)->filePath);
2010 sqlite3ThreadSafeFree(id->lockingContext);
2011 }
2012
2013 if( id->dirfd>=0 ) close(id->dirfd);
2014 id->dirfd = -1;
2015 close(id->h);
2016 id->isOpen = 0;
drh4f0c5872007-03-26 22:05:01 +00002017 OSTRACE2("CLOSE %-3d\n", id->h);
drhbfe66312006-10-03 17:40:40 +00002018 OpenCounter(-1);
2019 sqlite3ThreadSafeFree(id);
2020 *pId = 0;
2021 return SQLITE_OK;
2022}
2023
2024
2025#pragma mark flock() style locking
2026
2027/*
2028 ** The flockLockingContext is not used
2029 */
2030typedef void flockLockingContext;
2031
2032static int flockUnixCheckReservedLock(OsFile *id) {
2033 unixFile *pFile = (unixFile*)id;
2034
2035 if (pFile->locktype == RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00002036 return 1; /* already have a reserved lock */
drhbfe66312006-10-03 17:40:40 +00002037 } else {
drh3b62b2f2007-06-08 18:27:03 +00002038 /* attempt to get the lock */
drhbfe66312006-10-03 17:40:40 +00002039 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
2040 if (!rc) {
drh3b62b2f2007-06-08 18:27:03 +00002041 /* got the lock, unlock it */
drhbfe66312006-10-03 17:40:40 +00002042 flock(pFile->h, LOCK_UN);
drh3b62b2f2007-06-08 18:27:03 +00002043 return 0; /* no one has it reserved */
drhbfe66312006-10-03 17:40:40 +00002044 }
drh3b62b2f2007-06-08 18:27:03 +00002045 return 1; /* someone else might have it reserved */
drhbfe66312006-10-03 17:40:40 +00002046 }
2047}
2048
2049static int flockUnixLock(OsFile *id, int locktype) {
2050 unixFile *pFile = (unixFile*)id;
2051
drh3b62b2f2007-06-08 18:27:03 +00002052 /* if we already have a lock, it is exclusive.
2053 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002054 if (pFile->locktype > NO_LOCK) {
2055 pFile->locktype = locktype;
2056 return SQLITE_OK;
2057 }
2058
drh3b62b2f2007-06-08 18:27:03 +00002059 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00002060 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
2061 if (rc) {
drh3b62b2f2007-06-08 18:27:03 +00002062 /* didn't get, must be busy */
drhbfe66312006-10-03 17:40:40 +00002063 return SQLITE_BUSY;
2064 } else {
drh3b62b2f2007-06-08 18:27:03 +00002065 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002066 pFile->locktype = locktype;
2067 return SQLITE_OK;
2068 }
2069}
2070
2071static int flockUnixUnlock(OsFile *id, int locktype) {
2072 unixFile *pFile = (unixFile*)id;
2073
2074 assert( locktype<=SHARED_LOCK );
2075
drh3b62b2f2007-06-08 18:27:03 +00002076 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002077 if( pFile->locktype==locktype ){
2078 return SQLITE_OK;
2079 }
2080
drh3b62b2f2007-06-08 18:27:03 +00002081 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002082 if (locktype==SHARED_LOCK) {
2083 pFile->locktype = locktype;
2084 return SQLITE_OK;
2085 }
2086
drh3b62b2f2007-06-08 18:27:03 +00002087 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00002088 int rc = flock(pFile->h, LOCK_UN);
2089 if (rc)
2090 return SQLITE_IOERR_UNLOCK;
2091 else {
2092 pFile->locktype = NO_LOCK;
2093 return SQLITE_OK;
2094 }
2095}
2096
2097/*
2098 ** Close a file.
2099 */
2100static int flockUnixClose(OsFile **pId) {
2101 unixFile *id = (unixFile*)*pId;
2102
2103 if( !id ) return SQLITE_OK;
2104 flockUnixUnlock(*pId, NO_LOCK);
2105
2106 if( id->dirfd>=0 ) close(id->dirfd);
2107 id->dirfd = -1;
2108 sqlite3OsEnterMutex();
2109
2110 close(id->h);
2111 sqlite3OsLeaveMutex();
2112 id->isOpen = 0;
drh4f0c5872007-03-26 22:05:01 +00002113 OSTRACE2("CLOSE %-3d\n", id->h);
drhbfe66312006-10-03 17:40:40 +00002114 OpenCounter(-1);
2115 sqlite3ThreadSafeFree(id);
2116 *pId = 0;
2117 return SQLITE_OK;
2118}
2119
2120#pragma mark Old-School .lock file based locking
2121
2122/*
2123 ** The dotlockLockingContext structure contains all dotlock (.lock) lock
2124 ** specific state
2125 */
2126typedef struct dotlockLockingContext dotlockLockingContext;
2127struct dotlockLockingContext {
2128 char *lockPath;
2129};
2130
2131
2132static int dotlockUnixCheckReservedLock(OsFile *id) {
2133 unixFile *pFile = (unixFile*)id;
2134 dotlockLockingContext *context =
2135 (dotlockLockingContext *) pFile->lockingContext;
2136
2137 if (pFile->locktype == RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00002138 return 1; /* already have a reserved lock */
drhbfe66312006-10-03 17:40:40 +00002139 } else {
2140 struct stat statBuf;
2141 if (lstat(context->lockPath,&statBuf) == 0)
drh3b62b2f2007-06-08 18:27:03 +00002142 /* file exists, someone else has the lock */
drhbfe66312006-10-03 17:40:40 +00002143 return 1;
2144 else
drh3b62b2f2007-06-08 18:27:03 +00002145 /* file does not exist, we could have it if we want it */
drhbfe66312006-10-03 17:40:40 +00002146 return 0;
2147 }
2148}
2149
2150static int dotlockUnixLock(OsFile *id, int locktype) {
2151 unixFile *pFile = (unixFile*)id;
2152 dotlockLockingContext *context =
2153 (dotlockLockingContext *) pFile->lockingContext;
2154
drh3b62b2f2007-06-08 18:27:03 +00002155 /* if we already have a lock, it is exclusive.
2156 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002157 if (pFile->locktype > NO_LOCK) {
2158 pFile->locktype = locktype;
2159
2160 /* Always update the timestamp on the old file */
2161 utimes(context->lockPath,NULL);
2162 return SQLITE_OK;
2163 }
2164
drh3b62b2f2007-06-08 18:27:03 +00002165 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00002166 struct stat statBuf;
2167 if (lstat(context->lockPath,&statBuf) == 0){
drh3b62b2f2007-06-08 18:27:03 +00002168 return SQLITE_BUSY; /* it does, busy */
drhbfe66312006-10-03 17:40:40 +00002169 }
2170
drh3b62b2f2007-06-08 18:27:03 +00002171 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00002172 int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600);
2173 if (fd < 0) {
drh3b62b2f2007-06-08 18:27:03 +00002174 /* failed to open/create the file, someone else may have stolen the lock */
drhbfe66312006-10-03 17:40:40 +00002175 return SQLITE_BUSY;
2176 }
2177 close(fd);
2178
drh3b62b2f2007-06-08 18:27:03 +00002179 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002180 pFile->locktype = locktype;
2181 return SQLITE_OK;
2182}
2183
2184static int dotlockUnixUnlock(OsFile *id, int locktype) {
2185 unixFile *pFile = (unixFile*)id;
2186 dotlockLockingContext *context =
2187 (dotlockLockingContext *) pFile->lockingContext;
2188
2189 assert( locktype<=SHARED_LOCK );
2190
drh3b62b2f2007-06-08 18:27:03 +00002191 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002192 if( pFile->locktype==locktype ){
2193 return SQLITE_OK;
2194 }
2195
drh3b62b2f2007-06-08 18:27:03 +00002196 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002197 if (locktype==SHARED_LOCK) {
2198 pFile->locktype = locktype;
2199 return SQLITE_OK;
2200 }
2201
drh3b62b2f2007-06-08 18:27:03 +00002202 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00002203 unlink(context->lockPath);
2204 pFile->locktype = NO_LOCK;
2205 return SQLITE_OK;
2206}
2207
2208/*
2209 ** Close a file.
2210 */
2211static int dotlockUnixClose(OsFile **pId) {
2212 unixFile *id = (unixFile*)*pId;
2213
2214 if( !id ) return SQLITE_OK;
2215 dotlockUnixUnlock(*pId, NO_LOCK);
2216 /* free the dotlock locking structure */
2217 if (id->lockingContext != NULL) {
2218 if (((dotlockLockingContext *)id->lockingContext)->lockPath != NULL)
2219 sqlite3ThreadSafeFree( ( (dotlockLockingContext *)
2220 id->lockingContext)->lockPath);
2221 sqlite3ThreadSafeFree(id->lockingContext);
2222 }
2223
2224 if( id->dirfd>=0 ) close(id->dirfd);
2225 id->dirfd = -1;
2226 sqlite3OsEnterMutex();
2227
2228 close(id->h);
2229
2230 sqlite3OsLeaveMutex();
2231 id->isOpen = 0;
drh4f0c5872007-03-26 22:05:01 +00002232 OSTRACE2("CLOSE %-3d\n", id->h);
drhbfe66312006-10-03 17:40:40 +00002233 OpenCounter(-1);
2234 sqlite3ThreadSafeFree(id);
2235 *pId = 0;
2236 return SQLITE_OK;
2237}
2238
2239
2240#pragma mark No locking
2241
2242/*
2243 ** The nolockLockingContext is void
2244 */
2245typedef void nolockLockingContext;
2246
2247static int nolockUnixCheckReservedLock(OsFile *id) {
2248 return 0;
2249}
2250
2251static int nolockUnixLock(OsFile *id, int locktype) {
2252 return SQLITE_OK;
2253}
2254
2255static int nolockUnixUnlock(OsFile *id, int locktype) {
2256 return SQLITE_OK;
2257}
2258
2259/*
2260 ** Close a file.
2261 */
2262static int nolockUnixClose(OsFile **pId) {
2263 unixFile *id = (unixFile*)*pId;
2264
2265 if( !id ) return SQLITE_OK;
2266 if( id->dirfd>=0 ) close(id->dirfd);
2267 id->dirfd = -1;
2268 sqlite3OsEnterMutex();
2269
2270 close(id->h);
2271
2272 sqlite3OsLeaveMutex();
2273 id->isOpen = 0;
drh4f0c5872007-03-26 22:05:01 +00002274 OSTRACE2("CLOSE %-3d\n", id->h);
drhbfe66312006-10-03 17:40:40 +00002275 OpenCounter(-1);
2276 sqlite3ThreadSafeFree(id);
2277 *pId = 0;
2278 return SQLITE_OK;
2279}
2280
2281#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2282
danielk1977e3026632004-06-22 11:29:02 +00002283/*
drh0ccebe72005-06-07 22:22:50 +00002284** Turn a relative pathname into a full pathname. Return a pointer
2285** to the full pathname stored in space obtained from sqliteMalloc().
2286** The calling function is responsible for freeing this space once it
2287** is no longer needed.
2288*/
drh66560ad2006-01-06 14:32:19 +00002289char *sqlite3UnixFullPathname(const char *zRelative){
drh0ccebe72005-06-07 22:22:50 +00002290 char *zFull = 0;
2291 if( zRelative[0]=='/' ){
2292 sqlite3SetString(&zFull, zRelative, (char*)0);
2293 }else{
drh79158e12005-09-06 21:40:45 +00002294 char *zBuf = sqliteMalloc(5000);
2295 if( zBuf==0 ){
2296 return 0;
2297 }
drh0ccebe72005-06-07 22:22:50 +00002298 zBuf[0] = 0;
drh79158e12005-09-06 21:40:45 +00002299 sqlite3SetString(&zFull, getcwd(zBuf, 5000), "/", zRelative,
drh0ccebe72005-06-07 22:22:50 +00002300 (char*)0);
drh79158e12005-09-06 21:40:45 +00002301 sqliteFree(zBuf);
drh0ccebe72005-06-07 22:22:50 +00002302 }
drh4eb9a972006-02-13 18:42:21 +00002303
2304#if 0
drh89ea9312006-02-13 17:03:47 +00002305 /*
2306 ** Remove "/./" path elements and convert "/A/./" path elements
2307 ** to just "/".
2308 */
2309 if( zFull ){
drh4eb9a972006-02-13 18:42:21 +00002310 int i, j;
drh89ea9312006-02-13 17:03:47 +00002311 for(i=j=0; zFull[i]; i++){
2312 if( zFull[i]=='/' ){
2313 if( zFull[i+1]=='/' ) continue;
2314 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
2315 i += 1;
2316 continue;
2317 }
2318 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
2319 while( j>0 && zFull[j-1]!='/' ){ j--; }
2320 i += 3;
2321 continue;
2322 }
2323 }
2324 zFull[j++] = zFull[i];
2325 }
2326 zFull[j] = 0;
2327 }
drh4eb9a972006-02-13 18:42:21 +00002328#endif
2329
drh0ccebe72005-06-07 22:22:50 +00002330 return zFull;
2331}
2332
drh18839212005-11-26 03:43:23 +00002333/*
drh9cbe6352005-11-29 03:13:21 +00002334** Change the value of the fullsync flag in the given file descriptor.
drh18839212005-11-26 03:43:23 +00002335*/
drh9cbe6352005-11-29 03:13:21 +00002336static void unixSetFullSync(OsFile *id, int v){
drh054889e2005-11-30 03:20:31 +00002337 ((unixFile*)id)->fullSync = v;
drh9cbe6352005-11-29 03:13:21 +00002338}
2339
2340/*
2341** Return the underlying file handle for an OsFile
2342*/
2343static int unixFileHandle(OsFile *id){
drh054889e2005-11-30 03:20:31 +00002344 return ((unixFile*)id)->h;
drh9cbe6352005-11-29 03:13:21 +00002345}
2346
2347/*
2348** Return an integer that indices the type of lock currently held
2349** by this handle. (Used for testing and analysis only.)
2350*/
2351static int unixLockState(OsFile *id){
drh054889e2005-11-30 03:20:31 +00002352 return ((unixFile*)id)->locktype;
drh18839212005-11-26 03:43:23 +00002353}
drh0ccebe72005-06-07 22:22:50 +00002354
drh9c06c952005-11-26 00:25:00 +00002355/*
danielk1977a3d4c882007-03-23 10:08:38 +00002356** Return the sector size in bytes of the underlying block device for
2357** the specified file. This is almost always 512 bytes, but may be
2358** larger for some devices.
2359**
2360** SQLite code assumes this function cannot fail. It also assumes that
2361** if two files are created in the same file-system directory (i.e.
2362** a database and it's journal file) that the sector size will be the
2363** same for both.
2364*/
danielk197762079062007-08-15 17:08:46 +00002365static int unixSectorSize(sqlite3_file *id){
drh3ceeb752007-03-29 18:19:52 +00002366 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002367}
2368
danielk197762079062007-08-15 17:08:46 +00002369static int unixDeviceCharacteristics(sqlite3_file *id){
2370 return 0;
2371}
2372
2373static int unixBreakLock(sqlite3_file *id){
2374 assert(!"TODO: unixBreakLock()");
2375 return 0;
2376}
2377
danielk1977a3d4c882007-03-23 10:08:38 +00002378/*
drh054889e2005-11-30 03:20:31 +00002379** This vector defines all the methods that can operate on an OsFile
2380** for unix.
drh9c06c952005-11-26 00:25:00 +00002381*/
danielk197762079062007-08-15 17:08:46 +00002382static const sqlite3_io_methods sqlite3UnixIoMethod = {
2383 1, /* iVersion */
drh9c06c952005-11-26 00:25:00 +00002384 unixClose,
2385 unixRead,
2386 unixWrite,
drh9c06c952005-11-26 00:25:00 +00002387 unixTruncate,
drh054889e2005-11-30 03:20:31 +00002388 unixSync,
drh054889e2005-11-30 03:20:31 +00002389 unixFileSize,
2390 unixLock,
2391 unixUnlock,
drh054889e2005-11-30 03:20:31 +00002392 unixCheckReservedLock,
danielk197762079062007-08-15 17:08:46 +00002393 unixBreakLock,
danielk1977a3d4c882007-03-23 10:08:38 +00002394 unixSectorSize,
danielk197762079062007-08-15 17:08:46 +00002395 unixDeviceCharacteristics
drh9c06c952005-11-26 00:25:00 +00002396};
2397
drhbfe66312006-10-03 17:40:40 +00002398#ifdef SQLITE_ENABLE_LOCKING_STYLE
drh054889e2005-11-30 03:20:31 +00002399/*
drhbfe66312006-10-03 17:40:40 +00002400 ** This vector defines all the methods that can operate on an OsFile
2401 ** for unix with AFP style file locking.
2402 */
2403static const IoMethod sqlite3AFPLockingUnixIoMethod = {
2404 afpUnixClose,
2405 unixOpenDirectory,
2406 unixRead,
2407 unixWrite,
2408 unixSeek,
2409 unixTruncate,
2410 unixSync,
2411 unixSetFullSync,
2412 unixFileHandle,
2413 unixFileSize,
2414 afpUnixLock,
2415 afpUnixUnlock,
2416 unixLockState,
2417 afpUnixCheckReservedLock,
danielk1977a3d4c882007-03-23 10:08:38 +00002418 unixSectorSize,
drhbfe66312006-10-03 17:40:40 +00002419};
2420
2421/*
2422 ** This vector defines all the methods that can operate on an OsFile
2423 ** for unix with flock() style file locking.
2424 */
2425static const IoMethod sqlite3FlockLockingUnixIoMethod = {
2426 flockUnixClose,
2427 unixOpenDirectory,
2428 unixRead,
2429 unixWrite,
2430 unixSeek,
2431 unixTruncate,
2432 unixSync,
2433 unixSetFullSync,
2434 unixFileHandle,
2435 unixFileSize,
2436 flockUnixLock,
2437 flockUnixUnlock,
2438 unixLockState,
2439 flockUnixCheckReservedLock,
danielk1977a3d4c882007-03-23 10:08:38 +00002440 unixSectorSize,
drhbfe66312006-10-03 17:40:40 +00002441};
2442
2443/*
2444 ** This vector defines all the methods that can operate on an OsFile
2445 ** for unix with dotlock style file locking.
2446 */
2447static const IoMethod sqlite3DotlockLockingUnixIoMethod = {
2448 dotlockUnixClose,
2449 unixOpenDirectory,
2450 unixRead,
2451 unixWrite,
2452 unixSeek,
2453 unixTruncate,
2454 unixSync,
2455 unixSetFullSync,
2456 unixFileHandle,
2457 unixFileSize,
2458 dotlockUnixLock,
2459 dotlockUnixUnlock,
2460 unixLockState,
2461 dotlockUnixCheckReservedLock,
danielk1977a3d4c882007-03-23 10:08:38 +00002462 unixSectorSize,
drhbfe66312006-10-03 17:40:40 +00002463};
2464
2465/*
2466 ** This vector defines all the methods that can operate on an OsFile
2467 ** for unix with dotlock style file locking.
2468 */
2469static const IoMethod sqlite3NolockLockingUnixIoMethod = {
2470 nolockUnixClose,
2471 unixOpenDirectory,
2472 unixRead,
2473 unixWrite,
2474 unixSeek,
2475 unixTruncate,
2476 unixSync,
2477 unixSetFullSync,
2478 unixFileHandle,
2479 unixFileSize,
2480 nolockUnixLock,
2481 nolockUnixUnlock,
2482 unixLockState,
2483 nolockUnixCheckReservedLock,
danielk1977a3d4c882007-03-23 10:08:38 +00002484 unixSectorSize,
drhbfe66312006-10-03 17:40:40 +00002485};
2486
2487#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2488
2489/*
2490** Allocate memory for a new unixFile and initialize that unixFile.
2491** Write a pointer to the new unixFile into *pId.
2492** If we run out of memory, close the file and return an error.
drh054889e2005-11-30 03:20:31 +00002493*/
drhbfe66312006-10-03 17:40:40 +00002494#ifdef SQLITE_ENABLE_LOCKING_STYLE
2495/*
2496 ** When locking extensions are enabled, the filepath and locking style
2497 ** are needed to determine the unixFile pMethod to use for locking operations.
2498 ** The locking-style specific lockingContext data structure is created
2499 ** and assigned here also.
2500 */
2501static int allocateUnixFile(
2502 int h, /* Open file descriptor of file being opened */
2503 OsFile **pId, /* Write completed initialization here */
2504 const char *zFilename, /* Name of the file being opened */
2505 int delFlag /* Delete-on-or-before-close flag */
2506){
aswift108bc322006-10-11 17:19:46 +00002507 sqlite3LockingStyle lockingStyle;
drh054889e2005-11-30 03:20:31 +00002508 unixFile *pNew;
drhbfe66312006-10-03 17:40:40 +00002509 unixFile f;
2510 int rc;
2511
drh61fc5952007-04-01 23:49:51 +00002512 memset(&f, 0, sizeof(f));
aswift448aa6f2006-11-11 01:31:58 +00002513 lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
drhbfe66312006-10-03 17:40:40 +00002514 if ( lockingStyle == posixLockingStyle ) {
2515 sqlite3OsEnterMutex();
2516 rc = findLockInfo(h, &f.pLock, &f.pOpen);
2517 sqlite3OsLeaveMutex();
2518 if( rc ){
2519 close(h);
2520 unlink(zFilename);
2521 return SQLITE_NOMEM;
2522 }
2523 } else {
drh3b62b2f2007-06-08 18:27:03 +00002524 /* pLock and pOpen are only used for posix advisory locking */
drhbfe66312006-10-03 17:40:40 +00002525 f.pLock = NULL;
2526 f.pOpen = NULL;
2527 }
2528 if( delFlag ){
2529 unlink(zFilename);
2530 }
2531 f.dirfd = -1;
drhbfe66312006-10-03 17:40:40 +00002532 f.h = h;
2533 SET_THREADID(&f);
danielk1977750b03e2006-02-14 10:48:39 +00002534 pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
drh054889e2005-11-30 03:20:31 +00002535 if( pNew==0 ){
drhbfe66312006-10-03 17:40:40 +00002536 close(h);
drh029b44b2006-01-15 00:13:15 +00002537 sqlite3OsEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002538 releaseLockInfo(f.pLock);
2539 releaseOpenCnt(f.pOpen);
drh029b44b2006-01-15 00:13:15 +00002540 sqlite3OsLeaveMutex();
drh054889e2005-11-30 03:20:31 +00002541 *pId = 0;
2542 return SQLITE_NOMEM;
2543 }else{
drhbfe66312006-10-03 17:40:40 +00002544 *pNew = f;
aswift108bc322006-10-11 17:19:46 +00002545 switch(lockingStyle) {
drh5bb3eb92007-05-04 13:15:55 +00002546 case afpLockingStyle: {
drhbfe66312006-10-03 17:40:40 +00002547 /* afp locking uses the file path so it needs to be included in
2548 ** the afpLockingContext */
drh5bb3eb92007-05-04 13:15:55 +00002549 int nFilename;
drhbfe66312006-10-03 17:40:40 +00002550 pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
2551 pNew->lockingContext =
2552 sqlite3ThreadSafeMalloc(sizeof(afpLockingContext));
drh5bb3eb92007-05-04 13:15:55 +00002553 nFilename = strlen(zFilename)+1;
drhbfe66312006-10-03 17:40:40 +00002554 ((afpLockingContext *)pNew->lockingContext)->filePath =
drh5bb3eb92007-05-04 13:15:55 +00002555 sqlite3ThreadSafeMalloc(nFilename);
2556 memcpy(((afpLockingContext *)pNew->lockingContext)->filePath,
2557 zFilename, nFilename);
drhbfe66312006-10-03 17:40:40 +00002558 srandomdev();
2559 break;
drh5bb3eb92007-05-04 13:15:55 +00002560 }
drhbfe66312006-10-03 17:40:40 +00002561 case flockLockingStyle:
2562 /* flock locking doesn't need additional lockingContext information */
2563 pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
2564 break;
drh5bb3eb92007-05-04 13:15:55 +00002565 case dotlockLockingStyle: {
drhbfe66312006-10-03 17:40:40 +00002566 /* dotlock locking uses the file path so it needs to be included in
2567 ** the dotlockLockingContext */
drh5bb3eb92007-05-04 13:15:55 +00002568 int nFilename;
drhbfe66312006-10-03 17:40:40 +00002569 pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
2570 pNew->lockingContext = sqlite3ThreadSafeMalloc(
2571 sizeof(dotlockLockingContext));
drh5bb3eb92007-05-04 13:15:55 +00002572 nFilename = strlen(zFilename) + 6;
drhbfe66312006-10-03 17:40:40 +00002573 ((dotlockLockingContext *)pNew->lockingContext)->lockPath =
drh5bb3eb92007-05-04 13:15:55 +00002574 sqlite3ThreadSafeMalloc( nFilename );
2575 sqlite3_snprintf(nFilename,
2576 ((dotlockLockingContext *)pNew->lockingContext)->lockPath,
drhbfe66312006-10-03 17:40:40 +00002577 "%s.lock", zFilename);
2578 break;
drh5bb3eb92007-05-04 13:15:55 +00002579 }
drhbfe66312006-10-03 17:40:40 +00002580 case posixLockingStyle:
2581 /* posix locking doesn't need additional lockingContext information */
2582 pNew->pMethod = &sqlite3UnixIoMethod;
2583 break;
2584 case noLockingStyle:
2585 case unsupportedLockingStyle:
2586 default:
2587 pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
2588 }
2589 *pId = (OsFile*)pNew;
2590 OpenCounter(+1);
2591 return SQLITE_OK;
2592 }
2593}
2594#else /* SQLITE_ENABLE_LOCKING_STYLE */
2595static int allocateUnixFile(
2596 int h, /* Open file descriptor on file being opened */
danielk197762079062007-08-15 17:08:46 +00002597 sqlite3_file **pId, /* Write the resul unixFile structure here */
drhbfe66312006-10-03 17:40:40 +00002598 const char *zFilename, /* Name of the file being opened */
2599 int delFlag /* If true, delete the file on or before closing */
2600){
2601 unixFile *pNew;
2602 unixFile f;
2603 int rc;
2604
drhe78669b2007-06-29 12:04:26 +00002605#ifdef FD_CLOEXEC
2606 fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
2607#endif
drh61fc5952007-04-01 23:49:51 +00002608 memset(&f, 0, sizeof(f));
drhbfe66312006-10-03 17:40:40 +00002609 sqlite3OsEnterMutex();
2610 rc = findLockInfo(h, &f.pLock, &f.pOpen);
2611 sqlite3OsLeaveMutex();
2612 if( delFlag ){
2613 unlink(zFilename);
2614 }
2615 if( rc ){
2616 close(h);
2617 return SQLITE_NOMEM;
2618 }
drh4f0c5872007-03-26 22:05:01 +00002619 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
drhbfe66312006-10-03 17:40:40 +00002620 f.dirfd = -1;
drhbfe66312006-10-03 17:40:40 +00002621 f.h = h;
2622 SET_THREADID(&f);
2623 pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
2624 if( pNew==0 ){
2625 close(h);
2626 sqlite3OsEnterMutex();
2627 releaseLockInfo(f.pLock);
2628 releaseOpenCnt(f.pOpen);
2629 sqlite3OsLeaveMutex();
2630 *pId = 0;
2631 return SQLITE_NOMEM;
2632 }else{
2633 *pNew = f;
drh054889e2005-11-30 03:20:31 +00002634 pNew->pMethod = &sqlite3UnixIoMethod;
danielk197762079062007-08-15 17:08:46 +00002635 *pId = (sqlite3_file*)pNew;
drh054889e2005-11-30 03:20:31 +00002636 OpenCounter(+1);
2637 return SQLITE_OK;
2638 }
2639}
drhbfe66312006-10-03 17:40:40 +00002640#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9c06c952005-11-26 00:25:00 +00002641
drh0ccebe72005-06-07 22:22:50 +00002642#endif /* SQLITE_OMIT_DISKIO */
2643/***************************************************************************
2644** Everything above deals with file I/O. Everything that follows deals
2645** with other miscellanous aspects of the operating system interface
2646****************************************************************************/
2647
2648
drh761df872006-12-21 01:29:22 +00002649#ifndef SQLITE_OMIT_LOAD_EXTENSION
2650/*
2651** Interfaces for opening a shared library, finding entry points
2652** within the shared library, and closing the shared library.
2653*/
2654#include <dlfcn.h>
2655void *sqlite3UnixDlopen(const char *zFilename){
2656 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
2657}
2658void *sqlite3UnixDlsym(void *pHandle, const char *zSymbol){
2659 return dlsym(pHandle, zSymbol);
2660}
2661int sqlite3UnixDlclose(void *pHandle){
2662 return dlclose(pHandle);
2663}
2664#endif /* SQLITE_OMIT_LOAD_EXTENSION */
2665
drh0ccebe72005-06-07 22:22:50 +00002666/*
drhbbd42a62004-05-22 17:41:58 +00002667** Get information to seed the random number generator. The seed
2668** is written into the buffer zBuf[256]. The calling function must
2669** supply a sufficiently large buffer.
2670*/
drh66560ad2006-01-06 14:32:19 +00002671int sqlite3UnixRandomSeed(char *zBuf){
drhbbd42a62004-05-22 17:41:58 +00002672 /* We have to initialize zBuf to prevent valgrind from reporting
2673 ** errors. The reports issued by valgrind are incorrect - we would
2674 ** prefer that the randomness be increased by making use of the
2675 ** uninitialized space in zBuf - but valgrind errors tend to worry
2676 ** some users. Rather than argue, it seems easier just to initialize
2677 ** the whole array and silence valgrind, even if that means less randomness
2678 ** in the random seed.
2679 **
2680 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00002681 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00002682 ** tests repeatable.
2683 */
2684 memset(zBuf, 0, 256);
2685#if !defined(SQLITE_TEST)
2686 {
drh842b8642005-01-21 17:53:17 +00002687 int pid, fd;
2688 fd = open("/dev/urandom", O_RDONLY);
2689 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00002690 time_t t;
2691 time(&t);
2692 memcpy(zBuf, &t, sizeof(t));
drh842b8642005-01-21 17:53:17 +00002693 pid = getpid();
2694 memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
2695 }else{
2696 read(fd, zBuf, 256);
2697 close(fd);
2698 }
drhbbd42a62004-05-22 17:41:58 +00002699 }
2700#endif
2701 return SQLITE_OK;
2702}
2703
2704/*
2705** Sleep for a little while. Return the amount of time slept.
drhf1a221e2006-01-15 17:27:17 +00002706** The argument is the number of milliseconds we want to sleep.
drhbbd42a62004-05-22 17:41:58 +00002707*/
drh66560ad2006-01-06 14:32:19 +00002708int sqlite3UnixSleep(int ms){
drhbbd42a62004-05-22 17:41:58 +00002709#if defined(HAVE_USLEEP) && HAVE_USLEEP
2710 usleep(ms*1000);
2711 return ms;
2712#else
2713 sleep((ms+999)/1000);
2714 return 1000*((ms+999)/1000);
2715#endif
2716}
2717
2718/*
drh5c111232006-02-10 04:33:12 +00002719** Static variables used for thread synchronization.
2720**
2721** inMutex the nesting depth of the recursive mutex. The thread
2722** holding mutexMain can read this variable at any time.
2723** But is must hold mutexAux to change this variable. Other
drh6a3d6702006-02-10 13:11:32 +00002724** threads must hold mutexAux to read the variable and can
2725** never write.
drh5c111232006-02-10 04:33:12 +00002726**
2727** mutexOwner The thread id of the thread holding mutexMain. Same
2728** access rules as for inMutex.
2729**
drh6a3d6702006-02-10 13:11:32 +00002730** mutexOwnerValid True if the value in mutexOwner is valid. The same
2731** access rules apply as for inMutex.
drh5c111232006-02-10 04:33:12 +00002732**
2733** mutexMain The main mutex. Hold this mutex in order to get exclusive
2734** access to SQLite data structures.
2735**
2736** mutexAux An auxiliary mutex needed to access variables defined above.
2737**
drh6a3d6702006-02-10 13:11:32 +00002738** Mutexes are always acquired in this order: mutexMain mutexAux. It
2739** is not necessary to acquire mutexMain in order to get mutexAux - just
2740** do not attempt to acquire them in the reverse order: mutexAux mutexMain.
2741** Either get the mutexes with mutexMain first or get mutexAux only.
2742**
2743** When running on a platform where the three variables inMutex, mutexOwner,
2744** and mutexOwnerValid can be set atomically, the mutexAux is not required.
2745** On many systems, all three are 32-bit integers and writing to a 32-bit
2746** integer is atomic. I think. But there are no guarantees. So it seems
2747** safer to protect them using mutexAux.
drhbbd42a62004-05-22 17:41:58 +00002748*/
2749static int inMutex = 0;
drh79069752004-05-22 21:30:40 +00002750#ifdef SQLITE_UNIX_THREADS
drh6a3d6702006-02-10 13:11:32 +00002751static pthread_t mutexOwner; /* Thread holding mutexMain */
drh5c111232006-02-10 04:33:12 +00002752static int mutexOwnerValid = 0; /* True if mutexOwner is valid */
2753static pthread_mutex_t mutexMain = PTHREAD_MUTEX_INITIALIZER; /* The mutex */
2754static pthread_mutex_t mutexAux = PTHREAD_MUTEX_INITIALIZER; /* Aux mutex */
drh79069752004-05-22 21:30:40 +00002755#endif
drhbbd42a62004-05-22 17:41:58 +00002756
2757/*
2758** The following pair of routine implement mutual exclusion for
2759** multi-threaded processes. Only a single thread is allowed to
2760** executed code that is surrounded by EnterMutex() and LeaveMutex().
2761**
2762** SQLite uses only a single Mutex. There is not much critical
2763** code and what little there is executes quickly and without blocking.
drhf1a221e2006-01-15 17:27:17 +00002764**
drh757b04e2006-01-18 17:25:45 +00002765** As of version 3.3.2, this mutex must be recursive.
drhbbd42a62004-05-22 17:41:58 +00002766*/
drh66560ad2006-01-06 14:32:19 +00002767void sqlite3UnixEnterMutex(){
drhbbd42a62004-05-22 17:41:58 +00002768#ifdef SQLITE_UNIX_THREADS
drh5c111232006-02-10 04:33:12 +00002769 pthread_mutex_lock(&mutexAux);
2770 if( !mutexOwnerValid || !pthread_equal(mutexOwner, pthread_self()) ){
2771 pthread_mutex_unlock(&mutexAux);
2772 pthread_mutex_lock(&mutexMain);
2773 assert( inMutex==0 );
2774 assert( !mutexOwnerValid );
2775 pthread_mutex_lock(&mutexAux);
drha3fad6f2006-01-18 14:06:37 +00002776 mutexOwner = pthread_self();
drh5c111232006-02-10 04:33:12 +00002777 mutexOwnerValid = 1;
drha3fad6f2006-01-18 14:06:37 +00002778 }
drha3fad6f2006-01-18 14:06:37 +00002779 inMutex++;
drh5c111232006-02-10 04:33:12 +00002780 pthread_mutex_unlock(&mutexAux);
2781#else
drhe9565a62006-02-11 02:03:52 +00002782 inMutex++;
drh5c111232006-02-10 04:33:12 +00002783#endif
drhbbd42a62004-05-22 17:41:58 +00002784}
drh66560ad2006-01-06 14:32:19 +00002785void sqlite3UnixLeaveMutex(){
drha3fad6f2006-01-18 14:06:37 +00002786 assert( inMutex>0 );
drhbbd42a62004-05-22 17:41:58 +00002787#ifdef SQLITE_UNIX_THREADS
drh5c111232006-02-10 04:33:12 +00002788 pthread_mutex_lock(&mutexAux);
drha3fad6f2006-01-18 14:06:37 +00002789 inMutex--;
drh5c111232006-02-10 04:33:12 +00002790 assert( pthread_equal(mutexOwner, pthread_self()) );
drha3fad6f2006-01-18 14:06:37 +00002791 if( inMutex==0 ){
drh5c111232006-02-10 04:33:12 +00002792 assert( mutexOwnerValid );
2793 mutexOwnerValid = 0;
2794 pthread_mutex_unlock(&mutexMain);
drha3fad6f2006-01-18 14:06:37 +00002795 }
drh5c111232006-02-10 04:33:12 +00002796 pthread_mutex_unlock(&mutexAux);
drha3fad6f2006-01-18 14:06:37 +00002797#else
2798 inMutex--;
drhbbd42a62004-05-22 17:41:58 +00002799#endif
2800}
2801
2802/*
drh757b04e2006-01-18 17:25:45 +00002803** Return TRUE if the mutex is currently held.
2804**
drh5c111232006-02-10 04:33:12 +00002805** If the thisThrd parameter is true, return true only if the
drh757b04e2006-01-18 17:25:45 +00002806** calling thread holds the mutex. If the parameter is false, return
2807** true if any thread holds the mutex.
drh88f474a2006-01-02 20:00:12 +00002808*/
drh5c111232006-02-10 04:33:12 +00002809int sqlite3UnixInMutex(int thisThrd){
drha3fad6f2006-01-18 14:06:37 +00002810#ifdef SQLITE_UNIX_THREADS
drh5c111232006-02-10 04:33:12 +00002811 int rc;
2812 pthread_mutex_lock(&mutexAux);
2813 rc = inMutex>0 && (thisThrd==0 || pthread_equal(mutexOwner,pthread_self()));
2814 pthread_mutex_unlock(&mutexAux);
2815 return rc;
drha3fad6f2006-01-18 14:06:37 +00002816#else
drh757b04e2006-01-18 17:25:45 +00002817 return inMutex>0;
drha3fad6f2006-01-18 14:06:37 +00002818#endif
drh88f474a2006-01-02 20:00:12 +00002819}
2820
2821/*
drhb4bc7052006-01-11 23:40:33 +00002822** Remember the number of thread-specific-data blocks allocated.
2823** Use this to verify that we are not leaking thread-specific-data.
2824** Ticket #1601
2825*/
2826#ifdef SQLITE_TEST
2827int sqlite3_tsd_count = 0;
2828# ifdef SQLITE_UNIX_THREADS
2829 static pthread_mutex_t tsd_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
2830# define TSD_COUNTER(N) \
2831 pthread_mutex_lock(&tsd_counter_mutex); \
2832 sqlite3_tsd_count += N; \
2833 pthread_mutex_unlock(&tsd_counter_mutex);
2834# else
2835# define TSD_COUNTER(N) sqlite3_tsd_count += N
2836# endif
2837#else
2838# define TSD_COUNTER(N) /* no-op */
2839#endif
2840
drhb4bc7052006-01-11 23:40:33 +00002841/*
drhf1a221e2006-01-15 17:27:17 +00002842** If called with allocateFlag>0, then return a pointer to thread
drh6f7adc82006-01-11 21:41:20 +00002843** specific data for the current thread. Allocate and zero the
drhf1a221e2006-01-15 17:27:17 +00002844** thread-specific data if it does not already exist.
danielk197713a68c32005-12-15 10:11:30 +00002845**
drh6f7adc82006-01-11 21:41:20 +00002846** If called with allocateFlag==0, then check the current thread
drh70ff98a2006-01-12 01:25:18 +00002847** specific data. Return it if it exists. If it does not exist,
2848** then return NULL.
2849**
2850** If called with allocateFlag<0, check to see if the thread specific
2851** data is allocated and is all zero. If it is then deallocate it.
drh6f7adc82006-01-11 21:41:20 +00002852** Return a pointer to the thread specific data or NULL if it is
drh70ff98a2006-01-12 01:25:18 +00002853** unallocated or gets deallocated.
danielk197713a68c32005-12-15 10:11:30 +00002854*/
drh6f7adc82006-01-11 21:41:20 +00002855ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){
danielk19774d5238f2006-01-27 06:32:00 +00002856 static const ThreadData zeroData = {0}; /* Initializer to silence warnings
2857 ** from broken compilers */
danielk197713a68c32005-12-15 10:11:30 +00002858#ifdef SQLITE_UNIX_THREADS
2859 static pthread_key_t key;
2860 static int keyInit = 0;
drh6f7adc82006-01-11 21:41:20 +00002861 ThreadData *pTsd;
danielk197713a68c32005-12-15 10:11:30 +00002862
2863 if( !keyInit ){
drh66560ad2006-01-06 14:32:19 +00002864 sqlite3OsEnterMutex();
danielk197713a68c32005-12-15 10:11:30 +00002865 if( !keyInit ){
2866 int rc;
drh6f7adc82006-01-11 21:41:20 +00002867 rc = pthread_key_create(&key, 0);
danielk197713a68c32005-12-15 10:11:30 +00002868 if( rc ){
drh8c0ca7d2006-01-07 04:06:54 +00002869 sqlite3OsLeaveMutex();
danielk197713a68c32005-12-15 10:11:30 +00002870 return 0;
2871 }
2872 keyInit = 1;
2873 }
drh66560ad2006-01-06 14:32:19 +00002874 sqlite3OsLeaveMutex();
danielk197713a68c32005-12-15 10:11:30 +00002875 }
2876
drh3fbb0b12006-01-06 00:36:00 +00002877 pTsd = pthread_getspecific(key);
drh70ff98a2006-01-12 01:25:18 +00002878 if( allocateFlag>0 ){
drh6f7adc82006-01-11 21:41:20 +00002879 if( pTsd==0 ){
danielk197776e8d1a2006-01-18 18:22:43 +00002880 if( !sqlite3TestMallocFail() ){
2881 pTsd = sqlite3OsMalloc(sizeof(zeroData));
2882 }
2883#ifdef SQLITE_MEMDEBUG
2884 sqlite3_isFail = 0;
2885#endif
drh6f7adc82006-01-11 21:41:20 +00002886 if( pTsd ){
2887 *pTsd = zeroData;
2888 pthread_setspecific(key, pTsd);
drhb4bc7052006-01-11 23:40:33 +00002889 TSD_COUNTER(+1);
drh6f7adc82006-01-11 21:41:20 +00002890 }
danielk197713a68c32005-12-15 10:11:30 +00002891 }
drh70ff98a2006-01-12 01:25:18 +00002892 }else if( pTsd!=0 && allocateFlag<0
danielk19779e128002006-01-18 16:51:35 +00002893 && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
drh6f7adc82006-01-11 21:41:20 +00002894 sqlite3OsFree(pTsd);
2895 pthread_setspecific(key, 0);
drhb4bc7052006-01-11 23:40:33 +00002896 TSD_COUNTER(-1);
drh6f7adc82006-01-11 21:41:20 +00002897 pTsd = 0;
danielk197713a68c32005-12-15 10:11:30 +00002898 }
2899 return pTsd;
2900#else
drh6f7adc82006-01-11 21:41:20 +00002901 static ThreadData *pTsd = 0;
drh70ff98a2006-01-12 01:25:18 +00002902 if( allocateFlag>0 ){
drh6f7adc82006-01-11 21:41:20 +00002903 if( pTsd==0 ){
danielk197776e8d1a2006-01-18 18:22:43 +00002904 if( !sqlite3TestMallocFail() ){
2905 pTsd = sqlite3OsMalloc( sizeof(zeroData) );
2906 }
2907#ifdef SQLITE_MEMDEBUG
2908 sqlite3_isFail = 0;
2909#endif
drh6f7adc82006-01-11 21:41:20 +00002910 if( pTsd ){
2911 *pTsd = zeroData;
drhb4bc7052006-01-11 23:40:33 +00002912 TSD_COUNTER(+1);
drh6f7adc82006-01-11 21:41:20 +00002913 }
drh3fbb0b12006-01-06 00:36:00 +00002914 }
drh70ff98a2006-01-12 01:25:18 +00002915 }else if( pTsd!=0 && allocateFlag<0
danielk19779e128002006-01-18 16:51:35 +00002916 && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
drh6f7adc82006-01-11 21:41:20 +00002917 sqlite3OsFree(pTsd);
drhb4bc7052006-01-11 23:40:33 +00002918 TSD_COUNTER(-1);
drh6f7adc82006-01-11 21:41:20 +00002919 pTsd = 0;
danielk197713a68c32005-12-15 10:11:30 +00002920 }
drh3fbb0b12006-01-06 00:36:00 +00002921 return pTsd;
danielk197713a68c32005-12-15 10:11:30 +00002922#endif
2923}
2924
2925/*
drhbbd42a62004-05-22 17:41:58 +00002926** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00002927** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00002928*/
2929#ifdef SQLITE_TEST
2930int sqlite3_current_time = 0;
2931#endif
2932
2933/*
2934** Find the current time (in Universal Coordinated Time). Write the
2935** current time and date as a Julian Day number into *prNow and
2936** return 0. Return 1 if the time and date cannot be found.
2937*/
drh66560ad2006-01-06 14:32:19 +00002938int sqlite3UnixCurrentTime(double *prNow){
drh19e2d372005-08-29 23:00:03 +00002939#ifdef NO_GETTOD
drhbbd42a62004-05-22 17:41:58 +00002940 time_t t;
2941 time(&t);
2942 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00002943#else
2944 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00002945 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00002946 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
2947#endif
drhbbd42a62004-05-22 17:41:58 +00002948#ifdef SQLITE_TEST
2949 if( sqlite3_current_time ){
2950 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
2951 }
2952#endif
2953 return 0;
2954}
2955
drhbbd42a62004-05-22 17:41:58 +00002956#endif /* OS_UNIX */