blob: b59d768fd6ca54cd4a69c00a74959bbb87625877 [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**
danielk197762c14b32008-11-19 09:05:26 +000015** $Id: os_unix.c,v 1.212 2008/11/19 09:05:27 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/*
43** These #defines should enable >2GB file support on Posix if the
44** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000045** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000046**
47** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
48** on the compiler command line. This is necessary if you are compiling
49** on a recent machine (ex: RedHat 7.2) but you want your code to work
50** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
51** without this option, LFS is enable. But LFS does not exist in the kernel
52** in RedHat 6.0, so the code won't work. Hence, for maximum binary
53** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000054*/
55#ifndef SQLITE_DISABLE_LFS
56# define _LARGE_FILE 1
57# ifndef _FILE_OFFSET_BITS
58# define _FILE_OFFSET_BITS 64
59# endif
60# define _LARGEFILE_SOURCE 1
61#endif
drhbbd42a62004-05-22 17:41:58 +000062
drh9cbe6352005-11-29 03:13:21 +000063/*
64** standard include files.
65*/
66#include <sys/types.h>
67#include <sys/stat.h>
68#include <fcntl.h>
69#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000070#include <time.h>
drh19e2d372005-08-29 23:00:03 +000071#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000072#include <errno.h>
danielk1977e339d652008-06-28 11:23:00 +000073
drh40bbb0a2008-09-23 10:23:26 +000074#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +000075#include <sys/ioctl.h>
chw97185482008-11-17 08:05:31 +000076#if defined(__RTP__) || defined(_WRS_KERNEL)
77#define lstat stat
78#include <semaphore.h>
79#include <limits.h>
80#else
drhbfe66312006-10-03 17:40:40 +000081#include <sys/param.h>
82#include <sys/mount.h>
chw97185482008-11-17 08:05:31 +000083#endif
drhbfe66312006-10-03 17:40:40 +000084#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000085
86/*
drhf1a221e2006-01-15 17:27:17 +000087** If we are to be thread-safe, include the pthreads header and define
88** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +000089*/
drhd677b3d2007-08-20 22:48:41 +000090#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +000091# include <pthread.h>
92# define SQLITE_UNIX_THREADS 1
93#endif
94
95/*
96** Default permissions when creating a new file
97*/
98#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
99# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
100#endif
101
danielk1977b4b47412007-08-17 15:53:36 +0000102/*
103** Maximum supported path-length.
104*/
105#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +0000106
107
108/*
danielk1977ad94b582007-08-20 06:44:22 +0000109** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +0000110** protability layer.
drh9cbe6352005-11-29 03:13:21 +0000111*/
drh054889e2005-11-30 03:20:31 +0000112typedef struct unixFile unixFile;
113struct unixFile {
danielk197762079062007-08-15 17:08:46 +0000114 sqlite3_io_methods const *pMethod; /* Always the first entry */
danielk1977967a4a12007-08-20 14:23:44 +0000115#ifdef SQLITE_TEST
116 /* In test mode, increase the size of this structure a bit so that
117 ** it is larger than the struct CrashFile defined in test6.c.
118 */
119 char aPadding[32];
120#endif
drh9cbe6352005-11-29 03:13:21 +0000121 struct openCnt *pOpen; /* Info about all open fd's on this inode */
122 struct lockInfo *pLock; /* Info about locks on this inode */
drh40bbb0a2008-09-23 10:23:26 +0000123#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +0000124 void *lockingContext; /* Locking style specific state */
danielk1977e339d652008-06-28 11:23:00 +0000125#endif
drh9cbe6352005-11-29 03:13:21 +0000126 int h; /* The file descriptor */
127 unsigned char locktype; /* The type of lock held on this fd */
drh9cbe6352005-11-29 03:13:21 +0000128 int dirfd; /* File descriptor for the directory */
drhd677b3d2007-08-20 22:48:41 +0000129#if SQLITE_THREADSAFE
danielk1977ad94b582007-08-20 06:44:22 +0000130 pthread_t tid; /* The thread that "owns" this unixFile */
drh9cbe6352005-11-29 03:13:21 +0000131#endif
aswift5b1a2562008-08-22 00:22:35 +0000132 int lastErrno; /* The unix errno from the last I/O error */
chw97185482008-11-17 08:05:31 +0000133#if defined(__RTP__) || defined(_WRS_KERNEL)
134 int isDelete; /* Delete on close if true */
135 char *zRealpath;
136#endif
drh9cbe6352005-11-29 03:13:21 +0000137};
138
drh0ccebe72005-06-07 22:22:50 +0000139/*
drh198bf392006-01-06 21:52:49 +0000140** Include code that is common to all os_*.c files
141*/
142#include "os_common.h"
143
144/*
drh0ccebe72005-06-07 22:22:50 +0000145** Define various macros that are missing from some systems.
146*/
drhbbd42a62004-05-22 17:41:58 +0000147#ifndef O_LARGEFILE
148# define O_LARGEFILE 0
149#endif
150#ifdef SQLITE_DISABLE_LFS
151# undef O_LARGEFILE
152# define O_LARGEFILE 0
153#endif
154#ifndef O_NOFOLLOW
155# define O_NOFOLLOW 0
156#endif
157#ifndef O_BINARY
158# define O_BINARY 0
159#endif
160
161/*
162** The DJGPP compiler environment looks mostly like Unix, but it
163** lacks the fcntl() system call. So redefine fcntl() to be something
164** that always succeeds. This means that locking does not occur under
drh85b623f2007-12-13 21:54:09 +0000165** DJGPP. But it is DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000166*/
167#ifdef __DJGPP__
168# define fcntl(A,B,C) 0
169#endif
170
171/*
drh2b4b5962005-06-15 17:47:55 +0000172** The threadid macro resolves to the thread-id or to 0. Used for
173** testing and debugging only.
174*/
drhd677b3d2007-08-20 22:48:41 +0000175#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000176#define threadid pthread_self()
177#else
178#define threadid 0
179#endif
180
181/*
danielk1977ad94b582007-08-20 06:44:22 +0000182** Set or check the unixFile.tid field. This field is set when an unixFile
183** is first opened. All subsequent uses of the unixFile verify that the
184** same thread is operating on the unixFile. Some operating systems do
drh2b4b5962005-06-15 17:47:55 +0000185** not allow locks to be overridden by other threads and that restriction
186** means that sqlite3* database handles cannot be moved from one thread
187** to another. This logic makes sure a user does not try to do that
188** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000189**
danielk1977ad94b582007-08-20 06:44:22 +0000190** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
drhf1a221e2006-01-15 17:27:17 +0000191** another as long as we are running on a system that supports threads
192** overriding each others locks (which now the most common behavior)
danielk1977ad94b582007-08-20 06:44:22 +0000193** or if no locks are held. But the unixFile.pLock field needs to be
drhf1a221e2006-01-15 17:27:17 +0000194** recomputed because its key includes the thread-id. See the
195** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000196*/
drhd677b3d2007-08-20 22:48:41 +0000197#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000198# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000199# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
200 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000201#else
202# define SET_THREADID(X)
203# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000204#endif
205
drhbbd42a62004-05-22 17:41:58 +0000206/*
207** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
208** section 6.5.2.2 lines 483 through 490 specify that when a process
209** sets or clears a lock, that operation overrides any prior locks set
210** by the same process. It does not explicitly say so, but this implies
211** that it overrides locks set by the same process using a different
212** file descriptor. Consider this test case:
drhbbd42a62004-05-22 17:41:58 +0000213** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
214**
215** Suppose ./file1 and ./file2 are really the same file (because
216** one is a hard or symbolic link to the other) then if you set
217** an exclusive lock on fd1, then try to get an exclusive lock
218** on fd2, it works. I would have expected the second lock to
219** fail since there was already a lock on the file due to fd1.
220** But not so. Since both locks came from the same process, the
221** second overrides the first, even though they were on different
222** file descriptors opened on different file names.
223**
224** Bummer. If you ask me, this is broken. Badly broken. It means
225** that we cannot use POSIX locks to synchronize file access among
226** competing threads of the same process. POSIX locks will work fine
227** to synchronize access for threads in separate processes, but not
228** threads within the same process.
229**
230** To work around the problem, SQLite has to manage file locks internally
231** on its own. Whenever a new database is opened, we have to find the
232** specific inode of the database file (the inode is determined by the
233** st_dev and st_ino fields of the stat structure that fstat() fills in)
234** and check for locks already existing on that inode. When locks are
235** created or removed, we have to look at our own internal record of the
236** locks to see if another thread has previously set a lock on that same
237** inode.
238**
danielk1977ad94b582007-08-20 06:44:22 +0000239** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000240** descriptor. It is now a structure that holds the integer file
241** descriptor and a pointer to a structure that describes the internal
242** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000243** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000244** point to the same locking structure. The locking structure keeps
245** a reference count (so we will know when to delete it) and a "cnt"
246** field that tells us its internal lock status. cnt==0 means the
247** file is unlocked. cnt==-1 means the file has an exclusive lock.
248** cnt>0 means there are cnt shared locks on the file.
249**
250** Any attempt to lock or unlock a file first checks the locking
251** structure. The fcntl() system call is only invoked to set a
252** POSIX lock if the internal lock structure transitions between
253** a locked and an unlocked state.
254**
255** 2004-Jan-11:
256** More recent discoveries about POSIX advisory locks. (The more
257** I discover, the more I realize the a POSIX advisory locks are
258** an abomination.)
259**
260** If you close a file descriptor that points to a file that has locks,
261** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000262** released. To work around this problem, each unixFile structure contains
drhbbd42a62004-05-22 17:41:58 +0000263** a pointer to an openCnt structure. There is one openCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000264** per open inode, which means that multiple unixFile can point to a single
265** openCnt. When an attempt is made to close an unixFile, if there are
266** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000267** to close() the file descriptor is deferred until all of the locks clear.
268** The openCnt structure keeps a list of file descriptors that need to
269** be closed and that list is walked (and cleared) when the last lock
270** clears.
271**
272** First, under Linux threads, because each thread has a separate
273** process ID, lock operations in one thread do not override locks
274** to the same file in other threads. Linux threads behave like
275** separate processes in this respect. But, if you close a file
276** descriptor in linux threads, all locks are cleared, even locks
277** on other threads and even though the other threads have different
278** process IDs. Linux threads is inconsistent in this respect.
279** (I'm beginning to think that linux threads is an abomination too.)
280** The consequence of this all is that the hash table for the lockInfo
281** structure has to include the process id as part of its key because
282** locks in different threads are treated as distinct. But the
283** openCnt structure should not include the process id in its
284** key because close() clears lock on all threads, not just the current
285** thread. Were it not for this goofiness in linux threads, we could
286** combine the lockInfo and openCnt structures into a single structure.
drh5fdae772004-06-29 03:29:00 +0000287**
288** 2004-Jun-28:
289** On some versions of linux, threads can override each others locks.
290** On others not. Sometimes you can change the behavior on the same
291** system by setting the LD_ASSUME_KERNEL environment variable. The
292** POSIX standard is silent as to which behavior is correct, as far
293** as I can tell, so other versions of unix might show the same
294** inconsistency. There is no little doubt in my mind that posix
295** advisory locks and linux threads are profoundly broken.
296**
297** To work around the inconsistencies, we have to test at runtime
298** whether or not threads can override each others locks. This test
299** is run once, the first time any lock is attempted. A static
300** variable is set to record the results of this test for future
301** use.
drhbbd42a62004-05-22 17:41:58 +0000302*/
303
304/*
305** An instance of the following structure serves as the key used
drh5fdae772004-06-29 03:29:00 +0000306** to locate a particular lockInfo structure given its inode.
307**
308** If threads cannot override each others locks, then we set the
309** lockKey.tid field to the thread ID. If threads can override
drhf1a221e2006-01-15 17:27:17 +0000310** each others locks then tid is always set to zero. tid is omitted
311** if we compile without threading support.
drhbbd42a62004-05-22 17:41:58 +0000312*/
313struct lockKey {
drh5fdae772004-06-29 03:29:00 +0000314 dev_t dev; /* Device number */
chw97185482008-11-17 08:05:31 +0000315#if defined(__RTP__) || defined(_WRS_KERNEL)
316 void *rnam; /* Realname since inode unusable */
317#else
drh5fdae772004-06-29 03:29:00 +0000318 ino_t ino; /* Inode number */
chw97185482008-11-17 08:05:31 +0000319#endif
drhd677b3d2007-08-20 22:48:41 +0000320#if SQLITE_THREADSAFE
drhd9cb6ac2005-10-20 07:28:17 +0000321 pthread_t tid; /* Thread ID or zero if threads can override each other */
drh5fdae772004-06-29 03:29:00 +0000322#endif
drhbbd42a62004-05-22 17:41:58 +0000323};
324
325/*
326** An instance of the following structure is allocated for each open
327** inode on each thread with a different process ID. (Threads have
328** different process IDs on linux, but not on most other unixes.)
329**
danielk1977ad94b582007-08-20 06:44:22 +0000330** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000331** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000332** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000333*/
334struct lockInfo {
335 struct lockKey key; /* The lookup key */
drh2ac3ee92004-06-07 16:27:46 +0000336 int cnt; /* Number of SHARED locks held */
danielk19779a1d0ab2004-06-01 14:09:28 +0000337 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
drhbbd42a62004-05-22 17:41:58 +0000338 int nRef; /* Number of pointers to this structure */
drhda0e7682008-07-30 15:27:54 +0000339 struct lockInfo *pNext, *pPrev; /* List of all lockInfo objects */
drhbbd42a62004-05-22 17:41:58 +0000340};
341
342/*
343** An instance of the following structure serves as the key used
344** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000345** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000346*/
347struct openKey {
348 dev_t dev; /* Device number */
chw97185482008-11-17 08:05:31 +0000349#if defined(__RTP__) || defined(_WRS_KERNEL)
350 void *rnam; /* Realname since inode unusable */
351#else
drhbbd42a62004-05-22 17:41:58 +0000352 ino_t ino; /* Inode number */
chw97185482008-11-17 08:05:31 +0000353#endif
drhbbd42a62004-05-22 17:41:58 +0000354};
355
356/*
357** An instance of the following structure is allocated for each open
358** inode. This structure keeps track of the number of locks on that
359** inode. If a close is attempted against an inode that is holding
360** locks, the close is deferred until all locks clear by adding the
361** file descriptor to be closed to the pending list.
362*/
363struct openCnt {
364 struct openKey key; /* The lookup key */
365 int nRef; /* Number of pointers to this structure */
366 int nLock; /* Number of outstanding locks */
367 int nPending; /* Number of pending close() operations */
368 int *aPending; /* Malloced space holding fd's awaiting a close() */
chw97185482008-11-17 08:05:31 +0000369#if defined(__RTP__) || defined(_WRS_KERNEL)
370 sem_t *pSem; /* Named POSIX semaphore */
371 char aSemName[MAX_PATHNAME+1]; /* Name of that semaphore */
372#endif
drhda0e7682008-07-30 15:27:54 +0000373 struct openCnt *pNext, *pPrev; /* List of all openCnt objects */
drhbbd42a62004-05-22 17:41:58 +0000374};
375
drhda0e7682008-07-30 15:27:54 +0000376/*
377** List of all lockInfo and openCnt objects. This used to be a hash
378** table. But the number of objects is rarely more than a dozen and
379** never exceeds a few thousand. And lookup is not on a critical
380** path oo a simple linked list will suffice.
drhbbd42a62004-05-22 17:41:58 +0000381*/
drhda0e7682008-07-30 15:27:54 +0000382static struct lockInfo *lockList = 0;
383static struct openCnt *openList = 0;
drh5fdae772004-06-29 03:29:00 +0000384
chw97185482008-11-17 08:05:31 +0000385#if defined(__RTP__) || defined(_WRS_KERNEL)
386/*
387** This hash table is used to bind the canonical file name to a
388** unixFile structure and use the hash key (= canonical name)
389** instead of the Inode number of the file to find the matching
390** lockInfo and openCnt structures. It also helps to make the
391** name of the semaphore when LOCKING_STYLE_NAMEDSEM is used
392** for the file.
393*/
394static Hash nameHash;
395#endif
396
drhbfe66312006-10-03 17:40:40 +0000397/*
398** The locking styles are associated with the different file locking
399** capabilities supported by different file systems.
400**
401** POSIX locking style fully supports shared and exclusive byte-range locks
danielk1977e339d652008-06-28 11:23:00 +0000402** AFP locking only supports exclusive byte-range locks
drhbfe66312006-10-03 17:40:40 +0000403** FLOCK only supports a single file-global exclusive lock
404** DOTLOCK isn't a true locking style, it refers to the use of a special
405** file named the same as the database file with a '.lock' extension, this
406** can be used on file systems that do not offer any reliable file locking
407** NO locking means that no locking will be attempted, this is only used for
408** read-only file systems currently
chw97185482008-11-17 08:05:31 +0000409** NAMEDSEM is similar to DOTLOCK but uses a named semaphore instead of an
410** indicator file.
drhbfe66312006-10-03 17:40:40 +0000411** UNSUPPORTED means that no locking will be attempted, this is only used for
412** file systems that are known to be unsupported
413*/
danielk1977e339d652008-06-28 11:23:00 +0000414#define LOCKING_STYLE_POSIX 1
drhda0e7682008-07-30 15:27:54 +0000415#define LOCKING_STYLE_NONE 2
danielk1977e339d652008-06-28 11:23:00 +0000416#define LOCKING_STYLE_DOTFILE 3
drhda0e7682008-07-30 15:27:54 +0000417#define LOCKING_STYLE_FLOCK 4
danielk1977e339d652008-06-28 11:23:00 +0000418#define LOCKING_STYLE_AFP 5
chw97185482008-11-17 08:05:31 +0000419#define LOCKING_STYLE_NAMEDSEM 6
drhbfe66312006-10-03 17:40:40 +0000420
danielk1977ad94b582007-08-20 06:44:22 +0000421/*
aswift5b1a2562008-08-22 00:22:35 +0000422** Only set the lastErrno if the error code is a real error and not
423** a normal expected return code of SQLITE_BUSY or SQLITE_OK
424*/
425#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
426
427/*
danielk1977ad94b582007-08-20 06:44:22 +0000428** Helper functions to obtain and relinquish the global mutex.
429*/
danielk19770afae932008-09-24 09:12:46 +0000430static void enterMutex(void){
danielk197759f8c082008-06-18 17:09:10 +0000431 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000432}
danielk19770afae932008-09-24 09:12:46 +0000433static void leaveMutex(void){
danielk197759f8c082008-06-18 17:09:10 +0000434 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000435}
436
drhd677b3d2007-08-20 22:48:41 +0000437#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000438/*
439** This variable records whether or not threads can override each others
440** locks.
441**
442** 0: No. Threads cannot override each others locks.
443** 1: Yes. Threads can override each others locks.
444** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000445**
drh5062d3a2006-01-31 23:03:35 +0000446** On some systems, we know at compile-time if threads can override each
447** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
448** will be set appropriately. On other systems, we have to check at
449** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
450** undefined.
451**
drhf1a221e2006-01-15 17:27:17 +0000452** This variable normally has file scope only. But during testing, we make
453** it a global so that the test code can change its value in order to verify
454** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000455*/
drh5062d3a2006-01-31 23:03:35 +0000456#ifndef SQLITE_THREAD_OVERRIDE_LOCK
457# define SQLITE_THREAD_OVERRIDE_LOCK -1
458#endif
drh029b44b2006-01-15 00:13:15 +0000459#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000460int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000461#else
drh5062d3a2006-01-31 23:03:35 +0000462static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000463#endif
drh5fdae772004-06-29 03:29:00 +0000464
465/*
466** This structure holds information passed into individual test
467** threads by the testThreadLockingBehavior() routine.
468*/
469struct threadTestData {
470 int fd; /* File to be locked */
471 struct flock lock; /* The locking operation */
472 int result; /* Result of the locking operation */
473};
474
drh2b4b5962005-06-15 17:47:55 +0000475#ifdef SQLITE_LOCK_TRACE
476/*
477** Print out information about all locking operations.
478**
479** This routine is used for troubleshooting locks on multithreaded
480** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
481** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000482** turned off.
drh2b4b5962005-06-15 17:47:55 +0000483*/
484static int lockTrace(int fd, int op, struct flock *p){
485 char *zOpName, *zType;
486 int s;
487 int savedErrno;
488 if( op==F_GETLK ){
489 zOpName = "GETLK";
490 }else if( op==F_SETLK ){
491 zOpName = "SETLK";
492 }else{
493 s = fcntl(fd, op, p);
494 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
495 return s;
496 }
497 if( p->l_type==F_RDLCK ){
498 zType = "RDLCK";
499 }else if( p->l_type==F_WRLCK ){
500 zType = "WRLCK";
501 }else if( p->l_type==F_UNLCK ){
502 zType = "UNLCK";
503 }else{
504 assert( 0 );
505 }
506 assert( p->l_whence==SEEK_SET );
507 s = fcntl(fd, op, p);
508 savedErrno = errno;
509 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
510 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
511 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000512 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000513 struct flock l2;
514 l2 = *p;
515 fcntl(fd, F_GETLK, &l2);
516 if( l2.l_type==F_RDLCK ){
517 zType = "RDLCK";
518 }else if( l2.l_type==F_WRLCK ){
519 zType = "WRLCK";
520 }else if( l2.l_type==F_UNLCK ){
521 zType = "UNLCK";
522 }else{
523 assert( 0 );
524 }
525 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
526 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
527 }
528 errno = savedErrno;
529 return s;
530}
531#define fcntl lockTrace
532#endif /* SQLITE_LOCK_TRACE */
533
danielk197741a6a612008-11-11 18:34:35 +0000534#ifdef __linux__
drh5fdae772004-06-29 03:29:00 +0000535/*
danielk197741a6a612008-11-11 18:34:35 +0000536** This function is used as the main routine for a thread launched by
537** testThreadLockingBehavior(). It tests whether the shared-lock obtained
538** by the main thread in testThreadLockingBehavior() conflicts with a
539** hypothetical write-lock obtained by this thread on the same file.
540**
541** The write-lock is not actually acquired, as this is not possible if
542** the file is open in read-only mode (see ticket #3472).
543*/
drh5fdae772004-06-29 03:29:00 +0000544static void *threadLockingTest(void *pArg){
545 struct threadTestData *pData = (struct threadTestData*)pArg;
danielk197741a6a612008-11-11 18:34:35 +0000546 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
drh5fdae772004-06-29 03:29:00 +0000547 return pArg;
548}
549
550/*
551** This procedure attempts to determine whether or not threads
552** can override each others locks then sets the
553** threadsOverrideEachOthersLocks variable appropriately.
554*/
danielk19774d5238f2006-01-27 06:32:00 +0000555static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000556 int fd;
danielk197741a6a612008-11-11 18:34:35 +0000557 int rc;
558 struct threadTestData d;
559 struct flock l;
560 pthread_t t;
drh5fdae772004-06-29 03:29:00 +0000561
562 fd = dup(fd_orig);
563 if( fd<0 ) return;
danielk197741a6a612008-11-11 18:34:35 +0000564 memset(&l, 0, sizeof(l));
565 l.l_type = F_RDLCK;
566 l.l_len = 1;
567 l.l_start = 0;
568 l.l_whence = SEEK_SET;
569 rc = fcntl(fd_orig, F_SETLK, &l);
570 if( rc!=0 ) return;
571 memset(&d, 0, sizeof(d));
572 d.fd = fd;
573 d.lock = l;
574 d.lock.l_type = F_WRLCK;
575 pthread_create(&t, 0, threadLockingTest, &d);
576 pthread_join(t, 0);
drh5fdae772004-06-29 03:29:00 +0000577 close(fd);
danielk197741a6a612008-11-11 18:34:35 +0000578 if( d.result!=0 ) return;
579 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
drh5fdae772004-06-29 03:29:00 +0000580}
danielk197741a6a612008-11-11 18:34:35 +0000581#else
582/*
583** On anything other than linux, assume threads override each others locks.
584*/
585static void testThreadLockingBehavior(int fd_orig){
586 threadsOverrideEachOthersLocks = 1;
587}
588#endif /* __linux__ */
589
drhd677b3d2007-08-20 22:48:41 +0000590#endif /* SQLITE_THREADSAFE */
drh5fdae772004-06-29 03:29:00 +0000591
drhbbd42a62004-05-22 17:41:58 +0000592/*
593** Release a lockInfo structure previously allocated by findLockInfo().
594*/
595static void releaseLockInfo(struct lockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000596 if( pLock ){
597 pLock->nRef--;
598 if( pLock->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000599 if( pLock->pPrev ){
600 assert( pLock->pPrev->pNext==pLock );
601 pLock->pPrev->pNext = pLock->pNext;
602 }else{
603 assert( lockList==pLock );
604 lockList = pLock->pNext;
605 }
606 if( pLock->pNext ){
607 assert( pLock->pNext->pPrev==pLock );
608 pLock->pNext->pPrev = pLock->pPrev;
609 }
danielk1977e339d652008-06-28 11:23:00 +0000610 sqlite3_free(pLock);
611 }
drhbbd42a62004-05-22 17:41:58 +0000612 }
613}
614
615/*
616** Release a openCnt structure previously allocated by findLockInfo().
617*/
618static void releaseOpenCnt(struct openCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000619 if( pOpen ){
620 pOpen->nRef--;
621 if( pOpen->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000622 if( pOpen->pPrev ){
623 assert( pOpen->pPrev->pNext==pOpen );
624 pOpen->pPrev->pNext = pOpen->pNext;
625 }else{
626 assert( openList==pOpen );
627 openList = pOpen->pNext;
628 }
629 if( pOpen->pNext ){
630 assert( pOpen->pNext->pPrev==pOpen );
631 pOpen->pNext->pPrev = pOpen->pPrev;
632 }
633 sqlite3_free(pOpen->aPending);
danielk1977e339d652008-06-28 11:23:00 +0000634 sqlite3_free(pOpen);
635 }
drhbbd42a62004-05-22 17:41:58 +0000636 }
637}
638
chw97185482008-11-17 08:05:31 +0000639#if defined(__RTP__) || defined(_WRS_KERNEL)
640/*
641** Implementation of a realpath() like function for vxWorks
642** to determine canonical path name from given name. It does
643** not support symlinks. Neither does it handle volume prefixes.
644*/
645char *
646vxrealpath(const char *pathname, int dostat)
647{
648 struct stat sbuf;
649 int len;
650 char *where, *ptr, *last;
651 char *result, *curpath, *workpath, *namebuf;
652
653 len = pathconf(pathname, _PC_PATH_MAX);
654 if( len<0 ){
655 len = PATH_MAX;
656 }
657 result = sqlite3_malloc(len * 4);
658 if( !result ){
659 return 0;
660 }
661 curpath = result + len;
662 workpath = curpath + len;
663 namebuf = workpath + len;
664 strcpy(curpath, pathname);
665 if( *pathname!='/' ){
666 if( !getcwd(workpath, len) ){
667 sqlite3_free(result);
668 return 0;
669 }
670 }else{
671 *workpath = '\0';
672 }
673 where = curpath;
674 while( *where ){
675 if( !strcmp(where, ".") ){
676 where++;
677 continue;
678 }
679 if( !strncmp(where, "./", 2) ){
680 where += 2;
681 continue;
682 }
683 if( !strncmp(where, "../", 3) ){
684 where += 3;
685 ptr = last = workpath;
686 while( *ptr ){
687 if( *ptr=='/' ){
688 last = ptr;
689 }
690 ptr++;
691 }
692 *last = '\0';
693 continue;
694 }
695 ptr = strchr(where, '/');
696 if( !ptr ){
697 ptr = where + strlen(where) - 1;
698 }else{
699 *ptr = '\0';
700 }
701 strcpy(namebuf, workpath);
702 for( last = namebuf; *last; last++ ){
703 continue;
704 }
705 if( *--last!='/' ){
706 strcat(namebuf, "/");
707 }
708 strcat(namebuf, where);
709 where = ++ptr;
710 if( dostat ){
711 if( stat(namebuf, &sbuf)==-1 ){
712 sqlite3_free(result);
713 return 0;
714 }
715 if( (sbuf.st_mode & S_IFDIR)==S_IFDIR ){
716 strcpy(workpath, namebuf);
717 continue;
718 }
719 if( *where ){
720 sqlite3_free(result);
721 return 0;
722 }
723 }
724 strcpy(workpath, namebuf);
725 }
726 strcpy(result, workpath);
727 return result;
728}
729#endif
730
drh40bbb0a2008-09-23 10:23:26 +0000731#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +0000732/*
733** Tests a byte-range locking query to see if byte range locks are
734** supported, if not we fall back to dotlockLockingStyle.
chw97185482008-11-17 08:05:31 +0000735** On vxWorks we fall back to namedsemLockingStyle.
drhbfe66312006-10-03 17:40:40 +0000736*/
danielk1977e339d652008-06-28 11:23:00 +0000737static int testLockingStyle(int fd){
drhbfe66312006-10-03 17:40:40 +0000738 struct flock lockInfo;
danielk1977e339d652008-06-28 11:23:00 +0000739
740 /* Test byte-range lock using fcntl(). If the call succeeds,
741 ** assume that the file-system supports POSIX style locks.
742 */
drhbfe66312006-10-03 17:40:40 +0000743 lockInfo.l_len = 1;
744 lockInfo.l_start = 0;
745 lockInfo.l_whence = SEEK_SET;
746 lockInfo.l_type = F_RDLCK;
danielk1977ad94b582007-08-20 06:44:22 +0000747 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
danielk1977e339d652008-06-28 11:23:00 +0000748 return LOCKING_STYLE_POSIX;
749 }
drhbfe66312006-10-03 17:40:40 +0000750
danielk1977e339d652008-06-28 11:23:00 +0000751 /* Testing for flock() can give false positives. So if if the above
752 ** test fails, then we fall back to using dot-file style locking.
chw97185482008-11-17 08:05:31 +0000753 */
754#if defined(__RTP__) || defined(_WRS_KERNEL)
755 return LOCKING_STYLE_NAMEDSEM;
756#else
danielk1977e339d652008-06-28 11:23:00 +0000757 return LOCKING_STYLE_DOTFILE;
chw97185482008-11-17 08:05:31 +0000758#endif
drhbfe66312006-10-03 17:40:40 +0000759}
drh93a960a2008-07-10 00:32:42 +0000760#endif
drhbfe66312006-10-03 17:40:40 +0000761
762/*
danielk1977e339d652008-06-28 11:23:00 +0000763** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
764** f_fstypename entry in the statfs structure as returned by stat() for
765** the file system hosting the database file and selects the appropriate
766** locking style based on its value. These values and assignments are
767** based on Darwin/OSX behavior and have not been thoroughly tested on
drhbfe66312006-10-03 17:40:40 +0000768** other systems.
danielk1977e339d652008-06-28 11:23:00 +0000769**
770** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
771** returns LOCKING_STYLE_POSIX.
drhbfe66312006-10-03 17:40:40 +0000772*/
danielk197762c14b32008-11-19 09:05:26 +0000773#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +0000774static int detectLockingStyle(
775 sqlite3_vfs *pVfs,
danielk1977ad94b582007-08-20 06:44:22 +0000776 const char *filePath,
777 int fd
778){
chw97185482008-11-17 08:05:31 +0000779#if defined(__RTP__) || defined(_WRS_KERNEL)
780 if( !filePath ){
781 return LOCKING_STYLE_NONE;
782 }
783 if( pVfs->pAppData ){
784 return SQLITE_PTR_TO_INT(pVfs->pAppData);
785 }
786 if (access(filePath, 0) != -1){
787 return testLockingStyle(fd);
788 }
789#else
danielk1977e339d652008-06-28 11:23:00 +0000790 struct Mapping {
791 const char *zFilesystem;
792 int eLockingStyle;
793 } aMap[] = {
794 { "hfs", LOCKING_STYLE_POSIX },
795 { "ufs", LOCKING_STYLE_POSIX },
796 { "afpfs", LOCKING_STYLE_AFP },
aswift5b1a2562008-08-22 00:22:35 +0000797#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
798 { "smbfs", LOCKING_STYLE_AFP },
799#else
danielk1977e339d652008-06-28 11:23:00 +0000800 { "smbfs", LOCKING_STYLE_FLOCK },
aswift5b1a2562008-08-22 00:22:35 +0000801#endif
danielk1977e339d652008-06-28 11:23:00 +0000802 { "msdos", LOCKING_STYLE_DOTFILE },
803 { "webdav", LOCKING_STYLE_NONE },
804 { 0, 0 }
805 };
806 int i;
drhbfe66312006-10-03 17:40:40 +0000807 struct statfs fsInfo;
808
danielk1977e339d652008-06-28 11:23:00 +0000809 if( !filePath ){
810 return LOCKING_STYLE_NONE;
drh339eb0b2008-03-07 15:34:11 +0000811 }
danielk1977e339d652008-06-28 11:23:00 +0000812 if( pVfs->pAppData ){
aswiftf54b1b32008-08-22 18:41:37 +0000813 return SQLITE_PTR_TO_INT(pVfs->pAppData);
drh339eb0b2008-03-07 15:34:11 +0000814 }
drhbfe66312006-10-03 17:40:40 +0000815
danielk1977e339d652008-06-28 11:23:00 +0000816 if( statfs(filePath, &fsInfo) != -1 ){
817 if( fsInfo.f_flags & MNT_RDONLY ){
818 return LOCKING_STYLE_NONE;
819 }
820 for(i=0; aMap[i].zFilesystem; i++){
821 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
822 return aMap[i].eLockingStyle;
823 }
824 }
825 }
826
827 /* Default case. Handles, amongst others, "nfs". */
828 return testLockingStyle(fd);
danielk197762c14b32008-11-19 09:05:26 +0000829#endif /* if defined(__RTP__) || defined(_WRS_KERNEL) */
danielk1977e339d652008-06-28 11:23:00 +0000830 return LOCKING_STYLE_POSIX;
831}
danielk197762c14b32008-11-19 09:05:26 +0000832#else
833 #define detectLockingStyle(x,y,z) LOCKING_STYLE_POSIX
834#endif /* ifdef SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000835
drhbbd42a62004-05-22 17:41:58 +0000836/*
837** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000838** describes that file descriptor. Create new ones if necessary. The
839** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000840**
drh65594042008-05-05 16:56:34 +0000841** Return an appropriate error code.
drhbbd42a62004-05-22 17:41:58 +0000842*/
drh38f82712004-06-18 17:10:16 +0000843static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000844 int fd, /* The file descriptor used in the key */
chw97185482008-11-17 08:05:31 +0000845#if defined(__RTP__) || defined(_WRS_KERNEL)
846 void *rnam, /* vxWorks realname */
847#endif
drhbbd42a62004-05-22 17:41:58 +0000848 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000849 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000850){
851 int rc;
852 struct lockKey key1;
853 struct openKey key2;
854 struct stat statbuf;
855 struct lockInfo *pLock;
856 struct openCnt *pOpen;
857 rc = fstat(fd, &statbuf);
drh65594042008-05-05 16:56:34 +0000858 if( rc!=0 ){
859#ifdef EOVERFLOW
860 if( errno==EOVERFLOW ) return SQLITE_NOLFS;
861#endif
862 return SQLITE_IOERR;
863 }
danielk1977441b09a2006-01-05 13:48:29 +0000864
drh54626242008-07-30 17:28:04 +0000865 /* On OS X on an msdos filesystem, the inode number is reported
866 ** incorrectly for zero-size files. See ticket #3260. To work
867 ** around this problem (we consider it a bug in OS X, not SQLite)
868 ** we always increase the file size to 1 by writing a single byte
869 ** prior to accessing the inode number. The one byte written is
870 ** an ASCII 'S' character which also happens to be the first byte
871 ** in the header of every SQLite database. In this way, if there
872 ** is a race condition such that another thread has already populated
873 ** the first page of the database, no damage is done.
874 */
875 if( statbuf.st_size==0 ){
876 write(fd, "S", 1);
877 rc = fstat(fd, &statbuf);
878 if( rc!=0 ){
879 return SQLITE_IOERR;
880 }
881 }
882
drhbbd42a62004-05-22 17:41:58 +0000883 memset(&key1, 0, sizeof(key1));
884 key1.dev = statbuf.st_dev;
chw97185482008-11-17 08:05:31 +0000885#if defined(__RTP__) || defined(_WRS_KERNEL)
886 key1.rnam = rnam;
887#else
drhbbd42a62004-05-22 17:41:58 +0000888 key1.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000889#endif
drhd677b3d2007-08-20 22:48:41 +0000890#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000891 if( threadsOverrideEachOthersLocks<0 ){
892 testThreadLockingBehavior(fd);
893 }
894 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
895#endif
drhbbd42a62004-05-22 17:41:58 +0000896 memset(&key2, 0, sizeof(key2));
897 key2.dev = statbuf.st_dev;
chw97185482008-11-17 08:05:31 +0000898#if defined(__RTP__) || defined(_WRS_KERNEL)
899 key2.rnam = rnam;
900#else
drhbbd42a62004-05-22 17:41:58 +0000901 key2.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000902#endif
drhda0e7682008-07-30 15:27:54 +0000903 pLock = lockList;
904 while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
905 pLock = pLock->pNext;
906 }
drhbbd42a62004-05-22 17:41:58 +0000907 if( pLock==0 ){
drh17435752007-08-16 04:30:38 +0000908 pLock = sqlite3_malloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000909 if( pLock==0 ){
drh65594042008-05-05 16:56:34 +0000910 rc = SQLITE_NOMEM;
danielk1977441b09a2006-01-05 13:48:29 +0000911 goto exit_findlockinfo;
912 }
drhbbd42a62004-05-22 17:41:58 +0000913 pLock->key = key1;
914 pLock->nRef = 1;
915 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000916 pLock->locktype = 0;
drhda0e7682008-07-30 15:27:54 +0000917 pLock->pNext = lockList;
918 pLock->pPrev = 0;
919 if( lockList ) lockList->pPrev = pLock;
920 lockList = pLock;
drhbbd42a62004-05-22 17:41:58 +0000921 }else{
922 pLock->nRef++;
923 }
924 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000925 if( ppOpen!=0 ){
drhda0e7682008-07-30 15:27:54 +0000926 pOpen = openList;
927 while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){
928 pOpen = pOpen->pNext;
929 }
drhbbd42a62004-05-22 17:41:58 +0000930 if( pOpen==0 ){
drh17435752007-08-16 04:30:38 +0000931 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000932 if( pOpen==0 ){
933 releaseLockInfo(pLock);
drh65594042008-05-05 16:56:34 +0000934 rc = SQLITE_NOMEM;
drh029b44b2006-01-15 00:13:15 +0000935 goto exit_findlockinfo;
936 }
937 pOpen->key = key2;
938 pOpen->nRef = 1;
939 pOpen->nLock = 0;
940 pOpen->nPending = 0;
941 pOpen->aPending = 0;
drhda0e7682008-07-30 15:27:54 +0000942 pOpen->pNext = openList;
943 pOpen->pPrev = 0;
944 if( openList ) openList->pPrev = pOpen;
945 openList = pOpen;
chw97185482008-11-17 08:05:31 +0000946#if defined(__RTP__) || defined(_WRS_KERNEL)
947 pOpen->pSem = NULL;
948 pOpen->aSemName[0] = '\0';
949#endif
drh029b44b2006-01-15 00:13:15 +0000950 }else{
951 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000952 }
drh029b44b2006-01-15 00:13:15 +0000953 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000954 }
danielk1977441b09a2006-01-05 13:48:29 +0000955
956exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000957 return rc;
drhbbd42a62004-05-22 17:41:58 +0000958}
959
drh64b1bea2006-01-15 02:30:57 +0000960#ifdef SQLITE_DEBUG
961/*
962** Helper function for printing out trace information from debugging
963** binaries. This returns the string represetation of the supplied
964** integer lock-type.
965*/
966static const char *locktypeName(int locktype){
967 switch( locktype ){
968 case NO_LOCK: return "NONE";
969 case SHARED_LOCK: return "SHARED";
970 case RESERVED_LOCK: return "RESERVED";
971 case PENDING_LOCK: return "PENDING";
972 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
973 }
974 return "ERROR";
975}
976#endif
977
drhbbd42a62004-05-22 17:41:58 +0000978/*
drh029b44b2006-01-15 00:13:15 +0000979** If we are currently in a different thread than the thread that the
980** unixFile argument belongs to, then transfer ownership of the unixFile
981** over to the current thread.
982**
983** A unixFile is only owned by a thread on systems where one thread is
984** unable to override locks created by a different thread. RedHat9 is
985** an example of such a system.
986**
987** Ownership transfer is only allowed if the unixFile is currently unlocked.
988** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000989** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +0000990*/
drhd677b3d2007-08-20 22:48:41 +0000991#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +0000992static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +0000993 int rc;
drh029b44b2006-01-15 00:13:15 +0000994 pthread_t hSelf;
995 if( threadsOverrideEachOthersLocks ){
996 /* Ownership transfers not needed on this system */
997 return SQLITE_OK;
998 }
999 hSelf = pthread_self();
1000 if( pthread_equal(pFile->tid, hSelf) ){
1001 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +00001002 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +00001003 return SQLITE_OK;
1004 }
1005 if( pFile->locktype!=NO_LOCK ){
1006 /* We cannot change ownership while we are holding a lock! */
1007 return SQLITE_MISUSE;
1008 }
drh4f0c5872007-03-26 22:05:01 +00001009 OSTRACE4("Transfer ownership of %d from %d to %d\n",
1010 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +00001011 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +00001012 if (pFile->pLock != NULL) {
1013 releaseLockInfo(pFile->pLock);
chw97185482008-11-17 08:05:31 +00001014#if defined(__RTP__) || defined(_WRS_KERNEL)
1015 rc = findLockInfo(pFile->h, pFile->zRealpath, &pFile->pLock, 0);
1016#else
drhbfe66312006-10-03 17:40:40 +00001017 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
chw97185482008-11-17 08:05:31 +00001018#endif
drh4f0c5872007-03-26 22:05:01 +00001019 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001020 locktypeName(pFile->locktype),
1021 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
1022 return rc;
1023 } else {
1024 return SQLITE_OK;
1025 }
drh029b44b2006-01-15 00:13:15 +00001026}
1027#else
drhf1a221e2006-01-15 17:27:17 +00001028 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +00001029# define transferOwnership(X) SQLITE_OK
1030#endif
1031
1032/*
danielk19772a6bdf62007-08-20 16:07:00 +00001033** Seek to the offset passed as the second argument, then read cnt
1034** bytes into pBuf. Return the number of bytes actually read.
drh9e0ebbf2007-10-23 15:59:18 +00001035**
1036** NB: If you define USE_PREAD or USE_PREAD64, then it might also
1037** be necessary to define _XOPEN_SOURCE to be 500. This varies from
1038** one system to another. Since SQLite does not define USE_PREAD
1039** any any form by default, we will not attempt to define _XOPEN_SOURCE.
1040** See tickets #2741 and #2681.
drhb912b282006-03-23 22:42:20 +00001041*/
danielk197762079062007-08-15 17:08:46 +00001042static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001043 int got;
drh8ebf6702007-02-06 11:11:08 +00001044 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001045 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001046#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001047 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001048 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +00001049#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001050 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001051 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +00001052#else
danielk197762079062007-08-15 17:08:46 +00001053 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +00001054 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +00001055 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +00001056 return -1;
1057 }
drhb912b282006-03-23 22:42:20 +00001058 got = read(id->h, pBuf, cnt);
1059#endif
drh15d00c42007-02-27 02:01:14 +00001060 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +00001061 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001062 return got;
1063}
1064
1065/*
drhbbd42a62004-05-22 17:41:58 +00001066** Read data from a file into a buffer. Return SQLITE_OK if all
1067** bytes were read successfully and SQLITE_IOERR if anything goes
1068** wrong.
1069*/
danielk197762079062007-08-15 17:08:46 +00001070static int unixRead(
1071 sqlite3_file *id,
1072 void *pBuf,
1073 int amt,
1074 sqlite3_int64 offset
1075){
drhbbd42a62004-05-22 17:41:58 +00001076 int got;
drh9cbe6352005-11-29 03:13:21 +00001077 assert( id );
danielk197762079062007-08-15 17:08:46 +00001078 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +00001079 if( got==amt ){
1080 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +00001081 }else if( got<0 ){
1082 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +00001083 }else{
drh4c17c3f2008-11-07 00:06:18 +00001084 /* Unread parts of the buffer must be zero-filled */
drhbafda092007-01-03 23:36:22 +00001085 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +00001086 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +00001087 }
1088}
1089
1090/*
drhb912b282006-03-23 22:42:20 +00001091** Seek to the offset in id->offset then read cnt bytes into pBuf.
1092** Return the number of bytes actually read. Update the offset.
1093*/
danielk197762079062007-08-15 17:08:46 +00001094static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001095 int got;
drh8ebf6702007-02-06 11:11:08 +00001096 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001097 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001098#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001099 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +00001100#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001101 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +00001102#else
danielk197762079062007-08-15 17:08:46 +00001103 newOffset = lseek(id->h, offset, SEEK_SET);
1104 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +00001105 return -1;
1106 }
drhb912b282006-03-23 22:42:20 +00001107 got = write(id->h, pBuf, cnt);
1108#endif
drh15d00c42007-02-27 02:01:14 +00001109 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +00001110 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001111 return got;
1112}
1113
1114
1115/*
drhbbd42a62004-05-22 17:41:58 +00001116** Write data from a buffer into a file. Return SQLITE_OK on success
1117** or some other error code on failure.
1118*/
danielk197762079062007-08-15 17:08:46 +00001119static int unixWrite(
1120 sqlite3_file *id,
1121 const void *pBuf,
1122 int amt,
1123 sqlite3_int64 offset
1124){
drhbbd42a62004-05-22 17:41:58 +00001125 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +00001126 assert( id );
drh4c7f9412005-02-03 00:29:47 +00001127 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +00001128 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +00001129 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +00001130 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +00001131 pBuf = &((char*)pBuf)[wrote];
1132 }
drh59685932006-09-14 13:47:11 +00001133 SimulateIOError(( wrote=(-1), amt=1 ));
1134 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +00001135 if( amt>0 ){
drh59685932006-09-14 13:47:11 +00001136 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +00001137 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +00001138 }else{
1139 return SQLITE_FULL;
1140 }
drhbbd42a62004-05-22 17:41:58 +00001141 }
1142 return SQLITE_OK;
1143}
1144
drhb851b2c2005-03-10 14:11:12 +00001145#ifdef SQLITE_TEST
1146/*
1147** Count the number of fullsyncs and normal syncs. This is used to test
1148** that syncs and fullsyncs are occuring at the right times.
1149*/
1150int sqlite3_sync_count = 0;
1151int sqlite3_fullsync_count = 0;
1152#endif
1153
drhf2f23912005-10-05 10:29:36 +00001154/*
1155** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
1156** Otherwise use fsync() in its place.
1157*/
1158#ifndef HAVE_FDATASYNC
1159# define fdatasync fsync
1160#endif
1161
drhac530b12006-02-11 01:25:50 +00001162/*
1163** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
1164** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
1165** only available on Mac OS X. But that could change.
1166*/
1167#ifdef F_FULLFSYNC
1168# define HAVE_FULLFSYNC 1
1169#else
1170# define HAVE_FULLFSYNC 0
1171#endif
1172
drhb851b2c2005-03-10 14:11:12 +00001173
drhbbd42a62004-05-22 17:41:58 +00001174/*
drhdd809b02004-07-17 21:44:57 +00001175** The fsync() system call does not work as advertised on many
1176** unix systems. The following procedure is an attempt to make
1177** it work better.
drh1398ad32005-01-19 23:24:50 +00001178**
1179** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
1180** for testing when we want to run through the test suite quickly.
1181** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
1182** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
1183** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +00001184*/
drheb796a72005-09-08 12:38:41 +00001185static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +00001186 int rc;
drhb851b2c2005-03-10 14:11:12 +00001187
1188 /* Record the number of times that we do a normal fsync() and
1189 ** FULLSYNC. This is used during testing to verify that this procedure
1190 ** gets called with the correct arguments.
1191 */
1192#ifdef SQLITE_TEST
1193 if( fullSync ) sqlite3_fullsync_count++;
1194 sqlite3_sync_count++;
1195#endif
1196
1197 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
1198 ** no-op
1199 */
1200#ifdef SQLITE_NO_SYNC
1201 rc = SQLITE_OK;
1202#else
1203
drhac530b12006-02-11 01:25:50 +00001204#if HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +00001205 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +00001206 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +00001207 }else{
1208 rc = 1;
1209 }
1210 /* If the FULLFSYNC failed, fall back to attempting an fsync().
1211 * It shouldn't be possible for fullfsync to fail on the local
1212 * file system (on OSX), so failure indicates that FULLFSYNC
1213 * isn't supported for this file system. So, attempt an fsync
1214 * and (for now) ignore the overhead of a superfluous fcntl call.
1215 * It'd be better to detect fullfsync support once and avoid
1216 * the fcntl call every time sync is called.
1217 */
1218 if( rc ) rc = fsync(fd);
1219
1220#else
drheb796a72005-09-08 12:38:41 +00001221 if( dataOnly ){
1222 rc = fdatasync(fd);
chw97185482008-11-17 08:05:31 +00001223#if defined(__RTP__) || defined(_WRS_KERNEL)
1224 if( rc==-1 && errno==ENOTSUP ){
1225 rc = fsync(fd);
1226 }
1227#endif
drhf2f23912005-10-05 10:29:36 +00001228 }else{
drheb796a72005-09-08 12:38:41 +00001229 rc = fsync(fd);
1230 }
aswiftae0943b2007-01-31 23:37:07 +00001231#endif /* HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +00001232#endif /* defined(SQLITE_NO_SYNC) */
1233
chw97185482008-11-17 08:05:31 +00001234#if defined(__RTP__) || defined(_WRS_KERNEL)
1235 if( rc!= -1 ){
1236 rc = 0;
1237 }
1238#endif
1239
drhdd809b02004-07-17 21:44:57 +00001240 return rc;
1241}
1242
1243/*
drhbbd42a62004-05-22 17:41:58 +00001244** Make sure all writes to a particular file are committed to disk.
1245**
drheb796a72005-09-08 12:38:41 +00001246** If dataOnly==0 then both the file itself and its metadata (file
1247** size, access time, etc) are synced. If dataOnly!=0 then only the
1248** file data is synced.
1249**
drhbbd42a62004-05-22 17:41:58 +00001250** Under Unix, also make sure that the directory entry for the file
1251** has been created by fsync-ing the directory that contains the file.
1252** If we do not do this and we encounter a power failure, the directory
1253** entry for the journal might not exist after we reboot. The next
1254** SQLite to access the file will not know that the journal exists (because
1255** the directory entry for the journal was never created) and the transaction
1256** will not roll back - possibly leading to database corruption.
1257*/
danielk197790949c22007-08-17 16:50:38 +00001258static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +00001259 int rc;
drh054889e2005-11-30 03:20:31 +00001260 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +00001261
danielk1977f036aef2007-08-20 05:36:51 +00001262 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
1263 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
1264
danielk1977c16d4632007-08-30 14:49:58 +00001265 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
danielk1977f036aef2007-08-20 05:36:51 +00001266 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
1267 || (flags&0x0F)==SQLITE_SYNC_FULL
danielk1977f036aef2007-08-20 05:36:51 +00001268 );
danielk197790949c22007-08-17 16:50:38 +00001269
danielk1977cd3b3c82008-09-22 11:46:32 +00001270 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
1271 ** line is to test that doing so does not cause any problems.
1272 */
1273 SimulateDiskfullError( return SQLITE_FULL );
1274
drh054889e2005-11-30 03:20:31 +00001275 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001276 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001277 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001278 SimulateIOError( rc=1 );
1279 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001280 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001281 }
drh054889e2005-11-30 03:20:31 +00001282 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001283 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001284 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001285#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001286 /* The directory sync is only attempted if full_fsync is
1287 ** turned off or unavailable. If a full_fsync occurred above,
1288 ** then the directory sync is superfluous.
1289 */
danielk197790949c22007-08-17 16:50:38 +00001290 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001291 /*
1292 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001293 ** errors when applied to directories on certain file systems.
1294 ** A failed directory sync is not a big deal. So it seems
1295 ** better to ignore the error. Ticket #1657
1296 */
1297 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001298 }
danielk1977d7c03f72005-11-25 10:38:22 +00001299#endif
drh054889e2005-11-30 03:20:31 +00001300 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1301 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001302 }
drha2854222004-06-17 19:04:17 +00001303 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001304}
1305
1306/*
1307** Truncate an open file to a specified size
1308*/
danielk197762079062007-08-15 17:08:46 +00001309static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001310 int rc;
drh9cbe6352005-11-29 03:13:21 +00001311 assert( id );
drh93aed5a2008-01-16 17:46:38 +00001312 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
drh63fff5f2007-06-19 10:50:38 +00001313 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001314 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001315 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001316 }else{
1317 return SQLITE_OK;
1318 }
drhbbd42a62004-05-22 17:41:58 +00001319}
1320
1321/*
1322** Determine the current size of a file in bytes
1323*/
danielk197762079062007-08-15 17:08:46 +00001324static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001325 int rc;
drhbbd42a62004-05-22 17:41:58 +00001326 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001327 assert( id );
drh59685932006-09-14 13:47:11 +00001328 rc = fstat(((unixFile*)id)->h, &buf);
1329 SimulateIOError( rc=1 );
1330 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001331 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001332 }
1333 *pSize = buf.st_size;
drh54626242008-07-30 17:28:04 +00001334
1335 /* When opening a zero-size database, the findLockInfo() procedure
1336 ** writes a single byte into that file in order to work around a bug
1337 ** in the OS-X msdos filesystem. In order to avoid problems with upper
1338 ** layers, we need to report this file size as zero even though it is
1339 ** really 1. Ticket #3260.
1340 */
1341 if( *pSize==1 ) *pSize = 0;
1342
1343
drhbbd42a62004-05-22 17:41:58 +00001344 return SQLITE_OK;
1345}
1346
danielk19779a1d0ab2004-06-01 14:09:28 +00001347/*
aswift5b1a2562008-08-22 00:22:35 +00001348** This routine translates a standard POSIX errno code into something
1349** useful to the clients of the sqlite3 functions. Specifically, it is
1350** intended to translate a variety of "try again" errors into SQLITE_BUSY
1351** and a variety of "please close the file descriptor NOW" errors into
1352** SQLITE_IOERR
1353**
1354** Errors during initialization of locks, or file system support for locks,
1355** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
1356*/
1357static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
1358 switch (posixError) {
1359 case 0:
1360 return SQLITE_OK;
1361
1362 case EAGAIN:
1363 case ETIMEDOUT:
1364 case EBUSY:
1365 case EINTR:
1366 case ENOLCK:
1367 /* random NFS retry error, unless during file system support
1368 * introspection, in which it actually means what it says */
1369 return SQLITE_BUSY;
1370
1371 case EACCES:
1372 /* EACCES is like EAGAIN during locking operations, but not any other time*/
1373 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
1374 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
1375 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
1376 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
1377 return SQLITE_BUSY;
1378 }
1379 /* else fall through */
1380 case EPERM:
1381 return SQLITE_PERM;
1382
1383 case EDEADLK:
1384 return SQLITE_IOERR_BLOCKED;
1385
drhf489c452008-08-22 00:47:53 +00001386#if EOPNOTSUPP!=ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001387 case EOPNOTSUPP:
1388 /* something went terribly awry, unless during file system support
1389 * introspection, in which it actually means what it says */
drhf489c452008-08-22 00:47:53 +00001390#endif
danielk19775ad6a882008-09-15 04:20:31 +00001391#ifdef ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001392 case ENOTSUP:
1393 /* invalid fd, unless during file system support introspection, in which
1394 * it actually means what it says */
danielk19775ad6a882008-09-15 04:20:31 +00001395#endif
aswift5b1a2562008-08-22 00:22:35 +00001396 case EIO:
1397 case EBADF:
1398 case EINVAL:
1399 case ENOTCONN:
1400 case ENODEV:
1401 case ENXIO:
1402 case ENOENT:
1403 case ESTALE:
1404 case ENOSYS:
1405 /* these should force the client to close the file and reconnect */
1406
1407 default:
1408 return sqliteIOErr;
1409 }
1410}
1411
1412/*
danielk197713adf8a2004-06-03 16:08:41 +00001413** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001414** file by this or any other process. If such a lock is held, set *pResOut
1415** to a non-zero value otherwise *pResOut is set to zero. The return value
1416** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001417*/
danielk1977861f7452008-06-05 11:39:11 +00001418static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001419 int rc = SQLITE_OK;
1420 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001421 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001422
danielk1977861f7452008-06-05 11:39:11 +00001423 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1424
drh054889e2005-11-30 03:20:31 +00001425 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001426 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001427
1428 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001429 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001430 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001431 }
1432
drh2ac3ee92004-06-07 16:27:46 +00001433 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001434 */
aswift5b1a2562008-08-22 00:22:35 +00001435 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001436 struct flock lock;
1437 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001438 lock.l_start = RESERVED_BYTE;
1439 lock.l_len = 1;
1440 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001441 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1442 int tErrno = errno;
1443 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1444 pFile->lastErrno = tErrno;
1445 } else if( lock.l_type!=F_UNLCK ){
1446 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001447 }
1448 }
1449
danielk1977b4b47412007-08-17 15:53:36 +00001450 leaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001451 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001452
aswift5b1a2562008-08-22 00:22:35 +00001453 *pResOut = reserved;
1454 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001455}
1456
1457/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001458** Lock the file with the lock specified by parameter locktype - one
1459** of the following:
1460**
drh2ac3ee92004-06-07 16:27:46 +00001461** (1) SHARED_LOCK
1462** (2) RESERVED_LOCK
1463** (3) PENDING_LOCK
1464** (4) EXCLUSIVE_LOCK
1465**
drhb3e04342004-06-08 00:47:47 +00001466** Sometimes when requesting one lock state, additional lock states
1467** are inserted in between. The locking might fail on one of the later
1468** transitions leaving the lock state different from what it started but
1469** still short of its goal. The following chart shows the allowed
1470** transitions and the inserted intermediate states:
1471**
1472** UNLOCKED -> SHARED
1473** SHARED -> RESERVED
1474** SHARED -> (PENDING) -> EXCLUSIVE
1475** RESERVED -> (PENDING) -> EXCLUSIVE
1476** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001477**
drha6abd042004-06-09 17:37:22 +00001478** This routine will only increase a lock. Use the sqlite3OsUnlock()
1479** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001480*/
danielk197762079062007-08-15 17:08:46 +00001481static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001482 /* The following describes the implementation of the various locks and
1483 ** lock transitions in terms of the POSIX advisory shared and exclusive
1484 ** lock primitives (called read-locks and write-locks below, to avoid
1485 ** confusion with SQLite lock names). The algorithms are complicated
1486 ** slightly in order to be compatible with windows systems simultaneously
1487 ** accessing the same database file, in case that is ever required.
1488 **
1489 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1490 ** byte', each single bytes at well known offsets, and the 'shared byte
1491 ** range', a range of 510 bytes at a well known offset.
1492 **
1493 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1494 ** byte'. If this is successful, a random byte from the 'shared byte
1495 ** range' is read-locked and the lock on the 'pending byte' released.
1496 **
danielk197790ba3bd2004-06-25 08:32:25 +00001497 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1498 ** A RESERVED lock is implemented by grabbing a write-lock on the
1499 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001500 **
1501 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001502 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1503 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1504 ** obtained, but existing SHARED locks are allowed to persist. A process
1505 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1506 ** This property is used by the algorithm for rolling back a journal file
1507 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001508 **
danielk197790ba3bd2004-06-25 08:32:25 +00001509 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1510 ** implemented by obtaining a write-lock on the entire 'shared byte
1511 ** range'. Since all other locks require a read-lock on one of the bytes
1512 ** within this range, this ensures that no other locks are held on the
1513 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001514 **
1515 ** The reason a single byte cannot be used instead of the 'shared byte
1516 ** range' is that some versions of windows do not support read-locks. By
1517 ** locking a random byte from a range, concurrent SHARED locks may exist
1518 ** even if the locking primitive used is always a write-lock.
1519 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001520 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001521 unixFile *pFile = (unixFile*)id;
1522 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001523 struct flock lock;
1524 int s;
1525
drh054889e2005-11-30 03:20:31 +00001526 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001527 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001528 locktypeName(locktype), locktypeName(pFile->locktype),
1529 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001530
1531 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001532 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001533 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001534 */
drh054889e2005-11-30 03:20:31 +00001535 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001536 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001537 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001538 return SQLITE_OK;
1539 }
1540
drhb3e04342004-06-08 00:47:47 +00001541 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001542 */
drh054889e2005-11-30 03:20:31 +00001543 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001544 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001545 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001546
drh054889e2005-11-30 03:20:31 +00001547 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001548 */
danielk1977b4b47412007-08-17 15:53:36 +00001549 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001550
drh029b44b2006-01-15 00:13:15 +00001551 /* Make sure the current thread owns the pFile.
1552 */
1553 rc = transferOwnership(pFile);
1554 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001555 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001556 return rc;
1557 }
drh64b1bea2006-01-15 02:30:57 +00001558 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001559
danielk1977ad94b582007-08-20 06:44:22 +00001560 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001561 ** handle that precludes the requested lock, return BUSY.
1562 */
drh054889e2005-11-30 03:20:31 +00001563 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001564 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001565 ){
1566 rc = SQLITE_BUSY;
1567 goto end_lock;
1568 }
1569
1570 /* If a SHARED lock is requested, and some thread using this PID already
1571 ** has a SHARED or RESERVED lock, then increment reference counts and
1572 ** return SQLITE_OK.
1573 */
1574 if( locktype==SHARED_LOCK &&
1575 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1576 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001577 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001578 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001579 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001580 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001581 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001582 goto end_lock;
1583 }
1584
danielk197713adf8a2004-06-03 16:08:41 +00001585 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001586
danielk19779a1d0ab2004-06-01 14:09:28 +00001587 lock.l_whence = SEEK_SET;
1588
drh3cde3bb2004-06-12 02:17:14 +00001589 /* A PENDING lock is needed before acquiring a SHARED lock and before
1590 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1591 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001592 */
drh3cde3bb2004-06-12 02:17:14 +00001593 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001594 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001595 ){
danielk1977489468c2004-06-28 08:25:47 +00001596 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001597 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001598 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001599 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001600 int tErrno = errno;
1601 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1602 if( IS_LOCK_ERROR(rc) ){
1603 pFile->lastErrno = tErrno;
1604 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001605 goto end_lock;
1606 }
drh3cde3bb2004-06-12 02:17:14 +00001607 }
1608
1609
1610 /* If control gets to this point, then actually go ahead and make
1611 ** operating system calls for the specified lock.
1612 */
1613 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001614 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001615 assert( pLock->cnt==0 );
1616 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001617
drh2ac3ee92004-06-07 16:27:46 +00001618 /* Now get the read-lock */
1619 lock.l_start = SHARED_FIRST;
1620 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001621 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1622 tErrno = errno;
1623 }
drh2ac3ee92004-06-07 16:27:46 +00001624 /* Drop the temporary PENDING lock */
1625 lock.l_start = PENDING_BYTE;
1626 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001627 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001628 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001629 if( s != -1 ){
1630 /* This could happen with a network mount */
1631 tErrno = errno;
1632 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1633 if( IS_LOCK_ERROR(rc) ){
1634 pFile->lastErrno = tErrno;
1635 }
1636 goto end_lock;
1637 }
drh2b4b5962005-06-15 17:47:55 +00001638 }
drhe2396a12007-03-29 20:19:58 +00001639 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001640 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1641 if( IS_LOCK_ERROR(rc) ){
1642 pFile->lastErrno = tErrno;
1643 }
drhbbd42a62004-05-22 17:41:58 +00001644 }else{
drh054889e2005-11-30 03:20:31 +00001645 pFile->locktype = SHARED_LOCK;
1646 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001647 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001648 }
drh3cde3bb2004-06-12 02:17:14 +00001649 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1650 /* We are trying for an exclusive lock but another thread in this
1651 ** same process is still holding a shared lock. */
1652 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001653 }else{
drh3cde3bb2004-06-12 02:17:14 +00001654 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001655 ** assumed that there is a SHARED or greater lock on the file
1656 ** already.
1657 */
drh054889e2005-11-30 03:20:31 +00001658 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001659 lock.l_type = F_WRLCK;
1660 switch( locktype ){
1661 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001662 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001663 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001664 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001665 lock.l_start = SHARED_FIRST;
1666 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001667 break;
1668 default:
1669 assert(0);
1670 }
drh054889e2005-11-30 03:20:31 +00001671 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001672 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001673 int tErrno = errno;
1674 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1675 if( IS_LOCK_ERROR(rc) ){
1676 pFile->lastErrno = tErrno;
1677 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001678 }
drhbbd42a62004-05-22 17:41:58 +00001679 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001680
danielk1977ecb2a962004-06-02 06:30:16 +00001681 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001682 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001683 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001684 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001685 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001686 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001687 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001688
1689end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001690 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001691 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001692 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001693 return rc;
1694}
1695
1696/*
drh054889e2005-11-30 03:20:31 +00001697** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001698** must be either NO_LOCK or SHARED_LOCK.
1699**
1700** If the locking level of the file descriptor is already at or below
1701** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001702*/
danielk197762079062007-08-15 17:08:46 +00001703static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001704 struct lockInfo *pLock;
1705 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001706 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001707 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001708 int h;
drha6abd042004-06-09 17:37:22 +00001709
drh054889e2005-11-30 03:20:31 +00001710 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001711 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001712 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001713
1714 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001715 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001716 return SQLITE_OK;
1717 }
drhf1a221e2006-01-15 17:27:17 +00001718 if( CHECK_THREADID(pFile) ){
1719 return SQLITE_MISUSE;
1720 }
danielk1977b4b47412007-08-17 15:53:36 +00001721 enterMutex();
drh1aa5af12008-03-07 19:51:14 +00001722 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001723 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001724 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001725 if( pFile->locktype>SHARED_LOCK ){
1726 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001727 SimulateIOErrorBenign(1);
1728 SimulateIOError( h=(-1) )
1729 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001730 if( locktype==SHARED_LOCK ){
1731 lock.l_type = F_RDLCK;
1732 lock.l_whence = SEEK_SET;
1733 lock.l_start = SHARED_FIRST;
1734 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001735 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001736 int tErrno = errno;
1737 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1738 if( IS_LOCK_ERROR(rc) ){
1739 pFile->lastErrno = tErrno;
1740 }
1741 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001742 }
1743 }
drhbbd42a62004-05-22 17:41:58 +00001744 lock.l_type = F_UNLCK;
1745 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001746 lock.l_start = PENDING_BYTE;
1747 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001748 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001749 pLock->locktype = SHARED_LOCK;
1750 }else{
aswift5b1a2562008-08-22 00:22:35 +00001751 int tErrno = errno;
1752 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1753 if( IS_LOCK_ERROR(rc) ){
1754 pFile->lastErrno = tErrno;
1755 }
1756 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001757 }
drhbbd42a62004-05-22 17:41:58 +00001758 }
drha6abd042004-06-09 17:37:22 +00001759 if( locktype==NO_LOCK ){
1760 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001761
drha6abd042004-06-09 17:37:22 +00001762 /* Decrement the shared lock counter. Release the lock using an
1763 ** OS call only when all threads in this same process have released
1764 ** the lock.
1765 */
1766 pLock->cnt--;
1767 if( pLock->cnt==0 ){
1768 lock.l_type = F_UNLCK;
1769 lock.l_whence = SEEK_SET;
1770 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001771 SimulateIOErrorBenign(1);
1772 SimulateIOError( h=(-1) )
1773 SimulateIOErrorBenign(0);
1774 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001775 pLock->locktype = NO_LOCK;
1776 }else{
aswift5b1a2562008-08-22 00:22:35 +00001777 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001778 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001779 if( IS_LOCK_ERROR(rc) ){
1780 pFile->lastErrno = tErrno;
1781 }
drh1aa5af12008-03-07 19:51:14 +00001782 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001783 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001784 }
drha6abd042004-06-09 17:37:22 +00001785 }
1786
drhbbd42a62004-05-22 17:41:58 +00001787 /* Decrement the count of locks against this same file. When the
1788 ** count reaches zero, close any other file descriptors whose close
1789 ** was deferred because of outstanding locks.
1790 */
drh1aa5af12008-03-07 19:51:14 +00001791 if( rc==SQLITE_OK ){
1792 pOpen = pFile->pOpen;
1793 pOpen->nLock--;
1794 assert( pOpen->nLock>=0 );
1795 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1796 int i;
1797 for(i=0; i<pOpen->nPending; i++){
1798 close(pOpen->aPending[i]);
1799 }
drhda0e7682008-07-30 15:27:54 +00001800 sqlite3_free(pOpen->aPending);
drh1aa5af12008-03-07 19:51:14 +00001801 pOpen->nPending = 0;
1802 pOpen->aPending = 0;
drhbbd42a62004-05-22 17:41:58 +00001803 }
drhbbd42a62004-05-22 17:41:58 +00001804 }
1805 }
aswift5b1a2562008-08-22 00:22:35 +00001806
1807end_unlock:
danielk1977b4b47412007-08-17 15:53:36 +00001808 leaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001809 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001810 return rc;
drhbbd42a62004-05-22 17:41:58 +00001811}
1812
1813/*
danielk1977e339d652008-06-28 11:23:00 +00001814** This function performs the parts of the "close file" operation
1815** common to all locking schemes. It closes the directory and file
1816** handles, if they are valid, and sets all fields of the unixFile
1817** structure to 0.
1818*/
1819static int closeUnixFile(sqlite3_file *id){
1820 unixFile *pFile = (unixFile*)id;
1821 if( pFile ){
1822 if( pFile->dirfd>=0 ){
1823 close(pFile->dirfd);
1824 }
1825 if( pFile->h>=0 ){
1826 close(pFile->h);
1827 }
chw97185482008-11-17 08:05:31 +00001828#if defined(__RTP__) || defined(_WRS_KERNEL)
1829 if( pFile->isDelete && pFile->zRealpath ){
1830 unlink(pFile->zRealpath);
1831 }
1832 if( pFile->zRealpath ){
1833 HashElem *pElem;
1834 int n = strlen(pFile->zRealpath) + 1;
1835 pElem = sqlite3HashFindElem(&nameHash, pFile->zRealpath, n);
1836 if( pElem ){
1837 long cnt = (long)pElem->data;
1838 cnt--;
1839 if( cnt==0 ){
1840 sqlite3HashInsert(&nameHash, pFile->zRealpath, n, 0);
1841 }else{
1842 pElem->data = (void*)cnt;
1843 }
1844 }
1845 }
1846#endif
danielk1977e339d652008-06-28 11:23:00 +00001847 OSTRACE2("CLOSE %-3d\n", pFile->h);
1848 OpenCounter(-1);
1849 memset(pFile, 0, sizeof(unixFile));
1850 }
1851 return SQLITE_OK;
1852}
1853
1854/*
danielk1977e3026632004-06-22 11:29:02 +00001855** Close a file.
1856*/
danielk197762079062007-08-15 17:08:46 +00001857static int unixClose(sqlite3_file *id){
danielk1977e339d652008-06-28 11:23:00 +00001858 if( id ){
1859 unixFile *pFile = (unixFile *)id;
1860 unixUnlock(id, NO_LOCK);
1861 enterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001862 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001863 /* If there are outstanding locks, do not actually close the file just
1864 ** yet because that would clear those locks. Instead, add the file
1865 ** descriptor to pOpen->aPending. It will be automatically closed when
1866 ** the last lock is cleared.
1867 */
1868 int *aNew;
1869 struct openCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001870 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001871 if( aNew==0 ){
1872 /* If a malloc fails, just leak the file descriptor */
1873 }else{
1874 pOpen->aPending = aNew;
1875 pOpen->aPending[pOpen->nPending] = pFile->h;
1876 pOpen->nPending++;
1877 pFile->h = -1;
1878 }
danielk1977e3026632004-06-22 11:29:02 +00001879 }
danielk1977e339d652008-06-28 11:23:00 +00001880 releaseLockInfo(pFile->pLock);
1881 releaseOpenCnt(pFile->pOpen);
1882 closeUnixFile(id);
1883 leaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00001884 }
drh02afc862006-01-20 18:10:57 +00001885 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001886}
1887
drhbfe66312006-10-03 17:40:40 +00001888
drh40bbb0a2008-09-23 10:23:26 +00001889#if SQLITE_ENABLE_LOCKING_STYLE
chw97185482008-11-17 08:05:31 +00001890
1891#if !defined(__RTP__) && !defined(_WRS_KERNEL)
drhbfe66312006-10-03 17:40:40 +00001892#pragma mark AFP Support
1893
1894/*
1895 ** The afpLockingContext structure contains all afp lock specific state
1896 */
1897typedef struct afpLockingContext afpLockingContext;
1898struct afpLockingContext {
drh1aa5af12008-03-07 19:51:14 +00001899 unsigned long long sharedLockByte;
drh308aa322008-03-07 20:14:38 +00001900 const char *filePath;
drhbfe66312006-10-03 17:40:40 +00001901};
1902
1903struct ByteRangeLockPB2
1904{
1905 unsigned long long offset; /* offset to first byte to lock */
1906 unsigned long long length; /* nbr of bytes to lock */
1907 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1908 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1909 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1910 int fd; /* file desc to assoc this lock with */
1911};
1912
drhfd131da2007-08-07 17:13:03 +00001913#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001914
danielk1977ad94b582007-08-20 06:44:22 +00001915/*
aswift5b1a2562008-08-22 00:22:35 +00001916 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
1917 */
danielk1977ad94b582007-08-20 06:44:22 +00001918static int _AFPFSSetLock(
1919 const char *path,
aswift5b1a2562008-08-22 00:22:35 +00001920 unixFile *pFile,
danielk1977ad94b582007-08-20 06:44:22 +00001921 unsigned long long offset,
1922 unsigned long long length,
1923 int setLockFlag
1924){
drhfd131da2007-08-07 17:13:03 +00001925 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001926 int err;
1927
1928 pb.unLockFlag = setLockFlag ? 0 : 1;
1929 pb.startEndFlag = 0;
1930 pb.offset = offset;
1931 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00001932 pb.fd = pFile->h;
drh4f0c5872007-03-26 22:05:01 +00001933 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
aswift5b1a2562008-08-22 00:22:35 +00001934 (setLockFlag?"ON":"OFF"), pFile->h, offset, length);
drhbfe66312006-10-03 17:40:40 +00001935 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1936 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00001937 int rc;
1938 int tErrno = errno;
1939 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno));
1940 rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); /* error */
1941 if( IS_LOCK_ERROR(rc) ){
1942 pFile->lastErrno = tErrno;
1943 }
1944 return rc;
drhbfe66312006-10-03 17:40:40 +00001945 } else {
aswift5b1a2562008-08-22 00:22:35 +00001946 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001947 }
1948}
1949
aswift5b1a2562008-08-22 00:22:35 +00001950/* AFP-style reserved lock checking following the behavior of
1951** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00001952static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001953 int rc = SQLITE_OK;
1954 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00001955 unixFile *pFile = (unixFile*)id;
1956
aswift5b1a2562008-08-22 00:22:35 +00001957 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1958
1959 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00001960 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1961
1962 /* Check if a thread in this process holds such a lock */
1963 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001964 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00001965 }
1966
1967 /* Otherwise see if some other process holds it.
1968 */
aswift5b1a2562008-08-22 00:22:35 +00001969 if( !reserved ){
1970 /* lock the RESERVED byte */
1971 int lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
1972 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00001973 /* if we succeeded in taking the reserved lock, unlock it to restore
1974 ** the original state */
aswift5b1a2562008-08-22 00:22:35 +00001975 lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1, 0);
1976 } else {
1977 /* if we failed to get the lock then someone else must have it */
1978 reserved = 1;
1979 }
1980 if( IS_LOCK_ERROR(lrc) ){
1981 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00001982 }
1983 }
drhbfe66312006-10-03 17:40:40 +00001984
aswift5b1a2562008-08-22 00:22:35 +00001985 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
1986
1987 *pResOut = reserved;
1988 return rc;
drhbfe66312006-10-03 17:40:40 +00001989}
1990
1991/* AFP-style locking following the behavior of unixLock, see the unixLock
1992** function comments for details of lock management. */
danielk1977e339d652008-06-28 11:23:00 +00001993static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00001994 int rc = SQLITE_OK;
1995 unixFile *pFile = (unixFile*)id;
1996 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00001997
1998 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001999 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002000 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2001
drhbfe66312006-10-03 17:40:40 +00002002 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002003 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2004 ** enterMutex() hasn't been called yet.
2005 */
drhbfe66312006-10-03 17:40:40 +00002006 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002007 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002008 locktypeName(locktype));
2009 return SQLITE_OK;
2010 }
2011
2012 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002013 */
drhbfe66312006-10-03 17:40:40 +00002014 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2015 assert( locktype!=PENDING_LOCK );
2016 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2017
2018 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002019 */
danielk1977b4b47412007-08-17 15:53:36 +00002020 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002021
2022 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002023 */
drhbfe66312006-10-03 17:40:40 +00002024 rc = transferOwnership(pFile);
2025 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00002026 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002027 return rc;
2028 }
2029
2030 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002031 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2032 ** be released.
2033 */
drhbfe66312006-10-03 17:40:40 +00002034 if( locktype==SHARED_LOCK
2035 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002036 ){
2037 int failed;
aswift5b1a2562008-08-22 00:22:35 +00002038 failed = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002039 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002040 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002041 goto afp_end_lock;
2042 }
2043 }
2044
2045 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002046 ** operating system calls for the specified lock.
2047 */
drhbfe66312006-10-03 17:40:40 +00002048 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002049 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002050
aswift5b1a2562008-08-22 00:22:35 +00002051 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002052 /* note that the quality of the randomness doesn't matter that much */
2053 lk = random();
2054 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
aswift5b1a2562008-08-22 00:22:35 +00002055 lrc1 = _AFPFSSetLock(context->filePath, pFile,
2056 SHARED_FIRST+context->sharedLockByte, 1, 1);
2057 if( IS_LOCK_ERROR(lrc1) ){
2058 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002059 }
aswift5b1a2562008-08-22 00:22:35 +00002060 /* Drop the temporary PENDING lock */
2061 lrc2 = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002062
aswift5b1a2562008-08-22 00:22:35 +00002063 if( IS_LOCK_ERROR(lrc1) ) {
2064 pFile->lastErrno = lrc1Errno;
2065 rc = lrc1;
2066 goto afp_end_lock;
2067 } else if( IS_LOCK_ERROR(lrc2) ){
2068 rc = lrc2;
2069 goto afp_end_lock;
2070 } else if( lrc1 != SQLITE_OK ) {
2071 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002072 } else {
2073 pFile->locktype = SHARED_LOCK;
2074 }
2075 }else{
2076 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2077 ** assumed that there is a SHARED or greater lock on the file
2078 ** already.
2079 */
2080 int failed = 0;
2081 assert( 0!=pFile->locktype );
2082 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2083 /* Acquire a RESERVED lock */
aswift5b1a2562008-08-22 00:22:35 +00002084 failed = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002085 }
2086 if (!failed && locktype == EXCLUSIVE_LOCK) {
2087 /* Acquire an EXCLUSIVE lock */
2088
2089 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002090 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002091 */
aswift5b1a2562008-08-22 00:22:35 +00002092 if (!(failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST +
2093 context->sharedLockByte, 1, 0))) {
drhbfe66312006-10-03 17:40:40 +00002094 /* now attemmpt to get the exclusive lock range */
aswift5b1a2562008-08-22 00:22:35 +00002095 failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002096 SHARED_SIZE, 1);
aswift5b1a2562008-08-22 00:22:35 +00002097 if (failed && (failed = _AFPFSSetLock(context->filePath, pFile,
2098 SHARED_FIRST + context->sharedLockByte, 1, 1))) {
2099 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002100 }
2101 } else {
aswift5b1a2562008-08-22 00:22:35 +00002102 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002103 }
2104 }
aswift5b1a2562008-08-22 00:22:35 +00002105 if( failed ){
2106 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002107 }
2108 }
2109
2110 if( rc==SQLITE_OK ){
2111 pFile->locktype = locktype;
2112 }else if( locktype==EXCLUSIVE_LOCK ){
2113 pFile->locktype = PENDING_LOCK;
2114 }
2115
2116afp_end_lock:
drh339eb0b2008-03-07 15:34:11 +00002117 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002118 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002119 rc==SQLITE_OK ? "ok" : "failed");
2120 return rc;
2121}
2122
2123/*
drh339eb0b2008-03-07 15:34:11 +00002124** Lower the locking level on file descriptor pFile to locktype. locktype
2125** must be either NO_LOCK or SHARED_LOCK.
2126**
2127** If the locking level of the file descriptor is already at or below
2128** the requested locking level, this routine is a no-op.
2129*/
danielk1977e339d652008-06-28 11:23:00 +00002130static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002131 int rc = SQLITE_OK;
2132 unixFile *pFile = (unixFile*)id;
2133 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2134
2135 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002136 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002137 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002138
drhbfe66312006-10-03 17:40:40 +00002139 assert( locktype<=SHARED_LOCK );
2140 if( pFile->locktype<=locktype ){
2141 return SQLITE_OK;
2142 }
2143 if( CHECK_THREADID(pFile) ){
2144 return SQLITE_MISUSE;
2145 }
danielk1977b4b47412007-08-17 15:53:36 +00002146 enterMutex();
aswift5b1a2562008-08-22 00:22:35 +00002147 int failed = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002148 if( pFile->locktype>SHARED_LOCK ){
2149 if( locktype==SHARED_LOCK ){
drhbfe66312006-10-03 17:40:40 +00002150
2151 /* unlock the exclusive range - then re-establish the shared lock */
2152 if (pFile->locktype==EXCLUSIVE_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002153 failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002154 SHARED_SIZE, 0);
2155 if (!failed) {
2156 /* successfully removed the exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002157 if ((failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST+
2158 context->sharedLockByte, 1, 1))) {
drhbfe66312006-10-03 17:40:40 +00002159 /* failed to re-establish our shared lock */
aswift5b1a2562008-08-22 00:22:35 +00002160 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002161 }
2162 } else {
aswift5b1a2562008-08-22 00:22:35 +00002163 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002164 }
2165 }
2166 }
2167 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002168 if ((failed = _AFPFSSetLock(context->filePath, pFile,
2169 PENDING_BYTE, 1, 0))){
drhbfe66312006-10-03 17:40:40 +00002170 /* failed to release the pending lock */
aswift5b1a2562008-08-22 00:22:35 +00002171 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002172 }
2173 }
2174 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00002175 if ((failed = _AFPFSSetLock(context->filePath, pFile,
2176 RESERVED_BYTE, 1, 0))) {
drhbfe66312006-10-03 17:40:40 +00002177 /* failed to release the reserved lock */
aswift5b1a2562008-08-22 00:22:35 +00002178 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002179 }
2180 }
2181 }
2182 if( locktype==NO_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002183 int failed = _AFPFSSetLock(context->filePath, pFile,
drhbfe66312006-10-03 17:40:40 +00002184 SHARED_FIRST + context->sharedLockByte, 1, 0);
2185 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002186 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002187 }
2188 }
2189 if (rc == SQLITE_OK)
2190 pFile->locktype = locktype;
danielk1977b4b47412007-08-17 15:53:36 +00002191 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002192 return rc;
2193}
2194
2195/*
drh339eb0b2008-03-07 15:34:11 +00002196** Close a file & cleanup AFP specific locking context
2197*/
danielk1977e339d652008-06-28 11:23:00 +00002198static int afpClose(sqlite3_file *id) {
2199 if( id ){
2200 unixFile *pFile = (unixFile*)id;
2201 afpUnlock(id, NO_LOCK);
2202 sqlite3_free(pFile->lockingContext);
2203 }
2204 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002205}
2206
2207
2208#pragma mark flock() style locking
2209
2210/*
drh339eb0b2008-03-07 15:34:11 +00002211** The flockLockingContext is not used
2212*/
drhbfe66312006-10-03 17:40:40 +00002213typedef void flockLockingContext;
2214
aswift5b1a2562008-08-22 00:22:35 +00002215/* flock-style reserved lock checking following the behavior of
2216 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002217static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002218 int rc = SQLITE_OK;
2219 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002220 unixFile *pFile = (unixFile*)id;
2221
aswift5b1a2562008-08-22 00:22:35 +00002222 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2223
2224 assert( pFile );
2225
2226 /* Check if a thread in this process holds such a lock */
2227 if( pFile->locktype>SHARED_LOCK ){
2228 reserved = 1;
2229 }
2230
2231 /* Otherwise see if some other process holds it. */
2232 if( !reserved ){
drh3b62b2f2007-06-08 18:27:03 +00002233 /* attempt to get the lock */
aswift5b1a2562008-08-22 00:22:35 +00002234 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
2235 if( !lrc ){
drh3b62b2f2007-06-08 18:27:03 +00002236 /* got the lock, unlock it */
aswift5b1a2562008-08-22 00:22:35 +00002237 lrc = flock(pFile->h, LOCK_UN);
2238 if ( lrc ) {
2239 int tErrno = errno;
2240 /* unlock failed with an error */
2241 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2242 if( IS_LOCK_ERROR(lrc) ){
2243 pFile->lastErrno = tErrno;
2244 rc = lrc;
2245 }
2246 }
2247 } else {
2248 int tErrno = errno;
2249 reserved = 1;
2250 /* someone else might have it reserved */
2251 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2252 if( IS_LOCK_ERROR(lrc) ){
2253 pFile->lastErrno = tErrno;
2254 rc = lrc;
2255 }
drhbfe66312006-10-03 17:40:40 +00002256 }
drhbfe66312006-10-03 17:40:40 +00002257 }
aswift5b1a2562008-08-22 00:22:35 +00002258 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002259
aswift5b1a2562008-08-22 00:22:35 +00002260 *pResOut = reserved;
2261 return rc;
drhbfe66312006-10-03 17:40:40 +00002262}
2263
danielk1977e339d652008-06-28 11:23:00 +00002264static int flockLock(sqlite3_file *id, int locktype) {
aswift5b1a2562008-08-22 00:22:35 +00002265 int rc = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002266 unixFile *pFile = (unixFile*)id;
aswift5b1a2562008-08-22 00:22:35 +00002267
2268 assert( pFile );
2269
drh3b62b2f2007-06-08 18:27:03 +00002270 /* if we already have a lock, it is exclusive.
2271 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002272 if (pFile->locktype > NO_LOCK) {
2273 pFile->locktype = locktype;
2274 return SQLITE_OK;
2275 }
2276
drh3b62b2f2007-06-08 18:27:03 +00002277 /* grab an exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002278
2279 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
2280 int tErrno = errno;
drh3b62b2f2007-06-08 18:27:03 +00002281 /* didn't get, must be busy */
aswift5b1a2562008-08-22 00:22:35 +00002282 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2283 if( IS_LOCK_ERROR(rc) ){
2284 pFile->lastErrno = tErrno;
2285 }
drhbfe66312006-10-03 17:40:40 +00002286 } else {
drh3b62b2f2007-06-08 18:27:03 +00002287 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002288 pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002289 }
aswift5b1a2562008-08-22 00:22:35 +00002290 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
2291 rc==SQLITE_OK ? "ok" : "failed");
2292 return rc;
drhbfe66312006-10-03 17:40:40 +00002293}
2294
danielk1977e339d652008-06-28 11:23:00 +00002295static int flockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002296 unixFile *pFile = (unixFile*)id;
2297
aswift5b1a2562008-08-22 00:22:35 +00002298 assert( pFile );
2299 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2300 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002301 assert( locktype<=SHARED_LOCK );
2302
drh3b62b2f2007-06-08 18:27:03 +00002303 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002304 if( pFile->locktype==locktype ){
2305 return SQLITE_OK;
2306 }
2307
drh3b62b2f2007-06-08 18:27:03 +00002308 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002309 if (locktype==SHARED_LOCK) {
2310 pFile->locktype = locktype;
2311 return SQLITE_OK;
2312 }
2313
drh3b62b2f2007-06-08 18:27:03 +00002314 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00002315 int rc = flock(pFile->h, LOCK_UN);
aswift5b1a2562008-08-22 00:22:35 +00002316 if (rc) {
2317 int r, tErrno = errno;
2318 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2319 if( IS_LOCK_ERROR(r) ){
2320 pFile->lastErrno = tErrno;
2321 }
2322 return r;
2323 } else {
drhbfe66312006-10-03 17:40:40 +00002324 pFile->locktype = NO_LOCK;
2325 return SQLITE_OK;
2326 }
2327}
2328
2329/*
drh339eb0b2008-03-07 15:34:11 +00002330** Close a file.
2331*/
danielk1977e339d652008-06-28 11:23:00 +00002332static int flockClose(sqlite3_file *id) {
2333 if( id ){
2334 flockUnlock(id, NO_LOCK);
2335 }
2336 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002337}
2338
chw97185482008-11-17 08:05:31 +00002339#endif /* !defined(__RTP__) && !defined(_WRS_KERNEL) */
2340
drhbfe66312006-10-03 17:40:40 +00002341#pragma mark Old-School .lock file based locking
2342
aswift5b1a2562008-08-22 00:22:35 +00002343/* Dotlock-style reserved lock checking following the behavior of
2344** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002345static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
aswift5b1a2562008-08-22 00:22:35 +00002346 int rc = SQLITE_OK;
2347 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002348 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002349
aswift5b1a2562008-08-22 00:22:35 +00002350 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2351
2352 assert( pFile );
2353
2354 /* Check if a thread in this process holds such a lock */
2355 if( pFile->locktype>SHARED_LOCK ){
2356 reserved = 1;
2357 }
2358
2359 /* Otherwise see if some other process holds it. */
2360 if( !reserved ){
2361 char *zLockFile = (char *)pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002362 struct stat statBuf;
aswift5b1a2562008-08-22 00:22:35 +00002363
2364 if( lstat(zLockFile, &statBuf)==0 ){
2365 /* file exists, someone else has the lock */
2366 reserved = 1;
2367 }else{
drh3b62b2f2007-06-08 18:27:03 +00002368 /* file does not exist, we could have it if we want it */
chw97185482008-11-17 08:05:31 +00002369 int tErrno = errno;
aswift5b1a2562008-08-22 00:22:35 +00002370 if( ENOENT != tErrno ){
2371 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2372 pFile->lastErrno = tErrno;
2373 }
drh339eb0b2008-03-07 15:34:11 +00002374 }
drhbfe66312006-10-03 17:40:40 +00002375 }
aswift5b1a2562008-08-22 00:22:35 +00002376 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002377
aswift5b1a2562008-08-22 00:22:35 +00002378 *pResOut = reserved;
2379 return rc;
drhbfe66312006-10-03 17:40:40 +00002380}
2381
danielk1977e339d652008-06-28 11:23:00 +00002382static int dotlockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002383 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002384 int fd;
danielk1977e339d652008-06-28 11:23:00 +00002385 char *zLockFile = (char *)pFile->lockingContext;
aswift5b1a2562008-08-22 00:22:35 +00002386 int rc=SQLITE_OK;
drh339eb0b2008-03-07 15:34:11 +00002387
drh3b62b2f2007-06-08 18:27:03 +00002388 /* if we already have a lock, it is exclusive.
2389 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002390 if (pFile->locktype > NO_LOCK) {
2391 pFile->locktype = locktype;
chw97185482008-11-17 08:05:31 +00002392#if !defined(__RTP__) && !defined(_WRS_KERNEL)
drhbfe66312006-10-03 17:40:40 +00002393 /* Always update the timestamp on the old file */
danielk1977e339d652008-06-28 11:23:00 +00002394 utimes(zLockFile, NULL);
chw97185482008-11-17 08:05:31 +00002395#endif
aswift5b1a2562008-08-22 00:22:35 +00002396 rc = SQLITE_OK;
2397 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002398 }
2399
drh3b62b2f2007-06-08 18:27:03 +00002400 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00002401 struct stat statBuf;
danielk1977e339d652008-06-28 11:23:00 +00002402 if (lstat(zLockFile,&statBuf) == 0){
aswift5b1a2562008-08-22 00:22:35 +00002403 rc = SQLITE_BUSY; /* it does, busy */
2404 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002405 }
2406
drh3b62b2f2007-06-08 18:27:03 +00002407 /* grab an exclusive lock */
danielk1977e339d652008-06-28 11:23:00 +00002408 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
drh339eb0b2008-03-07 15:34:11 +00002409 if( fd<0 ){
drh3b62b2f2007-06-08 18:27:03 +00002410 /* failed to open/create the file, someone else may have stolen the lock */
aswift5b1a2562008-08-22 00:22:35 +00002411 int tErrno = errno;
2412 if( EEXIST == tErrno ){
2413 rc = SQLITE_BUSY;
2414 } else {
2415 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2416 if( IS_LOCK_ERROR(rc) ){
2417 pFile->lastErrno = tErrno;
2418 }
2419 }
2420 goto dotlock_end_lock;
2421 }
drhbfe66312006-10-03 17:40:40 +00002422 close(fd);
2423
drh3b62b2f2007-06-08 18:27:03 +00002424 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002425 pFile->locktype = locktype;
aswift5b1a2562008-08-22 00:22:35 +00002426
2427 dotlock_end_lock:
2428 return rc;
drhbfe66312006-10-03 17:40:40 +00002429}
2430
danielk1977e339d652008-06-28 11:23:00 +00002431static int dotlockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002432 unixFile *pFile = (unixFile*)id;
danielk1977e339d652008-06-28 11:23:00 +00002433 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00002434
aswift5b1a2562008-08-22 00:22:35 +00002435 assert( pFile );
2436 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2437 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002438 assert( locktype<=SHARED_LOCK );
2439
drh3b62b2f2007-06-08 18:27:03 +00002440 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002441 if( pFile->locktype==locktype ){
2442 return SQLITE_OK;
2443 }
2444
drh3b62b2f2007-06-08 18:27:03 +00002445 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002446 if (locktype==SHARED_LOCK) {
2447 pFile->locktype = locktype;
2448 return SQLITE_OK;
2449 }
2450
drh3b62b2f2007-06-08 18:27:03 +00002451 /* no, really, unlock. */
aswift5b1a2562008-08-22 00:22:35 +00002452 if (unlink(zLockFile) ) {
2453 int rc, tErrno = errno;
2454 if( ENOENT != tErrno ){
2455 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2456 }
2457 if( IS_LOCK_ERROR(rc) ){
2458 pFile->lastErrno = tErrno;
2459 }
2460 return rc;
2461 }
drhbfe66312006-10-03 17:40:40 +00002462 pFile->locktype = NO_LOCK;
2463 return SQLITE_OK;
2464}
2465
2466/*
2467 ** Close a file.
2468 */
danielk1977e339d652008-06-28 11:23:00 +00002469static int dotlockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00002470#if defined(__RTP__) || defined(_WRS_KERNEL)
2471 int rc;
2472#endif
danielk1977e339d652008-06-28 11:23:00 +00002473 if( id ){
2474 unixFile *pFile = (unixFile*)id;
2475 dotlockUnlock(id, NO_LOCK);
2476 sqlite3_free(pFile->lockingContext);
2477 }
chw97185482008-11-17 08:05:31 +00002478#if defined(__RTP__) || defined(_WRS_KERNEL)
2479 enterMutex();
2480 rc = closeUnixFile(id);
2481 leaveMutex();
2482 return rc;
2483#else
danielk1977e339d652008-06-28 11:23:00 +00002484 return closeUnixFile(id);
chw97185482008-11-17 08:05:31 +00002485#endif
drhbfe66312006-10-03 17:40:40 +00002486}
2487
chw97185482008-11-17 08:05:31 +00002488#if defined(__RTP__) || defined(_WRS_KERNEL)
2489
2490#pragma mark POSIX/vxWorks named semaphore based locking
2491
2492/* Namedsem-style reserved lock checking following the behavior of
2493** unixCheckReservedLock, see the unixCheckReservedLock function comments */
2494static int namedsemCheckReservedLock(sqlite3_file *id, int *pResOut) {
2495 int rc = SQLITE_OK;
2496 int reserved = 0;
2497 unixFile *pFile = (unixFile*)id;
2498
2499 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2500
2501 assert( pFile );
2502
2503 /* Check if a thread in this process holds such a lock */
2504 if( pFile->locktype>SHARED_LOCK ){
2505 reserved = 1;
2506 }
2507
2508 /* Otherwise see if some other process holds it. */
2509 if( !reserved ){
2510 sem_t *pSem = pFile->pOpen->pSem;
2511 struct stat statBuf;
2512
2513 if( sem_trywait(pSem)==-1 ){
2514 int tErrno = errno;
2515 if( EAGAIN != tErrno ){
2516 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2517 pFile->lastErrno = tErrno;
2518 } else {
2519 /* someone else has the lock when we are in NO_LOCK */
2520 reserved = (pFile->locktype < SHARED_LOCK);
2521 }
2522 }else{
2523 /* we could have it if we want it */
2524 sem_post(pSem);
2525 }
2526 }
2527 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2528
2529 *pResOut = reserved;
2530 return rc;
2531}
2532
2533static int namedsemLock(sqlite3_file *id, int locktype) {
2534 unixFile *pFile = (unixFile*)id;
2535 int fd;
2536 sem_t *pSem = pFile->pOpen->pSem;
2537 int rc = SQLITE_OK;
2538
2539 /* if we already have a lock, it is exclusive.
2540 ** Just adjust level and punt on outta here. */
2541 if (pFile->locktype > NO_LOCK) {
2542 pFile->locktype = locktype;
2543 rc = SQLITE_OK;
2544 goto namedsem_end_lock;
2545 }
2546
2547 /* lock semaphore now but bail out when already locked. */
2548 if( sem_trywait(pSem)==-1 ){
2549 rc = SQLITE_BUSY;
2550 goto namedsem_end_lock;
2551 }
2552
2553 /* got it, set the type and return ok */
2554 pFile->locktype = locktype;
2555
2556 namedsem_end_lock:
2557 return rc;
2558}
2559
2560static int namedsemUnlock(sqlite3_file *id, int locktype) {
2561 unixFile *pFile = (unixFile*)id;
2562 sem_t *pSem = pFile->pOpen->pSem;
2563
2564 assert( pFile );
2565 assert( pSem );
2566 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2567 pFile->locktype, getpid());
2568 assert( locktype<=SHARED_LOCK );
2569
2570 /* no-op if possible */
2571 if( pFile->locktype==locktype ){
2572 return SQLITE_OK;
2573 }
2574
2575 /* shared can just be set because we always have an exclusive */
2576 if (locktype==SHARED_LOCK) {
2577 pFile->locktype = locktype;
2578 return SQLITE_OK;
2579 }
2580
2581 /* no, really unlock. */
2582 if ( sem_post(pSem)==-1 ) {
2583 int rc, tErrno = errno;
2584 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2585 if( IS_LOCK_ERROR(rc) ){
2586 pFile->lastErrno = tErrno;
2587 }
2588 return rc;
2589 }
2590 pFile->locktype = NO_LOCK;
2591 return SQLITE_OK;
2592}
2593
2594/*
2595 ** Close a file.
2596 */
2597static int namedsemClose(sqlite3_file *id) {
2598 if( id ){
2599 unixFile *pFile = (unixFile*)id;
2600 namedsemUnlock(id, NO_LOCK);
2601 assert( pFile );
2602 enterMutex();
2603 releaseLockInfo(pFile->pLock);
2604 releaseOpenCnt(pFile->pOpen);
2605 closeUnixFile(id);
2606 leaveMutex();
2607 }
2608 return SQLITE_OK;
2609}
2610
2611#endif /* defined(__RTP__) || defined(_WRS_KERNEL) */
drhbfe66312006-10-03 17:40:40 +00002612
drhda0e7682008-07-30 15:27:54 +00002613#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +00002614
2615/*
drh339eb0b2008-03-07 15:34:11 +00002616** The nolockLockingContext is void
2617*/
drhbfe66312006-10-03 17:40:40 +00002618typedef void nolockLockingContext;
2619
danielk1977e339d652008-06-28 11:23:00 +00002620static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) {
danielk1977861f7452008-06-05 11:39:11 +00002621 *pResOut = 0;
2622 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002623}
2624
danielk1977e339d652008-06-28 11:23:00 +00002625static int nolockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002626 return SQLITE_OK;
2627}
2628
danielk1977e339d652008-06-28 11:23:00 +00002629static int nolockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002630 return SQLITE_OK;
2631}
2632
2633/*
drh339eb0b2008-03-07 15:34:11 +00002634** Close a file.
2635*/
danielk1977e339d652008-06-28 11:23:00 +00002636static int nolockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00002637#if defined(__RTP__) || defined(_WRS_KERNEL)
2638 int rc;
2639 enterMutex();
2640 rc = closeUnixFile(id);
2641 leaveMutex();
2642 return rc;
2643#else
danielk1977e339d652008-06-28 11:23:00 +00002644 return closeUnixFile(id);
chw97185482008-11-17 08:05:31 +00002645#endif
drhbfe66312006-10-03 17:40:40 +00002646}
2647
danielk1977ad94b582007-08-20 06:44:22 +00002648
danielk1977e3026632004-06-22 11:29:02 +00002649/*
drh9e33c2c2007-08-31 18:34:59 +00002650** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00002651*/
drhcc6bb3e2007-08-31 16:11:35 +00002652static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00002653 switch( op ){
2654 case SQLITE_FCNTL_LOCKSTATE: {
2655 *(int*)pArg = ((unixFile*)id)->locktype;
2656 return SQLITE_OK;
2657 }
2658 }
drhcc6bb3e2007-08-31 16:11:35 +00002659 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00002660}
2661
2662/*
danielk1977a3d4c882007-03-23 10:08:38 +00002663** Return the sector size in bytes of the underlying block device for
2664** the specified file. This is almost always 512 bytes, but may be
2665** larger for some devices.
2666**
2667** SQLite code assumes this function cannot fail. It also assumes that
2668** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00002669** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00002670** same for both.
2671*/
danielk197762079062007-08-15 17:08:46 +00002672static int unixSectorSize(sqlite3_file *id){
drh3ceeb752007-03-29 18:19:52 +00002673 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002674}
2675
danielk197790949c22007-08-17 16:50:38 +00002676/*
2677** Return the device characteristics for the file. This is always 0.
2678*/
danielk197762079062007-08-15 17:08:46 +00002679static int unixDeviceCharacteristics(sqlite3_file *id){
2680 return 0;
2681}
2682
danielk1977a3d4c882007-03-23 10:08:38 +00002683/*
danielk1977e339d652008-06-28 11:23:00 +00002684** Initialize the contents of the unixFile structure pointed to by pId.
2685**
danielk1977ad94b582007-08-20 06:44:22 +00002686** When locking extensions are enabled, the filepath and locking style
2687** are needed to determine the unixFile pMethod to use for locking operations.
2688** The locking-style specific lockingContext data structure is created
2689** and assigned here also.
2690*/
2691static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00002692 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00002693 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00002694 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00002695 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00002696 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00002697 int noLock, /* Omit locking if true */
2698 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00002699){
drhda0e7682008-07-30 15:27:54 +00002700 int eLockingStyle;
2701 unixFile *pNew = (unixFile *)pId;
2702 int rc = SQLITE_OK;
2703
danielk1977e339d652008-06-28 11:23:00 +00002704 /* Macro to define the static contents of an sqlite3_io_methods
2705 ** structure for a unix backend file. Different locking methods
2706 ** require different functions for the xClose, xLock, xUnlock and
2707 ** xCheckReservedLock methods.
2708 */
2709 #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \
2710 1, /* iVersion */ \
2711 xClose, /* xClose */ \
2712 unixRead, /* xRead */ \
2713 unixWrite, /* xWrite */ \
2714 unixTruncate, /* xTruncate */ \
2715 unixSync, /* xSync */ \
2716 unixFileSize, /* xFileSize */ \
2717 xLock, /* xLock */ \
2718 xUnlock, /* xUnlock */ \
2719 xCheckReservedLock, /* xCheckReservedLock */ \
2720 unixFileControl, /* xFileControl */ \
2721 unixSectorSize, /* xSectorSize */ \
2722 unixDeviceCharacteristics /* xDeviceCapabilities */ \
2723 }
2724 static sqlite3_io_methods aIoMethod[] = {
2725 IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
danielk1977e339d652008-06-28 11:23:00 +00002726 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
drh40bbb0a2008-09-23 10:23:26 +00002727#if SQLITE_ENABLE_LOCKING_STYLE
drhda0e7682008-07-30 15:27:54 +00002728 ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
chw97185482008-11-17 08:05:31 +00002729#if defined(__RTP__) || defined(_WRS_KERNEL)
2730 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2731 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2732 ,IOMETHODS(namedsemClose, namedsemLock, namedsemUnlock, namedsemCheckReservedLock)
2733#else
drhda0e7682008-07-30 15:27:54 +00002734 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
danielk1977e339d652008-06-28 11:23:00 +00002735 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
chw97185482008-11-17 08:05:31 +00002736 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2737#endif
drh218c5082008-03-07 00:27:10 +00002738#endif
danielk1977e339d652008-06-28 11:23:00 +00002739 };
drhda0e7682008-07-30 15:27:54 +00002740 /* The order of the IOMETHODS macros above is important. It must be the
2741 ** same order as the LOCKING_STYLE numbers
2742 */
2743 assert(LOCKING_STYLE_POSIX==1);
2744 assert(LOCKING_STYLE_NONE==2);
2745 assert(LOCKING_STYLE_DOTFILE==3);
2746 assert(LOCKING_STYLE_FLOCK==4);
2747 assert(LOCKING_STYLE_AFP==5);
chw97185482008-11-17 08:05:31 +00002748 assert(LOCKING_STYLE_NAMEDSEM==6);
drh218c5082008-03-07 00:27:10 +00002749
danielk197717b90b52008-06-06 11:11:25 +00002750 assert( pNew->pLock==NULL );
2751 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00002752
2753 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00002754 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00002755 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00002756 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00002757
chw97185482008-11-17 08:05:31 +00002758#if defined(__RTP__) || defined(_WRS_KERNEL)
2759 {
2760 HashElem *pElem;
2761 char *zRealname = vxrealpath(zFilename, 1);
2762 int n;
2763 pNew->zRealpath = 0;
2764 if( !zRealname ){
2765 rc = SQLITE_NOMEM;
2766 eLockingStyle = LOCKING_STYLE_NONE;
2767 }else{
2768 n = strlen(zRealname) + 1;
2769 enterMutex();
2770 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
2771 if( pElem ){
2772 long cnt = (long)pElem->data;
2773 cnt++;
2774 pNew->zRealpath = pElem->pKey;
2775 pElem->data = (void*)cnt;
2776 }else{
2777 if( sqlite3HashInsert(&nameHash, zRealname, n, (void*)1)==0 ){
2778 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
2779 if( pElem ){
2780 pNew->zRealpath = pElem->pKey;
2781 }else{
2782 sqlite3HashInsert(&nameHash, zRealname, n, 0);
2783 rc = SQLITE_NOMEM;
2784 eLockingStyle = LOCKING_STYLE_NONE;
2785 }
2786 }
2787 }
2788 leaveMutex();
2789 sqlite3_free(zRealname);
2790 }
2791 }
2792#endif
2793
drhda0e7682008-07-30 15:27:54 +00002794 if( noLock ){
2795 eLockingStyle = LOCKING_STYLE_NONE;
2796 }else{
2797 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
2798 }
danielk1977e339d652008-06-28 11:23:00 +00002799
2800 switch( eLockingStyle ){
2801
2802 case LOCKING_STYLE_POSIX: {
2803 enterMutex();
chw97185482008-11-17 08:05:31 +00002804#if defined(__RTP__) || defined(_WRS_KERNEL)
2805 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
2806#else
danielk1977e339d652008-06-28 11:23:00 +00002807 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
chw97185482008-11-17 08:05:31 +00002808#endif
danielk1977e339d652008-06-28 11:23:00 +00002809 leaveMutex();
drh218c5082008-03-07 00:27:10 +00002810 break;
drhbfe66312006-10-03 17:40:40 +00002811 }
danielk1977e339d652008-06-28 11:23:00 +00002812
drh40bbb0a2008-09-23 10:23:26 +00002813#if SQLITE_ENABLE_LOCKING_STYLE
chw97185482008-11-17 08:05:31 +00002814
2815#if !defined(__RTP__) && !defined(_WRS_KERNEL)
danielk1977e339d652008-06-28 11:23:00 +00002816 case LOCKING_STYLE_AFP: {
2817 /* AFP locking uses the file path so it needs to be included in
2818 ** the afpLockingContext.
2819 */
2820 afpLockingContext *pCtx;
2821 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
2822 if( pCtx==0 ){
2823 rc = SQLITE_NOMEM;
2824 }else{
2825 /* NB: zFilename exists and remains valid until the file is closed
2826 ** according to requirement F11141. So we do not need to make a
2827 ** copy of the filename. */
2828 pCtx->filePath = zFilename;
2829 srandomdev();
2830 }
drh218c5082008-03-07 00:27:10 +00002831 break;
danielk1977e339d652008-06-28 11:23:00 +00002832 }
chw97185482008-11-17 08:05:31 +00002833#endif
danielk1977e339d652008-06-28 11:23:00 +00002834
2835 case LOCKING_STYLE_DOTFILE: {
2836 /* Dotfile locking uses the file path so it needs to be included in
2837 ** the dotlockLockingContext
2838 */
2839 char *zLockFile;
drh218c5082008-03-07 00:27:10 +00002840 int nFilename;
danielk1977e339d652008-06-28 11:23:00 +00002841 nFilename = strlen(zFilename) + 6;
2842 zLockFile = (char *)sqlite3_malloc(nFilename);
2843 if( zLockFile==0 ){
2844 rc = SQLITE_NOMEM;
2845 }else{
2846 sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
drh339eb0b2008-03-07 15:34:11 +00002847 }
danielk1977e339d652008-06-28 11:23:00 +00002848 pNew->lockingContext = zLockFile;
drh218c5082008-03-07 00:27:10 +00002849 break;
2850 }
danielk1977e339d652008-06-28 11:23:00 +00002851
chw97185482008-11-17 08:05:31 +00002852#if defined(__RTP__) || defined(_WRS_KERNEL)
2853 case LOCKING_STYLE_NAMEDSEM: {
2854 /* Named semaphore locking uses the file path so it needs to be
2855 ** included in the namedsemLockingContext
2856 */
2857 enterMutex();
2858 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
2859 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
2860 char *zSemName = pNew->pOpen->aSemName;
2861 int n;
2862 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem", pNew->zRealpath);
2863 for( n=0; zSemName[n]; n++ )
2864 if( zSemName[n]=='/' ) zSemName[n] = '_';
2865 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
2866 if( pNew->pOpen->pSem == SEM_FAILED ){
2867 rc = SQLITE_NOMEM;
2868 pNew->pOpen->aSemName[0] = '\0';
2869 }
2870 }
2871 leaveMutex();
2872 break;
2873 }
2874#endif
2875
2876#if !defined(__RTP__) && !defined(_WRS_KERNEL)
danielk1977e339d652008-06-28 11:23:00 +00002877 case LOCKING_STYLE_FLOCK:
chw97185482008-11-17 08:05:31 +00002878#endif
danielk1977e339d652008-06-28 11:23:00 +00002879 case LOCKING_STYLE_NONE:
drh218c5082008-03-07 00:27:10 +00002880 break;
drhe78669b2007-06-29 12:04:26 +00002881#endif
danielk1977e339d652008-06-28 11:23:00 +00002882 }
aswift5b1a2562008-08-22 00:22:35 +00002883
2884 pNew->lastErrno = 0;
chw97185482008-11-17 08:05:31 +00002885#if defined(__RTP__) || defined(_WRS_KERNEL)
2886 if( rc!=SQLITE_OK ){
2887 unlink(zFilename);
2888 isDelete = 0;
2889 }
2890 pNew->isDelete = isDelete;
2891#endif
danielk1977e339d652008-06-28 11:23:00 +00002892 if( rc!=SQLITE_OK ){
danielk19777c055b92007-10-30 17:28:51 +00002893 if( dirfd>=0 ) close(dirfd);
drhbfe66312006-10-03 17:40:40 +00002894 close(h);
danielk1977e339d652008-06-28 11:23:00 +00002895 }else{
danielk19776cb427f2008-06-30 10:16:04 +00002896 pNew->pMethod = &aIoMethod[eLockingStyle-1];
danielk1977e339d652008-06-28 11:23:00 +00002897 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00002898 }
danielk1977e339d652008-06-28 11:23:00 +00002899 return rc;
drh054889e2005-11-30 03:20:31 +00002900}
drh9c06c952005-11-26 00:25:00 +00002901
danielk1977ad94b582007-08-20 06:44:22 +00002902/*
2903** Open a file descriptor to the directory containing file zFilename.
2904** If successful, *pFd is set to the opened file descriptor and
2905** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2906** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2907** value.
2908**
2909** If SQLITE_OK is returned, the caller is responsible for closing
2910** the file descriptor *pFd using close().
2911*/
danielk1977fee2d252007-08-18 10:59:19 +00002912static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00002913 int ii;
drh777b17a2007-09-20 10:02:54 +00002914 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00002915 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00002916
drh153c62c2007-08-24 03:51:33 +00002917 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00002918 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2919 if( ii>0 ){
2920 zDirname[ii] = '\0';
2921 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00002922 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002923#ifdef FD_CLOEXEC
2924 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2925#endif
2926 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2927 }
2928 }
danielk1977fee2d252007-08-18 10:59:19 +00002929 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00002930 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00002931}
2932
danielk1977b4b47412007-08-17 15:53:36 +00002933/*
danielk197717b90b52008-06-06 11:11:25 +00002934** Create a temporary file name in zBuf. zBuf must be allocated
2935** by the calling process and must be big enough to hold at least
2936** pVfs->mxPathname bytes.
2937*/
2938static int getTempname(int nBuf, char *zBuf){
2939 static const char *azDirs[] = {
2940 0,
2941 "/var/tmp",
2942 "/usr/tmp",
2943 "/tmp",
2944 ".",
2945 };
2946 static const unsigned char zChars[] =
2947 "abcdefghijklmnopqrstuvwxyz"
2948 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2949 "0123456789";
2950 int i, j;
2951 struct stat buf;
2952 const char *zDir = ".";
2953
2954 /* It's odd to simulate an io-error here, but really this is just
2955 ** using the io-error infrastructure to test that SQLite handles this
2956 ** function failing.
2957 */
2958 SimulateIOError( return SQLITE_IOERR );
2959
2960 azDirs[0] = sqlite3_temp_directory;
danielk197700e13612008-11-17 19:18:54 +00002961 for(i=0; i<ArraySize(azDirs); i++){
danielk197717b90b52008-06-06 11:11:25 +00002962 if( azDirs[i]==0 ) continue;
2963 if( stat(azDirs[i], &buf) ) continue;
2964 if( !S_ISDIR(buf.st_mode) ) continue;
2965 if( access(azDirs[i], 07) ) continue;
2966 zDir = azDirs[i];
2967 break;
2968 }
2969
2970 /* Check that the output buffer is large enough for the temporary file
2971 ** name. If it is not, return SQLITE_ERROR.
2972 */
danielk197700e13612008-11-17 19:18:54 +00002973 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00002974 return SQLITE_ERROR;
2975 }
2976
2977 do{
2978 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
2979 j = strlen(zBuf);
2980 sqlite3_randomness(15, &zBuf[j]);
2981 for(i=0; i<15; i++, j++){
2982 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2983 }
2984 zBuf[j] = 0;
2985 }while( access(zBuf,0)==0 );
2986 return SQLITE_OK;
2987}
2988
2989
2990/*
danielk1977ad94b582007-08-20 06:44:22 +00002991** Open the file zPath.
2992**
danielk1977b4b47412007-08-17 15:53:36 +00002993** Previously, the SQLite OS layer used three functions in place of this
2994** one:
2995**
2996** sqlite3OsOpenReadWrite();
2997** sqlite3OsOpenReadOnly();
2998** sqlite3OsOpenExclusive();
2999**
3000** These calls correspond to the following combinations of flags:
3001**
3002** ReadWrite() -> (READWRITE | CREATE)
3003** ReadOnly() -> (READONLY)
3004** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
3005**
3006** The old OpenExclusive() accepted a boolean argument - "delFlag". If
3007** true, the file was configured to be automatically deleted when the
3008** file handle closed. To achieve the same effect using this new
3009** interface, add the DELETEONCLOSE flag to those specified above for
3010** OpenExclusive().
3011*/
3012static int unixOpen(
drh153c62c2007-08-24 03:51:33 +00003013 sqlite3_vfs *pVfs,
danielk1977b4b47412007-08-17 15:53:36 +00003014 const char *zPath,
3015 sqlite3_file *pFile,
3016 int flags,
3017 int *pOutFlags
3018){
danielk1977fee2d252007-08-18 10:59:19 +00003019 int fd = 0; /* File descriptor returned by open() */
3020 int dirfd = -1; /* Directory file descriptor */
3021 int oflags = 0; /* Flags to pass to open() */
3022 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00003023 int noLock; /* True to omit locking primitives */
danielk1977b4b47412007-08-17 15:53:36 +00003024
3025 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3026 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3027 int isCreate = (flags & SQLITE_OPEN_CREATE);
3028 int isReadonly = (flags & SQLITE_OPEN_READONLY);
3029 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3030
danielk1977fee2d252007-08-18 10:59:19 +00003031 /* If creating a master or main-file journal, this function will open
3032 ** a file-descriptor on the directory too. The first time unixSync()
3033 ** is called the directory file descriptor will be fsync()ed and close()d.
3034 */
3035 int isOpenDirectory = (isCreate &&
3036 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
3037 );
3038
danielk197717b90b52008-06-06 11:11:25 +00003039 /* If argument zPath is a NULL pointer, this function is required to open
3040 ** a temporary file. Use this buffer to store the file name in.
3041 */
3042 char zTmpname[MAX_PATHNAME+1];
3043 const char *zName = zPath;
3044
danielk1977fee2d252007-08-18 10:59:19 +00003045 /* Check the following statements are true:
3046 **
3047 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3048 ** (b) if CREATE is set, then READWRITE must also be set, and
3049 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00003050 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00003051 */
danielk1977b4b47412007-08-17 15:53:36 +00003052 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00003053 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00003054 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00003055 assert(isDelete==0 || isCreate);
3056
drh33f4e022007-09-03 15:19:34 +00003057 /* The main DB, main journal, and master journal are never automatically
3058 ** deleted
3059 */
3060 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
3061 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
3062 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00003063
danielk1977fee2d252007-08-18 10:59:19 +00003064 /* Assert that the upper layer has set one of the "file-type" flags. */
3065 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
3066 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
3067 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00003068 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00003069 );
3070
danielk1977e339d652008-06-28 11:23:00 +00003071 memset(pFile, 0, sizeof(unixFile));
3072
danielk197717b90b52008-06-06 11:11:25 +00003073 if( !zName ){
3074 int rc;
3075 assert(isDelete && !isOpenDirectory);
3076 rc = getTempname(MAX_PATHNAME+1, zTmpname);
3077 if( rc!=SQLITE_OK ){
3078 return rc;
3079 }
3080 zName = zTmpname;
3081 }
3082
danielk1977b4b47412007-08-17 15:53:36 +00003083 if( isReadonly ) oflags |= O_RDONLY;
3084 if( isReadWrite ) oflags |= O_RDWR;
3085 if( isCreate ) oflags |= O_CREAT;
3086 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
3087 oflags |= (O_LARGEFILE|O_BINARY);
3088
danielk197717b90b52008-06-06 11:11:25 +00003089 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
chw97185482008-11-17 08:05:31 +00003090 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, oflags);
danielk19772f2d8c72007-08-30 16:13:33 +00003091 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00003092 /* Failed to open the file for read/write access. Try read-only. */
3093 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
3094 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00003095 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00003096 }
3097 if( fd<0 ){
3098 return SQLITE_CANTOPEN;
3099 }
3100 if( isDelete ){
chw97185482008-11-17 08:05:31 +00003101#if defined(__RTP__) || defined(_WRS_KERNEL)
3102 zPath = zName;
3103#else
danielk197717b90b52008-06-06 11:11:25 +00003104 unlink(zName);
chw97185482008-11-17 08:05:31 +00003105#endif
danielk1977b4b47412007-08-17 15:53:36 +00003106 }
3107 if( pOutFlags ){
3108 *pOutFlags = flags;
3109 }
3110
3111 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00003112 if( isOpenDirectory ){
3113 int rc = openDirectory(zPath, &dirfd);
3114 if( rc!=SQLITE_OK ){
3115 close(fd);
3116 return rc;
3117 }
3118 }
danielk1977e339d652008-06-28 11:23:00 +00003119
3120#ifdef FD_CLOEXEC
3121 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3122#endif
3123
drhda0e7682008-07-30 15:27:54 +00003124 noLock = eType!=SQLITE_OPEN_MAIN_DB;
chw97185482008-11-17 08:05:31 +00003125 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00003126}
3127
3128/*
danielk1977fee2d252007-08-18 10:59:19 +00003129** Delete the file at zPath. If the dirSync argument is true, fsync()
3130** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00003131*/
drh153c62c2007-08-24 03:51:33 +00003132static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
danielk1977fee2d252007-08-18 10:59:19 +00003133 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003134 SimulateIOError(return SQLITE_IOERR_DELETE);
3135 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00003136#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00003137 if( dirSync ){
3138 int fd;
3139 rc = openDirectory(zPath, &fd);
3140 if( rc==SQLITE_OK ){
chw97185482008-11-17 08:05:31 +00003141#if defined(__RTP__) || defined(_WRS_KERNEL)
3142 if( fsync(fd)==-1 )
3143#else
3144 if( fsync(fd) )
3145#endif
3146 {
danielk1977fee2d252007-08-18 10:59:19 +00003147 rc = SQLITE_IOERR_DIR_FSYNC;
3148 }
3149 close(fd);
3150 }
3151 }
danielk1977d138dd82008-10-15 16:02:48 +00003152#endif
danielk1977fee2d252007-08-18 10:59:19 +00003153 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00003154}
3155
danielk197790949c22007-08-17 16:50:38 +00003156/*
3157** Test the existance of or access permissions of file zPath. The
3158** test performed depends on the value of flags:
3159**
3160** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
3161** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
3162** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
3163**
3164** Otherwise return 0.
3165*/
danielk1977861f7452008-06-05 11:39:11 +00003166static int unixAccess(
3167 sqlite3_vfs *pVfs,
3168 const char *zPath,
3169 int flags,
3170 int *pResOut
3171){
rse25c0d1a2007-09-20 08:38:14 +00003172 int amode = 0;
danielk1977861f7452008-06-05 11:39:11 +00003173 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00003174 switch( flags ){
3175 case SQLITE_ACCESS_EXISTS:
3176 amode = F_OK;
3177 break;
3178 case SQLITE_ACCESS_READWRITE:
3179 amode = W_OK|R_OK;
3180 break;
drh50d3f902007-08-27 21:10:36 +00003181 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00003182 amode = R_OK;
3183 break;
3184
3185 default:
3186 assert(!"Invalid flags argument");
3187 }
danielk1977861f7452008-06-05 11:39:11 +00003188 *pResOut = (access(zPath, amode)==0);
3189 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003190}
3191
danielk1977b4b47412007-08-17 15:53:36 +00003192
3193/*
3194** Turn a relative pathname into a full pathname. The relative path
3195** is stored as a nul-terminated string in the buffer pointed to by
3196** zPath.
3197**
3198** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
3199** (in this case, MAX_PATHNAME bytes). The full-path is written to
3200** this buffer before returning.
3201*/
danielk1977adfb9b02007-09-17 07:02:56 +00003202static int unixFullPathname(
3203 sqlite3_vfs *pVfs, /* Pointer to vfs object */
3204 const char *zPath, /* Possibly relative input path */
3205 int nOut, /* Size of output buffer in bytes */
3206 char *zOut /* Output buffer */
3207){
danielk1977843e65f2007-09-01 16:16:15 +00003208
3209 /* It's odd to simulate an io-error here, but really this is just
3210 ** using the io-error infrastructure to test that SQLite handles this
3211 ** function failing. This function could fail if, for example, the
3212 ** current working directly has been unlinked.
3213 */
3214 SimulateIOError( return SQLITE_ERROR );
3215
drh153c62c2007-08-24 03:51:33 +00003216 assert( pVfs->mxPathname==MAX_PATHNAME );
chw97185482008-11-17 08:05:31 +00003217
3218#if defined(__RTP__) || defined(_WRS_KERNEL)
3219 {
3220 char *zRealname = vxrealpath(zPath, 0);
3221 zOut[0] = '\0';
3222 if( !zRealname ){
3223 return SQLITE_CANTOPEN;
3224 }
3225 sqlite3_snprintf(nOut, zOut, "%s", zRealname);
3226 sqlite3_free(zRealname);
3227 return SQLITE_OK;
3228 }
3229#else
drh3c7f2dc2007-12-06 13:26:20 +00003230 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00003231 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00003232 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003233 }else{
3234 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00003235 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00003236 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00003237 }
3238 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00003239 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003240 }
3241 return SQLITE_OK;
3242
3243#if 0
3244 /*
3245 ** Remove "/./" path elements and convert "/A/./" path elements
3246 ** to just "/".
3247 */
3248 if( zFull ){
3249 int i, j;
3250 for(i=j=0; zFull[i]; i++){
3251 if( zFull[i]=='/' ){
3252 if( zFull[i+1]=='/' ) continue;
3253 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
3254 i += 1;
3255 continue;
3256 }
3257 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
3258 while( j>0 && zFull[j-1]!='/' ){ j--; }
3259 i += 3;
3260 continue;
3261 }
3262 }
3263 zFull[j++] = zFull[i];
3264 }
3265 zFull[j] = 0;
3266 }
3267#endif
chw97185482008-11-17 08:05:31 +00003268#endif
danielk1977b4b47412007-08-17 15:53:36 +00003269}
3270
drh0ccebe72005-06-07 22:22:50 +00003271
drh761df872006-12-21 01:29:22 +00003272#ifndef SQLITE_OMIT_LOAD_EXTENSION
3273/*
3274** Interfaces for opening a shared library, finding entry points
3275** within the shared library, and closing the shared library.
3276*/
3277#include <dlfcn.h>
drh153c62c2007-08-24 03:51:33 +00003278static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
drh761df872006-12-21 01:29:22 +00003279 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
3280}
danielk197795c8a542007-09-01 06:51:27 +00003281
3282/*
3283** SQLite calls this function immediately after a call to unixDlSym() or
3284** unixDlOpen() fails (returns a null pointer). If a more detailed error
3285** message is available, it is written to zBufOut. If no error message
3286** is available, zBufOut is left unmodified and SQLite uses a default
3287** error message.
3288*/
drh153c62c2007-08-24 03:51:33 +00003289static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00003290 char *zErr;
3291 enterMutex();
3292 zErr = dlerror();
3293 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00003294 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00003295 }
3296 leaveMutex();
3297}
drh46c99e02007-08-27 23:26:59 +00003298static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
drh761df872006-12-21 01:29:22 +00003299 return dlsym(pHandle, zSymbol);
3300}
drh46c99e02007-08-27 23:26:59 +00003301static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
danielk1977b4b47412007-08-17 15:53:36 +00003302 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00003303}
danielk1977b4b47412007-08-17 15:53:36 +00003304#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
3305 #define unixDlOpen 0
3306 #define unixDlError 0
3307 #define unixDlSym 0
3308 #define unixDlClose 0
3309#endif
3310
3311/*
danielk197790949c22007-08-17 16:50:38 +00003312** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00003313*/
drh153c62c2007-08-24 03:51:33 +00003314static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
danielk197790949c22007-08-17 16:50:38 +00003315
danielk197700e13612008-11-17 19:18:54 +00003316 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00003317
drhbbd42a62004-05-22 17:41:58 +00003318 /* We have to initialize zBuf to prevent valgrind from reporting
3319 ** errors. The reports issued by valgrind are incorrect - we would
3320 ** prefer that the randomness be increased by making use of the
3321 ** uninitialized space in zBuf - but valgrind errors tend to worry
3322 ** some users. Rather than argue, it seems easier just to initialize
3323 ** the whole array and silence valgrind, even if that means less randomness
3324 ** in the random seed.
3325 **
3326 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00003327 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00003328 ** tests repeatable.
3329 */
danielk1977b4b47412007-08-17 15:53:36 +00003330 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00003331#if !defined(SQLITE_TEST)
3332 {
drh842b8642005-01-21 17:53:17 +00003333 int pid, fd;
3334 fd = open("/dev/urandom", O_RDONLY);
3335 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00003336 time_t t;
3337 time(&t);
danielk197790949c22007-08-17 16:50:38 +00003338 memcpy(zBuf, &t, sizeof(t));
3339 pid = getpid();
3340 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00003341 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00003342 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00003343 }else{
drh72cbd072008-10-14 17:58:38 +00003344 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00003345 close(fd);
3346 }
drhbbd42a62004-05-22 17:41:58 +00003347 }
3348#endif
drh72cbd072008-10-14 17:58:38 +00003349 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00003350}
3351
danielk1977b4b47412007-08-17 15:53:36 +00003352
drhbbd42a62004-05-22 17:41:58 +00003353/*
3354** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00003355** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00003356** The return value is the number of microseconds of sleep actually
3357** requested from the underlying operating system, a number which
3358** might be greater than or equal to the argument, but not less
3359** than the argument.
drhbbd42a62004-05-22 17:41:58 +00003360*/
drh153c62c2007-08-24 03:51:33 +00003361static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
chw97185482008-11-17 08:05:31 +00003362#if defined(__RTP__) || defined(_WRS_KERNEL)
3363 struct timespec sp;
3364
3365 sp.tv_sec = microseconds / 1000000;
3366 sp.tv_nsec = (microseconds % 1000000) * 1000;
3367 nanosleep(&sp, NULL);
3368#else
drhbbd42a62004-05-22 17:41:58 +00003369#if defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00003370 usleep(microseconds);
3371 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00003372#else
danielk1977b4b47412007-08-17 15:53:36 +00003373 int seconds = (microseconds+999999)/1000000;
3374 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00003375 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00003376#endif
chw97185482008-11-17 08:05:31 +00003377#endif
drh88f474a2006-01-02 20:00:12 +00003378}
3379
3380/*
drhbbd42a62004-05-22 17:41:58 +00003381** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00003382** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00003383*/
3384#ifdef SQLITE_TEST
3385int sqlite3_current_time = 0;
3386#endif
3387
3388/*
3389** Find the current time (in Universal Coordinated Time). Write the
3390** current time and date as a Julian Day number into *prNow and
3391** return 0. Return 1 if the time and date cannot be found.
3392*/
drh153c62c2007-08-24 03:51:33 +00003393static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
chw97185482008-11-17 08:05:31 +00003394#if defined(__RTP__) || defined(_WRS_KERNEL)
3395 struct timespec sNow;
3396 clock_gettime(CLOCK_REALTIME, &sNow);
3397 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
3398#else
drh19e2d372005-08-29 23:00:03 +00003399#ifdef NO_GETTOD
drhbbd42a62004-05-22 17:41:58 +00003400 time_t t;
3401 time(&t);
3402 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00003403#else
3404 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00003405 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00003406 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
3407#endif
chw97185482008-11-17 08:05:31 +00003408#endif
drhbbd42a62004-05-22 17:41:58 +00003409#ifdef SQLITE_TEST
3410 if( sqlite3_current_time ){
3411 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
3412 }
3413#endif
3414 return 0;
3415}
danielk1977b4b47412007-08-17 15:53:36 +00003416
danielk1977bcb97fe2008-06-06 15:49:29 +00003417static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
3418 return 0;
3419}
3420
drh153c62c2007-08-24 03:51:33 +00003421/*
danielk1977e339d652008-06-28 11:23:00 +00003422** Initialize the operating system interface.
drh153c62c2007-08-24 03:51:33 +00003423*/
danielk1977c0fa4c52008-06-25 17:19:00 +00003424int sqlite3_os_init(void){
danielk1977e339d652008-06-28 11:23:00 +00003425 /* Macro to define the static contents of an sqlite3_vfs structure for
3426 ** the unix backend. The two parameters are the values to use for
3427 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
3428 **
3429 */
3430 #define UNIXVFS(zVfsName, pVfsAppData) { \
3431 1, /* iVersion */ \
3432 sizeof(unixFile), /* szOsFile */ \
3433 MAX_PATHNAME, /* mxPathname */ \
3434 0, /* pNext */ \
3435 zVfsName, /* zName */ \
3436 (void *)pVfsAppData, /* pAppData */ \
3437 unixOpen, /* xOpen */ \
3438 unixDelete, /* xDelete */ \
3439 unixAccess, /* xAccess */ \
3440 unixFullPathname, /* xFullPathname */ \
3441 unixDlOpen, /* xDlOpen */ \
3442 unixDlError, /* xDlError */ \
3443 unixDlSym, /* xDlSym */ \
3444 unixDlClose, /* xDlClose */ \
3445 unixRandomness, /* xRandomness */ \
3446 unixSleep, /* xSleep */ \
3447 unixCurrentTime, /* xCurrentTime */ \
3448 unixGetLastError /* xGetLastError */ \
3449 }
3450
3451 static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
drh40bbb0a2008-09-23 10:23:26 +00003452#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +00003453 int i;
3454 static sqlite3_vfs aVfs[] = {
3455 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
3456 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
3457 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
3458 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
chw97185482008-11-17 08:05:31 +00003459 UNIXVFS("unix-none", LOCKING_STYLE_NONE),
3460 UNIXVFS("unix-namedsem",LOCKING_STYLE_NAMEDSEM),
drh153c62c2007-08-24 03:51:33 +00003461 };
danielk1977e339d652008-06-28 11:23:00 +00003462 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
3463 sqlite3_vfs_register(&aVfs[i], 0);
3464 }
3465#endif
chw97185482008-11-17 08:05:31 +00003466#if defined(__RTP__) || defined(_WRS_KERNEL)
3467 sqlite3HashInit(&nameHash, 1);
3468#endif
danielk1977c0fa4c52008-06-25 17:19:00 +00003469 sqlite3_vfs_register(&unixVfs, 1);
3470 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00003471}
danielk1977e339d652008-06-28 11:23:00 +00003472
3473/*
3474** Shutdown the operating system interface. This is a no-op for unix.
3475*/
danielk1977c0fa4c52008-06-25 17:19:00 +00003476int sqlite3_os_end(void){
3477 return SQLITE_OK;
3478}
drhdce8bdb2007-08-16 13:01:44 +00003479
danielk197729bafea2008-06-26 10:41:19 +00003480#endif /* SQLITE_OS_UNIX */