blob: 9f538e49589cc5c97e058f45b4782bfc696d6c73 [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.
danielk1977822a5162008-05-16 04:51:54 +000014**
danielk1977397d65f2008-11-19 11:35:39 +000015** $Id: os_unix.c,v 1.213 2008/11/19 11:35:40 danielk1977 Exp $
drhbbd42a62004-05-22 17:41:58 +000016*/
drhbbd42a62004-05-22 17:41:58 +000017#include "sqliteInt.h"
danielk197729bafea2008-06-26 10:41:19 +000018#if SQLITE_OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000019
danielk1977e339d652008-06-28 11:23:00 +000020/*
drh40bbb0a2008-09-23 10:23:26 +000021** If SQLITE_ENABLE_LOCKING_STYLE is defined and is non-zero, then several
22** alternative locking implementations are provided:
danielk1977e339d652008-06-28 11:23:00 +000023**
24** * POSIX locking (the default),
25** * No locking,
26** * Dot-file locking,
27** * flock() locking,
chw97185482008-11-17 08:05:31 +000028** * AFP locking (OSX only),
29** * Named POSIX semaphores (VXWorks only).
drh40bbb0a2008-09-23 10:23:26 +000030**
31** SQLITE_ENABLE_LOCKING_STYLE only works on a Mac. It is turned on by
32** default on a Mac and disabled on all other posix platforms.
danielk1977e339d652008-06-28 11:23:00 +000033*/
drh40bbb0a2008-09-23 10:23:26 +000034#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
35# if defined(__DARWIN__)
36# define SQLITE_ENABLE_LOCKING_STYLE 1
37# else
38# define SQLITE_ENABLE_LOCKING_STYLE 0
39# endif
40#endif
drhbfe66312006-10-03 17:40:40 +000041
drh9cbe6352005-11-29 03:13:21 +000042/*
danielk1977397d65f2008-11-19 11:35:39 +000043** Define the IS_VXWORKS pre-processor macro to 1 if building on
44** vxworks, or 0 otherwise.
45*/
46#if defined(__RTP__) || defined(_WRS_KERNEL)
47# define IS_VXWORKS 1
48#else
49# define IS_VXWORKS 0
50#endif
51
52/*
drh9cbe6352005-11-29 03:13:21 +000053** These #defines should enable >2GB file support on Posix if the
54** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000055** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000056**
57** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
58** on the compiler command line. This is necessary if you are compiling
59** on a recent machine (ex: RedHat 7.2) but you want your code to work
60** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
61** without this option, LFS is enable. But LFS does not exist in the kernel
62** in RedHat 6.0, so the code won't work. Hence, for maximum binary
63** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000064*/
65#ifndef SQLITE_DISABLE_LFS
66# define _LARGE_FILE 1
67# ifndef _FILE_OFFSET_BITS
68# define _FILE_OFFSET_BITS 64
69# endif
70# define _LARGEFILE_SOURCE 1
71#endif
drhbbd42a62004-05-22 17:41:58 +000072
drh9cbe6352005-11-29 03:13:21 +000073/*
74** standard include files.
75*/
76#include <sys/types.h>
77#include <sys/stat.h>
78#include <fcntl.h>
79#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000080#include <time.h>
drh19e2d372005-08-29 23:00:03 +000081#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000082#include <errno.h>
danielk1977e339d652008-06-28 11:23:00 +000083
drh40bbb0a2008-09-23 10:23:26 +000084#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +000085#include <sys/ioctl.h>
chw97185482008-11-17 08:05:31 +000086#if defined(__RTP__) || defined(_WRS_KERNEL)
87#define lstat stat
88#include <semaphore.h>
89#include <limits.h>
90#else
drhbfe66312006-10-03 17:40:40 +000091#include <sys/param.h>
92#include <sys/mount.h>
chw97185482008-11-17 08:05:31 +000093#endif
drhbfe66312006-10-03 17:40:40 +000094#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000095
96/*
drhf1a221e2006-01-15 17:27:17 +000097** If we are to be thread-safe, include the pthreads header and define
98** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +000099*/
drhd677b3d2007-08-20 22:48:41 +0000100#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000101# include <pthread.h>
102# define SQLITE_UNIX_THREADS 1
103#endif
104
105/*
106** Default permissions when creating a new file
107*/
108#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
109# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
110#endif
111
danielk1977b4b47412007-08-17 15:53:36 +0000112/*
113** Maximum supported path-length.
114*/
115#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +0000116
117
118/*
danielk1977ad94b582007-08-20 06:44:22 +0000119** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +0000120** protability layer.
drh9cbe6352005-11-29 03:13:21 +0000121*/
drh054889e2005-11-30 03:20:31 +0000122typedef struct unixFile unixFile;
123struct unixFile {
danielk197762079062007-08-15 17:08:46 +0000124 sqlite3_io_methods const *pMethod; /* Always the first entry */
danielk1977967a4a12007-08-20 14:23:44 +0000125#ifdef SQLITE_TEST
126 /* In test mode, increase the size of this structure a bit so that
127 ** it is larger than the struct CrashFile defined in test6.c.
128 */
129 char aPadding[32];
130#endif
drh9cbe6352005-11-29 03:13:21 +0000131 struct openCnt *pOpen; /* Info about all open fd's on this inode */
132 struct lockInfo *pLock; /* Info about locks on this inode */
drh40bbb0a2008-09-23 10:23:26 +0000133#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +0000134 void *lockingContext; /* Locking style specific state */
danielk1977e339d652008-06-28 11:23:00 +0000135#endif
drh9cbe6352005-11-29 03:13:21 +0000136 int h; /* The file descriptor */
137 unsigned char locktype; /* The type of lock held on this fd */
drh9cbe6352005-11-29 03:13:21 +0000138 int dirfd; /* File descriptor for the directory */
drhd677b3d2007-08-20 22:48:41 +0000139#if SQLITE_THREADSAFE
danielk1977ad94b582007-08-20 06:44:22 +0000140 pthread_t tid; /* The thread that "owns" this unixFile */
drh9cbe6352005-11-29 03:13:21 +0000141#endif
aswift5b1a2562008-08-22 00:22:35 +0000142 int lastErrno; /* The unix errno from the last I/O error */
chw97185482008-11-17 08:05:31 +0000143#if defined(__RTP__) || defined(_WRS_KERNEL)
144 int isDelete; /* Delete on close if true */
145 char *zRealpath;
146#endif
drh9cbe6352005-11-29 03:13:21 +0000147};
148
drh0ccebe72005-06-07 22:22:50 +0000149/*
drh198bf392006-01-06 21:52:49 +0000150** Include code that is common to all os_*.c files
151*/
152#include "os_common.h"
153
154/*
drh0ccebe72005-06-07 22:22:50 +0000155** Define various macros that are missing from some systems.
156*/
drhbbd42a62004-05-22 17:41:58 +0000157#ifndef O_LARGEFILE
158# define O_LARGEFILE 0
159#endif
160#ifdef SQLITE_DISABLE_LFS
161# undef O_LARGEFILE
162# define O_LARGEFILE 0
163#endif
164#ifndef O_NOFOLLOW
165# define O_NOFOLLOW 0
166#endif
167#ifndef O_BINARY
168# define O_BINARY 0
169#endif
170
171/*
172** The DJGPP compiler environment looks mostly like Unix, but it
173** lacks the fcntl() system call. So redefine fcntl() to be something
174** that always succeeds. This means that locking does not occur under
drh85b623f2007-12-13 21:54:09 +0000175** DJGPP. But it is DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000176*/
177#ifdef __DJGPP__
178# define fcntl(A,B,C) 0
179#endif
180
181/*
drh2b4b5962005-06-15 17:47:55 +0000182** The threadid macro resolves to the thread-id or to 0. Used for
183** testing and debugging only.
184*/
drhd677b3d2007-08-20 22:48:41 +0000185#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000186#define threadid pthread_self()
187#else
188#define threadid 0
189#endif
190
191/*
danielk1977ad94b582007-08-20 06:44:22 +0000192** Set or check the unixFile.tid field. This field is set when an unixFile
193** is first opened. All subsequent uses of the unixFile verify that the
194** same thread is operating on the unixFile. Some operating systems do
drh2b4b5962005-06-15 17:47:55 +0000195** not allow locks to be overridden by other threads and that restriction
196** means that sqlite3* database handles cannot be moved from one thread
197** to another. This logic makes sure a user does not try to do that
198** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000199**
danielk1977ad94b582007-08-20 06:44:22 +0000200** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
drhf1a221e2006-01-15 17:27:17 +0000201** another as long as we are running on a system that supports threads
202** overriding each others locks (which now the most common behavior)
danielk1977ad94b582007-08-20 06:44:22 +0000203** or if no locks are held. But the unixFile.pLock field needs to be
drhf1a221e2006-01-15 17:27:17 +0000204** recomputed because its key includes the thread-id. See the
205** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000206*/
drhd677b3d2007-08-20 22:48:41 +0000207#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000208# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000209# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
210 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000211#else
212# define SET_THREADID(X)
213# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000214#endif
215
drhbbd42a62004-05-22 17:41:58 +0000216/*
217** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
218** section 6.5.2.2 lines 483 through 490 specify that when a process
219** sets or clears a lock, that operation overrides any prior locks set
220** by the same process. It does not explicitly say so, but this implies
221** that it overrides locks set by the same process using a different
222** file descriptor. Consider this test case:
drhbbd42a62004-05-22 17:41:58 +0000223** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
224**
225** Suppose ./file1 and ./file2 are really the same file (because
226** one is a hard or symbolic link to the other) then if you set
227** an exclusive lock on fd1, then try to get an exclusive lock
228** on fd2, it works. I would have expected the second lock to
229** fail since there was already a lock on the file due to fd1.
230** But not so. Since both locks came from the same process, the
231** second overrides the first, even though they were on different
232** file descriptors opened on different file names.
233**
234** Bummer. If you ask me, this is broken. Badly broken. It means
235** that we cannot use POSIX locks to synchronize file access among
236** competing threads of the same process. POSIX locks will work fine
237** to synchronize access for threads in separate processes, but not
238** threads within the same process.
239**
240** To work around the problem, SQLite has to manage file locks internally
241** on its own. Whenever a new database is opened, we have to find the
242** specific inode of the database file (the inode is determined by the
243** st_dev and st_ino fields of the stat structure that fstat() fills in)
244** and check for locks already existing on that inode. When locks are
245** created or removed, we have to look at our own internal record of the
246** locks to see if another thread has previously set a lock on that same
247** inode.
248**
danielk1977ad94b582007-08-20 06:44:22 +0000249** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000250** descriptor. It is now a structure that holds the integer file
251** descriptor and a pointer to a structure that describes the internal
252** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000253** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000254** point to the same locking structure. The locking structure keeps
255** a reference count (so we will know when to delete it) and a "cnt"
256** field that tells us its internal lock status. cnt==0 means the
257** file is unlocked. cnt==-1 means the file has an exclusive lock.
258** cnt>0 means there are cnt shared locks on the file.
259**
260** Any attempt to lock or unlock a file first checks the locking
261** structure. The fcntl() system call is only invoked to set a
262** POSIX lock if the internal lock structure transitions between
263** a locked and an unlocked state.
264**
265** 2004-Jan-11:
266** More recent discoveries about POSIX advisory locks. (The more
267** I discover, the more I realize the a POSIX advisory locks are
268** an abomination.)
269**
270** If you close a file descriptor that points to a file that has locks,
271** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000272** released. To work around this problem, each unixFile structure contains
drhbbd42a62004-05-22 17:41:58 +0000273** a pointer to an openCnt structure. There is one openCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000274** per open inode, which means that multiple unixFile can point to a single
275** openCnt. When an attempt is made to close an unixFile, if there are
276** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000277** to close() the file descriptor is deferred until all of the locks clear.
278** The openCnt structure keeps a list of file descriptors that need to
279** be closed and that list is walked (and cleared) when the last lock
280** clears.
281**
282** First, under Linux threads, because each thread has a separate
283** process ID, lock operations in one thread do not override locks
284** to the same file in other threads. Linux threads behave like
285** separate processes in this respect. But, if you close a file
286** descriptor in linux threads, all locks are cleared, even locks
287** on other threads and even though the other threads have different
288** process IDs. Linux threads is inconsistent in this respect.
289** (I'm beginning to think that linux threads is an abomination too.)
290** The consequence of this all is that the hash table for the lockInfo
291** structure has to include the process id as part of its key because
292** locks in different threads are treated as distinct. But the
293** openCnt structure should not include the process id in its
294** key because close() clears lock on all threads, not just the current
295** thread. Were it not for this goofiness in linux threads, we could
296** combine the lockInfo and openCnt structures into a single structure.
drh5fdae772004-06-29 03:29:00 +0000297**
298** 2004-Jun-28:
299** On some versions of linux, threads can override each others locks.
300** On others not. Sometimes you can change the behavior on the same
301** system by setting the LD_ASSUME_KERNEL environment variable. The
302** POSIX standard is silent as to which behavior is correct, as far
303** as I can tell, so other versions of unix might show the same
304** inconsistency. There is no little doubt in my mind that posix
305** advisory locks and linux threads are profoundly broken.
306**
307** To work around the inconsistencies, we have to test at runtime
308** whether or not threads can override each others locks. This test
309** is run once, the first time any lock is attempted. A static
310** variable is set to record the results of this test for future
311** use.
drhbbd42a62004-05-22 17:41:58 +0000312*/
313
314/*
315** An instance of the following structure serves as the key used
drh5fdae772004-06-29 03:29:00 +0000316** to locate a particular lockInfo structure given its inode.
317**
318** If threads cannot override each others locks, then we set the
319** lockKey.tid field to the thread ID. If threads can override
drhf1a221e2006-01-15 17:27:17 +0000320** each others locks then tid is always set to zero. tid is omitted
321** if we compile without threading support.
drhbbd42a62004-05-22 17:41:58 +0000322*/
323struct lockKey {
drh5fdae772004-06-29 03:29:00 +0000324 dev_t dev; /* Device number */
chw97185482008-11-17 08:05:31 +0000325#if defined(__RTP__) || defined(_WRS_KERNEL)
326 void *rnam; /* Realname since inode unusable */
327#else
drh5fdae772004-06-29 03:29:00 +0000328 ino_t ino; /* Inode number */
chw97185482008-11-17 08:05:31 +0000329#endif
drhd677b3d2007-08-20 22:48:41 +0000330#if SQLITE_THREADSAFE
drhd9cb6ac2005-10-20 07:28:17 +0000331 pthread_t tid; /* Thread ID or zero if threads can override each other */
drh5fdae772004-06-29 03:29:00 +0000332#endif
drhbbd42a62004-05-22 17:41:58 +0000333};
334
335/*
336** An instance of the following structure is allocated for each open
337** inode on each thread with a different process ID. (Threads have
338** different process IDs on linux, but not on most other unixes.)
339**
danielk1977ad94b582007-08-20 06:44:22 +0000340** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000341** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000342** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000343*/
344struct lockInfo {
345 struct lockKey key; /* The lookup key */
drh2ac3ee92004-06-07 16:27:46 +0000346 int cnt; /* Number of SHARED locks held */
danielk19779a1d0ab2004-06-01 14:09:28 +0000347 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
drhbbd42a62004-05-22 17:41:58 +0000348 int nRef; /* Number of pointers to this structure */
drhda0e7682008-07-30 15:27:54 +0000349 struct lockInfo *pNext, *pPrev; /* List of all lockInfo objects */
drhbbd42a62004-05-22 17:41:58 +0000350};
351
352/*
353** An instance of the following structure serves as the key used
354** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000355** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000356*/
357struct openKey {
358 dev_t dev; /* Device number */
chw97185482008-11-17 08:05:31 +0000359#if defined(__RTP__) || defined(_WRS_KERNEL)
360 void *rnam; /* Realname since inode unusable */
361#else
drhbbd42a62004-05-22 17:41:58 +0000362 ino_t ino; /* Inode number */
chw97185482008-11-17 08:05:31 +0000363#endif
drhbbd42a62004-05-22 17:41:58 +0000364};
365
366/*
367** An instance of the following structure is allocated for each open
368** inode. This structure keeps track of the number of locks on that
369** inode. If a close is attempted against an inode that is holding
370** locks, the close is deferred until all locks clear by adding the
371** file descriptor to be closed to the pending list.
372*/
373struct openCnt {
374 struct openKey key; /* The lookup key */
375 int nRef; /* Number of pointers to this structure */
376 int nLock; /* Number of outstanding locks */
377 int nPending; /* Number of pending close() operations */
378 int *aPending; /* Malloced space holding fd's awaiting a close() */
chw97185482008-11-17 08:05:31 +0000379#if defined(__RTP__) || defined(_WRS_KERNEL)
380 sem_t *pSem; /* Named POSIX semaphore */
381 char aSemName[MAX_PATHNAME+1]; /* Name of that semaphore */
382#endif
drhda0e7682008-07-30 15:27:54 +0000383 struct openCnt *pNext, *pPrev; /* List of all openCnt objects */
drhbbd42a62004-05-22 17:41:58 +0000384};
385
drhda0e7682008-07-30 15:27:54 +0000386/*
387** List of all lockInfo and openCnt objects. This used to be a hash
388** table. But the number of objects is rarely more than a dozen and
389** never exceeds a few thousand. And lookup is not on a critical
390** path oo a simple linked list will suffice.
drhbbd42a62004-05-22 17:41:58 +0000391*/
drhda0e7682008-07-30 15:27:54 +0000392static struct lockInfo *lockList = 0;
393static struct openCnt *openList = 0;
drh5fdae772004-06-29 03:29:00 +0000394
chw97185482008-11-17 08:05:31 +0000395#if defined(__RTP__) || defined(_WRS_KERNEL)
396/*
397** This hash table is used to bind the canonical file name to a
398** unixFile structure and use the hash key (= canonical name)
399** instead of the Inode number of the file to find the matching
400** lockInfo and openCnt structures. It also helps to make the
401** name of the semaphore when LOCKING_STYLE_NAMEDSEM is used
402** for the file.
403*/
404static Hash nameHash;
405#endif
406
drhbfe66312006-10-03 17:40:40 +0000407/*
408** The locking styles are associated with the different file locking
409** capabilities supported by different file systems.
410**
411** POSIX locking style fully supports shared and exclusive byte-range locks
danielk1977e339d652008-06-28 11:23:00 +0000412** AFP locking only supports exclusive byte-range locks
drhbfe66312006-10-03 17:40:40 +0000413** FLOCK only supports a single file-global exclusive lock
414** DOTLOCK isn't a true locking style, it refers to the use of a special
415** file named the same as the database file with a '.lock' extension, this
416** can be used on file systems that do not offer any reliable file locking
417** NO locking means that no locking will be attempted, this is only used for
418** read-only file systems currently
chw97185482008-11-17 08:05:31 +0000419** NAMEDSEM is similar to DOTLOCK but uses a named semaphore instead of an
420** indicator file.
drhbfe66312006-10-03 17:40:40 +0000421** UNSUPPORTED means that no locking will be attempted, this is only used for
422** file systems that are known to be unsupported
423*/
danielk1977e339d652008-06-28 11:23:00 +0000424#define LOCKING_STYLE_POSIX 1
drhda0e7682008-07-30 15:27:54 +0000425#define LOCKING_STYLE_NONE 2
danielk1977e339d652008-06-28 11:23:00 +0000426#define LOCKING_STYLE_DOTFILE 3
drhda0e7682008-07-30 15:27:54 +0000427#define LOCKING_STYLE_FLOCK 4
danielk1977e339d652008-06-28 11:23:00 +0000428#define LOCKING_STYLE_AFP 5
chw97185482008-11-17 08:05:31 +0000429#define LOCKING_STYLE_NAMEDSEM 6
drhbfe66312006-10-03 17:40:40 +0000430
danielk1977ad94b582007-08-20 06:44:22 +0000431/*
aswift5b1a2562008-08-22 00:22:35 +0000432** Only set the lastErrno if the error code is a real error and not
433** a normal expected return code of SQLITE_BUSY or SQLITE_OK
434*/
435#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
436
437/*
danielk1977ad94b582007-08-20 06:44:22 +0000438** Helper functions to obtain and relinquish the global mutex.
439*/
danielk19770afae932008-09-24 09:12:46 +0000440static void enterMutex(void){
danielk197759f8c082008-06-18 17:09:10 +0000441 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000442}
danielk19770afae932008-09-24 09:12:46 +0000443static void leaveMutex(void){
danielk197759f8c082008-06-18 17:09:10 +0000444 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000445}
446
drhd677b3d2007-08-20 22:48:41 +0000447#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000448/*
449** This variable records whether or not threads can override each others
450** locks.
451**
452** 0: No. Threads cannot override each others locks.
453** 1: Yes. Threads can override each others locks.
454** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000455**
drh5062d3a2006-01-31 23:03:35 +0000456** On some systems, we know at compile-time if threads can override each
457** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
458** will be set appropriately. On other systems, we have to check at
459** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
460** undefined.
461**
drhf1a221e2006-01-15 17:27:17 +0000462** This variable normally has file scope only. But during testing, we make
463** it a global so that the test code can change its value in order to verify
464** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000465*/
drh5062d3a2006-01-31 23:03:35 +0000466#ifndef SQLITE_THREAD_OVERRIDE_LOCK
467# define SQLITE_THREAD_OVERRIDE_LOCK -1
468#endif
drh029b44b2006-01-15 00:13:15 +0000469#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000470int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000471#else
drh5062d3a2006-01-31 23:03:35 +0000472static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000473#endif
drh5fdae772004-06-29 03:29:00 +0000474
475/*
476** This structure holds information passed into individual test
477** threads by the testThreadLockingBehavior() routine.
478*/
479struct threadTestData {
480 int fd; /* File to be locked */
481 struct flock lock; /* The locking operation */
482 int result; /* Result of the locking operation */
483};
484
drh2b4b5962005-06-15 17:47:55 +0000485#ifdef SQLITE_LOCK_TRACE
486/*
487** Print out information about all locking operations.
488**
489** This routine is used for troubleshooting locks on multithreaded
490** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
491** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000492** turned off.
drh2b4b5962005-06-15 17:47:55 +0000493*/
494static int lockTrace(int fd, int op, struct flock *p){
495 char *zOpName, *zType;
496 int s;
497 int savedErrno;
498 if( op==F_GETLK ){
499 zOpName = "GETLK";
500 }else if( op==F_SETLK ){
501 zOpName = "SETLK";
502 }else{
503 s = fcntl(fd, op, p);
504 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
505 return s;
506 }
507 if( p->l_type==F_RDLCK ){
508 zType = "RDLCK";
509 }else if( p->l_type==F_WRLCK ){
510 zType = "WRLCK";
511 }else if( p->l_type==F_UNLCK ){
512 zType = "UNLCK";
513 }else{
514 assert( 0 );
515 }
516 assert( p->l_whence==SEEK_SET );
517 s = fcntl(fd, op, p);
518 savedErrno = errno;
519 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
520 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
521 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000522 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000523 struct flock l2;
524 l2 = *p;
525 fcntl(fd, F_GETLK, &l2);
526 if( l2.l_type==F_RDLCK ){
527 zType = "RDLCK";
528 }else if( l2.l_type==F_WRLCK ){
529 zType = "WRLCK";
530 }else if( l2.l_type==F_UNLCK ){
531 zType = "UNLCK";
532 }else{
533 assert( 0 );
534 }
535 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
536 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
537 }
538 errno = savedErrno;
539 return s;
540}
541#define fcntl lockTrace
542#endif /* SQLITE_LOCK_TRACE */
543
danielk197741a6a612008-11-11 18:34:35 +0000544#ifdef __linux__
drh5fdae772004-06-29 03:29:00 +0000545/*
danielk197741a6a612008-11-11 18:34:35 +0000546** This function is used as the main routine for a thread launched by
547** testThreadLockingBehavior(). It tests whether the shared-lock obtained
548** by the main thread in testThreadLockingBehavior() conflicts with a
549** hypothetical write-lock obtained by this thread on the same file.
550**
551** The write-lock is not actually acquired, as this is not possible if
552** the file is open in read-only mode (see ticket #3472).
553*/
drh5fdae772004-06-29 03:29:00 +0000554static void *threadLockingTest(void *pArg){
555 struct threadTestData *pData = (struct threadTestData*)pArg;
danielk197741a6a612008-11-11 18:34:35 +0000556 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
drh5fdae772004-06-29 03:29:00 +0000557 return pArg;
558}
559
560/*
561** This procedure attempts to determine whether or not threads
562** can override each others locks then sets the
563** threadsOverrideEachOthersLocks variable appropriately.
564*/
danielk19774d5238f2006-01-27 06:32:00 +0000565static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000566 int fd;
danielk197741a6a612008-11-11 18:34:35 +0000567 int rc;
568 struct threadTestData d;
569 struct flock l;
570 pthread_t t;
drh5fdae772004-06-29 03:29:00 +0000571
572 fd = dup(fd_orig);
573 if( fd<0 ) return;
danielk197741a6a612008-11-11 18:34:35 +0000574 memset(&l, 0, sizeof(l));
575 l.l_type = F_RDLCK;
576 l.l_len = 1;
577 l.l_start = 0;
578 l.l_whence = SEEK_SET;
579 rc = fcntl(fd_orig, F_SETLK, &l);
580 if( rc!=0 ) return;
581 memset(&d, 0, sizeof(d));
582 d.fd = fd;
583 d.lock = l;
584 d.lock.l_type = F_WRLCK;
585 pthread_create(&t, 0, threadLockingTest, &d);
586 pthread_join(t, 0);
drh5fdae772004-06-29 03:29:00 +0000587 close(fd);
danielk197741a6a612008-11-11 18:34:35 +0000588 if( d.result!=0 ) return;
589 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
drh5fdae772004-06-29 03:29:00 +0000590}
danielk197741a6a612008-11-11 18:34:35 +0000591#else
592/*
593** On anything other than linux, assume threads override each others locks.
594*/
595static void testThreadLockingBehavior(int fd_orig){
596 threadsOverrideEachOthersLocks = 1;
597}
598#endif /* __linux__ */
599
drhd677b3d2007-08-20 22:48:41 +0000600#endif /* SQLITE_THREADSAFE */
drh5fdae772004-06-29 03:29:00 +0000601
drhbbd42a62004-05-22 17:41:58 +0000602/*
603** Release a lockInfo structure previously allocated by findLockInfo().
604*/
605static void releaseLockInfo(struct lockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000606 if( pLock ){
607 pLock->nRef--;
608 if( pLock->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000609 if( pLock->pPrev ){
610 assert( pLock->pPrev->pNext==pLock );
611 pLock->pPrev->pNext = pLock->pNext;
612 }else{
613 assert( lockList==pLock );
614 lockList = pLock->pNext;
615 }
616 if( pLock->pNext ){
617 assert( pLock->pNext->pPrev==pLock );
618 pLock->pNext->pPrev = pLock->pPrev;
619 }
danielk1977e339d652008-06-28 11:23:00 +0000620 sqlite3_free(pLock);
621 }
drhbbd42a62004-05-22 17:41:58 +0000622 }
623}
624
625/*
626** Release a openCnt structure previously allocated by findLockInfo().
627*/
628static void releaseOpenCnt(struct openCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000629 if( pOpen ){
630 pOpen->nRef--;
631 if( pOpen->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000632 if( pOpen->pPrev ){
633 assert( pOpen->pPrev->pNext==pOpen );
634 pOpen->pPrev->pNext = pOpen->pNext;
635 }else{
636 assert( openList==pOpen );
637 openList = pOpen->pNext;
638 }
639 if( pOpen->pNext ){
640 assert( pOpen->pNext->pPrev==pOpen );
641 pOpen->pNext->pPrev = pOpen->pPrev;
642 }
643 sqlite3_free(pOpen->aPending);
danielk1977e339d652008-06-28 11:23:00 +0000644 sqlite3_free(pOpen);
645 }
drhbbd42a62004-05-22 17:41:58 +0000646 }
647}
648
chw97185482008-11-17 08:05:31 +0000649#if defined(__RTP__) || defined(_WRS_KERNEL)
650/*
651** Implementation of a realpath() like function for vxWorks
652** to determine canonical path name from given name. It does
653** not support symlinks. Neither does it handle volume prefixes.
654*/
655char *
656vxrealpath(const char *pathname, int dostat)
657{
658 struct stat sbuf;
659 int len;
660 char *where, *ptr, *last;
661 char *result, *curpath, *workpath, *namebuf;
662
663 len = pathconf(pathname, _PC_PATH_MAX);
664 if( len<0 ){
665 len = PATH_MAX;
666 }
667 result = sqlite3_malloc(len * 4);
668 if( !result ){
669 return 0;
670 }
671 curpath = result + len;
672 workpath = curpath + len;
673 namebuf = workpath + len;
674 strcpy(curpath, pathname);
675 if( *pathname!='/' ){
676 if( !getcwd(workpath, len) ){
677 sqlite3_free(result);
678 return 0;
679 }
680 }else{
681 *workpath = '\0';
682 }
683 where = curpath;
684 while( *where ){
685 if( !strcmp(where, ".") ){
686 where++;
687 continue;
688 }
689 if( !strncmp(where, "./", 2) ){
690 where += 2;
691 continue;
692 }
693 if( !strncmp(where, "../", 3) ){
694 where += 3;
695 ptr = last = workpath;
696 while( *ptr ){
697 if( *ptr=='/' ){
698 last = ptr;
699 }
700 ptr++;
701 }
702 *last = '\0';
703 continue;
704 }
705 ptr = strchr(where, '/');
706 if( !ptr ){
707 ptr = where + strlen(where) - 1;
708 }else{
709 *ptr = '\0';
710 }
711 strcpy(namebuf, workpath);
712 for( last = namebuf; *last; last++ ){
713 continue;
714 }
715 if( *--last!='/' ){
716 strcat(namebuf, "/");
717 }
718 strcat(namebuf, where);
719 where = ++ptr;
720 if( dostat ){
721 if( stat(namebuf, &sbuf)==-1 ){
722 sqlite3_free(result);
723 return 0;
724 }
725 if( (sbuf.st_mode & S_IFDIR)==S_IFDIR ){
726 strcpy(workpath, namebuf);
727 continue;
728 }
729 if( *where ){
730 sqlite3_free(result);
731 return 0;
732 }
733 }
734 strcpy(workpath, namebuf);
735 }
736 strcpy(result, workpath);
737 return result;
738}
739#endif
740
drh40bbb0a2008-09-23 10:23:26 +0000741#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +0000742/*
743** Tests a byte-range locking query to see if byte range locks are
744** supported, if not we fall back to dotlockLockingStyle.
chw97185482008-11-17 08:05:31 +0000745** On vxWorks we fall back to namedsemLockingStyle.
drhbfe66312006-10-03 17:40:40 +0000746*/
danielk1977e339d652008-06-28 11:23:00 +0000747static int testLockingStyle(int fd){
drhbfe66312006-10-03 17:40:40 +0000748 struct flock lockInfo;
danielk1977e339d652008-06-28 11:23:00 +0000749
750 /* Test byte-range lock using fcntl(). If the call succeeds,
751 ** assume that the file-system supports POSIX style locks.
752 */
drhbfe66312006-10-03 17:40:40 +0000753 lockInfo.l_len = 1;
754 lockInfo.l_start = 0;
755 lockInfo.l_whence = SEEK_SET;
756 lockInfo.l_type = F_RDLCK;
danielk1977ad94b582007-08-20 06:44:22 +0000757 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
danielk1977e339d652008-06-28 11:23:00 +0000758 return LOCKING_STYLE_POSIX;
759 }
drhbfe66312006-10-03 17:40:40 +0000760
danielk1977e339d652008-06-28 11:23:00 +0000761 /* Testing for flock() can give false positives. So if if the above
762 ** test fails, then we fall back to using dot-file style locking.
chw97185482008-11-17 08:05:31 +0000763 */
764#if defined(__RTP__) || defined(_WRS_KERNEL)
765 return LOCKING_STYLE_NAMEDSEM;
766#else
danielk1977e339d652008-06-28 11:23:00 +0000767 return LOCKING_STYLE_DOTFILE;
chw97185482008-11-17 08:05:31 +0000768#endif
drhbfe66312006-10-03 17:40:40 +0000769}
drh93a960a2008-07-10 00:32:42 +0000770#endif
drhbfe66312006-10-03 17:40:40 +0000771
772/*
danielk1977e339d652008-06-28 11:23:00 +0000773** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
774** f_fstypename entry in the statfs structure as returned by stat() for
775** the file system hosting the database file and selects the appropriate
776** locking style based on its value. These values and assignments are
777** based on Darwin/OSX behavior and have not been thoroughly tested on
drhbfe66312006-10-03 17:40:40 +0000778** other systems.
danielk1977e339d652008-06-28 11:23:00 +0000779**
780** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
781** returns LOCKING_STYLE_POSIX.
drhbfe66312006-10-03 17:40:40 +0000782*/
danielk197762c14b32008-11-19 09:05:26 +0000783#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +0000784static int detectLockingStyle(
785 sqlite3_vfs *pVfs,
danielk1977ad94b582007-08-20 06:44:22 +0000786 const char *filePath,
787 int fd
788){
chw97185482008-11-17 08:05:31 +0000789#if defined(__RTP__) || defined(_WRS_KERNEL)
790 if( !filePath ){
791 return LOCKING_STYLE_NONE;
792 }
793 if( pVfs->pAppData ){
794 return SQLITE_PTR_TO_INT(pVfs->pAppData);
795 }
796 if (access(filePath, 0) != -1){
797 return testLockingStyle(fd);
798 }
799#else
danielk1977e339d652008-06-28 11:23:00 +0000800 struct Mapping {
801 const char *zFilesystem;
802 int eLockingStyle;
803 } aMap[] = {
804 { "hfs", LOCKING_STYLE_POSIX },
805 { "ufs", LOCKING_STYLE_POSIX },
806 { "afpfs", LOCKING_STYLE_AFP },
aswift5b1a2562008-08-22 00:22:35 +0000807#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
808 { "smbfs", LOCKING_STYLE_AFP },
809#else
danielk1977e339d652008-06-28 11:23:00 +0000810 { "smbfs", LOCKING_STYLE_FLOCK },
aswift5b1a2562008-08-22 00:22:35 +0000811#endif
danielk1977e339d652008-06-28 11:23:00 +0000812 { "msdos", LOCKING_STYLE_DOTFILE },
813 { "webdav", LOCKING_STYLE_NONE },
814 { 0, 0 }
815 };
816 int i;
drhbfe66312006-10-03 17:40:40 +0000817 struct statfs fsInfo;
818
danielk1977e339d652008-06-28 11:23:00 +0000819 if( !filePath ){
820 return LOCKING_STYLE_NONE;
drh339eb0b2008-03-07 15:34:11 +0000821 }
danielk1977e339d652008-06-28 11:23:00 +0000822 if( pVfs->pAppData ){
aswiftf54b1b32008-08-22 18:41:37 +0000823 return SQLITE_PTR_TO_INT(pVfs->pAppData);
drh339eb0b2008-03-07 15:34:11 +0000824 }
drhbfe66312006-10-03 17:40:40 +0000825
danielk1977e339d652008-06-28 11:23:00 +0000826 if( statfs(filePath, &fsInfo) != -1 ){
827 if( fsInfo.f_flags & MNT_RDONLY ){
828 return LOCKING_STYLE_NONE;
829 }
830 for(i=0; aMap[i].zFilesystem; i++){
831 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
832 return aMap[i].eLockingStyle;
833 }
834 }
835 }
836
837 /* Default case. Handles, amongst others, "nfs". */
838 return testLockingStyle(fd);
danielk197762c14b32008-11-19 09:05:26 +0000839#endif /* if defined(__RTP__) || defined(_WRS_KERNEL) */
danielk1977e339d652008-06-28 11:23:00 +0000840 return LOCKING_STYLE_POSIX;
841}
danielk197762c14b32008-11-19 09:05:26 +0000842#else
843 #define detectLockingStyle(x,y,z) LOCKING_STYLE_POSIX
844#endif /* ifdef SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000845
drhbbd42a62004-05-22 17:41:58 +0000846/*
847** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000848** describes that file descriptor. Create new ones if necessary. The
849** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000850**
drh65594042008-05-05 16:56:34 +0000851** Return an appropriate error code.
drhbbd42a62004-05-22 17:41:58 +0000852*/
drh38f82712004-06-18 17:10:16 +0000853static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000854 int fd, /* The file descriptor used in the key */
chw97185482008-11-17 08:05:31 +0000855#if defined(__RTP__) || defined(_WRS_KERNEL)
856 void *rnam, /* vxWorks realname */
857#endif
drhbbd42a62004-05-22 17:41:58 +0000858 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000859 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000860){
861 int rc;
862 struct lockKey key1;
863 struct openKey key2;
864 struct stat statbuf;
865 struct lockInfo *pLock;
866 struct openCnt *pOpen;
867 rc = fstat(fd, &statbuf);
drh65594042008-05-05 16:56:34 +0000868 if( rc!=0 ){
869#ifdef EOVERFLOW
870 if( errno==EOVERFLOW ) return SQLITE_NOLFS;
871#endif
872 return SQLITE_IOERR;
873 }
danielk1977441b09a2006-01-05 13:48:29 +0000874
drh54626242008-07-30 17:28:04 +0000875 /* On OS X on an msdos filesystem, the inode number is reported
876 ** incorrectly for zero-size files. See ticket #3260. To work
877 ** around this problem (we consider it a bug in OS X, not SQLite)
878 ** we always increase the file size to 1 by writing a single byte
879 ** prior to accessing the inode number. The one byte written is
880 ** an ASCII 'S' character which also happens to be the first byte
881 ** in the header of every SQLite database. In this way, if there
882 ** is a race condition such that another thread has already populated
883 ** the first page of the database, no damage is done.
884 */
885 if( statbuf.st_size==0 ){
886 write(fd, "S", 1);
887 rc = fstat(fd, &statbuf);
888 if( rc!=0 ){
889 return SQLITE_IOERR;
890 }
891 }
892
drhbbd42a62004-05-22 17:41:58 +0000893 memset(&key1, 0, sizeof(key1));
894 key1.dev = statbuf.st_dev;
chw97185482008-11-17 08:05:31 +0000895#if defined(__RTP__) || defined(_WRS_KERNEL)
896 key1.rnam = rnam;
897#else
drhbbd42a62004-05-22 17:41:58 +0000898 key1.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000899#endif
drhd677b3d2007-08-20 22:48:41 +0000900#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000901 if( threadsOverrideEachOthersLocks<0 ){
902 testThreadLockingBehavior(fd);
903 }
904 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
905#endif
drhbbd42a62004-05-22 17:41:58 +0000906 memset(&key2, 0, sizeof(key2));
907 key2.dev = statbuf.st_dev;
chw97185482008-11-17 08:05:31 +0000908#if defined(__RTP__) || defined(_WRS_KERNEL)
909 key2.rnam = rnam;
910#else
drhbbd42a62004-05-22 17:41:58 +0000911 key2.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000912#endif
drhda0e7682008-07-30 15:27:54 +0000913 pLock = lockList;
914 while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
915 pLock = pLock->pNext;
916 }
drhbbd42a62004-05-22 17:41:58 +0000917 if( pLock==0 ){
drh17435752007-08-16 04:30:38 +0000918 pLock = sqlite3_malloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000919 if( pLock==0 ){
drh65594042008-05-05 16:56:34 +0000920 rc = SQLITE_NOMEM;
danielk1977441b09a2006-01-05 13:48:29 +0000921 goto exit_findlockinfo;
922 }
drhbbd42a62004-05-22 17:41:58 +0000923 pLock->key = key1;
924 pLock->nRef = 1;
925 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000926 pLock->locktype = 0;
drhda0e7682008-07-30 15:27:54 +0000927 pLock->pNext = lockList;
928 pLock->pPrev = 0;
929 if( lockList ) lockList->pPrev = pLock;
930 lockList = pLock;
drhbbd42a62004-05-22 17:41:58 +0000931 }else{
932 pLock->nRef++;
933 }
934 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000935 if( ppOpen!=0 ){
drhda0e7682008-07-30 15:27:54 +0000936 pOpen = openList;
937 while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){
938 pOpen = pOpen->pNext;
939 }
drhbbd42a62004-05-22 17:41:58 +0000940 if( pOpen==0 ){
drh17435752007-08-16 04:30:38 +0000941 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000942 if( pOpen==0 ){
943 releaseLockInfo(pLock);
drh65594042008-05-05 16:56:34 +0000944 rc = SQLITE_NOMEM;
drh029b44b2006-01-15 00:13:15 +0000945 goto exit_findlockinfo;
946 }
947 pOpen->key = key2;
948 pOpen->nRef = 1;
949 pOpen->nLock = 0;
950 pOpen->nPending = 0;
951 pOpen->aPending = 0;
drhda0e7682008-07-30 15:27:54 +0000952 pOpen->pNext = openList;
953 pOpen->pPrev = 0;
954 if( openList ) openList->pPrev = pOpen;
955 openList = pOpen;
chw97185482008-11-17 08:05:31 +0000956#if defined(__RTP__) || defined(_WRS_KERNEL)
957 pOpen->pSem = NULL;
958 pOpen->aSemName[0] = '\0';
959#endif
drh029b44b2006-01-15 00:13:15 +0000960 }else{
961 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000962 }
drh029b44b2006-01-15 00:13:15 +0000963 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000964 }
danielk1977441b09a2006-01-05 13:48:29 +0000965
966exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000967 return rc;
drhbbd42a62004-05-22 17:41:58 +0000968}
969
drh64b1bea2006-01-15 02:30:57 +0000970#ifdef SQLITE_DEBUG
971/*
972** Helper function for printing out trace information from debugging
973** binaries. This returns the string represetation of the supplied
974** integer lock-type.
975*/
976static const char *locktypeName(int locktype){
977 switch( locktype ){
978 case NO_LOCK: return "NONE";
979 case SHARED_LOCK: return "SHARED";
980 case RESERVED_LOCK: return "RESERVED";
981 case PENDING_LOCK: return "PENDING";
982 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
983 }
984 return "ERROR";
985}
986#endif
987
drhbbd42a62004-05-22 17:41:58 +0000988/*
drh029b44b2006-01-15 00:13:15 +0000989** If we are currently in a different thread than the thread that the
990** unixFile argument belongs to, then transfer ownership of the unixFile
991** over to the current thread.
992**
993** A unixFile is only owned by a thread on systems where one thread is
994** unable to override locks created by a different thread. RedHat9 is
995** an example of such a system.
996**
997** Ownership transfer is only allowed if the unixFile is currently unlocked.
998** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000999** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +00001000*/
drhd677b3d2007-08-20 22:48:41 +00001001#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +00001002static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +00001003 int rc;
drh029b44b2006-01-15 00:13:15 +00001004 pthread_t hSelf;
1005 if( threadsOverrideEachOthersLocks ){
1006 /* Ownership transfers not needed on this system */
1007 return SQLITE_OK;
1008 }
1009 hSelf = pthread_self();
1010 if( pthread_equal(pFile->tid, hSelf) ){
1011 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +00001012 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +00001013 return SQLITE_OK;
1014 }
1015 if( pFile->locktype!=NO_LOCK ){
1016 /* We cannot change ownership while we are holding a lock! */
1017 return SQLITE_MISUSE;
1018 }
drh4f0c5872007-03-26 22:05:01 +00001019 OSTRACE4("Transfer ownership of %d from %d to %d\n",
1020 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +00001021 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +00001022 if (pFile->pLock != NULL) {
1023 releaseLockInfo(pFile->pLock);
chw97185482008-11-17 08:05:31 +00001024#if defined(__RTP__) || defined(_WRS_KERNEL)
1025 rc = findLockInfo(pFile->h, pFile->zRealpath, &pFile->pLock, 0);
1026#else
drhbfe66312006-10-03 17:40:40 +00001027 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
chw97185482008-11-17 08:05:31 +00001028#endif
drh4f0c5872007-03-26 22:05:01 +00001029 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001030 locktypeName(pFile->locktype),
1031 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
1032 return rc;
1033 } else {
1034 return SQLITE_OK;
1035 }
drh029b44b2006-01-15 00:13:15 +00001036}
1037#else
drhf1a221e2006-01-15 17:27:17 +00001038 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +00001039# define transferOwnership(X) SQLITE_OK
1040#endif
1041
1042/*
danielk19772a6bdf62007-08-20 16:07:00 +00001043** Seek to the offset passed as the second argument, then read cnt
1044** bytes into pBuf. Return the number of bytes actually read.
drh9e0ebbf2007-10-23 15:59:18 +00001045**
1046** NB: If you define USE_PREAD or USE_PREAD64, then it might also
1047** be necessary to define _XOPEN_SOURCE to be 500. This varies from
1048** one system to another. Since SQLite does not define USE_PREAD
1049** any any form by default, we will not attempt to define _XOPEN_SOURCE.
1050** See tickets #2741 and #2681.
drhb912b282006-03-23 22:42:20 +00001051*/
danielk197762079062007-08-15 17:08:46 +00001052static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001053 int got;
drh8ebf6702007-02-06 11:11:08 +00001054 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001055 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001056#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001057 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001058 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +00001059#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001060 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001061 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +00001062#else
danielk197762079062007-08-15 17:08:46 +00001063 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +00001064 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +00001065 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +00001066 return -1;
1067 }
drhb912b282006-03-23 22:42:20 +00001068 got = read(id->h, pBuf, cnt);
1069#endif
drh15d00c42007-02-27 02:01:14 +00001070 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +00001071 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001072 return got;
1073}
1074
1075/*
drhbbd42a62004-05-22 17:41:58 +00001076** Read data from a file into a buffer. Return SQLITE_OK if all
1077** bytes were read successfully and SQLITE_IOERR if anything goes
1078** wrong.
1079*/
danielk197762079062007-08-15 17:08:46 +00001080static int unixRead(
1081 sqlite3_file *id,
1082 void *pBuf,
1083 int amt,
1084 sqlite3_int64 offset
1085){
drhbbd42a62004-05-22 17:41:58 +00001086 int got;
drh9cbe6352005-11-29 03:13:21 +00001087 assert( id );
danielk197762079062007-08-15 17:08:46 +00001088 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +00001089 if( got==amt ){
1090 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +00001091 }else if( got<0 ){
1092 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +00001093 }else{
drh4c17c3f2008-11-07 00:06:18 +00001094 /* Unread parts of the buffer must be zero-filled */
drhbafda092007-01-03 23:36:22 +00001095 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +00001096 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +00001097 }
1098}
1099
1100/*
drhb912b282006-03-23 22:42:20 +00001101** Seek to the offset in id->offset then read cnt bytes into pBuf.
1102** Return the number of bytes actually read. Update the offset.
1103*/
danielk197762079062007-08-15 17:08:46 +00001104static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001105 int got;
drh8ebf6702007-02-06 11:11:08 +00001106 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001107 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001108#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001109 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +00001110#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001111 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +00001112#else
danielk197762079062007-08-15 17:08:46 +00001113 newOffset = lseek(id->h, offset, SEEK_SET);
1114 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +00001115 return -1;
1116 }
drhb912b282006-03-23 22:42:20 +00001117 got = write(id->h, pBuf, cnt);
1118#endif
drh15d00c42007-02-27 02:01:14 +00001119 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +00001120 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001121 return got;
1122}
1123
1124
1125/*
drhbbd42a62004-05-22 17:41:58 +00001126** Write data from a buffer into a file. Return SQLITE_OK on success
1127** or some other error code on failure.
1128*/
danielk197762079062007-08-15 17:08:46 +00001129static int unixWrite(
1130 sqlite3_file *id,
1131 const void *pBuf,
1132 int amt,
1133 sqlite3_int64 offset
1134){
drhbbd42a62004-05-22 17:41:58 +00001135 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +00001136 assert( id );
drh4c7f9412005-02-03 00:29:47 +00001137 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +00001138 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +00001139 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +00001140 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +00001141 pBuf = &((char*)pBuf)[wrote];
1142 }
drh59685932006-09-14 13:47:11 +00001143 SimulateIOError(( wrote=(-1), amt=1 ));
1144 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +00001145 if( amt>0 ){
drh59685932006-09-14 13:47:11 +00001146 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +00001147 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +00001148 }else{
1149 return SQLITE_FULL;
1150 }
drhbbd42a62004-05-22 17:41:58 +00001151 }
1152 return SQLITE_OK;
1153}
1154
drhb851b2c2005-03-10 14:11:12 +00001155#ifdef SQLITE_TEST
1156/*
1157** Count the number of fullsyncs and normal syncs. This is used to test
1158** that syncs and fullsyncs are occuring at the right times.
1159*/
1160int sqlite3_sync_count = 0;
1161int sqlite3_fullsync_count = 0;
1162#endif
1163
drhf2f23912005-10-05 10:29:36 +00001164/*
1165** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
1166** Otherwise use fsync() in its place.
1167*/
1168#ifndef HAVE_FDATASYNC
1169# define fdatasync fsync
1170#endif
1171
drhac530b12006-02-11 01:25:50 +00001172/*
1173** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
1174** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
1175** only available on Mac OS X. But that could change.
1176*/
1177#ifdef F_FULLFSYNC
1178# define HAVE_FULLFSYNC 1
1179#else
1180# define HAVE_FULLFSYNC 0
1181#endif
1182
drhb851b2c2005-03-10 14:11:12 +00001183
drhbbd42a62004-05-22 17:41:58 +00001184/*
drhdd809b02004-07-17 21:44:57 +00001185** The fsync() system call does not work as advertised on many
1186** unix systems. The following procedure is an attempt to make
1187** it work better.
drh1398ad32005-01-19 23:24:50 +00001188**
1189** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
1190** for testing when we want to run through the test suite quickly.
1191** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
1192** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
1193** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +00001194*/
drheb796a72005-09-08 12:38:41 +00001195static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +00001196 int rc;
drhb851b2c2005-03-10 14:11:12 +00001197
danielk1977397d65f2008-11-19 11:35:39 +00001198 /* The following "ifdef/elif/else/" block has the same structure as
1199 ** the one below. It is replicated here solely to avoid cluttering
1200 ** up the real code with the UNUSED_PARAMETER() macros.
1201 */
1202#ifdef SQLITE_NO_SYNC
1203 UNUSED_PARAMETER(fd);
1204 UNUSED_PARAMETER(fullSync);
1205 UNUSED_PARAMETER(dataOnly);
1206#elif HAVE_FULLFSYNC
1207 UNUSED_PARAMETER(fullSync);
1208#else
1209 UNUSED_PARAMETER(dataOnly);
1210#endif
1211
drhb851b2c2005-03-10 14:11:12 +00001212 /* Record the number of times that we do a normal fsync() and
1213 ** FULLSYNC. This is used during testing to verify that this procedure
1214 ** gets called with the correct arguments.
1215 */
1216#ifdef SQLITE_TEST
1217 if( fullSync ) sqlite3_fullsync_count++;
1218 sqlite3_sync_count++;
1219#endif
1220
1221 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
1222 ** no-op
1223 */
1224#ifdef SQLITE_NO_SYNC
1225 rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00001226#elif HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +00001227 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +00001228 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +00001229 }else{
1230 rc = 1;
1231 }
1232 /* If the FULLFSYNC failed, fall back to attempting an fsync().
1233 * It shouldn't be possible for fullfsync to fail on the local
1234 * file system (on OSX), so failure indicates that FULLFSYNC
1235 * isn't supported for this file system. So, attempt an fsync
1236 * and (for now) ignore the overhead of a superfluous fcntl call.
1237 * It'd be better to detect fullfsync support once and avoid
1238 * the fcntl call every time sync is called.
1239 */
1240 if( rc ) rc = fsync(fd);
1241
1242#else
drheb796a72005-09-08 12:38:41 +00001243 if( dataOnly ){
1244 rc = fdatasync(fd);
danielk1977397d65f2008-11-19 11:35:39 +00001245 if( IS_VXWORKS && rc==-1 && errno==ENOTSUP ){
chw97185482008-11-17 08:05:31 +00001246 rc = fsync(fd);
1247 }
drhf2f23912005-10-05 10:29:36 +00001248 }else{
drheb796a72005-09-08 12:38:41 +00001249 rc = fsync(fd);
1250 }
danielk1977397d65f2008-11-19 11:35:39 +00001251#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +00001252
danielk1977397d65f2008-11-19 11:35:39 +00001253 if( IS_VXWORKS && rc!= -1 ){
chw97185482008-11-17 08:05:31 +00001254 rc = 0;
1255 }
drhdd809b02004-07-17 21:44:57 +00001256 return rc;
1257}
1258
1259/*
drhbbd42a62004-05-22 17:41:58 +00001260** Make sure all writes to a particular file are committed to disk.
1261**
drheb796a72005-09-08 12:38:41 +00001262** If dataOnly==0 then both the file itself and its metadata (file
1263** size, access time, etc) are synced. If dataOnly!=0 then only the
1264** file data is synced.
1265**
drhbbd42a62004-05-22 17:41:58 +00001266** Under Unix, also make sure that the directory entry for the file
1267** has been created by fsync-ing the directory that contains the file.
1268** If we do not do this and we encounter a power failure, the directory
1269** entry for the journal might not exist after we reboot. The next
1270** SQLite to access the file will not know that the journal exists (because
1271** the directory entry for the journal was never created) and the transaction
1272** will not roll back - possibly leading to database corruption.
1273*/
danielk197790949c22007-08-17 16:50:38 +00001274static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +00001275 int rc;
drh054889e2005-11-30 03:20:31 +00001276 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +00001277
danielk1977f036aef2007-08-20 05:36:51 +00001278 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
1279 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
1280
danielk1977c16d4632007-08-30 14:49:58 +00001281 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
danielk1977f036aef2007-08-20 05:36:51 +00001282 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
1283 || (flags&0x0F)==SQLITE_SYNC_FULL
danielk1977f036aef2007-08-20 05:36:51 +00001284 );
danielk197790949c22007-08-17 16:50:38 +00001285
danielk1977cd3b3c82008-09-22 11:46:32 +00001286 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
1287 ** line is to test that doing so does not cause any problems.
1288 */
1289 SimulateDiskfullError( return SQLITE_FULL );
1290
drh054889e2005-11-30 03:20:31 +00001291 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001292 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001293 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001294 SimulateIOError( rc=1 );
1295 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001296 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001297 }
drh054889e2005-11-30 03:20:31 +00001298 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001299 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001300 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001301#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001302 /* The directory sync is only attempted if full_fsync is
1303 ** turned off or unavailable. If a full_fsync occurred above,
1304 ** then the directory sync is superfluous.
1305 */
danielk197790949c22007-08-17 16:50:38 +00001306 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001307 /*
1308 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001309 ** errors when applied to directories on certain file systems.
1310 ** A failed directory sync is not a big deal. So it seems
1311 ** better to ignore the error. Ticket #1657
1312 */
1313 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001314 }
danielk1977d7c03f72005-11-25 10:38:22 +00001315#endif
drh054889e2005-11-30 03:20:31 +00001316 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1317 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001318 }
drha2854222004-06-17 19:04:17 +00001319 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001320}
1321
1322/*
1323** Truncate an open file to a specified size
1324*/
danielk197762079062007-08-15 17:08:46 +00001325static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001326 int rc;
drh9cbe6352005-11-29 03:13:21 +00001327 assert( id );
drh93aed5a2008-01-16 17:46:38 +00001328 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
drh63fff5f2007-06-19 10:50:38 +00001329 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001330 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001331 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001332 }else{
1333 return SQLITE_OK;
1334 }
drhbbd42a62004-05-22 17:41:58 +00001335}
1336
1337/*
1338** Determine the current size of a file in bytes
1339*/
danielk197762079062007-08-15 17:08:46 +00001340static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001341 int rc;
drhbbd42a62004-05-22 17:41:58 +00001342 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001343 assert( id );
drh59685932006-09-14 13:47:11 +00001344 rc = fstat(((unixFile*)id)->h, &buf);
1345 SimulateIOError( rc=1 );
1346 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001347 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001348 }
1349 *pSize = buf.st_size;
drh54626242008-07-30 17:28:04 +00001350
1351 /* When opening a zero-size database, the findLockInfo() procedure
1352 ** writes a single byte into that file in order to work around a bug
1353 ** in the OS-X msdos filesystem. In order to avoid problems with upper
1354 ** layers, we need to report this file size as zero even though it is
1355 ** really 1. Ticket #3260.
1356 */
1357 if( *pSize==1 ) *pSize = 0;
1358
1359
drhbbd42a62004-05-22 17:41:58 +00001360 return SQLITE_OK;
1361}
1362
danielk19779a1d0ab2004-06-01 14:09:28 +00001363/*
aswift5b1a2562008-08-22 00:22:35 +00001364** This routine translates a standard POSIX errno code into something
1365** useful to the clients of the sqlite3 functions. Specifically, it is
1366** intended to translate a variety of "try again" errors into SQLITE_BUSY
1367** and a variety of "please close the file descriptor NOW" errors into
1368** SQLITE_IOERR
1369**
1370** Errors during initialization of locks, or file system support for locks,
1371** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
1372*/
1373static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
1374 switch (posixError) {
1375 case 0:
1376 return SQLITE_OK;
1377
1378 case EAGAIN:
1379 case ETIMEDOUT:
1380 case EBUSY:
1381 case EINTR:
1382 case ENOLCK:
1383 /* random NFS retry error, unless during file system support
1384 * introspection, in which it actually means what it says */
1385 return SQLITE_BUSY;
1386
1387 case EACCES:
1388 /* EACCES is like EAGAIN during locking operations, but not any other time*/
1389 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
1390 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
1391 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
1392 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
1393 return SQLITE_BUSY;
1394 }
1395 /* else fall through */
1396 case EPERM:
1397 return SQLITE_PERM;
1398
1399 case EDEADLK:
1400 return SQLITE_IOERR_BLOCKED;
1401
drhf489c452008-08-22 00:47:53 +00001402#if EOPNOTSUPP!=ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001403 case EOPNOTSUPP:
1404 /* something went terribly awry, unless during file system support
1405 * introspection, in which it actually means what it says */
drhf489c452008-08-22 00:47:53 +00001406#endif
danielk19775ad6a882008-09-15 04:20:31 +00001407#ifdef ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001408 case ENOTSUP:
1409 /* invalid fd, unless during file system support introspection, in which
1410 * it actually means what it says */
danielk19775ad6a882008-09-15 04:20:31 +00001411#endif
aswift5b1a2562008-08-22 00:22:35 +00001412 case EIO:
1413 case EBADF:
1414 case EINVAL:
1415 case ENOTCONN:
1416 case ENODEV:
1417 case ENXIO:
1418 case ENOENT:
1419 case ESTALE:
1420 case ENOSYS:
1421 /* these should force the client to close the file and reconnect */
1422
1423 default:
1424 return sqliteIOErr;
1425 }
1426}
1427
1428/*
danielk197713adf8a2004-06-03 16:08:41 +00001429** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001430** file by this or any other process. If such a lock is held, set *pResOut
1431** to a non-zero value otherwise *pResOut is set to zero. The return value
1432** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001433*/
danielk1977861f7452008-06-05 11:39:11 +00001434static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001435 int rc = SQLITE_OK;
1436 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001437 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001438
danielk1977861f7452008-06-05 11:39:11 +00001439 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1440
drh054889e2005-11-30 03:20:31 +00001441 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001442 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001443
1444 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001445 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001446 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001447 }
1448
drh2ac3ee92004-06-07 16:27:46 +00001449 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001450 */
aswift5b1a2562008-08-22 00:22:35 +00001451 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001452 struct flock lock;
1453 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001454 lock.l_start = RESERVED_BYTE;
1455 lock.l_len = 1;
1456 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001457 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1458 int tErrno = errno;
1459 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1460 pFile->lastErrno = tErrno;
1461 } else if( lock.l_type!=F_UNLCK ){
1462 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001463 }
1464 }
1465
danielk1977b4b47412007-08-17 15:53:36 +00001466 leaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001467 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001468
aswift5b1a2562008-08-22 00:22:35 +00001469 *pResOut = reserved;
1470 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001471}
1472
1473/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001474** Lock the file with the lock specified by parameter locktype - one
1475** of the following:
1476**
drh2ac3ee92004-06-07 16:27:46 +00001477** (1) SHARED_LOCK
1478** (2) RESERVED_LOCK
1479** (3) PENDING_LOCK
1480** (4) EXCLUSIVE_LOCK
1481**
drhb3e04342004-06-08 00:47:47 +00001482** Sometimes when requesting one lock state, additional lock states
1483** are inserted in between. The locking might fail on one of the later
1484** transitions leaving the lock state different from what it started but
1485** still short of its goal. The following chart shows the allowed
1486** transitions and the inserted intermediate states:
1487**
1488** UNLOCKED -> SHARED
1489** SHARED -> RESERVED
1490** SHARED -> (PENDING) -> EXCLUSIVE
1491** RESERVED -> (PENDING) -> EXCLUSIVE
1492** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001493**
drha6abd042004-06-09 17:37:22 +00001494** This routine will only increase a lock. Use the sqlite3OsUnlock()
1495** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001496*/
danielk197762079062007-08-15 17:08:46 +00001497static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001498 /* The following describes the implementation of the various locks and
1499 ** lock transitions in terms of the POSIX advisory shared and exclusive
1500 ** lock primitives (called read-locks and write-locks below, to avoid
1501 ** confusion with SQLite lock names). The algorithms are complicated
1502 ** slightly in order to be compatible with windows systems simultaneously
1503 ** accessing the same database file, in case that is ever required.
1504 **
1505 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1506 ** byte', each single bytes at well known offsets, and the 'shared byte
1507 ** range', a range of 510 bytes at a well known offset.
1508 **
1509 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1510 ** byte'. If this is successful, a random byte from the 'shared byte
1511 ** range' is read-locked and the lock on the 'pending byte' released.
1512 **
danielk197790ba3bd2004-06-25 08:32:25 +00001513 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1514 ** A RESERVED lock is implemented by grabbing a write-lock on the
1515 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001516 **
1517 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001518 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1519 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1520 ** obtained, but existing SHARED locks are allowed to persist. A process
1521 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1522 ** This property is used by the algorithm for rolling back a journal file
1523 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001524 **
danielk197790ba3bd2004-06-25 08:32:25 +00001525 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1526 ** implemented by obtaining a write-lock on the entire 'shared byte
1527 ** range'. Since all other locks require a read-lock on one of the bytes
1528 ** within this range, this ensures that no other locks are held on the
1529 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001530 **
1531 ** The reason a single byte cannot be used instead of the 'shared byte
1532 ** range' is that some versions of windows do not support read-locks. By
1533 ** locking a random byte from a range, concurrent SHARED locks may exist
1534 ** even if the locking primitive used is always a write-lock.
1535 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001536 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001537 unixFile *pFile = (unixFile*)id;
1538 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001539 struct flock lock;
1540 int s;
1541
drh054889e2005-11-30 03:20:31 +00001542 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001543 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001544 locktypeName(locktype), locktypeName(pFile->locktype),
1545 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001546
1547 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001548 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001549 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001550 */
drh054889e2005-11-30 03:20:31 +00001551 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001552 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001553 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001554 return SQLITE_OK;
1555 }
1556
drhb3e04342004-06-08 00:47:47 +00001557 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001558 */
drh054889e2005-11-30 03:20:31 +00001559 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001560 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001561 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001562
drh054889e2005-11-30 03:20:31 +00001563 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001564 */
danielk1977b4b47412007-08-17 15:53:36 +00001565 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001566
drh029b44b2006-01-15 00:13:15 +00001567 /* Make sure the current thread owns the pFile.
1568 */
1569 rc = transferOwnership(pFile);
1570 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001571 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001572 return rc;
1573 }
drh64b1bea2006-01-15 02:30:57 +00001574 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001575
danielk1977ad94b582007-08-20 06:44:22 +00001576 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001577 ** handle that precludes the requested lock, return BUSY.
1578 */
drh054889e2005-11-30 03:20:31 +00001579 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001580 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001581 ){
1582 rc = SQLITE_BUSY;
1583 goto end_lock;
1584 }
1585
1586 /* If a SHARED lock is requested, and some thread using this PID already
1587 ** has a SHARED or RESERVED lock, then increment reference counts and
1588 ** return SQLITE_OK.
1589 */
1590 if( locktype==SHARED_LOCK &&
1591 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1592 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001593 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001594 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001595 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001596 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001597 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001598 goto end_lock;
1599 }
1600
danielk197713adf8a2004-06-03 16:08:41 +00001601 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001602
danielk19779a1d0ab2004-06-01 14:09:28 +00001603 lock.l_whence = SEEK_SET;
1604
drh3cde3bb2004-06-12 02:17:14 +00001605 /* A PENDING lock is needed before acquiring a SHARED lock and before
1606 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1607 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001608 */
drh3cde3bb2004-06-12 02:17:14 +00001609 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001610 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001611 ){
danielk1977489468c2004-06-28 08:25:47 +00001612 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001613 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001614 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001615 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001616 int tErrno = errno;
1617 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1618 if( IS_LOCK_ERROR(rc) ){
1619 pFile->lastErrno = tErrno;
1620 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001621 goto end_lock;
1622 }
drh3cde3bb2004-06-12 02:17:14 +00001623 }
1624
1625
1626 /* If control gets to this point, then actually go ahead and make
1627 ** operating system calls for the specified lock.
1628 */
1629 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001630 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001631 assert( pLock->cnt==0 );
1632 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001633
drh2ac3ee92004-06-07 16:27:46 +00001634 /* Now get the read-lock */
1635 lock.l_start = SHARED_FIRST;
1636 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001637 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1638 tErrno = errno;
1639 }
drh2ac3ee92004-06-07 16:27:46 +00001640 /* Drop the temporary PENDING lock */
1641 lock.l_start = PENDING_BYTE;
1642 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001643 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001644 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001645 if( s != -1 ){
1646 /* This could happen with a network mount */
1647 tErrno = errno;
1648 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1649 if( IS_LOCK_ERROR(rc) ){
1650 pFile->lastErrno = tErrno;
1651 }
1652 goto end_lock;
1653 }
drh2b4b5962005-06-15 17:47:55 +00001654 }
drhe2396a12007-03-29 20:19:58 +00001655 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001656 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1657 if( IS_LOCK_ERROR(rc) ){
1658 pFile->lastErrno = tErrno;
1659 }
drhbbd42a62004-05-22 17:41:58 +00001660 }else{
drh054889e2005-11-30 03:20:31 +00001661 pFile->locktype = SHARED_LOCK;
1662 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001663 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001664 }
drh3cde3bb2004-06-12 02:17:14 +00001665 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1666 /* We are trying for an exclusive lock but another thread in this
1667 ** same process is still holding a shared lock. */
1668 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001669 }else{
drh3cde3bb2004-06-12 02:17:14 +00001670 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001671 ** assumed that there is a SHARED or greater lock on the file
1672 ** already.
1673 */
drh054889e2005-11-30 03:20:31 +00001674 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001675 lock.l_type = F_WRLCK;
1676 switch( locktype ){
1677 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001678 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001679 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001680 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001681 lock.l_start = SHARED_FIRST;
1682 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001683 break;
1684 default:
1685 assert(0);
1686 }
drh054889e2005-11-30 03:20:31 +00001687 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001688 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001689 int tErrno = errno;
1690 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1691 if( IS_LOCK_ERROR(rc) ){
1692 pFile->lastErrno = tErrno;
1693 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001694 }
drhbbd42a62004-05-22 17:41:58 +00001695 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001696
danielk1977ecb2a962004-06-02 06:30:16 +00001697 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001698 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001699 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001700 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001701 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001702 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001703 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001704
1705end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001706 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001707 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001708 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001709 return rc;
1710}
1711
1712/*
drh054889e2005-11-30 03:20:31 +00001713** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001714** must be either NO_LOCK or SHARED_LOCK.
1715**
1716** If the locking level of the file descriptor is already at or below
1717** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001718*/
danielk197762079062007-08-15 17:08:46 +00001719static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001720 struct lockInfo *pLock;
1721 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001722 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001723 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001724 int h;
drha6abd042004-06-09 17:37:22 +00001725
drh054889e2005-11-30 03:20:31 +00001726 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001727 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001728 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001729
1730 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001731 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001732 return SQLITE_OK;
1733 }
drhf1a221e2006-01-15 17:27:17 +00001734 if( CHECK_THREADID(pFile) ){
1735 return SQLITE_MISUSE;
1736 }
danielk1977b4b47412007-08-17 15:53:36 +00001737 enterMutex();
drh1aa5af12008-03-07 19:51:14 +00001738 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001739 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001740 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001741 if( pFile->locktype>SHARED_LOCK ){
1742 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001743 SimulateIOErrorBenign(1);
1744 SimulateIOError( h=(-1) )
1745 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001746 if( locktype==SHARED_LOCK ){
1747 lock.l_type = F_RDLCK;
1748 lock.l_whence = SEEK_SET;
1749 lock.l_start = SHARED_FIRST;
1750 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001751 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001752 int tErrno = errno;
1753 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1754 if( IS_LOCK_ERROR(rc) ){
1755 pFile->lastErrno = tErrno;
1756 }
1757 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001758 }
1759 }
drhbbd42a62004-05-22 17:41:58 +00001760 lock.l_type = F_UNLCK;
1761 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001762 lock.l_start = PENDING_BYTE;
1763 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001764 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001765 pLock->locktype = SHARED_LOCK;
1766 }else{
aswift5b1a2562008-08-22 00:22:35 +00001767 int tErrno = errno;
1768 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1769 if( IS_LOCK_ERROR(rc) ){
1770 pFile->lastErrno = tErrno;
1771 }
1772 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001773 }
drhbbd42a62004-05-22 17:41:58 +00001774 }
drha6abd042004-06-09 17:37:22 +00001775 if( locktype==NO_LOCK ){
1776 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001777
drha6abd042004-06-09 17:37:22 +00001778 /* Decrement the shared lock counter. Release the lock using an
1779 ** OS call only when all threads in this same process have released
1780 ** the lock.
1781 */
1782 pLock->cnt--;
1783 if( pLock->cnt==0 ){
1784 lock.l_type = F_UNLCK;
1785 lock.l_whence = SEEK_SET;
1786 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001787 SimulateIOErrorBenign(1);
1788 SimulateIOError( h=(-1) )
1789 SimulateIOErrorBenign(0);
1790 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001791 pLock->locktype = NO_LOCK;
1792 }else{
aswift5b1a2562008-08-22 00:22:35 +00001793 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001794 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001795 if( IS_LOCK_ERROR(rc) ){
1796 pFile->lastErrno = tErrno;
1797 }
drh1aa5af12008-03-07 19:51:14 +00001798 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001799 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001800 }
drha6abd042004-06-09 17:37:22 +00001801 }
1802
drhbbd42a62004-05-22 17:41:58 +00001803 /* Decrement the count of locks against this same file. When the
1804 ** count reaches zero, close any other file descriptors whose close
1805 ** was deferred because of outstanding locks.
1806 */
drh1aa5af12008-03-07 19:51:14 +00001807 if( rc==SQLITE_OK ){
1808 pOpen = pFile->pOpen;
1809 pOpen->nLock--;
1810 assert( pOpen->nLock>=0 );
1811 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1812 int i;
1813 for(i=0; i<pOpen->nPending; i++){
1814 close(pOpen->aPending[i]);
1815 }
drhda0e7682008-07-30 15:27:54 +00001816 sqlite3_free(pOpen->aPending);
drh1aa5af12008-03-07 19:51:14 +00001817 pOpen->nPending = 0;
1818 pOpen->aPending = 0;
drhbbd42a62004-05-22 17:41:58 +00001819 }
drhbbd42a62004-05-22 17:41:58 +00001820 }
1821 }
aswift5b1a2562008-08-22 00:22:35 +00001822
1823end_unlock:
danielk1977b4b47412007-08-17 15:53:36 +00001824 leaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001825 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001826 return rc;
drhbbd42a62004-05-22 17:41:58 +00001827}
1828
1829/*
danielk1977e339d652008-06-28 11:23:00 +00001830** This function performs the parts of the "close file" operation
1831** common to all locking schemes. It closes the directory and file
1832** handles, if they are valid, and sets all fields of the unixFile
1833** structure to 0.
1834*/
1835static int closeUnixFile(sqlite3_file *id){
1836 unixFile *pFile = (unixFile*)id;
1837 if( pFile ){
1838 if( pFile->dirfd>=0 ){
1839 close(pFile->dirfd);
1840 }
1841 if( pFile->h>=0 ){
1842 close(pFile->h);
1843 }
chw97185482008-11-17 08:05:31 +00001844#if defined(__RTP__) || defined(_WRS_KERNEL)
1845 if( pFile->isDelete && pFile->zRealpath ){
1846 unlink(pFile->zRealpath);
1847 }
1848 if( pFile->zRealpath ){
1849 HashElem *pElem;
1850 int n = strlen(pFile->zRealpath) + 1;
1851 pElem = sqlite3HashFindElem(&nameHash, pFile->zRealpath, n);
1852 if( pElem ){
1853 long cnt = (long)pElem->data;
1854 cnt--;
1855 if( cnt==0 ){
1856 sqlite3HashInsert(&nameHash, pFile->zRealpath, n, 0);
1857 }else{
1858 pElem->data = (void*)cnt;
1859 }
1860 }
1861 }
1862#endif
danielk1977e339d652008-06-28 11:23:00 +00001863 OSTRACE2("CLOSE %-3d\n", pFile->h);
1864 OpenCounter(-1);
1865 memset(pFile, 0, sizeof(unixFile));
1866 }
1867 return SQLITE_OK;
1868}
1869
1870/*
danielk1977e3026632004-06-22 11:29:02 +00001871** Close a file.
1872*/
danielk197762079062007-08-15 17:08:46 +00001873static int unixClose(sqlite3_file *id){
danielk1977e339d652008-06-28 11:23:00 +00001874 if( id ){
1875 unixFile *pFile = (unixFile *)id;
1876 unixUnlock(id, NO_LOCK);
1877 enterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001878 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001879 /* If there are outstanding locks, do not actually close the file just
1880 ** yet because that would clear those locks. Instead, add the file
1881 ** descriptor to pOpen->aPending. It will be automatically closed when
1882 ** the last lock is cleared.
1883 */
1884 int *aNew;
1885 struct openCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001886 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001887 if( aNew==0 ){
1888 /* If a malloc fails, just leak the file descriptor */
1889 }else{
1890 pOpen->aPending = aNew;
1891 pOpen->aPending[pOpen->nPending] = pFile->h;
1892 pOpen->nPending++;
1893 pFile->h = -1;
1894 }
danielk1977e3026632004-06-22 11:29:02 +00001895 }
danielk1977e339d652008-06-28 11:23:00 +00001896 releaseLockInfo(pFile->pLock);
1897 releaseOpenCnt(pFile->pOpen);
1898 closeUnixFile(id);
1899 leaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00001900 }
drh02afc862006-01-20 18:10:57 +00001901 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001902}
1903
drhbfe66312006-10-03 17:40:40 +00001904
drh40bbb0a2008-09-23 10:23:26 +00001905#if SQLITE_ENABLE_LOCKING_STYLE
chw97185482008-11-17 08:05:31 +00001906
1907#if !defined(__RTP__) && !defined(_WRS_KERNEL)
drhbfe66312006-10-03 17:40:40 +00001908#pragma mark AFP Support
1909
1910/*
1911 ** The afpLockingContext structure contains all afp lock specific state
1912 */
1913typedef struct afpLockingContext afpLockingContext;
1914struct afpLockingContext {
drh1aa5af12008-03-07 19:51:14 +00001915 unsigned long long sharedLockByte;
drh308aa322008-03-07 20:14:38 +00001916 const char *filePath;
drhbfe66312006-10-03 17:40:40 +00001917};
1918
1919struct ByteRangeLockPB2
1920{
1921 unsigned long long offset; /* offset to first byte to lock */
1922 unsigned long long length; /* nbr of bytes to lock */
1923 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1924 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1925 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1926 int fd; /* file desc to assoc this lock with */
1927};
1928
drhfd131da2007-08-07 17:13:03 +00001929#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001930
danielk1977ad94b582007-08-20 06:44:22 +00001931/*
aswift5b1a2562008-08-22 00:22:35 +00001932 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
1933 */
danielk1977ad94b582007-08-20 06:44:22 +00001934static int _AFPFSSetLock(
1935 const char *path,
aswift5b1a2562008-08-22 00:22:35 +00001936 unixFile *pFile,
danielk1977ad94b582007-08-20 06:44:22 +00001937 unsigned long long offset,
1938 unsigned long long length,
1939 int setLockFlag
1940){
drhfd131da2007-08-07 17:13:03 +00001941 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001942 int err;
1943
1944 pb.unLockFlag = setLockFlag ? 0 : 1;
1945 pb.startEndFlag = 0;
1946 pb.offset = offset;
1947 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00001948 pb.fd = pFile->h;
drh4f0c5872007-03-26 22:05:01 +00001949 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
aswift5b1a2562008-08-22 00:22:35 +00001950 (setLockFlag?"ON":"OFF"), pFile->h, offset, length);
drhbfe66312006-10-03 17:40:40 +00001951 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1952 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00001953 int rc;
1954 int tErrno = errno;
1955 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno));
1956 rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); /* error */
1957 if( IS_LOCK_ERROR(rc) ){
1958 pFile->lastErrno = tErrno;
1959 }
1960 return rc;
drhbfe66312006-10-03 17:40:40 +00001961 } else {
aswift5b1a2562008-08-22 00:22:35 +00001962 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001963 }
1964}
1965
aswift5b1a2562008-08-22 00:22:35 +00001966/* AFP-style reserved lock checking following the behavior of
1967** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00001968static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001969 int rc = SQLITE_OK;
1970 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00001971 unixFile *pFile = (unixFile*)id;
1972
aswift5b1a2562008-08-22 00:22:35 +00001973 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1974
1975 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00001976 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1977
1978 /* Check if a thread in this process holds such a lock */
1979 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001980 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00001981 }
1982
1983 /* Otherwise see if some other process holds it.
1984 */
aswift5b1a2562008-08-22 00:22:35 +00001985 if( !reserved ){
1986 /* lock the RESERVED byte */
1987 int lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
1988 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00001989 /* if we succeeded in taking the reserved lock, unlock it to restore
1990 ** the original state */
aswift5b1a2562008-08-22 00:22:35 +00001991 lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1, 0);
1992 } else {
1993 /* if we failed to get the lock then someone else must have it */
1994 reserved = 1;
1995 }
1996 if( IS_LOCK_ERROR(lrc) ){
1997 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00001998 }
1999 }
drhbfe66312006-10-03 17:40:40 +00002000
aswift5b1a2562008-08-22 00:22:35 +00002001 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2002
2003 *pResOut = reserved;
2004 return rc;
drhbfe66312006-10-03 17:40:40 +00002005}
2006
2007/* AFP-style locking following the behavior of unixLock, see the unixLock
2008** function comments for details of lock management. */
danielk1977e339d652008-06-28 11:23:00 +00002009static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00002010 int rc = SQLITE_OK;
2011 unixFile *pFile = (unixFile*)id;
2012 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002013
2014 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002015 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002016 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2017
drhbfe66312006-10-03 17:40:40 +00002018 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002019 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2020 ** enterMutex() hasn't been called yet.
2021 */
drhbfe66312006-10-03 17:40:40 +00002022 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002023 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002024 locktypeName(locktype));
2025 return SQLITE_OK;
2026 }
2027
2028 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002029 */
drhbfe66312006-10-03 17:40:40 +00002030 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2031 assert( locktype!=PENDING_LOCK );
2032 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2033
2034 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002035 */
danielk1977b4b47412007-08-17 15:53:36 +00002036 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002037
2038 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002039 */
drhbfe66312006-10-03 17:40:40 +00002040 rc = transferOwnership(pFile);
2041 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00002042 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002043 return rc;
2044 }
2045
2046 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002047 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2048 ** be released.
2049 */
drhbfe66312006-10-03 17:40:40 +00002050 if( locktype==SHARED_LOCK
2051 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002052 ){
2053 int failed;
aswift5b1a2562008-08-22 00:22:35 +00002054 failed = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002055 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002056 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002057 goto afp_end_lock;
2058 }
2059 }
2060
2061 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002062 ** operating system calls for the specified lock.
2063 */
drhbfe66312006-10-03 17:40:40 +00002064 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002065 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002066
aswift5b1a2562008-08-22 00:22:35 +00002067 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002068 /* note that the quality of the randomness doesn't matter that much */
2069 lk = random();
2070 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
aswift5b1a2562008-08-22 00:22:35 +00002071 lrc1 = _AFPFSSetLock(context->filePath, pFile,
2072 SHARED_FIRST+context->sharedLockByte, 1, 1);
2073 if( IS_LOCK_ERROR(lrc1) ){
2074 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002075 }
aswift5b1a2562008-08-22 00:22:35 +00002076 /* Drop the temporary PENDING lock */
2077 lrc2 = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002078
aswift5b1a2562008-08-22 00:22:35 +00002079 if( IS_LOCK_ERROR(lrc1) ) {
2080 pFile->lastErrno = lrc1Errno;
2081 rc = lrc1;
2082 goto afp_end_lock;
2083 } else if( IS_LOCK_ERROR(lrc2) ){
2084 rc = lrc2;
2085 goto afp_end_lock;
2086 } else if( lrc1 != SQLITE_OK ) {
2087 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002088 } else {
2089 pFile->locktype = SHARED_LOCK;
2090 }
2091 }else{
2092 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2093 ** assumed that there is a SHARED or greater lock on the file
2094 ** already.
2095 */
2096 int failed = 0;
2097 assert( 0!=pFile->locktype );
2098 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2099 /* Acquire a RESERVED lock */
aswift5b1a2562008-08-22 00:22:35 +00002100 failed = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002101 }
2102 if (!failed && locktype == EXCLUSIVE_LOCK) {
2103 /* Acquire an EXCLUSIVE lock */
2104
2105 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002106 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002107 */
aswift5b1a2562008-08-22 00:22:35 +00002108 if (!(failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST +
2109 context->sharedLockByte, 1, 0))) {
drhbfe66312006-10-03 17:40:40 +00002110 /* now attemmpt to get the exclusive lock range */
aswift5b1a2562008-08-22 00:22:35 +00002111 failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002112 SHARED_SIZE, 1);
aswift5b1a2562008-08-22 00:22:35 +00002113 if (failed && (failed = _AFPFSSetLock(context->filePath, pFile,
2114 SHARED_FIRST + context->sharedLockByte, 1, 1))) {
2115 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002116 }
2117 } else {
aswift5b1a2562008-08-22 00:22:35 +00002118 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002119 }
2120 }
aswift5b1a2562008-08-22 00:22:35 +00002121 if( failed ){
2122 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002123 }
2124 }
2125
2126 if( rc==SQLITE_OK ){
2127 pFile->locktype = locktype;
2128 }else if( locktype==EXCLUSIVE_LOCK ){
2129 pFile->locktype = PENDING_LOCK;
2130 }
2131
2132afp_end_lock:
drh339eb0b2008-03-07 15:34:11 +00002133 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002134 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002135 rc==SQLITE_OK ? "ok" : "failed");
2136 return rc;
2137}
2138
2139/*
drh339eb0b2008-03-07 15:34:11 +00002140** Lower the locking level on file descriptor pFile to locktype. locktype
2141** must be either NO_LOCK or SHARED_LOCK.
2142**
2143** If the locking level of the file descriptor is already at or below
2144** the requested locking level, this routine is a no-op.
2145*/
danielk1977e339d652008-06-28 11:23:00 +00002146static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002147 int rc = SQLITE_OK;
2148 unixFile *pFile = (unixFile*)id;
2149 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2150
2151 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002152 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002153 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002154
drhbfe66312006-10-03 17:40:40 +00002155 assert( locktype<=SHARED_LOCK );
2156 if( pFile->locktype<=locktype ){
2157 return SQLITE_OK;
2158 }
2159 if( CHECK_THREADID(pFile) ){
2160 return SQLITE_MISUSE;
2161 }
danielk1977b4b47412007-08-17 15:53:36 +00002162 enterMutex();
aswift5b1a2562008-08-22 00:22:35 +00002163 int failed = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002164 if( pFile->locktype>SHARED_LOCK ){
2165 if( locktype==SHARED_LOCK ){
drhbfe66312006-10-03 17:40:40 +00002166
2167 /* unlock the exclusive range - then re-establish the shared lock */
2168 if (pFile->locktype==EXCLUSIVE_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002169 failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002170 SHARED_SIZE, 0);
2171 if (!failed) {
2172 /* successfully removed the exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002173 if ((failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST+
2174 context->sharedLockByte, 1, 1))) {
drhbfe66312006-10-03 17:40:40 +00002175 /* failed to re-establish our shared lock */
aswift5b1a2562008-08-22 00:22:35 +00002176 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002177 }
2178 } else {
aswift5b1a2562008-08-22 00:22:35 +00002179 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002180 }
2181 }
2182 }
2183 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002184 if ((failed = _AFPFSSetLock(context->filePath, pFile,
2185 PENDING_BYTE, 1, 0))){
drhbfe66312006-10-03 17:40:40 +00002186 /* failed to release the pending lock */
aswift5b1a2562008-08-22 00:22:35 +00002187 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002188 }
2189 }
2190 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002191 if ((failed = _AFPFSSetLock(context->filePath, pFile,
2192 RESERVED_BYTE, 1, 0))) {
drhbfe66312006-10-03 17:40:40 +00002193 /* failed to release the reserved lock */
aswift5b1a2562008-08-22 00:22:35 +00002194 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002195 }
2196 }
2197 }
2198 if( locktype==NO_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002199 int failed = _AFPFSSetLock(context->filePath, pFile,
drhbfe66312006-10-03 17:40:40 +00002200 SHARED_FIRST + context->sharedLockByte, 1, 0);
2201 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002202 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002203 }
2204 }
2205 if (rc == SQLITE_OK)
2206 pFile->locktype = locktype;
danielk1977b4b47412007-08-17 15:53:36 +00002207 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002208 return rc;
2209}
2210
2211/*
drh339eb0b2008-03-07 15:34:11 +00002212** Close a file & cleanup AFP specific locking context
2213*/
danielk1977e339d652008-06-28 11:23:00 +00002214static int afpClose(sqlite3_file *id) {
2215 if( id ){
2216 unixFile *pFile = (unixFile*)id;
2217 afpUnlock(id, NO_LOCK);
2218 sqlite3_free(pFile->lockingContext);
2219 }
2220 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002221}
2222
2223
2224#pragma mark flock() style locking
2225
2226/*
drh339eb0b2008-03-07 15:34:11 +00002227** The flockLockingContext is not used
2228*/
drhbfe66312006-10-03 17:40:40 +00002229typedef void flockLockingContext;
2230
aswift5b1a2562008-08-22 00:22:35 +00002231/* flock-style reserved lock checking following the behavior of
2232 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002233static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002234 int rc = SQLITE_OK;
2235 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002236 unixFile *pFile = (unixFile*)id;
2237
aswift5b1a2562008-08-22 00:22:35 +00002238 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2239
2240 assert( pFile );
2241
2242 /* Check if a thread in this process holds such a lock */
2243 if( pFile->locktype>SHARED_LOCK ){
2244 reserved = 1;
2245 }
2246
2247 /* Otherwise see if some other process holds it. */
2248 if( !reserved ){
drh3b62b2f2007-06-08 18:27:03 +00002249 /* attempt to get the lock */
aswift5b1a2562008-08-22 00:22:35 +00002250 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
2251 if( !lrc ){
drh3b62b2f2007-06-08 18:27:03 +00002252 /* got the lock, unlock it */
aswift5b1a2562008-08-22 00:22:35 +00002253 lrc = flock(pFile->h, LOCK_UN);
2254 if ( lrc ) {
2255 int tErrno = errno;
2256 /* unlock failed with an error */
2257 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2258 if( IS_LOCK_ERROR(lrc) ){
2259 pFile->lastErrno = tErrno;
2260 rc = lrc;
2261 }
2262 }
2263 } else {
2264 int tErrno = errno;
2265 reserved = 1;
2266 /* someone else might have it reserved */
2267 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2268 if( IS_LOCK_ERROR(lrc) ){
2269 pFile->lastErrno = tErrno;
2270 rc = lrc;
2271 }
drhbfe66312006-10-03 17:40:40 +00002272 }
drhbfe66312006-10-03 17:40:40 +00002273 }
aswift5b1a2562008-08-22 00:22:35 +00002274 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002275
aswift5b1a2562008-08-22 00:22:35 +00002276 *pResOut = reserved;
2277 return rc;
drhbfe66312006-10-03 17:40:40 +00002278}
2279
danielk1977e339d652008-06-28 11:23:00 +00002280static int flockLock(sqlite3_file *id, int locktype) {
aswift5b1a2562008-08-22 00:22:35 +00002281 int rc = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002282 unixFile *pFile = (unixFile*)id;
aswift5b1a2562008-08-22 00:22:35 +00002283
2284 assert( pFile );
2285
drh3b62b2f2007-06-08 18:27:03 +00002286 /* if we already have a lock, it is exclusive.
2287 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002288 if (pFile->locktype > NO_LOCK) {
2289 pFile->locktype = locktype;
2290 return SQLITE_OK;
2291 }
2292
drh3b62b2f2007-06-08 18:27:03 +00002293 /* grab an exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002294
2295 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
2296 int tErrno = errno;
drh3b62b2f2007-06-08 18:27:03 +00002297 /* didn't get, must be busy */
aswift5b1a2562008-08-22 00:22:35 +00002298 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2299 if( IS_LOCK_ERROR(rc) ){
2300 pFile->lastErrno = tErrno;
2301 }
drhbfe66312006-10-03 17:40:40 +00002302 } else {
drh3b62b2f2007-06-08 18:27:03 +00002303 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002304 pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002305 }
aswift5b1a2562008-08-22 00:22:35 +00002306 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
2307 rc==SQLITE_OK ? "ok" : "failed");
2308 return rc;
drhbfe66312006-10-03 17:40:40 +00002309}
2310
danielk1977e339d652008-06-28 11:23:00 +00002311static int flockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002312 unixFile *pFile = (unixFile*)id;
2313
aswift5b1a2562008-08-22 00:22:35 +00002314 assert( pFile );
2315 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2316 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002317 assert( locktype<=SHARED_LOCK );
2318
drh3b62b2f2007-06-08 18:27:03 +00002319 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002320 if( pFile->locktype==locktype ){
2321 return SQLITE_OK;
2322 }
2323
drh3b62b2f2007-06-08 18:27:03 +00002324 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002325 if (locktype==SHARED_LOCK) {
2326 pFile->locktype = locktype;
2327 return SQLITE_OK;
2328 }
2329
drh3b62b2f2007-06-08 18:27:03 +00002330 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00002331 int rc = flock(pFile->h, LOCK_UN);
aswift5b1a2562008-08-22 00:22:35 +00002332 if (rc) {
2333 int r, tErrno = errno;
2334 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2335 if( IS_LOCK_ERROR(r) ){
2336 pFile->lastErrno = tErrno;
2337 }
2338 return r;
2339 } else {
drhbfe66312006-10-03 17:40:40 +00002340 pFile->locktype = NO_LOCK;
2341 return SQLITE_OK;
2342 }
2343}
2344
2345/*
drh339eb0b2008-03-07 15:34:11 +00002346** Close a file.
2347*/
danielk1977e339d652008-06-28 11:23:00 +00002348static int flockClose(sqlite3_file *id) {
2349 if( id ){
2350 flockUnlock(id, NO_LOCK);
2351 }
2352 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002353}
2354
chw97185482008-11-17 08:05:31 +00002355#endif /* !defined(__RTP__) && !defined(_WRS_KERNEL) */
2356
drhbfe66312006-10-03 17:40:40 +00002357#pragma mark Old-School .lock file based locking
2358
aswift5b1a2562008-08-22 00:22:35 +00002359/* Dotlock-style reserved lock checking following the behavior of
2360** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002361static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
aswift5b1a2562008-08-22 00:22:35 +00002362 int rc = SQLITE_OK;
2363 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002364 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002365
aswift5b1a2562008-08-22 00:22:35 +00002366 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2367
2368 assert( pFile );
2369
2370 /* Check if a thread in this process holds such a lock */
2371 if( pFile->locktype>SHARED_LOCK ){
2372 reserved = 1;
2373 }
2374
2375 /* Otherwise see if some other process holds it. */
2376 if( !reserved ){
2377 char *zLockFile = (char *)pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002378 struct stat statBuf;
aswift5b1a2562008-08-22 00:22:35 +00002379
2380 if( lstat(zLockFile, &statBuf)==0 ){
2381 /* file exists, someone else has the lock */
2382 reserved = 1;
2383 }else{
drh3b62b2f2007-06-08 18:27:03 +00002384 /* file does not exist, we could have it if we want it */
chw97185482008-11-17 08:05:31 +00002385 int tErrno = errno;
aswift5b1a2562008-08-22 00:22:35 +00002386 if( ENOENT != tErrno ){
2387 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2388 pFile->lastErrno = tErrno;
2389 }
drh339eb0b2008-03-07 15:34:11 +00002390 }
drhbfe66312006-10-03 17:40:40 +00002391 }
aswift5b1a2562008-08-22 00:22:35 +00002392 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002393
aswift5b1a2562008-08-22 00:22:35 +00002394 *pResOut = reserved;
2395 return rc;
drhbfe66312006-10-03 17:40:40 +00002396}
2397
danielk1977e339d652008-06-28 11:23:00 +00002398static int dotlockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002399 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002400 int fd;
danielk1977e339d652008-06-28 11:23:00 +00002401 char *zLockFile = (char *)pFile->lockingContext;
aswift5b1a2562008-08-22 00:22:35 +00002402 int rc=SQLITE_OK;
drh339eb0b2008-03-07 15:34:11 +00002403
drh3b62b2f2007-06-08 18:27:03 +00002404 /* if we already have a lock, it is exclusive.
2405 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002406 if (pFile->locktype > NO_LOCK) {
2407 pFile->locktype = locktype;
chw97185482008-11-17 08:05:31 +00002408#if !defined(__RTP__) && !defined(_WRS_KERNEL)
drhbfe66312006-10-03 17:40:40 +00002409 /* Always update the timestamp on the old file */
danielk1977e339d652008-06-28 11:23:00 +00002410 utimes(zLockFile, NULL);
chw97185482008-11-17 08:05:31 +00002411#endif
aswift5b1a2562008-08-22 00:22:35 +00002412 rc = SQLITE_OK;
2413 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002414 }
2415
drh3b62b2f2007-06-08 18:27:03 +00002416 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00002417 struct stat statBuf;
danielk1977e339d652008-06-28 11:23:00 +00002418 if (lstat(zLockFile,&statBuf) == 0){
aswift5b1a2562008-08-22 00:22:35 +00002419 rc = SQLITE_BUSY; /* it does, busy */
2420 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002421 }
2422
drh3b62b2f2007-06-08 18:27:03 +00002423 /* grab an exclusive lock */
danielk1977e339d652008-06-28 11:23:00 +00002424 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
drh339eb0b2008-03-07 15:34:11 +00002425 if( fd<0 ){
drh3b62b2f2007-06-08 18:27:03 +00002426 /* failed to open/create the file, someone else may have stolen the lock */
aswift5b1a2562008-08-22 00:22:35 +00002427 int tErrno = errno;
2428 if( EEXIST == tErrno ){
2429 rc = SQLITE_BUSY;
2430 } else {
2431 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2432 if( IS_LOCK_ERROR(rc) ){
2433 pFile->lastErrno = tErrno;
2434 }
2435 }
2436 goto dotlock_end_lock;
2437 }
drhbfe66312006-10-03 17:40:40 +00002438 close(fd);
2439
drh3b62b2f2007-06-08 18:27:03 +00002440 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002441 pFile->locktype = locktype;
aswift5b1a2562008-08-22 00:22:35 +00002442
2443 dotlock_end_lock:
2444 return rc;
drhbfe66312006-10-03 17:40:40 +00002445}
2446
danielk1977e339d652008-06-28 11:23:00 +00002447static int dotlockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002448 unixFile *pFile = (unixFile*)id;
danielk1977e339d652008-06-28 11:23:00 +00002449 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00002450
aswift5b1a2562008-08-22 00:22:35 +00002451 assert( pFile );
2452 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2453 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002454 assert( locktype<=SHARED_LOCK );
2455
drh3b62b2f2007-06-08 18:27:03 +00002456 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002457 if( pFile->locktype==locktype ){
2458 return SQLITE_OK;
2459 }
2460
drh3b62b2f2007-06-08 18:27:03 +00002461 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002462 if (locktype==SHARED_LOCK) {
2463 pFile->locktype = locktype;
2464 return SQLITE_OK;
2465 }
2466
drh3b62b2f2007-06-08 18:27:03 +00002467 /* no, really, unlock. */
aswift5b1a2562008-08-22 00:22:35 +00002468 if (unlink(zLockFile) ) {
2469 int rc, tErrno = errno;
2470 if( ENOENT != tErrno ){
2471 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2472 }
2473 if( IS_LOCK_ERROR(rc) ){
2474 pFile->lastErrno = tErrno;
2475 }
2476 return rc;
2477 }
drhbfe66312006-10-03 17:40:40 +00002478 pFile->locktype = NO_LOCK;
2479 return SQLITE_OK;
2480}
2481
2482/*
2483 ** Close a file.
2484 */
danielk1977e339d652008-06-28 11:23:00 +00002485static int dotlockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00002486#if defined(__RTP__) || defined(_WRS_KERNEL)
2487 int rc;
2488#endif
danielk1977e339d652008-06-28 11:23:00 +00002489 if( id ){
2490 unixFile *pFile = (unixFile*)id;
2491 dotlockUnlock(id, NO_LOCK);
2492 sqlite3_free(pFile->lockingContext);
2493 }
chw97185482008-11-17 08:05:31 +00002494#if defined(__RTP__) || defined(_WRS_KERNEL)
2495 enterMutex();
2496 rc = closeUnixFile(id);
2497 leaveMutex();
2498 return rc;
2499#else
danielk1977e339d652008-06-28 11:23:00 +00002500 return closeUnixFile(id);
chw97185482008-11-17 08:05:31 +00002501#endif
drhbfe66312006-10-03 17:40:40 +00002502}
2503
chw97185482008-11-17 08:05:31 +00002504#if defined(__RTP__) || defined(_WRS_KERNEL)
2505
2506#pragma mark POSIX/vxWorks named semaphore based locking
2507
2508/* Namedsem-style reserved lock checking following the behavior of
2509** unixCheckReservedLock, see the unixCheckReservedLock function comments */
2510static int namedsemCheckReservedLock(sqlite3_file *id, int *pResOut) {
2511 int rc = SQLITE_OK;
2512 int reserved = 0;
2513 unixFile *pFile = (unixFile*)id;
2514
2515 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2516
2517 assert( pFile );
2518
2519 /* Check if a thread in this process holds such a lock */
2520 if( pFile->locktype>SHARED_LOCK ){
2521 reserved = 1;
2522 }
2523
2524 /* Otherwise see if some other process holds it. */
2525 if( !reserved ){
2526 sem_t *pSem = pFile->pOpen->pSem;
2527 struct stat statBuf;
2528
2529 if( sem_trywait(pSem)==-1 ){
2530 int tErrno = errno;
2531 if( EAGAIN != tErrno ){
2532 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2533 pFile->lastErrno = tErrno;
2534 } else {
2535 /* someone else has the lock when we are in NO_LOCK */
2536 reserved = (pFile->locktype < SHARED_LOCK);
2537 }
2538 }else{
2539 /* we could have it if we want it */
2540 sem_post(pSem);
2541 }
2542 }
2543 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2544
2545 *pResOut = reserved;
2546 return rc;
2547}
2548
2549static int namedsemLock(sqlite3_file *id, int locktype) {
2550 unixFile *pFile = (unixFile*)id;
2551 int fd;
2552 sem_t *pSem = pFile->pOpen->pSem;
2553 int rc = SQLITE_OK;
2554
2555 /* if we already have a lock, it is exclusive.
2556 ** Just adjust level and punt on outta here. */
2557 if (pFile->locktype > NO_LOCK) {
2558 pFile->locktype = locktype;
2559 rc = SQLITE_OK;
2560 goto namedsem_end_lock;
2561 }
2562
2563 /* lock semaphore now but bail out when already locked. */
2564 if( sem_trywait(pSem)==-1 ){
2565 rc = SQLITE_BUSY;
2566 goto namedsem_end_lock;
2567 }
2568
2569 /* got it, set the type and return ok */
2570 pFile->locktype = locktype;
2571
2572 namedsem_end_lock:
2573 return rc;
2574}
2575
2576static int namedsemUnlock(sqlite3_file *id, int locktype) {
2577 unixFile *pFile = (unixFile*)id;
2578 sem_t *pSem = pFile->pOpen->pSem;
2579
2580 assert( pFile );
2581 assert( pSem );
2582 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2583 pFile->locktype, getpid());
2584 assert( locktype<=SHARED_LOCK );
2585
2586 /* no-op if possible */
2587 if( pFile->locktype==locktype ){
2588 return SQLITE_OK;
2589 }
2590
2591 /* shared can just be set because we always have an exclusive */
2592 if (locktype==SHARED_LOCK) {
2593 pFile->locktype = locktype;
2594 return SQLITE_OK;
2595 }
2596
2597 /* no, really unlock. */
2598 if ( sem_post(pSem)==-1 ) {
2599 int rc, tErrno = errno;
2600 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2601 if( IS_LOCK_ERROR(rc) ){
2602 pFile->lastErrno = tErrno;
2603 }
2604 return rc;
2605 }
2606 pFile->locktype = NO_LOCK;
2607 return SQLITE_OK;
2608}
2609
2610/*
2611 ** Close a file.
2612 */
2613static int namedsemClose(sqlite3_file *id) {
2614 if( id ){
2615 unixFile *pFile = (unixFile*)id;
2616 namedsemUnlock(id, NO_LOCK);
2617 assert( pFile );
2618 enterMutex();
2619 releaseLockInfo(pFile->pLock);
2620 releaseOpenCnt(pFile->pOpen);
2621 closeUnixFile(id);
2622 leaveMutex();
2623 }
2624 return SQLITE_OK;
2625}
2626
2627#endif /* defined(__RTP__) || defined(_WRS_KERNEL) */
drhbfe66312006-10-03 17:40:40 +00002628
drhda0e7682008-07-30 15:27:54 +00002629#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +00002630
2631/*
drh339eb0b2008-03-07 15:34:11 +00002632** The nolockLockingContext is void
2633*/
drhbfe66312006-10-03 17:40:40 +00002634typedef void nolockLockingContext;
2635
danielk1977397d65f2008-11-19 11:35:39 +00002636static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
2637 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00002638 *pResOut = 0;
2639 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002640}
2641
danielk1977397d65f2008-11-19 11:35:39 +00002642static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
2643 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbfe66312006-10-03 17:40:40 +00002644 return SQLITE_OK;
2645}
2646
danielk1977397d65f2008-11-19 11:35:39 +00002647static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
2648 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbfe66312006-10-03 17:40:40 +00002649 return SQLITE_OK;
2650}
2651
2652/*
drh339eb0b2008-03-07 15:34:11 +00002653** Close a file.
2654*/
danielk1977e339d652008-06-28 11:23:00 +00002655static int nolockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00002656 int rc;
danielk1977397d65f2008-11-19 11:35:39 +00002657 if( IS_VXWORKS ) enterMutex();
chw97185482008-11-17 08:05:31 +00002658 rc = closeUnixFile(id);
danielk1977397d65f2008-11-19 11:35:39 +00002659 if( IS_VXWORKS ) leaveMutex();
chw97185482008-11-17 08:05:31 +00002660 return rc;
drhbfe66312006-10-03 17:40:40 +00002661}
2662
danielk1977ad94b582007-08-20 06:44:22 +00002663
danielk1977e3026632004-06-22 11:29:02 +00002664/*
drh9e33c2c2007-08-31 18:34:59 +00002665** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00002666*/
drhcc6bb3e2007-08-31 16:11:35 +00002667static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00002668 switch( op ){
2669 case SQLITE_FCNTL_LOCKSTATE: {
2670 *(int*)pArg = ((unixFile*)id)->locktype;
2671 return SQLITE_OK;
2672 }
2673 }
drhcc6bb3e2007-08-31 16:11:35 +00002674 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00002675}
2676
2677/*
danielk1977a3d4c882007-03-23 10:08:38 +00002678** Return the sector size in bytes of the underlying block device for
2679** the specified file. This is almost always 512 bytes, but may be
2680** larger for some devices.
2681**
2682** SQLite code assumes this function cannot fail. It also assumes that
2683** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00002684** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00002685** same for both.
2686*/
danielk1977397d65f2008-11-19 11:35:39 +00002687static int unixSectorSize(sqlite3_file *NotUsed){
2688 UNUSED_PARAMETER(NotUsed);
drh3ceeb752007-03-29 18:19:52 +00002689 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002690}
2691
danielk197790949c22007-08-17 16:50:38 +00002692/*
danielk1977397d65f2008-11-19 11:35:39 +00002693** Return the device characteristics for the file. This is always 0 for unix.
danielk197790949c22007-08-17 16:50:38 +00002694*/
danielk1977397d65f2008-11-19 11:35:39 +00002695static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
2696 UNUSED_PARAMETER(NotUsed);
danielk197762079062007-08-15 17:08:46 +00002697 return 0;
2698}
2699
danielk1977a3d4c882007-03-23 10:08:38 +00002700/*
danielk1977e339d652008-06-28 11:23:00 +00002701** Initialize the contents of the unixFile structure pointed to by pId.
2702**
danielk1977ad94b582007-08-20 06:44:22 +00002703** When locking extensions are enabled, the filepath and locking style
2704** are needed to determine the unixFile pMethod to use for locking operations.
2705** The locking-style specific lockingContext data structure is created
2706** and assigned here also.
2707*/
2708static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00002709 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00002710 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00002711 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00002712 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00002713 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00002714 int noLock, /* Omit locking if true */
2715 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00002716){
drhda0e7682008-07-30 15:27:54 +00002717 int eLockingStyle;
2718 unixFile *pNew = (unixFile *)pId;
2719 int rc = SQLITE_OK;
2720
danielk1977e339d652008-06-28 11:23:00 +00002721 /* Macro to define the static contents of an sqlite3_io_methods
2722 ** structure for a unix backend file. Different locking methods
2723 ** require different functions for the xClose, xLock, xUnlock and
2724 ** xCheckReservedLock methods.
2725 */
2726 #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \
2727 1, /* iVersion */ \
2728 xClose, /* xClose */ \
2729 unixRead, /* xRead */ \
2730 unixWrite, /* xWrite */ \
2731 unixTruncate, /* xTruncate */ \
2732 unixSync, /* xSync */ \
2733 unixFileSize, /* xFileSize */ \
2734 xLock, /* xLock */ \
2735 xUnlock, /* xUnlock */ \
2736 xCheckReservedLock, /* xCheckReservedLock */ \
2737 unixFileControl, /* xFileControl */ \
2738 unixSectorSize, /* xSectorSize */ \
2739 unixDeviceCharacteristics /* xDeviceCapabilities */ \
2740 }
2741 static sqlite3_io_methods aIoMethod[] = {
2742 IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
danielk1977e339d652008-06-28 11:23:00 +00002743 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
drh40bbb0a2008-09-23 10:23:26 +00002744#if SQLITE_ENABLE_LOCKING_STYLE
drhda0e7682008-07-30 15:27:54 +00002745 ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
chw97185482008-11-17 08:05:31 +00002746#if defined(__RTP__) || defined(_WRS_KERNEL)
2747 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2748 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2749 ,IOMETHODS(namedsemClose, namedsemLock, namedsemUnlock, namedsemCheckReservedLock)
2750#else
drhda0e7682008-07-30 15:27:54 +00002751 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
danielk1977e339d652008-06-28 11:23:00 +00002752 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
chw97185482008-11-17 08:05:31 +00002753 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2754#endif
drh218c5082008-03-07 00:27:10 +00002755#endif
danielk1977e339d652008-06-28 11:23:00 +00002756 };
drhda0e7682008-07-30 15:27:54 +00002757 /* The order of the IOMETHODS macros above is important. It must be the
2758 ** same order as the LOCKING_STYLE numbers
2759 */
2760 assert(LOCKING_STYLE_POSIX==1);
2761 assert(LOCKING_STYLE_NONE==2);
2762 assert(LOCKING_STYLE_DOTFILE==3);
2763 assert(LOCKING_STYLE_FLOCK==4);
2764 assert(LOCKING_STYLE_AFP==5);
chw97185482008-11-17 08:05:31 +00002765 assert(LOCKING_STYLE_NAMEDSEM==6);
drh218c5082008-03-07 00:27:10 +00002766
danielk197717b90b52008-06-06 11:11:25 +00002767 assert( pNew->pLock==NULL );
2768 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00002769
2770 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00002771 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00002772 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00002773 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00002774
chw97185482008-11-17 08:05:31 +00002775#if defined(__RTP__) || defined(_WRS_KERNEL)
2776 {
2777 HashElem *pElem;
2778 char *zRealname = vxrealpath(zFilename, 1);
2779 int n;
2780 pNew->zRealpath = 0;
2781 if( !zRealname ){
2782 rc = SQLITE_NOMEM;
2783 eLockingStyle = LOCKING_STYLE_NONE;
2784 }else{
2785 n = strlen(zRealname) + 1;
2786 enterMutex();
2787 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
2788 if( pElem ){
2789 long cnt = (long)pElem->data;
2790 cnt++;
2791 pNew->zRealpath = pElem->pKey;
2792 pElem->data = (void*)cnt;
2793 }else{
2794 if( sqlite3HashInsert(&nameHash, zRealname, n, (void*)1)==0 ){
2795 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
2796 if( pElem ){
2797 pNew->zRealpath = pElem->pKey;
2798 }else{
2799 sqlite3HashInsert(&nameHash, zRealname, n, 0);
2800 rc = SQLITE_NOMEM;
2801 eLockingStyle = LOCKING_STYLE_NONE;
2802 }
2803 }
2804 }
2805 leaveMutex();
2806 sqlite3_free(zRealname);
2807 }
2808 }
2809#endif
2810
drhda0e7682008-07-30 15:27:54 +00002811 if( noLock ){
2812 eLockingStyle = LOCKING_STYLE_NONE;
2813 }else{
2814 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
2815 }
danielk1977e339d652008-06-28 11:23:00 +00002816
2817 switch( eLockingStyle ){
2818
2819 case LOCKING_STYLE_POSIX: {
2820 enterMutex();
chw97185482008-11-17 08:05:31 +00002821#if defined(__RTP__) || defined(_WRS_KERNEL)
2822 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
2823#else
danielk1977e339d652008-06-28 11:23:00 +00002824 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
chw97185482008-11-17 08:05:31 +00002825#endif
danielk1977e339d652008-06-28 11:23:00 +00002826 leaveMutex();
drh218c5082008-03-07 00:27:10 +00002827 break;
drhbfe66312006-10-03 17:40:40 +00002828 }
danielk1977e339d652008-06-28 11:23:00 +00002829
drh40bbb0a2008-09-23 10:23:26 +00002830#if SQLITE_ENABLE_LOCKING_STYLE
chw97185482008-11-17 08:05:31 +00002831
2832#if !defined(__RTP__) && !defined(_WRS_KERNEL)
danielk1977e339d652008-06-28 11:23:00 +00002833 case LOCKING_STYLE_AFP: {
2834 /* AFP locking uses the file path so it needs to be included in
2835 ** the afpLockingContext.
2836 */
2837 afpLockingContext *pCtx;
2838 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
2839 if( pCtx==0 ){
2840 rc = SQLITE_NOMEM;
2841 }else{
2842 /* NB: zFilename exists and remains valid until the file is closed
2843 ** according to requirement F11141. So we do not need to make a
2844 ** copy of the filename. */
2845 pCtx->filePath = zFilename;
2846 srandomdev();
2847 }
drh218c5082008-03-07 00:27:10 +00002848 break;
danielk1977e339d652008-06-28 11:23:00 +00002849 }
chw97185482008-11-17 08:05:31 +00002850#endif
danielk1977e339d652008-06-28 11:23:00 +00002851
2852 case LOCKING_STYLE_DOTFILE: {
2853 /* Dotfile locking uses the file path so it needs to be included in
2854 ** the dotlockLockingContext
2855 */
2856 char *zLockFile;
drh218c5082008-03-07 00:27:10 +00002857 int nFilename;
danielk1977e339d652008-06-28 11:23:00 +00002858 nFilename = strlen(zFilename) + 6;
2859 zLockFile = (char *)sqlite3_malloc(nFilename);
2860 if( zLockFile==0 ){
2861 rc = SQLITE_NOMEM;
2862 }else{
2863 sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
drh339eb0b2008-03-07 15:34:11 +00002864 }
danielk1977e339d652008-06-28 11:23:00 +00002865 pNew->lockingContext = zLockFile;
drh218c5082008-03-07 00:27:10 +00002866 break;
2867 }
danielk1977e339d652008-06-28 11:23:00 +00002868
chw97185482008-11-17 08:05:31 +00002869#if defined(__RTP__) || defined(_WRS_KERNEL)
2870 case LOCKING_STYLE_NAMEDSEM: {
2871 /* Named semaphore locking uses the file path so it needs to be
2872 ** included in the namedsemLockingContext
2873 */
2874 enterMutex();
2875 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
2876 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
2877 char *zSemName = pNew->pOpen->aSemName;
2878 int n;
2879 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem", pNew->zRealpath);
2880 for( n=0; zSemName[n]; n++ )
2881 if( zSemName[n]=='/' ) zSemName[n] = '_';
2882 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
2883 if( pNew->pOpen->pSem == SEM_FAILED ){
2884 rc = SQLITE_NOMEM;
2885 pNew->pOpen->aSemName[0] = '\0';
2886 }
2887 }
2888 leaveMutex();
2889 break;
2890 }
2891#endif
2892
2893#if !defined(__RTP__) && !defined(_WRS_KERNEL)
danielk1977e339d652008-06-28 11:23:00 +00002894 case LOCKING_STYLE_FLOCK:
chw97185482008-11-17 08:05:31 +00002895#endif
danielk1977e339d652008-06-28 11:23:00 +00002896 case LOCKING_STYLE_NONE:
drh218c5082008-03-07 00:27:10 +00002897 break;
drhe78669b2007-06-29 12:04:26 +00002898#endif
danielk1977e339d652008-06-28 11:23:00 +00002899 }
aswift5b1a2562008-08-22 00:22:35 +00002900
2901 pNew->lastErrno = 0;
chw97185482008-11-17 08:05:31 +00002902#if defined(__RTP__) || defined(_WRS_KERNEL)
2903 if( rc!=SQLITE_OK ){
2904 unlink(zFilename);
2905 isDelete = 0;
2906 }
2907 pNew->isDelete = isDelete;
2908#endif
danielk1977e339d652008-06-28 11:23:00 +00002909 if( rc!=SQLITE_OK ){
danielk19777c055b92007-10-30 17:28:51 +00002910 if( dirfd>=0 ) close(dirfd);
drhbfe66312006-10-03 17:40:40 +00002911 close(h);
danielk1977e339d652008-06-28 11:23:00 +00002912 }else{
danielk19776cb427f2008-06-30 10:16:04 +00002913 pNew->pMethod = &aIoMethod[eLockingStyle-1];
danielk1977e339d652008-06-28 11:23:00 +00002914 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00002915 }
danielk1977e339d652008-06-28 11:23:00 +00002916 return rc;
drh054889e2005-11-30 03:20:31 +00002917}
drh9c06c952005-11-26 00:25:00 +00002918
danielk1977ad94b582007-08-20 06:44:22 +00002919/*
2920** Open a file descriptor to the directory containing file zFilename.
2921** If successful, *pFd is set to the opened file descriptor and
2922** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2923** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2924** value.
2925**
2926** If SQLITE_OK is returned, the caller is responsible for closing
2927** the file descriptor *pFd using close().
2928*/
danielk1977fee2d252007-08-18 10:59:19 +00002929static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00002930 int ii;
drh777b17a2007-09-20 10:02:54 +00002931 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00002932 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00002933
drh153c62c2007-08-24 03:51:33 +00002934 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00002935 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2936 if( ii>0 ){
2937 zDirname[ii] = '\0';
2938 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00002939 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002940#ifdef FD_CLOEXEC
2941 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2942#endif
2943 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2944 }
2945 }
danielk1977fee2d252007-08-18 10:59:19 +00002946 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00002947 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00002948}
2949
danielk1977b4b47412007-08-17 15:53:36 +00002950/*
danielk197717b90b52008-06-06 11:11:25 +00002951** Create a temporary file name in zBuf. zBuf must be allocated
2952** by the calling process and must be big enough to hold at least
2953** pVfs->mxPathname bytes.
2954*/
2955static int getTempname(int nBuf, char *zBuf){
2956 static const char *azDirs[] = {
2957 0,
2958 "/var/tmp",
2959 "/usr/tmp",
2960 "/tmp",
2961 ".",
2962 };
2963 static const unsigned char zChars[] =
2964 "abcdefghijklmnopqrstuvwxyz"
2965 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2966 "0123456789";
2967 int i, j;
2968 struct stat buf;
2969 const char *zDir = ".";
2970
2971 /* It's odd to simulate an io-error here, but really this is just
2972 ** using the io-error infrastructure to test that SQLite handles this
2973 ** function failing.
2974 */
2975 SimulateIOError( return SQLITE_IOERR );
2976
2977 azDirs[0] = sqlite3_temp_directory;
danielk197700e13612008-11-17 19:18:54 +00002978 for(i=0; i<ArraySize(azDirs); i++){
danielk197717b90b52008-06-06 11:11:25 +00002979 if( azDirs[i]==0 ) continue;
2980 if( stat(azDirs[i], &buf) ) continue;
2981 if( !S_ISDIR(buf.st_mode) ) continue;
2982 if( access(azDirs[i], 07) ) continue;
2983 zDir = azDirs[i];
2984 break;
2985 }
2986
2987 /* Check that the output buffer is large enough for the temporary file
2988 ** name. If it is not, return SQLITE_ERROR.
2989 */
danielk197700e13612008-11-17 19:18:54 +00002990 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00002991 return SQLITE_ERROR;
2992 }
2993
2994 do{
2995 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
2996 j = strlen(zBuf);
2997 sqlite3_randomness(15, &zBuf[j]);
2998 for(i=0; i<15; i++, j++){
2999 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3000 }
3001 zBuf[j] = 0;
3002 }while( access(zBuf,0)==0 );
3003 return SQLITE_OK;
3004}
3005
3006
3007/*
danielk1977ad94b582007-08-20 06:44:22 +00003008** Open the file zPath.
3009**
danielk1977b4b47412007-08-17 15:53:36 +00003010** Previously, the SQLite OS layer used three functions in place of this
3011** one:
3012**
3013** sqlite3OsOpenReadWrite();
3014** sqlite3OsOpenReadOnly();
3015** sqlite3OsOpenExclusive();
3016**
3017** These calls correspond to the following combinations of flags:
3018**
3019** ReadWrite() -> (READWRITE | CREATE)
3020** ReadOnly() -> (READONLY)
3021** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
3022**
3023** The old OpenExclusive() accepted a boolean argument - "delFlag". If
3024** true, the file was configured to be automatically deleted when the
3025** file handle closed. To achieve the same effect using this new
3026** interface, add the DELETEONCLOSE flag to those specified above for
3027** OpenExclusive().
3028*/
3029static int unixOpen(
drh153c62c2007-08-24 03:51:33 +00003030 sqlite3_vfs *pVfs,
danielk1977b4b47412007-08-17 15:53:36 +00003031 const char *zPath,
3032 sqlite3_file *pFile,
3033 int flags,
3034 int *pOutFlags
3035){
danielk1977fee2d252007-08-18 10:59:19 +00003036 int fd = 0; /* File descriptor returned by open() */
3037 int dirfd = -1; /* Directory file descriptor */
3038 int oflags = 0; /* Flags to pass to open() */
3039 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00003040 int noLock; /* True to omit locking primitives */
danielk1977b4b47412007-08-17 15:53:36 +00003041
3042 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3043 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3044 int isCreate = (flags & SQLITE_OPEN_CREATE);
3045 int isReadonly = (flags & SQLITE_OPEN_READONLY);
3046 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3047
danielk1977fee2d252007-08-18 10:59:19 +00003048 /* If creating a master or main-file journal, this function will open
3049 ** a file-descriptor on the directory too. The first time unixSync()
3050 ** is called the directory file descriptor will be fsync()ed and close()d.
3051 */
3052 int isOpenDirectory = (isCreate &&
3053 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
3054 );
3055
danielk197717b90b52008-06-06 11:11:25 +00003056 /* If argument zPath is a NULL pointer, this function is required to open
3057 ** a temporary file. Use this buffer to store the file name in.
3058 */
3059 char zTmpname[MAX_PATHNAME+1];
3060 const char *zName = zPath;
3061
danielk1977fee2d252007-08-18 10:59:19 +00003062 /* Check the following statements are true:
3063 **
3064 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3065 ** (b) if CREATE is set, then READWRITE must also be set, and
3066 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00003067 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00003068 */
danielk1977b4b47412007-08-17 15:53:36 +00003069 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00003070 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00003071 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00003072 assert(isDelete==0 || isCreate);
3073
drh33f4e022007-09-03 15:19:34 +00003074 /* The main DB, main journal, and master journal are never automatically
3075 ** deleted
3076 */
3077 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
3078 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
3079 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00003080
danielk1977fee2d252007-08-18 10:59:19 +00003081 /* Assert that the upper layer has set one of the "file-type" flags. */
3082 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
3083 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
3084 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00003085 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00003086 );
3087
danielk1977e339d652008-06-28 11:23:00 +00003088 memset(pFile, 0, sizeof(unixFile));
3089
danielk197717b90b52008-06-06 11:11:25 +00003090 if( !zName ){
3091 int rc;
3092 assert(isDelete && !isOpenDirectory);
3093 rc = getTempname(MAX_PATHNAME+1, zTmpname);
3094 if( rc!=SQLITE_OK ){
3095 return rc;
3096 }
3097 zName = zTmpname;
3098 }
3099
danielk1977b4b47412007-08-17 15:53:36 +00003100 if( isReadonly ) oflags |= O_RDONLY;
3101 if( isReadWrite ) oflags |= O_RDWR;
3102 if( isCreate ) oflags |= O_CREAT;
3103 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
3104 oflags |= (O_LARGEFILE|O_BINARY);
3105
danielk197717b90b52008-06-06 11:11:25 +00003106 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
chw97185482008-11-17 08:05:31 +00003107 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, oflags);
danielk19772f2d8c72007-08-30 16:13:33 +00003108 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00003109 /* Failed to open the file for read/write access. Try read-only. */
3110 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
3111 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00003112 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00003113 }
3114 if( fd<0 ){
3115 return SQLITE_CANTOPEN;
3116 }
3117 if( isDelete ){
chw97185482008-11-17 08:05:31 +00003118#if defined(__RTP__) || defined(_WRS_KERNEL)
3119 zPath = zName;
3120#else
danielk197717b90b52008-06-06 11:11:25 +00003121 unlink(zName);
chw97185482008-11-17 08:05:31 +00003122#endif
danielk1977b4b47412007-08-17 15:53:36 +00003123 }
3124 if( pOutFlags ){
3125 *pOutFlags = flags;
3126 }
3127
3128 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00003129 if( isOpenDirectory ){
3130 int rc = openDirectory(zPath, &dirfd);
3131 if( rc!=SQLITE_OK ){
3132 close(fd);
3133 return rc;
3134 }
3135 }
danielk1977e339d652008-06-28 11:23:00 +00003136
3137#ifdef FD_CLOEXEC
3138 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3139#endif
3140
drhda0e7682008-07-30 15:27:54 +00003141 noLock = eType!=SQLITE_OPEN_MAIN_DB;
chw97185482008-11-17 08:05:31 +00003142 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00003143}
3144
3145/*
danielk1977fee2d252007-08-18 10:59:19 +00003146** Delete the file at zPath. If the dirSync argument is true, fsync()
3147** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00003148*/
danielk1977397d65f2008-11-19 11:35:39 +00003149static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){
danielk1977fee2d252007-08-18 10:59:19 +00003150 int rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00003151 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003152 SimulateIOError(return SQLITE_IOERR_DELETE);
3153 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00003154#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00003155 if( dirSync ){
3156 int fd;
3157 rc = openDirectory(zPath, &fd);
3158 if( rc==SQLITE_OK ){
chw97185482008-11-17 08:05:31 +00003159#if defined(__RTP__) || defined(_WRS_KERNEL)
3160 if( fsync(fd)==-1 )
3161#else
3162 if( fsync(fd) )
3163#endif
3164 {
danielk1977fee2d252007-08-18 10:59:19 +00003165 rc = SQLITE_IOERR_DIR_FSYNC;
3166 }
3167 close(fd);
3168 }
3169 }
danielk1977d138dd82008-10-15 16:02:48 +00003170#endif
danielk1977fee2d252007-08-18 10:59:19 +00003171 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00003172}
3173
danielk197790949c22007-08-17 16:50:38 +00003174/*
3175** Test the existance of or access permissions of file zPath. The
3176** test performed depends on the value of flags:
3177**
3178** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
3179** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
3180** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
3181**
3182** Otherwise return 0.
3183*/
danielk1977861f7452008-06-05 11:39:11 +00003184static int unixAccess(
danielk1977397d65f2008-11-19 11:35:39 +00003185 sqlite3_vfs *NotUsed,
danielk1977861f7452008-06-05 11:39:11 +00003186 const char *zPath,
3187 int flags,
3188 int *pResOut
3189){
rse25c0d1a2007-09-20 08:38:14 +00003190 int amode = 0;
danielk1977397d65f2008-11-19 11:35:39 +00003191 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00003192 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00003193 switch( flags ){
3194 case SQLITE_ACCESS_EXISTS:
3195 amode = F_OK;
3196 break;
3197 case SQLITE_ACCESS_READWRITE:
3198 amode = W_OK|R_OK;
3199 break;
drh50d3f902007-08-27 21:10:36 +00003200 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00003201 amode = R_OK;
3202 break;
3203
3204 default:
3205 assert(!"Invalid flags argument");
3206 }
danielk1977861f7452008-06-05 11:39:11 +00003207 *pResOut = (access(zPath, amode)==0);
3208 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003209}
3210
danielk1977b4b47412007-08-17 15:53:36 +00003211
3212/*
3213** Turn a relative pathname into a full pathname. The relative path
3214** is stored as a nul-terminated string in the buffer pointed to by
3215** zPath.
3216**
3217** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
3218** (in this case, MAX_PATHNAME bytes). The full-path is written to
3219** this buffer before returning.
3220*/
danielk1977adfb9b02007-09-17 07:02:56 +00003221static int unixFullPathname(
3222 sqlite3_vfs *pVfs, /* Pointer to vfs object */
3223 const char *zPath, /* Possibly relative input path */
3224 int nOut, /* Size of output buffer in bytes */
3225 char *zOut /* Output buffer */
3226){
danielk1977843e65f2007-09-01 16:16:15 +00003227
3228 /* It's odd to simulate an io-error here, but really this is just
3229 ** using the io-error infrastructure to test that SQLite handles this
3230 ** function failing. This function could fail if, for example, the
3231 ** current working directly has been unlinked.
3232 */
3233 SimulateIOError( return SQLITE_ERROR );
3234
drh153c62c2007-08-24 03:51:33 +00003235 assert( pVfs->mxPathname==MAX_PATHNAME );
chw97185482008-11-17 08:05:31 +00003236
3237#if defined(__RTP__) || defined(_WRS_KERNEL)
3238 {
3239 char *zRealname = vxrealpath(zPath, 0);
3240 zOut[0] = '\0';
3241 if( !zRealname ){
3242 return SQLITE_CANTOPEN;
3243 }
3244 sqlite3_snprintf(nOut, zOut, "%s", zRealname);
3245 sqlite3_free(zRealname);
3246 return SQLITE_OK;
3247 }
3248#else
drh3c7f2dc2007-12-06 13:26:20 +00003249 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00003250 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00003251 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003252 }else{
3253 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00003254 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00003255 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00003256 }
3257 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00003258 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003259 }
3260 return SQLITE_OK;
3261
3262#if 0
3263 /*
3264 ** Remove "/./" path elements and convert "/A/./" path elements
3265 ** to just "/".
3266 */
3267 if( zFull ){
3268 int i, j;
3269 for(i=j=0; zFull[i]; i++){
3270 if( zFull[i]=='/' ){
3271 if( zFull[i+1]=='/' ) continue;
3272 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
3273 i += 1;
3274 continue;
3275 }
3276 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
3277 while( j>0 && zFull[j-1]!='/' ){ j--; }
3278 i += 3;
3279 continue;
3280 }
3281 }
3282 zFull[j++] = zFull[i];
3283 }
3284 zFull[j] = 0;
3285 }
3286#endif
chw97185482008-11-17 08:05:31 +00003287#endif
danielk1977b4b47412007-08-17 15:53:36 +00003288}
3289
drh0ccebe72005-06-07 22:22:50 +00003290
drh761df872006-12-21 01:29:22 +00003291#ifndef SQLITE_OMIT_LOAD_EXTENSION
3292/*
3293** Interfaces for opening a shared library, finding entry points
3294** within the shared library, and closing the shared library.
3295*/
3296#include <dlfcn.h>
danielk1977397d65f2008-11-19 11:35:39 +00003297static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
3298 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003299 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
3300}
danielk197795c8a542007-09-01 06:51:27 +00003301
3302/*
3303** SQLite calls this function immediately after a call to unixDlSym() or
3304** unixDlOpen() fails (returns a null pointer). If a more detailed error
3305** message is available, it is written to zBufOut. If no error message
3306** is available, zBufOut is left unmodified and SQLite uses a default
3307** error message.
3308*/
danielk1977397d65f2008-11-19 11:35:39 +00003309static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00003310 char *zErr;
danielk1977397d65f2008-11-19 11:35:39 +00003311 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003312 enterMutex();
3313 zErr = dlerror();
3314 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00003315 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00003316 }
3317 leaveMutex();
3318}
danielk1977397d65f2008-11-19 11:35:39 +00003319static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
3320 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003321 return dlsym(pHandle, zSymbol);
3322}
danielk1977397d65f2008-11-19 11:35:39 +00003323static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
3324 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003325 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00003326}
danielk1977b4b47412007-08-17 15:53:36 +00003327#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
3328 #define unixDlOpen 0
3329 #define unixDlError 0
3330 #define unixDlSym 0
3331 #define unixDlClose 0
3332#endif
3333
3334/*
danielk197790949c22007-08-17 16:50:38 +00003335** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00003336*/
danielk1977397d65f2008-11-19 11:35:39 +00003337static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
3338 UNUSED_PARAMETER(NotUsed);
danielk197700e13612008-11-17 19:18:54 +00003339 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00003340
drhbbd42a62004-05-22 17:41:58 +00003341 /* We have to initialize zBuf to prevent valgrind from reporting
3342 ** errors. The reports issued by valgrind are incorrect - we would
3343 ** prefer that the randomness be increased by making use of the
3344 ** uninitialized space in zBuf - but valgrind errors tend to worry
3345 ** some users. Rather than argue, it seems easier just to initialize
3346 ** the whole array and silence valgrind, even if that means less randomness
3347 ** in the random seed.
3348 **
3349 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00003350 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00003351 ** tests repeatable.
3352 */
danielk1977b4b47412007-08-17 15:53:36 +00003353 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00003354#if !defined(SQLITE_TEST)
3355 {
drh842b8642005-01-21 17:53:17 +00003356 int pid, fd;
3357 fd = open("/dev/urandom", O_RDONLY);
3358 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00003359 time_t t;
3360 time(&t);
danielk197790949c22007-08-17 16:50:38 +00003361 memcpy(zBuf, &t, sizeof(t));
3362 pid = getpid();
3363 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00003364 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00003365 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00003366 }else{
drh72cbd072008-10-14 17:58:38 +00003367 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00003368 close(fd);
3369 }
drhbbd42a62004-05-22 17:41:58 +00003370 }
3371#endif
drh72cbd072008-10-14 17:58:38 +00003372 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00003373}
3374
danielk1977b4b47412007-08-17 15:53:36 +00003375
drhbbd42a62004-05-22 17:41:58 +00003376/*
3377** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00003378** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00003379** The return value is the number of microseconds of sleep actually
3380** requested from the underlying operating system, a number which
3381** might be greater than or equal to the argument, but not less
3382** than the argument.
drhbbd42a62004-05-22 17:41:58 +00003383*/
danielk1977397d65f2008-11-19 11:35:39 +00003384static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
chw97185482008-11-17 08:05:31 +00003385#if defined(__RTP__) || defined(_WRS_KERNEL)
3386 struct timespec sp;
3387
3388 sp.tv_sec = microseconds / 1000000;
3389 sp.tv_nsec = (microseconds % 1000000) * 1000;
3390 nanosleep(&sp, NULL);
danielk1977397d65f2008-11-19 11:35:39 +00003391 return microseconds;
3392#elif defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00003393 usleep(microseconds);
3394 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00003395#else
danielk1977b4b47412007-08-17 15:53:36 +00003396 int seconds = (microseconds+999999)/1000000;
3397 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00003398 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00003399#endif
danielk1977397d65f2008-11-19 11:35:39 +00003400 UNUSED_PARAMETER(NotUsed);
drh88f474a2006-01-02 20:00:12 +00003401}
3402
3403/*
drhbbd42a62004-05-22 17:41:58 +00003404** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00003405** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00003406*/
3407#ifdef SQLITE_TEST
3408int sqlite3_current_time = 0;
3409#endif
3410
3411/*
3412** Find the current time (in Universal Coordinated Time). Write the
3413** current time and date as a Julian Day number into *prNow and
3414** return 0. Return 1 if the time and date cannot be found.
3415*/
danielk1977397d65f2008-11-19 11:35:39 +00003416static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
chw97185482008-11-17 08:05:31 +00003417#if defined(__RTP__) || defined(_WRS_KERNEL)
3418 struct timespec sNow;
3419 clock_gettime(CLOCK_REALTIME, &sNow);
3420 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
danielk1977397d65f2008-11-19 11:35:39 +00003421#elif defined(NO_GETTOD)
drhbbd42a62004-05-22 17:41:58 +00003422 time_t t;
3423 time(&t);
3424 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00003425#else
3426 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00003427 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00003428 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
3429#endif
danielk1977397d65f2008-11-19 11:35:39 +00003430
drhbbd42a62004-05-22 17:41:58 +00003431#ifdef SQLITE_TEST
3432 if( sqlite3_current_time ){
3433 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
3434 }
3435#endif
danielk1977397d65f2008-11-19 11:35:39 +00003436 UNUSED_PARAMETER(NotUsed);
drhbbd42a62004-05-22 17:41:58 +00003437 return 0;
3438}
danielk1977b4b47412007-08-17 15:53:36 +00003439
danielk1977397d65f2008-11-19 11:35:39 +00003440static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
3441 UNUSED_PARAMETER(NotUsed);
3442 UNUSED_PARAMETER(NotUsed2);
3443 UNUSED_PARAMETER(NotUsed3);
danielk1977bcb97fe2008-06-06 15:49:29 +00003444 return 0;
3445}
3446
drh153c62c2007-08-24 03:51:33 +00003447/*
danielk1977e339d652008-06-28 11:23:00 +00003448** Initialize the operating system interface.
drh153c62c2007-08-24 03:51:33 +00003449*/
danielk1977c0fa4c52008-06-25 17:19:00 +00003450int sqlite3_os_init(void){
danielk1977e339d652008-06-28 11:23:00 +00003451 /* Macro to define the static contents of an sqlite3_vfs structure for
3452 ** the unix backend. The two parameters are the values to use for
3453 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
3454 **
3455 */
3456 #define UNIXVFS(zVfsName, pVfsAppData) { \
3457 1, /* iVersion */ \
3458 sizeof(unixFile), /* szOsFile */ \
3459 MAX_PATHNAME, /* mxPathname */ \
3460 0, /* pNext */ \
3461 zVfsName, /* zName */ \
3462 (void *)pVfsAppData, /* pAppData */ \
3463 unixOpen, /* xOpen */ \
3464 unixDelete, /* xDelete */ \
3465 unixAccess, /* xAccess */ \
3466 unixFullPathname, /* xFullPathname */ \
3467 unixDlOpen, /* xDlOpen */ \
3468 unixDlError, /* xDlError */ \
3469 unixDlSym, /* xDlSym */ \
3470 unixDlClose, /* xDlClose */ \
3471 unixRandomness, /* xRandomness */ \
3472 unixSleep, /* xSleep */ \
3473 unixCurrentTime, /* xCurrentTime */ \
3474 unixGetLastError /* xGetLastError */ \
3475 }
3476
3477 static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
drh40bbb0a2008-09-23 10:23:26 +00003478#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +00003479 int i;
3480 static sqlite3_vfs aVfs[] = {
3481 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
3482 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
3483 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
3484 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
chw97185482008-11-17 08:05:31 +00003485 UNIXVFS("unix-none", LOCKING_STYLE_NONE),
3486 UNIXVFS("unix-namedsem",LOCKING_STYLE_NAMEDSEM),
drh153c62c2007-08-24 03:51:33 +00003487 };
danielk1977e339d652008-06-28 11:23:00 +00003488 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
3489 sqlite3_vfs_register(&aVfs[i], 0);
3490 }
3491#endif
chw97185482008-11-17 08:05:31 +00003492#if defined(__RTP__) || defined(_WRS_KERNEL)
3493 sqlite3HashInit(&nameHash, 1);
3494#endif
danielk1977c0fa4c52008-06-25 17:19:00 +00003495 sqlite3_vfs_register(&unixVfs, 1);
3496 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00003497}
danielk1977e339d652008-06-28 11:23:00 +00003498
3499/*
3500** Shutdown the operating system interface. This is a no-op for unix.
3501*/
danielk1977c0fa4c52008-06-25 17:19:00 +00003502int sqlite3_os_end(void){
3503 return SQLITE_OK;
3504}
drhdce8bdb2007-08-16 13:01:44 +00003505
danielk197729bafea2008-06-26 10:41:19 +00003506#endif /* SQLITE_OS_UNIX */