blob: e016fecde0e4140f6f582fb76829302b42470f0c [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**
danielk1977f3d3c272008-11-19 16:52:44 +000015** $Id: os_unix.c,v 1.216 2008/11/19 16:52:44 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
danielk1977c70dfc42008-11-19 13:52:30 +000085# include <sys/ioctl.h>
86# if IS_VXWORKS
87# define lstat stat
88# include <semaphore.h>
89# include <limits.h>
90# else
91# include <sys/param.h>
92# include <sys/mount.h>
93# 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 */
danielk1977c70dfc42008-11-19 13:52:30 +0000143#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000144 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 */
danielk1977c70dfc42008-11-19 13:52:30 +0000325#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000326 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 */
danielk1977c70dfc42008-11-19 13:52:30 +0000359#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000360 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() */
danielk1977c70dfc42008-11-19 13:52:30 +0000379#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000380 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
danielk1977c70dfc42008-11-19 13:52:30 +0000395#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000396/*
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
danielk1977c70dfc42008-11-19 13:52:30 +0000649#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000650/*
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
danielk1977c70dfc42008-11-19 13:52:30 +0000762 ** test fails, then we fall back to using dot-file style locking (or
763 ** named-semaphore locking on vxworks).
chw97185482008-11-17 08:05:31 +0000764 */
danielk1977c70dfc42008-11-19 13:52:30 +0000765 return (IS_VXWORKS ? LOCKING_STYLE_NAMEDSEM : LOCKING_STYLE_DOTFILE);
drhbfe66312006-10-03 17:40:40 +0000766}
drh93a960a2008-07-10 00:32:42 +0000767#endif
drhbfe66312006-10-03 17:40:40 +0000768
769/*
danielk1977e339d652008-06-28 11:23:00 +0000770** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
771** f_fstypename entry in the statfs structure as returned by stat() for
772** the file system hosting the database file and selects the appropriate
773** locking style based on its value. These values and assignments are
774** based on Darwin/OSX behavior and have not been thoroughly tested on
drhbfe66312006-10-03 17:40:40 +0000775** other systems.
danielk1977e339d652008-06-28 11:23:00 +0000776**
777** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
778** returns LOCKING_STYLE_POSIX.
drhbfe66312006-10-03 17:40:40 +0000779*/
danielk197762c14b32008-11-19 09:05:26 +0000780#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +0000781static int detectLockingStyle(
782 sqlite3_vfs *pVfs,
danielk1977ad94b582007-08-20 06:44:22 +0000783 const char *filePath,
784 int fd
785){
danielk1977c70dfc42008-11-19 13:52:30 +0000786#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000787 if( !filePath ){
788 return LOCKING_STYLE_NONE;
789 }
790 if( pVfs->pAppData ){
791 return SQLITE_PTR_TO_INT(pVfs->pAppData);
792 }
793 if (access(filePath, 0) != -1){
794 return testLockingStyle(fd);
795 }
796#else
danielk1977e339d652008-06-28 11:23:00 +0000797 struct Mapping {
798 const char *zFilesystem;
799 int eLockingStyle;
800 } aMap[] = {
801 { "hfs", LOCKING_STYLE_POSIX },
802 { "ufs", LOCKING_STYLE_POSIX },
803 { "afpfs", LOCKING_STYLE_AFP },
aswift5b1a2562008-08-22 00:22:35 +0000804#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
805 { "smbfs", LOCKING_STYLE_AFP },
806#else
danielk1977e339d652008-06-28 11:23:00 +0000807 { "smbfs", LOCKING_STYLE_FLOCK },
aswift5b1a2562008-08-22 00:22:35 +0000808#endif
danielk1977e339d652008-06-28 11:23:00 +0000809 { "msdos", LOCKING_STYLE_DOTFILE },
810 { "webdav", LOCKING_STYLE_NONE },
811 { 0, 0 }
812 };
813 int i;
drhbfe66312006-10-03 17:40:40 +0000814 struct statfs fsInfo;
815
danielk1977e339d652008-06-28 11:23:00 +0000816 if( !filePath ){
817 return LOCKING_STYLE_NONE;
drh339eb0b2008-03-07 15:34:11 +0000818 }
danielk1977e339d652008-06-28 11:23:00 +0000819 if( pVfs->pAppData ){
aswiftf54b1b32008-08-22 18:41:37 +0000820 return SQLITE_PTR_TO_INT(pVfs->pAppData);
drh339eb0b2008-03-07 15:34:11 +0000821 }
drhbfe66312006-10-03 17:40:40 +0000822
danielk1977e339d652008-06-28 11:23:00 +0000823 if( statfs(filePath, &fsInfo) != -1 ){
824 if( fsInfo.f_flags & MNT_RDONLY ){
825 return LOCKING_STYLE_NONE;
826 }
827 for(i=0; aMap[i].zFilesystem; i++){
828 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
829 return aMap[i].eLockingStyle;
830 }
831 }
832 }
833
834 /* Default case. Handles, amongst others, "nfs". */
835 return testLockingStyle(fd);
danielk1977c70dfc42008-11-19 13:52:30 +0000836#endif /* if IS_VXWORKS */
danielk1977e339d652008-06-28 11:23:00 +0000837 return LOCKING_STYLE_POSIX;
838}
danielk197762c14b32008-11-19 09:05:26 +0000839#else
840 #define detectLockingStyle(x,y,z) LOCKING_STYLE_POSIX
841#endif /* ifdef SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000842
drhbbd42a62004-05-22 17:41:58 +0000843/*
844** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000845** describes that file descriptor. Create new ones if necessary. The
846** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000847**
drh65594042008-05-05 16:56:34 +0000848** Return an appropriate error code.
drhbbd42a62004-05-22 17:41:58 +0000849*/
drh38f82712004-06-18 17:10:16 +0000850static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000851 int fd, /* The file descriptor used in the key */
danielk1977c70dfc42008-11-19 13:52:30 +0000852#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000853 void *rnam, /* vxWorks realname */
854#endif
drhbbd42a62004-05-22 17:41:58 +0000855 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000856 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000857){
858 int rc;
859 struct lockKey key1;
860 struct openKey key2;
861 struct stat statbuf;
862 struct lockInfo *pLock;
863 struct openCnt *pOpen;
864 rc = fstat(fd, &statbuf);
drh65594042008-05-05 16:56:34 +0000865 if( rc!=0 ){
866#ifdef EOVERFLOW
867 if( errno==EOVERFLOW ) return SQLITE_NOLFS;
868#endif
869 return SQLITE_IOERR;
870 }
danielk1977441b09a2006-01-05 13:48:29 +0000871
drh54626242008-07-30 17:28:04 +0000872 /* On OS X on an msdos filesystem, the inode number is reported
873 ** incorrectly for zero-size files. See ticket #3260. To work
874 ** around this problem (we consider it a bug in OS X, not SQLite)
875 ** we always increase the file size to 1 by writing a single byte
876 ** prior to accessing the inode number. The one byte written is
877 ** an ASCII 'S' character which also happens to be the first byte
878 ** in the header of every SQLite database. In this way, if there
879 ** is a race condition such that another thread has already populated
880 ** the first page of the database, no damage is done.
881 */
882 if( statbuf.st_size==0 ){
883 write(fd, "S", 1);
884 rc = fstat(fd, &statbuf);
885 if( rc!=0 ){
886 return SQLITE_IOERR;
887 }
888 }
889
drhbbd42a62004-05-22 17:41:58 +0000890 memset(&key1, 0, sizeof(key1));
891 key1.dev = statbuf.st_dev;
danielk1977c70dfc42008-11-19 13:52:30 +0000892#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000893 key1.rnam = rnam;
894#else
drhbbd42a62004-05-22 17:41:58 +0000895 key1.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000896#endif
drhd677b3d2007-08-20 22:48:41 +0000897#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000898 if( threadsOverrideEachOthersLocks<0 ){
899 testThreadLockingBehavior(fd);
900 }
901 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
902#endif
drhbbd42a62004-05-22 17:41:58 +0000903 memset(&key2, 0, sizeof(key2));
904 key2.dev = statbuf.st_dev;
danielk1977c70dfc42008-11-19 13:52:30 +0000905#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000906 key2.rnam = rnam;
907#else
drhbbd42a62004-05-22 17:41:58 +0000908 key2.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000909#endif
drhda0e7682008-07-30 15:27:54 +0000910 pLock = lockList;
911 while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
912 pLock = pLock->pNext;
913 }
drhbbd42a62004-05-22 17:41:58 +0000914 if( pLock==0 ){
drh17435752007-08-16 04:30:38 +0000915 pLock = sqlite3_malloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000916 if( pLock==0 ){
drh65594042008-05-05 16:56:34 +0000917 rc = SQLITE_NOMEM;
danielk1977441b09a2006-01-05 13:48:29 +0000918 goto exit_findlockinfo;
919 }
drhbbd42a62004-05-22 17:41:58 +0000920 pLock->key = key1;
921 pLock->nRef = 1;
922 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000923 pLock->locktype = 0;
drhda0e7682008-07-30 15:27:54 +0000924 pLock->pNext = lockList;
925 pLock->pPrev = 0;
926 if( lockList ) lockList->pPrev = pLock;
927 lockList = pLock;
drhbbd42a62004-05-22 17:41:58 +0000928 }else{
929 pLock->nRef++;
930 }
931 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000932 if( ppOpen!=0 ){
drhda0e7682008-07-30 15:27:54 +0000933 pOpen = openList;
934 while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){
935 pOpen = pOpen->pNext;
936 }
drhbbd42a62004-05-22 17:41:58 +0000937 if( pOpen==0 ){
drh17435752007-08-16 04:30:38 +0000938 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000939 if( pOpen==0 ){
940 releaseLockInfo(pLock);
drh65594042008-05-05 16:56:34 +0000941 rc = SQLITE_NOMEM;
drh029b44b2006-01-15 00:13:15 +0000942 goto exit_findlockinfo;
943 }
944 pOpen->key = key2;
945 pOpen->nRef = 1;
946 pOpen->nLock = 0;
947 pOpen->nPending = 0;
948 pOpen->aPending = 0;
drhda0e7682008-07-30 15:27:54 +0000949 pOpen->pNext = openList;
950 pOpen->pPrev = 0;
951 if( openList ) openList->pPrev = pOpen;
952 openList = pOpen;
danielk1977c70dfc42008-11-19 13:52:30 +0000953#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000954 pOpen->pSem = NULL;
955 pOpen->aSemName[0] = '\0';
956#endif
drh029b44b2006-01-15 00:13:15 +0000957 }else{
958 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000959 }
drh029b44b2006-01-15 00:13:15 +0000960 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000961 }
danielk1977441b09a2006-01-05 13:48:29 +0000962
963exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000964 return rc;
drhbbd42a62004-05-22 17:41:58 +0000965}
966
drh64b1bea2006-01-15 02:30:57 +0000967#ifdef SQLITE_DEBUG
968/*
969** Helper function for printing out trace information from debugging
970** binaries. This returns the string represetation of the supplied
971** integer lock-type.
972*/
973static const char *locktypeName(int locktype){
974 switch( locktype ){
975 case NO_LOCK: return "NONE";
976 case SHARED_LOCK: return "SHARED";
977 case RESERVED_LOCK: return "RESERVED";
978 case PENDING_LOCK: return "PENDING";
979 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
980 }
981 return "ERROR";
982}
983#endif
984
drhbbd42a62004-05-22 17:41:58 +0000985/*
drh029b44b2006-01-15 00:13:15 +0000986** If we are currently in a different thread than the thread that the
987** unixFile argument belongs to, then transfer ownership of the unixFile
988** over to the current thread.
989**
990** A unixFile is only owned by a thread on systems where one thread is
991** unable to override locks created by a different thread. RedHat9 is
992** an example of such a system.
993**
994** Ownership transfer is only allowed if the unixFile is currently unlocked.
995** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000996** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +0000997*/
drhd677b3d2007-08-20 22:48:41 +0000998#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +0000999static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +00001000 int rc;
drh029b44b2006-01-15 00:13:15 +00001001 pthread_t hSelf;
1002 if( threadsOverrideEachOthersLocks ){
1003 /* Ownership transfers not needed on this system */
1004 return SQLITE_OK;
1005 }
1006 hSelf = pthread_self();
1007 if( pthread_equal(pFile->tid, hSelf) ){
1008 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +00001009 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +00001010 return SQLITE_OK;
1011 }
1012 if( pFile->locktype!=NO_LOCK ){
1013 /* We cannot change ownership while we are holding a lock! */
1014 return SQLITE_MISUSE;
1015 }
drh4f0c5872007-03-26 22:05:01 +00001016 OSTRACE4("Transfer ownership of %d from %d to %d\n",
1017 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +00001018 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +00001019 if (pFile->pLock != NULL) {
1020 releaseLockInfo(pFile->pLock);
danielk1977c70dfc42008-11-19 13:52:30 +00001021#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00001022 rc = findLockInfo(pFile->h, pFile->zRealpath, &pFile->pLock, 0);
1023#else
drhbfe66312006-10-03 17:40:40 +00001024 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
chw97185482008-11-17 08:05:31 +00001025#endif
drh4f0c5872007-03-26 22:05:01 +00001026 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001027 locktypeName(pFile->locktype),
1028 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
1029 return rc;
1030 } else {
1031 return SQLITE_OK;
1032 }
drh029b44b2006-01-15 00:13:15 +00001033}
1034#else
drhf1a221e2006-01-15 17:27:17 +00001035 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +00001036# define transferOwnership(X) SQLITE_OK
1037#endif
1038
1039/*
danielk19772a6bdf62007-08-20 16:07:00 +00001040** Seek to the offset passed as the second argument, then read cnt
1041** bytes into pBuf. Return the number of bytes actually read.
drh9e0ebbf2007-10-23 15:59:18 +00001042**
1043** NB: If you define USE_PREAD or USE_PREAD64, then it might also
1044** be necessary to define _XOPEN_SOURCE to be 500. This varies from
1045** one system to another. Since SQLite does not define USE_PREAD
1046** any any form by default, we will not attempt to define _XOPEN_SOURCE.
1047** See tickets #2741 and #2681.
drhb912b282006-03-23 22:42:20 +00001048*/
danielk197762079062007-08-15 17:08:46 +00001049static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001050 int got;
drh8ebf6702007-02-06 11:11:08 +00001051 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001052 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001053#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001054 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001055 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +00001056#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001057 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001058 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +00001059#else
danielk197762079062007-08-15 17:08:46 +00001060 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +00001061 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +00001062 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +00001063 return -1;
1064 }
drhb912b282006-03-23 22:42:20 +00001065 got = read(id->h, pBuf, cnt);
1066#endif
drh15d00c42007-02-27 02:01:14 +00001067 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +00001068 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001069 return got;
1070}
1071
1072/*
drhbbd42a62004-05-22 17:41:58 +00001073** Read data from a file into a buffer. Return SQLITE_OK if all
1074** bytes were read successfully and SQLITE_IOERR if anything goes
1075** wrong.
1076*/
danielk197762079062007-08-15 17:08:46 +00001077static int unixRead(
1078 sqlite3_file *id,
1079 void *pBuf,
1080 int amt,
1081 sqlite3_int64 offset
1082){
drhbbd42a62004-05-22 17:41:58 +00001083 int got;
drh9cbe6352005-11-29 03:13:21 +00001084 assert( id );
danielk197762079062007-08-15 17:08:46 +00001085 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +00001086 if( got==amt ){
1087 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +00001088 }else if( got<0 ){
1089 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +00001090 }else{
drh4c17c3f2008-11-07 00:06:18 +00001091 /* Unread parts of the buffer must be zero-filled */
drhbafda092007-01-03 23:36:22 +00001092 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +00001093 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +00001094 }
1095}
1096
1097/*
drhb912b282006-03-23 22:42:20 +00001098** Seek to the offset in id->offset then read cnt bytes into pBuf.
1099** Return the number of bytes actually read. Update the offset.
1100*/
danielk197762079062007-08-15 17:08:46 +00001101static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001102 int got;
drh8ebf6702007-02-06 11:11:08 +00001103 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001104 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001105#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001106 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +00001107#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001108 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +00001109#else
danielk197762079062007-08-15 17:08:46 +00001110 newOffset = lseek(id->h, offset, SEEK_SET);
1111 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +00001112 return -1;
1113 }
drhb912b282006-03-23 22:42:20 +00001114 got = write(id->h, pBuf, cnt);
1115#endif
drh15d00c42007-02-27 02:01:14 +00001116 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +00001117 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001118 return got;
1119}
1120
1121
1122/*
drhbbd42a62004-05-22 17:41:58 +00001123** Write data from a buffer into a file. Return SQLITE_OK on success
1124** or some other error code on failure.
1125*/
danielk197762079062007-08-15 17:08:46 +00001126static int unixWrite(
1127 sqlite3_file *id,
1128 const void *pBuf,
1129 int amt,
1130 sqlite3_int64 offset
1131){
drhbbd42a62004-05-22 17:41:58 +00001132 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +00001133 assert( id );
drh4c7f9412005-02-03 00:29:47 +00001134 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +00001135 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +00001136 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +00001137 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +00001138 pBuf = &((char*)pBuf)[wrote];
1139 }
drh59685932006-09-14 13:47:11 +00001140 SimulateIOError(( wrote=(-1), amt=1 ));
1141 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +00001142 if( amt>0 ){
drh59685932006-09-14 13:47:11 +00001143 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +00001144 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +00001145 }else{
1146 return SQLITE_FULL;
1147 }
drhbbd42a62004-05-22 17:41:58 +00001148 }
1149 return SQLITE_OK;
1150}
1151
drhb851b2c2005-03-10 14:11:12 +00001152#ifdef SQLITE_TEST
1153/*
1154** Count the number of fullsyncs and normal syncs. This is used to test
1155** that syncs and fullsyncs are occuring at the right times.
1156*/
1157int sqlite3_sync_count = 0;
1158int sqlite3_fullsync_count = 0;
1159#endif
1160
drhf2f23912005-10-05 10:29:36 +00001161/*
1162** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
1163** Otherwise use fsync() in its place.
1164*/
1165#ifndef HAVE_FDATASYNC
1166# define fdatasync fsync
1167#endif
1168
drhac530b12006-02-11 01:25:50 +00001169/*
1170** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
1171** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
1172** only available on Mac OS X. But that could change.
1173*/
1174#ifdef F_FULLFSYNC
1175# define HAVE_FULLFSYNC 1
1176#else
1177# define HAVE_FULLFSYNC 0
1178#endif
1179
drhb851b2c2005-03-10 14:11:12 +00001180
drhbbd42a62004-05-22 17:41:58 +00001181/*
drhdd809b02004-07-17 21:44:57 +00001182** The fsync() system call does not work as advertised on many
1183** unix systems. The following procedure is an attempt to make
1184** it work better.
drh1398ad32005-01-19 23:24:50 +00001185**
1186** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
1187** for testing when we want to run through the test suite quickly.
1188** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
1189** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
1190** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +00001191*/
drheb796a72005-09-08 12:38:41 +00001192static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +00001193 int rc;
drhb851b2c2005-03-10 14:11:12 +00001194
danielk1977397d65f2008-11-19 11:35:39 +00001195 /* The following "ifdef/elif/else/" block has the same structure as
1196 ** the one below. It is replicated here solely to avoid cluttering
1197 ** up the real code with the UNUSED_PARAMETER() macros.
1198 */
1199#ifdef SQLITE_NO_SYNC
1200 UNUSED_PARAMETER(fd);
1201 UNUSED_PARAMETER(fullSync);
1202 UNUSED_PARAMETER(dataOnly);
1203#elif HAVE_FULLFSYNC
danielk1977397d65f2008-11-19 11:35:39 +00001204 UNUSED_PARAMETER(dataOnly);
danielk1977f3d3c272008-11-19 16:52:44 +00001205#else
1206 UNUSED_PARAMETER(fullSync);
danielk1977397d65f2008-11-19 11:35:39 +00001207#endif
1208
drhb851b2c2005-03-10 14:11:12 +00001209 /* Record the number of times that we do a normal fsync() and
1210 ** FULLSYNC. This is used during testing to verify that this procedure
1211 ** gets called with the correct arguments.
1212 */
1213#ifdef SQLITE_TEST
1214 if( fullSync ) sqlite3_fullsync_count++;
1215 sqlite3_sync_count++;
1216#endif
1217
1218 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
1219 ** no-op
1220 */
1221#ifdef SQLITE_NO_SYNC
1222 rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00001223#elif HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +00001224 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +00001225 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +00001226 }else{
1227 rc = 1;
1228 }
1229 /* If the FULLFSYNC failed, fall back to attempting an fsync().
1230 * It shouldn't be possible for fullfsync to fail on the local
1231 * file system (on OSX), so failure indicates that FULLFSYNC
1232 * isn't supported for this file system. So, attempt an fsync
1233 * and (for now) ignore the overhead of a superfluous fcntl call.
1234 * It'd be better to detect fullfsync support once and avoid
1235 * the fcntl call every time sync is called.
1236 */
1237 if( rc ) rc = fsync(fd);
1238
1239#else
drheb796a72005-09-08 12:38:41 +00001240 if( dataOnly ){
1241 rc = fdatasync(fd);
danielk1977397d65f2008-11-19 11:35:39 +00001242 if( IS_VXWORKS && rc==-1 && errno==ENOTSUP ){
chw97185482008-11-17 08:05:31 +00001243 rc = fsync(fd);
1244 }
drhf2f23912005-10-05 10:29:36 +00001245 }else{
drheb796a72005-09-08 12:38:41 +00001246 rc = fsync(fd);
1247 }
danielk1977397d65f2008-11-19 11:35:39 +00001248#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +00001249
danielk1977397d65f2008-11-19 11:35:39 +00001250 if( IS_VXWORKS && rc!= -1 ){
chw97185482008-11-17 08:05:31 +00001251 rc = 0;
1252 }
drhdd809b02004-07-17 21:44:57 +00001253 return rc;
1254}
1255
1256/*
drhbbd42a62004-05-22 17:41:58 +00001257** Make sure all writes to a particular file are committed to disk.
1258**
drheb796a72005-09-08 12:38:41 +00001259** If dataOnly==0 then both the file itself and its metadata (file
1260** size, access time, etc) are synced. If dataOnly!=0 then only the
1261** file data is synced.
1262**
drhbbd42a62004-05-22 17:41:58 +00001263** Under Unix, also make sure that the directory entry for the file
1264** has been created by fsync-ing the directory that contains the file.
1265** If we do not do this and we encounter a power failure, the directory
1266** entry for the journal might not exist after we reboot. The next
1267** SQLite to access the file will not know that the journal exists (because
1268** the directory entry for the journal was never created) and the transaction
1269** will not roll back - possibly leading to database corruption.
1270*/
danielk197790949c22007-08-17 16:50:38 +00001271static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +00001272 int rc;
drh054889e2005-11-30 03:20:31 +00001273 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +00001274
danielk1977f036aef2007-08-20 05:36:51 +00001275 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
1276 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
1277
danielk1977c16d4632007-08-30 14:49:58 +00001278 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
danielk1977f036aef2007-08-20 05:36:51 +00001279 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
1280 || (flags&0x0F)==SQLITE_SYNC_FULL
danielk1977f036aef2007-08-20 05:36:51 +00001281 );
danielk197790949c22007-08-17 16:50:38 +00001282
danielk1977cd3b3c82008-09-22 11:46:32 +00001283 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
1284 ** line is to test that doing so does not cause any problems.
1285 */
1286 SimulateDiskfullError( return SQLITE_FULL );
1287
drh054889e2005-11-30 03:20:31 +00001288 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001289 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001290 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001291 SimulateIOError( rc=1 );
1292 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001293 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001294 }
drh054889e2005-11-30 03:20:31 +00001295 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001296 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001297 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001298#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001299 /* The directory sync is only attempted if full_fsync is
1300 ** turned off or unavailable. If a full_fsync occurred above,
1301 ** then the directory sync is superfluous.
1302 */
danielk197790949c22007-08-17 16:50:38 +00001303 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001304 /*
1305 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001306 ** errors when applied to directories on certain file systems.
1307 ** A failed directory sync is not a big deal. So it seems
1308 ** better to ignore the error. Ticket #1657
1309 */
1310 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001311 }
danielk1977d7c03f72005-11-25 10:38:22 +00001312#endif
drh054889e2005-11-30 03:20:31 +00001313 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1314 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001315 }
drha2854222004-06-17 19:04:17 +00001316 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001317}
1318
1319/*
1320** Truncate an open file to a specified size
1321*/
danielk197762079062007-08-15 17:08:46 +00001322static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001323 int rc;
drh9cbe6352005-11-29 03:13:21 +00001324 assert( id );
drh93aed5a2008-01-16 17:46:38 +00001325 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
drh63fff5f2007-06-19 10:50:38 +00001326 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001327 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001328 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001329 }else{
1330 return SQLITE_OK;
1331 }
drhbbd42a62004-05-22 17:41:58 +00001332}
1333
1334/*
1335** Determine the current size of a file in bytes
1336*/
danielk197762079062007-08-15 17:08:46 +00001337static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001338 int rc;
drhbbd42a62004-05-22 17:41:58 +00001339 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001340 assert( id );
drh59685932006-09-14 13:47:11 +00001341 rc = fstat(((unixFile*)id)->h, &buf);
1342 SimulateIOError( rc=1 );
1343 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001344 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001345 }
1346 *pSize = buf.st_size;
drh54626242008-07-30 17:28:04 +00001347
1348 /* When opening a zero-size database, the findLockInfo() procedure
1349 ** writes a single byte into that file in order to work around a bug
1350 ** in the OS-X msdos filesystem. In order to avoid problems with upper
1351 ** layers, we need to report this file size as zero even though it is
1352 ** really 1. Ticket #3260.
1353 */
1354 if( *pSize==1 ) *pSize = 0;
1355
1356
drhbbd42a62004-05-22 17:41:58 +00001357 return SQLITE_OK;
1358}
1359
danielk19779a1d0ab2004-06-01 14:09:28 +00001360/*
aswift5b1a2562008-08-22 00:22:35 +00001361** This routine translates a standard POSIX errno code into something
1362** useful to the clients of the sqlite3 functions. Specifically, it is
1363** intended to translate a variety of "try again" errors into SQLITE_BUSY
1364** and a variety of "please close the file descriptor NOW" errors into
1365** SQLITE_IOERR
1366**
1367** Errors during initialization of locks, or file system support for locks,
1368** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
1369*/
1370static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
1371 switch (posixError) {
1372 case 0:
1373 return SQLITE_OK;
1374
1375 case EAGAIN:
1376 case ETIMEDOUT:
1377 case EBUSY:
1378 case EINTR:
1379 case ENOLCK:
1380 /* random NFS retry error, unless during file system support
1381 * introspection, in which it actually means what it says */
1382 return SQLITE_BUSY;
1383
1384 case EACCES:
1385 /* EACCES is like EAGAIN during locking operations, but not any other time*/
1386 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
1387 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
1388 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
1389 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
1390 return SQLITE_BUSY;
1391 }
1392 /* else fall through */
1393 case EPERM:
1394 return SQLITE_PERM;
1395
1396 case EDEADLK:
1397 return SQLITE_IOERR_BLOCKED;
1398
drhf489c452008-08-22 00:47:53 +00001399#if EOPNOTSUPP!=ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001400 case EOPNOTSUPP:
1401 /* something went terribly awry, unless during file system support
1402 * introspection, in which it actually means what it says */
drhf489c452008-08-22 00:47:53 +00001403#endif
danielk19775ad6a882008-09-15 04:20:31 +00001404#ifdef ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001405 case ENOTSUP:
1406 /* invalid fd, unless during file system support introspection, in which
1407 * it actually means what it says */
danielk19775ad6a882008-09-15 04:20:31 +00001408#endif
aswift5b1a2562008-08-22 00:22:35 +00001409 case EIO:
1410 case EBADF:
1411 case EINVAL:
1412 case ENOTCONN:
1413 case ENODEV:
1414 case ENXIO:
1415 case ENOENT:
1416 case ESTALE:
1417 case ENOSYS:
1418 /* these should force the client to close the file and reconnect */
1419
1420 default:
1421 return sqliteIOErr;
1422 }
1423}
1424
1425/*
danielk197713adf8a2004-06-03 16:08:41 +00001426** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001427** file by this or any other process. If such a lock is held, set *pResOut
1428** to a non-zero value otherwise *pResOut is set to zero. The return value
1429** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001430*/
danielk1977861f7452008-06-05 11:39:11 +00001431static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001432 int rc = SQLITE_OK;
1433 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001434 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001435
danielk1977861f7452008-06-05 11:39:11 +00001436 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1437
drh054889e2005-11-30 03:20:31 +00001438 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001439 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001440
1441 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001442 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001443 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001444 }
1445
drh2ac3ee92004-06-07 16:27:46 +00001446 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001447 */
aswift5b1a2562008-08-22 00:22:35 +00001448 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001449 struct flock lock;
1450 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001451 lock.l_start = RESERVED_BYTE;
1452 lock.l_len = 1;
1453 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001454 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1455 int tErrno = errno;
1456 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1457 pFile->lastErrno = tErrno;
1458 } else if( lock.l_type!=F_UNLCK ){
1459 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001460 }
1461 }
1462
danielk1977b4b47412007-08-17 15:53:36 +00001463 leaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001464 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001465
aswift5b1a2562008-08-22 00:22:35 +00001466 *pResOut = reserved;
1467 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001468}
1469
1470/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001471** Lock the file with the lock specified by parameter locktype - one
1472** of the following:
1473**
drh2ac3ee92004-06-07 16:27:46 +00001474** (1) SHARED_LOCK
1475** (2) RESERVED_LOCK
1476** (3) PENDING_LOCK
1477** (4) EXCLUSIVE_LOCK
1478**
drhb3e04342004-06-08 00:47:47 +00001479** Sometimes when requesting one lock state, additional lock states
1480** are inserted in between. The locking might fail on one of the later
1481** transitions leaving the lock state different from what it started but
1482** still short of its goal. The following chart shows the allowed
1483** transitions and the inserted intermediate states:
1484**
1485** UNLOCKED -> SHARED
1486** SHARED -> RESERVED
1487** SHARED -> (PENDING) -> EXCLUSIVE
1488** RESERVED -> (PENDING) -> EXCLUSIVE
1489** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001490**
drha6abd042004-06-09 17:37:22 +00001491** This routine will only increase a lock. Use the sqlite3OsUnlock()
1492** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001493*/
danielk197762079062007-08-15 17:08:46 +00001494static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001495 /* The following describes the implementation of the various locks and
1496 ** lock transitions in terms of the POSIX advisory shared and exclusive
1497 ** lock primitives (called read-locks and write-locks below, to avoid
1498 ** confusion with SQLite lock names). The algorithms are complicated
1499 ** slightly in order to be compatible with windows systems simultaneously
1500 ** accessing the same database file, in case that is ever required.
1501 **
1502 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1503 ** byte', each single bytes at well known offsets, and the 'shared byte
1504 ** range', a range of 510 bytes at a well known offset.
1505 **
1506 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1507 ** byte'. If this is successful, a random byte from the 'shared byte
1508 ** range' is read-locked and the lock on the 'pending byte' released.
1509 **
danielk197790ba3bd2004-06-25 08:32:25 +00001510 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1511 ** A RESERVED lock is implemented by grabbing a write-lock on the
1512 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001513 **
1514 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001515 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1516 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1517 ** obtained, but existing SHARED locks are allowed to persist. A process
1518 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1519 ** This property is used by the algorithm for rolling back a journal file
1520 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001521 **
danielk197790ba3bd2004-06-25 08:32:25 +00001522 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1523 ** implemented by obtaining a write-lock on the entire 'shared byte
1524 ** range'. Since all other locks require a read-lock on one of the bytes
1525 ** within this range, this ensures that no other locks are held on the
1526 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001527 **
1528 ** The reason a single byte cannot be used instead of the 'shared byte
1529 ** range' is that some versions of windows do not support read-locks. By
1530 ** locking a random byte from a range, concurrent SHARED locks may exist
1531 ** even if the locking primitive used is always a write-lock.
1532 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001533 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001534 unixFile *pFile = (unixFile*)id;
1535 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001536 struct flock lock;
1537 int s;
1538
drh054889e2005-11-30 03:20:31 +00001539 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001540 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001541 locktypeName(locktype), locktypeName(pFile->locktype),
1542 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001543
1544 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001545 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001546 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001547 */
drh054889e2005-11-30 03:20:31 +00001548 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001549 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001550 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001551 return SQLITE_OK;
1552 }
1553
drhb3e04342004-06-08 00:47:47 +00001554 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001555 */
drh054889e2005-11-30 03:20:31 +00001556 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001557 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001558 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001559
drh054889e2005-11-30 03:20:31 +00001560 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001561 */
danielk1977b4b47412007-08-17 15:53:36 +00001562 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001563
drh029b44b2006-01-15 00:13:15 +00001564 /* Make sure the current thread owns the pFile.
1565 */
1566 rc = transferOwnership(pFile);
1567 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001568 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001569 return rc;
1570 }
drh64b1bea2006-01-15 02:30:57 +00001571 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001572
danielk1977ad94b582007-08-20 06:44:22 +00001573 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001574 ** handle that precludes the requested lock, return BUSY.
1575 */
drh054889e2005-11-30 03:20:31 +00001576 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001577 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001578 ){
1579 rc = SQLITE_BUSY;
1580 goto end_lock;
1581 }
1582
1583 /* If a SHARED lock is requested, and some thread using this PID already
1584 ** has a SHARED or RESERVED lock, then increment reference counts and
1585 ** return SQLITE_OK.
1586 */
1587 if( locktype==SHARED_LOCK &&
1588 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1589 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001590 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001591 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001592 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001593 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001594 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001595 goto end_lock;
1596 }
1597
danielk197713adf8a2004-06-03 16:08:41 +00001598 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001599
danielk19779a1d0ab2004-06-01 14:09:28 +00001600 lock.l_whence = SEEK_SET;
1601
drh3cde3bb2004-06-12 02:17:14 +00001602 /* A PENDING lock is needed before acquiring a SHARED lock and before
1603 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1604 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001605 */
drh3cde3bb2004-06-12 02:17:14 +00001606 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001607 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001608 ){
danielk1977489468c2004-06-28 08:25:47 +00001609 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001610 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001611 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001612 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001613 int tErrno = errno;
1614 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1615 if( IS_LOCK_ERROR(rc) ){
1616 pFile->lastErrno = tErrno;
1617 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001618 goto end_lock;
1619 }
drh3cde3bb2004-06-12 02:17:14 +00001620 }
1621
1622
1623 /* If control gets to this point, then actually go ahead and make
1624 ** operating system calls for the specified lock.
1625 */
1626 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001627 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001628 assert( pLock->cnt==0 );
1629 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001630
drh2ac3ee92004-06-07 16:27:46 +00001631 /* Now get the read-lock */
1632 lock.l_start = SHARED_FIRST;
1633 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001634 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1635 tErrno = errno;
1636 }
drh2ac3ee92004-06-07 16:27:46 +00001637 /* Drop the temporary PENDING lock */
1638 lock.l_start = PENDING_BYTE;
1639 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001640 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001641 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001642 if( s != -1 ){
1643 /* This could happen with a network mount */
1644 tErrno = errno;
1645 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1646 if( IS_LOCK_ERROR(rc) ){
1647 pFile->lastErrno = tErrno;
1648 }
1649 goto end_lock;
1650 }
drh2b4b5962005-06-15 17:47:55 +00001651 }
drhe2396a12007-03-29 20:19:58 +00001652 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001653 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1654 if( IS_LOCK_ERROR(rc) ){
1655 pFile->lastErrno = tErrno;
1656 }
drhbbd42a62004-05-22 17:41:58 +00001657 }else{
drh054889e2005-11-30 03:20:31 +00001658 pFile->locktype = SHARED_LOCK;
1659 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001660 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001661 }
drh3cde3bb2004-06-12 02:17:14 +00001662 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1663 /* We are trying for an exclusive lock but another thread in this
1664 ** same process is still holding a shared lock. */
1665 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001666 }else{
drh3cde3bb2004-06-12 02:17:14 +00001667 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001668 ** assumed that there is a SHARED or greater lock on the file
1669 ** already.
1670 */
drh054889e2005-11-30 03:20:31 +00001671 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001672 lock.l_type = F_WRLCK;
1673 switch( locktype ){
1674 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001675 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001676 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001677 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001678 lock.l_start = SHARED_FIRST;
1679 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001680 break;
1681 default:
1682 assert(0);
1683 }
drh054889e2005-11-30 03:20:31 +00001684 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001685 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001686 int tErrno = errno;
1687 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1688 if( IS_LOCK_ERROR(rc) ){
1689 pFile->lastErrno = tErrno;
1690 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001691 }
drhbbd42a62004-05-22 17:41:58 +00001692 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001693
danielk1977ecb2a962004-06-02 06:30:16 +00001694 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001695 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001696 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001697 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001698 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001699 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001700 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001701
1702end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001703 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001704 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001705 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001706 return rc;
1707}
1708
1709/*
drh054889e2005-11-30 03:20:31 +00001710** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001711** must be either NO_LOCK or SHARED_LOCK.
1712**
1713** If the locking level of the file descriptor is already at or below
1714** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001715*/
danielk197762079062007-08-15 17:08:46 +00001716static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001717 struct lockInfo *pLock;
1718 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001719 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001720 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001721 int h;
drha6abd042004-06-09 17:37:22 +00001722
drh054889e2005-11-30 03:20:31 +00001723 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001724 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001725 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001726
1727 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001728 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001729 return SQLITE_OK;
1730 }
drhf1a221e2006-01-15 17:27:17 +00001731 if( CHECK_THREADID(pFile) ){
1732 return SQLITE_MISUSE;
1733 }
danielk1977b4b47412007-08-17 15:53:36 +00001734 enterMutex();
drh1aa5af12008-03-07 19:51:14 +00001735 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001736 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001737 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001738 if( pFile->locktype>SHARED_LOCK ){
1739 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001740 SimulateIOErrorBenign(1);
1741 SimulateIOError( h=(-1) )
1742 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001743 if( locktype==SHARED_LOCK ){
1744 lock.l_type = F_RDLCK;
1745 lock.l_whence = SEEK_SET;
1746 lock.l_start = SHARED_FIRST;
1747 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001748 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001749 int tErrno = errno;
1750 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1751 if( IS_LOCK_ERROR(rc) ){
1752 pFile->lastErrno = tErrno;
1753 }
1754 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001755 }
1756 }
drhbbd42a62004-05-22 17:41:58 +00001757 lock.l_type = F_UNLCK;
1758 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001759 lock.l_start = PENDING_BYTE;
1760 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001761 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001762 pLock->locktype = SHARED_LOCK;
1763 }else{
aswift5b1a2562008-08-22 00:22:35 +00001764 int tErrno = errno;
1765 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1766 if( IS_LOCK_ERROR(rc) ){
1767 pFile->lastErrno = tErrno;
1768 }
1769 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001770 }
drhbbd42a62004-05-22 17:41:58 +00001771 }
drha6abd042004-06-09 17:37:22 +00001772 if( locktype==NO_LOCK ){
1773 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001774
drha6abd042004-06-09 17:37:22 +00001775 /* Decrement the shared lock counter. Release the lock using an
1776 ** OS call only when all threads in this same process have released
1777 ** the lock.
1778 */
1779 pLock->cnt--;
1780 if( pLock->cnt==0 ){
1781 lock.l_type = F_UNLCK;
1782 lock.l_whence = SEEK_SET;
1783 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001784 SimulateIOErrorBenign(1);
1785 SimulateIOError( h=(-1) )
1786 SimulateIOErrorBenign(0);
1787 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001788 pLock->locktype = NO_LOCK;
1789 }else{
aswift5b1a2562008-08-22 00:22:35 +00001790 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001791 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001792 if( IS_LOCK_ERROR(rc) ){
1793 pFile->lastErrno = tErrno;
1794 }
drh1aa5af12008-03-07 19:51:14 +00001795 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001796 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001797 }
drha6abd042004-06-09 17:37:22 +00001798 }
1799
drhbbd42a62004-05-22 17:41:58 +00001800 /* Decrement the count of locks against this same file. When the
1801 ** count reaches zero, close any other file descriptors whose close
1802 ** was deferred because of outstanding locks.
1803 */
drh1aa5af12008-03-07 19:51:14 +00001804 if( rc==SQLITE_OK ){
1805 pOpen = pFile->pOpen;
1806 pOpen->nLock--;
1807 assert( pOpen->nLock>=0 );
1808 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1809 int i;
1810 for(i=0; i<pOpen->nPending; i++){
1811 close(pOpen->aPending[i]);
1812 }
drhda0e7682008-07-30 15:27:54 +00001813 sqlite3_free(pOpen->aPending);
drh1aa5af12008-03-07 19:51:14 +00001814 pOpen->nPending = 0;
1815 pOpen->aPending = 0;
drhbbd42a62004-05-22 17:41:58 +00001816 }
drhbbd42a62004-05-22 17:41:58 +00001817 }
1818 }
aswift5b1a2562008-08-22 00:22:35 +00001819
1820end_unlock:
danielk1977b4b47412007-08-17 15:53:36 +00001821 leaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001822 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001823 return rc;
drhbbd42a62004-05-22 17:41:58 +00001824}
1825
1826/*
danielk1977e339d652008-06-28 11:23:00 +00001827** This function performs the parts of the "close file" operation
1828** common to all locking schemes. It closes the directory and file
1829** handles, if they are valid, and sets all fields of the unixFile
1830** structure to 0.
1831*/
1832static int closeUnixFile(sqlite3_file *id){
1833 unixFile *pFile = (unixFile*)id;
1834 if( pFile ){
1835 if( pFile->dirfd>=0 ){
1836 close(pFile->dirfd);
1837 }
1838 if( pFile->h>=0 ){
1839 close(pFile->h);
1840 }
danielk1977c70dfc42008-11-19 13:52:30 +00001841#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00001842 if( pFile->isDelete && pFile->zRealpath ){
1843 unlink(pFile->zRealpath);
1844 }
1845 if( pFile->zRealpath ){
1846 HashElem *pElem;
1847 int n = strlen(pFile->zRealpath) + 1;
1848 pElem = sqlite3HashFindElem(&nameHash, pFile->zRealpath, n);
1849 if( pElem ){
1850 long cnt = (long)pElem->data;
1851 cnt--;
1852 if( cnt==0 ){
1853 sqlite3HashInsert(&nameHash, pFile->zRealpath, n, 0);
1854 }else{
1855 pElem->data = (void*)cnt;
1856 }
1857 }
1858 }
1859#endif
danielk1977e339d652008-06-28 11:23:00 +00001860 OSTRACE2("CLOSE %-3d\n", pFile->h);
1861 OpenCounter(-1);
1862 memset(pFile, 0, sizeof(unixFile));
1863 }
1864 return SQLITE_OK;
1865}
1866
1867/*
danielk1977e3026632004-06-22 11:29:02 +00001868** Close a file.
1869*/
danielk197762079062007-08-15 17:08:46 +00001870static int unixClose(sqlite3_file *id){
danielk1977e339d652008-06-28 11:23:00 +00001871 if( id ){
1872 unixFile *pFile = (unixFile *)id;
1873 unixUnlock(id, NO_LOCK);
1874 enterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001875 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001876 /* If there are outstanding locks, do not actually close the file just
1877 ** yet because that would clear those locks. Instead, add the file
1878 ** descriptor to pOpen->aPending. It will be automatically closed when
1879 ** the last lock is cleared.
1880 */
1881 int *aNew;
1882 struct openCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001883 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001884 if( aNew==0 ){
1885 /* If a malloc fails, just leak the file descriptor */
1886 }else{
1887 pOpen->aPending = aNew;
1888 pOpen->aPending[pOpen->nPending] = pFile->h;
1889 pOpen->nPending++;
1890 pFile->h = -1;
1891 }
danielk1977e3026632004-06-22 11:29:02 +00001892 }
danielk1977e339d652008-06-28 11:23:00 +00001893 releaseLockInfo(pFile->pLock);
1894 releaseOpenCnt(pFile->pOpen);
1895 closeUnixFile(id);
1896 leaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00001897 }
drh02afc862006-01-20 18:10:57 +00001898 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001899}
1900
drhbfe66312006-10-03 17:40:40 +00001901
drh40bbb0a2008-09-23 10:23:26 +00001902#if SQLITE_ENABLE_LOCKING_STYLE
chw97185482008-11-17 08:05:31 +00001903
danielk1977c70dfc42008-11-19 13:52:30 +00001904#if !IS_VXWORKS
drhbfe66312006-10-03 17:40:40 +00001905#pragma mark AFP Support
1906
1907/*
1908 ** The afpLockingContext structure contains all afp lock specific state
1909 */
1910typedef struct afpLockingContext afpLockingContext;
1911struct afpLockingContext {
drh1aa5af12008-03-07 19:51:14 +00001912 unsigned long long sharedLockByte;
drh308aa322008-03-07 20:14:38 +00001913 const char *filePath;
drhbfe66312006-10-03 17:40:40 +00001914};
1915
1916struct ByteRangeLockPB2
1917{
1918 unsigned long long offset; /* offset to first byte to lock */
1919 unsigned long long length; /* nbr of bytes to lock */
1920 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1921 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1922 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1923 int fd; /* file desc to assoc this lock with */
1924};
1925
drhfd131da2007-08-07 17:13:03 +00001926#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001927
danielk1977ad94b582007-08-20 06:44:22 +00001928/*
aswift5b1a2562008-08-22 00:22:35 +00001929 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
1930 */
danielk1977ad94b582007-08-20 06:44:22 +00001931static int _AFPFSSetLock(
1932 const char *path,
aswift5b1a2562008-08-22 00:22:35 +00001933 unixFile *pFile,
danielk1977ad94b582007-08-20 06:44:22 +00001934 unsigned long long offset,
1935 unsigned long long length,
1936 int setLockFlag
1937){
drhfd131da2007-08-07 17:13:03 +00001938 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001939 int err;
1940
1941 pb.unLockFlag = setLockFlag ? 0 : 1;
1942 pb.startEndFlag = 0;
1943 pb.offset = offset;
1944 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00001945 pb.fd = pFile->h;
drh4f0c5872007-03-26 22:05:01 +00001946 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
aswift5b1a2562008-08-22 00:22:35 +00001947 (setLockFlag?"ON":"OFF"), pFile->h, offset, length);
drhbfe66312006-10-03 17:40:40 +00001948 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1949 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00001950 int rc;
1951 int tErrno = errno;
1952 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno));
1953 rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); /* error */
1954 if( IS_LOCK_ERROR(rc) ){
1955 pFile->lastErrno = tErrno;
1956 }
1957 return rc;
drhbfe66312006-10-03 17:40:40 +00001958 } else {
aswift5b1a2562008-08-22 00:22:35 +00001959 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001960 }
1961}
1962
aswift5b1a2562008-08-22 00:22:35 +00001963/* AFP-style reserved lock checking following the behavior of
1964** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00001965static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001966 int rc = SQLITE_OK;
1967 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00001968 unixFile *pFile = (unixFile*)id;
1969
aswift5b1a2562008-08-22 00:22:35 +00001970 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1971
1972 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00001973 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1974
1975 /* Check if a thread in this process holds such a lock */
1976 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001977 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00001978 }
1979
1980 /* Otherwise see if some other process holds it.
1981 */
aswift5b1a2562008-08-22 00:22:35 +00001982 if( !reserved ){
1983 /* lock the RESERVED byte */
1984 int lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
1985 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00001986 /* if we succeeded in taking the reserved lock, unlock it to restore
1987 ** the original state */
aswift5b1a2562008-08-22 00:22:35 +00001988 lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1, 0);
1989 } else {
1990 /* if we failed to get the lock then someone else must have it */
1991 reserved = 1;
1992 }
1993 if( IS_LOCK_ERROR(lrc) ){
1994 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00001995 }
1996 }
drhbfe66312006-10-03 17:40:40 +00001997
aswift5b1a2562008-08-22 00:22:35 +00001998 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
1999
2000 *pResOut = reserved;
2001 return rc;
drhbfe66312006-10-03 17:40:40 +00002002}
2003
2004/* AFP-style locking following the behavior of unixLock, see the unixLock
2005** function comments for details of lock management. */
danielk1977e339d652008-06-28 11:23:00 +00002006static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00002007 int rc = SQLITE_OK;
2008 unixFile *pFile = (unixFile*)id;
2009 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002010
2011 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002012 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002013 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2014
drhbfe66312006-10-03 17:40:40 +00002015 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002016 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2017 ** enterMutex() hasn't been called yet.
2018 */
drhbfe66312006-10-03 17:40:40 +00002019 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002020 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002021 locktypeName(locktype));
2022 return SQLITE_OK;
2023 }
2024
2025 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002026 */
drhbfe66312006-10-03 17:40:40 +00002027 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2028 assert( locktype!=PENDING_LOCK );
2029 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2030
2031 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002032 */
danielk1977b4b47412007-08-17 15:53:36 +00002033 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002034
2035 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002036 */
drhbfe66312006-10-03 17:40:40 +00002037 rc = transferOwnership(pFile);
2038 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00002039 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002040 return rc;
2041 }
2042
2043 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002044 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2045 ** be released.
2046 */
drhbfe66312006-10-03 17:40:40 +00002047 if( locktype==SHARED_LOCK
2048 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002049 ){
2050 int failed;
aswift5b1a2562008-08-22 00:22:35 +00002051 failed = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002052 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002053 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002054 goto afp_end_lock;
2055 }
2056 }
2057
2058 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002059 ** operating system calls for the specified lock.
2060 */
drhbfe66312006-10-03 17:40:40 +00002061 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002062 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002063
aswift5b1a2562008-08-22 00:22:35 +00002064 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002065 /* note that the quality of the randomness doesn't matter that much */
2066 lk = random();
2067 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
aswift5b1a2562008-08-22 00:22:35 +00002068 lrc1 = _AFPFSSetLock(context->filePath, pFile,
2069 SHARED_FIRST+context->sharedLockByte, 1, 1);
2070 if( IS_LOCK_ERROR(lrc1) ){
2071 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002072 }
aswift5b1a2562008-08-22 00:22:35 +00002073 /* Drop the temporary PENDING lock */
2074 lrc2 = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002075
aswift5b1a2562008-08-22 00:22:35 +00002076 if( IS_LOCK_ERROR(lrc1) ) {
2077 pFile->lastErrno = lrc1Errno;
2078 rc = lrc1;
2079 goto afp_end_lock;
2080 } else if( IS_LOCK_ERROR(lrc2) ){
2081 rc = lrc2;
2082 goto afp_end_lock;
2083 } else if( lrc1 != SQLITE_OK ) {
2084 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002085 } else {
2086 pFile->locktype = SHARED_LOCK;
2087 }
2088 }else{
2089 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2090 ** assumed that there is a SHARED or greater lock on the file
2091 ** already.
2092 */
2093 int failed = 0;
2094 assert( 0!=pFile->locktype );
2095 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2096 /* Acquire a RESERVED lock */
aswift5b1a2562008-08-22 00:22:35 +00002097 failed = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002098 }
2099 if (!failed && locktype == EXCLUSIVE_LOCK) {
2100 /* Acquire an EXCLUSIVE lock */
2101
2102 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002103 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002104 */
aswift5b1a2562008-08-22 00:22:35 +00002105 if (!(failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST +
2106 context->sharedLockByte, 1, 0))) {
drhbfe66312006-10-03 17:40:40 +00002107 /* now attemmpt to get the exclusive lock range */
aswift5b1a2562008-08-22 00:22:35 +00002108 failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002109 SHARED_SIZE, 1);
aswift5b1a2562008-08-22 00:22:35 +00002110 if (failed && (failed = _AFPFSSetLock(context->filePath, pFile,
2111 SHARED_FIRST + context->sharedLockByte, 1, 1))) {
2112 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002113 }
2114 } else {
aswift5b1a2562008-08-22 00:22:35 +00002115 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002116 }
2117 }
aswift5b1a2562008-08-22 00:22:35 +00002118 if( failed ){
2119 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002120 }
2121 }
2122
2123 if( rc==SQLITE_OK ){
2124 pFile->locktype = locktype;
2125 }else if( locktype==EXCLUSIVE_LOCK ){
2126 pFile->locktype = PENDING_LOCK;
2127 }
2128
2129afp_end_lock:
drh339eb0b2008-03-07 15:34:11 +00002130 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002131 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002132 rc==SQLITE_OK ? "ok" : "failed");
2133 return rc;
2134}
2135
2136/*
drh339eb0b2008-03-07 15:34:11 +00002137** Lower the locking level on file descriptor pFile to locktype. locktype
2138** must be either NO_LOCK or SHARED_LOCK.
2139**
2140** If the locking level of the file descriptor is already at or below
2141** the requested locking level, this routine is a no-op.
2142*/
danielk1977e339d652008-06-28 11:23:00 +00002143static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002144 int rc = SQLITE_OK;
2145 unixFile *pFile = (unixFile*)id;
2146 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2147
2148 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002149 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002150 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002151
drhbfe66312006-10-03 17:40:40 +00002152 assert( locktype<=SHARED_LOCK );
2153 if( pFile->locktype<=locktype ){
2154 return SQLITE_OK;
2155 }
2156 if( CHECK_THREADID(pFile) ){
2157 return SQLITE_MISUSE;
2158 }
danielk1977b4b47412007-08-17 15:53:36 +00002159 enterMutex();
aswift5b1a2562008-08-22 00:22:35 +00002160 int failed = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002161 if( pFile->locktype>SHARED_LOCK ){
2162 if( locktype==SHARED_LOCK ){
drhbfe66312006-10-03 17:40:40 +00002163
2164 /* unlock the exclusive range - then re-establish the shared lock */
2165 if (pFile->locktype==EXCLUSIVE_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002166 failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002167 SHARED_SIZE, 0);
2168 if (!failed) {
2169 /* successfully removed the exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002170 if ((failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST+
2171 context->sharedLockByte, 1, 1))) {
drhbfe66312006-10-03 17:40:40 +00002172 /* failed to re-establish our shared lock */
aswift5b1a2562008-08-22 00:22:35 +00002173 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002174 }
2175 } else {
aswift5b1a2562008-08-22 00:22:35 +00002176 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002177 }
2178 }
2179 }
2180 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002181 if ((failed = _AFPFSSetLock(context->filePath, pFile,
2182 PENDING_BYTE, 1, 0))){
drhbfe66312006-10-03 17:40:40 +00002183 /* failed to release the pending lock */
aswift5b1a2562008-08-22 00:22:35 +00002184 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002185 }
2186 }
2187 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002188 if ((failed = _AFPFSSetLock(context->filePath, pFile,
2189 RESERVED_BYTE, 1, 0))) {
drhbfe66312006-10-03 17:40:40 +00002190 /* failed to release the reserved lock */
aswift5b1a2562008-08-22 00:22:35 +00002191 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002192 }
2193 }
2194 }
2195 if( locktype==NO_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002196 int failed = _AFPFSSetLock(context->filePath, pFile,
drhbfe66312006-10-03 17:40:40 +00002197 SHARED_FIRST + context->sharedLockByte, 1, 0);
2198 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002199 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002200 }
2201 }
2202 if (rc == SQLITE_OK)
2203 pFile->locktype = locktype;
danielk1977b4b47412007-08-17 15:53:36 +00002204 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002205 return rc;
2206}
2207
2208/*
drh339eb0b2008-03-07 15:34:11 +00002209** Close a file & cleanup AFP specific locking context
2210*/
danielk1977e339d652008-06-28 11:23:00 +00002211static int afpClose(sqlite3_file *id) {
2212 if( id ){
2213 unixFile *pFile = (unixFile*)id;
2214 afpUnlock(id, NO_LOCK);
2215 sqlite3_free(pFile->lockingContext);
2216 }
2217 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002218}
2219
2220
2221#pragma mark flock() style locking
2222
2223/*
drh339eb0b2008-03-07 15:34:11 +00002224** The flockLockingContext is not used
2225*/
drhbfe66312006-10-03 17:40:40 +00002226typedef void flockLockingContext;
2227
aswift5b1a2562008-08-22 00:22:35 +00002228/* flock-style reserved lock checking following the behavior of
2229 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002230static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002231 int rc = SQLITE_OK;
2232 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002233 unixFile *pFile = (unixFile*)id;
2234
aswift5b1a2562008-08-22 00:22:35 +00002235 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2236
2237 assert( pFile );
2238
2239 /* Check if a thread in this process holds such a lock */
2240 if( pFile->locktype>SHARED_LOCK ){
2241 reserved = 1;
2242 }
2243
2244 /* Otherwise see if some other process holds it. */
2245 if( !reserved ){
drh3b62b2f2007-06-08 18:27:03 +00002246 /* attempt to get the lock */
aswift5b1a2562008-08-22 00:22:35 +00002247 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
2248 if( !lrc ){
drh3b62b2f2007-06-08 18:27:03 +00002249 /* got the lock, unlock it */
aswift5b1a2562008-08-22 00:22:35 +00002250 lrc = flock(pFile->h, LOCK_UN);
2251 if ( lrc ) {
2252 int tErrno = errno;
2253 /* unlock failed with an error */
2254 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2255 if( IS_LOCK_ERROR(lrc) ){
2256 pFile->lastErrno = tErrno;
2257 rc = lrc;
2258 }
2259 }
2260 } else {
2261 int tErrno = errno;
2262 reserved = 1;
2263 /* someone else might have it reserved */
2264 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2265 if( IS_LOCK_ERROR(lrc) ){
2266 pFile->lastErrno = tErrno;
2267 rc = lrc;
2268 }
drhbfe66312006-10-03 17:40:40 +00002269 }
drhbfe66312006-10-03 17:40:40 +00002270 }
aswift5b1a2562008-08-22 00:22:35 +00002271 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002272
aswift5b1a2562008-08-22 00:22:35 +00002273 *pResOut = reserved;
2274 return rc;
drhbfe66312006-10-03 17:40:40 +00002275}
2276
danielk1977e339d652008-06-28 11:23:00 +00002277static int flockLock(sqlite3_file *id, int locktype) {
aswift5b1a2562008-08-22 00:22:35 +00002278 int rc = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002279 unixFile *pFile = (unixFile*)id;
aswift5b1a2562008-08-22 00:22:35 +00002280
2281 assert( pFile );
2282
drh3b62b2f2007-06-08 18:27:03 +00002283 /* if we already have a lock, it is exclusive.
2284 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002285 if (pFile->locktype > NO_LOCK) {
2286 pFile->locktype = locktype;
2287 return SQLITE_OK;
2288 }
2289
drh3b62b2f2007-06-08 18:27:03 +00002290 /* grab an exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002291
2292 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
2293 int tErrno = errno;
drh3b62b2f2007-06-08 18:27:03 +00002294 /* didn't get, must be busy */
aswift5b1a2562008-08-22 00:22:35 +00002295 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2296 if( IS_LOCK_ERROR(rc) ){
2297 pFile->lastErrno = tErrno;
2298 }
drhbfe66312006-10-03 17:40:40 +00002299 } else {
drh3b62b2f2007-06-08 18:27:03 +00002300 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002301 pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002302 }
aswift5b1a2562008-08-22 00:22:35 +00002303 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
2304 rc==SQLITE_OK ? "ok" : "failed");
2305 return rc;
drhbfe66312006-10-03 17:40:40 +00002306}
2307
danielk1977e339d652008-06-28 11:23:00 +00002308static int flockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002309 unixFile *pFile = (unixFile*)id;
2310
aswift5b1a2562008-08-22 00:22:35 +00002311 assert( pFile );
2312 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2313 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002314 assert( locktype<=SHARED_LOCK );
2315
drh3b62b2f2007-06-08 18:27:03 +00002316 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002317 if( pFile->locktype==locktype ){
2318 return SQLITE_OK;
2319 }
2320
drh3b62b2f2007-06-08 18:27:03 +00002321 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002322 if (locktype==SHARED_LOCK) {
2323 pFile->locktype = locktype;
2324 return SQLITE_OK;
2325 }
2326
drh3b62b2f2007-06-08 18:27:03 +00002327 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00002328 int rc = flock(pFile->h, LOCK_UN);
aswift5b1a2562008-08-22 00:22:35 +00002329 if (rc) {
2330 int r, tErrno = errno;
2331 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2332 if( IS_LOCK_ERROR(r) ){
2333 pFile->lastErrno = tErrno;
2334 }
2335 return r;
2336 } else {
drhbfe66312006-10-03 17:40:40 +00002337 pFile->locktype = NO_LOCK;
2338 return SQLITE_OK;
2339 }
2340}
2341
2342/*
drh339eb0b2008-03-07 15:34:11 +00002343** Close a file.
2344*/
danielk1977e339d652008-06-28 11:23:00 +00002345static int flockClose(sqlite3_file *id) {
2346 if( id ){
2347 flockUnlock(id, NO_LOCK);
2348 }
2349 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002350}
2351
danielk1977c70dfc42008-11-19 13:52:30 +00002352#endif /* !IS_VXWORKS */
chw97185482008-11-17 08:05:31 +00002353
drhbfe66312006-10-03 17:40:40 +00002354#pragma mark Old-School .lock file based locking
2355
aswift5b1a2562008-08-22 00:22:35 +00002356/* Dotlock-style reserved lock checking following the behavior of
2357** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002358static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
aswift5b1a2562008-08-22 00:22:35 +00002359 int rc = SQLITE_OK;
2360 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002361 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002362
aswift5b1a2562008-08-22 00:22:35 +00002363 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2364
2365 assert( pFile );
2366
2367 /* Check if a thread in this process holds such a lock */
2368 if( pFile->locktype>SHARED_LOCK ){
2369 reserved = 1;
2370 }
2371
2372 /* Otherwise see if some other process holds it. */
2373 if( !reserved ){
2374 char *zLockFile = (char *)pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002375 struct stat statBuf;
aswift5b1a2562008-08-22 00:22:35 +00002376
2377 if( lstat(zLockFile, &statBuf)==0 ){
2378 /* file exists, someone else has the lock */
2379 reserved = 1;
2380 }else{
drh3b62b2f2007-06-08 18:27:03 +00002381 /* file does not exist, we could have it if we want it */
chw97185482008-11-17 08:05:31 +00002382 int tErrno = errno;
aswift5b1a2562008-08-22 00:22:35 +00002383 if( ENOENT != tErrno ){
2384 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2385 pFile->lastErrno = tErrno;
2386 }
drh339eb0b2008-03-07 15:34:11 +00002387 }
drhbfe66312006-10-03 17:40:40 +00002388 }
aswift5b1a2562008-08-22 00:22:35 +00002389 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002390
aswift5b1a2562008-08-22 00:22:35 +00002391 *pResOut = reserved;
2392 return rc;
drhbfe66312006-10-03 17:40:40 +00002393}
2394
danielk1977e339d652008-06-28 11:23:00 +00002395static int dotlockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002396 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002397 int fd;
danielk1977e339d652008-06-28 11:23:00 +00002398 char *zLockFile = (char *)pFile->lockingContext;
aswift5b1a2562008-08-22 00:22:35 +00002399 int rc=SQLITE_OK;
drh339eb0b2008-03-07 15:34:11 +00002400
drh3b62b2f2007-06-08 18:27:03 +00002401 /* if we already have a lock, it is exclusive.
2402 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002403 if (pFile->locktype > NO_LOCK) {
2404 pFile->locktype = locktype;
danielk1977c70dfc42008-11-19 13:52:30 +00002405#if !IS_VXWORKS
drhbfe66312006-10-03 17:40:40 +00002406 /* Always update the timestamp on the old file */
danielk1977e339d652008-06-28 11:23:00 +00002407 utimes(zLockFile, NULL);
chw97185482008-11-17 08:05:31 +00002408#endif
aswift5b1a2562008-08-22 00:22:35 +00002409 rc = SQLITE_OK;
2410 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002411 }
2412
drh3b62b2f2007-06-08 18:27:03 +00002413 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00002414 struct stat statBuf;
danielk1977e339d652008-06-28 11:23:00 +00002415 if (lstat(zLockFile,&statBuf) == 0){
aswift5b1a2562008-08-22 00:22:35 +00002416 rc = SQLITE_BUSY; /* it does, busy */
2417 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002418 }
2419
drh3b62b2f2007-06-08 18:27:03 +00002420 /* grab an exclusive lock */
danielk1977e339d652008-06-28 11:23:00 +00002421 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
drh339eb0b2008-03-07 15:34:11 +00002422 if( fd<0 ){
drh3b62b2f2007-06-08 18:27:03 +00002423 /* failed to open/create the file, someone else may have stolen the lock */
aswift5b1a2562008-08-22 00:22:35 +00002424 int tErrno = errno;
2425 if( EEXIST == tErrno ){
2426 rc = SQLITE_BUSY;
2427 } else {
2428 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2429 if( IS_LOCK_ERROR(rc) ){
2430 pFile->lastErrno = tErrno;
2431 }
2432 }
2433 goto dotlock_end_lock;
2434 }
drhbfe66312006-10-03 17:40:40 +00002435 close(fd);
2436
drh3b62b2f2007-06-08 18:27:03 +00002437 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002438 pFile->locktype = locktype;
aswift5b1a2562008-08-22 00:22:35 +00002439
2440 dotlock_end_lock:
2441 return rc;
drhbfe66312006-10-03 17:40:40 +00002442}
2443
danielk1977e339d652008-06-28 11:23:00 +00002444static int dotlockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002445 unixFile *pFile = (unixFile*)id;
danielk1977e339d652008-06-28 11:23:00 +00002446 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00002447
aswift5b1a2562008-08-22 00:22:35 +00002448 assert( pFile );
2449 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2450 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002451 assert( locktype<=SHARED_LOCK );
2452
drh3b62b2f2007-06-08 18:27:03 +00002453 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002454 if( pFile->locktype==locktype ){
2455 return SQLITE_OK;
2456 }
2457
drh3b62b2f2007-06-08 18:27:03 +00002458 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002459 if (locktype==SHARED_LOCK) {
2460 pFile->locktype = locktype;
2461 return SQLITE_OK;
2462 }
2463
drh3b62b2f2007-06-08 18:27:03 +00002464 /* no, really, unlock. */
aswift5b1a2562008-08-22 00:22:35 +00002465 if (unlink(zLockFile) ) {
2466 int rc, tErrno = errno;
2467 if( ENOENT != tErrno ){
2468 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2469 }
2470 if( IS_LOCK_ERROR(rc) ){
2471 pFile->lastErrno = tErrno;
2472 }
2473 return rc;
2474 }
drhbfe66312006-10-03 17:40:40 +00002475 pFile->locktype = NO_LOCK;
2476 return SQLITE_OK;
2477}
2478
2479/*
2480 ** Close a file.
2481 */
danielk1977e339d652008-06-28 11:23:00 +00002482static int dotlockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00002483 int rc;
danielk1977e339d652008-06-28 11:23:00 +00002484 if( id ){
2485 unixFile *pFile = (unixFile*)id;
2486 dotlockUnlock(id, NO_LOCK);
2487 sqlite3_free(pFile->lockingContext);
2488 }
danielk1977c70dfc42008-11-19 13:52:30 +00002489 if( IS_VXWORKS ) enterMutex();
chw97185482008-11-17 08:05:31 +00002490 rc = closeUnixFile(id);
danielk1977c70dfc42008-11-19 13:52:30 +00002491 if( IS_VXWORKS ) leaveMutex();
chw97185482008-11-17 08:05:31 +00002492 return rc;
drhbfe66312006-10-03 17:40:40 +00002493}
2494
danielk1977c70dfc42008-11-19 13:52:30 +00002495#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00002496
2497#pragma mark POSIX/vxWorks named semaphore based locking
2498
2499/* Namedsem-style reserved lock checking following the behavior of
2500** unixCheckReservedLock, see the unixCheckReservedLock function comments */
2501static int namedsemCheckReservedLock(sqlite3_file *id, int *pResOut) {
2502 int rc = SQLITE_OK;
2503 int reserved = 0;
2504 unixFile *pFile = (unixFile*)id;
2505
2506 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2507
2508 assert( pFile );
2509
2510 /* Check if a thread in this process holds such a lock */
2511 if( pFile->locktype>SHARED_LOCK ){
2512 reserved = 1;
2513 }
2514
2515 /* Otherwise see if some other process holds it. */
2516 if( !reserved ){
2517 sem_t *pSem = pFile->pOpen->pSem;
2518 struct stat statBuf;
2519
2520 if( sem_trywait(pSem)==-1 ){
2521 int tErrno = errno;
2522 if( EAGAIN != tErrno ){
2523 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2524 pFile->lastErrno = tErrno;
2525 } else {
2526 /* someone else has the lock when we are in NO_LOCK */
2527 reserved = (pFile->locktype < SHARED_LOCK);
2528 }
2529 }else{
2530 /* we could have it if we want it */
2531 sem_post(pSem);
2532 }
2533 }
2534 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2535
2536 *pResOut = reserved;
2537 return rc;
2538}
2539
2540static int namedsemLock(sqlite3_file *id, int locktype) {
2541 unixFile *pFile = (unixFile*)id;
2542 int fd;
2543 sem_t *pSem = pFile->pOpen->pSem;
2544 int rc = SQLITE_OK;
2545
2546 /* if we already have a lock, it is exclusive.
2547 ** Just adjust level and punt on outta here. */
2548 if (pFile->locktype > NO_LOCK) {
2549 pFile->locktype = locktype;
2550 rc = SQLITE_OK;
2551 goto namedsem_end_lock;
2552 }
2553
2554 /* lock semaphore now but bail out when already locked. */
2555 if( sem_trywait(pSem)==-1 ){
2556 rc = SQLITE_BUSY;
2557 goto namedsem_end_lock;
2558 }
2559
2560 /* got it, set the type and return ok */
2561 pFile->locktype = locktype;
2562
2563 namedsem_end_lock:
2564 return rc;
2565}
2566
2567static int namedsemUnlock(sqlite3_file *id, int locktype) {
2568 unixFile *pFile = (unixFile*)id;
2569 sem_t *pSem = pFile->pOpen->pSem;
2570
2571 assert( pFile );
2572 assert( pSem );
2573 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2574 pFile->locktype, getpid());
2575 assert( locktype<=SHARED_LOCK );
2576
2577 /* no-op if possible */
2578 if( pFile->locktype==locktype ){
2579 return SQLITE_OK;
2580 }
2581
2582 /* shared can just be set because we always have an exclusive */
2583 if (locktype==SHARED_LOCK) {
2584 pFile->locktype = locktype;
2585 return SQLITE_OK;
2586 }
2587
2588 /* no, really unlock. */
2589 if ( sem_post(pSem)==-1 ) {
2590 int rc, tErrno = errno;
2591 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2592 if( IS_LOCK_ERROR(rc) ){
2593 pFile->lastErrno = tErrno;
2594 }
2595 return rc;
2596 }
2597 pFile->locktype = NO_LOCK;
2598 return SQLITE_OK;
2599}
2600
2601/*
2602 ** Close a file.
2603 */
2604static int namedsemClose(sqlite3_file *id) {
2605 if( id ){
2606 unixFile *pFile = (unixFile*)id;
2607 namedsemUnlock(id, NO_LOCK);
2608 assert( pFile );
2609 enterMutex();
2610 releaseLockInfo(pFile->pLock);
2611 releaseOpenCnt(pFile->pOpen);
2612 closeUnixFile(id);
2613 leaveMutex();
2614 }
2615 return SQLITE_OK;
2616}
2617
danielk1977c70dfc42008-11-19 13:52:30 +00002618#endif /* IS_VXWORKS */
drhbfe66312006-10-03 17:40:40 +00002619
drhda0e7682008-07-30 15:27:54 +00002620#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +00002621
2622/*
drh339eb0b2008-03-07 15:34:11 +00002623** The nolockLockingContext is void
2624*/
drhbfe66312006-10-03 17:40:40 +00002625typedef void nolockLockingContext;
2626
danielk1977397d65f2008-11-19 11:35:39 +00002627static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
2628 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00002629 *pResOut = 0;
2630 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002631}
2632
danielk1977397d65f2008-11-19 11:35:39 +00002633static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
2634 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbfe66312006-10-03 17:40:40 +00002635 return SQLITE_OK;
2636}
2637
danielk1977397d65f2008-11-19 11:35:39 +00002638static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
2639 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbfe66312006-10-03 17:40:40 +00002640 return SQLITE_OK;
2641}
2642
2643/*
drh339eb0b2008-03-07 15:34:11 +00002644** Close a file.
2645*/
danielk1977e339d652008-06-28 11:23:00 +00002646static int nolockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00002647 int rc;
danielk1977397d65f2008-11-19 11:35:39 +00002648 if( IS_VXWORKS ) enterMutex();
chw97185482008-11-17 08:05:31 +00002649 rc = closeUnixFile(id);
danielk1977397d65f2008-11-19 11:35:39 +00002650 if( IS_VXWORKS ) leaveMutex();
chw97185482008-11-17 08:05:31 +00002651 return rc;
drhbfe66312006-10-03 17:40:40 +00002652}
2653
danielk1977ad94b582007-08-20 06:44:22 +00002654
danielk1977e3026632004-06-22 11:29:02 +00002655/*
drh9e33c2c2007-08-31 18:34:59 +00002656** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00002657*/
drhcc6bb3e2007-08-31 16:11:35 +00002658static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00002659 switch( op ){
2660 case SQLITE_FCNTL_LOCKSTATE: {
2661 *(int*)pArg = ((unixFile*)id)->locktype;
2662 return SQLITE_OK;
2663 }
2664 }
drhcc6bb3e2007-08-31 16:11:35 +00002665 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00002666}
2667
2668/*
danielk1977a3d4c882007-03-23 10:08:38 +00002669** Return the sector size in bytes of the underlying block device for
2670** the specified file. This is almost always 512 bytes, but may be
2671** larger for some devices.
2672**
2673** SQLite code assumes this function cannot fail. It also assumes that
2674** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00002675** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00002676** same for both.
2677*/
danielk1977397d65f2008-11-19 11:35:39 +00002678static int unixSectorSize(sqlite3_file *NotUsed){
2679 UNUSED_PARAMETER(NotUsed);
drh3ceeb752007-03-29 18:19:52 +00002680 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002681}
2682
danielk197790949c22007-08-17 16:50:38 +00002683/*
danielk1977397d65f2008-11-19 11:35:39 +00002684** Return the device characteristics for the file. This is always 0 for unix.
danielk197790949c22007-08-17 16:50:38 +00002685*/
danielk1977397d65f2008-11-19 11:35:39 +00002686static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
2687 UNUSED_PARAMETER(NotUsed);
danielk197762079062007-08-15 17:08:46 +00002688 return 0;
2689}
2690
danielk1977a3d4c882007-03-23 10:08:38 +00002691/*
danielk1977e339d652008-06-28 11:23:00 +00002692** Initialize the contents of the unixFile structure pointed to by pId.
2693**
danielk1977ad94b582007-08-20 06:44:22 +00002694** When locking extensions are enabled, the filepath and locking style
2695** are needed to determine the unixFile pMethod to use for locking operations.
2696** The locking-style specific lockingContext data structure is created
2697** and assigned here also.
2698*/
2699static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00002700 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00002701 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00002702 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00002703 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00002704 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00002705 int noLock, /* Omit locking if true */
2706 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00002707){
drhda0e7682008-07-30 15:27:54 +00002708 int eLockingStyle;
2709 unixFile *pNew = (unixFile *)pId;
2710 int rc = SQLITE_OK;
2711
danielk1977e339d652008-06-28 11:23:00 +00002712 /* Macro to define the static contents of an sqlite3_io_methods
2713 ** structure for a unix backend file. Different locking methods
2714 ** require different functions for the xClose, xLock, xUnlock and
2715 ** xCheckReservedLock methods.
2716 */
2717 #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \
2718 1, /* iVersion */ \
2719 xClose, /* xClose */ \
2720 unixRead, /* xRead */ \
2721 unixWrite, /* xWrite */ \
2722 unixTruncate, /* xTruncate */ \
2723 unixSync, /* xSync */ \
2724 unixFileSize, /* xFileSize */ \
2725 xLock, /* xLock */ \
2726 xUnlock, /* xUnlock */ \
2727 xCheckReservedLock, /* xCheckReservedLock */ \
2728 unixFileControl, /* xFileControl */ \
2729 unixSectorSize, /* xSectorSize */ \
2730 unixDeviceCharacteristics /* xDeviceCapabilities */ \
2731 }
2732 static sqlite3_io_methods aIoMethod[] = {
2733 IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
danielk1977e339d652008-06-28 11:23:00 +00002734 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
drh40bbb0a2008-09-23 10:23:26 +00002735#if SQLITE_ENABLE_LOCKING_STYLE
drhda0e7682008-07-30 15:27:54 +00002736 ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
danielk1977c70dfc42008-11-19 13:52:30 +00002737#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00002738 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2739 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2740 ,IOMETHODS(namedsemClose, namedsemLock, namedsemUnlock, namedsemCheckReservedLock)
2741#else
drhda0e7682008-07-30 15:27:54 +00002742 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
danielk1977e339d652008-06-28 11:23:00 +00002743 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
chw97185482008-11-17 08:05:31 +00002744 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2745#endif
drh218c5082008-03-07 00:27:10 +00002746#endif
danielk1977e339d652008-06-28 11:23:00 +00002747 };
drhda0e7682008-07-30 15:27:54 +00002748 /* The order of the IOMETHODS macros above is important. It must be the
2749 ** same order as the LOCKING_STYLE numbers
2750 */
2751 assert(LOCKING_STYLE_POSIX==1);
2752 assert(LOCKING_STYLE_NONE==2);
2753 assert(LOCKING_STYLE_DOTFILE==3);
2754 assert(LOCKING_STYLE_FLOCK==4);
2755 assert(LOCKING_STYLE_AFP==5);
chw97185482008-11-17 08:05:31 +00002756 assert(LOCKING_STYLE_NAMEDSEM==6);
drh218c5082008-03-07 00:27:10 +00002757
danielk197717b90b52008-06-06 11:11:25 +00002758 assert( pNew->pLock==NULL );
2759 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00002760
danielk1977a03396a2008-11-19 14:35:46 +00002761 /* Parameter isDelete is only used on vxworks. Parameter pVfs is only
2762 ** used if ENABLE_LOCKING_STYLE is defined. Express this explicitly
2763 ** here to prevent compiler warnings about unused parameters.
2764 */
danielk1977f3d3c272008-11-19 16:52:44 +00002765 if( !IS_VXWORKS ) UNUSED_PARAMETER(isDelete);
2766 if( !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(pVfs);
2767 if( !IS_VXWORKS && !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(zFilename);
danielk1977a03396a2008-11-19 14:35:46 +00002768
drh218c5082008-03-07 00:27:10 +00002769 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00002770 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00002771 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00002772 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00002773
danielk1977c70dfc42008-11-19 13:52:30 +00002774#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00002775 {
2776 HashElem *pElem;
2777 char *zRealname = vxrealpath(zFilename, 1);
2778 int n;
2779 pNew->zRealpath = 0;
2780 if( !zRealname ){
2781 rc = SQLITE_NOMEM;
2782 eLockingStyle = LOCKING_STYLE_NONE;
2783 }else{
2784 n = strlen(zRealname) + 1;
2785 enterMutex();
2786 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
2787 if( pElem ){
2788 long cnt = (long)pElem->data;
2789 cnt++;
2790 pNew->zRealpath = pElem->pKey;
2791 pElem->data = (void*)cnt;
2792 }else{
2793 if( sqlite3HashInsert(&nameHash, zRealname, n, (void*)1)==0 ){
2794 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
2795 if( pElem ){
2796 pNew->zRealpath = pElem->pKey;
2797 }else{
2798 sqlite3HashInsert(&nameHash, zRealname, n, 0);
2799 rc = SQLITE_NOMEM;
2800 eLockingStyle = LOCKING_STYLE_NONE;
2801 }
2802 }
2803 }
2804 leaveMutex();
2805 sqlite3_free(zRealname);
2806 }
2807 }
2808#endif
2809
drhda0e7682008-07-30 15:27:54 +00002810 if( noLock ){
2811 eLockingStyle = LOCKING_STYLE_NONE;
2812 }else{
2813 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
2814 }
danielk1977e339d652008-06-28 11:23:00 +00002815
2816 switch( eLockingStyle ){
2817
2818 case LOCKING_STYLE_POSIX: {
2819 enterMutex();
danielk1977c70dfc42008-11-19 13:52:30 +00002820#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00002821 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
2822#else
danielk1977e339d652008-06-28 11:23:00 +00002823 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
chw97185482008-11-17 08:05:31 +00002824#endif
danielk1977e339d652008-06-28 11:23:00 +00002825 leaveMutex();
drh218c5082008-03-07 00:27:10 +00002826 break;
drhbfe66312006-10-03 17:40:40 +00002827 }
danielk1977e339d652008-06-28 11:23:00 +00002828
drh40bbb0a2008-09-23 10:23:26 +00002829#if SQLITE_ENABLE_LOCKING_STYLE
chw97185482008-11-17 08:05:31 +00002830
danielk1977c70dfc42008-11-19 13:52:30 +00002831#if !IS_VXWORKS
danielk1977e339d652008-06-28 11:23:00 +00002832 case LOCKING_STYLE_AFP: {
2833 /* AFP locking uses the file path so it needs to be included in
2834 ** the afpLockingContext.
2835 */
2836 afpLockingContext *pCtx;
2837 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
2838 if( pCtx==0 ){
2839 rc = SQLITE_NOMEM;
2840 }else{
2841 /* NB: zFilename exists and remains valid until the file is closed
2842 ** according to requirement F11141. So we do not need to make a
2843 ** copy of the filename. */
2844 pCtx->filePath = zFilename;
2845 srandomdev();
2846 }
drh218c5082008-03-07 00:27:10 +00002847 break;
danielk1977e339d652008-06-28 11:23:00 +00002848 }
chw97185482008-11-17 08:05:31 +00002849#endif
danielk1977e339d652008-06-28 11:23:00 +00002850
2851 case LOCKING_STYLE_DOTFILE: {
2852 /* Dotfile locking uses the file path so it needs to be included in
2853 ** the dotlockLockingContext
2854 */
2855 char *zLockFile;
drh218c5082008-03-07 00:27:10 +00002856 int nFilename;
danielk1977e339d652008-06-28 11:23:00 +00002857 nFilename = strlen(zFilename) + 6;
2858 zLockFile = (char *)sqlite3_malloc(nFilename);
2859 if( zLockFile==0 ){
2860 rc = SQLITE_NOMEM;
2861 }else{
2862 sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
drh339eb0b2008-03-07 15:34:11 +00002863 }
danielk1977e339d652008-06-28 11:23:00 +00002864 pNew->lockingContext = zLockFile;
drh218c5082008-03-07 00:27:10 +00002865 break;
2866 }
danielk1977e339d652008-06-28 11:23:00 +00002867
danielk1977c70dfc42008-11-19 13:52:30 +00002868#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00002869 case LOCKING_STYLE_NAMEDSEM: {
2870 /* Named semaphore locking uses the file path so it needs to be
2871 ** included in the namedsemLockingContext
2872 */
2873 enterMutex();
2874 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
2875 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
2876 char *zSemName = pNew->pOpen->aSemName;
2877 int n;
2878 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem", pNew->zRealpath);
2879 for( n=0; zSemName[n]; n++ )
2880 if( zSemName[n]=='/' ) zSemName[n] = '_';
2881 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
2882 if( pNew->pOpen->pSem == SEM_FAILED ){
2883 rc = SQLITE_NOMEM;
2884 pNew->pOpen->aSemName[0] = '\0';
2885 }
2886 }
2887 leaveMutex();
2888 break;
2889 }
2890#endif
2891
danielk1977e339d652008-06-28 11:23:00 +00002892 case LOCKING_STYLE_FLOCK:
2893 case LOCKING_STYLE_NONE:
drh218c5082008-03-07 00:27:10 +00002894 break;
drhe78669b2007-06-29 12:04:26 +00002895#endif
danielk1977e339d652008-06-28 11:23:00 +00002896 }
aswift5b1a2562008-08-22 00:22:35 +00002897
2898 pNew->lastErrno = 0;
danielk1977c70dfc42008-11-19 13:52:30 +00002899#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00002900 if( rc!=SQLITE_OK ){
2901 unlink(zFilename);
2902 isDelete = 0;
2903 }
2904 pNew->isDelete = isDelete;
2905#endif
danielk1977e339d652008-06-28 11:23:00 +00002906 if( rc!=SQLITE_OK ){
danielk19777c055b92007-10-30 17:28:51 +00002907 if( dirfd>=0 ) close(dirfd);
drhbfe66312006-10-03 17:40:40 +00002908 close(h);
danielk1977e339d652008-06-28 11:23:00 +00002909 }else{
danielk19776cb427f2008-06-30 10:16:04 +00002910 pNew->pMethod = &aIoMethod[eLockingStyle-1];
danielk1977e339d652008-06-28 11:23:00 +00002911 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00002912 }
danielk1977e339d652008-06-28 11:23:00 +00002913 return rc;
drh054889e2005-11-30 03:20:31 +00002914}
drh9c06c952005-11-26 00:25:00 +00002915
danielk1977ad94b582007-08-20 06:44:22 +00002916/*
2917** Open a file descriptor to the directory containing file zFilename.
2918** If successful, *pFd is set to the opened file descriptor and
2919** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2920** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2921** value.
2922**
2923** If SQLITE_OK is returned, the caller is responsible for closing
2924** the file descriptor *pFd using close().
2925*/
danielk1977fee2d252007-08-18 10:59:19 +00002926static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00002927 int ii;
drh777b17a2007-09-20 10:02:54 +00002928 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00002929 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00002930
drh153c62c2007-08-24 03:51:33 +00002931 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00002932 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2933 if( ii>0 ){
2934 zDirname[ii] = '\0';
2935 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00002936 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002937#ifdef FD_CLOEXEC
2938 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2939#endif
2940 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2941 }
2942 }
danielk1977fee2d252007-08-18 10:59:19 +00002943 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00002944 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00002945}
2946
danielk1977b4b47412007-08-17 15:53:36 +00002947/*
danielk197717b90b52008-06-06 11:11:25 +00002948** Create a temporary file name in zBuf. zBuf must be allocated
2949** by the calling process and must be big enough to hold at least
2950** pVfs->mxPathname bytes.
2951*/
2952static int getTempname(int nBuf, char *zBuf){
2953 static const char *azDirs[] = {
2954 0,
2955 "/var/tmp",
2956 "/usr/tmp",
2957 "/tmp",
2958 ".",
2959 };
2960 static const unsigned char zChars[] =
2961 "abcdefghijklmnopqrstuvwxyz"
2962 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2963 "0123456789";
2964 int i, j;
2965 struct stat buf;
2966 const char *zDir = ".";
2967
2968 /* It's odd to simulate an io-error here, but really this is just
2969 ** using the io-error infrastructure to test that SQLite handles this
2970 ** function failing.
2971 */
2972 SimulateIOError( return SQLITE_IOERR );
2973
2974 azDirs[0] = sqlite3_temp_directory;
danielk197700e13612008-11-17 19:18:54 +00002975 for(i=0; i<ArraySize(azDirs); i++){
danielk197717b90b52008-06-06 11:11:25 +00002976 if( azDirs[i]==0 ) continue;
2977 if( stat(azDirs[i], &buf) ) continue;
2978 if( !S_ISDIR(buf.st_mode) ) continue;
2979 if( access(azDirs[i], 07) ) continue;
2980 zDir = azDirs[i];
2981 break;
2982 }
2983
2984 /* Check that the output buffer is large enough for the temporary file
2985 ** name. If it is not, return SQLITE_ERROR.
2986 */
danielk197700e13612008-11-17 19:18:54 +00002987 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00002988 return SQLITE_ERROR;
2989 }
2990
2991 do{
2992 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
2993 j = strlen(zBuf);
2994 sqlite3_randomness(15, &zBuf[j]);
2995 for(i=0; i<15; i++, j++){
2996 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2997 }
2998 zBuf[j] = 0;
2999 }while( access(zBuf,0)==0 );
3000 return SQLITE_OK;
3001}
3002
3003
3004/*
danielk1977ad94b582007-08-20 06:44:22 +00003005** Open the file zPath.
3006**
danielk1977b4b47412007-08-17 15:53:36 +00003007** Previously, the SQLite OS layer used three functions in place of this
3008** one:
3009**
3010** sqlite3OsOpenReadWrite();
3011** sqlite3OsOpenReadOnly();
3012** sqlite3OsOpenExclusive();
3013**
3014** These calls correspond to the following combinations of flags:
3015**
3016** ReadWrite() -> (READWRITE | CREATE)
3017** ReadOnly() -> (READONLY)
3018** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
3019**
3020** The old OpenExclusive() accepted a boolean argument - "delFlag". If
3021** true, the file was configured to be automatically deleted when the
3022** file handle closed. To achieve the same effect using this new
3023** interface, add the DELETEONCLOSE flag to those specified above for
3024** OpenExclusive().
3025*/
3026static int unixOpen(
drh153c62c2007-08-24 03:51:33 +00003027 sqlite3_vfs *pVfs,
danielk1977b4b47412007-08-17 15:53:36 +00003028 const char *zPath,
3029 sqlite3_file *pFile,
3030 int flags,
3031 int *pOutFlags
3032){
danielk1977fee2d252007-08-18 10:59:19 +00003033 int fd = 0; /* File descriptor returned by open() */
3034 int dirfd = -1; /* Directory file descriptor */
3035 int oflags = 0; /* Flags to pass to open() */
3036 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00003037 int noLock; /* True to omit locking primitives */
danielk1977b4b47412007-08-17 15:53:36 +00003038
3039 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3040 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3041 int isCreate = (flags & SQLITE_OPEN_CREATE);
3042 int isReadonly = (flags & SQLITE_OPEN_READONLY);
3043 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3044
danielk1977fee2d252007-08-18 10:59:19 +00003045 /* If creating a master or main-file journal, this function will open
3046 ** a file-descriptor on the directory too. The first time unixSync()
3047 ** is called the directory file descriptor will be fsync()ed and close()d.
3048 */
3049 int isOpenDirectory = (isCreate &&
3050 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
3051 );
3052
danielk197717b90b52008-06-06 11:11:25 +00003053 /* If argument zPath is a NULL pointer, this function is required to open
3054 ** a temporary file. Use this buffer to store the file name in.
3055 */
3056 char zTmpname[MAX_PATHNAME+1];
3057 const char *zName = zPath;
3058
danielk1977fee2d252007-08-18 10:59:19 +00003059 /* Check the following statements are true:
3060 **
3061 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3062 ** (b) if CREATE is set, then READWRITE must also be set, and
3063 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00003064 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00003065 */
danielk1977b4b47412007-08-17 15:53:36 +00003066 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00003067 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00003068 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00003069 assert(isDelete==0 || isCreate);
3070
drh33f4e022007-09-03 15:19:34 +00003071 /* The main DB, main journal, and master journal are never automatically
3072 ** deleted
3073 */
3074 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
3075 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
3076 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00003077
danielk1977fee2d252007-08-18 10:59:19 +00003078 /* Assert that the upper layer has set one of the "file-type" flags. */
3079 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
3080 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
3081 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00003082 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00003083 );
3084
danielk1977e339d652008-06-28 11:23:00 +00003085 memset(pFile, 0, sizeof(unixFile));
3086
danielk197717b90b52008-06-06 11:11:25 +00003087 if( !zName ){
3088 int rc;
3089 assert(isDelete && !isOpenDirectory);
3090 rc = getTempname(MAX_PATHNAME+1, zTmpname);
3091 if( rc!=SQLITE_OK ){
3092 return rc;
3093 }
3094 zName = zTmpname;
3095 }
3096
danielk1977b4b47412007-08-17 15:53:36 +00003097 if( isReadonly ) oflags |= O_RDONLY;
3098 if( isReadWrite ) oflags |= O_RDWR;
3099 if( isCreate ) oflags |= O_CREAT;
3100 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
3101 oflags |= (O_LARGEFILE|O_BINARY);
3102
danielk197717b90b52008-06-06 11:11:25 +00003103 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
chw97185482008-11-17 08:05:31 +00003104 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, oflags);
danielk19772f2d8c72007-08-30 16:13:33 +00003105 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00003106 /* Failed to open the file for read/write access. Try read-only. */
3107 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
3108 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00003109 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00003110 }
3111 if( fd<0 ){
3112 return SQLITE_CANTOPEN;
3113 }
3114 if( isDelete ){
danielk1977c70dfc42008-11-19 13:52:30 +00003115#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003116 zPath = zName;
3117#else
danielk197717b90b52008-06-06 11:11:25 +00003118 unlink(zName);
chw97185482008-11-17 08:05:31 +00003119#endif
danielk1977b4b47412007-08-17 15:53:36 +00003120 }
3121 if( pOutFlags ){
3122 *pOutFlags = flags;
3123 }
3124
3125 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00003126 if( isOpenDirectory ){
3127 int rc = openDirectory(zPath, &dirfd);
3128 if( rc!=SQLITE_OK ){
3129 close(fd);
3130 return rc;
3131 }
3132 }
danielk1977e339d652008-06-28 11:23:00 +00003133
3134#ifdef FD_CLOEXEC
3135 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3136#endif
3137
drhda0e7682008-07-30 15:27:54 +00003138 noLock = eType!=SQLITE_OPEN_MAIN_DB;
chw97185482008-11-17 08:05:31 +00003139 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00003140}
3141
3142/*
danielk1977fee2d252007-08-18 10:59:19 +00003143** Delete the file at zPath. If the dirSync argument is true, fsync()
3144** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00003145*/
danielk1977397d65f2008-11-19 11:35:39 +00003146static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){
danielk1977fee2d252007-08-18 10:59:19 +00003147 int rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00003148 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003149 SimulateIOError(return SQLITE_IOERR_DELETE);
3150 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00003151#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00003152 if( dirSync ){
3153 int fd;
3154 rc = openDirectory(zPath, &fd);
3155 if( rc==SQLITE_OK ){
danielk1977c70dfc42008-11-19 13:52:30 +00003156#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003157 if( fsync(fd)==-1 )
3158#else
3159 if( fsync(fd) )
3160#endif
3161 {
danielk1977fee2d252007-08-18 10:59:19 +00003162 rc = SQLITE_IOERR_DIR_FSYNC;
3163 }
3164 close(fd);
3165 }
3166 }
danielk1977d138dd82008-10-15 16:02:48 +00003167#endif
danielk1977fee2d252007-08-18 10:59:19 +00003168 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00003169}
3170
danielk197790949c22007-08-17 16:50:38 +00003171/*
3172** Test the existance of or access permissions of file zPath. The
3173** test performed depends on the value of flags:
3174**
3175** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
3176** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
3177** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
3178**
3179** Otherwise return 0.
3180*/
danielk1977861f7452008-06-05 11:39:11 +00003181static int unixAccess(
danielk1977397d65f2008-11-19 11:35:39 +00003182 sqlite3_vfs *NotUsed,
danielk1977861f7452008-06-05 11:39:11 +00003183 const char *zPath,
3184 int flags,
3185 int *pResOut
3186){
rse25c0d1a2007-09-20 08:38:14 +00003187 int amode = 0;
danielk1977397d65f2008-11-19 11:35:39 +00003188 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00003189 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00003190 switch( flags ){
3191 case SQLITE_ACCESS_EXISTS:
3192 amode = F_OK;
3193 break;
3194 case SQLITE_ACCESS_READWRITE:
3195 amode = W_OK|R_OK;
3196 break;
drh50d3f902007-08-27 21:10:36 +00003197 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00003198 amode = R_OK;
3199 break;
3200
3201 default:
3202 assert(!"Invalid flags argument");
3203 }
danielk1977861f7452008-06-05 11:39:11 +00003204 *pResOut = (access(zPath, amode)==0);
3205 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003206}
3207
danielk1977b4b47412007-08-17 15:53:36 +00003208
3209/*
3210** Turn a relative pathname into a full pathname. The relative path
3211** is stored as a nul-terminated string in the buffer pointed to by
3212** zPath.
3213**
3214** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
3215** (in this case, MAX_PATHNAME bytes). The full-path is written to
3216** this buffer before returning.
3217*/
danielk1977adfb9b02007-09-17 07:02:56 +00003218static int unixFullPathname(
3219 sqlite3_vfs *pVfs, /* Pointer to vfs object */
3220 const char *zPath, /* Possibly relative input path */
3221 int nOut, /* Size of output buffer in bytes */
3222 char *zOut /* Output buffer */
3223){
danielk1977843e65f2007-09-01 16:16:15 +00003224
3225 /* It's odd to simulate an io-error here, but really this is just
3226 ** using the io-error infrastructure to test that SQLite handles this
3227 ** function failing. This function could fail if, for example, the
3228 ** current working directly has been unlinked.
3229 */
3230 SimulateIOError( return SQLITE_ERROR );
3231
drh153c62c2007-08-24 03:51:33 +00003232 assert( pVfs->mxPathname==MAX_PATHNAME );
danielk1977f3d3c272008-11-19 16:52:44 +00003233 UNUSED_PARAMETER(pVfs);
chw97185482008-11-17 08:05:31 +00003234
danielk1977c70dfc42008-11-19 13:52:30 +00003235#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003236 {
3237 char *zRealname = vxrealpath(zPath, 0);
3238 zOut[0] = '\0';
3239 if( !zRealname ){
3240 return SQLITE_CANTOPEN;
3241 }
3242 sqlite3_snprintf(nOut, zOut, "%s", zRealname);
3243 sqlite3_free(zRealname);
3244 return SQLITE_OK;
3245 }
3246#else
drh3c7f2dc2007-12-06 13:26:20 +00003247 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00003248 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00003249 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003250 }else{
3251 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00003252 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00003253 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00003254 }
3255 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00003256 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003257 }
3258 return SQLITE_OK;
3259
3260#if 0
3261 /*
3262 ** Remove "/./" path elements and convert "/A/./" path elements
3263 ** to just "/".
3264 */
3265 if( zFull ){
3266 int i, j;
3267 for(i=j=0; zFull[i]; i++){
3268 if( zFull[i]=='/' ){
3269 if( zFull[i+1]=='/' ) continue;
3270 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
3271 i += 1;
3272 continue;
3273 }
3274 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
3275 while( j>0 && zFull[j-1]!='/' ){ j--; }
3276 i += 3;
3277 continue;
3278 }
3279 }
3280 zFull[j++] = zFull[i];
3281 }
3282 zFull[j] = 0;
3283 }
3284#endif
chw97185482008-11-17 08:05:31 +00003285#endif
danielk1977b4b47412007-08-17 15:53:36 +00003286}
3287
drh0ccebe72005-06-07 22:22:50 +00003288
drh761df872006-12-21 01:29:22 +00003289#ifndef SQLITE_OMIT_LOAD_EXTENSION
3290/*
3291** Interfaces for opening a shared library, finding entry points
3292** within the shared library, and closing the shared library.
3293*/
3294#include <dlfcn.h>
danielk1977397d65f2008-11-19 11:35:39 +00003295static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
3296 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003297 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
3298}
danielk197795c8a542007-09-01 06:51:27 +00003299
3300/*
3301** SQLite calls this function immediately after a call to unixDlSym() or
3302** unixDlOpen() fails (returns a null pointer). If a more detailed error
3303** message is available, it is written to zBufOut. If no error message
3304** is available, zBufOut is left unmodified and SQLite uses a default
3305** error message.
3306*/
danielk1977397d65f2008-11-19 11:35:39 +00003307static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00003308 char *zErr;
danielk1977397d65f2008-11-19 11:35:39 +00003309 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003310 enterMutex();
3311 zErr = dlerror();
3312 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00003313 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00003314 }
3315 leaveMutex();
3316}
danielk1977397d65f2008-11-19 11:35:39 +00003317static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
3318 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003319 return dlsym(pHandle, zSymbol);
3320}
danielk1977397d65f2008-11-19 11:35:39 +00003321static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
3322 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003323 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00003324}
danielk1977b4b47412007-08-17 15:53:36 +00003325#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
3326 #define unixDlOpen 0
3327 #define unixDlError 0
3328 #define unixDlSym 0
3329 #define unixDlClose 0
3330#endif
3331
3332/*
danielk197790949c22007-08-17 16:50:38 +00003333** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00003334*/
danielk1977397d65f2008-11-19 11:35:39 +00003335static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
3336 UNUSED_PARAMETER(NotUsed);
danielk197700e13612008-11-17 19:18:54 +00003337 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00003338
drhbbd42a62004-05-22 17:41:58 +00003339 /* We have to initialize zBuf to prevent valgrind from reporting
3340 ** errors. The reports issued by valgrind are incorrect - we would
3341 ** prefer that the randomness be increased by making use of the
3342 ** uninitialized space in zBuf - but valgrind errors tend to worry
3343 ** some users. Rather than argue, it seems easier just to initialize
3344 ** the whole array and silence valgrind, even if that means less randomness
3345 ** in the random seed.
3346 **
3347 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00003348 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00003349 ** tests repeatable.
3350 */
danielk1977b4b47412007-08-17 15:53:36 +00003351 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00003352#if !defined(SQLITE_TEST)
3353 {
drh842b8642005-01-21 17:53:17 +00003354 int pid, fd;
3355 fd = open("/dev/urandom", O_RDONLY);
3356 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00003357 time_t t;
3358 time(&t);
danielk197790949c22007-08-17 16:50:38 +00003359 memcpy(zBuf, &t, sizeof(t));
3360 pid = getpid();
3361 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00003362 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00003363 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00003364 }else{
drh72cbd072008-10-14 17:58:38 +00003365 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00003366 close(fd);
3367 }
drhbbd42a62004-05-22 17:41:58 +00003368 }
3369#endif
drh72cbd072008-10-14 17:58:38 +00003370 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00003371}
3372
danielk1977b4b47412007-08-17 15:53:36 +00003373
drhbbd42a62004-05-22 17:41:58 +00003374/*
3375** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00003376** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00003377** The return value is the number of microseconds of sleep actually
3378** requested from the underlying operating system, a number which
3379** might be greater than or equal to the argument, but not less
3380** than the argument.
drhbbd42a62004-05-22 17:41:58 +00003381*/
danielk1977397d65f2008-11-19 11:35:39 +00003382static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
danielk1977c70dfc42008-11-19 13:52:30 +00003383#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003384 struct timespec sp;
3385
3386 sp.tv_sec = microseconds / 1000000;
3387 sp.tv_nsec = (microseconds % 1000000) * 1000;
3388 nanosleep(&sp, NULL);
danielk1977397d65f2008-11-19 11:35:39 +00003389 return microseconds;
3390#elif defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00003391 usleep(microseconds);
3392 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00003393#else
danielk1977b4b47412007-08-17 15:53:36 +00003394 int seconds = (microseconds+999999)/1000000;
3395 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00003396 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00003397#endif
danielk1977397d65f2008-11-19 11:35:39 +00003398 UNUSED_PARAMETER(NotUsed);
drh88f474a2006-01-02 20:00:12 +00003399}
3400
3401/*
drhbbd42a62004-05-22 17:41:58 +00003402** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00003403** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00003404*/
3405#ifdef SQLITE_TEST
3406int sqlite3_current_time = 0;
3407#endif
3408
3409/*
3410** Find the current time (in Universal Coordinated Time). Write the
3411** current time and date as a Julian Day number into *prNow and
3412** return 0. Return 1 if the time and date cannot be found.
3413*/
danielk1977397d65f2008-11-19 11:35:39 +00003414static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
danielk1977c70dfc42008-11-19 13:52:30 +00003415#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003416 struct timespec sNow;
3417 clock_gettime(CLOCK_REALTIME, &sNow);
3418 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
danielk1977397d65f2008-11-19 11:35:39 +00003419#elif defined(NO_GETTOD)
drhbbd42a62004-05-22 17:41:58 +00003420 time_t t;
3421 time(&t);
3422 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00003423#else
3424 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00003425 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00003426 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
3427#endif
danielk1977397d65f2008-11-19 11:35:39 +00003428
drhbbd42a62004-05-22 17:41:58 +00003429#ifdef SQLITE_TEST
3430 if( sqlite3_current_time ){
3431 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
3432 }
3433#endif
danielk1977397d65f2008-11-19 11:35:39 +00003434 UNUSED_PARAMETER(NotUsed);
drhbbd42a62004-05-22 17:41:58 +00003435 return 0;
3436}
danielk1977b4b47412007-08-17 15:53:36 +00003437
danielk1977397d65f2008-11-19 11:35:39 +00003438static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
3439 UNUSED_PARAMETER(NotUsed);
3440 UNUSED_PARAMETER(NotUsed2);
3441 UNUSED_PARAMETER(NotUsed3);
danielk1977bcb97fe2008-06-06 15:49:29 +00003442 return 0;
3443}
3444
drh153c62c2007-08-24 03:51:33 +00003445/*
danielk1977e339d652008-06-28 11:23:00 +00003446** Initialize the operating system interface.
drh153c62c2007-08-24 03:51:33 +00003447*/
danielk1977c0fa4c52008-06-25 17:19:00 +00003448int sqlite3_os_init(void){
danielk1977e339d652008-06-28 11:23:00 +00003449 /* Macro to define the static contents of an sqlite3_vfs structure for
3450 ** the unix backend. The two parameters are the values to use for
3451 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
3452 **
3453 */
3454 #define UNIXVFS(zVfsName, pVfsAppData) { \
3455 1, /* iVersion */ \
3456 sizeof(unixFile), /* szOsFile */ \
3457 MAX_PATHNAME, /* mxPathname */ \
3458 0, /* pNext */ \
3459 zVfsName, /* zName */ \
3460 (void *)pVfsAppData, /* pAppData */ \
3461 unixOpen, /* xOpen */ \
3462 unixDelete, /* xDelete */ \
3463 unixAccess, /* xAccess */ \
3464 unixFullPathname, /* xFullPathname */ \
3465 unixDlOpen, /* xDlOpen */ \
3466 unixDlError, /* xDlError */ \
3467 unixDlSym, /* xDlSym */ \
3468 unixDlClose, /* xDlClose */ \
3469 unixRandomness, /* xRandomness */ \
3470 unixSleep, /* xSleep */ \
3471 unixCurrentTime, /* xCurrentTime */ \
3472 unixGetLastError /* xGetLastError */ \
3473 }
3474
3475 static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
drh40bbb0a2008-09-23 10:23:26 +00003476#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +00003477 int i;
3478 static sqlite3_vfs aVfs[] = {
3479 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
3480 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
3481 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
3482 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
chw97185482008-11-17 08:05:31 +00003483 UNIXVFS("unix-none", LOCKING_STYLE_NONE),
3484 UNIXVFS("unix-namedsem",LOCKING_STYLE_NAMEDSEM),
drh153c62c2007-08-24 03:51:33 +00003485 };
danielk1977e339d652008-06-28 11:23:00 +00003486 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
3487 sqlite3_vfs_register(&aVfs[i], 0);
3488 }
3489#endif
danielk1977c70dfc42008-11-19 13:52:30 +00003490#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003491 sqlite3HashInit(&nameHash, 1);
3492#endif
danielk1977c0fa4c52008-06-25 17:19:00 +00003493 sqlite3_vfs_register(&unixVfs, 1);
3494 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00003495}
danielk1977e339d652008-06-28 11:23:00 +00003496
3497/*
3498** Shutdown the operating system interface. This is a no-op for unix.
3499*/
danielk1977c0fa4c52008-06-25 17:19:00 +00003500int sqlite3_os_end(void){
3501 return SQLITE_OK;
3502}
drhdce8bdb2007-08-16 13:01:44 +00003503
danielk197729bafea2008-06-26 10:41:19 +00003504#endif /* SQLITE_OS_UNIX */