blob: 349725765c80f40175817dc214524088b7e29363 [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**
drh947bd802008-12-04 12:34:15 +000046** $Id: os_unix.c,v 1.229 2008/12/04 12:34:16 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)
69# if defined(__DARWIN__)
70# 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]=='/' );
486 n = strlen(zAbsoluteName);
487 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
2177#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
2178/*
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 //SimulateIOErrorBenign(1);
2221 //SimulateIOError( pb.fd=(-1) )
2222 //SimulateIOErrorBenign(0);
2223
2224 OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
drh734c9862008-11-28 15:37:20 +00002225 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
2226 offset, length);
drhbfe66312006-10-03 17:40:40 +00002227 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
2228 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00002229 int rc;
2230 int tErrno = errno;
drh734c9862008-11-28 15:37:20 +00002231 OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
2232 path, tErrno, strerror(tErrno));
aswiftaebf4132008-11-21 00:10:35 +00002233#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2234 rc = SQLITE_BUSY;
2235#else
drh734c9862008-11-28 15:37:20 +00002236 rc = sqliteErrorFromPosixError(tErrno,
2237 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
aswiftaebf4132008-11-21 00:10:35 +00002238#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002239 if( IS_LOCK_ERROR(rc) ){
2240 pFile->lastErrno = tErrno;
2241 }
2242 return rc;
drhbfe66312006-10-03 17:40:40 +00002243 } else {
aswift5b1a2562008-08-22 00:22:35 +00002244 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002245 }
2246}
2247
drh6b9d6dd2008-12-03 19:34:47 +00002248/*
2249** This routine checks if there is a RESERVED lock held on the specified
2250** file by this or any other process. If such a lock is held, set *pResOut
2251** to a non-zero value otherwise *pResOut is set to zero. The return value
2252** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2253*/
danielk1977e339d652008-06-28 11:23:00 +00002254static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002255 int rc = SQLITE_OK;
2256 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002257 unixFile *pFile = (unixFile*)id;
2258
aswift5b1a2562008-08-22 00:22:35 +00002259 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2260
2261 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00002262 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2263
2264 /* Check if a thread in this process holds such a lock */
2265 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002266 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00002267 }
2268
2269 /* Otherwise see if some other process holds it.
2270 */
aswift5b1a2562008-08-22 00:22:35 +00002271 if( !reserved ){
2272 /* lock the RESERVED byte */
drh6b9d6dd2008-12-03 19:34:47 +00002273 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
aswift5b1a2562008-08-22 00:22:35 +00002274 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00002275 /* if we succeeded in taking the reserved lock, unlock it to restore
2276 ** the original state */
drh6b9d6dd2008-12-03 19:34:47 +00002277 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswift5b1a2562008-08-22 00:22:35 +00002278 } else {
2279 /* if we failed to get the lock then someone else must have it */
2280 reserved = 1;
2281 }
2282 if( IS_LOCK_ERROR(lrc) ){
2283 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00002284 }
2285 }
drhbfe66312006-10-03 17:40:40 +00002286
aswift5b1a2562008-08-22 00:22:35 +00002287 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2288
2289 *pResOut = reserved;
2290 return rc;
drhbfe66312006-10-03 17:40:40 +00002291}
2292
drh6b9d6dd2008-12-03 19:34:47 +00002293/*
2294** Lock the file with the lock specified by parameter locktype - one
2295** of the following:
2296**
2297** (1) SHARED_LOCK
2298** (2) RESERVED_LOCK
2299** (3) PENDING_LOCK
2300** (4) EXCLUSIVE_LOCK
2301**
2302** Sometimes when requesting one lock state, additional lock states
2303** are inserted in between. The locking might fail on one of the later
2304** transitions leaving the lock state different from what it started but
2305** still short of its goal. The following chart shows the allowed
2306** transitions and the inserted intermediate states:
2307**
2308** UNLOCKED -> SHARED
2309** SHARED -> RESERVED
2310** SHARED -> (PENDING) -> EXCLUSIVE
2311** RESERVED -> (PENDING) -> EXCLUSIVE
2312** PENDING -> EXCLUSIVE
2313**
2314** This routine will only increase a lock. Use the sqlite3OsUnlock()
2315** routine to lower a locking level.
2316*/
danielk1977e339d652008-06-28 11:23:00 +00002317static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00002318 int rc = SQLITE_OK;
2319 unixFile *pFile = (unixFile*)id;
2320 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002321
2322 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002323 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002324 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2325
drhbfe66312006-10-03 17:40:40 +00002326 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002327 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
drh6c7d5c52008-11-21 20:32:33 +00002328 ** unixEnterMutex() hasn't been called yet.
drh339eb0b2008-03-07 15:34:11 +00002329 */
drhbfe66312006-10-03 17:40:40 +00002330 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002331 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002332 locktypeName(locktype));
2333 return SQLITE_OK;
2334 }
2335
2336 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002337 */
drhbfe66312006-10-03 17:40:40 +00002338 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2339 assert( locktype!=PENDING_LOCK );
2340 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2341
2342 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002343 */
drh6c7d5c52008-11-21 20:32:33 +00002344 unixEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002345
2346 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002347 */
drhbfe66312006-10-03 17:40:40 +00002348 rc = transferOwnership(pFile);
2349 if( rc!=SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00002350 unixLeaveMutex();
drhbfe66312006-10-03 17:40:40 +00002351 return rc;
2352 }
2353
2354 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002355 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2356 ** be released.
2357 */
drhbfe66312006-10-03 17:40:40 +00002358 if( locktype==SHARED_LOCK
2359 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002360 ){
2361 int failed;
drh6b9d6dd2008-12-03 19:34:47 +00002362 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002363 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002364 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002365 goto afp_end_lock;
2366 }
2367 }
2368
2369 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002370 ** operating system calls for the specified lock.
2371 */
drhbfe66312006-10-03 17:40:40 +00002372 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002373 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002374
aswift5b1a2562008-08-22 00:22:35 +00002375 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002376 /* note that the quality of the randomness doesn't matter that much */
2377 lk = random();
aswiftaebf4132008-11-21 00:10:35 +00002378 context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
drh6b9d6dd2008-12-03 19:34:47 +00002379 lrc1 = afpSetLock(context->dbPath, pFile,
aswiftaebf4132008-11-21 00:10:35 +00002380 SHARED_FIRST+context->sharedByte, 1, 1);
aswift5b1a2562008-08-22 00:22:35 +00002381 if( IS_LOCK_ERROR(lrc1) ){
2382 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002383 }
aswift5b1a2562008-08-22 00:22:35 +00002384 /* Drop the temporary PENDING lock */
drh6b9d6dd2008-12-03 19:34:47 +00002385 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002386
aswift5b1a2562008-08-22 00:22:35 +00002387 if( IS_LOCK_ERROR(lrc1) ) {
2388 pFile->lastErrno = lrc1Errno;
2389 rc = lrc1;
2390 goto afp_end_lock;
2391 } else if( IS_LOCK_ERROR(lrc2) ){
2392 rc = lrc2;
2393 goto afp_end_lock;
2394 } else if( lrc1 != SQLITE_OK ) {
2395 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002396 } else {
2397 pFile->locktype = SHARED_LOCK;
aswiftaebf4132008-11-21 00:10:35 +00002398 pFile->pOpen->nLock++;
drhbfe66312006-10-03 17:40:40 +00002399 }
2400 }else{
2401 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2402 ** assumed that there is a SHARED or greater lock on the file
2403 ** already.
2404 */
2405 int failed = 0;
2406 assert( 0!=pFile->locktype );
2407 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2408 /* Acquire a RESERVED lock */
drh6b9d6dd2008-12-03 19:34:47 +00002409 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002410 }
2411 if (!failed && locktype == EXCLUSIVE_LOCK) {
2412 /* Acquire an EXCLUSIVE lock */
2413
2414 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002415 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002416 */
drh6b9d6dd2008-12-03 19:34:47 +00002417 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
aswiftaebf4132008-11-21 00:10:35 +00002418 context->sharedByte, 1, 0)) ){
2419 int failed2 = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002420 /* now attemmpt to get the exclusive lock range */
drh6b9d6dd2008-12-03 19:34:47 +00002421 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002422 SHARED_SIZE, 1);
drh6b9d6dd2008-12-03 19:34:47 +00002423 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
aswiftaebf4132008-11-21 00:10:35 +00002424 SHARED_FIRST + context->sharedByte, 1, 1)) ){
2425 /* Can't reestablish the shared lock. Sqlite can't deal, this is
2426 ** a critical I/O error
2427 */
2428 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
2429 SQLITE_IOERR_LOCK;
2430 goto afp_end_lock;
2431 }
2432 }else{
aswift5b1a2562008-08-22 00:22:35 +00002433 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002434 }
2435 }
aswift5b1a2562008-08-22 00:22:35 +00002436 if( failed ){
2437 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002438 }
2439 }
2440
2441 if( rc==SQLITE_OK ){
2442 pFile->locktype = locktype;
2443 }else if( locktype==EXCLUSIVE_LOCK ){
2444 pFile->locktype = PENDING_LOCK;
2445 }
2446
2447afp_end_lock:
drh6c7d5c52008-11-21 20:32:33 +00002448 unixLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002449 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002450 rc==SQLITE_OK ? "ok" : "failed");
2451 return rc;
2452}
2453
2454/*
drh339eb0b2008-03-07 15:34:11 +00002455** Lower the locking level on file descriptor pFile to locktype. locktype
2456** must be either NO_LOCK or SHARED_LOCK.
2457**
2458** If the locking level of the file descriptor is already at or below
2459** the requested locking level, this routine is a no-op.
2460*/
danielk1977e339d652008-06-28 11:23:00 +00002461static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002462 int rc = SQLITE_OK;
2463 unixFile *pFile = (unixFile*)id;
aswiftaebf4132008-11-21 00:10:35 +00002464 afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002465
2466 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002467 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002468 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002469
drhbfe66312006-10-03 17:40:40 +00002470 assert( locktype<=SHARED_LOCK );
2471 if( pFile->locktype<=locktype ){
2472 return SQLITE_OK;
2473 }
2474 if( CHECK_THREADID(pFile) ){
2475 return SQLITE_MISUSE;
2476 }
drh6c7d5c52008-11-21 20:32:33 +00002477 unixEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002478 if( pFile->locktype>SHARED_LOCK ){
aswiftaebf4132008-11-21 00:10:35 +00002479
2480 if( pFile->locktype==EXCLUSIVE_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002481 rc = afpSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
aswiftaebf4132008-11-21 00:10:35 +00002482 if( rc==SQLITE_OK && locktype==SHARED_LOCK ){
2483 /* only re-establish the shared lock if necessary */
2484 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
drh6b9d6dd2008-12-03 19:34:47 +00002485 rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
aswiftaebf4132008-11-21 00:10:35 +00002486 }
2487 }
2488 if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002489 rc = afpSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002490 }
2491 if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002492 rc = afpSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002493 }
2494 }else if( locktype==NO_LOCK ){
2495 /* clear the shared lock */
2496 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
drh6b9d6dd2008-12-03 19:34:47 +00002497 rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002498 }
drhbfe66312006-10-03 17:40:40 +00002499
aswiftaebf4132008-11-21 00:10:35 +00002500 if( rc==SQLITE_OK ){
2501 if( locktype==NO_LOCK ){
drh6c7d5c52008-11-21 20:32:33 +00002502 struct unixOpenCnt *pOpen = pFile->pOpen;
aswiftaebf4132008-11-21 00:10:35 +00002503 pOpen->nLock--;
2504 assert( pOpen->nLock>=0 );
2505 if( pOpen->nLock==0 && pOpen->nPending>0 ){
2506 int i;
2507 for(i=0; i<pOpen->nPending; i++){
2508 if( pOpen->aPending[i] < 0 ) continue;
2509 if( close(pOpen->aPending[i]) ){
2510 pFile->lastErrno = errno;
2511 rc = SQLITE_IOERR_CLOSE;
2512 }else{
2513 pOpen->aPending[i] = -1;
drhbfe66312006-10-03 17:40:40 +00002514 }
aswiftaebf4132008-11-21 00:10:35 +00002515 }
2516 if( rc==SQLITE_OK ){
2517 sqlite3_free(pOpen->aPending);
2518 pOpen->nPending = 0;
2519 pOpen->aPending = 0;
2520 }
drhbfe66312006-10-03 17:40:40 +00002521 }
2522 }
drhbfe66312006-10-03 17:40:40 +00002523 }
drh6c7d5c52008-11-21 20:32:33 +00002524 unixLeaveMutex();
aswiftaebf4132008-11-21 00:10:35 +00002525 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002526 return rc;
2527}
2528
2529/*
drh339eb0b2008-03-07 15:34:11 +00002530** Close a file & cleanup AFP specific locking context
2531*/
danielk1977e339d652008-06-28 11:23:00 +00002532static int afpClose(sqlite3_file *id) {
2533 if( id ){
2534 unixFile *pFile = (unixFile*)id;
2535 afpUnlock(id, NO_LOCK);
drh6c7d5c52008-11-21 20:32:33 +00002536 unixEnterMutex();
aswiftaebf4132008-11-21 00:10:35 +00002537 if( pFile->pOpen && pFile->pOpen->nLock ){
2538 /* If there are outstanding locks, do not actually close the file just
drh734c9862008-11-28 15:37:20 +00002539 ** yet because that would clear those locks. Instead, add the file
2540 ** descriptor to pOpen->aPending. It will be automatically closed when
2541 ** the last lock is cleared.
2542 */
aswiftaebf4132008-11-21 00:10:35 +00002543 int *aNew;
drh6c7d5c52008-11-21 20:32:33 +00002544 struct unixOpenCnt *pOpen = pFile->pOpen;
aswiftaebf4132008-11-21 00:10:35 +00002545 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
2546 if( aNew==0 ){
2547 /* If a malloc fails, just leak the file descriptor */
2548 }else{
2549 pOpen->aPending = aNew;
2550 pOpen->aPending[pOpen->nPending] = pFile->h;
2551 pOpen->nPending++;
2552 pFile->h = -1;
2553 }
2554 }
2555 releaseOpenCnt(pFile->pOpen);
danielk1977e339d652008-06-28 11:23:00 +00002556 sqlite3_free(pFile->lockingContext);
aswiftaebf4132008-11-21 00:10:35 +00002557 closeUnixFile(id);
drh6c7d5c52008-11-21 20:32:33 +00002558 unixLeaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00002559 }
aswiftaebf4132008-11-21 00:10:35 +00002560 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002561}
2562
drh734c9862008-11-28 15:37:20 +00002563#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
2564/*
2565** The code above is the AFP lock implementation. The code is specific
2566** to MacOSX and does not work on other unix platforms. No alternative
2567** is available. If you don't compile for a mac, then the "unix-afp"
2568** VFS is not available.
2569**
2570********************* End of the AFP lock implementation **********************
2571******************************************************************************/
drhbfe66312006-10-03 17:40:40 +00002572
drh734c9862008-11-28 15:37:20 +00002573
2574/******************************************************************************
2575**************** Non-locking sqlite3_file methods *****************************
2576**
2577** The next division contains implementations for all methods of the
2578** sqlite3_file object other than the locking methods. The locking
2579** methods were defined in divisions above (one locking method per
2580** division). Those methods that are common to all locking modes
2581** are gather together into this division.
2582*/
drhbfe66312006-10-03 17:40:40 +00002583
2584/*
drh734c9862008-11-28 15:37:20 +00002585** Seek to the offset passed as the second argument, then read cnt
2586** bytes into pBuf. Return the number of bytes actually read.
2587**
2588** NB: If you define USE_PREAD or USE_PREAD64, then it might also
2589** be necessary to define _XOPEN_SOURCE to be 500. This varies from
2590** one system to another. Since SQLite does not define USE_PREAD
2591** any any form by default, we will not attempt to define _XOPEN_SOURCE.
2592** See tickets #2741 and #2681.
2593**
2594** To avoid stomping the errno value on a failed read the lastErrno value
2595** is set before returning.
drh339eb0b2008-03-07 15:34:11 +00002596*/
drh734c9862008-11-28 15:37:20 +00002597static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
2598 int got;
2599 i64 newOffset;
2600 TIMER_START;
2601#if defined(USE_PREAD)
2602 got = pread(id->h, pBuf, cnt, offset);
2603 SimulateIOError( got = -1 );
2604#elif defined(USE_PREAD64)
2605 got = pread64(id->h, pBuf, cnt, offset);
2606 SimulateIOError( got = -1 );
2607#else
2608 newOffset = lseek(id->h, offset, SEEK_SET);
2609 SimulateIOError( newOffset-- );
2610 if( newOffset!=offset ){
2611 if( newOffset == -1 ){
2612 ((unixFile*)id)->lastErrno = errno;
2613 }else{
2614 ((unixFile*)id)->lastErrno = 0;
2615 }
2616 return -1;
2617 }
2618 got = read(id->h, pBuf, cnt);
2619#endif
2620 TIMER_END;
2621 if( got<0 ){
2622 ((unixFile*)id)->lastErrno = errno;
2623 }
2624 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
2625 return got;
drhbfe66312006-10-03 17:40:40 +00002626}
2627
2628/*
drh734c9862008-11-28 15:37:20 +00002629** Read data from a file into a buffer. Return SQLITE_OK if all
2630** bytes were read successfully and SQLITE_IOERR if anything goes
2631** wrong.
drh339eb0b2008-03-07 15:34:11 +00002632*/
drh734c9862008-11-28 15:37:20 +00002633static int unixRead(
2634 sqlite3_file *id,
2635 void *pBuf,
2636 int amt,
2637 sqlite3_int64 offset
2638){
2639 int got;
2640 assert( id );
2641 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
2642 if( got==amt ){
2643 return SQLITE_OK;
2644 }else if( got<0 ){
2645 /* lastErrno set by seekAndRead */
2646 return SQLITE_IOERR_READ;
2647 }else{
2648 ((unixFile*)id)->lastErrno = 0; /* not a system error */
2649 /* Unread parts of the buffer must be zero-filled */
2650 memset(&((char*)pBuf)[got], 0, amt-got);
2651 return SQLITE_IOERR_SHORT_READ;
2652 }
2653}
2654
2655/*
2656** Seek to the offset in id->offset then read cnt bytes into pBuf.
2657** Return the number of bytes actually read. Update the offset.
2658**
2659** To avoid stomping the errno value on a failed write the lastErrno value
2660** is set before returning.
2661*/
2662static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
2663 int got;
2664 i64 newOffset;
2665 TIMER_START;
2666#if defined(USE_PREAD)
2667 got = pwrite(id->h, pBuf, cnt, offset);
2668#elif defined(USE_PREAD64)
2669 got = pwrite64(id->h, pBuf, cnt, offset);
2670#else
2671 newOffset = lseek(id->h, offset, SEEK_SET);
2672 if( newOffset!=offset ){
2673 if( newOffset == -1 ){
2674 ((unixFile*)id)->lastErrno = errno;
2675 }else{
2676 ((unixFile*)id)->lastErrno = 0;
2677 }
2678 return -1;
2679 }
2680 got = write(id->h, pBuf, cnt);
2681#endif
2682 TIMER_END;
2683 if( got<0 ){
2684 ((unixFile*)id)->lastErrno = errno;
2685 }
2686
2687 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
2688 return got;
2689}
2690
2691
2692/*
2693** Write data from a buffer into a file. Return SQLITE_OK on success
2694** or some other error code on failure.
2695*/
2696static int unixWrite(
2697 sqlite3_file *id,
2698 const void *pBuf,
2699 int amt,
2700 sqlite3_int64 offset
2701){
2702 int wrote = 0;
2703 assert( id );
2704 assert( amt>0 );
2705 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
2706 amt -= wrote;
2707 offset += wrote;
2708 pBuf = &((char*)pBuf)[wrote];
2709 }
2710 SimulateIOError(( wrote=(-1), amt=1 ));
2711 SimulateDiskfullError(( wrote=0, amt=1 ));
2712 if( amt>0 ){
2713 if( wrote<0 ){
2714 /* lastErrno set by seekAndWrite */
2715 return SQLITE_IOERR_WRITE;
2716 }else{
2717 ((unixFile*)id)->lastErrno = 0; /* not a system error */
2718 return SQLITE_FULL;
2719 }
2720 }
2721 return SQLITE_OK;
2722}
2723
2724#ifdef SQLITE_TEST
2725/*
2726** Count the number of fullsyncs and normal syncs. This is used to test
drh6b9d6dd2008-12-03 19:34:47 +00002727** that syncs and fullsyncs are occurring at the right times.
drh734c9862008-11-28 15:37:20 +00002728*/
2729int sqlite3_sync_count = 0;
2730int sqlite3_fullsync_count = 0;
2731#endif
2732
2733/*
2734** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
2735** Otherwise use fsync() in its place.
2736*/
2737#ifndef HAVE_FDATASYNC
2738# define fdatasync fsync
2739#endif
2740
2741/*
2742** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
2743** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
2744** only available on Mac OS X. But that could change.
2745*/
2746#ifdef F_FULLFSYNC
2747# define HAVE_FULLFSYNC 1
2748#else
2749# define HAVE_FULLFSYNC 0
2750#endif
2751
2752
2753/*
2754** The fsync() system call does not work as advertised on many
2755** unix systems. The following procedure is an attempt to make
2756** it work better.
2757**
2758** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
2759** for testing when we want to run through the test suite quickly.
2760** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
2761** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
2762** or power failure will likely corrupt the database file.
2763*/
2764static int full_fsync(int fd, int fullSync, int dataOnly){
chw97185482008-11-17 08:05:31 +00002765 int rc;
drh734c9862008-11-28 15:37:20 +00002766
2767 /* The following "ifdef/elif/else/" block has the same structure as
2768 ** the one below. It is replicated here solely to avoid cluttering
2769 ** up the real code with the UNUSED_PARAMETER() macros.
2770 */
2771#ifdef SQLITE_NO_SYNC
2772 UNUSED_PARAMETER(fd);
2773 UNUSED_PARAMETER(fullSync);
2774 UNUSED_PARAMETER(dataOnly);
2775#elif HAVE_FULLFSYNC
2776 UNUSED_PARAMETER(dataOnly);
2777#else
2778 UNUSED_PARAMETER(fullSync);
2779#endif
2780
2781 /* Record the number of times that we do a normal fsync() and
2782 ** FULLSYNC. This is used during testing to verify that this procedure
2783 ** gets called with the correct arguments.
2784 */
2785#ifdef SQLITE_TEST
2786 if( fullSync ) sqlite3_fullsync_count++;
2787 sqlite3_sync_count++;
2788#endif
2789
2790 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
2791 ** no-op
2792 */
2793#ifdef SQLITE_NO_SYNC
2794 rc = SQLITE_OK;
2795#elif HAVE_FULLFSYNC
2796 if( fullSync ){
2797 rc = fcntl(fd, F_FULLFSYNC, 0);
2798 }else{
2799 rc = 1;
2800 }
2801 /* If the FULLFSYNC failed, fall back to attempting an fsync().
drh6b9d6dd2008-12-03 19:34:47 +00002802 ** It shouldn't be possible for fullfsync to fail on the local
2803 ** file system (on OSX), so failure indicates that FULLFSYNC
2804 ** isn't supported for this file system. So, attempt an fsync
2805 ** and (for now) ignore the overhead of a superfluous fcntl call.
2806 ** It'd be better to detect fullfsync support once and avoid
2807 ** the fcntl call every time sync is called.
2808 */
drh734c9862008-11-28 15:37:20 +00002809 if( rc ) rc = fsync(fd);
2810
2811#else
2812 if( dataOnly ){
2813 rc = fdatasync(fd);
2814 if( OS_VXWORKS && rc==-1 && errno==ENOTSUP ){
2815 rc = fsync(fd);
2816 }
2817 }else{
2818 rc = fsync(fd);
2819 }
2820#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
2821
2822 if( OS_VXWORKS && rc!= -1 ){
2823 rc = 0;
2824 }
chw97185482008-11-17 08:05:31 +00002825 return rc;
drhbfe66312006-10-03 17:40:40 +00002826}
2827
drh734c9862008-11-28 15:37:20 +00002828/*
2829** Make sure all writes to a particular file are committed to disk.
2830**
2831** If dataOnly==0 then both the file itself and its metadata (file
2832** size, access time, etc) are synced. If dataOnly!=0 then only the
2833** file data is synced.
2834**
2835** Under Unix, also make sure that the directory entry for the file
2836** has been created by fsync-ing the directory that contains the file.
2837** If we do not do this and we encounter a power failure, the directory
2838** entry for the journal might not exist after we reboot. The next
2839** SQLite to access the file will not know that the journal exists (because
2840** the directory entry for the journal was never created) and the transaction
2841** will not roll back - possibly leading to database corruption.
2842*/
2843static int unixSync(sqlite3_file *id, int flags){
2844 int rc;
2845 unixFile *pFile = (unixFile*)id;
2846
2847 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
2848 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
2849
2850 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
2851 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
2852 || (flags&0x0F)==SQLITE_SYNC_FULL
2853 );
2854
2855 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
2856 ** line is to test that doing so does not cause any problems.
2857 */
2858 SimulateDiskfullError( return SQLITE_FULL );
2859
2860 assert( pFile );
2861 OSTRACE2("SYNC %-3d\n", pFile->h);
2862 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
2863 SimulateIOError( rc=1 );
2864 if( rc ){
2865 pFile->lastErrno = errno;
2866 return SQLITE_IOERR_FSYNC;
2867 }
2868 if( pFile->dirfd>=0 ){
2869 int err;
2870 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
2871 HAVE_FULLFSYNC, isFullsync);
2872#ifndef SQLITE_DISABLE_DIRSYNC
2873 /* The directory sync is only attempted if full_fsync is
2874 ** turned off or unavailable. If a full_fsync occurred above,
2875 ** then the directory sync is superfluous.
2876 */
2877 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
2878 /*
2879 ** We have received multiple reports of fsync() returning
2880 ** errors when applied to directories on certain file systems.
2881 ** A failed directory sync is not a big deal. So it seems
2882 ** better to ignore the error. Ticket #1657
2883 */
2884 /* pFile->lastErrno = errno; */
2885 /* return SQLITE_IOERR; */
2886 }
2887#endif
2888 err = close(pFile->dirfd); /* Only need to sync once, so close the */
2889 if( err==0 ){ /* directory when we are done */
2890 pFile->dirfd = -1;
2891 }else{
2892 pFile->lastErrno = errno;
2893 rc = SQLITE_IOERR_DIR_CLOSE;
2894 }
2895 }
2896 return rc;
2897}
2898
2899/*
2900** Truncate an open file to a specified size
2901*/
2902static int unixTruncate(sqlite3_file *id, i64 nByte){
2903 int rc;
2904 assert( id );
2905 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
2906 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
2907 if( rc ){
2908 ((unixFile*)id)->lastErrno = errno;
2909 return SQLITE_IOERR_TRUNCATE;
2910 }else{
2911 return SQLITE_OK;
2912 }
2913}
2914
2915/*
2916** Determine the current size of a file in bytes
2917*/
2918static int unixFileSize(sqlite3_file *id, i64 *pSize){
2919 int rc;
2920 struct stat buf;
2921 assert( id );
2922 rc = fstat(((unixFile*)id)->h, &buf);
2923 SimulateIOError( rc=1 );
2924 if( rc!=0 ){
2925 ((unixFile*)id)->lastErrno = errno;
2926 return SQLITE_IOERR_FSTAT;
2927 }
2928 *pSize = buf.st_size;
2929
2930 /* When opening a zero-size database, the findLockInfo() procedure
2931 ** writes a single byte into that file in order to work around a bug
2932 ** in the OS-X msdos filesystem. In order to avoid problems with upper
2933 ** layers, we need to report this file size as zero even though it is
2934 ** really 1. Ticket #3260.
2935 */
2936 if( *pSize==1 ) *pSize = 0;
2937
2938
2939 return SQLITE_OK;
2940}
2941
drh947bd802008-12-04 12:34:15 +00002942#if SQLITE_ENABLE_LOCKING_MODE && defined(__DARWIN__)
drh715ff302008-12-03 22:32:44 +00002943/*
2944** Handler for proxy-locking file-control verbs. Defined below in the
2945** proxying locking division.
2946*/
2947static int proxyFileControl(sqlite3_file*,int,void*);
drh947bd802008-12-04 12:34:15 +00002948#endif
drh715ff302008-12-03 22:32:44 +00002949
danielk1977ad94b582007-08-20 06:44:22 +00002950
danielk1977e3026632004-06-22 11:29:02 +00002951/*
drh9e33c2c2007-08-31 18:34:59 +00002952** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00002953*/
drhcc6bb3e2007-08-31 16:11:35 +00002954static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00002955 switch( op ){
2956 case SQLITE_FCNTL_LOCKSTATE: {
2957 *(int*)pArg = ((unixFile*)id)->locktype;
2958 return SQLITE_OK;
2959 }
drh7708e972008-11-29 00:56:52 +00002960 case SQLITE_LAST_ERRNO: {
2961 *(int*)pArg = ((unixFile*)id)->lastErrno;
2962 return SQLITE_OK;
2963 }
2964#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
drh715ff302008-12-03 22:32:44 +00002965 case SQLITE_SET_LOCKPROXYFILE:
aswiftaebf4132008-11-21 00:10:35 +00002966 case SQLITE_GET_LOCKPROXYFILE: {
drh715ff302008-12-03 22:32:44 +00002967 return proxyFileControl(id,op,pArg);
drh7708e972008-11-29 00:56:52 +00002968 }
drh6b9d6dd2008-12-03 19:34:47 +00002969#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__) */
drh9e33c2c2007-08-31 18:34:59 +00002970 }
drhcc6bb3e2007-08-31 16:11:35 +00002971 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00002972}
2973
2974/*
danielk1977a3d4c882007-03-23 10:08:38 +00002975** Return the sector size in bytes of the underlying block device for
2976** the specified file. This is almost always 512 bytes, but may be
2977** larger for some devices.
2978**
2979** SQLite code assumes this function cannot fail. It also assumes that
2980** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00002981** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00002982** same for both.
2983*/
danielk1977397d65f2008-11-19 11:35:39 +00002984static int unixSectorSize(sqlite3_file *NotUsed){
2985 UNUSED_PARAMETER(NotUsed);
drh3ceeb752007-03-29 18:19:52 +00002986 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002987}
2988
danielk197790949c22007-08-17 16:50:38 +00002989/*
danielk1977397d65f2008-11-19 11:35:39 +00002990** Return the device characteristics for the file. This is always 0 for unix.
danielk197790949c22007-08-17 16:50:38 +00002991*/
danielk1977397d65f2008-11-19 11:35:39 +00002992static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
2993 UNUSED_PARAMETER(NotUsed);
danielk197762079062007-08-15 17:08:46 +00002994 return 0;
2995}
2996
drh734c9862008-11-28 15:37:20 +00002997/*
2998** Here ends the implementation of all sqlite3_file methods.
2999**
3000********************** End sqlite3_file Methods *******************************
3001******************************************************************************/
3002
3003/*
drh6b9d6dd2008-12-03 19:34:47 +00003004** This division contains definitions of sqlite3_io_methods objects that
3005** implement various file locking strategies. It also contains definitions
3006** of "finder" functions. A finder-function is used to locate the appropriate
3007** sqlite3_io_methods object for a particular database file. The pAppData
3008** field of the sqlite3_vfs VFS objects are initialized to be pointers to
3009** the correct finder-function for that VFS.
3010**
3011** Most finder functions return a pointer to a fixed sqlite3_io_methods
3012** object. The only interesting finder-function is autolockIoFinder, which
3013** looks at the filesystem type and tries to guess the best locking
3014** strategy from that.
3015**
3016**
drh7708e972008-11-29 00:56:52 +00003017** Each instance of this macro generates two objects:
drh734c9862008-11-28 15:37:20 +00003018**
drh7708e972008-11-29 00:56:52 +00003019** * A constant sqlite3_io_methods object call METHOD that has locking
3020** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
3021**
3022** * An I/O method finder function called FINDER that returns a pointer
3023** to the METHOD object in the previous bullet.
drh734c9862008-11-28 15:37:20 +00003024*/
drh7708e972008-11-29 00:56:52 +00003025#define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK) \
3026static const sqlite3_io_methods METHOD = { \
3027 1, /* iVersion */ \
3028 CLOSE, /* xClose */ \
3029 unixRead, /* xRead */ \
3030 unixWrite, /* xWrite */ \
3031 unixTruncate, /* xTruncate */ \
3032 unixSync, /* xSync */ \
3033 unixFileSize, /* xFileSize */ \
3034 LOCK, /* xLock */ \
3035 UNLOCK, /* xUnlock */ \
3036 CKLOCK, /* xCheckReservedLock */ \
3037 unixFileControl, /* xFileControl */ \
3038 unixSectorSize, /* xSectorSize */ \
3039 unixDeviceCharacteristics /* xDeviceCapabilities */ \
3040}; \
3041static const sqlite3_io_methods *FINDER(const char *z, int h){ \
3042 UNUSED_PARAMETER(z); UNUSED_PARAMETER(h); \
3043 return &METHOD; \
aswiftaebf4132008-11-21 00:10:35 +00003044}
drh7708e972008-11-29 00:56:52 +00003045
3046/*
3047** Here are all of the sqlite3_io_methods objects for each of the
3048** locking strategies. Functions that return pointers to these methods
3049** are also created.
3050*/
3051IOMETHODS(
3052 posixIoFinder, /* Finder function name */
3053 posixIoMethods, /* sqlite3_io_methods object name */
3054 unixClose, /* xClose method */
3055 unixLock, /* xLock method */
3056 unixUnlock, /* xUnlock method */
3057 unixCheckReservedLock /* xCheckReservedLock method */
3058);
3059IOMETHODS(
3060 nolockIoFinder, /* Finder function name */
3061 nolockIoMethods, /* sqlite3_io_methods object name */
3062 nolockClose, /* xClose method */
3063 nolockLock, /* xLock method */
3064 nolockUnlock, /* xUnlock method */
3065 nolockCheckReservedLock /* xCheckReservedLock method */
3066);
3067IOMETHODS(
3068 dotlockIoFinder, /* Finder function name */
3069 dotlockIoMethods, /* sqlite3_io_methods object name */
3070 dotlockClose, /* xClose method */
3071 dotlockLock, /* xLock method */
3072 dotlockUnlock, /* xUnlock method */
3073 dotlockCheckReservedLock /* xCheckReservedLock method */
3074);
3075
3076#if SQLITE_ENABLE_LOCKING_STYLE
3077IOMETHODS(
3078 flockIoFinder, /* Finder function name */
3079 flockIoMethods, /* sqlite3_io_methods object name */
3080 flockClose, /* xClose method */
3081 flockLock, /* xLock method */
3082 flockUnlock, /* xUnlock method */
3083 flockCheckReservedLock /* xCheckReservedLock method */
3084);
3085#endif
3086
drh6c7d5c52008-11-21 20:32:33 +00003087#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00003088IOMETHODS(
3089 semIoFinder, /* Finder function name */
3090 semIoMethods, /* sqlite3_io_methods object name */
3091 semClose, /* xClose method */
3092 semLock, /* xLock method */
3093 semUnlock, /* xUnlock method */
3094 semCheckReservedLock /* xCheckReservedLock method */
3095);
aswiftaebf4132008-11-21 00:10:35 +00003096#endif
drh7708e972008-11-29 00:56:52 +00003097
drh734c9862008-11-28 15:37:20 +00003098#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00003099IOMETHODS(
3100 afpIoFinder, /* Finder function name */
3101 afpIoMethods, /* sqlite3_io_methods object name */
3102 afpClose, /* xClose method */
3103 afpLock, /* xLock method */
3104 afpUnlock, /* xUnlock method */
3105 afpCheckReservedLock /* xCheckReservedLock method */
3106);
drh715ff302008-12-03 22:32:44 +00003107#endif
3108
3109/*
3110** The proxy locking method is a "super-method" in the sense that it
3111** opens secondary file descriptors for the conch and lock files and
3112** it uses proxy, dot-file, AFP, and flock() locking methods on those
3113** secondary files. For this reason, the division that implements
3114** proxy locking is located much further down in the file. But we need
3115** to go ahead and define the sqlite3_io_methods and finder function
3116** for proxy locking here. So we forward declare the I/O methods.
3117*/
3118#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
3119static int proxyClose(sqlite3_file*);
3120static int proxyLock(sqlite3_file*, int);
3121static int proxyUnlock(sqlite3_file*, int);
3122static int proxyCheckReservedLock(sqlite3_file*, int*);
drh7708e972008-11-29 00:56:52 +00003123IOMETHODS(
3124 proxyIoFinder, /* Finder function name */
3125 proxyIoMethods, /* sqlite3_io_methods object name */
3126 proxyClose, /* xClose method */
3127 proxyLock, /* xLock method */
3128 proxyUnlock, /* xUnlock method */
3129 proxyCheckReservedLock /* xCheckReservedLock method */
3130);
aswiftaebf4132008-11-21 00:10:35 +00003131#endif
drh7708e972008-11-29 00:56:52 +00003132
3133
3134#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
3135/*
drh6b9d6dd2008-12-03 19:34:47 +00003136** This "finder" function attempts to determine the best locking strategy
3137** for the database file "filePath". It then returns the sqlite3_io_methods
drh7708e972008-11-29 00:56:52 +00003138** object that implements that strategy.
3139**
3140** This is for MacOSX only.
3141*/
3142static const sqlite3_io_methods *autolockIoFinder(
3143 const char *filePath, /* name of the database file */
3144 int fd /* file descriptor open on the database file */
3145){
3146 static const struct Mapping {
drh6b9d6dd2008-12-03 19:34:47 +00003147 const char *zFilesystem; /* Filesystem type name */
3148 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
drh7708e972008-11-29 00:56:52 +00003149 } aMap[] = {
3150 { "hfs", &posixIoMethods },
3151 { "ufs", &posixIoMethods },
3152 { "afpfs", &afpIoMethods },
3153#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
3154 { "smbfs", &afpIoMethods },
3155#else
3156 { "smbfs", &flockIoMethods },
3157#endif
3158 { "webdav", &nolockIoMethods },
3159 { 0, 0 }
3160 };
3161 int i;
3162 struct statfs fsInfo;
3163 struct flock lockInfo;
3164
3165 if( !filePath ){
drh6b9d6dd2008-12-03 19:34:47 +00003166 /* If filePath==NULL that means we are dealing with a transient file
3167 ** that does not need to be locked. */
drh7708e972008-11-29 00:56:52 +00003168 return &nolockIoMethods;
3169 }
3170 if( statfs(filePath, &fsInfo) != -1 ){
3171 if( fsInfo.f_flags & MNT_RDONLY ){
3172 return &nolockIoMethods;
3173 }
3174 for(i=0; aMap[i].zFilesystem; i++){
3175 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
3176 return aMap[i].pMethods;
3177 }
3178 }
3179 }
3180
3181 /* Default case. Handles, amongst others, "nfs".
3182 ** Test byte-range lock using fcntl(). If the call succeeds,
3183 ** assume that the file-system supports POSIX style locks.
drh734c9862008-11-28 15:37:20 +00003184 */
drh7708e972008-11-29 00:56:52 +00003185 lockInfo.l_len = 1;
3186 lockInfo.l_start = 0;
3187 lockInfo.l_whence = SEEK_SET;
3188 lockInfo.l_type = F_RDLCK;
3189 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
3190 return &posixIoMethods;
3191 }else{
3192 return &dotlockIoMethods;
3193 }
3194}
3195#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
3196
3197/*
3198** An abstract type for a pointer to a IO method finder function:
3199*/
3200typedef const sqlite3_io_methods *(*finder_type)(const char*,int);
3201
aswiftaebf4132008-11-21 00:10:35 +00003202
drh734c9862008-11-28 15:37:20 +00003203/****************************************************************************
3204**************************** sqlite3_vfs methods ****************************
3205**
3206** This division contains the implementation of methods on the
3207** sqlite3_vfs object.
3208*/
3209
danielk1977a3d4c882007-03-23 10:08:38 +00003210/*
danielk1977e339d652008-06-28 11:23:00 +00003211** Initialize the contents of the unixFile structure pointed to by pId.
danielk1977ad94b582007-08-20 06:44:22 +00003212*/
3213static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00003214 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00003215 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00003216 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00003217 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00003218 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00003219 int noLock, /* Omit locking if true */
3220 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00003221){
drh7708e972008-11-29 00:56:52 +00003222 const sqlite3_io_methods *pLockingStyle;
drhda0e7682008-07-30 15:27:54 +00003223 unixFile *pNew = (unixFile *)pId;
3224 int rc = SQLITE_OK;
3225
danielk197717b90b52008-06-06 11:11:25 +00003226 assert( pNew->pLock==NULL );
3227 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00003228
drh715ff302008-12-03 22:32:44 +00003229 /* Parameter isDelete is only used on vxworks.
3230 ** Express this explicitly here to prevent compiler warnings
3231 ** about unused parameters.
danielk1977a03396a2008-11-19 14:35:46 +00003232 */
drh7708e972008-11-29 00:56:52 +00003233#if !OS_VXWORKS
3234 UNUSED_PARAMETER(isDelete);
3235#endif
danielk1977a03396a2008-11-19 14:35:46 +00003236
drh218c5082008-03-07 00:27:10 +00003237 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00003238 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00003239 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00003240 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00003241
drh6c7d5c52008-11-21 20:32:33 +00003242#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +00003243 pNew->pId = vxworksFindFileId(zFilename);
3244 if( pNew->pId==0 ){
3245 noLock = 1;
3246 rc = SQLITE_NOMEM;
chw97185482008-11-17 08:05:31 +00003247 }
3248#endif
3249
drhda0e7682008-07-30 15:27:54 +00003250 if( noLock ){
drh7708e972008-11-29 00:56:52 +00003251 pLockingStyle = &nolockIoMethods;
drhda0e7682008-07-30 15:27:54 +00003252 }else{
drh7708e972008-11-29 00:56:52 +00003253 pLockingStyle = (*(finder_type)pVfs->pAppData)(zFilename, h);
aswiftaebf4132008-11-21 00:10:35 +00003254#if SQLITE_ENABLE_LOCKING_STYLE
3255 /* Cache zFilename in the locking context (AFP and dotlock override) for
3256 ** proxyLock activation is possible (remote proxy is based on db name)
3257 ** zFilename remains valid until file is closed, to support */
3258 pNew->lockingContext = (void*)zFilename;
3259#endif
drhda0e7682008-07-30 15:27:54 +00003260 }
danielk1977e339d652008-06-28 11:23:00 +00003261
drh7708e972008-11-29 00:56:52 +00003262 if( pLockingStyle == &posixIoMethods ){
3263 unixEnterMutex();
3264 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
3265 unixLeaveMutex();
3266 }
danielk1977e339d652008-06-28 11:23:00 +00003267
drh7708e972008-11-29 00:56:52 +00003268#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
aswiftf0551ee2008-12-03 21:26:19 +00003269 else if( pLockingStyle == &afpIoMethods ){
drh7708e972008-11-29 00:56:52 +00003270 /* AFP locking uses the file path so it needs to be included in
3271 ** the afpLockingContext.
3272 */
3273 afpLockingContext *pCtx;
3274 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
3275 if( pCtx==0 ){
3276 rc = SQLITE_NOMEM;
3277 }else{
3278 /* NB: zFilename exists and remains valid until the file is closed
3279 ** according to requirement F11141. So we do not need to make a
3280 ** copy of the filename. */
3281 pCtx->dbPath = zFilename;
3282 srandomdev();
drh6c7d5c52008-11-21 20:32:33 +00003283 unixEnterMutex();
drh7708e972008-11-29 00:56:52 +00003284 rc = findLockInfo(pNew, NULL, &pNew->pOpen);
3285 unixLeaveMutex();
drhbfe66312006-10-03 17:40:40 +00003286 }
drh7708e972008-11-29 00:56:52 +00003287 }
3288#endif
danielk1977e339d652008-06-28 11:23:00 +00003289
drh7708e972008-11-29 00:56:52 +00003290 else if( pLockingStyle == &dotlockIoMethods ){
3291 /* Dotfile locking uses the file path so it needs to be included in
3292 ** the dotlockLockingContext
3293 */
3294 char *zLockFile;
3295 int nFilename;
3296 nFilename = strlen(zFilename) + 6;
3297 zLockFile = (char *)sqlite3_malloc(nFilename);
3298 if( zLockFile==0 ){
3299 rc = SQLITE_NOMEM;
3300 }else{
3301 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
danielk1977e339d652008-06-28 11:23:00 +00003302 }
drh7708e972008-11-29 00:56:52 +00003303 pNew->lockingContext = zLockFile;
3304 }
danielk1977e339d652008-06-28 11:23:00 +00003305
drh6c7d5c52008-11-21 20:32:33 +00003306#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00003307 else if( pLockingStyle == &semIoMethods ){
3308 /* Named semaphore locking uses the file path so it needs to be
3309 ** included in the semLockingContext
3310 */
3311 unixEnterMutex();
3312 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
3313 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
3314 char *zSemName = pNew->pOpen->aSemName;
3315 int n;
3316 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem",
3317 pNew->pId->zCanonicalName);
3318 for( n=0; zSemName[n]; n++ )
3319 if( zSemName[n]=='/' ) zSemName[n] = '_';
3320 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
3321 if( pNew->pOpen->pSem == SEM_FAILED ){
3322 rc = SQLITE_NOMEM;
3323 pNew->pOpen->aSemName[0] = '\0';
chw97185482008-11-17 08:05:31 +00003324 }
chw97185482008-11-17 08:05:31 +00003325 }
drh7708e972008-11-29 00:56:52 +00003326 unixLeaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00003327 }
drh7708e972008-11-29 00:56:52 +00003328#endif
aswift5b1a2562008-08-22 00:22:35 +00003329
3330 pNew->lastErrno = 0;
drh6c7d5c52008-11-21 20:32:33 +00003331#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003332 if( rc!=SQLITE_OK ){
3333 unlink(zFilename);
3334 isDelete = 0;
3335 }
3336 pNew->isDelete = isDelete;
3337#endif
danielk1977e339d652008-06-28 11:23:00 +00003338 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003339 if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */
drhbfe66312006-10-03 17:40:40 +00003340 close(h);
danielk1977e339d652008-06-28 11:23:00 +00003341 }else{
drh7708e972008-11-29 00:56:52 +00003342 pNew->pMethod = pLockingStyle;
danielk1977e339d652008-06-28 11:23:00 +00003343 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00003344 }
danielk1977e339d652008-06-28 11:23:00 +00003345 return rc;
drh054889e2005-11-30 03:20:31 +00003346}
drh9c06c952005-11-26 00:25:00 +00003347
danielk1977ad94b582007-08-20 06:44:22 +00003348/*
3349** Open a file descriptor to the directory containing file zFilename.
3350** If successful, *pFd is set to the opened file descriptor and
3351** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
3352** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
3353** value.
3354**
3355** If SQLITE_OK is returned, the caller is responsible for closing
3356** the file descriptor *pFd using close().
3357*/
danielk1977fee2d252007-08-18 10:59:19 +00003358static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00003359 int ii;
drh777b17a2007-09-20 10:02:54 +00003360 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00003361 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00003362
drh153c62c2007-08-24 03:51:33 +00003363 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00003364 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
3365 if( ii>0 ){
3366 zDirname[ii] = '\0';
3367 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00003368 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003369#ifdef FD_CLOEXEC
3370 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3371#endif
3372 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
3373 }
3374 }
danielk1977fee2d252007-08-18 10:59:19 +00003375 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00003376 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00003377}
3378
danielk1977b4b47412007-08-17 15:53:36 +00003379/*
danielk197717b90b52008-06-06 11:11:25 +00003380** Create a temporary file name in zBuf. zBuf must be allocated
3381** by the calling process and must be big enough to hold at least
3382** pVfs->mxPathname bytes.
3383*/
3384static int getTempname(int nBuf, char *zBuf){
3385 static const char *azDirs[] = {
3386 0,
aswiftaebf4132008-11-21 00:10:35 +00003387 0,
danielk197717b90b52008-06-06 11:11:25 +00003388 "/var/tmp",
3389 "/usr/tmp",
3390 "/tmp",
3391 ".",
3392 };
3393 static const unsigned char zChars[] =
3394 "abcdefghijklmnopqrstuvwxyz"
3395 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3396 "0123456789";
drh41022642008-11-21 00:24:42 +00003397 unsigned int i, j;
danielk197717b90b52008-06-06 11:11:25 +00003398 struct stat buf;
3399 const char *zDir = ".";
3400
3401 /* It's odd to simulate an io-error here, but really this is just
3402 ** using the io-error infrastructure to test that SQLite handles this
3403 ** function failing.
3404 */
3405 SimulateIOError( return SQLITE_IOERR );
3406
3407 azDirs[0] = sqlite3_temp_directory;
aswiftaebf4132008-11-21 00:10:35 +00003408 if (NULL == azDirs[1]) {
3409 azDirs[1] = getenv("TMPDIR");
3410 }
3411
3412 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
danielk197717b90b52008-06-06 11:11:25 +00003413 if( azDirs[i]==0 ) continue;
3414 if( stat(azDirs[i], &buf) ) continue;
3415 if( !S_ISDIR(buf.st_mode) ) continue;
3416 if( access(azDirs[i], 07) ) continue;
3417 zDir = azDirs[i];
3418 break;
3419 }
3420
3421 /* Check that the output buffer is large enough for the temporary file
3422 ** name. If it is not, return SQLITE_ERROR.
3423 */
danielk197700e13612008-11-17 19:18:54 +00003424 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00003425 return SQLITE_ERROR;
3426 }
3427
3428 do{
3429 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
3430 j = strlen(zBuf);
3431 sqlite3_randomness(15, &zBuf[j]);
3432 for(i=0; i<15; i++, j++){
3433 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3434 }
3435 zBuf[j] = 0;
3436 }while( access(zBuf,0)==0 );
3437 return SQLITE_OK;
3438}
3439
drh947bd802008-12-04 12:34:15 +00003440#if SQLITE_ENABLE_LOCKING_MODE && defined(__DARWIN__)
drhc66d5b62008-12-03 22:48:32 +00003441/*
3442** Routine to transform a unixFile into a proxy-locking unixFile.
3443** Implementation in the proxy-lock division, but used by unixOpen()
3444** if SQLITE_PREFER_PROXY_LOCKING is defined.
3445*/
3446static int proxyTransformUnixFile(unixFile*, const char*);
drh947bd802008-12-04 12:34:15 +00003447#endif
drhc66d5b62008-12-03 22:48:32 +00003448
danielk197717b90b52008-06-06 11:11:25 +00003449
3450/*
danielk1977ad94b582007-08-20 06:44:22 +00003451** Open the file zPath.
3452**
danielk1977b4b47412007-08-17 15:53:36 +00003453** Previously, the SQLite OS layer used three functions in place of this
3454** one:
3455**
3456** sqlite3OsOpenReadWrite();
3457** sqlite3OsOpenReadOnly();
3458** sqlite3OsOpenExclusive();
3459**
3460** These calls correspond to the following combinations of flags:
3461**
3462** ReadWrite() -> (READWRITE | CREATE)
3463** ReadOnly() -> (READONLY)
3464** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
3465**
3466** The old OpenExclusive() accepted a boolean argument - "delFlag". If
3467** true, the file was configured to be automatically deleted when the
3468** file handle closed. To achieve the same effect using this new
3469** interface, add the DELETEONCLOSE flag to those specified above for
3470** OpenExclusive().
3471*/
3472static int unixOpen(
drh6b9d6dd2008-12-03 19:34:47 +00003473 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
3474 const char *zPath, /* Pathname of file to be opened */
3475 sqlite3_file *pFile, /* The file descriptor to be filled in */
3476 int flags, /* Input flags to control the opening */
3477 int *pOutFlags /* Output flags returned to SQLite core */
danielk1977b4b47412007-08-17 15:53:36 +00003478){
danielk1977fee2d252007-08-18 10:59:19 +00003479 int fd = 0; /* File descriptor returned by open() */
3480 int dirfd = -1; /* Directory file descriptor */
drh6b9d6dd2008-12-03 19:34:47 +00003481 int openFlags = 0; /* Flags to pass to open() */
danielk1977fee2d252007-08-18 10:59:19 +00003482 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00003483 int noLock; /* True to omit locking primitives */
aswiftaebf4132008-11-21 00:10:35 +00003484 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003485
3486 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3487 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3488 int isCreate = (flags & SQLITE_OPEN_CREATE);
3489 int isReadonly = (flags & SQLITE_OPEN_READONLY);
3490 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3491
danielk1977fee2d252007-08-18 10:59:19 +00003492 /* If creating a master or main-file journal, this function will open
3493 ** a file-descriptor on the directory too. The first time unixSync()
3494 ** is called the directory file descriptor will be fsync()ed and close()d.
3495 */
3496 int isOpenDirectory = (isCreate &&
3497 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
3498 );
3499
danielk197717b90b52008-06-06 11:11:25 +00003500 /* If argument zPath is a NULL pointer, this function is required to open
3501 ** a temporary file. Use this buffer to store the file name in.
3502 */
3503 char zTmpname[MAX_PATHNAME+1];
3504 const char *zName = zPath;
3505
danielk1977fee2d252007-08-18 10:59:19 +00003506 /* Check the following statements are true:
3507 **
3508 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3509 ** (b) if CREATE is set, then READWRITE must also be set, and
3510 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00003511 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00003512 */
danielk1977b4b47412007-08-17 15:53:36 +00003513 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00003514 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00003515 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00003516 assert(isDelete==0 || isCreate);
3517
drh33f4e022007-09-03 15:19:34 +00003518 /* The main DB, main journal, and master journal are never automatically
3519 ** deleted
3520 */
3521 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
3522 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
3523 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00003524
danielk1977fee2d252007-08-18 10:59:19 +00003525 /* Assert that the upper layer has set one of the "file-type" flags. */
3526 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
3527 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
3528 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00003529 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00003530 );
3531
danielk1977e339d652008-06-28 11:23:00 +00003532 memset(pFile, 0, sizeof(unixFile));
3533
danielk197717b90b52008-06-06 11:11:25 +00003534 if( !zName ){
danielk197717b90b52008-06-06 11:11:25 +00003535 assert(isDelete && !isOpenDirectory);
3536 rc = getTempname(MAX_PATHNAME+1, zTmpname);
3537 if( rc!=SQLITE_OK ){
3538 return rc;
3539 }
3540 zName = zTmpname;
3541 }
3542
drh734c9862008-11-28 15:37:20 +00003543 if( isReadonly ) openFlags |= O_RDONLY;
3544 if( isReadWrite ) openFlags |= O_RDWR;
3545 if( isCreate ) openFlags |= O_CREAT;
3546 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
3547 openFlags |= (O_LARGEFILE|O_BINARY);
danielk1977b4b47412007-08-17 15:53:36 +00003548
drh734c9862008-11-28 15:37:20 +00003549 fd = open(zName, openFlags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
3550 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, openFlags);
danielk19772f2d8c72007-08-30 16:13:33 +00003551 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00003552 /* Failed to open the file for read/write access. Try read-only. */
3553 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
3554 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00003555 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00003556 }
3557 if( fd<0 ){
3558 return SQLITE_CANTOPEN;
3559 }
3560 if( isDelete ){
drh6c7d5c52008-11-21 20:32:33 +00003561#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003562 zPath = zName;
3563#else
danielk197717b90b52008-06-06 11:11:25 +00003564 unlink(zName);
chw97185482008-11-17 08:05:31 +00003565#endif
danielk1977b4b47412007-08-17 15:53:36 +00003566 }
drh41022642008-11-21 00:24:42 +00003567#if SQLITE_ENABLE_LOCKING_STYLE
3568 else{
drh734c9862008-11-28 15:37:20 +00003569 ((unixFile*)pFile)->openFlags = openFlags;
drh41022642008-11-21 00:24:42 +00003570 }
3571#endif
danielk1977b4b47412007-08-17 15:53:36 +00003572 if( pOutFlags ){
3573 *pOutFlags = flags;
3574 }
3575
3576 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00003577 if( isOpenDirectory ){
aswiftaebf4132008-11-21 00:10:35 +00003578 rc = openDirectory(zPath, &dirfd);
danielk1977fee2d252007-08-18 10:59:19 +00003579 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003580 close(fd); /* silently leak if fail, already in error */
danielk1977fee2d252007-08-18 10:59:19 +00003581 return rc;
3582 }
3583 }
danielk1977e339d652008-06-28 11:23:00 +00003584
3585#ifdef FD_CLOEXEC
3586 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3587#endif
3588
drhda0e7682008-07-30 15:27:54 +00003589 noLock = eType!=SQLITE_OPEN_MAIN_DB;
aswiftaebf4132008-11-21 00:10:35 +00003590
3591#if SQLITE_PREFER_PROXY_LOCKING
3592 if( zPath!=NULL && !noLock ){
3593 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
3594 int useProxy = 0;
3595
3596 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy,
drh7708e972008-11-29 00:56:52 +00003597 ** 0 means never use proxy, NULL means use proxy for non-local files only
3598 */
aswiftaebf4132008-11-21 00:10:35 +00003599 if( envforce!=NULL ){
3600 useProxy = atoi(envforce)>0;
3601 }else{
3602 struct statfs fsInfo;
3603
3604 if( statfs(zPath, &fsInfo) == -1 ){
3605 ((unixFile*)pFile)->lastErrno = errno;
3606 if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
3607 close(fd); /* silently leak if fail, in error */
3608 return SQLITE_IOERR_ACCESS;
3609 }
3610 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
3611 }
3612 if( useProxy ){
3613 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
3614 if( rc==SQLITE_OK ){
drh715ff302008-12-03 22:32:44 +00003615 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
aswiftaebf4132008-11-21 00:10:35 +00003616 }
3617 return rc;
3618 }
3619 }
3620#endif
3621
chw97185482008-11-17 08:05:31 +00003622 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00003623}
3624
3625/*
danielk1977fee2d252007-08-18 10:59:19 +00003626** Delete the file at zPath. If the dirSync argument is true, fsync()
3627** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00003628*/
drh6b9d6dd2008-12-03 19:34:47 +00003629static int unixDelete(
3630 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
3631 const char *zPath, /* Name of file to be deleted */
3632 int dirSync /* If true, fsync() directory after deleting file */
3633){
danielk1977fee2d252007-08-18 10:59:19 +00003634 int rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00003635 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003636 SimulateIOError(return SQLITE_IOERR_DELETE);
3637 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00003638#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00003639 if( dirSync ){
3640 int fd;
3641 rc = openDirectory(zPath, &fd);
3642 if( rc==SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00003643#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003644 if( fsync(fd)==-1 )
3645#else
3646 if( fsync(fd) )
3647#endif
3648 {
danielk1977fee2d252007-08-18 10:59:19 +00003649 rc = SQLITE_IOERR_DIR_FSYNC;
3650 }
aswiftaebf4132008-11-21 00:10:35 +00003651 if( close(fd)&&!rc ){
3652 rc = SQLITE_IOERR_DIR_CLOSE;
3653 }
danielk1977fee2d252007-08-18 10:59:19 +00003654 }
3655 }
danielk1977d138dd82008-10-15 16:02:48 +00003656#endif
danielk1977fee2d252007-08-18 10:59:19 +00003657 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00003658}
3659
danielk197790949c22007-08-17 16:50:38 +00003660/*
3661** Test the existance of or access permissions of file zPath. The
3662** test performed depends on the value of flags:
3663**
3664** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
3665** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
3666** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
3667**
3668** Otherwise return 0.
3669*/
danielk1977861f7452008-06-05 11:39:11 +00003670static int unixAccess(
drh6b9d6dd2008-12-03 19:34:47 +00003671 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
3672 const char *zPath, /* Path of the file to examine */
3673 int flags, /* What do we want to learn about the zPath file? */
3674 int *pResOut /* Write result boolean here */
danielk1977861f7452008-06-05 11:39:11 +00003675){
rse25c0d1a2007-09-20 08:38:14 +00003676 int amode = 0;
danielk1977397d65f2008-11-19 11:35:39 +00003677 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00003678 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00003679 switch( flags ){
3680 case SQLITE_ACCESS_EXISTS:
3681 amode = F_OK;
3682 break;
3683 case SQLITE_ACCESS_READWRITE:
3684 amode = W_OK|R_OK;
3685 break;
drh50d3f902007-08-27 21:10:36 +00003686 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00003687 amode = R_OK;
3688 break;
3689
3690 default:
3691 assert(!"Invalid flags argument");
3692 }
danielk1977861f7452008-06-05 11:39:11 +00003693 *pResOut = (access(zPath, amode)==0);
3694 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003695}
3696
danielk1977b4b47412007-08-17 15:53:36 +00003697
3698/*
3699** Turn a relative pathname into a full pathname. The relative path
3700** is stored as a nul-terminated string in the buffer pointed to by
3701** zPath.
3702**
3703** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
3704** (in this case, MAX_PATHNAME bytes). The full-path is written to
3705** this buffer before returning.
3706*/
danielk1977adfb9b02007-09-17 07:02:56 +00003707static int unixFullPathname(
3708 sqlite3_vfs *pVfs, /* Pointer to vfs object */
3709 const char *zPath, /* Possibly relative input path */
3710 int nOut, /* Size of output buffer in bytes */
3711 char *zOut /* Output buffer */
3712){
danielk1977843e65f2007-09-01 16:16:15 +00003713
3714 /* It's odd to simulate an io-error here, but really this is just
3715 ** using the io-error infrastructure to test that SQLite handles this
3716 ** function failing. This function could fail if, for example, the
drh6b9d6dd2008-12-03 19:34:47 +00003717 ** current working directory has been unlinked.
danielk1977843e65f2007-09-01 16:16:15 +00003718 */
3719 SimulateIOError( return SQLITE_ERROR );
3720
drh153c62c2007-08-24 03:51:33 +00003721 assert( pVfs->mxPathname==MAX_PATHNAME );
danielk1977f3d3c272008-11-19 16:52:44 +00003722 UNUSED_PARAMETER(pVfs);
chw97185482008-11-17 08:05:31 +00003723
drh3c7f2dc2007-12-06 13:26:20 +00003724 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00003725 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00003726 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003727 }else{
3728 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00003729 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00003730 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00003731 }
3732 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00003733 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003734 }
3735 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003736}
3737
drh0ccebe72005-06-07 22:22:50 +00003738
drh761df872006-12-21 01:29:22 +00003739#ifndef SQLITE_OMIT_LOAD_EXTENSION
3740/*
3741** Interfaces for opening a shared library, finding entry points
3742** within the shared library, and closing the shared library.
3743*/
3744#include <dlfcn.h>
danielk1977397d65f2008-11-19 11:35:39 +00003745static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
3746 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003747 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
3748}
danielk197795c8a542007-09-01 06:51:27 +00003749
3750/*
3751** SQLite calls this function immediately after a call to unixDlSym() or
3752** unixDlOpen() fails (returns a null pointer). If a more detailed error
3753** message is available, it is written to zBufOut. If no error message
3754** is available, zBufOut is left unmodified and SQLite uses a default
3755** error message.
3756*/
danielk1977397d65f2008-11-19 11:35:39 +00003757static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00003758 char *zErr;
danielk1977397d65f2008-11-19 11:35:39 +00003759 UNUSED_PARAMETER(NotUsed);
drh6c7d5c52008-11-21 20:32:33 +00003760 unixEnterMutex();
danielk1977b4b47412007-08-17 15:53:36 +00003761 zErr = dlerror();
3762 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00003763 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00003764 }
drh6c7d5c52008-11-21 20:32:33 +00003765 unixLeaveMutex();
danielk1977b4b47412007-08-17 15:53:36 +00003766}
danielk1977397d65f2008-11-19 11:35:39 +00003767static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
3768 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003769 return dlsym(pHandle, zSymbol);
3770}
danielk1977397d65f2008-11-19 11:35:39 +00003771static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
3772 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003773 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00003774}
danielk1977b4b47412007-08-17 15:53:36 +00003775#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
3776 #define unixDlOpen 0
3777 #define unixDlError 0
3778 #define unixDlSym 0
3779 #define unixDlClose 0
3780#endif
3781
3782/*
danielk197790949c22007-08-17 16:50:38 +00003783** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00003784*/
danielk1977397d65f2008-11-19 11:35:39 +00003785static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
3786 UNUSED_PARAMETER(NotUsed);
danielk197700e13612008-11-17 19:18:54 +00003787 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00003788
drhbbd42a62004-05-22 17:41:58 +00003789 /* We have to initialize zBuf to prevent valgrind from reporting
3790 ** errors. The reports issued by valgrind are incorrect - we would
3791 ** prefer that the randomness be increased by making use of the
3792 ** uninitialized space in zBuf - but valgrind errors tend to worry
3793 ** some users. Rather than argue, it seems easier just to initialize
3794 ** the whole array and silence valgrind, even if that means less randomness
3795 ** in the random seed.
3796 **
3797 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00003798 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00003799 ** tests repeatable.
3800 */
danielk1977b4b47412007-08-17 15:53:36 +00003801 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00003802#if !defined(SQLITE_TEST)
3803 {
drh842b8642005-01-21 17:53:17 +00003804 int pid, fd;
3805 fd = open("/dev/urandom", O_RDONLY);
3806 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00003807 time_t t;
3808 time(&t);
danielk197790949c22007-08-17 16:50:38 +00003809 memcpy(zBuf, &t, sizeof(t));
3810 pid = getpid();
3811 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00003812 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00003813 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00003814 }else{
drh72cbd072008-10-14 17:58:38 +00003815 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00003816 close(fd);
3817 }
drhbbd42a62004-05-22 17:41:58 +00003818 }
3819#endif
drh72cbd072008-10-14 17:58:38 +00003820 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00003821}
3822
danielk1977b4b47412007-08-17 15:53:36 +00003823
drhbbd42a62004-05-22 17:41:58 +00003824/*
3825** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00003826** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00003827** The return value is the number of microseconds of sleep actually
3828** requested from the underlying operating system, a number which
3829** might be greater than or equal to the argument, but not less
3830** than the argument.
drhbbd42a62004-05-22 17:41:58 +00003831*/
danielk1977397d65f2008-11-19 11:35:39 +00003832static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
drh6c7d5c52008-11-21 20:32:33 +00003833#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003834 struct timespec sp;
3835
3836 sp.tv_sec = microseconds / 1000000;
3837 sp.tv_nsec = (microseconds % 1000000) * 1000;
3838 nanosleep(&sp, NULL);
danielk1977397d65f2008-11-19 11:35:39 +00003839 return microseconds;
3840#elif defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00003841 usleep(microseconds);
3842 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00003843#else
danielk1977b4b47412007-08-17 15:53:36 +00003844 int seconds = (microseconds+999999)/1000000;
3845 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00003846 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00003847#endif
danielk1977397d65f2008-11-19 11:35:39 +00003848 UNUSED_PARAMETER(NotUsed);
drh88f474a2006-01-02 20:00:12 +00003849}
3850
3851/*
drh6b9d6dd2008-12-03 19:34:47 +00003852** The following variable, if set to a non-zero value, is interpreted as
3853** the number of seconds since 1970 and is used to set the result of
3854** sqlite3OsCurrentTime() during testing.
drhbbd42a62004-05-22 17:41:58 +00003855*/
3856#ifdef SQLITE_TEST
drh6b9d6dd2008-12-03 19:34:47 +00003857int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
drhbbd42a62004-05-22 17:41:58 +00003858#endif
3859
3860/*
3861** Find the current time (in Universal Coordinated Time). Write the
3862** current time and date as a Julian Day number into *prNow and
3863** return 0. Return 1 if the time and date cannot be found.
3864*/
danielk1977397d65f2008-11-19 11:35:39 +00003865static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
drh6c7d5c52008-11-21 20:32:33 +00003866#if defined(NO_GETTOD)
drhbbd42a62004-05-22 17:41:58 +00003867 time_t t;
3868 time(&t);
3869 *prNow = t/86400.0 + 2440587.5;
drh6c7d5c52008-11-21 20:32:33 +00003870#elif OS_VXWORKS
3871 struct timespec sNow;
3872 clock_gettime(CLOCK_REALTIME, &sNow);
3873 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
drh19e2d372005-08-29 23:00:03 +00003874#else
3875 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00003876 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00003877 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
3878#endif
danielk1977397d65f2008-11-19 11:35:39 +00003879
drhbbd42a62004-05-22 17:41:58 +00003880#ifdef SQLITE_TEST
3881 if( sqlite3_current_time ){
3882 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
3883 }
3884#endif
danielk1977397d65f2008-11-19 11:35:39 +00003885 UNUSED_PARAMETER(NotUsed);
drhbbd42a62004-05-22 17:41:58 +00003886 return 0;
3887}
danielk1977b4b47412007-08-17 15:53:36 +00003888
drh6b9d6dd2008-12-03 19:34:47 +00003889/*
3890** We added the xGetLastError() method with the intention of providing
3891** better low-level error messages when operating-system problems come up
3892** during SQLite operation. But so far, none of that has been implemented
3893** in the core. So this routine is never called. For now, it is merely
3894** a place-holder.
3895*/
danielk1977397d65f2008-11-19 11:35:39 +00003896static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
3897 UNUSED_PARAMETER(NotUsed);
3898 UNUSED_PARAMETER(NotUsed2);
3899 UNUSED_PARAMETER(NotUsed3);
danielk1977bcb97fe2008-06-06 15:49:29 +00003900 return 0;
3901}
3902
drh153c62c2007-08-24 03:51:33 +00003903/*
drh734c9862008-11-28 15:37:20 +00003904************************ End of sqlite3_vfs methods ***************************
3905******************************************************************************/
3906
drh715ff302008-12-03 22:32:44 +00003907/******************************************************************************
3908************************** Begin Proxy Locking ********************************
3909**
3910** Proxy locking is a "uber-locking-method" in this sense: It uses the
3911** other locking methods on secondary lock files. Proxy locking is a
3912** meta-layer over top of the primitive locking implemented above. For
3913** this reason, the division that implements of proxy locking is deferred
3914** until late in the file (here) after all of the other I/O methods have
3915** been defined - so that the primitive locking methods are available
3916** as services to help with the implementation of proxy locking.
3917**
3918****
3919**
3920** The default locking schemes in SQLite use byte-range locks on the
3921** database file to coordinate safe, concurrent access by multiple readers
3922** and writers [http://sqlite.org/lockingv3.html]. The five file locking
3923** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
3924** as POSIX read & write locks over fixed set of locations (via fsctl),
3925** on AFP and SMB only exclusive byte-range locks are available via fsctl
3926** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
3927** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
3928** address in the shared range is taken for a SHARED lock, the entire
3929** shared range is taken for an EXCLUSIVE lock):
3930**
3931** PENDING_BYTE 0x40000000
3932** RESERVED_BYTE 0x40000001
3933** SHARED_RANGE 0x40000002 -> 0x40000200
3934**
3935** This works well on the local file system, but shows a nearly 100x
3936** slowdown in read performance on AFP because the AFP client disables
3937** the read cache when byte-range locks are present. Enabling the read
3938** cache exposes a cache coherency problem that is present on all OS X
3939** supported network file systems. NFS and AFP both observe the
3940** close-to-open semantics for ensuring cache coherency
3941** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
3942** address the requirements for concurrent database access by multiple
3943** readers and writers
3944** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
3945**
3946** To address the performance and cache coherency issues, proxy file locking
3947** changes the way database access is controlled by limiting access to a
3948** single host at a time and moving file locks off of the database file
3949** and onto a proxy file on the local file system.
3950**
3951**
3952** Using proxy locks
3953** -----------------
3954**
3955** C APIs
3956**
3957** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
3958** <proxy_path> | ":auto:");
3959** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
3960**
3961**
3962** SQL pragmas
3963**
3964** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
3965** PRAGMA [database.]lock_proxy_file
3966**
3967** Specifying ":auto:" means that if there is a conch file with a matching
3968** host ID in it, the proxy path in the conch file will be used, otherwise
3969** a proxy path based on the user's temp dir
3970** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
3971** actual proxy file name is generated from the name and path of the
3972** database file. For example:
3973**
3974** For database path "/Users/me/foo.db"
3975** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
3976**
3977** Once a lock proxy is configured for a database connection, it can not
3978** be removed, however it may be switched to a different proxy path via
3979** the above APIs (assuming the conch file is not being held by another
3980** connection or process).
3981**
3982**
3983** How proxy locking works
3984** -----------------------
3985**
3986** Proxy file locking relies primarily on two new supporting files:
3987**
3988** * conch file to limit access to the database file to a single host
3989** at a time
3990**
3991** * proxy file to act as a proxy for the advisory locks normally
3992** taken on the database
3993**
3994** The conch file - to use a proxy file, sqlite must first "hold the conch"
3995** by taking an sqlite-style shared lock on the conch file, reading the
3996** contents and comparing the host's unique host ID (see below) and lock
3997** proxy path against the values stored in the conch. The conch file is
3998** stored in the same directory as the database file and the file name
3999** is patterned after the database file name as ".<databasename>-conch".
4000** If the conch file does not exist, or it's contents do not match the
4001** host ID and/or proxy path, then the lock is escalated to an exclusive
4002** lock and the conch file contents is updated with the host ID and proxy
4003** path and the lock is downgraded to a shared lock again. If the conch
4004** is held by another process (with a shared lock), the exclusive lock
4005** will fail and SQLITE_BUSY is returned.
4006**
4007** The proxy file - a single-byte file used for all advisory file locks
4008** normally taken on the database file. This allows for safe sharing
4009** of the database file for multiple readers and writers on the same
4010** host (the conch ensures that they all use the same local lock file).
4011**
4012** There is a third file - the host ID file - used as a persistent record
4013** of a unique identifier for the host, a 128-byte unique host id file
4014** in the path defined by the HOSTIDPATH macro (default value is
4015** /Library/Caches/.com.apple.sqliteConchHostId).
4016**
4017** Requesting the lock proxy does not immediately take the conch, it is
4018** only taken when the first request to lock database file is made.
4019** This matches the semantics of the traditional locking behavior, where
4020** opening a connection to a database file does not take a lock on it.
4021** The shared lock and an open file descriptor are maintained until
4022** the connection to the database is closed.
4023**
4024** The proxy file and the lock file are never deleted so they only need
4025** to be created the first time they are used.
4026**
4027** Configuration options
4028** ---------------------
4029**
4030** SQLITE_PREFER_PROXY_LOCKING
4031**
4032** Database files accessed on non-local file systems are
4033** automatically configured for proxy locking, lock files are
4034** named automatically using the same logic as
4035** PRAGMA lock_proxy_file=":auto:"
4036**
4037** SQLITE_PROXY_DEBUG
4038**
4039** Enables the logging of error messages during host id file
4040** retrieval and creation
4041**
4042** HOSTIDPATH
4043**
4044** Overrides the default host ID file path location
4045**
4046** LOCKPROXYDIR
4047**
4048** Overrides the default directory used for lock proxy files that
4049** are named automatically via the ":auto:" setting
4050**
4051** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
4052**
4053** Permissions to use when creating a directory for storing the
4054** lock proxy files, only used when LOCKPROXYDIR is not set.
4055**
4056**
4057** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
4058** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
4059** force proxy locking to be used for every database file opened, and 0
4060** will force automatic proxy locking to be disabled for all database
4061** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
4062** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
4063*/
4064
4065/*
4066** Proxy locking is only available on MacOSX
4067*/
4068#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
4069
4070#ifdef SQLITE_TEST
4071/* simulate multiple hosts by creating unique hostid file paths */
4072int sqlite3_hostid_num = 0;
4073#endif
4074
4075/*
4076** The proxyLockingContext has the path and file structures for the remote
4077** and local proxy files in it
4078*/
4079typedef struct proxyLockingContext proxyLockingContext;
4080struct proxyLockingContext {
4081 unixFile *conchFile; /* Open conch file */
4082 char *conchFilePath; /* Name of the conch file */
4083 unixFile *lockProxy; /* Open proxy lock file */
4084 char *lockProxyPath; /* Name of the proxy lock file */
4085 char *dbPath; /* Name of the open file */
4086 int conchHeld; /* True if the conch is currently held */
4087 void *oldLockingContext; /* Original lockingcontext to restore on close */
4088 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
4089};
4090
4091/* HOSTIDLEN and CONCHLEN both include space for the string
4092** terminating nul
4093*/
4094#define HOSTIDLEN 128
4095#define CONCHLEN (MAXPATHLEN+HOSTIDLEN+1)
4096#ifndef HOSTIDPATH
4097# define HOSTIDPATH "/Library/Caches/.com.apple.sqliteConchHostId"
4098#endif
4099
4100/* basically a copy of unixRandomness with different
4101** test behavior built in */
4102static int proxyGenerateHostID(char *pHostID){
4103 int pid, fd, len;
4104 unsigned char *key = (unsigned char *)pHostID;
4105
4106 memset(key, 0, HOSTIDLEN);
4107 len = 0;
4108 fd = open("/dev/urandom", O_RDONLY);
4109 if( fd>=0 ){
4110 len = read(fd, key, HOSTIDLEN);
4111 close(fd); /* silently leak the fd if it fails */
4112 }
4113 if( len < HOSTIDLEN ){
4114 time_t t;
4115 time(&t);
4116 memcpy(key, &t, sizeof(t));
4117 pid = getpid();
4118 memcpy(&key[sizeof(t)], &pid, sizeof(pid));
4119 }
4120
4121#ifdef MAKE_PRETTY_HOSTID
4122 {
4123 int i;
4124 /* filter the bytes into printable ascii characters and NUL terminate */
4125 key[(HOSTIDLEN-1)] = 0x00;
4126 for( i=0; i<(HOSTIDLEN-1); i++ ){
4127 unsigned char pa = key[i]&0x7F;
4128 if( pa<0x20 ){
4129 key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20;
4130 }else if( pa==0x7F ){
4131 key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E;
4132 }
4133 }
4134 }
4135#endif
4136 return SQLITE_OK;
4137}
4138
4139/* writes the host id path to path, path should be an pre-allocated buffer
4140** with enough space for a path
4141*/
4142static void proxyGetHostIDPath(char *path, size_t len){
4143 strlcpy(path, HOSTIDPATH, len);
4144#ifdef SQLITE_TEST
4145 if( sqlite3_hostid_num>0 ){
4146 char suffix[2] = "1";
4147 suffix[0] = suffix[0] + sqlite3_hostid_num;
4148 strlcat(path, suffix, len);
4149 }
4150#endif
4151 OSTRACE3("GETHOSTIDPATH %s pid=%d\n", path, getpid());
4152}
4153
4154/* get the host ID from a sqlite hostid file stored in the
4155** user-specific tmp directory, create the ID if it's not there already
4156*/
4157static int proxyGetHostID(char *pHostID, int *pError){
4158 int fd;
4159 char path[MAXPATHLEN];
4160 size_t len;
4161 int rc=SQLITE_OK;
4162
4163 proxyGetHostIDPath(path, MAXPATHLEN);
4164 /* try to create the host ID file, if it already exists read the contents */
4165 fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644);
4166 if( fd<0 ){
4167 int err=errno;
4168
4169 if( err!=EEXIST ){
4170#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
4171 fprintf(stderr, "sqlite error creating host ID file %s: %s\n",
4172 path, strerror(err));
4173#endif
4174 return SQLITE_PERM;
4175 }
4176 /* couldn't create the file, read it instead */
4177 fd = open(path, O_RDONLY|O_EXCL);
4178 if( fd<0 ){
4179#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
4180 int err = errno;
4181 fprintf(stderr, "sqlite error opening host ID file %s: %s\n",
4182 path, strerror(err));
4183#endif
4184 return SQLITE_PERM;
4185 }
4186 len = pread(fd, pHostID, HOSTIDLEN, 0);
4187 if( len<0 ){
4188 *pError = errno;
4189 rc = SQLITE_IOERR_READ;
4190 }else if( len<HOSTIDLEN ){
4191 *pError = 0;
4192 rc = SQLITE_IOERR_SHORT_READ;
4193 }
4194 close(fd); /* silently leak the fd if it fails */
4195 OSTRACE3("GETHOSTID read %s pid=%d\n", pHostID, getpid());
4196 return rc;
4197 }else{
4198 /* we're creating the host ID file (use a random string of bytes) */
4199 proxyGenerateHostID(pHostID);
4200 len = pwrite(fd, pHostID, HOSTIDLEN, 0);
4201 if( len<0 ){
4202 *pError = errno;
4203 rc = SQLITE_IOERR_WRITE;
4204 }else if( len<HOSTIDLEN ){
4205 *pError = 0;
4206 rc = SQLITE_IOERR_WRITE;
4207 }
4208 close(fd); /* silently leak the fd if it fails */
4209 OSTRACE3("GETHOSTID wrote %s pid=%d\n", pHostID, getpid());
4210 return rc;
4211 }
4212}
4213
4214static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
4215 int len;
4216 int dbLen;
4217 int i;
4218
4219#ifdef LOCKPROXYDIR
4220 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
4221#else
4222# ifdef _CS_DARWIN_USER_TEMP_DIR
4223 {
4224 confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
4225 len = strlcat(lPath, "sqliteplocks", maxLen);
4226 if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
4227 /* if mkdir fails, handle as lock file creation failure */
4228 int err = errno;
4229# ifdef SQLITE_DEBUG
4230 if( err!=EEXIST ){
4231 fprintf(stderr, "proxyGetLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
4232 SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
4233 }
4234# endif
4235 }else{
4236 OSTRACE3("GETLOCKPATH mkdir %s pid=%d\n", lPath, getpid());
4237 }
4238
4239 }
4240# else
4241 len = strlcpy(lPath, "/tmp/", maxLen);
4242# endif
4243#endif
4244
4245 if( lPath[len-1]!='/' ){
4246 len = strlcat(lPath, "/", maxLen);
4247 }
4248
4249 /* transform the db path to a unique cache name */
4250 dbLen = strlen(dbPath);
4251 for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
4252 char c = dbPath[i];
4253 lPath[i+len] = (c=='/')?'_':c;
4254 }
4255 lPath[i+len]='\0';
4256 strlcat(lPath, ":auto:", maxLen);
4257 return SQLITE_OK;
4258}
4259
4260/*
4261** Create a new VFS file descriptor (stored in memory obtained from
4262** sqlite3_malloc) and open the file named "path" in the file descriptor.
4263**
4264** The caller is responsible not only for closing the file descriptor
4265** but also for freeing the memory associated with the file descriptor.
4266*/
4267static int proxyCreateUnixFile(const char *path, unixFile **ppFile) {
4268 int fd;
4269 int dirfd = -1;
4270 unixFile *pNew;
4271 int rc = SQLITE_OK;
4272 sqlite3_vfs dummyVfs;
4273
4274 fd = open(path, O_RDWR | O_CREAT, SQLITE_DEFAULT_FILE_PERMISSIONS);
4275 if( fd<0 ){
4276 return SQLITE_CANTOPEN;
4277 }
4278
4279 pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
4280 if( pNew==NULL ){
4281 rc = SQLITE_NOMEM;
4282 goto end_create_proxy;
4283 }
4284 memset(pNew, 0, sizeof(unixFile));
4285
4286 dummyVfs.pAppData = (void*)autolockIoFinder;
4287 rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
4288 if( rc==SQLITE_OK ){
4289 *ppFile = pNew;
4290 return SQLITE_OK;
4291 }
4292end_create_proxy:
4293 close(fd); /* silently leak fd if error, we're already in error */
4294 sqlite3_free(pNew);
4295 return rc;
4296}
4297
4298/* takes the conch by taking a shared lock and read the contents conch, if
4299** lockPath is non-NULL, the host ID and lock file path must match. A NULL
4300** lockPath means that the lockPath in the conch file will be used if the
4301** host IDs match, or a new lock path will be generated automatically
4302** and written to the conch file.
4303*/
4304static int proxyTakeConch(unixFile *pFile){
4305 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4306
4307 if( pCtx->conchHeld>0 ){
4308 return SQLITE_OK;
4309 }else{
4310 unixFile *conchFile = pCtx->conchFile;
4311 char testValue[CONCHLEN];
4312 char conchValue[CONCHLEN];
4313 char lockPath[MAXPATHLEN];
4314 char *tLockPath = NULL;
4315 int rc = SQLITE_OK;
4316 int readRc = SQLITE_OK;
4317 int syncPerms = 0;
4318
4319 OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h,
4320 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
4321
4322 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
4323 if( rc==SQLITE_OK ){
4324 int pError = 0;
4325 memset(testValue, 0, CONCHLEN); // conch is fixed size
4326 rc = proxyGetHostID(testValue, &pError);
4327 if( (rc&0xff)==SQLITE_IOERR ){
4328 pFile->lastErrno = pError;
4329 }
4330 if( pCtx->lockProxyPath ){
4331 strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN);
4332 }
4333 }
4334 if( rc!=SQLITE_OK ){
4335 goto end_takeconch;
4336 }
4337
4338 readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0);
4339 if( readRc!=SQLITE_IOERR_SHORT_READ ){
4340 if( readRc!=SQLITE_OK ){
4341 if( (rc&0xff)==SQLITE_IOERR ){
4342 pFile->lastErrno = conchFile->lastErrno;
4343 }
4344 rc = readRc;
4345 goto end_takeconch;
4346 }
4347 /* if the conch has data compare the contents */
4348 if( !pCtx->lockProxyPath ){
4349 /* for auto-named local lock file, just check the host ID and we'll
4350 ** use the local lock file path that's already in there */
4351 if( !memcmp(testValue, conchValue, HOSTIDLEN) ){
4352 tLockPath = (char *)&conchValue[HOSTIDLEN];
4353 goto end_takeconch;
4354 }
4355 }else{
4356 /* we've got the conch if conchValue matches our path and host ID */
4357 if( !memcmp(testValue, conchValue, CONCHLEN) ){
4358 goto end_takeconch;
4359 }
4360 }
4361 }else{
4362 /* a short read means we're "creating" the conch (even though it could
4363 ** have been user-intervention), if we acquire the exclusive lock,
4364 ** we'll try to match the current on-disk permissions of the database
4365 */
4366 syncPerms = 1;
4367 }
4368
4369 /* either conch was emtpy or didn't match */
4370 if( !pCtx->lockProxyPath ){
4371 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
4372 tLockPath = lockPath;
4373 strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN);
4374 }
4375
4376 /* update conch with host and path (this will fail if other process
4377 ** has a shared lock already) */
4378 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
4379 if( rc==SQLITE_OK ){
4380 rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0);
4381 if( rc==SQLITE_OK && syncPerms ){
4382 struct stat buf;
4383 int err = fstat(pFile->h, &buf);
4384 if( err==0 ){
4385 /* try to match the database file permissions, ignore failure */
4386#ifndef SQLITE_PROXY_DEBUG
4387 fchmod(conchFile->h, buf.st_mode);
4388#else
4389 if( fchmod(conchFile->h, buf.st_mode)!=0 ){
4390 int code = errno;
4391 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
4392 buf.st_mode, code, strerror(code));
4393 } else {
4394 fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode);
4395 }
4396 }else{
4397 int code = errno;
4398 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
4399 err, code, strerror(code));
4400#endif
4401 }
4402 }
4403 }
4404 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
4405
4406end_takeconch:
4407 OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h);
4408 if( rc==SQLITE_OK && pFile->openFlags ){
4409 if( pFile->h>=0 ){
4410#ifdef STRICT_CLOSE_ERROR
4411 if( close(pFile->h) ){
4412 pFile->lastErrno = errno;
4413 return SQLITE_IOERR_CLOSE;
4414 }
4415#else
4416 close(pFile->h); /* silently leak fd if fail */
4417#endif
4418 }
4419 pFile->h = -1;
4420 int fd = open(pCtx->dbPath, pFile->openFlags,
4421 SQLITE_DEFAULT_FILE_PERMISSIONS);
4422 OSTRACE2("TRANSPROXY: OPEN %d\n", fd);
4423 if( fd>=0 ){
4424 pFile->h = fd;
4425 }else{
4426 rc=SQLITE_CANTOPEN; // SQLITE_BUSY? proxyTakeConch called during locking
4427 }
4428 }
4429 if( rc==SQLITE_OK && !pCtx->lockProxy ){
4430 char *path = tLockPath ? tLockPath : pCtx->lockProxyPath;
4431 // ACS: Need to make a copy of path sometimes
4432 rc = proxyCreateUnixFile(path, &pCtx->lockProxy);
4433 }
4434 if( rc==SQLITE_OK ){
4435 pCtx->conchHeld = 1;
4436
4437 if( tLockPath ){
4438 pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath);
4439 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
4440 ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath =
4441 pCtx->lockProxyPath;
4442 }
4443 }
4444 } else {
4445 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
4446 }
4447 OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed");
4448 return rc;
4449 }
4450}
4451
4452/*
4453** If pFile holds a lock on a conch file, then release that lock.
4454*/
4455static int proxyReleaseConch(unixFile *pFile){
4456 int rc; /* Subroutine return code */
4457 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
4458 unixFile *conchFile; /* Name of the conch file */
4459
4460 pCtx = (proxyLockingContext *)pFile->lockingContext;
4461 conchFile = pCtx->conchFile;
4462 OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
4463 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
4464 getpid());
4465 pCtx->conchHeld = 0;
4466 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
4467 OSTRACE3("RELEASECONCH %d %s\n", conchFile->h,
4468 (rc==SQLITE_OK ? "ok" : "failed"));
4469 return rc;
4470}
4471
4472/*
4473** Given the name of a database file, compute the name of its conch file.
4474** Store the conch filename in memory obtained from sqlite3_malloc().
4475** Make *pConchPath point to the new name. Return SQLITE_OK on success
4476** or SQLITE_NOMEM if unable to obtain memory.
4477**
4478** The caller is responsible for ensuring that the allocated memory
4479** space is eventually freed.
4480**
4481** *pConchPath is set to NULL if a memory allocation error occurs.
4482*/
4483static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
4484 int i; /* Loop counter */
4485 int len = strlen(dbPath); /* Length of database filename - dbPath */
4486 char *conchPath; /* buffer in which to construct conch name */
4487
4488 /* Allocate space for the conch filename and initialize the name to
4489 ** the name of the original database file. */
4490 *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
4491 if( conchPath==0 ){
4492 return SQLITE_NOMEM;
4493 }
4494 memcpy(conchPath, dbPath, len+1);
4495
4496 /* now insert a "." before the last / character */
4497 for( i=(len-1); i>=0; i-- ){
4498 if( conchPath[i]=='/' ){
4499 i++;
4500 break;
4501 }
4502 }
4503 conchPath[i]='.';
4504 while ( i<len ){
4505 conchPath[i+1]=dbPath[i];
4506 i++;
4507 }
4508
4509 /* append the "-conch" suffix to the file */
4510 memcpy(&conchPath[i+1], "-conch", 7);
4511 assert( strlen(conchPath) == len+7 );
4512
4513 return SQLITE_OK;
4514}
4515
4516
4517/* Takes a fully configured proxy locking-style unix file and switches
4518** the local lock file path
4519*/
4520static int switchLockProxyPath(unixFile *pFile, const char *path) {
4521 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
4522 char *oldPath = pCtx->lockProxyPath;
4523 int rc = SQLITE_OK;
4524
4525 if( pFile->locktype!=NO_LOCK ){
4526 return SQLITE_BUSY;
4527 }
4528
4529 /* nothing to do if the path is NULL, :auto: or matches the existing path */
4530 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
4531 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
4532 return SQLITE_OK;
4533 }else{
4534 unixFile *lockProxy = pCtx->lockProxy;
4535 pCtx->lockProxy=NULL;
4536 pCtx->conchHeld = 0;
4537 if( lockProxy!=NULL ){
4538 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
4539 if( rc ) return rc;
4540 sqlite3_free(lockProxy);
4541 }
4542 sqlite3_free(oldPath);
4543 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
4544 }
4545
4546 return rc;
4547}
4548
4549/*
4550** pFile is a file that has been opened by a prior xOpen call. dbPath
4551** is a string buffer at least MAXPATHLEN+1 characters in size.
4552**
4553** This routine find the filename associated with pFile and writes it
4554** int dbPath.
4555*/
4556static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
4557#if defined(__DARWIN__)
4558 if( pFile->pMethod == &afpIoMethods ){
4559 /* afp style keeps a reference to the db path in the filePath field
4560 ** of the struct */
4561 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
4562 strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath);
4563 }else
4564#endif
4565 if( pFile->pMethod == &dotlockIoMethods ){
4566 /* dot lock style uses the locking context to store the dot lock
4567 ** file path */
4568 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
4569 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
4570 }else{
4571 /* all other styles use the locking context to store the db file path */
4572 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
4573 strcpy(dbPath, (char *)pFile->lockingContext);
4574 }
4575 return SQLITE_OK;
4576}
4577
4578/*
4579** Takes an already filled in unix file and alters it so all file locking
4580** will be performed on the local proxy lock file. The following fields
4581** are preserved in the locking context so that they can be restored and
4582** the unix structure properly cleaned up at close time:
4583** ->lockingContext
4584** ->pMethod
4585*/
4586static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
4587 proxyLockingContext *pCtx;
4588 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
4589 char *lockPath=NULL;
4590 int rc = SQLITE_OK;
4591
4592 if( pFile->locktype!=NO_LOCK ){
4593 return SQLITE_BUSY;
4594 }
4595 proxyGetDbPathForUnixFile(pFile, dbPath);
4596 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
4597 lockPath=NULL;
4598 }else{
4599 lockPath=(char *)path;
4600 }
4601
4602 OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h,
4603 (lockPath ? lockPath : ":auto:"), getpid());
4604
4605 pCtx = sqlite3_malloc( sizeof(*pCtx) );
4606 if( pCtx==0 ){
4607 return SQLITE_NOMEM;
4608 }
4609 memset(pCtx, 0, sizeof(*pCtx));
4610
4611 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
4612 if( rc==SQLITE_OK ){
4613 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
4614 }
4615 if( rc==SQLITE_OK && lockPath ){
4616 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
4617 }
4618
4619 if( rc==SQLITE_OK ){
4620 /* all memory is allocated, proxys are created and assigned,
4621 ** switch the locking context and pMethod then return.
4622 */
4623 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
4624 pCtx->oldLockingContext = pFile->lockingContext;
4625 pFile->lockingContext = pCtx;
4626 pCtx->pOldMethod = pFile->pMethod;
4627 pFile->pMethod = &proxyIoMethods;
4628 }else{
4629 if( pCtx->conchFile ){
4630 rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
4631 if( rc ) return rc;
4632 sqlite3_free(pCtx->conchFile);
4633 }
4634 sqlite3_free(pCtx->conchFilePath);
4635 sqlite3_free(pCtx);
4636 }
4637 OSTRACE3("TRANSPROXY %d %s\n", pFile->h,
4638 (rc==SQLITE_OK ? "ok" : "failed"));
4639 return rc;
4640}
4641
4642
4643/*
4644** This routine handles sqlite3_file_control() calls that are specific
4645** to proxy locking.
4646*/
4647static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
4648 switch( op ){
4649 case SQLITE_GET_LOCKPROXYFILE: {
4650 unixFile *pFile = (unixFile*)id;
4651 if( pFile->pMethod == &proxyIoMethods ){
4652 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
4653 proxyTakeConch(pFile);
4654 if( pCtx->lockProxyPath ){
4655 *(const char **)pArg = pCtx->lockProxyPath;
4656 }else{
4657 *(const char **)pArg = ":auto: (not held)";
4658 }
4659 } else {
4660 *(const char **)pArg = NULL;
4661 }
4662 return SQLITE_OK;
4663 }
4664 case SQLITE_SET_LOCKPROXYFILE: {
4665 unixFile *pFile = (unixFile*)id;
4666 int rc = SQLITE_OK;
4667 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
4668 if( pArg==NULL || (const char *)pArg==0 ){
4669 if( isProxyStyle ){
4670 /* turn off proxy locking - not supported */
4671 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
4672 }else{
4673 /* turn off proxy locking - already off - NOOP */
4674 rc = SQLITE_OK;
4675 }
4676 }else{
4677 const char *proxyPath = (const char *)pArg;
4678 if( isProxyStyle ){
4679 proxyLockingContext *pCtx =
4680 (proxyLockingContext*)pFile->lockingContext;
4681 if( !strcmp(pArg, ":auto:")
4682 || (pCtx->lockProxyPath &&
4683 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
4684 ){
4685 rc = SQLITE_OK;
4686 }else{
4687 rc = switchLockProxyPath(pFile, proxyPath);
4688 }
4689 }else{
4690 /* turn on proxy file locking */
4691 rc = proxyTransformUnixFile(pFile, proxyPath);
4692 }
4693 }
4694 return rc;
4695 }
4696 default: {
4697 assert( 0 ); /* The call assures that only valid opcodes are sent */
4698 }
4699 }
4700 /*NOTREACHED*/
4701 return SQLITE_ERROR;
4702}
4703
4704/*
4705** Within this division (the proxying locking implementation) the procedures
4706** above this point are all utilities. The lock-related methods of the
4707** proxy-locking sqlite3_io_method object follow.
4708*/
4709
4710
4711/*
4712** This routine checks if there is a RESERVED lock held on the specified
4713** file by this or any other process. If such a lock is held, set *pResOut
4714** to a non-zero value otherwise *pResOut is set to zero. The return value
4715** is set to SQLITE_OK unless an I/O error occurs during lock checking.
4716*/
4717static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
4718 unixFile *pFile = (unixFile*)id;
4719 int rc = proxyTakeConch(pFile);
4720 if( rc==SQLITE_OK ){
4721 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4722 unixFile *proxy = pCtx->lockProxy;
4723 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
4724 }
4725 return rc;
4726}
4727
4728/*
4729** Lock the file with the lock specified by parameter locktype - one
4730** of the following:
4731**
4732** (1) SHARED_LOCK
4733** (2) RESERVED_LOCK
4734** (3) PENDING_LOCK
4735** (4) EXCLUSIVE_LOCK
4736**
4737** Sometimes when requesting one lock state, additional lock states
4738** are inserted in between. The locking might fail on one of the later
4739** transitions leaving the lock state different from what it started but
4740** still short of its goal. The following chart shows the allowed
4741** transitions and the inserted intermediate states:
4742**
4743** UNLOCKED -> SHARED
4744** SHARED -> RESERVED
4745** SHARED -> (PENDING) -> EXCLUSIVE
4746** RESERVED -> (PENDING) -> EXCLUSIVE
4747** PENDING -> EXCLUSIVE
4748**
4749** This routine will only increase a lock. Use the sqlite3OsUnlock()
4750** routine to lower a locking level.
4751*/
4752static int proxyLock(sqlite3_file *id, int locktype) {
4753 unixFile *pFile = (unixFile*)id;
4754 int rc = proxyTakeConch(pFile);
4755 if( rc==SQLITE_OK ){
4756 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4757 unixFile *proxy = pCtx->lockProxy;
4758 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
4759 pFile->locktype = proxy->locktype;
4760 }
4761 return rc;
4762}
4763
4764
4765/*
4766** Lower the locking level on file descriptor pFile to locktype. locktype
4767** must be either NO_LOCK or SHARED_LOCK.
4768**
4769** If the locking level of the file descriptor is already at or below
4770** the requested locking level, this routine is a no-op.
4771*/
4772static int proxyUnlock(sqlite3_file *id, int locktype) {
4773 unixFile *pFile = (unixFile*)id;
4774 int rc = proxyTakeConch(pFile);
4775 if( rc==SQLITE_OK ){
4776 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4777 unixFile *proxy = pCtx->lockProxy;
4778 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
4779 pFile->locktype = proxy->locktype;
4780 }
4781 return rc;
4782}
4783
4784/*
4785** Close a file that uses proxy locks.
4786*/
4787static int proxyClose(sqlite3_file *id) {
4788 if( id ){
4789 unixFile *pFile = (unixFile*)id;
4790 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4791 unixFile *lockProxy = pCtx->lockProxy;
4792 unixFile *conchFile = pCtx->conchFile;
4793 int rc = SQLITE_OK;
4794
4795 if( lockProxy ){
4796 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
4797 if( rc ) return rc;
4798 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
4799 if( rc ) return rc;
4800 sqlite3_free(lockProxy);
4801 pCtx->lockProxy = 0;
4802 }
4803 if( conchFile ){
4804 if( pCtx->conchHeld ){
4805 rc = proxyReleaseConch(pFile);
4806 if( rc ) return rc;
4807 }
4808 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
4809 if( rc ) return rc;
4810 sqlite3_free(conchFile);
4811 }
4812 sqlite3_free(pCtx->lockProxyPath);
4813 sqlite3_free(pCtx->conchFilePath);
4814 sqlite3_free(pCtx->dbPath);
4815 /* restore the original locking context and pMethod then close it */
4816 pFile->lockingContext = pCtx->oldLockingContext;
4817 pFile->pMethod = pCtx->pOldMethod;
4818 sqlite3_free(pCtx);
4819 return pFile->pMethod->xClose(id);
4820 }
4821 return SQLITE_OK;
4822}
4823
4824
4825
4826#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
4827/*
4828** The proxy locking style is intended for use with AFP filesystems.
4829** And since AFP is only supported on MacOSX, the proxy locking is also
4830** restricted to MacOSX.
4831**
4832**
4833******************* End of the proxy lock implementation **********************
4834******************************************************************************/
4835
drh734c9862008-11-28 15:37:20 +00004836/*
danielk1977e339d652008-06-28 11:23:00 +00004837** Initialize the operating system interface.
drh734c9862008-11-28 15:37:20 +00004838**
4839** This routine registers all VFS implementations for unix-like operating
4840** systems. This routine, and the sqlite3_os_end() routine that follows,
4841** should be the only routines in this file that are visible from other
4842** files.
drh6b9d6dd2008-12-03 19:34:47 +00004843**
4844** This routine is called once during SQLite initialization and by a
4845** single thread. The memory allocation and mutex subsystems have not
4846** necessarily been initialized when this routine is called, and so they
4847** should not be used.
drh153c62c2007-08-24 03:51:33 +00004848*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004849int sqlite3_os_init(void){
drh6b9d6dd2008-12-03 19:34:47 +00004850 /*
4851 ** The following macro defines an initializer for an sqlite3_vfs object.
4852 ** The name of the VFS is NAME. The pAppData is a pointer to a "finder"
4853 ** function. The FINDER parameter to this macro is the name of the
4854 ** finder-function. The finder-function returns a pointer to the
4855 ** sqlite_io_methods object that implements the desired locking
4856 ** behaviors. See the division above that contains the IOMETHODS
4857 ** macro for addition information on finder-functions.
4858 **
4859 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
4860 ** object. But the "autolockIoFinder" available on MacOSX does a little
4861 ** more than that; it looks at the filesystem type that hosts the
4862 ** database file and tries to choose an locking method appropriate for
4863 ** that filesystem time.
danielk1977e339d652008-06-28 11:23:00 +00004864 */
drh7708e972008-11-29 00:56:52 +00004865 #define UNIXVFS(VFSNAME, FINDER) { \
danielk1977e339d652008-06-28 11:23:00 +00004866 1, /* iVersion */ \
4867 sizeof(unixFile), /* szOsFile */ \
4868 MAX_PATHNAME, /* mxPathname */ \
4869 0, /* pNext */ \
drh7708e972008-11-29 00:56:52 +00004870 VFSNAME, /* zName */ \
4871 (void*)FINDER, /* pAppData */ \
danielk1977e339d652008-06-28 11:23:00 +00004872 unixOpen, /* xOpen */ \
4873 unixDelete, /* xDelete */ \
4874 unixAccess, /* xAccess */ \
4875 unixFullPathname, /* xFullPathname */ \
4876 unixDlOpen, /* xDlOpen */ \
4877 unixDlError, /* xDlError */ \
4878 unixDlSym, /* xDlSym */ \
4879 unixDlClose, /* xDlClose */ \
4880 unixRandomness, /* xRandomness */ \
4881 unixSleep, /* xSleep */ \
4882 unixCurrentTime, /* xCurrentTime */ \
4883 unixGetLastError /* xGetLastError */ \
4884 }
4885
drh6b9d6dd2008-12-03 19:34:47 +00004886 /*
4887 ** All default VFSes for unix are contained in the following array.
4888 **
4889 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
4890 ** by the SQLite core when the VFS is registered. So the following
4891 ** array cannot be const.
4892 */
danielk1977e339d652008-06-28 11:23:00 +00004893 static sqlite3_vfs aVfs[] = {
drh7708e972008-11-29 00:56:52 +00004894#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
4895 UNIXVFS("unix", autolockIoFinder ),
4896#else
4897 UNIXVFS("unix", posixIoFinder ),
4898#endif
4899 UNIXVFS("unix-none", nolockIoFinder ),
4900 UNIXVFS("unix-dotfile", dotlockIoFinder ),
drh734c9862008-11-28 15:37:20 +00004901#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00004902 UNIXVFS("unix-namedsem", semIoFinder ),
drh734c9862008-11-28 15:37:20 +00004903#endif
4904#if SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00004905 UNIXVFS("unix-posix", posixIoFinder ),
4906 UNIXVFS("unix-flock", flockIoFinder ),
drh734c9862008-11-28 15:37:20 +00004907#endif
4908#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
drh7708e972008-11-29 00:56:52 +00004909 UNIXVFS("unix-afp", afpIoFinder ),
4910 UNIXVFS("unix-proxy", proxyIoFinder ),
drh734c9862008-11-28 15:37:20 +00004911#endif
drh153c62c2007-08-24 03:51:33 +00004912 };
drh6b9d6dd2008-12-03 19:34:47 +00004913 unsigned int i; /* Loop counter */
4914
4915 /* Register all VFSes defined in the aVfs[] array */
danielk1977e339d652008-06-28 11:23:00 +00004916 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
drh734c9862008-11-28 15:37:20 +00004917 sqlite3_vfs_register(&aVfs[i], i==0);
danielk1977e339d652008-06-28 11:23:00 +00004918 }
danielk1977c0fa4c52008-06-25 17:19:00 +00004919 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00004920}
danielk1977e339d652008-06-28 11:23:00 +00004921
4922/*
drh6b9d6dd2008-12-03 19:34:47 +00004923** Shutdown the operating system interface.
4924**
4925** Some operating systems might need to do some cleanup in this routine,
4926** to release dynamically allocated objects. But not on unix.
4927** This routine is a no-op for unix.
danielk1977e339d652008-06-28 11:23:00 +00004928*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004929int sqlite3_os_end(void){
4930 return SQLITE_OK;
4931}
drhdce8bdb2007-08-16 13:01:44 +00004932
danielk197729bafea2008-06-26 10:41:19 +00004933#endif /* SQLITE_OS_UNIX */