blob: 3333f0a8b4e84c2c6d9c96004a5920876922b0e7 [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**
drh41022642008-11-21 00:24:42 +000015** $Id: os_unix.c,v 1.218 2008/11/21 00:24:42 drh 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
drh41022642008-11-21 00:24:42 +0000406#ifdef SQLITE_TEST
407/* simulate multiple hosts by creating unique hostid file paths */
408int sqlite3_hostid_num = 0;
409#endif
410
411
danielk1977c70dfc42008-11-19 13:52:30 +0000412#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000413/*
414** This hash table is used to bind the canonical file name to a
415** unixFile structure and use the hash key (= canonical name)
416** instead of the Inode number of the file to find the matching
417** lockInfo and openCnt structures. It also helps to make the
418** name of the semaphore when LOCKING_STYLE_NAMEDSEM is used
419** for the file.
420*/
421static Hash nameHash;
422#endif
423
drhbfe66312006-10-03 17:40:40 +0000424/*
425** The locking styles are associated with the different file locking
426** capabilities supported by different file systems.
427**
428** POSIX locking style fully supports shared and exclusive byte-range locks
danielk1977e339d652008-06-28 11:23:00 +0000429** AFP locking only supports exclusive byte-range locks
drhbfe66312006-10-03 17:40:40 +0000430** FLOCK only supports a single file-global exclusive lock
431** DOTLOCK isn't a true locking style, it refers to the use of a special
432** file named the same as the database file with a '.lock' extension, this
433** can be used on file systems that do not offer any reliable file locking
aswiftaebf4132008-11-21 00:10:35 +0000434** NONE locking means that no locking will be attempted, this is only used for
drhbfe66312006-10-03 17:40:40 +0000435** read-only file systems currently
chw97185482008-11-17 08:05:31 +0000436** NAMEDSEM is similar to DOTLOCK but uses a named semaphore instead of an
437** indicator file.
aswiftaebf4132008-11-21 00:10:35 +0000438** PROXY uses a second file to represent the lock state of the database file
439** which is never actually locked, a third file controls access to the proxy
drhbfe66312006-10-03 17:40:40 +0000440** UNSUPPORTED means that no locking will be attempted, this is only used for
441** file systems that are known to be unsupported
442*/
danielk1977e339d652008-06-28 11:23:00 +0000443#define LOCKING_STYLE_POSIX 1
drhda0e7682008-07-30 15:27:54 +0000444#define LOCKING_STYLE_NONE 2
danielk1977e339d652008-06-28 11:23:00 +0000445#define LOCKING_STYLE_DOTFILE 3
drhda0e7682008-07-30 15:27:54 +0000446#define LOCKING_STYLE_FLOCK 4
danielk1977e339d652008-06-28 11:23:00 +0000447#define LOCKING_STYLE_AFP 5
chw97185482008-11-17 08:05:31 +0000448#define LOCKING_STYLE_NAMEDSEM 6
aswiftaebf4132008-11-21 00:10:35 +0000449#define LOCKING_STYLE_PROXY 7
drhbfe66312006-10-03 17:40:40 +0000450
danielk1977ad94b582007-08-20 06:44:22 +0000451/*
aswift5b1a2562008-08-22 00:22:35 +0000452** Only set the lastErrno if the error code is a real error and not
453** a normal expected return code of SQLITE_BUSY or SQLITE_OK
454*/
455#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
456
457/*
danielk1977ad94b582007-08-20 06:44:22 +0000458** Helper functions to obtain and relinquish the global mutex.
459*/
danielk19770afae932008-09-24 09:12:46 +0000460static void enterMutex(void){
danielk197759f8c082008-06-18 17:09:10 +0000461 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000462}
danielk19770afae932008-09-24 09:12:46 +0000463static void leaveMutex(void){
danielk197759f8c082008-06-18 17:09:10 +0000464 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000465}
466
drhd677b3d2007-08-20 22:48:41 +0000467#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000468/*
469** This variable records whether or not threads can override each others
470** locks.
471**
472** 0: No. Threads cannot override each others locks.
473** 1: Yes. Threads can override each others locks.
474** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000475**
drh5062d3a2006-01-31 23:03:35 +0000476** On some systems, we know at compile-time if threads can override each
477** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
478** will be set appropriately. On other systems, we have to check at
479** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
480** undefined.
481**
drhf1a221e2006-01-15 17:27:17 +0000482** This variable normally has file scope only. But during testing, we make
483** it a global so that the test code can change its value in order to verify
484** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000485*/
drh5062d3a2006-01-31 23:03:35 +0000486#ifndef SQLITE_THREAD_OVERRIDE_LOCK
487# define SQLITE_THREAD_OVERRIDE_LOCK -1
488#endif
drh029b44b2006-01-15 00:13:15 +0000489#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000490int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000491#else
drh5062d3a2006-01-31 23:03:35 +0000492static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000493#endif
drh5fdae772004-06-29 03:29:00 +0000494
495/*
496** This structure holds information passed into individual test
497** threads by the testThreadLockingBehavior() routine.
498*/
499struct threadTestData {
500 int fd; /* File to be locked */
501 struct flock lock; /* The locking operation */
502 int result; /* Result of the locking operation */
503};
504
drh2b4b5962005-06-15 17:47:55 +0000505#ifdef SQLITE_LOCK_TRACE
506/*
507** Print out information about all locking operations.
508**
509** This routine is used for troubleshooting locks on multithreaded
510** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
511** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000512** turned off.
drh2b4b5962005-06-15 17:47:55 +0000513*/
514static int lockTrace(int fd, int op, struct flock *p){
515 char *zOpName, *zType;
516 int s;
517 int savedErrno;
518 if( op==F_GETLK ){
519 zOpName = "GETLK";
520 }else if( op==F_SETLK ){
521 zOpName = "SETLK";
522 }else{
523 s = fcntl(fd, op, p);
524 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
525 return s;
526 }
527 if( p->l_type==F_RDLCK ){
528 zType = "RDLCK";
529 }else if( p->l_type==F_WRLCK ){
530 zType = "WRLCK";
531 }else if( p->l_type==F_UNLCK ){
532 zType = "UNLCK";
533 }else{
534 assert( 0 );
535 }
536 assert( p->l_whence==SEEK_SET );
537 s = fcntl(fd, op, p);
538 savedErrno = errno;
539 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
540 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
541 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000542 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000543 struct flock l2;
544 l2 = *p;
545 fcntl(fd, F_GETLK, &l2);
546 if( l2.l_type==F_RDLCK ){
547 zType = "RDLCK";
548 }else if( l2.l_type==F_WRLCK ){
549 zType = "WRLCK";
550 }else if( l2.l_type==F_UNLCK ){
551 zType = "UNLCK";
552 }else{
553 assert( 0 );
554 }
555 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
556 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
557 }
558 errno = savedErrno;
559 return s;
560}
561#define fcntl lockTrace
562#endif /* SQLITE_LOCK_TRACE */
563
danielk197741a6a612008-11-11 18:34:35 +0000564#ifdef __linux__
drh5fdae772004-06-29 03:29:00 +0000565/*
danielk197741a6a612008-11-11 18:34:35 +0000566** This function is used as the main routine for a thread launched by
567** testThreadLockingBehavior(). It tests whether the shared-lock obtained
568** by the main thread in testThreadLockingBehavior() conflicts with a
569** hypothetical write-lock obtained by this thread on the same file.
570**
571** The write-lock is not actually acquired, as this is not possible if
572** the file is open in read-only mode (see ticket #3472).
573*/
drh5fdae772004-06-29 03:29:00 +0000574static void *threadLockingTest(void *pArg){
575 struct threadTestData *pData = (struct threadTestData*)pArg;
danielk197741a6a612008-11-11 18:34:35 +0000576 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
drh5fdae772004-06-29 03:29:00 +0000577 return pArg;
578}
579
580/*
581** This procedure attempts to determine whether or not threads
582** can override each others locks then sets the
583** threadsOverrideEachOthersLocks variable appropriately.
584*/
danielk19774d5238f2006-01-27 06:32:00 +0000585static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000586 int fd;
danielk197741a6a612008-11-11 18:34:35 +0000587 int rc;
588 struct threadTestData d;
589 struct flock l;
590 pthread_t t;
drh5fdae772004-06-29 03:29:00 +0000591
592 fd = dup(fd_orig);
593 if( fd<0 ) return;
danielk197741a6a612008-11-11 18:34:35 +0000594 memset(&l, 0, sizeof(l));
595 l.l_type = F_RDLCK;
596 l.l_len = 1;
597 l.l_start = 0;
598 l.l_whence = SEEK_SET;
599 rc = fcntl(fd_orig, F_SETLK, &l);
600 if( rc!=0 ) return;
601 memset(&d, 0, sizeof(d));
602 d.fd = fd;
603 d.lock = l;
604 d.lock.l_type = F_WRLCK;
605 pthread_create(&t, 0, threadLockingTest, &d);
606 pthread_join(t, 0);
drh5fdae772004-06-29 03:29:00 +0000607 close(fd);
danielk197741a6a612008-11-11 18:34:35 +0000608 if( d.result!=0 ) return;
609 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
drh5fdae772004-06-29 03:29:00 +0000610}
danielk197741a6a612008-11-11 18:34:35 +0000611#else
612/*
613** On anything other than linux, assume threads override each others locks.
614*/
615static void testThreadLockingBehavior(int fd_orig){
616 threadsOverrideEachOthersLocks = 1;
617}
618#endif /* __linux__ */
619
drhd677b3d2007-08-20 22:48:41 +0000620#endif /* SQLITE_THREADSAFE */
drh5fdae772004-06-29 03:29:00 +0000621
drhbbd42a62004-05-22 17:41:58 +0000622/*
623** Release a lockInfo structure previously allocated by findLockInfo().
624*/
625static void releaseLockInfo(struct lockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000626 if( pLock ){
627 pLock->nRef--;
628 if( pLock->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000629 if( pLock->pPrev ){
630 assert( pLock->pPrev->pNext==pLock );
631 pLock->pPrev->pNext = pLock->pNext;
632 }else{
633 assert( lockList==pLock );
634 lockList = pLock->pNext;
635 }
636 if( pLock->pNext ){
637 assert( pLock->pNext->pPrev==pLock );
638 pLock->pNext->pPrev = pLock->pPrev;
639 }
danielk1977e339d652008-06-28 11:23:00 +0000640 sqlite3_free(pLock);
641 }
drhbbd42a62004-05-22 17:41:58 +0000642 }
643}
644
645/*
646** Release a openCnt structure previously allocated by findLockInfo().
647*/
648static void releaseOpenCnt(struct openCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000649 if( pOpen ){
650 pOpen->nRef--;
651 if( pOpen->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000652 if( pOpen->pPrev ){
653 assert( pOpen->pPrev->pNext==pOpen );
654 pOpen->pPrev->pNext = pOpen->pNext;
655 }else{
656 assert( openList==pOpen );
657 openList = pOpen->pNext;
658 }
659 if( pOpen->pNext ){
660 assert( pOpen->pNext->pPrev==pOpen );
661 pOpen->pNext->pPrev = pOpen->pPrev;
662 }
663 sqlite3_free(pOpen->aPending);
danielk1977e339d652008-06-28 11:23:00 +0000664 sqlite3_free(pOpen);
665 }
drhbbd42a62004-05-22 17:41:58 +0000666 }
667}
668
danielk1977c70dfc42008-11-19 13:52:30 +0000669#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000670/*
671** Implementation of a realpath() like function for vxWorks
672** to determine canonical path name from given name. It does
673** not support symlinks. Neither does it handle volume prefixes.
674*/
675char *
676vxrealpath(const char *pathname, int dostat)
677{
678 struct stat sbuf;
679 int len;
680 char *where, *ptr, *last;
681 char *result, *curpath, *workpath, *namebuf;
682
683 len = pathconf(pathname, _PC_PATH_MAX);
684 if( len<0 ){
685 len = PATH_MAX;
686 }
687 result = sqlite3_malloc(len * 4);
688 if( !result ){
689 return 0;
690 }
691 curpath = result + len;
692 workpath = curpath + len;
693 namebuf = workpath + len;
694 strcpy(curpath, pathname);
695 if( *pathname!='/' ){
696 if( !getcwd(workpath, len) ){
697 sqlite3_free(result);
698 return 0;
699 }
700 }else{
701 *workpath = '\0';
702 }
703 where = curpath;
704 while( *where ){
705 if( !strcmp(where, ".") ){
706 where++;
707 continue;
708 }
709 if( !strncmp(where, "./", 2) ){
710 where += 2;
711 continue;
712 }
713 if( !strncmp(where, "../", 3) ){
714 where += 3;
715 ptr = last = workpath;
716 while( *ptr ){
717 if( *ptr=='/' ){
718 last = ptr;
719 }
720 ptr++;
721 }
722 *last = '\0';
723 continue;
724 }
725 ptr = strchr(where, '/');
726 if( !ptr ){
727 ptr = where + strlen(where) - 1;
728 }else{
729 *ptr = '\0';
730 }
731 strcpy(namebuf, workpath);
732 for( last = namebuf; *last; last++ ){
733 continue;
734 }
735 if( *--last!='/' ){
736 strcat(namebuf, "/");
737 }
738 strcat(namebuf, where);
739 where = ++ptr;
740 if( dostat ){
741 if( stat(namebuf, &sbuf)==-1 ){
742 sqlite3_free(result);
743 return 0;
744 }
745 if( (sbuf.st_mode & S_IFDIR)==S_IFDIR ){
746 strcpy(workpath, namebuf);
747 continue;
748 }
749 if( *where ){
750 sqlite3_free(result);
751 return 0;
752 }
753 }
754 strcpy(workpath, namebuf);
755 }
756 strcpy(result, workpath);
757 return result;
758}
759#endif
760
drh40bbb0a2008-09-23 10:23:26 +0000761#if SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +0000762/*
aswiftaebf4132008-11-21 00:10:35 +0000763** The proxyLockingContext has the path and file structures for the remote
764** and local proxy files in it
765*/
766typedef struct proxyLockingContext proxyLockingContext;
767struct proxyLockingContext {
768 unixFile *conchFile;
769 char *conchFilePath;
770 unixFile *lockProxy;
771 char *lockProxyPath;
772 char *dbPath;
773 int conchHeld;
774 void *oldLockingContext; /* preserve the original locking context for close */
775 sqlite3_io_methods const *pOldMethod; /* ditto pMethod */
776};
777
778static int getDbPathForUnixFile(unixFile *pFile, char *dbPath);
779static int getLockPath(const char *dbPath, char *lPath, size_t maxLen);
780static sqlite3_io_methods *ioMethodForLockingStyle(int style);
781static int createProxyUnixFile(const char *path, unixFile **ppFile);
782static int fillInUnixFile(sqlite3_vfs *pVfs, int h, int dirfd, sqlite3_file *pId, const char *zFilename, int noLock, int isDelete);
783static int takeConch(unixFile *pFile);
784static int releaseConch(unixFile *pFile);
785static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf);
786
787/*
drhbfe66312006-10-03 17:40:40 +0000788** Tests a byte-range locking query to see if byte range locks are
789** supported, if not we fall back to dotlockLockingStyle.
chw97185482008-11-17 08:05:31 +0000790** On vxWorks we fall back to namedsemLockingStyle.
drhbfe66312006-10-03 17:40:40 +0000791*/
danielk1977e339d652008-06-28 11:23:00 +0000792static int testLockingStyle(int fd){
drhbfe66312006-10-03 17:40:40 +0000793 struct flock lockInfo;
danielk1977e339d652008-06-28 11:23:00 +0000794
795 /* Test byte-range lock using fcntl(). If the call succeeds,
796 ** assume that the file-system supports POSIX style locks.
797 */
drhbfe66312006-10-03 17:40:40 +0000798 lockInfo.l_len = 1;
799 lockInfo.l_start = 0;
800 lockInfo.l_whence = SEEK_SET;
801 lockInfo.l_type = F_RDLCK;
danielk1977ad94b582007-08-20 06:44:22 +0000802 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
danielk1977e339d652008-06-28 11:23:00 +0000803 return LOCKING_STYLE_POSIX;
804 }
drhbfe66312006-10-03 17:40:40 +0000805
danielk1977e339d652008-06-28 11:23:00 +0000806 /* Testing for flock() can give false positives. So if if the above
danielk1977c70dfc42008-11-19 13:52:30 +0000807 ** test fails, then we fall back to using dot-file style locking (or
808 ** named-semaphore locking on vxworks).
chw97185482008-11-17 08:05:31 +0000809 */
danielk1977c70dfc42008-11-19 13:52:30 +0000810 return (IS_VXWORKS ? LOCKING_STYLE_NAMEDSEM : LOCKING_STYLE_DOTFILE);
drhbfe66312006-10-03 17:40:40 +0000811}
drh93a960a2008-07-10 00:32:42 +0000812#endif
drhbfe66312006-10-03 17:40:40 +0000813
814/*
danielk1977e339d652008-06-28 11:23:00 +0000815** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
816** f_fstypename entry in the statfs structure as returned by stat() for
817** the file system hosting the database file and selects the appropriate
818** locking style based on its value. These values and assignments are
819** based on Darwin/OSX behavior and have not been thoroughly tested on
drhbfe66312006-10-03 17:40:40 +0000820** other systems.
danielk1977e339d652008-06-28 11:23:00 +0000821**
822** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
823** returns LOCKING_STYLE_POSIX.
drhbfe66312006-10-03 17:40:40 +0000824*/
danielk197762c14b32008-11-19 09:05:26 +0000825#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +0000826static int detectLockingStyle(
827 sqlite3_vfs *pVfs,
danielk1977ad94b582007-08-20 06:44:22 +0000828 const char *filePath,
829 int fd
830){
danielk1977c70dfc42008-11-19 13:52:30 +0000831#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000832 if( !filePath ){
833 return LOCKING_STYLE_NONE;
834 }
835 if( pVfs->pAppData ){
836 return SQLITE_PTR_TO_INT(pVfs->pAppData);
837 }
838 if (access(filePath, 0) != -1){
839 return testLockingStyle(fd);
840 }
841#else
danielk1977e339d652008-06-28 11:23:00 +0000842 struct Mapping {
843 const char *zFilesystem;
844 int eLockingStyle;
845 } aMap[] = {
846 { "hfs", LOCKING_STYLE_POSIX },
847 { "ufs", LOCKING_STYLE_POSIX },
848 { "afpfs", LOCKING_STYLE_AFP },
aswift5b1a2562008-08-22 00:22:35 +0000849#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
850 { "smbfs", LOCKING_STYLE_AFP },
851#else
danielk1977e339d652008-06-28 11:23:00 +0000852 { "smbfs", LOCKING_STYLE_FLOCK },
aswift5b1a2562008-08-22 00:22:35 +0000853#endif
danielk1977e339d652008-06-28 11:23:00 +0000854 { "webdav", LOCKING_STYLE_NONE },
855 { 0, 0 }
856 };
857 int i;
drhbfe66312006-10-03 17:40:40 +0000858 struct statfs fsInfo;
859
danielk1977e339d652008-06-28 11:23:00 +0000860 if( !filePath ){
861 return LOCKING_STYLE_NONE;
drh339eb0b2008-03-07 15:34:11 +0000862 }
aswiftaebf4132008-11-21 00:10:35 +0000863 if( pVfs && pVfs->pAppData ){
aswiftf54b1b32008-08-22 18:41:37 +0000864 return SQLITE_PTR_TO_INT(pVfs->pAppData);
drh339eb0b2008-03-07 15:34:11 +0000865 }
drhbfe66312006-10-03 17:40:40 +0000866
danielk1977e339d652008-06-28 11:23:00 +0000867 if( statfs(filePath, &fsInfo) != -1 ){
868 if( fsInfo.f_flags & MNT_RDONLY ){
869 return LOCKING_STYLE_NONE;
870 }
871 for(i=0; aMap[i].zFilesystem; i++){
872 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
873 return aMap[i].eLockingStyle;
874 }
875 }
876 }
877
878 /* Default case. Handles, amongst others, "nfs". */
879 return testLockingStyle(fd);
danielk1977c70dfc42008-11-19 13:52:30 +0000880#endif /* if IS_VXWORKS */
danielk1977e339d652008-06-28 11:23:00 +0000881 return LOCKING_STYLE_POSIX;
882}
danielk197762c14b32008-11-19 09:05:26 +0000883#else
884 #define detectLockingStyle(x,y,z) LOCKING_STYLE_POSIX
aswiftaebf4132008-11-21 00:10:35 +0000885#endif /* if SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000886
drhbbd42a62004-05-22 17:41:58 +0000887/*
888** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000889** describes that file descriptor. Create new ones if necessary. The
890** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000891**
drh65594042008-05-05 16:56:34 +0000892** Return an appropriate error code.
drhbbd42a62004-05-22 17:41:58 +0000893*/
drh38f82712004-06-18 17:10:16 +0000894static int findLockInfo(
aswiftaebf4132008-11-21 00:10:35 +0000895 unixFile *pFile, /* Unix file with file desc used in the key */
danielk1977c70dfc42008-11-19 13:52:30 +0000896#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000897 void *rnam, /* vxWorks realname */
898#endif
drhbbd42a62004-05-22 17:41:58 +0000899 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000900 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000901){
902 int rc;
aswiftaebf4132008-11-21 00:10:35 +0000903 int fd;
drhbbd42a62004-05-22 17:41:58 +0000904 struct lockKey key1;
905 struct openKey key2;
906 struct stat statbuf;
907 struct lockInfo *pLock;
908 struct openCnt *pOpen;
aswiftaebf4132008-11-21 00:10:35 +0000909 fd = pFile->h;
drhbbd42a62004-05-22 17:41:58 +0000910 rc = fstat(fd, &statbuf);
drh65594042008-05-05 16:56:34 +0000911 if( rc!=0 ){
aswiftaebf4132008-11-21 00:10:35 +0000912 pFile->lastErrno = errno;
drh65594042008-05-05 16:56:34 +0000913#ifdef EOVERFLOW
aswiftaebf4132008-11-21 00:10:35 +0000914 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
drh65594042008-05-05 16:56:34 +0000915#endif
916 return SQLITE_IOERR;
917 }
danielk1977441b09a2006-01-05 13:48:29 +0000918
drh54626242008-07-30 17:28:04 +0000919 /* On OS X on an msdos filesystem, the inode number is reported
920 ** incorrectly for zero-size files. See ticket #3260. To work
921 ** around this problem (we consider it a bug in OS X, not SQLite)
922 ** we always increase the file size to 1 by writing a single byte
923 ** prior to accessing the inode number. The one byte written is
924 ** an ASCII 'S' character which also happens to be the first byte
925 ** in the header of every SQLite database. In this way, if there
926 ** is a race condition such that another thread has already populated
927 ** the first page of the database, no damage is done.
928 */
929 if( statbuf.st_size==0 ){
930 write(fd, "S", 1);
931 rc = fstat(fd, &statbuf);
932 if( rc!=0 ){
aswiftaebf4132008-11-21 00:10:35 +0000933 pFile->lastErrno = errno;
drh54626242008-07-30 17:28:04 +0000934 return SQLITE_IOERR;
935 }
936 }
937
drhbbd42a62004-05-22 17:41:58 +0000938 memset(&key1, 0, sizeof(key1));
939 key1.dev = statbuf.st_dev;
danielk1977c70dfc42008-11-19 13:52:30 +0000940#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000941 key1.rnam = rnam;
942#else
drhbbd42a62004-05-22 17:41:58 +0000943 key1.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000944#endif
drhd677b3d2007-08-20 22:48:41 +0000945#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000946 if( threadsOverrideEachOthersLocks<0 ){
947 testThreadLockingBehavior(fd);
948 }
949 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
950#endif
drhbbd42a62004-05-22 17:41:58 +0000951 memset(&key2, 0, sizeof(key2));
952 key2.dev = statbuf.st_dev;
danielk1977c70dfc42008-11-19 13:52:30 +0000953#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +0000954 key2.rnam = rnam;
955#else
drhbbd42a62004-05-22 17:41:58 +0000956 key2.ino = statbuf.st_ino;
chw97185482008-11-17 08:05:31 +0000957#endif
aswiftaebf4132008-11-21 00:10:35 +0000958 if( ppLock!=0 ){
959 pLock = lockList;
960 while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
961 pLock = pLock->pNext;
danielk1977441b09a2006-01-05 13:48:29 +0000962 }
aswiftaebf4132008-11-21 00:10:35 +0000963 if( pLock==0 ){
964 pLock = sqlite3_malloc( sizeof(*pLock) );
965 if( pLock==0 ){
966 rc = SQLITE_NOMEM;
967 goto exit_findlockinfo;
968 }
969 pLock->key = key1;
970 pLock->nRef = 1;
971 pLock->cnt = 0;
972 pLock->locktype = 0;
973 pLock->pNext = lockList;
974 pLock->pPrev = 0;
975 if( lockList ) lockList->pPrev = pLock;
976 lockList = pLock;
977 }else{
978 pLock->nRef++;
979 }
980 *ppLock = pLock;
drhbbd42a62004-05-22 17:41:58 +0000981 }
drh029b44b2006-01-15 00:13:15 +0000982 if( ppOpen!=0 ){
drhda0e7682008-07-30 15:27:54 +0000983 pOpen = openList;
984 while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){
985 pOpen = pOpen->pNext;
986 }
drhbbd42a62004-05-22 17:41:58 +0000987 if( pOpen==0 ){
drh17435752007-08-16 04:30:38 +0000988 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000989 if( pOpen==0 ){
990 releaseLockInfo(pLock);
drh65594042008-05-05 16:56:34 +0000991 rc = SQLITE_NOMEM;
drh029b44b2006-01-15 00:13:15 +0000992 goto exit_findlockinfo;
993 }
994 pOpen->key = key2;
995 pOpen->nRef = 1;
996 pOpen->nLock = 0;
997 pOpen->nPending = 0;
998 pOpen->aPending = 0;
drhda0e7682008-07-30 15:27:54 +0000999 pOpen->pNext = openList;
1000 pOpen->pPrev = 0;
1001 if( openList ) openList->pPrev = pOpen;
1002 openList = pOpen;
danielk1977c70dfc42008-11-19 13:52:30 +00001003#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00001004 pOpen->pSem = NULL;
1005 pOpen->aSemName[0] = '\0';
1006#endif
drh029b44b2006-01-15 00:13:15 +00001007 }else{
1008 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +00001009 }
drh029b44b2006-01-15 00:13:15 +00001010 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +00001011 }
danielk1977441b09a2006-01-05 13:48:29 +00001012
1013exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +00001014 return rc;
drhbbd42a62004-05-22 17:41:58 +00001015}
1016
drh64b1bea2006-01-15 02:30:57 +00001017#ifdef SQLITE_DEBUG
1018/*
1019** Helper function for printing out trace information from debugging
1020** binaries. This returns the string represetation of the supplied
1021** integer lock-type.
1022*/
1023static const char *locktypeName(int locktype){
1024 switch( locktype ){
1025 case NO_LOCK: return "NONE";
1026 case SHARED_LOCK: return "SHARED";
1027 case RESERVED_LOCK: return "RESERVED";
1028 case PENDING_LOCK: return "PENDING";
1029 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
1030 }
1031 return "ERROR";
1032}
1033#endif
1034
drhbbd42a62004-05-22 17:41:58 +00001035/*
drh029b44b2006-01-15 00:13:15 +00001036** If we are currently in a different thread than the thread that the
1037** unixFile argument belongs to, then transfer ownership of the unixFile
1038** over to the current thread.
1039**
1040** A unixFile is only owned by a thread on systems where one thread is
1041** unable to override locks created by a different thread. RedHat9 is
1042** an example of such a system.
1043**
1044** Ownership transfer is only allowed if the unixFile is currently unlocked.
1045** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +00001046** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +00001047*/
drhd677b3d2007-08-20 22:48:41 +00001048#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +00001049static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +00001050 int rc;
drh029b44b2006-01-15 00:13:15 +00001051 pthread_t hSelf;
1052 if( threadsOverrideEachOthersLocks ){
1053 /* Ownership transfers not needed on this system */
1054 return SQLITE_OK;
1055 }
1056 hSelf = pthread_self();
1057 if( pthread_equal(pFile->tid, hSelf) ){
1058 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +00001059 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +00001060 return SQLITE_OK;
1061 }
1062 if( pFile->locktype!=NO_LOCK ){
1063 /* We cannot change ownership while we are holding a lock! */
1064 return SQLITE_MISUSE;
1065 }
drh4f0c5872007-03-26 22:05:01 +00001066 OSTRACE4("Transfer ownership of %d from %d to %d\n",
1067 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +00001068 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +00001069 if (pFile->pLock != NULL) {
1070 releaseLockInfo(pFile->pLock);
danielk1977c70dfc42008-11-19 13:52:30 +00001071#if IS_VXWORKS
aswiftaebf4132008-11-21 00:10:35 +00001072 rc = findLockInfo(pFile, pFile->zRealpath, &pFile->pLock, 0);
chw97185482008-11-17 08:05:31 +00001073#else
aswiftaebf4132008-11-21 00:10:35 +00001074 rc = findLockInfo(pFile, &pFile->pLock, 0);
chw97185482008-11-17 08:05:31 +00001075#endif
drh4f0c5872007-03-26 22:05:01 +00001076 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001077 locktypeName(pFile->locktype),
1078 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
1079 return rc;
1080 } else {
1081 return SQLITE_OK;
1082 }
drh029b44b2006-01-15 00:13:15 +00001083}
1084#else
drhf1a221e2006-01-15 17:27:17 +00001085 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +00001086# define transferOwnership(X) SQLITE_OK
1087#endif
1088
1089/*
danielk19772a6bdf62007-08-20 16:07:00 +00001090** Seek to the offset passed as the second argument, then read cnt
1091** bytes into pBuf. Return the number of bytes actually read.
drh9e0ebbf2007-10-23 15:59:18 +00001092**
1093** NB: If you define USE_PREAD or USE_PREAD64, then it might also
1094** be necessary to define _XOPEN_SOURCE to be 500. This varies from
1095** one system to another. Since SQLite does not define USE_PREAD
1096** any any form by default, we will not attempt to define _XOPEN_SOURCE.
1097** See tickets #2741 and #2681.
aswiftaebf4132008-11-21 00:10:35 +00001098**
1099** To avoid stomping the errno value on a failed read the lastErrno value
1100** is set before returning.
drhb912b282006-03-23 22:42:20 +00001101*/
danielk197762079062007-08-15 17:08:46 +00001102static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001103 int got;
drh8ebf6702007-02-06 11:11:08 +00001104 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001105 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001106#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001107 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001108 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +00001109#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001110 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +00001111 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +00001112#else
danielk197762079062007-08-15 17:08:46 +00001113 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +00001114 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +00001115 if( newOffset!=offset ){
drh41022642008-11-21 00:24:42 +00001116 if( newOffset == -1 ){
aswiftaebf4132008-11-21 00:10:35 +00001117 ((unixFile*)id)->lastErrno = errno;
1118 }else{
1119 ((unixFile*)id)->lastErrno = 0;
1120 }
drh8ebf6702007-02-06 11:11:08 +00001121 return -1;
1122 }
drhb912b282006-03-23 22:42:20 +00001123 got = read(id->h, pBuf, cnt);
1124#endif
drh15d00c42007-02-27 02:01:14 +00001125 TIMER_END;
aswiftaebf4132008-11-21 00:10:35 +00001126 if( got<0 ){
1127 ((unixFile*)id)->lastErrno = errno;
1128 }
shane9bcbdad2008-05-29 20:22:37 +00001129 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001130 return got;
1131}
1132
1133/*
drhbbd42a62004-05-22 17:41:58 +00001134** Read data from a file into a buffer. Return SQLITE_OK if all
1135** bytes were read successfully and SQLITE_IOERR if anything goes
1136** wrong.
1137*/
danielk197762079062007-08-15 17:08:46 +00001138static int unixRead(
1139 sqlite3_file *id,
1140 void *pBuf,
1141 int amt,
1142 sqlite3_int64 offset
1143){
drhbbd42a62004-05-22 17:41:58 +00001144 int got;
drh9cbe6352005-11-29 03:13:21 +00001145 assert( id );
danielk197762079062007-08-15 17:08:46 +00001146 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +00001147 if( got==amt ){
1148 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +00001149 }else if( got<0 ){
aswiftaebf4132008-11-21 00:10:35 +00001150 /* lastErrno set by seekAndRead */
drh4ac285a2006-09-15 07:28:50 +00001151 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +00001152 }else{
aswiftaebf4132008-11-21 00:10:35 +00001153 ((unixFile*)id)->lastErrno = 0; /* not a system error */
drh4c17c3f2008-11-07 00:06:18 +00001154 /* Unread parts of the buffer must be zero-filled */
drhbafda092007-01-03 23:36:22 +00001155 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +00001156 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +00001157 }
1158}
1159
1160/*
drhb912b282006-03-23 22:42:20 +00001161** Seek to the offset in id->offset then read cnt bytes into pBuf.
1162** Return the number of bytes actually read. Update the offset.
aswiftaebf4132008-11-21 00:10:35 +00001163**
1164** To avoid stomping the errno value on a failed write the lastErrno value
1165** is set before returning.
drhb912b282006-03-23 22:42:20 +00001166*/
danielk197762079062007-08-15 17:08:46 +00001167static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +00001168 int got;
drh8ebf6702007-02-06 11:11:08 +00001169 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +00001170 TIMER_START;
drh8350a212007-03-22 15:22:06 +00001171#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +00001172 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +00001173#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +00001174 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +00001175#else
danielk197762079062007-08-15 17:08:46 +00001176 newOffset = lseek(id->h, offset, SEEK_SET);
1177 if( newOffset!=offset ){
drh41022642008-11-21 00:24:42 +00001178 if( newOffset == -1 ){
aswiftaebf4132008-11-21 00:10:35 +00001179 ((unixFile*)id)->lastErrno = errno;
1180 }else{
1181 ((unixFile*)id)->lastErrno = 0;
1182 }
drh8ebf6702007-02-06 11:11:08 +00001183 return -1;
1184 }
drhb912b282006-03-23 22:42:20 +00001185 got = write(id->h, pBuf, cnt);
1186#endif
drh15d00c42007-02-27 02:01:14 +00001187 TIMER_END;
aswiftaebf4132008-11-21 00:10:35 +00001188 if( got<0 ){
1189 ((unixFile*)id)->lastErrno = errno;
1190 }
1191
shane9bcbdad2008-05-29 20:22:37 +00001192 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +00001193 return got;
1194}
1195
1196
1197/*
drhbbd42a62004-05-22 17:41:58 +00001198** Write data from a buffer into a file. Return SQLITE_OK on success
1199** or some other error code on failure.
1200*/
danielk197762079062007-08-15 17:08:46 +00001201static int unixWrite(
1202 sqlite3_file *id,
1203 const void *pBuf,
1204 int amt,
1205 sqlite3_int64 offset
1206){
drhbbd42a62004-05-22 17:41:58 +00001207 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +00001208 assert( id );
drh4c7f9412005-02-03 00:29:47 +00001209 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +00001210 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +00001211 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +00001212 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +00001213 pBuf = &((char*)pBuf)[wrote];
1214 }
drh59685932006-09-14 13:47:11 +00001215 SimulateIOError(( wrote=(-1), amt=1 ));
1216 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +00001217 if( amt>0 ){
drh59685932006-09-14 13:47:11 +00001218 if( wrote<0 ){
aswiftaebf4132008-11-21 00:10:35 +00001219 /* lastErrno set by seekAndWrite */
drh4ac285a2006-09-15 07:28:50 +00001220 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +00001221 }else{
aswiftaebf4132008-11-21 00:10:35 +00001222 ((unixFile*)id)->lastErrno = 0; /* not a system error */
drh59685932006-09-14 13:47:11 +00001223 return SQLITE_FULL;
1224 }
drhbbd42a62004-05-22 17:41:58 +00001225 }
1226 return SQLITE_OK;
1227}
1228
drhb851b2c2005-03-10 14:11:12 +00001229#ifdef SQLITE_TEST
1230/*
1231** Count the number of fullsyncs and normal syncs. This is used to test
1232** that syncs and fullsyncs are occuring at the right times.
1233*/
1234int sqlite3_sync_count = 0;
1235int sqlite3_fullsync_count = 0;
1236#endif
1237
drhf2f23912005-10-05 10:29:36 +00001238/*
1239** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
1240** Otherwise use fsync() in its place.
1241*/
1242#ifndef HAVE_FDATASYNC
1243# define fdatasync fsync
1244#endif
1245
drhac530b12006-02-11 01:25:50 +00001246/*
1247** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
1248** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
1249** only available on Mac OS X. But that could change.
1250*/
1251#ifdef F_FULLFSYNC
1252# define HAVE_FULLFSYNC 1
1253#else
1254# define HAVE_FULLFSYNC 0
1255#endif
1256
drhb851b2c2005-03-10 14:11:12 +00001257
drhbbd42a62004-05-22 17:41:58 +00001258/*
drhdd809b02004-07-17 21:44:57 +00001259** The fsync() system call does not work as advertised on many
1260** unix systems. The following procedure is an attempt to make
1261** it work better.
drh1398ad32005-01-19 23:24:50 +00001262**
1263** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
1264** for testing when we want to run through the test suite quickly.
1265** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
1266** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
1267** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +00001268*/
drheb796a72005-09-08 12:38:41 +00001269static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +00001270 int rc;
drhb851b2c2005-03-10 14:11:12 +00001271
danielk1977397d65f2008-11-19 11:35:39 +00001272 /* The following "ifdef/elif/else/" block has the same structure as
1273 ** the one below. It is replicated here solely to avoid cluttering
1274 ** up the real code with the UNUSED_PARAMETER() macros.
1275 */
1276#ifdef SQLITE_NO_SYNC
1277 UNUSED_PARAMETER(fd);
1278 UNUSED_PARAMETER(fullSync);
1279 UNUSED_PARAMETER(dataOnly);
1280#elif HAVE_FULLFSYNC
danielk1977397d65f2008-11-19 11:35:39 +00001281 UNUSED_PARAMETER(dataOnly);
danielk1977f3d3c272008-11-19 16:52:44 +00001282#else
1283 UNUSED_PARAMETER(fullSync);
danielk1977397d65f2008-11-19 11:35:39 +00001284#endif
1285
drhb851b2c2005-03-10 14:11:12 +00001286 /* Record the number of times that we do a normal fsync() and
1287 ** FULLSYNC. This is used during testing to verify that this procedure
1288 ** gets called with the correct arguments.
1289 */
1290#ifdef SQLITE_TEST
1291 if( fullSync ) sqlite3_fullsync_count++;
1292 sqlite3_sync_count++;
1293#endif
1294
1295 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
1296 ** no-op
1297 */
1298#ifdef SQLITE_NO_SYNC
1299 rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00001300#elif HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +00001301 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +00001302 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +00001303 }else{
1304 rc = 1;
1305 }
1306 /* If the FULLFSYNC failed, fall back to attempting an fsync().
1307 * It shouldn't be possible for fullfsync to fail on the local
1308 * file system (on OSX), so failure indicates that FULLFSYNC
1309 * isn't supported for this file system. So, attempt an fsync
1310 * and (for now) ignore the overhead of a superfluous fcntl call.
1311 * It'd be better to detect fullfsync support once and avoid
1312 * the fcntl call every time sync is called.
1313 */
1314 if( rc ) rc = fsync(fd);
1315
1316#else
drheb796a72005-09-08 12:38:41 +00001317 if( dataOnly ){
1318 rc = fdatasync(fd);
danielk1977397d65f2008-11-19 11:35:39 +00001319 if( IS_VXWORKS && rc==-1 && errno==ENOTSUP ){
chw97185482008-11-17 08:05:31 +00001320 rc = fsync(fd);
1321 }
drhf2f23912005-10-05 10:29:36 +00001322 }else{
drheb796a72005-09-08 12:38:41 +00001323 rc = fsync(fd);
1324 }
danielk1977397d65f2008-11-19 11:35:39 +00001325#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +00001326
danielk1977397d65f2008-11-19 11:35:39 +00001327 if( IS_VXWORKS && rc!= -1 ){
chw97185482008-11-17 08:05:31 +00001328 rc = 0;
1329 }
drhdd809b02004-07-17 21:44:57 +00001330 return rc;
1331}
1332
1333/*
drhbbd42a62004-05-22 17:41:58 +00001334** Make sure all writes to a particular file are committed to disk.
1335**
drheb796a72005-09-08 12:38:41 +00001336** If dataOnly==0 then both the file itself and its metadata (file
1337** size, access time, etc) are synced. If dataOnly!=0 then only the
1338** file data is synced.
1339**
drhbbd42a62004-05-22 17:41:58 +00001340** Under Unix, also make sure that the directory entry for the file
1341** has been created by fsync-ing the directory that contains the file.
1342** If we do not do this and we encounter a power failure, the directory
1343** entry for the journal might not exist after we reboot. The next
1344** SQLite to access the file will not know that the journal exists (because
1345** the directory entry for the journal was never created) and the transaction
1346** will not roll back - possibly leading to database corruption.
1347*/
danielk197790949c22007-08-17 16:50:38 +00001348static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +00001349 int rc;
drh054889e2005-11-30 03:20:31 +00001350 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +00001351
danielk1977f036aef2007-08-20 05:36:51 +00001352 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
1353 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
1354
danielk1977c16d4632007-08-30 14:49:58 +00001355 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
danielk1977f036aef2007-08-20 05:36:51 +00001356 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
1357 || (flags&0x0F)==SQLITE_SYNC_FULL
danielk1977f036aef2007-08-20 05:36:51 +00001358 );
danielk197790949c22007-08-17 16:50:38 +00001359
danielk1977cd3b3c82008-09-22 11:46:32 +00001360 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
1361 ** line is to test that doing so does not cause any problems.
1362 */
1363 SimulateDiskfullError( return SQLITE_FULL );
1364
drh054889e2005-11-30 03:20:31 +00001365 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001366 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001367 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001368 SimulateIOError( rc=1 );
1369 if( rc ){
aswiftaebf4132008-11-21 00:10:35 +00001370 pFile->lastErrno = errno;
drh4ac285a2006-09-15 07:28:50 +00001371 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001372 }
drh054889e2005-11-30 03:20:31 +00001373 if( pFile->dirfd>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001374 int err;
drh4f0c5872007-03-26 22:05:01 +00001375 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001376 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001377#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001378 /* The directory sync is only attempted if full_fsync is
1379 ** turned off or unavailable. If a full_fsync occurred above,
1380 ** then the directory sync is superfluous.
1381 */
danielk197790949c22007-08-17 16:50:38 +00001382 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001383 /*
1384 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001385 ** errors when applied to directories on certain file systems.
1386 ** A failed directory sync is not a big deal. So it seems
1387 ** better to ignore the error. Ticket #1657
1388 */
aswiftaebf4132008-11-21 00:10:35 +00001389 /* pFile->lastErrno = errno; */
drh86631a52006-02-09 23:05:51 +00001390 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001391 }
danielk1977d7c03f72005-11-25 10:38:22 +00001392#endif
aswiftaebf4132008-11-21 00:10:35 +00001393 err = close(pFile->dirfd); /* Only need to sync once, so close the */
1394 if( err==0 ){ /* directory when we are done */
1395 pFile->dirfd = -1;
1396 }else{
1397 pFile->lastErrno = errno;
1398 rc = SQLITE_IOERR_DIR_CLOSE;
1399 }
drha2854222004-06-17 19:04:17 +00001400 }
aswiftaebf4132008-11-21 00:10:35 +00001401 return rc;
drhbbd42a62004-05-22 17:41:58 +00001402}
1403
1404/*
1405** Truncate an open file to a specified size
1406*/
danielk197762079062007-08-15 17:08:46 +00001407static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001408 int rc;
drh9cbe6352005-11-29 03:13:21 +00001409 assert( id );
drh93aed5a2008-01-16 17:46:38 +00001410 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
drh63fff5f2007-06-19 10:50:38 +00001411 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001412 if( rc ){
aswiftaebf4132008-11-21 00:10:35 +00001413 ((unixFile*)id)->lastErrno = errno;
drh4ac285a2006-09-15 07:28:50 +00001414 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001415 }else{
1416 return SQLITE_OK;
1417 }
drhbbd42a62004-05-22 17:41:58 +00001418}
1419
1420/*
1421** Determine the current size of a file in bytes
1422*/
danielk197762079062007-08-15 17:08:46 +00001423static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001424 int rc;
drhbbd42a62004-05-22 17:41:58 +00001425 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001426 assert( id );
drh59685932006-09-14 13:47:11 +00001427 rc = fstat(((unixFile*)id)->h, &buf);
1428 SimulateIOError( rc=1 );
1429 if( rc!=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001430 ((unixFile*)id)->lastErrno = errno;
drh4ac285a2006-09-15 07:28:50 +00001431 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001432 }
1433 *pSize = buf.st_size;
drh54626242008-07-30 17:28:04 +00001434
1435 /* When opening a zero-size database, the findLockInfo() procedure
1436 ** writes a single byte into that file in order to work around a bug
1437 ** in the OS-X msdos filesystem. In order to avoid problems with upper
1438 ** layers, we need to report this file size as zero even though it is
1439 ** really 1. Ticket #3260.
1440 */
1441 if( *pSize==1 ) *pSize = 0;
1442
1443
drhbbd42a62004-05-22 17:41:58 +00001444 return SQLITE_OK;
1445}
1446
danielk19779a1d0ab2004-06-01 14:09:28 +00001447/*
aswift5b1a2562008-08-22 00:22:35 +00001448** This routine translates a standard POSIX errno code into something
1449** useful to the clients of the sqlite3 functions. Specifically, it is
1450** intended to translate a variety of "try again" errors into SQLITE_BUSY
1451** and a variety of "please close the file descriptor NOW" errors into
1452** SQLITE_IOERR
1453**
1454** Errors during initialization of locks, or file system support for locks,
1455** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
1456*/
1457static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
1458 switch (posixError) {
1459 case 0:
1460 return SQLITE_OK;
1461
1462 case EAGAIN:
1463 case ETIMEDOUT:
1464 case EBUSY:
1465 case EINTR:
1466 case ENOLCK:
1467 /* random NFS retry error, unless during file system support
1468 * introspection, in which it actually means what it says */
1469 return SQLITE_BUSY;
1470
1471 case EACCES:
1472 /* EACCES is like EAGAIN during locking operations, but not any other time*/
1473 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
1474 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
1475 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
1476 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
1477 return SQLITE_BUSY;
1478 }
1479 /* else fall through */
1480 case EPERM:
1481 return SQLITE_PERM;
1482
1483 case EDEADLK:
1484 return SQLITE_IOERR_BLOCKED;
1485
drhf489c452008-08-22 00:47:53 +00001486#if EOPNOTSUPP!=ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001487 case EOPNOTSUPP:
1488 /* something went terribly awry, unless during file system support
1489 * introspection, in which it actually means what it says */
drhf489c452008-08-22 00:47:53 +00001490#endif
danielk19775ad6a882008-09-15 04:20:31 +00001491#ifdef ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001492 case ENOTSUP:
1493 /* invalid fd, unless during file system support introspection, in which
1494 * it actually means what it says */
danielk19775ad6a882008-09-15 04:20:31 +00001495#endif
aswift5b1a2562008-08-22 00:22:35 +00001496 case EIO:
1497 case EBADF:
1498 case EINVAL:
1499 case ENOTCONN:
1500 case ENODEV:
1501 case ENXIO:
1502 case ENOENT:
1503 case ESTALE:
1504 case ENOSYS:
1505 /* these should force the client to close the file and reconnect */
1506
1507 default:
1508 return sqliteIOErr;
1509 }
1510}
1511
1512/*
danielk197713adf8a2004-06-03 16:08:41 +00001513** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001514** file by this or any other process. If such a lock is held, set *pResOut
1515** to a non-zero value otherwise *pResOut is set to zero. The return value
1516** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001517*/
danielk1977861f7452008-06-05 11:39:11 +00001518static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001519 int rc = SQLITE_OK;
1520 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001521 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001522
danielk1977861f7452008-06-05 11:39:11 +00001523 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1524
drh054889e2005-11-30 03:20:31 +00001525 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001526 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001527
1528 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001529 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001530 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001531 }
1532
drh2ac3ee92004-06-07 16:27:46 +00001533 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001534 */
aswift5b1a2562008-08-22 00:22:35 +00001535 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001536 struct flock lock;
1537 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001538 lock.l_start = RESERVED_BYTE;
1539 lock.l_len = 1;
1540 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001541 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1542 int tErrno = errno;
1543 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1544 pFile->lastErrno = tErrno;
1545 } else if( lock.l_type!=F_UNLCK ){
1546 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001547 }
1548 }
1549
danielk1977b4b47412007-08-17 15:53:36 +00001550 leaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001551 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001552
aswift5b1a2562008-08-22 00:22:35 +00001553 *pResOut = reserved;
1554 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001555}
1556
1557/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001558** Lock the file with the lock specified by parameter locktype - one
1559** of the following:
1560**
drh2ac3ee92004-06-07 16:27:46 +00001561** (1) SHARED_LOCK
1562** (2) RESERVED_LOCK
1563** (3) PENDING_LOCK
1564** (4) EXCLUSIVE_LOCK
1565**
drhb3e04342004-06-08 00:47:47 +00001566** Sometimes when requesting one lock state, additional lock states
1567** are inserted in between. The locking might fail on one of the later
1568** transitions leaving the lock state different from what it started but
1569** still short of its goal. The following chart shows the allowed
1570** transitions and the inserted intermediate states:
1571**
1572** UNLOCKED -> SHARED
1573** SHARED -> RESERVED
1574** SHARED -> (PENDING) -> EXCLUSIVE
1575** RESERVED -> (PENDING) -> EXCLUSIVE
1576** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001577**
drha6abd042004-06-09 17:37:22 +00001578** This routine will only increase a lock. Use the sqlite3OsUnlock()
1579** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001580*/
danielk197762079062007-08-15 17:08:46 +00001581static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001582 /* The following describes the implementation of the various locks and
1583 ** lock transitions in terms of the POSIX advisory shared and exclusive
1584 ** lock primitives (called read-locks and write-locks below, to avoid
1585 ** confusion with SQLite lock names). The algorithms are complicated
1586 ** slightly in order to be compatible with windows systems simultaneously
1587 ** accessing the same database file, in case that is ever required.
1588 **
1589 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1590 ** byte', each single bytes at well known offsets, and the 'shared byte
1591 ** range', a range of 510 bytes at a well known offset.
1592 **
1593 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1594 ** byte'. If this is successful, a random byte from the 'shared byte
1595 ** range' is read-locked and the lock on the 'pending byte' released.
1596 **
danielk197790ba3bd2004-06-25 08:32:25 +00001597 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1598 ** A RESERVED lock is implemented by grabbing a write-lock on the
1599 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001600 **
1601 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001602 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1603 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1604 ** obtained, but existing SHARED locks are allowed to persist. A process
1605 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1606 ** This property is used by the algorithm for rolling back a journal file
1607 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001608 **
danielk197790ba3bd2004-06-25 08:32:25 +00001609 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1610 ** implemented by obtaining a write-lock on the entire 'shared byte
1611 ** range'. Since all other locks require a read-lock on one of the bytes
1612 ** within this range, this ensures that no other locks are held on the
1613 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001614 **
1615 ** The reason a single byte cannot be used instead of the 'shared byte
1616 ** range' is that some versions of windows do not support read-locks. By
1617 ** locking a random byte from a range, concurrent SHARED locks may exist
1618 ** even if the locking primitive used is always a write-lock.
1619 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001620 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001621 unixFile *pFile = (unixFile*)id;
1622 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001623 struct flock lock;
1624 int s;
1625
drh054889e2005-11-30 03:20:31 +00001626 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001627 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001628 locktypeName(locktype), locktypeName(pFile->locktype),
1629 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001630
1631 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001632 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001633 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001634 */
drh054889e2005-11-30 03:20:31 +00001635 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001636 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001637 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001638 return SQLITE_OK;
1639 }
1640
drhb3e04342004-06-08 00:47:47 +00001641 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001642 */
drh054889e2005-11-30 03:20:31 +00001643 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001644 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001645 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001646
drh054889e2005-11-30 03:20:31 +00001647 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001648 */
danielk1977b4b47412007-08-17 15:53:36 +00001649 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001650
drh029b44b2006-01-15 00:13:15 +00001651 /* Make sure the current thread owns the pFile.
1652 */
1653 rc = transferOwnership(pFile);
1654 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001655 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001656 return rc;
1657 }
drh64b1bea2006-01-15 02:30:57 +00001658 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001659
danielk1977ad94b582007-08-20 06:44:22 +00001660 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001661 ** handle that precludes the requested lock, return BUSY.
1662 */
drh054889e2005-11-30 03:20:31 +00001663 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001664 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001665 ){
1666 rc = SQLITE_BUSY;
1667 goto end_lock;
1668 }
1669
1670 /* If a SHARED lock is requested, and some thread using this PID already
1671 ** has a SHARED or RESERVED lock, then increment reference counts and
1672 ** return SQLITE_OK.
1673 */
1674 if( locktype==SHARED_LOCK &&
1675 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1676 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001677 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001678 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001679 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001680 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001681 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001682 goto end_lock;
1683 }
1684
danielk197713adf8a2004-06-03 16:08:41 +00001685 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001686
danielk19779a1d0ab2004-06-01 14:09:28 +00001687 lock.l_whence = SEEK_SET;
1688
drh3cde3bb2004-06-12 02:17:14 +00001689 /* A PENDING lock is needed before acquiring a SHARED lock and before
1690 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1691 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001692 */
drh3cde3bb2004-06-12 02:17:14 +00001693 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001694 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001695 ){
danielk1977489468c2004-06-28 08:25:47 +00001696 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001697 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001698 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001699 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001700 int tErrno = errno;
1701 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1702 if( IS_LOCK_ERROR(rc) ){
1703 pFile->lastErrno = tErrno;
1704 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001705 goto end_lock;
1706 }
drh3cde3bb2004-06-12 02:17:14 +00001707 }
1708
1709
1710 /* If control gets to this point, then actually go ahead and make
1711 ** operating system calls for the specified lock.
1712 */
1713 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001714 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001715 assert( pLock->cnt==0 );
1716 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001717
drh2ac3ee92004-06-07 16:27:46 +00001718 /* Now get the read-lock */
1719 lock.l_start = SHARED_FIRST;
1720 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001721 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1722 tErrno = errno;
1723 }
drh2ac3ee92004-06-07 16:27:46 +00001724 /* Drop the temporary PENDING lock */
1725 lock.l_start = PENDING_BYTE;
1726 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001727 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001728 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001729 if( s != -1 ){
1730 /* This could happen with a network mount */
1731 tErrno = errno;
1732 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1733 if( IS_LOCK_ERROR(rc) ){
1734 pFile->lastErrno = tErrno;
1735 }
1736 goto end_lock;
1737 }
drh2b4b5962005-06-15 17:47:55 +00001738 }
drhe2396a12007-03-29 20:19:58 +00001739 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001740 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1741 if( IS_LOCK_ERROR(rc) ){
1742 pFile->lastErrno = tErrno;
1743 }
drhbbd42a62004-05-22 17:41:58 +00001744 }else{
drh054889e2005-11-30 03:20:31 +00001745 pFile->locktype = SHARED_LOCK;
1746 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001747 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001748 }
drh3cde3bb2004-06-12 02:17:14 +00001749 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1750 /* We are trying for an exclusive lock but another thread in this
1751 ** same process is still holding a shared lock. */
1752 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001753 }else{
drh3cde3bb2004-06-12 02:17:14 +00001754 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001755 ** assumed that there is a SHARED or greater lock on the file
1756 ** already.
1757 */
drh054889e2005-11-30 03:20:31 +00001758 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001759 lock.l_type = F_WRLCK;
1760 switch( locktype ){
1761 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001762 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001763 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001764 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001765 lock.l_start = SHARED_FIRST;
1766 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001767 break;
1768 default:
1769 assert(0);
1770 }
drh054889e2005-11-30 03:20:31 +00001771 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001772 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001773 int tErrno = errno;
1774 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1775 if( IS_LOCK_ERROR(rc) ){
1776 pFile->lastErrno = tErrno;
1777 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001778 }
drhbbd42a62004-05-22 17:41:58 +00001779 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001780
danielk1977ecb2a962004-06-02 06:30:16 +00001781 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001782 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001783 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001784 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001785 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001786 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001787 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001788
1789end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001790 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001791 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001792 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001793 return rc;
1794}
1795
1796/*
drh054889e2005-11-30 03:20:31 +00001797** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001798** must be either NO_LOCK or SHARED_LOCK.
1799**
1800** If the locking level of the file descriptor is already at or below
1801** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001802*/
danielk197762079062007-08-15 17:08:46 +00001803static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001804 struct lockInfo *pLock;
1805 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001806 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001807 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001808 int h;
drha6abd042004-06-09 17:37:22 +00001809
drh054889e2005-11-30 03:20:31 +00001810 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001811 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001812 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001813
1814 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001815 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001816 return SQLITE_OK;
1817 }
drhf1a221e2006-01-15 17:27:17 +00001818 if( CHECK_THREADID(pFile) ){
1819 return SQLITE_MISUSE;
1820 }
danielk1977b4b47412007-08-17 15:53:36 +00001821 enterMutex();
drh1aa5af12008-03-07 19:51:14 +00001822 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001823 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001824 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001825 if( pFile->locktype>SHARED_LOCK ){
1826 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001827 SimulateIOErrorBenign(1);
1828 SimulateIOError( h=(-1) )
1829 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001830 if( locktype==SHARED_LOCK ){
1831 lock.l_type = F_RDLCK;
1832 lock.l_whence = SEEK_SET;
1833 lock.l_start = SHARED_FIRST;
1834 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001835 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001836 int tErrno = errno;
1837 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1838 if( IS_LOCK_ERROR(rc) ){
1839 pFile->lastErrno = tErrno;
1840 }
1841 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001842 }
1843 }
drhbbd42a62004-05-22 17:41:58 +00001844 lock.l_type = F_UNLCK;
1845 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001846 lock.l_start = PENDING_BYTE;
1847 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001848 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001849 pLock->locktype = SHARED_LOCK;
1850 }else{
aswift5b1a2562008-08-22 00:22:35 +00001851 int tErrno = errno;
1852 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1853 if( IS_LOCK_ERROR(rc) ){
1854 pFile->lastErrno = tErrno;
1855 }
1856 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001857 }
drhbbd42a62004-05-22 17:41:58 +00001858 }
drha6abd042004-06-09 17:37:22 +00001859 if( locktype==NO_LOCK ){
1860 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001861
drha6abd042004-06-09 17:37:22 +00001862 /* Decrement the shared lock counter. Release the lock using an
1863 ** OS call only when all threads in this same process have released
1864 ** the lock.
1865 */
1866 pLock->cnt--;
1867 if( pLock->cnt==0 ){
1868 lock.l_type = F_UNLCK;
1869 lock.l_whence = SEEK_SET;
1870 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001871 SimulateIOErrorBenign(1);
1872 SimulateIOError( h=(-1) )
1873 SimulateIOErrorBenign(0);
1874 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001875 pLock->locktype = NO_LOCK;
1876 }else{
aswift5b1a2562008-08-22 00:22:35 +00001877 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001878 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001879 if( IS_LOCK_ERROR(rc) ){
1880 pFile->lastErrno = tErrno;
1881 }
drh1aa5af12008-03-07 19:51:14 +00001882 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001883 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001884 }
drha6abd042004-06-09 17:37:22 +00001885 }
1886
drhbbd42a62004-05-22 17:41:58 +00001887 /* Decrement the count of locks against this same file. When the
1888 ** count reaches zero, close any other file descriptors whose close
1889 ** was deferred because of outstanding locks.
1890 */
drh1aa5af12008-03-07 19:51:14 +00001891 if( rc==SQLITE_OK ){
1892 pOpen = pFile->pOpen;
1893 pOpen->nLock--;
1894 assert( pOpen->nLock>=0 );
1895 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1896 int i;
1897 for(i=0; i<pOpen->nPending; i++){
aswiftaebf4132008-11-21 00:10:35 +00001898 /* close pending fds, but if closing fails don't free the array
1899 ** assign -1 to the successfully closed descriptors and record the
1900 ** error. The next attempt to unlock will try again. */
1901 if( pOpen->aPending[i] < 0 ) continue;
1902 if( close(pOpen->aPending[i]) ){
1903 pFile->lastErrno = errno;
1904 rc = SQLITE_IOERR_CLOSE;
1905 }else{
1906 pOpen->aPending[i] = -1;
1907 }
drh1aa5af12008-03-07 19:51:14 +00001908 }
aswiftaebf4132008-11-21 00:10:35 +00001909 if( rc==SQLITE_OK ){
1910 sqlite3_free(pOpen->aPending);
1911 pOpen->nPending = 0;
1912 pOpen->aPending = 0;
1913 }
drhbbd42a62004-05-22 17:41:58 +00001914 }
drhbbd42a62004-05-22 17:41:58 +00001915 }
1916 }
aswift5b1a2562008-08-22 00:22:35 +00001917
1918end_unlock:
danielk1977b4b47412007-08-17 15:53:36 +00001919 leaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001920 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001921 return rc;
drhbbd42a62004-05-22 17:41:58 +00001922}
1923
1924/*
danielk1977e339d652008-06-28 11:23:00 +00001925** This function performs the parts of the "close file" operation
1926** common to all locking schemes. It closes the directory and file
1927** handles, if they are valid, and sets all fields of the unixFile
1928** structure to 0.
1929*/
1930static int closeUnixFile(sqlite3_file *id){
1931 unixFile *pFile = (unixFile*)id;
1932 if( pFile ){
1933 if( pFile->dirfd>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001934 int err = close(pFile->dirfd);
1935 if( err ){
1936 pFile->lastErrno = errno;
1937 return SQLITE_IOERR_DIR_CLOSE;
1938 }else{
1939 pFile->dirfd=-1;
1940 }
danielk1977e339d652008-06-28 11:23:00 +00001941 }
1942 if( pFile->h>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001943 int err = close(pFile->h);
1944 if( err ){
1945 pFile->lastErrno = errno;
1946 return SQLITE_IOERR_CLOSE;
1947 }
danielk1977e339d652008-06-28 11:23:00 +00001948 }
danielk1977c70dfc42008-11-19 13:52:30 +00001949#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00001950 if( pFile->isDelete && pFile->zRealpath ){
1951 unlink(pFile->zRealpath);
1952 }
1953 if( pFile->zRealpath ){
1954 HashElem *pElem;
1955 int n = strlen(pFile->zRealpath) + 1;
1956 pElem = sqlite3HashFindElem(&nameHash, pFile->zRealpath, n);
1957 if( pElem ){
1958 long cnt = (long)pElem->data;
1959 cnt--;
1960 if( cnt==0 ){
1961 sqlite3HashInsert(&nameHash, pFile->zRealpath, n, 0);
1962 }else{
1963 pElem->data = (void*)cnt;
1964 }
1965 }
1966 }
1967#endif
danielk1977e339d652008-06-28 11:23:00 +00001968 OSTRACE2("CLOSE %-3d\n", pFile->h);
1969 OpenCounter(-1);
1970 memset(pFile, 0, sizeof(unixFile));
1971 }
1972 return SQLITE_OK;
1973}
1974
1975/*
danielk1977e3026632004-06-22 11:29:02 +00001976** Close a file.
1977*/
danielk197762079062007-08-15 17:08:46 +00001978static int unixClose(sqlite3_file *id){
aswiftaebf4132008-11-21 00:10:35 +00001979 int rc = SQLITE_OK;
danielk1977e339d652008-06-28 11:23:00 +00001980 if( id ){
1981 unixFile *pFile = (unixFile *)id;
1982 unixUnlock(id, NO_LOCK);
1983 enterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001984 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001985 /* If there are outstanding locks, do not actually close the file just
1986 ** yet because that would clear those locks. Instead, add the file
1987 ** descriptor to pOpen->aPending. It will be automatically closed when
1988 ** the last lock is cleared.
1989 */
1990 int *aNew;
1991 struct openCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001992 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001993 if( aNew==0 ){
1994 /* If a malloc fails, just leak the file descriptor */
1995 }else{
1996 pOpen->aPending = aNew;
1997 pOpen->aPending[pOpen->nPending] = pFile->h;
1998 pOpen->nPending++;
1999 pFile->h = -1;
2000 }
danielk1977e3026632004-06-22 11:29:02 +00002001 }
danielk1977e339d652008-06-28 11:23:00 +00002002 releaseLockInfo(pFile->pLock);
2003 releaseOpenCnt(pFile->pOpen);
aswiftaebf4132008-11-21 00:10:35 +00002004 rc = closeUnixFile(id);
danielk1977e339d652008-06-28 11:23:00 +00002005 leaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00002006 }
aswiftaebf4132008-11-21 00:10:35 +00002007 return rc;
danielk1977e3026632004-06-22 11:29:02 +00002008}
2009
drhbfe66312006-10-03 17:40:40 +00002010
drh40bbb0a2008-09-23 10:23:26 +00002011#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977c70dfc42008-11-19 13:52:30 +00002012#if !IS_VXWORKS
aswiftaebf4132008-11-21 00:10:35 +00002013#pragma mark AFP support
drhbfe66312006-10-03 17:40:40 +00002014
2015/*
2016 ** The afpLockingContext structure contains all afp lock specific state
2017 */
2018typedef struct afpLockingContext afpLockingContext;
2019struct afpLockingContext {
aswiftaebf4132008-11-21 00:10:35 +00002020 unsigned long long sharedByte;
2021 const char *dbPath;
drhbfe66312006-10-03 17:40:40 +00002022};
2023
2024struct ByteRangeLockPB2
2025{
2026 unsigned long long offset; /* offset to first byte to lock */
2027 unsigned long long length; /* nbr of bytes to lock */
2028 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
2029 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
2030 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
2031 int fd; /* file desc to assoc this lock with */
2032};
2033
drhfd131da2007-08-07 17:13:03 +00002034#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00002035
danielk1977ad94b582007-08-20 06:44:22 +00002036/*
aswift5b1a2562008-08-22 00:22:35 +00002037 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
2038 */
danielk1977ad94b582007-08-20 06:44:22 +00002039static int _AFPFSSetLock(
2040 const char *path,
aswift5b1a2562008-08-22 00:22:35 +00002041 unixFile *pFile,
danielk1977ad94b582007-08-20 06:44:22 +00002042 unsigned long long offset,
2043 unsigned long long length,
2044 int setLockFlag
2045){
drhfd131da2007-08-07 17:13:03 +00002046 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00002047 int err;
2048
2049 pb.unLockFlag = setLockFlag ? 0 : 1;
2050 pb.startEndFlag = 0;
2051 pb.offset = offset;
2052 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00002053 pb.fd = pFile->h;
aswiftaebf4132008-11-21 00:10:35 +00002054 //SimulateIOErrorBenign(1);
2055 //SimulateIOError( pb.fd=(-1) )
2056 //SimulateIOErrorBenign(0);
2057
2058 OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
2059 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), offset, length);
drhbfe66312006-10-03 17:40:40 +00002060 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
2061 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00002062 int rc;
2063 int tErrno = errno;
aswiftaebf4132008-11-21 00:10:35 +00002064 OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno));
2065#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2066 rc = SQLITE_BUSY;
2067#else
2068 rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
2069#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002070 if( IS_LOCK_ERROR(rc) ){
2071 pFile->lastErrno = tErrno;
2072 }
2073 return rc;
drhbfe66312006-10-03 17:40:40 +00002074 } else {
aswift5b1a2562008-08-22 00:22:35 +00002075 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002076 }
2077}
2078
aswift5b1a2562008-08-22 00:22:35 +00002079/* AFP-style reserved lock checking following the behavior of
2080** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002081static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002082 int rc = SQLITE_OK;
2083 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002084 unixFile *pFile = (unixFile*)id;
2085
aswift5b1a2562008-08-22 00:22:35 +00002086 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2087
2088 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00002089 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2090
2091 /* Check if a thread in this process holds such a lock */
2092 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002093 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00002094 }
2095
2096 /* Otherwise see if some other process holds it.
2097 */
aswift5b1a2562008-08-22 00:22:35 +00002098 if( !reserved ){
2099 /* lock the RESERVED byte */
aswiftaebf4132008-11-21 00:10:35 +00002100 int lrc = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
aswift5b1a2562008-08-22 00:22:35 +00002101 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00002102 /* if we succeeded in taking the reserved lock, unlock it to restore
2103 ** the original state */
aswiftaebf4132008-11-21 00:10:35 +00002104 lrc = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswift5b1a2562008-08-22 00:22:35 +00002105 } else {
2106 /* if we failed to get the lock then someone else must have it */
2107 reserved = 1;
2108 }
2109 if( IS_LOCK_ERROR(lrc) ){
2110 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00002111 }
2112 }
drhbfe66312006-10-03 17:40:40 +00002113
aswift5b1a2562008-08-22 00:22:35 +00002114 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2115
2116 *pResOut = reserved;
2117 return rc;
drhbfe66312006-10-03 17:40:40 +00002118}
2119
2120/* AFP-style locking following the behavior of unixLock, see the unixLock
2121** function comments for details of lock management. */
danielk1977e339d652008-06-28 11:23:00 +00002122static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00002123 int rc = SQLITE_OK;
2124 unixFile *pFile = (unixFile*)id;
2125 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002126
2127 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002128 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002129 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2130
drhbfe66312006-10-03 17:40:40 +00002131 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002132 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2133 ** enterMutex() hasn't been called yet.
2134 */
drhbfe66312006-10-03 17:40:40 +00002135 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002136 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002137 locktypeName(locktype));
2138 return SQLITE_OK;
2139 }
2140
2141 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002142 */
drhbfe66312006-10-03 17:40:40 +00002143 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2144 assert( locktype!=PENDING_LOCK );
2145 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2146
2147 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002148 */
danielk1977b4b47412007-08-17 15:53:36 +00002149 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002150
2151 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002152 */
drhbfe66312006-10-03 17:40:40 +00002153 rc = transferOwnership(pFile);
2154 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00002155 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002156 return rc;
2157 }
2158
2159 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002160 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2161 ** be released.
2162 */
drhbfe66312006-10-03 17:40:40 +00002163 if( locktype==SHARED_LOCK
2164 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002165 ){
2166 int failed;
aswiftaebf4132008-11-21 00:10:35 +00002167 failed = _AFPFSSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002168 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002169 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002170 goto afp_end_lock;
2171 }
2172 }
2173
2174 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002175 ** operating system calls for the specified lock.
2176 */
drhbfe66312006-10-03 17:40:40 +00002177 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002178 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002179
aswift5b1a2562008-08-22 00:22:35 +00002180 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002181 /* note that the quality of the randomness doesn't matter that much */
2182 lk = random();
aswiftaebf4132008-11-21 00:10:35 +00002183 context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
2184 lrc1 = _AFPFSSetLock(context->dbPath, pFile,
2185 SHARED_FIRST+context->sharedByte, 1, 1);
aswift5b1a2562008-08-22 00:22:35 +00002186 if( IS_LOCK_ERROR(lrc1) ){
2187 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002188 }
aswift5b1a2562008-08-22 00:22:35 +00002189 /* Drop the temporary PENDING lock */
aswiftaebf4132008-11-21 00:10:35 +00002190 lrc2 = _AFPFSSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002191
aswift5b1a2562008-08-22 00:22:35 +00002192 if( IS_LOCK_ERROR(lrc1) ) {
2193 pFile->lastErrno = lrc1Errno;
2194 rc = lrc1;
2195 goto afp_end_lock;
2196 } else if( IS_LOCK_ERROR(lrc2) ){
2197 rc = lrc2;
2198 goto afp_end_lock;
2199 } else if( lrc1 != SQLITE_OK ) {
2200 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002201 } else {
2202 pFile->locktype = SHARED_LOCK;
aswiftaebf4132008-11-21 00:10:35 +00002203 pFile->pOpen->nLock++;
drhbfe66312006-10-03 17:40:40 +00002204 }
2205 }else{
2206 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2207 ** assumed that there is a SHARED or greater lock on the file
2208 ** already.
2209 */
2210 int failed = 0;
2211 assert( 0!=pFile->locktype );
2212 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2213 /* Acquire a RESERVED lock */
aswiftaebf4132008-11-21 00:10:35 +00002214 failed = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002215 }
2216 if (!failed && locktype == EXCLUSIVE_LOCK) {
2217 /* Acquire an EXCLUSIVE lock */
2218
2219 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002220 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002221 */
aswiftaebf4132008-11-21 00:10:35 +00002222 if( !(failed = _AFPFSSetLock(context->dbPath, pFile, SHARED_FIRST +
2223 context->sharedByte, 1, 0)) ){
2224 int failed2 = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002225 /* now attemmpt to get the exclusive lock range */
aswiftaebf4132008-11-21 00:10:35 +00002226 failed = _AFPFSSetLock(context->dbPath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002227 SHARED_SIZE, 1);
aswiftaebf4132008-11-21 00:10:35 +00002228 if( failed && (failed2 = _AFPFSSetLock(context->dbPath, pFile,
2229 SHARED_FIRST + context->sharedByte, 1, 1)) ){
2230 /* Can't reestablish the shared lock. Sqlite can't deal, this is
2231 ** a critical I/O error
2232 */
2233 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
2234 SQLITE_IOERR_LOCK;
2235 goto afp_end_lock;
2236 }
2237 }else{
aswift5b1a2562008-08-22 00:22:35 +00002238 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002239 }
2240 }
aswift5b1a2562008-08-22 00:22:35 +00002241 if( failed ){
2242 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002243 }
2244 }
2245
2246 if( rc==SQLITE_OK ){
2247 pFile->locktype = locktype;
2248 }else if( locktype==EXCLUSIVE_LOCK ){
2249 pFile->locktype = PENDING_LOCK;
2250 }
2251
2252afp_end_lock:
drh339eb0b2008-03-07 15:34:11 +00002253 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002254 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002255 rc==SQLITE_OK ? "ok" : "failed");
2256 return rc;
2257}
2258
2259/*
drh339eb0b2008-03-07 15:34:11 +00002260** Lower the locking level on file descriptor pFile to locktype. locktype
2261** must be either NO_LOCK or SHARED_LOCK.
2262**
2263** If the locking level of the file descriptor is already at or below
2264** the requested locking level, this routine is a no-op.
2265*/
danielk1977e339d652008-06-28 11:23:00 +00002266static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002267 int rc = SQLITE_OK;
2268 unixFile *pFile = (unixFile*)id;
aswiftaebf4132008-11-21 00:10:35 +00002269 afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002270
2271 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002272 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002273 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002274
drhbfe66312006-10-03 17:40:40 +00002275 assert( locktype<=SHARED_LOCK );
2276 if( pFile->locktype<=locktype ){
2277 return SQLITE_OK;
2278 }
2279 if( CHECK_THREADID(pFile) ){
2280 return SQLITE_MISUSE;
2281 }
danielk1977b4b47412007-08-17 15:53:36 +00002282 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002283 if( pFile->locktype>SHARED_LOCK ){
aswiftaebf4132008-11-21 00:10:35 +00002284
2285 if( pFile->locktype==EXCLUSIVE_LOCK ){
2286 rc = _AFPFSSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
2287 if( rc==SQLITE_OK && locktype==SHARED_LOCK ){
2288 /* only re-establish the shared lock if necessary */
2289 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
2290 rc = _AFPFSSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
2291 }
2292 }
2293 if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
2294 rc = _AFPFSSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
2295 }
2296 if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){
2297 rc = _AFPFSSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
2298 }
2299 }else if( locktype==NO_LOCK ){
2300 /* clear the shared lock */
2301 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
2302 rc = _AFPFSSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
2303 }
drhbfe66312006-10-03 17:40:40 +00002304
aswiftaebf4132008-11-21 00:10:35 +00002305 if( rc==SQLITE_OK ){
2306 if( locktype==NO_LOCK ){
2307 struct openCnt *pOpen = pFile->pOpen;
2308 pOpen->nLock--;
2309 assert( pOpen->nLock>=0 );
2310 if( pOpen->nLock==0 && pOpen->nPending>0 ){
2311 int i;
2312 for(i=0; i<pOpen->nPending; i++){
2313 if( pOpen->aPending[i] < 0 ) continue;
2314 if( close(pOpen->aPending[i]) ){
2315 pFile->lastErrno = errno;
2316 rc = SQLITE_IOERR_CLOSE;
2317 }else{
2318 pOpen->aPending[i] = -1;
drhbfe66312006-10-03 17:40:40 +00002319 }
aswiftaebf4132008-11-21 00:10:35 +00002320 }
2321 if( rc==SQLITE_OK ){
2322 sqlite3_free(pOpen->aPending);
2323 pOpen->nPending = 0;
2324 pOpen->aPending = 0;
2325 }
drhbfe66312006-10-03 17:40:40 +00002326 }
2327 }
drhbfe66312006-10-03 17:40:40 +00002328 }
aswiftaebf4132008-11-21 00:10:35 +00002329end_afpunlock:
danielk1977b4b47412007-08-17 15:53:36 +00002330 leaveMutex();
aswiftaebf4132008-11-21 00:10:35 +00002331 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002332 return rc;
2333}
2334
2335/*
drh339eb0b2008-03-07 15:34:11 +00002336** Close a file & cleanup AFP specific locking context
2337*/
danielk1977e339d652008-06-28 11:23:00 +00002338static int afpClose(sqlite3_file *id) {
2339 if( id ){
2340 unixFile *pFile = (unixFile*)id;
2341 afpUnlock(id, NO_LOCK);
aswiftaebf4132008-11-21 00:10:35 +00002342 enterMutex();
2343 if( pFile->pOpen && pFile->pOpen->nLock ){
2344 /* If there are outstanding locks, do not actually close the file just
2345 ** yet because that would clear those locks. Instead, add the file
2346 ** descriptor to pOpen->aPending. It will be automatically closed when
2347 ** the last lock is cleared.
2348 */
2349 int *aNew;
2350 struct openCnt *pOpen = pFile->pOpen;
2351 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
2352 if( aNew==0 ){
2353 /* If a malloc fails, just leak the file descriptor */
2354 }else{
2355 pOpen->aPending = aNew;
2356 pOpen->aPending[pOpen->nPending] = pFile->h;
2357 pOpen->nPending++;
2358 pFile->h = -1;
2359 }
2360 }
2361 releaseOpenCnt(pFile->pOpen);
danielk1977e339d652008-06-28 11:23:00 +00002362 sqlite3_free(pFile->lockingContext);
aswiftaebf4132008-11-21 00:10:35 +00002363 closeUnixFile(id);
2364 leaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00002365 }
aswiftaebf4132008-11-21 00:10:35 +00002366 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002367}
2368
2369
2370#pragma mark flock() style locking
2371
2372/*
drh339eb0b2008-03-07 15:34:11 +00002373** The flockLockingContext is not used
2374*/
drhbfe66312006-10-03 17:40:40 +00002375typedef void flockLockingContext;
2376
aswift5b1a2562008-08-22 00:22:35 +00002377/* flock-style reserved lock checking following the behavior of
2378 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002379static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002380 int rc = SQLITE_OK;
2381 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002382 unixFile *pFile = (unixFile*)id;
2383
aswift5b1a2562008-08-22 00:22:35 +00002384 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2385
2386 assert( pFile );
2387
2388 /* Check if a thread in this process holds such a lock */
2389 if( pFile->locktype>SHARED_LOCK ){
2390 reserved = 1;
2391 }
2392
2393 /* Otherwise see if some other process holds it. */
2394 if( !reserved ){
drh3b62b2f2007-06-08 18:27:03 +00002395 /* attempt to get the lock */
aswift5b1a2562008-08-22 00:22:35 +00002396 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
2397 if( !lrc ){
drh3b62b2f2007-06-08 18:27:03 +00002398 /* got the lock, unlock it */
aswift5b1a2562008-08-22 00:22:35 +00002399 lrc = flock(pFile->h, LOCK_UN);
2400 if ( lrc ) {
2401 int tErrno = errno;
2402 /* unlock failed with an error */
2403 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2404 if( IS_LOCK_ERROR(lrc) ){
2405 pFile->lastErrno = tErrno;
2406 rc = lrc;
2407 }
2408 }
2409 } else {
2410 int tErrno = errno;
2411 reserved = 1;
2412 /* someone else might have it reserved */
2413 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2414 if( IS_LOCK_ERROR(lrc) ){
2415 pFile->lastErrno = tErrno;
2416 rc = lrc;
2417 }
drhbfe66312006-10-03 17:40:40 +00002418 }
drhbfe66312006-10-03 17:40:40 +00002419 }
aswift5b1a2562008-08-22 00:22:35 +00002420 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002421
aswiftaebf4132008-11-21 00:10:35 +00002422#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2423 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
2424 rc = SQLITE_OK;
2425 reserved=1;
2426 }
2427#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002428 *pResOut = reserved;
2429 return rc;
drhbfe66312006-10-03 17:40:40 +00002430}
2431
danielk1977e339d652008-06-28 11:23:00 +00002432static int flockLock(sqlite3_file *id, int locktype) {
aswift5b1a2562008-08-22 00:22:35 +00002433 int rc = SQLITE_OK;
aswiftaebf4132008-11-21 00:10:35 +00002434 int lrc;
drhbfe66312006-10-03 17:40:40 +00002435 unixFile *pFile = (unixFile*)id;
aswift5b1a2562008-08-22 00:22:35 +00002436
2437 assert( pFile );
2438
drh3b62b2f2007-06-08 18:27:03 +00002439 /* if we already have a lock, it is exclusive.
2440 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002441 if (pFile->locktype > NO_LOCK) {
2442 pFile->locktype = locktype;
2443 return SQLITE_OK;
2444 }
2445
drh3b62b2f2007-06-08 18:27:03 +00002446 /* grab an exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002447
2448 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
2449 int tErrno = errno;
drh3b62b2f2007-06-08 18:27:03 +00002450 /* didn't get, must be busy */
aswift5b1a2562008-08-22 00:22:35 +00002451 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2452 if( IS_LOCK_ERROR(rc) ){
2453 pFile->lastErrno = tErrno;
2454 }
drhbfe66312006-10-03 17:40:40 +00002455 } else {
drh3b62b2f2007-06-08 18:27:03 +00002456 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002457 pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002458 }
aswift5b1a2562008-08-22 00:22:35 +00002459 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
2460 rc==SQLITE_OK ? "ok" : "failed");
aswiftaebf4132008-11-21 00:10:35 +00002461#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2462 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
2463 rc = SQLITE_BUSY;
2464 }
2465#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002466 return rc;
drhbfe66312006-10-03 17:40:40 +00002467}
2468
danielk1977e339d652008-06-28 11:23:00 +00002469static int flockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002470 unixFile *pFile = (unixFile*)id;
2471
aswift5b1a2562008-08-22 00:22:35 +00002472 assert( pFile );
2473 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2474 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002475 assert( locktype<=SHARED_LOCK );
2476
drh3b62b2f2007-06-08 18:27:03 +00002477 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002478 if( pFile->locktype==locktype ){
2479 return SQLITE_OK;
2480 }
2481
drh3b62b2f2007-06-08 18:27:03 +00002482 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002483 if (locktype==SHARED_LOCK) {
2484 pFile->locktype = locktype;
2485 return SQLITE_OK;
2486 }
2487
drh3b62b2f2007-06-08 18:27:03 +00002488 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00002489 int rc = flock(pFile->h, LOCK_UN);
aswift5b1a2562008-08-22 00:22:35 +00002490 if (rc) {
2491 int r, tErrno = errno;
2492 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2493 if( IS_LOCK_ERROR(r) ){
2494 pFile->lastErrno = tErrno;
2495 }
aswiftaebf4132008-11-21 00:10:35 +00002496#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2497 if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
2498 r = SQLITE_BUSY;
2499 }
2500#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2501
aswift5b1a2562008-08-22 00:22:35 +00002502 return r;
2503 } else {
drhbfe66312006-10-03 17:40:40 +00002504 pFile->locktype = NO_LOCK;
2505 return SQLITE_OK;
2506 }
2507}
2508
2509/*
drh339eb0b2008-03-07 15:34:11 +00002510** Close a file.
2511*/
danielk1977e339d652008-06-28 11:23:00 +00002512static int flockClose(sqlite3_file *id) {
2513 if( id ){
2514 flockUnlock(id, NO_LOCK);
2515 }
2516 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002517}
2518
danielk1977c70dfc42008-11-19 13:52:30 +00002519#endif /* !IS_VXWORKS */
chw97185482008-11-17 08:05:31 +00002520
drhbfe66312006-10-03 17:40:40 +00002521#pragma mark Old-School .lock file based locking
aswiftaebf4132008-11-21 00:10:35 +00002522#define DOTLOCK_SUFFIX ".lock"
drhbfe66312006-10-03 17:40:40 +00002523
aswift5b1a2562008-08-22 00:22:35 +00002524/* Dotlock-style reserved lock checking following the behavior of
2525** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002526static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
aswift5b1a2562008-08-22 00:22:35 +00002527 int rc = SQLITE_OK;
2528 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002529 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002530
aswift5b1a2562008-08-22 00:22:35 +00002531 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2532
2533 assert( pFile );
2534
2535 /* Check if a thread in this process holds such a lock */
2536 if( pFile->locktype>SHARED_LOCK ){
2537 reserved = 1;
2538 }
2539
2540 /* Otherwise see if some other process holds it. */
2541 if( !reserved ){
2542 char *zLockFile = (char *)pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002543 struct stat statBuf;
aswift5b1a2562008-08-22 00:22:35 +00002544
2545 if( lstat(zLockFile, &statBuf)==0 ){
2546 /* file exists, someone else has the lock */
2547 reserved = 1;
2548 }else{
drh3b62b2f2007-06-08 18:27:03 +00002549 /* file does not exist, we could have it if we want it */
chw97185482008-11-17 08:05:31 +00002550 int tErrno = errno;
aswift5b1a2562008-08-22 00:22:35 +00002551 if( ENOENT != tErrno ){
2552 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2553 pFile->lastErrno = tErrno;
2554 }
drh339eb0b2008-03-07 15:34:11 +00002555 }
drhbfe66312006-10-03 17:40:40 +00002556 }
aswift5b1a2562008-08-22 00:22:35 +00002557 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002558
aswift5b1a2562008-08-22 00:22:35 +00002559 *pResOut = reserved;
2560 return rc;
drhbfe66312006-10-03 17:40:40 +00002561}
2562
danielk1977e339d652008-06-28 11:23:00 +00002563static int dotlockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002564 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002565 int fd;
danielk1977e339d652008-06-28 11:23:00 +00002566 char *zLockFile = (char *)pFile->lockingContext;
aswift5b1a2562008-08-22 00:22:35 +00002567 int rc=SQLITE_OK;
drh339eb0b2008-03-07 15:34:11 +00002568
drh3b62b2f2007-06-08 18:27:03 +00002569 /* if we already have a lock, it is exclusive.
2570 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002571 if (pFile->locktype > NO_LOCK) {
2572 pFile->locktype = locktype;
danielk1977c70dfc42008-11-19 13:52:30 +00002573#if !IS_VXWORKS
drhbfe66312006-10-03 17:40:40 +00002574 /* Always update the timestamp on the old file */
danielk1977e339d652008-06-28 11:23:00 +00002575 utimes(zLockFile, NULL);
chw97185482008-11-17 08:05:31 +00002576#endif
aswift5b1a2562008-08-22 00:22:35 +00002577 rc = SQLITE_OK;
2578 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002579 }
2580
drh3b62b2f2007-06-08 18:27:03 +00002581 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00002582 struct stat statBuf;
danielk1977e339d652008-06-28 11:23:00 +00002583 if (lstat(zLockFile,&statBuf) == 0){
aswift5b1a2562008-08-22 00:22:35 +00002584 rc = SQLITE_BUSY; /* it does, busy */
2585 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002586 }
2587
drh3b62b2f2007-06-08 18:27:03 +00002588 /* grab an exclusive lock */
danielk1977e339d652008-06-28 11:23:00 +00002589 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
drh339eb0b2008-03-07 15:34:11 +00002590 if( fd<0 ){
drh3b62b2f2007-06-08 18:27:03 +00002591 /* failed to open/create the file, someone else may have stolen the lock */
aswift5b1a2562008-08-22 00:22:35 +00002592 int tErrno = errno;
2593 if( EEXIST == tErrno ){
2594 rc = SQLITE_BUSY;
2595 } else {
2596 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2597 if( IS_LOCK_ERROR(rc) ){
aswiftaebf4132008-11-21 00:10:35 +00002598 pFile->lastErrno = tErrno;
aswift5b1a2562008-08-22 00:22:35 +00002599 }
2600 }
2601 goto dotlock_end_lock;
2602 }
aswiftaebf4132008-11-21 00:10:35 +00002603 if( close(fd) ){
2604 pFile->lastErrno = errno;
2605 rc = SQLITE_IOERR_CLOSE;
2606 }
drhbfe66312006-10-03 17:40:40 +00002607
drh3b62b2f2007-06-08 18:27:03 +00002608 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002609 pFile->locktype = locktype;
aswift5b1a2562008-08-22 00:22:35 +00002610
2611 dotlock_end_lock:
2612 return rc;
drhbfe66312006-10-03 17:40:40 +00002613}
2614
danielk1977e339d652008-06-28 11:23:00 +00002615static int dotlockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002616 unixFile *pFile = (unixFile*)id;
danielk1977e339d652008-06-28 11:23:00 +00002617 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00002618
aswift5b1a2562008-08-22 00:22:35 +00002619 assert( pFile );
2620 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2621 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002622 assert( locktype<=SHARED_LOCK );
2623
drh3b62b2f2007-06-08 18:27:03 +00002624 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002625 if( pFile->locktype==locktype ){
2626 return SQLITE_OK;
2627 }
2628
drh3b62b2f2007-06-08 18:27:03 +00002629 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002630 if (locktype==SHARED_LOCK) {
2631 pFile->locktype = locktype;
2632 return SQLITE_OK;
2633 }
2634
drh3b62b2f2007-06-08 18:27:03 +00002635 /* no, really, unlock. */
aswift5b1a2562008-08-22 00:22:35 +00002636 if (unlink(zLockFile) ) {
2637 int rc, tErrno = errno;
2638 if( ENOENT != tErrno ){
2639 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2640 }
2641 if( IS_LOCK_ERROR(rc) ){
2642 pFile->lastErrno = tErrno;
2643 }
2644 return rc;
2645 }
drhbfe66312006-10-03 17:40:40 +00002646 pFile->locktype = NO_LOCK;
2647 return SQLITE_OK;
2648}
2649
2650/*
2651 ** Close a file.
2652 */
danielk1977e339d652008-06-28 11:23:00 +00002653static int dotlockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00002654 int rc;
danielk1977e339d652008-06-28 11:23:00 +00002655 if( id ){
2656 unixFile *pFile = (unixFile*)id;
2657 dotlockUnlock(id, NO_LOCK);
2658 sqlite3_free(pFile->lockingContext);
2659 }
danielk1977c70dfc42008-11-19 13:52:30 +00002660 if( IS_VXWORKS ) enterMutex();
chw97185482008-11-17 08:05:31 +00002661 rc = closeUnixFile(id);
danielk1977c70dfc42008-11-19 13:52:30 +00002662 if( IS_VXWORKS ) leaveMutex();
chw97185482008-11-17 08:05:31 +00002663 return rc;
drhbfe66312006-10-03 17:40:40 +00002664}
2665
danielk1977c70dfc42008-11-19 13:52:30 +00002666#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00002667
2668#pragma mark POSIX/vxWorks named semaphore based locking
2669
2670/* Namedsem-style reserved lock checking following the behavior of
2671** unixCheckReservedLock, see the unixCheckReservedLock function comments */
2672static int namedsemCheckReservedLock(sqlite3_file *id, int *pResOut) {
2673 int rc = SQLITE_OK;
2674 int reserved = 0;
2675 unixFile *pFile = (unixFile*)id;
2676
2677 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2678
2679 assert( pFile );
2680
2681 /* Check if a thread in this process holds such a lock */
2682 if( pFile->locktype>SHARED_LOCK ){
2683 reserved = 1;
2684 }
2685
2686 /* Otherwise see if some other process holds it. */
2687 if( !reserved ){
2688 sem_t *pSem = pFile->pOpen->pSem;
2689 struct stat statBuf;
2690
2691 if( sem_trywait(pSem)==-1 ){
2692 int tErrno = errno;
2693 if( EAGAIN != tErrno ){
2694 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2695 pFile->lastErrno = tErrno;
2696 } else {
aswiftaebf4132008-11-21 00:10:35 +00002697 /* someone else has the lock when we are in NO_LOCK */
2698 reserved = (pFile->locktype < SHARED_LOCK);
chw97185482008-11-17 08:05:31 +00002699 }
2700 }else{
2701 /* we could have it if we want it */
2702 sem_post(pSem);
2703 }
2704 }
2705 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2706
2707 *pResOut = reserved;
2708 return rc;
2709}
2710
2711static int namedsemLock(sqlite3_file *id, int locktype) {
2712 unixFile *pFile = (unixFile*)id;
2713 int fd;
2714 sem_t *pSem = pFile->pOpen->pSem;
2715 int rc = SQLITE_OK;
2716
2717 /* if we already have a lock, it is exclusive.
2718 ** Just adjust level and punt on outta here. */
2719 if (pFile->locktype > NO_LOCK) {
2720 pFile->locktype = locktype;
2721 rc = SQLITE_OK;
2722 goto namedsem_end_lock;
2723 }
2724
2725 /* lock semaphore now but bail out when already locked. */
2726 if( sem_trywait(pSem)==-1 ){
2727 rc = SQLITE_BUSY;
2728 goto namedsem_end_lock;
2729 }
2730
2731 /* got it, set the type and return ok */
2732 pFile->locktype = locktype;
2733
2734 namedsem_end_lock:
2735 return rc;
2736}
2737
2738static int namedsemUnlock(sqlite3_file *id, int locktype) {
2739 unixFile *pFile = (unixFile*)id;
2740 sem_t *pSem = pFile->pOpen->pSem;
2741
2742 assert( pFile );
2743 assert( pSem );
2744 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2745 pFile->locktype, getpid());
2746 assert( locktype<=SHARED_LOCK );
2747
2748 /* no-op if possible */
2749 if( pFile->locktype==locktype ){
2750 return SQLITE_OK;
2751 }
2752
2753 /* shared can just be set because we always have an exclusive */
2754 if (locktype==SHARED_LOCK) {
2755 pFile->locktype = locktype;
2756 return SQLITE_OK;
2757 }
2758
2759 /* no, really unlock. */
2760 if ( sem_post(pSem)==-1 ) {
2761 int rc, tErrno = errno;
2762 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2763 if( IS_LOCK_ERROR(rc) ){
2764 pFile->lastErrno = tErrno;
2765 }
2766 return rc;
2767 }
2768 pFile->locktype = NO_LOCK;
2769 return SQLITE_OK;
2770}
2771
2772/*
2773 ** Close a file.
2774 */
2775static int namedsemClose(sqlite3_file *id) {
2776 if( id ){
2777 unixFile *pFile = (unixFile*)id;
2778 namedsemUnlock(id, NO_LOCK);
2779 assert( pFile );
2780 enterMutex();
2781 releaseLockInfo(pFile->pLock);
2782 releaseOpenCnt(pFile->pOpen);
2783 closeUnixFile(id);
2784 leaveMutex();
2785 }
2786 return SQLITE_OK;
2787}
2788
danielk1977c70dfc42008-11-19 13:52:30 +00002789#endif /* IS_VXWORKS */
drhbfe66312006-10-03 17:40:40 +00002790
aswiftaebf4132008-11-21 00:10:35 +00002791#pragma mark Proxy locking support
2792
2793static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
2794 unixFile *pFile = (unixFile*)id;
2795 int rc = takeConch(pFile);
2796 if( rc==SQLITE_OK ){
2797 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2798 unixFile *proxy = pCtx->lockProxy;
2799 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
2800 }
2801 return rc;
2802}
2803
2804static int proxyLock(sqlite3_file *id, int locktype) {
2805 unixFile *pFile = (unixFile*)id;
2806 int rc = takeConch(pFile);
2807 if( rc==SQLITE_OK ){
2808 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2809 unixFile *proxy = pCtx->lockProxy;
2810 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
2811 pFile->locktype = proxy->locktype;
2812 }
2813 return rc;
2814}
2815
2816static int proxyUnlock(sqlite3_file *id, int locktype) {
2817 unixFile *pFile = (unixFile*)id;
2818 int rc = takeConch(pFile);
2819 if( rc==SQLITE_OK ){
2820 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2821 unixFile *proxy = pCtx->lockProxy;
2822 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
2823 pFile->locktype = proxy->locktype;
2824 }
2825 return rc;
2826}
2827
2828/*
2829 ** Close a file.
2830 */
2831static int proxyClose(sqlite3_file *id) {
2832 if( id ){
2833 unixFile *pFile = (unixFile*)id;
2834 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2835 unixFile *lockProxy = pCtx->lockProxy;
2836 unixFile *conchFile = pCtx->conchFile;
2837 int rc = SQLITE_OK;
2838
2839 if( lockProxy ){
2840 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
2841 if( rc ) return rc;
2842 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
2843 if( rc ) return rc;
2844 sqlite3_free(lockProxy);
2845 }
2846 if( conchFile ){
2847 if( pCtx->conchHeld ){
2848 rc = releaseConch(pFile);
2849 if( rc ) return rc;
2850 }
2851 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
2852 if( rc ) return rc;
2853 sqlite3_free(conchFile);
2854 }
2855 sqlite3_free(pCtx->lockProxyPath);
2856 sqlite3_free(pCtx->conchFilePath);
2857 sqlite3_free(pCtx->dbPath);
2858 /* restore the original locking context and pMethod then close it */
2859 pFile->lockingContext = pCtx->oldLockingContext;
2860 pFile->pMethod = pCtx->pOldMethod;
2861 sqlite3_free(pCtx);
2862 return pFile->pMethod->xClose(id);
2863 }
2864 return SQLITE_OK;
2865}
2866
2867/* HOSTIDLEN and CONCHLEN both include space for the string
2868** terminating nul
2869*/
2870#define HOSTIDLEN 128
2871#define CONCHLEN (MAXPATHLEN+HOSTIDLEN+1)
2872#ifndef HOSTIDPATH
2873# define HOSTIDPATH "/Library/Caches/.com.apple.sqliteConchHostId"
2874#endif
2875
2876/* basically a copy of unixRandomness with different
2877** test behavior built in */
2878static int genHostID(char *pHostID){
2879 int pid, fd, i, len;
2880 unsigned char *key = (unsigned char *)pHostID;
2881
2882 memset(key, 0, HOSTIDLEN);
2883 len = 0;
2884 fd = open("/dev/urandom", O_RDONLY);
2885 if( fd>=0 ){
2886 len = read(fd, key, HOSTIDLEN);
2887 close(fd); /* silently leak the fd if it fails */
2888 }
2889 if( len < HOSTIDLEN ){
2890 time_t t;
2891 time(&t);
2892 memcpy(key, &t, sizeof(t));
2893 pid = getpid();
2894 memcpy(&key[sizeof(t)], &pid, sizeof(pid));
2895 }
2896
2897#ifdef MAKE_PRETTY_HOSTID
2898 /* filter the bytes into printable ascii characters and NUL terminate */
2899 key[(HOSTIDLEN-1)] = 0x00;
2900 for( i=0; i<(HOSTIDLEN-1); i++ ){
2901 unsigned char pa = key[i]&0x7F;
2902 if( pa<0x20 ){
2903 key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20;
2904 }else if( pa==0x7F ){
2905 key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E;
2906 }
2907 }
2908#endif
2909 return SQLITE_OK;
2910}
2911
aswiftaebf4132008-11-21 00:10:35 +00002912/* writes the host id path to path, path should be an pre-allocated buffer
2913** with enough space for a path */
2914static int getHostIDPath(char *path, size_t len){
2915 strlcpy(path, HOSTIDPATH, len);
2916#ifdef SQLITE_TEST
2917 if( sqlite3_hostid_num>0 ){
2918 char suffix[2] = "1";
2919 suffix[0] = suffix[0] + sqlite3_hostid_num;
2920 strlcat(path, suffix, len);
2921 }
2922#endif
2923 OSTRACE3("GETHOSTIDPATH %s pid=%d\n", path, getpid());
2924}
2925
2926/* get the host ID from a sqlite hostid file stored in the
2927** user-specific tmp directory, create the ID if it's not there already
2928*/
2929static int getHostID(char *pHostID, int *pError){
2930 int fd;
2931 char path[MAXPATHLEN];
2932 size_t len;
2933 int rc=SQLITE_OK;
2934
2935 getHostIDPath(path, MAXPATHLEN);
2936 /* try to create the host ID file, if it already exists read the contents */
2937 fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644);
2938 if( fd<0 ){
2939 int err=errno;
2940
2941 if( err!=EEXIST ){
2942#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
2943 fprintf(stderr, "sqlite error creating host ID file %s: %s\n", path, strerror(err));
2944#endif
2945 return SQLITE_PERM;
2946 }
2947 /* couldn't create the file, read it instead */
2948 fd = open(path, O_RDONLY|O_EXCL);
2949 if( fd<0 ){
2950 int err = errno;
2951#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
2952 fprintf(stderr, "sqlite error opening host ID file %s: %s\n", path, strerror(err));
2953#endif
2954 return SQLITE_PERM;
2955 }
2956 len = pread(fd, pHostID, HOSTIDLEN, 0);
2957 if( len<0 ){
2958 *pError = errno;
2959 rc = SQLITE_IOERR_READ;
2960 }else if( len<HOSTIDLEN ){
2961 *pError = 0;
2962 rc = SQLITE_IOERR_SHORT_READ;
2963 }
2964 close(fd); /* silently leak the fd if it fails */
2965 OSTRACE3("GETHOSTID read %s pid=%d\n", pHostID, getpid());
2966 return rc;
2967 }else{
2968 int i;
2969 /* we're creating the host ID file (use a random string of bytes) */
2970 genHostID(pHostID);
2971 len = pwrite(fd, pHostID, HOSTIDLEN, 0);
2972 if( len<0 ){
2973 *pError = errno;
2974 rc = SQLITE_IOERR_WRITE;
2975 }else if( len<HOSTIDLEN ){
2976 *pError = 0;
2977 rc = SQLITE_IOERR_WRITE;
2978 }
2979 close(fd); /* silently leak the fd if it fails */
2980 OSTRACE3("GETHOSTID wrote %s pid=%d\n", pHostID, getpid());
2981 return rc;
2982 }
2983}
2984
2985/* takes the conch by taking a shared lock and read the contents conch, if
2986** lockPath is non-NULL, the host ID and lock file path must match. A NULL
2987** lockPath means that the lockPath in the conch file will be used if the
2988** host IDs match, or a new lock path will be generated automatically
2989** and written to the conch file.
2990*/
2991static int takeConch(unixFile *pFile){
2992 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2993
2994 if( pCtx->conchHeld>0 ){
2995 return SQLITE_OK;
2996 }else{
2997 unixFile *conchFile = pCtx->conchFile;
2998 char testValue[CONCHLEN];
2999 char conchValue[CONCHLEN];
3000 char lockPath[MAXPATHLEN];
3001 char *tLockPath = NULL;
3002 int rc = SQLITE_OK;
3003 int readRc = SQLITE_OK;
3004 int syncPerms = 0;
3005
3006 OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h,
3007 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
3008
3009 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
3010 if( rc==SQLITE_OK ){
3011 int pError = 0;
3012 memset(testValue, 0, CONCHLEN); // conch is fixed size
3013 rc = getHostID(testValue, &pError);
3014 if( rc&SQLITE_IOERR==SQLITE_IOERR ){
3015 pFile->lastErrno = pError;
3016 }
3017 if( pCtx->lockProxyPath ){
3018 strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN);
3019 }
3020 }
3021 if( rc!=SQLITE_OK ){
3022 goto end_takeconch;
3023 }
3024
3025 readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0);
3026 if( readRc!=SQLITE_IOERR_SHORT_READ ){
3027 int match = 0;
3028 if( readRc!=SQLITE_OK ){
3029 if( rc&SQLITE_IOERR==SQLITE_IOERR ){
3030 pFile->lastErrno = conchFile->lastErrno;
3031 }
3032 rc = readRc;
3033 goto end_takeconch;
3034 }
3035 /* if the conch has data compare the contents */
3036 if( !pCtx->lockProxyPath ){
3037 /* for auto-named local lock file, just check the host ID and we'll
3038 ** use the local lock file path that's already in there */
3039 if( !memcmp(testValue, conchValue, HOSTIDLEN) ){
3040 tLockPath = (char *)&conchValue[HOSTIDLEN];
3041 goto end_takeconch;
3042 }
3043 }else{
3044 /* we've got the conch if conchValue matches our path and host ID */
3045 if( !memcmp(testValue, conchValue, CONCHLEN) ){
3046 goto end_takeconch;
3047 }
3048 }
3049 }else{
3050 /* a short read means we're "creating" the conch (even though it could
3051 ** have been user-intervention), if we acquire the exclusive lock,
3052 ** we'll try to match the current on-disk permissions of the database
3053 */
3054 syncPerms = 1;
3055 }
3056
3057 /* either conch was emtpy or didn't match */
3058 if( !pCtx->lockProxyPath ){
3059 getLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
3060 tLockPath = lockPath;
3061 strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN);
3062 }
3063
3064 /* update conch with host and path (this will fail if other process
3065 ** has a shared lock already) */
3066 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
3067 if( rc==SQLITE_OK ){
3068 rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0);
3069 if( rc==SQLITE_OK && syncPerms ){
3070 struct stat buf;
3071 int err = fstat(pFile->h, &buf);
3072 if( err==0 ){
3073 mode_t mode = buf.st_mode & 0100666;
3074 /* try to match the database file permissions, ignore failure */
3075#ifndef SQLITE_PROXY_DEBUG
3076 fchmod(conchFile->h, buf.st_mode);
3077#else
3078 if( fchmod(conchFile->h, buf.st_mode)!=0 ){
3079 int code = errno;
3080 fprintf(stderr, "fchmod %o FAILED with %d %s\n",buf.st_mode, code, strerror(code));
3081 } else {
3082 fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode);
3083 }
3084 }else{
3085 int code = errno;
3086 fprintf(stderr, "STAT FAILED[%d] with %d %s\n", err, code, strerror(code));
3087#endif
3088 }
3089 }
3090 }
3091 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
3092
3093end_takeconch:
3094 OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h);
3095 if( rc==SQLITE_OK && pFile->oflags ){
3096 if( pFile->h>=0 ){
3097#ifdef STRICT_CLOSE_ERROR
3098 if( close(pFile->h) ){
3099 pFile->lastErrno = errno;
3100 return SQLITE_IOERR_CLOSE;
3101 }
3102#else
3103 close(pFile->h); /* silently leak fd if fail */
3104#endif
3105 }
3106 pFile->h = -1;
3107 int fd = open(pCtx->dbPath, pFile->oflags, SQLITE_DEFAULT_FILE_PERMISSIONS);
3108 OSTRACE2("TRANSPROXY: OPEN %d\n", fd);
3109 if( fd>=0 ){
3110 pFile->h = fd;
3111 }else{
3112 rc=SQLITE_CANTOPEN; // SQLITE_BUSY? takeConch called during locking
3113 }
3114 }
3115 if( rc==SQLITE_OK && !pCtx->lockProxy ){
3116 char *path = tLockPath ? tLockPath : pCtx->lockProxyPath;
3117 // ACS: Need to make a copy of path sometimes
3118 rc = createProxyUnixFile(path, &pCtx->lockProxy);
3119 }
3120 if( rc==SQLITE_OK ){
3121 pCtx->conchHeld = 1;
3122
3123 if( tLockPath ){
3124 pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath);
3125 if( pCtx->lockProxy->pMethod == ioMethodForLockingStyle(LOCKING_STYLE_AFP) ){
3126 ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath = pCtx->lockProxyPath;
3127 }
3128 }
3129 } else {
3130 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
3131 }
3132 OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK ? "ok" : "failed");
3133 return rc;
3134 }
3135}
3136
3137static int releaseConch(unixFile *pFile){
3138 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
3139 int rc;
3140 unixFile *conchFile = pCtx->conchFile;
3141
3142 OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
3143 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
3144 getpid());
3145 pCtx->conchHeld = 0;
3146 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
3147 OSTRACE3("RELEASECONCH %d %s\n", conchFile->h,
3148 (rc==SQLITE_OK ? "ok" : "failed"));
3149 return rc;
3150}
3151
3152static int getConchPathFromDBPath(char *dbPath, char **pConchPath){
3153 int i;
3154 int len = strlen(dbPath);
3155 char *conchPath;
3156
3157 conchPath = (char *)sqlite3_malloc(len + 8);
3158 if( conchPath==0 ){
3159 return SQLITE_NOMEM;
3160 }
3161 strlcpy(conchPath, dbPath, len+1);
3162
3163 /* now insert a "." before the last / character */
3164 for( i=(len-1); i>=0; i-- ){
3165 if( conchPath[i]=='/' ){
3166 i++;
3167 break;
3168 }
3169 }
3170 conchPath[i]='.';
3171 while ( i<len ){
3172 conchPath[i+1]=dbPath[i];
3173 i++;
3174 }
3175 conchPath[i+1]='\0';
3176 strlcat(conchPath, "-conch", len + 8);
3177 *pConchPath = conchPath;
3178 return SQLITE_OK;
3179}
3180
3181static int getLockPath(const char *dbPath, char *lPath, size_t maxLen){
3182 int len;
3183 int dbLen;
3184 int i;
3185
3186#ifdef LOCKPROXYDIR
3187 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
3188#else
3189# ifdef _CS_DARWIN_USER_TEMP_DIR
3190 {
3191 char utdir[MAXPATHLEN];
3192
3193 confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
3194 len = strlcat(lPath, "sqliteplocks", maxLen);
3195 if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
3196 /* if mkdir fails, handle as lock file creation failure */
3197 int err = errno;
3198# ifdef SQLITE_DEBUG
3199 if( err!=EEXIST ){
3200 fprintf(stderr, "getLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
3201 SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
3202 }
3203# endif
3204 }else{
3205 OSTRACE3("GETLOCKPATH mkdir %s pid=%d\n", lPath, getpid());
3206 }
3207
3208 }
3209# else
3210 len = strlcpy(lPath, "/tmp/", maxLen);
3211# endif
3212#endif
3213
3214 if( lPath[len-1]!='/' ){
3215 len = strlcat(lPath, "/", maxLen);
3216 }
3217
3218 /* transform the db path to a unique cache name */
3219 dbLen = strlen(dbPath);
3220 for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
3221 char c = dbPath[i];
3222 lPath[i+len] = (c=='/')?'_':c;
3223 }
3224 lPath[i+len]='\0';
3225 strlcat(lPath, ":auto:", maxLen);
3226 return SQLITE_OK;
3227}
3228
3229/* Takes a fully configured proxy locking-style unix file and switches
3230** the local lock file path
3231*/
3232static int switchLockProxyPath(unixFile *pFile, const char *path) {
3233 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
3234 char *oldPath = pCtx->lockProxyPath;
3235 int taken = 0;
3236 int rc = SQLITE_OK;
3237
3238 if( pFile->locktype!=NO_LOCK ){
3239 return SQLITE_BUSY;
3240 }
3241
3242 /* nothing to do if the path is NULL, :auto: or matches the existing path */
3243 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
3244 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
3245 return SQLITE_OK;
3246 }else{
3247 unixFile *lockProxy = pCtx->lockProxy;
3248 pCtx->lockProxy=NULL;
3249 pCtx->conchHeld = 0;
3250 if( lockProxy!=NULL ){
3251 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
3252 if( rc ) return rc;
3253 sqlite3_free(lockProxy);
3254 }
3255 sqlite3_free(oldPath);
3256 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
3257 }
3258
3259 return rc;
3260}
3261
3262/*
3263** Takes an already filled in unix file and alters it so all file locking
3264** will be performed on the local proxy lock file. The following fields
3265** are preserved in the locking context so that they can be restored and
3266** the unix structure properly cleaned up at close time:
3267** ->lockingContext
3268** ->pMethod
3269*/
3270static int transformUnixFileForLockProxy(unixFile *pFile, const char *path) {
3271 proxyLockingContext *pCtx;
3272 char dbPath[MAXPATHLEN];
3273 char *lockPath=NULL;
3274 int rc = SQLITE_OK;
3275
3276 if( pFile->locktype!=NO_LOCK ){
3277 return SQLITE_BUSY;
3278 }
3279 getDbPathForUnixFile(pFile, dbPath);
3280 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
3281 lockPath=NULL;
3282 }else{
3283 lockPath=(char *)path;
3284 }
3285
3286 OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h,
3287 (lockPath ? lockPath : ":auto:"), getpid());
3288
3289 pCtx = sqlite3_malloc( sizeof(*pCtx) );
3290 if( pCtx==0 ){
3291 return SQLITE_NOMEM;
3292 }
3293 memset(pCtx, 0, sizeof(*pCtx));
3294
3295 rc = getConchPathFromDBPath(dbPath, &pCtx->conchFilePath);
3296 if( rc==SQLITE_OK ){
3297 rc = createProxyUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
3298 }
3299 if( rc==SQLITE_OK && lockPath ){
3300 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
3301 }
3302
3303end_transform_file:
3304 if( rc==SQLITE_OK ){
3305 /* all memory is allocated, proxys are created and assigned,
3306 ** switch the locking context and pMethod then return.
3307 */
3308 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
3309 pCtx->oldLockingContext = pFile->lockingContext;
3310 pFile->lockingContext = pCtx;
3311 pCtx->pOldMethod = pFile->pMethod;
3312 pFile->pMethod = ioMethodForLockingStyle(LOCKING_STYLE_PROXY);
3313 }else{
3314 if( pCtx->conchFile ){
3315 rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
3316 if( rc ) return rc;
3317 sqlite3_free(pCtx->conchFile);
3318 }
3319 sqlite3_free(pCtx->conchFilePath);
3320 sqlite3_free(pCtx);
3321 }
3322 OSTRACE3("TRANSPROXY %d %s\n", pFile->h,
3323 (rc==SQLITE_OK ? "ok" : "failed"));
3324 return rc;
3325}
3326
3327static int createProxyUnixFile(const char *path, unixFile **ppFile) {
3328 int fd;
3329 int dirfd = -1;
3330 unixFile *pNew;
3331 int rc = SQLITE_OK;
3332
3333 fd = open(path, O_RDWR | O_CREAT, SQLITE_DEFAULT_FILE_PERMISSIONS);
3334 if( fd<0 ){
3335 return SQLITE_CANTOPEN;
3336 }
3337
3338 pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
3339 if( pNew==NULL ){
3340 rc = SQLITE_NOMEM;
3341 goto end_create_proxy;
3342 }
3343 memset(pNew, 0, sizeof(unixFile));
3344
3345 rc = fillInUnixFile(NULL, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
3346 if( rc==SQLITE_OK ){
3347 *ppFile = pNew;
3348 return SQLITE_OK;
3349 }
3350end_create_proxy:
3351 close(fd); /* silently leak fd if error, we're already in error */
3352 sqlite3_free(pNew);
3353 return rc;
3354}
3355
3356
drhda0e7682008-07-30 15:27:54 +00003357#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +00003358
3359/*
drh339eb0b2008-03-07 15:34:11 +00003360** The nolockLockingContext is void
3361*/
drhbfe66312006-10-03 17:40:40 +00003362typedef void nolockLockingContext;
3363
danielk1977397d65f2008-11-19 11:35:39 +00003364static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
3365 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00003366 *pResOut = 0;
3367 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00003368}
3369
danielk1977397d65f2008-11-19 11:35:39 +00003370static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
3371 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbfe66312006-10-03 17:40:40 +00003372 return SQLITE_OK;
3373}
3374
danielk1977397d65f2008-11-19 11:35:39 +00003375static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
3376 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhbfe66312006-10-03 17:40:40 +00003377 return SQLITE_OK;
3378}
3379
3380/*
drh339eb0b2008-03-07 15:34:11 +00003381** Close a file.
3382*/
danielk1977e339d652008-06-28 11:23:00 +00003383static int nolockClose(sqlite3_file *id) {
chw97185482008-11-17 08:05:31 +00003384 int rc;
danielk1977397d65f2008-11-19 11:35:39 +00003385 if( IS_VXWORKS ) enterMutex();
chw97185482008-11-17 08:05:31 +00003386 rc = closeUnixFile(id);
danielk1977397d65f2008-11-19 11:35:39 +00003387 if( IS_VXWORKS ) leaveMutex();
chw97185482008-11-17 08:05:31 +00003388 return rc;
drhbfe66312006-10-03 17:40:40 +00003389}
3390
danielk1977ad94b582007-08-20 06:44:22 +00003391
danielk1977e3026632004-06-22 11:29:02 +00003392/*
drh9e33c2c2007-08-31 18:34:59 +00003393** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00003394*/
drhcc6bb3e2007-08-31 16:11:35 +00003395static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00003396 switch( op ){
3397 case SQLITE_FCNTL_LOCKSTATE: {
3398 *(int*)pArg = ((unixFile*)id)->locktype;
3399 return SQLITE_OK;
3400 }
aswiftaebf4132008-11-21 00:10:35 +00003401 case SQLITE_GET_LOCKPROXYFILE: {
3402#if SQLITE_ENABLE_LOCKING_STYLE
3403 unixFile *pFile = (unixFile*)id;
3404 if( pFile->pMethod == ioMethodForLockingStyle(LOCKING_STYLE_PROXY) ){
3405 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
3406 takeConch(pFile);
3407 if( pCtx->lockProxyPath ){
3408 *(const char **)pArg = pCtx->lockProxyPath;
3409 }else{
3410 *(const char **)pArg = ":auto: (not held)";
3411 }
3412 } else {
3413 *(const char **)pArg = NULL;
3414 }
3415#else
drh41022642008-11-21 00:24:42 +00003416 *(void**)pArg = NULL;
aswiftaebf4132008-11-21 00:10:35 +00003417#endif
3418 return SQLITE_OK;
3419 }
3420 case SQLITE_SET_LOCKPROXYFILE: {
3421#if SQLITE_ENABLE_LOCKING_STYLE
3422 unixFile *pFile = (unixFile*)id;
3423 int rc = SQLITE_OK;
3424 int isProxyStyle = (pFile->pMethod == ioMethodForLockingStyle(LOCKING_STYLE_PROXY));
3425 if( pArg==NULL || (const char *)pArg==0 ){
3426 if( isProxyStyle ){
3427 // turn off proxy locking - not supported
3428 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
3429 }else{
3430 // turn off proxy locking - already off - NOOP
3431 rc = SQLITE_OK;
3432 }
3433 }else{
3434 const char *proxyPath = (const char *)pArg;
3435 if( isProxyStyle ){
3436 proxyLockingContext *pCtx =
3437 (proxyLockingContext*)pFile->lockingContext;
3438 if( !strcmp(pArg, ":auto:") || (pCtx->lockProxyPath && !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) ){
3439 rc = SQLITE_OK;
3440 }else{
3441 rc = switchLockProxyPath(pFile, proxyPath);
3442 }
3443 }else{
3444 // turn on proxy file locking
3445 rc = transformUnixFileForLockProxy(pFile, proxyPath);
3446 }
3447 }
3448 return rc;
3449#else
3450 return SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
3451#endif
3452 }
3453 case SQLITE_LAST_ERRNO: {
3454 *(int*)pArg = ((unixFile*)id)->lastErrno;
3455 return SQLITE_OK;
3456 }
3457
drh9e33c2c2007-08-31 18:34:59 +00003458 }
drhcc6bb3e2007-08-31 16:11:35 +00003459 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00003460}
3461
3462/*
danielk1977a3d4c882007-03-23 10:08:38 +00003463** Return the sector size in bytes of the underlying block device for
3464** the specified file. This is almost always 512 bytes, but may be
3465** larger for some devices.
3466**
3467** SQLite code assumes this function cannot fail. It also assumes that
3468** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00003469** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00003470** same for both.
3471*/
danielk1977397d65f2008-11-19 11:35:39 +00003472static int unixSectorSize(sqlite3_file *NotUsed){
3473 UNUSED_PARAMETER(NotUsed);
drh3ceeb752007-03-29 18:19:52 +00003474 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00003475}
3476
danielk197790949c22007-08-17 16:50:38 +00003477/*
danielk1977397d65f2008-11-19 11:35:39 +00003478** Return the device characteristics for the file. This is always 0 for unix.
danielk197790949c22007-08-17 16:50:38 +00003479*/
danielk1977397d65f2008-11-19 11:35:39 +00003480static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
3481 UNUSED_PARAMETER(NotUsed);
danielk197762079062007-08-15 17:08:46 +00003482 return 0;
3483}
3484
aswiftaebf4132008-11-21 00:10:35 +00003485#define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \
34861, /* iVersion */ \
3487xClose, /* xClose */ \
3488unixRead, /* xRead */ \
3489unixWrite, /* xWrite */ \
3490unixTruncate, /* xTruncate */ \
3491unixSync, /* xSync */ \
3492unixFileSize, /* xFileSize */ \
3493xLock, /* xLock */ \
3494xUnlock, /* xUnlock */ \
3495xCheckReservedLock, /* xCheckReservedLock */ \
3496unixFileControl, /* xFileControl */ \
3497unixSectorSize, /* xSectorSize */ \
3498unixDeviceCharacteristics /* xDeviceCapabilities */ \
3499}
3500static sqlite3_io_methods aIoMethod[] = {
3501IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
3502,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3503#if SQLITE_ENABLE_LOCKING_STYLE
3504,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
3505#if IS_VXWORKS
3506 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3507 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3508 ,IOMETHODS(namedsemClose, namedsemLock, namedsemUnlock, namedsemCheckReservedLock)
3509 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3510#else
3511 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
3512 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
3513 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
3514 ,IOMETHODS(proxyClose, proxyLock, proxyUnlock, proxyCheckReservedLock)
3515#endif
3516#endif
3517/* The order of the IOMETHODS macros above is important. It must be the
3518 ** same order as the LOCKING_STYLE numbers
3519 */
3520};
3521
danielk1977a3d4c882007-03-23 10:08:38 +00003522/*
danielk1977e339d652008-06-28 11:23:00 +00003523** Initialize the contents of the unixFile structure pointed to by pId.
3524**
danielk1977ad94b582007-08-20 06:44:22 +00003525** When locking extensions are enabled, the filepath and locking style
3526** are needed to determine the unixFile pMethod to use for locking operations.
3527** The locking-style specific lockingContext data structure is created
3528** and assigned here also.
3529*/
3530static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00003531 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00003532 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00003533 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00003534 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00003535 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00003536 int noLock, /* Omit locking if true */
3537 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00003538){
drhda0e7682008-07-30 15:27:54 +00003539 int eLockingStyle;
3540 unixFile *pNew = (unixFile *)pId;
3541 int rc = SQLITE_OK;
3542
danielk197717b90b52008-06-06 11:11:25 +00003543 assert( pNew->pLock==NULL );
3544 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00003545
danielk1977a03396a2008-11-19 14:35:46 +00003546 /* Parameter isDelete is only used on vxworks. Parameter pVfs is only
3547 ** used if ENABLE_LOCKING_STYLE is defined. Express this explicitly
3548 ** here to prevent compiler warnings about unused parameters.
3549 */
danielk1977f3d3c272008-11-19 16:52:44 +00003550 if( !IS_VXWORKS ) UNUSED_PARAMETER(isDelete);
3551 if( !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(pVfs);
3552 if( !IS_VXWORKS && !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(zFilename);
danielk1977a03396a2008-11-19 14:35:46 +00003553
drh218c5082008-03-07 00:27:10 +00003554 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00003555 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00003556 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00003557 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00003558
danielk1977c70dfc42008-11-19 13:52:30 +00003559#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003560 {
3561 HashElem *pElem;
3562 char *zRealname = vxrealpath(zFilename, 1);
3563 int n;
3564 pNew->zRealpath = 0;
3565 if( !zRealname ){
3566 rc = SQLITE_NOMEM;
3567 eLockingStyle = LOCKING_STYLE_NONE;
3568 }else{
3569 n = strlen(zRealname) + 1;
3570 enterMutex();
3571 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
3572 if( pElem ){
3573 long cnt = (long)pElem->data;
3574 cnt++;
3575 pNew->zRealpath = pElem->pKey;
3576 pElem->data = (void*)cnt;
3577 }else{
3578 if( sqlite3HashInsert(&nameHash, zRealname, n, (void*)1)==0 ){
3579 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
3580 if( pElem ){
3581 pNew->zRealpath = pElem->pKey;
3582 }else{
3583 sqlite3HashInsert(&nameHash, zRealname, n, 0);
3584 rc = SQLITE_NOMEM;
3585 eLockingStyle = LOCKING_STYLE_NONE;
3586 }
3587 }
3588 }
3589 leaveMutex();
3590 sqlite3_free(zRealname);
3591 }
3592 }
3593#endif
3594
drhda0e7682008-07-30 15:27:54 +00003595 if( noLock ){
3596 eLockingStyle = LOCKING_STYLE_NONE;
3597 }else{
3598 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
aswiftaebf4132008-11-21 00:10:35 +00003599#if SQLITE_ENABLE_LOCKING_STYLE
3600 /* Cache zFilename in the locking context (AFP and dotlock override) for
3601 ** proxyLock activation is possible (remote proxy is based on db name)
3602 ** zFilename remains valid until file is closed, to support */
3603 pNew->lockingContext = (void*)zFilename;
3604#endif
drhda0e7682008-07-30 15:27:54 +00003605 }
danielk1977e339d652008-06-28 11:23:00 +00003606
aswiftaebf4132008-11-21 00:10:35 +00003607 /* Macro to define the static contents of an sqlite3_io_methods
3608 ** structure for a unix backend file. Different locking methods
3609 ** require different functions for the xClose, xLock, xUnlock and
3610 ** xCheckReservedLock methods.
3611 */
3612 assert(LOCKING_STYLE_POSIX==1);
3613 assert(LOCKING_STYLE_NONE==2);
3614 assert(LOCKING_STYLE_DOTFILE==3);
3615 assert(LOCKING_STYLE_FLOCK==4);
3616 assert(LOCKING_STYLE_AFP==5);
3617 assert(LOCKING_STYLE_NAMEDSEM==6);
3618 assert(LOCKING_STYLE_PROXY==7);
3619
danielk1977e339d652008-06-28 11:23:00 +00003620 switch( eLockingStyle ){
3621
3622 case LOCKING_STYLE_POSIX: {
3623 enterMutex();
danielk1977c70dfc42008-11-19 13:52:30 +00003624#if IS_VXWORKS
aswiftaebf4132008-11-21 00:10:35 +00003625 rc = findLockInfo(pNew, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
chw97185482008-11-17 08:05:31 +00003626#else
aswiftaebf4132008-11-21 00:10:35 +00003627 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
chw97185482008-11-17 08:05:31 +00003628#endif
danielk1977e339d652008-06-28 11:23:00 +00003629 leaveMutex();
drh218c5082008-03-07 00:27:10 +00003630 break;
drhbfe66312006-10-03 17:40:40 +00003631 }
danielk1977e339d652008-06-28 11:23:00 +00003632
drh40bbb0a2008-09-23 10:23:26 +00003633#if SQLITE_ENABLE_LOCKING_STYLE
chw97185482008-11-17 08:05:31 +00003634
danielk1977c70dfc42008-11-19 13:52:30 +00003635#if !IS_VXWORKS
danielk1977e339d652008-06-28 11:23:00 +00003636 case LOCKING_STYLE_AFP: {
3637 /* AFP locking uses the file path so it needs to be included in
3638 ** the afpLockingContext.
3639 */
3640 afpLockingContext *pCtx;
3641 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
3642 if( pCtx==0 ){
3643 rc = SQLITE_NOMEM;
3644 }else{
3645 /* NB: zFilename exists and remains valid until the file is closed
3646 ** according to requirement F11141. So we do not need to make a
3647 ** copy of the filename. */
aswiftaebf4132008-11-21 00:10:35 +00003648 pCtx->dbPath = zFilename;
danielk1977e339d652008-06-28 11:23:00 +00003649 srandomdev();
aswiftaebf4132008-11-21 00:10:35 +00003650 enterMutex();
3651 rc = findLockInfo(pNew, NULL, &pNew->pOpen);
3652 leaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00003653 }
drh218c5082008-03-07 00:27:10 +00003654 break;
danielk1977e339d652008-06-28 11:23:00 +00003655 }
chw97185482008-11-17 08:05:31 +00003656#endif
danielk1977e339d652008-06-28 11:23:00 +00003657
3658 case LOCKING_STYLE_DOTFILE: {
3659 /* Dotfile locking uses the file path so it needs to be included in
3660 ** the dotlockLockingContext
3661 */
3662 char *zLockFile;
drh218c5082008-03-07 00:27:10 +00003663 int nFilename;
danielk1977e339d652008-06-28 11:23:00 +00003664 nFilename = strlen(zFilename) + 6;
3665 zLockFile = (char *)sqlite3_malloc(nFilename);
3666 if( zLockFile==0 ){
3667 rc = SQLITE_NOMEM;
3668 }else{
aswiftaebf4132008-11-21 00:10:35 +00003669 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
drh339eb0b2008-03-07 15:34:11 +00003670 }
danielk1977e339d652008-06-28 11:23:00 +00003671 pNew->lockingContext = zLockFile;
drh218c5082008-03-07 00:27:10 +00003672 break;
3673 }
danielk1977e339d652008-06-28 11:23:00 +00003674
danielk1977c70dfc42008-11-19 13:52:30 +00003675#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003676 case LOCKING_STYLE_NAMEDSEM: {
3677 /* Named semaphore locking uses the file path so it needs to be
aswiftaebf4132008-11-21 00:10:35 +00003678 ** included in the namedsemLockingContext
3679 */
chw97185482008-11-17 08:05:31 +00003680 enterMutex();
3681 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
3682 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
3683 char *zSemName = pNew->pOpen->aSemName;
3684 int n;
3685 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem", pNew->zRealpath);
3686 for( n=0; zSemName[n]; n++ )
3687 if( zSemName[n]=='/' ) zSemName[n] = '_';
3688 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
3689 if( pNew->pOpen->pSem == SEM_FAILED ){
3690 rc = SQLITE_NOMEM;
3691 pNew->pOpen->aSemName[0] = '\0';
3692 }
3693 }
3694 leaveMutex();
3695 break;
3696 }
3697#endif
3698
danielk1977e339d652008-06-28 11:23:00 +00003699 case LOCKING_STYLE_FLOCK:
3700 case LOCKING_STYLE_NONE:
drh218c5082008-03-07 00:27:10 +00003701 break;
drhe78669b2007-06-29 12:04:26 +00003702#endif
danielk1977e339d652008-06-28 11:23:00 +00003703 }
aswift5b1a2562008-08-22 00:22:35 +00003704
3705 pNew->lastErrno = 0;
danielk1977c70dfc42008-11-19 13:52:30 +00003706#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003707 if( rc!=SQLITE_OK ){
3708 unlink(zFilename);
3709 isDelete = 0;
3710 }
3711 pNew->isDelete = isDelete;
3712#endif
danielk1977e339d652008-06-28 11:23:00 +00003713 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003714 if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */
drhbfe66312006-10-03 17:40:40 +00003715 close(h);
danielk1977e339d652008-06-28 11:23:00 +00003716 }else{
danielk19776cb427f2008-06-30 10:16:04 +00003717 pNew->pMethod = &aIoMethod[eLockingStyle-1];
danielk1977e339d652008-06-28 11:23:00 +00003718 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00003719 }
danielk1977e339d652008-06-28 11:23:00 +00003720 return rc;
drh054889e2005-11-30 03:20:31 +00003721}
drh9c06c952005-11-26 00:25:00 +00003722
aswiftaebf4132008-11-21 00:10:35 +00003723#if SQLITE_ENABLE_LOCKING_STYLE
3724static sqlite3_io_methods *ioMethodForLockingStyle(int style){
3725 return &aIoMethod[style-1];
3726}
3727
3728static int getDbPathForUnixFile(unixFile *pFile, char *dbPath){
3729 if( pFile->pMethod==ioMethodForLockingStyle(LOCKING_STYLE_AFP) ){
3730 /* afp style keeps a reference to the db path in the filePath field of the struct */
3731 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
3732 return SQLITE_OK;
3733 }
3734 if( pFile->pMethod==ioMethodForLockingStyle(LOCKING_STYLE_DOTFILE) ){
3735 /* dot lock style uses the locking context to store the dot lock file path */
3736 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
3737 strlcpy(dbPath, (char *)pFile->lockingContext, len + 1);
3738 return SQLITE_OK;
3739 }
3740 /* all other styles use the locking context to store the db file path */
3741 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
3742 return SQLITE_OK;
3743}
3744#endif
3745
danielk1977ad94b582007-08-20 06:44:22 +00003746/*
3747** Open a file descriptor to the directory containing file zFilename.
3748** If successful, *pFd is set to the opened file descriptor and
3749** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
3750** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
3751** value.
3752**
3753** If SQLITE_OK is returned, the caller is responsible for closing
3754** the file descriptor *pFd using close().
3755*/
danielk1977fee2d252007-08-18 10:59:19 +00003756static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00003757 int ii;
drh777b17a2007-09-20 10:02:54 +00003758 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00003759 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00003760
drh153c62c2007-08-24 03:51:33 +00003761 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00003762 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
3763 if( ii>0 ){
3764 zDirname[ii] = '\0';
3765 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00003766 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003767#ifdef FD_CLOEXEC
3768 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3769#endif
3770 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
3771 }
3772 }
danielk1977fee2d252007-08-18 10:59:19 +00003773 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00003774 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00003775}
3776
danielk1977b4b47412007-08-17 15:53:36 +00003777/*
danielk197717b90b52008-06-06 11:11:25 +00003778** Create a temporary file name in zBuf. zBuf must be allocated
3779** by the calling process and must be big enough to hold at least
3780** pVfs->mxPathname bytes.
3781*/
3782static int getTempname(int nBuf, char *zBuf){
3783 static const char *azDirs[] = {
3784 0,
aswiftaebf4132008-11-21 00:10:35 +00003785 0,
danielk197717b90b52008-06-06 11:11:25 +00003786 "/var/tmp",
3787 "/usr/tmp",
3788 "/tmp",
3789 ".",
3790 };
3791 static const unsigned char zChars[] =
3792 "abcdefghijklmnopqrstuvwxyz"
3793 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3794 "0123456789";
drh41022642008-11-21 00:24:42 +00003795 unsigned int i, j;
danielk197717b90b52008-06-06 11:11:25 +00003796 struct stat buf;
3797 const char *zDir = ".";
3798
3799 /* It's odd to simulate an io-error here, but really this is just
3800 ** using the io-error infrastructure to test that SQLite handles this
3801 ** function failing.
3802 */
3803 SimulateIOError( return SQLITE_IOERR );
3804
3805 azDirs[0] = sqlite3_temp_directory;
aswiftaebf4132008-11-21 00:10:35 +00003806 if (NULL == azDirs[1]) {
3807 azDirs[1] = getenv("TMPDIR");
3808 }
3809
3810 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
danielk197717b90b52008-06-06 11:11:25 +00003811 if( azDirs[i]==0 ) continue;
3812 if( stat(azDirs[i], &buf) ) continue;
3813 if( !S_ISDIR(buf.st_mode) ) continue;
3814 if( access(azDirs[i], 07) ) continue;
3815 zDir = azDirs[i];
3816 break;
3817 }
3818
3819 /* Check that the output buffer is large enough for the temporary file
3820 ** name. If it is not, return SQLITE_ERROR.
3821 */
danielk197700e13612008-11-17 19:18:54 +00003822 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00003823 return SQLITE_ERROR;
3824 }
3825
3826 do{
3827 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
3828 j = strlen(zBuf);
3829 sqlite3_randomness(15, &zBuf[j]);
3830 for(i=0; i<15; i++, j++){
3831 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3832 }
3833 zBuf[j] = 0;
3834 }while( access(zBuf,0)==0 );
3835 return SQLITE_OK;
3836}
3837
3838
3839/*
danielk1977ad94b582007-08-20 06:44:22 +00003840** Open the file zPath.
3841**
danielk1977b4b47412007-08-17 15:53:36 +00003842** Previously, the SQLite OS layer used three functions in place of this
3843** one:
3844**
3845** sqlite3OsOpenReadWrite();
3846** sqlite3OsOpenReadOnly();
3847** sqlite3OsOpenExclusive();
3848**
3849** These calls correspond to the following combinations of flags:
3850**
3851** ReadWrite() -> (READWRITE | CREATE)
3852** ReadOnly() -> (READONLY)
3853** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
3854**
3855** The old OpenExclusive() accepted a boolean argument - "delFlag". If
3856** true, the file was configured to be automatically deleted when the
3857** file handle closed. To achieve the same effect using this new
3858** interface, add the DELETEONCLOSE flag to those specified above for
3859** OpenExclusive().
3860*/
3861static int unixOpen(
drh153c62c2007-08-24 03:51:33 +00003862 sqlite3_vfs *pVfs,
danielk1977b4b47412007-08-17 15:53:36 +00003863 const char *zPath,
3864 sqlite3_file *pFile,
3865 int flags,
3866 int *pOutFlags
3867){
danielk1977fee2d252007-08-18 10:59:19 +00003868 int fd = 0; /* File descriptor returned by open() */
3869 int dirfd = -1; /* Directory file descriptor */
3870 int oflags = 0; /* Flags to pass to open() */
3871 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00003872 int noLock; /* True to omit locking primitives */
aswiftaebf4132008-11-21 00:10:35 +00003873 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003874
3875 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3876 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3877 int isCreate = (flags & SQLITE_OPEN_CREATE);
3878 int isReadonly = (flags & SQLITE_OPEN_READONLY);
3879 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3880
danielk1977fee2d252007-08-18 10:59:19 +00003881 /* If creating a master or main-file journal, this function will open
3882 ** a file-descriptor on the directory too. The first time unixSync()
3883 ** is called the directory file descriptor will be fsync()ed and close()d.
3884 */
3885 int isOpenDirectory = (isCreate &&
3886 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
3887 );
3888
danielk197717b90b52008-06-06 11:11:25 +00003889 /* If argument zPath is a NULL pointer, this function is required to open
3890 ** a temporary file. Use this buffer to store the file name in.
3891 */
3892 char zTmpname[MAX_PATHNAME+1];
3893 const char *zName = zPath;
3894
danielk1977fee2d252007-08-18 10:59:19 +00003895 /* Check the following statements are true:
3896 **
3897 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3898 ** (b) if CREATE is set, then READWRITE must also be set, and
3899 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00003900 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00003901 */
danielk1977b4b47412007-08-17 15:53:36 +00003902 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00003903 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00003904 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00003905 assert(isDelete==0 || isCreate);
3906
drh33f4e022007-09-03 15:19:34 +00003907 /* The main DB, main journal, and master journal are never automatically
3908 ** deleted
3909 */
3910 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
3911 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
3912 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00003913
danielk1977fee2d252007-08-18 10:59:19 +00003914 /* Assert that the upper layer has set one of the "file-type" flags. */
3915 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
3916 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
3917 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00003918 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00003919 );
3920
danielk1977e339d652008-06-28 11:23:00 +00003921 memset(pFile, 0, sizeof(unixFile));
3922
danielk197717b90b52008-06-06 11:11:25 +00003923 if( !zName ){
danielk197717b90b52008-06-06 11:11:25 +00003924 assert(isDelete && !isOpenDirectory);
3925 rc = getTempname(MAX_PATHNAME+1, zTmpname);
3926 if( rc!=SQLITE_OK ){
3927 return rc;
3928 }
3929 zName = zTmpname;
3930 }
3931
danielk1977b4b47412007-08-17 15:53:36 +00003932 if( isReadonly ) oflags |= O_RDONLY;
3933 if( isReadWrite ) oflags |= O_RDWR;
3934 if( isCreate ) oflags |= O_CREAT;
3935 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
3936 oflags |= (O_LARGEFILE|O_BINARY);
3937
danielk197717b90b52008-06-06 11:11:25 +00003938 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
chw97185482008-11-17 08:05:31 +00003939 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, oflags);
danielk19772f2d8c72007-08-30 16:13:33 +00003940 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00003941 /* Failed to open the file for read/write access. Try read-only. */
3942 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
3943 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00003944 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00003945 }
3946 if( fd<0 ){
3947 return SQLITE_CANTOPEN;
3948 }
3949 if( isDelete ){
danielk1977c70dfc42008-11-19 13:52:30 +00003950#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00003951 zPath = zName;
3952#else
danielk197717b90b52008-06-06 11:11:25 +00003953 unlink(zName);
chw97185482008-11-17 08:05:31 +00003954#endif
danielk1977b4b47412007-08-17 15:53:36 +00003955 }
drh41022642008-11-21 00:24:42 +00003956#if SQLITE_ENABLE_LOCKING_STYLE
3957 else{
3958 ((unixFile*)pFile)->oflags = oflags;
3959 }
3960#endif
danielk1977b4b47412007-08-17 15:53:36 +00003961 if( pOutFlags ){
3962 *pOutFlags = flags;
3963 }
3964
3965 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00003966 if( isOpenDirectory ){
aswiftaebf4132008-11-21 00:10:35 +00003967 rc = openDirectory(zPath, &dirfd);
danielk1977fee2d252007-08-18 10:59:19 +00003968 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003969 close(fd); /* silently leak if fail, already in error */
danielk1977fee2d252007-08-18 10:59:19 +00003970 return rc;
3971 }
3972 }
danielk1977e339d652008-06-28 11:23:00 +00003973
3974#ifdef FD_CLOEXEC
3975 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3976#endif
3977
drhda0e7682008-07-30 15:27:54 +00003978 noLock = eType!=SQLITE_OPEN_MAIN_DB;
aswiftaebf4132008-11-21 00:10:35 +00003979
3980#if SQLITE_PREFER_PROXY_LOCKING
3981 if( zPath!=NULL && !noLock ){
3982 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
3983 int useProxy = 0;
3984
3985 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy,
3986 ** 0 means never use proxy, NULL means use proxy for non-local files only
3987 */
3988 if( envforce!=NULL ){
3989 useProxy = atoi(envforce)>0;
3990 }else{
3991 struct statfs fsInfo;
3992
3993 if( statfs(zPath, &fsInfo) == -1 ){
3994 ((unixFile*)pFile)->lastErrno = errno;
3995 if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
3996 close(fd); /* silently leak if fail, in error */
3997 return SQLITE_IOERR_ACCESS;
3998 }
3999 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
4000 }
4001 if( useProxy ){
4002 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
4003 if( rc==SQLITE_OK ){
4004 rc = transformUnixFileForLockProxy((unixFile*)pFile, ":auto:");
4005 }
4006 return rc;
4007 }
4008 }
4009#endif
4010
chw97185482008-11-17 08:05:31 +00004011 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00004012}
4013
4014/*
danielk1977fee2d252007-08-18 10:59:19 +00004015** Delete the file at zPath. If the dirSync argument is true, fsync()
4016** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00004017*/
danielk1977397d65f2008-11-19 11:35:39 +00004018static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){
danielk1977fee2d252007-08-18 10:59:19 +00004019 int rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00004020 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00004021 SimulateIOError(return SQLITE_IOERR_DELETE);
4022 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00004023#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00004024 if( dirSync ){
4025 int fd;
4026 rc = openDirectory(zPath, &fd);
4027 if( rc==SQLITE_OK ){
danielk1977c70dfc42008-11-19 13:52:30 +00004028#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004029 if( fsync(fd)==-1 )
4030#else
4031 if( fsync(fd) )
4032#endif
4033 {
danielk1977fee2d252007-08-18 10:59:19 +00004034 rc = SQLITE_IOERR_DIR_FSYNC;
4035 }
aswiftaebf4132008-11-21 00:10:35 +00004036 if( close(fd)&&!rc ){
4037 rc = SQLITE_IOERR_DIR_CLOSE;
4038 }
danielk1977fee2d252007-08-18 10:59:19 +00004039 }
4040 }
danielk1977d138dd82008-10-15 16:02:48 +00004041#endif
danielk1977fee2d252007-08-18 10:59:19 +00004042 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00004043}
4044
danielk197790949c22007-08-17 16:50:38 +00004045/*
4046** Test the existance of or access permissions of file zPath. The
4047** test performed depends on the value of flags:
4048**
4049** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
4050** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
4051** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
4052**
4053** Otherwise return 0.
4054*/
danielk1977861f7452008-06-05 11:39:11 +00004055static int unixAccess(
danielk1977397d65f2008-11-19 11:35:39 +00004056 sqlite3_vfs *NotUsed,
danielk1977861f7452008-06-05 11:39:11 +00004057 const char *zPath,
4058 int flags,
4059 int *pResOut
4060){
rse25c0d1a2007-09-20 08:38:14 +00004061 int amode = 0;
danielk1977397d65f2008-11-19 11:35:39 +00004062 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00004063 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00004064 switch( flags ){
4065 case SQLITE_ACCESS_EXISTS:
4066 amode = F_OK;
4067 break;
4068 case SQLITE_ACCESS_READWRITE:
4069 amode = W_OK|R_OK;
4070 break;
drh50d3f902007-08-27 21:10:36 +00004071 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00004072 amode = R_OK;
4073 break;
4074
4075 default:
4076 assert(!"Invalid flags argument");
4077 }
danielk1977861f7452008-06-05 11:39:11 +00004078 *pResOut = (access(zPath, amode)==0);
4079 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00004080}
4081
danielk1977b4b47412007-08-17 15:53:36 +00004082
4083/*
4084** Turn a relative pathname into a full pathname. The relative path
4085** is stored as a nul-terminated string in the buffer pointed to by
4086** zPath.
4087**
4088** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
4089** (in this case, MAX_PATHNAME bytes). The full-path is written to
4090** this buffer before returning.
4091*/
danielk1977adfb9b02007-09-17 07:02:56 +00004092static int unixFullPathname(
4093 sqlite3_vfs *pVfs, /* Pointer to vfs object */
4094 const char *zPath, /* Possibly relative input path */
4095 int nOut, /* Size of output buffer in bytes */
4096 char *zOut /* Output buffer */
4097){
danielk1977843e65f2007-09-01 16:16:15 +00004098
4099 /* It's odd to simulate an io-error here, but really this is just
4100 ** using the io-error infrastructure to test that SQLite handles this
4101 ** function failing. This function could fail if, for example, the
4102 ** current working directly has been unlinked.
4103 */
4104 SimulateIOError( return SQLITE_ERROR );
4105
drh153c62c2007-08-24 03:51:33 +00004106 assert( pVfs->mxPathname==MAX_PATHNAME );
danielk1977f3d3c272008-11-19 16:52:44 +00004107 UNUSED_PARAMETER(pVfs);
chw97185482008-11-17 08:05:31 +00004108
danielk1977c70dfc42008-11-19 13:52:30 +00004109#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004110 {
4111 char *zRealname = vxrealpath(zPath, 0);
4112 zOut[0] = '\0';
4113 if( !zRealname ){
4114 return SQLITE_CANTOPEN;
4115 }
4116 sqlite3_snprintf(nOut, zOut, "%s", zRealname);
4117 sqlite3_free(zRealname);
4118 return SQLITE_OK;
4119 }
4120#else
drh3c7f2dc2007-12-06 13:26:20 +00004121 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00004122 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00004123 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00004124 }else{
4125 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00004126 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00004127 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00004128 }
4129 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00004130 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00004131 }
4132 return SQLITE_OK;
4133
4134#if 0
4135 /*
4136 ** Remove "/./" path elements and convert "/A/./" path elements
4137 ** to just "/".
4138 */
4139 if( zFull ){
4140 int i, j;
4141 for(i=j=0; zFull[i]; i++){
4142 if( zFull[i]=='/' ){
4143 if( zFull[i+1]=='/' ) continue;
4144 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
4145 i += 1;
4146 continue;
4147 }
4148 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
4149 while( j>0 && zFull[j-1]!='/' ){ j--; }
4150 i += 3;
4151 continue;
4152 }
4153 }
4154 zFull[j++] = zFull[i];
4155 }
4156 zFull[j] = 0;
4157 }
4158#endif
aswiftaebf4132008-11-21 00:10:35 +00004159#endif /* #if IS_VXWORKS */
danielk1977b4b47412007-08-17 15:53:36 +00004160}
4161
drh0ccebe72005-06-07 22:22:50 +00004162
drh761df872006-12-21 01:29:22 +00004163#ifndef SQLITE_OMIT_LOAD_EXTENSION
4164/*
4165** Interfaces for opening a shared library, finding entry points
4166** within the shared library, and closing the shared library.
4167*/
4168#include <dlfcn.h>
danielk1977397d65f2008-11-19 11:35:39 +00004169static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
4170 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00004171 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
4172}
danielk197795c8a542007-09-01 06:51:27 +00004173
4174/*
4175** SQLite calls this function immediately after a call to unixDlSym() or
4176** unixDlOpen() fails (returns a null pointer). If a more detailed error
4177** message is available, it is written to zBufOut. If no error message
4178** is available, zBufOut is left unmodified and SQLite uses a default
4179** error message.
4180*/
danielk1977397d65f2008-11-19 11:35:39 +00004181static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00004182 char *zErr;
danielk1977397d65f2008-11-19 11:35:39 +00004183 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00004184 enterMutex();
4185 zErr = dlerror();
4186 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00004187 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00004188 }
4189 leaveMutex();
4190}
danielk1977397d65f2008-11-19 11:35:39 +00004191static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
4192 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00004193 return dlsym(pHandle, zSymbol);
4194}
danielk1977397d65f2008-11-19 11:35:39 +00004195static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
4196 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00004197 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00004198}
danielk1977b4b47412007-08-17 15:53:36 +00004199#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
4200 #define unixDlOpen 0
4201 #define unixDlError 0
4202 #define unixDlSym 0
4203 #define unixDlClose 0
4204#endif
4205
4206/*
danielk197790949c22007-08-17 16:50:38 +00004207** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00004208*/
danielk1977397d65f2008-11-19 11:35:39 +00004209static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
4210 UNUSED_PARAMETER(NotUsed);
danielk197700e13612008-11-17 19:18:54 +00004211 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00004212
drhbbd42a62004-05-22 17:41:58 +00004213 /* We have to initialize zBuf to prevent valgrind from reporting
4214 ** errors. The reports issued by valgrind are incorrect - we would
4215 ** prefer that the randomness be increased by making use of the
4216 ** uninitialized space in zBuf - but valgrind errors tend to worry
4217 ** some users. Rather than argue, it seems easier just to initialize
4218 ** the whole array and silence valgrind, even if that means less randomness
4219 ** in the random seed.
4220 **
4221 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00004222 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00004223 ** tests repeatable.
4224 */
danielk1977b4b47412007-08-17 15:53:36 +00004225 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00004226#if !defined(SQLITE_TEST)
4227 {
drh842b8642005-01-21 17:53:17 +00004228 int pid, fd;
4229 fd = open("/dev/urandom", O_RDONLY);
4230 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00004231 time_t t;
4232 time(&t);
danielk197790949c22007-08-17 16:50:38 +00004233 memcpy(zBuf, &t, sizeof(t));
4234 pid = getpid();
4235 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00004236 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00004237 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00004238 }else{
drh72cbd072008-10-14 17:58:38 +00004239 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00004240 close(fd);
4241 }
drhbbd42a62004-05-22 17:41:58 +00004242 }
4243#endif
drh72cbd072008-10-14 17:58:38 +00004244 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00004245}
4246
danielk1977b4b47412007-08-17 15:53:36 +00004247
drhbbd42a62004-05-22 17:41:58 +00004248/*
4249** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00004250** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00004251** The return value is the number of microseconds of sleep actually
4252** requested from the underlying operating system, a number which
4253** might be greater than or equal to the argument, but not less
4254** than the argument.
drhbbd42a62004-05-22 17:41:58 +00004255*/
danielk1977397d65f2008-11-19 11:35:39 +00004256static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
danielk1977c70dfc42008-11-19 13:52:30 +00004257#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004258 struct timespec sp;
4259
4260 sp.tv_sec = microseconds / 1000000;
4261 sp.tv_nsec = (microseconds % 1000000) * 1000;
4262 nanosleep(&sp, NULL);
danielk1977397d65f2008-11-19 11:35:39 +00004263 return microseconds;
4264#elif defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00004265 usleep(microseconds);
4266 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00004267#else
danielk1977b4b47412007-08-17 15:53:36 +00004268 int seconds = (microseconds+999999)/1000000;
4269 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00004270 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00004271#endif
danielk1977397d65f2008-11-19 11:35:39 +00004272 UNUSED_PARAMETER(NotUsed);
drh88f474a2006-01-02 20:00:12 +00004273}
4274
4275/*
drhbbd42a62004-05-22 17:41:58 +00004276** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00004277** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00004278*/
4279#ifdef SQLITE_TEST
4280int sqlite3_current_time = 0;
4281#endif
4282
4283/*
4284** Find the current time (in Universal Coordinated Time). Write the
4285** current time and date as a Julian Day number into *prNow and
4286** return 0. Return 1 if the time and date cannot be found.
4287*/
danielk1977397d65f2008-11-19 11:35:39 +00004288static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
danielk1977c70dfc42008-11-19 13:52:30 +00004289#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004290 struct timespec sNow;
4291 clock_gettime(CLOCK_REALTIME, &sNow);
4292 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
danielk1977397d65f2008-11-19 11:35:39 +00004293#elif defined(NO_GETTOD)
drhbbd42a62004-05-22 17:41:58 +00004294 time_t t;
4295 time(&t);
4296 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00004297#else
4298 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00004299 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00004300 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
4301#endif
danielk1977397d65f2008-11-19 11:35:39 +00004302
drhbbd42a62004-05-22 17:41:58 +00004303#ifdef SQLITE_TEST
4304 if( sqlite3_current_time ){
4305 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
4306 }
4307#endif
danielk1977397d65f2008-11-19 11:35:39 +00004308 UNUSED_PARAMETER(NotUsed);
drhbbd42a62004-05-22 17:41:58 +00004309 return 0;
4310}
danielk1977b4b47412007-08-17 15:53:36 +00004311
danielk1977397d65f2008-11-19 11:35:39 +00004312static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
4313 UNUSED_PARAMETER(NotUsed);
4314 UNUSED_PARAMETER(NotUsed2);
4315 UNUSED_PARAMETER(NotUsed3);
danielk1977bcb97fe2008-06-06 15:49:29 +00004316 return 0;
4317}
4318
drh153c62c2007-08-24 03:51:33 +00004319/*
danielk1977e339d652008-06-28 11:23:00 +00004320** Initialize the operating system interface.
drh153c62c2007-08-24 03:51:33 +00004321*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004322int sqlite3_os_init(void){
danielk1977e339d652008-06-28 11:23:00 +00004323 /* Macro to define the static contents of an sqlite3_vfs structure for
4324 ** the unix backend. The two parameters are the values to use for
4325 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
4326 **
4327 */
4328 #define UNIXVFS(zVfsName, pVfsAppData) { \
4329 1, /* iVersion */ \
4330 sizeof(unixFile), /* szOsFile */ \
4331 MAX_PATHNAME, /* mxPathname */ \
4332 0, /* pNext */ \
4333 zVfsName, /* zName */ \
4334 (void *)pVfsAppData, /* pAppData */ \
4335 unixOpen, /* xOpen */ \
4336 unixDelete, /* xDelete */ \
4337 unixAccess, /* xAccess */ \
4338 unixFullPathname, /* xFullPathname */ \
4339 unixDlOpen, /* xDlOpen */ \
4340 unixDlError, /* xDlError */ \
4341 unixDlSym, /* xDlSym */ \
4342 unixDlClose, /* xDlClose */ \
4343 unixRandomness, /* xRandomness */ \
4344 unixSleep, /* xSleep */ \
4345 unixCurrentTime, /* xCurrentTime */ \
4346 unixGetLastError /* xGetLastError */ \
4347 }
4348
4349 static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
drh40bbb0a2008-09-23 10:23:26 +00004350#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +00004351 int i;
4352 static sqlite3_vfs aVfs[] = {
4353 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
4354 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
4355 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
4356 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
chw97185482008-11-17 08:05:31 +00004357 UNIXVFS("unix-none", LOCKING_STYLE_NONE),
4358 UNIXVFS("unix-namedsem",LOCKING_STYLE_NAMEDSEM),
aswiftaebf4132008-11-21 00:10:35 +00004359 UNIXVFS("unix-proxy", LOCKING_STYLE_PROXY)
drh153c62c2007-08-24 03:51:33 +00004360 };
danielk1977e339d652008-06-28 11:23:00 +00004361 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
4362 sqlite3_vfs_register(&aVfs[i], 0);
4363 }
4364#endif
danielk1977c70dfc42008-11-19 13:52:30 +00004365#if IS_VXWORKS
chw97185482008-11-17 08:05:31 +00004366 sqlite3HashInit(&nameHash, 1);
4367#endif
danielk1977c0fa4c52008-06-25 17:19:00 +00004368 sqlite3_vfs_register(&unixVfs, 1);
4369 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00004370}
danielk1977e339d652008-06-28 11:23:00 +00004371
4372/*
4373** Shutdown the operating system interface. This is a no-op for unix.
4374*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004375int sqlite3_os_end(void){
4376 return SQLITE_OK;
4377}
drhdce8bdb2007-08-16 13:01:44 +00004378
danielk197729bafea2008-06-26 10:41:19 +00004379#endif /* SQLITE_OS_UNIX */