blob: 5c7a8a529290244c2f94d478c990e5f18b88e60b [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**
drh734c9862008-11-28 15:37:20 +000013** This file contains the VFS implementation for unix-like operating systems
14** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
danielk1977822a5162008-05-16 04:51:54 +000015**
drh734c9862008-11-28 15:37:20 +000016** There are actually several different VFS implementations in this file.
17** The differences are in the way that file locking is done. The default
18** implementation uses Posix Advisory Locks. Alternative implementations
19** use flock(), dot-files, various proprietary locking schemas, or simply
20** skip locking all together.
21**
drh9b35ea62008-11-29 02:20:26 +000022** This source file is organized into divisions where the logic for various
drh734c9862008-11-28 15:37:20 +000023** subfunctions is contained within the appropriate division. PLEASE
24** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
25** in the correct division and should be clearly labeled.
26**
drh6b9d6dd2008-12-03 19:34:47 +000027** The layout of divisions is as follows:
drh734c9862008-11-28 15:37:20 +000028**
29** * General-purpose declarations and utility functions.
30** * Unique file ID logic used by VxWorks.
drh715ff302008-12-03 22:32:44 +000031** * Various locking primitive implementations (all except proxy locking):
drh734c9862008-11-28 15:37:20 +000032** + for Posix Advisory Locks
33** + for no-op locks
34** + for dot-file locks
35** + for flock() locking
36** + for named semaphore locks (VxWorks only)
37** + for AFP filesystem locks (MacOSX only)
drh9b35ea62008-11-29 02:20:26 +000038** * sqlite3_file methods not associated with locking.
39** * Definitions of sqlite3_io_methods objects for all locking
40** methods plus "finder" functions for each locking method.
drh6b9d6dd2008-12-03 19:34:47 +000041** * sqlite3_vfs method implementations.
drh715ff302008-12-03 22:32:44 +000042** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
drh9b35ea62008-11-29 02:20:26 +000043** * Definitions of sqlite3_vfs objects for all locking methods
44** plus implementations of sqlite3_os_init() and sqlite3_os_end().
drh734c9862008-11-28 15:37:20 +000045**
drhd2cb50b2009-01-09 21:41:17 +000046** $Id: os_unix.c,v 1.235 2009/01/09 21:41:17 drh Exp $
drhbbd42a62004-05-22 17:41:58 +000047*/
drhbbd42a62004-05-22 17:41:58 +000048#include "sqliteInt.h"
danielk197729bafea2008-06-26 10:41:19 +000049#if SQLITE_OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000050
danielk1977e339d652008-06-28 11:23:00 +000051/*
drh6b9d6dd2008-12-03 19:34:47 +000052** There are various methods for file locking used for concurrency
53** control:
danielk1977e339d652008-06-28 11:23:00 +000054**
drh734c9862008-11-28 15:37:20 +000055** 1. POSIX locking (the default),
56** 2. No locking,
57** 3. Dot-file locking,
58** 4. flock() locking,
59** 5. AFP locking (OSX only),
60** 6. Named POSIX semaphores (VXWorks only),
61** 7. proxy locking. (OSX only)
62**
63** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
64** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
65** selection of the appropriate locking style based on the filesystem
66** where the database is located.
danielk1977e339d652008-06-28 11:23:00 +000067*/
drh40bbb0a2008-09-23 10:23:26 +000068#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
drhd2cb50b2009-01-09 21:41:17 +000069# if defined(__APPLE__)
drh40bbb0a2008-09-23 10:23:26 +000070# define SQLITE_ENABLE_LOCKING_STYLE 1
71# else
72# define SQLITE_ENABLE_LOCKING_STYLE 0
73# endif
74#endif
drhbfe66312006-10-03 17:40:40 +000075
drh9cbe6352005-11-29 03:13:21 +000076/*
drh6c7d5c52008-11-21 20:32:33 +000077** Define the OS_VXWORKS pre-processor macro to 1 if building on
danielk1977397d65f2008-11-19 11:35:39 +000078** vxworks, or 0 otherwise.
79*/
drh6c7d5c52008-11-21 20:32:33 +000080#ifndef OS_VXWORKS
81# if defined(__RTP__) || defined(_WRS_KERNEL)
82# define OS_VXWORKS 1
83# else
84# define OS_VXWORKS 0
85# endif
danielk1977397d65f2008-11-19 11:35:39 +000086#endif
87
88/*
drh9cbe6352005-11-29 03:13:21 +000089** These #defines should enable >2GB file support on Posix if the
90** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000091** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000092**
93** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
94** on the compiler command line. This is necessary if you are compiling
95** on a recent machine (ex: RedHat 7.2) but you want your code to work
96** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
97** without this option, LFS is enable. But LFS does not exist in the kernel
98** in RedHat 6.0, so the code won't work. Hence, for maximum binary
99** portability you should omit LFS.
drh9b35ea62008-11-29 02:20:26 +0000100**
101** The previous paragraph was written in 2005. (This paragraph is written
102** on 2008-11-28.) These days, all Linux kernels support large files, so
103** you should probably leave LFS enabled. But some embedded platforms might
104** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
drh9cbe6352005-11-29 03:13:21 +0000105*/
106#ifndef SQLITE_DISABLE_LFS
107# define _LARGE_FILE 1
108# ifndef _FILE_OFFSET_BITS
109# define _FILE_OFFSET_BITS 64
110# endif
111# define _LARGEFILE_SOURCE 1
112#endif
drhbbd42a62004-05-22 17:41:58 +0000113
drh9cbe6352005-11-29 03:13:21 +0000114/*
115** standard include files.
116*/
117#include <sys/types.h>
118#include <sys/stat.h>
119#include <fcntl.h>
120#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +0000121#include <time.h>
drh19e2d372005-08-29 23:00:03 +0000122#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +0000123#include <errno.h>
danielk1977e339d652008-06-28 11:23:00 +0000124
drh40bbb0a2008-09-23 10:23:26 +0000125#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977c70dfc42008-11-19 13:52:30 +0000126# include <sys/ioctl.h>
drh6c7d5c52008-11-21 20:32:33 +0000127# if OS_VXWORKS
danielk1977c70dfc42008-11-19 13:52:30 +0000128# include <semaphore.h>
129# include <limits.h>
130# else
drh9b35ea62008-11-29 02:20:26 +0000131# include <sys/file.h>
danielk1977c70dfc42008-11-19 13:52:30 +0000132# include <sys/param.h>
133# include <sys/mount.h>
134# endif
drhbfe66312006-10-03 17:40:40 +0000135#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +0000136
137/*
drhf1a221e2006-01-15 17:27:17 +0000138** If we are to be thread-safe, include the pthreads header and define
139** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +0000140*/
drhd677b3d2007-08-20 22:48:41 +0000141#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000142# include <pthread.h>
143# define SQLITE_UNIX_THREADS 1
144#endif
145
146/*
147** Default permissions when creating a new file
148*/
149#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
150# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
151#endif
152
danielk1977b4b47412007-08-17 15:53:36 +0000153/*
aswiftaebf4132008-11-21 00:10:35 +0000154 ** Default permissions when creating auto proxy dir
155 */
156#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
157# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
158#endif
159
160/*
danielk1977b4b47412007-08-17 15:53:36 +0000161** Maximum supported path-length.
162*/
163#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +0000164
drh734c9862008-11-28 15:37:20 +0000165/*
drh734c9862008-11-28 15:37:20 +0000166** Only set the lastErrno if the error code is a real error and not
167** a normal expected return code of SQLITE_BUSY or SQLITE_OK
168*/
169#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
170
drh9cbe6352005-11-29 03:13:21 +0000171
172/*
drh9b35ea62008-11-29 02:20:26 +0000173** The unixFile structure is subclass of sqlite3_file specific to the unix
174** VFS implementations.
drh9cbe6352005-11-29 03:13:21 +0000175*/
drh054889e2005-11-30 03:20:31 +0000176typedef struct unixFile unixFile;
177struct unixFile {
danielk197762079062007-08-15 17:08:46 +0000178 sqlite3_io_methods const *pMethod; /* Always the first entry */
drh6c7d5c52008-11-21 20:32:33 +0000179 struct unixOpenCnt *pOpen; /* Info about all open fd's on this inode */
180 struct unixLockInfo *pLock; /* Info about locks on this inode */
181 int h; /* The file descriptor */
182 int dirfd; /* File descriptor for the directory */
183 unsigned char locktype; /* The type of lock held on this fd */
184 int lastErrno; /* The unix errno from the last I/O error */
drh6c7d5c52008-11-21 20:32:33 +0000185 void *lockingContext; /* Locking style specific state */
drh734c9862008-11-28 15:37:20 +0000186 int openFlags; /* The flags specified at open */
187#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000188 pthread_t tid; /* The thread that "owns" this unixFile */
189#endif
190#if OS_VXWORKS
191 int isDelete; /* Delete on close if true */
drh107886a2008-11-21 22:21:50 +0000192 struct vxworksFileId *pId; /* Unique file ID */
drh6c7d5c52008-11-21 20:32:33 +0000193#endif
danielk1977967a4a12007-08-20 14:23:44 +0000194#ifdef SQLITE_TEST
195 /* In test mode, increase the size of this structure a bit so that
196 ** it is larger than the struct CrashFile defined in test6.c.
197 */
198 char aPadding[32];
199#endif
drh9cbe6352005-11-29 03:13:21 +0000200};
201
drh0ccebe72005-06-07 22:22:50 +0000202/*
drh198bf392006-01-06 21:52:49 +0000203** Include code that is common to all os_*.c files
204*/
205#include "os_common.h"
206
207/*
drh0ccebe72005-06-07 22:22:50 +0000208** Define various macros that are missing from some systems.
209*/
drhbbd42a62004-05-22 17:41:58 +0000210#ifndef O_LARGEFILE
211# define O_LARGEFILE 0
212#endif
213#ifdef SQLITE_DISABLE_LFS
214# undef O_LARGEFILE
215# define O_LARGEFILE 0
216#endif
217#ifndef O_NOFOLLOW
218# define O_NOFOLLOW 0
219#endif
220#ifndef O_BINARY
221# define O_BINARY 0
222#endif
223
224/*
225** The DJGPP compiler environment looks mostly like Unix, but it
226** lacks the fcntl() system call. So redefine fcntl() to be something
227** that always succeeds. This means that locking does not occur under
drh85b623f2007-12-13 21:54:09 +0000228** DJGPP. But it is DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000229*/
230#ifdef __DJGPP__
231# define fcntl(A,B,C) 0
232#endif
233
234/*
drh2b4b5962005-06-15 17:47:55 +0000235** The threadid macro resolves to the thread-id or to 0. Used for
236** testing and debugging only.
237*/
drhd677b3d2007-08-20 22:48:41 +0000238#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000239#define threadid pthread_self()
240#else
241#define threadid 0
242#endif
243
danielk197713adf8a2004-06-03 16:08:41 +0000244
drh107886a2008-11-21 22:21:50 +0000245/*
246** Helper functions to obtain and relinquish the global mutex.
247*/
248static void unixEnterMutex(void){
249 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
250}
251static void unixLeaveMutex(void){
252 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
253}
254
drh734c9862008-11-28 15:37:20 +0000255
256#ifdef SQLITE_DEBUG
257/*
258** Helper function for printing out trace information from debugging
259** binaries. This returns the string represetation of the supplied
260** integer lock-type.
261*/
262static const char *locktypeName(int locktype){
263 switch( locktype ){
264 case NO_LOCK: return "NONE";
265 case SHARED_LOCK: return "SHARED";
266 case RESERVED_LOCK: return "RESERVED";
267 case PENDING_LOCK: return "PENDING";
268 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
269 }
270 return "ERROR";
271}
272#endif
273
274#ifdef SQLITE_LOCK_TRACE
275/*
276** Print out information about all locking operations.
drh6c7d5c52008-11-21 20:32:33 +0000277**
drh734c9862008-11-28 15:37:20 +0000278** This routine is used for troubleshooting locks on multithreaded
279** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
280** command-line option on the compiler. This code is normally
281** turned off.
282*/
283static int lockTrace(int fd, int op, struct flock *p){
284 char *zOpName, *zType;
285 int s;
286 int savedErrno;
287 if( op==F_GETLK ){
288 zOpName = "GETLK";
289 }else if( op==F_SETLK ){
290 zOpName = "SETLK";
291 }else{
292 s = fcntl(fd, op, p);
293 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
294 return s;
295 }
296 if( p->l_type==F_RDLCK ){
297 zType = "RDLCK";
298 }else if( p->l_type==F_WRLCK ){
299 zType = "WRLCK";
300 }else if( p->l_type==F_UNLCK ){
301 zType = "UNLCK";
302 }else{
303 assert( 0 );
304 }
305 assert( p->l_whence==SEEK_SET );
306 s = fcntl(fd, op, p);
307 savedErrno = errno;
308 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
309 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
310 (int)p->l_pid, s);
311 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
312 struct flock l2;
313 l2 = *p;
314 fcntl(fd, F_GETLK, &l2);
315 if( l2.l_type==F_RDLCK ){
316 zType = "RDLCK";
317 }else if( l2.l_type==F_WRLCK ){
318 zType = "WRLCK";
319 }else if( l2.l_type==F_UNLCK ){
320 zType = "UNLCK";
321 }else{
322 assert( 0 );
323 }
324 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
325 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
326 }
327 errno = savedErrno;
328 return s;
329}
330#define fcntl lockTrace
331#endif /* SQLITE_LOCK_TRACE */
332
333
334
335/*
336** This routine translates a standard POSIX errno code into something
337** useful to the clients of the sqlite3 functions. Specifically, it is
338** intended to translate a variety of "try again" errors into SQLITE_BUSY
339** and a variety of "please close the file descriptor NOW" errors into
340** SQLITE_IOERR
341**
342** Errors during initialization of locks, or file system support for locks,
343** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
344*/
345static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
346 switch (posixError) {
347 case 0:
348 return SQLITE_OK;
349
350 case EAGAIN:
351 case ETIMEDOUT:
352 case EBUSY:
353 case EINTR:
354 case ENOLCK:
355 /* random NFS retry error, unless during file system support
356 * introspection, in which it actually means what it says */
357 return SQLITE_BUSY;
358
359 case EACCES:
360 /* EACCES is like EAGAIN during locking operations, but not any other time*/
361 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
362 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
363 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
364 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
365 return SQLITE_BUSY;
366 }
367 /* else fall through */
368 case EPERM:
369 return SQLITE_PERM;
370
371 case EDEADLK:
372 return SQLITE_IOERR_BLOCKED;
373
374#if EOPNOTSUPP!=ENOTSUP
375 case EOPNOTSUPP:
376 /* something went terribly awry, unless during file system support
377 * introspection, in which it actually means what it says */
378#endif
379#ifdef ENOTSUP
380 case ENOTSUP:
381 /* invalid fd, unless during file system support introspection, in which
382 * it actually means what it says */
383#endif
384 case EIO:
385 case EBADF:
386 case EINVAL:
387 case ENOTCONN:
388 case ENODEV:
389 case ENXIO:
390 case ENOENT:
391 case ESTALE:
392 case ENOSYS:
393 /* these should force the client to close the file and reconnect */
394
395 default:
396 return sqliteIOErr;
397 }
398}
399
400
401
402/******************************************************************************
403****************** Begin Unique File ID Utility Used By VxWorks ***************
404**
405** On most versions of unix, we can get a unique ID for a file by concatenating
406** the device number and the inode number. But this does not work on VxWorks.
407** On VxWorks, a unique file id must be based on the canonical filename.
408**
409** A pointer to an instance of the following structure can be used as a
410** unique file ID in VxWorks. Each instance of this structure contains
411** a copy of the canonical filename. There is also a reference count.
412** The structure is reclaimed when the number of pointers to it drops to
413** zero.
414**
415** There are never very many files open at one time and lookups are not
416** a performance-critical path, so it is sufficient to put these
417** structures on a linked list.
418*/
419struct vxworksFileId {
420 struct vxworksFileId *pNext; /* Next in a list of them all */
421 int nRef; /* Number of references to this one */
422 int nName; /* Length of the zCanonicalName[] string */
423 char *zCanonicalName; /* Canonical filename */
424};
425
426#if OS_VXWORKS
427/*
drh9b35ea62008-11-29 02:20:26 +0000428** All unique filenames are held on a linked list headed by this
drh734c9862008-11-28 15:37:20 +0000429** variable:
430*/
431static struct vxworksFileId *vxworksFileList = 0;
432
433/*
434** Simplify a filename into its canonical form
435** by making the following changes:
436**
437** * removing any trailing and duplicate /
drh9b35ea62008-11-29 02:20:26 +0000438** * convert /./ into just /
439** * convert /A/../ where A is any simple name into just /
drh734c9862008-11-28 15:37:20 +0000440**
441** Changes are made in-place. Return the new name length.
442**
443** The original filename is in z[0..n-1]. Return the number of
444** characters in the simplified name.
445*/
446static int vxworksSimplifyName(char *z, int n){
447 int i, j;
448 while( n>1 && z[n-1]=='/' ){ n--; }
449 for(i=j=0; i<n; i++){
450 if( z[i]=='/' ){
451 if( z[i+1]=='/' ) continue;
452 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
453 i += 1;
454 continue;
455 }
456 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
457 while( j>0 && z[j-1]!='/' ){ j--; }
458 if( j>0 ){ j--; }
459 i += 2;
460 continue;
461 }
462 }
463 z[j++] = z[i];
464 }
465 z[j] = 0;
466 return j;
467}
468
469/*
470** Find a unique file ID for the given absolute pathname. Return
471** a pointer to the vxworksFileId object. This pointer is the unique
472** file ID.
473**
474** The nRef field of the vxworksFileId object is incremented before
475** the object is returned. A new vxworksFileId object is created
476** and added to the global list if necessary.
477**
478** If a memory allocation error occurs, return NULL.
479*/
480static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
481 struct vxworksFileId *pNew; /* search key and new file ID */
482 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
483 int n; /* Length of zAbsoluteName string */
484
485 assert( zAbsoluteName[0]=='/' );
drhea678832008-12-10 19:26:22 +0000486 n = (int)strlen(zAbsoluteName);
drh734c9862008-11-28 15:37:20 +0000487 pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
488 if( pNew==0 ) return 0;
489 pNew->zCanonicalName = (char*)&pNew[1];
490 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
491 n = vxworksSimplifyName(pNew->zCanonicalName, n);
492
493 /* Search for an existing entry that matching the canonical name.
494 ** If found, increment the reference count and return a pointer to
495 ** the existing file ID.
496 */
497 unixEnterMutex();
498 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
499 if( pCandidate->nName==n
500 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
501 ){
502 sqlite3_free(pNew);
503 pCandidate->nRef++;
504 unixLeaveMutex();
505 return pCandidate;
506 }
507 }
508
509 /* No match was found. We will make a new file ID */
510 pNew->nRef = 1;
511 pNew->nName = n;
512 pNew->pNext = vxworksFileList;
513 vxworksFileList = pNew;
514 unixLeaveMutex();
515 return pNew;
516}
517
518/*
519** Decrement the reference count on a vxworksFileId object. Free
520** the object when the reference count reaches zero.
521*/
522static void vxworksReleaseFileId(struct vxworksFileId *pId){
523 unixEnterMutex();
524 assert( pId->nRef>0 );
525 pId->nRef--;
526 if( pId->nRef==0 ){
527 struct vxworksFileId **pp;
528 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
529 assert( *pp==pId );
530 *pp = pId->pNext;
531 sqlite3_free(pId);
532 }
533 unixLeaveMutex();
534}
535#endif /* OS_VXWORKS */
536/*************** End of Unique File ID Utility Used By VxWorks ****************
537******************************************************************************/
538
539
540/******************************************************************************
541*************************** Posix Advisory Locking ****************************
542**
drh9b35ea62008-11-29 02:20:26 +0000543** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
drhbbd42a62004-05-22 17:41:58 +0000544** section 6.5.2.2 lines 483 through 490 specify that when a process
545** sets or clears a lock, that operation overrides any prior locks set
546** by the same process. It does not explicitly say so, but this implies
547** that it overrides locks set by the same process using a different
548** file descriptor. Consider this test case:
drh6c7d5c52008-11-21 20:32:33 +0000549**
550** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
drhbbd42a62004-05-22 17:41:58 +0000551** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
552**
553** Suppose ./file1 and ./file2 are really the same file (because
554** one is a hard or symbolic link to the other) then if you set
555** an exclusive lock on fd1, then try to get an exclusive lock
556** on fd2, it works. I would have expected the second lock to
557** fail since there was already a lock on the file due to fd1.
558** But not so. Since both locks came from the same process, the
559** second overrides the first, even though they were on different
560** file descriptors opened on different file names.
561**
drh734c9862008-11-28 15:37:20 +0000562** This means that we cannot use POSIX locks to synchronize file access
563** among competing threads of the same process. POSIX locks will work fine
drhbbd42a62004-05-22 17:41:58 +0000564** to synchronize access for threads in separate processes, but not
565** threads within the same process.
566**
567** To work around the problem, SQLite has to manage file locks internally
568** on its own. Whenever a new database is opened, we have to find the
569** specific inode of the database file (the inode is determined by the
570** st_dev and st_ino fields of the stat structure that fstat() fills in)
571** and check for locks already existing on that inode. When locks are
572** created or removed, we have to look at our own internal record of the
573** locks to see if another thread has previously set a lock on that same
574** inode.
575**
drh9b35ea62008-11-29 02:20:26 +0000576** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
577** For VxWorks, we have to use the alternative unique ID system based on
578** canonical filename and implemented in the previous division.)
579**
danielk1977ad94b582007-08-20 06:44:22 +0000580** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000581** descriptor. It is now a structure that holds the integer file
582** descriptor and a pointer to a structure that describes the internal
583** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000584** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000585** point to the same locking structure. The locking structure keeps
586** a reference count (so we will know when to delete it) and a "cnt"
587** field that tells us its internal lock status. cnt==0 means the
588** file is unlocked. cnt==-1 means the file has an exclusive lock.
589** cnt>0 means there are cnt shared locks on the file.
590**
591** Any attempt to lock or unlock a file first checks the locking
592** structure. The fcntl() system call is only invoked to set a
593** POSIX lock if the internal lock structure transitions between
594** a locked and an unlocked state.
595**
drh734c9862008-11-28 15:37:20 +0000596** But wait: there are yet more problems with POSIX advisory locks.
drhbbd42a62004-05-22 17:41:58 +0000597**
598** If you close a file descriptor that points to a file that has locks,
599** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000600** released. To work around this problem, each unixFile structure contains
drh6c7d5c52008-11-21 20:32:33 +0000601** a pointer to an unixOpenCnt structure. There is one unixOpenCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000602** per open inode, which means that multiple unixFile can point to a single
drh6c7d5c52008-11-21 20:32:33 +0000603** unixOpenCnt. When an attempt is made to close an unixFile, if there are
danielk1977ad94b582007-08-20 06:44:22 +0000604** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000605** to close() the file descriptor is deferred until all of the locks clear.
drh6c7d5c52008-11-21 20:32:33 +0000606** The unixOpenCnt structure keeps a list of file descriptors that need to
drhbbd42a62004-05-22 17:41:58 +0000607** be closed and that list is walked (and cleared) when the last lock
608** clears.
609**
drh9b35ea62008-11-29 02:20:26 +0000610** Yet another problem: LinuxThreads do not play well with posix locks.
drh5fdae772004-06-29 03:29:00 +0000611**
drh9b35ea62008-11-29 02:20:26 +0000612** Many older versions of linux use the LinuxThreads library which is
613** not posix compliant. Under LinuxThreads, a lock created by thread
drh734c9862008-11-28 15:37:20 +0000614** A cannot be modified or overridden by a different thread B.
615** Only thread A can modify the lock. Locking behavior is correct
616** if the appliation uses the newer Native Posix Thread Library (NPTL)
617** on linux - with NPTL a lock created by thread A can override locks
618** in thread B. But there is no way to know at compile-time which
619** threading library is being used. So there is no way to know at
620** compile-time whether or not thread A can override locks on thread B.
621** We have to do a run-time check to discover the behavior of the
622** current process.
drh5fdae772004-06-29 03:29:00 +0000623**
drh734c9862008-11-28 15:37:20 +0000624** On systems where thread A is unable to modify locks created by
625** thread B, we have to keep track of which thread created each
drh9b35ea62008-11-29 02:20:26 +0000626** lock. Hence there is an extra field in the key to the unixLockInfo
drh734c9862008-11-28 15:37:20 +0000627** structure to record this information. And on those systems it
628** is illegal to begin a transaction in one thread and finish it
629** in another. For this latter restriction, there is no work-around.
630** It is a limitation of LinuxThreads.
drhbbd42a62004-05-22 17:41:58 +0000631*/
632
633/*
drh6c7d5c52008-11-21 20:32:33 +0000634** Set or check the unixFile.tid field. This field is set when an unixFile
635** is first opened. All subsequent uses of the unixFile verify that the
636** same thread is operating on the unixFile. Some operating systems do
637** not allow locks to be overridden by other threads and that restriction
638** means that sqlite3* database handles cannot be moved from one thread
drh734c9862008-11-28 15:37:20 +0000639** to another while locks are held.
drh6c7d5c52008-11-21 20:32:33 +0000640**
641** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
642** another as long as we are running on a system that supports threads
drh734c9862008-11-28 15:37:20 +0000643** overriding each others locks (which is now the most common behavior)
drh6c7d5c52008-11-21 20:32:33 +0000644** or if no locks are held. But the unixFile.pLock field needs to be
645** recomputed because its key includes the thread-id. See the
646** transferOwnership() function below for additional information
647*/
drh734c9862008-11-28 15:37:20 +0000648#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000649# define SET_THREADID(X) (X)->tid = pthread_self()
650# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
651 !pthread_equal((X)->tid, pthread_self()))
652#else
653# define SET_THREADID(X)
654# define CHECK_THREADID(X) 0
655#endif
656
657/*
drhbbd42a62004-05-22 17:41:58 +0000658** An instance of the following structure serves as the key used
drh6c7d5c52008-11-21 20:32:33 +0000659** to locate a particular unixOpenCnt structure given its inode. This
660** is the same as the unixLockKey except that the thread ID is omitted.
661*/
662struct unixFileId {
drh107886a2008-11-21 22:21:50 +0000663 dev_t dev; /* Device number */
drh6c7d5c52008-11-21 20:32:33 +0000664#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +0000665 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
drh6c7d5c52008-11-21 20:32:33 +0000666#else
drh107886a2008-11-21 22:21:50 +0000667 ino_t ino; /* Inode number */
drh6c7d5c52008-11-21 20:32:33 +0000668#endif
669};
670
671/*
672** An instance of the following structure serves as the key used
673** to locate a particular unixLockInfo structure given its inode.
drh5fdae772004-06-29 03:29:00 +0000674**
drh734c9862008-11-28 15:37:20 +0000675** If threads cannot override each others locks (LinuxThreads), then we
676** set the unixLockKey.tid field to the thread ID. If threads can override
677** each others locks (Posix and NPTL) then tid is always set to zero.
678** tid is omitted if we compile without threading support or on an OS
679** other than linux.
drhbbd42a62004-05-22 17:41:58 +0000680*/
drh6c7d5c52008-11-21 20:32:33 +0000681struct unixLockKey {
682 struct unixFileId fid; /* Unique identifier for the file */
drh734c9862008-11-28 15:37:20 +0000683#if SQLITE_THREADSAFE && defined(__linux__)
684 pthread_t tid; /* Thread ID of lock owner. Zero if not using LinuxThreads */
drh5fdae772004-06-29 03:29:00 +0000685#endif
drhbbd42a62004-05-22 17:41:58 +0000686};
687
688/*
689** An instance of the following structure is allocated for each open
drh9b35ea62008-11-29 02:20:26 +0000690** inode. Or, on LinuxThreads, there is one of these structures for
691** each inode opened by each thread.
drhbbd42a62004-05-22 17:41:58 +0000692**
danielk1977ad94b582007-08-20 06:44:22 +0000693** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000694** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000695** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000696*/
drh6c7d5c52008-11-21 20:32:33 +0000697struct unixLockInfo {
drh734c9862008-11-28 15:37:20 +0000698 struct unixLockKey lockKey; /* The lookup key */
699 int cnt; /* Number of SHARED locks held */
700 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
701 int nRef; /* Number of pointers to this structure */
702 struct unixLockInfo *pNext; /* List of all unixLockInfo objects */
703 struct unixLockInfo *pPrev; /* .... doubly linked */
drhbbd42a62004-05-22 17:41:58 +0000704};
705
706/*
707** An instance of the following structure is allocated for each open
708** inode. This structure keeps track of the number of locks on that
709** inode. If a close is attempted against an inode that is holding
710** locks, the close is deferred until all locks clear by adding the
711** file descriptor to be closed to the pending list.
drh9b35ea62008-11-29 02:20:26 +0000712**
713** TODO: Consider changing this so that there is only a single file
714** descriptor for each open file, even when it is opened multiple times.
715** The close() system call would only occur when the last database
716** using the file closes.
drhbbd42a62004-05-22 17:41:58 +0000717*/
drh6c7d5c52008-11-21 20:32:33 +0000718struct unixOpenCnt {
719 struct unixFileId fileId; /* The lookup key */
720 int nRef; /* Number of pointers to this structure */
721 int nLock; /* Number of outstanding locks */
722 int nPending; /* Number of pending close() operations */
723 int *aPending; /* Malloced space holding fd's awaiting a close() */
724#if OS_VXWORKS
725 sem_t *pSem; /* Named POSIX semaphore */
chw97185482008-11-17 08:05:31 +0000726 char aSemName[MAX_PATHNAME+1]; /* Name of that semaphore */
727#endif
drh6c7d5c52008-11-21 20:32:33 +0000728 struct unixOpenCnt *pNext, *pPrev; /* List of all unixOpenCnt objects */
drhbbd42a62004-05-22 17:41:58 +0000729};
730
drhda0e7682008-07-30 15:27:54 +0000731/*
drh9b35ea62008-11-29 02:20:26 +0000732** Lists of all unixLockInfo and unixOpenCnt objects. These used to be hash
733** tables. But the number of objects is rarely more than a dozen and
drhda0e7682008-07-30 15:27:54 +0000734** never exceeds a few thousand. And lookup is not on a critical
drh6c7d5c52008-11-21 20:32:33 +0000735** path so a simple linked list will suffice.
drhbbd42a62004-05-22 17:41:58 +0000736*/
drh6c7d5c52008-11-21 20:32:33 +0000737static struct unixLockInfo *lockList = 0;
738static struct unixOpenCnt *openList = 0;
drh5fdae772004-06-29 03:29:00 +0000739
drh5fdae772004-06-29 03:29:00 +0000740/*
drh9b35ea62008-11-29 02:20:26 +0000741** This variable remembers whether or not threads can override each others
drh5fdae772004-06-29 03:29:00 +0000742** locks.
743**
drh9b35ea62008-11-29 02:20:26 +0000744** 0: No. Threads cannot override each others locks. (LinuxThreads)
745** 1: Yes. Threads can override each others locks. (Posix & NLPT)
drh5fdae772004-06-29 03:29:00 +0000746** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000747**
drh5062d3a2006-01-31 23:03:35 +0000748** On some systems, we know at compile-time if threads can override each
749** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
750** will be set appropriately. On other systems, we have to check at
751** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
752** undefined.
753**
drhf1a221e2006-01-15 17:27:17 +0000754** This variable normally has file scope only. But during testing, we make
755** it a global so that the test code can change its value in order to verify
756** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000757*/
drh715ff302008-12-03 22:32:44 +0000758#if SQLITE_THREADSAFE && defined(__linux__)
759# ifndef SQLITE_THREAD_OVERRIDE_LOCK
760# define SQLITE_THREAD_OVERRIDE_LOCK -1
761# endif
762# ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000763int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh715ff302008-12-03 22:32:44 +0000764# else
drh5062d3a2006-01-31 23:03:35 +0000765static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh715ff302008-12-03 22:32:44 +0000766# endif
drh029b44b2006-01-15 00:13:15 +0000767#endif
drh5fdae772004-06-29 03:29:00 +0000768
769/*
770** This structure holds information passed into individual test
771** threads by the testThreadLockingBehavior() routine.
772*/
773struct threadTestData {
774 int fd; /* File to be locked */
775 struct flock lock; /* The locking operation */
776 int result; /* Result of the locking operation */
777};
778
drh6c7d5c52008-11-21 20:32:33 +0000779#if SQLITE_THREADSAFE && defined(__linux__)
drh5fdae772004-06-29 03:29:00 +0000780/*
danielk197741a6a612008-11-11 18:34:35 +0000781** This function is used as the main routine for a thread launched by
782** testThreadLockingBehavior(). It tests whether the shared-lock obtained
783** by the main thread in testThreadLockingBehavior() conflicts with a
784** hypothetical write-lock obtained by this thread on the same file.
785**
786** The write-lock is not actually acquired, as this is not possible if
787** the file is open in read-only mode (see ticket #3472).
788*/
drh5fdae772004-06-29 03:29:00 +0000789static void *threadLockingTest(void *pArg){
790 struct threadTestData *pData = (struct threadTestData*)pArg;
danielk197741a6a612008-11-11 18:34:35 +0000791 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
drh5fdae772004-06-29 03:29:00 +0000792 return pArg;
793}
drh6c7d5c52008-11-21 20:32:33 +0000794#endif /* SQLITE_THREADSAFE && defined(__linux__) */
drh5fdae772004-06-29 03:29:00 +0000795
drh6c7d5c52008-11-21 20:32:33 +0000796
797#if SQLITE_THREADSAFE && defined(__linux__)
drh5fdae772004-06-29 03:29:00 +0000798/*
799** This procedure attempts to determine whether or not threads
800** can override each others locks then sets the
801** threadsOverrideEachOthersLocks variable appropriately.
802*/
danielk19774d5238f2006-01-27 06:32:00 +0000803static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000804 int fd;
danielk197741a6a612008-11-11 18:34:35 +0000805 int rc;
806 struct threadTestData d;
807 struct flock l;
808 pthread_t t;
drh5fdae772004-06-29 03:29:00 +0000809
810 fd = dup(fd_orig);
811 if( fd<0 ) return;
danielk197741a6a612008-11-11 18:34:35 +0000812 memset(&l, 0, sizeof(l));
813 l.l_type = F_RDLCK;
814 l.l_len = 1;
815 l.l_start = 0;
816 l.l_whence = SEEK_SET;
817 rc = fcntl(fd_orig, F_SETLK, &l);
818 if( rc!=0 ) return;
819 memset(&d, 0, sizeof(d));
820 d.fd = fd;
821 d.lock = l;
822 d.lock.l_type = F_WRLCK;
823 pthread_create(&t, 0, threadLockingTest, &d);
824 pthread_join(t, 0);
drh5fdae772004-06-29 03:29:00 +0000825 close(fd);
danielk197741a6a612008-11-11 18:34:35 +0000826 if( d.result!=0 ) return;
827 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
drh5fdae772004-06-29 03:29:00 +0000828}
drh6c7d5c52008-11-21 20:32:33 +0000829#endif /* SQLITE_THERADSAFE && defined(__linux__) */
drh5fdae772004-06-29 03:29:00 +0000830
drhbbd42a62004-05-22 17:41:58 +0000831/*
drh6c7d5c52008-11-21 20:32:33 +0000832** Release a unixLockInfo structure previously allocated by findLockInfo().
833*/
834static void releaseLockInfo(struct unixLockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000835 if( pLock ){
836 pLock->nRef--;
837 if( pLock->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000838 if( pLock->pPrev ){
839 assert( pLock->pPrev->pNext==pLock );
840 pLock->pPrev->pNext = pLock->pNext;
841 }else{
842 assert( lockList==pLock );
843 lockList = pLock->pNext;
844 }
845 if( pLock->pNext ){
846 assert( pLock->pNext->pPrev==pLock );
847 pLock->pNext->pPrev = pLock->pPrev;
848 }
danielk1977e339d652008-06-28 11:23:00 +0000849 sqlite3_free(pLock);
850 }
drhbbd42a62004-05-22 17:41:58 +0000851 }
852}
853
854/*
drh6c7d5c52008-11-21 20:32:33 +0000855** Release a unixOpenCnt structure previously allocated by findLockInfo().
drhbbd42a62004-05-22 17:41:58 +0000856*/
drh6c7d5c52008-11-21 20:32:33 +0000857static void releaseOpenCnt(struct unixOpenCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000858 if( pOpen ){
859 pOpen->nRef--;
860 if( pOpen->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000861 if( pOpen->pPrev ){
862 assert( pOpen->pPrev->pNext==pOpen );
863 pOpen->pPrev->pNext = pOpen->pNext;
864 }else{
865 assert( openList==pOpen );
866 openList = pOpen->pNext;
867 }
868 if( pOpen->pNext ){
869 assert( pOpen->pNext->pPrev==pOpen );
870 pOpen->pNext->pPrev = pOpen->pPrev;
871 }
872 sqlite3_free(pOpen->aPending);
danielk1977e339d652008-06-28 11:23:00 +0000873 sqlite3_free(pOpen);
874 }
drhbbd42a62004-05-22 17:41:58 +0000875 }
876}
877
drh6c7d5c52008-11-21 20:32:33 +0000878/*
879** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that
880** describes that file descriptor. Create new ones if necessary. The
881** return values might be uninitialized if an error occurs.
882**
883** Return an appropriate error code.
884*/
885static int findLockInfo(
886 unixFile *pFile, /* Unix file with file desc used in the key */
887 struct unixLockInfo **ppLock, /* Return the unixLockInfo structure here */
888 struct unixOpenCnt **ppOpen /* Return the unixOpenCnt structure here */
889){
890 int rc; /* System call return code */
891 int fd; /* The file descriptor for pFile */
892 struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */
893 struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */
894 struct stat statbuf; /* Low-level file information */
895 struct unixLockInfo *pLock; /* Candidate unixLockInfo object */
896 struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */
897
898 /* Get low-level information about the file that we can used to
899 ** create a unique name for the file.
900 */
901 fd = pFile->h;
902 rc = fstat(fd, &statbuf);
903 if( rc!=0 ){
904 pFile->lastErrno = errno;
905#ifdef EOVERFLOW
906 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
907#endif
908 return SQLITE_IOERR;
909 }
910
911 /* On OS X on an msdos filesystem, the inode number is reported
912 ** incorrectly for zero-size files. See ticket #3260. To work
913 ** around this problem (we consider it a bug in OS X, not SQLite)
914 ** we always increase the file size to 1 by writing a single byte
915 ** prior to accessing the inode number. The one byte written is
916 ** an ASCII 'S' character which also happens to be the first byte
917 ** in the header of every SQLite database. In this way, if there
918 ** is a race condition such that another thread has already populated
919 ** the first page of the database, no damage is done.
920 */
921 if( statbuf.st_size==0 ){
922 write(fd, "S", 1);
923 rc = fstat(fd, &statbuf);
924 if( rc!=0 ){
925 pFile->lastErrno = errno;
926 return SQLITE_IOERR;
927 }
928 }
929
930 memset(&lockKey, 0, sizeof(lockKey));
931 lockKey.fid.dev = statbuf.st_dev;
932#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +0000933 lockKey.fid.pId = pFile->pId;
drh6c7d5c52008-11-21 20:32:33 +0000934#else
935 lockKey.fid.ino = statbuf.st_ino;
936#endif
drh734c9862008-11-28 15:37:20 +0000937#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000938 if( threadsOverrideEachOthersLocks<0 ){
939 testThreadLockingBehavior(fd);
940 }
941 lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
942#endif
943 fileId = lockKey.fid;
944 if( ppLock!=0 ){
945 pLock = lockList;
946 while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){
947 pLock = pLock->pNext;
948 }
949 if( pLock==0 ){
950 pLock = sqlite3_malloc( sizeof(*pLock) );
951 if( pLock==0 ){
952 rc = SQLITE_NOMEM;
953 goto exit_findlockinfo;
954 }
955 pLock->lockKey = lockKey;
956 pLock->nRef = 1;
957 pLock->cnt = 0;
958 pLock->locktype = 0;
959 pLock->pNext = lockList;
960 pLock->pPrev = 0;
961 if( lockList ) lockList->pPrev = pLock;
962 lockList = pLock;
963 }else{
964 pLock->nRef++;
965 }
966 *ppLock = pLock;
967 }
968 if( ppOpen!=0 ){
969 pOpen = openList;
970 while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){
971 pOpen = pOpen->pNext;
972 }
973 if( pOpen==0 ){
974 pOpen = sqlite3_malloc( sizeof(*pOpen) );
975 if( pOpen==0 ){
976 releaseLockInfo(pLock);
977 rc = SQLITE_NOMEM;
978 goto exit_findlockinfo;
979 }
980 pOpen->fileId = fileId;
981 pOpen->nRef = 1;
982 pOpen->nLock = 0;
983 pOpen->nPending = 0;
984 pOpen->aPending = 0;
985 pOpen->pNext = openList;
986 pOpen->pPrev = 0;
987 if( openList ) openList->pPrev = pOpen;
988 openList = pOpen;
989#if OS_VXWORKS
990 pOpen->pSem = NULL;
991 pOpen->aSemName[0] = '\0';
992#endif
993 }else{
994 pOpen->nRef++;
995 }
996 *ppOpen = pOpen;
997 }
998
999exit_findlockinfo:
1000 return rc;
1001}
drh6c7d5c52008-11-21 20:32:33 +00001002
drh7708e972008-11-29 00:56:52 +00001003/*
1004** If we are currently in a different thread than the thread that the
1005** unixFile argument belongs to, then transfer ownership of the unixFile
1006** over to the current thread.
1007**
1008** A unixFile is only owned by a thread on systems that use LinuxThreads.
1009**
1010** Ownership transfer is only allowed if the unixFile is currently unlocked.
1011** If the unixFile is locked and an ownership is wrong, then return
1012** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
1013*/
1014#if SQLITE_THREADSAFE && defined(__linux__)
1015static int transferOwnership(unixFile *pFile){
1016 int rc;
1017 pthread_t hSelf;
1018 if( threadsOverrideEachOthersLocks ){
1019 /* Ownership transfers not needed on this system */
1020 return SQLITE_OK;
1021 }
1022 hSelf = pthread_self();
1023 if( pthread_equal(pFile->tid, hSelf) ){
1024 /* We are still in the same thread */
1025 OSTRACE1("No-transfer, same thread\n");
1026 return SQLITE_OK;
1027 }
1028 if( pFile->locktype!=NO_LOCK ){
1029 /* We cannot change ownership while we are holding a lock! */
1030 return SQLITE_MISUSE;
1031 }
1032 OSTRACE4("Transfer ownership of %d from %d to %d\n",
1033 pFile->h, pFile->tid, hSelf);
1034 pFile->tid = hSelf;
1035 if (pFile->pLock != NULL) {
1036 releaseLockInfo(pFile->pLock);
1037 rc = findLockInfo(pFile, &pFile->pLock, 0);
1038 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
1039 locktypeName(pFile->locktype),
1040 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
1041 return rc;
1042 } else {
1043 return SQLITE_OK;
1044 }
1045}
1046#else /* if not SQLITE_THREADSAFE */
1047 /* On single-threaded builds, ownership transfer is a no-op */
1048# define transferOwnership(X) SQLITE_OK
1049#endif /* SQLITE_THREADSAFE */
1050
aswift5b1a2562008-08-22 00:22:35 +00001051
1052/*
danielk197713adf8a2004-06-03 16:08:41 +00001053** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001054** file by this or any other process. If such a lock is held, set *pResOut
1055** to a non-zero value otherwise *pResOut is set to zero. The return value
1056** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001057*/
danielk1977861f7452008-06-05 11:39:11 +00001058static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001059 int rc = SQLITE_OK;
1060 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001061 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001062
danielk1977861f7452008-06-05 11:39:11 +00001063 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1064
drh054889e2005-11-30 03:20:31 +00001065 assert( pFile );
drh6c7d5c52008-11-21 20:32:33 +00001066 unixEnterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001067
1068 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001069 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001070 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001071 }
1072
drh2ac3ee92004-06-07 16:27:46 +00001073 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001074 */
aswift5b1a2562008-08-22 00:22:35 +00001075 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001076 struct flock lock;
1077 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001078 lock.l_start = RESERVED_BYTE;
1079 lock.l_len = 1;
1080 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001081 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1082 int tErrno = errno;
1083 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1084 pFile->lastErrno = tErrno;
1085 } else if( lock.l_type!=F_UNLCK ){
1086 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001087 }
1088 }
1089
drh6c7d5c52008-11-21 20:32:33 +00001090 unixLeaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001091 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001092
aswift5b1a2562008-08-22 00:22:35 +00001093 *pResOut = reserved;
1094 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001095}
1096
1097/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001098** Lock the file with the lock specified by parameter locktype - one
1099** of the following:
1100**
drh2ac3ee92004-06-07 16:27:46 +00001101** (1) SHARED_LOCK
1102** (2) RESERVED_LOCK
1103** (3) PENDING_LOCK
1104** (4) EXCLUSIVE_LOCK
1105**
drhb3e04342004-06-08 00:47:47 +00001106** Sometimes when requesting one lock state, additional lock states
1107** are inserted in between. The locking might fail on one of the later
1108** transitions leaving the lock state different from what it started but
1109** still short of its goal. The following chart shows the allowed
1110** transitions and the inserted intermediate states:
1111**
1112** UNLOCKED -> SHARED
1113** SHARED -> RESERVED
1114** SHARED -> (PENDING) -> EXCLUSIVE
1115** RESERVED -> (PENDING) -> EXCLUSIVE
1116** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001117**
drha6abd042004-06-09 17:37:22 +00001118** This routine will only increase a lock. Use the sqlite3OsUnlock()
1119** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001120*/
danielk197762079062007-08-15 17:08:46 +00001121static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001122 /* The following describes the implementation of the various locks and
1123 ** lock transitions in terms of the POSIX advisory shared and exclusive
1124 ** lock primitives (called read-locks and write-locks below, to avoid
1125 ** confusion with SQLite lock names). The algorithms are complicated
1126 ** slightly in order to be compatible with windows systems simultaneously
1127 ** accessing the same database file, in case that is ever required.
1128 **
1129 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1130 ** byte', each single bytes at well known offsets, and the 'shared byte
1131 ** range', a range of 510 bytes at a well known offset.
1132 **
1133 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1134 ** byte'. If this is successful, a random byte from the 'shared byte
1135 ** range' is read-locked and the lock on the 'pending byte' released.
1136 **
danielk197790ba3bd2004-06-25 08:32:25 +00001137 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1138 ** A RESERVED lock is implemented by grabbing a write-lock on the
1139 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001140 **
1141 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001142 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1143 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1144 ** obtained, but existing SHARED locks are allowed to persist. A process
1145 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1146 ** This property is used by the algorithm for rolling back a journal file
1147 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001148 **
danielk197790ba3bd2004-06-25 08:32:25 +00001149 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1150 ** implemented by obtaining a write-lock on the entire 'shared byte
1151 ** range'. Since all other locks require a read-lock on one of the bytes
1152 ** within this range, this ensures that no other locks are held on the
1153 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001154 **
1155 ** The reason a single byte cannot be used instead of the 'shared byte
1156 ** range' is that some versions of windows do not support read-locks. By
1157 ** locking a random byte from a range, concurrent SHARED locks may exist
1158 ** even if the locking primitive used is always a write-lock.
1159 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001160 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001161 unixFile *pFile = (unixFile*)id;
drh6c7d5c52008-11-21 20:32:33 +00001162 struct unixLockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001163 struct flock lock;
1164 int s;
1165
drh054889e2005-11-30 03:20:31 +00001166 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001167 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001168 locktypeName(locktype), locktypeName(pFile->locktype),
1169 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001170
1171 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001172 ** unixFile, do nothing. Don't use the end_lock: exit path, as
drh6c7d5c52008-11-21 20:32:33 +00001173 ** unixEnterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001174 */
drh054889e2005-11-30 03:20:31 +00001175 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001176 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001177 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001178 return SQLITE_OK;
1179 }
1180
drhb3e04342004-06-08 00:47:47 +00001181 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001182 */
drh054889e2005-11-30 03:20:31 +00001183 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001184 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001185 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001186
drh054889e2005-11-30 03:20:31 +00001187 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001188 */
drh6c7d5c52008-11-21 20:32:33 +00001189 unixEnterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001190
drh029b44b2006-01-15 00:13:15 +00001191 /* Make sure the current thread owns the pFile.
1192 */
1193 rc = transferOwnership(pFile);
1194 if( rc!=SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00001195 unixLeaveMutex();
drh029b44b2006-01-15 00:13:15 +00001196 return rc;
1197 }
drh64b1bea2006-01-15 02:30:57 +00001198 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001199
danielk1977ad94b582007-08-20 06:44:22 +00001200 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001201 ** handle that precludes the requested lock, return BUSY.
1202 */
drh054889e2005-11-30 03:20:31 +00001203 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001204 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001205 ){
1206 rc = SQLITE_BUSY;
1207 goto end_lock;
1208 }
1209
1210 /* If a SHARED lock is requested, and some thread using this PID already
1211 ** has a SHARED or RESERVED lock, then increment reference counts and
1212 ** return SQLITE_OK.
1213 */
1214 if( locktype==SHARED_LOCK &&
1215 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1216 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001217 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001218 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001219 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001220 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001221 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001222 goto end_lock;
1223 }
1224
danielk197713adf8a2004-06-03 16:08:41 +00001225 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001226
danielk19779a1d0ab2004-06-01 14:09:28 +00001227 lock.l_whence = SEEK_SET;
1228
drh3cde3bb2004-06-12 02:17:14 +00001229 /* A PENDING lock is needed before acquiring a SHARED lock and before
1230 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1231 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001232 */
drh3cde3bb2004-06-12 02:17:14 +00001233 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001234 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001235 ){
danielk1977489468c2004-06-28 08:25:47 +00001236 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001237 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001238 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001239 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001240 int tErrno = errno;
1241 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1242 if( IS_LOCK_ERROR(rc) ){
1243 pFile->lastErrno = tErrno;
1244 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001245 goto end_lock;
1246 }
drh3cde3bb2004-06-12 02:17:14 +00001247 }
1248
1249
1250 /* If control gets to this point, then actually go ahead and make
1251 ** operating system calls for the specified lock.
1252 */
1253 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001254 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001255 assert( pLock->cnt==0 );
1256 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001257
drh2ac3ee92004-06-07 16:27:46 +00001258 /* Now get the read-lock */
1259 lock.l_start = SHARED_FIRST;
1260 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001261 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1262 tErrno = errno;
1263 }
drh2ac3ee92004-06-07 16:27:46 +00001264 /* Drop the temporary PENDING lock */
1265 lock.l_start = PENDING_BYTE;
1266 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001267 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001268 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001269 if( s != -1 ){
1270 /* This could happen with a network mount */
1271 tErrno = errno;
1272 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1273 if( IS_LOCK_ERROR(rc) ){
1274 pFile->lastErrno = tErrno;
1275 }
1276 goto end_lock;
1277 }
drh2b4b5962005-06-15 17:47:55 +00001278 }
drhe2396a12007-03-29 20:19:58 +00001279 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001280 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1281 if( IS_LOCK_ERROR(rc) ){
1282 pFile->lastErrno = tErrno;
1283 }
drhbbd42a62004-05-22 17:41:58 +00001284 }else{
drh054889e2005-11-30 03:20:31 +00001285 pFile->locktype = SHARED_LOCK;
1286 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001287 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001288 }
drh3cde3bb2004-06-12 02:17:14 +00001289 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1290 /* We are trying for an exclusive lock but another thread in this
1291 ** same process is still holding a shared lock. */
1292 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001293 }else{
drh3cde3bb2004-06-12 02:17:14 +00001294 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001295 ** assumed that there is a SHARED or greater lock on the file
1296 ** already.
1297 */
drh054889e2005-11-30 03:20:31 +00001298 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001299 lock.l_type = F_WRLCK;
1300 switch( locktype ){
1301 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001302 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001303 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001304 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001305 lock.l_start = SHARED_FIRST;
1306 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001307 break;
1308 default:
1309 assert(0);
1310 }
drh054889e2005-11-30 03:20:31 +00001311 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001312 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001313 int tErrno = errno;
1314 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1315 if( IS_LOCK_ERROR(rc) ){
1316 pFile->lastErrno = tErrno;
1317 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001318 }
drhbbd42a62004-05-22 17:41:58 +00001319 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001320
danielk1977ecb2a962004-06-02 06:30:16 +00001321 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001322 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001323 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001324 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001325 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001326 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001327 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001328
1329end_lock:
drh6c7d5c52008-11-21 20:32:33 +00001330 unixLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001331 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001332 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001333 return rc;
1334}
1335
1336/*
drh054889e2005-11-30 03:20:31 +00001337** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001338** must be either NO_LOCK or SHARED_LOCK.
1339**
1340** If the locking level of the file descriptor is already at or below
1341** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001342*/
danielk197762079062007-08-15 17:08:46 +00001343static int unixUnlock(sqlite3_file *id, int locktype){
drh6c7d5c52008-11-21 20:32:33 +00001344 struct unixLockInfo *pLock;
drha6abd042004-06-09 17:37:22 +00001345 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001346 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001347 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001348 int h;
drha6abd042004-06-09 17:37:22 +00001349
drh054889e2005-11-30 03:20:31 +00001350 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001351 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001352 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001353
1354 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001355 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001356 return SQLITE_OK;
1357 }
drhf1a221e2006-01-15 17:27:17 +00001358 if( CHECK_THREADID(pFile) ){
1359 return SQLITE_MISUSE;
1360 }
drh6c7d5c52008-11-21 20:32:33 +00001361 unixEnterMutex();
drh1aa5af12008-03-07 19:51:14 +00001362 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001363 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001364 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001365 if( pFile->locktype>SHARED_LOCK ){
1366 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001367 SimulateIOErrorBenign(1);
1368 SimulateIOError( h=(-1) )
1369 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001370 if( locktype==SHARED_LOCK ){
1371 lock.l_type = F_RDLCK;
1372 lock.l_whence = SEEK_SET;
1373 lock.l_start = SHARED_FIRST;
1374 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001375 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001376 int tErrno = errno;
1377 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1378 if( IS_LOCK_ERROR(rc) ){
1379 pFile->lastErrno = tErrno;
1380 }
1381 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001382 }
1383 }
drhbbd42a62004-05-22 17:41:58 +00001384 lock.l_type = F_UNLCK;
1385 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001386 lock.l_start = PENDING_BYTE;
1387 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001388 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001389 pLock->locktype = SHARED_LOCK;
1390 }else{
aswift5b1a2562008-08-22 00:22:35 +00001391 int tErrno = errno;
1392 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1393 if( IS_LOCK_ERROR(rc) ){
1394 pFile->lastErrno = tErrno;
1395 }
1396 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001397 }
drhbbd42a62004-05-22 17:41:58 +00001398 }
drha6abd042004-06-09 17:37:22 +00001399 if( locktype==NO_LOCK ){
drh6c7d5c52008-11-21 20:32:33 +00001400 struct unixOpenCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001401
drha6abd042004-06-09 17:37:22 +00001402 /* Decrement the shared lock counter. Release the lock using an
1403 ** OS call only when all threads in this same process have released
1404 ** the lock.
1405 */
1406 pLock->cnt--;
1407 if( pLock->cnt==0 ){
1408 lock.l_type = F_UNLCK;
1409 lock.l_whence = SEEK_SET;
1410 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001411 SimulateIOErrorBenign(1);
1412 SimulateIOError( h=(-1) )
1413 SimulateIOErrorBenign(0);
1414 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001415 pLock->locktype = NO_LOCK;
1416 }else{
aswift5b1a2562008-08-22 00:22:35 +00001417 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001418 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001419 if( IS_LOCK_ERROR(rc) ){
1420 pFile->lastErrno = tErrno;
1421 }
drh1aa5af12008-03-07 19:51:14 +00001422 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001423 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001424 }
drha6abd042004-06-09 17:37:22 +00001425 }
1426
drhbbd42a62004-05-22 17:41:58 +00001427 /* Decrement the count of locks against this same file. When the
1428 ** count reaches zero, close any other file descriptors whose close
1429 ** was deferred because of outstanding locks.
1430 */
drh1aa5af12008-03-07 19:51:14 +00001431 if( rc==SQLITE_OK ){
1432 pOpen = pFile->pOpen;
1433 pOpen->nLock--;
1434 assert( pOpen->nLock>=0 );
1435 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1436 int i;
1437 for(i=0; i<pOpen->nPending; i++){
aswiftaebf4132008-11-21 00:10:35 +00001438 /* close pending fds, but if closing fails don't free the array
1439 ** assign -1 to the successfully closed descriptors and record the
1440 ** error. The next attempt to unlock will try again. */
1441 if( pOpen->aPending[i] < 0 ) continue;
1442 if( close(pOpen->aPending[i]) ){
1443 pFile->lastErrno = errno;
1444 rc = SQLITE_IOERR_CLOSE;
1445 }else{
1446 pOpen->aPending[i] = -1;
1447 }
drh1aa5af12008-03-07 19:51:14 +00001448 }
aswiftaebf4132008-11-21 00:10:35 +00001449 if( rc==SQLITE_OK ){
1450 sqlite3_free(pOpen->aPending);
1451 pOpen->nPending = 0;
1452 pOpen->aPending = 0;
1453 }
drhbbd42a62004-05-22 17:41:58 +00001454 }
drhbbd42a62004-05-22 17:41:58 +00001455 }
1456 }
aswift5b1a2562008-08-22 00:22:35 +00001457
1458end_unlock:
drh6c7d5c52008-11-21 20:32:33 +00001459 unixLeaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001460 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001461 return rc;
drhbbd42a62004-05-22 17:41:58 +00001462}
1463
1464/*
danielk1977e339d652008-06-28 11:23:00 +00001465** This function performs the parts of the "close file" operation
1466** common to all locking schemes. It closes the directory and file
1467** handles, if they are valid, and sets all fields of the unixFile
1468** structure to 0.
drh9b35ea62008-11-29 02:20:26 +00001469**
1470** It is *not* necessary to hold the mutex when this routine is called,
1471** even on VxWorks. A mutex will be acquired on VxWorks by the
1472** vxworksReleaseFileId() routine.
danielk1977e339d652008-06-28 11:23:00 +00001473*/
1474static int closeUnixFile(sqlite3_file *id){
1475 unixFile *pFile = (unixFile*)id;
1476 if( pFile ){
1477 if( pFile->dirfd>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001478 int err = close(pFile->dirfd);
1479 if( err ){
1480 pFile->lastErrno = errno;
1481 return SQLITE_IOERR_DIR_CLOSE;
1482 }else{
1483 pFile->dirfd=-1;
1484 }
danielk1977e339d652008-06-28 11:23:00 +00001485 }
1486 if( pFile->h>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001487 int err = close(pFile->h);
1488 if( err ){
1489 pFile->lastErrno = errno;
1490 return SQLITE_IOERR_CLOSE;
1491 }
danielk1977e339d652008-06-28 11:23:00 +00001492 }
drh6c7d5c52008-11-21 20:32:33 +00001493#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +00001494 if( pFile->pId ){
1495 if( pFile->isDelete ){
drh9b35ea62008-11-29 02:20:26 +00001496 unlink(pFile->pId->zCanonicalName);
chw97185482008-11-17 08:05:31 +00001497 }
drh107886a2008-11-21 22:21:50 +00001498 vxworksReleaseFileId(pFile->pId);
1499 pFile->pId = 0;
chw97185482008-11-17 08:05:31 +00001500 }
1501#endif
danielk1977e339d652008-06-28 11:23:00 +00001502 OSTRACE2("CLOSE %-3d\n", pFile->h);
1503 OpenCounter(-1);
1504 memset(pFile, 0, sizeof(unixFile));
1505 }
1506 return SQLITE_OK;
1507}
1508
1509/*
danielk1977e3026632004-06-22 11:29:02 +00001510** Close a file.
1511*/
danielk197762079062007-08-15 17:08:46 +00001512static int unixClose(sqlite3_file *id){
aswiftaebf4132008-11-21 00:10:35 +00001513 int rc = SQLITE_OK;
danielk1977e339d652008-06-28 11:23:00 +00001514 if( id ){
1515 unixFile *pFile = (unixFile *)id;
1516 unixUnlock(id, NO_LOCK);
drh6c7d5c52008-11-21 20:32:33 +00001517 unixEnterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001518 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001519 /* If there are outstanding locks, do not actually close the file just
1520 ** yet because that would clear those locks. Instead, add the file
1521 ** descriptor to pOpen->aPending. It will be automatically closed when
1522 ** the last lock is cleared.
1523 */
1524 int *aNew;
drh6c7d5c52008-11-21 20:32:33 +00001525 struct unixOpenCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001526 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001527 if( aNew==0 ){
1528 /* If a malloc fails, just leak the file descriptor */
1529 }else{
1530 pOpen->aPending = aNew;
1531 pOpen->aPending[pOpen->nPending] = pFile->h;
1532 pOpen->nPending++;
1533 pFile->h = -1;
1534 }
danielk1977e3026632004-06-22 11:29:02 +00001535 }
danielk1977e339d652008-06-28 11:23:00 +00001536 releaseLockInfo(pFile->pLock);
1537 releaseOpenCnt(pFile->pOpen);
aswiftaebf4132008-11-21 00:10:35 +00001538 rc = closeUnixFile(id);
drh6c7d5c52008-11-21 20:32:33 +00001539 unixLeaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00001540 }
aswiftaebf4132008-11-21 00:10:35 +00001541 return rc;
danielk1977e3026632004-06-22 11:29:02 +00001542}
1543
drh734c9862008-11-28 15:37:20 +00001544/************** End of the posix advisory lock implementation *****************
1545******************************************************************************/
drhbfe66312006-10-03 17:40:40 +00001546
drh734c9862008-11-28 15:37:20 +00001547/******************************************************************************
1548****************************** No-op Locking **********************************
1549**
1550** Of the various locking implementations available, this is by far the
1551** simplest: locking is ignored. No attempt is made to lock the database
1552** file for reading or writing.
1553**
1554** This locking mode is appropriate for use on read-only databases
1555** (ex: databases that are burned into CD-ROM, for example.) It can
1556** also be used if the application employs some external mechanism to
1557** prevent simultaneous access of the same database by two or more
1558** database connections. But there is a serious risk of database
1559** corruption if this locking mode is used in situations where multiple
1560** database connections are accessing the same database file at the same
1561** time and one or more of those connections are writing.
1562*/
drhbfe66312006-10-03 17:40:40 +00001563
drh734c9862008-11-28 15:37:20 +00001564static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
1565 UNUSED_PARAMETER(NotUsed);
1566 *pResOut = 0;
1567 return SQLITE_OK;
1568}
drh734c9862008-11-28 15:37:20 +00001569static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
1570 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1571 return SQLITE_OK;
1572}
drh734c9862008-11-28 15:37:20 +00001573static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
1574 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1575 return SQLITE_OK;
1576}
1577
1578/*
drh9b35ea62008-11-29 02:20:26 +00001579** Close the file.
drh734c9862008-11-28 15:37:20 +00001580*/
1581static int nolockClose(sqlite3_file *id) {
drh9b35ea62008-11-29 02:20:26 +00001582 return closeUnixFile(id);
drh734c9862008-11-28 15:37:20 +00001583}
1584
1585/******************* End of the no-op lock implementation *********************
1586******************************************************************************/
1587
1588/******************************************************************************
1589************************* Begin dot-file Locking ******************************
1590**
1591** The dotfile locking implementation uses the existing of separate lock
1592** files in order to control access to the database. This works on just
1593** about every filesystem imaginable. But there are serious downsides:
1594**
1595** (1) There is zero concurrency. A single reader blocks all other
1596** connections from reading or writing the database.
1597**
1598** (2) An application crash or power loss can leave stale lock files
1599** sitting around that need to be cleared manually.
1600**
1601** Nevertheless, a dotlock is an appropriate locking mode for use if no
1602** other locking strategy is available.
drh7708e972008-11-29 00:56:52 +00001603**
1604** Dotfile locking works by creating a file in the same directory as the
1605** database and with the same name but with a ".lock" extension added.
1606** The existance of a lock file implies an EXCLUSIVE lock. All other lock
1607** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
drh734c9862008-11-28 15:37:20 +00001608*/
1609
1610/*
1611** The file suffix added to the data base filename in order to create the
1612** lock file.
1613*/
1614#define DOTLOCK_SUFFIX ".lock"
1615
drh7708e972008-11-29 00:56:52 +00001616/*
1617** This routine checks if there is a RESERVED lock held on the specified
1618** file by this or any other process. If such a lock is held, set *pResOut
1619** to a non-zero value otherwise *pResOut is set to zero. The return value
1620** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1621**
1622** In dotfile locking, either a lock exists or it does not. So in this
1623** variation of CheckReservedLock(), *pResOut is set to true if any lock
1624** is held on the file and false if the file is unlocked.
1625*/
drh734c9862008-11-28 15:37:20 +00001626static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
1627 int rc = SQLITE_OK;
1628 int reserved = 0;
1629 unixFile *pFile = (unixFile*)id;
1630
1631 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1632
1633 assert( pFile );
1634
1635 /* Check if a thread in this process holds such a lock */
1636 if( pFile->locktype>SHARED_LOCK ){
drh7708e972008-11-29 00:56:52 +00001637 /* Either this connection or some other connection in the same process
1638 ** holds a lock on the file. No need to check further. */
drh734c9862008-11-28 15:37:20 +00001639 reserved = 1;
drh7708e972008-11-29 00:56:52 +00001640 }else{
1641 /* The lock is held if and only if the lockfile exists */
1642 const char *zLockFile = (const char*)pFile->lockingContext;
1643 reserved = access(zLockFile, 0)==0;
drh734c9862008-11-28 15:37:20 +00001644 }
1645 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
drh734c9862008-11-28 15:37:20 +00001646 *pResOut = reserved;
1647 return rc;
1648}
1649
drh7708e972008-11-29 00:56:52 +00001650/*
1651** Lock the file with the lock specified by parameter locktype - one
1652** of the following:
1653**
1654** (1) SHARED_LOCK
1655** (2) RESERVED_LOCK
1656** (3) PENDING_LOCK
1657** (4) EXCLUSIVE_LOCK
1658**
1659** Sometimes when requesting one lock state, additional lock states
1660** are inserted in between. The locking might fail on one of the later
1661** transitions leaving the lock state different from what it started but
1662** still short of its goal. The following chart shows the allowed
1663** transitions and the inserted intermediate states:
1664**
1665** UNLOCKED -> SHARED
1666** SHARED -> RESERVED
1667** SHARED -> (PENDING) -> EXCLUSIVE
1668** RESERVED -> (PENDING) -> EXCLUSIVE
1669** PENDING -> EXCLUSIVE
1670**
1671** This routine will only increase a lock. Use the sqlite3OsUnlock()
1672** routine to lower a locking level.
1673**
1674** With dotfile locking, we really only support state (4): EXCLUSIVE.
1675** But we track the other locking levels internally.
1676*/
drh734c9862008-11-28 15:37:20 +00001677static int dotlockLock(sqlite3_file *id, int locktype) {
1678 unixFile *pFile = (unixFile*)id;
1679 int fd;
1680 char *zLockFile = (char *)pFile->lockingContext;
drh7708e972008-11-29 00:56:52 +00001681 int rc = SQLITE_OK;
drh734c9862008-11-28 15:37:20 +00001682
drh7708e972008-11-29 00:56:52 +00001683
1684 /* If we have any lock, then the lock file already exists. All we have
1685 ** to do is adjust our internal record of the lock level.
1686 */
1687 if( pFile->locktype > NO_LOCK ){
drh734c9862008-11-28 15:37:20 +00001688 pFile->locktype = locktype;
1689#if !OS_VXWORKS
1690 /* Always update the timestamp on the old file */
1691 utimes(zLockFile, NULL);
1692#endif
drh7708e972008-11-29 00:56:52 +00001693 return SQLITE_OK;
drh734c9862008-11-28 15:37:20 +00001694 }
1695
1696 /* grab an exclusive lock */
1697 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
1698 if( fd<0 ){
1699 /* failed to open/create the file, someone else may have stolen the lock */
1700 int tErrno = errno;
1701 if( EEXIST == tErrno ){
1702 rc = SQLITE_BUSY;
1703 } else {
1704 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1705 if( IS_LOCK_ERROR(rc) ){
1706 pFile->lastErrno = tErrno;
1707 }
1708 }
drh7708e972008-11-29 00:56:52 +00001709 return rc;
drh734c9862008-11-28 15:37:20 +00001710 }
1711 if( close(fd) ){
1712 pFile->lastErrno = errno;
1713 rc = SQLITE_IOERR_CLOSE;
1714 }
1715
1716 /* got it, set the type and return ok */
1717 pFile->locktype = locktype;
drh734c9862008-11-28 15:37:20 +00001718 return rc;
1719}
1720
drh7708e972008-11-29 00:56:52 +00001721/*
1722** Lower the locking level on file descriptor pFile to locktype. locktype
1723** must be either NO_LOCK or SHARED_LOCK.
1724**
1725** If the locking level of the file descriptor is already at or below
1726** the requested locking level, this routine is a no-op.
1727**
1728** When the locking level reaches NO_LOCK, delete the lock file.
1729*/
drh734c9862008-11-28 15:37:20 +00001730static int dotlockUnlock(sqlite3_file *id, int locktype) {
1731 unixFile *pFile = (unixFile*)id;
1732 char *zLockFile = (char *)pFile->lockingContext;
1733
1734 assert( pFile );
1735 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
1736 pFile->locktype, getpid());
1737 assert( locktype<=SHARED_LOCK );
1738
1739 /* no-op if possible */
1740 if( pFile->locktype==locktype ){
1741 return SQLITE_OK;
1742 }
drh7708e972008-11-29 00:56:52 +00001743
1744 /* To downgrade to shared, simply update our internal notion of the
1745 ** lock state. No need to mess with the file on disk.
1746 */
1747 if( locktype==SHARED_LOCK ){
1748 pFile->locktype = SHARED_LOCK;
drh734c9862008-11-28 15:37:20 +00001749 return SQLITE_OK;
1750 }
1751
drh7708e972008-11-29 00:56:52 +00001752 /* To fully unlock the database, delete the lock file */
1753 assert( locktype==NO_LOCK );
1754 if( unlink(zLockFile) ){
drh734c9862008-11-28 15:37:20 +00001755 int rc, tErrno = errno;
1756 if( ENOENT != tErrno ){
1757 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1758 }
1759 if( IS_LOCK_ERROR(rc) ){
1760 pFile->lastErrno = tErrno;
1761 }
1762 return rc;
1763 }
1764 pFile->locktype = NO_LOCK;
1765 return SQLITE_OK;
1766}
1767
1768/*
drh9b35ea62008-11-29 02:20:26 +00001769** Close a file. Make sure the lock has been released before closing.
drh734c9862008-11-28 15:37:20 +00001770*/
1771static int dotlockClose(sqlite3_file *id) {
1772 int rc;
1773 if( id ){
1774 unixFile *pFile = (unixFile*)id;
1775 dotlockUnlock(id, NO_LOCK);
1776 sqlite3_free(pFile->lockingContext);
1777 }
drh734c9862008-11-28 15:37:20 +00001778 rc = closeUnixFile(id);
drh734c9862008-11-28 15:37:20 +00001779 return rc;
1780}
1781/****************** End of the dot-file lock implementation *******************
1782******************************************************************************/
1783
1784/******************************************************************************
1785************************** Begin flock Locking ********************************
1786**
1787** Use the flock() system call to do file locking.
1788**
drh6b9d6dd2008-12-03 19:34:47 +00001789** flock() locking is like dot-file locking in that the various
1790** fine-grain locking levels supported by SQLite are collapsed into
1791** a single exclusive lock. In other words, SHARED, RESERVED, and
1792** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
1793** still works when you do this, but concurrency is reduced since
1794** only a single process can be reading the database at a time.
1795**
drh734c9862008-11-28 15:37:20 +00001796** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
1797** compiling for VXWORKS.
1798*/
1799#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
drh734c9862008-11-28 15:37:20 +00001800
drh6b9d6dd2008-12-03 19:34:47 +00001801/*
1802** This routine checks if there is a RESERVED lock held on the specified
1803** file by this or any other process. If such a lock is held, set *pResOut
1804** to a non-zero value otherwise *pResOut is set to zero. The return value
1805** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1806*/
drh734c9862008-11-28 15:37:20 +00001807static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
1808 int rc = SQLITE_OK;
1809 int reserved = 0;
1810 unixFile *pFile = (unixFile*)id;
1811
1812 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1813
1814 assert( pFile );
1815
1816 /* Check if a thread in this process holds such a lock */
1817 if( pFile->locktype>SHARED_LOCK ){
1818 reserved = 1;
1819 }
1820
1821 /* Otherwise see if some other process holds it. */
1822 if( !reserved ){
1823 /* attempt to get the lock */
1824 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
1825 if( !lrc ){
1826 /* got the lock, unlock it */
1827 lrc = flock(pFile->h, LOCK_UN);
1828 if ( lrc ) {
1829 int tErrno = errno;
1830 /* unlock failed with an error */
1831 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1832 if( IS_LOCK_ERROR(lrc) ){
1833 pFile->lastErrno = tErrno;
1834 rc = lrc;
1835 }
1836 }
1837 } else {
1838 int tErrno = errno;
1839 reserved = 1;
1840 /* someone else might have it reserved */
1841 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1842 if( IS_LOCK_ERROR(lrc) ){
1843 pFile->lastErrno = tErrno;
1844 rc = lrc;
1845 }
1846 }
1847 }
1848 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
1849
1850#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1851 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
1852 rc = SQLITE_OK;
1853 reserved=1;
1854 }
1855#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1856 *pResOut = reserved;
1857 return rc;
1858}
1859
drh6b9d6dd2008-12-03 19:34:47 +00001860/*
1861** Lock the file with the lock specified by parameter locktype - one
1862** of the following:
1863**
1864** (1) SHARED_LOCK
1865** (2) RESERVED_LOCK
1866** (3) PENDING_LOCK
1867** (4) EXCLUSIVE_LOCK
1868**
1869** Sometimes when requesting one lock state, additional lock states
1870** are inserted in between. The locking might fail on one of the later
1871** transitions leaving the lock state different from what it started but
1872** still short of its goal. The following chart shows the allowed
1873** transitions and the inserted intermediate states:
1874**
1875** UNLOCKED -> SHARED
1876** SHARED -> RESERVED
1877** SHARED -> (PENDING) -> EXCLUSIVE
1878** RESERVED -> (PENDING) -> EXCLUSIVE
1879** PENDING -> EXCLUSIVE
1880**
1881** flock() only really support EXCLUSIVE locks. We track intermediate
1882** lock states in the sqlite3_file structure, but all locks SHARED or
1883** above are really EXCLUSIVE locks and exclude all other processes from
1884** access the file.
1885**
1886** This routine will only increase a lock. Use the sqlite3OsUnlock()
1887** routine to lower a locking level.
1888*/
drh734c9862008-11-28 15:37:20 +00001889static int flockLock(sqlite3_file *id, int locktype) {
1890 int rc = SQLITE_OK;
drh734c9862008-11-28 15:37:20 +00001891 unixFile *pFile = (unixFile*)id;
1892
1893 assert( pFile );
1894
1895 /* if we already have a lock, it is exclusive.
1896 ** Just adjust level and punt on outta here. */
1897 if (pFile->locktype > NO_LOCK) {
1898 pFile->locktype = locktype;
1899 return SQLITE_OK;
1900 }
1901
1902 /* grab an exclusive lock */
1903
1904 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
1905 int tErrno = errno;
1906 /* didn't get, must be busy */
1907 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1908 if( IS_LOCK_ERROR(rc) ){
1909 pFile->lastErrno = tErrno;
1910 }
1911 } else {
1912 /* got it, set the type and return ok */
1913 pFile->locktype = locktype;
1914 }
1915 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
1916 rc==SQLITE_OK ? "ok" : "failed");
1917#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1918 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
1919 rc = SQLITE_BUSY;
1920 }
1921#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1922 return rc;
1923}
1924
drh6b9d6dd2008-12-03 19:34:47 +00001925
1926/*
1927** Lower the locking level on file descriptor pFile to locktype. locktype
1928** must be either NO_LOCK or SHARED_LOCK.
1929**
1930** If the locking level of the file descriptor is already at or below
1931** the requested locking level, this routine is a no-op.
1932*/
drh734c9862008-11-28 15:37:20 +00001933static int flockUnlock(sqlite3_file *id, int locktype) {
1934 unixFile *pFile = (unixFile*)id;
1935
1936 assert( pFile );
1937 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
1938 pFile->locktype, getpid());
1939 assert( locktype<=SHARED_LOCK );
1940
1941 /* no-op if possible */
1942 if( pFile->locktype==locktype ){
1943 return SQLITE_OK;
1944 }
1945
1946 /* shared can just be set because we always have an exclusive */
1947 if (locktype==SHARED_LOCK) {
1948 pFile->locktype = locktype;
1949 return SQLITE_OK;
1950 }
1951
1952 /* no, really, unlock. */
1953 int rc = flock(pFile->h, LOCK_UN);
1954 if (rc) {
1955 int r, tErrno = errno;
1956 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1957 if( IS_LOCK_ERROR(r) ){
1958 pFile->lastErrno = tErrno;
1959 }
1960#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1961 if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
1962 r = SQLITE_BUSY;
1963 }
1964#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1965
1966 return r;
1967 } else {
1968 pFile->locktype = NO_LOCK;
1969 return SQLITE_OK;
1970 }
1971}
1972
1973/*
1974** Close a file.
1975*/
1976static int flockClose(sqlite3_file *id) {
1977 if( id ){
1978 flockUnlock(id, NO_LOCK);
1979 }
1980 return closeUnixFile(id);
1981}
1982
1983#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
1984
1985/******************* End of the flock lock implementation *********************
1986******************************************************************************/
1987
1988/******************************************************************************
1989************************ Begin Named Semaphore Locking ************************
1990**
1991** Named semaphore locking is only supported on VxWorks.
drh6b9d6dd2008-12-03 19:34:47 +00001992**
1993** Semaphore locking is like dot-lock and flock in that it really only
1994** supports EXCLUSIVE locking. Only a single process can read or write
1995** the database file at a time. This reduces potential concurrency, but
1996** makes the lock implementation much easier.
drh734c9862008-11-28 15:37:20 +00001997*/
1998#if OS_VXWORKS
1999
drh6b9d6dd2008-12-03 19:34:47 +00002000/*
2001** This routine checks if there is a RESERVED lock held on the specified
2002** file by this or any other process. If such a lock is held, set *pResOut
2003** to a non-zero value otherwise *pResOut is set to zero. The return value
2004** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2005*/
drh734c9862008-11-28 15:37:20 +00002006static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
2007 int rc = SQLITE_OK;
2008 int reserved = 0;
2009 unixFile *pFile = (unixFile*)id;
2010
2011 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2012
2013 assert( pFile );
2014
2015 /* Check if a thread in this process holds such a lock */
2016 if( pFile->locktype>SHARED_LOCK ){
2017 reserved = 1;
2018 }
2019
2020 /* Otherwise see if some other process holds it. */
2021 if( !reserved ){
2022 sem_t *pSem = pFile->pOpen->pSem;
2023 struct stat statBuf;
2024
2025 if( sem_trywait(pSem)==-1 ){
2026 int tErrno = errno;
2027 if( EAGAIN != tErrno ){
2028 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2029 pFile->lastErrno = tErrno;
2030 } else {
2031 /* someone else has the lock when we are in NO_LOCK */
2032 reserved = (pFile->locktype < SHARED_LOCK);
2033 }
2034 }else{
2035 /* we could have it if we want it */
2036 sem_post(pSem);
2037 }
2038 }
2039 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2040
2041 *pResOut = reserved;
2042 return rc;
2043}
2044
drh6b9d6dd2008-12-03 19:34:47 +00002045/*
2046** Lock the file with the lock specified by parameter locktype - one
2047** of the following:
2048**
2049** (1) SHARED_LOCK
2050** (2) RESERVED_LOCK
2051** (3) PENDING_LOCK
2052** (4) EXCLUSIVE_LOCK
2053**
2054** Sometimes when requesting one lock state, additional lock states
2055** are inserted in between. The locking might fail on one of the later
2056** transitions leaving the lock state different from what it started but
2057** still short of its goal. The following chart shows the allowed
2058** transitions and the inserted intermediate states:
2059**
2060** UNLOCKED -> SHARED
2061** SHARED -> RESERVED
2062** SHARED -> (PENDING) -> EXCLUSIVE
2063** RESERVED -> (PENDING) -> EXCLUSIVE
2064** PENDING -> EXCLUSIVE
2065**
2066** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
2067** lock states in the sqlite3_file structure, but all locks SHARED or
2068** above are really EXCLUSIVE locks and exclude all other processes from
2069** access the file.
2070**
2071** This routine will only increase a lock. Use the sqlite3OsUnlock()
2072** routine to lower a locking level.
2073*/
drh734c9862008-11-28 15:37:20 +00002074static int semLock(sqlite3_file *id, int locktype) {
2075 unixFile *pFile = (unixFile*)id;
2076 int fd;
2077 sem_t *pSem = pFile->pOpen->pSem;
2078 int rc = SQLITE_OK;
2079
2080 /* if we already have a lock, it is exclusive.
2081 ** Just adjust level and punt on outta here. */
2082 if (pFile->locktype > NO_LOCK) {
2083 pFile->locktype = locktype;
2084 rc = SQLITE_OK;
2085 goto sem_end_lock;
2086 }
2087
2088 /* lock semaphore now but bail out when already locked. */
2089 if( sem_trywait(pSem)==-1 ){
2090 rc = SQLITE_BUSY;
2091 goto sem_end_lock;
2092 }
2093
2094 /* got it, set the type and return ok */
2095 pFile->locktype = locktype;
2096
2097 sem_end_lock:
2098 return rc;
2099}
2100
drh6b9d6dd2008-12-03 19:34:47 +00002101/*
2102** Lower the locking level on file descriptor pFile to locktype. locktype
2103** must be either NO_LOCK or SHARED_LOCK.
2104**
2105** If the locking level of the file descriptor is already at or below
2106** the requested locking level, this routine is a no-op.
2107*/
drh734c9862008-11-28 15:37:20 +00002108static int semUnlock(sqlite3_file *id, int locktype) {
2109 unixFile *pFile = (unixFile*)id;
2110 sem_t *pSem = pFile->pOpen->pSem;
2111
2112 assert( pFile );
2113 assert( pSem );
2114 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2115 pFile->locktype, getpid());
2116 assert( locktype<=SHARED_LOCK );
2117
2118 /* no-op if possible */
2119 if( pFile->locktype==locktype ){
2120 return SQLITE_OK;
2121 }
2122
2123 /* shared can just be set because we always have an exclusive */
2124 if (locktype==SHARED_LOCK) {
2125 pFile->locktype = locktype;
2126 return SQLITE_OK;
2127 }
2128
2129 /* no, really unlock. */
2130 if ( sem_post(pSem)==-1 ) {
2131 int rc, tErrno = errno;
2132 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2133 if( IS_LOCK_ERROR(rc) ){
2134 pFile->lastErrno = tErrno;
2135 }
2136 return rc;
2137 }
2138 pFile->locktype = NO_LOCK;
2139 return SQLITE_OK;
2140}
2141
2142/*
2143 ** Close a file.
drhbfe66312006-10-03 17:40:40 +00002144 */
drh734c9862008-11-28 15:37:20 +00002145static int semClose(sqlite3_file *id) {
2146 if( id ){
2147 unixFile *pFile = (unixFile*)id;
2148 semUnlock(id, NO_LOCK);
2149 assert( pFile );
2150 unixEnterMutex();
2151 releaseLockInfo(pFile->pLock);
2152 releaseOpenCnt(pFile->pOpen);
2153 closeUnixFile(id);
2154 unixLeaveMutex();
2155 }
2156 return SQLITE_OK;
2157}
2158
2159#endif /* OS_VXWORKS */
2160/*
2161** Named semaphore locking is only available on VxWorks.
2162**
2163*************** End of the named semaphore lock implementation ****************
2164******************************************************************************/
2165
2166
2167/******************************************************************************
2168*************************** Begin AFP Locking *********************************
2169**
2170** AFP is the Apple Filing Protocol. AFP is a network filesystem found
2171** on Apple Macintosh computers - both OS9 and OSX.
2172**
2173** Third-party implementations of AFP are available. But this code here
2174** only works on OSX.
2175*/
2176
drhd2cb50b2009-01-09 21:41:17 +00002177#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
drh734c9862008-11-28 15:37:20 +00002178/*
2179** The afpLockingContext structure contains all afp lock specific state
2180*/
drhbfe66312006-10-03 17:40:40 +00002181typedef struct afpLockingContext afpLockingContext;
2182struct afpLockingContext {
aswiftaebf4132008-11-21 00:10:35 +00002183 unsigned long long sharedByte;
drh6b9d6dd2008-12-03 19:34:47 +00002184 const char *dbPath; /* Name of the open file */
drhbfe66312006-10-03 17:40:40 +00002185};
2186
2187struct ByteRangeLockPB2
2188{
2189 unsigned long long offset; /* offset to first byte to lock */
2190 unsigned long long length; /* nbr of bytes to lock */
2191 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
2192 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
2193 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
2194 int fd; /* file desc to assoc this lock with */
2195};
2196
drhfd131da2007-08-07 17:13:03 +00002197#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00002198
drh6b9d6dd2008-12-03 19:34:47 +00002199/*
2200** This is a utility for setting or clearing a bit-range lock on an
2201** AFP filesystem.
2202**
2203** Return SQLITE_OK on success, SQLITE_BUSY on failure.
2204*/
2205static int afpSetLock(
2206 const char *path, /* Name of the file to be locked or unlocked */
2207 unixFile *pFile, /* Open file descriptor on path */
2208 unsigned long long offset, /* First byte to be locked */
2209 unsigned long long length, /* Number of bytes to lock */
2210 int setLockFlag /* True to set lock. False to clear lock */
danielk1977ad94b582007-08-20 06:44:22 +00002211){
drh6b9d6dd2008-12-03 19:34:47 +00002212 struct ByteRangeLockPB2 pb;
2213 int err;
drhbfe66312006-10-03 17:40:40 +00002214
2215 pb.unLockFlag = setLockFlag ? 0 : 1;
2216 pb.startEndFlag = 0;
2217 pb.offset = offset;
2218 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00002219 pb.fd = pFile->h;
aswiftaebf4132008-11-21 00:10:35 +00002220
2221 OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
drh734c9862008-11-28 15:37:20 +00002222 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
2223 offset, length);
drhbfe66312006-10-03 17:40:40 +00002224 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
2225 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00002226 int rc;
2227 int tErrno = errno;
drh734c9862008-11-28 15:37:20 +00002228 OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
2229 path, tErrno, strerror(tErrno));
aswiftaebf4132008-11-21 00:10:35 +00002230#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2231 rc = SQLITE_BUSY;
2232#else
drh734c9862008-11-28 15:37:20 +00002233 rc = sqliteErrorFromPosixError(tErrno,
2234 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
aswiftaebf4132008-11-21 00:10:35 +00002235#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002236 if( IS_LOCK_ERROR(rc) ){
2237 pFile->lastErrno = tErrno;
2238 }
2239 return rc;
drhbfe66312006-10-03 17:40:40 +00002240 } else {
aswift5b1a2562008-08-22 00:22:35 +00002241 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002242 }
2243}
2244
drh6b9d6dd2008-12-03 19:34:47 +00002245/*
2246** This routine checks if there is a RESERVED lock held on the specified
2247** file by this or any other process. If such a lock is held, set *pResOut
2248** to a non-zero value otherwise *pResOut is set to zero. The return value
2249** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2250*/
danielk1977e339d652008-06-28 11:23:00 +00002251static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002252 int rc = SQLITE_OK;
2253 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002254 unixFile *pFile = (unixFile*)id;
2255
aswift5b1a2562008-08-22 00:22:35 +00002256 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2257
2258 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00002259 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2260
2261 /* Check if a thread in this process holds such a lock */
2262 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002263 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00002264 }
2265
2266 /* Otherwise see if some other process holds it.
2267 */
aswift5b1a2562008-08-22 00:22:35 +00002268 if( !reserved ){
2269 /* lock the RESERVED byte */
drh6b9d6dd2008-12-03 19:34:47 +00002270 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
aswift5b1a2562008-08-22 00:22:35 +00002271 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00002272 /* if we succeeded in taking the reserved lock, unlock it to restore
2273 ** the original state */
drh6b9d6dd2008-12-03 19:34:47 +00002274 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswift5b1a2562008-08-22 00:22:35 +00002275 } else {
2276 /* if we failed to get the lock then someone else must have it */
2277 reserved = 1;
2278 }
2279 if( IS_LOCK_ERROR(lrc) ){
2280 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00002281 }
2282 }
drhbfe66312006-10-03 17:40:40 +00002283
aswift5b1a2562008-08-22 00:22:35 +00002284 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2285
2286 *pResOut = reserved;
2287 return rc;
drhbfe66312006-10-03 17:40:40 +00002288}
2289
drh6b9d6dd2008-12-03 19:34:47 +00002290/*
2291** Lock the file with the lock specified by parameter locktype - one
2292** of the following:
2293**
2294** (1) SHARED_LOCK
2295** (2) RESERVED_LOCK
2296** (3) PENDING_LOCK
2297** (4) EXCLUSIVE_LOCK
2298**
2299** Sometimes when requesting one lock state, additional lock states
2300** are inserted in between. The locking might fail on one of the later
2301** transitions leaving the lock state different from what it started but
2302** still short of its goal. The following chart shows the allowed
2303** transitions and the inserted intermediate states:
2304**
2305** UNLOCKED -> SHARED
2306** SHARED -> RESERVED
2307** SHARED -> (PENDING) -> EXCLUSIVE
2308** RESERVED -> (PENDING) -> EXCLUSIVE
2309** PENDING -> EXCLUSIVE
2310**
2311** This routine will only increase a lock. Use the sqlite3OsUnlock()
2312** routine to lower a locking level.
2313*/
danielk1977e339d652008-06-28 11:23:00 +00002314static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00002315 int rc = SQLITE_OK;
2316 unixFile *pFile = (unixFile*)id;
2317 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002318
2319 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002320 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002321 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2322
drhbfe66312006-10-03 17:40:40 +00002323 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002324 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
drh6c7d5c52008-11-21 20:32:33 +00002325 ** unixEnterMutex() hasn't been called yet.
drh339eb0b2008-03-07 15:34:11 +00002326 */
drhbfe66312006-10-03 17:40:40 +00002327 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002328 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002329 locktypeName(locktype));
2330 return SQLITE_OK;
2331 }
2332
2333 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002334 */
drhbfe66312006-10-03 17:40:40 +00002335 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2336 assert( locktype!=PENDING_LOCK );
2337 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2338
2339 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002340 */
drh6c7d5c52008-11-21 20:32:33 +00002341 unixEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002342
2343 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002344 */
drhbfe66312006-10-03 17:40:40 +00002345 rc = transferOwnership(pFile);
2346 if( rc!=SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00002347 unixLeaveMutex();
drhbfe66312006-10-03 17:40:40 +00002348 return rc;
2349 }
2350
2351 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002352 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2353 ** be released.
2354 */
drhbfe66312006-10-03 17:40:40 +00002355 if( locktype==SHARED_LOCK
2356 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002357 ){
2358 int failed;
drh6b9d6dd2008-12-03 19:34:47 +00002359 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002360 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002361 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002362 goto afp_end_lock;
2363 }
2364 }
2365
2366 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002367 ** operating system calls for the specified lock.
2368 */
drhbfe66312006-10-03 17:40:40 +00002369 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002370 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002371
aswift5b1a2562008-08-22 00:22:35 +00002372 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002373 /* note that the quality of the randomness doesn't matter that much */
2374 lk = random();
aswiftaebf4132008-11-21 00:10:35 +00002375 context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
drh6b9d6dd2008-12-03 19:34:47 +00002376 lrc1 = afpSetLock(context->dbPath, pFile,
aswiftaebf4132008-11-21 00:10:35 +00002377 SHARED_FIRST+context->sharedByte, 1, 1);
aswift5b1a2562008-08-22 00:22:35 +00002378 if( IS_LOCK_ERROR(lrc1) ){
2379 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002380 }
aswift5b1a2562008-08-22 00:22:35 +00002381 /* Drop the temporary PENDING lock */
drh6b9d6dd2008-12-03 19:34:47 +00002382 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002383
aswift5b1a2562008-08-22 00:22:35 +00002384 if( IS_LOCK_ERROR(lrc1) ) {
2385 pFile->lastErrno = lrc1Errno;
2386 rc = lrc1;
2387 goto afp_end_lock;
2388 } else if( IS_LOCK_ERROR(lrc2) ){
2389 rc = lrc2;
2390 goto afp_end_lock;
2391 } else if( lrc1 != SQLITE_OK ) {
2392 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002393 } else {
2394 pFile->locktype = SHARED_LOCK;
aswiftaebf4132008-11-21 00:10:35 +00002395 pFile->pOpen->nLock++;
drhbfe66312006-10-03 17:40:40 +00002396 }
2397 }else{
2398 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2399 ** assumed that there is a SHARED or greater lock on the file
2400 ** already.
2401 */
2402 int failed = 0;
2403 assert( 0!=pFile->locktype );
2404 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2405 /* Acquire a RESERVED lock */
drh6b9d6dd2008-12-03 19:34:47 +00002406 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002407 }
2408 if (!failed && locktype == EXCLUSIVE_LOCK) {
2409 /* Acquire an EXCLUSIVE lock */
2410
2411 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002412 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002413 */
drh6b9d6dd2008-12-03 19:34:47 +00002414 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
aswiftaebf4132008-11-21 00:10:35 +00002415 context->sharedByte, 1, 0)) ){
2416 int failed2 = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002417 /* now attemmpt to get the exclusive lock range */
drh6b9d6dd2008-12-03 19:34:47 +00002418 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002419 SHARED_SIZE, 1);
drh6b9d6dd2008-12-03 19:34:47 +00002420 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
aswiftaebf4132008-11-21 00:10:35 +00002421 SHARED_FIRST + context->sharedByte, 1, 1)) ){
2422 /* Can't reestablish the shared lock. Sqlite can't deal, this is
2423 ** a critical I/O error
2424 */
2425 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
2426 SQLITE_IOERR_LOCK;
2427 goto afp_end_lock;
2428 }
2429 }else{
aswift5b1a2562008-08-22 00:22:35 +00002430 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002431 }
2432 }
aswift5b1a2562008-08-22 00:22:35 +00002433 if( failed ){
2434 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002435 }
2436 }
2437
2438 if( rc==SQLITE_OK ){
2439 pFile->locktype = locktype;
2440 }else if( locktype==EXCLUSIVE_LOCK ){
2441 pFile->locktype = PENDING_LOCK;
2442 }
2443
2444afp_end_lock:
drh6c7d5c52008-11-21 20:32:33 +00002445 unixLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002446 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002447 rc==SQLITE_OK ? "ok" : "failed");
2448 return rc;
2449}
2450
2451/*
drh339eb0b2008-03-07 15:34:11 +00002452** Lower the locking level on file descriptor pFile to locktype. locktype
2453** must be either NO_LOCK or SHARED_LOCK.
2454**
2455** If the locking level of the file descriptor is already at or below
2456** the requested locking level, this routine is a no-op.
2457*/
danielk1977e339d652008-06-28 11:23:00 +00002458static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002459 int rc = SQLITE_OK;
2460 unixFile *pFile = (unixFile*)id;
aswiftaebf4132008-11-21 00:10:35 +00002461 afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002462
2463 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002464 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002465 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002466
drhbfe66312006-10-03 17:40:40 +00002467 assert( locktype<=SHARED_LOCK );
2468 if( pFile->locktype<=locktype ){
2469 return SQLITE_OK;
2470 }
2471 if( CHECK_THREADID(pFile) ){
2472 return SQLITE_MISUSE;
2473 }
drh6c7d5c52008-11-21 20:32:33 +00002474 unixEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002475 if( pFile->locktype>SHARED_LOCK ){
aswiftaebf4132008-11-21 00:10:35 +00002476
2477 if( pFile->locktype==EXCLUSIVE_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002478 rc = afpSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
aswiftaebf4132008-11-21 00:10:35 +00002479 if( rc==SQLITE_OK && locktype==SHARED_LOCK ){
2480 /* only re-establish the shared lock if necessary */
2481 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
drh6b9d6dd2008-12-03 19:34:47 +00002482 rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
aswiftaebf4132008-11-21 00:10:35 +00002483 }
2484 }
2485 if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002486 rc = afpSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002487 }
2488 if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002489 rc = afpSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002490 }
2491 }else if( locktype==NO_LOCK ){
2492 /* clear the shared lock */
2493 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
drh6b9d6dd2008-12-03 19:34:47 +00002494 rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002495 }
drhbfe66312006-10-03 17:40:40 +00002496
aswiftaebf4132008-11-21 00:10:35 +00002497 if( rc==SQLITE_OK ){
2498 if( locktype==NO_LOCK ){
drh6c7d5c52008-11-21 20:32:33 +00002499 struct unixOpenCnt *pOpen = pFile->pOpen;
aswiftaebf4132008-11-21 00:10:35 +00002500 pOpen->nLock--;
2501 assert( pOpen->nLock>=0 );
2502 if( pOpen->nLock==0 && pOpen->nPending>0 ){
2503 int i;
2504 for(i=0; i<pOpen->nPending; i++){
2505 if( pOpen->aPending[i] < 0 ) continue;
2506 if( close(pOpen->aPending[i]) ){
2507 pFile->lastErrno = errno;
2508 rc = SQLITE_IOERR_CLOSE;
2509 }else{
2510 pOpen->aPending[i] = -1;
drhbfe66312006-10-03 17:40:40 +00002511 }
aswiftaebf4132008-11-21 00:10:35 +00002512 }
2513 if( rc==SQLITE_OK ){
2514 sqlite3_free(pOpen->aPending);
2515 pOpen->nPending = 0;
2516 pOpen->aPending = 0;
2517 }
drhbfe66312006-10-03 17:40:40 +00002518 }
2519 }
drhbfe66312006-10-03 17:40:40 +00002520 }
drh6c7d5c52008-11-21 20:32:33 +00002521 unixLeaveMutex();
aswiftaebf4132008-11-21 00:10:35 +00002522 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002523 return rc;
2524}
2525
2526/*
drh339eb0b2008-03-07 15:34:11 +00002527** Close a file & cleanup AFP specific locking context
2528*/
danielk1977e339d652008-06-28 11:23:00 +00002529static int afpClose(sqlite3_file *id) {
2530 if( id ){
2531 unixFile *pFile = (unixFile*)id;
2532 afpUnlock(id, NO_LOCK);
drh6c7d5c52008-11-21 20:32:33 +00002533 unixEnterMutex();
aswiftaebf4132008-11-21 00:10:35 +00002534 if( pFile->pOpen && pFile->pOpen->nLock ){
2535 /* If there are outstanding locks, do not actually close the file just
drh734c9862008-11-28 15:37:20 +00002536 ** yet because that would clear those locks. Instead, add the file
2537 ** descriptor to pOpen->aPending. It will be automatically closed when
2538 ** the last lock is cleared.
2539 */
aswiftaebf4132008-11-21 00:10:35 +00002540 int *aNew;
drh6c7d5c52008-11-21 20:32:33 +00002541 struct unixOpenCnt *pOpen = pFile->pOpen;
aswiftaebf4132008-11-21 00:10:35 +00002542 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
2543 if( aNew==0 ){
2544 /* If a malloc fails, just leak the file descriptor */
2545 }else{
2546 pOpen->aPending = aNew;
2547 pOpen->aPending[pOpen->nPending] = pFile->h;
2548 pOpen->nPending++;
2549 pFile->h = -1;
2550 }
2551 }
2552 releaseOpenCnt(pFile->pOpen);
danielk1977e339d652008-06-28 11:23:00 +00002553 sqlite3_free(pFile->lockingContext);
aswiftaebf4132008-11-21 00:10:35 +00002554 closeUnixFile(id);
drh6c7d5c52008-11-21 20:32:33 +00002555 unixLeaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00002556 }
aswiftaebf4132008-11-21 00:10:35 +00002557 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002558}
2559
drhd2cb50b2009-01-09 21:41:17 +00002560#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
drh734c9862008-11-28 15:37:20 +00002561/*
2562** The code above is the AFP lock implementation. The code is specific
2563** to MacOSX and does not work on other unix platforms. No alternative
2564** is available. If you don't compile for a mac, then the "unix-afp"
2565** VFS is not available.
2566**
2567********************* End of the AFP lock implementation **********************
2568******************************************************************************/
drhbfe66312006-10-03 17:40:40 +00002569
drh734c9862008-11-28 15:37:20 +00002570
2571/******************************************************************************
2572**************** Non-locking sqlite3_file methods *****************************
2573**
2574** The next division contains implementations for all methods of the
2575** sqlite3_file object other than the locking methods. The locking
2576** methods were defined in divisions above (one locking method per
2577** division). Those methods that are common to all locking modes
2578** are gather together into this division.
2579*/
drhbfe66312006-10-03 17:40:40 +00002580
2581/*
drh734c9862008-11-28 15:37:20 +00002582** Seek to the offset passed as the second argument, then read cnt
2583** bytes into pBuf. Return the number of bytes actually read.
2584**
2585** NB: If you define USE_PREAD or USE_PREAD64, then it might also
2586** be necessary to define _XOPEN_SOURCE to be 500. This varies from
2587** one system to another. Since SQLite does not define USE_PREAD
2588** any any form by default, we will not attempt to define _XOPEN_SOURCE.
2589** See tickets #2741 and #2681.
2590**
2591** To avoid stomping the errno value on a failed read the lastErrno value
2592** is set before returning.
drh339eb0b2008-03-07 15:34:11 +00002593*/
drh734c9862008-11-28 15:37:20 +00002594static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
2595 int got;
2596 i64 newOffset;
2597 TIMER_START;
2598#if defined(USE_PREAD)
2599 got = pread(id->h, pBuf, cnt, offset);
2600 SimulateIOError( got = -1 );
2601#elif defined(USE_PREAD64)
2602 got = pread64(id->h, pBuf, cnt, offset);
2603 SimulateIOError( got = -1 );
2604#else
2605 newOffset = lseek(id->h, offset, SEEK_SET);
2606 SimulateIOError( newOffset-- );
2607 if( newOffset!=offset ){
2608 if( newOffset == -1 ){
2609 ((unixFile*)id)->lastErrno = errno;
2610 }else{
2611 ((unixFile*)id)->lastErrno = 0;
2612 }
2613 return -1;
2614 }
2615 got = read(id->h, pBuf, cnt);
2616#endif
2617 TIMER_END;
2618 if( got<0 ){
2619 ((unixFile*)id)->lastErrno = errno;
2620 }
2621 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
2622 return got;
drhbfe66312006-10-03 17:40:40 +00002623}
2624
2625/*
drh734c9862008-11-28 15:37:20 +00002626** Read data from a file into a buffer. Return SQLITE_OK if all
2627** bytes were read successfully and SQLITE_IOERR if anything goes
2628** wrong.
drh339eb0b2008-03-07 15:34:11 +00002629*/
drh734c9862008-11-28 15:37:20 +00002630static int unixRead(
2631 sqlite3_file *id,
2632 void *pBuf,
2633 int amt,
2634 sqlite3_int64 offset
2635){
2636 int got;
2637 assert( id );
2638 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
2639 if( got==amt ){
2640 return SQLITE_OK;
2641 }else if( got<0 ){
2642 /* lastErrno set by seekAndRead */
2643 return SQLITE_IOERR_READ;
2644 }else{
2645 ((unixFile*)id)->lastErrno = 0; /* not a system error */
2646 /* Unread parts of the buffer must be zero-filled */
2647 memset(&((char*)pBuf)[got], 0, amt-got);
2648 return SQLITE_IOERR_SHORT_READ;
2649 }
2650}
2651
2652/*
2653** Seek to the offset in id->offset then read cnt bytes into pBuf.
2654** Return the number of bytes actually read. Update the offset.
2655**
2656** To avoid stomping the errno value on a failed write the lastErrno value
2657** is set before returning.
2658*/
2659static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
2660 int got;
2661 i64 newOffset;
2662 TIMER_START;
2663#if defined(USE_PREAD)
2664 got = pwrite(id->h, pBuf, cnt, offset);
2665#elif defined(USE_PREAD64)
2666 got = pwrite64(id->h, pBuf, cnt, offset);
2667#else
2668 newOffset = lseek(id->h, offset, SEEK_SET);
2669 if( newOffset!=offset ){
2670 if( newOffset == -1 ){
2671 ((unixFile*)id)->lastErrno = errno;
2672 }else{
2673 ((unixFile*)id)->lastErrno = 0;
2674 }
2675 return -1;
2676 }
2677 got = write(id->h, pBuf, cnt);
2678#endif
2679 TIMER_END;
2680 if( got<0 ){
2681 ((unixFile*)id)->lastErrno = errno;
2682 }
2683
2684 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
2685 return got;
2686}
2687
2688
2689/*
2690** Write data from a buffer into a file. Return SQLITE_OK on success
2691** or some other error code on failure.
2692*/
2693static int unixWrite(
2694 sqlite3_file *id,
2695 const void *pBuf,
2696 int amt,
2697 sqlite3_int64 offset
2698){
2699 int wrote = 0;
2700 assert( id );
2701 assert( amt>0 );
2702 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
2703 amt -= wrote;
2704 offset += wrote;
2705 pBuf = &((char*)pBuf)[wrote];
2706 }
2707 SimulateIOError(( wrote=(-1), amt=1 ));
2708 SimulateDiskfullError(( wrote=0, amt=1 ));
2709 if( amt>0 ){
2710 if( wrote<0 ){
2711 /* lastErrno set by seekAndWrite */
2712 return SQLITE_IOERR_WRITE;
2713 }else{
2714 ((unixFile*)id)->lastErrno = 0; /* not a system error */
2715 return SQLITE_FULL;
2716 }
2717 }
2718 return SQLITE_OK;
2719}
2720
2721#ifdef SQLITE_TEST
2722/*
2723** Count the number of fullsyncs and normal syncs. This is used to test
drh6b9d6dd2008-12-03 19:34:47 +00002724** that syncs and fullsyncs are occurring at the right times.
drh734c9862008-11-28 15:37:20 +00002725*/
2726int sqlite3_sync_count = 0;
2727int sqlite3_fullsync_count = 0;
2728#endif
2729
2730/*
2731** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
2732** Otherwise use fsync() in its place.
2733*/
2734#ifndef HAVE_FDATASYNC
2735# define fdatasync fsync
2736#endif
2737
2738/*
2739** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
2740** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
2741** only available on Mac OS X. But that could change.
2742*/
2743#ifdef F_FULLFSYNC
2744# define HAVE_FULLFSYNC 1
2745#else
2746# define HAVE_FULLFSYNC 0
2747#endif
2748
2749
2750/*
2751** The fsync() system call does not work as advertised on many
2752** unix systems. The following procedure is an attempt to make
2753** it work better.
2754**
2755** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
2756** for testing when we want to run through the test suite quickly.
2757** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
2758** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
2759** or power failure will likely corrupt the database file.
2760*/
2761static int full_fsync(int fd, int fullSync, int dataOnly){
chw97185482008-11-17 08:05:31 +00002762 int rc;
drh734c9862008-11-28 15:37:20 +00002763
2764 /* The following "ifdef/elif/else/" block has the same structure as
2765 ** the one below. It is replicated here solely to avoid cluttering
2766 ** up the real code with the UNUSED_PARAMETER() macros.
2767 */
2768#ifdef SQLITE_NO_SYNC
2769 UNUSED_PARAMETER(fd);
2770 UNUSED_PARAMETER(fullSync);
2771 UNUSED_PARAMETER(dataOnly);
2772#elif HAVE_FULLFSYNC
2773 UNUSED_PARAMETER(dataOnly);
2774#else
2775 UNUSED_PARAMETER(fullSync);
2776#endif
2777
2778 /* Record the number of times that we do a normal fsync() and
2779 ** FULLSYNC. This is used during testing to verify that this procedure
2780 ** gets called with the correct arguments.
2781 */
2782#ifdef SQLITE_TEST
2783 if( fullSync ) sqlite3_fullsync_count++;
2784 sqlite3_sync_count++;
2785#endif
2786
2787 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
2788 ** no-op
2789 */
2790#ifdef SQLITE_NO_SYNC
2791 rc = SQLITE_OK;
2792#elif HAVE_FULLFSYNC
2793 if( fullSync ){
2794 rc = fcntl(fd, F_FULLFSYNC, 0);
2795 }else{
2796 rc = 1;
2797 }
2798 /* If the FULLFSYNC failed, fall back to attempting an fsync().
drh6b9d6dd2008-12-03 19:34:47 +00002799 ** It shouldn't be possible for fullfsync to fail on the local
2800 ** file system (on OSX), so failure indicates that FULLFSYNC
2801 ** isn't supported for this file system. So, attempt an fsync
2802 ** and (for now) ignore the overhead of a superfluous fcntl call.
2803 ** It'd be better to detect fullfsync support once and avoid
2804 ** the fcntl call every time sync is called.
2805 */
drh734c9862008-11-28 15:37:20 +00002806 if( rc ) rc = fsync(fd);
2807
2808#else
2809 if( dataOnly ){
2810 rc = fdatasync(fd);
2811 if( OS_VXWORKS && rc==-1 && errno==ENOTSUP ){
2812 rc = fsync(fd);
2813 }
2814 }else{
2815 rc = fsync(fd);
2816 }
2817#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
2818
2819 if( OS_VXWORKS && rc!= -1 ){
2820 rc = 0;
2821 }
chw97185482008-11-17 08:05:31 +00002822 return rc;
drhbfe66312006-10-03 17:40:40 +00002823}
2824
drh734c9862008-11-28 15:37:20 +00002825/*
2826** Make sure all writes to a particular file are committed to disk.
2827**
2828** If dataOnly==0 then both the file itself and its metadata (file
2829** size, access time, etc) are synced. If dataOnly!=0 then only the
2830** file data is synced.
2831**
2832** Under Unix, also make sure that the directory entry for the file
2833** has been created by fsync-ing the directory that contains the file.
2834** If we do not do this and we encounter a power failure, the directory
2835** entry for the journal might not exist after we reboot. The next
2836** SQLite to access the file will not know that the journal exists (because
2837** the directory entry for the journal was never created) and the transaction
2838** will not roll back - possibly leading to database corruption.
2839*/
2840static int unixSync(sqlite3_file *id, int flags){
2841 int rc;
2842 unixFile *pFile = (unixFile*)id;
2843
2844 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
2845 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
2846
2847 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
2848 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
2849 || (flags&0x0F)==SQLITE_SYNC_FULL
2850 );
2851
2852 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
2853 ** line is to test that doing so does not cause any problems.
2854 */
2855 SimulateDiskfullError( return SQLITE_FULL );
2856
2857 assert( pFile );
2858 OSTRACE2("SYNC %-3d\n", pFile->h);
2859 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
2860 SimulateIOError( rc=1 );
2861 if( rc ){
2862 pFile->lastErrno = errno;
2863 return SQLITE_IOERR_FSYNC;
2864 }
2865 if( pFile->dirfd>=0 ){
2866 int err;
2867 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
2868 HAVE_FULLFSYNC, isFullsync);
2869#ifndef SQLITE_DISABLE_DIRSYNC
2870 /* The directory sync is only attempted if full_fsync is
2871 ** turned off or unavailable. If a full_fsync occurred above,
2872 ** then the directory sync is superfluous.
2873 */
2874 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
2875 /*
2876 ** We have received multiple reports of fsync() returning
2877 ** errors when applied to directories on certain file systems.
2878 ** A failed directory sync is not a big deal. So it seems
2879 ** better to ignore the error. Ticket #1657
2880 */
2881 /* pFile->lastErrno = errno; */
2882 /* return SQLITE_IOERR; */
2883 }
2884#endif
2885 err = close(pFile->dirfd); /* Only need to sync once, so close the */
2886 if( err==0 ){ /* directory when we are done */
2887 pFile->dirfd = -1;
2888 }else{
2889 pFile->lastErrno = errno;
2890 rc = SQLITE_IOERR_DIR_CLOSE;
2891 }
2892 }
2893 return rc;
2894}
2895
2896/*
2897** Truncate an open file to a specified size
2898*/
2899static int unixTruncate(sqlite3_file *id, i64 nByte){
2900 int rc;
2901 assert( id );
2902 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
2903 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
2904 if( rc ){
2905 ((unixFile*)id)->lastErrno = errno;
2906 return SQLITE_IOERR_TRUNCATE;
2907 }else{
2908 return SQLITE_OK;
2909 }
2910}
2911
2912/*
2913** Determine the current size of a file in bytes
2914*/
2915static int unixFileSize(sqlite3_file *id, i64 *pSize){
2916 int rc;
2917 struct stat buf;
2918 assert( id );
2919 rc = fstat(((unixFile*)id)->h, &buf);
2920 SimulateIOError( rc=1 );
2921 if( rc!=0 ){
2922 ((unixFile*)id)->lastErrno = errno;
2923 return SQLITE_IOERR_FSTAT;
2924 }
2925 *pSize = buf.st_size;
2926
2927 /* When opening a zero-size database, the findLockInfo() procedure
2928 ** writes a single byte into that file in order to work around a bug
2929 ** in the OS-X msdos filesystem. In order to avoid problems with upper
2930 ** layers, we need to report this file size as zero even though it is
2931 ** really 1. Ticket #3260.
2932 */
2933 if( *pSize==1 ) *pSize = 0;
2934
2935
2936 return SQLITE_OK;
2937}
2938
drhd2cb50b2009-01-09 21:41:17 +00002939#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
drh715ff302008-12-03 22:32:44 +00002940/*
2941** Handler for proxy-locking file-control verbs. Defined below in the
2942** proxying locking division.
2943*/
2944static int proxyFileControl(sqlite3_file*,int,void*);
drh947bd802008-12-04 12:34:15 +00002945#endif
drh715ff302008-12-03 22:32:44 +00002946
danielk1977ad94b582007-08-20 06:44:22 +00002947
danielk1977e3026632004-06-22 11:29:02 +00002948/*
drh9e33c2c2007-08-31 18:34:59 +00002949** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00002950*/
drhcc6bb3e2007-08-31 16:11:35 +00002951static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00002952 switch( op ){
2953 case SQLITE_FCNTL_LOCKSTATE: {
2954 *(int*)pArg = ((unixFile*)id)->locktype;
2955 return SQLITE_OK;
2956 }
drh7708e972008-11-29 00:56:52 +00002957 case SQLITE_LAST_ERRNO: {
2958 *(int*)pArg = ((unixFile*)id)->lastErrno;
2959 return SQLITE_OK;
2960 }
drhd2cb50b2009-01-09 21:41:17 +00002961#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
drh715ff302008-12-03 22:32:44 +00002962 case SQLITE_SET_LOCKPROXYFILE:
aswiftaebf4132008-11-21 00:10:35 +00002963 case SQLITE_GET_LOCKPROXYFILE: {
drh715ff302008-12-03 22:32:44 +00002964 return proxyFileControl(id,op,pArg);
drh7708e972008-11-29 00:56:52 +00002965 }
drhd2cb50b2009-01-09 21:41:17 +00002966#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
drh9e33c2c2007-08-31 18:34:59 +00002967 }
drhcc6bb3e2007-08-31 16:11:35 +00002968 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00002969}
2970
2971/*
danielk1977a3d4c882007-03-23 10:08:38 +00002972** Return the sector size in bytes of the underlying block device for
2973** the specified file. This is almost always 512 bytes, but may be
2974** larger for some devices.
2975**
2976** SQLite code assumes this function cannot fail. It also assumes that
2977** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00002978** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00002979** same for both.
2980*/
danielk1977397d65f2008-11-19 11:35:39 +00002981static int unixSectorSize(sqlite3_file *NotUsed){
2982 UNUSED_PARAMETER(NotUsed);
drh3ceeb752007-03-29 18:19:52 +00002983 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002984}
2985
danielk197790949c22007-08-17 16:50:38 +00002986/*
danielk1977397d65f2008-11-19 11:35:39 +00002987** Return the device characteristics for the file. This is always 0 for unix.
danielk197790949c22007-08-17 16:50:38 +00002988*/
danielk1977397d65f2008-11-19 11:35:39 +00002989static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
2990 UNUSED_PARAMETER(NotUsed);
danielk197762079062007-08-15 17:08:46 +00002991 return 0;
2992}
2993
drh734c9862008-11-28 15:37:20 +00002994/*
2995** Here ends the implementation of all sqlite3_file methods.
2996**
2997********************** End sqlite3_file Methods *******************************
2998******************************************************************************/
2999
3000/*
drh6b9d6dd2008-12-03 19:34:47 +00003001** This division contains definitions of sqlite3_io_methods objects that
3002** implement various file locking strategies. It also contains definitions
3003** of "finder" functions. A finder-function is used to locate the appropriate
3004** sqlite3_io_methods object for a particular database file. The pAppData
3005** field of the sqlite3_vfs VFS objects are initialized to be pointers to
3006** the correct finder-function for that VFS.
3007**
3008** Most finder functions return a pointer to a fixed sqlite3_io_methods
3009** object. The only interesting finder-function is autolockIoFinder, which
3010** looks at the filesystem type and tries to guess the best locking
3011** strategy from that.
3012**
drh1875f7a2008-12-08 18:19:17 +00003013** For finder-funtion F, two objects are created:
3014**
3015** (1) The real finder-function named "FImpt()".
3016**
3017** (2) A constant pointer to this functio named just "F".
3018**
3019**
3020** A pointer to the F pointer is used as the pAppData value for VFS
3021** objects. We have to do this instead of letting pAppData point
3022** directly at the finder-function since C90 rules prevent a void*
3023** from be cast into a function pointer.
3024**
drh6b9d6dd2008-12-03 19:34:47 +00003025**
drh7708e972008-11-29 00:56:52 +00003026** Each instance of this macro generates two objects:
drh734c9862008-11-28 15:37:20 +00003027**
drh7708e972008-11-29 00:56:52 +00003028** * A constant sqlite3_io_methods object call METHOD that has locking
3029** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
3030**
3031** * An I/O method finder function called FINDER that returns a pointer
3032** to the METHOD object in the previous bullet.
drh734c9862008-11-28 15:37:20 +00003033*/
drh7708e972008-11-29 00:56:52 +00003034#define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK) \
3035static const sqlite3_io_methods METHOD = { \
3036 1, /* iVersion */ \
3037 CLOSE, /* xClose */ \
3038 unixRead, /* xRead */ \
3039 unixWrite, /* xWrite */ \
3040 unixTruncate, /* xTruncate */ \
3041 unixSync, /* xSync */ \
3042 unixFileSize, /* xFileSize */ \
3043 LOCK, /* xLock */ \
3044 UNLOCK, /* xUnlock */ \
3045 CKLOCK, /* xCheckReservedLock */ \
3046 unixFileControl, /* xFileControl */ \
3047 unixSectorSize, /* xSectorSize */ \
3048 unixDeviceCharacteristics /* xDeviceCapabilities */ \
3049}; \
drh1875f7a2008-12-08 18:19:17 +00003050static const sqlite3_io_methods *FINDER##Impl(const char *z, int h){ \
drh7708e972008-11-29 00:56:52 +00003051 UNUSED_PARAMETER(z); UNUSED_PARAMETER(h); \
3052 return &METHOD; \
drh1875f7a2008-12-08 18:19:17 +00003053} \
3054static const sqlite3_io_methods *(*const FINDER)(const char*,int) \
3055 = FINDER##Impl;
drh7708e972008-11-29 00:56:52 +00003056
3057/*
3058** Here are all of the sqlite3_io_methods objects for each of the
3059** locking strategies. Functions that return pointers to these methods
3060** are also created.
3061*/
3062IOMETHODS(
3063 posixIoFinder, /* Finder function name */
3064 posixIoMethods, /* sqlite3_io_methods object name */
3065 unixClose, /* xClose method */
3066 unixLock, /* xLock method */
3067 unixUnlock, /* xUnlock method */
3068 unixCheckReservedLock /* xCheckReservedLock method */
drh1875f7a2008-12-08 18:19:17 +00003069)
drh7708e972008-11-29 00:56:52 +00003070IOMETHODS(
3071 nolockIoFinder, /* Finder function name */
3072 nolockIoMethods, /* sqlite3_io_methods object name */
3073 nolockClose, /* xClose method */
3074 nolockLock, /* xLock method */
3075 nolockUnlock, /* xUnlock method */
3076 nolockCheckReservedLock /* xCheckReservedLock method */
drh1875f7a2008-12-08 18:19:17 +00003077)
drh7708e972008-11-29 00:56:52 +00003078IOMETHODS(
3079 dotlockIoFinder, /* Finder function name */
3080 dotlockIoMethods, /* sqlite3_io_methods object name */
3081 dotlockClose, /* xClose method */
3082 dotlockLock, /* xLock method */
3083 dotlockUnlock, /* xUnlock method */
3084 dotlockCheckReservedLock /* xCheckReservedLock method */
drh1875f7a2008-12-08 18:19:17 +00003085)
drh7708e972008-11-29 00:56:52 +00003086
3087#if SQLITE_ENABLE_LOCKING_STYLE
3088IOMETHODS(
3089 flockIoFinder, /* Finder function name */
3090 flockIoMethods, /* sqlite3_io_methods object name */
3091 flockClose, /* xClose method */
3092 flockLock, /* xLock method */
3093 flockUnlock, /* xUnlock method */
3094 flockCheckReservedLock /* xCheckReservedLock method */
drh1875f7a2008-12-08 18:19:17 +00003095)
drh7708e972008-11-29 00:56:52 +00003096#endif
3097
drh6c7d5c52008-11-21 20:32:33 +00003098#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00003099IOMETHODS(
3100 semIoFinder, /* Finder function name */
3101 semIoMethods, /* sqlite3_io_methods object name */
3102 semClose, /* xClose method */
3103 semLock, /* xLock method */
3104 semUnlock, /* xUnlock method */
3105 semCheckReservedLock /* xCheckReservedLock method */
drh1875f7a2008-12-08 18:19:17 +00003106)
aswiftaebf4132008-11-21 00:10:35 +00003107#endif
drh7708e972008-11-29 00:56:52 +00003108
drhd2cb50b2009-01-09 21:41:17 +00003109#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00003110IOMETHODS(
3111 afpIoFinder, /* Finder function name */
3112 afpIoMethods, /* sqlite3_io_methods object name */
3113 afpClose, /* xClose method */
3114 afpLock, /* xLock method */
3115 afpUnlock, /* xUnlock method */
3116 afpCheckReservedLock /* xCheckReservedLock method */
drh1875f7a2008-12-08 18:19:17 +00003117)
drh715ff302008-12-03 22:32:44 +00003118#endif
3119
3120/*
3121** The proxy locking method is a "super-method" in the sense that it
3122** opens secondary file descriptors for the conch and lock files and
3123** it uses proxy, dot-file, AFP, and flock() locking methods on those
3124** secondary files. For this reason, the division that implements
3125** proxy locking is located much further down in the file. But we need
3126** to go ahead and define the sqlite3_io_methods and finder function
3127** for proxy locking here. So we forward declare the I/O methods.
3128*/
drhd2cb50b2009-01-09 21:41:17 +00003129#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
drh715ff302008-12-03 22:32:44 +00003130static int proxyClose(sqlite3_file*);
3131static int proxyLock(sqlite3_file*, int);
3132static int proxyUnlock(sqlite3_file*, int);
3133static int proxyCheckReservedLock(sqlite3_file*, int*);
drh7708e972008-11-29 00:56:52 +00003134IOMETHODS(
3135 proxyIoFinder, /* Finder function name */
3136 proxyIoMethods, /* sqlite3_io_methods object name */
3137 proxyClose, /* xClose method */
3138 proxyLock, /* xLock method */
3139 proxyUnlock, /* xUnlock method */
3140 proxyCheckReservedLock /* xCheckReservedLock method */
drh1875f7a2008-12-08 18:19:17 +00003141)
aswiftaebf4132008-11-21 00:10:35 +00003142#endif
drh7708e972008-11-29 00:56:52 +00003143
3144
drhd2cb50b2009-01-09 21:41:17 +00003145#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00003146/*
drh6b9d6dd2008-12-03 19:34:47 +00003147** This "finder" function attempts to determine the best locking strategy
3148** for the database file "filePath". It then returns the sqlite3_io_methods
drh7708e972008-11-29 00:56:52 +00003149** object that implements that strategy.
3150**
3151** This is for MacOSX only.
3152*/
drh1875f7a2008-12-08 18:19:17 +00003153static const sqlite3_io_methods *autolockIoFinderImpl(
drh7708e972008-11-29 00:56:52 +00003154 const char *filePath, /* name of the database file */
3155 int fd /* file descriptor open on the database file */
3156){
3157 static const struct Mapping {
drh6b9d6dd2008-12-03 19:34:47 +00003158 const char *zFilesystem; /* Filesystem type name */
3159 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
drh7708e972008-11-29 00:56:52 +00003160 } aMap[] = {
3161 { "hfs", &posixIoMethods },
3162 { "ufs", &posixIoMethods },
3163 { "afpfs", &afpIoMethods },
3164#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
3165 { "smbfs", &afpIoMethods },
3166#else
3167 { "smbfs", &flockIoMethods },
3168#endif
3169 { "webdav", &nolockIoMethods },
3170 { 0, 0 }
3171 };
3172 int i;
3173 struct statfs fsInfo;
3174 struct flock lockInfo;
3175
3176 if( !filePath ){
drh6b9d6dd2008-12-03 19:34:47 +00003177 /* If filePath==NULL that means we are dealing with a transient file
3178 ** that does not need to be locked. */
drh7708e972008-11-29 00:56:52 +00003179 return &nolockIoMethods;
3180 }
3181 if( statfs(filePath, &fsInfo) != -1 ){
3182 if( fsInfo.f_flags & MNT_RDONLY ){
3183 return &nolockIoMethods;
3184 }
3185 for(i=0; aMap[i].zFilesystem; i++){
3186 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
3187 return aMap[i].pMethods;
3188 }
3189 }
3190 }
3191
3192 /* Default case. Handles, amongst others, "nfs".
3193 ** Test byte-range lock using fcntl(). If the call succeeds,
3194 ** assume that the file-system supports POSIX style locks.
drh734c9862008-11-28 15:37:20 +00003195 */
drh7708e972008-11-29 00:56:52 +00003196 lockInfo.l_len = 1;
3197 lockInfo.l_start = 0;
3198 lockInfo.l_whence = SEEK_SET;
3199 lockInfo.l_type = F_RDLCK;
3200 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
3201 return &posixIoMethods;
3202 }else{
3203 return &dotlockIoMethods;
3204 }
3205}
danielk1977852e2322008-12-22 03:36:59 +00003206static const sqlite3_io_methods *(*const autolockIoFinder)(const char*,int)
drh1875f7a2008-12-08 18:19:17 +00003207 = autolockIoFinderImpl;
3208
drhd2cb50b2009-01-09 21:41:17 +00003209#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
drh7708e972008-11-29 00:56:52 +00003210
3211/*
3212** An abstract type for a pointer to a IO method finder function:
3213*/
3214typedef const sqlite3_io_methods *(*finder_type)(const char*,int);
3215
aswiftaebf4132008-11-21 00:10:35 +00003216
drh734c9862008-11-28 15:37:20 +00003217/****************************************************************************
3218**************************** sqlite3_vfs methods ****************************
3219**
3220** This division contains the implementation of methods on the
3221** sqlite3_vfs object.
3222*/
3223
danielk1977a3d4c882007-03-23 10:08:38 +00003224/*
danielk1977e339d652008-06-28 11:23:00 +00003225** Initialize the contents of the unixFile structure pointed to by pId.
danielk1977ad94b582007-08-20 06:44:22 +00003226*/
3227static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00003228 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00003229 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00003230 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00003231 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00003232 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00003233 int noLock, /* Omit locking if true */
3234 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00003235){
drh7708e972008-11-29 00:56:52 +00003236 const sqlite3_io_methods *pLockingStyle;
drhda0e7682008-07-30 15:27:54 +00003237 unixFile *pNew = (unixFile *)pId;
3238 int rc = SQLITE_OK;
3239
danielk197717b90b52008-06-06 11:11:25 +00003240 assert( pNew->pLock==NULL );
3241 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00003242
drh715ff302008-12-03 22:32:44 +00003243 /* Parameter isDelete is only used on vxworks.
3244 ** Express this explicitly here to prevent compiler warnings
3245 ** about unused parameters.
danielk1977a03396a2008-11-19 14:35:46 +00003246 */
drh7708e972008-11-29 00:56:52 +00003247#if !OS_VXWORKS
3248 UNUSED_PARAMETER(isDelete);
3249#endif
danielk1977a03396a2008-11-19 14:35:46 +00003250
drh218c5082008-03-07 00:27:10 +00003251 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00003252 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00003253 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00003254 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00003255
drh6c7d5c52008-11-21 20:32:33 +00003256#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +00003257 pNew->pId = vxworksFindFileId(zFilename);
3258 if( pNew->pId==0 ){
3259 noLock = 1;
3260 rc = SQLITE_NOMEM;
chw97185482008-11-17 08:05:31 +00003261 }
3262#endif
3263
drhda0e7682008-07-30 15:27:54 +00003264 if( noLock ){
drh7708e972008-11-29 00:56:52 +00003265 pLockingStyle = &nolockIoMethods;
drhda0e7682008-07-30 15:27:54 +00003266 }else{
drh1875f7a2008-12-08 18:19:17 +00003267 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, h);
aswiftaebf4132008-11-21 00:10:35 +00003268#if SQLITE_ENABLE_LOCKING_STYLE
3269 /* Cache zFilename in the locking context (AFP and dotlock override) for
3270 ** proxyLock activation is possible (remote proxy is based on db name)
3271 ** zFilename remains valid until file is closed, to support */
3272 pNew->lockingContext = (void*)zFilename;
3273#endif
drhda0e7682008-07-30 15:27:54 +00003274 }
danielk1977e339d652008-06-28 11:23:00 +00003275
drh7708e972008-11-29 00:56:52 +00003276 if( pLockingStyle == &posixIoMethods ){
3277 unixEnterMutex();
3278 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
3279 unixLeaveMutex();
3280 }
danielk1977e339d652008-06-28 11:23:00 +00003281
drhd2cb50b2009-01-09 21:41:17 +00003282#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
aswiftf0551ee2008-12-03 21:26:19 +00003283 else if( pLockingStyle == &afpIoMethods ){
drh7708e972008-11-29 00:56:52 +00003284 /* AFP locking uses the file path so it needs to be included in
3285 ** the afpLockingContext.
3286 */
3287 afpLockingContext *pCtx;
3288 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
3289 if( pCtx==0 ){
3290 rc = SQLITE_NOMEM;
3291 }else{
3292 /* NB: zFilename exists and remains valid until the file is closed
3293 ** according to requirement F11141. So we do not need to make a
3294 ** copy of the filename. */
3295 pCtx->dbPath = zFilename;
3296 srandomdev();
drh6c7d5c52008-11-21 20:32:33 +00003297 unixEnterMutex();
drh7708e972008-11-29 00:56:52 +00003298 rc = findLockInfo(pNew, NULL, &pNew->pOpen);
3299 unixLeaveMutex();
drhbfe66312006-10-03 17:40:40 +00003300 }
drh7708e972008-11-29 00:56:52 +00003301 }
3302#endif
danielk1977e339d652008-06-28 11:23:00 +00003303
drh7708e972008-11-29 00:56:52 +00003304 else if( pLockingStyle == &dotlockIoMethods ){
3305 /* Dotfile locking uses the file path so it needs to be included in
3306 ** the dotlockLockingContext
3307 */
3308 char *zLockFile;
3309 int nFilename;
drhea678832008-12-10 19:26:22 +00003310 nFilename = (int)strlen(zFilename) + 6;
drh7708e972008-11-29 00:56:52 +00003311 zLockFile = (char *)sqlite3_malloc(nFilename);
3312 if( zLockFile==0 ){
3313 rc = SQLITE_NOMEM;
3314 }else{
3315 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
danielk1977e339d652008-06-28 11:23:00 +00003316 }
drh7708e972008-11-29 00:56:52 +00003317 pNew->lockingContext = zLockFile;
3318 }
danielk1977e339d652008-06-28 11:23:00 +00003319
drh6c7d5c52008-11-21 20:32:33 +00003320#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00003321 else if( pLockingStyle == &semIoMethods ){
3322 /* Named semaphore locking uses the file path so it needs to be
3323 ** included in the semLockingContext
3324 */
3325 unixEnterMutex();
3326 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
3327 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
3328 char *zSemName = pNew->pOpen->aSemName;
3329 int n;
3330 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem",
3331 pNew->pId->zCanonicalName);
3332 for( n=0; zSemName[n]; n++ )
3333 if( zSemName[n]=='/' ) zSemName[n] = '_';
3334 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
3335 if( pNew->pOpen->pSem == SEM_FAILED ){
3336 rc = SQLITE_NOMEM;
3337 pNew->pOpen->aSemName[0] = '\0';
chw97185482008-11-17 08:05:31 +00003338 }
chw97185482008-11-17 08:05:31 +00003339 }
drh7708e972008-11-29 00:56:52 +00003340 unixLeaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00003341 }
drh7708e972008-11-29 00:56:52 +00003342#endif
aswift5b1a2562008-08-22 00:22:35 +00003343
3344 pNew->lastErrno = 0;
drh6c7d5c52008-11-21 20:32:33 +00003345#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003346 if( rc!=SQLITE_OK ){
3347 unlink(zFilename);
3348 isDelete = 0;
3349 }
3350 pNew->isDelete = isDelete;
3351#endif
danielk1977e339d652008-06-28 11:23:00 +00003352 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003353 if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */
drhbfe66312006-10-03 17:40:40 +00003354 close(h);
danielk1977e339d652008-06-28 11:23:00 +00003355 }else{
drh7708e972008-11-29 00:56:52 +00003356 pNew->pMethod = pLockingStyle;
danielk1977e339d652008-06-28 11:23:00 +00003357 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00003358 }
danielk1977e339d652008-06-28 11:23:00 +00003359 return rc;
drh054889e2005-11-30 03:20:31 +00003360}
drh9c06c952005-11-26 00:25:00 +00003361
danielk1977ad94b582007-08-20 06:44:22 +00003362/*
3363** Open a file descriptor to the directory containing file zFilename.
3364** If successful, *pFd is set to the opened file descriptor and
3365** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
3366** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
3367** value.
3368**
3369** If SQLITE_OK is returned, the caller is responsible for closing
3370** the file descriptor *pFd using close().
3371*/
danielk1977fee2d252007-08-18 10:59:19 +00003372static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00003373 int ii;
drh777b17a2007-09-20 10:02:54 +00003374 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00003375 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00003376
drh153c62c2007-08-24 03:51:33 +00003377 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
drh617634e2009-01-08 14:36:20 +00003378 for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
danielk1977fee2d252007-08-18 10:59:19 +00003379 if( ii>0 ){
3380 zDirname[ii] = '\0';
3381 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00003382 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003383#ifdef FD_CLOEXEC
3384 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3385#endif
3386 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
3387 }
3388 }
danielk1977fee2d252007-08-18 10:59:19 +00003389 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00003390 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00003391}
3392
danielk1977b4b47412007-08-17 15:53:36 +00003393/*
danielk197717b90b52008-06-06 11:11:25 +00003394** Create a temporary file name in zBuf. zBuf must be allocated
3395** by the calling process and must be big enough to hold at least
3396** pVfs->mxPathname bytes.
3397*/
3398static int getTempname(int nBuf, char *zBuf){
3399 static const char *azDirs[] = {
3400 0,
aswiftaebf4132008-11-21 00:10:35 +00003401 0,
danielk197717b90b52008-06-06 11:11:25 +00003402 "/var/tmp",
3403 "/usr/tmp",
3404 "/tmp",
3405 ".",
3406 };
3407 static const unsigned char zChars[] =
3408 "abcdefghijklmnopqrstuvwxyz"
3409 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3410 "0123456789";
drh41022642008-11-21 00:24:42 +00003411 unsigned int i, j;
danielk197717b90b52008-06-06 11:11:25 +00003412 struct stat buf;
3413 const char *zDir = ".";
3414
3415 /* It's odd to simulate an io-error here, but really this is just
3416 ** using the io-error infrastructure to test that SQLite handles this
3417 ** function failing.
3418 */
3419 SimulateIOError( return SQLITE_IOERR );
3420
3421 azDirs[0] = sqlite3_temp_directory;
aswiftaebf4132008-11-21 00:10:35 +00003422 if (NULL == azDirs[1]) {
3423 azDirs[1] = getenv("TMPDIR");
3424 }
3425
3426 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
danielk197717b90b52008-06-06 11:11:25 +00003427 if( azDirs[i]==0 ) continue;
3428 if( stat(azDirs[i], &buf) ) continue;
3429 if( !S_ISDIR(buf.st_mode) ) continue;
3430 if( access(azDirs[i], 07) ) continue;
3431 zDir = azDirs[i];
3432 break;
3433 }
3434
3435 /* Check that the output buffer is large enough for the temporary file
3436 ** name. If it is not, return SQLITE_ERROR.
3437 */
danielk197700e13612008-11-17 19:18:54 +00003438 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00003439 return SQLITE_ERROR;
3440 }
3441
3442 do{
3443 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
drhea678832008-12-10 19:26:22 +00003444 j = (int)strlen(zBuf);
danielk197717b90b52008-06-06 11:11:25 +00003445 sqlite3_randomness(15, &zBuf[j]);
3446 for(i=0; i<15; i++, j++){
3447 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3448 }
3449 zBuf[j] = 0;
3450 }while( access(zBuf,0)==0 );
3451 return SQLITE_OK;
3452}
3453
drhd2cb50b2009-01-09 21:41:17 +00003454#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
drhc66d5b62008-12-03 22:48:32 +00003455/*
3456** Routine to transform a unixFile into a proxy-locking unixFile.
3457** Implementation in the proxy-lock division, but used by unixOpen()
3458** if SQLITE_PREFER_PROXY_LOCKING is defined.
3459*/
3460static int proxyTransformUnixFile(unixFile*, const char*);
drh947bd802008-12-04 12:34:15 +00003461#endif
drhc66d5b62008-12-03 22:48:32 +00003462
danielk197717b90b52008-06-06 11:11:25 +00003463
3464/*
danielk1977ad94b582007-08-20 06:44:22 +00003465** Open the file zPath.
3466**
danielk1977b4b47412007-08-17 15:53:36 +00003467** Previously, the SQLite OS layer used three functions in place of this
3468** one:
3469**
3470** sqlite3OsOpenReadWrite();
3471** sqlite3OsOpenReadOnly();
3472** sqlite3OsOpenExclusive();
3473**
3474** These calls correspond to the following combinations of flags:
3475**
3476** ReadWrite() -> (READWRITE | CREATE)
3477** ReadOnly() -> (READONLY)
3478** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
3479**
3480** The old OpenExclusive() accepted a boolean argument - "delFlag". If
3481** true, the file was configured to be automatically deleted when the
3482** file handle closed. To achieve the same effect using this new
3483** interface, add the DELETEONCLOSE flag to those specified above for
3484** OpenExclusive().
3485*/
3486static int unixOpen(
drh6b9d6dd2008-12-03 19:34:47 +00003487 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
3488 const char *zPath, /* Pathname of file to be opened */
3489 sqlite3_file *pFile, /* The file descriptor to be filled in */
3490 int flags, /* Input flags to control the opening */
3491 int *pOutFlags /* Output flags returned to SQLite core */
danielk1977b4b47412007-08-17 15:53:36 +00003492){
danielk1977fee2d252007-08-18 10:59:19 +00003493 int fd = 0; /* File descriptor returned by open() */
3494 int dirfd = -1; /* Directory file descriptor */
drh6b9d6dd2008-12-03 19:34:47 +00003495 int openFlags = 0; /* Flags to pass to open() */
danielk1977fee2d252007-08-18 10:59:19 +00003496 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00003497 int noLock; /* True to omit locking primitives */
aswiftaebf4132008-11-21 00:10:35 +00003498 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003499
3500 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3501 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3502 int isCreate = (flags & SQLITE_OPEN_CREATE);
3503 int isReadonly = (flags & SQLITE_OPEN_READONLY);
3504 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3505
danielk1977fee2d252007-08-18 10:59:19 +00003506 /* If creating a master or main-file journal, this function will open
3507 ** a file-descriptor on the directory too. The first time unixSync()
3508 ** is called the directory file descriptor will be fsync()ed and close()d.
3509 */
3510 int isOpenDirectory = (isCreate &&
3511 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
3512 );
3513
danielk197717b90b52008-06-06 11:11:25 +00003514 /* If argument zPath is a NULL pointer, this function is required to open
3515 ** a temporary file. Use this buffer to store the file name in.
3516 */
3517 char zTmpname[MAX_PATHNAME+1];
3518 const char *zName = zPath;
3519
danielk1977fee2d252007-08-18 10:59:19 +00003520 /* Check the following statements are true:
3521 **
3522 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3523 ** (b) if CREATE is set, then READWRITE must also be set, and
3524 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00003525 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00003526 */
danielk1977b4b47412007-08-17 15:53:36 +00003527 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00003528 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00003529 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00003530 assert(isDelete==0 || isCreate);
3531
drh33f4e022007-09-03 15:19:34 +00003532 /* The main DB, main journal, and master journal are never automatically
3533 ** deleted
3534 */
3535 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
3536 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
3537 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00003538
danielk1977fee2d252007-08-18 10:59:19 +00003539 /* Assert that the upper layer has set one of the "file-type" flags. */
3540 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
3541 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
3542 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00003543 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00003544 );
3545
danielk1977e339d652008-06-28 11:23:00 +00003546 memset(pFile, 0, sizeof(unixFile));
3547
danielk197717b90b52008-06-06 11:11:25 +00003548 if( !zName ){
danielk197717b90b52008-06-06 11:11:25 +00003549 assert(isDelete && !isOpenDirectory);
3550 rc = getTempname(MAX_PATHNAME+1, zTmpname);
3551 if( rc!=SQLITE_OK ){
3552 return rc;
3553 }
3554 zName = zTmpname;
3555 }
3556
drh734c9862008-11-28 15:37:20 +00003557 if( isReadonly ) openFlags |= O_RDONLY;
3558 if( isReadWrite ) openFlags |= O_RDWR;
3559 if( isCreate ) openFlags |= O_CREAT;
3560 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
3561 openFlags |= (O_LARGEFILE|O_BINARY);
danielk1977b4b47412007-08-17 15:53:36 +00003562
drh734c9862008-11-28 15:37:20 +00003563 fd = open(zName, openFlags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
3564 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, openFlags);
danielk19772f2d8c72007-08-30 16:13:33 +00003565 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00003566 /* Failed to open the file for read/write access. Try read-only. */
3567 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
3568 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00003569 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00003570 }
3571 if( fd<0 ){
3572 return SQLITE_CANTOPEN;
3573 }
3574 if( isDelete ){
drh6c7d5c52008-11-21 20:32:33 +00003575#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003576 zPath = zName;
3577#else
danielk197717b90b52008-06-06 11:11:25 +00003578 unlink(zName);
chw97185482008-11-17 08:05:31 +00003579#endif
danielk1977b4b47412007-08-17 15:53:36 +00003580 }
drh41022642008-11-21 00:24:42 +00003581#if SQLITE_ENABLE_LOCKING_STYLE
3582 else{
drh734c9862008-11-28 15:37:20 +00003583 ((unixFile*)pFile)->openFlags = openFlags;
drh41022642008-11-21 00:24:42 +00003584 }
3585#endif
danielk1977b4b47412007-08-17 15:53:36 +00003586 if( pOutFlags ){
3587 *pOutFlags = flags;
3588 }
3589
3590 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00003591 if( isOpenDirectory ){
aswiftaebf4132008-11-21 00:10:35 +00003592 rc = openDirectory(zPath, &dirfd);
danielk1977fee2d252007-08-18 10:59:19 +00003593 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003594 close(fd); /* silently leak if fail, already in error */
danielk1977fee2d252007-08-18 10:59:19 +00003595 return rc;
3596 }
3597 }
danielk1977e339d652008-06-28 11:23:00 +00003598
3599#ifdef FD_CLOEXEC
3600 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3601#endif
3602
drhda0e7682008-07-30 15:27:54 +00003603 noLock = eType!=SQLITE_OPEN_MAIN_DB;
aswiftaebf4132008-11-21 00:10:35 +00003604
3605#if SQLITE_PREFER_PROXY_LOCKING
3606 if( zPath!=NULL && !noLock ){
3607 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
3608 int useProxy = 0;
3609
3610 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy,
drh7708e972008-11-29 00:56:52 +00003611 ** 0 means never use proxy, NULL means use proxy for non-local files only
3612 */
aswiftaebf4132008-11-21 00:10:35 +00003613 if( envforce!=NULL ){
3614 useProxy = atoi(envforce)>0;
3615 }else{
3616 struct statfs fsInfo;
3617
3618 if( statfs(zPath, &fsInfo) == -1 ){
3619 ((unixFile*)pFile)->lastErrno = errno;
3620 if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
3621 close(fd); /* silently leak if fail, in error */
3622 return SQLITE_IOERR_ACCESS;
3623 }
3624 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
3625 }
3626 if( useProxy ){
3627 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
3628 if( rc==SQLITE_OK ){
drh715ff302008-12-03 22:32:44 +00003629 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
aswiftaebf4132008-11-21 00:10:35 +00003630 }
3631 return rc;
3632 }
3633 }
3634#endif
3635
chw97185482008-11-17 08:05:31 +00003636 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00003637}
3638
3639/*
danielk1977fee2d252007-08-18 10:59:19 +00003640** Delete the file at zPath. If the dirSync argument is true, fsync()
3641** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00003642*/
drh6b9d6dd2008-12-03 19:34:47 +00003643static int unixDelete(
3644 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
3645 const char *zPath, /* Name of file to be deleted */
3646 int dirSync /* If true, fsync() directory after deleting file */
3647){
danielk1977fee2d252007-08-18 10:59:19 +00003648 int rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00003649 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003650 SimulateIOError(return SQLITE_IOERR_DELETE);
3651 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00003652#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00003653 if( dirSync ){
3654 int fd;
3655 rc = openDirectory(zPath, &fd);
3656 if( rc==SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00003657#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003658 if( fsync(fd)==-1 )
3659#else
3660 if( fsync(fd) )
3661#endif
3662 {
danielk1977fee2d252007-08-18 10:59:19 +00003663 rc = SQLITE_IOERR_DIR_FSYNC;
3664 }
aswiftaebf4132008-11-21 00:10:35 +00003665 if( close(fd)&&!rc ){
3666 rc = SQLITE_IOERR_DIR_CLOSE;
3667 }
danielk1977fee2d252007-08-18 10:59:19 +00003668 }
3669 }
danielk1977d138dd82008-10-15 16:02:48 +00003670#endif
danielk1977fee2d252007-08-18 10:59:19 +00003671 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00003672}
3673
danielk197790949c22007-08-17 16:50:38 +00003674/*
3675** Test the existance of or access permissions of file zPath. The
3676** test performed depends on the value of flags:
3677**
3678** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
3679** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
3680** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
3681**
3682** Otherwise return 0.
3683*/
danielk1977861f7452008-06-05 11:39:11 +00003684static int unixAccess(
drh6b9d6dd2008-12-03 19:34:47 +00003685 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
3686 const char *zPath, /* Path of the file to examine */
3687 int flags, /* What do we want to learn about the zPath file? */
3688 int *pResOut /* Write result boolean here */
danielk1977861f7452008-06-05 11:39:11 +00003689){
rse25c0d1a2007-09-20 08:38:14 +00003690 int amode = 0;
danielk1977397d65f2008-11-19 11:35:39 +00003691 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00003692 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00003693 switch( flags ){
3694 case SQLITE_ACCESS_EXISTS:
3695 amode = F_OK;
3696 break;
3697 case SQLITE_ACCESS_READWRITE:
3698 amode = W_OK|R_OK;
3699 break;
drh50d3f902007-08-27 21:10:36 +00003700 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00003701 amode = R_OK;
3702 break;
3703
3704 default:
3705 assert(!"Invalid flags argument");
3706 }
danielk1977861f7452008-06-05 11:39:11 +00003707 *pResOut = (access(zPath, amode)==0);
3708 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003709}
3710
danielk1977b4b47412007-08-17 15:53:36 +00003711
3712/*
3713** Turn a relative pathname into a full pathname. The relative path
3714** is stored as a nul-terminated string in the buffer pointed to by
3715** zPath.
3716**
3717** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
3718** (in this case, MAX_PATHNAME bytes). The full-path is written to
3719** this buffer before returning.
3720*/
danielk1977adfb9b02007-09-17 07:02:56 +00003721static int unixFullPathname(
3722 sqlite3_vfs *pVfs, /* Pointer to vfs object */
3723 const char *zPath, /* Possibly relative input path */
3724 int nOut, /* Size of output buffer in bytes */
3725 char *zOut /* Output buffer */
3726){
danielk1977843e65f2007-09-01 16:16:15 +00003727
3728 /* It's odd to simulate an io-error here, but really this is just
3729 ** using the io-error infrastructure to test that SQLite handles this
3730 ** function failing. This function could fail if, for example, the
drh6b9d6dd2008-12-03 19:34:47 +00003731 ** current working directory has been unlinked.
danielk1977843e65f2007-09-01 16:16:15 +00003732 */
3733 SimulateIOError( return SQLITE_ERROR );
3734
drh153c62c2007-08-24 03:51:33 +00003735 assert( pVfs->mxPathname==MAX_PATHNAME );
danielk1977f3d3c272008-11-19 16:52:44 +00003736 UNUSED_PARAMETER(pVfs);
chw97185482008-11-17 08:05:31 +00003737
drh3c7f2dc2007-12-06 13:26:20 +00003738 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00003739 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00003740 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003741 }else{
3742 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00003743 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00003744 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00003745 }
drhea678832008-12-10 19:26:22 +00003746 nCwd = (int)strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00003747 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003748 }
3749 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003750}
3751
drh0ccebe72005-06-07 22:22:50 +00003752
drh761df872006-12-21 01:29:22 +00003753#ifndef SQLITE_OMIT_LOAD_EXTENSION
3754/*
3755** Interfaces for opening a shared library, finding entry points
3756** within the shared library, and closing the shared library.
3757*/
3758#include <dlfcn.h>
danielk1977397d65f2008-11-19 11:35:39 +00003759static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
3760 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003761 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
3762}
danielk197795c8a542007-09-01 06:51:27 +00003763
3764/*
3765** SQLite calls this function immediately after a call to unixDlSym() or
3766** unixDlOpen() fails (returns a null pointer). If a more detailed error
3767** message is available, it is written to zBufOut. If no error message
3768** is available, zBufOut is left unmodified and SQLite uses a default
3769** error message.
3770*/
danielk1977397d65f2008-11-19 11:35:39 +00003771static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00003772 char *zErr;
danielk1977397d65f2008-11-19 11:35:39 +00003773 UNUSED_PARAMETER(NotUsed);
drh6c7d5c52008-11-21 20:32:33 +00003774 unixEnterMutex();
danielk1977b4b47412007-08-17 15:53:36 +00003775 zErr = dlerror();
3776 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00003777 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00003778 }
drh6c7d5c52008-11-21 20:32:33 +00003779 unixLeaveMutex();
danielk1977b4b47412007-08-17 15:53:36 +00003780}
drh1875f7a2008-12-08 18:19:17 +00003781static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
3782 /*
3783 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
3784 ** cast into a pointer to a function. And yet the library dlsym() routine
3785 ** returns a void* which is really a pointer to a function. So how do we
3786 ** use dlsym() with -pedantic-errors?
3787 **
3788 ** Variable x below is defined to be a pointer to a function taking
3789 ** parameters void* and const char* and returning a pointer to a function.
3790 ** We initialize x by assigning it a pointer to the dlsym() function.
3791 ** (That assignment requires a cast.) Then we call the function that
3792 ** x points to.
3793 **
3794 ** This work-around is unlikely to work correctly on any system where
3795 ** you really cannot cast a function pointer into void*. But then, on the
3796 ** other hand, dlsym() will not work on such a system either, so we have
3797 ** not really lost anything.
3798 */
3799 void (*(*x)(void*,const char*))(void);
danielk1977397d65f2008-11-19 11:35:39 +00003800 UNUSED_PARAMETER(NotUsed);
drh1875f7a2008-12-08 18:19:17 +00003801 x = (void(*(*)(void*,const char*))(void))dlsym;
3802 return (*x)(p, zSym);
drh761df872006-12-21 01:29:22 +00003803}
danielk1977397d65f2008-11-19 11:35:39 +00003804static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
3805 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003806 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00003807}
danielk1977b4b47412007-08-17 15:53:36 +00003808#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
3809 #define unixDlOpen 0
3810 #define unixDlError 0
3811 #define unixDlSym 0
3812 #define unixDlClose 0
3813#endif
3814
3815/*
danielk197790949c22007-08-17 16:50:38 +00003816** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00003817*/
danielk1977397d65f2008-11-19 11:35:39 +00003818static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
3819 UNUSED_PARAMETER(NotUsed);
danielk197700e13612008-11-17 19:18:54 +00003820 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00003821
drhbbd42a62004-05-22 17:41:58 +00003822 /* We have to initialize zBuf to prevent valgrind from reporting
3823 ** errors. The reports issued by valgrind are incorrect - we would
3824 ** prefer that the randomness be increased by making use of the
3825 ** uninitialized space in zBuf - but valgrind errors tend to worry
3826 ** some users. Rather than argue, it seems easier just to initialize
3827 ** the whole array and silence valgrind, even if that means less randomness
3828 ** in the random seed.
3829 **
3830 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00003831 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00003832 ** tests repeatable.
3833 */
danielk1977b4b47412007-08-17 15:53:36 +00003834 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00003835#if !defined(SQLITE_TEST)
3836 {
drh842b8642005-01-21 17:53:17 +00003837 int pid, fd;
3838 fd = open("/dev/urandom", O_RDONLY);
3839 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00003840 time_t t;
3841 time(&t);
danielk197790949c22007-08-17 16:50:38 +00003842 memcpy(zBuf, &t, sizeof(t));
3843 pid = getpid();
3844 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00003845 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00003846 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00003847 }else{
drh72cbd072008-10-14 17:58:38 +00003848 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00003849 close(fd);
3850 }
drhbbd42a62004-05-22 17:41:58 +00003851 }
3852#endif
drh72cbd072008-10-14 17:58:38 +00003853 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00003854}
3855
danielk1977b4b47412007-08-17 15:53:36 +00003856
drhbbd42a62004-05-22 17:41:58 +00003857/*
3858** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00003859** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00003860** The return value is the number of microseconds of sleep actually
3861** requested from the underlying operating system, a number which
3862** might be greater than or equal to the argument, but not less
3863** than the argument.
drhbbd42a62004-05-22 17:41:58 +00003864*/
danielk1977397d65f2008-11-19 11:35:39 +00003865static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
drh6c7d5c52008-11-21 20:32:33 +00003866#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003867 struct timespec sp;
3868
3869 sp.tv_sec = microseconds / 1000000;
3870 sp.tv_nsec = (microseconds % 1000000) * 1000;
3871 nanosleep(&sp, NULL);
danielk1977397d65f2008-11-19 11:35:39 +00003872 return microseconds;
3873#elif defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00003874 usleep(microseconds);
3875 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00003876#else
danielk1977b4b47412007-08-17 15:53:36 +00003877 int seconds = (microseconds+999999)/1000000;
3878 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00003879 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00003880#endif
danielk1977397d65f2008-11-19 11:35:39 +00003881 UNUSED_PARAMETER(NotUsed);
drh88f474a2006-01-02 20:00:12 +00003882}
3883
3884/*
drh6b9d6dd2008-12-03 19:34:47 +00003885** The following variable, if set to a non-zero value, is interpreted as
3886** the number of seconds since 1970 and is used to set the result of
3887** sqlite3OsCurrentTime() during testing.
drhbbd42a62004-05-22 17:41:58 +00003888*/
3889#ifdef SQLITE_TEST
drh6b9d6dd2008-12-03 19:34:47 +00003890int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
drhbbd42a62004-05-22 17:41:58 +00003891#endif
3892
3893/*
3894** Find the current time (in Universal Coordinated Time). Write the
3895** current time and date as a Julian Day number into *prNow and
3896** return 0. Return 1 if the time and date cannot be found.
3897*/
danielk1977397d65f2008-11-19 11:35:39 +00003898static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
drh6c7d5c52008-11-21 20:32:33 +00003899#if defined(NO_GETTOD)
drhbbd42a62004-05-22 17:41:58 +00003900 time_t t;
3901 time(&t);
3902 *prNow = t/86400.0 + 2440587.5;
drh6c7d5c52008-11-21 20:32:33 +00003903#elif OS_VXWORKS
3904 struct timespec sNow;
3905 clock_gettime(CLOCK_REALTIME, &sNow);
3906 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
drh19e2d372005-08-29 23:00:03 +00003907#else
3908 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00003909 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00003910 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
3911#endif
danielk1977397d65f2008-11-19 11:35:39 +00003912
drhbbd42a62004-05-22 17:41:58 +00003913#ifdef SQLITE_TEST
3914 if( sqlite3_current_time ){
3915 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
3916 }
3917#endif
danielk1977397d65f2008-11-19 11:35:39 +00003918 UNUSED_PARAMETER(NotUsed);
drhbbd42a62004-05-22 17:41:58 +00003919 return 0;
3920}
danielk1977b4b47412007-08-17 15:53:36 +00003921
drh6b9d6dd2008-12-03 19:34:47 +00003922/*
3923** We added the xGetLastError() method with the intention of providing
3924** better low-level error messages when operating-system problems come up
3925** during SQLite operation. But so far, none of that has been implemented
3926** in the core. So this routine is never called. For now, it is merely
3927** a place-holder.
3928*/
danielk1977397d65f2008-11-19 11:35:39 +00003929static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
3930 UNUSED_PARAMETER(NotUsed);
3931 UNUSED_PARAMETER(NotUsed2);
3932 UNUSED_PARAMETER(NotUsed3);
danielk1977bcb97fe2008-06-06 15:49:29 +00003933 return 0;
3934}
3935
drh153c62c2007-08-24 03:51:33 +00003936/*
drh734c9862008-11-28 15:37:20 +00003937************************ End of sqlite3_vfs methods ***************************
3938******************************************************************************/
3939
drh715ff302008-12-03 22:32:44 +00003940/******************************************************************************
3941************************** Begin Proxy Locking ********************************
3942**
3943** Proxy locking is a "uber-locking-method" in this sense: It uses the
3944** other locking methods on secondary lock files. Proxy locking is a
3945** meta-layer over top of the primitive locking implemented above. For
3946** this reason, the division that implements of proxy locking is deferred
3947** until late in the file (here) after all of the other I/O methods have
3948** been defined - so that the primitive locking methods are available
3949** as services to help with the implementation of proxy locking.
3950**
3951****
3952**
3953** The default locking schemes in SQLite use byte-range locks on the
3954** database file to coordinate safe, concurrent access by multiple readers
3955** and writers [http://sqlite.org/lockingv3.html]. The five file locking
3956** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
3957** as POSIX read & write locks over fixed set of locations (via fsctl),
3958** on AFP and SMB only exclusive byte-range locks are available via fsctl
3959** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
3960** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
3961** address in the shared range is taken for a SHARED lock, the entire
3962** shared range is taken for an EXCLUSIVE lock):
3963**
3964** PENDING_BYTE 0x40000000
3965** RESERVED_BYTE 0x40000001
3966** SHARED_RANGE 0x40000002 -> 0x40000200
3967**
3968** This works well on the local file system, but shows a nearly 100x
3969** slowdown in read performance on AFP because the AFP client disables
3970** the read cache when byte-range locks are present. Enabling the read
3971** cache exposes a cache coherency problem that is present on all OS X
3972** supported network file systems. NFS and AFP both observe the
3973** close-to-open semantics for ensuring cache coherency
3974** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
3975** address the requirements for concurrent database access by multiple
3976** readers and writers
3977** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
3978**
3979** To address the performance and cache coherency issues, proxy file locking
3980** changes the way database access is controlled by limiting access to a
3981** single host at a time and moving file locks off of the database file
3982** and onto a proxy file on the local file system.
3983**
3984**
3985** Using proxy locks
3986** -----------------
3987**
3988** C APIs
3989**
3990** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
3991** <proxy_path> | ":auto:");
3992** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
3993**
3994**
3995** SQL pragmas
3996**
3997** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
3998** PRAGMA [database.]lock_proxy_file
3999**
4000** Specifying ":auto:" means that if there is a conch file with a matching
4001** host ID in it, the proxy path in the conch file will be used, otherwise
4002** a proxy path based on the user's temp dir
4003** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
4004** actual proxy file name is generated from the name and path of the
4005** database file. For example:
4006**
4007** For database path "/Users/me/foo.db"
4008** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
4009**
4010** Once a lock proxy is configured for a database connection, it can not
4011** be removed, however it may be switched to a different proxy path via
4012** the above APIs (assuming the conch file is not being held by another
4013** connection or process).
4014**
4015**
4016** How proxy locking works
4017** -----------------------
4018**
4019** Proxy file locking relies primarily on two new supporting files:
4020**
4021** * conch file to limit access to the database file to a single host
4022** at a time
4023**
4024** * proxy file to act as a proxy for the advisory locks normally
4025** taken on the database
4026**
4027** The conch file - to use a proxy file, sqlite must first "hold the conch"
4028** by taking an sqlite-style shared lock on the conch file, reading the
4029** contents and comparing the host's unique host ID (see below) and lock
4030** proxy path against the values stored in the conch. The conch file is
4031** stored in the same directory as the database file and the file name
4032** is patterned after the database file name as ".<databasename>-conch".
4033** If the conch file does not exist, or it's contents do not match the
4034** host ID and/or proxy path, then the lock is escalated to an exclusive
4035** lock and the conch file contents is updated with the host ID and proxy
4036** path and the lock is downgraded to a shared lock again. If the conch
4037** is held by another process (with a shared lock), the exclusive lock
4038** will fail and SQLITE_BUSY is returned.
4039**
4040** The proxy file - a single-byte file used for all advisory file locks
4041** normally taken on the database file. This allows for safe sharing
4042** of the database file for multiple readers and writers on the same
4043** host (the conch ensures that they all use the same local lock file).
4044**
4045** There is a third file - the host ID file - used as a persistent record
4046** of a unique identifier for the host, a 128-byte unique host id file
4047** in the path defined by the HOSTIDPATH macro (default value is
4048** /Library/Caches/.com.apple.sqliteConchHostId).
4049**
4050** Requesting the lock proxy does not immediately take the conch, it is
4051** only taken when the first request to lock database file is made.
4052** This matches the semantics of the traditional locking behavior, where
4053** opening a connection to a database file does not take a lock on it.
4054** The shared lock and an open file descriptor are maintained until
4055** the connection to the database is closed.
4056**
4057** The proxy file and the lock file are never deleted so they only need
4058** to be created the first time they are used.
4059**
4060** Configuration options
4061** ---------------------
4062**
4063** SQLITE_PREFER_PROXY_LOCKING
4064**
4065** Database files accessed on non-local file systems are
4066** automatically configured for proxy locking, lock files are
4067** named automatically using the same logic as
4068** PRAGMA lock_proxy_file=":auto:"
4069**
4070** SQLITE_PROXY_DEBUG
4071**
4072** Enables the logging of error messages during host id file
4073** retrieval and creation
4074**
4075** HOSTIDPATH
4076**
4077** Overrides the default host ID file path location
4078**
4079** LOCKPROXYDIR
4080**
4081** Overrides the default directory used for lock proxy files that
4082** are named automatically via the ":auto:" setting
4083**
4084** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
4085**
4086** Permissions to use when creating a directory for storing the
4087** lock proxy files, only used when LOCKPROXYDIR is not set.
4088**
4089**
4090** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
4091** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
4092** force proxy locking to be used for every database file opened, and 0
4093** will force automatic proxy locking to be disabled for all database
4094** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
4095** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
4096*/
4097
4098/*
4099** Proxy locking is only available on MacOSX
4100*/
drhd2cb50b2009-01-09 21:41:17 +00004101#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
drh715ff302008-12-03 22:32:44 +00004102
4103#ifdef SQLITE_TEST
4104/* simulate multiple hosts by creating unique hostid file paths */
4105int sqlite3_hostid_num = 0;
4106#endif
4107
4108/*
4109** The proxyLockingContext has the path and file structures for the remote
4110** and local proxy files in it
4111*/
4112typedef struct proxyLockingContext proxyLockingContext;
4113struct proxyLockingContext {
4114 unixFile *conchFile; /* Open conch file */
4115 char *conchFilePath; /* Name of the conch file */
4116 unixFile *lockProxy; /* Open proxy lock file */
4117 char *lockProxyPath; /* Name of the proxy lock file */
4118 char *dbPath; /* Name of the open file */
4119 int conchHeld; /* True if the conch is currently held */
4120 void *oldLockingContext; /* Original lockingcontext to restore on close */
4121 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
4122};
4123
4124/* HOSTIDLEN and CONCHLEN both include space for the string
4125** terminating nul
4126*/
4127#define HOSTIDLEN 128
4128#define CONCHLEN (MAXPATHLEN+HOSTIDLEN+1)
4129#ifndef HOSTIDPATH
4130# define HOSTIDPATH "/Library/Caches/.com.apple.sqliteConchHostId"
4131#endif
4132
4133/* basically a copy of unixRandomness with different
4134** test behavior built in */
4135static int proxyGenerateHostID(char *pHostID){
4136 int pid, fd, len;
4137 unsigned char *key = (unsigned char *)pHostID;
4138
4139 memset(key, 0, HOSTIDLEN);
4140 len = 0;
4141 fd = open("/dev/urandom", O_RDONLY);
4142 if( fd>=0 ){
4143 len = read(fd, key, HOSTIDLEN);
4144 close(fd); /* silently leak the fd if it fails */
4145 }
4146 if( len < HOSTIDLEN ){
4147 time_t t;
4148 time(&t);
4149 memcpy(key, &t, sizeof(t));
4150 pid = getpid();
4151 memcpy(&key[sizeof(t)], &pid, sizeof(pid));
4152 }
4153
4154#ifdef MAKE_PRETTY_HOSTID
4155 {
4156 int i;
4157 /* filter the bytes into printable ascii characters and NUL terminate */
4158 key[(HOSTIDLEN-1)] = 0x00;
4159 for( i=0; i<(HOSTIDLEN-1); i++ ){
4160 unsigned char pa = key[i]&0x7F;
4161 if( pa<0x20 ){
4162 key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20;
4163 }else if( pa==0x7F ){
4164 key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E;
4165 }
4166 }
4167 }
4168#endif
4169 return SQLITE_OK;
4170}
4171
4172/* writes the host id path to path, path should be an pre-allocated buffer
4173** with enough space for a path
4174*/
4175static void proxyGetHostIDPath(char *path, size_t len){
4176 strlcpy(path, HOSTIDPATH, len);
4177#ifdef SQLITE_TEST
4178 if( sqlite3_hostid_num>0 ){
4179 char suffix[2] = "1";
4180 suffix[0] = suffix[0] + sqlite3_hostid_num;
4181 strlcat(path, suffix, len);
4182 }
4183#endif
4184 OSTRACE3("GETHOSTIDPATH %s pid=%d\n", path, getpid());
4185}
4186
4187/* get the host ID from a sqlite hostid file stored in the
4188** user-specific tmp directory, create the ID if it's not there already
4189*/
4190static int proxyGetHostID(char *pHostID, int *pError){
4191 int fd;
4192 char path[MAXPATHLEN];
4193 size_t len;
4194 int rc=SQLITE_OK;
4195
4196 proxyGetHostIDPath(path, MAXPATHLEN);
4197 /* try to create the host ID file, if it already exists read the contents */
4198 fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644);
4199 if( fd<0 ){
4200 int err=errno;
4201
4202 if( err!=EEXIST ){
4203#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
4204 fprintf(stderr, "sqlite error creating host ID file %s: %s\n",
4205 path, strerror(err));
4206#endif
4207 return SQLITE_PERM;
4208 }
4209 /* couldn't create the file, read it instead */
4210 fd = open(path, O_RDONLY|O_EXCL);
4211 if( fd<0 ){
4212#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
4213 int err = errno;
4214 fprintf(stderr, "sqlite error opening host ID file %s: %s\n",
4215 path, strerror(err));
4216#endif
4217 return SQLITE_PERM;
4218 }
4219 len = pread(fd, pHostID, HOSTIDLEN, 0);
4220 if( len<0 ){
4221 *pError = errno;
4222 rc = SQLITE_IOERR_READ;
4223 }else if( len<HOSTIDLEN ){
4224 *pError = 0;
4225 rc = SQLITE_IOERR_SHORT_READ;
4226 }
4227 close(fd); /* silently leak the fd if it fails */
4228 OSTRACE3("GETHOSTID read %s pid=%d\n", pHostID, getpid());
4229 return rc;
4230 }else{
4231 /* we're creating the host ID file (use a random string of bytes) */
4232 proxyGenerateHostID(pHostID);
4233 len = pwrite(fd, pHostID, HOSTIDLEN, 0);
4234 if( len<0 ){
4235 *pError = errno;
4236 rc = SQLITE_IOERR_WRITE;
4237 }else if( len<HOSTIDLEN ){
4238 *pError = 0;
4239 rc = SQLITE_IOERR_WRITE;
4240 }
4241 close(fd); /* silently leak the fd if it fails */
4242 OSTRACE3("GETHOSTID wrote %s pid=%d\n", pHostID, getpid());
4243 return rc;
4244 }
4245}
4246
4247static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
4248 int len;
4249 int dbLen;
4250 int i;
4251
4252#ifdef LOCKPROXYDIR
4253 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
4254#else
4255# ifdef _CS_DARWIN_USER_TEMP_DIR
4256 {
4257 confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
4258 len = strlcat(lPath, "sqliteplocks", maxLen);
4259 if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
4260 /* if mkdir fails, handle as lock file creation failure */
4261 int err = errno;
4262# ifdef SQLITE_DEBUG
4263 if( err!=EEXIST ){
4264 fprintf(stderr, "proxyGetLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
4265 SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
4266 }
4267# endif
4268 }else{
4269 OSTRACE3("GETLOCKPATH mkdir %s pid=%d\n", lPath, getpid());
4270 }
4271
4272 }
4273# else
4274 len = strlcpy(lPath, "/tmp/", maxLen);
4275# endif
4276#endif
4277
4278 if( lPath[len-1]!='/' ){
4279 len = strlcat(lPath, "/", maxLen);
4280 }
4281
4282 /* transform the db path to a unique cache name */
drhea678832008-12-10 19:26:22 +00004283 dbLen = (int)strlen(dbPath);
drh715ff302008-12-03 22:32:44 +00004284 for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
4285 char c = dbPath[i];
4286 lPath[i+len] = (c=='/')?'_':c;
4287 }
4288 lPath[i+len]='\0';
4289 strlcat(lPath, ":auto:", maxLen);
4290 return SQLITE_OK;
4291}
4292
4293/*
4294** Create a new VFS file descriptor (stored in memory obtained from
4295** sqlite3_malloc) and open the file named "path" in the file descriptor.
4296**
4297** The caller is responsible not only for closing the file descriptor
4298** but also for freeing the memory associated with the file descriptor.
4299*/
4300static int proxyCreateUnixFile(const char *path, unixFile **ppFile) {
4301 int fd;
4302 int dirfd = -1;
4303 unixFile *pNew;
4304 int rc = SQLITE_OK;
4305 sqlite3_vfs dummyVfs;
4306
4307 fd = open(path, O_RDWR | O_CREAT, SQLITE_DEFAULT_FILE_PERMISSIONS);
4308 if( fd<0 ){
4309 return SQLITE_CANTOPEN;
4310 }
4311
4312 pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
4313 if( pNew==NULL ){
4314 rc = SQLITE_NOMEM;
4315 goto end_create_proxy;
4316 }
4317 memset(pNew, 0, sizeof(unixFile));
4318
drh1875f7a2008-12-08 18:19:17 +00004319 dummyVfs.pAppData = (void*)&autolockIoFinder;
drh715ff302008-12-03 22:32:44 +00004320 rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
4321 if( rc==SQLITE_OK ){
4322 *ppFile = pNew;
4323 return SQLITE_OK;
4324 }
4325end_create_proxy:
4326 close(fd); /* silently leak fd if error, we're already in error */
4327 sqlite3_free(pNew);
4328 return rc;
4329}
4330
4331/* takes the conch by taking a shared lock and read the contents conch, if
4332** lockPath is non-NULL, the host ID and lock file path must match. A NULL
4333** lockPath means that the lockPath in the conch file will be used if the
4334** host IDs match, or a new lock path will be generated automatically
4335** and written to the conch file.
4336*/
4337static int proxyTakeConch(unixFile *pFile){
4338 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4339
4340 if( pCtx->conchHeld>0 ){
4341 return SQLITE_OK;
4342 }else{
4343 unixFile *conchFile = pCtx->conchFile;
4344 char testValue[CONCHLEN];
4345 char conchValue[CONCHLEN];
4346 char lockPath[MAXPATHLEN];
4347 char *tLockPath = NULL;
4348 int rc = SQLITE_OK;
4349 int readRc = SQLITE_OK;
4350 int syncPerms = 0;
4351
4352 OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h,
4353 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
4354
4355 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
4356 if( rc==SQLITE_OK ){
4357 int pError = 0;
drh1875f7a2008-12-08 18:19:17 +00004358 memset(testValue, 0, CONCHLEN); /* conch is fixed size */
drh715ff302008-12-03 22:32:44 +00004359 rc = proxyGetHostID(testValue, &pError);
4360 if( (rc&0xff)==SQLITE_IOERR ){
4361 pFile->lastErrno = pError;
4362 }
4363 if( pCtx->lockProxyPath ){
4364 strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN);
4365 }
4366 }
4367 if( rc!=SQLITE_OK ){
4368 goto end_takeconch;
4369 }
4370
4371 readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0);
4372 if( readRc!=SQLITE_IOERR_SHORT_READ ){
4373 if( readRc!=SQLITE_OK ){
4374 if( (rc&0xff)==SQLITE_IOERR ){
4375 pFile->lastErrno = conchFile->lastErrno;
4376 }
4377 rc = readRc;
4378 goto end_takeconch;
4379 }
4380 /* if the conch has data compare the contents */
4381 if( !pCtx->lockProxyPath ){
4382 /* for auto-named local lock file, just check the host ID and we'll
4383 ** use the local lock file path that's already in there */
4384 if( !memcmp(testValue, conchValue, HOSTIDLEN) ){
4385 tLockPath = (char *)&conchValue[HOSTIDLEN];
4386 goto end_takeconch;
4387 }
4388 }else{
4389 /* we've got the conch if conchValue matches our path and host ID */
4390 if( !memcmp(testValue, conchValue, CONCHLEN) ){
4391 goto end_takeconch;
4392 }
4393 }
4394 }else{
4395 /* a short read means we're "creating" the conch (even though it could
4396 ** have been user-intervention), if we acquire the exclusive lock,
4397 ** we'll try to match the current on-disk permissions of the database
4398 */
4399 syncPerms = 1;
4400 }
4401
4402 /* either conch was emtpy or didn't match */
4403 if( !pCtx->lockProxyPath ){
4404 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
4405 tLockPath = lockPath;
4406 strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN);
4407 }
4408
4409 /* update conch with host and path (this will fail if other process
4410 ** has a shared lock already) */
4411 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
4412 if( rc==SQLITE_OK ){
4413 rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0);
4414 if( rc==SQLITE_OK && syncPerms ){
4415 struct stat buf;
4416 int err = fstat(pFile->h, &buf);
4417 if( err==0 ){
4418 /* try to match the database file permissions, ignore failure */
4419#ifndef SQLITE_PROXY_DEBUG
4420 fchmod(conchFile->h, buf.st_mode);
4421#else
4422 if( fchmod(conchFile->h, buf.st_mode)!=0 ){
4423 int code = errno;
4424 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
4425 buf.st_mode, code, strerror(code));
4426 } else {
4427 fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode);
4428 }
4429 }else{
4430 int code = errno;
4431 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
4432 err, code, strerror(code));
4433#endif
4434 }
4435 }
4436 }
4437 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
4438
4439end_takeconch:
4440 OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h);
4441 if( rc==SQLITE_OK && pFile->openFlags ){
4442 if( pFile->h>=0 ){
4443#ifdef STRICT_CLOSE_ERROR
4444 if( close(pFile->h) ){
4445 pFile->lastErrno = errno;
4446 return SQLITE_IOERR_CLOSE;
4447 }
4448#else
4449 close(pFile->h); /* silently leak fd if fail */
4450#endif
4451 }
4452 pFile->h = -1;
4453 int fd = open(pCtx->dbPath, pFile->openFlags,
4454 SQLITE_DEFAULT_FILE_PERMISSIONS);
4455 OSTRACE2("TRANSPROXY: OPEN %d\n", fd);
4456 if( fd>=0 ){
4457 pFile->h = fd;
4458 }else{
drh1875f7a2008-12-08 18:19:17 +00004459 rc=SQLITE_CANTOPEN; /* SQLITE_BUSY? proxyTakeConch called
4460 during locking */
drh715ff302008-12-03 22:32:44 +00004461 }
4462 }
4463 if( rc==SQLITE_OK && !pCtx->lockProxy ){
4464 char *path = tLockPath ? tLockPath : pCtx->lockProxyPath;
drh1875f7a2008-12-08 18:19:17 +00004465 /* ACS: Need to make a copy of path sometimes */
drh715ff302008-12-03 22:32:44 +00004466 rc = proxyCreateUnixFile(path, &pCtx->lockProxy);
4467 }
4468 if( rc==SQLITE_OK ){
4469 pCtx->conchHeld = 1;
4470
4471 if( tLockPath ){
4472 pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath);
4473 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
4474 ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath =
4475 pCtx->lockProxyPath;
4476 }
4477 }
4478 } else {
4479 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
4480 }
4481 OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed");
4482 return rc;
4483 }
4484}
4485
4486/*
4487** If pFile holds a lock on a conch file, then release that lock.
4488*/
4489static int proxyReleaseConch(unixFile *pFile){
4490 int rc; /* Subroutine return code */
4491 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
4492 unixFile *conchFile; /* Name of the conch file */
4493
4494 pCtx = (proxyLockingContext *)pFile->lockingContext;
4495 conchFile = pCtx->conchFile;
4496 OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
4497 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
4498 getpid());
4499 pCtx->conchHeld = 0;
4500 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
4501 OSTRACE3("RELEASECONCH %d %s\n", conchFile->h,
4502 (rc==SQLITE_OK ? "ok" : "failed"));
4503 return rc;
4504}
4505
4506/*
4507** Given the name of a database file, compute the name of its conch file.
4508** Store the conch filename in memory obtained from sqlite3_malloc().
4509** Make *pConchPath point to the new name. Return SQLITE_OK on success
4510** or SQLITE_NOMEM if unable to obtain memory.
4511**
4512** The caller is responsible for ensuring that the allocated memory
4513** space is eventually freed.
4514**
4515** *pConchPath is set to NULL if a memory allocation error occurs.
4516*/
4517static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
4518 int i; /* Loop counter */
drhea678832008-12-10 19:26:22 +00004519 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
drh715ff302008-12-03 22:32:44 +00004520 char *conchPath; /* buffer in which to construct conch name */
4521
4522 /* Allocate space for the conch filename and initialize the name to
4523 ** the name of the original database file. */
4524 *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
4525 if( conchPath==0 ){
4526 return SQLITE_NOMEM;
4527 }
4528 memcpy(conchPath, dbPath, len+1);
4529
4530 /* now insert a "." before the last / character */
4531 for( i=(len-1); i>=0; i-- ){
4532 if( conchPath[i]=='/' ){
4533 i++;
4534 break;
4535 }
4536 }
4537 conchPath[i]='.';
4538 while ( i<len ){
4539 conchPath[i+1]=dbPath[i];
4540 i++;
4541 }
4542
4543 /* append the "-conch" suffix to the file */
4544 memcpy(&conchPath[i+1], "-conch", 7);
drhea678832008-12-10 19:26:22 +00004545 assert( (int)strlen(conchPath) == len+7 );
drh715ff302008-12-03 22:32:44 +00004546
4547 return SQLITE_OK;
4548}
4549
4550
4551/* Takes a fully configured proxy locking-style unix file and switches
4552** the local lock file path
4553*/
4554static int switchLockProxyPath(unixFile *pFile, const char *path) {
4555 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
4556 char *oldPath = pCtx->lockProxyPath;
4557 int rc = SQLITE_OK;
4558
4559 if( pFile->locktype!=NO_LOCK ){
4560 return SQLITE_BUSY;
4561 }
4562
4563 /* nothing to do if the path is NULL, :auto: or matches the existing path */
4564 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
4565 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
4566 return SQLITE_OK;
4567 }else{
4568 unixFile *lockProxy = pCtx->lockProxy;
4569 pCtx->lockProxy=NULL;
4570 pCtx->conchHeld = 0;
4571 if( lockProxy!=NULL ){
4572 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
4573 if( rc ) return rc;
4574 sqlite3_free(lockProxy);
4575 }
4576 sqlite3_free(oldPath);
4577 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
4578 }
4579
4580 return rc;
4581}
4582
4583/*
4584** pFile is a file that has been opened by a prior xOpen call. dbPath
4585** is a string buffer at least MAXPATHLEN+1 characters in size.
4586**
4587** This routine find the filename associated with pFile and writes it
4588** int dbPath.
4589*/
4590static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
drhd2cb50b2009-01-09 21:41:17 +00004591#if defined(__APPLE__)
drh715ff302008-12-03 22:32:44 +00004592 if( pFile->pMethod == &afpIoMethods ){
4593 /* afp style keeps a reference to the db path in the filePath field
4594 ** of the struct */
drhea678832008-12-10 19:26:22 +00004595 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
drh715ff302008-12-03 22:32:44 +00004596 strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath);
4597 }else
4598#endif
4599 if( pFile->pMethod == &dotlockIoMethods ){
4600 /* dot lock style uses the locking context to store the dot lock
4601 ** file path */
4602 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
4603 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
4604 }else{
4605 /* all other styles use the locking context to store the db file path */
4606 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
4607 strcpy(dbPath, (char *)pFile->lockingContext);
4608 }
4609 return SQLITE_OK;
4610}
4611
4612/*
4613** Takes an already filled in unix file and alters it so all file locking
4614** will be performed on the local proxy lock file. The following fields
4615** are preserved in the locking context so that they can be restored and
4616** the unix structure properly cleaned up at close time:
4617** ->lockingContext
4618** ->pMethod
4619*/
4620static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
4621 proxyLockingContext *pCtx;
4622 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
4623 char *lockPath=NULL;
4624 int rc = SQLITE_OK;
4625
4626 if( pFile->locktype!=NO_LOCK ){
4627 return SQLITE_BUSY;
4628 }
4629 proxyGetDbPathForUnixFile(pFile, dbPath);
4630 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
4631 lockPath=NULL;
4632 }else{
4633 lockPath=(char *)path;
4634 }
4635
4636 OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h,
4637 (lockPath ? lockPath : ":auto:"), getpid());
4638
4639 pCtx = sqlite3_malloc( sizeof(*pCtx) );
4640 if( pCtx==0 ){
4641 return SQLITE_NOMEM;
4642 }
4643 memset(pCtx, 0, sizeof(*pCtx));
4644
4645 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
4646 if( rc==SQLITE_OK ){
4647 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
4648 }
4649 if( rc==SQLITE_OK && lockPath ){
4650 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
4651 }
4652
4653 if( rc==SQLITE_OK ){
4654 /* all memory is allocated, proxys are created and assigned,
4655 ** switch the locking context and pMethod then return.
4656 */
4657 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
4658 pCtx->oldLockingContext = pFile->lockingContext;
4659 pFile->lockingContext = pCtx;
4660 pCtx->pOldMethod = pFile->pMethod;
4661 pFile->pMethod = &proxyIoMethods;
4662 }else{
4663 if( pCtx->conchFile ){
4664 rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
4665 if( rc ) return rc;
4666 sqlite3_free(pCtx->conchFile);
4667 }
4668 sqlite3_free(pCtx->conchFilePath);
4669 sqlite3_free(pCtx);
4670 }
4671 OSTRACE3("TRANSPROXY %d %s\n", pFile->h,
4672 (rc==SQLITE_OK ? "ok" : "failed"));
4673 return rc;
4674}
4675
4676
4677/*
4678** This routine handles sqlite3_file_control() calls that are specific
4679** to proxy locking.
4680*/
4681static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
4682 switch( op ){
4683 case SQLITE_GET_LOCKPROXYFILE: {
4684 unixFile *pFile = (unixFile*)id;
4685 if( pFile->pMethod == &proxyIoMethods ){
4686 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
4687 proxyTakeConch(pFile);
4688 if( pCtx->lockProxyPath ){
4689 *(const char **)pArg = pCtx->lockProxyPath;
4690 }else{
4691 *(const char **)pArg = ":auto: (not held)";
4692 }
4693 } else {
4694 *(const char **)pArg = NULL;
4695 }
4696 return SQLITE_OK;
4697 }
4698 case SQLITE_SET_LOCKPROXYFILE: {
4699 unixFile *pFile = (unixFile*)id;
4700 int rc = SQLITE_OK;
4701 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
4702 if( pArg==NULL || (const char *)pArg==0 ){
4703 if( isProxyStyle ){
4704 /* turn off proxy locking - not supported */
4705 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
4706 }else{
4707 /* turn off proxy locking - already off - NOOP */
4708 rc = SQLITE_OK;
4709 }
4710 }else{
4711 const char *proxyPath = (const char *)pArg;
4712 if( isProxyStyle ){
4713 proxyLockingContext *pCtx =
4714 (proxyLockingContext*)pFile->lockingContext;
4715 if( !strcmp(pArg, ":auto:")
4716 || (pCtx->lockProxyPath &&
4717 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
4718 ){
4719 rc = SQLITE_OK;
4720 }else{
4721 rc = switchLockProxyPath(pFile, proxyPath);
4722 }
4723 }else{
4724 /* turn on proxy file locking */
4725 rc = proxyTransformUnixFile(pFile, proxyPath);
4726 }
4727 }
4728 return rc;
4729 }
4730 default: {
4731 assert( 0 ); /* The call assures that only valid opcodes are sent */
4732 }
4733 }
4734 /*NOTREACHED*/
4735 return SQLITE_ERROR;
4736}
4737
4738/*
4739** Within this division (the proxying locking implementation) the procedures
4740** above this point are all utilities. The lock-related methods of the
4741** proxy-locking sqlite3_io_method object follow.
4742*/
4743
4744
4745/*
4746** This routine checks if there is a RESERVED lock held on the specified
4747** file by this or any other process. If such a lock is held, set *pResOut
4748** to a non-zero value otherwise *pResOut is set to zero. The return value
4749** is set to SQLITE_OK unless an I/O error occurs during lock checking.
4750*/
4751static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
4752 unixFile *pFile = (unixFile*)id;
4753 int rc = proxyTakeConch(pFile);
4754 if( rc==SQLITE_OK ){
4755 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4756 unixFile *proxy = pCtx->lockProxy;
4757 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
4758 }
4759 return rc;
4760}
4761
4762/*
4763** Lock the file with the lock specified by parameter locktype - one
4764** of the following:
4765**
4766** (1) SHARED_LOCK
4767** (2) RESERVED_LOCK
4768** (3) PENDING_LOCK
4769** (4) EXCLUSIVE_LOCK
4770**
4771** Sometimes when requesting one lock state, additional lock states
4772** are inserted in between. The locking might fail on one of the later
4773** transitions leaving the lock state different from what it started but
4774** still short of its goal. The following chart shows the allowed
4775** transitions and the inserted intermediate states:
4776**
4777** UNLOCKED -> SHARED
4778** SHARED -> RESERVED
4779** SHARED -> (PENDING) -> EXCLUSIVE
4780** RESERVED -> (PENDING) -> EXCLUSIVE
4781** PENDING -> EXCLUSIVE
4782**
4783** This routine will only increase a lock. Use the sqlite3OsUnlock()
4784** routine to lower a locking level.
4785*/
4786static int proxyLock(sqlite3_file *id, int locktype) {
4787 unixFile *pFile = (unixFile*)id;
4788 int rc = proxyTakeConch(pFile);
4789 if( rc==SQLITE_OK ){
4790 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4791 unixFile *proxy = pCtx->lockProxy;
4792 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
4793 pFile->locktype = proxy->locktype;
4794 }
4795 return rc;
4796}
4797
4798
4799/*
4800** Lower the locking level on file descriptor pFile to locktype. locktype
4801** must be either NO_LOCK or SHARED_LOCK.
4802**
4803** If the locking level of the file descriptor is already at or below
4804** the requested locking level, this routine is a no-op.
4805*/
4806static int proxyUnlock(sqlite3_file *id, int locktype) {
4807 unixFile *pFile = (unixFile*)id;
4808 int rc = proxyTakeConch(pFile);
4809 if( rc==SQLITE_OK ){
4810 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4811 unixFile *proxy = pCtx->lockProxy;
4812 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
4813 pFile->locktype = proxy->locktype;
4814 }
4815 return rc;
4816}
4817
4818/*
4819** Close a file that uses proxy locks.
4820*/
4821static int proxyClose(sqlite3_file *id) {
4822 if( id ){
4823 unixFile *pFile = (unixFile*)id;
4824 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4825 unixFile *lockProxy = pCtx->lockProxy;
4826 unixFile *conchFile = pCtx->conchFile;
4827 int rc = SQLITE_OK;
4828
4829 if( lockProxy ){
4830 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
4831 if( rc ) return rc;
4832 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
4833 if( rc ) return rc;
4834 sqlite3_free(lockProxy);
4835 pCtx->lockProxy = 0;
4836 }
4837 if( conchFile ){
4838 if( pCtx->conchHeld ){
4839 rc = proxyReleaseConch(pFile);
4840 if( rc ) return rc;
4841 }
4842 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
4843 if( rc ) return rc;
4844 sqlite3_free(conchFile);
4845 }
4846 sqlite3_free(pCtx->lockProxyPath);
4847 sqlite3_free(pCtx->conchFilePath);
4848 sqlite3_free(pCtx->dbPath);
4849 /* restore the original locking context and pMethod then close it */
4850 pFile->lockingContext = pCtx->oldLockingContext;
4851 pFile->pMethod = pCtx->pOldMethod;
4852 sqlite3_free(pCtx);
4853 return pFile->pMethod->xClose(id);
4854 }
4855 return SQLITE_OK;
4856}
4857
4858
4859
drhd2cb50b2009-01-09 21:41:17 +00004860#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
drh715ff302008-12-03 22:32:44 +00004861/*
4862** The proxy locking style is intended for use with AFP filesystems.
4863** And since AFP is only supported on MacOSX, the proxy locking is also
4864** restricted to MacOSX.
4865**
4866**
4867******************* End of the proxy lock implementation **********************
4868******************************************************************************/
4869
drh734c9862008-11-28 15:37:20 +00004870/*
danielk1977e339d652008-06-28 11:23:00 +00004871** Initialize the operating system interface.
drh734c9862008-11-28 15:37:20 +00004872**
4873** This routine registers all VFS implementations for unix-like operating
4874** systems. This routine, and the sqlite3_os_end() routine that follows,
4875** should be the only routines in this file that are visible from other
4876** files.
drh6b9d6dd2008-12-03 19:34:47 +00004877**
4878** This routine is called once during SQLite initialization and by a
4879** single thread. The memory allocation and mutex subsystems have not
4880** necessarily been initialized when this routine is called, and so they
4881** should not be used.
drh153c62c2007-08-24 03:51:33 +00004882*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004883int sqlite3_os_init(void){
drh6b9d6dd2008-12-03 19:34:47 +00004884 /*
4885 ** The following macro defines an initializer for an sqlite3_vfs object.
drh1875f7a2008-12-08 18:19:17 +00004886 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
4887 ** to the "finder" function. (pAppData is a pointer to a pointer because
4888 ** silly C90 rules prohibit a void* from being cast to a function pointer
4889 ** and so we have to go through the intermediate pointer to avoid problems
4890 ** when compiling with -pedantic-errors on GCC.)
4891 **
4892 ** The FINDER parameter to this macro is the name of the pointer to the
drh6b9d6dd2008-12-03 19:34:47 +00004893 ** finder-function. The finder-function returns a pointer to the
4894 ** sqlite_io_methods object that implements the desired locking
4895 ** behaviors. See the division above that contains the IOMETHODS
4896 ** macro for addition information on finder-functions.
4897 **
4898 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
4899 ** object. But the "autolockIoFinder" available on MacOSX does a little
4900 ** more than that; it looks at the filesystem type that hosts the
4901 ** database file and tries to choose an locking method appropriate for
4902 ** that filesystem time.
danielk1977e339d652008-06-28 11:23:00 +00004903 */
drh7708e972008-11-29 00:56:52 +00004904 #define UNIXVFS(VFSNAME, FINDER) { \
danielk1977e339d652008-06-28 11:23:00 +00004905 1, /* iVersion */ \
4906 sizeof(unixFile), /* szOsFile */ \
4907 MAX_PATHNAME, /* mxPathname */ \
4908 0, /* pNext */ \
drh7708e972008-11-29 00:56:52 +00004909 VFSNAME, /* zName */ \
drh1875f7a2008-12-08 18:19:17 +00004910 (void*)&FINDER, /* pAppData */ \
danielk1977e339d652008-06-28 11:23:00 +00004911 unixOpen, /* xOpen */ \
4912 unixDelete, /* xDelete */ \
4913 unixAccess, /* xAccess */ \
4914 unixFullPathname, /* xFullPathname */ \
4915 unixDlOpen, /* xDlOpen */ \
4916 unixDlError, /* xDlError */ \
4917 unixDlSym, /* xDlSym */ \
4918 unixDlClose, /* xDlClose */ \
4919 unixRandomness, /* xRandomness */ \
4920 unixSleep, /* xSleep */ \
4921 unixCurrentTime, /* xCurrentTime */ \
4922 unixGetLastError /* xGetLastError */ \
4923 }
4924
drh6b9d6dd2008-12-03 19:34:47 +00004925 /*
4926 ** All default VFSes for unix are contained in the following array.
4927 **
4928 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
4929 ** by the SQLite core when the VFS is registered. So the following
4930 ** array cannot be const.
4931 */
danielk1977e339d652008-06-28 11:23:00 +00004932 static sqlite3_vfs aVfs[] = {
drhd2cb50b2009-01-09 21:41:17 +00004933#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
drh7708e972008-11-29 00:56:52 +00004934 UNIXVFS("unix", autolockIoFinder ),
4935#else
4936 UNIXVFS("unix", posixIoFinder ),
4937#endif
4938 UNIXVFS("unix-none", nolockIoFinder ),
4939 UNIXVFS("unix-dotfile", dotlockIoFinder ),
drh734c9862008-11-28 15:37:20 +00004940#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00004941 UNIXVFS("unix-namedsem", semIoFinder ),
drh734c9862008-11-28 15:37:20 +00004942#endif
4943#if SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00004944 UNIXVFS("unix-posix", posixIoFinder ),
4945 UNIXVFS("unix-flock", flockIoFinder ),
drh734c9862008-11-28 15:37:20 +00004946#endif
drhd2cb50b2009-01-09 21:41:17 +00004947#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
drh7708e972008-11-29 00:56:52 +00004948 UNIXVFS("unix-afp", afpIoFinder ),
4949 UNIXVFS("unix-proxy", proxyIoFinder ),
drh734c9862008-11-28 15:37:20 +00004950#endif
drh153c62c2007-08-24 03:51:33 +00004951 };
drh6b9d6dd2008-12-03 19:34:47 +00004952 unsigned int i; /* Loop counter */
4953
4954 /* Register all VFSes defined in the aVfs[] array */
danielk1977e339d652008-06-28 11:23:00 +00004955 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
drh734c9862008-11-28 15:37:20 +00004956 sqlite3_vfs_register(&aVfs[i], i==0);
danielk1977e339d652008-06-28 11:23:00 +00004957 }
danielk1977c0fa4c52008-06-25 17:19:00 +00004958 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00004959}
danielk1977e339d652008-06-28 11:23:00 +00004960
4961/*
drh6b9d6dd2008-12-03 19:34:47 +00004962** Shutdown the operating system interface.
4963**
4964** Some operating systems might need to do some cleanup in this routine,
4965** to release dynamically allocated objects. But not on unix.
4966** This routine is a no-op for unix.
danielk1977e339d652008-06-28 11:23:00 +00004967*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004968int sqlite3_os_end(void){
4969 return SQLITE_OK;
4970}
drhdce8bdb2007-08-16 13:01:44 +00004971
danielk197729bafea2008-06-26 10:41:19 +00004972#endif /* SQLITE_OS_UNIX */