blob: a75514d8af081f201b5ae018ada4e4e4c62f73cb [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**
aswiftaebf4132008-11-21 00:10:35 +000015** $Id: os_unix.c,v 1.217 2008/11/21 00:10:35 aswift 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),
aswiftaebf4132008-11-21 00:10:35 +000029** * Named POSIX semaphores (VXWorks only),
30** * proxy locking.
drh40bbb0a2008-09-23 10:23:26 +000031**
32** SQLITE_ENABLE_LOCKING_STYLE only works on a Mac. It is turned on by
33** default on a Mac and disabled on all other posix platforms.
danielk1977e339d652008-06-28 11:23:00 +000034*/
drh40bbb0a2008-09-23 10:23:26 +000035#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
36# if defined(__DARWIN__)
37# define SQLITE_ENABLE_LOCKING_STYLE 1
38# else
39# define SQLITE_ENABLE_LOCKING_STYLE 0
40# endif
41#endif
drhbfe66312006-10-03 17:40:40 +000042
drh9cbe6352005-11-29 03:13:21 +000043/*
danielk1977397d65f2008-11-19 11:35:39 +000044** Define the IS_VXWORKS pre-processor macro to 1 if building on
45** vxworks, or 0 otherwise.
46*/
47#if defined(__RTP__) || defined(_WRS_KERNEL)
48# define IS_VXWORKS 1
49#else
50# define IS_VXWORKS 0
51#endif
52
53/*
drh9cbe6352005-11-29 03:13:21 +000054** These #defines should enable >2GB file support on Posix if the
55** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000056** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000057**
58** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
59** on the compiler command line. This is necessary if you are compiling
60** on a recent machine (ex: RedHat 7.2) but you want your code to work
61** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
62** without this option, LFS is enable. But LFS does not exist in the kernel
63** in RedHat 6.0, so the code won't work. Hence, for maximum binary
64** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000065*/
66#ifndef SQLITE_DISABLE_LFS
67# define _LARGE_FILE 1
68# ifndef _FILE_OFFSET_BITS
69# define _FILE_OFFSET_BITS 64
70# endif
71# define _LARGEFILE_SOURCE 1
72#endif
drhbbd42a62004-05-22 17:41:58 +000073
drh9cbe6352005-11-29 03:13:21 +000074/*
75** standard include files.
76*/
77#include <sys/types.h>
78#include <sys/stat.h>
79#include <fcntl.h>
80#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000081#include <time.h>
drh19e2d372005-08-29 23:00:03 +000082#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000083#include <errno.h>
danielk1977e339d652008-06-28 11:23:00 +000084
drh40bbb0a2008-09-23 10:23:26 +000085#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977c70dfc42008-11-19 13:52:30 +000086# include <sys/ioctl.h>
87# if IS_VXWORKS
88# define lstat stat
89# include <semaphore.h>
90# include <limits.h>
91# else
92# include <sys/param.h>
93# include <sys/mount.h>
94# endif
drhbfe66312006-10-03 17:40:40 +000095#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000096
97/*
drhf1a221e2006-01-15 17:27:17 +000098** If we are to be thread-safe, include the pthreads header and define
99** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +0000100*/
drhd677b3d2007-08-20 22:48:41 +0000101#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000102# include <pthread.h>
103# define SQLITE_UNIX_THREADS 1
104#endif
105
106/*
107** Default permissions when creating a new file
108*/
109#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
110# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
111#endif
112
danielk1977b4b47412007-08-17 15:53:36 +0000113/*
aswiftaebf4132008-11-21 00:10:35 +0000114 ** Default permissions when creating auto proxy dir
115 */
116#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
117# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
118#endif
119
120/*
danielk1977b4b47412007-08-17 15:53:36 +0000121** Maximum supported path-length.
122*/
123#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +0000124
125
126/*
danielk1977ad94b582007-08-20 06:44:22 +0000127** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +0000128** protability layer.
drh9cbe6352005-11-29 03:13:21 +0000129*/
drh054889e2005-11-30 03:20:31 +0000130typedef struct unixFile unixFile;
131struct unixFile {
danielk197762079062007-08-15 17:08:46 +0000132 sqlite3_io_methods const *pMethod; /* Always the first entry */
danielk1977967a4a12007-08-20 14:23:44 +0000133#ifdef SQLITE_TEST
134 /* In test mode, increase the size of this structure a bit so that
135 ** it is larger than the struct CrashFile defined in test6.c.
136 */
137 char aPadding[32];
138#endif
drh9cbe6352005-11-29 03:13:21 +0000139 struct openCnt *pOpen; /* Info about all open fd's on this inode */
140 struct lockInfo *pLock; /* Info about locks on this inode */
drh40bbb0a2008-09-23 10:23:26 +0000141#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +0000142 void *lockingContext; /* Locking style specific state */
danielk1977e339d652008-06-28 11:23:00 +0000143#endif
drh9cbe6352005-11-29 03:13:21 +0000144 int h; /* The file descriptor */
145 unsigned char locktype; /* The type of lock held on this fd */
drh9cbe6352005-11-29 03:13:21 +0000146 int dirfd; /* File descriptor for the directory */
drhd677b3d2007-08-20 22:48:41 +0000147#if SQLITE_THREADSAFE
danielk1977ad94b582007-08-20 06:44:22 +0000148 pthread_t tid; /* The thread that "owns" this unixFile */
drh9cbe6352005-11-29 03:13:21 +0000149#endif
aswift5b1a2562008-08-22 00:22:35 +0000150 int lastErrno; /* The unix errno from the last I/O error */
danielk1977c70dfc42008-11-19 13:52:30 +0000151#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000152 int isDelete; /* Delete on close if true */
153 char *zRealpath;
154#endif
aswiftaebf4132008-11-21 00:10:35 +0000155#if SQLITE_ENABLE_LOCKING_STYLE
156 int oflags; /* The flags specified at open */
157#endif
drh9cbe6352005-11-29 03:13:21 +0000158};
159
drh0ccebe72005-06-07 22:22:50 +0000160/*
drh198bf392006-01-06 21:52:49 +0000161** Include code that is common to all os_*.c files
162*/
163#include "os_common.h"
164
165/*
drh0ccebe72005-06-07 22:22:50 +0000166** Define various macros that are missing from some systems.
167*/
drhbbd42a62004-05-22 17:41:58 +0000168#ifndef O_LARGEFILE
169# define O_LARGEFILE 0
170#endif
171#ifdef SQLITE_DISABLE_LFS
172# undef O_LARGEFILE
173# define O_LARGEFILE 0
174#endif
175#ifndef O_NOFOLLOW
176# define O_NOFOLLOW 0
177#endif
178#ifndef O_BINARY
179# define O_BINARY 0
180#endif
181
182/*
183** The DJGPP compiler environment looks mostly like Unix, but it
184** lacks the fcntl() system call. So redefine fcntl() to be something
185** that always succeeds. This means that locking does not occur under
drh85b623f2007-12-13 21:54:09 +0000186** DJGPP. But it is DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000187*/
188#ifdef __DJGPP__
189# define fcntl(A,B,C) 0
190#endif
191
192/*
drh2b4b5962005-06-15 17:47:55 +0000193** The threadid macro resolves to the thread-id or to 0. Used for
194** testing and debugging only.
195*/
drhd677b3d2007-08-20 22:48:41 +0000196#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000197#define threadid pthread_self()
198#else
199#define threadid 0
200#endif
201
202/*
danielk1977ad94b582007-08-20 06:44:22 +0000203** Set or check the unixFile.tid field. This field is set when an unixFile
204** is first opened. All subsequent uses of the unixFile verify that the
205** same thread is operating on the unixFile. Some operating systems do
drh2b4b5962005-06-15 17:47:55 +0000206** not allow locks to be overridden by other threads and that restriction
207** means that sqlite3* database handles cannot be moved from one thread
208** to another. This logic makes sure a user does not try to do that
209** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000210**
danielk1977ad94b582007-08-20 06:44:22 +0000211** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
drhf1a221e2006-01-15 17:27:17 +0000212** another as long as we are running on a system that supports threads
213** overriding each others locks (which now the most common behavior)
danielk1977ad94b582007-08-20 06:44:22 +0000214** or if no locks are held. But the unixFile.pLock field needs to be
drhf1a221e2006-01-15 17:27:17 +0000215** recomputed because its key includes the thread-id. See the
216** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000217*/
drhd677b3d2007-08-20 22:48:41 +0000218#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000219# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000220# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
221 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000222#else
223# define SET_THREADID(X)
224# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000225#endif
226
drhbbd42a62004-05-22 17:41:58 +0000227/*
228** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
229** section 6.5.2.2 lines 483 through 490 specify that when a process
230** sets or clears a lock, that operation overrides any prior locks set
231** by the same process. It does not explicitly say so, but this implies
232** that it overrides locks set by the same process using a different
233** file descriptor. Consider this test case:
drhbbd42a62004-05-22 17:41:58 +0000234** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
235**
236** Suppose ./file1 and ./file2 are really the same file (because
237** one is a hard or symbolic link to the other) then if you set
238** an exclusive lock on fd1, then try to get an exclusive lock
239** on fd2, it works. I would have expected the second lock to
240** fail since there was already a lock on the file due to fd1.
241** But not so. Since both locks came from the same process, the
242** second overrides the first, even though they were on different
243** file descriptors opened on different file names.
244**
245** Bummer. If you ask me, this is broken. Badly broken. It means
246** that we cannot use POSIX locks to synchronize file access among
247** competing threads of the same process. POSIX locks will work fine
248** to synchronize access for threads in separate processes, but not
249** threads within the same process.
250**
251** To work around the problem, SQLite has to manage file locks internally
252** on its own. Whenever a new database is opened, we have to find the
253** specific inode of the database file (the inode is determined by the
254** st_dev and st_ino fields of the stat structure that fstat() fills in)
255** and check for locks already existing on that inode. When locks are
256** created or removed, we have to look at our own internal record of the
257** locks to see if another thread has previously set a lock on that same
258** inode.
259**
danielk1977ad94b582007-08-20 06:44:22 +0000260** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000261** descriptor. It is now a structure that holds the integer file
262** descriptor and a pointer to a structure that describes the internal
263** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000264** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000265** point to the same locking structure. The locking structure keeps
266** a reference count (so we will know when to delete it) and a "cnt"
267** field that tells us its internal lock status. cnt==0 means the
268** file is unlocked. cnt==-1 means the file has an exclusive lock.
269** cnt>0 means there are cnt shared locks on the file.
270**
271** Any attempt to lock or unlock a file first checks the locking
272** structure. The fcntl() system call is only invoked to set a
273** POSIX lock if the internal lock structure transitions between
274** a locked and an unlocked state.
275**
276** 2004-Jan-11:
277** More recent discoveries about POSIX advisory locks. (The more
278** I discover, the more I realize the a POSIX advisory locks are
279** an abomination.)
280**
281** If you close a file descriptor that points to a file that has locks,
282** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000283** released. To work around this problem, each unixFile structure contains
drhbbd42a62004-05-22 17:41:58 +0000284** a pointer to an openCnt structure. There is one openCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000285** per open inode, which means that multiple unixFile can point to a single
286** openCnt. When an attempt is made to close an unixFile, if there are
287** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000288** to close() the file descriptor is deferred until all of the locks clear.
289** The openCnt structure keeps a list of file descriptors that need to
290** be closed and that list is walked (and cleared) when the last lock
291** clears.
292**
293** First, under Linux threads, because each thread has a separate
294** process ID, lock operations in one thread do not override locks
295** to the same file in other threads. Linux threads behave like
296** separate processes in this respect. But, if you close a file
297** descriptor in linux threads, all locks are cleared, even locks
298** on other threads and even though the other threads have different
299** process IDs. Linux threads is inconsistent in this respect.
300** (I'm beginning to think that linux threads is an abomination too.)
301** The consequence of this all is that the hash table for the lockInfo
302** structure has to include the process id as part of its key because
303** locks in different threads are treated as distinct. But the
304** openCnt structure should not include the process id in its
305** key because close() clears lock on all threads, not just the current
306** thread. Were it not for this goofiness in linux threads, we could
307** combine the lockInfo and openCnt structures into a single structure.
drh5fdae772004-06-29 03:29:00 +0000308**
309** 2004-Jun-28:
310** On some versions of linux, threads can override each others locks.
311** On others not. Sometimes you can change the behavior on the same
312** system by setting the LD_ASSUME_KERNEL environment variable. The
313** POSIX standard is silent as to which behavior is correct, as far
314** as I can tell, so other versions of unix might show the same
315** inconsistency. There is no little doubt in my mind that posix
316** advisory locks and linux threads are profoundly broken.
317**
318** To work around the inconsistencies, we have to test at runtime
319** whether or not threads can override each others locks. This test
320** is run once, the first time any lock is attempted. A static
321** variable is set to record the results of this test for future
322** use.
drhbbd42a62004-05-22 17:41:58 +0000323*/
324
325/*
326** An instance of the following structure serves as the key used
drh5fdae772004-06-29 03:29:00 +0000327** to locate a particular lockInfo structure given its inode.
328**
329** If threads cannot override each others locks, then we set the
330** lockKey.tid field to the thread ID. If threads can override
drhf1a221e2006-01-15 17:27:17 +0000331** each others locks then tid is always set to zero. tid is omitted
332** if we compile without threading support.
drhbbd42a62004-05-22 17:41:58 +0000333*/
334struct lockKey {
drh5fdae772004-06-29 03:29:00 +0000335 dev_t dev; /* Device number */
danielk1977c70dfc42008-11-19 13:52:30 +0000336#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000337 void *rnam; /* Realname since inode unusable */
338#else
drh5fdae772004-06-29 03:29:00 +0000339 ino_t ino; /* Inode number */
chw97185482008-11-17 08:05:31 +0000340#endif
drhd677b3d2007-08-20 22:48:41 +0000341#if SQLITE_THREADSAFE
drhd9cb6ac2005-10-20 07:28:17 +0000342 pthread_t tid; /* Thread ID or zero if threads can override each other */
drh5fdae772004-06-29 03:29:00 +0000343#endif
drhbbd42a62004-05-22 17:41:58 +0000344};
345
346/*
347** An instance of the following structure is allocated for each open
348** inode on each thread with a different process ID. (Threads have
349** different process IDs on linux, but not on most other unixes.)
350**
danielk1977ad94b582007-08-20 06:44:22 +0000351** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000352** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000353** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000354*/
355struct lockInfo {
356 struct lockKey key; /* The lookup key */
drh2ac3ee92004-06-07 16:27:46 +0000357 int cnt; /* Number of SHARED locks held */
danielk19779a1d0ab2004-06-01 14:09:28 +0000358 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
drhbbd42a62004-05-22 17:41:58 +0000359 int nRef; /* Number of pointers to this structure */
drhda0e7682008-07-30 15:27:54 +0000360 struct lockInfo *pNext, *pPrev; /* List of all lockInfo objects */
drhbbd42a62004-05-22 17:41:58 +0000361};
362
363/*
364** An instance of the following structure serves as the key used
365** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000366** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000367*/
368struct openKey {
369 dev_t dev; /* Device number */
danielk1977c70dfc42008-11-19 13:52:30 +0000370#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000371 void *rnam; /* Realname since inode unusable */
372#else
drhbbd42a62004-05-22 17:41:58 +0000373 ino_t ino; /* Inode number */
chw97185482008-11-17 08:05:31 +0000374#endif
drhbbd42a62004-05-22 17:41:58 +0000375};
376
377/*
378** An instance of the following structure is allocated for each open
379** inode. This structure keeps track of the number of locks on that
380** inode. If a close is attempted against an inode that is holding
381** locks, the close is deferred until all locks clear by adding the
382** file descriptor to be closed to the pending list.
383*/
384struct openCnt {
385 struct openKey key; /* The lookup key */
386 int nRef; /* Number of pointers to this structure */
387 int nLock; /* Number of outstanding locks */
388 int nPending; /* Number of pending close() operations */
389 int *aPending; /* Malloced space holding fd's awaiting a close() */
danielk1977c70dfc42008-11-19 13:52:30 +0000390#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000391 sem_t *pSem; /* Named POSIX semaphore */
392 char aSemName[MAX_PATHNAME+1]; /* Name of that semaphore */
393#endif
drhda0e7682008-07-30 15:27:54 +0000394 struct openCnt *pNext, *pPrev; /* List of all openCnt objects */
drhbbd42a62004-05-22 17:41:58 +0000395};
396
drhda0e7682008-07-30 15:27:54 +0000397/*
398** List of all lockInfo and openCnt objects. This used to be a hash
399** table. But the number of objects is rarely more than a dozen and
400** never exceeds a few thousand. And lookup is not on a critical
401** path oo a simple linked list will suffice.
drhbbd42a62004-05-22 17:41:58 +0000402*/
drhda0e7682008-07-30 15:27:54 +0000403static struct lockInfo *lockList = 0;
404static struct openCnt *openList = 0;
drh5fdae772004-06-29 03:29:00 +0000405
danielk1977c70dfc42008-11-19 13:52:30 +0000406#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000407/*
408** This hash table is used to bind the canonical file name to a
409** unixFile structure and use the hash key (= canonical name)
410** instead of the Inode number of the file to find the matching
411** lockInfo and openCnt structures. It also helps to make the
412** name of the semaphore when LOCKING_STYLE_NAMEDSEM is used
413** for the file.
414*/
415static Hash nameHash;
416#endif
417
drhbfe66312006-10-03 17:40:40 +0000418/*
419** The locking styles are associated with the different file locking
420** capabilities supported by different file systems.
421**
422** POSIX locking style fully supports shared and exclusive byte-range locks
danielk1977e339d652008-06-28 11:23:00 +0000423** AFP locking only supports exclusive byte-range locks
drhbfe66312006-10-03 17:40:40 +0000424** FLOCK only supports a single file-global exclusive lock
425** DOTLOCK isn't a true locking style, it refers to the use of a special
426** file named the same as the database file with a '.lock' extension, this
427** can be used on file systems that do not offer any reliable file locking
aswiftaebf4132008-11-21 00:10:35 +0000428** NONE locking means that no locking will be attempted, this is only used for
drhbfe66312006-10-03 17:40:40 +0000429** read-only file systems currently
chw97185482008-11-17 08:05:31 +0000430** NAMEDSEM is similar to DOTLOCK but uses a named semaphore instead of an
431** indicator file.
aswiftaebf4132008-11-21 00:10:35 +0000432** PROXY uses a second file to represent the lock state of the database file
433** which is never actually locked, a third file controls access to the proxy
drhbfe66312006-10-03 17:40:40 +0000434** UNSUPPORTED means that no locking will be attempted, this is only used for
435** file systems that are known to be unsupported
436*/
danielk1977e339d652008-06-28 11:23:00 +0000437#define LOCKING_STYLE_POSIX 1
drhda0e7682008-07-30 15:27:54 +0000438#define LOCKING_STYLE_NONE 2
danielk1977e339d652008-06-28 11:23:00 +0000439#define LOCKING_STYLE_DOTFILE 3
drhda0e7682008-07-30 15:27:54 +0000440#define LOCKING_STYLE_FLOCK 4
danielk1977e339d652008-06-28 11:23:00 +0000441#define LOCKING_STYLE_AFP 5
chw97185482008-11-17 08:05:31 +0000442#define LOCKING_STYLE_NAMEDSEM 6
aswiftaebf4132008-11-21 00:10:35 +0000443#define LOCKING_STYLE_PROXY 7
drhbfe66312006-10-03 17:40:40 +0000444
danielk1977ad94b582007-08-20 06:44:22 +0000445/*
aswift5b1a2562008-08-22 00:22:35 +0000446** Only set the lastErrno if the error code is a real error and not
447** a normal expected return code of SQLITE_BUSY or SQLITE_OK
448*/
449#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
450
451/*
danielk1977ad94b582007-08-20 06:44:22 +0000452** Helper functions to obtain and relinquish the global mutex.
453*/
danielk19770afae932008-09-24 09:12:46 +0000454static void enterMutex(void){
danielk197759f8c082008-06-18 17:09:10 +0000455 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000456}
danielk19770afae932008-09-24 09:12:46 +0000457static void leaveMutex(void){
danielk197759f8c082008-06-18 17:09:10 +0000458 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000459}
460
drhd677b3d2007-08-20 22:48:41 +0000461#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000462/*
463** This variable records whether or not threads can override each others
464** locks.
465**
466** 0: No. Threads cannot override each others locks.
467** 1: Yes. Threads can override each others locks.
468** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000469**
drh5062d3a2006-01-31 23:03:35 +0000470** On some systems, we know at compile-time if threads can override each
471** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
472** will be set appropriately. On other systems, we have to check at
473** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
474** undefined.
475**
drhf1a221e2006-01-15 17:27:17 +0000476** This variable normally has file scope only. But during testing, we make
477** it a global so that the test code can change its value in order to verify
478** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000479*/
drh5062d3a2006-01-31 23:03:35 +0000480#ifndef SQLITE_THREAD_OVERRIDE_LOCK
481# define SQLITE_THREAD_OVERRIDE_LOCK -1
482#endif
drh029b44b2006-01-15 00:13:15 +0000483#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000484int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000485#else
drh5062d3a2006-01-31 23:03:35 +0000486static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000487#endif
drh5fdae772004-06-29 03:29:00 +0000488
489/*
490** This structure holds information passed into individual test
491** threads by the testThreadLockingBehavior() routine.
492*/
493struct threadTestData {
494 int fd; /* File to be locked */
495 struct flock lock; /* The locking operation */
496 int result; /* Result of the locking operation */
497};
498
drh2b4b5962005-06-15 17:47:55 +0000499#ifdef SQLITE_LOCK_TRACE
500/*
501** Print out information about all locking operations.
502**
503** This routine is used for troubleshooting locks on multithreaded
504** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
505** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000506** turned off.
drh2b4b5962005-06-15 17:47:55 +0000507*/
508static int lockTrace(int fd, int op, struct flock *p){
509 char *zOpName, *zType;
510 int s;
511 int savedErrno;
512 if( op==F_GETLK ){
513 zOpName = "GETLK";
514 }else if( op==F_SETLK ){
515 zOpName = "SETLK";
516 }else{
517 s = fcntl(fd, op, p);
518 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
519 return s;
520 }
521 if( p->l_type==F_RDLCK ){
522 zType = "RDLCK";
523 }else if( p->l_type==F_WRLCK ){
524 zType = "WRLCK";
525 }else if( p->l_type==F_UNLCK ){
526 zType = "UNLCK";
527 }else{
528 assert( 0 );
529 }
530 assert( p->l_whence==SEEK_SET );
531 s = fcntl(fd, op, p);
532 savedErrno = errno;
533 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
534 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
535 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000536 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000537 struct flock l2;
538 l2 = *p;
539 fcntl(fd, F_GETLK, &l2);
540 if( l2.l_type==F_RDLCK ){
541 zType = "RDLCK";
542 }else if( l2.l_type==F_WRLCK ){
543 zType = "WRLCK";
544 }else if( l2.l_type==F_UNLCK ){
545 zType = "UNLCK";
546 }else{
547 assert( 0 );
548 }
549 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
550 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
551 }
552 errno = savedErrno;
553 return s;
554}
555#define fcntl lockTrace
556#endif /* SQLITE_LOCK_TRACE */
557
danielk197741a6a612008-11-11 18:34:35 +0000558#ifdef __linux__
drh5fdae772004-06-29 03:29:00 +0000559/*
danielk197741a6a612008-11-11 18:34:35 +0000560** This function is used as the main routine for a thread launched by
561** testThreadLockingBehavior(). It tests whether the shared-lock obtained
562** by the main thread in testThreadLockingBehavior() conflicts with a
563** hypothetical write-lock obtained by this thread on the same file.
564**
565** The write-lock is not actually acquired, as this is not possible if
566** the file is open in read-only mode (see ticket #3472).
567*/
drh5fdae772004-06-29 03:29:00 +0000568static void *threadLockingTest(void *pArg){
569 struct threadTestData *pData = (struct threadTestData*)pArg;
danielk197741a6a612008-11-11 18:34:35 +0000570 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
drh5fdae772004-06-29 03:29:00 +0000571 return pArg;
572}
573
574/*
575** This procedure attempts to determine whether or not threads
576** can override each others locks then sets the
577** threadsOverrideEachOthersLocks variable appropriately.
578*/
danielk19774d5238f2006-01-27 06:32:00 +0000579static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000580 int fd;
danielk197741a6a612008-11-11 18:34:35 +0000581 int rc;
582 struct threadTestData d;
583 struct flock l;
584 pthread_t t;
drh5fdae772004-06-29 03:29:00 +0000585
586 fd = dup(fd_orig);
587 if( fd<0 ) return;
danielk197741a6a612008-11-11 18:34:35 +0000588 memset(&l, 0, sizeof(l));
589 l.l_type = F_RDLCK;
590 l.l_len = 1;
591 l.l_start = 0;
592 l.l_whence = SEEK_SET;
593 rc = fcntl(fd_orig, F_SETLK, &l);
594 if( rc!=0 ) return;
595 memset(&d, 0, sizeof(d));
596 d.fd = fd;
597 d.lock = l;
598 d.lock.l_type = F_WRLCK;
599 pthread_create(&t, 0, threadLockingTest, &d);
600 pthread_join(t, 0);
drh5fdae772004-06-29 03:29:00 +0000601 close(fd);
danielk197741a6a612008-11-11 18:34:35 +0000602 if( d.result!=0 ) return;
603 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
drh5fdae772004-06-29 03:29:00 +0000604}
danielk197741a6a612008-11-11 18:34:35 +0000605#else
606/*
607** On anything other than linux, assume threads override each others locks.
608*/
609static void testThreadLockingBehavior(int fd_orig){
610 threadsOverrideEachOthersLocks = 1;
611}
612#endif /* __linux__ */
613
drhd677b3d2007-08-20 22:48:41 +0000614#endif /* SQLITE_THREADSAFE */
drh5fdae772004-06-29 03:29:00 +0000615
drhbbd42a62004-05-22 17:41:58 +0000616/*
617** Release a lockInfo structure previously allocated by findLockInfo().
618*/
619static void releaseLockInfo(struct lockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000620 if( pLock ){
621 pLock->nRef--;
622 if( pLock->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000623 if( pLock->pPrev ){
624 assert( pLock->pPrev->pNext==pLock );
625 pLock->pPrev->pNext = pLock->pNext;
626 }else{
627 assert( lockList==pLock );
628 lockList = pLock->pNext;
629 }
630 if( pLock->pNext ){
631 assert( pLock->pNext->pPrev==pLock );
632 pLock->pNext->pPrev = pLock->pPrev;
633 }
danielk1977e339d652008-06-28 11:23:00 +0000634 sqlite3_free(pLock);
635 }
drhbbd42a62004-05-22 17:41:58 +0000636 }
637}
638
639/*
640** Release a openCnt structure previously allocated by findLockInfo().
641*/
642static void releaseOpenCnt(struct openCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000643 if( pOpen ){
644 pOpen->nRef--;
645 if( pOpen->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000646 if( pOpen->pPrev ){
647 assert( pOpen->pPrev->pNext==pOpen );
648 pOpen->pPrev->pNext = pOpen->pNext;
649 }else{
650 assert( openList==pOpen );
651 openList = pOpen->pNext;
652 }
653 if( pOpen->pNext ){
654 assert( pOpen->pNext->pPrev==pOpen );
655 pOpen->pNext->pPrev = pOpen->pPrev;
656 }
657 sqlite3_free(pOpen->aPending);
danielk1977e339d652008-06-28 11:23:00 +0000658 sqlite3_free(pOpen);
659 }
drhbbd42a62004-05-22 17:41:58 +0000660 }
661}
662
danielk1977c70dfc42008-11-19 13:52:30 +0000663#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000664/*
665** Implementation of a realpath() like function for vxWorks
666** to determine canonical path name from given name. It does
667** not support symlinks. Neither does it handle volume prefixes.
668*/
669char *
670vxrealpath(const char *pathname, int dostat)
671{
672 struct stat sbuf;
673 int len;
674 char *where, *ptr, *last;
675 char *result, *curpath, *workpath, *namebuf;
676
677 len = pathconf(pathname, _PC_PATH_MAX);
678 if( len<0 ){
679 len = PATH_MAX;
680 }
681 result = sqlite3_malloc(len * 4);
682 if( !result ){
683 return 0;
684 }
685 curpath = result + len;
686 workpath = curpath + len;
687 namebuf = workpath + len;
688 strcpy(curpath, pathname);
689 if( *pathname!='/' ){
690 if( !getcwd(workpath, len) ){
691 sqlite3_free(result);
692 return 0;
693 }
694 }else{
695 *workpath = '\0';
696 }
697 where = curpath;
698 while( *where ){
699 if( !strcmp(where, ".") ){
700 where++;
701 continue;
702 }
703 if( !strncmp(where, "./", 2) ){
704 where += 2;
705 continue;
706 }
707 if( !strncmp(where, "../", 3) ){
708 where += 3;
709 ptr = last = workpath;
710 while( *ptr ){
711 if( *ptr=='/' ){
712 last = ptr;
713 }
714 ptr++;
715 }
716 *last = '\0';
717 continue;
718 }
719 ptr = strchr(where, '/');
720 if( !ptr ){
721 ptr = where + strlen(where) - 1;
722 }else{
723 *ptr = '\0';
724 }
725 strcpy(namebuf, workpath);
726 for( last = namebuf; *last; last++ ){
727 continue;
728 }
729 if( *--last!='/' ){
730 strcat(namebuf, "/");
731 }
732 strcat(namebuf, where);
733 where = ++ptr;
734 if( dostat ){
735 if( stat(namebuf, &sbuf)==-1 ){
736 sqlite3_free(result);
737 return 0;
738 }
739 if( (sbuf.st_mode & S_IFDIR)==S_IFDIR ){
740 strcpy(workpath, namebuf);
741 continue;
742 }
743 if( *where ){
744 sqlite3_free(result);
745 return 0;
746 }
747 }
748 strcpy(workpath, namebuf);
749 }
750 strcpy(result, workpath);
751 return result;
752}
753#endif
754
drh40bbb0a2008-09-23 10:23:26 +0000755#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +0000756/*
aswiftaebf4132008-11-21 00:10:35 +0000757** The proxyLockingContext has the path and file structures for the remote
758** and local proxy files in it
759*/
760typedef struct proxyLockingContext proxyLockingContext;
761struct proxyLockingContext {
762 unixFile *conchFile;
763 char *conchFilePath;
764 unixFile *lockProxy;
765 char *lockProxyPath;
766 char *dbPath;
767 int conchHeld;
768 void *oldLockingContext; /* preserve the original locking context for close */
769 sqlite3_io_methods const *pOldMethod; /* ditto pMethod */
770};
771
772static int getDbPathForUnixFile(unixFile *pFile, char *dbPath);
773static int getLockPath(const char *dbPath, char *lPath, size_t maxLen);
774static sqlite3_io_methods *ioMethodForLockingStyle(int style);
775static int createProxyUnixFile(const char *path, unixFile **ppFile);
776static int fillInUnixFile(sqlite3_vfs *pVfs, int h, int dirfd, sqlite3_file *pId, const char *zFilename, int noLock, int isDelete);
777static int takeConch(unixFile *pFile);
778static int releaseConch(unixFile *pFile);
779static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf);
780
781/*
drhbfe66312006-10-03 17:40:40 +0000782** Tests a byte-range locking query to see if byte range locks are
783** supported, if not we fall back to dotlockLockingStyle.
chw97185482008-11-17 08:05:31 +0000784** On vxWorks we fall back to namedsemLockingStyle.
drhbfe66312006-10-03 17:40:40 +0000785*/
danielk1977e339d652008-06-28 11:23:00 +0000786static int testLockingStyle(int fd){
drhbfe66312006-10-03 17:40:40 +0000787 struct flock lockInfo;
danielk1977e339d652008-06-28 11:23:00 +0000788
789 /* Test byte-range lock using fcntl(). If the call succeeds,
790 ** assume that the file-system supports POSIX style locks.
791 */
drhbfe66312006-10-03 17:40:40 +0000792 lockInfo.l_len = 1;
793 lockInfo.l_start = 0;
794 lockInfo.l_whence = SEEK_SET;
795 lockInfo.l_type = F_RDLCK;
danielk1977ad94b582007-08-20 06:44:22 +0000796 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
danielk1977e339d652008-06-28 11:23:00 +0000797 return LOCKING_STYLE_POSIX;
798 }
drhbfe66312006-10-03 17:40:40 +0000799
danielk1977e339d652008-06-28 11:23:00 +0000800 /* Testing for flock() can give false positives. So if if the above
danielk1977c70dfc42008-11-19 13:52:30 +0000801 ** test fails, then we fall back to using dot-file style locking (or
802 ** named-semaphore locking on vxworks).
chw97185482008-11-17 08:05:31 +0000803 */
danielk1977c70dfc42008-11-19 13:52:30 +0000804 return (IS_VXWORKS ? LOCKING_STYLE_NAMEDSEM : LOCKING_STYLE_DOTFILE);
drhbfe66312006-10-03 17:40:40 +0000805}
drh93a960a2008-07-10 00:32:42 +0000806#endif
drhbfe66312006-10-03 17:40:40 +0000807
808/*
danielk1977e339d652008-06-28 11:23:00 +0000809** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
810** f_fstypename entry in the statfs structure as returned by stat() for
811** the file system hosting the database file and selects the appropriate
812** locking style based on its value. These values and assignments are
813** based on Darwin/OSX behavior and have not been thoroughly tested on
drhbfe66312006-10-03 17:40:40 +0000814** other systems.
danielk1977e339d652008-06-28 11:23:00 +0000815**
816** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
817** returns LOCKING_STYLE_POSIX.
drhbfe66312006-10-03 17:40:40 +0000818*/
danielk197762c14b32008-11-19 09:05:26 +0000819#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +0000820static int detectLockingStyle(
821 sqlite3_vfs *pVfs,
danielk1977ad94b582007-08-20 06:44:22 +0000822 const char *filePath,
823 int fd
824){
danielk1977c70dfc42008-11-19 13:52:30 +0000825#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000826 if( !filePath ){
827 return LOCKING_STYLE_NONE;
828 }
829 if( pVfs->pAppData ){
830 return SQLITE_PTR_TO_INT(pVfs->pAppData);
831 }
832 if (access(filePath, 0) != -1){
833 return testLockingStyle(fd);
834 }
835#else
danielk1977e339d652008-06-28 11:23:00 +0000836 struct Mapping {
837 const char *zFilesystem;
838 int eLockingStyle;
839 } aMap[] = {
840 { "hfs", LOCKING_STYLE_POSIX },
841 { "ufs", LOCKING_STYLE_POSIX },
842 { "afpfs", LOCKING_STYLE_AFP },
aswift5b1a2562008-08-22 00:22:35 +0000843#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
844 { "smbfs", LOCKING_STYLE_AFP },
845#else
danielk1977e339d652008-06-28 11:23:00 +0000846 { "smbfs", LOCKING_STYLE_FLOCK },
aswift5b1a2562008-08-22 00:22:35 +0000847#endif
danielk1977e339d652008-06-28 11:23:00 +0000848 { "webdav", LOCKING_STYLE_NONE },
849 { 0, 0 }
850 };
851 int i;
drhbfe66312006-10-03 17:40:40 +0000852 struct statfs fsInfo;
853
danielk1977e339d652008-06-28 11:23:00 +0000854 if( !filePath ){
855 return LOCKING_STYLE_NONE;
drh339eb0b2008-03-07 15:34:11 +0000856 }
aswiftaebf4132008-11-21 00:10:35 +0000857 if( pVfs && pVfs->pAppData ){
aswiftf54b1b32008-08-22 18:41:37 +0000858 return SQLITE_PTR_TO_INT(pVfs->pAppData);
drh339eb0b2008-03-07 15:34:11 +0000859 }
drhbfe66312006-10-03 17:40:40 +0000860
danielk1977e339d652008-06-28 11:23:00 +0000861 if( statfs(filePath, &fsInfo) != -1 ){
862 if( fsInfo.f_flags & MNT_RDONLY ){
863 return LOCKING_STYLE_NONE;
864 }
865 for(i=0; aMap[i].zFilesystem; i++){
866 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
867 return aMap[i].eLockingStyle;
868 }
869 }
870 }
871
872 /* Default case. Handles, amongst others, "nfs". */
873 return testLockingStyle(fd);
danielk1977c70dfc42008-11-19 13:52:30 +0000874#endif /* if IS_VXWORKS */
danielk1977e339d652008-06-28 11:23:00 +0000875 return LOCKING_STYLE_POSIX;
876}
danielk197762c14b32008-11-19 09:05:26 +0000877#else
878 #define detectLockingStyle(x,y,z) LOCKING_STYLE_POSIX
aswiftaebf4132008-11-21 00:10:35 +0000879#endif /* if SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000880
drhbbd42a62004-05-22 17:41:58 +0000881/*
882** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000883** describes that file descriptor. Create new ones if necessary. The
884** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000885**
drh65594042008-05-05 16:56:34 +0000886** Return an appropriate error code.
drhbbd42a62004-05-22 17:41:58 +0000887*/
drh38f82712004-06-18 17:10:16 +0000888static int findLockInfo(
aswiftaebf4132008-11-21 00:10:35 +0000889 unixFile *pFile, /* Unix file with file desc used in the key */
danielk1977c70dfc42008-11-19 13:52:30 +0000890#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000891 void *rnam, /* vxWorks realname */
892#endif
drhbbd42a62004-05-22 17:41:58 +0000893 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000894 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000895){
896 int rc;
aswiftaebf4132008-11-21 00:10:35 +0000897 int fd;
drhbbd42a62004-05-22 17:41:58 +0000898 struct lockKey key1;
899 struct openKey key2;
900 struct stat statbuf;
901 struct lockInfo *pLock;
902 struct openCnt *pOpen;
aswiftaebf4132008-11-21 00:10:35 +0000903 fd = pFile->h;
drhbbd42a62004-05-22 17:41:58 +0000904 rc = fstat(fd, &statbuf);
drh65594042008-05-05 16:56:34 +0000905 if( rc!=0 ){
aswiftaebf4132008-11-21 00:10:35 +0000906 pFile->lastErrno = errno;
drh65594042008-05-05 16:56:34 +0000907#ifdef EOVERFLOW
aswiftaebf4132008-11-21 00:10:35 +0000908 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
drh65594042008-05-05 16:56:34 +0000909#endif
910 return SQLITE_IOERR;
911 }
danielk1977441b09a2006-01-05 13:48:29 +0000912
drh54626242008-07-30 17:28:04 +0000913 /* On OS X on an msdos filesystem, the inode number is reported
914 ** incorrectly for zero-size files. See ticket #3260. To work
915 ** around this problem (we consider it a bug in OS X, not SQLite)
916 ** we always increase the file size to 1 by writing a single byte
917 ** prior to accessing the inode number. The one byte written is
918 ** an ASCII 'S' character which also happens to be the first byte
919 ** in the header of every SQLite database. In this way, if there
920 ** is a race condition such that another thread has already populated
921 ** the first page of the database, no damage is done.
922 */
923 if( statbuf.st_size==0 ){
924 write(fd, "S", 1);
925 rc = fstat(fd, &statbuf);
926 if( rc!=0 ){
aswiftaebf4132008-11-21 00:10:35 +0000927 pFile->lastErrno = errno;
drh54626242008-07-30 17:28:04 +0000928 return SQLITE_IOERR;
929 }
930 }
931
drhbbd42a62004-05-22 17:41:58 +0000932 memset(&key1, 0, sizeof(key1));
933 key1.dev = statbuf.st_dev;
danielk1977c70dfc42008-11-19 13:52:30 +0000934#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000935 key1.rnam = rnam;
936#else
drhbbd42a62004-05-22 17:41:58 +0000937 key1.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000938#endif
drhd677b3d2007-08-20 22:48:41 +0000939#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000940 if( threadsOverrideEachOthersLocks<0 ){
941 testThreadLockingBehavior(fd);
942 }
943 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
944#endif
drhbbd42a62004-05-22 17:41:58 +0000945 memset(&key2, 0, sizeof(key2));
946 key2.dev = statbuf.st_dev;
danielk1977c70dfc42008-11-19 13:52:30 +0000947#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000948 key2.rnam = rnam;
949#else
drhbbd42a62004-05-22 17:41:58 +0000950 key2.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000951#endif
aswiftaebf4132008-11-21 00:10:35 +0000952 if( ppLock!=0 ){
953 pLock = lockList;
954 while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
955 pLock = pLock->pNext;
danielk1977441b09a2006-01-05 13:48:29 +0000956 }
aswiftaebf4132008-11-21 00:10:35 +0000957 if( pLock==0 ){
958 pLock = sqlite3_malloc( sizeof(*pLock) );
959 if( pLock==0 ){
960 rc = SQLITE_NOMEM;
961 goto exit_findlockinfo;
962 }
963 pLock->key = key1;
964 pLock->nRef = 1;
965 pLock->cnt = 0;
966 pLock->locktype = 0;
967 pLock->pNext = lockList;
968 pLock->pPrev = 0;
969 if( lockList ) lockList->pPrev = pLock;
970 lockList = pLock;
971 }else{
972 pLock->nRef++;
973 }
974 *ppLock = pLock;
drhbbd42a62004-05-22 17:41:58 +0000975 }
drh029b44b2006-01-15 00:13:15 +0000976 if( ppOpen!=0 ){
drhda0e7682008-07-30 15:27:54 +0000977 pOpen = openList;
978 while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){
979 pOpen = pOpen->pNext;
980 }
drhbbd42a62004-05-22 17:41:58 +0000981 if( pOpen==0 ){
drh17435752007-08-16 04:30:38 +0000982 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000983 if( pOpen==0 ){
984 releaseLockInfo(pLock);
drh65594042008-05-05 16:56:34 +0000985 rc = SQLITE_NOMEM;
drh029b44b2006-01-15 00:13:15 +0000986 goto exit_findlockinfo;
987 }
988 pOpen->key = key2;
989 pOpen->nRef = 1;
990 pOpen->nLock = 0;
991 pOpen->nPending = 0;
992 pOpen->aPending = 0;
drhda0e7682008-07-30 15:27:54 +0000993 pOpen->pNext = openList;
994 pOpen->pPrev = 0;
995 if( openList ) openList->pPrev = pOpen;
996 openList = pOpen;
danielk1977c70dfc42008-11-19 13:52:30 +0000997#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000998 pOpen->pSem = NULL;
999 pOpen->aSemName[0] = '\0';
1000#endif
drh029b44b2006-01-15 00:13:15 +00001001 }else{
1002 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +00001003 }
drh029b44b2006-01-15 00:13:15 +00001004 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +00001005 }
danielk1977441b09a2006-01-05 13:48:29 +00001006
1007exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +00001008 return rc;
drhbbd42a62004-05-22 17:41:58 +00001009}
1010
drh64b1bea2006-01-15 02:30:57 +00001011#ifdef SQLITE_DEBUG
1012/*
1013** Helper function for printing out trace information from debugging
1014** binaries. This returns the string represetation of the supplied
1015** integer lock-type.
1016*/
1017static const char *locktypeName(int locktype){
1018 switch( locktype ){
1019 case NO_LOCK: return "NONE";
1020 case SHARED_LOCK: return "SHARED";
1021 case RESERVED_LOCK: return "RESERVED";
1022 case PENDING_LOCK: return "PENDING";
1023 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
1024 }
1025 return "ERROR";
1026}
1027#endif
1028
drhbbd42a62004-05-22 17:41:58 +00001029/*
drh029b44b2006-01-15 00:13:15 +00001030** If we are currently in a different thread than the thread that the
1031** unixFile argument belongs to, then transfer ownership of the unixFile
1032** over to the current thread.
1033**
1034** A unixFile is only owned by a thread on systems where one thread is
1035** unable to override locks created by a different thread. RedHat9 is
1036** an example of such a system.
1037**
1038** Ownership transfer is only allowed if the unixFile is currently unlocked.
1039** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +00001040** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +00001041*/
drhd677b3d2007-08-20 22:48:41 +00001042#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +00001043static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +00001044 int rc;
drh029b44b2006-01-15 00:13:15 +00001045 pthread_t hSelf;
1046 if( threadsOverrideEachOthersLocks ){
1047 /* Ownership transfers not needed on this system */
1048 return SQLITE_OK;
1049 }
1050 hSelf = pthread_self();
1051 if( pthread_equal(pFile->tid, hSelf) ){
1052 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +00001053 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +00001054 return SQLITE_OK;
1055 }
1056 if( pFile->locktype!=NO_LOCK ){
1057 /* We cannot change ownership while we are holding a lock! */
1058 return SQLITE_MISUSE;
1059 }
drh4f0c5872007-03-26 22:05:01 +00001060 OSTRACE4("Transfer ownership of %d from %d to %d\n",
1061 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +00001062 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +00001063 if (pFile->pLock != NULL) {
1064 releaseLockInfo(pFile->pLock);
danielk1977c70dfc42008-11-19 13:52:30 +00001065#if IS_VXWORKS
aswiftaebf4132008-11-21 00:10:35 +00001066 rc = findLockInfo(pFile, pFile->zRealpath, &pFile->pLock, 0);
chw97185482008-11-17 08:05:31 +00001067#else
aswiftaebf4132008-11-21 00:10:35 +00001068 rc = findLockInfo(pFile, &pFile->pLock, 0);
chw97185482008-11-17 08:05:31 +00001069#endif
drh4f0c5872007-03-26 22:05:01 +00001070 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001071 locktypeName(pFile->locktype),
1072 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
1073 return rc;
1074 } else {
1075 return SQLITE_OK;
1076 }
drh029b44b2006-01-15 00:13:15 +00001077}
1078#else
drhf1a221e2006-01-15 17:27:17 +00001079 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +00001080# define transferOwnership(X) SQLITE_OK
1081#endif
1082
1083/*
danielk19772a6bdf62007-08-20 16:07:00 +00001084** Seek to the offset passed as the second argument, then read cnt
1085** bytes into pBuf. Return the number of bytes actually read.
drh9e0ebbf2007-10-23 15:59:18 +00001086**
1087** NB: If you define USE_PREAD or USE_PREAD64, then it might also
1088** be necessary to define _XOPEN_SOURCE to be 500. This varies from
1089** one system to another. Since SQLite does not define USE_PREAD
1090** any any form by default, we will not attempt to define _XOPEN_SOURCE.
1091** See tickets #2741 and #2681.
aswiftaebf4132008-11-21 00:10:35 +00001092**
1093** To avoid stomping the errno value on a failed read the lastErrno value
1094** is set before returning.
drhb912b282006-03-23 22:42:20 +00001095*/
danielk197762079062007-08-15 17:08:46 +00001096static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001097 int got;
drh8ebf6702007-02-06 11:11:08 +00001098 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001099 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001100#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001101 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001102 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +00001103#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001104 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001105 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +00001106#else
danielk197762079062007-08-15 17:08:46 +00001107 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +00001108 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +00001109 if( newOffset!=offset ){
aswiftaebf4132008-11-21 00:10:35 +00001110 if( newOffet == -1 ){
1111 ((unixFile*)id)->lastErrno = errno;
1112 }else{
1113 ((unixFile*)id)->lastErrno = 0;
1114 }
drh8ebf6702007-02-06 11:11:08 +00001115 return -1;
1116 }
drhb912b282006-03-23 22:42:20 +00001117 got = read(id->h, pBuf, cnt);
1118#endif
drh15d00c42007-02-27 02:01:14 +00001119 TIMER_END;
aswiftaebf4132008-11-21 00:10:35 +00001120 if( got<0 ){
1121 ((unixFile*)id)->lastErrno = errno;
1122 }
shane9bcbdad2008-05-29 20:22:37 +00001123 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001124 return got;
1125}
1126
1127/*
drhbbd42a62004-05-22 17:41:58 +00001128** Read data from a file into a buffer. Return SQLITE_OK if all
1129** bytes were read successfully and SQLITE_IOERR if anything goes
1130** wrong.
1131*/
danielk197762079062007-08-15 17:08:46 +00001132static int unixRead(
1133 sqlite3_file *id,
1134 void *pBuf,
1135 int amt,
1136 sqlite3_int64 offset
1137){
drhbbd42a62004-05-22 17:41:58 +00001138 int got;
drh9cbe6352005-11-29 03:13:21 +00001139 assert( id );
danielk197762079062007-08-15 17:08:46 +00001140 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +00001141 if( got==amt ){
1142 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +00001143 }else if( got<0 ){
aswiftaebf4132008-11-21 00:10:35 +00001144 /* lastErrno set by seekAndRead */
drh4ac285a2006-09-15 07:28:50 +00001145 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +00001146 }else{
aswiftaebf4132008-11-21 00:10:35 +00001147 ((unixFile*)id)->lastErrno = 0; /* not a system error */
drh4c17c3f2008-11-07 00:06:18 +00001148 /* Unread parts of the buffer must be zero-filled */
drhbafda092007-01-03 23:36:22 +00001149 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +00001150 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +00001151 }
1152}
1153
1154/*
drhb912b282006-03-23 22:42:20 +00001155** Seek to the offset in id->offset then read cnt bytes into pBuf.
1156** Return the number of bytes actually read. Update the offset.
aswiftaebf4132008-11-21 00:10:35 +00001157**
1158** To avoid stomping the errno value on a failed write the lastErrno value
1159** is set before returning.
drhb912b282006-03-23 22:42:20 +00001160*/
danielk197762079062007-08-15 17:08:46 +00001161static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001162 int got;
drh8ebf6702007-02-06 11:11:08 +00001163 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001164 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001165#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001166 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +00001167#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001168 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +00001169#else
danielk197762079062007-08-15 17:08:46 +00001170 newOffset = lseek(id->h, offset, SEEK_SET);
1171 if( newOffset!=offset ){
aswiftaebf4132008-11-21 00:10:35 +00001172 if( newOffet == -1 ){
1173 ((unixFile*)id)->lastErrno = errno;
1174 }else{
1175 ((unixFile*)id)->lastErrno = 0;
1176 }
drh8ebf6702007-02-06 11:11:08 +00001177 return -1;
1178 }
drhb912b282006-03-23 22:42:20 +00001179 got = write(id->h, pBuf, cnt);
1180#endif
drh15d00c42007-02-27 02:01:14 +00001181 TIMER_END;
aswiftaebf4132008-11-21 00:10:35 +00001182 if( got<0 ){
1183 ((unixFile*)id)->lastErrno = errno;
1184 }
1185
shane9bcbdad2008-05-29 20:22:37 +00001186 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001187 return got;
1188}
1189
1190
1191/*
drhbbd42a62004-05-22 17:41:58 +00001192** Write data from a buffer into a file. Return SQLITE_OK on success
1193** or some other error code on failure.
1194*/
danielk197762079062007-08-15 17:08:46 +00001195static int unixWrite(
1196 sqlite3_file *id,
1197 const void *pBuf,
1198 int amt,
1199 sqlite3_int64 offset
1200){
drhbbd42a62004-05-22 17:41:58 +00001201 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +00001202 assert( id );
drh4c7f9412005-02-03 00:29:47 +00001203 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +00001204 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +00001205 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +00001206 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +00001207 pBuf = &((char*)pBuf)[wrote];
1208 }
drh59685932006-09-14 13:47:11 +00001209 SimulateIOError(( wrote=(-1), amt=1 ));
1210 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +00001211 if( amt>0 ){
drh59685932006-09-14 13:47:11 +00001212 if( wrote<0 ){
aswiftaebf4132008-11-21 00:10:35 +00001213 /* lastErrno set by seekAndWrite */
drh4ac285a2006-09-15 07:28:50 +00001214 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +00001215 }else{
aswiftaebf4132008-11-21 00:10:35 +00001216 ((unixFile*)id)->lastErrno = 0; /* not a system error */
drh59685932006-09-14 13:47:11 +00001217 return SQLITE_FULL;
1218 }
drhbbd42a62004-05-22 17:41:58 +00001219 }
1220 return SQLITE_OK;
1221}
1222
drhb851b2c2005-03-10 14:11:12 +00001223#ifdef SQLITE_TEST
1224/*
1225** Count the number of fullsyncs and normal syncs. This is used to test
1226** that syncs and fullsyncs are occuring at the right times.
1227*/
1228int sqlite3_sync_count = 0;
1229int sqlite3_fullsync_count = 0;
1230#endif
1231
drhf2f23912005-10-05 10:29:36 +00001232/*
1233** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
1234** Otherwise use fsync() in its place.
1235*/
1236#ifndef HAVE_FDATASYNC
1237# define fdatasync fsync
1238#endif
1239
drhac530b12006-02-11 01:25:50 +00001240/*
1241** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
1242** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
1243** only available on Mac OS X. But that could change.
1244*/
1245#ifdef F_FULLFSYNC
1246# define HAVE_FULLFSYNC 1
1247#else
1248# define HAVE_FULLFSYNC 0
1249#endif
1250
drhb851b2c2005-03-10 14:11:12 +00001251
drhbbd42a62004-05-22 17:41:58 +00001252/*
drhdd809b02004-07-17 21:44:57 +00001253** The fsync() system call does not work as advertised on many
1254** unix systems. The following procedure is an attempt to make
1255** it work better.
drh1398ad32005-01-19 23:24:50 +00001256**
1257** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
1258** for testing when we want to run through the test suite quickly.
1259** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
1260** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
1261** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +00001262*/
drheb796a72005-09-08 12:38:41 +00001263static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +00001264 int rc;
drhb851b2c2005-03-10 14:11:12 +00001265
danielk1977397d65f2008-11-19 11:35:39 +00001266 /* The following "ifdef/elif/else/" block has the same structure as
1267 ** the one below. It is replicated here solely to avoid cluttering
1268 ** up the real code with the UNUSED_PARAMETER() macros.
1269 */
1270#ifdef SQLITE_NO_SYNC
1271 UNUSED_PARAMETER(fd);
1272 UNUSED_PARAMETER(fullSync);
1273 UNUSED_PARAMETER(dataOnly);
1274#elif HAVE_FULLFSYNC
danielk1977397d65f2008-11-19 11:35:39 +00001275 UNUSED_PARAMETER(dataOnly);
danielk1977f3d3c272008-11-19 16:52:44 +00001276#else
1277 UNUSED_PARAMETER(fullSync);
danielk1977397d65f2008-11-19 11:35:39 +00001278#endif
1279
drhb851b2c2005-03-10 14:11:12 +00001280 /* Record the number of times that we do a normal fsync() and
1281 ** FULLSYNC. This is used during testing to verify that this procedure
1282 ** gets called with the correct arguments.
1283 */
1284#ifdef SQLITE_TEST
1285 if( fullSync ) sqlite3_fullsync_count++;
1286 sqlite3_sync_count++;
1287#endif
1288
1289 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
1290 ** no-op
1291 */
1292#ifdef SQLITE_NO_SYNC
1293 rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00001294#elif HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +00001295 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +00001296 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +00001297 }else{
1298 rc = 1;
1299 }
1300 /* If the FULLFSYNC failed, fall back to attempting an fsync().
1301 * It shouldn't be possible for fullfsync to fail on the local
1302 * file system (on OSX), so failure indicates that FULLFSYNC
1303 * isn't supported for this file system. So, attempt an fsync
1304 * and (for now) ignore the overhead of a superfluous fcntl call.
1305 * It'd be better to detect fullfsync support once and avoid
1306 * the fcntl call every time sync is called.
1307 */
1308 if( rc ) rc = fsync(fd);
1309
1310#else
drheb796a72005-09-08 12:38:41 +00001311 if( dataOnly ){
1312 rc = fdatasync(fd);
danielk1977397d65f2008-11-19 11:35:39 +00001313 if( IS_VXWORKS && rc==-1 && errno==ENOTSUP ){
chw97185482008-11-17 08:05:31 +00001314 rc = fsync(fd);
1315 }
drhf2f23912005-10-05 10:29:36 +00001316 }else{
drheb796a72005-09-08 12:38:41 +00001317 rc = fsync(fd);
1318 }
danielk1977397d65f2008-11-19 11:35:39 +00001319#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +00001320
danielk1977397d65f2008-11-19 11:35:39 +00001321 if( IS_VXWORKS && rc!= -1 ){
chw97185482008-11-17 08:05:31 +00001322 rc = 0;
1323 }
drhdd809b02004-07-17 21:44:57 +00001324 return rc;
1325}
1326
1327/*
drhbbd42a62004-05-22 17:41:58 +00001328** Make sure all writes to a particular file are committed to disk.
1329**
drheb796a72005-09-08 12:38:41 +00001330** If dataOnly==0 then both the file itself and its metadata (file
1331** size, access time, etc) are synced. If dataOnly!=0 then only the
1332** file data is synced.
1333**
drhbbd42a62004-05-22 17:41:58 +00001334** Under Unix, also make sure that the directory entry for the file
1335** has been created by fsync-ing the directory that contains the file.
1336** If we do not do this and we encounter a power failure, the directory
1337** entry for the journal might not exist after we reboot. The next
1338** SQLite to access the file will not know that the journal exists (because
1339** the directory entry for the journal was never created) and the transaction
1340** will not roll back - possibly leading to database corruption.
1341*/
danielk197790949c22007-08-17 16:50:38 +00001342static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +00001343 int rc;
drh054889e2005-11-30 03:20:31 +00001344 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +00001345
danielk1977f036aef2007-08-20 05:36:51 +00001346 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
1347 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
1348
danielk1977c16d4632007-08-30 14:49:58 +00001349 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
danielk1977f036aef2007-08-20 05:36:51 +00001350 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
1351 || (flags&0x0F)==SQLITE_SYNC_FULL
danielk1977f036aef2007-08-20 05:36:51 +00001352 );
danielk197790949c22007-08-17 16:50:38 +00001353
danielk1977cd3b3c82008-09-22 11:46:32 +00001354 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
1355 ** line is to test that doing so does not cause any problems.
1356 */
1357 SimulateDiskfullError( return SQLITE_FULL );
1358
drh054889e2005-11-30 03:20:31 +00001359 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001360 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001361 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001362 SimulateIOError( rc=1 );
1363 if( rc ){
aswiftaebf4132008-11-21 00:10:35 +00001364 pFile->lastErrno = errno;
drh4ac285a2006-09-15 07:28:50 +00001365 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001366 }
drh054889e2005-11-30 03:20:31 +00001367 if( pFile->dirfd>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001368 int err;
drh4f0c5872007-03-26 22:05:01 +00001369 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001370 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001371#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001372 /* The directory sync is only attempted if full_fsync is
1373 ** turned off or unavailable. If a full_fsync occurred above,
1374 ** then the directory sync is superfluous.
1375 */
danielk197790949c22007-08-17 16:50:38 +00001376 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001377 /*
1378 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001379 ** errors when applied to directories on certain file systems.
1380 ** A failed directory sync is not a big deal. So it seems
1381 ** better to ignore the error. Ticket #1657
1382 */
aswiftaebf4132008-11-21 00:10:35 +00001383 /* pFile->lastErrno = errno; */
drh86631a52006-02-09 23:05:51 +00001384 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001385 }
danielk1977d7c03f72005-11-25 10:38:22 +00001386#endif
aswiftaebf4132008-11-21 00:10:35 +00001387 err = close(pFile->dirfd); /* Only need to sync once, so close the */
1388 if( err==0 ){ /* directory when we are done */
1389 pFile->dirfd = -1;
1390 }else{
1391 pFile->lastErrno = errno;
1392 rc = SQLITE_IOERR_DIR_CLOSE;
1393 }
drha2854222004-06-17 19:04:17 +00001394 }
aswiftaebf4132008-11-21 00:10:35 +00001395 return rc;
drhbbd42a62004-05-22 17:41:58 +00001396}
1397
1398/*
1399** Truncate an open file to a specified size
1400*/
danielk197762079062007-08-15 17:08:46 +00001401static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001402 int rc;
drh9cbe6352005-11-29 03:13:21 +00001403 assert( id );
drh93aed5a2008-01-16 17:46:38 +00001404 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
drh63fff5f2007-06-19 10:50:38 +00001405 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001406 if( rc ){
aswiftaebf4132008-11-21 00:10:35 +00001407 ((unixFile*)id)->lastErrno = errno;
drh4ac285a2006-09-15 07:28:50 +00001408 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001409 }else{
1410 return SQLITE_OK;
1411 }
drhbbd42a62004-05-22 17:41:58 +00001412}
1413
1414/*
1415** Determine the current size of a file in bytes
1416*/
danielk197762079062007-08-15 17:08:46 +00001417static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001418 int rc;
drhbbd42a62004-05-22 17:41:58 +00001419 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001420 assert( id );
drh59685932006-09-14 13:47:11 +00001421 rc = fstat(((unixFile*)id)->h, &buf);
1422 SimulateIOError( rc=1 );
1423 if( rc!=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001424 ((unixFile*)id)->lastErrno = errno;
drh4ac285a2006-09-15 07:28:50 +00001425 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001426 }
1427 *pSize = buf.st_size;
drh54626242008-07-30 17:28:04 +00001428
1429 /* When opening a zero-size database, the findLockInfo() procedure
1430 ** writes a single byte into that file in order to work around a bug
1431 ** in the OS-X msdos filesystem. In order to avoid problems with upper
1432 ** layers, we need to report this file size as zero even though it is
1433 ** really 1. Ticket #3260.
1434 */
1435 if( *pSize==1 ) *pSize = 0;
1436
1437
drhbbd42a62004-05-22 17:41:58 +00001438 return SQLITE_OK;
1439}
1440
danielk19779a1d0ab2004-06-01 14:09:28 +00001441/*
aswift5b1a2562008-08-22 00:22:35 +00001442** This routine translates a standard POSIX errno code into something
1443** useful to the clients of the sqlite3 functions. Specifically, it is
1444** intended to translate a variety of "try again" errors into SQLITE_BUSY
1445** and a variety of "please close the file descriptor NOW" errors into
1446** SQLITE_IOERR
1447**
1448** Errors during initialization of locks, or file system support for locks,
1449** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
1450*/
1451static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
1452 switch (posixError) {
1453 case 0:
1454 return SQLITE_OK;
1455
1456 case EAGAIN:
1457 case ETIMEDOUT:
1458 case EBUSY:
1459 case EINTR:
1460 case ENOLCK:
1461 /* random NFS retry error, unless during file system support
1462 * introspection, in which it actually means what it says */
1463 return SQLITE_BUSY;
1464
1465 case EACCES:
1466 /* EACCES is like EAGAIN during locking operations, but not any other time*/
1467 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
1468 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
1469 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
1470 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
1471 return SQLITE_BUSY;
1472 }
1473 /* else fall through */
1474 case EPERM:
1475 return SQLITE_PERM;
1476
1477 case EDEADLK:
1478 return SQLITE_IOERR_BLOCKED;
1479
drhf489c452008-08-22 00:47:53 +00001480#if EOPNOTSUPP!=ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001481 case EOPNOTSUPP:
1482 /* something went terribly awry, unless during file system support
1483 * introspection, in which it actually means what it says */
drhf489c452008-08-22 00:47:53 +00001484#endif
danielk19775ad6a882008-09-15 04:20:31 +00001485#ifdef ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001486 case ENOTSUP:
1487 /* invalid fd, unless during file system support introspection, in which
1488 * it actually means what it says */
danielk19775ad6a882008-09-15 04:20:31 +00001489#endif
aswift5b1a2562008-08-22 00:22:35 +00001490 case EIO:
1491 case EBADF:
1492 case EINVAL:
1493 case ENOTCONN:
1494 case ENODEV:
1495 case ENXIO:
1496 case ENOENT:
1497 case ESTALE:
1498 case ENOSYS:
1499 /* these should force the client to close the file and reconnect */
1500
1501 default:
1502 return sqliteIOErr;
1503 }
1504}
1505
1506/*
danielk197713adf8a2004-06-03 16:08:41 +00001507** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001508** file by this or any other process. If such a lock is held, set *pResOut
1509** to a non-zero value otherwise *pResOut is set to zero. The return value
1510** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001511*/
danielk1977861f7452008-06-05 11:39:11 +00001512static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001513 int rc = SQLITE_OK;
1514 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001515 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001516
danielk1977861f7452008-06-05 11:39:11 +00001517 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1518
drh054889e2005-11-30 03:20:31 +00001519 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001520 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001521
1522 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001523 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001524 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001525 }
1526
drh2ac3ee92004-06-07 16:27:46 +00001527 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001528 */
aswift5b1a2562008-08-22 00:22:35 +00001529 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001530 struct flock lock;
1531 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001532 lock.l_start = RESERVED_BYTE;
1533 lock.l_len = 1;
1534 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001535 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1536 int tErrno = errno;
1537 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1538 pFile->lastErrno = tErrno;
1539 } else if( lock.l_type!=F_UNLCK ){
1540 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001541 }
1542 }
1543
danielk1977b4b47412007-08-17 15:53:36 +00001544 leaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001545 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001546
aswift5b1a2562008-08-22 00:22:35 +00001547 *pResOut = reserved;
1548 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001549}
1550
1551/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001552** Lock the file with the lock specified by parameter locktype - one
1553** of the following:
1554**
drh2ac3ee92004-06-07 16:27:46 +00001555** (1) SHARED_LOCK
1556** (2) RESERVED_LOCK
1557** (3) PENDING_LOCK
1558** (4) EXCLUSIVE_LOCK
1559**
drhb3e04342004-06-08 00:47:47 +00001560** Sometimes when requesting one lock state, additional lock states
1561** are inserted in between. The locking might fail on one of the later
1562** transitions leaving the lock state different from what it started but
1563** still short of its goal. The following chart shows the allowed
1564** transitions and the inserted intermediate states:
1565**
1566** UNLOCKED -> SHARED
1567** SHARED -> RESERVED
1568** SHARED -> (PENDING) -> EXCLUSIVE
1569** RESERVED -> (PENDING) -> EXCLUSIVE
1570** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001571**
drha6abd042004-06-09 17:37:22 +00001572** This routine will only increase a lock. Use the sqlite3OsUnlock()
1573** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001574*/
danielk197762079062007-08-15 17:08:46 +00001575static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001576 /* The following describes the implementation of the various locks and
1577 ** lock transitions in terms of the POSIX advisory shared and exclusive
1578 ** lock primitives (called read-locks and write-locks below, to avoid
1579 ** confusion with SQLite lock names). The algorithms are complicated
1580 ** slightly in order to be compatible with windows systems simultaneously
1581 ** accessing the same database file, in case that is ever required.
1582 **
1583 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1584 ** byte', each single bytes at well known offsets, and the 'shared byte
1585 ** range', a range of 510 bytes at a well known offset.
1586 **
1587 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1588 ** byte'. If this is successful, a random byte from the 'shared byte
1589 ** range' is read-locked and the lock on the 'pending byte' released.
1590 **
danielk197790ba3bd2004-06-25 08:32:25 +00001591 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1592 ** A RESERVED lock is implemented by grabbing a write-lock on the
1593 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001594 **
1595 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001596 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1597 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1598 ** obtained, but existing SHARED locks are allowed to persist. A process
1599 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1600 ** This property is used by the algorithm for rolling back a journal file
1601 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001602 **
danielk197790ba3bd2004-06-25 08:32:25 +00001603 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1604 ** implemented by obtaining a write-lock on the entire 'shared byte
1605 ** range'. Since all other locks require a read-lock on one of the bytes
1606 ** within this range, this ensures that no other locks are held on the
1607 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001608 **
1609 ** The reason a single byte cannot be used instead of the 'shared byte
1610 ** range' is that some versions of windows do not support read-locks. By
1611 ** locking a random byte from a range, concurrent SHARED locks may exist
1612 ** even if the locking primitive used is always a write-lock.
1613 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001614 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001615 unixFile *pFile = (unixFile*)id;
1616 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001617 struct flock lock;
1618 int s;
1619
drh054889e2005-11-30 03:20:31 +00001620 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001621 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001622 locktypeName(locktype), locktypeName(pFile->locktype),
1623 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001624
1625 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001626 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001627 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001628 */
drh054889e2005-11-30 03:20:31 +00001629 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001630 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001631 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001632 return SQLITE_OK;
1633 }
1634
drhb3e04342004-06-08 00:47:47 +00001635 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001636 */
drh054889e2005-11-30 03:20:31 +00001637 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001638 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001639 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001640
drh054889e2005-11-30 03:20:31 +00001641 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001642 */
danielk1977b4b47412007-08-17 15:53:36 +00001643 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001644
drh029b44b2006-01-15 00:13:15 +00001645 /* Make sure the current thread owns the pFile.
1646 */
1647 rc = transferOwnership(pFile);
1648 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001649 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001650 return rc;
1651 }
drh64b1bea2006-01-15 02:30:57 +00001652 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001653
danielk1977ad94b582007-08-20 06:44:22 +00001654 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001655 ** handle that precludes the requested lock, return BUSY.
1656 */
drh054889e2005-11-30 03:20:31 +00001657 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001658 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001659 ){
1660 rc = SQLITE_BUSY;
1661 goto end_lock;
1662 }
1663
1664 /* If a SHARED lock is requested, and some thread using this PID already
1665 ** has a SHARED or RESERVED lock, then increment reference counts and
1666 ** return SQLITE_OK.
1667 */
1668 if( locktype==SHARED_LOCK &&
1669 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1670 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001671 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001672 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001673 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001674 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001675 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001676 goto end_lock;
1677 }
1678
danielk197713adf8a2004-06-03 16:08:41 +00001679 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001680
danielk19779a1d0ab2004-06-01 14:09:28 +00001681 lock.l_whence = SEEK_SET;
1682
drh3cde3bb2004-06-12 02:17:14 +00001683 /* A PENDING lock is needed before acquiring a SHARED lock and before
1684 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1685 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001686 */
drh3cde3bb2004-06-12 02:17:14 +00001687 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001688 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001689 ){
danielk1977489468c2004-06-28 08:25:47 +00001690 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001691 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001692 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001693 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001694 int tErrno = errno;
1695 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1696 if( IS_LOCK_ERROR(rc) ){
1697 pFile->lastErrno = tErrno;
1698 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001699 goto end_lock;
1700 }
drh3cde3bb2004-06-12 02:17:14 +00001701 }
1702
1703
1704 /* If control gets to this point, then actually go ahead and make
1705 ** operating system calls for the specified lock.
1706 */
1707 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001708 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001709 assert( pLock->cnt==0 );
1710 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001711
drh2ac3ee92004-06-07 16:27:46 +00001712 /* Now get the read-lock */
1713 lock.l_start = SHARED_FIRST;
1714 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001715 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1716 tErrno = errno;
1717 }
drh2ac3ee92004-06-07 16:27:46 +00001718 /* Drop the temporary PENDING lock */
1719 lock.l_start = PENDING_BYTE;
1720 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001721 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001722 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001723 if( s != -1 ){
1724 /* This could happen with a network mount */
1725 tErrno = errno;
1726 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1727 if( IS_LOCK_ERROR(rc) ){
1728 pFile->lastErrno = tErrno;
1729 }
1730 goto end_lock;
1731 }
drh2b4b5962005-06-15 17:47:55 +00001732 }
drhe2396a12007-03-29 20:19:58 +00001733 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001734 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1735 if( IS_LOCK_ERROR(rc) ){
1736 pFile->lastErrno = tErrno;
1737 }
drhbbd42a62004-05-22 17:41:58 +00001738 }else{
drh054889e2005-11-30 03:20:31 +00001739 pFile->locktype = SHARED_LOCK;
1740 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001741 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001742 }
drh3cde3bb2004-06-12 02:17:14 +00001743 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1744 /* We are trying for an exclusive lock but another thread in this
1745 ** same process is still holding a shared lock. */
1746 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001747 }else{
drh3cde3bb2004-06-12 02:17:14 +00001748 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001749 ** assumed that there is a SHARED or greater lock on the file
1750 ** already.
1751 */
drh054889e2005-11-30 03:20:31 +00001752 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001753 lock.l_type = F_WRLCK;
1754 switch( locktype ){
1755 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001756 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001757 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001758 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001759 lock.l_start = SHARED_FIRST;
1760 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001761 break;
1762 default:
1763 assert(0);
1764 }
drh054889e2005-11-30 03:20:31 +00001765 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001766 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001767 int tErrno = errno;
1768 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1769 if( IS_LOCK_ERROR(rc) ){
1770 pFile->lastErrno = tErrno;
1771 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001772 }
drhbbd42a62004-05-22 17:41:58 +00001773 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001774
danielk1977ecb2a962004-06-02 06:30:16 +00001775 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001776 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001777 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001778 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001779 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001780 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001781 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001782
1783end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001784 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001785 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001786 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001787 return rc;
1788}
1789
1790/*
drh054889e2005-11-30 03:20:31 +00001791** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001792** must be either NO_LOCK or SHARED_LOCK.
1793**
1794** If the locking level of the file descriptor is already at or below
1795** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001796*/
danielk197762079062007-08-15 17:08:46 +00001797static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001798 struct lockInfo *pLock;
1799 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001800 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001801 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001802 int h;
drha6abd042004-06-09 17:37:22 +00001803
drh054889e2005-11-30 03:20:31 +00001804 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001805 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001806 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001807
1808 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001809 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001810 return SQLITE_OK;
1811 }
drhf1a221e2006-01-15 17:27:17 +00001812 if( CHECK_THREADID(pFile) ){
1813 return SQLITE_MISUSE;
1814 }
danielk1977b4b47412007-08-17 15:53:36 +00001815 enterMutex();
drh1aa5af12008-03-07 19:51:14 +00001816 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001817 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001818 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001819 if( pFile->locktype>SHARED_LOCK ){
1820 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001821 SimulateIOErrorBenign(1);
1822 SimulateIOError( h=(-1) )
1823 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001824 if( locktype==SHARED_LOCK ){
1825 lock.l_type = F_RDLCK;
1826 lock.l_whence = SEEK_SET;
1827 lock.l_start = SHARED_FIRST;
1828 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001829 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001830 int tErrno = errno;
1831 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1832 if( IS_LOCK_ERROR(rc) ){
1833 pFile->lastErrno = tErrno;
1834 }
1835 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001836 }
1837 }
drhbbd42a62004-05-22 17:41:58 +00001838 lock.l_type = F_UNLCK;
1839 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001840 lock.l_start = PENDING_BYTE;
1841 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001842 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001843 pLock->locktype = SHARED_LOCK;
1844 }else{
aswift5b1a2562008-08-22 00:22:35 +00001845 int tErrno = errno;
1846 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1847 if( IS_LOCK_ERROR(rc) ){
1848 pFile->lastErrno = tErrno;
1849 }
1850 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001851 }
drhbbd42a62004-05-22 17:41:58 +00001852 }
drha6abd042004-06-09 17:37:22 +00001853 if( locktype==NO_LOCK ){
1854 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001855
drha6abd042004-06-09 17:37:22 +00001856 /* Decrement the shared lock counter. Release the lock using an
1857 ** OS call only when all threads in this same process have released
1858 ** the lock.
1859 */
1860 pLock->cnt--;
1861 if( pLock->cnt==0 ){
1862 lock.l_type = F_UNLCK;
1863 lock.l_whence = SEEK_SET;
1864 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001865 SimulateIOErrorBenign(1);
1866 SimulateIOError( h=(-1) )
1867 SimulateIOErrorBenign(0);
1868 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001869 pLock->locktype = NO_LOCK;
1870 }else{
aswift5b1a2562008-08-22 00:22:35 +00001871 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001872 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001873 if( IS_LOCK_ERROR(rc) ){
1874 pFile->lastErrno = tErrno;
1875 }
drh1aa5af12008-03-07 19:51:14 +00001876 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001877 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001878 }
drha6abd042004-06-09 17:37:22 +00001879 }
1880
drhbbd42a62004-05-22 17:41:58 +00001881 /* Decrement the count of locks against this same file. When the
1882 ** count reaches zero, close any other file descriptors whose close
1883 ** was deferred because of outstanding locks.
1884 */
drh1aa5af12008-03-07 19:51:14 +00001885 if( rc==SQLITE_OK ){
1886 pOpen = pFile->pOpen;
1887 pOpen->nLock--;
1888 assert( pOpen->nLock>=0 );
1889 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1890 int i;
1891 for(i=0; i<pOpen->nPending; i++){
aswiftaebf4132008-11-21 00:10:35 +00001892 /* close pending fds, but if closing fails don't free the array
1893 ** assign -1 to the successfully closed descriptors and record the
1894 ** error. The next attempt to unlock will try again. */
1895 if( pOpen->aPending[i] < 0 ) continue;
1896 if( close(pOpen->aPending[i]) ){
1897 pFile->lastErrno = errno;
1898 rc = SQLITE_IOERR_CLOSE;
1899 }else{
1900 pOpen->aPending[i] = -1;
1901 }
drh1aa5af12008-03-07 19:51:14 +00001902 }
aswiftaebf4132008-11-21 00:10:35 +00001903 if( rc==SQLITE_OK ){
1904 sqlite3_free(pOpen->aPending);
1905 pOpen->nPending = 0;
1906 pOpen->aPending = 0;
1907 }
drhbbd42a62004-05-22 17:41:58 +00001908 }
drhbbd42a62004-05-22 17:41:58 +00001909 }
1910 }
aswift5b1a2562008-08-22 00:22:35 +00001911
1912end_unlock:
danielk1977b4b47412007-08-17 15:53:36 +00001913 leaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001914 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001915 return rc;
drhbbd42a62004-05-22 17:41:58 +00001916}
1917
1918/*
danielk1977e339d652008-06-28 11:23:00 +00001919** This function performs the parts of the "close file" operation
1920** common to all locking schemes. It closes the directory and file
1921** handles, if they are valid, and sets all fields of the unixFile
1922** structure to 0.
1923*/
1924static int closeUnixFile(sqlite3_file *id){
1925 unixFile *pFile = (unixFile*)id;
1926 if( pFile ){
1927 if( pFile->dirfd>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001928 int err = close(pFile->dirfd);
1929 if( err ){
1930 pFile->lastErrno = errno;
1931 return SQLITE_IOERR_DIR_CLOSE;
1932 }else{
1933 pFile->dirfd=-1;
1934 }
danielk1977e339d652008-06-28 11:23:00 +00001935 }
1936 if( pFile->h>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001937 int err = close(pFile->h);
1938 if( err ){
1939 pFile->lastErrno = errno;
1940 return SQLITE_IOERR_CLOSE;
1941 }
danielk1977e339d652008-06-28 11:23:00 +00001942 }
danielk1977c70dfc42008-11-19 13:52:30 +00001943#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00001944 if( pFile->isDelete && pFile->zRealpath ){
1945 unlink(pFile->zRealpath);
1946 }
1947 if( pFile->zRealpath ){
1948 HashElem *pElem;
1949 int n = strlen(pFile->zRealpath) + 1;
1950 pElem = sqlite3HashFindElem(&nameHash, pFile->zRealpath, n);
1951 if( pElem ){
1952 long cnt = (long)pElem->data;
1953 cnt--;
1954 if( cnt==0 ){
1955 sqlite3HashInsert(&nameHash, pFile->zRealpath, n, 0);
1956 }else{
1957 pElem->data = (void*)cnt;
1958 }
1959 }
1960 }
1961#endif
danielk1977e339d652008-06-28 11:23:00 +00001962 OSTRACE2("CLOSE %-3d\n", pFile->h);
1963 OpenCounter(-1);
1964 memset(pFile, 0, sizeof(unixFile));
1965 }
1966 return SQLITE_OK;
1967}
1968
1969/*
danielk1977e3026632004-06-22 11:29:02 +00001970** Close a file.
1971*/
danielk197762079062007-08-15 17:08:46 +00001972static int unixClose(sqlite3_file *id){
aswiftaebf4132008-11-21 00:10:35 +00001973 int rc = SQLITE_OK;
danielk1977e339d652008-06-28 11:23:00 +00001974 if( id ){
1975 unixFile *pFile = (unixFile *)id;
1976 unixUnlock(id, NO_LOCK);
1977 enterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001978 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001979 /* If there are outstanding locks, do not actually close the file just
1980 ** yet because that would clear those locks. Instead, add the file
1981 ** descriptor to pOpen->aPending. It will be automatically closed when
1982 ** the last lock is cleared.
1983 */
1984 int *aNew;
1985 struct openCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001986 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001987 if( aNew==0 ){
1988 /* If a malloc fails, just leak the file descriptor */
1989 }else{
1990 pOpen->aPending = aNew;
1991 pOpen->aPending[pOpen->nPending] = pFile->h;
1992 pOpen->nPending++;
1993 pFile->h = -1;
1994 }
danielk1977e3026632004-06-22 11:29:02 +00001995 }
danielk1977e339d652008-06-28 11:23:00 +00001996 releaseLockInfo(pFile->pLock);
1997 releaseOpenCnt(pFile->pOpen);
aswiftaebf4132008-11-21 00:10:35 +00001998 rc = closeUnixFile(id);
danielk1977e339d652008-06-28 11:23:00 +00001999 leaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00002000 }
aswiftaebf4132008-11-21 00:10:35 +00002001 return rc;
danielk1977e3026632004-06-22 11:29:02 +00002002}
2003
drhbfe66312006-10-03 17:40:40 +00002004
drh40bbb0a2008-09-23 10:23:26 +00002005#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977c70dfc42008-11-19 13:52:30 +00002006#if !IS_VXWORKS
aswiftaebf4132008-11-21 00:10:35 +00002007#pragma mark AFP support
drhbfe66312006-10-03 17:40:40 +00002008
2009/*
2010 ** The afpLockingContext structure contains all afp lock specific state
2011 */
2012typedef struct afpLockingContext afpLockingContext;
2013struct afpLockingContext {
aswiftaebf4132008-11-21 00:10:35 +00002014 unsigned long long sharedByte;
2015 const char *dbPath;
drhbfe66312006-10-03 17:40:40 +00002016};
2017
2018struct ByteRangeLockPB2
2019{
2020 unsigned long long offset; /* offset to first byte to lock */
2021 unsigned long long length; /* nbr of bytes to lock */
2022 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
2023 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
2024 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
2025 int fd; /* file desc to assoc this lock with */
2026};
2027
drhfd131da2007-08-07 17:13:03 +00002028#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00002029
danielk1977ad94b582007-08-20 06:44:22 +00002030/*
aswift5b1a2562008-08-22 00:22:35 +00002031 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
2032 */
danielk1977ad94b582007-08-20 06:44:22 +00002033static int _AFPFSSetLock(
2034 const char *path,
aswift5b1a2562008-08-22 00:22:35 +00002035 unixFile *pFile,
danielk1977ad94b582007-08-20 06:44:22 +00002036 unsigned long long offset,
2037 unsigned long long length,
2038 int setLockFlag
2039){
drhfd131da2007-08-07 17:13:03 +00002040 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00002041 int err;
2042
2043 pb.unLockFlag = setLockFlag ? 0 : 1;
2044 pb.startEndFlag = 0;
2045 pb.offset = offset;
2046 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00002047 pb.fd = pFile->h;
aswiftaebf4132008-11-21 00:10:35 +00002048 //SimulateIOErrorBenign(1);
2049 //SimulateIOError( pb.fd=(-1) )
2050 //SimulateIOErrorBenign(0);
2051
2052 OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
2053 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), offset, length);
drhbfe66312006-10-03 17:40:40 +00002054 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
2055 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00002056 int rc;
2057 int tErrno = errno;
aswiftaebf4132008-11-21 00:10:35 +00002058 OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno));
2059#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2060 rc = SQLITE_BUSY;
2061#else
2062 rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
2063#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002064 if( IS_LOCK_ERROR(rc) ){
2065 pFile->lastErrno = tErrno;
2066 }
2067 return rc;
drhbfe66312006-10-03 17:40:40 +00002068 } else {
aswift5b1a2562008-08-22 00:22:35 +00002069 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002070 }
2071}
2072
aswift5b1a2562008-08-22 00:22:35 +00002073/* AFP-style reserved lock checking following the behavior of
2074** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002075static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002076 int rc = SQLITE_OK;
2077 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002078 unixFile *pFile = (unixFile*)id;
2079
aswift5b1a2562008-08-22 00:22:35 +00002080 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2081
2082 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00002083 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2084
2085 /* Check if a thread in this process holds such a lock */
2086 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002087 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00002088 }
2089
2090 /* Otherwise see if some other process holds it.
2091 */
aswift5b1a2562008-08-22 00:22:35 +00002092 if( !reserved ){
2093 /* lock the RESERVED byte */
aswiftaebf4132008-11-21 00:10:35 +00002094 int lrc = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
aswift5b1a2562008-08-22 00:22:35 +00002095 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00002096 /* if we succeeded in taking the reserved lock, unlock it to restore
2097 ** the original state */
aswiftaebf4132008-11-21 00:10:35 +00002098 lrc = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswift5b1a2562008-08-22 00:22:35 +00002099 } else {
2100 /* if we failed to get the lock then someone else must have it */
2101 reserved = 1;
2102 }
2103 if( IS_LOCK_ERROR(lrc) ){
2104 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00002105 }
2106 }
drhbfe66312006-10-03 17:40:40 +00002107
aswift5b1a2562008-08-22 00:22:35 +00002108 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2109
2110 *pResOut = reserved;
2111 return rc;
drhbfe66312006-10-03 17:40:40 +00002112}
2113
2114/* AFP-style locking following the behavior of unixLock, see the unixLock
2115** function comments for details of lock management. */
danielk1977e339d652008-06-28 11:23:00 +00002116static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00002117 int rc = SQLITE_OK;
2118 unixFile *pFile = (unixFile*)id;
2119 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002120
2121 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002122 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002123 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2124
drhbfe66312006-10-03 17:40:40 +00002125 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002126 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2127 ** enterMutex() hasn't been called yet.
2128 */
drhbfe66312006-10-03 17:40:40 +00002129 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002130 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002131 locktypeName(locktype));
2132 return SQLITE_OK;
2133 }
2134
2135 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002136 */
drhbfe66312006-10-03 17:40:40 +00002137 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2138 assert( locktype!=PENDING_LOCK );
2139 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2140
2141 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002142 */
danielk1977b4b47412007-08-17 15:53:36 +00002143 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002144
2145 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002146 */
drhbfe66312006-10-03 17:40:40 +00002147 rc = transferOwnership(pFile);
2148 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00002149 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002150 return rc;
2151 }
2152
2153 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002154 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2155 ** be released.
2156 */
drhbfe66312006-10-03 17:40:40 +00002157 if( locktype==SHARED_LOCK
2158 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002159 ){
2160 int failed;
aswiftaebf4132008-11-21 00:10:35 +00002161 failed = _AFPFSSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002162 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002163 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002164 goto afp_end_lock;
2165 }
2166 }
2167
2168 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002169 ** operating system calls for the specified lock.
2170 */
drhbfe66312006-10-03 17:40:40 +00002171 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002172 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002173
aswift5b1a2562008-08-22 00:22:35 +00002174 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002175 /* note that the quality of the randomness doesn't matter that much */
2176 lk = random();
aswiftaebf4132008-11-21 00:10:35 +00002177 context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
2178 lrc1 = _AFPFSSetLock(context->dbPath, pFile,
2179 SHARED_FIRST+context->sharedByte, 1, 1);
aswift5b1a2562008-08-22 00:22:35 +00002180 if( IS_LOCK_ERROR(lrc1) ){
2181 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002182 }
aswift5b1a2562008-08-22 00:22:35 +00002183 /* Drop the temporary PENDING lock */
aswiftaebf4132008-11-21 00:10:35 +00002184 lrc2 = _AFPFSSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002185
aswift5b1a2562008-08-22 00:22:35 +00002186 if( IS_LOCK_ERROR(lrc1) ) {
2187 pFile->lastErrno = lrc1Errno;
2188 rc = lrc1;
2189 goto afp_end_lock;
2190 } else if( IS_LOCK_ERROR(lrc2) ){
2191 rc = lrc2;
2192 goto afp_end_lock;
2193 } else if( lrc1 != SQLITE_OK ) {
2194 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002195 } else {
2196 pFile->locktype = SHARED_LOCK;
aswiftaebf4132008-11-21 00:10:35 +00002197 pFile->pOpen->nLock++;
drhbfe66312006-10-03 17:40:40 +00002198 }
2199 }else{
2200 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2201 ** assumed that there is a SHARED or greater lock on the file
2202 ** already.
2203 */
2204 int failed = 0;
2205 assert( 0!=pFile->locktype );
2206 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2207 /* Acquire a RESERVED lock */
aswiftaebf4132008-11-21 00:10:35 +00002208 failed = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002209 }
2210 if (!failed && locktype == EXCLUSIVE_LOCK) {
2211 /* Acquire an EXCLUSIVE lock */
2212
2213 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002214 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002215 */
aswiftaebf4132008-11-21 00:10:35 +00002216 if( !(failed = _AFPFSSetLock(context->dbPath, pFile, SHARED_FIRST +
2217 context->sharedByte, 1, 0)) ){
2218 int failed2 = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002219 /* now attemmpt to get the exclusive lock range */
aswiftaebf4132008-11-21 00:10:35 +00002220 failed = _AFPFSSetLock(context->dbPath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002221 SHARED_SIZE, 1);
aswiftaebf4132008-11-21 00:10:35 +00002222 if( failed && (failed2 = _AFPFSSetLock(context->dbPath, pFile,
2223 SHARED_FIRST + context->sharedByte, 1, 1)) ){
2224 /* Can't reestablish the shared lock. Sqlite can't deal, this is
2225 ** a critical I/O error
2226 */
2227 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
2228 SQLITE_IOERR_LOCK;
2229 goto afp_end_lock;
2230 }
2231 }else{
aswift5b1a2562008-08-22 00:22:35 +00002232 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002233 }
2234 }
aswift5b1a2562008-08-22 00:22:35 +00002235 if( failed ){
2236 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002237 }
2238 }
2239
2240 if( rc==SQLITE_OK ){
2241 pFile->locktype = locktype;
2242 }else if( locktype==EXCLUSIVE_LOCK ){
2243 pFile->locktype = PENDING_LOCK;
2244 }
2245
2246afp_end_lock:
drh339eb0b2008-03-07 15:34:11 +00002247 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002248 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002249 rc==SQLITE_OK ? "ok" : "failed");
2250 return rc;
2251}
2252
2253/*
drh339eb0b2008-03-07 15:34:11 +00002254** Lower the locking level on file descriptor pFile to locktype. locktype
2255** must be either NO_LOCK or SHARED_LOCK.
2256**
2257** If the locking level of the file descriptor is already at or below
2258** the requested locking level, this routine is a no-op.
2259*/
danielk1977e339d652008-06-28 11:23:00 +00002260static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002261 int rc = SQLITE_OK;
2262 unixFile *pFile = (unixFile*)id;
aswiftaebf4132008-11-21 00:10:35 +00002263 afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002264
2265 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002266 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002267 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002268
drhbfe66312006-10-03 17:40:40 +00002269 assert( locktype<=SHARED_LOCK );
2270 if( pFile->locktype<=locktype ){
2271 return SQLITE_OK;
2272 }
2273 if( CHECK_THREADID(pFile) ){
2274 return SQLITE_MISUSE;
2275 }
danielk1977b4b47412007-08-17 15:53:36 +00002276 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002277 if( pFile->locktype>SHARED_LOCK ){
aswiftaebf4132008-11-21 00:10:35 +00002278
2279 if( pFile->locktype==EXCLUSIVE_LOCK ){
2280 rc = _AFPFSSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
2281 if( rc==SQLITE_OK && locktype==SHARED_LOCK ){
2282 /* only re-establish the shared lock if necessary */
2283 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
2284 rc = _AFPFSSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
2285 }
2286 }
2287 if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
2288 rc = _AFPFSSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
2289 }
2290 if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){
2291 rc = _AFPFSSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
2292 }
2293 }else if( locktype==NO_LOCK ){
2294 /* clear the shared lock */
2295 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
2296 rc = _AFPFSSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
2297 }
drhbfe66312006-10-03 17:40:40 +00002298
aswiftaebf4132008-11-21 00:10:35 +00002299 if( rc==SQLITE_OK ){
2300 if( locktype==NO_LOCK ){
2301 struct openCnt *pOpen = pFile->pOpen;
2302 pOpen->nLock--;
2303 assert( pOpen->nLock>=0 );
2304 if( pOpen->nLock==0 && pOpen->nPending>0 ){
2305 int i;
2306 for(i=0; i<pOpen->nPending; i++){
2307 if( pOpen->aPending[i] < 0 ) continue;
2308 if( close(pOpen->aPending[i]) ){
2309 pFile->lastErrno = errno;
2310 rc = SQLITE_IOERR_CLOSE;
2311 }else{
2312 pOpen->aPending[i] = -1;
drhbfe66312006-10-03 17:40:40 +00002313 }
aswiftaebf4132008-11-21 00:10:35 +00002314 }
2315 if( rc==SQLITE_OK ){
2316 sqlite3_free(pOpen->aPending);
2317 pOpen->nPending = 0;
2318 pOpen->aPending = 0;
2319 }
drhbfe66312006-10-03 17:40:40 +00002320 }
2321 }
drhbfe66312006-10-03 17:40:40 +00002322 }
aswiftaebf4132008-11-21 00:10:35 +00002323end_afpunlock:
danielk1977b4b47412007-08-17 15:53:36 +00002324 leaveMutex();
aswiftaebf4132008-11-21 00:10:35 +00002325 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002326 return rc;
2327}
2328
2329/*
drh339eb0b2008-03-07 15:34:11 +00002330** Close a file & cleanup AFP specific locking context
2331*/
danielk1977e339d652008-06-28 11:23:00 +00002332static int afpClose(sqlite3_file *id) {
2333 if( id ){
2334 unixFile *pFile = (unixFile*)id;
2335 afpUnlock(id, NO_LOCK);
aswiftaebf4132008-11-21 00:10:35 +00002336 enterMutex();
2337 if( pFile->pOpen && pFile->pOpen->nLock ){
2338 /* If there are outstanding locks, do not actually close the file just
2339 ** yet because that would clear those locks. Instead, add the file
2340 ** descriptor to pOpen->aPending. It will be automatically closed when
2341 ** the last lock is cleared.
2342 */
2343 int *aNew;
2344 struct openCnt *pOpen = pFile->pOpen;
2345 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
2346 if( aNew==0 ){
2347 /* If a malloc fails, just leak the file descriptor */
2348 }else{
2349 pOpen->aPending = aNew;
2350 pOpen->aPending[pOpen->nPending] = pFile->h;
2351 pOpen->nPending++;
2352 pFile->h = -1;
2353 }
2354 }
2355 releaseOpenCnt(pFile->pOpen);
danielk1977e339d652008-06-28 11:23:00 +00002356 sqlite3_free(pFile->lockingContext);
aswiftaebf4132008-11-21 00:10:35 +00002357 closeUnixFile(id);
2358 leaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00002359 }
aswiftaebf4132008-11-21 00:10:35 +00002360 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002361}
2362
2363
2364#pragma mark flock() style locking
2365
2366/*
drh339eb0b2008-03-07 15:34:11 +00002367** The flockLockingContext is not used
2368*/
drhbfe66312006-10-03 17:40:40 +00002369typedef void flockLockingContext;
2370
aswift5b1a2562008-08-22 00:22:35 +00002371/* flock-style reserved lock checking following the behavior of
2372 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002373static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002374 int rc = SQLITE_OK;
2375 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002376 unixFile *pFile = (unixFile*)id;
2377
aswift5b1a2562008-08-22 00:22:35 +00002378 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2379
2380 assert( pFile );
2381
2382 /* Check if a thread in this process holds such a lock */
2383 if( pFile->locktype>SHARED_LOCK ){
2384 reserved = 1;
2385 }
2386
2387 /* Otherwise see if some other process holds it. */
2388 if( !reserved ){
drh3b62b2f2007-06-08 18:27:03 +00002389 /* attempt to get the lock */
aswift5b1a2562008-08-22 00:22:35 +00002390 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
2391 if( !lrc ){
drh3b62b2f2007-06-08 18:27:03 +00002392 /* got the lock, unlock it */
aswift5b1a2562008-08-22 00:22:35 +00002393 lrc = flock(pFile->h, LOCK_UN);
2394 if ( lrc ) {
2395 int tErrno = errno;
2396 /* unlock failed with an error */
2397 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2398 if( IS_LOCK_ERROR(lrc) ){
2399 pFile->lastErrno = tErrno;
2400 rc = lrc;
2401 }
2402 }
2403 } else {
2404 int tErrno = errno;
2405 reserved = 1;
2406 /* someone else might have it reserved */
2407 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2408 if( IS_LOCK_ERROR(lrc) ){
2409 pFile->lastErrno = tErrno;
2410 rc = lrc;
2411 }
drhbfe66312006-10-03 17:40:40 +00002412 }
drhbfe66312006-10-03 17:40:40 +00002413 }
aswift5b1a2562008-08-22 00:22:35 +00002414 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002415
aswiftaebf4132008-11-21 00:10:35 +00002416#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2417 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
2418 rc = SQLITE_OK;
2419 reserved=1;
2420 }
2421#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002422 *pResOut = reserved;
2423 return rc;
drhbfe66312006-10-03 17:40:40 +00002424}
2425
danielk1977e339d652008-06-28 11:23:00 +00002426static int flockLock(sqlite3_file *id, int locktype) {
aswift5b1a2562008-08-22 00:22:35 +00002427 int rc = SQLITE_OK;
aswiftaebf4132008-11-21 00:10:35 +00002428 int lrc;
drhbfe66312006-10-03 17:40:40 +00002429 unixFile *pFile = (unixFile*)id;
aswift5b1a2562008-08-22 00:22:35 +00002430
2431 assert( pFile );
2432
drh3b62b2f2007-06-08 18:27:03 +00002433 /* if we already have a lock, it is exclusive.
2434 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002435 if (pFile->locktype > NO_LOCK) {
2436 pFile->locktype = locktype;
2437 return SQLITE_OK;
2438 }
2439
drh3b62b2f2007-06-08 18:27:03 +00002440 /* grab an exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002441
2442 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
2443 int tErrno = errno;
drh3b62b2f2007-06-08 18:27:03 +00002444 /* didn't get, must be busy */
aswift5b1a2562008-08-22 00:22:35 +00002445 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2446 if( IS_LOCK_ERROR(rc) ){
2447 pFile->lastErrno = tErrno;
2448 }
drhbfe66312006-10-03 17:40:40 +00002449 } else {
drh3b62b2f2007-06-08 18:27:03 +00002450 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002451 pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002452 }
aswift5b1a2562008-08-22 00:22:35 +00002453 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
2454 rc==SQLITE_OK ? "ok" : "failed");
aswiftaebf4132008-11-21 00:10:35 +00002455#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2456 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
2457 rc = SQLITE_BUSY;
2458 }
2459#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002460 return rc;
drhbfe66312006-10-03 17:40:40 +00002461}
2462
danielk1977e339d652008-06-28 11:23:00 +00002463static int flockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002464 unixFile *pFile = (unixFile*)id;
2465
aswift5b1a2562008-08-22 00:22:35 +00002466 assert( pFile );
2467 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2468 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002469 assert( locktype<=SHARED_LOCK );
2470
drh3b62b2f2007-06-08 18:27:03 +00002471 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002472 if( pFile->locktype==locktype ){
2473 return SQLITE_OK;
2474 }
2475
drh3b62b2f2007-06-08 18:27:03 +00002476 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002477 if (locktype==SHARED_LOCK) {
2478 pFile->locktype = locktype;
2479 return SQLITE_OK;
2480 }
2481
drh3b62b2f2007-06-08 18:27:03 +00002482 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00002483 int rc = flock(pFile->h, LOCK_UN);
aswift5b1a2562008-08-22 00:22:35 +00002484 if (rc) {
2485 int r, tErrno = errno;
2486 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2487 if( IS_LOCK_ERROR(r) ){
2488 pFile->lastErrno = tErrno;
2489 }
aswiftaebf4132008-11-21 00:10:35 +00002490#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2491 if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
2492 r = SQLITE_BUSY;
2493 }
2494#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2495
aswift5b1a2562008-08-22 00:22:35 +00002496 return r;
2497 } else {
drhbfe66312006-10-03 17:40:40 +00002498 pFile->locktype = NO_LOCK;
2499 return SQLITE_OK;
2500 }
2501}
2502
2503/*
drh339eb0b2008-03-07 15:34:11 +00002504** Close a file.
2505*/
danielk1977e339d652008-06-28 11:23:00 +00002506static int flockClose(sqlite3_file *id) {
2507 if( id ){
2508 flockUnlock(id, NO_LOCK);
2509 }
2510 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002511}
2512
danielk1977c70dfc42008-11-19 13:52:30 +00002513#endif /* !IS_VXWORKS */
chw97185482008-11-17 08:05:31 +00002514
drhbfe66312006-10-03 17:40:40 +00002515#pragma mark Old-School .lock file based locking
aswiftaebf4132008-11-21 00:10:35 +00002516#define DOTLOCK_SUFFIX ".lock"
drhbfe66312006-10-03 17:40:40 +00002517
aswift5b1a2562008-08-22 00:22:35 +00002518/* Dotlock-style reserved lock checking following the behavior of
2519** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002520static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
aswift5b1a2562008-08-22 00:22:35 +00002521 int rc = SQLITE_OK;
2522 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002523 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002524
aswift5b1a2562008-08-22 00:22:35 +00002525 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2526
2527 assert( pFile );
2528
2529 /* Check if a thread in this process holds such a lock */
2530 if( pFile->locktype>SHARED_LOCK ){
2531 reserved = 1;
2532 }
2533
2534 /* Otherwise see if some other process holds it. */
2535 if( !reserved ){
2536 char *zLockFile = (char *)pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002537 struct stat statBuf;
aswift5b1a2562008-08-22 00:22:35 +00002538
2539 if( lstat(zLockFile, &statBuf)==0 ){
2540 /* file exists, someone else has the lock */
2541 reserved = 1;
2542 }else{
drh3b62b2f2007-06-08 18:27:03 +00002543 /* file does not exist, we could have it if we want it */
chw97185482008-11-17 08:05:31 +00002544 int tErrno = errno;
aswift5b1a2562008-08-22 00:22:35 +00002545 if( ENOENT != tErrno ){
2546 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2547 pFile->lastErrno = tErrno;
2548 }
drh339eb0b2008-03-07 15:34:11 +00002549 }
drhbfe66312006-10-03 17:40:40 +00002550 }
aswift5b1a2562008-08-22 00:22:35 +00002551 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002552
aswift5b1a2562008-08-22 00:22:35 +00002553 *pResOut = reserved;
2554 return rc;
drhbfe66312006-10-03 17:40:40 +00002555}
2556
danielk1977e339d652008-06-28 11:23:00 +00002557static int dotlockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002558 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002559 int fd;
danielk1977e339d652008-06-28 11:23:00 +00002560 char *zLockFile = (char *)pFile->lockingContext;
aswift5b1a2562008-08-22 00:22:35 +00002561 int rc=SQLITE_OK;
drh339eb0b2008-03-07 15:34:11 +00002562
drh3b62b2f2007-06-08 18:27:03 +00002563 /* if we already have a lock, it is exclusive.
2564 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002565 if (pFile->locktype > NO_LOCK) {
2566 pFile->locktype = locktype;
danielk1977c70dfc42008-11-19 13:52:30 +00002567#if !IS_VXWORKS
drhbfe66312006-10-03 17:40:40 +00002568 /* Always update the timestamp on the old file */
danielk1977e339d652008-06-28 11:23:00 +00002569 utimes(zLockFile, NULL);
chw97185482008-11-17 08:05:31 +00002570#endif
aswift5b1a2562008-08-22 00:22:35 +00002571 rc = SQLITE_OK;
2572 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002573 }
2574
drh3b62b2f2007-06-08 18:27:03 +00002575 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00002576 struct stat statBuf;
danielk1977e339d652008-06-28 11:23:00 +00002577 if (lstat(zLockFile,&statBuf) == 0){
aswift5b1a2562008-08-22 00:22:35 +00002578 rc = SQLITE_BUSY; /* it does, busy */
2579 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002580 }
2581
drh3b62b2f2007-06-08 18:27:03 +00002582 /* grab an exclusive lock */
danielk1977e339d652008-06-28 11:23:00 +00002583 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
drh339eb0b2008-03-07 15:34:11 +00002584 if( fd<0 ){
drh3b62b2f2007-06-08 18:27:03 +00002585 /* failed to open/create the file, someone else may have stolen the lock */
aswift5b1a2562008-08-22 00:22:35 +00002586 int tErrno = errno;
2587 if( EEXIST == tErrno ){
2588 rc = SQLITE_BUSY;
2589 } else {
2590 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2591 if( IS_LOCK_ERROR(rc) ){
aswiftaebf4132008-11-21 00:10:35 +00002592 pFile->lastErrno = tErrno;
aswift5b1a2562008-08-22 00:22:35 +00002593 }
2594 }
2595 goto dotlock_end_lock;
2596 }
aswiftaebf4132008-11-21 00:10:35 +00002597 if( close(fd) ){
2598 pFile->lastErrno = errno;
2599 rc = SQLITE_IOERR_CLOSE;
2600 }
drhbfe66312006-10-03 17:40:40 +00002601
drh3b62b2f2007-06-08 18:27:03 +00002602 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002603 pFile->locktype = locktype;
aswift5b1a2562008-08-22 00:22:35 +00002604
2605 dotlock_end_lock:
2606 return rc;
drhbfe66312006-10-03 17:40:40 +00002607}
2608
danielk1977e339d652008-06-28 11:23:00 +00002609static int dotlockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002610 unixFile *pFile = (unixFile*)id;
danielk1977e339d652008-06-28 11:23:00 +00002611 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00002612
aswift5b1a2562008-08-22 00:22:35 +00002613 assert( pFile );
2614 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2615 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002616 assert( locktype<=SHARED_LOCK );
2617
drh3b62b2f2007-06-08 18:27:03 +00002618 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002619 if( pFile->locktype==locktype ){
2620 return SQLITE_OK;
2621 }
2622
drh3b62b2f2007-06-08 18:27:03 +00002623 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002624 if (locktype==SHARED_LOCK) {
2625 pFile->locktype = locktype;
2626 return SQLITE_OK;
2627 }
2628
drh3b62b2f2007-06-08 18:27:03 +00002629 /* no, really, unlock. */
aswift5b1a2562008-08-22 00:22:35 +00002630 if (unlink(zLockFile) ) {
2631 int rc, tErrno = errno;
2632 if( ENOENT != tErrno ){
2633 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2634 }
2635 if( IS_LOCK_ERROR(rc) ){
2636 pFile->lastErrno = tErrno;
2637 }
2638 return rc;
2639 }
drhbfe66312006-10-03 17:40:40 +00002640 pFile->locktype = NO_LOCK;
2641 return SQLITE_OK;
2642}
2643
2644/*
2645 ** Close a file.
2646 */
danielk1977e339d652008-06-28 11:23:00 +00002647static int dotlockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00002648 int rc;
danielk1977e339d652008-06-28 11:23:00 +00002649 if( id ){
2650 unixFile *pFile = (unixFile*)id;
2651 dotlockUnlock(id, NO_LOCK);
2652 sqlite3_free(pFile->lockingContext);
2653 }
danielk1977c70dfc42008-11-19 13:52:30 +00002654 if( IS_VXWORKS ) enterMutex();
chw97185482008-11-17 08:05:31 +00002655 rc = closeUnixFile(id);
danielk1977c70dfc42008-11-19 13:52:30 +00002656 if( IS_VXWORKS ) leaveMutex();
chw97185482008-11-17 08:05:31 +00002657 return rc;
drhbfe66312006-10-03 17:40:40 +00002658}
2659
danielk1977c70dfc42008-11-19 13:52:30 +00002660#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00002661
2662#pragma mark POSIX/vxWorks named semaphore based locking
2663
2664/* Namedsem-style reserved lock checking following the behavior of
2665** unixCheckReservedLock, see the unixCheckReservedLock function comments */
2666static int namedsemCheckReservedLock(sqlite3_file *id, int *pResOut) {
2667 int rc = SQLITE_OK;
2668 int reserved = 0;
2669 unixFile *pFile = (unixFile*)id;
2670
2671 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2672
2673 assert( pFile );
2674
2675 /* Check if a thread in this process holds such a lock */
2676 if( pFile->locktype>SHARED_LOCK ){
2677 reserved = 1;
2678 }
2679
2680 /* Otherwise see if some other process holds it. */
2681 if( !reserved ){
2682 sem_t *pSem = pFile->pOpen->pSem;
2683 struct stat statBuf;
2684
2685 if( sem_trywait(pSem)==-1 ){
2686 int tErrno = errno;
2687 if( EAGAIN != tErrno ){
2688 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2689 pFile->lastErrno = tErrno;
2690 } else {
aswiftaebf4132008-11-21 00:10:35 +00002691 /* someone else has the lock when we are in NO_LOCK */
2692 reserved = (pFile->locktype < SHARED_LOCK);
chw97185482008-11-17 08:05:31 +00002693 }
2694 }else{
2695 /* we could have it if we want it */
2696 sem_post(pSem);
2697 }
2698 }
2699 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2700
2701 *pResOut = reserved;
2702 return rc;
2703}
2704
2705static int namedsemLock(sqlite3_file *id, int locktype) {
2706 unixFile *pFile = (unixFile*)id;
2707 int fd;
2708 sem_t *pSem = pFile->pOpen->pSem;
2709 int rc = SQLITE_OK;
2710
2711 /* if we already have a lock, it is exclusive.
2712 ** Just adjust level and punt on outta here. */
2713 if (pFile->locktype > NO_LOCK) {
2714 pFile->locktype = locktype;
2715 rc = SQLITE_OK;
2716 goto namedsem_end_lock;
2717 }
2718
2719 /* lock semaphore now but bail out when already locked. */
2720 if( sem_trywait(pSem)==-1 ){
2721 rc = SQLITE_BUSY;
2722 goto namedsem_end_lock;
2723 }
2724
2725 /* got it, set the type and return ok */
2726 pFile->locktype = locktype;
2727
2728 namedsem_end_lock:
2729 return rc;
2730}
2731
2732static int namedsemUnlock(sqlite3_file *id, int locktype) {
2733 unixFile *pFile = (unixFile*)id;
2734 sem_t *pSem = pFile->pOpen->pSem;
2735
2736 assert( pFile );
2737 assert( pSem );
2738 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2739 pFile->locktype, getpid());
2740 assert( locktype<=SHARED_LOCK );
2741
2742 /* no-op if possible */
2743 if( pFile->locktype==locktype ){
2744 return SQLITE_OK;
2745 }
2746
2747 /* shared can just be set because we always have an exclusive */
2748 if (locktype==SHARED_LOCK) {
2749 pFile->locktype = locktype;
2750 return SQLITE_OK;
2751 }
2752
2753 /* no, really unlock. */
2754 if ( sem_post(pSem)==-1 ) {
2755 int rc, tErrno = errno;
2756 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2757 if( IS_LOCK_ERROR(rc) ){
2758 pFile->lastErrno = tErrno;
2759 }
2760 return rc;
2761 }
2762 pFile->locktype = NO_LOCK;
2763 return SQLITE_OK;
2764}
2765
2766/*
2767 ** Close a file.
2768 */
2769static int namedsemClose(sqlite3_file *id) {
2770 if( id ){
2771 unixFile *pFile = (unixFile*)id;
2772 namedsemUnlock(id, NO_LOCK);
2773 assert( pFile );
2774 enterMutex();
2775 releaseLockInfo(pFile->pLock);
2776 releaseOpenCnt(pFile->pOpen);
2777 closeUnixFile(id);
2778 leaveMutex();
2779 }
2780 return SQLITE_OK;
2781}
2782
danielk1977c70dfc42008-11-19 13:52:30 +00002783#endif /* IS_VXWORKS */
drhbfe66312006-10-03 17:40:40 +00002784
aswiftaebf4132008-11-21 00:10:35 +00002785#pragma mark Proxy locking support
2786
2787static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
2788 unixFile *pFile = (unixFile*)id;
2789 int rc = takeConch(pFile);
2790 if( rc==SQLITE_OK ){
2791 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2792 unixFile *proxy = pCtx->lockProxy;
2793 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
2794 }
2795 return rc;
2796}
2797
2798static int proxyLock(sqlite3_file *id, int locktype) {
2799 unixFile *pFile = (unixFile*)id;
2800 int rc = takeConch(pFile);
2801 if( rc==SQLITE_OK ){
2802 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2803 unixFile *proxy = pCtx->lockProxy;
2804 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
2805 pFile->locktype = proxy->locktype;
2806 }
2807 return rc;
2808}
2809
2810static int proxyUnlock(sqlite3_file *id, int locktype) {
2811 unixFile *pFile = (unixFile*)id;
2812 int rc = takeConch(pFile);
2813 if( rc==SQLITE_OK ){
2814 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2815 unixFile *proxy = pCtx->lockProxy;
2816 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
2817 pFile->locktype = proxy->locktype;
2818 }
2819 return rc;
2820}
2821
2822/*
2823 ** Close a file.
2824 */
2825static int proxyClose(sqlite3_file *id) {
2826 if( id ){
2827 unixFile *pFile = (unixFile*)id;
2828 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2829 unixFile *lockProxy = pCtx->lockProxy;
2830 unixFile *conchFile = pCtx->conchFile;
2831 int rc = SQLITE_OK;
2832
2833 if( lockProxy ){
2834 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
2835 if( rc ) return rc;
2836 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
2837 if( rc ) return rc;
2838 sqlite3_free(lockProxy);
2839 }
2840 if( conchFile ){
2841 if( pCtx->conchHeld ){
2842 rc = releaseConch(pFile);
2843 if( rc ) return rc;
2844 }
2845 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
2846 if( rc ) return rc;
2847 sqlite3_free(conchFile);
2848 }
2849 sqlite3_free(pCtx->lockProxyPath);
2850 sqlite3_free(pCtx->conchFilePath);
2851 sqlite3_free(pCtx->dbPath);
2852 /* restore the original locking context and pMethod then close it */
2853 pFile->lockingContext = pCtx->oldLockingContext;
2854 pFile->pMethod = pCtx->pOldMethod;
2855 sqlite3_free(pCtx);
2856 return pFile->pMethod->xClose(id);
2857 }
2858 return SQLITE_OK;
2859}
2860
2861/* HOSTIDLEN and CONCHLEN both include space for the string
2862** terminating nul
2863*/
2864#define HOSTIDLEN 128
2865#define CONCHLEN (MAXPATHLEN+HOSTIDLEN+1)
2866#ifndef HOSTIDPATH
2867# define HOSTIDPATH "/Library/Caches/.com.apple.sqliteConchHostId"
2868#endif
2869
2870/* basically a copy of unixRandomness with different
2871** test behavior built in */
2872static int genHostID(char *pHostID){
2873 int pid, fd, i, len;
2874 unsigned char *key = (unsigned char *)pHostID;
2875
2876 memset(key, 0, HOSTIDLEN);
2877 len = 0;
2878 fd = open("/dev/urandom", O_RDONLY);
2879 if( fd>=0 ){
2880 len = read(fd, key, HOSTIDLEN);
2881 close(fd); /* silently leak the fd if it fails */
2882 }
2883 if( len < HOSTIDLEN ){
2884 time_t t;
2885 time(&t);
2886 memcpy(key, &t, sizeof(t));
2887 pid = getpid();
2888 memcpy(&key[sizeof(t)], &pid, sizeof(pid));
2889 }
2890
2891#ifdef MAKE_PRETTY_HOSTID
2892 /* filter the bytes into printable ascii characters and NUL terminate */
2893 key[(HOSTIDLEN-1)] = 0x00;
2894 for( i=0; i<(HOSTIDLEN-1); i++ ){
2895 unsigned char pa = key[i]&0x7F;
2896 if( pa<0x20 ){
2897 key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20;
2898 }else if( pa==0x7F ){
2899 key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E;
2900 }
2901 }
2902#endif
2903 return SQLITE_OK;
2904}
2905
2906#ifdef SQLITE_TEST
2907/* simulate multiple hosts by creating unique hostid file paths */
2908int sqlite3_hostid_num = 0;
2909#endif
2910
2911/* writes the host id path to path, path should be an pre-allocated buffer
2912** with enough space for a path */
2913static int getHostIDPath(char *path, size_t len){
2914 strlcpy(path, HOSTIDPATH, len);
2915#ifdef SQLITE_TEST
2916 if( sqlite3_hostid_num>0 ){
2917 char suffix[2] = "1";
2918 suffix[0] = suffix[0] + sqlite3_hostid_num;
2919 strlcat(path, suffix, len);
2920 }
2921#endif
2922 OSTRACE3("GETHOSTIDPATH %s pid=%d\n", path, getpid());
2923}
2924
2925/* get the host ID from a sqlite hostid file stored in the
2926** user-specific tmp directory, create the ID if it's not there already
2927*/
2928static int getHostID(char *pHostID, int *pError){
2929 int fd;
2930 char path[MAXPATHLEN];
2931 size_t len;
2932 int rc=SQLITE_OK;
2933
2934 getHostIDPath(path, MAXPATHLEN);
2935 /* try to create the host ID file, if it already exists read the contents */
2936 fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644);
2937 if( fd<0 ){
2938 int err=errno;
2939
2940 if( err!=EEXIST ){
2941#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
2942 fprintf(stderr, "sqlite error creating host ID file %s: %s\n", path, strerror(err));
2943#endif
2944 return SQLITE_PERM;
2945 }
2946 /* couldn't create the file, read it instead */
2947 fd = open(path, O_RDONLY|O_EXCL);
2948 if( fd<0 ){
2949 int err = errno;
2950#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
2951 fprintf(stderr, "sqlite error opening host ID file %s: %s\n", path, strerror(err));
2952#endif
2953 return SQLITE_PERM;
2954 }
2955 len = pread(fd, pHostID, HOSTIDLEN, 0);
2956 if( len<0 ){
2957 *pError = errno;
2958 rc = SQLITE_IOERR_READ;
2959 }else if( len<HOSTIDLEN ){
2960 *pError = 0;
2961 rc = SQLITE_IOERR_SHORT_READ;
2962 }
2963 close(fd); /* silently leak the fd if it fails */
2964 OSTRACE3("GETHOSTID read %s pid=%d\n", pHostID, getpid());
2965 return rc;
2966 }else{
2967 int i;
2968 /* we're creating the host ID file (use a random string of bytes) */
2969 genHostID(pHostID);
2970 len = pwrite(fd, pHostID, HOSTIDLEN, 0);
2971 if( len<0 ){
2972 *pError = errno;
2973 rc = SQLITE_IOERR_WRITE;
2974 }else if( len<HOSTIDLEN ){
2975 *pError = 0;
2976 rc = SQLITE_IOERR_WRITE;
2977 }
2978 close(fd); /* silently leak the fd if it fails */
2979 OSTRACE3("GETHOSTID wrote %s pid=%d\n", pHostID, getpid());
2980 return rc;
2981 }
2982}
2983
2984/* takes the conch by taking a shared lock and read the contents conch, if
2985** lockPath is non-NULL, the host ID and lock file path must match. A NULL
2986** lockPath means that the lockPath in the conch file will be used if the
2987** host IDs match, or a new lock path will be generated automatically
2988** and written to the conch file.
2989*/
2990static int takeConch(unixFile *pFile){
2991 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2992
2993 if( pCtx->conchHeld>0 ){
2994 return SQLITE_OK;
2995 }else{
2996 unixFile *conchFile = pCtx->conchFile;
2997 char testValue[CONCHLEN];
2998 char conchValue[CONCHLEN];
2999 char lockPath[MAXPATHLEN];
3000 char *tLockPath = NULL;
3001 int rc = SQLITE_OK;
3002 int readRc = SQLITE_OK;
3003 int syncPerms = 0;
3004
3005 OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h,
3006 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
3007
3008 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
3009 if( rc==SQLITE_OK ){
3010 int pError = 0;
3011 memset(testValue, 0, CONCHLEN); // conch is fixed size
3012 rc = getHostID(testValue, &pError);
3013 if( rc&SQLITE_IOERR==SQLITE_IOERR ){
3014 pFile->lastErrno = pError;
3015 }
3016 if( pCtx->lockProxyPath ){
3017 strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN);
3018 }
3019 }
3020 if( rc!=SQLITE_OK ){
3021 goto end_takeconch;
3022 }
3023
3024 readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0);
3025 if( readRc!=SQLITE_IOERR_SHORT_READ ){
3026 int match = 0;
3027 if( readRc!=SQLITE_OK ){
3028 if( rc&SQLITE_IOERR==SQLITE_IOERR ){
3029 pFile->lastErrno = conchFile->lastErrno;
3030 }
3031 rc = readRc;
3032 goto end_takeconch;
3033 }
3034 /* if the conch has data compare the contents */
3035 if( !pCtx->lockProxyPath ){
3036 /* for auto-named local lock file, just check the host ID and we'll
3037 ** use the local lock file path that's already in there */
3038 if( !memcmp(testValue, conchValue, HOSTIDLEN) ){
3039 tLockPath = (char *)&conchValue[HOSTIDLEN];
3040 goto end_takeconch;
3041 }
3042 }else{
3043 /* we've got the conch if conchValue matches our path and host ID */
3044 if( !memcmp(testValue, conchValue, CONCHLEN) ){
3045 goto end_takeconch;
3046 }
3047 }
3048 }else{
3049 /* a short read means we're "creating" the conch (even though it could
3050 ** have been user-intervention), if we acquire the exclusive lock,
3051 ** we'll try to match the current on-disk permissions of the database
3052 */
3053 syncPerms = 1;
3054 }
3055
3056 /* either conch was emtpy or didn't match */
3057 if( !pCtx->lockProxyPath ){
3058 getLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
3059 tLockPath = lockPath;
3060 strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN);
3061 }
3062
3063 /* update conch with host and path (this will fail if other process
3064 ** has a shared lock already) */
3065 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
3066 if( rc==SQLITE_OK ){
3067 rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0);
3068 if( rc==SQLITE_OK && syncPerms ){
3069 struct stat buf;
3070 int err = fstat(pFile->h, &buf);
3071 if( err==0 ){
3072 mode_t mode = buf.st_mode & 0100666;
3073 /* try to match the database file permissions, ignore failure */
3074#ifndef SQLITE_PROXY_DEBUG
3075 fchmod(conchFile->h, buf.st_mode);
3076#else
3077 if( fchmod(conchFile->h, buf.st_mode)!=0 ){
3078 int code = errno;
3079 fprintf(stderr, "fchmod %o FAILED with %d %s\n",buf.st_mode, code, strerror(code));
3080 } else {
3081 fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode);
3082 }
3083 }else{
3084 int code = errno;
3085 fprintf(stderr, "STAT FAILED[%d] with %d %s\n", err, code, strerror(code));
3086#endif
3087 }
3088 }
3089 }
3090 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
3091
3092end_takeconch:
3093 OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h);
3094 if( rc==SQLITE_OK && pFile->oflags ){
3095 if( pFile->h>=0 ){
3096#ifdef STRICT_CLOSE_ERROR
3097 if( close(pFile->h) ){
3098 pFile->lastErrno = errno;
3099 return SQLITE_IOERR_CLOSE;
3100 }
3101#else
3102 close(pFile->h); /* silently leak fd if fail */
3103#endif
3104 }
3105 pFile->h = -1;
3106 int fd = open(pCtx->dbPath, pFile->oflags, SQLITE_DEFAULT_FILE_PERMISSIONS);
3107 OSTRACE2("TRANSPROXY: OPEN %d\n", fd);
3108 if( fd>=0 ){
3109 pFile->h = fd;
3110 }else{
3111 rc=SQLITE_CANTOPEN; // SQLITE_BUSY? takeConch called during locking
3112 }
3113 }
3114 if( rc==SQLITE_OK && !pCtx->lockProxy ){
3115 char *path = tLockPath ? tLockPath : pCtx->lockProxyPath;
3116 // ACS: Need to make a copy of path sometimes
3117 rc = createProxyUnixFile(path, &pCtx->lockProxy);
3118 }
3119 if( rc==SQLITE_OK ){
3120 pCtx->conchHeld = 1;
3121
3122 if( tLockPath ){
3123 pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath);
3124 if( pCtx->lockProxy->pMethod == ioMethodForLockingStyle(LOCKING_STYLE_AFP) ){
3125 ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath = pCtx->lockProxyPath;
3126 }
3127 }
3128 } else {
3129 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
3130 }
3131 OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK ? "ok" : "failed");
3132 return rc;
3133 }
3134}
3135
3136static int releaseConch(unixFile *pFile){
3137 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
3138 int rc;
3139 unixFile *conchFile = pCtx->conchFile;
3140
3141 OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
3142 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
3143 getpid());
3144 pCtx->conchHeld = 0;
3145 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
3146 OSTRACE3("RELEASECONCH %d %s\n", conchFile->h,
3147 (rc==SQLITE_OK ? "ok" : "failed"));
3148 return rc;
3149}
3150
3151static int getConchPathFromDBPath(char *dbPath, char **pConchPath){
3152 int i;
3153 int len = strlen(dbPath);
3154 char *conchPath;
3155
3156 conchPath = (char *)sqlite3_malloc(len + 8);
3157 if( conchPath==0 ){
3158 return SQLITE_NOMEM;
3159 }
3160 strlcpy(conchPath, dbPath, len+1);
3161
3162 /* now insert a "." before the last / character */
3163 for( i=(len-1); i>=0; i-- ){
3164 if( conchPath[i]=='/' ){
3165 i++;
3166 break;
3167 }
3168 }
3169 conchPath[i]='.';
3170 while ( i<len ){
3171 conchPath[i+1]=dbPath[i];
3172 i++;
3173 }
3174 conchPath[i+1]='\0';
3175 strlcat(conchPath, "-conch", len + 8);
3176 *pConchPath = conchPath;
3177 return SQLITE_OK;
3178}
3179
3180static int getLockPath(const char *dbPath, char *lPath, size_t maxLen){
3181 int len;
3182 int dbLen;
3183 int i;
3184
3185#ifdef LOCKPROXYDIR
3186 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
3187#else
3188# ifdef _CS_DARWIN_USER_TEMP_DIR
3189 {
3190 char utdir[MAXPATHLEN];
3191
3192 confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
3193 len = strlcat(lPath, "sqliteplocks", maxLen);
3194 if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
3195 /* if mkdir fails, handle as lock file creation failure */
3196 int err = errno;
3197# ifdef SQLITE_DEBUG
3198 if( err!=EEXIST ){
3199 fprintf(stderr, "getLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
3200 SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
3201 }
3202# endif
3203 }else{
3204 OSTRACE3("GETLOCKPATH mkdir %s pid=%d\n", lPath, getpid());
3205 }
3206
3207 }
3208# else
3209 len = strlcpy(lPath, "/tmp/", maxLen);
3210# endif
3211#endif
3212
3213 if( lPath[len-1]!='/' ){
3214 len = strlcat(lPath, "/", maxLen);
3215 }
3216
3217 /* transform the db path to a unique cache name */
3218 dbLen = strlen(dbPath);
3219 for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
3220 char c = dbPath[i];
3221 lPath[i+len] = (c=='/')?'_':c;
3222 }
3223 lPath[i+len]='\0';
3224 strlcat(lPath, ":auto:", maxLen);
3225 return SQLITE_OK;
3226}
3227
3228/* Takes a fully configured proxy locking-style unix file and switches
3229** the local lock file path
3230*/
3231static int switchLockProxyPath(unixFile *pFile, const char *path) {
3232 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
3233 char *oldPath = pCtx->lockProxyPath;
3234 int taken = 0;
3235 int rc = SQLITE_OK;
3236
3237 if( pFile->locktype!=NO_LOCK ){
3238 return SQLITE_BUSY;
3239 }
3240
3241 /* nothing to do if the path is NULL, :auto: or matches the existing path */
3242 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
3243 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
3244 return SQLITE_OK;
3245 }else{
3246 unixFile *lockProxy = pCtx->lockProxy;
3247 pCtx->lockProxy=NULL;
3248 pCtx->conchHeld = 0;
3249 if( lockProxy!=NULL ){
3250 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
3251 if( rc ) return rc;
3252 sqlite3_free(lockProxy);
3253 }
3254 sqlite3_free(oldPath);
3255 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
3256 }
3257
3258 return rc;
3259}
3260
3261/*
3262** Takes an already filled in unix file and alters it so all file locking
3263** will be performed on the local proxy lock file. The following fields
3264** are preserved in the locking context so that they can be restored and
3265** the unix structure properly cleaned up at close time:
3266** ->lockingContext
3267** ->pMethod
3268*/
3269static int transformUnixFileForLockProxy(unixFile *pFile, const char *path) {
3270 proxyLockingContext *pCtx;
3271 char dbPath[MAXPATHLEN];
3272 char *lockPath=NULL;
3273 int rc = SQLITE_OK;
3274
3275 if( pFile->locktype!=NO_LOCK ){
3276 return SQLITE_BUSY;
3277 }
3278 getDbPathForUnixFile(pFile, dbPath);
3279 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
3280 lockPath=NULL;
3281 }else{
3282 lockPath=(char *)path;
3283 }
3284
3285 OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h,
3286 (lockPath ? lockPath : ":auto:"), getpid());
3287
3288 pCtx = sqlite3_malloc( sizeof(*pCtx) );
3289 if( pCtx==0 ){
3290 return SQLITE_NOMEM;
3291 }
3292 memset(pCtx, 0, sizeof(*pCtx));
3293
3294 rc = getConchPathFromDBPath(dbPath, &pCtx->conchFilePath);
3295 if( rc==SQLITE_OK ){
3296 rc = createProxyUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
3297 }
3298 if( rc==SQLITE_OK && lockPath ){
3299 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
3300 }
3301
3302end_transform_file:
3303 if( rc==SQLITE_OK ){
3304 /* all memory is allocated, proxys are created and assigned,
3305 ** switch the locking context and pMethod then return.
3306 */
3307 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
3308 pCtx->oldLockingContext = pFile->lockingContext;
3309 pFile->lockingContext = pCtx;
3310 pCtx->pOldMethod = pFile->pMethod;
3311 pFile->pMethod = ioMethodForLockingStyle(LOCKING_STYLE_PROXY);
3312 }else{
3313 if( pCtx->conchFile ){
3314 rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
3315 if( rc ) return rc;
3316 sqlite3_free(pCtx->conchFile);
3317 }
3318 sqlite3_free(pCtx->conchFilePath);
3319 sqlite3_free(pCtx);
3320 }
3321 OSTRACE3("TRANSPROXY %d %s\n", pFile->h,
3322 (rc==SQLITE_OK ? "ok" : "failed"));
3323 return rc;
3324}
3325
3326static int createProxyUnixFile(const char *path, unixFile **ppFile) {
3327 int fd;
3328 int dirfd = -1;
3329 unixFile *pNew;
3330 int rc = SQLITE_OK;
3331
3332 fd = open(path, O_RDWR | O_CREAT, SQLITE_DEFAULT_FILE_PERMISSIONS);
3333 if( fd<0 ){
3334 return SQLITE_CANTOPEN;
3335 }
3336
3337 pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
3338 if( pNew==NULL ){
3339 rc = SQLITE_NOMEM;
3340 goto end_create_proxy;
3341 }
3342 memset(pNew, 0, sizeof(unixFile));
3343
3344 rc = fillInUnixFile(NULL, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
3345 if( rc==SQLITE_OK ){
3346 *ppFile = pNew;
3347 return SQLITE_OK;
3348 }
3349end_create_proxy:
3350 close(fd); /* silently leak fd if error, we're already in error */
3351 sqlite3_free(pNew);
3352 return rc;
3353}
3354
3355
drhda0e7682008-07-30 15:27:54 +00003356#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +00003357
3358/*
drh339eb0b2008-03-07 15:34:11 +00003359** The nolockLockingContext is void
3360*/
drhbfe66312006-10-03 17:40:40 +00003361typedef void nolockLockingContext;
3362
danielk1977397d65f2008-11-19 11:35:39 +00003363static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
3364 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00003365 *pResOut = 0;
3366 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00003367}
3368
danielk1977397d65f2008-11-19 11:35:39 +00003369static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
3370 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbfe66312006-10-03 17:40:40 +00003371 return SQLITE_OK;
3372}
3373
danielk1977397d65f2008-11-19 11:35:39 +00003374static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
3375 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbfe66312006-10-03 17:40:40 +00003376 return SQLITE_OK;
3377}
3378
3379/*
drh339eb0b2008-03-07 15:34:11 +00003380** Close a file.
3381*/
danielk1977e339d652008-06-28 11:23:00 +00003382static int nolockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00003383 int rc;
danielk1977397d65f2008-11-19 11:35:39 +00003384 if( IS_VXWORKS ) enterMutex();
chw97185482008-11-17 08:05:31 +00003385 rc = closeUnixFile(id);
danielk1977397d65f2008-11-19 11:35:39 +00003386 if( IS_VXWORKS ) leaveMutex();
chw97185482008-11-17 08:05:31 +00003387 return rc;
drhbfe66312006-10-03 17:40:40 +00003388}
3389
danielk1977ad94b582007-08-20 06:44:22 +00003390
danielk1977e3026632004-06-22 11:29:02 +00003391/*
drh9e33c2c2007-08-31 18:34:59 +00003392** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00003393*/
drhcc6bb3e2007-08-31 16:11:35 +00003394static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00003395 switch( op ){
3396 case SQLITE_FCNTL_LOCKSTATE: {
3397 *(int*)pArg = ((unixFile*)id)->locktype;
3398 return SQLITE_OK;
3399 }
aswiftaebf4132008-11-21 00:10:35 +00003400 case SQLITE_GET_LOCKPROXYFILE: {
3401#if SQLITE_ENABLE_LOCKING_STYLE
3402 unixFile *pFile = (unixFile*)id;
3403 if( pFile->pMethod == ioMethodForLockingStyle(LOCKING_STYLE_PROXY) ){
3404 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
3405 takeConch(pFile);
3406 if( pCtx->lockProxyPath ){
3407 *(const char **)pArg = pCtx->lockProxyPath;
3408 }else{
3409 *(const char **)pArg = ":auto: (not held)";
3410 }
3411 } else {
3412 *(const char **)pArg = NULL;
3413 }
3414#else
3415 *(void*)pArg = NULL;
3416#endif
3417 return SQLITE_OK;
3418 }
3419 case SQLITE_SET_LOCKPROXYFILE: {
3420#if SQLITE_ENABLE_LOCKING_STYLE
3421 unixFile *pFile = (unixFile*)id;
3422 int rc = SQLITE_OK;
3423 int isProxyStyle = (pFile->pMethod == ioMethodForLockingStyle(LOCKING_STYLE_PROXY));
3424 if( pArg==NULL || (const char *)pArg==0 ){
3425 if( isProxyStyle ){
3426 // turn off proxy locking - not supported
3427 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
3428 }else{
3429 // turn off proxy locking - already off - NOOP
3430 rc = SQLITE_OK;
3431 }
3432 }else{
3433 const char *proxyPath = (const char *)pArg;
3434 if( isProxyStyle ){
3435 proxyLockingContext *pCtx =
3436 (proxyLockingContext*)pFile->lockingContext;
3437 if( !strcmp(pArg, ":auto:") || (pCtx->lockProxyPath && !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) ){
3438 rc = SQLITE_OK;
3439 }else{
3440 rc = switchLockProxyPath(pFile, proxyPath);
3441 }
3442 }else{
3443 // turn on proxy file locking
3444 rc = transformUnixFileForLockProxy(pFile, proxyPath);
3445 }
3446 }
3447 return rc;
3448#else
3449 return SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
3450#endif
3451 }
3452 case SQLITE_LAST_ERRNO: {
3453 *(int*)pArg = ((unixFile*)id)->lastErrno;
3454 return SQLITE_OK;
3455 }
3456
drh9e33c2c2007-08-31 18:34:59 +00003457 }
drhcc6bb3e2007-08-31 16:11:35 +00003458 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00003459}
3460
3461/*
danielk1977a3d4c882007-03-23 10:08:38 +00003462** Return the sector size in bytes of the underlying block device for
3463** the specified file. This is almost always 512 bytes, but may be
3464** larger for some devices.
3465**
3466** SQLite code assumes this function cannot fail. It also assumes that
3467** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00003468** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00003469** same for both.
3470*/
danielk1977397d65f2008-11-19 11:35:39 +00003471static int unixSectorSize(sqlite3_file *NotUsed){
3472 UNUSED_PARAMETER(NotUsed);
drh3ceeb752007-03-29 18:19:52 +00003473 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00003474}
3475
danielk197790949c22007-08-17 16:50:38 +00003476/*
danielk1977397d65f2008-11-19 11:35:39 +00003477** Return the device characteristics for the file. This is always 0 for unix.
danielk197790949c22007-08-17 16:50:38 +00003478*/
danielk1977397d65f2008-11-19 11:35:39 +00003479static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
3480 UNUSED_PARAMETER(NotUsed);
danielk197762079062007-08-15 17:08:46 +00003481 return 0;
3482}
3483
aswiftaebf4132008-11-21 00:10:35 +00003484#define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \
34851, /* iVersion */ \
3486xClose, /* xClose */ \
3487unixRead, /* xRead */ \
3488unixWrite, /* xWrite */ \
3489unixTruncate, /* xTruncate */ \
3490unixSync, /* xSync */ \
3491unixFileSize, /* xFileSize */ \
3492xLock, /* xLock */ \
3493xUnlock, /* xUnlock */ \
3494xCheckReservedLock, /* xCheckReservedLock */ \
3495unixFileControl, /* xFileControl */ \
3496unixSectorSize, /* xSectorSize */ \
3497unixDeviceCharacteristics /* xDeviceCapabilities */ \
3498}
3499static sqlite3_io_methods aIoMethod[] = {
3500IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
3501,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3502#if SQLITE_ENABLE_LOCKING_STYLE
3503,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
3504#if IS_VXWORKS
3505 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3506 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3507 ,IOMETHODS(namedsemClose, namedsemLock, namedsemUnlock, namedsemCheckReservedLock)
3508 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3509#else
3510 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
3511 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
3512 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3513 ,IOMETHODS(proxyClose, proxyLock, proxyUnlock, proxyCheckReservedLock)
3514#endif
3515#endif
3516/* The order of the IOMETHODS macros above is important. It must be the
3517 ** same order as the LOCKING_STYLE numbers
3518 */
3519};
3520
danielk1977a3d4c882007-03-23 10:08:38 +00003521/*
danielk1977e339d652008-06-28 11:23:00 +00003522** Initialize the contents of the unixFile structure pointed to by pId.
3523**
danielk1977ad94b582007-08-20 06:44:22 +00003524** When locking extensions are enabled, the filepath and locking style
3525** are needed to determine the unixFile pMethod to use for locking operations.
3526** The locking-style specific lockingContext data structure is created
3527** and assigned here also.
3528*/
3529static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00003530 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00003531 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00003532 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00003533 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00003534 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00003535 int noLock, /* Omit locking if true */
3536 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00003537){
drhda0e7682008-07-30 15:27:54 +00003538 int eLockingStyle;
3539 unixFile *pNew = (unixFile *)pId;
3540 int rc = SQLITE_OK;
3541
danielk197717b90b52008-06-06 11:11:25 +00003542 assert( pNew->pLock==NULL );
3543 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00003544
danielk1977a03396a2008-11-19 14:35:46 +00003545 /* Parameter isDelete is only used on vxworks. Parameter pVfs is only
3546 ** used if ENABLE_LOCKING_STYLE is defined. Express this explicitly
3547 ** here to prevent compiler warnings about unused parameters.
3548 */
danielk1977f3d3c272008-11-19 16:52:44 +00003549 if( !IS_VXWORKS ) UNUSED_PARAMETER(isDelete);
3550 if( !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(pVfs);
3551 if( !IS_VXWORKS && !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(zFilename);
danielk1977a03396a2008-11-19 14:35:46 +00003552
drh218c5082008-03-07 00:27:10 +00003553 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00003554 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00003555 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00003556 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00003557
danielk1977c70dfc42008-11-19 13:52:30 +00003558#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003559 {
3560 HashElem *pElem;
3561 char *zRealname = vxrealpath(zFilename, 1);
3562 int n;
3563 pNew->zRealpath = 0;
3564 if( !zRealname ){
3565 rc = SQLITE_NOMEM;
3566 eLockingStyle = LOCKING_STYLE_NONE;
3567 }else{
3568 n = strlen(zRealname) + 1;
3569 enterMutex();
3570 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
3571 if( pElem ){
3572 long cnt = (long)pElem->data;
3573 cnt++;
3574 pNew->zRealpath = pElem->pKey;
3575 pElem->data = (void*)cnt;
3576 }else{
3577 if( sqlite3HashInsert(&nameHash, zRealname, n, (void*)1)==0 ){
3578 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
3579 if( pElem ){
3580 pNew->zRealpath = pElem->pKey;
3581 }else{
3582 sqlite3HashInsert(&nameHash, zRealname, n, 0);
3583 rc = SQLITE_NOMEM;
3584 eLockingStyle = LOCKING_STYLE_NONE;
3585 }
3586 }
3587 }
3588 leaveMutex();
3589 sqlite3_free(zRealname);
3590 }
3591 }
3592#endif
3593
drhda0e7682008-07-30 15:27:54 +00003594 if( noLock ){
3595 eLockingStyle = LOCKING_STYLE_NONE;
3596 }else{
3597 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
aswiftaebf4132008-11-21 00:10:35 +00003598#if SQLITE_ENABLE_LOCKING_STYLE
3599 /* Cache zFilename in the locking context (AFP and dotlock override) for
3600 ** proxyLock activation is possible (remote proxy is based on db name)
3601 ** zFilename remains valid until file is closed, to support */
3602 pNew->lockingContext = (void*)zFilename;
3603#endif
drhda0e7682008-07-30 15:27:54 +00003604 }
danielk1977e339d652008-06-28 11:23:00 +00003605
aswiftaebf4132008-11-21 00:10:35 +00003606 /* Macro to define the static contents of an sqlite3_io_methods
3607 ** structure for a unix backend file. Different locking methods
3608 ** require different functions for the xClose, xLock, xUnlock and
3609 ** xCheckReservedLock methods.
3610 */
3611 assert(LOCKING_STYLE_POSIX==1);
3612 assert(LOCKING_STYLE_NONE==2);
3613 assert(LOCKING_STYLE_DOTFILE==3);
3614 assert(LOCKING_STYLE_FLOCK==4);
3615 assert(LOCKING_STYLE_AFP==5);
3616 assert(LOCKING_STYLE_NAMEDSEM==6);
3617 assert(LOCKING_STYLE_PROXY==7);
3618
danielk1977e339d652008-06-28 11:23:00 +00003619 switch( eLockingStyle ){
3620
3621 case LOCKING_STYLE_POSIX: {
3622 enterMutex();
danielk1977c70dfc42008-11-19 13:52:30 +00003623#if IS_VXWORKS
aswiftaebf4132008-11-21 00:10:35 +00003624 rc = findLockInfo(pNew, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
chw97185482008-11-17 08:05:31 +00003625#else
aswiftaebf4132008-11-21 00:10:35 +00003626 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
chw97185482008-11-17 08:05:31 +00003627#endif
danielk1977e339d652008-06-28 11:23:00 +00003628 leaveMutex();
drh218c5082008-03-07 00:27:10 +00003629 break;
drhbfe66312006-10-03 17:40:40 +00003630 }
danielk1977e339d652008-06-28 11:23:00 +00003631
drh40bbb0a2008-09-23 10:23:26 +00003632#if SQLITE_ENABLE_LOCKING_STYLE
chw97185482008-11-17 08:05:31 +00003633
danielk1977c70dfc42008-11-19 13:52:30 +00003634#if !IS_VXWORKS
danielk1977e339d652008-06-28 11:23:00 +00003635 case LOCKING_STYLE_AFP: {
3636 /* AFP locking uses the file path so it needs to be included in
3637 ** the afpLockingContext.
3638 */
3639 afpLockingContext *pCtx;
3640 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
3641 if( pCtx==0 ){
3642 rc = SQLITE_NOMEM;
3643 }else{
3644 /* NB: zFilename exists and remains valid until the file is closed
3645 ** according to requirement F11141. So we do not need to make a
3646 ** copy of the filename. */
aswiftaebf4132008-11-21 00:10:35 +00003647 pCtx->dbPath = zFilename;
danielk1977e339d652008-06-28 11:23:00 +00003648 srandomdev();
aswiftaebf4132008-11-21 00:10:35 +00003649 enterMutex();
3650 rc = findLockInfo(pNew, NULL, &pNew->pOpen);
3651 leaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00003652 }
drh218c5082008-03-07 00:27:10 +00003653 break;
danielk1977e339d652008-06-28 11:23:00 +00003654 }
chw97185482008-11-17 08:05:31 +00003655#endif
danielk1977e339d652008-06-28 11:23:00 +00003656
3657 case LOCKING_STYLE_DOTFILE: {
3658 /* Dotfile locking uses the file path so it needs to be included in
3659 ** the dotlockLockingContext
3660 */
3661 char *zLockFile;
drh218c5082008-03-07 00:27:10 +00003662 int nFilename;
danielk1977e339d652008-06-28 11:23:00 +00003663 nFilename = strlen(zFilename) + 6;
3664 zLockFile = (char *)sqlite3_malloc(nFilename);
3665 if( zLockFile==0 ){
3666 rc = SQLITE_NOMEM;
3667 }else{
aswiftaebf4132008-11-21 00:10:35 +00003668 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
drh339eb0b2008-03-07 15:34:11 +00003669 }
danielk1977e339d652008-06-28 11:23:00 +00003670 pNew->lockingContext = zLockFile;
drh218c5082008-03-07 00:27:10 +00003671 break;
3672 }
danielk1977e339d652008-06-28 11:23:00 +00003673
danielk1977c70dfc42008-11-19 13:52:30 +00003674#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003675 case LOCKING_STYLE_NAMEDSEM: {
3676 /* Named semaphore locking uses the file path so it needs to be
aswiftaebf4132008-11-21 00:10:35 +00003677 ** included in the namedsemLockingContext
3678 */
chw97185482008-11-17 08:05:31 +00003679 enterMutex();
3680 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
3681 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
3682 char *zSemName = pNew->pOpen->aSemName;
3683 int n;
3684 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem", pNew->zRealpath);
3685 for( n=0; zSemName[n]; n++ )
3686 if( zSemName[n]=='/' ) zSemName[n] = '_';
3687 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
3688 if( pNew->pOpen->pSem == SEM_FAILED ){
3689 rc = SQLITE_NOMEM;
3690 pNew->pOpen->aSemName[0] = '\0';
3691 }
3692 }
3693 leaveMutex();
3694 break;
3695 }
3696#endif
3697
danielk1977e339d652008-06-28 11:23:00 +00003698 case LOCKING_STYLE_FLOCK:
3699 case LOCKING_STYLE_NONE:
drh218c5082008-03-07 00:27:10 +00003700 break;
drhe78669b2007-06-29 12:04:26 +00003701#endif
danielk1977e339d652008-06-28 11:23:00 +00003702 }
aswift5b1a2562008-08-22 00:22:35 +00003703
3704 pNew->lastErrno = 0;
danielk1977c70dfc42008-11-19 13:52:30 +00003705#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003706 if( rc!=SQLITE_OK ){
3707 unlink(zFilename);
3708 isDelete = 0;
3709 }
3710 pNew->isDelete = isDelete;
3711#endif
danielk1977e339d652008-06-28 11:23:00 +00003712 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003713 if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */
drhbfe66312006-10-03 17:40:40 +00003714 close(h);
danielk1977e339d652008-06-28 11:23:00 +00003715 }else{
danielk19776cb427f2008-06-30 10:16:04 +00003716 pNew->pMethod = &aIoMethod[eLockingStyle-1];
danielk1977e339d652008-06-28 11:23:00 +00003717 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00003718 }
danielk1977e339d652008-06-28 11:23:00 +00003719 return rc;
drh054889e2005-11-30 03:20:31 +00003720}
drh9c06c952005-11-26 00:25:00 +00003721
aswiftaebf4132008-11-21 00:10:35 +00003722#if SQLITE_ENABLE_LOCKING_STYLE
3723static sqlite3_io_methods *ioMethodForLockingStyle(int style){
3724 return &aIoMethod[style-1];
3725}
3726
3727static int getDbPathForUnixFile(unixFile *pFile, char *dbPath){
3728 if( pFile->pMethod==ioMethodForLockingStyle(LOCKING_STYLE_AFP) ){
3729 /* afp style keeps a reference to the db path in the filePath field of the struct */
3730 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
3731 return SQLITE_OK;
3732 }
3733 if( pFile->pMethod==ioMethodForLockingStyle(LOCKING_STYLE_DOTFILE) ){
3734 /* dot lock style uses the locking context to store the dot lock file path */
3735 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
3736 strlcpy(dbPath, (char *)pFile->lockingContext, len + 1);
3737 return SQLITE_OK;
3738 }
3739 /* all other styles use the locking context to store the db file path */
3740 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
3741 return SQLITE_OK;
3742}
3743#endif
3744
danielk1977ad94b582007-08-20 06:44:22 +00003745/*
3746** Open a file descriptor to the directory containing file zFilename.
3747** If successful, *pFd is set to the opened file descriptor and
3748** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
3749** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
3750** value.
3751**
3752** If SQLITE_OK is returned, the caller is responsible for closing
3753** the file descriptor *pFd using close().
3754*/
danielk1977fee2d252007-08-18 10:59:19 +00003755static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00003756 int ii;
drh777b17a2007-09-20 10:02:54 +00003757 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00003758 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00003759
drh153c62c2007-08-24 03:51:33 +00003760 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00003761 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
3762 if( ii>0 ){
3763 zDirname[ii] = '\0';
3764 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00003765 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003766#ifdef FD_CLOEXEC
3767 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3768#endif
3769 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
3770 }
3771 }
danielk1977fee2d252007-08-18 10:59:19 +00003772 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00003773 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00003774}
3775
danielk1977b4b47412007-08-17 15:53:36 +00003776/*
danielk197717b90b52008-06-06 11:11:25 +00003777** Create a temporary file name in zBuf. zBuf must be allocated
3778** by the calling process and must be big enough to hold at least
3779** pVfs->mxPathname bytes.
3780*/
3781static int getTempname(int nBuf, char *zBuf){
3782 static const char *azDirs[] = {
3783 0,
aswiftaebf4132008-11-21 00:10:35 +00003784 0,
danielk197717b90b52008-06-06 11:11:25 +00003785 "/var/tmp",
3786 "/usr/tmp",
3787 "/tmp",
3788 ".",
3789 };
3790 static const unsigned char zChars[] =
3791 "abcdefghijklmnopqrstuvwxyz"
3792 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3793 "0123456789";
3794 int i, j;
3795 struct stat buf;
3796 const char *zDir = ".";
3797
3798 /* It's odd to simulate an io-error here, but really this is just
3799 ** using the io-error infrastructure to test that SQLite handles this
3800 ** function failing.
3801 */
3802 SimulateIOError( return SQLITE_IOERR );
3803
3804 azDirs[0] = sqlite3_temp_directory;
aswiftaebf4132008-11-21 00:10:35 +00003805 if (NULL == azDirs[1]) {
3806 azDirs[1] = getenv("TMPDIR");
3807 }
3808
3809 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
danielk197717b90b52008-06-06 11:11:25 +00003810 if( azDirs[i]==0 ) continue;
3811 if( stat(azDirs[i], &buf) ) continue;
3812 if( !S_ISDIR(buf.st_mode) ) continue;
3813 if( access(azDirs[i], 07) ) continue;
3814 zDir = azDirs[i];
3815 break;
3816 }
3817
3818 /* Check that the output buffer is large enough for the temporary file
3819 ** name. If it is not, return SQLITE_ERROR.
3820 */
danielk197700e13612008-11-17 19:18:54 +00003821 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00003822 return SQLITE_ERROR;
3823 }
3824
3825 do{
3826 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
3827 j = strlen(zBuf);
3828 sqlite3_randomness(15, &zBuf[j]);
3829 for(i=0; i<15; i++, j++){
3830 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3831 }
3832 zBuf[j] = 0;
3833 }while( access(zBuf,0)==0 );
3834 return SQLITE_OK;
3835}
3836
3837
3838/*
danielk1977ad94b582007-08-20 06:44:22 +00003839** Open the file zPath.
3840**
danielk1977b4b47412007-08-17 15:53:36 +00003841** Previously, the SQLite OS layer used three functions in place of this
3842** one:
3843**
3844** sqlite3OsOpenReadWrite();
3845** sqlite3OsOpenReadOnly();
3846** sqlite3OsOpenExclusive();
3847**
3848** These calls correspond to the following combinations of flags:
3849**
3850** ReadWrite() -> (READWRITE | CREATE)
3851** ReadOnly() -> (READONLY)
3852** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
3853**
3854** The old OpenExclusive() accepted a boolean argument - "delFlag". If
3855** true, the file was configured to be automatically deleted when the
3856** file handle closed. To achieve the same effect using this new
3857** interface, add the DELETEONCLOSE flag to those specified above for
3858** OpenExclusive().
3859*/
3860static int unixOpen(
drh153c62c2007-08-24 03:51:33 +00003861 sqlite3_vfs *pVfs,
danielk1977b4b47412007-08-17 15:53:36 +00003862 const char *zPath,
3863 sqlite3_file *pFile,
3864 int flags,
3865 int *pOutFlags
3866){
danielk1977fee2d252007-08-18 10:59:19 +00003867 int fd = 0; /* File descriptor returned by open() */
3868 int dirfd = -1; /* Directory file descriptor */
3869 int oflags = 0; /* Flags to pass to open() */
3870 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00003871 int noLock; /* True to omit locking primitives */
aswiftaebf4132008-11-21 00:10:35 +00003872 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003873
3874 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3875 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3876 int isCreate = (flags & SQLITE_OPEN_CREATE);
3877 int isReadonly = (flags & SQLITE_OPEN_READONLY);
3878 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3879
danielk1977fee2d252007-08-18 10:59:19 +00003880 /* If creating a master or main-file journal, this function will open
3881 ** a file-descriptor on the directory too. The first time unixSync()
3882 ** is called the directory file descriptor will be fsync()ed and close()d.
3883 */
3884 int isOpenDirectory = (isCreate &&
3885 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
3886 );
3887
danielk197717b90b52008-06-06 11:11:25 +00003888 /* If argument zPath is a NULL pointer, this function is required to open
3889 ** a temporary file. Use this buffer to store the file name in.
3890 */
3891 char zTmpname[MAX_PATHNAME+1];
3892 const char *zName = zPath;
3893
danielk1977fee2d252007-08-18 10:59:19 +00003894 /* Check the following statements are true:
3895 **
3896 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3897 ** (b) if CREATE is set, then READWRITE must also be set, and
3898 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00003899 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00003900 */
danielk1977b4b47412007-08-17 15:53:36 +00003901 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00003902 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00003903 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00003904 assert(isDelete==0 || isCreate);
3905
drh33f4e022007-09-03 15:19:34 +00003906 /* The main DB, main journal, and master journal are never automatically
3907 ** deleted
3908 */
3909 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
3910 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
3911 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00003912
danielk1977fee2d252007-08-18 10:59:19 +00003913 /* Assert that the upper layer has set one of the "file-type" flags. */
3914 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
3915 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
3916 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00003917 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00003918 );
3919
danielk1977e339d652008-06-28 11:23:00 +00003920 memset(pFile, 0, sizeof(unixFile));
3921
danielk197717b90b52008-06-06 11:11:25 +00003922 if( !zName ){
danielk197717b90b52008-06-06 11:11:25 +00003923 assert(isDelete && !isOpenDirectory);
3924 rc = getTempname(MAX_PATHNAME+1, zTmpname);
3925 if( rc!=SQLITE_OK ){
3926 return rc;
3927 }
3928 zName = zTmpname;
3929 }
3930
danielk1977b4b47412007-08-17 15:53:36 +00003931 if( isReadonly ) oflags |= O_RDONLY;
3932 if( isReadWrite ) oflags |= O_RDWR;
3933 if( isCreate ) oflags |= O_CREAT;
3934 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
3935 oflags |= (O_LARGEFILE|O_BINARY);
3936
danielk197717b90b52008-06-06 11:11:25 +00003937 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
chw97185482008-11-17 08:05:31 +00003938 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, oflags);
danielk19772f2d8c72007-08-30 16:13:33 +00003939 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00003940 /* Failed to open the file for read/write access. Try read-only. */
3941 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
3942 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00003943 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00003944 }
3945 if( fd<0 ){
3946 return SQLITE_CANTOPEN;
3947 }
3948 if( isDelete ){
danielk1977c70dfc42008-11-19 13:52:30 +00003949#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003950 zPath = zName;
3951#else
danielk197717b90b52008-06-06 11:11:25 +00003952 unlink(zName);
chw97185482008-11-17 08:05:31 +00003953#endif
aswiftaebf4132008-11-21 00:10:35 +00003954 }else{
3955 ((unixFile *)pFile)->oflags = oflags;
danielk1977b4b47412007-08-17 15:53:36 +00003956 }
3957 if( pOutFlags ){
3958 *pOutFlags = flags;
3959 }
3960
3961 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00003962 if( isOpenDirectory ){
aswiftaebf4132008-11-21 00:10:35 +00003963 rc = openDirectory(zPath, &dirfd);
danielk1977fee2d252007-08-18 10:59:19 +00003964 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003965 close(fd); /* silently leak if fail, already in error */
danielk1977fee2d252007-08-18 10:59:19 +00003966 return rc;
3967 }
3968 }
danielk1977e339d652008-06-28 11:23:00 +00003969
3970#ifdef FD_CLOEXEC
3971 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3972#endif
3973
drhda0e7682008-07-30 15:27:54 +00003974 noLock = eType!=SQLITE_OPEN_MAIN_DB;
aswiftaebf4132008-11-21 00:10:35 +00003975
3976#if SQLITE_PREFER_PROXY_LOCKING
3977 if( zPath!=NULL && !noLock ){
3978 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
3979 int useProxy = 0;
3980
3981 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy,
3982 ** 0 means never use proxy, NULL means use proxy for non-local files only
3983 */
3984 if( envforce!=NULL ){
3985 useProxy = atoi(envforce)>0;
3986 }else{
3987 struct statfs fsInfo;
3988
3989 if( statfs(zPath, &fsInfo) == -1 ){
3990 ((unixFile*)pFile)->lastErrno = errno;
3991 if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
3992 close(fd); /* silently leak if fail, in error */
3993 return SQLITE_IOERR_ACCESS;
3994 }
3995 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
3996 }
3997 if( useProxy ){
3998 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
3999 if( rc==SQLITE_OK ){
4000 rc = transformUnixFileForLockProxy((unixFile*)pFile, ":auto:");
4001 }
4002 return rc;
4003 }
4004 }
4005#endif
4006
chw97185482008-11-17 08:05:31 +00004007 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00004008}
4009
4010/*
danielk1977fee2d252007-08-18 10:59:19 +00004011** Delete the file at zPath. If the dirSync argument is true, fsync()
4012** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00004013*/
danielk1977397d65f2008-11-19 11:35:39 +00004014static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){
danielk1977fee2d252007-08-18 10:59:19 +00004015 int rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00004016 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00004017 SimulateIOError(return SQLITE_IOERR_DELETE);
4018 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00004019#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00004020 if( dirSync ){
4021 int fd;
4022 rc = openDirectory(zPath, &fd);
4023 if( rc==SQLITE_OK ){
danielk1977c70dfc42008-11-19 13:52:30 +00004024#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004025 if( fsync(fd)==-1 )
4026#else
4027 if( fsync(fd) )
4028#endif
4029 {
danielk1977fee2d252007-08-18 10:59:19 +00004030 rc = SQLITE_IOERR_DIR_FSYNC;
4031 }
aswiftaebf4132008-11-21 00:10:35 +00004032 if( close(fd)&&!rc ){
4033 rc = SQLITE_IOERR_DIR_CLOSE;
4034 }
danielk1977fee2d252007-08-18 10:59:19 +00004035 }
4036 }
danielk1977d138dd82008-10-15 16:02:48 +00004037#endif
danielk1977fee2d252007-08-18 10:59:19 +00004038 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00004039}
4040
danielk197790949c22007-08-17 16:50:38 +00004041/*
4042** Test the existance of or access permissions of file zPath. The
4043** test performed depends on the value of flags:
4044**
4045** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
4046** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
4047** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
4048**
4049** Otherwise return 0.
4050*/
danielk1977861f7452008-06-05 11:39:11 +00004051static int unixAccess(
danielk1977397d65f2008-11-19 11:35:39 +00004052 sqlite3_vfs *NotUsed,
danielk1977861f7452008-06-05 11:39:11 +00004053 const char *zPath,
4054 int flags,
4055 int *pResOut
4056){
rse25c0d1a2007-09-20 08:38:14 +00004057 int amode = 0;
danielk1977397d65f2008-11-19 11:35:39 +00004058 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00004059 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00004060 switch( flags ){
4061 case SQLITE_ACCESS_EXISTS:
4062 amode = F_OK;
4063 break;
4064 case SQLITE_ACCESS_READWRITE:
4065 amode = W_OK|R_OK;
4066 break;
drh50d3f902007-08-27 21:10:36 +00004067 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00004068 amode = R_OK;
4069 break;
4070
4071 default:
4072 assert(!"Invalid flags argument");
4073 }
danielk1977861f7452008-06-05 11:39:11 +00004074 *pResOut = (access(zPath, amode)==0);
4075 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00004076}
4077
danielk1977b4b47412007-08-17 15:53:36 +00004078
4079/*
4080** Turn a relative pathname into a full pathname. The relative path
4081** is stored as a nul-terminated string in the buffer pointed to by
4082** zPath.
4083**
4084** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
4085** (in this case, MAX_PATHNAME bytes). The full-path is written to
4086** this buffer before returning.
4087*/
danielk1977adfb9b02007-09-17 07:02:56 +00004088static int unixFullPathname(
4089 sqlite3_vfs *pVfs, /* Pointer to vfs object */
4090 const char *zPath, /* Possibly relative input path */
4091 int nOut, /* Size of output buffer in bytes */
4092 char *zOut /* Output buffer */
4093){
danielk1977843e65f2007-09-01 16:16:15 +00004094
4095 /* It's odd to simulate an io-error here, but really this is just
4096 ** using the io-error infrastructure to test that SQLite handles this
4097 ** function failing. This function could fail if, for example, the
4098 ** current working directly has been unlinked.
4099 */
4100 SimulateIOError( return SQLITE_ERROR );
4101
drh153c62c2007-08-24 03:51:33 +00004102 assert( pVfs->mxPathname==MAX_PATHNAME );
danielk1977f3d3c272008-11-19 16:52:44 +00004103 UNUSED_PARAMETER(pVfs);
chw97185482008-11-17 08:05:31 +00004104
danielk1977c70dfc42008-11-19 13:52:30 +00004105#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004106 {
4107 char *zRealname = vxrealpath(zPath, 0);
4108 zOut[0] = '\0';
4109 if( !zRealname ){
4110 return SQLITE_CANTOPEN;
4111 }
4112 sqlite3_snprintf(nOut, zOut, "%s", zRealname);
4113 sqlite3_free(zRealname);
4114 return SQLITE_OK;
4115 }
4116#else
drh3c7f2dc2007-12-06 13:26:20 +00004117 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00004118 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00004119 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00004120 }else{
4121 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00004122 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00004123 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00004124 }
4125 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00004126 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00004127 }
4128 return SQLITE_OK;
4129
4130#if 0
4131 /*
4132 ** Remove "/./" path elements and convert "/A/./" path elements
4133 ** to just "/".
4134 */
4135 if( zFull ){
4136 int i, j;
4137 for(i=j=0; zFull[i]; i++){
4138 if( zFull[i]=='/' ){
4139 if( zFull[i+1]=='/' ) continue;
4140 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
4141 i += 1;
4142 continue;
4143 }
4144 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
4145 while( j>0 && zFull[j-1]!='/' ){ j--; }
4146 i += 3;
4147 continue;
4148 }
4149 }
4150 zFull[j++] = zFull[i];
4151 }
4152 zFull[j] = 0;
4153 }
4154#endif
aswiftaebf4132008-11-21 00:10:35 +00004155#endif /* #if IS_VXWORKS */
danielk1977b4b47412007-08-17 15:53:36 +00004156}
4157
drh0ccebe72005-06-07 22:22:50 +00004158
drh761df872006-12-21 01:29:22 +00004159#ifndef SQLITE_OMIT_LOAD_EXTENSION
4160/*
4161** Interfaces for opening a shared library, finding entry points
4162** within the shared library, and closing the shared library.
4163*/
4164#include <dlfcn.h>
danielk1977397d65f2008-11-19 11:35:39 +00004165static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
4166 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00004167 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
4168}
danielk197795c8a542007-09-01 06:51:27 +00004169
4170/*
4171** SQLite calls this function immediately after a call to unixDlSym() or
4172** unixDlOpen() fails (returns a null pointer). If a more detailed error
4173** message is available, it is written to zBufOut. If no error message
4174** is available, zBufOut is left unmodified and SQLite uses a default
4175** error message.
4176*/
danielk1977397d65f2008-11-19 11:35:39 +00004177static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00004178 char *zErr;
danielk1977397d65f2008-11-19 11:35:39 +00004179 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00004180 enterMutex();
4181 zErr = dlerror();
4182 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00004183 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00004184 }
4185 leaveMutex();
4186}
danielk1977397d65f2008-11-19 11:35:39 +00004187static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
4188 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00004189 return dlsym(pHandle, zSymbol);
4190}
danielk1977397d65f2008-11-19 11:35:39 +00004191static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
4192 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00004193 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00004194}
danielk1977b4b47412007-08-17 15:53:36 +00004195#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
4196 #define unixDlOpen 0
4197 #define unixDlError 0
4198 #define unixDlSym 0
4199 #define unixDlClose 0
4200#endif
4201
4202/*
danielk197790949c22007-08-17 16:50:38 +00004203** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00004204*/
danielk1977397d65f2008-11-19 11:35:39 +00004205static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
4206 UNUSED_PARAMETER(NotUsed);
danielk197700e13612008-11-17 19:18:54 +00004207 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00004208
drhbbd42a62004-05-22 17:41:58 +00004209 /* We have to initialize zBuf to prevent valgrind from reporting
4210 ** errors. The reports issued by valgrind are incorrect - we would
4211 ** prefer that the randomness be increased by making use of the
4212 ** uninitialized space in zBuf - but valgrind errors tend to worry
4213 ** some users. Rather than argue, it seems easier just to initialize
4214 ** the whole array and silence valgrind, even if that means less randomness
4215 ** in the random seed.
4216 **
4217 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00004218 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00004219 ** tests repeatable.
4220 */
danielk1977b4b47412007-08-17 15:53:36 +00004221 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00004222#if !defined(SQLITE_TEST)
4223 {
drh842b8642005-01-21 17:53:17 +00004224 int pid, fd;
4225 fd = open("/dev/urandom", O_RDONLY);
4226 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00004227 time_t t;
4228 time(&t);
danielk197790949c22007-08-17 16:50:38 +00004229 memcpy(zBuf, &t, sizeof(t));
4230 pid = getpid();
4231 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00004232 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00004233 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00004234 }else{
drh72cbd072008-10-14 17:58:38 +00004235 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00004236 close(fd);
4237 }
drhbbd42a62004-05-22 17:41:58 +00004238 }
4239#endif
drh72cbd072008-10-14 17:58:38 +00004240 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00004241}
4242
danielk1977b4b47412007-08-17 15:53:36 +00004243
drhbbd42a62004-05-22 17:41:58 +00004244/*
4245** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00004246** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00004247** The return value is the number of microseconds of sleep actually
4248** requested from the underlying operating system, a number which
4249** might be greater than or equal to the argument, but not less
4250** than the argument.
drhbbd42a62004-05-22 17:41:58 +00004251*/
danielk1977397d65f2008-11-19 11:35:39 +00004252static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
danielk1977c70dfc42008-11-19 13:52:30 +00004253#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004254 struct timespec sp;
4255
4256 sp.tv_sec = microseconds / 1000000;
4257 sp.tv_nsec = (microseconds % 1000000) * 1000;
4258 nanosleep(&sp, NULL);
danielk1977397d65f2008-11-19 11:35:39 +00004259 return microseconds;
4260#elif defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00004261 usleep(microseconds);
4262 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00004263#else
danielk1977b4b47412007-08-17 15:53:36 +00004264 int seconds = (microseconds+999999)/1000000;
4265 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00004266 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00004267#endif
danielk1977397d65f2008-11-19 11:35:39 +00004268 UNUSED_PARAMETER(NotUsed);
drh88f474a2006-01-02 20:00:12 +00004269}
4270
4271/*
drhbbd42a62004-05-22 17:41:58 +00004272** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00004273** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00004274*/
4275#ifdef SQLITE_TEST
4276int sqlite3_current_time = 0;
4277#endif
4278
4279/*
4280** Find the current time (in Universal Coordinated Time). Write the
4281** current time and date as a Julian Day number into *prNow and
4282** return 0. Return 1 if the time and date cannot be found.
4283*/
danielk1977397d65f2008-11-19 11:35:39 +00004284static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
danielk1977c70dfc42008-11-19 13:52:30 +00004285#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004286 struct timespec sNow;
4287 clock_gettime(CLOCK_REALTIME, &sNow);
4288 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
danielk1977397d65f2008-11-19 11:35:39 +00004289#elif defined(NO_GETTOD)
drhbbd42a62004-05-22 17:41:58 +00004290 time_t t;
4291 time(&t);
4292 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00004293#else
4294 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00004295 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00004296 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
4297#endif
danielk1977397d65f2008-11-19 11:35:39 +00004298
drhbbd42a62004-05-22 17:41:58 +00004299#ifdef SQLITE_TEST
4300 if( sqlite3_current_time ){
4301 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
4302 }
4303#endif
danielk1977397d65f2008-11-19 11:35:39 +00004304 UNUSED_PARAMETER(NotUsed);
drhbbd42a62004-05-22 17:41:58 +00004305 return 0;
4306}
danielk1977b4b47412007-08-17 15:53:36 +00004307
danielk1977397d65f2008-11-19 11:35:39 +00004308static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
4309 UNUSED_PARAMETER(NotUsed);
4310 UNUSED_PARAMETER(NotUsed2);
4311 UNUSED_PARAMETER(NotUsed3);
danielk1977bcb97fe2008-06-06 15:49:29 +00004312 return 0;
4313}
4314
drh153c62c2007-08-24 03:51:33 +00004315/*
danielk1977e339d652008-06-28 11:23:00 +00004316** Initialize the operating system interface.
drh153c62c2007-08-24 03:51:33 +00004317*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004318int sqlite3_os_init(void){
danielk1977e339d652008-06-28 11:23:00 +00004319 /* Macro to define the static contents of an sqlite3_vfs structure for
4320 ** the unix backend. The two parameters are the values to use for
4321 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
4322 **
4323 */
4324 #define UNIXVFS(zVfsName, pVfsAppData) { \
4325 1, /* iVersion */ \
4326 sizeof(unixFile), /* szOsFile */ \
4327 MAX_PATHNAME, /* mxPathname */ \
4328 0, /* pNext */ \
4329 zVfsName, /* zName */ \
4330 (void *)pVfsAppData, /* pAppData */ \
4331 unixOpen, /* xOpen */ \
4332 unixDelete, /* xDelete */ \
4333 unixAccess, /* xAccess */ \
4334 unixFullPathname, /* xFullPathname */ \
4335 unixDlOpen, /* xDlOpen */ \
4336 unixDlError, /* xDlError */ \
4337 unixDlSym, /* xDlSym */ \
4338 unixDlClose, /* xDlClose */ \
4339 unixRandomness, /* xRandomness */ \
4340 unixSleep, /* xSleep */ \
4341 unixCurrentTime, /* xCurrentTime */ \
4342 unixGetLastError /* xGetLastError */ \
4343 }
4344
4345 static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
drh40bbb0a2008-09-23 10:23:26 +00004346#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +00004347 int i;
4348 static sqlite3_vfs aVfs[] = {
4349 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
4350 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
4351 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
4352 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
chw97185482008-11-17 08:05:31 +00004353 UNIXVFS("unix-none", LOCKING_STYLE_NONE),
4354 UNIXVFS("unix-namedsem",LOCKING_STYLE_NAMEDSEM),
aswiftaebf4132008-11-21 00:10:35 +00004355 UNIXVFS("unix-proxy", LOCKING_STYLE_PROXY)
drh153c62c2007-08-24 03:51:33 +00004356 };
danielk1977e339d652008-06-28 11:23:00 +00004357 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
4358 sqlite3_vfs_register(&aVfs[i], 0);
4359 }
4360#endif
danielk1977c70dfc42008-11-19 13:52:30 +00004361#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004362 sqlite3HashInit(&nameHash, 1);
4363#endif
danielk1977c0fa4c52008-06-25 17:19:00 +00004364 sqlite3_vfs_register(&unixVfs, 1);
4365 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00004366}
danielk1977e339d652008-06-28 11:23:00 +00004367
4368/*
4369** Shutdown the operating system interface. This is a no-op for unix.
4370*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004371int sqlite3_os_end(void){
4372 return SQLITE_OK;
4373}
drhdce8bdb2007-08-16 13:01:44 +00004374
danielk197729bafea2008-06-26 10:41:19 +00004375#endif /* SQLITE_OS_UNIX */