4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 ******************************************************************************
13 ** This file contains the VFS implementation for unix-like operating systems
14 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
16 ** 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.
22 ** This source file is organized into divisions where the logic for various
23 ** 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.
27 ** The layout of divisions is as follows:
29 ** * General-purpose declarations and utility functions.
30 ** * Unique file ID logic used by VxWorks.
31 ** * Various locking primitive implementations (all except proxy locking):
32 ** + for Posix Advisory Locks
34 ** + for dot-file locks
35 ** + for flock() locking
36 ** + for named semaphore locks (VxWorks only)
37 ** + for AFP filesystem locks (MacOSX only)
38 ** * 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.
41 ** * sqlite3_vfs method implementations.
42 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
43 ** * Definitions of sqlite3_vfs objects for all locking methods
44 ** plus implementations of sqlite3_os_init() and sqlite3_os_end().
46 #include "sqliteInt.h"
47 #if SQLITE_OS_UNIX /* This file is used on unix only */
50 ** There are various methods for file locking used for concurrency
53 ** 1. POSIX locking (the default),
55 ** 3. Dot-file locking,
56 ** 4. flock() locking,
57 ** 5. AFP locking (OSX only),
58 ** 6. Named POSIX semaphores (VXWorks only),
59 ** 7. proxy locking. (OSX only)
61 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
62 ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
63 ** selection of the appropriate locking style based on the filesystem
64 ** where the database is located.
66 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
67 # if defined(__APPLE__)
68 # define SQLITE_ENABLE_LOCKING_STYLE 1
70 # define SQLITE_ENABLE_LOCKING_STYLE 0
75 ** Define the OS_VXWORKS pre-processor macro to 1 if building on
76 ** vxworks, or 0 otherwise.
79 # if defined(__RTP__) || defined(_WRS_KERNEL)
87 ** These #defines should enable >2GB file support on Posix if the
88 ** underlying operating system supports it. If the OS lacks
89 ** large file support, these should be no-ops.
91 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
92 ** on the compiler command line. This is necessary if you are compiling
93 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
94 ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
95 ** without this option, LFS is enable. But LFS does not exist in the kernel
96 ** in RedHat 6.0, so the code won't work. Hence, for maximum binary
97 ** portability you should omit LFS.
99 ** The previous paragraph was written in 2005. (This paragraph is written
100 ** on 2008-11-28.) These days, all Linux kernels support large files, so
101 ** you should probably leave LFS enabled. But some embedded platforms might
102 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
104 #ifndef SQLITE_DISABLE_LFS
105 # define _LARGE_FILE 1
106 # ifndef _FILE_OFFSET_BITS
107 # define _FILE_OFFSET_BITS 64
109 # define _LARGEFILE_SOURCE 1
113 ** standard include files.
115 #include <sys/types.h>
116 #include <sys/stat.h>
120 #include <sys/time.h>
122 #ifndef SQLITE_OMIT_WAL
123 #include <sys/mman.h>
127 #if SQLITE_ENABLE_LOCKING_STYLE
128 # include <sys/ioctl.h>
130 # include <semaphore.h>
133 # include <sys/file.h>
134 # include <sys/param.h>
136 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
138 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
139 # include <sys/mount.h>
147 ** Allowed values of unixFile.fsFlags
149 #define SQLITE_FSFLAGS_IS_MSDOS 0x1
152 ** If we are to be thread-safe, include the pthreads header and define
153 ** the SQLITE_UNIX_THREADS macro.
155 #if SQLITE_THREADSAFE
156 # include <pthread.h>
157 # define SQLITE_UNIX_THREADS 1
161 ** Default permissions when creating a new file
163 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
164 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
168 ** Default permissions when creating auto proxy dir
170 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
171 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
175 ** Maximum supported path-length.
177 #define MAX_PATHNAME 512
180 ** Only set the lastErrno if the error code is a real error and not
181 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
183 #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
185 /* Forward references */
186 typedef struct unixShm unixShm
; /* Connection shared memory */
187 typedef struct unixShmNode unixShmNode
; /* Shared memory instance */
188 typedef struct unixInodeInfo unixInodeInfo
; /* An i-node */
189 typedef struct UnixUnusedFd UnixUnusedFd
; /* An unused file descriptor */
192 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
193 ** cannot be closed immediately. In these cases, instances of the following
194 ** structure are used to store the file descriptor while waiting for an
195 ** opportunity to either close or reuse it.
197 struct UnixUnusedFd
{
198 int fd
; /* File descriptor to close */
199 int flags
; /* Flags this file descriptor was opened with */
200 UnixUnusedFd
*pNext
; /* Next unused file descriptor on same file */
204 ** The unixFile structure is subclass of sqlite3_file specific to the unix
205 ** VFS implementations.
207 typedef struct unixFile unixFile
;
209 sqlite3_io_methods
const *pMethod
; /* Always the first entry */
210 sqlite3_vfs
*pVfs
; /* The VFS that created this unixFile */
211 unixInodeInfo
*pInode
; /* Info about locks on this inode */
212 int h
; /* The file descriptor */
213 unsigned char eFileLock
; /* The type of lock held on this fd */
214 unsigned char ctrlFlags
; /* Behavioral bits. UNIXFILE_* flags */
215 int lastErrno
; /* The unix errno from last I/O error */
216 void *lockingContext
; /* Locking style specific state */
217 UnixUnusedFd
*pUnused
; /* Pre-allocated UnixUnusedFd */
218 const char *zPath
; /* Name of the file */
219 unixShm
*pShm
; /* Shared memory segment information */
220 int szChunk
; /* Configured by FCNTL_CHUNK_SIZE */
221 #if SQLITE_ENABLE_LOCKING_STYLE
222 int openFlags
; /* The flags specified at open() */
224 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
225 unsigned fsFlags
; /* cached details from statfs() */
228 struct vxworksFileId
*pId
; /* Unique file ID */
231 /* The next group of variables are used to track whether or not the
232 ** transaction counter in bytes 24-27 of database files are updated
233 ** whenever any part of the database changes. An assertion fault will
234 ** occur if a file is updated without also updating the transaction
235 ** counter. This test is made to avoid new problems similar to the
236 ** one described by ticket #3584.
238 unsigned char transCntrChng
; /* True if the transaction counter changed */
239 unsigned char dbUpdate
; /* True if any part of database file changed */
240 unsigned char inNormalWrite
; /* True if in a normal write operation */
243 /* In test mode, increase the size of this structure a bit so that
244 ** it is larger than the struct CrashFile defined in test6.c.
251 ** Allowed values for the unixFile.ctrlFlags bitmask:
253 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
254 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
255 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
256 #ifndef SQLITE_DISABLE_DIRSYNC
257 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
259 # define UNIXFILE_DIRSYNC 0x00
261 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
262 #define UNIXFILE_DELETE 0x20 /* Delete on close */
263 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
264 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
267 ** Include code that is common to all os_*.c files
269 #include "os_common.h"
272 ** Define various macros that are missing from some systems.
275 # define O_LARGEFILE 0
277 #ifdef SQLITE_DISABLE_LFS
279 # define O_LARGEFILE 0
282 # define O_NOFOLLOW 0
289 ** The threadid macro resolves to the thread-id or to 0. Used for
290 ** testing and debugging only.
292 #if SQLITE_THREADSAFE
293 #define threadid pthread_self()
299 ** Different Unix systems declare open() in different ways. Same use
300 ** open(const char*,int,mode_t). Others use open(const char*,int,...).
301 ** The difference is important when using a pointer to the function.
303 ** The safest way to deal with the problem is to always use this wrapper
304 ** which always has the same well-defined interface.
306 static int posixOpen(const char *zFile
, int flags
, int mode
){
307 return open(zFile
, flags
, mode
);
310 /* Forward reference */
311 static int openDirectory(const char*, int*);
314 ** Many system calls are accessed through pointer-to-functions so that
315 ** they may be overridden at runtime to facilitate fault injection during
316 ** testing and sandboxing. The following array holds the names and pointers
317 ** to all overrideable system calls.
319 static struct unix_syscall
{
320 const char *zName
; /* Name of the sytem call */
321 sqlite3_syscall_ptr pCurrent
; /* Current value of the system call */
322 sqlite3_syscall_ptr pDefault
; /* Default value */
324 { "open", (sqlite3_syscall_ptr
)posixOpen
, 0 },
325 #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
327 { "close", (sqlite3_syscall_ptr
)close
, 0 },
328 #define osClose ((int(*)(int))aSyscall[1].pCurrent)
330 { "access", (sqlite3_syscall_ptr
)access
, 0 },
331 #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
333 { "getcwd", (sqlite3_syscall_ptr
)getcwd
, 0 },
334 #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
336 { "stat", (sqlite3_syscall_ptr
)stat
, 0 },
337 #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
340 ** The DJGPP compiler environment looks mostly like Unix, but it
341 ** lacks the fcntl() system call. So redefine fcntl() to be something
342 ** that always succeeds. This means that locking does not occur under
343 ** DJGPP. But it is DOS - what did you expect?
347 #define osFstat(a,b,c) 0
349 { "fstat", (sqlite3_syscall_ptr
)fstat
, 0 },
350 #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
353 { "ftruncate", (sqlite3_syscall_ptr
)ftruncate
, 0 },
354 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
356 { "fcntl", (sqlite3_syscall_ptr
)fcntl
, 0 },
357 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
359 { "read", (sqlite3_syscall_ptr
)read
, 0 },
360 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
362 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
363 { "pread", (sqlite3_syscall_ptr
)pread
, 0 },
365 { "pread", (sqlite3_syscall_ptr
)0, 0 },
367 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
369 #if defined(USE_PREAD64)
370 { "pread64", (sqlite3_syscall_ptr
)pread64
, 0 },
372 { "pread64", (sqlite3_syscall_ptr
)0, 0 },
374 #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
376 { "write", (sqlite3_syscall_ptr
)write
, 0 },
377 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
379 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
380 { "pwrite", (sqlite3_syscall_ptr
)pwrite
, 0 },
382 { "pwrite", (sqlite3_syscall_ptr
)0, 0 },
384 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
385 aSyscall[12].pCurrent)
387 #if defined(USE_PREAD64)
388 { "pwrite64", (sqlite3_syscall_ptr
)pwrite64
, 0 },
390 { "pwrite64", (sqlite3_syscall_ptr
)0, 0 },
392 #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\
393 aSyscall[13].pCurrent)
395 #if SQLITE_ENABLE_LOCKING_STYLE
396 { "fchmod", (sqlite3_syscall_ptr
)fchmod
, 0 },
398 { "fchmod", (sqlite3_syscall_ptr
)0, 0 },
400 #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
402 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
403 { "fallocate", (sqlite3_syscall_ptr
)posix_fallocate
, 0 },
405 { "fallocate", (sqlite3_syscall_ptr
)0, 0 },
407 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
409 { "unlink", (sqlite3_syscall_ptr
)unlink
, 0 },
410 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
412 { "openDirectory", (sqlite3_syscall_ptr
)openDirectory
, 0 },
413 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
415 { "mkdir", (sqlite3_syscall_ptr
)mkdir
, 0 },
416 #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
418 { "rmdir", (sqlite3_syscall_ptr
)rmdir
, 0 },
419 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
421 }; /* End of the overrideable system calls */
424 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
425 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
426 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
427 ** system call named zName.
429 static int unixSetSystemCall(
430 sqlite3_vfs
*pNotUsed
, /* The VFS pointer. Not used */
431 const char *zName
, /* Name of system call to override */
432 sqlite3_syscall_ptr pNewFunc
/* Pointer to new system call value */
435 int rc
= SQLITE_NOTFOUND
;
437 UNUSED_PARAMETER(pNotUsed
);
439 /* If no zName is given, restore all system calls to their default
440 ** settings and return NULL
443 for(i
=0; i
<sizeof(aSyscall
)/sizeof(aSyscall
[0]); i
++){
444 if( aSyscall
[i
].pDefault
){
445 aSyscall
[i
].pCurrent
= aSyscall
[i
].pDefault
;
449 /* If zName is specified, operate on only the one system call
452 for(i
=0; i
<sizeof(aSyscall
)/sizeof(aSyscall
[0]); i
++){
453 if( strcmp(zName
, aSyscall
[i
].zName
)==0 ){
454 if( aSyscall
[i
].pDefault
==0 ){
455 aSyscall
[i
].pDefault
= aSyscall
[i
].pCurrent
;
458 if( pNewFunc
==0 ) pNewFunc
= aSyscall
[i
].pDefault
;
459 aSyscall
[i
].pCurrent
= pNewFunc
;
468 ** Return the value of a system call. Return NULL if zName is not a
469 ** recognized system call name. NULL is also returned if the system call
470 ** is currently undefined.
472 static sqlite3_syscall_ptr
unixGetSystemCall(
473 sqlite3_vfs
*pNotUsed
,
478 UNUSED_PARAMETER(pNotUsed
);
479 for(i
=0; i
<sizeof(aSyscall
)/sizeof(aSyscall
[0]); i
++){
480 if( strcmp(zName
, aSyscall
[i
].zName
)==0 ) return aSyscall
[i
].pCurrent
;
486 ** Return the name of the first system call after zName. If zName==NULL
487 ** then return the name of the first system call. Return NULL if zName
488 ** is the last system call or if zName is not the name of a valid
491 static const char *unixNextSystemCall(sqlite3_vfs
*p
, const char *zName
){
496 for(i
=0; i
<ArraySize(aSyscall
)-1; i
++){
497 if( strcmp(zName
, aSyscall
[i
].zName
)==0 ) break;
500 for(i
++; i
<ArraySize(aSyscall
); i
++){
501 if( aSyscall
[i
].pCurrent
!=0 ) return aSyscall
[i
].zName
;
507 ** Retry open() calls that fail due to EINTR
509 static int robust_open(const char *z
, int f
, int m
){
511 do{ rc
= osOpen(z
,f
,m
); }while( rc
<0 && errno
==EINTR
);
516 ** Helper functions to obtain and relinquish the global mutex. The
517 ** global mutex is used to protect the unixInodeInfo and
518 ** vxworksFileId objects used by this file, all of which may be
519 ** shared by multiple threads.
521 ** Function unixMutexHeld() is used to assert() that the global mutex
522 ** is held when required. This function is only used as part of assert()
526 ** assert( unixMutexHeld() );
529 static void unixEnterMutex(void){
530 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER
));
532 static void unixLeaveMutex(void){
533 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER
));
536 static int unixMutexHeld(void) {
537 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER
));
542 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
544 ** Helper function for printing out trace information from debugging
545 ** binaries. This returns the string represetation of the supplied
546 ** integer lock-type.
548 static const char *azFileLock(int eFileLock
){
550 case NO_LOCK
: return "NONE";
551 case SHARED_LOCK
: return "SHARED";
552 case RESERVED_LOCK
: return "RESERVED";
553 case PENDING_LOCK
: return "PENDING";
554 case EXCLUSIVE_LOCK
: return "EXCLUSIVE";
560 #ifdef SQLITE_LOCK_TRACE
562 ** Print out information about all locking operations.
564 ** This routine is used for troubleshooting locks on multithreaded
565 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
566 ** command-line option on the compiler. This code is normally
569 static int lockTrace(int fd
, int op
, struct flock
*p
){
570 char *zOpName
, *zType
;
575 }else if( op
==F_SETLK
){
578 s
= osFcntl(fd
, op
, p
);
579 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd
, op
, s
);
582 if( p
->l_type
==F_RDLCK
){
584 }else if( p
->l_type
==F_WRLCK
){
586 }else if( p
->l_type
==F_UNLCK
){
591 assert( p
->l_whence
==SEEK_SET
);
592 s
= osFcntl(fd
, op
, p
);
594 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
595 threadid
, fd
, zOpName
, zType
, (int)p
->l_start
, (int)p
->l_len
,
597 if( s
==(-1) && op
==F_SETLK
&& (p
->l_type
==F_RDLCK
|| p
->l_type
==F_WRLCK
) ){
600 osFcntl(fd
, F_GETLK
, &l2
);
601 if( l2
.l_type
==F_RDLCK
){
603 }else if( l2
.l_type
==F_WRLCK
){
605 }else if( l2
.l_type
==F_UNLCK
){
610 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
611 zType
, (int)l2
.l_start
, (int)l2
.l_len
, (int)l2
.l_pid
);
617 #define osFcntl lockTrace
618 #endif /* SQLITE_LOCK_TRACE */
621 ** Retry ftruncate() calls that fail due to EINTR
623 static int robust_ftruncate(int h
, sqlite3_int64 sz
){
625 do{ rc
= osFtruncate(h
,sz
); }while( rc
<0 && errno
==EINTR
);
630 ** This routine translates a standard POSIX errno code into something
631 ** useful to the clients of the sqlite3 functions. Specifically, it is
632 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
633 ** and a variety of "please close the file descriptor NOW" errors into
636 ** Errors during initialization of locks, or file system support for locks,
637 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
639 static int sqliteErrorFromPosixError(int posixError
, int sqliteIOErr
) {
640 switch (posixError
) {
642 /* At one point this code was not commented out. In theory, this branch
643 ** should never be hit, as this function should only be called after
644 ** a locking-related function (i.e. fcntl()) has returned non-zero with
645 ** the value of errno as the first argument. Since a system call has failed,
646 ** errno should be non-zero.
648 ** Despite this, if errno really is zero, we still don't want to return
649 ** SQLITE_OK. The system call failed, and *some* SQLite error should be
650 ** propagated back to the caller. Commenting this branch out means errno==0
651 ** will be handled by the "default:" case below.
662 /* random NFS retry error, unless during file system support
663 * introspection, in which it actually means what it says */
667 /* EACCES is like EAGAIN during locking operations, but not any other time*/
668 if( (sqliteIOErr
== SQLITE_IOERR_LOCK
) ||
669 (sqliteIOErr
== SQLITE_IOERR_UNLOCK
) ||
670 (sqliteIOErr
== SQLITE_IOERR_RDLOCK
) ||
671 (sqliteIOErr
== SQLITE_IOERR_CHECKRESERVEDLOCK
) ){
674 /* else fall through */
678 /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
679 ** this module never makes such a call. And the code in SQLite itself
680 ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
681 ** this case is also commented out. If the system does set errno to EDEADLK,
682 ** the default SQLITE_IOERR_XXX code will be returned. */
685 return SQLITE_IOERR_BLOCKED
;
688 #if EOPNOTSUPP!=ENOTSUP
690 /* something went terribly awry, unless during file system support
691 * introspection, in which it actually means what it says */
695 /* invalid fd, unless during file system support introspection, in which
696 * it actually means what it says */
705 #ifdef ESTALE /* ESTALE is not defined on Interix systems */
709 /* these should force the client to close the file and reconnect */
718 /******************************************************************************
719 ****************** Begin Unique File ID Utility Used By VxWorks ***************
721 ** On most versions of unix, we can get a unique ID for a file by concatenating
722 ** the device number and the inode number. But this does not work on VxWorks.
723 ** On VxWorks, a unique file id must be based on the canonical filename.
725 ** A pointer to an instance of the following structure can be used as a
726 ** unique file ID in VxWorks. Each instance of this structure contains
727 ** a copy of the canonical filename. There is also a reference count.
728 ** The structure is reclaimed when the number of pointers to it drops to
731 ** There are never very many files open at one time and lookups are not
732 ** a performance-critical path, so it is sufficient to put these
733 ** structures on a linked list.
735 struct vxworksFileId
{
736 struct vxworksFileId
*pNext
; /* Next in a list of them all */
737 int nRef
; /* Number of references to this one */
738 int nName
; /* Length of the zCanonicalName[] string */
739 char *zCanonicalName
; /* Canonical filename */
744 ** All unique filenames are held on a linked list headed by this
747 static struct vxworksFileId
*vxworksFileList
= 0;
750 ** Simplify a filename into its canonical form
751 ** by making the following changes:
753 ** * removing any trailing and duplicate /
754 ** * convert /./ into just /
755 ** * convert /A/../ where A is any simple name into just /
757 ** Changes are made in-place. Return the new name length.
759 ** The original filename is in z[0..n-1]. Return the number of
760 ** characters in the simplified name.
762 static int vxworksSimplifyName(char *z
, int n
){
764 while( n
>1 && z
[n
-1]=='/' ){ n
--; }
765 for(i
=j
=0; i
<n
; i
++){
767 if( z
[i
+1]=='/' ) continue;
768 if( z
[i
+1]=='.' && i
+2<n
&& z
[i
+2]=='/' ){
772 if( z
[i
+1]=='.' && i
+3<n
&& z
[i
+2]=='.' && z
[i
+3]=='/' ){
773 while( j
>0 && z
[j
-1]!='/' ){ j
--; }
786 ** Find a unique file ID for the given absolute pathname. Return
787 ** a pointer to the vxworksFileId object. This pointer is the unique
790 ** The nRef field of the vxworksFileId object is incremented before
791 ** the object is returned. A new vxworksFileId object is created
792 ** and added to the global list if necessary.
794 ** If a memory allocation error occurs, return NULL.
796 static struct vxworksFileId
*vxworksFindFileId(const char *zAbsoluteName
){
797 struct vxworksFileId
*pNew
; /* search key and new file ID */
798 struct vxworksFileId
*pCandidate
; /* For looping over existing file IDs */
799 int n
; /* Length of zAbsoluteName string */
801 assert( zAbsoluteName
[0]=='/' );
802 n
= (int)strlen(zAbsoluteName
);
803 pNew
= sqlite3_malloc( sizeof(*pNew
) + (n
+1) );
804 if( pNew
==0 ) return 0;
805 pNew
->zCanonicalName
= (char*)&pNew
[1];
806 memcpy(pNew
->zCanonicalName
, zAbsoluteName
, n
+1);
807 n
= vxworksSimplifyName(pNew
->zCanonicalName
, n
);
809 /* Search for an existing entry that matching the canonical name.
810 ** If found, increment the reference count and return a pointer to
811 ** the existing file ID.
814 for(pCandidate
=vxworksFileList
; pCandidate
; pCandidate
=pCandidate
->pNext
){
815 if( pCandidate
->nName
==n
816 && memcmp(pCandidate
->zCanonicalName
, pNew
->zCanonicalName
, n
)==0
825 /* No match was found. We will make a new file ID */
828 pNew
->pNext
= vxworksFileList
;
829 vxworksFileList
= pNew
;
835 ** Decrement the reference count on a vxworksFileId object. Free
836 ** the object when the reference count reaches zero.
838 static void vxworksReleaseFileId(struct vxworksFileId
*pId
){
840 assert( pId
->nRef
>0 );
843 struct vxworksFileId
**pp
;
844 for(pp
=&vxworksFileList
; *pp
&& *pp
!=pId
; pp
= &((*pp
)->pNext
)){}
851 #endif /* OS_VXWORKS */
852 /*************** End of Unique File ID Utility Used By VxWorks ****************
853 ******************************************************************************/
856 /******************************************************************************
857 *************************** Posix Advisory Locking ****************************
859 ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
860 ** section 6.5.2.2 lines 483 through 490 specify that when a process
861 ** sets or clears a lock, that operation overrides any prior locks set
862 ** by the same process. It does not explicitly say so, but this implies
863 ** that it overrides locks set by the same process using a different
864 ** file descriptor. Consider this test case:
866 ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
867 ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
869 ** Suppose ./file1 and ./file2 are really the same file (because
870 ** one is a hard or symbolic link to the other) then if you set
871 ** an exclusive lock on fd1, then try to get an exclusive lock
872 ** on fd2, it works. I would have expected the second lock to
873 ** fail since there was already a lock on the file due to fd1.
874 ** But not so. Since both locks came from the same process, the
875 ** second overrides the first, even though they were on different
876 ** file descriptors opened on different file names.
878 ** This means that we cannot use POSIX locks to synchronize file access
879 ** among competing threads of the same process. POSIX locks will work fine
880 ** to synchronize access for threads in separate processes, but not
881 ** threads within the same process.
883 ** To work around the problem, SQLite has to manage file locks internally
884 ** on its own. Whenever a new database is opened, we have to find the
885 ** specific inode of the database file (the inode is determined by the
886 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
887 ** and check for locks already existing on that inode. When locks are
888 ** created or removed, we have to look at our own internal record of the
889 ** locks to see if another thread has previously set a lock on that same
892 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
893 ** For VxWorks, we have to use the alternative unique ID system based on
894 ** canonical filename and implemented in the previous division.)
896 ** The sqlite3_file structure for POSIX is no longer just an integer file
897 ** descriptor. It is now a structure that holds the integer file
898 ** descriptor and a pointer to a structure that describes the internal
899 ** locks on the corresponding inode. There is one locking structure
900 ** per inode, so if the same inode is opened twice, both unixFile structures
901 ** point to the same locking structure. The locking structure keeps
902 ** a reference count (so we will know when to delete it) and a "cnt"
903 ** field that tells us its internal lock status. cnt==0 means the
904 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
905 ** cnt>0 means there are cnt shared locks on the file.
907 ** Any attempt to lock or unlock a file first checks the locking
908 ** structure. The fcntl() system call is only invoked to set a
909 ** POSIX lock if the internal lock structure transitions between
910 ** a locked and an unlocked state.
912 ** But wait: there are yet more problems with POSIX advisory locks.
914 ** If you close a file descriptor that points to a file that has locks,
915 ** all locks on that file that are owned by the current process are
916 ** released. To work around this problem, each unixInodeInfo object
917 ** maintains a count of the number of pending locks on tha inode.
918 ** When an attempt is made to close an unixFile, if there are
919 ** other unixFile open on the same inode that are holding locks, the call
920 ** to close() the file descriptor is deferred until all of the locks clear.
921 ** The unixInodeInfo structure keeps a list of file descriptors that need to
922 ** be closed and that list is walked (and cleared) when the last lock
925 ** Yet another problem: LinuxThreads do not play well with posix locks.
927 ** Many older versions of linux use the LinuxThreads library which is
928 ** not posix compliant. Under LinuxThreads, a lock created by thread
929 ** A cannot be modified or overridden by a different thread B.
930 ** Only thread A can modify the lock. Locking behavior is correct
931 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
932 ** on linux - with NPTL a lock created by thread A can override locks
933 ** in thread B. But there is no way to know at compile-time which
934 ** threading library is being used. So there is no way to know at
935 ** compile-time whether or not thread A can override locks on thread B.
936 ** One has to do a run-time check to discover the behavior of the
939 ** SQLite used to support LinuxThreads. But support for LinuxThreads
940 ** was dropped beginning with version 3.7.0. SQLite will still work with
941 ** LinuxThreads provided that (1) there is no more than one connection
942 ** per database file in the same process and (2) database connections
943 ** do not move across threads.
947 ** An instance of the following structure serves as the key used
948 ** to locate a particular unixInodeInfo object.
951 dev_t dev
; /* Device number */
953 struct vxworksFileId
*pId
; /* Unique file ID for vxworks. */
955 ino_t ino
; /* Inode number */
960 ** An instance of the following structure is allocated for each open
961 ** inode. Or, on LinuxThreads, there is one of these structures for
962 ** each inode opened by each thread.
964 ** A single inode can have multiple file descriptors, so each unixFile
965 ** structure contains a pointer to an instance of this object and this
966 ** object keeps a count of the number of unixFile pointing to it.
968 struct unixInodeInfo
{
969 struct unixFileId fileId
; /* The lookup key */
970 int nShared
; /* Number of SHARED locks held */
971 unsigned char eFileLock
; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
972 unsigned char bProcessLock
; /* An exclusive process lock is held */
973 int nRef
; /* Number of pointers to this structure */
974 unixShmNode
*pShmNode
; /* Shared memory associated with this inode */
975 int nLock
; /* Number of outstanding file locks */
976 UnixUnusedFd
*pUnused
; /* Unused file descriptors to close */
977 unixInodeInfo
*pNext
; /* List of all unixInodeInfo objects */
978 unixInodeInfo
*pPrev
; /* .... doubly linked */
979 #if SQLITE_ENABLE_LOCKING_STYLE
980 unsigned long long sharedByte
; /* for AFP simulated shared lock */
983 sem_t
*pSem
; /* Named POSIX semaphore */
984 char aSemName
[MAX_PATHNAME
+2]; /* Name of that semaphore */
989 ** A lists of all unixInodeInfo objects.
991 static unixInodeInfo
*inodeList
= 0;
995 ** This function - unixLogError_x(), is only ever called via the macro
998 ** It is invoked after an error occurs in an OS function and errno has been
999 ** set. It logs a message using sqlite3_log() containing the current value of
1000 ** errno and, if possible, the human-readable equivalent from strerror() or
1003 ** The first argument passed to the macro should be the error code that
1004 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
1005 ** The two subsequent arguments should be the name of the OS function that
1006 ** failed (e.g. "unlink", "open") and the the associated file-system path,
1009 #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
1010 static int unixLogErrorAtLine(
1011 int errcode
, /* SQLite error code */
1012 const char *zFunc
, /* Name of OS function that failed */
1013 const char *zPath
, /* File path associated with error */
1014 int iLine
/* Source line number where error occurred */
1016 char *zErr
; /* Message from strerror() or equivalent */
1017 int iErrno
= errno
; /* Saved syscall error number */
1019 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
1020 ** the strerror() function to obtain the human-readable error message
1021 ** equivalent to errno. Otherwise, use strerror_r().
1023 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
1025 memset(aErr
, 0, sizeof(aErr
));
1028 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
1029 ** assume that the system provides the the GNU version of strerror_r() that
1030 ** returns a pointer to a buffer containing the error message. That pointer
1031 ** may point to aErr[], or it may point to some static storage somewhere.
1032 ** Otherwise, assume that the system provides the POSIX version of
1033 ** strerror_r(), which always writes an error message into aErr[].
1035 ** If the code incorrectly assumes that it is the POSIX version that is
1036 ** available, the error message will often be an empty string. Not a
1037 ** huge problem. Incorrectly concluding that the GNU version is available
1038 ** could lead to a segfault though.
1040 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
1043 strerror_r(iErrno
, aErr
, sizeof(aErr
)-1);
1045 #elif SQLITE_THREADSAFE
1046 /* This is a threadsafe build, but strerror_r() is not available. */
1049 /* Non-threadsafe build, use strerror(). */
1050 zErr
= strerror(iErrno
);
1053 assert( errcode
!=SQLITE_OK
);
1054 if( zPath
==0 ) zPath
= "";
1055 sqlite3_log(errcode
,
1056 "os_unix.c:%d: (%d) %s(%s) - %s",
1057 iLine
, iErrno
, zFunc
, zPath
, zErr
1064 ** Close a file descriptor.
1066 ** We assume that close() almost always works, since it is only in a
1067 ** very sick application or on a very sick platform that it might fail.
1068 ** If it does fail, simply leak the file descriptor, but do log the
1071 ** Note that it is not safe to retry close() after EINTR since the
1072 ** file descriptor might have already been reused by another thread.
1073 ** So we don't even try to recover from an EINTR. Just log the error
1076 static void robust_close(unixFile
*pFile
, int h
, int lineno
){
1078 unixLogErrorAtLine(SQLITE_IOERR_CLOSE
, "close",
1079 pFile
? pFile
->zPath
: 0, lineno
);
1084 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
1086 static void closePendingFds(unixFile
*pFile
){
1087 unixInodeInfo
*pInode
= pFile
->pInode
;
1089 UnixUnusedFd
*pNext
;
1090 for(p
=pInode
->pUnused
; p
; p
=pNext
){
1092 robust_close(pFile
, p
->fd
, __LINE__
);
1095 pInode
->pUnused
= 0;
1099 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
1101 ** The mutex entered using the unixEnterMutex() function must be held
1102 ** when this function is called.
1104 static void releaseInodeInfo(unixFile
*pFile
){
1105 unixInodeInfo
*pInode
= pFile
->pInode
;
1106 assert( unixMutexHeld() );
1107 if( ALWAYS(pInode
) ){
1109 if( pInode
->nRef
==0 ){
1110 assert( pInode
->pShmNode
==0 );
1111 closePendingFds(pFile
);
1112 if( pInode
->pPrev
){
1113 assert( pInode
->pPrev
->pNext
==pInode
);
1114 pInode
->pPrev
->pNext
= pInode
->pNext
;
1116 assert( inodeList
==pInode
);
1117 inodeList
= pInode
->pNext
;
1119 if( pInode
->pNext
){
1120 assert( pInode
->pNext
->pPrev
==pInode
);
1121 pInode
->pNext
->pPrev
= pInode
->pPrev
;
1123 sqlite3_free(pInode
);
1129 ** Given a file descriptor, locate the unixInodeInfo object that
1130 ** describes that file descriptor. Create a new one if necessary. The
1131 ** return value might be uninitialized if an error occurs.
1133 ** The mutex entered using the unixEnterMutex() function must be held
1134 ** when this function is called.
1136 ** Return an appropriate error code.
1138 static int findInodeInfo(
1139 unixFile
*pFile
, /* Unix file with file desc used in the key */
1140 unixInodeInfo
**ppInode
/* Return the unixInodeInfo object here */
1142 int rc
; /* System call return code */
1143 int fd
; /* The file descriptor for pFile */
1144 struct unixFileId fileId
; /* Lookup key for the unixInodeInfo */
1145 struct stat statbuf
; /* Low-level file information */
1146 unixInodeInfo
*pInode
= 0; /* Candidate unixInodeInfo object */
1148 assert( unixMutexHeld() );
1150 /* Get low-level information about the file that we can used to
1151 ** create a unique name for the file.
1154 rc
= osFstat(fd
, &statbuf
);
1156 pFile
->lastErrno
= errno
;
1158 if( pFile
->lastErrno
==EOVERFLOW
) return SQLITE_NOLFS
;
1160 return SQLITE_IOERR
;
1164 /* On OS X on an msdos filesystem, the inode number is reported
1165 ** incorrectly for zero-size files. See ticket #3260. To work
1166 ** around this problem (we consider it a bug in OS X, not SQLite)
1167 ** we always increase the file size to 1 by writing a single byte
1168 ** prior to accessing the inode number. The one byte written is
1169 ** an ASCII 'S' character which also happens to be the first byte
1170 ** in the header of every SQLite database. In this way, if there
1171 ** is a race condition such that another thread has already populated
1172 ** the first page of the database, no damage is done.
1174 if( statbuf
.st_size
==0 && (pFile
->fsFlags
& SQLITE_FSFLAGS_IS_MSDOS
)!=0 ){
1175 do{ rc
= osWrite(fd
, "S", 1); }while( rc
<0 && errno
==EINTR
);
1177 pFile
->lastErrno
= errno
;
1178 return SQLITE_IOERR
;
1180 rc
= osFstat(fd
, &statbuf
);
1182 pFile
->lastErrno
= errno
;
1183 return SQLITE_IOERR
;
1188 memset(&fileId
, 0, sizeof(fileId
));
1189 fileId
.dev
= statbuf
.st_dev
;
1191 fileId
.pId
= pFile
->pId
;
1193 fileId
.ino
= statbuf
.st_ino
;
1196 while( pInode
&& memcmp(&fileId
, &pInode
->fileId
, sizeof(fileId
)) ){
1197 pInode
= pInode
->pNext
;
1200 pInode
= sqlite3_malloc( sizeof(*pInode
) );
1202 return SQLITE_NOMEM
;
1204 memset(pInode
, 0, sizeof(*pInode
));
1205 memcpy(&pInode
->fileId
, &fileId
, sizeof(fileId
));
1207 pInode
->pNext
= inodeList
;
1209 if( inodeList
) inodeList
->pPrev
= pInode
;
1220 ** This routine checks if there is a RESERVED lock held on the specified
1221 ** file by this or any other process. If such a lock is held, set *pResOut
1222 ** to a non-zero value otherwise *pResOut is set to zero. The return value
1223 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1225 static int unixCheckReservedLock(sqlite3_file
*id
, int *pResOut
){
1228 unixFile
*pFile
= (unixFile
*)id
;
1230 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK
; );
1233 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
1235 /* Check if a thread in this process holds such a lock */
1236 if( pFile
->pInode
->eFileLock
>SHARED_LOCK
){
1240 /* Otherwise see if some other process holds it.
1243 if( !reserved
&& !pFile
->pInode
->bProcessLock
){
1245 lock
.l_whence
= SEEK_SET
;
1246 lock
.l_start
= RESERVED_BYTE
;
1248 lock
.l_type
= F_WRLCK
;
1249 if( osFcntl(pFile
->h
, F_GETLK
, &lock
) ){
1250 rc
= SQLITE_IOERR_CHECKRESERVEDLOCK
;
1251 pFile
->lastErrno
= errno
;
1252 } else if( lock
.l_type
!=F_UNLCK
){
1259 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile
->h
, rc
, reserved
));
1261 *pResOut
= reserved
;
1266 ** Attempt to set a system-lock on the file pFile. The lock is
1267 ** described by pLock.
1269 ** If the pFile was opened read/write from unix-excl, then the only lock
1270 ** ever obtained is an exclusive lock, and it is obtained exactly once
1271 ** the first time any lock is attempted. All subsequent system locking
1272 ** operations become no-ops. Locking operations still happen internally,
1273 ** in order to coordinate access between separate database connections
1274 ** within this process, but all of that is handled in memory and the
1275 ** operating system does not participate.
1277 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
1278 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
1279 ** and is read-only.
1281 ** Zero is returned if the call completes successfully, or -1 if a call
1282 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
1284 static int unixFileLock(unixFile
*pFile
, struct flock
*pLock
){
1286 unixInodeInfo
*pInode
= pFile
->pInode
;
1287 assert( unixMutexHeld() );
1288 assert( pInode
!=0 );
1289 if( ((pFile
->ctrlFlags
& UNIXFILE_EXCL
)!=0 || pInode
->bProcessLock
)
1290 && ((pFile
->ctrlFlags
& UNIXFILE_RDONLY
)==0)
1292 if( pInode
->bProcessLock
==0 ){
1294 assert( pInode
->nLock
==0 );
1295 lock
.l_whence
= SEEK_SET
;
1296 lock
.l_start
= SHARED_FIRST
;
1297 lock
.l_len
= SHARED_SIZE
;
1298 lock
.l_type
= F_WRLCK
;
1299 rc
= osFcntl(pFile
->h
, F_SETLK
, &lock
);
1300 if( rc
<0 ) return rc
;
1301 pInode
->bProcessLock
= 1;
1307 rc
= osFcntl(pFile
->h
, F_SETLK
, pLock
);
1313 ** Lock the file with the lock specified by parameter eFileLock - one
1314 ** of the following:
1317 ** (2) RESERVED_LOCK
1319 ** (4) EXCLUSIVE_LOCK
1321 ** Sometimes when requesting one lock state, additional lock states
1322 ** are inserted in between. The locking might fail on one of the later
1323 ** transitions leaving the lock state different from what it started but
1324 ** still short of its goal. The following chart shows the allowed
1325 ** transitions and the inserted intermediate states:
1327 ** UNLOCKED -> SHARED
1328 ** SHARED -> RESERVED
1329 ** SHARED -> (PENDING) -> EXCLUSIVE
1330 ** RESERVED -> (PENDING) -> EXCLUSIVE
1331 ** PENDING -> EXCLUSIVE
1333 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
1334 ** routine to lower a locking level.
1336 static int unixLock(sqlite3_file
*id
, int eFileLock
){
1337 /* The following describes the implementation of the various locks and
1338 ** lock transitions in terms of the POSIX advisory shared and exclusive
1339 ** lock primitives (called read-locks and write-locks below, to avoid
1340 ** confusion with SQLite lock names). The algorithms are complicated
1341 ** slightly in order to be compatible with windows systems simultaneously
1342 ** accessing the same database file, in case that is ever required.
1344 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1345 ** byte', each single bytes at well known offsets, and the 'shared byte
1346 ** range', a range of 510 bytes at a well known offset.
1348 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1349 ** byte'. If this is successful, a random byte from the 'shared byte
1350 ** range' is read-locked and the lock on the 'pending byte' released.
1352 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1353 ** A RESERVED lock is implemented by grabbing a write-lock on the
1356 ** A process may only obtain a PENDING lock after it has obtained a
1357 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1358 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1359 ** obtained, but existing SHARED locks are allowed to persist. A process
1360 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1361 ** This property is used by the algorithm for rolling back a journal file
1364 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1365 ** implemented by obtaining a write-lock on the entire 'shared byte
1366 ** range'. Since all other locks require a read-lock on one of the bytes
1367 ** within this range, this ensures that no other locks are held on the
1370 ** The reason a single byte cannot be used instead of the 'shared byte
1371 ** range' is that some versions of windows do not support read-locks. By
1372 ** locking a random byte from a range, concurrent SHARED locks may exist
1373 ** even if the locking primitive used is always a write-lock.
1376 unixFile
*pFile
= (unixFile
*)id
;
1377 unixInodeInfo
*pInode
;
1382 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile
->h
,
1383 azFileLock(eFileLock
), azFileLock(pFile
->eFileLock
),
1384 azFileLock(pFile
->pInode
->eFileLock
), pFile
->pInode
->nShared
, getpid()));
1386 /* If there is already a lock of this type or more restrictive on the
1387 ** unixFile, do nothing. Don't use the end_lock: exit path, as
1388 ** unixEnterMutex() hasn't been called yet.
1390 if( pFile
->eFileLock
>=eFileLock
){
1391 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile
->h
,
1392 azFileLock(eFileLock
)));
1396 /* Make sure the locking sequence is correct.
1397 ** (1) We never move from unlocked to anything higher than shared lock.
1398 ** (2) SQLite never explicitly requests a pendig lock.
1399 ** (3) A shared lock is always held when a reserve lock is requested.
1401 assert( pFile
->eFileLock
!=NO_LOCK
|| eFileLock
==SHARED_LOCK
);
1402 assert( eFileLock
!=PENDING_LOCK
);
1403 assert( eFileLock
!=RESERVED_LOCK
|| pFile
->eFileLock
==SHARED_LOCK
);
1405 /* This mutex is needed because pFile->pInode is shared across threads
1408 pInode
= pFile
->pInode
;
1410 /* If some thread using this PID has a lock via a different unixFile*
1411 ** handle that precludes the requested lock, return BUSY.
1413 if( (pFile
->eFileLock
!=pInode
->eFileLock
&&
1414 (pInode
->eFileLock
>=PENDING_LOCK
|| eFileLock
>SHARED_LOCK
))
1420 /* If a SHARED lock is requested, and some thread using this PID already
1421 ** has a SHARED or RESERVED lock, then increment reference counts and
1422 ** return SQLITE_OK.
1424 if( eFileLock
==SHARED_LOCK
&&
1425 (pInode
->eFileLock
==SHARED_LOCK
|| pInode
->eFileLock
==RESERVED_LOCK
) ){
1426 assert( eFileLock
==SHARED_LOCK
);
1427 assert( pFile
->eFileLock
==0 );
1428 assert( pInode
->nShared
>0 );
1429 pFile
->eFileLock
= SHARED_LOCK
;
1436 /* A PENDING lock is needed before acquiring a SHARED lock and before
1437 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1441 lock
.l_whence
= SEEK_SET
;
1442 if( eFileLock
==SHARED_LOCK
1443 || (eFileLock
==EXCLUSIVE_LOCK
&& pFile
->eFileLock
<PENDING_LOCK
)
1445 lock
.l_type
= (eFileLock
==SHARED_LOCK
?F_RDLCK
:F_WRLCK
);
1446 lock
.l_start
= PENDING_BYTE
;
1447 if( unixFileLock(pFile
, &lock
) ){
1449 rc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_LOCK
);
1450 if( rc
!=SQLITE_BUSY
){
1451 pFile
->lastErrno
= tErrno
;
1458 /* If control gets to this point, then actually go ahead and make
1459 ** operating system calls for the specified lock.
1461 if( eFileLock
==SHARED_LOCK
){
1462 assert( pInode
->nShared
==0 );
1463 assert( pInode
->eFileLock
==0 );
1464 assert( rc
==SQLITE_OK
);
1466 /* Now get the read-lock */
1467 lock
.l_start
= SHARED_FIRST
;
1468 lock
.l_len
= SHARED_SIZE
;
1469 if( unixFileLock(pFile
, &lock
) ){
1471 rc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_LOCK
);
1474 /* Drop the temporary PENDING lock */
1475 lock
.l_start
= PENDING_BYTE
;
1477 lock
.l_type
= F_UNLCK
;
1478 if( unixFileLock(pFile
, &lock
) && rc
==SQLITE_OK
){
1479 /* This could happen with a network mount */
1481 rc
= SQLITE_IOERR_UNLOCK
;
1485 if( rc
!=SQLITE_BUSY
){
1486 pFile
->lastErrno
= tErrno
;
1490 pFile
->eFileLock
= SHARED_LOCK
;
1492 pInode
->nShared
= 1;
1494 }else if( eFileLock
==EXCLUSIVE_LOCK
&& pInode
->nShared
>1 ){
1495 /* We are trying for an exclusive lock but another thread in this
1496 ** same process is still holding a shared lock. */
1499 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1500 ** assumed that there is a SHARED or greater lock on the file
1503 assert( 0!=pFile
->eFileLock
);
1504 lock
.l_type
= F_WRLCK
;
1506 assert( eFileLock
==RESERVED_LOCK
|| eFileLock
==EXCLUSIVE_LOCK
);
1507 if( eFileLock
==RESERVED_LOCK
){
1508 lock
.l_start
= RESERVED_BYTE
;
1511 lock
.l_start
= SHARED_FIRST
;
1512 lock
.l_len
= SHARED_SIZE
;
1515 if( unixFileLock(pFile
, &lock
) ){
1517 rc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_LOCK
);
1518 if( rc
!=SQLITE_BUSY
){
1519 pFile
->lastErrno
= tErrno
;
1526 /* Set up the transaction-counter change checking flags when
1527 ** transitioning from a SHARED to a RESERVED lock. The change
1528 ** from SHARED to RESERVED marks the beginning of a normal
1529 ** write operation (not a hot journal rollback).
1532 && pFile
->eFileLock
<=SHARED_LOCK
1533 && eFileLock
==RESERVED_LOCK
1535 pFile
->transCntrChng
= 0;
1536 pFile
->dbUpdate
= 0;
1537 pFile
->inNormalWrite
= 1;
1542 if( rc
==SQLITE_OK
){
1543 pFile
->eFileLock
= eFileLock
;
1544 pInode
->eFileLock
= eFileLock
;
1545 }else if( eFileLock
==EXCLUSIVE_LOCK
){
1546 pFile
->eFileLock
= PENDING_LOCK
;
1547 pInode
->eFileLock
= PENDING_LOCK
;
1552 OSTRACE(("LOCK %d %s %s (unix)\n", pFile
->h
, azFileLock(eFileLock
),
1553 rc
==SQLITE_OK
? "ok" : "failed"));
1558 ** Add the file descriptor used by file handle pFile to the corresponding
1561 static void setPendingFd(unixFile
*pFile
){
1562 unixInodeInfo
*pInode
= pFile
->pInode
;
1563 UnixUnusedFd
*p
= pFile
->pUnused
;
1564 p
->pNext
= pInode
->pUnused
;
1565 pInode
->pUnused
= p
;
1571 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
1572 ** must be either NO_LOCK or SHARED_LOCK.
1574 ** If the locking level of the file descriptor is already at or below
1575 ** the requested locking level, this routine is a no-op.
1577 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
1578 ** the byte range is divided into 2 parts and the first part is unlocked then
1579 ** set to a read lock, then the other part is simply unlocked. This works
1580 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
1581 ** remove the write lock on a region when a read lock is set.
1583 static int posixUnlock(sqlite3_file
*id
, int eFileLock
, int handleNFSUnlock
){
1584 unixFile
*pFile
= (unixFile
*)id
;
1585 unixInodeInfo
*pInode
;
1590 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile
->h
, eFileLock
,
1591 pFile
->eFileLock
, pFile
->pInode
->eFileLock
, pFile
->pInode
->nShared
,
1594 assert( eFileLock
<=SHARED_LOCK
);
1595 if( pFile
->eFileLock
<=eFileLock
){
1599 pInode
= pFile
->pInode
;
1600 assert( pInode
->nShared
!=0 );
1601 if( pFile
->eFileLock
>SHARED_LOCK
){
1602 assert( pInode
->eFileLock
==pFile
->eFileLock
);
1605 /* When reducing a lock such that other processes can start
1606 ** reading the database file again, make sure that the
1607 ** transaction counter was updated if any part of the database
1608 ** file changed. If the transaction counter is not updated,
1609 ** other connections to the same file might not realize that
1610 ** the file has changed and hence might not know to flush their
1611 ** cache. The use of a stale cache can lead to database corruption.
1613 pFile
->inNormalWrite
= 0;
1616 /* downgrading to a shared lock on NFS involves clearing the write lock
1617 ** before establishing the readlock - to avoid a race condition we downgrade
1618 ** the lock in 2 blocks, so that part of the range will be covered by a
1619 ** write lock until the rest is covered by a read lock:
1625 if( eFileLock
==SHARED_LOCK
){
1627 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
1628 (void)handleNFSUnlock
;
1629 assert( handleNFSUnlock
==0 );
1631 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
1632 if( handleNFSUnlock
){
1633 int tErrno
; /* Error code from system call errors */
1634 off_t divSize
= SHARED_SIZE
- 1;
1636 lock
.l_type
= F_UNLCK
;
1637 lock
.l_whence
= SEEK_SET
;
1638 lock
.l_start
= SHARED_FIRST
;
1639 lock
.l_len
= divSize
;
1640 if( unixFileLock(pFile
, &lock
)==(-1) ){
1642 rc
= SQLITE_IOERR_UNLOCK
;
1643 if( IS_LOCK_ERROR(rc
) ){
1644 pFile
->lastErrno
= tErrno
;
1648 lock
.l_type
= F_RDLCK
;
1649 lock
.l_whence
= SEEK_SET
;
1650 lock
.l_start
= SHARED_FIRST
;
1651 lock
.l_len
= divSize
;
1652 if( unixFileLock(pFile
, &lock
)==(-1) ){
1654 rc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_RDLOCK
);
1655 if( IS_LOCK_ERROR(rc
) ){
1656 pFile
->lastErrno
= tErrno
;
1660 lock
.l_type
= F_UNLCK
;
1661 lock
.l_whence
= SEEK_SET
;
1662 lock
.l_start
= SHARED_FIRST
+divSize
;
1663 lock
.l_len
= SHARED_SIZE
-divSize
;
1664 if( unixFileLock(pFile
, &lock
)==(-1) ){
1666 rc
= SQLITE_IOERR_UNLOCK
;
1667 if( IS_LOCK_ERROR(rc
) ){
1668 pFile
->lastErrno
= tErrno
;
1673 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
1675 lock
.l_type
= F_RDLCK
;
1676 lock
.l_whence
= SEEK_SET
;
1677 lock
.l_start
= SHARED_FIRST
;
1678 lock
.l_len
= SHARED_SIZE
;
1679 if( unixFileLock(pFile
, &lock
) ){
1680 /* In theory, the call to unixFileLock() cannot fail because another
1681 ** process is holding an incompatible lock. If it does, this
1682 ** indicates that the other process is not following the locking
1683 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
1684 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
1685 ** an assert to fail). */
1686 rc
= SQLITE_IOERR_RDLOCK
;
1687 pFile
->lastErrno
= errno
;
1692 lock
.l_type
= F_UNLCK
;
1693 lock
.l_whence
= SEEK_SET
;
1694 lock
.l_start
= PENDING_BYTE
;
1695 lock
.l_len
= 2L; assert( PENDING_BYTE
+1==RESERVED_BYTE
);
1696 if( unixFileLock(pFile
, &lock
)==0 ){
1697 pInode
->eFileLock
= SHARED_LOCK
;
1699 rc
= SQLITE_IOERR_UNLOCK
;
1700 pFile
->lastErrno
= errno
;
1704 if( eFileLock
==NO_LOCK
){
1705 /* Decrement the shared lock counter. Release the lock using an
1706 ** OS call only when all threads in this same process have released
1710 if( pInode
->nShared
==0 ){
1711 lock
.l_type
= F_UNLCK
;
1712 lock
.l_whence
= SEEK_SET
;
1713 lock
.l_start
= lock
.l_len
= 0L;
1714 if( unixFileLock(pFile
, &lock
)==0 ){
1715 pInode
->eFileLock
= NO_LOCK
;
1717 rc
= SQLITE_IOERR_UNLOCK
;
1718 pFile
->lastErrno
= errno
;
1719 pInode
->eFileLock
= NO_LOCK
;
1720 pFile
->eFileLock
= NO_LOCK
;
1724 /* Decrement the count of locks against this same file. When the
1725 ** count reaches zero, close any other file descriptors whose close
1726 ** was deferred because of outstanding locks.
1729 assert( pInode
->nLock
>=0 );
1730 if( pInode
->nLock
==0 ){
1731 closePendingFds(pFile
);
1737 if( rc
==SQLITE_OK
) pFile
->eFileLock
= eFileLock
;
1742 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
1743 ** must be either NO_LOCK or SHARED_LOCK.
1745 ** If the locking level of the file descriptor is already at or below
1746 ** the requested locking level, this routine is a no-op.
1748 static int unixUnlock(sqlite3_file
*id
, int eFileLock
){
1749 return posixUnlock(id
, eFileLock
, 0);
1753 ** This function performs the parts of the "close file" operation
1754 ** common to all locking schemes. It closes the directory and file
1755 ** handles, if they are valid, and sets all fields of the unixFile
1758 ** It is *not* necessary to hold the mutex when this routine is called,
1759 ** even on VxWorks. A mutex will be acquired on VxWorks by the
1760 ** vxworksReleaseFileId() routine.
1762 static int closeUnixFile(sqlite3_file
*id
){
1763 unixFile
*pFile
= (unixFile
*)id
;
1765 robust_close(pFile
, pFile
->h
, __LINE__
);
1770 if( pFile
->ctrlFlags
& UNIXFILE_DELETE
){
1771 osUnlink(pFile
->pId
->zCanonicalName
);
1773 vxworksReleaseFileId(pFile
->pId
);
1777 OSTRACE(("CLOSE %-3d\n", pFile
->h
));
1779 sqlite3_free(pFile
->pUnused
);
1780 memset(pFile
, 0, sizeof(unixFile
));
1787 static int unixClose(sqlite3_file
*id
){
1789 unixFile
*pFile
= (unixFile
*)id
;
1790 unixUnlock(id
, NO_LOCK
);
1793 /* unixFile.pInode is always valid here. Otherwise, a different close
1794 ** routine (e.g. nolockClose()) would be called instead.
1796 assert( pFile
->pInode
->nLock
>0 || pFile
->pInode
->bProcessLock
==0 );
1797 if( ALWAYS(pFile
->pInode
) && pFile
->pInode
->nLock
){
1798 /* If there are outstanding locks, do not actually close the file just
1799 ** yet because that would clear those locks. Instead, add the file
1800 ** descriptor to pInode->pUnused list. It will be automatically closed
1801 ** when the last lock is cleared.
1803 setPendingFd(pFile
);
1805 releaseInodeInfo(pFile
);
1806 rc
= closeUnixFile(id
);
1811 /************** End of the posix advisory lock implementation *****************
1812 ******************************************************************************/
1814 /******************************************************************************
1815 ****************************** No-op Locking **********************************
1817 ** Of the various locking implementations available, this is by far the
1818 ** simplest: locking is ignored. No attempt is made to lock the database
1819 ** file for reading or writing.
1821 ** This locking mode is appropriate for use on read-only databases
1822 ** (ex: databases that are burned into CD-ROM, for example.) It can
1823 ** also be used if the application employs some external mechanism to
1824 ** prevent simultaneous access of the same database by two or more
1825 ** database connections. But there is a serious risk of database
1826 ** corruption if this locking mode is used in situations where multiple
1827 ** database connections are accessing the same database file at the same
1828 ** time and one or more of those connections are writing.
1831 static int nolockCheckReservedLock(sqlite3_file
*NotUsed
, int *pResOut
){
1832 UNUSED_PARAMETER(NotUsed
);
1836 static int nolockLock(sqlite3_file
*NotUsed
, int NotUsed2
){
1837 UNUSED_PARAMETER2(NotUsed
, NotUsed2
);
1840 static int nolockUnlock(sqlite3_file
*NotUsed
, int NotUsed2
){
1841 UNUSED_PARAMETER2(NotUsed
, NotUsed2
);
1848 static int nolockClose(sqlite3_file
*id
) {
1849 return closeUnixFile(id
);
1852 /******************* End of the no-op lock implementation *********************
1853 ******************************************************************************/
1855 /******************************************************************************
1856 ************************* Begin dot-file Locking ******************************
1858 ** The dotfile locking implementation uses the existance of separate lock
1859 ** files (really a directory) to control access to the database. This works
1860 ** on just about every filesystem imaginable. But there are serious downsides:
1862 ** (1) There is zero concurrency. A single reader blocks all other
1863 ** connections from reading or writing the database.
1865 ** (2) An application crash or power loss can leave stale lock files
1866 ** sitting around that need to be cleared manually.
1868 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
1869 ** other locking strategy is available.
1871 ** Dotfile locking works by creating a subdirectory in the same directory as
1872 ** the database and with the same name but with a ".lock" extension added.
1873 ** The existance of a lock directory implies an EXCLUSIVE lock. All other
1874 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
1878 ** The file suffix added to the data base filename in order to create the
1881 #define DOTLOCK_SUFFIX ".lock"
1884 ** This routine checks if there is a RESERVED lock held on the specified
1885 ** file by this or any other process. If such a lock is held, set *pResOut
1886 ** to a non-zero value otherwise *pResOut is set to zero. The return value
1887 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1889 ** In dotfile locking, either a lock exists or it does not. So in this
1890 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
1891 ** is held on the file and false if the file is unlocked.
1893 static int dotlockCheckReservedLock(sqlite3_file
*id
, int *pResOut
) {
1896 unixFile
*pFile
= (unixFile
*)id
;
1898 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK
; );
1902 /* Check if a thread in this process holds such a lock */
1903 if( pFile
->eFileLock
>SHARED_LOCK
){
1904 /* Either this connection or some other connection in the same process
1905 ** holds a lock on the file. No need to check further. */
1908 /* The lock is held if and only if the lockfile exists */
1909 const char *zLockFile
= (const char*)pFile
->lockingContext
;
1910 reserved
= osAccess(zLockFile
, 0)==0;
1912 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile
->h
, rc
, reserved
));
1913 *pResOut
= reserved
;
1918 ** Lock the file with the lock specified by parameter eFileLock - one
1919 ** of the following:
1922 ** (2) RESERVED_LOCK
1924 ** (4) EXCLUSIVE_LOCK
1926 ** Sometimes when requesting one lock state, additional lock states
1927 ** are inserted in between. The locking might fail on one of the later
1928 ** transitions leaving the lock state different from what it started but
1929 ** still short of its goal. The following chart shows the allowed
1930 ** transitions and the inserted intermediate states:
1932 ** UNLOCKED -> SHARED
1933 ** SHARED -> RESERVED
1934 ** SHARED -> (PENDING) -> EXCLUSIVE
1935 ** RESERVED -> (PENDING) -> EXCLUSIVE
1936 ** PENDING -> EXCLUSIVE
1938 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
1939 ** routine to lower a locking level.
1941 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
1942 ** But we track the other locking levels internally.
1944 static int dotlockLock(sqlite3_file
*id
, int eFileLock
) {
1945 unixFile
*pFile
= (unixFile
*)id
;
1946 char *zLockFile
= (char *)pFile
->lockingContext
;
1950 /* If we have any lock, then the lock file already exists. All we have
1951 ** to do is adjust our internal record of the lock level.
1953 if( pFile
->eFileLock
> NO_LOCK
){
1954 pFile
->eFileLock
= eFileLock
;
1955 /* Always update the timestamp on the old file */
1957 utime(zLockFile
, NULL
);
1959 utimes(zLockFile
, NULL
);
1964 /* grab an exclusive lock */
1965 rc
= osMkdir(zLockFile
, 0777);
1967 /* failed to open/create the lock directory */
1969 if( EEXIST
== tErrno
){
1972 rc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_LOCK
);
1973 if( IS_LOCK_ERROR(rc
) ){
1974 pFile
->lastErrno
= tErrno
;
1980 /* got it, set the type and return ok */
1981 pFile
->eFileLock
= eFileLock
;
1986 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
1987 ** must be either NO_LOCK or SHARED_LOCK.
1989 ** If the locking level of the file descriptor is already at or below
1990 ** the requested locking level, this routine is a no-op.
1992 ** When the locking level reaches NO_LOCK, delete the lock file.
1994 static int dotlockUnlock(sqlite3_file
*id
, int eFileLock
) {
1995 unixFile
*pFile
= (unixFile
*)id
;
1996 char *zLockFile
= (char *)pFile
->lockingContext
;
2000 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile
->h
, eFileLock
,
2001 pFile
->eFileLock
, getpid()));
2002 assert( eFileLock
<=SHARED_LOCK
);
2004 /* no-op if possible */
2005 if( pFile
->eFileLock
==eFileLock
){
2009 /* To downgrade to shared, simply update our internal notion of the
2010 ** lock state. No need to mess with the file on disk.
2012 if( eFileLock
==SHARED_LOCK
){
2013 pFile
->eFileLock
= SHARED_LOCK
;
2017 /* To fully unlock the database, delete the lock file */
2018 assert( eFileLock
==NO_LOCK
);
2019 rc
= osRmdir(zLockFile
);
2020 if( rc
<0 && errno
==ENOTDIR
) rc
= osUnlink(zLockFile
);
2024 if( ENOENT
!= tErrno
){
2025 rc
= SQLITE_IOERR_UNLOCK
;
2027 if( IS_LOCK_ERROR(rc
) ){
2028 pFile
->lastErrno
= tErrno
;
2032 pFile
->eFileLock
= NO_LOCK
;
2037 ** Close a file. Make sure the lock has been released before closing.
2039 static int dotlockClose(sqlite3_file
*id
) {
2042 unixFile
*pFile
= (unixFile
*)id
;
2043 dotlockUnlock(id
, NO_LOCK
);
2044 sqlite3_free(pFile
->lockingContext
);
2046 rc
= closeUnixFile(id
);
2049 /****************** End of the dot-file lock implementation *******************
2050 ******************************************************************************/
2052 /******************************************************************************
2053 ************************** Begin flock Locking ********************************
2055 ** Use the flock() system call to do file locking.
2057 ** flock() locking is like dot-file locking in that the various
2058 ** fine-grain locking levels supported by SQLite are collapsed into
2059 ** a single exclusive lock. In other words, SHARED, RESERVED, and
2060 ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
2061 ** still works when you do this, but concurrency is reduced since
2062 ** only a single process can be reading the database at a time.
2064 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
2065 ** compiling for VXWORKS.
2067 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
2070 ** Retry flock() calls that fail with EINTR
2073 static int robust_flock(int fd
, int op
){
2075 do{ rc
= flock(fd
,op
); }while( rc
<0 && errno
==EINTR
);
2079 # define robust_flock(a,b) flock(a,b)
2084 ** This routine checks if there is a RESERVED lock held on the specified
2085 ** file by this or any other process. If such a lock is held, set *pResOut
2086 ** to a non-zero value otherwise *pResOut is set to zero. The return value
2087 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2089 static int flockCheckReservedLock(sqlite3_file
*id
, int *pResOut
){
2092 unixFile
*pFile
= (unixFile
*)id
;
2094 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK
; );
2098 /* Check if a thread in this process holds such a lock */
2099 if( pFile
->eFileLock
>SHARED_LOCK
){
2103 /* Otherwise see if some other process holds it. */
2105 /* attempt to get the lock */
2106 int lrc
= robust_flock(pFile
->h
, LOCK_EX
| LOCK_NB
);
2108 /* got the lock, unlock it */
2109 lrc
= robust_flock(pFile
->h
, LOCK_UN
);
2112 /* unlock failed with an error */
2113 lrc
= SQLITE_IOERR_UNLOCK
;
2114 if( IS_LOCK_ERROR(lrc
) ){
2115 pFile
->lastErrno
= tErrno
;
2122 /* someone else might have it reserved */
2123 lrc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_LOCK
);
2124 if( IS_LOCK_ERROR(lrc
) ){
2125 pFile
->lastErrno
= tErrno
;
2130 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile
->h
, rc
, reserved
));
2132 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2133 if( (rc
& SQLITE_IOERR
) == SQLITE_IOERR
){
2137 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2138 *pResOut
= reserved
;
2143 ** Lock the file with the lock specified by parameter eFileLock - one
2144 ** of the following:
2147 ** (2) RESERVED_LOCK
2149 ** (4) EXCLUSIVE_LOCK
2151 ** Sometimes when requesting one lock state, additional lock states
2152 ** are inserted in between. The locking might fail on one of the later
2153 ** transitions leaving the lock state different from what it started but
2154 ** still short of its goal. The following chart shows the allowed
2155 ** transitions and the inserted intermediate states:
2157 ** UNLOCKED -> SHARED
2158 ** SHARED -> RESERVED
2159 ** SHARED -> (PENDING) -> EXCLUSIVE
2160 ** RESERVED -> (PENDING) -> EXCLUSIVE
2161 ** PENDING -> EXCLUSIVE
2163 ** flock() only really support EXCLUSIVE locks. We track intermediate
2164 ** lock states in the sqlite3_file structure, but all locks SHARED or
2165 ** above are really EXCLUSIVE locks and exclude all other processes from
2168 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
2169 ** routine to lower a locking level.
2171 static int flockLock(sqlite3_file
*id
, int eFileLock
) {
2173 unixFile
*pFile
= (unixFile
*)id
;
2177 /* if we already have a lock, it is exclusive.
2178 ** Just adjust level and punt on outta here. */
2179 if (pFile
->eFileLock
> NO_LOCK
) {
2180 pFile
->eFileLock
= eFileLock
;
2184 /* grab an exclusive lock */
2186 if (robust_flock(pFile
->h
, LOCK_EX
| LOCK_NB
)) {
2188 /* didn't get, must be busy */
2189 rc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_LOCK
);
2190 if( IS_LOCK_ERROR(rc
) ){
2191 pFile
->lastErrno
= tErrno
;
2194 /* got it, set the type and return ok */
2195 pFile
->eFileLock
= eFileLock
;
2197 OSTRACE(("LOCK %d %s %s (flock)\n", pFile
->h
, azFileLock(eFileLock
),
2198 rc
==SQLITE_OK
? "ok" : "failed"));
2199 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2200 if( (rc
& SQLITE_IOERR
) == SQLITE_IOERR
){
2203 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2209 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2210 ** must be either NO_LOCK or SHARED_LOCK.
2212 ** If the locking level of the file descriptor is already at or below
2213 ** the requested locking level, this routine is a no-op.
2215 static int flockUnlock(sqlite3_file
*id
, int eFileLock
) {
2216 unixFile
*pFile
= (unixFile
*)id
;
2219 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile
->h
, eFileLock
,
2220 pFile
->eFileLock
, getpid()));
2221 assert( eFileLock
<=SHARED_LOCK
);
2223 /* no-op if possible */
2224 if( pFile
->eFileLock
==eFileLock
){
2228 /* shared can just be set because we always have an exclusive */
2229 if (eFileLock
==SHARED_LOCK
) {
2230 pFile
->eFileLock
= eFileLock
;
2234 /* no, really, unlock. */
2235 if( robust_flock(pFile
->h
, LOCK_UN
) ){
2236 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2238 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2239 return SQLITE_IOERR_UNLOCK
;
2241 pFile
->eFileLock
= NO_LOCK
;
2249 static int flockClose(sqlite3_file
*id
) {
2251 flockUnlock(id
, NO_LOCK
);
2253 return closeUnixFile(id
);
2256 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
2258 /******************* End of the flock lock implementation *********************
2259 ******************************************************************************/
2261 /******************************************************************************
2262 ************************ Begin Named Semaphore Locking ************************
2264 ** Named semaphore locking is only supported on VxWorks.
2266 ** Semaphore locking is like dot-lock and flock in that it really only
2267 ** supports EXCLUSIVE locking. Only a single process can read or write
2268 ** the database file at a time. This reduces potential concurrency, but
2269 ** makes the lock implementation much easier.
2274 ** This routine checks if there is a RESERVED lock held on the specified
2275 ** file by this or any other process. If such a lock is held, set *pResOut
2276 ** to a non-zero value otherwise *pResOut is set to zero. The return value
2277 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2279 static int semCheckReservedLock(sqlite3_file
*id
, int *pResOut
) {
2282 unixFile
*pFile
= (unixFile
*)id
;
2284 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK
; );
2288 /* Check if a thread in this process holds such a lock */
2289 if( pFile
->eFileLock
>SHARED_LOCK
){
2293 /* Otherwise see if some other process holds it. */
2295 sem_t
*pSem
= pFile
->pInode
->pSem
;
2296 struct stat statBuf
;
2298 if( sem_trywait(pSem
)==-1 ){
2300 if( EAGAIN
!= tErrno
){
2301 rc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_CHECKRESERVEDLOCK
);
2302 pFile
->lastErrno
= tErrno
;
2304 /* someone else has the lock when we are in NO_LOCK */
2305 reserved
= (pFile
->eFileLock
< SHARED_LOCK
);
2308 /* we could have it if we want it */
2312 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile
->h
, rc
, reserved
));
2314 *pResOut
= reserved
;
2319 ** Lock the file with the lock specified by parameter eFileLock - one
2320 ** of the following:
2323 ** (2) RESERVED_LOCK
2325 ** (4) EXCLUSIVE_LOCK
2327 ** Sometimes when requesting one lock state, additional lock states
2328 ** are inserted in between. The locking might fail on one of the later
2329 ** transitions leaving the lock state different from what it started but
2330 ** still short of its goal. The following chart shows the allowed
2331 ** transitions and the inserted intermediate states:
2333 ** UNLOCKED -> SHARED
2334 ** SHARED -> RESERVED
2335 ** SHARED -> (PENDING) -> EXCLUSIVE
2336 ** RESERVED -> (PENDING) -> EXCLUSIVE
2337 ** PENDING -> EXCLUSIVE
2339 ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
2340 ** lock states in the sqlite3_file structure, but all locks SHARED or
2341 ** above are really EXCLUSIVE locks and exclude all other processes from
2344 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
2345 ** routine to lower a locking level.
2347 static int semLock(sqlite3_file
*id
, int eFileLock
) {
2348 unixFile
*pFile
= (unixFile
*)id
;
2350 sem_t
*pSem
= pFile
->pInode
->pSem
;
2353 /* if we already have a lock, it is exclusive.
2354 ** Just adjust level and punt on outta here. */
2355 if (pFile
->eFileLock
> NO_LOCK
) {
2356 pFile
->eFileLock
= eFileLock
;
2361 /* lock semaphore now but bail out when already locked. */
2362 if( sem_trywait(pSem
)==-1 ){
2367 /* got it, set the type and return ok */
2368 pFile
->eFileLock
= eFileLock
;
2375 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2376 ** must be either NO_LOCK or SHARED_LOCK.
2378 ** If the locking level of the file descriptor is already at or below
2379 ** the requested locking level, this routine is a no-op.
2381 static int semUnlock(sqlite3_file
*id
, int eFileLock
) {
2382 unixFile
*pFile
= (unixFile
*)id
;
2383 sem_t
*pSem
= pFile
->pInode
->pSem
;
2387 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile
->h
, eFileLock
,
2388 pFile
->eFileLock
, getpid()));
2389 assert( eFileLock
<=SHARED_LOCK
);
2391 /* no-op if possible */
2392 if( pFile
->eFileLock
==eFileLock
){
2396 /* shared can just be set because we always have an exclusive */
2397 if (eFileLock
==SHARED_LOCK
) {
2398 pFile
->eFileLock
= eFileLock
;
2402 /* no, really unlock. */
2403 if ( sem_post(pSem
)==-1 ) {
2404 int rc
, tErrno
= errno
;
2405 rc
= sqliteErrorFromPosixError(tErrno
, SQLITE_IOERR_UNLOCK
);
2406 if( IS_LOCK_ERROR(rc
) ){
2407 pFile
->lastErrno
= tErrno
;
2411 pFile
->eFileLock
= NO_LOCK
;
2418 static int semClose(sqlite3_file
*id
) {
2420 unixFile
*pFile
= (unixFile
*)id
;
2421 semUnlock(id
, NO_LOCK
);
2424 releaseInodeInfo(pFile
);
2431 #endif /* OS_VXWORKS */
2433 ** Named semaphore locking is only available on VxWorks.
2435 *************** End of the named semaphore lock implementation ****************
2436 ******************************************************************************/
2439 /******************************************************************************
2440 *************************** Begin AFP Locking *********************************
2442 ** AFP is the Apple Filing Protocol. AFP is a network filesystem found
2443 ** on Apple Macintosh computers - both OS9 and OSX.
2445 ** Third-party implementations of AFP are available. But this code here
2446 ** only works on OSX.
2449 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2451 ** The afpLockingContext structure contains all afp lock specific state
2453 typedef struct afpLockingContext afpLockingContext
;
2454 struct afpLockingContext
{
2456 const char *dbPath
; /* Name of the open file */
2459 struct ByteRangeLockPB2
2461 unsigned long long offset
; /* offset to first byte to lock */
2462 unsigned long long length
; /* nbr of bytes to lock */
2463 unsigned long long retRangeStart
; /* nbr of 1st byte locked if successful */
2464 unsigned char unLockFlag
; /* 1 = unlock, 0 = lock */
2465 unsigned char startEndFlag
; /* 1=rel to end of fork, 0=rel to start */
2466 int fd
; /* file desc to assoc this lock with */
2469 #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
2472 ** This is a utility for setting or clearing a bit-range lock on an
2475 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
2477 static int afpSetLock(
2478 const char *path
, /* Name of the file to be locked or unlocked */
2479 unixFile
*pFile
, /* Open file descriptor on path */
2480 unsigned long long offset
, /* First byte to be locked */
2481 unsigned long long length
, /* Number of bytes to lock */
2482 int setLockFlag
/* True to set lock. False to clear lock */
2484 struct ByteRangeLockPB2 pb
;
2487 pb
.unLockFlag
= setLockFlag
? 0 : 1;
2488 pb
.startEndFlag
= 0;
2493 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
2494 (setLockFlag
?"ON":"OFF"), pFile
->h
, (pb
.fd
==-1?"[testval-1]":""),
2496 err
= fsctl(path
, afpfsByteRangeLock2FSCTL
, &pb
, 0);
2500 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
2501 path
, tErrno
, strerror(tErrno
)));
2502 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2505 rc
= sqliteErrorFromPosixError(tErrno
,
2506 setLockFlag
? SQLITE_IOERR_LOCK
: SQLITE_IOERR_UNLOCK
);
2507 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
2508 if( IS_LOCK_ERROR(rc
) ){
2509 pFile
->lastErrno
= tErrno
;
2518 ** This routine checks if there is a RESERVED lock held on the specified
2519 ** file by this or any other process. If such a lock is held, set *pResOut
2520 ** to a non-zero value otherwise *pResOut is set to zero. The return value
2521 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2523 static int afpCheckReservedLock(sqlite3_file
*id
, int *pResOut
){
2526 unixFile
*pFile
= (unixFile
*)id
;
2527 afpLockingContext
*context
;
2529 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK
; );
2532 context
= (afpLockingContext
*) pFile
->lockingContext
;
2533 if( context
->reserved
){
2537 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
2539 /* Check if a thread in this process holds such a lock */
2540 if( pFile
->pInode
->eFileLock
>SHARED_LOCK
){
2544 /* Otherwise see if some other process holds it.
2547 /* lock the RESERVED byte */
2548 int lrc
= afpSetLock(context
->dbPath
, pFile
, RESERVED_BYTE
, 1,1);
2549 if( SQLITE_OK
==lrc
){
2550 /* if we succeeded in taking the reserved lock, unlock it to restore
2551 ** the original state */
2552 lrc
= afpSetLock(context
->dbPath
, pFile
, RESERVED_BYTE
, 1, 0);
2554 /* if we failed to get the lock then someone else must have it */
2557 if( IS_LOCK_ERROR(lrc
) ){
2563 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile
->h
, rc
, reserved
));
2565 *pResOut
= reserved
;
2570 ** Lock the file with the lock specified by parameter eFileLock - one
2571 ** of the following:
2574 ** (2) RESERVED_LOCK
2576 ** (4) EXCLUSIVE_LOCK
2578 ** Sometimes when requesting one lock state, additional lock states
2579 ** are inserted in between. The locking might fail on one of the later
2580 ** transitions leaving the lock state different from what it started but
2581 ** still short of its goal. The following chart shows the allowed
2582 ** transitions and the inserted intermediate states:
2584 ** UNLOCKED -> SHARED
2585 ** SHARED -> RESERVED
2586 ** SHARED -> (PENDING) -> EXCLUSIVE
2587 ** RESERVED -> (PENDING) -> EXCLUSIVE
2588 ** PENDING -> EXCLUSIVE
2590 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
2591 ** routine to lower a locking level.
2593 static int afpLock(sqlite3_file
*id
, int eFileLock
){
2595 unixFile
*pFile
= (unixFile
*)id
;
2596 unixInodeInfo
*pInode
= pFile
->pInode
;
2597 afpLockingContext
*context
= (afpLockingContext
*) pFile
->lockingContext
;
2600 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile
->h
,
2601 azFileLock(eFileLock
), azFileLock(pFile
->eFileLock
),
2602 azFileLock(pInode
->eFileLock
), pInode
->nShared
, getpid()));
2604 /* If there is already a lock of this type or more restrictive on the
2605 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2606 ** unixEnterMutex() hasn't been called yet.
2608 if( pFile
->eFileLock
>=eFileLock
){
2609 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile
->h
,
2610 azFileLock(eFileLock
)));
2614 /* Make sure the locking sequence is correct
2615 ** (1) We never move from unlocked to anything higher than shared lock.
2616 ** (2) SQLite never explicitly requests a pendig lock.
2617 ** (3) A shared lock is always held when a reserve lock is requested.
2619 assert( pFile
->eFileLock
!=NO_LOCK
|| eFileLock
==SHARED_LOCK
);
2620 assert( eFileLock
!=PENDING_LOCK
);
2621 assert( eFileLock
!=RESERVED_LOCK
|| pFile
->eFileLock
==SHARED_LOCK
);
2623 /* This mutex is needed because pFile->pInode is shared across threads
2626 pInode
= pFile
->pInode
;
2628 /* If some thread using this PID has a lock via a different unixFile*
2629 ** handle that precludes the requested lock, return BUSY.
2631 if( (pFile
->eFileLock
!=pInode
->eFileLock
&&
2632 (pInode
->eFileLock
>=PENDING_LOCK
|| eFileLock
>SHARED_LOCK
))
2638 /* If a SHARED lock is requested, and some thread using this PID already
2639 ** has a SHARED or RESERVED lock, then increment reference counts and
2640 ** return SQLITE_OK.
2642 if( eFileLock
==SHARED_LOCK
&&
2643 (pInode
->eFileLock
==SHARED_LOCK
|| pInode
->eFileLock
==RESERVED_LOCK
) ){
2644 assert( eFileLock
==SHARED_LOCK
);
2645 assert( pFile
->eFileLock
==0 );
2646 assert( pInode
->nShared
>0 );
2647 pFile
->eFileLock
= SHARED_LOCK
;
2653 /* A PENDING lock is needed before acquiring a SHARED lock and before
2654 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2657 if( eFileLock
==SHARED_LOCK
2658 || (eFileLock
==EXCLUSIVE_LOCK
&& pFile
->eFileLock
<PENDING_LOCK
)
2661 failed
= afpSetLock(context
->dbPath
, pFile
, PENDING_BYTE
, 1, 1);
2668 /* If control gets to this point, then actually go ahead and make
2669 ** operating system calls for the specified lock.
2671 if( eFileLock
==SHARED_LOCK
){
2672 int lrc1
, lrc2
, lrc1Errno
= 0;
2675 assert( pInode
->nShared
==0 );
2676 assert( pInode
->eFileLock
==0 );
2678 mask
= (sizeof(long)==8) ? LARGEST_INT64
: 0x7fffffff;
2679 /* Now get the read-lock SHARED_LOCK */
2680 /* note that the quality of the randomness doesn't matter that much */
2682 pInode
->sharedByte
= (lk
& mask
)%(SHARED_SIZE
- 1);
2683 lrc1
= afpSetLock(context
->dbPath
, pFile
,
2684 SHARED_FIRST
+pInode
->sharedByte
, 1, 1);
2685 if( IS_LOCK_ERROR(lrc1
) ){
2686 lrc1Errno
= pFile
->lastErrno
;
2688 /* Drop the temporary PENDING lock */
2689 lrc2
= afpSetLock(context
->dbPath
, pFile
, PENDING_BYTE
, 1, 0);
2691 if( IS_LOCK_ERROR(lrc1
) ) {
2692 pFile
->lastErrno
= lrc1Errno
;
2695 } else if( IS_LOCK_ERROR(lrc2
) ){
2698 } else if( lrc1
!= SQLITE_OK
) {
2701 pFile
->eFileLock
= SHARED_LOCK
;
2703 pInode
->nShared
= 1;
2705 }else if( eFileLock
==EXCLUSIVE_LOCK
&& pInode
->nShared
>1 ){
2706 /* We are trying for an exclusive lock but another thread in this
2707 ** same process is still holding a shared lock. */
2710 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2711 ** assumed that there is a SHARED or greater lock on the file
2715 assert( 0!=pFile
->eFileLock
);
2716 if (eFileLock
>= RESERVED_LOCK
&& pFile
->eFileLock
< RESERVED_LOCK
) {
2717 /* Acquire a RESERVED lock */
2718 failed
= afpSetLock(context
->dbPath
, pFile
, RESERVED_BYTE
, 1,1);
2720 context
->reserved
= 1;
2723 if (!failed
&& eFileLock
== EXCLUSIVE_LOCK
) {
2724 /* Acquire an EXCLUSIVE lock */
2726 /* Remove the shared lock before trying the range. we'll need to
2727 ** reestablish the shared lock if we can't get the afpUnlock
2729 if( !(failed
= afpSetLock(context
->dbPath
, pFile
, SHARED_FIRST
+
2730 pInode
->sharedByte
, 1, 0)) ){
2731 int failed2
= SQLITE_OK
;
2732 /* now attemmpt to get the exclusive lock range */
2733 failed
= afpSetLock(context
->dbPath
, pFile
, SHARED_FIRST
,
2735 if( failed
&& (failed2
= afpSetLock(context
->dbPath
, pFile
,
2736 SHARED_FIRST
+ pInode
->sharedByte
, 1, 1)) ){
2737 /* Can't reestablish the shared lock. Sqlite can't deal, this is
2738 ** a critical I/O error
2740 rc
= ((failed
& SQLITE_IOERR
) == SQLITE_IOERR
) ? failed2
:
2753 if( rc
==SQLITE_OK
){
2754 pFile
->eFileLock
= eFileLock
;
2755 pInode
->eFileLock
= eFileLock
;
2756 }else if( eFileLock
==EXCLUSIVE_LOCK
){
2757 pFile
->eFileLock
= PENDING_LOCK
;
2758 pInode
->eFileLock
= PENDING_LOCK
;
2763 OSTRACE(("LOCK %d %s %s (afp)\n", pFile
->h
, azFileLock(eFileLock
),
2764 rc
==SQLITE_OK
? "ok" : "failed"));
2769 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2770 ** must be either NO_LOCK or SHARED_LOCK.
2772 ** If the locking level of the file descriptor is already at or below
2773 ** the requested locking level, this routine is a no-op.
2775 static int afpUnlock(sqlite3_file
*id
, int eFileLock
) {
2777 unixFile
*pFile
= (unixFile
*)id
;
2778 unixInodeInfo
*pInode
;
2779 afpLockingContext
*context
= (afpLockingContext
*) pFile
->lockingContext
;
2786 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile
->h
, eFileLock
,
2787 pFile
->eFileLock
, pFile
->pInode
->eFileLock
, pFile
->pInode
->nShared
,
2790 assert( eFileLock
<=SHARED_LOCK
);
2791 if( pFile
->eFileLock
<=eFileLock
){
2795 pInode
= pFile
->pInode
;
2796 assert( pInode
->nShared
!=0 );
2797 if( pFile
->eFileLock
>SHARED_LOCK
){
2798 assert( pInode
->eFileLock
==pFile
->eFileLock
);
2799 SimulateIOErrorBenign(1);
2800 SimulateIOError( h
=(-1) )
2801 SimulateIOErrorBenign(0);
2804 /* When reducing a lock such that other processes can start
2805 ** reading the database file again, make sure that the
2806 ** transaction counter was updated if any part of the database
2807 ** file changed. If the transaction counter is not updated,
2808 ** other connections to the same file might not realize that
2809 ** the file has changed and hence might not know to flush their
2810 ** cache. The use of a stale cache can lead to database corruption.
2812 assert( pFile
->inNormalWrite
==0
2813 || pFile
->dbUpdate
==0
2814 || pFile
->transCntrChng
==1 );
2815 pFile
->inNormalWrite
= 0;
2818 if( pFile
->eFileLock
==EXCLUSIVE_LOCK
){
2819 rc
= afpSetLock(context
->dbPath
, pFile
, SHARED_FIRST
, SHARED_SIZE
, 0);
2820 if( rc
==SQLITE_OK
&& (eFileLock
==SHARED_LOCK
|| pInode
->nShared
>1) ){
2821 /* only re-establish the shared lock if necessary */
2822 int sharedLockByte
= SHARED_FIRST
+pInode
->sharedByte
;
2823 rc
= afpSetLock(context
->dbPath
, pFile
, sharedLockByte
, 1, 1);
2828 if( rc
==SQLITE_OK
&& pFile
->eFileLock
>=PENDING_LOCK
){
2829 rc
= afpSetLock(context
->dbPath
, pFile
, PENDING_BYTE
, 1, 0);
2831 if( rc
==SQLITE_OK
&& pFile
->eFileLock
>=RESERVED_LOCK
&& context
->reserved
){
2832 rc
= afpSetLock(context
->dbPath
, pFile
, RESERVED_BYTE
, 1, 0);
2834 context
->reserved
= 0;
2837 if( rc
==SQLITE_OK
&& (eFileLock
==SHARED_LOCK
|| pInode
->nShared
>1)){
2838 pInode
->eFileLock
= SHARED_LOCK
;
2841 if( rc
==SQLITE_OK
&& eFileLock
==NO_LOCK
){
2843 /* Decrement the shared lock counter. Release the lock using an
2844 ** OS call only when all threads in this same process have released
2847 unsigned long long sharedLockByte
= SHARED_FIRST
+pInode
->sharedByte
;
2849 if( pInode
->nShared
==0 ){
2850 SimulateIOErrorBenign(1);
2851 SimulateIOError( h
=(-1) )
2852 SimulateIOErrorBenign(0);
2854 rc
= afpSetLock(context
->dbPath
, pFile
, sharedLockByte
, 1, 0);
2857 pInode
->eFileLock
= NO_LOCK
;
2858 pFile
->eFileLock
= NO_LOCK
;
2861 if( rc
==SQLITE_OK
){
2863 assert( pInode
->nLock
>=0 );
2864 if( pInode
->nLock
==0 ){
2865 closePendingFds(pFile
);
2871 if( rc
==SQLITE_OK
) pFile
->eFileLock
= eFileLock
;
2876 ** Close a file & cleanup AFP specific locking context
2878 static int afpClose(sqlite3_file
*id
) {
2881 unixFile
*pFile
= (unixFile
*)id
;
2882 afpUnlock(id
, NO_LOCK
);
2884 if( pFile
->pInode
&& pFile
->pInode
->nLock
){
2885 /* If there are outstanding locks, do not actually close the file just
2886 ** yet because that would clear those locks. Instead, add the file
2887 ** descriptor to pInode->aPending. It will be automatically closed when
2888 ** the last lock is cleared.
2890 setPendingFd(pFile
);
2892 releaseInodeInfo(pFile
);
2893 sqlite3_free(pFile
->lockingContext
);
2894 rc
= closeUnixFile(id
);
2900 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
2902 ** The code above is the AFP lock implementation. The code is specific
2903 ** to MacOSX and does not work on other unix platforms. No alternative
2904 ** is available. If you don't compile for a mac, then the "unix-afp"
2905 ** VFS is not available.
2907 ********************* End of the AFP lock implementation **********************
2908 ******************************************************************************/
2910 /******************************************************************************
2911 *************************** Begin NFS Locking ********************************/
2913 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2915 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2916 ** must be either NO_LOCK or SHARED_LOCK.
2918 ** If the locking level of the file descriptor is already at or below
2919 ** the requested locking level, this routine is a no-op.
2921 static int nfsUnlock(sqlite3_file
*id
, int eFileLock
){
2922 return posixUnlock(id
, eFileLock
, 1);
2925 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
2927 ** The code above is the NFS lock implementation. The code is specific
2928 ** to MacOSX and does not work on other unix platforms. No alternative
2931 ********************* End of the NFS lock implementation **********************
2932 ******************************************************************************/
2934 /******************************************************************************
2935 **************** Non-locking sqlite3_file methods *****************************
2937 ** The next division contains implementations for all methods of the
2938 ** sqlite3_file object other than the locking methods. The locking
2939 ** methods were defined in divisions above (one locking method per
2940 ** division). Those methods that are common to all locking modes
2941 ** are gather together into this division.
2945 ** Seek to the offset passed as the second argument, then read cnt
2946 ** bytes into pBuf. Return the number of bytes actually read.
2948 ** NB: If you define USE_PREAD or USE_PREAD64, then it might also
2949 ** be necessary to define _XOPEN_SOURCE to be 500. This varies from
2950 ** one system to another. Since SQLite does not define USE_PREAD
2951 ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
2952 ** See tickets #2741 and #2681.
2954 ** To avoid stomping the errno value on a failed read the lastErrno value
2955 ** is set before returning.
2957 static int seekAndRead(unixFile
*id
, sqlite3_int64 offset
, void *pBuf
, int cnt
){
2960 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
2965 #if defined(USE_PREAD)
2966 got
= osPread(id
->h
, pBuf
, cnt
, offset
);
2967 SimulateIOError( got
= -1 );
2968 #elif defined(USE_PREAD64)
2969 got
= osPread64(id
->h
, pBuf
, cnt
, offset
);
2970 SimulateIOError( got
= -1 );
2972 newOffset
= lseek(id
->h
, offset
, SEEK_SET
);
2973 SimulateIOError( newOffset
-- );
2974 if( newOffset
!=offset
){
2975 if( newOffset
== -1 ){
2976 ((unixFile
*)id
)->lastErrno
= errno
;
2978 ((unixFile
*)id
)->lastErrno
= 0;
2982 got
= osRead(id
->h
, pBuf
, cnt
);
2984 if( got
==cnt
) break;
2986 if( errno
==EINTR
){ got
= 1; continue; }
2988 ((unixFile
*)id
)->lastErrno
= errno
;
2994 pBuf
= (void*)(got
+ (char*)pBuf
);
2998 OSTRACE(("READ %-3d %5d %7lld %llu\n",
2999 id
->h
, got
+prior
, offset
-prior
, TIMER_ELAPSED
));
3004 ** Read data from a file into a buffer. Return SQLITE_OK if all
3005 ** bytes were read successfully and SQLITE_IOERR if anything goes
3008 static int unixRead(
3012 sqlite3_int64 offset
3014 unixFile
*pFile
= (unixFile
*)id
;
3018 /* If this is a database file (not a journal, master-journal or temp
3019 ** file), the bytes in the locking range should never be read or written. */
3021 assert( pFile
->pUnused
==0
3022 || offset
>=PENDING_BYTE
+512
3023 || offset
+amt
<=PENDING_BYTE
3027 got
= seekAndRead(pFile
, offset
, pBuf
, amt
);
3031 /* lastErrno set by seekAndRead */
3032 return SQLITE_IOERR_READ
;
3034 pFile
->lastErrno
= 0; /* not a system error */
3035 /* Unread parts of the buffer must be zero-filled */
3036 memset(&((char*)pBuf
)[got
], 0, amt
-got
);
3037 return SQLITE_IOERR_SHORT_READ
;
3042 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
3043 ** Return the number of bytes actually read. Update the offset.
3045 ** To avoid stomping the errno value on a failed write the lastErrno value
3046 ** is set before returning.
3048 static int seekAndWrite(unixFile
*id
, i64 offset
, const void *pBuf
, int cnt
){
3050 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
3054 #if defined(USE_PREAD)
3055 do{ got
= osPwrite(id
->h
, pBuf
, cnt
, offset
); }while( got
<0 && errno
==EINTR
);
3056 #elif defined(USE_PREAD64)
3057 do{ got
= osPwrite64(id
->h
, pBuf
, cnt
, offset
);}while( got
<0 && errno
==EINTR
);
3060 newOffset
= lseek(id
->h
, offset
, SEEK_SET
);
3061 SimulateIOError( newOffset
-- );
3062 if( newOffset
!=offset
){
3063 if( newOffset
== -1 ){
3064 ((unixFile
*)id
)->lastErrno
= errno
;
3066 ((unixFile
*)id
)->lastErrno
= 0;
3070 got
= osWrite(id
->h
, pBuf
, cnt
);
3071 }while( got
<0 && errno
==EINTR
);
3075 ((unixFile
*)id
)->lastErrno
= errno
;
3078 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id
->h
, got
, offset
, TIMER_ELAPSED
));
3084 ** Write data from a buffer into a file. Return SQLITE_OK on success
3085 ** or some other error code on failure.
3087 static int unixWrite(
3091 sqlite3_int64 offset
3093 unixFile
*pFile
= (unixFile
*)id
;
3098 /* If this is a database file (not a journal, master-journal or temp
3099 ** file), the bytes in the locking range should never be read or written. */
3101 assert( pFile
->pUnused
==0
3102 || offset
>=PENDING_BYTE
+512
3103 || offset
+amt
<=PENDING_BYTE
3108 /* If we are doing a normal write to a database file (as opposed to
3109 ** doing a hot-journal rollback or a write to some file other than a
3110 ** normal database file) then record the fact that the database
3111 ** has changed. If the transaction counter is modified, record that
3114 if( pFile
->inNormalWrite
){
3115 pFile
->dbUpdate
= 1; /* The database has been modified */
3116 if( offset
<=24 && offset
+amt
>=27 ){
3119 SimulateIOErrorBenign(1);
3120 rc
= seekAndRead(pFile
, 24, oldCntr
, 4);
3121 SimulateIOErrorBenign(0);
3122 if( rc
!=4 || memcmp(oldCntr
, &((char*)pBuf
)[24-offset
], 4)!=0 ){
3123 pFile
->transCntrChng
= 1; /* The transaction counter has changed */
3129 while( amt
>0 && (wrote
= seekAndWrite(pFile
, offset
, pBuf
, amt
))>0 ){
3132 pBuf
= &((char*)pBuf
)[wrote
];
3134 SimulateIOError(( wrote
=(-1), amt
=1 ));
3135 SimulateDiskfullError(( wrote
=0, amt
=1 ));
3138 if( wrote
<0 && pFile
->lastErrno
!=ENOSPC
){
3139 /* lastErrno set by seekAndWrite */
3140 return SQLITE_IOERR_WRITE
;
3142 pFile
->lastErrno
= 0; /* not a system error */
3152 ** Count the number of fullsyncs and normal syncs. This is used to test
3153 ** that syncs and fullsyncs are occurring at the right times.
3155 int sqlite3_sync_count
= 0;
3156 int sqlite3_fullsync_count
= 0;
3160 ** We do not trust systems to provide a working fdatasync(). Some do.
3161 ** Others do no. To be safe, we will stick with the (slightly slower)
3162 ** fsync(). If you know that your system does support fdatasync() correctly,
3163 ** then simply compile with -Dfdatasync=fdatasync
3165 #if !defined(fdatasync)
3166 # define fdatasync fsync
3170 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
3171 ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
3172 ** only available on Mac OS X. But that could change.
3175 # define HAVE_FULLFSYNC 1
3177 # define HAVE_FULLFSYNC 0
3182 ** The fsync() system call does not work as advertised on many
3183 ** unix systems. The following procedure is an attempt to make
3186 ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
3187 ** for testing when we want to run through the test suite quickly.
3188 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
3189 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
3190 ** or power failure will likely corrupt the database file.
3192 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
3193 ** The idea behind dataOnly is that it should only write the file content
3194 ** to disk, not the inode. We only set dataOnly if the file size is
3195 ** unchanged since the file size is part of the inode. However,
3196 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
3197 ** file size has changed. The only real difference between fdatasync()
3198 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
3199 ** inode if the mtime or owner or other inode attributes have changed.
3200 ** We only care about the file size, not the other file attributes, so
3201 ** as far as SQLite is concerned, an fdatasync() is always adequate.
3202 ** So, we always use fdatasync() if it is available, regardless of
3203 ** the value of the dataOnly flag.
3205 static int full_fsync(int fd
, int fullSync
, int dataOnly
){
3208 /* The following "ifdef/elif/else/" block has the same structure as
3209 ** the one below. It is replicated here solely to avoid cluttering
3210 ** up the real code with the UNUSED_PARAMETER() macros.
3212 #ifdef SQLITE_NO_SYNC
3213 UNUSED_PARAMETER(fd
);
3214 UNUSED_PARAMETER(fullSync
);
3215 UNUSED_PARAMETER(dataOnly
);
3216 #elif HAVE_FULLFSYNC
3217 UNUSED_PARAMETER(dataOnly
);
3219 UNUSED_PARAMETER(fullSync
);
3220 UNUSED_PARAMETER(dataOnly
);
3223 /* Record the number of times that we do a normal fsync() and
3224 ** FULLSYNC. This is used during testing to verify that this procedure
3225 ** gets called with the correct arguments.
3228 if( fullSync
) sqlite3_fullsync_count
++;
3229 sqlite3_sync_count
++;
3232 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
3235 #ifdef SQLITE_NO_SYNC
3237 #elif HAVE_FULLFSYNC
3239 rc
= osFcntl(fd
, F_FULLFSYNC
, 0);
3243 /* If the FULLFSYNC failed, fall back to attempting an fsync().
3244 ** It shouldn't be possible for fullfsync to fail on the local
3245 ** file system (on OSX), so failure indicates that FULLFSYNC
3246 ** isn't supported for this file system. So, attempt an fsync
3247 ** and (for now) ignore the overhead of a superfluous fcntl call.
3248 ** It'd be better to detect fullfsync support once and avoid
3249 ** the fcntl call every time sync is called.
3251 if( rc
) rc
= fsync(fd
);
3253 #elif defined(__APPLE__)
3254 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
3255 ** so currently we default to the macro that redefines fdatasync to fsync
3261 if( rc
==-1 && errno
==ENOTSUP
){
3264 #endif /* OS_VXWORKS */
3265 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
3267 if( OS_VXWORKS
&& rc
!= -1 ){
3274 ** Open a file descriptor to the directory containing file zFilename.
3275 ** If successful, *pFd is set to the opened file descriptor and
3276 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
3277 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
3280 ** The directory file descriptor is used for only one thing - to
3281 ** fsync() a directory to make sure file creation and deletion events
3282 ** are flushed to disk. Such fsyncs are not needed on newer
3283 ** journaling filesystems, but are required on older filesystems.
3285 ** This routine can be overridden using the xSetSysCall interface.
3286 ** The ability to override this routine was added in support of the
3287 ** chromium sandbox. Opening a directory is a security risk (we are
3288 ** told) so making it overrideable allows the chromium sandbox to
3289 ** replace this routine with a harmless no-op. To make this routine
3290 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
3291 ** *pFd set to a negative number.
3293 ** If SQLITE_OK is returned, the caller is responsible for closing
3294 ** the file descriptor *pFd using close().
3296 static int openDirectory(const char *zFilename
, int *pFd
){
3299 char zDirname
[MAX_PATHNAME
+1];
3301 sqlite3_snprintf(MAX_PATHNAME
, zDirname
, "%s", zFilename
);
3302 for(ii
=(int)strlen(zDirname
); ii
>1 && zDirname
[ii
]!='/'; ii
--);
3304 zDirname
[ii
] = '\0';
3305 fd
= robust_open(zDirname
, O_RDONLY
|O_BINARY
, 0);
3308 osFcntl(fd
, F_SETFD
, osFcntl(fd
, F_GETFD
, 0) | FD_CLOEXEC
);
3310 OSTRACE(("OPENDIR %-3d %s\n", fd
, zDirname
));
3314 return (fd
>=0?SQLITE_OK
:unixLogError(SQLITE_CANTOPEN_BKPT
, "open", zDirname
));
3318 ** Make sure all writes to a particular file are committed to disk.
3320 ** If dataOnly==0 then both the file itself and its metadata (file
3321 ** size, access time, etc) are synced. If dataOnly!=0 then only the
3322 ** file data is synced.
3324 ** Under Unix, also make sure that the directory entry for the file
3325 ** has been created by fsync-ing the directory that contains the file.
3326 ** If we do not do this and we encounter a power failure, the directory
3327 ** entry for the journal might not exist after we reboot. The next
3328 ** SQLite to access the file will not know that the journal exists (because
3329 ** the directory entry for the journal was never created) and the transaction
3330 ** will not roll back - possibly leading to database corruption.
3332 static int unixSync(sqlite3_file
*id
, int flags
){
3334 unixFile
*pFile
= (unixFile
*)id
;
3336 int isDataOnly
= (flags
&SQLITE_SYNC_DATAONLY
);
3337 int isFullsync
= (flags
&0x0F)==SQLITE_SYNC_FULL
;
3339 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
3340 assert((flags
&0x0F)==SQLITE_SYNC_NORMAL
3341 || (flags
&0x0F)==SQLITE_SYNC_FULL
3344 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
3345 ** line is to test that doing so does not cause any problems.
3347 SimulateDiskfullError( return SQLITE_FULL
);
3350 OSTRACE(("SYNC %-3d\n", pFile
->h
));
3351 rc
= full_fsync(pFile
->h
, isFullsync
, isDataOnly
);
3352 SimulateIOError( rc
=1 );
3354 pFile
->lastErrno
= errno
;
3355 return unixLogError(SQLITE_IOERR_FSYNC
, "full_fsync", pFile
->zPath
);
3358 /* Also fsync the directory containing the file if the DIRSYNC flag
3359 ** is set. This is a one-time occurrance. Many systems (examples: AIX)
3360 ** are unable to fsync a directory, so ignore errors on the fsync.
3362 if( pFile
->ctrlFlags
& UNIXFILE_DIRSYNC
){
3364 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile
->zPath
,
3365 HAVE_FULLFSYNC
, isFullsync
));
3366 rc
= osOpenDirectory(pFile
->zPath
, &dirfd
);
3367 if( rc
==SQLITE_OK
&& dirfd
>=0 ){
3368 full_fsync(dirfd
, 0, 0);
3369 robust_close(pFile
, dirfd
, __LINE__
);
3370 }else if( rc
==SQLITE_CANTOPEN
){
3373 pFile
->ctrlFlags
&= ~UNIXFILE_DIRSYNC
;
3379 ** Truncate an open file to a specified size
3381 static int unixTruncate(sqlite3_file
*id
, i64 nByte
){
3382 unixFile
*pFile
= (unixFile
*)id
;
3385 SimulateIOError( return SQLITE_IOERR_TRUNCATE
);
3387 /* If the user has configured a chunk-size for this file, truncate the
3388 ** file so that it consists of an integer number of chunks (i.e. the
3389 ** actual file size after the operation may be larger than the requested
3392 if( pFile
->szChunk
){
3393 nByte
= ((nByte
+ pFile
->szChunk
- 1)/pFile
->szChunk
) * pFile
->szChunk
;
3396 rc
= robust_ftruncate(pFile
->h
, (off_t
)nByte
);
3398 pFile
->lastErrno
= errno
;
3399 return unixLogError(SQLITE_IOERR_TRUNCATE
, "ftruncate", pFile
->zPath
);
3402 /* If we are doing a normal write to a database file (as opposed to
3403 ** doing a hot-journal rollback or a write to some file other than a
3404 ** normal database file) and we truncate the file to zero length,
3405 ** that effectively updates the change counter. This might happen
3406 ** when restoring a database using the backup API from a zero-length
3409 if( pFile
->inNormalWrite
&& nByte
==0 ){
3410 pFile
->transCntrChng
= 1;
3419 ** Determine the current size of a file in bytes
3421 static int unixFileSize(sqlite3_file
*id
, i64
*pSize
){
3425 rc
= osFstat(((unixFile
*)id
)->h
, &buf
);
3426 SimulateIOError( rc
=1 );
3428 ((unixFile
*)id
)->lastErrno
= errno
;
3429 return SQLITE_IOERR_FSTAT
;
3431 *pSize
= buf
.st_size
;
3433 /* When opening a zero-size database, the findInodeInfo() procedure
3434 ** writes a single byte into that file in order to work around a bug
3435 ** in the OS-X msdos filesystem. In order to avoid problems with upper
3436 ** layers, we need to report this file size as zero even though it is
3437 ** really 1. Ticket #3260.
3439 if( *pSize
==1 ) *pSize
= 0;
3445 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
3447 ** Handler for proxy-locking file-control verbs. Defined below in the
3448 ** proxying locking division.
3450 static int proxyFileControl(sqlite3_file
*,int,void*);
3454 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
3455 ** file-control operation. Enlarge the database to nBytes in size
3456 ** (rounded up to the next chunk-size). If the database is already
3457 ** nBytes or larger, this routine is a no-op.
3459 static int fcntlSizeHint(unixFile
*pFile
, i64 nByte
){
3460 if( pFile
->szChunk
>0 ){
3461 i64 nSize
; /* Required file size */
3462 struct stat buf
; /* Used to hold return values of fstat() */
3464 if( osFstat(pFile
->h
, &buf
) ) return SQLITE_IOERR_FSTAT
;
3466 nSize
= ((nByte
+pFile
->szChunk
-1) / pFile
->szChunk
) * pFile
->szChunk
;
3467 if( nSize
>(i64
)buf
.st_size
){
3469 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
3470 /* The code below is handling the return value of osFallocate()
3471 ** correctly. posix_fallocate() is defined to "returns zero on success,
3472 ** or an error number on failure". See the manpage for details. */
3475 err
= osFallocate(pFile
->h
, buf
.st_size
, nSize
-buf
.st_size
);
3476 }while( err
==EINTR
);
3477 if( err
) return SQLITE_IOERR_WRITE
;
3479 /* If the OS does not have posix_fallocate(), fake it. First use
3480 ** ftruncate() to set the file size, then write a single byte to
3481 ** the last byte in each block within the extended region. This
3482 ** is the same technique used by glibc to implement posix_fallocate()
3483 ** on systems that do not have a real fallocate() system call.
3485 int nBlk
= buf
.st_blksize
; /* File-system block size */
3486 i64 iWrite
; /* Next offset to write to */
3488 if( robust_ftruncate(pFile
->h
, nSize
) ){
3489 pFile
->lastErrno
= errno
;
3490 return unixLogError(SQLITE_IOERR_TRUNCATE
, "ftruncate", pFile
->zPath
);
3492 iWrite
= ((buf
.st_size
+ 2*nBlk
- 1)/nBlk
)*nBlk
-1;
3493 while( iWrite
<nSize
){
3494 int nWrite
= seekAndWrite(pFile
, iWrite
, "", 1);
3495 if( nWrite
!=1 ) return SQLITE_IOERR_WRITE
;
3506 ** If *pArg is inititially negative then this is a query. Set *pArg to
3507 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
3509 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
3511 static void unixModeBit(unixFile
*pFile
, unsigned char mask
, int *pArg
){
3513 *pArg
= (pFile
->ctrlFlags
& mask
)!=0;
3514 }else if( (*pArg
)==0 ){
3515 pFile
->ctrlFlags
&= ~mask
;
3517 pFile
->ctrlFlags
|= mask
;
3522 ** Information and control of an open file handle.
3524 static int unixFileControl(sqlite3_file
*id
, int op
, void *pArg
){
3525 unixFile
*pFile
= (unixFile
*)id
;
3527 case SQLITE_FCNTL_LOCKSTATE
: {
3528 *(int*)pArg
= pFile
->eFileLock
;
3531 case SQLITE_LAST_ERRNO
: {
3532 *(int*)pArg
= pFile
->lastErrno
;
3535 case SQLITE_FCNTL_CHUNK_SIZE
: {
3536 pFile
->szChunk
= *(int *)pArg
;
3539 case SQLITE_FCNTL_SIZE_HINT
: {
3541 SimulateIOErrorBenign(1);
3542 rc
= fcntlSizeHint(pFile
, *(i64
*)pArg
);
3543 SimulateIOErrorBenign(0);
3546 case SQLITE_FCNTL_PERSIST_WAL
: {
3547 unixModeBit(pFile
, UNIXFILE_PERSIST_WAL
, (int*)pArg
);
3550 case SQLITE_FCNTL_POWERSAFE_OVERWRITE
: {
3551 unixModeBit(pFile
, UNIXFILE_PSOW
, (int*)pArg
);
3554 case SQLITE_FCNTL_VFSNAME
: {
3555 *(char**)pArg
= sqlite3_mprintf("%s", pFile
->pVfs
->zName
);
3559 /* The pager calls this method to signal that it has done
3560 ** a rollback and that the database is therefore unchanged and
3561 ** it hence it is OK for the transaction change counter to be
3564 case SQLITE_FCNTL_DB_UNCHANGED
: {
3565 ((unixFile
*)id
)->dbUpdate
= 0;
3569 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
3570 case SQLITE_SET_LOCKPROXYFILE
:
3571 case SQLITE_GET_LOCKPROXYFILE
: {
3572 return proxyFileControl(id
,op
,pArg
);
3574 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
3576 return SQLITE_NOTFOUND
;
3580 ** Return the sector size in bytes of the underlying block device for
3581 ** the specified file. This is almost always 512 bytes, but may be
3582 ** larger for some devices.
3584 ** SQLite code assumes this function cannot fail. It also assumes that
3585 ** if two files are created in the same file-system directory (i.e.
3586 ** a database and its journal file) that the sector size will be the
3589 static int unixSectorSize(sqlite3_file
*pFile
){
3591 return SQLITE_DEFAULT_SECTOR_SIZE
;
3595 ** Return the device characteristics for the file.
3597 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
3598 ** However, that choice is contraversial since technically the underlying
3599 ** file system does not always provide powersafe overwrites. (In other
3600 ** words, after a power-loss event, parts of the file that were never
3601 ** written might end up being altered.) However, non-PSOW behavior is very,
3602 ** very rare. And asserting PSOW makes a large reduction in the amount
3603 ** of required I/O for journaling, since a lot of padding is eliminated.
3604 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
3605 ** available to turn it off and URI query parameter available to turn it off.
3607 static int unixDeviceCharacteristics(sqlite3_file
*id
){
3608 unixFile
*p
= (unixFile
*)id
;
3609 if( p
->ctrlFlags
& UNIXFILE_PSOW
){
3610 return SQLITE_IOCAP_POWERSAFE_OVERWRITE
;
3616 #ifndef SQLITE_OMIT_WAL
3620 ** Object used to represent an shared memory buffer.
3622 ** When multiple threads all reference the same wal-index, each thread
3623 ** has its own unixShm object, but they all point to a single instance
3624 ** of this unixShmNode object. In other words, each wal-index is opened
3625 ** only once per process.
3627 ** Each unixShmNode object is connected to a single unixInodeInfo object.
3628 ** We could coalesce this object into unixInodeInfo, but that would mean
3629 ** every open file that does not use shared memory (in other words, most
3630 ** open files) would have to carry around this extra information. So
3631 ** the unixInodeInfo object contains a pointer to this unixShmNode object
3632 ** and the unixShmNode object is created only when needed.
3634 ** unixMutexHeld() must be true when creating or destroying
3635 ** this object or while reading or writing the following fields:
3639 ** The following fields are read-only after the object is created:
3644 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
3645 ** unixMutexHeld() is true when reading or writing any other field
3646 ** in this structure.
3648 struct unixShmNode
{
3649 unixInodeInfo
*pInode
; /* unixInodeInfo that owns this SHM node */
3650 sqlite3_mutex
*mutex
; /* Mutex to access this object */
3651 char *zFilename
; /* Name of the mmapped file */
3652 int h
; /* Open file descriptor */
3653 int szRegion
; /* Size of shared-memory regions */
3654 u16 nRegion
; /* Size of array apRegion */
3655 u8 isReadonly
; /* True if read-only */
3656 char **apRegion
; /* Array of mapped shared-memory regions */
3657 int nRef
; /* Number of unixShm objects pointing to this */
3658 unixShm
*pFirst
; /* All unixShm objects pointing to this */
3660 u8 exclMask
; /* Mask of exclusive locks held */
3661 u8 sharedMask
; /* Mask of shared locks held */
3662 u8 nextShmId
; /* Next available unixShm.id value */
3667 ** Structure used internally by this VFS to record the state of an
3668 ** open shared memory connection.
3670 ** The following fields are initialized when this object is created and
3671 ** are read-only thereafter:
3676 ** All other fields are read/write. The unixShm.pFile->mutex must be held
3677 ** while accessing any read/write fields.
3680 unixShmNode
*pShmNode
; /* The underlying unixShmNode object */
3681 unixShm
*pNext
; /* Next unixShm with the same unixShmNode */
3682 u8 hasMutex
; /* True if holding the unixShmNode mutex */
3683 u8 id
; /* Id of this connection within its unixShmNode */
3684 u16 sharedMask
; /* Mask of shared locks held */
3685 u16 exclMask
; /* Mask of exclusive locks held */
3689 ** Constants used for locking
3691 #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
3692 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
3695 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
3697 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
3700 static int unixShmSystemLock(
3701 unixShmNode
*pShmNode
, /* Apply locks to this open shared-memory segment */
3702 int lockType
, /* F_UNLCK, F_RDLCK, or F_WRLCK */
3703 int ofst
, /* First byte of the locking range */
3704 int n
/* Number of bytes to lock */
3706 struct flock f
; /* The posix advisory locking structure */
3707 int rc
= SQLITE_OK
; /* Result code form fcntl() */
3709 /* Access to the unixShmNode object is serialized by the caller */
3710 assert( sqlite3_mutex_held(pShmNode
->mutex
) || pShmNode
->nRef
==0 );
3712 /* Shared locks never span more than one byte */
3713 assert( n
==1 || lockType
!=F_RDLCK
);
3715 /* Locks are within range */
3716 assert( n
>=1 && n
<SQLITE_SHM_NLOCK
);
3718 if( pShmNode
->h
>=0 ){
3719 /* Initialize the locking parameters */
3720 memset(&f
, 0, sizeof(f
));
3721 f
.l_type
= lockType
;
3722 f
.l_whence
= SEEK_SET
;
3726 rc
= osFcntl(pShmNode
->h
, F_SETLK
, &f
);
3727 rc
= (rc
!=(-1)) ? SQLITE_OK
: SQLITE_BUSY
;
3730 /* Update the global lock state and do debug tracing */
3733 OSTRACE(("SHM-LOCK "));
3734 mask
= (1<<(ofst
+n
)) - (1<<ofst
);
3735 if( rc
==SQLITE_OK
){
3736 if( lockType
==F_UNLCK
){
3737 OSTRACE(("unlock %d ok", ofst
));
3738 pShmNode
->exclMask
&= ~mask
;
3739 pShmNode
->sharedMask
&= ~mask
;
3740 }else if( lockType
==F_RDLCK
){
3741 OSTRACE(("read-lock %d ok", ofst
));
3742 pShmNode
->exclMask
&= ~mask
;
3743 pShmNode
->sharedMask
|= mask
;
3745 assert( lockType
==F_WRLCK
);
3746 OSTRACE(("write-lock %d ok", ofst
));
3747 pShmNode
->exclMask
|= mask
;
3748 pShmNode
->sharedMask
&= ~mask
;
3751 if( lockType
==F_UNLCK
){
3752 OSTRACE(("unlock %d failed", ofst
));
3753 }else if( lockType
==F_RDLCK
){
3754 OSTRACE(("read-lock failed"));
3756 assert( lockType
==F_WRLCK
);
3757 OSTRACE(("write-lock %d failed", ofst
));
3760 OSTRACE((" - afterwards %03x,%03x\n",
3761 pShmNode
->sharedMask
, pShmNode
->exclMask
));
3770 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
3772 ** This is not a VFS shared-memory method; it is a utility function called
3773 ** by VFS shared-memory methods.
3775 static void unixShmPurge(unixFile
*pFd
){
3776 unixShmNode
*p
= pFd
->pInode
->pShmNode
;
3777 assert( unixMutexHeld() );
3778 if( p
&& p
->nRef
==0 ){
3780 assert( p
->pInode
==pFd
->pInode
);
3781 sqlite3_mutex_free(p
->mutex
);
3782 for(i
=0; i
<p
->nRegion
; i
++){
3784 munmap(p
->apRegion
[i
], p
->szRegion
);
3786 sqlite3_free(p
->apRegion
[i
]);
3789 sqlite3_free(p
->apRegion
);
3791 robust_close(pFd
, p
->h
, __LINE__
);
3794 p
->pInode
->pShmNode
= 0;
3800 ** Open a shared-memory area associated with open database file pDbFd.
3801 ** This particular implementation uses mmapped files.
3803 ** The file used to implement shared-memory is in the same directory
3804 ** as the open database file and has the same name as the open database
3805 ** file with the "-shm" suffix added. For example, if the database file
3806 ** is "/home/user1/config.db" then the file that is created and mmapped
3807 ** for shared memory will be called "/home/user1/config.db-shm".
3809 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
3810 ** some other tmpfs mount. But if a file in a different directory
3811 ** from the database file is used, then differing access permissions
3812 ** or a chroot() might cause two different processes on the same
3813 ** database to end up using different files for shared memory -
3814 ** meaning that their memory would not really be shared - resulting
3815 ** in database corruption. Nevertheless, this tmpfs file usage
3816 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
3817 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
3818 ** option results in an incompatible build of SQLite; builds of SQLite
3819 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
3820 ** same database file at the same time, database corruption will likely
3821 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
3822 ** "unsupported" and may go away in a future SQLite release.
3824 ** When opening a new shared-memory file, if no other instances of that
3825 ** file are currently open, in this process or in other processes, then
3826 ** the file must be truncated to zero length or have its header cleared.
3828 ** If the original database file (pDbFd) is using the "unix-excl" VFS
3829 ** that means that an exclusive lock is held on the database file and
3830 ** that no other processes are able to read or write the database. In
3831 ** that case, we do not really need shared memory. No shared memory
3832 ** file is created. The shared memory will be simulated with heap memory.
3834 static int unixOpenSharedMemory(unixFile
*pDbFd
){
3835 struct unixShm
*p
= 0; /* The connection to be opened */
3836 struct unixShmNode
*pShmNode
; /* The underlying mmapped file */
3837 int rc
; /* Result code */
3838 unixInodeInfo
*pInode
; /* The inode of fd */
3839 char *zShmFilename
; /* Name of the file used for SHM */
3840 int nShmFilename
; /* Size of the SHM filename in bytes */
3842 /* Allocate space for the new unixShm object. */
3843 p
= sqlite3_malloc( sizeof(*p
) );
3844 if( p
==0 ) return SQLITE_NOMEM
;
3845 memset(p
, 0, sizeof(*p
));
3846 assert( pDbFd
->pShm
==0 );
3848 /* Check to see if a unixShmNode object already exists. Reuse an existing
3849 ** one if present. Create a new one if necessary.
3852 pInode
= pDbFd
->pInode
;
3853 pShmNode
= pInode
->pShmNode
;
3855 struct stat sStat
; /* fstat() info for database file */
3857 /* Call fstat() to figure out the permissions on the database file. If
3858 ** a new *-shm file is created, an attempt will be made to create it
3859 ** with the same permissions. The actual permissions the file is created
3860 ** with are subject to the current umask setting.
3862 if( osFstat(pDbFd
->h
, &sStat
) && pInode
->bProcessLock
==0 ){
3863 rc
= SQLITE_IOERR_FSTAT
;
3867 #ifdef SQLITE_SHM_DIRECTORY
3868 nShmFilename
= sizeof(SQLITE_SHM_DIRECTORY
) + 31;
3870 nShmFilename
= 6 + (int)strlen(pDbFd
->zPath
);
3872 pShmNode
= sqlite3_malloc( sizeof(*pShmNode
) + nShmFilename
);
3877 memset(pShmNode
, 0, sizeof(*pShmNode
)+nShmFilename
);
3878 zShmFilename
= pShmNode
->zFilename
= (char*)&pShmNode
[1];
3879 #ifdef SQLITE_SHM_DIRECTORY
3880 sqlite3_snprintf(nShmFilename
, zShmFilename
,
3881 SQLITE_SHM_DIRECTORY
"/sqlite-shm-%x-%x",
3882 (u32
)sStat
.st_ino
, (u32
)sStat
.st_dev
);
3884 sqlite3_snprintf(nShmFilename
, zShmFilename
, "%s-shm", pDbFd
->zPath
);
3885 sqlite3FileSuffix3(pDbFd
->zPath
, zShmFilename
);
3888 pDbFd
->pInode
->pShmNode
= pShmNode
;
3889 pShmNode
->pInode
= pDbFd
->pInode
;
3890 pShmNode
->mutex
= sqlite3_mutex_alloc(SQLITE_MUTEX_FAST
);
3891 if( pShmNode
->mutex
==0 ){
3896 if( pInode
->bProcessLock
==0 ){
3897 int openFlags
= O_RDWR
| O_CREAT
;
3898 if( sqlite3_uri_boolean(pDbFd
->zPath
, "readonly_shm", 0) ){
3899 openFlags
= O_RDONLY
;
3900 pShmNode
->isReadonly
= 1;
3902 pShmNode
->h
= robust_open(zShmFilename
, openFlags
, (sStat
.st_mode
&0777));
3903 if( pShmNode
->h
<0 ){
3904 if( pShmNode
->h
<0 ){
3905 rc
= unixLogError(SQLITE_CANTOPEN_BKPT
, "open", zShmFilename
);
3910 /* Check to see if another process is holding the dead-man switch.
3911 ** If not, truncate the file to zero length.
3914 if( unixShmSystemLock(pShmNode
, F_WRLCK
, UNIX_SHM_DMS
, 1)==SQLITE_OK
){
3915 if( robust_ftruncate(pShmNode
->h
, 0) ){
3916 rc
= unixLogError(SQLITE_IOERR_SHMOPEN
, "ftruncate", zShmFilename
);
3919 if( rc
==SQLITE_OK
){
3920 rc
= unixShmSystemLock(pShmNode
, F_RDLCK
, UNIX_SHM_DMS
, 1);
3922 if( rc
) goto shm_open_err
;
3926 /* Make the new connection a child of the unixShmNode */
3927 p
->pShmNode
= pShmNode
;
3929 p
->id
= pShmNode
->nextShmId
++;
3935 /* The reference count on pShmNode has already been incremented under
3936 ** the cover of the unixEnterMutex() mutex and the pointer from the
3937 ** new (struct unixShm) object to the pShmNode has been set. All that is
3938 ** left to do is to link the new object into the linked list starting
3939 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
3942 sqlite3_mutex_enter(pShmNode
->mutex
);
3943 p
->pNext
= pShmNode
->pFirst
;
3944 pShmNode
->pFirst
= p
;
3945 sqlite3_mutex_leave(pShmNode
->mutex
);
3948 /* Jump here on any error */
3950 unixShmPurge(pDbFd
); /* This call frees pShmNode if required */
3957 ** This function is called to obtain a pointer to region iRegion of the
3958 ** shared-memory associated with the database file fd. Shared-memory regions
3959 ** are numbered starting from zero. Each shared-memory region is szRegion
3962 ** If an error occurs, an error code is returned and *pp is set to NULL.
3964 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
3965 ** region has not been allocated (by any client, including one running in a
3966 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
3967 ** bExtend is non-zero and the requested shared-memory region has not yet
3968 ** been allocated, it is allocated by this function.
3970 ** If the shared-memory region has already been allocated or is allocated by
3971 ** this call as described above, then it is mapped into this processes
3972 ** address space (if it is not already), *pp is set to point to the mapped
3973 ** memory and SQLITE_OK returned.
3975 static int unixShmMap(
3976 sqlite3_file
*fd
, /* Handle open on database file */
3977 int iRegion
, /* Region to retrieve */
3978 int szRegion
, /* Size of regions */
3979 int bExtend
, /* True to extend file if necessary */
3980 void volatile **pp
/* OUT: Mapped memory */
3982 unixFile
*pDbFd
= (unixFile
*)fd
;
3984 unixShmNode
*pShmNode
;
3987 /* If the shared-memory file has not yet been opened, open it now. */
3988 if( pDbFd
->pShm
==0 ){
3989 rc
= unixOpenSharedMemory(pDbFd
);
3990 if( rc
!=SQLITE_OK
) return rc
;
3994 pShmNode
= p
->pShmNode
;
3995 sqlite3_mutex_enter(pShmNode
->mutex
);
3996 assert( szRegion
==pShmNode
->szRegion
|| pShmNode
->nRegion
==0 );
3997 assert( pShmNode
->pInode
==pDbFd
->pInode
);
3998 assert( pShmNode
->h
>=0 || pDbFd
->pInode
->bProcessLock
==1 );
3999 assert( pShmNode
->h
<0 || pDbFd
->pInode
->bProcessLock
==0 );
4001 if( pShmNode
->nRegion
<=iRegion
){
4002 char **apNew
; /* New apRegion[] array */
4003 int nByte
= (iRegion
+1)*szRegion
; /* Minimum required file size */
4004 struct stat sStat
; /* Used by fstat() */
4006 pShmNode
->szRegion
= szRegion
;
4008 if( pShmNode
->h
>=0 ){
4009 /* The requested region is not mapped into this processes address space.
4010 ** Check to see if it has been allocated (i.e. if the wal-index file is
4011 ** large enough to contain the requested region).
4013 if( osFstat(pShmNode
->h
, &sStat
) ){
4014 rc
= SQLITE_IOERR_SHMSIZE
;
4018 if( sStat
.st_size
<nByte
){
4019 /* The requested memory region does not exist. If bExtend is set to
4020 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
4022 ** Alternatively, if bExtend is true, use ftruncate() to allocate
4023 ** the requested memory region.
4025 if( !bExtend
) goto shmpage_out
;
4026 if( robust_ftruncate(pShmNode
->h
, nByte
) ){
4027 rc
= unixLogError(SQLITE_IOERR_SHMSIZE
, "ftruncate",
4028 pShmNode
->zFilename
);
4034 /* Map the requested memory region into this processes address space. */
4035 apNew
= (char **)sqlite3_realloc(
4036 pShmNode
->apRegion
, (iRegion
+1)*sizeof(char *)
4039 rc
= SQLITE_IOERR_NOMEM
;
4042 pShmNode
->apRegion
= apNew
;
4043 while(pShmNode
->nRegion
<=iRegion
){
4045 if( pShmNode
->h
>=0 ){
4046 pMem
= mmap(0, szRegion
,
4047 pShmNode
->isReadonly
? PROT_READ
: PROT_READ
|PROT_WRITE
,
4048 MAP_SHARED
, pShmNode
->h
, pShmNode
->nRegion
*szRegion
4050 if( pMem
==MAP_FAILED
){
4051 rc
= unixLogError(SQLITE_IOERR_SHMMAP
, "mmap", pShmNode
->zFilename
);
4055 pMem
= sqlite3_malloc(szRegion
);
4060 memset(pMem
, 0, szRegion
);
4062 pShmNode
->apRegion
[pShmNode
->nRegion
] = pMem
;
4063 pShmNode
->nRegion
++;
4068 if( pShmNode
->nRegion
>iRegion
){
4069 *pp
= pShmNode
->apRegion
[iRegion
];
4073 if( pShmNode
->isReadonly
&& rc
==SQLITE_OK
) rc
= SQLITE_READONLY
;
4074 sqlite3_mutex_leave(pShmNode
->mutex
);
4079 ** Change the lock state for a shared-memory segment.
4081 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
4082 ** different here than in posix. In xShmLock(), one can go from unlocked
4083 ** to shared and back or from unlocked to exclusive and back. But one may
4084 ** not go from shared to exclusive or from exclusive to shared.
4086 static int unixShmLock(
4087 sqlite3_file
*fd
, /* Database file holding the shared memory */
4088 int ofst
, /* First lock to acquire or release */
4089 int n
, /* Number of locks to acquire or release */
4090 int flags
/* What to do with the lock */
4092 unixFile
*pDbFd
= (unixFile
*)fd
; /* Connection holding shared memory */
4093 unixShm
*p
= pDbFd
->pShm
; /* The shared memory being locked */
4094 unixShm
*pX
; /* For looping over all siblings */
4095 unixShmNode
*pShmNode
= p
->pShmNode
; /* The underlying file iNode */
4096 int rc
= SQLITE_OK
; /* Result code */
4097 u16 mask
; /* Mask of locks to take or release */
4099 assert( pShmNode
==pDbFd
->pInode
->pShmNode
);
4100 assert( pShmNode
->pInode
==pDbFd
->pInode
);
4101 assert( ofst
>=0 && ofst
+n
<=SQLITE_SHM_NLOCK
);
4103 assert( flags
==(SQLITE_SHM_LOCK
| SQLITE_SHM_SHARED
)
4104 || flags
==(SQLITE_SHM_LOCK
| SQLITE_SHM_EXCLUSIVE
)
4105 || flags
==(SQLITE_SHM_UNLOCK
| SQLITE_SHM_SHARED
)
4106 || flags
==(SQLITE_SHM_UNLOCK
| SQLITE_SHM_EXCLUSIVE
) );
4107 assert( n
==1 || (flags
& SQLITE_SHM_EXCLUSIVE
)!=0 );
4108 assert( pShmNode
->h
>=0 || pDbFd
->pInode
->bProcessLock
==1 );
4109 assert( pShmNode
->h
<0 || pDbFd
->pInode
->bProcessLock
==0 );
4111 mask
= (1<<(ofst
+n
)) - (1<<ofst
);
4112 assert( n
>1 || mask
==(1<<ofst
) );
4113 sqlite3_mutex_enter(pShmNode
->mutex
);
4114 if( flags
& SQLITE_SHM_UNLOCK
){
4115 u16 allMask
= 0; /* Mask of locks held by siblings */
4117 /* See if any siblings hold this same lock */
4118 for(pX
=pShmNode
->pFirst
; pX
; pX
=pX
->pNext
){
4119 if( pX
==p
) continue;
4120 assert( (pX
->exclMask
& (p
->exclMask
|p
->sharedMask
))==0 );
4121 allMask
|= pX
->sharedMask
;
4124 /* Unlock the system-level locks */
4125 if( (mask
& allMask
)==0 ){
4126 rc
= unixShmSystemLock(pShmNode
, F_UNLCK
, ofst
+UNIX_SHM_BASE
, n
);
4131 /* Undo the local locks */
4132 if( rc
==SQLITE_OK
){
4133 p
->exclMask
&= ~mask
;
4134 p
->sharedMask
&= ~mask
;
4136 }else if( flags
& SQLITE_SHM_SHARED
){
4137 u16 allShared
= 0; /* Union of locks held by connections other than "p" */
4139 /* Find out which shared locks are already held by sibling connections.
4140 ** If any sibling already holds an exclusive lock, go ahead and return
4143 for(pX
=pShmNode
->pFirst
; pX
; pX
=pX
->pNext
){
4144 if( (pX
->exclMask
& mask
)!=0 ){
4148 allShared
|= pX
->sharedMask
;
4151 /* Get shared locks at the system level, if necessary */
4152 if( rc
==SQLITE_OK
){
4153 if( (allShared
& mask
)==0 ){
4154 rc
= unixShmSystemLock(pShmNode
, F_RDLCK
, ofst
+UNIX_SHM_BASE
, n
);
4160 /* Get the local shared locks */
4161 if( rc
==SQLITE_OK
){
4162 p
->sharedMask
|= mask
;
4165 /* Make sure no sibling connections hold locks that will block this
4166 ** lock. If any do, return SQLITE_BUSY right away.
4168 for(pX
=pShmNode
->pFirst
; pX
; pX
=pX
->pNext
){
4169 if( (pX
->exclMask
& mask
)!=0 || (pX
->sharedMask
& mask
)!=0 ){
4175 /* Get the exclusive locks at the system level. Then if successful
4176 ** also mark the local connection as being locked.
4178 if( rc
==SQLITE_OK
){
4179 rc
= unixShmSystemLock(pShmNode
, F_WRLCK
, ofst
+UNIX_SHM_BASE
, n
);
4180 if( rc
==SQLITE_OK
){
4181 assert( (p
->sharedMask
& mask
)==0 );
4182 p
->exclMask
|= mask
;
4186 sqlite3_mutex_leave(pShmNode
->mutex
);
4187 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
4188 p
->id
, getpid(), p
->sharedMask
, p
->exclMask
));
4193 ** Implement a memory barrier or memory fence on shared memory.
4195 ** All loads and stores begun before the barrier must complete before
4196 ** any load or store begun after the barrier.
4198 static void unixShmBarrier(
4199 sqlite3_file
*fd
/* Database file holding the shared memory */
4201 UNUSED_PARAMETER(fd
);
4207 ** Close a connection to shared-memory. Delete the underlying
4208 ** storage if deleteFlag is true.
4210 ** If there is no shared memory associated with the connection then this
4211 ** routine is a harmless no-op.
4213 static int unixShmUnmap(
4214 sqlite3_file
*fd
, /* The underlying database file */
4215 int deleteFlag
/* Delete shared-memory if true */
4217 unixShm
*p
; /* The connection to be closed */
4218 unixShmNode
*pShmNode
; /* The underlying shared-memory file */
4219 unixShm
**pp
; /* For looping over sibling connections */
4220 unixFile
*pDbFd
; /* The underlying database file */
4222 pDbFd
= (unixFile
*)fd
;
4224 if( p
==0 ) return SQLITE_OK
;
4225 pShmNode
= p
->pShmNode
;
4227 assert( pShmNode
==pDbFd
->pInode
->pShmNode
);
4228 assert( pShmNode
->pInode
==pDbFd
->pInode
);
4230 /* Remove connection p from the set of connections associated
4232 sqlite3_mutex_enter(pShmNode
->mutex
);
4233 for(pp
=&pShmNode
->pFirst
; (*pp
)!=p
; pp
= &(*pp
)->pNext
){}
4236 /* Free the connection p */
4239 sqlite3_mutex_leave(pShmNode
->mutex
);
4241 /* If pShmNode->nRef has reached 0, then close the underlying
4242 ** shared-memory file, too */
4244 assert( pShmNode
->nRef
>0 );
4246 if( pShmNode
->nRef
==0 ){
4247 if( deleteFlag
&& pShmNode
->h
>=0 ) osUnlink(pShmNode
->zFilename
);
4248 unixShmPurge(pDbFd
);
4257 # define unixShmMap 0
4258 # define unixShmLock 0
4259 # define unixShmBarrier 0
4260 # define unixShmUnmap 0
4261 #endif /* #ifndef SQLITE_OMIT_WAL */
4264 ** Here ends the implementation of all sqlite3_file methods.
4266 ********************** End sqlite3_file Methods *******************************
4267 ******************************************************************************/
4270 ** This division contains definitions of sqlite3_io_methods objects that
4271 ** implement various file locking strategies. It also contains definitions
4272 ** of "finder" functions. A finder-function is used to locate the appropriate
4273 ** sqlite3_io_methods object for a particular database file. The pAppData
4274 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
4275 ** the correct finder-function for that VFS.
4277 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
4278 ** object. The only interesting finder-function is autolockIoFinder, which
4279 ** looks at the filesystem type and tries to guess the best locking
4280 ** strategy from that.
4282 ** For finder-funtion F, two objects are created:
4284 ** (1) The real finder-function named "FImpt()".
4286 ** (2) A constant pointer to this function named just "F".
4289 ** A pointer to the F pointer is used as the pAppData value for VFS
4290 ** objects. We have to do this instead of letting pAppData point
4291 ** directly at the finder-function since C90 rules prevent a void*
4292 ** from be cast into a function pointer.
4295 ** Each instance of this macro generates two objects:
4297 ** * A constant sqlite3_io_methods object call METHOD that has locking
4298 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
4300 ** * An I/O method finder function called FINDER that returns a pointer
4301 ** to the METHOD object in the previous bullet.
4303 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \
4304 static const sqlite3_io_methods METHOD = { \
4305 VERSION, /* iVersion */ \
4306 CLOSE, /* xClose */ \
4307 unixRead, /* xRead */ \
4308 unixWrite, /* xWrite */ \
4309 unixTruncate, /* xTruncate */ \
4310 unixSync, /* xSync */ \
4311 unixFileSize, /* xFileSize */ \
4313 UNLOCK, /* xUnlock */ \
4314 CKLOCK, /* xCheckReservedLock */ \
4315 unixFileControl, /* xFileControl */ \
4316 unixSectorSize, /* xSectorSize */ \
4317 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
4318 unixShmMap, /* xShmMap */ \
4319 unixShmLock, /* xShmLock */ \
4320 unixShmBarrier, /* xShmBarrier */ \
4321 unixShmUnmap /* xShmUnmap */ \
4323 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
4324 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
4327 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
4331 ** Here are all of the sqlite3_io_methods objects for each of the
4332 ** locking strategies. Functions that return pointers to these methods
4333 ** are also created.
4336 posixIoFinder
, /* Finder function name */
4337 posixIoMethods
, /* sqlite3_io_methods object name */
4338 2, /* shared memory is enabled */
4339 unixClose
, /* xClose method */
4340 unixLock
, /* xLock method */
4341 unixUnlock
, /* xUnlock method */
4342 unixCheckReservedLock
/* xCheckReservedLock method */
4345 nolockIoFinder
, /* Finder function name */
4346 nolockIoMethods
, /* sqlite3_io_methods object name */
4347 1, /* shared memory is disabled */
4348 nolockClose
, /* xClose method */
4349 nolockLock
, /* xLock method */
4350 nolockUnlock
, /* xUnlock method */
4351 nolockCheckReservedLock
/* xCheckReservedLock method */
4354 dotlockIoFinder
, /* Finder function name */
4355 dotlockIoMethods
, /* sqlite3_io_methods object name */
4356 1, /* shared memory is disabled */
4357 dotlockClose
, /* xClose method */
4358 dotlockLock
, /* xLock method */
4359 dotlockUnlock
, /* xUnlock method */
4360 dotlockCheckReservedLock
/* xCheckReservedLock method */
4363 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
4365 flockIoFinder
, /* Finder function name */
4366 flockIoMethods
, /* sqlite3_io_methods object name */
4367 1, /* shared memory is disabled */
4368 flockClose
, /* xClose method */
4369 flockLock
, /* xLock method */
4370 flockUnlock
, /* xUnlock method */
4371 flockCheckReservedLock
/* xCheckReservedLock method */
4377 semIoFinder
, /* Finder function name */
4378 semIoMethods
, /* sqlite3_io_methods object name */
4379 1, /* shared memory is disabled */
4380 semClose
, /* xClose method */
4381 semLock
, /* xLock method */
4382 semUnlock
, /* xUnlock method */
4383 semCheckReservedLock
/* xCheckReservedLock method */
4387 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4389 afpIoFinder
, /* Finder function name */
4390 afpIoMethods
, /* sqlite3_io_methods object name */
4391 1, /* shared memory is disabled */
4392 afpClose
, /* xClose method */
4393 afpLock
, /* xLock method */
4394 afpUnlock
, /* xUnlock method */
4395 afpCheckReservedLock
/* xCheckReservedLock method */
4400 ** The proxy locking method is a "super-method" in the sense that it
4401 ** opens secondary file descriptors for the conch and lock files and
4402 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
4403 ** secondary files. For this reason, the division that implements
4404 ** proxy locking is located much further down in the file. But we need
4405 ** to go ahead and define the sqlite3_io_methods and finder function
4406 ** for proxy locking here. So we forward declare the I/O methods.
4408 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4409 static int proxyClose(sqlite3_file
*);
4410 static int proxyLock(sqlite3_file
*, int);
4411 static int proxyUnlock(sqlite3_file
*, int);
4412 static int proxyCheckReservedLock(sqlite3_file
*, int*);
4414 proxyIoFinder
, /* Finder function name */
4415 proxyIoMethods
, /* sqlite3_io_methods object name */
4416 1, /* shared memory is disabled */
4417 proxyClose
, /* xClose method */
4418 proxyLock
, /* xLock method */
4419 proxyUnlock
, /* xUnlock method */
4420 proxyCheckReservedLock
/* xCheckReservedLock method */
4424 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
4425 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4427 nfsIoFinder
, /* Finder function name */
4428 nfsIoMethods
, /* sqlite3_io_methods object name */
4429 1, /* shared memory is disabled */
4430 unixClose
, /* xClose method */
4431 unixLock
, /* xLock method */
4432 nfsUnlock
, /* xUnlock method */
4433 unixCheckReservedLock
/* xCheckReservedLock method */
4437 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4439 ** This "finder" function attempts to determine the best locking strategy
4440 ** for the database file "filePath". It then returns the sqlite3_io_methods
4441 ** object that implements that strategy.
4443 ** This is for MacOSX only.
4445 static const sqlite3_io_methods
*autolockIoFinderImpl(
4446 const char *filePath
, /* name of the database file */
4447 unixFile
*pNew
/* open file object for the database file */
4449 static const struct Mapping
{
4450 const char *zFilesystem
; /* Filesystem type name */
4451 const sqlite3_io_methods
*pMethods
; /* Appropriate locking method */
4453 { "hfs", &posixIoMethods
},
4454 { "ufs", &posixIoMethods
},
4455 { "afpfs", &afpIoMethods
},
4456 { "smbfs", &afpIoMethods
},
4457 { "webdav", &nolockIoMethods
},
4461 struct statfs fsInfo
;
4462 struct flock lockInfo
;
4465 /* If filePath==NULL that means we are dealing with a transient file
4466 ** that does not need to be locked. */
4467 return &nolockIoMethods
;
4469 if( statfs(filePath
, &fsInfo
) != -1 ){
4470 if( fsInfo
.f_flags
& MNT_RDONLY
){
4471 return &nolockIoMethods
;
4473 for(i
=0; aMap
[i
].zFilesystem
; i
++){
4474 if( strcmp(fsInfo
.f_fstypename
, aMap
[i
].zFilesystem
)==0 ){
4475 return aMap
[i
].pMethods
;
4480 /* Default case. Handles, amongst others, "nfs".
4481 ** Test byte-range lock using fcntl(). If the call succeeds,
4482 ** assume that the file-system supports POSIX style locks.
4485 lockInfo
.l_start
= 0;
4486 lockInfo
.l_whence
= SEEK_SET
;
4487 lockInfo
.l_type
= F_RDLCK
;
4488 if( osFcntl(pNew
->h
, F_GETLK
, &lockInfo
)!=-1 ) {
4489 if( strcmp(fsInfo
.f_fstypename
, "nfs")==0 ){
4490 return &nfsIoMethods
;
4492 return &posixIoMethods
;
4495 return &dotlockIoMethods
;
4498 static const sqlite3_io_methods
4499 *(*const autolockIoFinder
)(const char*,unixFile
*) = autolockIoFinderImpl
;
4501 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
4503 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
4505 ** This "finder" function attempts to determine the best locking strategy
4506 ** for the database file "filePath". It then returns the sqlite3_io_methods
4507 ** object that implements that strategy.
4509 ** This is for VXWorks only.
4511 static const sqlite3_io_methods
*autolockIoFinderImpl(
4512 const char *filePath
, /* name of the database file */
4513 unixFile
*pNew
/* the open file object */
4515 struct flock lockInfo
;
4518 /* If filePath==NULL that means we are dealing with a transient file
4519 ** that does not need to be locked. */
4520 return &nolockIoMethods
;
4523 /* Test if fcntl() is supported and use POSIX style locks.
4524 ** Otherwise fall back to the named semaphore method.
4527 lockInfo
.l_start
= 0;
4528 lockInfo
.l_whence
= SEEK_SET
;
4529 lockInfo
.l_type
= F_RDLCK
;
4530 if( osFcntl(pNew
->h
, F_GETLK
, &lockInfo
)!=-1 ) {
4531 return &posixIoMethods
;
4533 return &semIoMethods
;
4536 static const sqlite3_io_methods
4537 *(*const autolockIoFinder
)(const char*,unixFile
*) = autolockIoFinderImpl
;
4539 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
4542 ** An abstract type for a pointer to a IO method finder function:
4544 typedef const sqlite3_io_methods
*(*finder_type
)(const char*,unixFile
*);
4547 /****************************************************************************
4548 **************************** sqlite3_vfs methods ****************************
4550 ** This division contains the implementation of methods on the
4551 ** sqlite3_vfs object.
4555 ** Initialize the contents of the unixFile structure pointed to by pId.
4557 static int fillInUnixFile(
4558 sqlite3_vfs
*pVfs
, /* Pointer to vfs object */
4559 int h
, /* Open file descriptor of file being opened */
4560 sqlite3_file
*pId
, /* Write to the unixFile structure here */
4561 const char *zFilename
, /* Name of the file being opened */
4562 int ctrlFlags
/* Zero or more UNIXFILE_* values */
4564 const sqlite3_io_methods
*pLockingStyle
;
4565 unixFile
*pNew
= (unixFile
*)pId
;
4568 assert( pNew
->pInode
==NULL
);
4570 /* Usually the path zFilename should not be a relative pathname. The
4571 ** exception is when opening the proxy "conch" file in builds that
4572 ** include the special Apple locking styles.
4574 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4575 assert( zFilename
==0 || zFilename
[0]=='/'
4576 || pVfs
->pAppData
==(void*)&autolockIoFinder
);
4578 assert( zFilename
==0 || zFilename
[0]=='/' );
4581 /* No locking occurs in temporary files */
4582 assert( zFilename
!=0 || (ctrlFlags
& UNIXFILE_NOLOCK
)!=0 );
4584 OSTRACE(("OPEN %-3d %s\n", h
, zFilename
));
4587 pNew
->zPath
= zFilename
;
4588 pNew
->ctrlFlags
= (u8
)ctrlFlags
;
4589 if( sqlite3_uri_boolean(((ctrlFlags
& UNIXFILE_URI
) ? zFilename
: 0),
4590 "psow", SQLITE_POWERSAFE_OVERWRITE
) ){
4591 pNew
->ctrlFlags
|= UNIXFILE_PSOW
;
4593 if( memcmp(pVfs
->zName
,"unix-excl",10)==0 ){
4594 pNew
->ctrlFlags
|= UNIXFILE_EXCL
;
4598 pNew
->pId
= vxworksFindFileId(zFilename
);
4600 ctrlFlags
|= UNIXFILE_NOLOCK
;
4605 if( ctrlFlags
& UNIXFILE_NOLOCK
){
4606 pLockingStyle
= &nolockIoMethods
;
4608 pLockingStyle
= (**(finder_type
*)pVfs
->pAppData
)(zFilename
, pNew
);
4609 #if SQLITE_ENABLE_LOCKING_STYLE
4610 /* Cache zFilename in the locking context (AFP and dotlock override) for
4611 ** proxyLock activation is possible (remote proxy is based on db name)
4612 ** zFilename remains valid until file is closed, to support */
4613 pNew
->lockingContext
= (void*)zFilename
;
4617 if( pLockingStyle
== &posixIoMethods
4618 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4619 || pLockingStyle
== &nfsIoMethods
4623 rc
= findInodeInfo(pNew
, &pNew
->pInode
);
4624 if( rc
!=SQLITE_OK
){
4625 /* If an error occured in findInodeInfo(), close the file descriptor
4626 ** immediately, before releasing the mutex. findInodeInfo() may fail
4627 ** in two scenarios:
4629 ** (a) A call to fstat() failed.
4630 ** (b) A malloc failed.
4632 ** Scenario (b) may only occur if the process is holding no other
4633 ** file descriptors open on the same file. If there were other file
4634 ** descriptors on this file, then no malloc would be required by
4635 ** findInodeInfo(). If this is the case, it is quite safe to close
4636 ** handle h - as it is guaranteed that no posix locks will be released
4639 ** If scenario (a) caused the error then things are not so safe. The
4640 ** implicit assumption here is that if fstat() fails, things are in
4641 ** such bad shape that dropping a lock or two doesn't matter much.
4643 robust_close(pNew
, h
, __LINE__
);
4649 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
4650 else if( pLockingStyle
== &afpIoMethods
){
4651 /* AFP locking uses the file path so it needs to be included in
4652 ** the afpLockingContext.
4654 afpLockingContext
*pCtx
;
4655 pNew
->lockingContext
= pCtx
= sqlite3_malloc( sizeof(*pCtx
) );
4659 /* NB: zFilename exists and remains valid until the file is closed
4660 ** according to requirement F11141. So we do not need to make a
4661 ** copy of the filename. */
4662 pCtx
->dbPath
= zFilename
;
4666 rc
= findInodeInfo(pNew
, &pNew
->pInode
);
4667 if( rc
!=SQLITE_OK
){
4668 sqlite3_free(pNew
->lockingContext
);
4669 robust_close(pNew
, h
, __LINE__
);
4677 else if( pLockingStyle
== &dotlockIoMethods
){
4678 /* Dotfile locking uses the file path so it needs to be included in
4679 ** the dotlockLockingContext
4683 assert( zFilename
!=0 );
4684 nFilename
= (int)strlen(zFilename
) + 6;
4685 zLockFile
= (char *)sqlite3_malloc(nFilename
);
4689 sqlite3_snprintf(nFilename
, zLockFile
, "%s" DOTLOCK_SUFFIX
, zFilename
);
4691 pNew
->lockingContext
= zLockFile
;
4695 else if( pLockingStyle
== &semIoMethods
){
4696 /* Named semaphore locking uses the file path so it needs to be
4697 ** included in the semLockingContext
4700 rc
= findInodeInfo(pNew
, &pNew
->pInode
);
4701 if( (rc
==SQLITE_OK
) && (pNew
->pInode
->pSem
==NULL
) ){
4702 char *zSemName
= pNew
->pInode
->aSemName
;
4704 sqlite3_snprintf(MAX_PATHNAME
, zSemName
, "/%s.sem",
4705 pNew
->pId
->zCanonicalName
);
4706 for( n
=1; zSemName
[n
]; n
++ )
4707 if( zSemName
[n
]=='/' ) zSemName
[n
] = '_';
4708 pNew
->pInode
->pSem
= sem_open(zSemName
, O_CREAT
, 0666, 1);
4709 if( pNew
->pInode
->pSem
== SEM_FAILED
){
4711 pNew
->pInode
->aSemName
[0] = '\0';
4718 pNew
->lastErrno
= 0;
4720 if( rc
!=SQLITE_OK
){
4721 if( h
>=0 ) robust_close(pNew
, h
, __LINE__
);
4723 osUnlink(zFilename
);
4726 if( isDelete
) pNew
->ctrlFlags
|= UNIXFILE_DELETE
;
4728 if( rc
!=SQLITE_OK
){
4729 if( h
>=0 ) robust_close(pNew
, h
, __LINE__
);
4731 pNew
->pMethod
= pLockingStyle
;
4738 ** Return the name of a directory in which to put temporary files.
4739 ** If no suitable temporary file directory can be found, return NULL.
4741 static const char *unixTempFileDir(void){
4742 static const char *azDirs
[] = {
4748 0 /* List terminator */
4752 const char *zDir
= 0;
4754 azDirs
[0] = sqlite3_temp_directory
;
4755 if( !azDirs
[1] ) azDirs
[1] = getenv("TMPDIR");
4756 for(i
=0; i
<sizeof(azDirs
)/sizeof(azDirs
[0]); zDir
=azDirs
[i
++]){
4757 if( zDir
==0 ) continue;
4758 if( osStat(zDir
, &buf
) ) continue;
4759 if( !S_ISDIR(buf
.st_mode
) ) continue;
4760 if( osAccess(zDir
, 07) ) continue;
4767 ** Create a temporary file name in zBuf. zBuf must be allocated
4768 ** by the calling process and must be big enough to hold at least
4769 ** pVfs->mxPathname bytes.
4771 static int unixGetTempname(int nBuf
, char *zBuf
){
4772 static const unsigned char zChars
[] =
4773 "abcdefghijklmnopqrstuvwxyz"
4774 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
4779 /* It's odd to simulate an io-error here, but really this is just
4780 ** using the io-error infrastructure to test that SQLite handles this
4781 ** function failing.
4783 SimulateIOError( return SQLITE_IOERR
);
4785 zDir
= unixTempFileDir();
4786 if( zDir
==0 ) zDir
= ".";
4788 /* Check that the output buffer is large enough for the temporary file
4789 ** name. If it is not, return SQLITE_ERROR.
4791 if( (strlen(zDir
) + strlen(SQLITE_TEMP_FILE_PREFIX
) + 18) >= (size_t)nBuf
){
4792 return SQLITE_ERROR
;
4796 sqlite3_snprintf(nBuf
-18, zBuf
, "%s/"SQLITE_TEMP_FILE_PREFIX
, zDir
);
4797 j
= (int)strlen(zBuf
);
4798 sqlite3_randomness(15, &zBuf
[j
]);
4799 for(i
=0; i
<15; i
++, j
++){
4800 zBuf
[j
] = (char)zChars
[ ((unsigned char)zBuf
[j
])%(sizeof(zChars
)-1) ];
4804 }while( osAccess(zBuf
,0)==0 );
4808 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
4810 ** Routine to transform a unixFile into a proxy-locking unixFile.
4811 ** Implementation in the proxy-lock division, but used by unixOpen()
4812 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
4814 static int proxyTransformUnixFile(unixFile
*, const char*);
4818 ** Search for an unused file descriptor that was opened on the database
4819 ** file (not a journal or master-journal file) identified by pathname
4820 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
4821 ** argument to this function.
4823 ** Such a file descriptor may exist if a database connection was closed
4824 ** but the associated file descriptor could not be closed because some
4825 ** other file descriptor open on the same file is holding a file-lock.
4826 ** Refer to comments in the unixClose() function and the lengthy comment
4827 ** describing "Posix Advisory Locking" at the start of this file for
4828 ** further details. Also, ticket #4018.
4830 ** If a suitable file descriptor is found, then it is returned. If no
4831 ** such file descriptor is located, -1 is returned.
4833 static UnixUnusedFd
*findReusableFd(const char *zPath
, int flags
){
4834 UnixUnusedFd
*pUnused
= 0;
4836 /* Do not search for an unused file descriptor on vxworks. Not because
4837 ** vxworks would not benefit from the change (it might, we're not sure),
4838 ** but because no way to test it is currently available. It is better
4839 ** not to risk breaking vxworks support for the sake of such an obscure
4842 struct stat sStat
; /* Results of stat() call */
4844 /* A stat() call may fail for various reasons. If this happens, it is
4845 ** almost certain that an open() call on the same path will also fail.
4846 ** For this reason, if an error occurs in the stat() call here, it is
4847 ** ignored and -1 is returned. The caller will try to open a new file
4848 ** descriptor on the same path, fail, and return an error to SQLite.
4850 ** Even if a subsequent open() call does succeed, the consequences of
4851 ** not searching for a resusable file descriptor are not dire. */
4852 if( 0==osStat(zPath
, &sStat
) ){
4853 unixInodeInfo
*pInode
;
4857 while( pInode
&& (pInode
->fileId
.dev
!=sStat
.st_dev
4858 || pInode
->fileId
.ino
!=sStat
.st_ino
) ){
4859 pInode
= pInode
->pNext
;
4863 for(pp
=&pInode
->pUnused
; *pp
&& (*pp
)->flags
!=flags
; pp
=&((*pp
)->pNext
));
4866 *pp
= pUnused
->pNext
;
4871 #endif /* if !OS_VXWORKS */
4876 ** This function is called by unixOpen() to determine the unix permissions
4877 ** to create new files with. If no error occurs, then SQLITE_OK is returned
4878 ** and a value suitable for passing as the third argument to open(2) is
4879 ** written to *pMode. If an IO error occurs, an SQLite error code is
4880 ** returned and the value of *pMode is not modified.
4882 ** If the file being opened is a temporary file, it is always created with
4883 ** the octal permissions 0600 (read/writable by owner only). If the file
4884 ** is a database or master journal file, it is created with the permissions
4885 ** mask SQLITE_DEFAULT_FILE_PERMISSIONS.
4887 ** Finally, if the file being opened is a WAL or regular journal file, then
4888 ** this function queries the file-system for the permissions on the
4889 ** corresponding database file and sets *pMode to this value. Whenever
4890 ** possible, WAL and journal files are created using the same permissions
4891 ** as the associated database file.
4893 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
4894 ** original filename is unavailable. But 8_3_NAMES is only used for
4895 ** FAT filesystems and permissions do not matter there, so just use
4896 ** the default permissions.
4898 static int findCreateFileMode(
4899 const char *zPath
, /* Path of file (possibly) being created */
4900 int flags
, /* Flags passed as 4th argument to xOpen() */
4901 mode_t
*pMode
/* OUT: Permissions to open file with */
4903 int rc
= SQLITE_OK
; /* Return Code */
4904 *pMode
= SQLITE_DEFAULT_FILE_PERMISSIONS
;
4905 if( flags
& (SQLITE_OPEN_WAL
|SQLITE_OPEN_MAIN_JOURNAL
) ){
4906 char zDb
[MAX_PATHNAME
+1]; /* Database file path */
4907 int nDb
; /* Number of valid bytes in zDb */
4908 struct stat sStat
; /* Output of stat() on database file */
4910 /* zPath is a path to a WAL or journal file. The following block derives
4911 ** the path to the associated database file from zPath. This block handles
4912 ** the following naming conventions:
4914 ** "<path to db>-journal"
4915 ** "<path to db>-wal"
4916 ** "<path to db>-journalNN"
4917 ** "<path to db>-walNN"
4919 ** where NN is a decimal number. The NN naming schemes are
4920 ** used by the test_multiplex.c module.
4922 nDb
= sqlite3Strlen30(zPath
) - 1;
4923 #ifdef SQLITE_ENABLE_8_3_NAMES
4924 while( nDb
>0 && sqlite3Isalnum(zPath
[nDb
]) ) nDb
--;
4925 if( nDb
==0 || zPath
[nDb
]!='-' ) return SQLITE_OK
;
4927 while( zPath
[nDb
]!='-' ){
4929 assert( zPath
[nDb
]!='\n' );
4933 memcpy(zDb
, zPath
, nDb
);
4936 if( 0==osStat(zDb
, &sStat
) ){
4937 *pMode
= sStat
.st_mode
& 0777;
4939 rc
= SQLITE_IOERR_FSTAT
;
4941 }else if( flags
& SQLITE_OPEN_DELETEONCLOSE
){
4948 ** Open the file zPath.
4950 ** Previously, the SQLite OS layer used three functions in place of this
4953 ** sqlite3OsOpenReadWrite();
4954 ** sqlite3OsOpenReadOnly();
4955 ** sqlite3OsOpenExclusive();
4957 ** These calls correspond to the following combinations of flags:
4959 ** ReadWrite() -> (READWRITE | CREATE)
4960 ** ReadOnly() -> (READONLY)
4961 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
4963 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
4964 ** true, the file was configured to be automatically deleted when the
4965 ** file handle closed. To achieve the same effect using this new
4966 ** interface, add the DELETEONCLOSE flag to those specified above for
4969 static int unixOpen(
4970 sqlite3_vfs
*pVfs
, /* The VFS for which this is the xOpen method */
4971 const char *zPath
, /* Pathname of file to be opened */
4972 sqlite3_file
*pFile
, /* The file descriptor to be filled in */
4973 int flags
, /* Input flags to control the opening */
4974 int *pOutFlags
/* Output flags returned to SQLite core */
4976 unixFile
*p
= (unixFile
*)pFile
;
4977 int fd
= -1; /* File descriptor returned by open() */
4978 int openFlags
= 0; /* Flags to pass to open() */
4979 int eType
= flags
&0xFFFFFF00; /* Type of file to open */
4980 int noLock
; /* True to omit locking primitives */
4981 int rc
= SQLITE_OK
; /* Function Return Code */
4982 int ctrlFlags
= 0; /* UNIXFILE_* flags */
4984 int isExclusive
= (flags
& SQLITE_OPEN_EXCLUSIVE
);
4985 int isDelete
= (flags
& SQLITE_OPEN_DELETEONCLOSE
);
4986 int isCreate
= (flags
& SQLITE_OPEN_CREATE
);
4987 int isReadonly
= (flags
& SQLITE_OPEN_READONLY
);
4988 int isReadWrite
= (flags
& SQLITE_OPEN_READWRITE
);
4989 #if SQLITE_ENABLE_LOCKING_STYLE
4990 int isAutoProxy
= (flags
& SQLITE_OPEN_AUTOPROXY
);
4992 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
4993 struct statfs fsInfo
;
4996 /* If creating a master or main-file journal, this function will open
4997 ** a file-descriptor on the directory too. The first time unixSync()
4998 ** is called the directory file descriptor will be fsync()ed and close()d.
5000 int syncDir
= (isCreate
&& (
5001 eType
==SQLITE_OPEN_MASTER_JOURNAL
5002 || eType
==SQLITE_OPEN_MAIN_JOURNAL
5003 || eType
==SQLITE_OPEN_WAL
5006 /* If argument zPath is a NULL pointer, this function is required to open
5007 ** a temporary file. Use this buffer to store the file name in.
5009 char zTmpname
[MAX_PATHNAME
+2];
5010 const char *zName
= zPath
;
5012 /* Check the following statements are true:
5014 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
5015 ** (b) if CREATE is set, then READWRITE must also be set, and
5016 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
5017 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
5019 assert((isReadonly
==0 || isReadWrite
==0) && (isReadWrite
|| isReadonly
));
5020 assert(isCreate
==0 || isReadWrite
);
5021 assert(isExclusive
==0 || isCreate
);
5022 assert(isDelete
==0 || isCreate
);
5024 /* The main DB, main journal, WAL file and master journal are never
5025 ** automatically deleted. Nor are they ever temporary files. */
5026 assert( (!isDelete
&& zName
) || eType
!=SQLITE_OPEN_MAIN_DB
);
5027 assert( (!isDelete
&& zName
) || eType
!=SQLITE_OPEN_MAIN_JOURNAL
);
5028 assert( (!isDelete
&& zName
) || eType
!=SQLITE_OPEN_MASTER_JOURNAL
);
5029 assert( (!isDelete
&& zName
) || eType
!=SQLITE_OPEN_WAL
);
5031 /* Assert that the upper layer has set one of the "file-type" flags. */
5032 assert( eType
==SQLITE_OPEN_MAIN_DB
|| eType
==SQLITE_OPEN_TEMP_DB
5033 || eType
==SQLITE_OPEN_MAIN_JOURNAL
|| eType
==SQLITE_OPEN_TEMP_JOURNAL
5034 || eType
==SQLITE_OPEN_SUBJOURNAL
|| eType
==SQLITE_OPEN_MASTER_JOURNAL
5035 || eType
==SQLITE_OPEN_TRANSIENT_DB
|| eType
==SQLITE_OPEN_WAL
5038 memset(p
, 0, sizeof(unixFile
));
5040 if( eType
==SQLITE_OPEN_MAIN_DB
){
5041 UnixUnusedFd
*pUnused
;
5042 pUnused
= findReusableFd(zName
, flags
);
5046 pUnused
= sqlite3_malloc(sizeof(*pUnused
));
5048 return SQLITE_NOMEM
;
5051 p
->pUnused
= pUnused
;
5053 /* Database filenames are double-zero terminated if they are not
5054 ** URIs with parameters. Hence, they can always be passed into
5055 ** sqlite3_uri_parameter(). */
5056 assert( (flags
& SQLITE_OPEN_URI
) || zName
[strlen(zName
)+1]==0 );
5059 /* If zName is NULL, the upper layer is requesting a temp file. */
5060 assert(isDelete
&& !syncDir
);
5061 rc
= unixGetTempname(MAX_PATHNAME
+2, zTmpname
);
5062 if( rc
!=SQLITE_OK
){
5067 /* Generated temporary filenames are always double-zero terminated
5068 ** for use by sqlite3_uri_parameter(). */
5069 assert( zName
[strlen(zName
)+1]==0 );
5072 /* Determine the value of the flags parameter passed to POSIX function
5073 ** open(). These must be calculated even if open() is not called, as
5074 ** they may be stored as part of the file handle and used by the
5075 ** 'conch file' locking functions later on. */
5076 if( isReadonly
) openFlags
|= O_RDONLY
;
5077 if( isReadWrite
) openFlags
|= O_RDWR
;
5078 if( isCreate
) openFlags
|= O_CREAT
;
5079 if( isExclusive
) openFlags
|= (O_EXCL
|O_NOFOLLOW
);
5080 openFlags
|= (O_LARGEFILE
|O_BINARY
);
5083 mode_t openMode
; /* Permissions to create file with */
5084 rc
= findCreateFileMode(zName
, flags
, &openMode
);
5085 if( rc
!=SQLITE_OK
){
5086 assert( !p
->pUnused
);
5087 assert( eType
==SQLITE_OPEN_WAL
|| eType
==SQLITE_OPEN_MAIN_JOURNAL
);
5090 fd
= robust_open(zName
, openFlags
, openMode
);
5091 OSTRACE(("OPENX %-3d %s 0%o\n", fd
, zName
, openFlags
));
5092 if( fd
<0 && errno
!=EISDIR
&& isReadWrite
&& !isExclusive
){
5093 /* Failed to open the file for read/write access. Try read-only. */
5094 flags
&= ~(SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
);
5095 openFlags
&= ~(O_RDWR
|O_CREAT
);
5096 flags
|= SQLITE_OPEN_READONLY
;
5097 openFlags
|= O_RDONLY
;
5099 fd
= robust_open(zName
, openFlags
, openMode
);
5102 rc
= unixLogError(SQLITE_CANTOPEN_BKPT
, "open", zName
);
5112 p
->pUnused
->fd
= fd
;
5113 p
->pUnused
->flags
= flags
;
5123 #if SQLITE_ENABLE_LOCKING_STYLE
5125 p
->openFlags
= openFlags
;
5130 osFcntl(fd
, F_SETFD
, osFcntl(fd
, F_GETFD
, 0) | FD_CLOEXEC
);
5133 noLock
= eType
!=SQLITE_OPEN_MAIN_DB
;
5136 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
5137 if( fstatfs(fd
, &fsInfo
) == -1 ){
5138 ((unixFile
*)pFile
)->lastErrno
= errno
;
5139 robust_close(p
, fd
, __LINE__
);
5140 return SQLITE_IOERR_ACCESS
;
5142 if (0 == strncmp("msdos", fsInfo
.f_fstypename
, 5)) {
5143 ((unixFile
*)pFile
)->fsFlags
|= SQLITE_FSFLAGS_IS_MSDOS
;
5147 /* Set up appropriate ctrlFlags */
5148 if( isDelete
) ctrlFlags
|= UNIXFILE_DELETE
;
5149 if( isReadonly
) ctrlFlags
|= UNIXFILE_RDONLY
;
5150 if( noLock
) ctrlFlags
|= UNIXFILE_NOLOCK
;
5151 if( syncDir
) ctrlFlags
|= UNIXFILE_DIRSYNC
;
5152 if( flags
& SQLITE_OPEN_URI
) ctrlFlags
|= UNIXFILE_URI
;
5154 #if SQLITE_ENABLE_LOCKING_STYLE
5155 #if SQLITE_PREFER_PROXY_LOCKING
5158 if( isAutoProxy
&& (zPath
!=NULL
) && (!noLock
) && pVfs
->xOpen
){
5159 char *envforce
= getenv("SQLITE_FORCE_PROXY_LOCKING");
5162 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
5163 ** never use proxy, NULL means use proxy for non-local files only. */
5164 if( envforce
!=NULL
){
5165 useProxy
= atoi(envforce
)>0;
5167 if( statfs(zPath
, &fsInfo
) == -1 ){
5168 /* In theory, the close(fd) call is sub-optimal. If the file opened
5169 ** with fd is a database file, and there are other connections open
5170 ** on that file that are currently holding advisory locks on it,
5171 ** then the call to close() will cancel those locks. In practice,
5172 ** we're assuming that statfs() doesn't fail very often. At least
5173 ** not while other file descriptors opened by the same process on
5174 ** the same file are working. */
5175 p
->lastErrno
= errno
;
5176 robust_close(p
, fd
, __LINE__
);
5177 rc
= SQLITE_IOERR_ACCESS
;
5180 useProxy
= !(fsInfo
.f_flags
&MNT_LOCAL
);
5183 rc
= fillInUnixFile(pVfs
, fd
, pFile
, zPath
, ctrlFlags
);
5184 if( rc
==SQLITE_OK
){
5185 rc
= proxyTransformUnixFile((unixFile
*)pFile
, ":auto:");
5186 if( rc
!=SQLITE_OK
){
5187 /* Use unixClose to clean up the resources added in fillInUnixFile
5188 ** and clear all the structure's references. Specifically,
5189 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
5200 rc
= fillInUnixFile(pVfs
, fd
, pFile
, zPath
, ctrlFlags
);
5203 if( rc
!=SQLITE_OK
){
5204 sqlite3_free(p
->pUnused
);
5211 ** Delete the file at zPath. If the dirSync argument is true, fsync()
5212 ** the directory after deleting the file.
5214 static int unixDelete(
5215 sqlite3_vfs
*NotUsed
, /* VFS containing this as the xDelete method */
5216 const char *zPath
, /* Name of file to be deleted */
5217 int dirSync
/* If true, fsync() directory after deleting file */
5220 UNUSED_PARAMETER(NotUsed
);
5221 SimulateIOError(return SQLITE_IOERR_DELETE
);
5222 if( osUnlink(zPath
)==(-1) && errno
!=ENOENT
){
5223 return unixLogError(SQLITE_IOERR_DELETE
, "unlink", zPath
);
5225 #ifndef SQLITE_DISABLE_DIRSYNC
5226 if( (dirSync
& 1)!=0 ){
5228 rc
= osOpenDirectory(zPath
, &fd
);
5229 if( rc
==SQLITE_OK
){
5236 rc
= unixLogError(SQLITE_IOERR_DIR_FSYNC
, "fsync", zPath
);
5238 robust_close(0, fd
, __LINE__
);
5239 }else if( rc
==SQLITE_CANTOPEN
){
5248 ** Test the existance of or access permissions of file zPath. The
5249 ** test performed depends on the value of flags:
5251 ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
5252 ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
5253 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
5255 ** Otherwise return 0.
5257 static int unixAccess(
5258 sqlite3_vfs
*NotUsed
, /* The VFS containing this xAccess method */
5259 const char *zPath
, /* Path of the file to examine */
5260 int flags
, /* What do we want to learn about the zPath file? */
5261 int *pResOut
/* Write result boolean here */
5264 UNUSED_PARAMETER(NotUsed
);
5265 SimulateIOError( return SQLITE_IOERR_ACCESS
; );
5267 case SQLITE_ACCESS_EXISTS
:
5270 case SQLITE_ACCESS_READWRITE
:
5273 case SQLITE_ACCESS_READ
:
5278 assert(!"Invalid flags argument");
5280 *pResOut
= (osAccess(zPath
, amode
)==0);
5281 if( flags
==SQLITE_ACCESS_EXISTS
&& *pResOut
){
5283 if( 0==osStat(zPath
, &buf
) && buf
.st_size
==0 ){
5292 ** Turn a relative pathname into a full pathname. The relative path
5293 ** is stored as a nul-terminated string in the buffer pointed to by
5296 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
5297 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
5298 ** this buffer before returning.
5300 static int unixFullPathname(
5301 sqlite3_vfs
*pVfs
, /* Pointer to vfs object */
5302 const char *zPath
, /* Possibly relative input path */
5303 int nOut
, /* Size of output buffer in bytes */
5304 char *zOut
/* Output buffer */
5307 /* It's odd to simulate an io-error here, but really this is just
5308 ** using the io-error infrastructure to test that SQLite handles this
5309 ** function failing. This function could fail if, for example, the
5310 ** current working directory has been unlinked.
5312 SimulateIOError( return SQLITE_ERROR
);
5314 assert( pVfs
->mxPathname
==MAX_PATHNAME
);
5315 UNUSED_PARAMETER(pVfs
);
5317 zOut
[nOut
-1] = '\0';
5318 if( zPath
[0]=='/' ){
5319 sqlite3_snprintf(nOut
, zOut
, "%s", zPath
);
5322 if( osGetcwd(zOut
, nOut
-1)==0 ){
5323 return unixLogError(SQLITE_CANTOPEN_BKPT
, "getcwd", zPath
);
5325 nCwd
= (int)strlen(zOut
);
5326 sqlite3_snprintf(nOut
-nCwd
, &zOut
[nCwd
], "/%s", zPath
);
5332 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5334 ** Interfaces for opening a shared library, finding entry points
5335 ** within the shared library, and closing the shared library.
5338 static void *unixDlOpen(sqlite3_vfs
*NotUsed
, const char *zFilename
){
5339 UNUSED_PARAMETER(NotUsed
);
5340 return dlopen(zFilename
, RTLD_NOW
| RTLD_GLOBAL
);
5344 ** SQLite calls this function immediately after a call to unixDlSym() or
5345 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
5346 ** message is available, it is written to zBufOut. If no error message
5347 ** is available, zBufOut is left unmodified and SQLite uses a default
5350 static void unixDlError(sqlite3_vfs
*NotUsed
, int nBuf
, char *zBufOut
){
5352 UNUSED_PARAMETER(NotUsed
);
5356 sqlite3_snprintf(nBuf
, zBufOut
, "%s", zErr
);
5360 static void (*unixDlSym(sqlite3_vfs
*NotUsed
, void *p
, const char*zSym
))(void){
5362 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
5363 ** cast into a pointer to a function. And yet the library dlsym() routine
5364 ** returns a void* which is really a pointer to a function. So how do we
5365 ** use dlsym() with -pedantic-errors?
5367 ** Variable x below is defined to be a pointer to a function taking
5368 ** parameters void* and const char* and returning a pointer to a function.
5369 ** We initialize x by assigning it a pointer to the dlsym() function.
5370 ** (That assignment requires a cast.) Then we call the function that
5373 ** This work-around is unlikely to work correctly on any system where
5374 ** you really cannot cast a function pointer into void*. But then, on the
5375 ** other hand, dlsym() will not work on such a system either, so we have
5376 ** not really lost anything.
5378 void (*(*x
)(void*,const char*))(void);
5379 UNUSED_PARAMETER(NotUsed
);
5380 x
= (void(*(*)(void*,const char*))(void))dlsym
;
5381 return (*x
)(p
, zSym
);
5383 static void unixDlClose(sqlite3_vfs
*NotUsed
, void *pHandle
){
5384 UNUSED_PARAMETER(NotUsed
);
5387 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
5388 #define unixDlOpen 0
5389 #define unixDlError 0
5391 #define unixDlClose 0
5395 ** Write nBuf bytes of random data to the supplied buffer zBuf.
5397 static int unixRandomness(sqlite3_vfs
*NotUsed
, int nBuf
, char *zBuf
){
5398 UNUSED_PARAMETER(NotUsed
);
5399 assert((size_t)nBuf
>=(sizeof(time_t)+sizeof(int)));
5401 /* We have to initialize zBuf to prevent valgrind from reporting
5402 ** errors. The reports issued by valgrind are incorrect - we would
5403 ** prefer that the randomness be increased by making use of the
5404 ** uninitialized space in zBuf - but valgrind errors tend to worry
5405 ** some users. Rather than argue, it seems easier just to initialize
5406 ** the whole array and silence valgrind, even if that means less randomness
5407 ** in the random seed.
5409 ** When testing, initializing zBuf[] to zero is all we do. That means
5410 ** that we always use the same random number sequence. This makes the
5411 ** tests repeatable.
5413 memset(zBuf
, 0, nBuf
);
5414 #if !defined(SQLITE_TEST)
5417 fd
= robust_open("/dev/urandom", O_RDONLY
, 0);
5421 memcpy(zBuf
, &t
, sizeof(t
));
5423 memcpy(&zBuf
[sizeof(t
)], &pid
, sizeof(pid
));
5424 assert( sizeof(t
)+sizeof(pid
)<=(size_t)nBuf
);
5425 nBuf
= sizeof(t
) + sizeof(pid
);
5427 do{ nBuf
= osRead(fd
, zBuf
, nBuf
); }while( nBuf
<0 && errno
==EINTR
);
5428 robust_close(0, fd
, __LINE__
);
5437 ** Sleep for a little while. Return the amount of time slept.
5438 ** The argument is the number of microseconds we want to sleep.
5439 ** The return value is the number of microseconds of sleep actually
5440 ** requested from the underlying operating system, a number which
5441 ** might be greater than or equal to the argument, but not less
5442 ** than the argument.
5444 static int unixSleep(sqlite3_vfs
*NotUsed
, int microseconds
){
5448 sp
.tv_sec
= microseconds
/ 1000000;
5449 sp
.tv_nsec
= (microseconds
% 1000000) * 1000;
5450 nanosleep(&sp
, NULL
);
5451 UNUSED_PARAMETER(NotUsed
);
5452 return microseconds
;
5453 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
5454 usleep(microseconds
);
5455 UNUSED_PARAMETER(NotUsed
);
5456 return microseconds
;
5458 int seconds
= (microseconds
+999999)/1000000;
5460 UNUSED_PARAMETER(NotUsed
);
5461 return seconds
*1000000;
5466 ** The following variable, if set to a non-zero value, is interpreted as
5467 ** the number of seconds since 1970 and is used to set the result of
5468 ** sqlite3OsCurrentTime() during testing.
5471 int sqlite3_current_time
= 0; /* Fake system time in seconds since 1970. */
5475 ** Find the current time (in Universal Coordinated Time). Write into *piNow
5476 ** the current time and date as a Julian Day number times 86_400_000. In
5477 ** other words, write into *piNow the number of milliseconds since the Julian
5478 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
5479 ** proleptic Gregorian calendar.
5481 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
5484 static int unixCurrentTimeInt64(sqlite3_vfs
*NotUsed
, sqlite3_int64
*piNow
){
5485 static const sqlite3_int64 unixEpoch
= 24405875*(sqlite3_int64
)8640000;
5487 #if defined(NO_GETTOD)
5490 *piNow
= ((sqlite3_int64
)t
)*1000 + unixEpoch
;
5492 struct timespec sNow
;
5493 clock_gettime(CLOCK_REALTIME
, &sNow
);
5494 *piNow
= unixEpoch
+ 1000*(sqlite3_int64
)sNow
.tv_sec
+ sNow
.tv_nsec
/1000000;
5496 struct timeval sNow
;
5497 if( gettimeofday(&sNow
, 0)==0 ){
5498 *piNow
= unixEpoch
+ 1000*(sqlite3_int64
)sNow
.tv_sec
+ sNow
.tv_usec
/1000;
5505 if( sqlite3_current_time
){
5506 *piNow
= 1000*(sqlite3_int64
)sqlite3_current_time
+ unixEpoch
;
5509 UNUSED_PARAMETER(NotUsed
);
5514 ** Find the current time (in Universal Coordinated Time). Write the
5515 ** current time and date as a Julian Day number into *prNow and
5516 ** return 0. Return 1 if the time and date cannot be found.
5518 static int unixCurrentTime(sqlite3_vfs
*NotUsed
, double *prNow
){
5519 sqlite3_int64 i
= 0;
5521 UNUSED_PARAMETER(NotUsed
);
5522 rc
= unixCurrentTimeInt64(0, &i
);
5523 *prNow
= i
/86400000.0;
5528 ** We added the xGetLastError() method with the intention of providing
5529 ** better low-level error messages when operating-system problems come up
5530 ** during SQLite operation. But so far, none of that has been implemented
5531 ** in the core. So this routine is never called. For now, it is merely
5534 static int unixGetLastError(sqlite3_vfs
*NotUsed
, int NotUsed2
, char *NotUsed3
){
5535 UNUSED_PARAMETER(NotUsed
);
5536 UNUSED_PARAMETER(NotUsed2
);
5537 UNUSED_PARAMETER(NotUsed3
);
5543 ************************ End of sqlite3_vfs methods ***************************
5544 ******************************************************************************/
5546 /******************************************************************************
5547 ************************** Begin Proxy Locking ********************************
5549 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
5550 ** other locking methods on secondary lock files. Proxy locking is a
5551 ** meta-layer over top of the primitive locking implemented above. For
5552 ** this reason, the division that implements of proxy locking is deferred
5553 ** until late in the file (here) after all of the other I/O methods have
5554 ** been defined - so that the primitive locking methods are available
5555 ** as services to help with the implementation of proxy locking.
5559 ** The default locking schemes in SQLite use byte-range locks on the
5560 ** database file to coordinate safe, concurrent access by multiple readers
5561 ** and writers [http://sqlite.org/lockingv3.html]. The five file locking
5562 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
5563 ** as POSIX read & write locks over fixed set of locations (via fsctl),
5564 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
5565 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
5566 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
5567 ** address in the shared range is taken for a SHARED lock, the entire
5568 ** shared range is taken for an EXCLUSIVE lock):
5570 ** PENDING_BYTE 0x40000000
5571 ** RESERVED_BYTE 0x40000001
5572 ** SHARED_RANGE 0x40000002 -> 0x40000200
5574 ** This works well on the local file system, but shows a nearly 100x
5575 ** slowdown in read performance on AFP because the AFP client disables
5576 ** the read cache when byte-range locks are present. Enabling the read
5577 ** cache exposes a cache coherency problem that is present on all OS X
5578 ** supported network file systems. NFS and AFP both observe the
5579 ** close-to-open semantics for ensuring cache coherency
5580 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
5581 ** address the requirements for concurrent database access by multiple
5582 ** readers and writers
5583 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
5585 ** To address the performance and cache coherency issues, proxy file locking
5586 ** changes the way database access is controlled by limiting access to a
5587 ** single host at a time and moving file locks off of the database file
5588 ** and onto a proxy file on the local file system.
5591 ** Using proxy locks
5592 ** -----------------
5596 ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
5597 ** <proxy_path> | ":auto:");
5598 ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
5603 ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
5604 ** PRAGMA [database.]lock_proxy_file
5606 ** Specifying ":auto:" means that if there is a conch file with a matching
5607 ** host ID in it, the proxy path in the conch file will be used, otherwise
5608 ** a proxy path based on the user's temp dir
5609 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
5610 ** actual proxy file name is generated from the name and path of the
5611 ** database file. For example:
5613 ** For database path "/Users/me/foo.db"
5614 ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
5616 ** Once a lock proxy is configured for a database connection, it can not
5617 ** be removed, however it may be switched to a different proxy path via
5618 ** the above APIs (assuming the conch file is not being held by another
5619 ** connection or process).
5622 ** How proxy locking works
5623 ** -----------------------
5625 ** Proxy file locking relies primarily on two new supporting files:
5627 ** * conch file to limit access to the database file to a single host
5630 ** * proxy file to act as a proxy for the advisory locks normally
5631 ** taken on the database
5633 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
5634 ** by taking an sqlite-style shared lock on the conch file, reading the
5635 ** contents and comparing the host's unique host ID (see below) and lock
5636 ** proxy path against the values stored in the conch. The conch file is
5637 ** stored in the same directory as the database file and the file name
5638 ** is patterned after the database file name as ".<databasename>-conch".
5639 ** If the conch file does not exist, or it's contents do not match the
5640 ** host ID and/or proxy path, then the lock is escalated to an exclusive
5641 ** lock and the conch file contents is updated with the host ID and proxy
5642 ** path and the lock is downgraded to a shared lock again. If the conch
5643 ** is held by another process (with a shared lock), the exclusive lock
5644 ** will fail and SQLITE_BUSY is returned.
5646 ** The proxy file - a single-byte file used for all advisory file locks
5647 ** normally taken on the database file. This allows for safe sharing
5648 ** of the database file for multiple readers and writers on the same
5649 ** host (the conch ensures that they all use the same local lock file).
5651 ** Requesting the lock proxy does not immediately take the conch, it is
5652 ** only taken when the first request to lock database file is made.
5653 ** This matches the semantics of the traditional locking behavior, where
5654 ** opening a connection to a database file does not take a lock on it.
5655 ** The shared lock and an open file descriptor are maintained until
5656 ** the connection to the database is closed.
5658 ** The proxy file and the lock file are never deleted so they only need
5659 ** to be created the first time they are used.
5661 ** Configuration options
5662 ** ---------------------
5664 ** SQLITE_PREFER_PROXY_LOCKING
5666 ** Database files accessed on non-local file systems are
5667 ** automatically configured for proxy locking, lock files are
5668 ** named automatically using the same logic as
5669 ** PRAGMA lock_proxy_file=":auto:"
5671 ** SQLITE_PROXY_DEBUG
5673 ** Enables the logging of error messages during host id file
5674 ** retrieval and creation
5678 ** Overrides the default directory used for lock proxy files that
5679 ** are named automatically via the ":auto:" setting
5681 ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
5683 ** Permissions to use when creating a directory for storing the
5684 ** lock proxy files, only used when LOCKPROXYDIR is not set.
5687 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
5688 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
5689 ** force proxy locking to be used for every database file opened, and 0
5690 ** will force automatic proxy locking to be disabled for all database
5691 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
5692 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
5696 ** Proxy locking is only available on MacOSX
5698 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
5701 ** The proxyLockingContext has the path and file structures for the remote
5702 ** and local proxy files in it
5704 typedef struct proxyLockingContext proxyLockingContext
;
5705 struct proxyLockingContext
{
5706 unixFile
*conchFile
; /* Open conch file */
5707 char *conchFilePath
; /* Name of the conch file */
5708 unixFile
*lockProxy
; /* Open proxy lock file */
5709 char *lockProxyPath
; /* Name of the proxy lock file */
5710 char *dbPath
; /* Name of the open file */
5711 int conchHeld
; /* 1 if the conch is held, -1 if lockless */
5712 void *oldLockingContext
; /* Original lockingcontext to restore on close */
5713 sqlite3_io_methods
const *pOldMethod
; /* Original I/O methods for close */
5717 ** The proxy lock file path for the database at dbPath is written into lPath,
5718 ** which must point to valid, writable memory large enough for a maxLen length
5721 static int proxyGetLockPath(const char *dbPath
, char *lPath
, size_t maxLen
){
5727 len
= strlcpy(lPath
, LOCKPROXYDIR
, maxLen
);
5729 # ifdef _CS_DARWIN_USER_TEMP_DIR
5731 if( !confstr(_CS_DARWIN_USER_TEMP_DIR
, lPath
, maxLen
) ){
5732 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
5733 lPath
, errno
, getpid()));
5734 return SQLITE_IOERR_LOCK
;
5736 len
= strlcat(lPath
, "sqliteplocks", maxLen
);
5739 len
= strlcpy(lPath
, "/tmp/", maxLen
);
5743 if( lPath
[len
-1]!='/' ){
5744 len
= strlcat(lPath
, "/", maxLen
);
5747 /* transform the db path to a unique cache name */
5748 dbLen
= (int)strlen(dbPath
);
5749 for( i
=0; i
<dbLen
&& (i
+len
+7)<(int)maxLen
; i
++){
5751 lPath
[i
+len
] = (c
=='/')?'_':c
;
5754 strlcat(lPath
, ":auto:", maxLen
);
5755 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath
, getpid()));
5760 ** Creates the lock file and any missing directories in lockPath
5762 static int proxyCreateLockPath(const char *lockPath
){
5764 char buf
[MAXPATHLEN
];
5767 assert(lockPath
!=NULL
);
5768 /* try to create all the intermediate directories */
5769 len
= (int)strlen(lockPath
);
5770 buf
[0] = lockPath
[0];
5771 for( i
=1; i
<len
; i
++ ){
5772 if( lockPath
[i
] == '/' && (i
- start
> 0) ){
5773 /* only mkdir if leaf dir != "." or "/" or ".." */
5774 if( i
-start
>2 || (i
-start
==1 && buf
[start
] != '.' && buf
[start
] != '/')
5775 || (i
-start
==2 && buf
[start
] != '.' && buf
[start
+1] != '.') ){
5777 if( osMkdir(buf
, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
) ){
5780 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
5781 "'%s' proxy lock path=%s pid=%d\n",
5782 buf
, strerror(err
), lockPath
, getpid()));
5789 buf
[i
] = lockPath
[i
];
5791 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath
, getpid()));
5796 ** Create a new VFS file descriptor (stored in memory obtained from
5797 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
5799 ** The caller is responsible not only for closing the file descriptor
5800 ** but also for freeing the memory associated with the file descriptor.
5802 static int proxyCreateUnixFile(
5803 const char *path
, /* path for the new unixFile */
5804 unixFile
**ppFile
, /* unixFile created and returned by ref */
5805 int islockfile
/* if non zero missing dirs will be created */
5810 int openFlags
= O_RDWR
| O_CREAT
;
5811 sqlite3_vfs dummyVfs
;
5813 UnixUnusedFd
*pUnused
= NULL
;
5815 /* 1. first try to open/create the file
5816 ** 2. if that fails, and this is a lock file (not-conch), try creating
5817 ** the parent directories and then try again.
5818 ** 3. if that fails, try to open the file read-only
5819 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
5821 pUnused
= findReusableFd(path
, openFlags
);
5825 pUnused
= sqlite3_malloc(sizeof(*pUnused
));
5827 return SQLITE_NOMEM
;
5831 fd
= robust_open(path
, openFlags
, SQLITE_DEFAULT_FILE_PERMISSIONS
);
5833 if( fd
<0 && errno
==ENOENT
&& islockfile
){
5834 if( proxyCreateLockPath(path
) == SQLITE_OK
){
5835 fd
= robust_open(path
, openFlags
, SQLITE_DEFAULT_FILE_PERMISSIONS
);
5840 openFlags
= O_RDONLY
;
5841 fd
= robust_open(path
, openFlags
, SQLITE_DEFAULT_FILE_PERMISSIONS
);
5852 return SQLITE_IOERR_LOCK
; /* even though it is the conch */
5854 return SQLITE_CANTOPEN_BKPT
;
5858 pNew
= (unixFile
*)sqlite3_malloc(sizeof(*pNew
));
5861 goto end_create_proxy
;
5863 memset(pNew
, 0, sizeof(unixFile
));
5864 pNew
->openFlags
= openFlags
;
5865 memset(&dummyVfs
, 0, sizeof(dummyVfs
));
5866 dummyVfs
.pAppData
= (void*)&autolockIoFinder
;
5867 dummyVfs
.zName
= "dummy";
5869 pUnused
->flags
= openFlags
;
5870 pNew
->pUnused
= pUnused
;
5872 rc
= fillInUnixFile(&dummyVfs
, fd
, (sqlite3_file
*)pNew
, path
, 0);
5873 if( rc
==SQLITE_OK
){
5878 robust_close(pNew
, fd
, __LINE__
);
5880 sqlite3_free(pUnused
);
5885 /* simulate multiple hosts by creating unique hostid file paths */
5886 int sqlite3_hostid_num
= 0;
5889 #define PROXY_HOSTIDLEN 16 /* conch file host id length */
5891 /* Not always defined in the headers as it ought to be */
5892 extern int gethostuuid(uuid_t id
, const struct timespec
*wait
);
5894 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
5895 ** bytes of writable memory.
5897 static int proxyGetHostID(unsigned char *pHostID
, int *pError
){
5898 assert(PROXY_HOSTIDLEN
== sizeof(uuid_t
));
5899 memset(pHostID
, 0, PROXY_HOSTIDLEN
);
5900 #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
5901 && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
5903 static const struct timespec timeout
= {1, 0}; /* 1 sec timeout */
5904 if( gethostuuid(pHostID
, &timeout
) ){
5909 return SQLITE_IOERR
;
5913 UNUSED_PARAMETER(pError
);
5916 /* simulate multiple hosts by creating unique hostid file paths */
5917 if( sqlite3_hostid_num
!= 0){
5918 pHostID
[0] = (char)(pHostID
[0] + (char)(sqlite3_hostid_num
& 0xFF));
5925 /* The conch file contains the header, host id and lock file path
5927 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
5928 #define PROXY_HEADERLEN 1 /* conch file header length */
5929 #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
5930 #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
5933 ** Takes an open conch file, copies the contents to a new path and then moves
5934 ** it back. The newly created file's file descriptor is assigned to the
5935 ** conch file structure and finally the original conch file descriptor is
5936 ** closed. Returns zero if successful.
5938 static int proxyBreakConchLock(unixFile
*pFile
, uuid_t myHostID
){
5939 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
5940 unixFile
*conchFile
= pCtx
->conchFile
;
5941 char tPath
[MAXPATHLEN
];
5942 char buf
[PROXY_MAXCONCHLEN
];
5943 char *cPath
= pCtx
->conchFilePath
;
5946 char errmsg
[64] = "";
5949 UNUSED_PARAMETER(myHostID
);
5951 /* create a new path by replace the trailing '-conch' with '-break' */
5952 pathLen
= strlcpy(tPath
, cPath
, MAXPATHLEN
);
5953 if( pathLen
>MAXPATHLEN
|| pathLen
<6 ||
5954 (strlcpy(&tPath
[pathLen
-5], "break", 6) != 5) ){
5955 sqlite3_snprintf(sizeof(errmsg
),errmsg
,"path error (len %d)",(int)pathLen
);
5958 /* read the conch content */
5959 readLen
= osPread(conchFile
->h
, buf
, PROXY_MAXCONCHLEN
, 0);
5960 if( readLen
<PROXY_PATHINDEX
){
5961 sqlite3_snprintf(sizeof(errmsg
),errmsg
,"read error (len %d)",(int)readLen
);
5964 /* write it out to the temporary break file */
5965 fd
= robust_open(tPath
, (O_RDWR
|O_CREAT
|O_EXCL
),
5966 SQLITE_DEFAULT_FILE_PERMISSIONS
);
5968 sqlite3_snprintf(sizeof(errmsg
), errmsg
, "create failed (%d)", errno
);
5971 if( osPwrite(fd
, buf
, readLen
, 0) != (ssize_t
)readLen
){
5972 sqlite3_snprintf(sizeof(errmsg
), errmsg
, "write failed (%d)", errno
);
5975 if( rename(tPath
, cPath
) ){
5976 sqlite3_snprintf(sizeof(errmsg
), errmsg
, "rename failed (%d)", errno
);
5980 fprintf(stderr
, "broke stale lock on %s\n", cPath
);
5981 robust_close(pFile
, conchFile
->h
, __LINE__
);
5983 conchFile
->openFlags
= O_RDWR
| O_CREAT
;
5989 robust_close(pFile
, fd
, __LINE__
);
5991 fprintf(stderr
, "failed to break stale lock on %s, %s\n", cPath
, errmsg
);
5996 /* Take the requested lock on the conch file and break a stale lock if the
5999 static int proxyConchLock(unixFile
*pFile
, uuid_t myHostID
, int lockType
){
6000 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6001 unixFile
*conchFile
= pCtx
->conchFile
;
6004 struct timespec conchModTime
;
6006 memset(&conchModTime
, 0, sizeof(conchModTime
));
6008 rc
= conchFile
->pMethod
->xLock((sqlite3_file
*)conchFile
, lockType
);
6010 if( rc
==SQLITE_BUSY
){
6011 /* If the lock failed (busy):
6012 * 1st try: get the mod time of the conch, wait 0.5s and try again.
6013 * 2nd try: fail if the mod time changed or host id is different, wait
6014 * 10 sec and try again
6015 * 3rd try: break the lock unless the mod time has changed.
6018 if( osFstat(conchFile
->h
, &buf
) ){
6019 pFile
->lastErrno
= errno
;
6020 return SQLITE_IOERR_LOCK
;
6024 conchModTime
= buf
.st_mtimespec
;
6025 usleep(500000); /* wait 0.5 sec and try the lock again*/
6030 if( conchModTime
.tv_sec
!= buf
.st_mtimespec
.tv_sec
||
6031 conchModTime
.tv_nsec
!= buf
.st_mtimespec
.tv_nsec
){
6036 char tBuf
[PROXY_MAXCONCHLEN
];
6037 int len
= osPread(conchFile
->h
, tBuf
, PROXY_MAXCONCHLEN
, 0);
6039 pFile
->lastErrno
= errno
;
6040 return SQLITE_IOERR_LOCK
;
6042 if( len
>PROXY_PATHINDEX
&& tBuf
[0]==(char)PROXY_CONCHVERSION
){
6043 /* don't break the lock if the host id doesn't match */
6044 if( 0!=memcmp(&tBuf
[PROXY_HEADERLEN
], myHostID
, PROXY_HOSTIDLEN
) ){
6048 /* don't break the lock on short read or a version mismatch */
6051 usleep(10000000); /* wait 10 sec and try the lock again */
6055 assert( nTries
==3 );
6056 if( 0==proxyBreakConchLock(pFile
, myHostID
) ){
6058 if( lockType
==EXCLUSIVE_LOCK
){
6059 rc
= conchFile
->pMethod
->xLock((sqlite3_file
*)conchFile
, SHARED_LOCK
);
6062 rc
= conchFile
->pMethod
->xLock((sqlite3_file
*)conchFile
, lockType
);
6066 } while( rc
==SQLITE_BUSY
&& nTries
<3 );
6071 /* Takes the conch by taking a shared lock and read the contents conch, if
6072 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
6073 ** lockPath means that the lockPath in the conch file will be used if the
6074 ** host IDs match, or a new lock path will be generated automatically
6075 ** and written to the conch file.
6077 static int proxyTakeConch(unixFile
*pFile
){
6078 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6080 if( pCtx
->conchHeld
!=0 ){
6083 unixFile
*conchFile
= pCtx
->conchFile
;
6086 char readBuf
[PROXY_MAXCONCHLEN
];
6087 char lockPath
[MAXPATHLEN
];
6088 char *tempLockPath
= NULL
;
6090 int createConch
= 0;
6091 int hostIdMatch
= 0;
6093 int tryOldLockPath
= 0;
6094 int forceNewLockPath
= 0;
6096 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile
->h
,
6097 (pCtx
->lockProxyPath
? pCtx
->lockProxyPath
: ":auto:"), getpid()));
6099 rc
= proxyGetHostID(myHostID
, &pError
);
6100 if( (rc
&0xff)==SQLITE_IOERR
){
6101 pFile
->lastErrno
= pError
;
6104 rc
= proxyConchLock(pFile
, myHostID
, SHARED_LOCK
);
6105 if( rc
!=SQLITE_OK
){
6108 /* read the existing conch file */
6109 readLen
= seekAndRead((unixFile
*)conchFile
, 0, readBuf
, PROXY_MAXCONCHLEN
);
6111 /* I/O error: lastErrno set by seekAndRead */
6112 pFile
->lastErrno
= conchFile
->lastErrno
;
6113 rc
= SQLITE_IOERR_READ
;
6115 }else if( readLen
<=(PROXY_HEADERLEN
+PROXY_HOSTIDLEN
) ||
6116 readBuf
[0]!=(char)PROXY_CONCHVERSION
){
6117 /* a short read or version format mismatch means we need to create a new
6122 /* if the host id matches and the lock path already exists in the conch
6123 ** we'll try to use the path there, if we can't open that path, we'll
6124 ** retry with a new auto-generated path
6126 do { /* in case we need to try again for an :auto: named lock file */
6128 if( !createConch
&& !forceNewLockPath
){
6129 hostIdMatch
= !memcmp(&readBuf
[PROXY_HEADERLEN
], myHostID
,
6131 /* if the conch has data compare the contents */
6132 if( !pCtx
->lockProxyPath
){
6133 /* for auto-named local lock file, just check the host ID and we'll
6134 ** use the local lock file path that's already in there
6137 size_t pathLen
= (readLen
- PROXY_PATHINDEX
);
6139 if( pathLen
>=MAXPATHLEN
){
6140 pathLen
=MAXPATHLEN
-1;
6142 memcpy(lockPath
, &readBuf
[PROXY_PATHINDEX
], pathLen
);
6143 lockPath
[pathLen
] = 0;
6144 tempLockPath
= lockPath
;
6146 /* create a copy of the lock path if the conch is taken */
6149 }else if( hostIdMatch
6150 && !strncmp(pCtx
->lockProxyPath
, &readBuf
[PROXY_PATHINDEX
],
6151 readLen
-PROXY_PATHINDEX
)
6153 /* conch host and lock path match */
6158 /* if the conch isn't writable and doesn't match, we can't take it */
6159 if( (conchFile
->openFlags
&O_RDWR
) == 0 ){
6164 /* either the conch didn't match or we need to create a new one */
6165 if( !pCtx
->lockProxyPath
){
6166 proxyGetLockPath(pCtx
->dbPath
, lockPath
, MAXPATHLEN
);
6167 tempLockPath
= lockPath
;
6168 /* create a copy of the lock path _only_ if the conch is taken */
6171 /* update conch with host and path (this will fail if other process
6172 ** has a shared lock already), if the host id matches, use the big
6175 futimes(conchFile
->h
, NULL
);
6176 if( hostIdMatch
&& !createConch
){
6177 if( conchFile
->pInode
&& conchFile
->pInode
->nShared
>1 ){
6178 /* We are trying for an exclusive lock but another thread in this
6179 ** same process is still holding a shared lock. */
6182 rc
= proxyConchLock(pFile
, myHostID
, EXCLUSIVE_LOCK
);
6185 rc
= conchFile
->pMethod
->xLock((sqlite3_file
*)conchFile
, EXCLUSIVE_LOCK
);
6187 if( rc
==SQLITE_OK
){
6188 char writeBuffer
[PROXY_MAXCONCHLEN
];
6191 writeBuffer
[0] = (char)PROXY_CONCHVERSION
;
6192 memcpy(&writeBuffer
[PROXY_HEADERLEN
], myHostID
, PROXY_HOSTIDLEN
);
6193 if( pCtx
->lockProxyPath
!=NULL
){
6194 strlcpy(&writeBuffer
[PROXY_PATHINDEX
], pCtx
->lockProxyPath
, MAXPATHLEN
);
6196 strlcpy(&writeBuffer
[PROXY_PATHINDEX
], tempLockPath
, MAXPATHLEN
);
6198 writeSize
= PROXY_PATHINDEX
+ strlen(&writeBuffer
[PROXY_PATHINDEX
]);
6199 robust_ftruncate(conchFile
->h
, writeSize
);
6200 rc
= unixWrite((sqlite3_file
*)conchFile
, writeBuffer
, writeSize
, 0);
6201 fsync(conchFile
->h
);
6202 /* If we created a new conch file (not just updated the contents of a
6203 ** valid conch file), try to match the permissions of the database
6205 if( rc
==SQLITE_OK
&& createConch
){
6207 int err
= osFstat(pFile
->h
, &buf
);
6209 mode_t cmode
= buf
.st_mode
&(S_IRUSR
|S_IWUSR
| S_IRGRP
|S_IWGRP
|
6211 /* try to match the database file R/W permissions, ignore failure */
6212 #ifndef SQLITE_PROXY_DEBUG
6213 osFchmod(conchFile
->h
, cmode
);
6216 rc
= osFchmod(conchFile
->h
, cmode
);
6217 }while( rc
==(-1) && errno
==EINTR
);
6220 fprintf(stderr
, "fchmod %o FAILED with %d %s\n",
6221 cmode
, code
, strerror(code
));
6223 fprintf(stderr
, "fchmod %o SUCCEDED\n",cmode
);
6227 fprintf(stderr
, "STAT FAILED[%d] with %d %s\n",
6228 err
, code
, strerror(code
));
6233 conchFile
->pMethod
->xUnlock((sqlite3_file
*)conchFile
, SHARED_LOCK
);
6236 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile
->h
));
6237 if( rc
==SQLITE_OK
&& pFile
->openFlags
){
6240 robust_close(pFile
, pFile
->h
, __LINE__
);
6243 fd
= robust_open(pCtx
->dbPath
, pFile
->openFlags
,
6244 SQLITE_DEFAULT_FILE_PERMISSIONS
);
6245 OSTRACE(("TRANSPROXY: OPEN %d\n", fd
));
6249 rc
=SQLITE_CANTOPEN_BKPT
; /* SQLITE_BUSY? proxyTakeConch called
6253 if( rc
==SQLITE_OK
&& !pCtx
->lockProxy
){
6254 char *path
= tempLockPath
? tempLockPath
: pCtx
->lockProxyPath
;
6255 rc
= proxyCreateUnixFile(path
, &pCtx
->lockProxy
, 1);
6256 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_NOMEM
&& tryOldLockPath
){
6257 /* we couldn't create the proxy lock file with the old lock file path
6258 ** so try again via auto-naming
6260 forceNewLockPath
= 1;
6262 continue; /* go back to the do {} while start point, try again */
6265 if( rc
==SQLITE_OK
){
6266 /* Need to make a copy of path if we extracted the value
6267 ** from the conch file or the path was allocated on the stack
6270 pCtx
->lockProxyPath
= sqlite3DbStrDup(0, tempLockPath
);
6271 if( !pCtx
->lockProxyPath
){
6276 if( rc
==SQLITE_OK
){
6277 pCtx
->conchHeld
= 1;
6279 if( pCtx
->lockProxy
->pMethod
== &afpIoMethods
){
6280 afpLockingContext
*afpCtx
;
6281 afpCtx
= (afpLockingContext
*)pCtx
->lockProxy
->lockingContext
;
6282 afpCtx
->dbPath
= pCtx
->lockProxyPath
;
6285 conchFile
->pMethod
->xUnlock((sqlite3_file
*)conchFile
, NO_LOCK
);
6287 OSTRACE(("TAKECONCH %d %s\n", conchFile
->h
,
6288 rc
==SQLITE_OK
?"ok":"failed"));
6290 } while (1); /* in case we need to retry the :auto: lock file -
6291 ** we should never get here except via the 'continue' call. */
6296 ** If pFile holds a lock on a conch file, then release that lock.
6298 static int proxyReleaseConch(unixFile
*pFile
){
6299 int rc
= SQLITE_OK
; /* Subroutine return code */
6300 proxyLockingContext
*pCtx
; /* The locking context for the proxy lock */
6301 unixFile
*conchFile
; /* Name of the conch file */
6303 pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6304 conchFile
= pCtx
->conchFile
;
6305 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile
->h
,
6306 (pCtx
->lockProxyPath
? pCtx
->lockProxyPath
: ":auto:"),
6308 if( pCtx
->conchHeld
>0 ){
6309 rc
= conchFile
->pMethod
->xUnlock((sqlite3_file
*)conchFile
, NO_LOCK
);
6311 pCtx
->conchHeld
= 0;
6312 OSTRACE(("RELEASECONCH %d %s\n", conchFile
->h
,
6313 (rc
==SQLITE_OK
? "ok" : "failed")));
6318 ** Given the name of a database file, compute the name of its conch file.
6319 ** Store the conch filename in memory obtained from sqlite3_malloc().
6320 ** Make *pConchPath point to the new name. Return SQLITE_OK on success
6321 ** or SQLITE_NOMEM if unable to obtain memory.
6323 ** The caller is responsible for ensuring that the allocated memory
6324 ** space is eventually freed.
6326 ** *pConchPath is set to NULL if a memory allocation error occurs.
6328 static int proxyCreateConchPathname(char *dbPath
, char **pConchPath
){
6329 int i
; /* Loop counter */
6330 int len
= (int)strlen(dbPath
); /* Length of database filename - dbPath */
6331 char *conchPath
; /* buffer in which to construct conch name */
6333 /* Allocate space for the conch filename and initialize the name to
6334 ** the name of the original database file. */
6335 *pConchPath
= conchPath
= (char *)sqlite3_malloc(len
+ 8);
6337 return SQLITE_NOMEM
;
6339 memcpy(conchPath
, dbPath
, len
+1);
6341 /* now insert a "." before the last / character */
6342 for( i
=(len
-1); i
>=0; i
-- ){
6343 if( conchPath
[i
]=='/' ){
6350 conchPath
[i
+1]=dbPath
[i
];
6354 /* append the "-conch" suffix to the file */
6355 memcpy(&conchPath
[i
+1], "-conch", 7);
6356 assert( (int)strlen(conchPath
) == len
+7 );
6362 /* Takes a fully configured proxy locking-style unix file and switches
6363 ** the local lock file path
6365 static int switchLockProxyPath(unixFile
*pFile
, const char *path
) {
6366 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6367 char *oldPath
= pCtx
->lockProxyPath
;
6370 if( pFile
->eFileLock
!=NO_LOCK
){
6374 /* nothing to do if the path is NULL, :auto: or matches the existing path */
6375 if( !path
|| path
[0]=='\0' || !strcmp(path
, ":auto:") ||
6376 (oldPath
&& !strncmp(oldPath
, path
, MAXPATHLEN
)) ){
6379 unixFile
*lockProxy
= pCtx
->lockProxy
;
6380 pCtx
->lockProxy
=NULL
;
6381 pCtx
->conchHeld
= 0;
6382 if( lockProxy
!=NULL
){
6383 rc
=lockProxy
->pMethod
->xClose((sqlite3_file
*)lockProxy
);
6385 sqlite3_free(lockProxy
);
6387 sqlite3_free(oldPath
);
6388 pCtx
->lockProxyPath
= sqlite3DbStrDup(0, path
);
6395 ** pFile is a file that has been opened by a prior xOpen call. dbPath
6396 ** is a string buffer at least MAXPATHLEN+1 characters in size.
6398 ** This routine find the filename associated with pFile and writes it
6401 static int proxyGetDbPathForUnixFile(unixFile
*pFile
, char *dbPath
){
6402 #if defined(__APPLE__)
6403 if( pFile
->pMethod
== &afpIoMethods
){
6404 /* afp style keeps a reference to the db path in the filePath field
6406 assert( (int)strlen((char*)pFile
->lockingContext
)<=MAXPATHLEN
);
6407 strlcpy(dbPath
, ((afpLockingContext
*)pFile
->lockingContext
)->dbPath
, MAXPATHLEN
);
6410 if( pFile
->pMethod
== &dotlockIoMethods
){
6411 /* dot lock style uses the locking context to store the dot lock
6413 int len
= strlen((char *)pFile
->lockingContext
) - strlen(DOTLOCK_SUFFIX
);
6414 memcpy(dbPath
, (char *)pFile
->lockingContext
, len
+ 1);
6416 /* all other styles use the locking context to store the db file path */
6417 assert( strlen((char*)pFile
->lockingContext
)<=MAXPATHLEN
);
6418 strlcpy(dbPath
, (char *)pFile
->lockingContext
, MAXPATHLEN
);
6424 ** Takes an already filled in unix file and alters it so all file locking
6425 ** will be performed on the local proxy lock file. The following fields
6426 ** are preserved in the locking context so that they can be restored and
6427 ** the unix structure properly cleaned up at close time:
6431 static int proxyTransformUnixFile(unixFile
*pFile
, const char *path
) {
6432 proxyLockingContext
*pCtx
;
6433 char dbPath
[MAXPATHLEN
+1]; /* Name of the database file */
6434 char *lockPath
=NULL
;
6437 if( pFile
->eFileLock
!=NO_LOCK
){
6440 proxyGetDbPathForUnixFile(pFile
, dbPath
);
6441 if( !path
|| path
[0]=='\0' || !strcmp(path
, ":auto:") ){
6444 lockPath
=(char *)path
;
6447 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile
->h
,
6448 (lockPath
? lockPath
: ":auto:"), getpid()));
6450 pCtx
= sqlite3_malloc( sizeof(*pCtx
) );
6452 return SQLITE_NOMEM
;
6454 memset(pCtx
, 0, sizeof(*pCtx
));
6456 rc
= proxyCreateConchPathname(dbPath
, &pCtx
->conchFilePath
);
6457 if( rc
==SQLITE_OK
){
6458 rc
= proxyCreateUnixFile(pCtx
->conchFilePath
, &pCtx
->conchFile
, 0);
6459 if( rc
==SQLITE_CANTOPEN
&& ((pFile
->openFlags
&O_RDWR
) == 0) ){
6460 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
6461 ** (c) the file system is read-only, then enable no-locking access.
6462 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
6463 ** that openFlags will have only one of O_RDONLY or O_RDWR.
6465 struct statfs fsInfo
;
6466 struct stat conchInfo
;
6469 if( osStat(pCtx
->conchFilePath
, &conchInfo
) == -1 ) {
6471 if( (err
==ENOENT
) && (statfs(dbPath
, &fsInfo
) != -1) ){
6472 goLockless
= (fsInfo
.f_flags
&MNT_RDONLY
) == MNT_RDONLY
;
6476 pCtx
->conchHeld
= -1; /* read only FS/ lockless */
6481 if( rc
==SQLITE_OK
&& lockPath
){
6482 pCtx
->lockProxyPath
= sqlite3DbStrDup(0, lockPath
);
6485 if( rc
==SQLITE_OK
){
6486 pCtx
->dbPath
= sqlite3DbStrDup(0, dbPath
);
6487 if( pCtx
->dbPath
==NULL
){
6491 if( rc
==SQLITE_OK
){
6492 /* all memory is allocated, proxys are created and assigned,
6493 ** switch the locking context and pMethod then return.
6495 pCtx
->oldLockingContext
= pFile
->lockingContext
;
6496 pFile
->lockingContext
= pCtx
;
6497 pCtx
->pOldMethod
= pFile
->pMethod
;
6498 pFile
->pMethod
= &proxyIoMethods
;
6500 if( pCtx
->conchFile
){
6501 pCtx
->conchFile
->pMethod
->xClose((sqlite3_file
*)pCtx
->conchFile
);
6502 sqlite3_free(pCtx
->conchFile
);
6504 sqlite3DbFree(0, pCtx
->lockProxyPath
);
6505 sqlite3_free(pCtx
->conchFilePath
);
6508 OSTRACE(("TRANSPROXY %d %s\n", pFile
->h
,
6509 (rc
==SQLITE_OK
? "ok" : "failed")));
6515 ** This routine handles sqlite3_file_control() calls that are specific
6516 ** to proxy locking.
6518 static int proxyFileControl(sqlite3_file
*id
, int op
, void *pArg
){
6520 case SQLITE_GET_LOCKPROXYFILE
: {
6521 unixFile
*pFile
= (unixFile
*)id
;
6522 if( pFile
->pMethod
== &proxyIoMethods
){
6523 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6524 proxyTakeConch(pFile
);
6525 if( pCtx
->lockProxyPath
){
6526 *(const char **)pArg
= pCtx
->lockProxyPath
;
6528 *(const char **)pArg
= ":auto: (not held)";
6531 *(const char **)pArg
= NULL
;
6535 case SQLITE_SET_LOCKPROXYFILE
: {
6536 unixFile
*pFile
= (unixFile
*)id
;
6538 int isProxyStyle
= (pFile
->pMethod
== &proxyIoMethods
);
6539 if( pArg
==NULL
|| (const char *)pArg
==0 ){
6541 /* turn off proxy locking - not supported */
6542 rc
= SQLITE_ERROR
/*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
6544 /* turn off proxy locking - already off - NOOP */
6548 const char *proxyPath
= (const char *)pArg
;
6550 proxyLockingContext
*pCtx
=
6551 (proxyLockingContext
*)pFile
->lockingContext
;
6552 if( !strcmp(pArg
, ":auto:")
6553 || (pCtx
->lockProxyPath
&&
6554 !strncmp(pCtx
->lockProxyPath
, proxyPath
, MAXPATHLEN
))
6558 rc
= switchLockProxyPath(pFile
, proxyPath
);
6561 /* turn on proxy file locking */
6562 rc
= proxyTransformUnixFile(pFile
, proxyPath
);
6568 assert( 0 ); /* The call assures that only valid opcodes are sent */
6572 return SQLITE_ERROR
;
6576 ** Within this division (the proxying locking implementation) the procedures
6577 ** above this point are all utilities. The lock-related methods of the
6578 ** proxy-locking sqlite3_io_method object follow.
6583 ** This routine checks if there is a RESERVED lock held on the specified
6584 ** file by this or any other process. If such a lock is held, set *pResOut
6585 ** to a non-zero value otherwise *pResOut is set to zero. The return value
6586 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
6588 static int proxyCheckReservedLock(sqlite3_file
*id
, int *pResOut
) {
6589 unixFile
*pFile
= (unixFile
*)id
;
6590 int rc
= proxyTakeConch(pFile
);
6591 if( rc
==SQLITE_OK
){
6592 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6593 if( pCtx
->conchHeld
>0 ){
6594 unixFile
*proxy
= pCtx
->lockProxy
;
6595 return proxy
->pMethod
->xCheckReservedLock((sqlite3_file
*)proxy
, pResOut
);
6596 }else{ /* conchHeld < 0 is lockless */
6604 ** Lock the file with the lock specified by parameter eFileLock - one
6605 ** of the following:
6608 ** (2) RESERVED_LOCK
6610 ** (4) EXCLUSIVE_LOCK
6612 ** Sometimes when requesting one lock state, additional lock states
6613 ** are inserted in between. The locking might fail on one of the later
6614 ** transitions leaving the lock state different from what it started but
6615 ** still short of its goal. The following chart shows the allowed
6616 ** transitions and the inserted intermediate states:
6618 ** UNLOCKED -> SHARED
6619 ** SHARED -> RESERVED
6620 ** SHARED -> (PENDING) -> EXCLUSIVE
6621 ** RESERVED -> (PENDING) -> EXCLUSIVE
6622 ** PENDING -> EXCLUSIVE
6624 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
6625 ** routine to lower a locking level.
6627 static int proxyLock(sqlite3_file
*id
, int eFileLock
) {
6628 unixFile
*pFile
= (unixFile
*)id
;
6629 int rc
= proxyTakeConch(pFile
);
6630 if( rc
==SQLITE_OK
){
6631 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6632 if( pCtx
->conchHeld
>0 ){
6633 unixFile
*proxy
= pCtx
->lockProxy
;
6634 rc
= proxy
->pMethod
->xLock((sqlite3_file
*)proxy
, eFileLock
);
6635 pFile
->eFileLock
= proxy
->eFileLock
;
6637 /* conchHeld < 0 is lockless */
6645 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
6646 ** must be either NO_LOCK or SHARED_LOCK.
6648 ** If the locking level of the file descriptor is already at or below
6649 ** the requested locking level, this routine is a no-op.
6651 static int proxyUnlock(sqlite3_file
*id
, int eFileLock
) {
6652 unixFile
*pFile
= (unixFile
*)id
;
6653 int rc
= proxyTakeConch(pFile
);
6654 if( rc
==SQLITE_OK
){
6655 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6656 if( pCtx
->conchHeld
>0 ){
6657 unixFile
*proxy
= pCtx
->lockProxy
;
6658 rc
= proxy
->pMethod
->xUnlock((sqlite3_file
*)proxy
, eFileLock
);
6659 pFile
->eFileLock
= proxy
->eFileLock
;
6661 /* conchHeld < 0 is lockless */
6668 ** Close a file that uses proxy locks.
6670 static int proxyClose(sqlite3_file
*id
) {
6672 unixFile
*pFile
= (unixFile
*)id
;
6673 proxyLockingContext
*pCtx
= (proxyLockingContext
*)pFile
->lockingContext
;
6674 unixFile
*lockProxy
= pCtx
->lockProxy
;
6675 unixFile
*conchFile
= pCtx
->conchFile
;
6679 rc
= lockProxy
->pMethod
->xUnlock((sqlite3_file
*)lockProxy
, NO_LOCK
);
6681 rc
= lockProxy
->pMethod
->xClose((sqlite3_file
*)lockProxy
);
6683 sqlite3_free(lockProxy
);
6684 pCtx
->lockProxy
= 0;
6687 if( pCtx
->conchHeld
){
6688 rc
= proxyReleaseConch(pFile
);
6691 rc
= conchFile
->pMethod
->xClose((sqlite3_file
*)conchFile
);
6693 sqlite3_free(conchFile
);
6695 sqlite3DbFree(0, pCtx
->lockProxyPath
);
6696 sqlite3_free(pCtx
->conchFilePath
);
6697 sqlite3DbFree(0, pCtx
->dbPath
);
6698 /* restore the original locking context and pMethod then close it */
6699 pFile
->lockingContext
= pCtx
->oldLockingContext
;
6700 pFile
->pMethod
= pCtx
->pOldMethod
;
6702 return pFile
->pMethod
->xClose(id
);
6709 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
6711 ** The proxy locking style is intended for use with AFP filesystems.
6712 ** And since AFP is only supported on MacOSX, the proxy locking is also
6713 ** restricted to MacOSX.
6716 ******************* End of the proxy lock implementation **********************
6717 ******************************************************************************/
6720 ** Initialize the operating system interface.
6722 ** This routine registers all VFS implementations for unix-like operating
6723 ** systems. This routine, and the sqlite3_os_end() routine that follows,
6724 ** should be the only routines in this file that are visible from other
6727 ** This routine is called once during SQLite initialization and by a
6728 ** single thread. The memory allocation and mutex subsystems have not
6729 ** necessarily been initialized when this routine is called, and so they
6730 ** should not be used.
6732 int sqlite3_os_init(void){
6734 ** The following macro defines an initializer for an sqlite3_vfs object.
6735 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
6736 ** to the "finder" function. (pAppData is a pointer to a pointer because
6737 ** silly C90 rules prohibit a void* from being cast to a function pointer
6738 ** and so we have to go through the intermediate pointer to avoid problems
6739 ** when compiling with -pedantic-errors on GCC.)
6741 ** The FINDER parameter to this macro is the name of the pointer to the
6742 ** finder-function. The finder-function returns a pointer to the
6743 ** sqlite_io_methods object that implements the desired locking
6744 ** behaviors. See the division above that contains the IOMETHODS
6745 ** macro for addition information on finder-functions.
6747 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
6748 ** object. But the "autolockIoFinder" available on MacOSX does a little
6749 ** more than that; it looks at the filesystem type that hosts the
6750 ** database file and tries to choose an locking method appropriate for
6751 ** that filesystem time.
6753 #define UNIXVFS(VFSNAME, FINDER) { \
6755 sizeof(unixFile), /* szOsFile */ \
6756 MAX_PATHNAME, /* mxPathname */ \
6758 VFSNAME, /* zName */ \
6759 (void*)&FINDER, /* pAppData */ \
6760 unixOpen, /* xOpen */ \
6761 unixDelete, /* xDelete */ \
6762 unixAccess, /* xAccess */ \
6763 unixFullPathname, /* xFullPathname */ \
6764 unixDlOpen, /* xDlOpen */ \
6765 unixDlError, /* xDlError */ \
6766 unixDlSym, /* xDlSym */ \
6767 unixDlClose, /* xDlClose */ \
6768 unixRandomness, /* xRandomness */ \
6769 unixSleep, /* xSleep */ \
6770 unixCurrentTime, /* xCurrentTime */ \
6771 unixGetLastError, /* xGetLastError */ \
6772 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
6773 unixSetSystemCall, /* xSetSystemCall */ \
6774 unixGetSystemCall, /* xGetSystemCall */ \
6775 unixNextSystemCall, /* xNextSystemCall */ \
6779 ** All default VFSes for unix are contained in the following array.
6781 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
6782 ** by the SQLite core when the VFS is registered. So the following
6783 ** array cannot be const.
6785 static sqlite3_vfs aVfs
[] = {
6786 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
6787 UNIXVFS("unix", autolockIoFinder
),
6789 UNIXVFS("unix", posixIoFinder
),
6791 UNIXVFS("unix-none", nolockIoFinder
),
6792 UNIXVFS("unix-dotfile", dotlockIoFinder
),
6793 UNIXVFS("unix-excl", posixIoFinder
),
6795 UNIXVFS("unix-namedsem", semIoFinder
),
6797 #if SQLITE_ENABLE_LOCKING_STYLE
6798 UNIXVFS("unix-posix", posixIoFinder
),
6800 UNIXVFS("unix-flock", flockIoFinder
),
6803 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
6804 UNIXVFS("unix-afp", afpIoFinder
),
6805 UNIXVFS("unix-nfs", nfsIoFinder
),
6806 UNIXVFS("unix-proxy", proxyIoFinder
),
6809 unsigned int i
; /* Loop counter */
6811 /* Double-check that the aSyscall[] array has been constructed
6812 ** correctly. See ticket [bb3a86e890c8e96ab] */
6813 assert( ArraySize(aSyscall
)==20 );
6815 /* Register all VFSes defined in the aVfs[] array */
6816 for(i
=0; i
<(sizeof(aVfs
)/sizeof(sqlite3_vfs
)); i
++){
6817 sqlite3_vfs_register(&aVfs
[i
], i
==0);
6823 ** Shutdown the operating system interface.
6825 ** Some operating systems might need to do some cleanup in this routine,
6826 ** to release dynamically allocated objects. But not on unix.
6827 ** This routine is a no-op for unix.
6829 int sqlite3_os_end(void){
6833 #endif /* SQLITE_OS_UNIX */