drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 13 | ** This file contains the VFS implementation for unix-like operating systems |
| 14 | ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 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. |
| 21 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 22 | ** This source file is organized into divisions where the logic for various |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 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. |
| 26 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 27 | ** The layout of divisions is as follows: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** * General-purpose declarations and utility functions. |
| 30 | ** * Unique file ID logic used by VxWorks. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 31 | ** * Various locking primitive implementations (all except proxy locking): |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 32 | ** + for Posix Advisory Locks |
| 33 | ** + for no-op locks |
| 34 | ** + for dot-file locks |
| 35 | ** + for flock() locking |
| 36 | ** + for named semaphore locks (VxWorks only) |
| 37 | ** + for AFP filesystem locks (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 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. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 41 | ** * sqlite3_vfs method implementations. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 42 | ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 43 | ** * Definitions of sqlite3_vfs objects for all locking methods |
| 44 | ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 45 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 46 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 48 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 49 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 50 | ** There are various methods for file locking used for concurrency |
| 51 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 52 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 53 | ** 1. POSIX locking (the default), |
| 54 | ** 2. No locking, |
| 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) |
| 60 | ** |
| 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. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 65 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 66 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 67 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 68 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 69 | # else |
| 70 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 71 | # endif |
| 72 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 73 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 74 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 75 | ** Define the OS_VXWORKS pre-processor macro to 1 if building on |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 76 | ** vxworks, or 0 otherwise. |
| 77 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 78 | #ifndef OS_VXWORKS |
| 79 | # if defined(__RTP__) || defined(_WRS_KERNEL) |
| 80 | # define OS_VXWORKS 1 |
| 81 | # else |
| 82 | # define OS_VXWORKS 0 |
| 83 | # endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 87 | ** These #defines should enable >2GB file support on Posix if the |
| 88 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 89 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 90 | ** |
| 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. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 98 | ** |
| 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. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 103 | */ |
| 104 | #ifndef SQLITE_DISABLE_LFS |
| 105 | # define _LARGE_FILE 1 |
| 106 | # ifndef _FILE_OFFSET_BITS |
| 107 | # define _FILE_OFFSET_BITS 64 |
| 108 | # endif |
| 109 | # define _LARGEFILE_SOURCE 1 |
| 110 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 111 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 112 | /* |
| 113 | ** standard include files. |
| 114 | */ |
| 115 | #include <sys/types.h> |
| 116 | #include <sys/stat.h> |
| 117 | #include <fcntl.h> |
| 118 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 119 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 120 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 121 | #include <errno.h> |
dan | 32c12fe | 2013-05-02 17:37:31 +0000 | [diff] [blame] | 122 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 123 | #include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 124 | #endif |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 125 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 126 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 127 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 128 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 129 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 130 | # include <semaphore.h> |
| 131 | # include <limits.h> |
| 132 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 133 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 134 | # include <sys/param.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 135 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 136 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 137 | |
drh | f8b4d8c | 2010-03-05 13:53:22 +0000 | [diff] [blame] | 138 | #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 139 | # include <sys/mount.h> |
| 140 | #endif |
| 141 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 142 | #ifdef HAVE_UTIME |
| 143 | # include <utime.h> |
| 144 | #endif |
| 145 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 146 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 147 | ** Allowed values of unixFile.fsFlags |
| 148 | */ |
| 149 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 150 | |
| 151 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 152 | ** If we are to be thread-safe, include the pthreads header and define |
| 153 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 154 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 155 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 156 | # include <pthread.h> |
| 157 | # define SQLITE_UNIX_THREADS 1 |
| 158 | #endif |
| 159 | |
| 160 | /* |
| 161 | ** Default permissions when creating a new file |
| 162 | */ |
| 163 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 164 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 165 | #endif |
| 166 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 167 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 168 | ** Default permissions when creating auto proxy dir |
| 169 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 170 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 171 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 172 | #endif |
| 173 | |
| 174 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 175 | ** Maximum supported path-length. |
| 176 | */ |
| 177 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 178 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 179 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 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 |
| 182 | */ |
| 183 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 184 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 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 */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 190 | |
| 191 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 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. |
| 196 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 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 */ |
| 201 | }; |
| 202 | |
| 203 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 204 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 205 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 206 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 207 | typedef struct unixFile unixFile; |
| 208 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 209 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 210 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 211 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 212 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 213 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 214 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 215 | int lastErrno; /* The unix errno from last I/O error */ |
| 216 | void *lockingContext; /* Locking style specific state */ |
| 217 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 218 | const char *zPath; /* Name of the file */ |
| 219 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 220 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 221 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 222 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 223 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 224 | sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 225 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 226 | void *pMapRegion; /* Memory mapped region */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 227 | #endif |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 228 | #ifdef __QNXNTO__ |
| 229 | int sectorSize; /* Device sector size */ |
| 230 | int deviceCharacteristics; /* Precomputed device characteristics */ |
| 231 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 232 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 233 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 234 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 235 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 236 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 237 | #endif |
| 238 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 239 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 240 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 241 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 242 | /* The next group of variables are used to track whether or not the |
| 243 | ** transaction counter in bytes 24-27 of database files are updated |
| 244 | ** whenever any part of the database changes. An assertion fault will |
| 245 | ** occur if a file is updated without also updating the transaction |
| 246 | ** counter. This test is made to avoid new problems similar to the |
| 247 | ** one described by ticket #3584. |
| 248 | */ |
| 249 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 250 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 251 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 252 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 253 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 254 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 255 | #ifdef SQLITE_TEST |
| 256 | /* In test mode, increase the size of this structure a bit so that |
| 257 | ** it is larger than the struct CrashFile defined in test6.c. |
| 258 | */ |
| 259 | char aPadding[32]; |
| 260 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 261 | }; |
| 262 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame^] | 263 | /* This variable holds the process id (pid) from when the xRandomness() |
| 264 | ** method was called. If xOpen() is called from a different process id, |
| 265 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 266 | */ |
| 267 | static int randomnessPid = 0; |
| 268 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 269 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 270 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 271 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 272 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 273 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 274 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 275 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 276 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 277 | #else |
| 278 | # define UNIXFILE_DIRSYNC 0x00 |
| 279 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 280 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 281 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 282 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 283 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 284 | #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings have been issued */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 285 | |
| 286 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 287 | ** Include code that is common to all os_*.c files |
| 288 | */ |
| 289 | #include "os_common.h" |
| 290 | |
| 291 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 292 | ** Define various macros that are missing from some systems. |
| 293 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 294 | #ifndef O_LARGEFILE |
| 295 | # define O_LARGEFILE 0 |
| 296 | #endif |
| 297 | #ifdef SQLITE_DISABLE_LFS |
| 298 | # undef O_LARGEFILE |
| 299 | # define O_LARGEFILE 0 |
| 300 | #endif |
| 301 | #ifndef O_NOFOLLOW |
| 302 | # define O_NOFOLLOW 0 |
| 303 | #endif |
| 304 | #ifndef O_BINARY |
| 305 | # define O_BINARY 0 |
| 306 | #endif |
| 307 | |
| 308 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 309 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 310 | ** testing and debugging only. |
| 311 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 312 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 313 | #define threadid pthread_self() |
| 314 | #else |
| 315 | #define threadid 0 |
| 316 | #endif |
| 317 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 318 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 319 | ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 320 | */ |
| 321 | #if !defined(HAVE_MREMAP) |
| 322 | # if defined(__linux__) && defined(_GNU_SOURCE) |
| 323 | # define HAVE_MREMAP 1 |
| 324 | # else |
| 325 | # define HAVE_MREMAP 0 |
| 326 | # endif |
| 327 | #endif |
| 328 | |
| 329 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 330 | ** Different Unix systems declare open() in different ways. Same use |
| 331 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 332 | ** The difference is important when using a pointer to the function. |
| 333 | ** |
| 334 | ** The safest way to deal with the problem is to always use this wrapper |
| 335 | ** which always has the same well-defined interface. |
| 336 | */ |
| 337 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 338 | return open(zFile, flags, mode); |
| 339 | } |
| 340 | |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 341 | /* |
| 342 | ** On some systems, calls to fchown() will trigger a message in a security |
| 343 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 344 | ** we are not running as root. |
| 345 | */ |
| 346 | static int posixFchown(int fd, uid_t uid, gid_t gid){ |
| 347 | return geteuid() ? 0 : fchown(fd,uid,gid); |
| 348 | } |
| 349 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 350 | /* Forward reference */ |
| 351 | static int openDirectory(const char*, int*); |
| 352 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 353 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 354 | ** Many system calls are accessed through pointer-to-functions so that |
| 355 | ** they may be overridden at runtime to facilitate fault injection during |
| 356 | ** testing and sandboxing. The following array holds the names and pointers |
| 357 | ** to all overrideable system calls. |
| 358 | */ |
| 359 | static struct unix_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 360 | const char *zName; /* Name of the system call */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 361 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 362 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 363 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 364 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 365 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 366 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 367 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 368 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 369 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 370 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 371 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 372 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 373 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 374 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 375 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 376 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 377 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 378 | |
| 379 | /* |
| 380 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 381 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 382 | ** that always succeeds. This means that locking does not occur under |
| 383 | ** DJGPP. But it is DOS - what did you expect? |
| 384 | */ |
| 385 | #ifdef __DJGPP__ |
| 386 | { "fstat", 0, 0 }, |
| 387 | #define osFstat(a,b,c) 0 |
| 388 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 389 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 390 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 391 | #endif |
| 392 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 393 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 394 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 395 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 396 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 397 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 398 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 399 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 400 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 401 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 402 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 403 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 404 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 405 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 406 | #endif |
| 407 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 408 | |
| 409 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 410 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 411 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 412 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 413 | #endif |
| 414 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 415 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 416 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 417 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 418 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 419 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 420 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 421 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 422 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 423 | #endif |
| 424 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 425 | aSyscall[12].pCurrent) |
| 426 | |
| 427 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 428 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 429 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 430 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 431 | #endif |
| 432 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 433 | aSyscall[13].pCurrent) |
| 434 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 435 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 436 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 437 | |
| 438 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 439 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 440 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 441 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 442 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 443 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 444 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 445 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 446 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 447 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 448 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 449 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 450 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 451 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 452 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 453 | |
| 454 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 455 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 456 | |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 457 | { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 }, |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 458 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 459 | |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 460 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
dan | 893c0ff | 2013-03-25 19:05:07 +0000 | [diff] [blame] | 461 | { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
| 462 | #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent) |
| 463 | |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 464 | { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
| 465 | #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent) |
| 466 | |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 467 | #if HAVE_MREMAP |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 468 | { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 469 | #else |
| 470 | { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 471 | #endif |
| 472 | #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent) |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 473 | #endif |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 474 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 475 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 479 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 480 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 481 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 482 | */ |
| 483 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 484 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 485 | const char *zName, /* Name of system call to override */ |
| 486 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 487 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 488 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 489 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 490 | |
| 491 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 492 | if( zName==0 ){ |
| 493 | /* If no zName is given, restore all system calls to their default |
| 494 | ** settings and return NULL |
| 495 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 496 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 497 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 498 | if( aSyscall[i].pDefault ){ |
| 499 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | }else{ |
| 503 | /* If zName is specified, operate on only the one system call |
| 504 | ** specified. |
| 505 | */ |
| 506 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 507 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 508 | if( aSyscall[i].pDefault==0 ){ |
| 509 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 510 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 511 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 512 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 513 | aSyscall[i].pCurrent = pNewFunc; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return rc; |
| 519 | } |
| 520 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 521 | /* |
| 522 | ** Return the value of a system call. Return NULL if zName is not a |
| 523 | ** recognized system call name. NULL is also returned if the system call |
| 524 | ** is currently undefined. |
| 525 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 526 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 527 | sqlite3_vfs *pNotUsed, |
| 528 | const char *zName |
| 529 | ){ |
| 530 | unsigned int i; |
| 531 | |
| 532 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 533 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 534 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | ** Return the name of the first system call after zName. If zName==NULL |
| 541 | ** then return the name of the first system call. Return NULL if zName |
| 542 | ** is the last system call or if zName is not the name of a valid |
| 543 | ** system call. |
| 544 | */ |
| 545 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 546 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 547 | |
| 548 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 549 | if( zName ){ |
| 550 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 551 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 554 | for(i++; i<ArraySize(aSyscall); i++){ |
| 555 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 556 | } |
| 557 | return 0; |
| 558 | } |
| 559 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 560 | /* |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 561 | ** Do not accept any file descriptor less than this value, in order to avoid |
| 562 | ** opening database file using file descriptors that are commonly used for |
| 563 | ** standard input, output, and error. |
| 564 | */ |
| 565 | #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 566 | # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 567 | #endif |
| 568 | |
| 569 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 570 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 571 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 572 | ** |
| 573 | ** If the file creation mode "m" is 0 then set it to the default for |
| 574 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 575 | ** 0644) as modified by the system umask. If m is not 0, then |
| 576 | ** make the file creation mode be exactly m ignoring the umask. |
| 577 | ** |
| 578 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 579 | ** and -shm files. We want those files to have *exactly* the same |
| 580 | ** permissions as their original database, unadulterated by the umask. |
| 581 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 582 | ** transaction crashes and leaves behind hot journals, then any |
| 583 | ** process that is able to write to the database will also be able to |
| 584 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 585 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 586 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 587 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 588 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 589 | while(1){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 590 | #if defined(O_CLOEXEC) |
| 591 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 592 | #else |
| 593 | fd = osOpen(z,f,m2); |
| 594 | #endif |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 595 | if( fd<0 ){ |
| 596 | if( errno==EINTR ) continue; |
| 597 | break; |
| 598 | } |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 599 | if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 600 | osClose(fd); |
| 601 | sqlite3_log(SQLITE_WARNING, |
| 602 | "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 603 | fd = -1; |
| 604 | if( osOpen("/dev/null", f, m)<0 ) break; |
| 605 | } |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 606 | if( fd>=0 ){ |
| 607 | if( m!=0 ){ |
| 608 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 609 | if( osFstat(fd, &statbuf)==0 |
| 610 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 611 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 612 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 613 | osFchmod(fd, m); |
| 614 | } |
| 615 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 616 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 617 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 618 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 619 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 620 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 621 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 622 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 623 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 624 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 625 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 626 | ** vxworksFileId objects used by this file, all of which may be |
| 627 | ** shared by multiple threads. |
| 628 | ** |
| 629 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 630 | ** is held when required. This function is only used as part of assert() |
| 631 | ** statements. e.g. |
| 632 | ** |
| 633 | ** unixEnterMutex() |
| 634 | ** assert( unixMutexHeld() ); |
| 635 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 636 | */ |
| 637 | static void unixEnterMutex(void){ |
| 638 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 639 | } |
| 640 | static void unixLeaveMutex(void){ |
| 641 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 642 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 643 | #ifdef SQLITE_DEBUG |
| 644 | static int unixMutexHeld(void) { |
| 645 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 646 | } |
| 647 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 648 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 649 | |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 650 | #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 651 | /* |
| 652 | ** Helper function for printing out trace information from debugging |
| 653 | ** binaries. This returns the string represetation of the supplied |
| 654 | ** integer lock-type. |
| 655 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 656 | static const char *azFileLock(int eFileLock){ |
| 657 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 658 | case NO_LOCK: return "NONE"; |
| 659 | case SHARED_LOCK: return "SHARED"; |
| 660 | case RESERVED_LOCK: return "RESERVED"; |
| 661 | case PENDING_LOCK: return "PENDING"; |
| 662 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 663 | } |
| 664 | return "ERROR"; |
| 665 | } |
| 666 | #endif |
| 667 | |
| 668 | #ifdef SQLITE_LOCK_TRACE |
| 669 | /* |
| 670 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 671 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 672 | ** This routine is used for troubleshooting locks on multithreaded |
| 673 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 674 | ** command-line option on the compiler. This code is normally |
| 675 | ** turned off. |
| 676 | */ |
| 677 | static int lockTrace(int fd, int op, struct flock *p){ |
| 678 | char *zOpName, *zType; |
| 679 | int s; |
| 680 | int savedErrno; |
| 681 | if( op==F_GETLK ){ |
| 682 | zOpName = "GETLK"; |
| 683 | }else if( op==F_SETLK ){ |
| 684 | zOpName = "SETLK"; |
| 685 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 686 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 687 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 688 | return s; |
| 689 | } |
| 690 | if( p->l_type==F_RDLCK ){ |
| 691 | zType = "RDLCK"; |
| 692 | }else if( p->l_type==F_WRLCK ){ |
| 693 | zType = "WRLCK"; |
| 694 | }else if( p->l_type==F_UNLCK ){ |
| 695 | zType = "UNLCK"; |
| 696 | }else{ |
| 697 | assert( 0 ); |
| 698 | } |
| 699 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 700 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 701 | savedErrno = errno; |
| 702 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 703 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 704 | (int)p->l_pid, s); |
| 705 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 706 | struct flock l2; |
| 707 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 708 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 709 | if( l2.l_type==F_RDLCK ){ |
| 710 | zType = "RDLCK"; |
| 711 | }else if( l2.l_type==F_WRLCK ){ |
| 712 | zType = "WRLCK"; |
| 713 | }else if( l2.l_type==F_UNLCK ){ |
| 714 | zType = "UNLCK"; |
| 715 | }else{ |
| 716 | assert( 0 ); |
| 717 | } |
| 718 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 719 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 720 | } |
| 721 | errno = savedErrno; |
| 722 | return s; |
| 723 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 724 | #undef osFcntl |
| 725 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 726 | #endif /* SQLITE_LOCK_TRACE */ |
| 727 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 728 | /* |
| 729 | ** Retry ftruncate() calls that fail due to EINTR |
| 730 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 731 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 732 | int rc; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 733 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 734 | return rc; |
| 735 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | ** This routine translates a standard POSIX errno code into something |
| 739 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 740 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 741 | ** and a variety of "please close the file descriptor NOW" errors into |
| 742 | ** SQLITE_IOERR |
| 743 | ** |
| 744 | ** Errors during initialization of locks, or file system support for locks, |
| 745 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 746 | */ |
| 747 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 748 | switch (posixError) { |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 749 | #if 0 |
| 750 | /* At one point this code was not commented out. In theory, this branch |
| 751 | ** should never be hit, as this function should only be called after |
| 752 | ** a locking-related function (i.e. fcntl()) has returned non-zero with |
| 753 | ** the value of errno as the first argument. Since a system call has failed, |
| 754 | ** errno should be non-zero. |
| 755 | ** |
| 756 | ** Despite this, if errno really is zero, we still don't want to return |
| 757 | ** SQLITE_OK. The system call failed, and *some* SQLite error should be |
| 758 | ** propagated back to the caller. Commenting this branch out means errno==0 |
| 759 | ** will be handled by the "default:" case below. |
| 760 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 761 | case 0: |
| 762 | return SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 763 | #endif |
| 764 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 765 | case EAGAIN: |
| 766 | case ETIMEDOUT: |
| 767 | case EBUSY: |
| 768 | case EINTR: |
| 769 | case ENOLCK: |
| 770 | /* random NFS retry error, unless during file system support |
| 771 | * introspection, in which it actually means what it says */ |
| 772 | return SQLITE_BUSY; |
| 773 | |
| 774 | case EACCES: |
| 775 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 776 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 777 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 778 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 779 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 780 | return SQLITE_BUSY; |
| 781 | } |
| 782 | /* else fall through */ |
| 783 | case EPERM: |
| 784 | return SQLITE_PERM; |
| 785 | |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 786 | /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And |
| 787 | ** this module never makes such a call. And the code in SQLite itself |
| 788 | ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons |
| 789 | ** this case is also commented out. If the system does set errno to EDEADLK, |
| 790 | ** the default SQLITE_IOERR_XXX code will be returned. */ |
| 791 | #if 0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 792 | case EDEADLK: |
| 793 | return SQLITE_IOERR_BLOCKED; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 794 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 795 | |
| 796 | #if EOPNOTSUPP!=ENOTSUP |
| 797 | case EOPNOTSUPP: |
| 798 | /* something went terribly awry, unless during file system support |
| 799 | * introspection, in which it actually means what it says */ |
| 800 | #endif |
| 801 | #ifdef ENOTSUP |
| 802 | case ENOTSUP: |
| 803 | /* invalid fd, unless during file system support introspection, in which |
| 804 | * it actually means what it says */ |
| 805 | #endif |
| 806 | case EIO: |
| 807 | case EBADF: |
| 808 | case EINVAL: |
| 809 | case ENOTCONN: |
| 810 | case ENODEV: |
| 811 | case ENXIO: |
| 812 | case ENOENT: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 813 | #ifdef ESTALE /* ESTALE is not defined on Interix systems */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 814 | case ESTALE: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 815 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 816 | case ENOSYS: |
| 817 | /* these should force the client to close the file and reconnect */ |
| 818 | |
| 819 | default: |
| 820 | return sqliteIOErr; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 825 | /****************************************************************************** |
| 826 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 827 | ** |
| 828 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 829 | ** the device number and the inode number. But this does not work on VxWorks. |
| 830 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 831 | ** |
| 832 | ** A pointer to an instance of the following structure can be used as a |
| 833 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 834 | ** a copy of the canonical filename. There is also a reference count. |
| 835 | ** The structure is reclaimed when the number of pointers to it drops to |
| 836 | ** zero. |
| 837 | ** |
| 838 | ** There are never very many files open at one time and lookups are not |
| 839 | ** a performance-critical path, so it is sufficient to put these |
| 840 | ** structures on a linked list. |
| 841 | */ |
| 842 | struct vxworksFileId { |
| 843 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 844 | int nRef; /* Number of references to this one */ |
| 845 | int nName; /* Length of the zCanonicalName[] string */ |
| 846 | char *zCanonicalName; /* Canonical filename */ |
| 847 | }; |
| 848 | |
| 849 | #if OS_VXWORKS |
| 850 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 851 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 852 | ** variable: |
| 853 | */ |
| 854 | static struct vxworksFileId *vxworksFileList = 0; |
| 855 | |
| 856 | /* |
| 857 | ** Simplify a filename into its canonical form |
| 858 | ** by making the following changes: |
| 859 | ** |
| 860 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 861 | ** * convert /./ into just / |
| 862 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 863 | ** |
| 864 | ** Changes are made in-place. Return the new name length. |
| 865 | ** |
| 866 | ** The original filename is in z[0..n-1]. Return the number of |
| 867 | ** characters in the simplified name. |
| 868 | */ |
| 869 | static int vxworksSimplifyName(char *z, int n){ |
| 870 | int i, j; |
| 871 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 872 | for(i=j=0; i<n; i++){ |
| 873 | if( z[i]=='/' ){ |
| 874 | if( z[i+1]=='/' ) continue; |
| 875 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 876 | i += 1; |
| 877 | continue; |
| 878 | } |
| 879 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 880 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 881 | if( j>0 ){ j--; } |
| 882 | i += 2; |
| 883 | continue; |
| 884 | } |
| 885 | } |
| 886 | z[j++] = z[i]; |
| 887 | } |
| 888 | z[j] = 0; |
| 889 | return j; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | ** Find a unique file ID for the given absolute pathname. Return |
| 894 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 895 | ** file ID. |
| 896 | ** |
| 897 | ** The nRef field of the vxworksFileId object is incremented before |
| 898 | ** the object is returned. A new vxworksFileId object is created |
| 899 | ** and added to the global list if necessary. |
| 900 | ** |
| 901 | ** If a memory allocation error occurs, return NULL. |
| 902 | */ |
| 903 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 904 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 905 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 906 | int n; /* Length of zAbsoluteName string */ |
| 907 | |
| 908 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 909 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 910 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 911 | if( pNew==0 ) return 0; |
| 912 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 913 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 914 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 915 | |
| 916 | /* Search for an existing entry that matching the canonical name. |
| 917 | ** If found, increment the reference count and return a pointer to |
| 918 | ** the existing file ID. |
| 919 | */ |
| 920 | unixEnterMutex(); |
| 921 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 922 | if( pCandidate->nName==n |
| 923 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 924 | ){ |
| 925 | sqlite3_free(pNew); |
| 926 | pCandidate->nRef++; |
| 927 | unixLeaveMutex(); |
| 928 | return pCandidate; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /* No match was found. We will make a new file ID */ |
| 933 | pNew->nRef = 1; |
| 934 | pNew->nName = n; |
| 935 | pNew->pNext = vxworksFileList; |
| 936 | vxworksFileList = pNew; |
| 937 | unixLeaveMutex(); |
| 938 | return pNew; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | ** Decrement the reference count on a vxworksFileId object. Free |
| 943 | ** the object when the reference count reaches zero. |
| 944 | */ |
| 945 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 946 | unixEnterMutex(); |
| 947 | assert( pId->nRef>0 ); |
| 948 | pId->nRef--; |
| 949 | if( pId->nRef==0 ){ |
| 950 | struct vxworksFileId **pp; |
| 951 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 952 | assert( *pp==pId ); |
| 953 | *pp = pId->pNext; |
| 954 | sqlite3_free(pId); |
| 955 | } |
| 956 | unixLeaveMutex(); |
| 957 | } |
| 958 | #endif /* OS_VXWORKS */ |
| 959 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 960 | ******************************************************************************/ |
| 961 | |
| 962 | |
| 963 | /****************************************************************************** |
| 964 | *************************** Posix Advisory Locking **************************** |
| 965 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 966 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 967 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 968 | ** sets or clears a lock, that operation overrides any prior locks set |
| 969 | ** by the same process. It does not explicitly say so, but this implies |
| 970 | ** that it overrides locks set by the same process using a different |
| 971 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 972 | ** |
| 973 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 974 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 975 | ** |
| 976 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 977 | ** one is a hard or symbolic link to the other) then if you set |
| 978 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 979 | ** on fd2, it works. I would have expected the second lock to |
| 980 | ** fail since there was already a lock on the file due to fd1. |
| 981 | ** But not so. Since both locks came from the same process, the |
| 982 | ** second overrides the first, even though they were on different |
| 983 | ** file descriptors opened on different file names. |
| 984 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 985 | ** This means that we cannot use POSIX locks to synchronize file access |
| 986 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 987 | ** to synchronize access for threads in separate processes, but not |
| 988 | ** threads within the same process. |
| 989 | ** |
| 990 | ** To work around the problem, SQLite has to manage file locks internally |
| 991 | ** on its own. Whenever a new database is opened, we have to find the |
| 992 | ** specific inode of the database file (the inode is determined by the |
| 993 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 994 | ** and check for locks already existing on that inode. When locks are |
| 995 | ** created or removed, we have to look at our own internal record of the |
| 996 | ** locks to see if another thread has previously set a lock on that same |
| 997 | ** inode. |
| 998 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 999 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 1000 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 1001 | ** canonical filename and implemented in the previous division.) |
| 1002 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1003 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1004 | ** descriptor. It is now a structure that holds the integer file |
| 1005 | ** descriptor and a pointer to a structure that describes the internal |
| 1006 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1007 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1008 | ** point to the same locking structure. The locking structure keeps |
| 1009 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 1010 | ** field that tells us its internal lock status. cnt==0 means the |
| 1011 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 1012 | ** cnt>0 means there are cnt shared locks on the file. |
| 1013 | ** |
| 1014 | ** Any attempt to lock or unlock a file first checks the locking |
| 1015 | ** structure. The fcntl() system call is only invoked to set a |
| 1016 | ** POSIX lock if the internal lock structure transitions between |
| 1017 | ** a locked and an unlocked state. |
| 1018 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1019 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1020 | ** |
| 1021 | ** If you close a file descriptor that points to a file that has locks, |
| 1022 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1023 | ** released. To work around this problem, each unixInodeInfo object |
| 1024 | ** maintains a count of the number of pending locks on tha inode. |
| 1025 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1026 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1027 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1028 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1029 | ** be closed and that list is walked (and cleared) when the last lock |
| 1030 | ** clears. |
| 1031 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1032 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1033 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1034 | ** Many older versions of linux use the LinuxThreads library which is |
| 1035 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1036 | ** A cannot be modified or overridden by a different thread B. |
| 1037 | ** Only thread A can modify the lock. Locking behavior is correct |
| 1038 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 1039 | ** on linux - with NPTL a lock created by thread A can override locks |
| 1040 | ** in thread B. But there is no way to know at compile-time which |
| 1041 | ** threading library is being used. So there is no way to know at |
| 1042 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1043 | ** One has to do a run-time check to discover the behavior of the |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1044 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1045 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1046 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1047 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1048 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1049 | ** per database file in the same process and (2) database connections |
| 1050 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1051 | */ |
| 1052 | |
| 1053 | /* |
| 1054 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1055 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1056 | */ |
| 1057 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1058 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1059 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1060 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1061 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1062 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1063 | #endif |
| 1064 | }; |
| 1065 | |
| 1066 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1067 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1068 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 1069 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1070 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1071 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1072 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1073 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1074 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1075 | struct unixInodeInfo { |
| 1076 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1077 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1078 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1079 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1080 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1081 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 1082 | int nLock; /* Number of outstanding file locks */ |
| 1083 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 1084 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1085 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1086 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1087 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1088 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1089 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1090 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1091 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1092 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1093 | }; |
| 1094 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1095 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1096 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1097 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1098 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1099 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1100 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1101 | ** |
| 1102 | ** This function - unixLogError_x(), is only ever called via the macro |
| 1103 | ** unixLogError(). |
| 1104 | ** |
| 1105 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1106 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1107 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1108 | ** strerror_r(). |
| 1109 | ** |
| 1110 | ** The first argument passed to the macro should be the error code that |
| 1111 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1112 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1113 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1114 | ** if any. |
| 1115 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1116 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1117 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1118 | int errcode, /* SQLite error code */ |
| 1119 | const char *zFunc, /* Name of OS function that failed */ |
| 1120 | const char *zPath, /* File path associated with error */ |
| 1121 | int iLine /* Source line number where error occurred */ |
| 1122 | ){ |
| 1123 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1124 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1125 | |
| 1126 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1127 | ** the strerror() function to obtain the human-readable error message |
| 1128 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1129 | */ |
| 1130 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1131 | char aErr[80]; |
| 1132 | memset(aErr, 0, sizeof(aErr)); |
| 1133 | zErr = aErr; |
| 1134 | |
| 1135 | /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1136 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1137 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1138 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1139 | ** Otherwise, assume that the system provides the POSIX version of |
| 1140 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1141 | ** |
| 1142 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1143 | ** available, the error message will often be an empty string. Not a |
| 1144 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1145 | ** could lead to a segfault though. |
| 1146 | */ |
| 1147 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1148 | zErr = |
| 1149 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1150 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1151 | |
| 1152 | #elif SQLITE_THREADSAFE |
| 1153 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1154 | zErr = ""; |
| 1155 | #else |
| 1156 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1157 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1158 | #endif |
| 1159 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1160 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1161 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1162 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1163 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1164 | ); |
| 1165 | |
| 1166 | return errcode; |
| 1167 | } |
| 1168 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1169 | /* |
| 1170 | ** Close a file descriptor. |
| 1171 | ** |
| 1172 | ** We assume that close() almost always works, since it is only in a |
| 1173 | ** very sick application or on a very sick platform that it might fail. |
| 1174 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1175 | ** error. |
| 1176 | ** |
| 1177 | ** Note that it is not safe to retry close() after EINTR since the |
| 1178 | ** file descriptor might have already been reused by another thread. |
| 1179 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1180 | ** and move on. |
| 1181 | */ |
| 1182 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1183 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1184 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1185 | pFile ? pFile->zPath : 0, lineno); |
| 1186 | } |
| 1187 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1190 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1191 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1192 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1193 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1194 | UnixUnusedFd *p; |
| 1195 | UnixUnusedFd *pNext; |
| 1196 | for(p=pInode->pUnused; p; p=pNext){ |
| 1197 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1198 | robust_close(pFile, p->fd, __LINE__); |
| 1199 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1200 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1201 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1205 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1206 | ** |
| 1207 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1208 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1209 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1210 | static void releaseInodeInfo(unixFile *pFile){ |
| 1211 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1212 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1213 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1214 | pInode->nRef--; |
| 1215 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1216 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1217 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1218 | if( pInode->pPrev ){ |
| 1219 | assert( pInode->pPrev->pNext==pInode ); |
| 1220 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1221 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1222 | assert( inodeList==pInode ); |
| 1223 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1224 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1225 | if( pInode->pNext ){ |
| 1226 | assert( pInode->pNext->pPrev==pInode ); |
| 1227 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1228 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1229 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1230 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1235 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1236 | ** describes that file descriptor. Create a new one if necessary. The |
| 1237 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1238 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1239 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1240 | ** when this function is called. |
| 1241 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1242 | ** Return an appropriate error code. |
| 1243 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1244 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1245 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1246 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1247 | ){ |
| 1248 | int rc; /* System call return code */ |
| 1249 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1250 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1251 | struct stat statbuf; /* Low-level file information */ |
| 1252 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1253 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1254 | assert( unixMutexHeld() ); |
| 1255 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1256 | /* Get low-level information about the file that we can used to |
| 1257 | ** create a unique name for the file. |
| 1258 | */ |
| 1259 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1260 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1261 | if( rc!=0 ){ |
| 1262 | pFile->lastErrno = errno; |
| 1263 | #ifdef EOVERFLOW |
| 1264 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1265 | #endif |
| 1266 | return SQLITE_IOERR; |
| 1267 | } |
| 1268 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1269 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1270 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1271 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1272 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1273 | ** we always increase the file size to 1 by writing a single byte |
| 1274 | ** prior to accessing the inode number. The one byte written is |
| 1275 | ** an ASCII 'S' character which also happens to be the first byte |
| 1276 | ** in the header of every SQLite database. In this way, if there |
| 1277 | ** is a race condition such that another thread has already populated |
| 1278 | ** the first page of the database, no damage is done. |
| 1279 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1280 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1281 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1282 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1283 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1284 | return SQLITE_IOERR; |
| 1285 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1286 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1287 | if( rc!=0 ){ |
| 1288 | pFile->lastErrno = errno; |
| 1289 | return SQLITE_IOERR; |
| 1290 | } |
| 1291 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1292 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1293 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1294 | memset(&fileId, 0, sizeof(fileId)); |
| 1295 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1296 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1297 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1298 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1299 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1300 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1301 | pInode = inodeList; |
| 1302 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1303 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1304 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1305 | if( pInode==0 ){ |
| 1306 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 1307 | if( pInode==0 ){ |
| 1308 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1309 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1310 | memset(pInode, 0, sizeof(*pInode)); |
| 1311 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1312 | pInode->nRef = 1; |
| 1313 | pInode->pNext = inodeList; |
| 1314 | pInode->pPrev = 0; |
| 1315 | if( inodeList ) inodeList->pPrev = pInode; |
| 1316 | inodeList = pInode; |
| 1317 | }else{ |
| 1318 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1319 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1320 | *ppInode = pInode; |
| 1321 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1322 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1323 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1324 | /* |
| 1325 | ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 1326 | */ |
| 1327 | static int fileHasMoved(unixFile *pFile){ |
| 1328 | struct stat buf; |
| 1329 | return pFile->pInode!=0 && |
| 1330 | (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino); |
| 1331 | } |
| 1332 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1333 | |
| 1334 | /* |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1335 | ** Check a unixFile that is a database. Verify the following: |
| 1336 | ** |
| 1337 | ** (1) There is exactly one hard link on the file |
| 1338 | ** (2) The file is not a symbolic link |
| 1339 | ** (3) The file has not been renamed or unlinked |
| 1340 | ** |
| 1341 | ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 1342 | */ |
| 1343 | static void verifyDbFile(unixFile *pFile){ |
| 1344 | struct stat buf; |
| 1345 | int rc; |
| 1346 | if( pFile->ctrlFlags & UNIXFILE_WARNED ){ |
| 1347 | /* One or more of the following warnings have already been issued. Do not |
| 1348 | ** repeat them so as not to clutter the error log */ |
| 1349 | return; |
| 1350 | } |
| 1351 | rc = osFstat(pFile->h, &buf); |
| 1352 | if( rc!=0 ){ |
| 1353 | sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
| 1354 | pFile->ctrlFlags |= UNIXFILE_WARNED; |
| 1355 | return; |
| 1356 | } |
| 1357 | if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){ |
| 1358 | sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
| 1359 | pFile->ctrlFlags |= UNIXFILE_WARNED; |
| 1360 | return; |
| 1361 | } |
| 1362 | if( buf.st_nlink>1 ){ |
| 1363 | sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
| 1364 | pFile->ctrlFlags |= UNIXFILE_WARNED; |
| 1365 | return; |
| 1366 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1367 | if( fileHasMoved(pFile) ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1368 | sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
| 1369 | pFile->ctrlFlags |= UNIXFILE_WARNED; |
| 1370 | return; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | |
| 1375 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1376 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1377 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1378 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1379 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1380 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1381 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1382 | int rc = SQLITE_OK; |
| 1383 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1384 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1385 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1386 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1387 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1388 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1389 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1392 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1393 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1396 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1397 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1398 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1399 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1400 | struct flock lock; |
| 1401 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1402 | lock.l_start = RESERVED_BYTE; |
| 1403 | lock.l_len = 1; |
| 1404 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1405 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1406 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
| 1407 | pFile->lastErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1408 | } else if( lock.l_type!=F_UNLCK ){ |
| 1409 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1412 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1413 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1414 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1415 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1416 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1417 | *pResOut = reserved; |
| 1418 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1422 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1423 | ** described by pLock. |
| 1424 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1425 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1426 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1427 | ** the first time any lock is attempted. All subsequent system locking |
| 1428 | ** operations become no-ops. Locking operations still happen internally, |
| 1429 | ** in order to coordinate access between separate database connections |
| 1430 | ** within this process, but all of that is handled in memory and the |
| 1431 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1432 | ** |
| 1433 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1434 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1435 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1436 | ** |
| 1437 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1438 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1439 | */ |
| 1440 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1441 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1442 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1443 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1444 | assert( pInode!=0 ); |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1445 | if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock) |
| 1446 | && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0) |
| 1447 | ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1448 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1449 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1450 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1451 | lock.l_whence = SEEK_SET; |
| 1452 | lock.l_start = SHARED_FIRST; |
| 1453 | lock.l_len = SHARED_SIZE; |
| 1454 | lock.l_type = F_WRLCK; |
| 1455 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1456 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1457 | pInode->bProcessLock = 1; |
| 1458 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1459 | }else{ |
| 1460 | rc = 0; |
| 1461 | } |
| 1462 | }else{ |
| 1463 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1464 | } |
| 1465 | return rc; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1469 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1470 | ** of the following: |
| 1471 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1472 | ** (1) SHARED_LOCK |
| 1473 | ** (2) RESERVED_LOCK |
| 1474 | ** (3) PENDING_LOCK |
| 1475 | ** (4) EXCLUSIVE_LOCK |
| 1476 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1477 | ** Sometimes when requesting one lock state, additional lock states |
| 1478 | ** are inserted in between. The locking might fail on one of the later |
| 1479 | ** transitions leaving the lock state different from what it started but |
| 1480 | ** still short of its goal. The following chart shows the allowed |
| 1481 | ** transitions and the inserted intermediate states: |
| 1482 | ** |
| 1483 | ** UNLOCKED -> SHARED |
| 1484 | ** SHARED -> RESERVED |
| 1485 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1486 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1487 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1488 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1489 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1490 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1491 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1492 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1493 | /* The following describes the implementation of the various locks and |
| 1494 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1495 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1496 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1497 | ** slightly in order to be compatible with windows systems simultaneously |
| 1498 | ** accessing the same database file, in case that is ever required. |
| 1499 | ** |
| 1500 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1501 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1502 | ** range', a range of 510 bytes at a well known offset. |
| 1503 | ** |
| 1504 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1505 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1506 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1507 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1508 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1509 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1510 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1511 | ** |
| 1512 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1513 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1514 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1515 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1516 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1517 | ** This property is used by the algorithm for rolling back a journal file |
| 1518 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1519 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1520 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1521 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1522 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1523 | ** within this range, this ensures that no other locks are held on the |
| 1524 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1525 | ** |
| 1526 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1527 | ** range' is that some versions of windows do not support read-locks. By |
| 1528 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1529 | ** even if the locking primitive used is always a write-lock. |
| 1530 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1531 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1532 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1533 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1534 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1535 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1536 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1537 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1538 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1539 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1540 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1541 | |
| 1542 | /* If there is already a lock of this type or more restrictive on the |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1543 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1544 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1545 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1546 | if( pFile->eFileLock>=eFileLock ){ |
| 1547 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1548 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1549 | return SQLITE_OK; |
| 1550 | } |
| 1551 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1552 | /* Make sure the locking sequence is correct. |
| 1553 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1554 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1555 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1556 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1557 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1558 | assert( eFileLock!=PENDING_LOCK ); |
| 1559 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1560 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1561 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1562 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1563 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1564 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1565 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1566 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1567 | ** handle that precludes the requested lock, return BUSY. |
| 1568 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1569 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1570 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1571 | ){ |
| 1572 | rc = SQLITE_BUSY; |
| 1573 | goto end_lock; |
| 1574 | } |
| 1575 | |
| 1576 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1577 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1578 | ** return SQLITE_OK. |
| 1579 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1580 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1581 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1582 | assert( eFileLock==SHARED_LOCK ); |
| 1583 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1584 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1585 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1586 | pInode->nShared++; |
| 1587 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1588 | goto end_lock; |
| 1589 | } |
| 1590 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1591 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1592 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1593 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1594 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1595 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1596 | lock.l_len = 1L; |
| 1597 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1598 | if( eFileLock==SHARED_LOCK |
| 1599 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1600 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1601 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1602 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1603 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1604 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1605 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1606 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1607 | pFile->lastErrno = tErrno; |
| 1608 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1609 | goto end_lock; |
| 1610 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | /* If control gets to this point, then actually go ahead and make |
| 1615 | ** operating system calls for the specified lock. |
| 1616 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1617 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1618 | assert( pInode->nShared==0 ); |
| 1619 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1620 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1621 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1622 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1623 | lock.l_start = SHARED_FIRST; |
| 1624 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1625 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1626 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1627 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1628 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1629 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1630 | /* Drop the temporary PENDING lock */ |
| 1631 | lock.l_start = PENDING_BYTE; |
| 1632 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1633 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1634 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1635 | /* This could happen with a network mount */ |
| 1636 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1637 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1638 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1639 | |
| 1640 | if( rc ){ |
| 1641 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1642 | pFile->lastErrno = tErrno; |
| 1643 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1644 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1645 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1646 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1647 | pInode->nLock++; |
| 1648 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1649 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1650 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1651 | /* We are trying for an exclusive lock but another thread in this |
| 1652 | ** same process is still holding a shared lock. */ |
| 1653 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1654 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1655 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1656 | ** assumed that there is a SHARED or greater lock on the file |
| 1657 | ** already. |
| 1658 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1659 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1660 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1661 | |
| 1662 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1663 | if( eFileLock==RESERVED_LOCK ){ |
| 1664 | lock.l_start = RESERVED_BYTE; |
| 1665 | lock.l_len = 1L; |
| 1666 | }else{ |
| 1667 | lock.l_start = SHARED_FIRST; |
| 1668 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1669 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1670 | |
| 1671 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1672 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1673 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1674 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1675 | pFile->lastErrno = tErrno; |
| 1676 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1677 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1678 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1679 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1680 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1681 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1682 | /* Set up the transaction-counter change checking flags when |
| 1683 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1684 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1685 | ** write operation (not a hot journal rollback). |
| 1686 | */ |
| 1687 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1688 | && pFile->eFileLock<=SHARED_LOCK |
| 1689 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1690 | ){ |
| 1691 | pFile->transCntrChng = 0; |
| 1692 | pFile->dbUpdate = 0; |
| 1693 | pFile->inNormalWrite = 1; |
| 1694 | } |
| 1695 | #endif |
| 1696 | |
| 1697 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1698 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1699 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1700 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1701 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1702 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1703 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1704 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1705 | |
| 1706 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1707 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1708 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1709 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1710 | return rc; |
| 1711 | } |
| 1712 | |
| 1713 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1714 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1715 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1716 | */ |
| 1717 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1718 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1719 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1720 | p->pNext = pInode->pUnused; |
| 1721 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1722 | pFile->h = -1; |
| 1723 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1727 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1728 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1729 | ** |
| 1730 | ** If the locking level of the file descriptor is already at or below |
| 1731 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1732 | ** |
| 1733 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1734 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1735 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1736 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1737 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1738 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1739 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1740 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1741 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1742 | struct flock lock; |
| 1743 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1744 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1745 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1746 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1747 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1748 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1749 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1750 | assert( eFileLock<=SHARED_LOCK ); |
| 1751 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1752 | return SQLITE_OK; |
| 1753 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1754 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1755 | pInode = pFile->pInode; |
| 1756 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1757 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1758 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1759 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1760 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1761 | /* When reducing a lock such that other processes can start |
| 1762 | ** reading the database file again, make sure that the |
| 1763 | ** transaction counter was updated if any part of the database |
| 1764 | ** file changed. If the transaction counter is not updated, |
| 1765 | ** other connections to the same file might not realize that |
| 1766 | ** the file has changed and hence might not know to flush their |
| 1767 | ** cache. The use of a stale cache can lead to database corruption. |
| 1768 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1769 | pFile->inNormalWrite = 0; |
| 1770 | #endif |
| 1771 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1772 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1773 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1774 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1775 | ** write lock until the rest is covered by a read lock: |
| 1776 | ** 1: [WWWWW] |
| 1777 | ** 2: [....W] |
| 1778 | ** 3: [RRRRW] |
| 1779 | ** 4: [RRRR.] |
| 1780 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1781 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1782 | |
| 1783 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1784 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1785 | assert( handleNFSUnlock==0 ); |
| 1786 | #endif |
| 1787 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1788 | if( handleNFSUnlock ){ |
drh | 026663d | 2011-04-01 13:29:29 +0000 | [diff] [blame] | 1789 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1790 | off_t divSize = SHARED_SIZE - 1; |
| 1791 | |
| 1792 | lock.l_type = F_UNLCK; |
| 1793 | lock.l_whence = SEEK_SET; |
| 1794 | lock.l_start = SHARED_FIRST; |
| 1795 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1796 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1797 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1798 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1799 | if( IS_LOCK_ERROR(rc) ){ |
| 1800 | pFile->lastErrno = tErrno; |
| 1801 | } |
| 1802 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1803 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1804 | lock.l_type = F_RDLCK; |
| 1805 | lock.l_whence = SEEK_SET; |
| 1806 | lock.l_start = SHARED_FIRST; |
| 1807 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1808 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1809 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1810 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1811 | if( IS_LOCK_ERROR(rc) ){ |
| 1812 | pFile->lastErrno = tErrno; |
| 1813 | } |
| 1814 | goto end_unlock; |
| 1815 | } |
| 1816 | lock.l_type = F_UNLCK; |
| 1817 | lock.l_whence = SEEK_SET; |
| 1818 | lock.l_start = SHARED_FIRST+divSize; |
| 1819 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1820 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1821 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1822 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1823 | if( IS_LOCK_ERROR(rc) ){ |
| 1824 | pFile->lastErrno = tErrno; |
| 1825 | } |
| 1826 | goto end_unlock; |
| 1827 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1828 | }else |
| 1829 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1830 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1831 | lock.l_type = F_RDLCK; |
| 1832 | lock.l_whence = SEEK_SET; |
| 1833 | lock.l_start = SHARED_FIRST; |
| 1834 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1835 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1836 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1837 | ** process is holding an incompatible lock. If it does, this |
| 1838 | ** indicates that the other process is not following the locking |
| 1839 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1840 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1841 | ** an assert to fail). */ |
| 1842 | rc = SQLITE_IOERR_RDLOCK; |
| 1843 | pFile->lastErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1844 | goto end_unlock; |
| 1845 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1846 | } |
| 1847 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1848 | lock.l_type = F_UNLCK; |
| 1849 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1850 | lock.l_start = PENDING_BYTE; |
| 1851 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1852 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1853 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1854 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1855 | rc = SQLITE_IOERR_UNLOCK; |
| 1856 | pFile->lastErrno = errno; |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1857 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1858 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1859 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1860 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1861 | /* Decrement the shared lock counter. Release the lock using an |
| 1862 | ** OS call only when all threads in this same process have released |
| 1863 | ** the lock. |
| 1864 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1865 | pInode->nShared--; |
| 1866 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1867 | lock.l_type = F_UNLCK; |
| 1868 | lock.l_whence = SEEK_SET; |
| 1869 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1870 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1871 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1872 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1873 | rc = SQLITE_IOERR_UNLOCK; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1874 | pFile->lastErrno = errno; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1875 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1876 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1877 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1880 | /* Decrement the count of locks against this same file. When the |
| 1881 | ** count reaches zero, close any other file descriptors whose close |
| 1882 | ** was deferred because of outstanding locks. |
| 1883 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1884 | pInode->nLock--; |
| 1885 | assert( pInode->nLock>=0 ); |
| 1886 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1887 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1888 | } |
| 1889 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1890 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1891 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1892 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1893 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1894 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1898 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1899 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1900 | ** |
| 1901 | ** If the locking level of the file descriptor is already at or below |
| 1902 | ** the requested locking level, this routine is a no-op. |
| 1903 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1904 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1905 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 1906 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1907 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1908 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1911 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1912 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 1913 | static void unixUnmapfile(unixFile *pFd); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1914 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1915 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1916 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1917 | ** This function performs the parts of the "close file" operation |
| 1918 | ** common to all locking schemes. It closes the directory and file |
| 1919 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1920 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1921 | ** |
| 1922 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1923 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1924 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1925 | */ |
| 1926 | static int closeUnixFile(sqlite3_file *id){ |
| 1927 | unixFile *pFile = (unixFile*)id; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1928 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1929 | unixUnmapfile(pFile); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1930 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1931 | if( pFile->h>=0 ){ |
| 1932 | robust_close(pFile, pFile->h, __LINE__); |
| 1933 | pFile->h = -1; |
| 1934 | } |
| 1935 | #if OS_VXWORKS |
| 1936 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 1937 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1938 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1939 | } |
| 1940 | vxworksReleaseFileId(pFile->pId); |
| 1941 | pFile->pId = 0; |
| 1942 | } |
| 1943 | #endif |
| 1944 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1945 | OpenCounter(-1); |
| 1946 | sqlite3_free(pFile->pUnused); |
| 1947 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1948 | return SQLITE_OK; |
| 1949 | } |
| 1950 | |
| 1951 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1952 | ** Close a file. |
| 1953 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1954 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1955 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1956 | unixFile *pFile = (unixFile *)id; |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1957 | verifyDbFile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1958 | unixUnlock(id, NO_LOCK); |
| 1959 | unixEnterMutex(); |
| 1960 | |
| 1961 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1962 | ** routine (e.g. nolockClose()) would be called instead. |
| 1963 | */ |
| 1964 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1965 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1966 | /* If there are outstanding locks, do not actually close the file just |
| 1967 | ** yet because that would clear those locks. Instead, add the file |
| 1968 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 1969 | ** when the last lock is cleared. |
| 1970 | */ |
| 1971 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1972 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1973 | releaseInodeInfo(pFile); |
| 1974 | rc = closeUnixFile(id); |
| 1975 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1976 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1979 | /************** End of the posix advisory lock implementation ***************** |
| 1980 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1981 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1982 | /****************************************************************************** |
| 1983 | ****************************** No-op Locking ********************************** |
| 1984 | ** |
| 1985 | ** Of the various locking implementations available, this is by far the |
| 1986 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1987 | ** file for reading or writing. |
| 1988 | ** |
| 1989 | ** This locking mode is appropriate for use on read-only databases |
| 1990 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1991 | ** also be used if the application employs some external mechanism to |
| 1992 | ** prevent simultaneous access of the same database by two or more |
| 1993 | ** database connections. But there is a serious risk of database |
| 1994 | ** corruption if this locking mode is used in situations where multiple |
| 1995 | ** database connections are accessing the same database file at the same |
| 1996 | ** time and one or more of those connections are writing. |
| 1997 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1998 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1999 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 2000 | UNUSED_PARAMETER(NotUsed); |
| 2001 | *pResOut = 0; |
| 2002 | return SQLITE_OK; |
| 2003 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2004 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2005 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2006 | return SQLITE_OK; |
| 2007 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2008 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2009 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2010 | return SQLITE_OK; |
| 2011 | } |
| 2012 | |
| 2013 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2014 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2015 | */ |
| 2016 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2017 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | /******************* End of the no-op lock implementation ********************* |
| 2021 | ******************************************************************************/ |
| 2022 | |
| 2023 | /****************************************************************************** |
| 2024 | ************************* Begin dot-file Locking ****************************** |
| 2025 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2026 | ** The dotfile locking implementation uses the existence of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2027 | ** files (really a directory) to control access to the database. This works |
| 2028 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2029 | ** |
| 2030 | ** (1) There is zero concurrency. A single reader blocks all other |
| 2031 | ** connections from reading or writing the database. |
| 2032 | ** |
| 2033 | ** (2) An application crash or power loss can leave stale lock files |
| 2034 | ** sitting around that need to be cleared manually. |
| 2035 | ** |
| 2036 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 2037 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2038 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2039 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 2040 | ** the database and with the same name but with a ".lock" extension added. |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2041 | ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2042 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2043 | */ |
| 2044 | |
| 2045 | /* |
| 2046 | ** The file suffix added to the data base filename in order to create the |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2047 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2048 | */ |
| 2049 | #define DOTLOCK_SUFFIX ".lock" |
| 2050 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2051 | /* |
| 2052 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2053 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2054 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2055 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2056 | ** |
| 2057 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 2058 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 2059 | ** is held on the file and false if the file is unlocked. |
| 2060 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2061 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2062 | int rc = SQLITE_OK; |
| 2063 | int reserved = 0; |
| 2064 | unixFile *pFile = (unixFile*)id; |
| 2065 | |
| 2066 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2067 | |
| 2068 | assert( pFile ); |
| 2069 | |
| 2070 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2071 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2072 | /* Either this connection or some other connection in the same process |
| 2073 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2074 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2075 | }else{ |
| 2076 | /* The lock is held if and only if the lockfile exists */ |
| 2077 | const char *zLockFile = (const char*)pFile->lockingContext; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 2078 | reserved = osAccess(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2079 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2080 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2081 | *pResOut = reserved; |
| 2082 | return rc; |
| 2083 | } |
| 2084 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2085 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2086 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2087 | ** of the following: |
| 2088 | ** |
| 2089 | ** (1) SHARED_LOCK |
| 2090 | ** (2) RESERVED_LOCK |
| 2091 | ** (3) PENDING_LOCK |
| 2092 | ** (4) EXCLUSIVE_LOCK |
| 2093 | ** |
| 2094 | ** Sometimes when requesting one lock state, additional lock states |
| 2095 | ** are inserted in between. The locking might fail on one of the later |
| 2096 | ** transitions leaving the lock state different from what it started but |
| 2097 | ** still short of its goal. The following chart shows the allowed |
| 2098 | ** transitions and the inserted intermediate states: |
| 2099 | ** |
| 2100 | ** UNLOCKED -> SHARED |
| 2101 | ** SHARED -> RESERVED |
| 2102 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2103 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2104 | ** PENDING -> EXCLUSIVE |
| 2105 | ** |
| 2106 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2107 | ** routine to lower a locking level. |
| 2108 | ** |
| 2109 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2110 | ** But we track the other locking levels internally. |
| 2111 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2112 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2113 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2114 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2115 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2116 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2117 | |
| 2118 | /* If we have any lock, then the lock file already exists. All we have |
| 2119 | ** to do is adjust our internal record of the lock level. |
| 2120 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2121 | if( pFile->eFileLock > NO_LOCK ){ |
| 2122 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2123 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2124 | #ifdef HAVE_UTIME |
| 2125 | utime(zLockFile, NULL); |
| 2126 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2127 | utimes(zLockFile, NULL); |
| 2128 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2129 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2133 | rc = osMkdir(zLockFile, 0777); |
| 2134 | if( rc<0 ){ |
| 2135 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2136 | int tErrno = errno; |
| 2137 | if( EEXIST == tErrno ){ |
| 2138 | rc = SQLITE_BUSY; |
| 2139 | } else { |
| 2140 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2141 | if( IS_LOCK_ERROR(rc) ){ |
| 2142 | pFile->lastErrno = tErrno; |
| 2143 | } |
| 2144 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2145 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2146 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2147 | |
| 2148 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2149 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2150 | return rc; |
| 2151 | } |
| 2152 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2153 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2154 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2155 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2156 | ** |
| 2157 | ** If the locking level of the file descriptor is already at or below |
| 2158 | ** the requested locking level, this routine is a no-op. |
| 2159 | ** |
| 2160 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2161 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2162 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2163 | unixFile *pFile = (unixFile*)id; |
| 2164 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2165 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2166 | |
| 2167 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2168 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2169 | pFile->eFileLock, getpid())); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2170 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2171 | |
| 2172 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2173 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2174 | return SQLITE_OK; |
| 2175 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2176 | |
| 2177 | /* To downgrade to shared, simply update our internal notion of the |
| 2178 | ** lock state. No need to mess with the file on disk. |
| 2179 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2180 | if( eFileLock==SHARED_LOCK ){ |
| 2181 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2182 | return SQLITE_OK; |
| 2183 | } |
| 2184 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2185 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2186 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2187 | rc = osRmdir(zLockFile); |
| 2188 | if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile); |
| 2189 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2190 | int tErrno = errno; |
drh | 13e0ea9 | 2011-12-11 02:29:25 +0000 | [diff] [blame] | 2191 | rc = 0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2192 | if( ENOENT != tErrno ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2193 | rc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2194 | } |
| 2195 | if( IS_LOCK_ERROR(rc) ){ |
| 2196 | pFile->lastErrno = tErrno; |
| 2197 | } |
| 2198 | return rc; |
| 2199 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2200 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2201 | return SQLITE_OK; |
| 2202 | } |
| 2203 | |
| 2204 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2205 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2206 | */ |
| 2207 | static int dotlockClose(sqlite3_file *id) { |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2208 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2209 | if( id ){ |
| 2210 | unixFile *pFile = (unixFile*)id; |
| 2211 | dotlockUnlock(id, NO_LOCK); |
| 2212 | sqlite3_free(pFile->lockingContext); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2213 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2214 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2215 | return rc; |
| 2216 | } |
| 2217 | /****************** End of the dot-file lock implementation ******************* |
| 2218 | ******************************************************************************/ |
| 2219 | |
| 2220 | /****************************************************************************** |
| 2221 | ************************** Begin flock Locking ******************************** |
| 2222 | ** |
| 2223 | ** Use the flock() system call to do file locking. |
| 2224 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2225 | ** flock() locking is like dot-file locking in that the various |
| 2226 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2227 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2228 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2229 | ** still works when you do this, but concurrency is reduced since |
| 2230 | ** only a single process can be reading the database at a time. |
| 2231 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2232 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 2233 | ** compiling for VXWORKS. |
| 2234 | */ |
| 2235 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2236 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2237 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2238 | ** Retry flock() calls that fail with EINTR |
| 2239 | */ |
| 2240 | #ifdef EINTR |
| 2241 | static int robust_flock(int fd, int op){ |
| 2242 | int rc; |
| 2243 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2244 | return rc; |
| 2245 | } |
| 2246 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2247 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2248 | #endif |
| 2249 | |
| 2250 | |
| 2251 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2252 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2253 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2254 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2255 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2256 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2257 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2258 | int rc = SQLITE_OK; |
| 2259 | int reserved = 0; |
| 2260 | unixFile *pFile = (unixFile*)id; |
| 2261 | |
| 2262 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2263 | |
| 2264 | assert( pFile ); |
| 2265 | |
| 2266 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2267 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2268 | reserved = 1; |
| 2269 | } |
| 2270 | |
| 2271 | /* Otherwise see if some other process holds it. */ |
| 2272 | if( !reserved ){ |
| 2273 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2274 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2275 | if( !lrc ){ |
| 2276 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2277 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2278 | if ( lrc ) { |
| 2279 | int tErrno = errno; |
| 2280 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2281 | lrc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2282 | if( IS_LOCK_ERROR(lrc) ){ |
| 2283 | pFile->lastErrno = tErrno; |
| 2284 | rc = lrc; |
| 2285 | } |
| 2286 | } |
| 2287 | } else { |
| 2288 | int tErrno = errno; |
| 2289 | reserved = 1; |
| 2290 | /* someone else might have it reserved */ |
| 2291 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2292 | if( IS_LOCK_ERROR(lrc) ){ |
| 2293 | pFile->lastErrno = tErrno; |
| 2294 | rc = lrc; |
| 2295 | } |
| 2296 | } |
| 2297 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2298 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2299 | |
| 2300 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2301 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2302 | rc = SQLITE_OK; |
| 2303 | reserved=1; |
| 2304 | } |
| 2305 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2306 | *pResOut = reserved; |
| 2307 | return rc; |
| 2308 | } |
| 2309 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2310 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2311 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2312 | ** of the following: |
| 2313 | ** |
| 2314 | ** (1) SHARED_LOCK |
| 2315 | ** (2) RESERVED_LOCK |
| 2316 | ** (3) PENDING_LOCK |
| 2317 | ** (4) EXCLUSIVE_LOCK |
| 2318 | ** |
| 2319 | ** Sometimes when requesting one lock state, additional lock states |
| 2320 | ** are inserted in between. The locking might fail on one of the later |
| 2321 | ** transitions leaving the lock state different from what it started but |
| 2322 | ** still short of its goal. The following chart shows the allowed |
| 2323 | ** transitions and the inserted intermediate states: |
| 2324 | ** |
| 2325 | ** UNLOCKED -> SHARED |
| 2326 | ** SHARED -> RESERVED |
| 2327 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2328 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2329 | ** PENDING -> EXCLUSIVE |
| 2330 | ** |
| 2331 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2332 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2333 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2334 | ** access the file. |
| 2335 | ** |
| 2336 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2337 | ** routine to lower a locking level. |
| 2338 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2339 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2340 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2341 | unixFile *pFile = (unixFile*)id; |
| 2342 | |
| 2343 | assert( pFile ); |
| 2344 | |
| 2345 | /* if we already have a lock, it is exclusive. |
| 2346 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2347 | if (pFile->eFileLock > NO_LOCK) { |
| 2348 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2349 | return SQLITE_OK; |
| 2350 | } |
| 2351 | |
| 2352 | /* grab an exclusive lock */ |
| 2353 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2354 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2355 | int tErrno = errno; |
| 2356 | /* didn't get, must be busy */ |
| 2357 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2358 | if( IS_LOCK_ERROR(rc) ){ |
| 2359 | pFile->lastErrno = tErrno; |
| 2360 | } |
| 2361 | } else { |
| 2362 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2363 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2364 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2365 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2366 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2367 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2368 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2369 | rc = SQLITE_BUSY; |
| 2370 | } |
| 2371 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2372 | return rc; |
| 2373 | } |
| 2374 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2375 | |
| 2376 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2377 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2378 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2379 | ** |
| 2380 | ** If the locking level of the file descriptor is already at or below |
| 2381 | ** the requested locking level, this routine is a no-op. |
| 2382 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2383 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2384 | unixFile *pFile = (unixFile*)id; |
| 2385 | |
| 2386 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2387 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 2388 | pFile->eFileLock, getpid())); |
| 2389 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2390 | |
| 2391 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2392 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2393 | return SQLITE_OK; |
| 2394 | } |
| 2395 | |
| 2396 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2397 | if (eFileLock==SHARED_LOCK) { |
| 2398 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2399 | return SQLITE_OK; |
| 2400 | } |
| 2401 | |
| 2402 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2403 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2404 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2405 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2406 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2407 | return SQLITE_IOERR_UNLOCK; |
| 2408 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2409 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2410 | return SQLITE_OK; |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | /* |
| 2415 | ** Close a file. |
| 2416 | */ |
| 2417 | static int flockClose(sqlite3_file *id) { |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2418 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2419 | if( id ){ |
| 2420 | flockUnlock(id, NO_LOCK); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2421 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2422 | } |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2423 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2427 | |
| 2428 | /******************* End of the flock lock implementation ********************* |
| 2429 | ******************************************************************************/ |
| 2430 | |
| 2431 | /****************************************************************************** |
| 2432 | ************************ Begin Named Semaphore Locking ************************ |
| 2433 | ** |
| 2434 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2435 | ** |
| 2436 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2437 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2438 | ** the database file at a time. This reduces potential concurrency, but |
| 2439 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2440 | */ |
| 2441 | #if OS_VXWORKS |
| 2442 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2443 | /* |
| 2444 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2445 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2446 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2447 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2448 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2449 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2450 | int rc = SQLITE_OK; |
| 2451 | int reserved = 0; |
| 2452 | unixFile *pFile = (unixFile*)id; |
| 2453 | |
| 2454 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2455 | |
| 2456 | assert( pFile ); |
| 2457 | |
| 2458 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2459 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2460 | reserved = 1; |
| 2461 | } |
| 2462 | |
| 2463 | /* Otherwise see if some other process holds it. */ |
| 2464 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2465 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2466 | struct stat statBuf; |
| 2467 | |
| 2468 | if( sem_trywait(pSem)==-1 ){ |
| 2469 | int tErrno = errno; |
| 2470 | if( EAGAIN != tErrno ){ |
| 2471 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2472 | pFile->lastErrno = tErrno; |
| 2473 | } else { |
| 2474 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2475 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2476 | } |
| 2477 | }else{ |
| 2478 | /* we could have it if we want it */ |
| 2479 | sem_post(pSem); |
| 2480 | } |
| 2481 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2482 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2483 | |
| 2484 | *pResOut = reserved; |
| 2485 | return rc; |
| 2486 | } |
| 2487 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2488 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2489 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2490 | ** of the following: |
| 2491 | ** |
| 2492 | ** (1) SHARED_LOCK |
| 2493 | ** (2) RESERVED_LOCK |
| 2494 | ** (3) PENDING_LOCK |
| 2495 | ** (4) EXCLUSIVE_LOCK |
| 2496 | ** |
| 2497 | ** Sometimes when requesting one lock state, additional lock states |
| 2498 | ** are inserted in between. The locking might fail on one of the later |
| 2499 | ** transitions leaving the lock state different from what it started but |
| 2500 | ** still short of its goal. The following chart shows the allowed |
| 2501 | ** transitions and the inserted intermediate states: |
| 2502 | ** |
| 2503 | ** UNLOCKED -> SHARED |
| 2504 | ** SHARED -> RESERVED |
| 2505 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2506 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2507 | ** PENDING -> EXCLUSIVE |
| 2508 | ** |
| 2509 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2510 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2511 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2512 | ** access the file. |
| 2513 | ** |
| 2514 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2515 | ** routine to lower a locking level. |
| 2516 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2517 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2518 | unixFile *pFile = (unixFile*)id; |
| 2519 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2520 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2521 | int rc = SQLITE_OK; |
| 2522 | |
| 2523 | /* if we already have a lock, it is exclusive. |
| 2524 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2525 | if (pFile->eFileLock > NO_LOCK) { |
| 2526 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2527 | rc = SQLITE_OK; |
| 2528 | goto sem_end_lock; |
| 2529 | } |
| 2530 | |
| 2531 | /* lock semaphore now but bail out when already locked. */ |
| 2532 | if( sem_trywait(pSem)==-1 ){ |
| 2533 | rc = SQLITE_BUSY; |
| 2534 | goto sem_end_lock; |
| 2535 | } |
| 2536 | |
| 2537 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2538 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2539 | |
| 2540 | sem_end_lock: |
| 2541 | return rc; |
| 2542 | } |
| 2543 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2544 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2545 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2546 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2547 | ** |
| 2548 | ** If the locking level of the file descriptor is already at or below |
| 2549 | ** the requested locking level, this routine is a no-op. |
| 2550 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2551 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2552 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2553 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2554 | |
| 2555 | assert( pFile ); |
| 2556 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2557 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2558 | pFile->eFileLock, getpid())); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2559 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2560 | |
| 2561 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2562 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2563 | return SQLITE_OK; |
| 2564 | } |
| 2565 | |
| 2566 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2567 | if (eFileLock==SHARED_LOCK) { |
| 2568 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2569 | return SQLITE_OK; |
| 2570 | } |
| 2571 | |
| 2572 | /* no, really unlock. */ |
| 2573 | if ( sem_post(pSem)==-1 ) { |
| 2574 | int rc, tErrno = errno; |
| 2575 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2576 | if( IS_LOCK_ERROR(rc) ){ |
| 2577 | pFile->lastErrno = tErrno; |
| 2578 | } |
| 2579 | return rc; |
| 2580 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2581 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2582 | return SQLITE_OK; |
| 2583 | } |
| 2584 | |
| 2585 | /* |
| 2586 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2587 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2588 | static int semClose(sqlite3_file *id) { |
| 2589 | if( id ){ |
| 2590 | unixFile *pFile = (unixFile*)id; |
| 2591 | semUnlock(id, NO_LOCK); |
| 2592 | assert( pFile ); |
| 2593 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2594 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2595 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2596 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2597 | } |
| 2598 | return SQLITE_OK; |
| 2599 | } |
| 2600 | |
| 2601 | #endif /* OS_VXWORKS */ |
| 2602 | /* |
| 2603 | ** Named semaphore locking is only available on VxWorks. |
| 2604 | ** |
| 2605 | *************** End of the named semaphore lock implementation **************** |
| 2606 | ******************************************************************************/ |
| 2607 | |
| 2608 | |
| 2609 | /****************************************************************************** |
| 2610 | *************************** Begin AFP Locking ********************************* |
| 2611 | ** |
| 2612 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2613 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2614 | ** |
| 2615 | ** Third-party implementations of AFP are available. But this code here |
| 2616 | ** only works on OSX. |
| 2617 | */ |
| 2618 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2619 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2620 | /* |
| 2621 | ** The afpLockingContext structure contains all afp lock specific state |
| 2622 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2623 | typedef struct afpLockingContext afpLockingContext; |
| 2624 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2625 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2626 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2627 | }; |
| 2628 | |
| 2629 | struct ByteRangeLockPB2 |
| 2630 | { |
| 2631 | unsigned long long offset; /* offset to first byte to lock */ |
| 2632 | unsigned long long length; /* nbr of bytes to lock */ |
| 2633 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2634 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2635 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2636 | int fd; /* file desc to assoc this lock with */ |
| 2637 | }; |
| 2638 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2639 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2640 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2641 | /* |
| 2642 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2643 | ** AFP filesystem. |
| 2644 | ** |
| 2645 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2646 | */ |
| 2647 | static int afpSetLock( |
| 2648 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2649 | unixFile *pFile, /* Open file descriptor on path */ |
| 2650 | unsigned long long offset, /* First byte to be locked */ |
| 2651 | unsigned long long length, /* Number of bytes to lock */ |
| 2652 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2653 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2654 | struct ByteRangeLockPB2 pb; |
| 2655 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2656 | |
| 2657 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2658 | pb.startEndFlag = 0; |
| 2659 | pb.offset = offset; |
| 2660 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2661 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2662 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2663 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2664 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2665 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2666 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2667 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2668 | int rc; |
| 2669 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2670 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2671 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2672 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2673 | rc = SQLITE_BUSY; |
| 2674 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2675 | rc = sqliteErrorFromPosixError(tErrno, |
| 2676 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2677 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2678 | if( IS_LOCK_ERROR(rc) ){ |
| 2679 | pFile->lastErrno = tErrno; |
| 2680 | } |
| 2681 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2682 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2683 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2684 | } |
| 2685 | } |
| 2686 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2687 | /* |
| 2688 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2689 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2690 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2691 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2692 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2693 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2694 | int rc = SQLITE_OK; |
| 2695 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2696 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2697 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2698 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2699 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2700 | |
| 2701 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2702 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2703 | if( context->reserved ){ |
| 2704 | *pResOut = 1; |
| 2705 | return SQLITE_OK; |
| 2706 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2707 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2708 | |
| 2709 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2710 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2711 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | /* Otherwise see if some other process holds it. |
| 2715 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2716 | if( !reserved ){ |
| 2717 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2718 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2719 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2720 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2721 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2722 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2723 | } else { |
| 2724 | /* if we failed to get the lock then someone else must have it */ |
| 2725 | reserved = 1; |
| 2726 | } |
| 2727 | if( IS_LOCK_ERROR(lrc) ){ |
| 2728 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2729 | } |
| 2730 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2731 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2732 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2733 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2734 | |
| 2735 | *pResOut = reserved; |
| 2736 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2739 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2740 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2741 | ** of the following: |
| 2742 | ** |
| 2743 | ** (1) SHARED_LOCK |
| 2744 | ** (2) RESERVED_LOCK |
| 2745 | ** (3) PENDING_LOCK |
| 2746 | ** (4) EXCLUSIVE_LOCK |
| 2747 | ** |
| 2748 | ** Sometimes when requesting one lock state, additional lock states |
| 2749 | ** are inserted in between. The locking might fail on one of the later |
| 2750 | ** transitions leaving the lock state different from what it started but |
| 2751 | ** still short of its goal. The following chart shows the allowed |
| 2752 | ** transitions and the inserted intermediate states: |
| 2753 | ** |
| 2754 | ** UNLOCKED -> SHARED |
| 2755 | ** SHARED -> RESERVED |
| 2756 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2757 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2758 | ** PENDING -> EXCLUSIVE |
| 2759 | ** |
| 2760 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2761 | ** routine to lower a locking level. |
| 2762 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2763 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2764 | int rc = SQLITE_OK; |
| 2765 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2766 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2767 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2768 | |
| 2769 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2770 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2771 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2772 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2773 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2774 | /* If there is already a lock of this type or more restrictive on the |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2775 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2776 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2777 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2778 | if( pFile->eFileLock>=eFileLock ){ |
| 2779 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2780 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2781 | return SQLITE_OK; |
| 2782 | } |
| 2783 | |
| 2784 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2785 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2786 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2787 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2788 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2789 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2790 | assert( eFileLock!=PENDING_LOCK ); |
| 2791 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2792 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2793 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2794 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2795 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2796 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2797 | |
| 2798 | /* If some thread using this PID has a lock via a different unixFile* |
| 2799 | ** handle that precludes the requested lock, return BUSY. |
| 2800 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2801 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2802 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2803 | ){ |
| 2804 | rc = SQLITE_BUSY; |
| 2805 | goto afp_end_lock; |
| 2806 | } |
| 2807 | |
| 2808 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2809 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2810 | ** return SQLITE_OK. |
| 2811 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2812 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2813 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2814 | assert( eFileLock==SHARED_LOCK ); |
| 2815 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2816 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2817 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2818 | pInode->nShared++; |
| 2819 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2820 | goto afp_end_lock; |
| 2821 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2822 | |
| 2823 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2824 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2825 | ** be released. |
| 2826 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2827 | if( eFileLock==SHARED_LOCK |
| 2828 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2829 | ){ |
| 2830 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2831 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2832 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2833 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2834 | goto afp_end_lock; |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2839 | ** operating system calls for the specified lock. |
| 2840 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2841 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2842 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2843 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2844 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2845 | assert( pInode->nShared==0 ); |
| 2846 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2847 | |
| 2848 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2849 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2850 | /* note that the quality of the randomness doesn't matter that much */ |
| 2851 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2852 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2853 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2854 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2855 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2856 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2857 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2858 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2859 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2860 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2861 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2862 | pFile->lastErrno = lrc1Errno; |
| 2863 | rc = lrc1; |
| 2864 | goto afp_end_lock; |
| 2865 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2866 | rc = lrc2; |
| 2867 | goto afp_end_lock; |
| 2868 | } else if( lrc1 != SQLITE_OK ) { |
| 2869 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2870 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2871 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2872 | pInode->nLock++; |
| 2873 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2874 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2875 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2876 | /* We are trying for an exclusive lock but another thread in this |
| 2877 | ** same process is still holding a shared lock. */ |
| 2878 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2879 | }else{ |
| 2880 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2881 | ** assumed that there is a SHARED or greater lock on the file |
| 2882 | ** already. |
| 2883 | */ |
| 2884 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2885 | assert( 0!=pFile->eFileLock ); |
| 2886 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2887 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2888 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2889 | if( !failed ){ |
| 2890 | context->reserved = 1; |
| 2891 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2892 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2893 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2894 | /* Acquire an EXCLUSIVE lock */ |
| 2895 | |
| 2896 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2897 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2898 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2899 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2900 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2901 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2902 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2903 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2904 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2905 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2906 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2907 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2908 | ** a critical I/O error |
| 2909 | */ |
| 2910 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2911 | SQLITE_IOERR_LOCK; |
| 2912 | goto afp_end_lock; |
| 2913 | } |
| 2914 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2915 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2916 | } |
| 2917 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2918 | if( failed ){ |
| 2919 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2920 | } |
| 2921 | } |
| 2922 | |
| 2923 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2924 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2925 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2926 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2927 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2928 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2932 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2933 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2934 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2935 | return rc; |
| 2936 | } |
| 2937 | |
| 2938 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2939 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2940 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2941 | ** |
| 2942 | ** If the locking level of the file descriptor is already at or below |
| 2943 | ** the requested locking level, this routine is a no-op. |
| 2944 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2945 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2946 | int rc = SQLITE_OK; |
| 2947 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2948 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2949 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2950 | int skipShared = 0; |
| 2951 | #ifdef SQLITE_TEST |
| 2952 | int h = pFile->h; |
| 2953 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2954 | |
| 2955 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2956 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2957 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2958 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2959 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2960 | assert( eFileLock<=SHARED_LOCK ); |
| 2961 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2962 | return SQLITE_OK; |
| 2963 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2964 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2965 | pInode = pFile->pInode; |
| 2966 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2967 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2968 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2969 | SimulateIOErrorBenign(1); |
| 2970 | SimulateIOError( h=(-1) ) |
| 2971 | SimulateIOErrorBenign(0); |
| 2972 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 2973 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2974 | /* When reducing a lock such that other processes can start |
| 2975 | ** reading the database file again, make sure that the |
| 2976 | ** transaction counter was updated if any part of the database |
| 2977 | ** file changed. If the transaction counter is not updated, |
| 2978 | ** other connections to the same file might not realize that |
| 2979 | ** the file has changed and hence might not know to flush their |
| 2980 | ** cache. The use of a stale cache can lead to database corruption. |
| 2981 | */ |
| 2982 | assert( pFile->inNormalWrite==0 |
| 2983 | || pFile->dbUpdate==0 |
| 2984 | || pFile->transCntrChng==1 ); |
| 2985 | pFile->inNormalWrite = 0; |
| 2986 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2987 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2988 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2989 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2990 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2991 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2992 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2993 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2994 | } else { |
| 2995 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2996 | } |
| 2997 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2998 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2999 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3000 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3001 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3002 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 3003 | if( !rc ){ |
| 3004 | context->reserved = 0; |
| 3005 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3006 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3007 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 3008 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3009 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3010 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3011 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3012 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3013 | /* Decrement the shared lock counter. Release the lock using an |
| 3014 | ** OS call only when all threads in this same process have released |
| 3015 | ** the lock. |
| 3016 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3017 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 3018 | pInode->nShared--; |
| 3019 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3020 | SimulateIOErrorBenign(1); |
| 3021 | SimulateIOError( h=(-1) ) |
| 3022 | SimulateIOErrorBenign(0); |
| 3023 | if( !skipShared ){ |
| 3024 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 3025 | } |
| 3026 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3027 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3028 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3032 | pInode->nLock--; |
| 3033 | assert( pInode->nLock>=0 ); |
| 3034 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3035 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3036 | } |
| 3037 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3038 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3039 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3040 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3041 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3042 | return rc; |
| 3043 | } |
| 3044 | |
| 3045 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3046 | ** Close a file & cleanup AFP specific locking context |
| 3047 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3048 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3049 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3050 | if( id ){ |
| 3051 | unixFile *pFile = (unixFile*)id; |
| 3052 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3053 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3054 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3055 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3056 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3057 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3058 | ** the last lock is cleared. |
| 3059 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3060 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3061 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 3062 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3063 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3064 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3065 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3066 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3067 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3070 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3071 | /* |
| 3072 | ** The code above is the AFP lock implementation. The code is specific |
| 3073 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3074 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 3075 | ** VFS is not available. |
| 3076 | ** |
| 3077 | ********************* End of the AFP lock implementation ********************** |
| 3078 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3079 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3080 | /****************************************************************************** |
| 3081 | *************************** Begin NFS Locking ********************************/ |
| 3082 | |
| 3083 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3084 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3085 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3086 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3087 | ** |
| 3088 | ** If the locking level of the file descriptor is already at or below |
| 3089 | ** the requested locking level, this routine is a no-op. |
| 3090 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3091 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 3092 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3096 | /* |
| 3097 | ** The code above is the NFS lock implementation. The code is specific |
| 3098 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3099 | ** is available. |
| 3100 | ** |
| 3101 | ********************* End of the NFS lock implementation ********************** |
| 3102 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3103 | |
| 3104 | /****************************************************************************** |
| 3105 | **************** Non-locking sqlite3_file methods ***************************** |
| 3106 | ** |
| 3107 | ** The next division contains implementations for all methods of the |
| 3108 | ** sqlite3_file object other than the locking methods. The locking |
| 3109 | ** methods were defined in divisions above (one locking method per |
| 3110 | ** division). Those methods that are common to all locking modes |
| 3111 | ** are gather together into this division. |
| 3112 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3113 | |
| 3114 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3115 | ** Seek to the offset passed as the second argument, then read cnt |
| 3116 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3117 | ** |
| 3118 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3119 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3120 | ** one system to another. Since SQLite does not define USE_PREAD |
| 3121 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 3122 | ** See tickets #2741 and #2681. |
| 3123 | ** |
| 3124 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3125 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3126 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3127 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3128 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3129 | int prior = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3130 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3131 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3132 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3133 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3134 | assert( cnt==(cnt&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3135 | assert( id->h>2 ); |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3136 | cnt &= 0x1ffff; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3137 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3138 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3139 | got = osPread(id->h, pBuf, cnt, offset); |
| 3140 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3141 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3142 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3143 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3144 | #else |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3145 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3146 | SimulateIOError( newOffset-- ); |
| 3147 | if( newOffset!=offset ){ |
| 3148 | if( newOffset == -1 ){ |
| 3149 | ((unixFile*)id)->lastErrno = errno; |
| 3150 | }else{ |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 3151 | ((unixFile*)id)->lastErrno = 0; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3152 | } |
| 3153 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3154 | } |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3155 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3156 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3157 | if( got==cnt ) break; |
| 3158 | if( got<0 ){ |
| 3159 | if( errno==EINTR ){ got = 1; continue; } |
| 3160 | prior = 0; |
| 3161 | ((unixFile*)id)->lastErrno = errno; |
| 3162 | break; |
| 3163 | }else if( got>0 ){ |
| 3164 | cnt -= got; |
| 3165 | offset += got; |
| 3166 | prior += got; |
| 3167 | pBuf = (void*)(got + (char*)pBuf); |
| 3168 | } |
| 3169 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3170 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3171 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3172 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3173 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3177 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3178 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3179 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3180 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3181 | static int unixRead( |
| 3182 | sqlite3_file *id, |
| 3183 | void *pBuf, |
| 3184 | int amt, |
| 3185 | sqlite3_int64 offset |
| 3186 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3187 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3188 | int got; |
| 3189 | assert( id ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 +0000 | [diff] [blame] | 3190 | assert( offset>=0 ); |
| 3191 | assert( amt>0 ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3192 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3193 | /* If this is a database file (not a journal, master-journal or temp |
| 3194 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3195 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3196 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3197 | || offset>=PENDING_BYTE+512 |
| 3198 | || offset+amt<=PENDING_BYTE |
| 3199 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3200 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3201 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3202 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 6c56963 | 2013-03-26 18:48:11 +0000 | [diff] [blame] | 3203 | /* Deal with as much of this read request as possible by transfering |
| 3204 | ** data from the memory mapping using memcpy(). */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3205 | if( offset<pFile->mmapSize ){ |
| 3206 | if( offset+amt <= pFile->mmapSize ){ |
| 3207 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3208 | return SQLITE_OK; |
| 3209 | }else{ |
| 3210 | int nCopy = pFile->mmapSize - offset; |
| 3211 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3212 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3213 | amt -= nCopy; |
| 3214 | offset += nCopy; |
| 3215 | } |
| 3216 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3217 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3218 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3219 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3220 | if( got==amt ){ |
| 3221 | return SQLITE_OK; |
| 3222 | }else if( got<0 ){ |
| 3223 | /* lastErrno set by seekAndRead */ |
| 3224 | return SQLITE_IOERR_READ; |
| 3225 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3226 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3227 | /* Unread parts of the buffer must be zero-filled */ |
| 3228 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3229 | return SQLITE_IOERR_SHORT_READ; |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | /* |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3234 | ** Attempt to seek the file-descriptor passed as the first argument to |
| 3235 | ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 3236 | ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 3237 | ** return the actual number of bytes written (which may be less than |
| 3238 | ** nBuf). |
| 3239 | */ |
| 3240 | static int seekAndWriteFd( |
| 3241 | int fd, /* File descriptor to write to */ |
| 3242 | i64 iOff, /* File offset to begin writing at */ |
| 3243 | const void *pBuf, /* Copy data from this buffer to the file */ |
| 3244 | int nBuf, /* Size of buffer pBuf in bytes */ |
| 3245 | int *piErrno /* OUT: Error number if error occurs */ |
| 3246 | ){ |
| 3247 | int rc = 0; /* Value returned by system call */ |
| 3248 | |
| 3249 | assert( nBuf==(nBuf&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3250 | assert( fd>2 ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3251 | nBuf &= 0x1ffff; |
| 3252 | TIMER_START; |
| 3253 | |
| 3254 | #if defined(USE_PREAD) |
| 3255 | do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
| 3256 | #elif defined(USE_PREAD64) |
| 3257 | do{ rc = osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
| 3258 | #else |
| 3259 | do{ |
| 3260 | i64 iSeek = lseek(fd, iOff, SEEK_SET); |
| 3261 | SimulateIOError( iSeek-- ); |
| 3262 | |
| 3263 | if( iSeek!=iOff ){ |
| 3264 | if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0); |
| 3265 | return -1; |
| 3266 | } |
| 3267 | rc = osWrite(fd, pBuf, nBuf); |
| 3268 | }while( rc<0 && errno==EINTR ); |
| 3269 | #endif |
| 3270 | |
| 3271 | TIMER_END; |
| 3272 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 3273 | |
| 3274 | if( rc<0 && piErrno ) *piErrno = errno; |
| 3275 | return rc; |
| 3276 | } |
| 3277 | |
| 3278 | |
| 3279 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3280 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3281 | ** Return the number of bytes actually read. Update the offset. |
| 3282 | ** |
| 3283 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3284 | ** is set before returning. |
| 3285 | */ |
| 3286 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3287 | return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | |
| 3291 | /* |
| 3292 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3293 | ** or some other error code on failure. |
| 3294 | */ |
| 3295 | static int unixWrite( |
| 3296 | sqlite3_file *id, |
| 3297 | const void *pBuf, |
| 3298 | int amt, |
| 3299 | sqlite3_int64 offset |
| 3300 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3301 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3302 | int wrote = 0; |
| 3303 | assert( id ); |
| 3304 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3305 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3306 | /* If this is a database file (not a journal, master-journal or temp |
| 3307 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3308 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3309 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3310 | || offset>=PENDING_BYTE+512 |
| 3311 | || offset+amt<=PENDING_BYTE |
| 3312 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3313 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3314 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3315 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3316 | /* If we are doing a normal write to a database file (as opposed to |
| 3317 | ** doing a hot-journal rollback or a write to some file other than a |
| 3318 | ** normal database file) then record the fact that the database |
| 3319 | ** has changed. If the transaction counter is modified, record that |
| 3320 | ** fact too. |
| 3321 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3322 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3323 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3324 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3325 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3326 | char oldCntr[4]; |
| 3327 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3328 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3329 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3330 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3331 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | #endif |
| 3336 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3337 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3338 | /* Deal with as much of this write request as possible by transfering |
| 3339 | ** data from the memory mapping using memcpy(). */ |
| 3340 | if( offset<pFile->mmapSize ){ |
| 3341 | if( offset+amt <= pFile->mmapSize ){ |
| 3342 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3343 | return SQLITE_OK; |
| 3344 | }else{ |
| 3345 | int nCopy = pFile->mmapSize - offset; |
| 3346 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3347 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3348 | amt -= nCopy; |
| 3349 | offset += nCopy; |
| 3350 | } |
| 3351 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3352 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3353 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3354 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3355 | amt -= wrote; |
| 3356 | offset += wrote; |
| 3357 | pBuf = &((char*)pBuf)[wrote]; |
| 3358 | } |
| 3359 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3360 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3361 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3362 | if( amt>0 ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3363 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3364 | /* lastErrno set by seekAndWrite */ |
| 3365 | return SQLITE_IOERR_WRITE; |
| 3366 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3367 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3368 | return SQLITE_FULL; |
| 3369 | } |
| 3370 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3371 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3372 | return SQLITE_OK; |
| 3373 | } |
| 3374 | |
| 3375 | #ifdef SQLITE_TEST |
| 3376 | /* |
| 3377 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3378 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3379 | */ |
| 3380 | int sqlite3_sync_count = 0; |
| 3381 | int sqlite3_fullsync_count = 0; |
| 3382 | #endif |
| 3383 | |
| 3384 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3385 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3386 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3387 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3388 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3389 | */ |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3390 | #if !defined(fdatasync) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3391 | # define fdatasync fsync |
| 3392 | #endif |
| 3393 | |
| 3394 | /* |
| 3395 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3396 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3397 | ** only available on Mac OS X. But that could change. |
| 3398 | */ |
| 3399 | #ifdef F_FULLFSYNC |
| 3400 | # define HAVE_FULLFSYNC 1 |
| 3401 | #else |
| 3402 | # define HAVE_FULLFSYNC 0 |
| 3403 | #endif |
| 3404 | |
| 3405 | |
| 3406 | /* |
| 3407 | ** The fsync() system call does not work as advertised on many |
| 3408 | ** unix systems. The following procedure is an attempt to make |
| 3409 | ** it work better. |
| 3410 | ** |
| 3411 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3412 | ** for testing when we want to run through the test suite quickly. |
| 3413 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3414 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3415 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3416 | ** |
| 3417 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3418 | ** The idea behind dataOnly is that it should only write the file content |
| 3419 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3420 | ** unchanged since the file size is part of the inode. However, |
| 3421 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3422 | ** file size has changed. The only real difference between fdatasync() |
| 3423 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3424 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3425 | ** We only care about the file size, not the other file attributes, so |
| 3426 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3427 | ** So, we always use fdatasync() if it is available, regardless of |
| 3428 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3429 | */ |
| 3430 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3431 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3432 | |
| 3433 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3434 | ** the one below. It is replicated here solely to avoid cluttering |
| 3435 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3436 | */ |
| 3437 | #ifdef SQLITE_NO_SYNC |
| 3438 | UNUSED_PARAMETER(fd); |
| 3439 | UNUSED_PARAMETER(fullSync); |
| 3440 | UNUSED_PARAMETER(dataOnly); |
| 3441 | #elif HAVE_FULLFSYNC |
| 3442 | UNUSED_PARAMETER(dataOnly); |
| 3443 | #else |
| 3444 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3445 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3446 | #endif |
| 3447 | |
| 3448 | /* Record the number of times that we do a normal fsync() and |
| 3449 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3450 | ** gets called with the correct arguments. |
| 3451 | */ |
| 3452 | #ifdef SQLITE_TEST |
| 3453 | if( fullSync ) sqlite3_fullsync_count++; |
| 3454 | sqlite3_sync_count++; |
| 3455 | #endif |
| 3456 | |
| 3457 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3458 | ** no-op |
| 3459 | */ |
| 3460 | #ifdef SQLITE_NO_SYNC |
| 3461 | rc = SQLITE_OK; |
| 3462 | #elif HAVE_FULLFSYNC |
| 3463 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3464 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3465 | }else{ |
| 3466 | rc = 1; |
| 3467 | } |
| 3468 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3469 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3470 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3471 | ** isn't supported for this file system. So, attempt an fsync |
| 3472 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3473 | ** It'd be better to detect fullfsync support once and avoid |
| 3474 | ** the fcntl call every time sync is called. |
| 3475 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3476 | if( rc ) rc = fsync(fd); |
| 3477 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3478 | #elif defined(__APPLE__) |
| 3479 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3480 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3481 | */ |
| 3482 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3483 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3484 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3485 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3486 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3487 | rc = fsync(fd); |
| 3488 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3489 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3490 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3491 | |
| 3492 | if( OS_VXWORKS && rc!= -1 ){ |
| 3493 | rc = 0; |
| 3494 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3495 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3498 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3499 | ** Open a file descriptor to the directory containing file zFilename. |
| 3500 | ** If successful, *pFd is set to the opened file descriptor and |
| 3501 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3502 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3503 | ** value. |
| 3504 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3505 | ** The directory file descriptor is used for only one thing - to |
| 3506 | ** fsync() a directory to make sure file creation and deletion events |
| 3507 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3508 | ** journaling filesystems, but are required on older filesystems. |
| 3509 | ** |
| 3510 | ** This routine can be overridden using the xSetSysCall interface. |
| 3511 | ** The ability to override this routine was added in support of the |
| 3512 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3513 | ** told) so making it overrideable allows the chromium sandbox to |
| 3514 | ** replace this routine with a harmless no-op. To make this routine |
| 3515 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3516 | ** *pFd set to a negative number. |
| 3517 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3518 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3519 | ** the file descriptor *pFd using close(). |
| 3520 | */ |
| 3521 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3522 | int ii; |
| 3523 | int fd = -1; |
| 3524 | char zDirname[MAX_PATHNAME+1]; |
| 3525 | |
| 3526 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
| 3527 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
| 3528 | if( ii>0 ){ |
| 3529 | zDirname[ii] = '\0'; |
| 3530 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3531 | if( fd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3532 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
| 3533 | } |
| 3534 | } |
| 3535 | *pFd = fd; |
| 3536 | return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); |
| 3537 | } |
| 3538 | |
| 3539 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3540 | ** Make sure all writes to a particular file are committed to disk. |
| 3541 | ** |
| 3542 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3543 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3544 | ** file data is synced. |
| 3545 | ** |
| 3546 | ** Under Unix, also make sure that the directory entry for the file |
| 3547 | ** has been created by fsync-ing the directory that contains the file. |
| 3548 | ** If we do not do this and we encounter a power failure, the directory |
| 3549 | ** entry for the journal might not exist after we reboot. The next |
| 3550 | ** SQLite to access the file will not know that the journal exists (because |
| 3551 | ** the directory entry for the journal was never created) and the transaction |
| 3552 | ** will not roll back - possibly leading to database corruption. |
| 3553 | */ |
| 3554 | static int unixSync(sqlite3_file *id, int flags){ |
| 3555 | int rc; |
| 3556 | unixFile *pFile = (unixFile*)id; |
| 3557 | |
| 3558 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3559 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3560 | |
| 3561 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3562 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3563 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3564 | ); |
| 3565 | |
| 3566 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3567 | ** line is to test that doing so does not cause any problems. |
| 3568 | */ |
| 3569 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3570 | |
| 3571 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3572 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3573 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3574 | SimulateIOError( rc=1 ); |
| 3575 | if( rc ){ |
| 3576 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3577 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3578 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3579 | |
| 3580 | /* Also fsync the directory containing the file if the DIRSYNC flag |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3581 | ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3582 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3583 | */ |
| 3584 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3585 | int dirfd; |
| 3586 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3587 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3588 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 3589 | if( rc==SQLITE_OK && dirfd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3590 | full_fsync(dirfd, 0, 0); |
| 3591 | robust_close(pFile, dirfd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3592 | }else if( rc==SQLITE_CANTOPEN ){ |
| 3593 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3594 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3595 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3596 | } |
| 3597 | return rc; |
| 3598 | } |
| 3599 | |
| 3600 | /* |
| 3601 | ** Truncate an open file to a specified size |
| 3602 | */ |
| 3603 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3604 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3605 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3606 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3607 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3608 | |
| 3609 | /* If the user has configured a chunk-size for this file, truncate the |
| 3610 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3611 | ** actual file size after the operation may be larger than the requested |
| 3612 | ** size). |
| 3613 | */ |
drh | b8af4b7 | 2012-04-05 20:04:39 +0000 | [diff] [blame] | 3614 | if( pFile->szChunk>0 ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3615 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3616 | } |
| 3617 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3618 | rc = robust_ftruncate(pFile->h, (off_t)nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3619 | if( rc ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3620 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3621 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3622 | }else{ |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3623 | #ifdef SQLITE_DEBUG |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3624 | /* If we are doing a normal write to a database file (as opposed to |
| 3625 | ** doing a hot-journal rollback or a write to some file other than a |
| 3626 | ** normal database file) and we truncate the file to zero length, |
| 3627 | ** that effectively updates the change counter. This might happen |
| 3628 | ** when restoring a database using the backup API from a zero-length |
| 3629 | ** source. |
| 3630 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3631 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3632 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3633 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3634 | #endif |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3635 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3636 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3637 | /* If the file was just truncated to a size smaller than the currently |
| 3638 | ** mapped region, reduce the effective mapping size as well. SQLite will |
| 3639 | ** use read() and write() to access data beyond this point from now on. |
| 3640 | */ |
| 3641 | if( nByte<pFile->mmapSize ){ |
| 3642 | pFile->mmapSize = nByte; |
| 3643 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3644 | #endif |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3645 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3646 | return SQLITE_OK; |
| 3647 | } |
| 3648 | } |
| 3649 | |
| 3650 | /* |
| 3651 | ** Determine the current size of a file in bytes |
| 3652 | */ |
| 3653 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3654 | int rc; |
| 3655 | struct stat buf; |
| 3656 | assert( id ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3657 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3658 | SimulateIOError( rc=1 ); |
| 3659 | if( rc!=0 ){ |
| 3660 | ((unixFile*)id)->lastErrno = errno; |
| 3661 | return SQLITE_IOERR_FSTAT; |
| 3662 | } |
| 3663 | *pSize = buf.st_size; |
| 3664 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3665 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3666 | ** writes a single byte into that file in order to work around a bug |
| 3667 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3668 | ** layers, we need to report this file size as zero even though it is |
| 3669 | ** really 1. Ticket #3260. |
| 3670 | */ |
| 3671 | if( *pSize==1 ) *pSize = 0; |
| 3672 | |
| 3673 | |
| 3674 | return SQLITE_OK; |
| 3675 | } |
| 3676 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3677 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3678 | /* |
| 3679 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3680 | ** proxying locking division. |
| 3681 | */ |
| 3682 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3683 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3684 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3685 | /* |
| 3686 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3687 | ** file-control operation. Enlarge the database to nBytes in size |
| 3688 | ** (rounded up to the next chunk-size). If the database is already |
| 3689 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3690 | */ |
| 3691 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3692 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3693 | i64 nSize; /* Required file size */ |
| 3694 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3695 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3696 | if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3697 | |
| 3698 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3699 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3700 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3701 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3702 | /* The code below is handling the return value of osFallocate() |
| 3703 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3704 | ** or an error number on failure". See the manpage for details. */ |
| 3705 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3706 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3707 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3708 | }while( err==EINTR ); |
| 3709 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3710 | #else |
| 3711 | /* If the OS does not have posix_fallocate(), fake it. First use |
| 3712 | ** ftruncate() to set the file size, then write a single byte to |
| 3713 | ** the last byte in each block within the extended region. This |
| 3714 | ** is the same technique used by glibc to implement posix_fallocate() |
| 3715 | ** on systems that do not have a real fallocate() system call. |
| 3716 | */ |
| 3717 | int nBlk = buf.st_blksize; /* File-system block size */ |
| 3718 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3719 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3720 | if( robust_ftruncate(pFile->h, nSize) ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3721 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3722 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3723 | } |
| 3724 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3725 | while( iWrite<nSize ){ |
| 3726 | int nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 3727 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3728 | iWrite += nBlk; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3729 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3730 | #endif |
| 3731 | } |
| 3732 | } |
| 3733 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3734 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3735 | if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3736 | int rc; |
| 3737 | if( pFile->szChunk<=0 ){ |
| 3738 | if( robust_ftruncate(pFile->h, nByte) ){ |
| 3739 | pFile->lastErrno = errno; |
| 3740 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | rc = unixMapfile(pFile, nByte); |
| 3745 | return rc; |
| 3746 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3747 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3748 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3749 | return SQLITE_OK; |
| 3750 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3751 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3752 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3753 | ** If *pArg is inititially negative then this is a query. Set *pArg to |
| 3754 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3755 | ** |
| 3756 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3757 | */ |
| 3758 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3759 | if( *pArg<0 ){ |
| 3760 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3761 | }else if( (*pArg)==0 ){ |
| 3762 | pFile->ctrlFlags &= ~mask; |
| 3763 | }else{ |
| 3764 | pFile->ctrlFlags |= mask; |
| 3765 | } |
| 3766 | } |
| 3767 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3768 | /* Forward declaration */ |
| 3769 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3770 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3771 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3772 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3773 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3774 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3775 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3776 | switch( op ){ |
| 3777 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3778 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3779 | return SQLITE_OK; |
| 3780 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3781 | case SQLITE_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3782 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3783 | return SQLITE_OK; |
| 3784 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3785 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3786 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3787 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3788 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3789 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3790 | int rc; |
| 3791 | SimulateIOErrorBenign(1); |
| 3792 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3793 | SimulateIOErrorBenign(0); |
| 3794 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3795 | } |
| 3796 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3797 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3798 | return SQLITE_OK; |
| 3799 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3800 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3801 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3802 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3803 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3804 | case SQLITE_FCNTL_VFSNAME: { |
| 3805 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3806 | return SQLITE_OK; |
| 3807 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3808 | case SQLITE_FCNTL_TEMPFILENAME: { |
| 3809 | char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname ); |
| 3810 | if( zTFile ){ |
| 3811 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3812 | *(char**)pArg = zTFile; |
| 3813 | } |
| 3814 | return SQLITE_OK; |
| 3815 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 3816 | case SQLITE_FCNTL_HAS_MOVED: { |
| 3817 | *(int*)pArg = fileHasMoved(pFile); |
| 3818 | return SQLITE_OK; |
| 3819 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3820 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3821 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 3822 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3823 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3824 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 3825 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 3826 | } |
| 3827 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3828 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3829 | pFile->mmapSizeMax = newLimit; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3830 | if( pFile->mmapSize>0 ){ |
| 3831 | unixUnmapfile(pFile); |
| 3832 | rc = unixMapfile(pFile, -1); |
| 3833 | } |
dan | bcb8a86 | 2013-04-08 15:30:41 +0000 | [diff] [blame] | 3834 | } |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3835 | return rc; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 3836 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3837 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3838 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3839 | /* The pager calls this method to signal that it has done |
| 3840 | ** a rollback and that the database is therefore unchanged and |
| 3841 | ** it hence it is OK for the transaction change counter to be |
| 3842 | ** unchanged. |
| 3843 | */ |
| 3844 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3845 | ((unixFile*)id)->dbUpdate = 0; |
| 3846 | return SQLITE_OK; |
| 3847 | } |
| 3848 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3849 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3850 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3851 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3852 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3853 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3854 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3855 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3856 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3860 | ** Return the sector size in bytes of the underlying block device for |
| 3861 | ** the specified file. This is almost always 512 bytes, but may be |
| 3862 | ** larger for some devices. |
| 3863 | ** |
| 3864 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3865 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3866 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3867 | ** same for both. |
| 3868 | */ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3869 | #ifndef __QNXNTO__ |
| 3870 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3871 | UNUSED_PARAMETER(NotUsed); |
drh | 8942d41 | 2012-01-02 18:20:14 +0000 | [diff] [blame] | 3872 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3873 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3874 | #endif |
| 3875 | |
| 3876 | /* |
| 3877 | ** The following version of unixSectorSize() is optimized for QNX. |
| 3878 | */ |
| 3879 | #ifdef __QNXNTO__ |
| 3880 | #include <sys/dcmd_blk.h> |
| 3881 | #include <sys/statvfs.h> |
| 3882 | static int unixSectorSize(sqlite3_file *id){ |
| 3883 | unixFile *pFile = (unixFile*)id; |
| 3884 | if( pFile->sectorSize == 0 ){ |
| 3885 | struct statvfs fsInfo; |
| 3886 | |
| 3887 | /* Set defaults for non-supported filesystems */ |
| 3888 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3889 | pFile->deviceCharacteristics = 0; |
| 3890 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
| 3891 | return pFile->sectorSize; |
| 3892 | } |
| 3893 | |
| 3894 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 3895 | pFile->sectorSize = fsInfo.f_bsize; |
| 3896 | pFile->deviceCharacteristics = |
| 3897 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 3898 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3899 | ** the write succeeds */ |
| 3900 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3901 | ** so it is ordered */ |
| 3902 | 0; |
| 3903 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 3904 | pFile->sectorSize = fsInfo.f_bsize; |
| 3905 | pFile->deviceCharacteristics = |
| 3906 | /* etfs cluster size writes are atomic */ |
| 3907 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 3908 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3909 | ** the write succeeds */ |
| 3910 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3911 | ** so it is ordered */ |
| 3912 | 0; |
| 3913 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 3914 | pFile->sectorSize = fsInfo.f_bsize; |
| 3915 | pFile->deviceCharacteristics = |
| 3916 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 3917 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3918 | ** the write succeeds */ |
| 3919 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3920 | ** so it is ordered */ |
| 3921 | 0; |
| 3922 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 3923 | pFile->sectorSize = fsInfo.f_bsize; |
| 3924 | pFile->deviceCharacteristics = |
| 3925 | /* full bitset of atomics from max sector size and smaller */ |
| 3926 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3927 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3928 | ** so it is ordered */ |
| 3929 | 0; |
| 3930 | }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 3931 | pFile->sectorSize = fsInfo.f_bsize; |
| 3932 | pFile->deviceCharacteristics = |
| 3933 | /* full bitset of atomics from max sector size and smaller */ |
| 3934 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3935 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3936 | ** so it is ordered */ |
| 3937 | 0; |
| 3938 | }else{ |
| 3939 | pFile->deviceCharacteristics = |
| 3940 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 3941 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3942 | ** the write succeeds */ |
| 3943 | 0; |
| 3944 | } |
| 3945 | } |
| 3946 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 3947 | ** then it isn't valid.*/ |
| 3948 | if( pFile->sectorSize % 512 != 0 ){ |
| 3949 | pFile->deviceCharacteristics = 0; |
| 3950 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3951 | } |
| 3952 | return pFile->sectorSize; |
| 3953 | } |
| 3954 | #endif /* __QNXNTO__ */ |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3955 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3956 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3957 | ** Return the device characteristics for the file. |
| 3958 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3959 | ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. |
| 3960 | ** However, that choice is contraversial since technically the underlying |
| 3961 | ** file system does not always provide powersafe overwrites. (In other |
| 3962 | ** words, after a power-loss event, parts of the file that were never |
| 3963 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 3964 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 3965 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 3966 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 3967 | ** available to turn it off and URI query parameter available to turn it off. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3968 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3969 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 3970 | unixFile *p = (unixFile*)id; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3971 | int rc = 0; |
| 3972 | #ifdef __QNXNTO__ |
| 3973 | if( p->sectorSize==0 ) unixSectorSize(id); |
| 3974 | rc = p->deviceCharacteristics; |
| 3975 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3976 | if( p->ctrlFlags & UNIXFILE_PSOW ){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3977 | rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3978 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3979 | return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3982 | #ifndef SQLITE_OMIT_WAL |
| 3983 | |
| 3984 | |
| 3985 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3986 | ** Object used to represent an shared memory buffer. |
| 3987 | ** |
| 3988 | ** When multiple threads all reference the same wal-index, each thread |
| 3989 | ** has its own unixShm object, but they all point to a single instance |
| 3990 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3991 | ** only once per process. |
| 3992 | ** |
| 3993 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3994 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3995 | ** every open file that does not use shared memory (in other words, most |
| 3996 | ** open files) would have to carry around this extra information. So |
| 3997 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3998 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3999 | ** |
| 4000 | ** unixMutexHeld() must be true when creating or destroying |
| 4001 | ** this object or while reading or writing the following fields: |
| 4002 | ** |
| 4003 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4004 | ** |
| 4005 | ** The following fields are read-only after the object is created: |
| 4006 | ** |
| 4007 | ** fid |
| 4008 | ** zFilename |
| 4009 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4010 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4011 | ** unixMutexHeld() is true when reading or writing any other field |
| 4012 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4013 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4014 | struct unixShmNode { |
| 4015 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4016 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4017 | char *zFilename; /* Name of the mmapped file */ |
| 4018 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4019 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4020 | u16 nRegion; /* Size of array apRegion */ |
| 4021 | u8 isReadonly; /* True if read-only */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4022 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4023 | int nRef; /* Number of unixShm objects pointing to this */ |
| 4024 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4025 | #ifdef SQLITE_DEBUG |
| 4026 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4027 | u8 sharedMask; /* Mask of shared locks held */ |
| 4028 | u8 nextShmId; /* Next available unixShm.id value */ |
| 4029 | #endif |
| 4030 | }; |
| 4031 | |
| 4032 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4033 | ** Structure used internally by this VFS to record the state of an |
| 4034 | ** open shared memory connection. |
| 4035 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4036 | ** The following fields are initialized when this object is created and |
| 4037 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4038 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4039 | ** unixShm.pFile |
| 4040 | ** unixShm.id |
| 4041 | ** |
| 4042 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 4043 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4044 | */ |
| 4045 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4046 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 4047 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4048 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 4049 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4050 | u16 sharedMask; /* Mask of shared locks held */ |
| 4051 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4052 | }; |
| 4053 | |
| 4054 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4055 | ** Constants used for locking |
| 4056 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 4057 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 4058 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4059 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4060 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4061 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4062 | ** |
| 4063 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 4064 | ** otherwise. |
| 4065 | */ |
| 4066 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4067 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 4068 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4069 | int ofst, /* First byte of the locking range */ |
| 4070 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4071 | ){ |
| 4072 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4073 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4074 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4075 | /* Access to the unixShmNode object is serialized by the caller */ |
| 4076 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4077 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4078 | /* Shared locks never span more than one byte */ |
| 4079 | assert( n==1 || lockType!=F_RDLCK ); |
| 4080 | |
| 4081 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4082 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4083 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4084 | if( pShmNode->h>=0 ){ |
| 4085 | /* Initialize the locking parameters */ |
| 4086 | memset(&f, 0, sizeof(f)); |
| 4087 | f.l_type = lockType; |
| 4088 | f.l_whence = SEEK_SET; |
| 4089 | f.l_start = ofst; |
| 4090 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4091 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4092 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
| 4093 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 4094 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4095 | |
| 4096 | /* Update the global lock state and do debug tracing */ |
| 4097 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4098 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4099 | OSTRACE(("SHM-LOCK ")); |
drh | 47676fe | 2013-12-05 16:41:55 +0000 | [diff] [blame] | 4100 | mask = ofst>31 ? 0xffffffff : (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4101 | if( rc==SQLITE_OK ){ |
| 4102 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4103 | OSTRACE(("unlock %d ok", ofst)); |
| 4104 | pShmNode->exclMask &= ~mask; |
| 4105 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4106 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4107 | OSTRACE(("read-lock %d ok", ofst)); |
| 4108 | pShmNode->exclMask &= ~mask; |
| 4109 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4110 | }else{ |
| 4111 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4112 | OSTRACE(("write-lock %d ok", ofst)); |
| 4113 | pShmNode->exclMask |= mask; |
| 4114 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4115 | } |
| 4116 | }else{ |
| 4117 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4118 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4119 | }else if( lockType==F_RDLCK ){ |
| 4120 | OSTRACE(("read-lock failed")); |
| 4121 | }else{ |
| 4122 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4123 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4124 | } |
| 4125 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4126 | OSTRACE((" - afterwards %03x,%03x\n", |
| 4127 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4128 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4129 | #endif |
| 4130 | |
| 4131 | return rc; |
| 4132 | } |
| 4133 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4134 | |
| 4135 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4136 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4137 | ** |
| 4138 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4139 | ** by VFS shared-memory methods. |
| 4140 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4141 | static void unixShmPurge(unixFile *pFd){ |
| 4142 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4143 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4144 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4145 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4146 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 4147 | sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4148 | for(i=0; i<p->nRegion; i++){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4149 | if( p->h>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4150 | osMunmap(p->apRegion[i], p->szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4151 | }else{ |
| 4152 | sqlite3_free(p->apRegion[i]); |
| 4153 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4154 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4155 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4156 | if( p->h>=0 ){ |
| 4157 | robust_close(pFd, p->h, __LINE__); |
| 4158 | p->h = -1; |
| 4159 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4160 | p->pInode->pShmNode = 0; |
| 4161 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4162 | } |
| 4163 | } |
| 4164 | |
| 4165 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4166 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4167 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4168 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4169 | ** The file used to implement shared-memory is in the same directory |
| 4170 | ** as the open database file and has the same name as the open database |
| 4171 | ** file with the "-shm" suffix added. For example, if the database file |
| 4172 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4173 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4174 | ** |
| 4175 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4176 | ** some other tmpfs mount. But if a file in a different directory |
| 4177 | ** from the database file is used, then differing access permissions |
| 4178 | ** or a chroot() might cause two different processes on the same |
| 4179 | ** database to end up using different files for shared memory - |
| 4180 | ** meaning that their memory would not really be shared - resulting |
| 4181 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4182 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4183 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4184 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4185 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4186 | ** same database file at the same time, database corruption will likely |
| 4187 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4188 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4189 | ** |
| 4190 | ** When opening a new shared-memory file, if no other instances of that |
| 4191 | ** file are currently open, in this process or in other processes, then |
| 4192 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4193 | ** |
| 4194 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4195 | ** that means that an exclusive lock is held on the database file and |
| 4196 | ** that no other processes are able to read or write the database. In |
| 4197 | ** that case, we do not really need shared memory. No shared memory |
| 4198 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4199 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4200 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4201 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4202 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 4203 | int rc; /* Result code */ |
| 4204 | unixInodeInfo *pInode; /* The inode of fd */ |
| 4205 | char *zShmFilename; /* Name of the file used for SHM */ |
| 4206 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4207 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4208 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4209 | p = sqlite3_malloc( sizeof(*p) ); |
| 4210 | if( p==0 ) return SQLITE_NOMEM; |
| 4211 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4212 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4213 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4214 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4215 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4216 | */ |
| 4217 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4218 | pInode = pDbFd->pInode; |
| 4219 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4220 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4221 | struct stat sStat; /* fstat() info for database file */ |
| 4222 | |
| 4223 | /* Call fstat() to figure out the permissions on the database file. If |
| 4224 | ** a new *-shm file is created, an attempt will be made to create it |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 4225 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4226 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4227 | if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4228 | rc = SQLITE_IOERR_FSTAT; |
| 4229 | goto shm_open_err; |
| 4230 | } |
| 4231 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4232 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4233 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4234 | #else |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4235 | nShmFilename = 6 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4236 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4237 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4238 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4239 | rc = SQLITE_NOMEM; |
| 4240 | goto shm_open_err; |
| 4241 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4242 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4243 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4244 | #ifdef SQLITE_SHM_DIRECTORY |
| 4245 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 4246 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4247 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4248 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4249 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4250 | sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4251 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4252 | pShmNode->h = -1; |
| 4253 | pDbFd->pInode->pShmNode = pShmNode; |
| 4254 | pShmNode->pInode = pDbFd->pInode; |
| 4255 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4256 | if( pShmNode->mutex==0 ){ |
| 4257 | rc = SQLITE_NOMEM; |
| 4258 | goto shm_open_err; |
| 4259 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4260 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4261 | if( pInode->bProcessLock==0 ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4262 | int openFlags = O_RDWR | O_CREAT; |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 4263 | if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4264 | openFlags = O_RDONLY; |
| 4265 | pShmNode->isReadonly = 1; |
| 4266 | } |
| 4267 | pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4268 | if( pShmNode->h<0 ){ |
drh | c96d1e7 | 2012-02-11 18:51:34 +0000 | [diff] [blame] | 4269 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 4270 | goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4271 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4272 | |
| 4273 | /* If this process is running as root, make sure that the SHM file |
| 4274 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4275 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4276 | */ |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4277 | osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4278 | |
| 4279 | /* Check to see if another process is holding the dead-man switch. |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4280 | ** If not, truncate the file to zero length. |
| 4281 | */ |
| 4282 | rc = SQLITE_OK; |
| 4283 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 4284 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 4285 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4286 | } |
| 4287 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4288 | if( rc==SQLITE_OK ){ |
| 4289 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
| 4290 | } |
| 4291 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4292 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4295 | /* Make the new connection a child of the unixShmNode */ |
| 4296 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4297 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4298 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4299 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4300 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4301 | pDbFd->pShm = p; |
| 4302 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4303 | |
| 4304 | /* The reference count on pShmNode has already been incremented under |
| 4305 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4306 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4307 | ** left to do is to link the new object into the linked list starting |
| 4308 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 4309 | ** mutex. |
| 4310 | */ |
| 4311 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4312 | p->pNext = pShmNode->pFirst; |
| 4313 | pShmNode->pFirst = p; |
| 4314 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4315 | return SQLITE_OK; |
| 4316 | |
| 4317 | /* Jump here on any error */ |
| 4318 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4319 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4320 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4321 | unixLeaveMutex(); |
| 4322 | return rc; |
| 4323 | } |
| 4324 | |
| 4325 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4326 | ** This function is called to obtain a pointer to region iRegion of the |
| 4327 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4328 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4329 | ** bytes in size. |
| 4330 | ** |
| 4331 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4332 | ** |
| 4333 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4334 | ** region has not been allocated (by any client, including one running in a |
| 4335 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4336 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4337 | ** been allocated, it is allocated by this function. |
| 4338 | ** |
| 4339 | ** If the shared-memory region has already been allocated or is allocated by |
| 4340 | ** this call as described above, then it is mapped into this processes |
| 4341 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4342 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4343 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4344 | static int unixShmMap( |
| 4345 | sqlite3_file *fd, /* Handle open on database file */ |
| 4346 | int iRegion, /* Region to retrieve */ |
| 4347 | int szRegion, /* Size of regions */ |
| 4348 | int bExtend, /* True to extend file if necessary */ |
| 4349 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4350 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4351 | unixFile *pDbFd = (unixFile*)fd; |
| 4352 | unixShm *p; |
| 4353 | unixShmNode *pShmNode; |
| 4354 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4355 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4356 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4357 | if( pDbFd->pShm==0 ){ |
| 4358 | rc = unixOpenSharedMemory(pDbFd); |
| 4359 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4360 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4361 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4362 | p = pDbFd->pShm; |
| 4363 | pShmNode = p->pShmNode; |
| 4364 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4365 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4366 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4367 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4368 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4369 | |
| 4370 | if( pShmNode->nRegion<=iRegion ){ |
| 4371 | char **apNew; /* New apRegion[] array */ |
| 4372 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 4373 | struct stat sStat; /* Used by fstat() */ |
| 4374 | |
| 4375 | pShmNode->szRegion = szRegion; |
| 4376 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4377 | if( pShmNode->h>=0 ){ |
| 4378 | /* The requested region is not mapped into this processes address space. |
| 4379 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4380 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4381 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4382 | if( osFstat(pShmNode->h, &sStat) ){ |
| 4383 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4384 | goto shmpage_out; |
| 4385 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4386 | |
| 4387 | if( sStat.st_size<nByte ){ |
| 4388 | /* The requested memory region does not exist. If bExtend is set to |
| 4389 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4390 | */ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4391 | if( !bExtend ){ |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4392 | goto shmpage_out; |
| 4393 | } |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4394 | |
| 4395 | /* Alternatively, if bExtend is true, extend the file. Do this by |
| 4396 | ** writing a single byte to the end of each (OS) page being |
| 4397 | ** allocated or extended. Technically, we need only write to the |
| 4398 | ** last page in order to extend the file. But writing to all new |
| 4399 | ** pages forces the OS to allocate them immediately, which reduces |
| 4400 | ** the chances of SIGBUS while accessing the mapped region later on. |
| 4401 | */ |
| 4402 | else{ |
| 4403 | static const int pgsz = 4096; |
| 4404 | int iPg; |
| 4405 | |
| 4406 | /* Write to the last byte of each newly allocated or extended page */ |
| 4407 | assert( (nByte % pgsz)==0 ); |
| 4408 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
| 4409 | if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){ |
| 4410 | const char *zFile = pShmNode->zFilename; |
| 4411 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 4412 | goto shmpage_out; |
| 4413 | } |
| 4414 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4415 | } |
| 4416 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | /* Map the requested memory region into this processes address space. */ |
| 4420 | apNew = (char **)sqlite3_realloc( |
| 4421 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 4422 | ); |
| 4423 | if( !apNew ){ |
| 4424 | rc = SQLITE_IOERR_NOMEM; |
| 4425 | goto shmpage_out; |
| 4426 | } |
| 4427 | pShmNode->apRegion = apNew; |
| 4428 | while(pShmNode->nRegion<=iRegion){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4429 | void *pMem; |
| 4430 | if( pShmNode->h>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4431 | pMem = osMmap(0, szRegion, |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4432 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 4433 | MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4434 | ); |
| 4435 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4436 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4437 | goto shmpage_out; |
| 4438 | } |
| 4439 | }else{ |
| 4440 | pMem = sqlite3_malloc(szRegion); |
| 4441 | if( pMem==0 ){ |
| 4442 | rc = SQLITE_NOMEM; |
| 4443 | goto shmpage_out; |
| 4444 | } |
| 4445 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4446 | } |
| 4447 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 4448 | pShmNode->nRegion++; |
| 4449 | } |
| 4450 | } |
| 4451 | |
| 4452 | shmpage_out: |
| 4453 | if( pShmNode->nRegion>iRegion ){ |
| 4454 | *pp = pShmNode->apRegion[iRegion]; |
| 4455 | }else{ |
| 4456 | *pp = 0; |
| 4457 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4458 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4459 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4460 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4464 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4465 | ** |
| 4466 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4467 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4468 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4469 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4470 | */ |
| 4471 | static int unixShmLock( |
| 4472 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4473 | int ofst, /* First lock to acquire or release */ |
| 4474 | int n, /* Number of locks to acquire or release */ |
| 4475 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4476 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4477 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4478 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4479 | unixShm *pX; /* For looping over all siblings */ |
| 4480 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4481 | int rc = SQLITE_OK; /* Result code */ |
| 4482 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4483 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4484 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4485 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4486 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4487 | assert( n>=1 ); |
| 4488 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4489 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4490 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4491 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4492 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4493 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4494 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4495 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4496 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4497 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4498 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4499 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4500 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4501 | |
| 4502 | /* See if any siblings hold this same lock */ |
| 4503 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4504 | if( pX==p ) continue; |
| 4505 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4506 | allMask |= pX->sharedMask; |
| 4507 | } |
| 4508 | |
| 4509 | /* Unlock the system-level locks */ |
| 4510 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4511 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4512 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4513 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4514 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4515 | |
| 4516 | /* Undo the local locks */ |
| 4517 | if( rc==SQLITE_OK ){ |
| 4518 | p->exclMask &= ~mask; |
| 4519 | p->sharedMask &= ~mask; |
| 4520 | } |
| 4521 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4522 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4523 | |
| 4524 | /* Find out which shared locks are already held by sibling connections. |
| 4525 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4526 | ** SQLITE_BUSY. |
| 4527 | */ |
| 4528 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4529 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4530 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4531 | break; |
| 4532 | } |
| 4533 | allShared |= pX->sharedMask; |
| 4534 | } |
| 4535 | |
| 4536 | /* Get shared locks at the system level, if necessary */ |
| 4537 | if( rc==SQLITE_OK ){ |
| 4538 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4539 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4540 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4541 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4542 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4543 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4544 | |
| 4545 | /* Get the local shared locks */ |
| 4546 | if( rc==SQLITE_OK ){ |
| 4547 | p->sharedMask |= mask; |
| 4548 | } |
| 4549 | }else{ |
| 4550 | /* Make sure no sibling connections hold locks that will block this |
| 4551 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4552 | */ |
| 4553 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4554 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4555 | rc = SQLITE_BUSY; |
| 4556 | break; |
| 4557 | } |
| 4558 | } |
| 4559 | |
| 4560 | /* Get the exclusive locks at the system level. Then if successful |
| 4561 | ** also mark the local connection as being locked. |
| 4562 | */ |
| 4563 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4564 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4565 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4566 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4567 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4568 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4569 | } |
| 4570 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4571 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4572 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 4573 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4574 | return rc; |
| 4575 | } |
| 4576 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4577 | /* |
| 4578 | ** Implement a memory barrier or memory fence on shared memory. |
| 4579 | ** |
| 4580 | ** All loads and stores begun before the barrier must complete before |
| 4581 | ** any load or store begun after the barrier. |
| 4582 | */ |
| 4583 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4584 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4585 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4586 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4587 | unixEnterMutex(); |
| 4588 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4591 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4592 | ** Close a connection to shared-memory. Delete the underlying |
| 4593 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4594 | ** |
| 4595 | ** If there is no shared memory associated with the connection then this |
| 4596 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4597 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4598 | static int unixShmUnmap( |
| 4599 | sqlite3_file *fd, /* The underlying database file */ |
| 4600 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4601 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4602 | unixShm *p; /* The connection to be closed */ |
| 4603 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4604 | unixShm **pp; /* For looping over sibling connections */ |
| 4605 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4606 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4607 | pDbFd = (unixFile*)fd; |
| 4608 | p = pDbFd->pShm; |
| 4609 | if( p==0 ) return SQLITE_OK; |
| 4610 | pShmNode = p->pShmNode; |
| 4611 | |
| 4612 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4613 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4614 | |
| 4615 | /* Remove connection p from the set of connections associated |
| 4616 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4617 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4618 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4619 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4620 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4621 | /* Free the connection p */ |
| 4622 | sqlite3_free(p); |
| 4623 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4624 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4625 | |
| 4626 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4627 | ** shared-memory file, too */ |
| 4628 | unixEnterMutex(); |
| 4629 | assert( pShmNode->nRef>0 ); |
| 4630 | pShmNode->nRef--; |
| 4631 | if( pShmNode->nRef==0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 4632 | if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4633 | unixShmPurge(pDbFd); |
| 4634 | } |
| 4635 | unixLeaveMutex(); |
| 4636 | |
| 4637 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4638 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4639 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4640 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4641 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4642 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4643 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4644 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4645 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4646 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4647 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4648 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4649 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4650 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4651 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4652 | static void unixUnmapfile(unixFile *pFd){ |
| 4653 | assert( pFd->nFetchOut==0 ); |
| 4654 | if( pFd->pMapRegion ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4655 | osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4656 | pFd->pMapRegion = 0; |
| 4657 | pFd->mmapSize = 0; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4658 | pFd->mmapSizeActual = 0; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4659 | } |
| 4660 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4661 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4662 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4663 | ** Return the system page size. |
| 4664 | */ |
| 4665 | static int unixGetPagesize(void){ |
| 4666 | #if HAVE_MREMAP |
| 4667 | return 512; |
drh | 85830a7 | 2013-04-03 00:42:01 +0000 | [diff] [blame] | 4668 | #elif defined(_BSD_SOURCE) |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4669 | return getpagesize(); |
| 4670 | #else |
| 4671 | return (int)sysconf(_SC_PAGESIZE); |
| 4672 | #endif |
| 4673 | } |
| 4674 | |
| 4675 | /* |
| 4676 | ** Attempt to set the size of the memory mapping maintained by file |
| 4677 | ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 4678 | ** |
| 4679 | ** If successful, this function sets the following variables: |
| 4680 | ** |
| 4681 | ** unixFile.pMapRegion |
| 4682 | ** unixFile.mmapSize |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4683 | ** unixFile.mmapSizeActual |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4684 | ** |
| 4685 | ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 4686 | ** the three variables above are zeroed. In this case SQLite should |
| 4687 | ** continue accessing the database using the xRead() and xWrite() |
| 4688 | ** methods. |
| 4689 | */ |
| 4690 | static void unixRemapfile( |
| 4691 | unixFile *pFd, /* File descriptor object */ |
| 4692 | i64 nNew /* Required mapping size */ |
| 4693 | ){ |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4694 | const char *zErr = "mmap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4695 | int h = pFd->h; /* File descriptor open on db file */ |
| 4696 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4697 | i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4698 | u8 *pNew = 0; /* Location of new mapping */ |
| 4699 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 4700 | |
| 4701 | assert( pFd->nFetchOut==0 ); |
| 4702 | assert( nNew>pFd->mmapSize ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4703 | assert( nNew<=pFd->mmapSizeMax ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4704 | assert( nNew>0 ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4705 | assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4706 | assert( MAP_FAILED!=0 ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4707 | |
| 4708 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
| 4709 | |
| 4710 | if( pOrig ){ |
| 4711 | const int szSyspage = unixGetPagesize(); |
| 4712 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
| 4713 | u8 *pReq = &pOrig[nReuse]; |
| 4714 | |
| 4715 | /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 4716 | if( nReuse!=nOrig ){ |
| 4717 | osMunmap(pReq, nOrig-nReuse); |
| 4718 | } |
| 4719 | |
| 4720 | #if HAVE_MREMAP |
| 4721 | pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4722 | zErr = "mremap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4723 | #else |
| 4724 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 4725 | if( pNew!=MAP_FAILED ){ |
| 4726 | if( pNew!=pReq ){ |
| 4727 | osMunmap(pNew, nNew - nReuse); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4728 | pNew = 0; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4729 | }else{ |
| 4730 | pNew = pOrig; |
| 4731 | } |
| 4732 | } |
| 4733 | #endif |
| 4734 | |
dan | 48ccef8 | 2013-04-02 20:55:01 +0000 | [diff] [blame] | 4735 | /* The attempt to extend the existing mapping failed. Free it. */ |
| 4736 | if( pNew==MAP_FAILED || pNew==0 ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4737 | osMunmap(pOrig, nReuse); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 4742 | if( pNew==0 ){ |
| 4743 | pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4746 | if( pNew==MAP_FAILED ){ |
| 4747 | pNew = 0; |
| 4748 | nNew = 0; |
| 4749 | unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 4750 | |
| 4751 | /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 4752 | ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 4753 | ** in this case. */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4754 | pFd->mmapSizeMax = 0; |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4755 | } |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4756 | pFd->pMapRegion = (void *)pNew; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4757 | pFd->mmapSize = pFd->mmapSizeActual = nNew; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4761 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 4762 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 4763 | ** there already exists a mapping for this file, and there are still |
| 4764 | ** outstanding xFetch() references to it, this function is a no-op. |
| 4765 | ** |
| 4766 | ** If parameter nByte is non-negative, then it is the requested size of |
| 4767 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 4768 | ** requested size is the size of the file on disk. The actual size of the |
| 4769 | ** created mapping is either the requested size or the value configured |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 4770 | ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4771 | ** |
| 4772 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 4773 | ** recreated as a result of outstanding references) or an SQLite error |
| 4774 | ** code otherwise. |
| 4775 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4776 | static int unixMapfile(unixFile *pFd, i64 nByte){ |
| 4777 | i64 nMap = nByte; |
| 4778 | int rc; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4779 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4780 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
| 4781 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 4782 | |
| 4783 | if( nMap<0 ){ |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4784 | struct stat statbuf; /* Low-level file information */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4785 | rc = osFstat(pFd->h, &statbuf); |
| 4786 | if( rc!=SQLITE_OK ){ |
| 4787 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4788 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4789 | nMap = statbuf.st_size; |
| 4790 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4791 | if( nMap>pFd->mmapSizeMax ){ |
| 4792 | nMap = pFd->mmapSizeMax; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4795 | if( nMap!=pFd->mmapSize ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4796 | if( nMap>0 ){ |
| 4797 | unixRemapfile(pFd, nMap); |
| 4798 | }else{ |
dan | b7e3a32 | 2013-03-25 20:30:13 +0000 | [diff] [blame] | 4799 | unixUnmapfile(pFd); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4800 | } |
| 4801 | } |
| 4802 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4803 | return SQLITE_OK; |
| 4804 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4805 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4806 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4807 | /* |
| 4808 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 4809 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 4810 | ** |
| 4811 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 4812 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 4813 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 4814 | ** value of *pp is undefined in this case. |
| 4815 | ** |
| 4816 | ** If this function does return a pointer, the caller must eventually |
| 4817 | ** release the reference by calling unixUnfetch(). |
| 4818 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4819 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4820 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4821 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 4822 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4823 | *pp = 0; |
| 4824 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4825 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 4826 | if( pFd->mmapSizeMax>0 ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4827 | if( pFd->pMapRegion==0 ){ |
| 4828 | int rc = unixMapfile(pFd, -1); |
| 4829 | if( rc!=SQLITE_OK ) return rc; |
| 4830 | } |
| 4831 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 4832 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 4833 | pFd->nFetchOut++; |
| 4834 | } |
| 4835 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 4836 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4837 | return SQLITE_OK; |
| 4838 | } |
| 4839 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4840 | /* |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4841 | ** If the third argument is non-NULL, then this function releases a |
| 4842 | ** reference obtained by an earlier call to unixFetch(). The second |
| 4843 | ** argument passed to this function must be the same as the corresponding |
| 4844 | ** argument that was passed to the unixFetch() invocation. |
| 4845 | ** |
| 4846 | ** Or, if the third argument is NULL, then this function is being called |
| 4847 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 4848 | ** may now be invalid and should be unmapped. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4849 | */ |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4850 | static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4851 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 4852 | UNUSED_PARAMETER(iOff); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4853 | |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 4854 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4855 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 4856 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 4857 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4858 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 4859 | |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4860 | /* If p!=0, it must match the iOff value. */ |
| 4861 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 4862 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4863 | if( p ){ |
| 4864 | pFd->nFetchOut--; |
| 4865 | }else{ |
| 4866 | unixUnmapfile(pFd); |
| 4867 | } |
| 4868 | |
| 4869 | assert( pFd->nFetchOut>=0 ); |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 4870 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4871 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4875 | ** Here ends the implementation of all sqlite3_file methods. |
| 4876 | ** |
| 4877 | ********************** End sqlite3_file Methods ******************************* |
| 4878 | ******************************************************************************/ |
| 4879 | |
| 4880 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4881 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4882 | ** implement various file locking strategies. It also contains definitions |
| 4883 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4884 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4885 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4886 | ** the correct finder-function for that VFS. |
| 4887 | ** |
| 4888 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4889 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4890 | ** looks at the filesystem type and tries to guess the best locking |
| 4891 | ** strategy from that. |
| 4892 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4893 | ** For finder-funtion F, two objects are created: |
| 4894 | ** |
| 4895 | ** (1) The real finder-function named "FImpt()". |
| 4896 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4897 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4898 | ** |
| 4899 | ** |
| 4900 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4901 | ** objects. We have to do this instead of letting pAppData point |
| 4902 | ** directly at the finder-function since C90 rules prevent a void* |
| 4903 | ** from be cast into a function pointer. |
| 4904 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4905 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4906 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4907 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4908 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 4909 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 4910 | ** |
| 4911 | ** * An I/O method finder function called FINDER that returns a pointer |
| 4912 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4913 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4914 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4915 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4916 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4917 | CLOSE, /* xClose */ \ |
| 4918 | unixRead, /* xRead */ \ |
| 4919 | unixWrite, /* xWrite */ \ |
| 4920 | unixTruncate, /* xTruncate */ \ |
| 4921 | unixSync, /* xSync */ \ |
| 4922 | unixFileSize, /* xFileSize */ \ |
| 4923 | LOCK, /* xLock */ \ |
| 4924 | UNLOCK, /* xUnlock */ \ |
| 4925 | CKLOCK, /* xCheckReservedLock */ \ |
| 4926 | unixFileControl, /* xFileControl */ \ |
| 4927 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4928 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4929 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4930 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4931 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4932 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4933 | unixFetch, /* xFetch */ \ |
| 4934 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4935 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4936 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 4937 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4938 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4939 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4940 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4941 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4942 | |
| 4943 | /* |
| 4944 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 4945 | ** locking strategies. Functions that return pointers to these methods |
| 4946 | ** are also created. |
| 4947 | */ |
| 4948 | IOMETHODS( |
| 4949 | posixIoFinder, /* Finder function name */ |
| 4950 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4951 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4952 | unixClose, /* xClose method */ |
| 4953 | unixLock, /* xLock method */ |
| 4954 | unixUnlock, /* xUnlock method */ |
| 4955 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4956 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4957 | IOMETHODS( |
| 4958 | nolockIoFinder, /* Finder function name */ |
| 4959 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4960 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4961 | nolockClose, /* xClose method */ |
| 4962 | nolockLock, /* xLock method */ |
| 4963 | nolockUnlock, /* xUnlock method */ |
| 4964 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4965 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4966 | IOMETHODS( |
| 4967 | dotlockIoFinder, /* Finder function name */ |
| 4968 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4969 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4970 | dotlockClose, /* xClose method */ |
| 4971 | dotlockLock, /* xLock method */ |
| 4972 | dotlockUnlock, /* xUnlock method */ |
| 4973 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4974 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4975 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4976 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4977 | IOMETHODS( |
| 4978 | flockIoFinder, /* Finder function name */ |
| 4979 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4980 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4981 | flockClose, /* xClose method */ |
| 4982 | flockLock, /* xLock method */ |
| 4983 | flockUnlock, /* xUnlock method */ |
| 4984 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4985 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4986 | #endif |
| 4987 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4988 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4989 | IOMETHODS( |
| 4990 | semIoFinder, /* Finder function name */ |
| 4991 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4992 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4993 | semClose, /* xClose method */ |
| 4994 | semLock, /* xLock method */ |
| 4995 | semUnlock, /* xUnlock method */ |
| 4996 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4997 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4998 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4999 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5000 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5001 | IOMETHODS( |
| 5002 | afpIoFinder, /* Finder function name */ |
| 5003 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5004 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5005 | afpClose, /* xClose method */ |
| 5006 | afpLock, /* xLock method */ |
| 5007 | afpUnlock, /* xUnlock method */ |
| 5008 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5009 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5010 | #endif |
| 5011 | |
| 5012 | /* |
| 5013 | ** The proxy locking method is a "super-method" in the sense that it |
| 5014 | ** opens secondary file descriptors for the conch and lock files and |
| 5015 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 5016 | ** secondary files. For this reason, the division that implements |
| 5017 | ** proxy locking is located much further down in the file. But we need |
| 5018 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 5019 | ** for proxy locking here. So we forward declare the I/O methods. |
| 5020 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5021 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5022 | static int proxyClose(sqlite3_file*); |
| 5023 | static int proxyLock(sqlite3_file*, int); |
| 5024 | static int proxyUnlock(sqlite3_file*, int); |
| 5025 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5026 | IOMETHODS( |
| 5027 | proxyIoFinder, /* Finder function name */ |
| 5028 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5029 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5030 | proxyClose, /* xClose method */ |
| 5031 | proxyLock, /* xLock method */ |
| 5032 | proxyUnlock, /* xUnlock method */ |
| 5033 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5034 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5035 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5036 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5037 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 5038 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5039 | IOMETHODS( |
| 5040 | nfsIoFinder, /* Finder function name */ |
| 5041 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5042 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5043 | unixClose, /* xClose method */ |
| 5044 | unixLock, /* xLock method */ |
| 5045 | nfsUnlock, /* xUnlock method */ |
| 5046 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 5047 | ) |
| 5048 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5049 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5050 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5051 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5052 | ** This "finder" function attempts to determine the best locking strategy |
| 5053 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5054 | ** object that implements that strategy. |
| 5055 | ** |
| 5056 | ** This is for MacOSX only. |
| 5057 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5058 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5059 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5060 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5061 | ){ |
| 5062 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5063 | const char *zFilesystem; /* Filesystem type name */ |
| 5064 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5065 | } aMap[] = { |
| 5066 | { "hfs", &posixIoMethods }, |
| 5067 | { "ufs", &posixIoMethods }, |
| 5068 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5069 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5070 | { "webdav", &nolockIoMethods }, |
| 5071 | { 0, 0 } |
| 5072 | }; |
| 5073 | int i; |
| 5074 | struct statfs fsInfo; |
| 5075 | struct flock lockInfo; |
| 5076 | |
| 5077 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5078 | /* If filePath==NULL that means we are dealing with a transient file |
| 5079 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5080 | return &nolockIoMethods; |
| 5081 | } |
| 5082 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 5083 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 5084 | return &nolockIoMethods; |
| 5085 | } |
| 5086 | for(i=0; aMap[i].zFilesystem; i++){ |
| 5087 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 5088 | return aMap[i].pMethods; |
| 5089 | } |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | /* Default case. Handles, amongst others, "nfs". |
| 5094 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 5095 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5096 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5097 | lockInfo.l_len = 1; |
| 5098 | lockInfo.l_start = 0; |
| 5099 | lockInfo.l_whence = SEEK_SET; |
| 5100 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5101 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5102 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 5103 | return &nfsIoMethods; |
| 5104 | } else { |
| 5105 | return &posixIoMethods; |
| 5106 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5107 | }else{ |
| 5108 | return &dotlockIoMethods; |
| 5109 | } |
| 5110 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5111 | static const sqlite3_io_methods |
| 5112 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5113 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5114 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5115 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5116 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 5117 | /* |
| 5118 | ** This "finder" function attempts to determine the best locking strategy |
| 5119 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 5120 | ** object that implements that strategy. |
| 5121 | ** |
| 5122 | ** This is for VXWorks only. |
| 5123 | */ |
| 5124 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 5125 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5126 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5127 | ){ |
| 5128 | struct flock lockInfo; |
| 5129 | |
| 5130 | if( !filePath ){ |
| 5131 | /* If filePath==NULL that means we are dealing with a transient file |
| 5132 | ** that does not need to be locked. */ |
| 5133 | return &nolockIoMethods; |
| 5134 | } |
| 5135 | |
| 5136 | /* Test if fcntl() is supported and use POSIX style locks. |
| 5137 | ** Otherwise fall back to the named semaphore method. |
| 5138 | */ |
| 5139 | lockInfo.l_len = 1; |
| 5140 | lockInfo.l_start = 0; |
| 5141 | lockInfo.l_whence = SEEK_SET; |
| 5142 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5143 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5144 | return &posixIoMethods; |
| 5145 | }else{ |
| 5146 | return &semIoMethods; |
| 5147 | } |
| 5148 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5149 | static const sqlite3_io_methods |
| 5150 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5151 | |
| 5152 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 5153 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5154 | /* |
| 5155 | ** An abstract type for a pointer to a IO method finder function: |
| 5156 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5157 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5158 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5159 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5160 | /**************************************************************************** |
| 5161 | **************************** sqlite3_vfs methods **************************** |
| 5162 | ** |
| 5163 | ** This division contains the implementation of methods on the |
| 5164 | ** sqlite3_vfs object. |
| 5165 | */ |
| 5166 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 5167 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5168 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5169 | */ |
| 5170 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5171 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5172 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5173 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5174 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5175 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5176 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5177 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5178 | unixFile *pNew = (unixFile *)pId; |
| 5179 | int rc = SQLITE_OK; |
| 5180 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5181 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5182 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5183 | /* Usually the path zFilename should not be a relative pathname. The |
| 5184 | ** exception is when opening the proxy "conch" file in builds that |
| 5185 | ** include the special Apple locking styles. |
| 5186 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5187 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 5188 | assert( zFilename==0 || zFilename[0]=='/' |
| 5189 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 5190 | #else |
| 5191 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5192 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5193 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5194 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5195 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5196 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5197 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5198 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5199 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5200 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5201 | pNew->ctrlFlags = (u8)ctrlFlags; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5202 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | ede01a9 | 2013-05-17 12:10:52 +0000 | [diff] [blame] | 5203 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5204 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5205 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 5206 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5207 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 5208 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 5209 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5210 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5211 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 5212 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5213 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5214 | pNew->pId = vxworksFindFileId(zFilename); |
| 5215 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5216 | ctrlFlags |= UNIXFILE_NOLOCK; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5217 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5218 | } |
| 5219 | #endif |
| 5220 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5221 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5222 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5223 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5224 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5225 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5226 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 5227 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 5228 | ** zFilename remains valid until file is closed, to support */ |
| 5229 | pNew->lockingContext = (void*)zFilename; |
| 5230 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5231 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5232 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5233 | if( pLockingStyle == &posixIoMethods |
| 5234 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5235 | || pLockingStyle == &nfsIoMethods |
| 5236 | #endif |
| 5237 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5238 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5239 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5240 | if( rc!=SQLITE_OK ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5241 | /* If an error occurred in findInodeInfo(), close the file descriptor |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5242 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5243 | ** in two scenarios: |
| 5244 | ** |
| 5245 | ** (a) A call to fstat() failed. |
| 5246 | ** (b) A malloc failed. |
| 5247 | ** |
| 5248 | ** Scenario (b) may only occur if the process is holding no other |
| 5249 | ** file descriptors open on the same file. If there were other file |
| 5250 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5251 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5252 | ** handle h - as it is guaranteed that no posix locks will be released |
| 5253 | ** by doing so. |
| 5254 | ** |
| 5255 | ** If scenario (a) caused the error then things are not so safe. The |
| 5256 | ** implicit assumption here is that if fstat() fails, things are in |
| 5257 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 5258 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5259 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5260 | h = -1; |
| 5261 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5262 | unixLeaveMutex(); |
| 5263 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5264 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5265 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5266 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5267 | /* AFP locking uses the file path so it needs to be included in |
| 5268 | ** the afpLockingContext. |
| 5269 | */ |
| 5270 | afpLockingContext *pCtx; |
| 5271 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 5272 | if( pCtx==0 ){ |
| 5273 | rc = SQLITE_NOMEM; |
| 5274 | }else{ |
| 5275 | /* NB: zFilename exists and remains valid until the file is closed |
| 5276 | ** according to requirement F11141. So we do not need to make a |
| 5277 | ** copy of the filename. */ |
| 5278 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5279 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5280 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5281 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5282 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5283 | if( rc!=SQLITE_OK ){ |
| 5284 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5285 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5286 | h = -1; |
| 5287 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5288 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5289 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5290 | } |
| 5291 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5292 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5293 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5294 | /* Dotfile locking uses the file path so it needs to be included in |
| 5295 | ** the dotlockLockingContext |
| 5296 | */ |
| 5297 | char *zLockFile; |
| 5298 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5299 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5300 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5301 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 5302 | if( zLockFile==0 ){ |
| 5303 | rc = SQLITE_NOMEM; |
| 5304 | }else{ |
| 5305 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5306 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5307 | pNew->lockingContext = zLockFile; |
| 5308 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5309 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5310 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5311 | else if( pLockingStyle == &semIoMethods ){ |
| 5312 | /* Named semaphore locking uses the file path so it needs to be |
| 5313 | ** included in the semLockingContext |
| 5314 | */ |
| 5315 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5316 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5317 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5318 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5319 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5320 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5321 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5322 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5323 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5324 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5325 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5326 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5327 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5328 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5329 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5330 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5331 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5332 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5333 | |
| 5334 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5335 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5336 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5337 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5338 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5339 | osUnlink(zFilename); |
drh | c579754 | 2013-04-27 12:13:29 +0000 | [diff] [blame] | 5340 | pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5341 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5342 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5343 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5344 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5345 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5346 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5347 | OpenCounter(+1); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5348 | verifyDbFile(pNew); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5349 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5350 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5351 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5352 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5353 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5354 | ** Return the name of a directory in which to put temporary files. |
| 5355 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5356 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5357 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5358 | static const char *azDirs[] = { |
| 5359 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5360 | 0, |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 +0000 | [diff] [blame] | 5361 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5362 | "/var/tmp", |
| 5363 | "/usr/tmp", |
| 5364 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5365 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5366 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5367 | unsigned int i; |
| 5368 | struct stat buf; |
| 5369 | const char *zDir = 0; |
| 5370 | |
| 5371 | azDirs[0] = sqlite3_temp_directory; |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 +0000 | [diff] [blame] | 5372 | if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR"); |
| 5373 | if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 5374 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5375 | if( zDir==0 ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5376 | if( osStat(zDir, &buf) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5377 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5378 | if( osAccess(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5379 | break; |
| 5380 | } |
| 5381 | return zDir; |
| 5382 | } |
| 5383 | |
| 5384 | /* |
| 5385 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5386 | ** by the calling process and must be big enough to hold at least |
| 5387 | ** pVfs->mxPathname bytes. |
| 5388 | */ |
| 5389 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5390 | static const unsigned char zChars[] = |
| 5391 | "abcdefghijklmnopqrstuvwxyz" |
| 5392 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 5393 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5394 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5395 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5396 | |
| 5397 | /* It's odd to simulate an io-error here, but really this is just |
| 5398 | ** using the io-error infrastructure to test that SQLite handles this |
| 5399 | ** function failing. |
| 5400 | */ |
| 5401 | SimulateIOError( return SQLITE_IOERR ); |
| 5402 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5403 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5404 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5405 | |
| 5406 | /* Check that the output buffer is large enough for the temporary file |
| 5407 | ** name. If it is not, return SQLITE_ERROR. |
| 5408 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5409 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5410 | return SQLITE_ERROR; |
| 5411 | } |
| 5412 | |
| 5413 | do{ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5414 | sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5415 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5416 | sqlite3_randomness(15, &zBuf[j]); |
| 5417 | for(i=0; i<15; i++, j++){ |
| 5418 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 5419 | } |
| 5420 | zBuf[j] = 0; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5421 | zBuf[j+1] = 0; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5422 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5423 | return SQLITE_OK; |
| 5424 | } |
| 5425 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5426 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5427 | /* |
| 5428 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5429 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5430 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5431 | */ |
| 5432 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5433 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5434 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5435 | /* |
| 5436 | ** Search for an unused file descriptor that was opened on the database |
| 5437 | ** file (not a journal or master-journal file) identified by pathname |
| 5438 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5439 | ** argument to this function. |
| 5440 | ** |
| 5441 | ** Such a file descriptor may exist if a database connection was closed |
| 5442 | ** but the associated file descriptor could not be closed because some |
| 5443 | ** other file descriptor open on the same file is holding a file-lock. |
| 5444 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5445 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5446 | ** further details. Also, ticket #4018. |
| 5447 | ** |
| 5448 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5449 | ** such file descriptor is located, -1 is returned. |
| 5450 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5451 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5452 | UnixUnusedFd *pUnused = 0; |
| 5453 | |
| 5454 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5455 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5456 | ** but because no way to test it is currently available. It is better |
| 5457 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5458 | ** feature. */ |
| 5459 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5460 | struct stat sStat; /* Results of stat() call */ |
| 5461 | |
| 5462 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5463 | ** almost certain that an open() call on the same path will also fail. |
| 5464 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5465 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5466 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5467 | ** |
| 5468 | ** Even if a subsequent open() call does succeed, the consequences of |
| 5469 | ** not searching for a resusable file descriptor are not dire. */ |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5470 | if( 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5471 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5472 | |
| 5473 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5474 | pInode = inodeList; |
| 5475 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 5476 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 5477 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5478 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5479 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5480 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5481 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5482 | pUnused = *pp; |
| 5483 | if( pUnused ){ |
| 5484 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5485 | } |
| 5486 | } |
| 5487 | unixLeaveMutex(); |
| 5488 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5489 | #endif /* if !OS_VXWORKS */ |
| 5490 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5491 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5492 | |
| 5493 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5494 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5495 | ** to create new files with. If no error occurs, then SQLITE_OK is returned |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5496 | ** and a value suitable for passing as the third argument to open(2) is |
| 5497 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5498 | ** returned and the value of *pMode is not modified. |
| 5499 | ** |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5500 | ** In most cases cases, this routine sets *pMode to 0, which will become |
| 5501 | ** an indication to robust_open() to create the file using |
| 5502 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5503 | ** But if the file being opened is a WAL or regular journal file, then |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5504 | ** this function queries the file-system for the permissions on the |
| 5505 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5506 | ** possible, WAL and journal files are created using the same permissions |
| 5507 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5508 | ** |
| 5509 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5510 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5511 | ** FAT filesystems and permissions do not matter there, so just use |
| 5512 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5513 | */ |
| 5514 | static int findCreateFileMode( |
| 5515 | const char *zPath, /* Path of file (possibly) being created */ |
| 5516 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5517 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5518 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5519 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5520 | ){ |
| 5521 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5522 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5523 | *pUid = 0; |
| 5524 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5525 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5526 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5527 | int nDb; /* Number of valid bytes in zDb */ |
| 5528 | struct stat sStat; /* Output of stat() on database file */ |
| 5529 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5530 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5531 | ** the path to the associated database file from zPath. This block handles |
| 5532 | ** the following naming conventions: |
| 5533 | ** |
| 5534 | ** "<path to db>-journal" |
| 5535 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5536 | ** "<path to db>-journalNN" |
| 5537 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5538 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5539 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5540 | ** used by the test_multiplex.c module. |
| 5541 | */ |
| 5542 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5543 | #ifdef SQLITE_ENABLE_8_3_NAMES |
dan | 28a67fd | 2011-12-12 19:48:43 +0000 | [diff] [blame] | 5544 | while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--; |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5545 | if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5546 | #else |
| 5547 | while( zPath[nDb]!='-' ){ |
| 5548 | assert( nDb>0 ); |
| 5549 | assert( zPath[nDb]!='\n' ); |
| 5550 | nDb--; |
| 5551 | } |
| 5552 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5553 | memcpy(zDb, zPath, nDb); |
| 5554 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5555 | |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5556 | if( 0==osStat(zDb, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5557 | *pMode = sStat.st_mode & 0777; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5558 | *pUid = sStat.st_uid; |
| 5559 | *pGid = sStat.st_gid; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5560 | }else{ |
| 5561 | rc = SQLITE_IOERR_FSTAT; |
| 5562 | } |
| 5563 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5564 | *pMode = 0600; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5565 | } |
| 5566 | return rc; |
| 5567 | } |
| 5568 | |
| 5569 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5570 | ** Open the file zPath. |
| 5571 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5572 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5573 | ** one: |
| 5574 | ** |
| 5575 | ** sqlite3OsOpenReadWrite(); |
| 5576 | ** sqlite3OsOpenReadOnly(); |
| 5577 | ** sqlite3OsOpenExclusive(); |
| 5578 | ** |
| 5579 | ** These calls correspond to the following combinations of flags: |
| 5580 | ** |
| 5581 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5582 | ** ReadOnly() -> (READONLY) |
| 5583 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5584 | ** |
| 5585 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5586 | ** true, the file was configured to be automatically deleted when the |
| 5587 | ** file handle closed. To achieve the same effect using this new |
| 5588 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5589 | ** OpenExclusive(). |
| 5590 | */ |
| 5591 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5592 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5593 | const char *zPath, /* Pathname of file to be opened */ |
| 5594 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5595 | int flags, /* Input flags to control the opening */ |
| 5596 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5597 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5598 | unixFile *p = (unixFile *)pFile; |
| 5599 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5600 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5601 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5602 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5603 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5604 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5605 | |
| 5606 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5607 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5608 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5609 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5610 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5611 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5612 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5613 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5614 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5615 | struct statfs fsInfo; |
| 5616 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5617 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5618 | /* If creating a master or main-file journal, this function will open |
| 5619 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5620 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5621 | */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5622 | int syncDir = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5623 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5624 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5625 | || eType==SQLITE_OPEN_WAL |
| 5626 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5627 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5628 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5629 | ** a temporary file. Use this buffer to store the file name in. |
| 5630 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5631 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5632 | const char *zName = zPath; |
| 5633 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5634 | /* Check the following statements are true: |
| 5635 | ** |
| 5636 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5637 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5638 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5639 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5640 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5641 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5642 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5643 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5644 | assert(isDelete==0 || isCreate); |
| 5645 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5646 | /* The main DB, main journal, WAL file and master journal are never |
| 5647 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5648 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5649 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5650 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5651 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5652 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5653 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5654 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5655 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5656 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5657 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5658 | ); |
| 5659 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame^] | 5660 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 5661 | ** here such that two or more threads all trying to open databases at |
| 5662 | ** the same instant might all reset the PRNG. But multiple resets |
| 5663 | ** are harmless. |
| 5664 | */ |
| 5665 | if( randomnessPid!=getpid() ){ |
| 5666 | randomnessPid = getpid(); |
| 5667 | sqlite3_randomness(0,0); |
| 5668 | } |
| 5669 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5670 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5671 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5672 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5673 | UnixUnusedFd *pUnused; |
| 5674 | pUnused = findReusableFd(zName, flags); |
| 5675 | if( pUnused ){ |
| 5676 | fd = pUnused->fd; |
| 5677 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 5678 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5679 | if( !pUnused ){ |
| 5680 | return SQLITE_NOMEM; |
| 5681 | } |
| 5682 | } |
| 5683 | p->pUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5684 | |
| 5685 | /* Database filenames are double-zero terminated if they are not |
| 5686 | ** URIs with parameters. Hence, they can always be passed into |
| 5687 | ** sqlite3_uri_parameter(). */ |
| 5688 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 5689 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5690 | }else if( !zName ){ |
| 5691 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5692 | assert(isDelete && !syncDir); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5693 | rc = unixGetTempname(MAX_PATHNAME+2, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5694 | if( rc!=SQLITE_OK ){ |
| 5695 | return rc; |
| 5696 | } |
| 5697 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5698 | |
| 5699 | /* Generated temporary filenames are always double-zero terminated |
| 5700 | ** for use by sqlite3_uri_parameter(). */ |
| 5701 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5704 | /* Determine the value of the flags parameter passed to POSIX function |
| 5705 | ** open(). These must be calculated even if open() is not called, as |
| 5706 | ** they may be stored as part of the file handle and used by the |
| 5707 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5708 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5709 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5710 | if( isCreate ) openFlags |= O_CREAT; |
| 5711 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5712 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5713 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5714 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5715 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5716 | uid_t uid; /* Userid for the file */ |
| 5717 | gid_t gid; /* Groupid for the file */ |
| 5718 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5719 | if( rc!=SQLITE_OK ){ |
| 5720 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5721 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5722 | return rc; |
| 5723 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5724 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5725 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5726 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 5727 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5728 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5729 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5730 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5731 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5732 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5733 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5734 | } |
| 5735 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5736 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5737 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5738 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5739 | |
| 5740 | /* If this process is running as root and if creating a new rollback |
| 5741 | ** journal or WAL file, set the ownership of the journal or WAL to be |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 5742 | ** the same as the original database. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5743 | */ |
| 5744 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 5745 | osFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5746 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5747 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5748 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5749 | if( pOutFlags ){ |
| 5750 | *pOutFlags = flags; |
| 5751 | } |
| 5752 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5753 | if( p->pUnused ){ |
| 5754 | p->pUnused->fd = fd; |
| 5755 | p->pUnused->flags = flags; |
| 5756 | } |
| 5757 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5758 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5759 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5760 | zPath = zName; |
| 5761 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5762 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5763 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5764 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5765 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5766 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5767 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5768 | } |
| 5769 | #endif |
| 5770 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5771 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5772 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5773 | |
| 5774 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5775 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 5776 | ((unixFile*)pFile)->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5777 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5778 | return SQLITE_IOERR_ACCESS; |
| 5779 | } |
| 5780 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5781 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5782 | } |
| 5783 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5784 | |
| 5785 | /* Set up appropriate ctrlFlags */ |
| 5786 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 5787 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
| 5788 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
| 5789 | if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; |
| 5790 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 5791 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5792 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5793 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5794 | isAutoProxy = 1; |
| 5795 | #endif |
| 5796 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5797 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5798 | int useProxy = 0; |
| 5799 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5800 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5801 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5802 | if( envforce!=NULL ){ |
| 5803 | useProxy = atoi(envforce)>0; |
| 5804 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5805 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5806 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 5807 | ** with fd is a database file, and there are other connections open |
| 5808 | ** on that file that are currently holding advisory locks on it, |
| 5809 | ** then the call to close() will cancel those locks. In practice, |
| 5810 | ** we're assuming that statfs() doesn't fail very often. At least |
| 5811 | ** not while other file descriptors opened by the same process on |
| 5812 | ** the same file are working. */ |
| 5813 | p->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5814 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5815 | rc = SQLITE_IOERR_ACCESS; |
| 5816 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5817 | } |
| 5818 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5819 | } |
| 5820 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5821 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5822 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5823 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5824 | if( rc!=SQLITE_OK ){ |
| 5825 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5826 | ** and clear all the structure's references. Specifically, |
| 5827 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5828 | */ |
| 5829 | unixClose(pFile); |
| 5830 | return rc; |
| 5831 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5832 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5833 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5834 | } |
| 5835 | } |
| 5836 | #endif |
| 5837 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5838 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 5839 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5840 | open_finished: |
| 5841 | if( rc!=SQLITE_OK ){ |
| 5842 | sqlite3_free(p->pUnused); |
| 5843 | } |
| 5844 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5847 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5848 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5849 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5850 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5851 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5852 | static int unixDelete( |
| 5853 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5854 | const char *zPath, /* Name of file to be deleted */ |
| 5855 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5856 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5857 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5858 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5859 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5860 | if( osUnlink(zPath)==(-1) ){ |
| 5861 | if( errno==ENOENT ){ |
| 5862 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 5863 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5864 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5865 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5866 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5867 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5868 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 5869 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5870 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 5871 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5872 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5873 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5874 | if( fsync(fd)==-1 ) |
| 5875 | #else |
| 5876 | if( fsync(fd) ) |
| 5877 | #endif |
| 5878 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5879 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5880 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5881 | robust_close(0, fd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 5882 | }else if( rc==SQLITE_CANTOPEN ){ |
| 5883 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5884 | } |
| 5885 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 5886 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5887 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5890 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5891 | ** Test the existence of or access permissions of file zPath. The |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5892 | ** test performed depends on the value of flags: |
| 5893 | ** |
| 5894 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 5895 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 5896 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 5897 | ** |
| 5898 | ** Otherwise return 0. |
| 5899 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5900 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5901 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 5902 | const char *zPath, /* Path of the file to examine */ |
| 5903 | int flags, /* What do we want to learn about the zPath file? */ |
| 5904 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5905 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 5906 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5907 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5908 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5909 | switch( flags ){ |
| 5910 | case SQLITE_ACCESS_EXISTS: |
| 5911 | amode = F_OK; |
| 5912 | break; |
| 5913 | case SQLITE_ACCESS_READWRITE: |
| 5914 | amode = W_OK|R_OK; |
| 5915 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 5916 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5917 | amode = R_OK; |
| 5918 | break; |
| 5919 | |
| 5920 | default: |
| 5921 | assert(!"Invalid flags argument"); |
| 5922 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5923 | *pResOut = (osAccess(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5924 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 5925 | struct stat buf; |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5926 | if( 0==osStat(zPath, &buf) && buf.st_size==0 ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5927 | *pResOut = 0; |
| 5928 | } |
| 5929 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5930 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5933 | |
| 5934 | /* |
| 5935 | ** Turn a relative pathname into a full pathname. The relative path |
| 5936 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 5937 | ** zPath. |
| 5938 | ** |
| 5939 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 5940 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 5941 | ** this buffer before returning. |
| 5942 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 5943 | static int unixFullPathname( |
| 5944 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 5945 | const char *zPath, /* Possibly relative input path */ |
| 5946 | int nOut, /* Size of output buffer in bytes */ |
| 5947 | char *zOut /* Output buffer */ |
| 5948 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5949 | |
| 5950 | /* It's odd to simulate an io-error here, but really this is just |
| 5951 | ** using the io-error infrastructure to test that SQLite handles this |
| 5952 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5953 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5954 | */ |
| 5955 | SimulateIOError( return SQLITE_ERROR ); |
| 5956 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5957 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 5958 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5959 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5960 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5961 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5962 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5963 | }else{ |
| 5964 | int nCwd; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5965 | if( osGetcwd(zOut, nOut-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5966 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5967 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5968 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5969 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5970 | } |
| 5971 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 5974 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5975 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 5976 | /* |
| 5977 | ** Interfaces for opening a shared library, finding entry points |
| 5978 | ** within the shared library, and closing the shared library. |
| 5979 | */ |
| 5980 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5981 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 5982 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5983 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 5984 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 5985 | |
| 5986 | /* |
| 5987 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 5988 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 5989 | ** message is available, it is written to zBufOut. If no error message |
| 5990 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 5991 | ** error message. |
| 5992 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5993 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 5994 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5995 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5996 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5997 | zErr = dlerror(); |
| 5998 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5999 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6000 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6001 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6002 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6003 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 6004 | /* |
| 6005 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 6006 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 6007 | ** returns a void* which is really a pointer to a function. So how do we |
| 6008 | ** use dlsym() with -pedantic-errors? |
| 6009 | ** |
| 6010 | ** Variable x below is defined to be a pointer to a function taking |
| 6011 | ** parameters void* and const char* and returning a pointer to a function. |
| 6012 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 6013 | ** (That assignment requires a cast.) Then we call the function that |
| 6014 | ** x points to. |
| 6015 | ** |
| 6016 | ** This work-around is unlikely to work correctly on any system where |
| 6017 | ** you really cannot cast a function pointer into void*. But then, on the |
| 6018 | ** other hand, dlsym() will not work on such a system either, so we have |
| 6019 | ** not really lost anything. |
| 6020 | */ |
| 6021 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6022 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6023 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 6024 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6025 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6026 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 6027 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6028 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6029 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6030 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6031 | #define unixDlOpen 0 |
| 6032 | #define unixDlError 0 |
| 6033 | #define unixDlSym 0 |
| 6034 | #define unixDlClose 0 |
| 6035 | #endif |
| 6036 | |
| 6037 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6038 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6039 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6040 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 6041 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 6042 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6043 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6044 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 6045 | ** errors. The reports issued by valgrind are incorrect - we would |
| 6046 | ** prefer that the randomness be increased by making use of the |
| 6047 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 6048 | ** some users. Rather than argue, it seems easier just to initialize |
| 6049 | ** the whole array and silence valgrind, even if that means less randomness |
| 6050 | ** in the random seed. |
| 6051 | ** |
| 6052 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 6053 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6054 | ** tests repeatable. |
| 6055 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6056 | memset(zBuf, 0, nBuf); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame^] | 6057 | randomnessPid = getpid(); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6058 | #if !defined(SQLITE_TEST) |
| 6059 | { |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame^] | 6060 | int fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6061 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6062 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 6063 | time_t t; |
| 6064 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6065 | memcpy(zBuf, &t, sizeof(t)); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame^] | 6066 | memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 6067 | assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 6068 | nBuf = sizeof(t) + sizeof(randomnessPid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6069 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 6070 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6071 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6072 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6073 | } |
| 6074 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 6075 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6078 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6079 | /* |
| 6080 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6081 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6082 | ** The return value is the number of microseconds of sleep actually |
| 6083 | ** requested from the underlying operating system, a number which |
| 6084 | ** might be greater than or equal to the argument, but not less |
| 6085 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6086 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6087 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6088 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6089 | struct timespec sp; |
| 6090 | |
| 6091 | sp.tv_sec = microseconds / 1000000; |
| 6092 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 6093 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6094 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6095 | return microseconds; |
| 6096 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6097 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6098 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6099 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6100 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6101 | int seconds = (microseconds+999999)/1000000; |
| 6102 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6103 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6104 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 6105 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6109 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6110 | ** the number of seconds since 1970 and is used to set the result of |
| 6111 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6112 | */ |
| 6113 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6114 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6115 | #endif |
| 6116 | |
| 6117 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6118 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6119 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6120 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6121 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6122 | ** proleptic Gregorian calendar. |
| 6123 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6124 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 6125 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6126 | */ |
| 6127 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 6128 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6129 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6130 | #if defined(NO_GETTOD) |
| 6131 | time_t t; |
| 6132 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 6133 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6134 | #elif OS_VXWORKS |
| 6135 | struct timespec sNow; |
| 6136 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 6137 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 6138 | #else |
| 6139 | struct timeval sNow; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6140 | if( gettimeofday(&sNow, 0)==0 ){ |
| 6141 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 6142 | }else{ |
| 6143 | rc = SQLITE_ERROR; |
| 6144 | } |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6145 | #endif |
| 6146 | |
| 6147 | #ifdef SQLITE_TEST |
| 6148 | if( sqlite3_current_time ){ |
| 6149 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 6150 | } |
| 6151 | #endif |
| 6152 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6153 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
| 6156 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6157 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6158 | ** current time and date as a Julian Day number into *prNow and |
| 6159 | ** return 0. Return 1 if the time and date cannot be found. |
| 6160 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6161 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 6162 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6163 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 6164 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6165 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 6166 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6167 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6168 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6169 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6170 | /* |
| 6171 | ** We added the xGetLastError() method with the intention of providing |
| 6172 | ** better low-level error messages when operating-system problems come up |
| 6173 | ** during SQLite operation. But so far, none of that has been implemented |
| 6174 | ** in the core. So this routine is never called. For now, it is merely |
| 6175 | ** a place-holder. |
| 6176 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6177 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 6178 | UNUSED_PARAMETER(NotUsed); |
| 6179 | UNUSED_PARAMETER(NotUsed2); |
| 6180 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6181 | return 0; |
| 6182 | } |
| 6183 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6184 | |
| 6185 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6186 | ************************ End of sqlite3_vfs methods *************************** |
| 6187 | ******************************************************************************/ |
| 6188 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6189 | /****************************************************************************** |
| 6190 | ************************** Begin Proxy Locking ******************************** |
| 6191 | ** |
| 6192 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 6193 | ** other locking methods on secondary lock files. Proxy locking is a |
| 6194 | ** meta-layer over top of the primitive locking implemented above. For |
| 6195 | ** this reason, the division that implements of proxy locking is deferred |
| 6196 | ** until late in the file (here) after all of the other I/O methods have |
| 6197 | ** been defined - so that the primitive locking methods are available |
| 6198 | ** as services to help with the implementation of proxy locking. |
| 6199 | ** |
| 6200 | **** |
| 6201 | ** |
| 6202 | ** The default locking schemes in SQLite use byte-range locks on the |
| 6203 | ** database file to coordinate safe, concurrent access by multiple readers |
| 6204 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 6205 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 6206 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 6207 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 6208 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 6209 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 6210 | ** address in the shared range is taken for a SHARED lock, the entire |
| 6211 | ** shared range is taken for an EXCLUSIVE lock): |
| 6212 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 6213 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6214 | ** RESERVED_BYTE 0x40000001 |
| 6215 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 6216 | ** |
| 6217 | ** This works well on the local file system, but shows a nearly 100x |
| 6218 | ** slowdown in read performance on AFP because the AFP client disables |
| 6219 | ** the read cache when byte-range locks are present. Enabling the read |
| 6220 | ** cache exposes a cache coherency problem that is present on all OS X |
| 6221 | ** supported network file systems. NFS and AFP both observe the |
| 6222 | ** close-to-open semantics for ensuring cache coherency |
| 6223 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 6224 | ** address the requirements for concurrent database access by multiple |
| 6225 | ** readers and writers |
| 6226 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 6227 | ** |
| 6228 | ** To address the performance and cache coherency issues, proxy file locking |
| 6229 | ** changes the way database access is controlled by limiting access to a |
| 6230 | ** single host at a time and moving file locks off of the database file |
| 6231 | ** and onto a proxy file on the local file system. |
| 6232 | ** |
| 6233 | ** |
| 6234 | ** Using proxy locks |
| 6235 | ** ----------------- |
| 6236 | ** |
| 6237 | ** C APIs |
| 6238 | ** |
| 6239 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 6240 | ** <proxy_path> | ":auto:"); |
| 6241 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 6242 | ** |
| 6243 | ** |
| 6244 | ** SQL pragmas |
| 6245 | ** |
| 6246 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 6247 | ** PRAGMA [database.]lock_proxy_file |
| 6248 | ** |
| 6249 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 6250 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 6251 | ** a proxy path based on the user's temp dir |
| 6252 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 6253 | ** actual proxy file name is generated from the name and path of the |
| 6254 | ** database file. For example: |
| 6255 | ** |
| 6256 | ** For database path "/Users/me/foo.db" |
| 6257 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 6258 | ** |
| 6259 | ** Once a lock proxy is configured for a database connection, it can not |
| 6260 | ** be removed, however it may be switched to a different proxy path via |
| 6261 | ** the above APIs (assuming the conch file is not being held by another |
| 6262 | ** connection or process). |
| 6263 | ** |
| 6264 | ** |
| 6265 | ** How proxy locking works |
| 6266 | ** ----------------------- |
| 6267 | ** |
| 6268 | ** Proxy file locking relies primarily on two new supporting files: |
| 6269 | ** |
| 6270 | ** * conch file to limit access to the database file to a single host |
| 6271 | ** at a time |
| 6272 | ** |
| 6273 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6274 | ** taken on the database |
| 6275 | ** |
| 6276 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6277 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6278 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6279 | ** proxy path against the values stored in the conch. The conch file is |
| 6280 | ** stored in the same directory as the database file and the file name |
| 6281 | ** is patterned after the database file name as ".<databasename>-conch". |
| 6282 | ** If the conch file does not exist, or it's contents do not match the |
| 6283 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6284 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6285 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6286 | ** is held by another process (with a shared lock), the exclusive lock |
| 6287 | ** will fail and SQLITE_BUSY is returned. |
| 6288 | ** |
| 6289 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6290 | ** normally taken on the database file. This allows for safe sharing |
| 6291 | ** of the database file for multiple readers and writers on the same |
| 6292 | ** host (the conch ensures that they all use the same local lock file). |
| 6293 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6294 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6295 | ** only taken when the first request to lock database file is made. |
| 6296 | ** This matches the semantics of the traditional locking behavior, where |
| 6297 | ** opening a connection to a database file does not take a lock on it. |
| 6298 | ** The shared lock and an open file descriptor are maintained until |
| 6299 | ** the connection to the database is closed. |
| 6300 | ** |
| 6301 | ** The proxy file and the lock file are never deleted so they only need |
| 6302 | ** to be created the first time they are used. |
| 6303 | ** |
| 6304 | ** Configuration options |
| 6305 | ** --------------------- |
| 6306 | ** |
| 6307 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6308 | ** |
| 6309 | ** Database files accessed on non-local file systems are |
| 6310 | ** automatically configured for proxy locking, lock files are |
| 6311 | ** named automatically using the same logic as |
| 6312 | ** PRAGMA lock_proxy_file=":auto:" |
| 6313 | ** |
| 6314 | ** SQLITE_PROXY_DEBUG |
| 6315 | ** |
| 6316 | ** Enables the logging of error messages during host id file |
| 6317 | ** retrieval and creation |
| 6318 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6319 | ** LOCKPROXYDIR |
| 6320 | ** |
| 6321 | ** Overrides the default directory used for lock proxy files that |
| 6322 | ** are named automatically via the ":auto:" setting |
| 6323 | ** |
| 6324 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6325 | ** |
| 6326 | ** Permissions to use when creating a directory for storing the |
| 6327 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6328 | ** |
| 6329 | ** |
| 6330 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6331 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6332 | ** force proxy locking to be used for every database file opened, and 0 |
| 6333 | ** will force automatic proxy locking to be disabled for all database |
| 6334 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 6335 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6336 | */ |
| 6337 | |
| 6338 | /* |
| 6339 | ** Proxy locking is only available on MacOSX |
| 6340 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6341 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6342 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6343 | /* |
| 6344 | ** The proxyLockingContext has the path and file structures for the remote |
| 6345 | ** and local proxy files in it |
| 6346 | */ |
| 6347 | typedef struct proxyLockingContext proxyLockingContext; |
| 6348 | struct proxyLockingContext { |
| 6349 | unixFile *conchFile; /* Open conch file */ |
| 6350 | char *conchFilePath; /* Name of the conch file */ |
| 6351 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6352 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6353 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6354 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6355 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6356 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6357 | }; |
| 6358 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6359 | /* |
| 6360 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6361 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6362 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6363 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6364 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6365 | int len; |
| 6366 | int dbLen; |
| 6367 | int i; |
| 6368 | |
| 6369 | #ifdef LOCKPROXYDIR |
| 6370 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6371 | #else |
| 6372 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6373 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6374 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6375 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 6376 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6377 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6378 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6379 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6380 | } |
| 6381 | # else |
| 6382 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6383 | # endif |
| 6384 | #endif |
| 6385 | |
| 6386 | if( lPath[len-1]!='/' ){ |
| 6387 | len = strlcat(lPath, "/", maxLen); |
| 6388 | } |
| 6389 | |
| 6390 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6391 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6392 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6393 | char c = dbPath[i]; |
| 6394 | lPath[i+len] = (c=='/')?'_':c; |
| 6395 | } |
| 6396 | lPath[i+len]='\0'; |
| 6397 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6398 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6399 | return SQLITE_OK; |
| 6400 | } |
| 6401 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6402 | /* |
| 6403 | ** Creates the lock file and any missing directories in lockPath |
| 6404 | */ |
| 6405 | static int proxyCreateLockPath(const char *lockPath){ |
| 6406 | int i, len; |
| 6407 | char buf[MAXPATHLEN]; |
| 6408 | int start = 0; |
| 6409 | |
| 6410 | assert(lockPath!=NULL); |
| 6411 | /* try to create all the intermediate directories */ |
| 6412 | len = (int)strlen(lockPath); |
| 6413 | buf[0] = lockPath[0]; |
| 6414 | for( i=1; i<len; i++ ){ |
| 6415 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6416 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6417 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6418 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6419 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6420 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6421 | int err=errno; |
| 6422 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6423 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6424 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6425 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6426 | return err; |
| 6427 | } |
| 6428 | } |
| 6429 | } |
| 6430 | start=i+1; |
| 6431 | } |
| 6432 | buf[i] = lockPath[i]; |
| 6433 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6434 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6435 | return 0; |
| 6436 | } |
| 6437 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6438 | /* |
| 6439 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6440 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6441 | ** |
| 6442 | ** The caller is responsible not only for closing the file descriptor |
| 6443 | ** but also for freeing the memory associated with the file descriptor. |
| 6444 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6445 | static int proxyCreateUnixFile( |
| 6446 | const char *path, /* path for the new unixFile */ |
| 6447 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6448 | int islockfile /* if non zero missing dirs will be created */ |
| 6449 | ) { |
| 6450 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6451 | unixFile *pNew; |
| 6452 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6453 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6454 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6455 | int terrno = 0; |
| 6456 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6457 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6458 | /* 1. first try to open/create the file |
| 6459 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 6460 | ** the parent directories and then try again. |
| 6461 | ** 3. if that fails, try to open the file read-only |
| 6462 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 6463 | */ |
| 6464 | pUnused = findReusableFd(path, openFlags); |
| 6465 | if( pUnused ){ |
| 6466 | fd = pUnused->fd; |
| 6467 | }else{ |
| 6468 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 6469 | if( !pUnused ){ |
| 6470 | return SQLITE_NOMEM; |
| 6471 | } |
| 6472 | } |
| 6473 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6474 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6475 | terrno = errno; |
| 6476 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 6477 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6478 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6479 | } |
| 6480 | } |
| 6481 | } |
| 6482 | if( fd<0 ){ |
| 6483 | openFlags = O_RDONLY; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6484 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6485 | terrno = errno; |
| 6486 | } |
| 6487 | if( fd<0 ){ |
| 6488 | if( islockfile ){ |
| 6489 | return SQLITE_BUSY; |
| 6490 | } |
| 6491 | switch (terrno) { |
| 6492 | case EACCES: |
| 6493 | return SQLITE_PERM; |
| 6494 | case EIO: |
| 6495 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 6496 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6497 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6498 | } |
| 6499 | } |
| 6500 | |
| 6501 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 6502 | if( pNew==NULL ){ |
| 6503 | rc = SQLITE_NOMEM; |
| 6504 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6505 | } |
| 6506 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6507 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6508 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6509 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6510 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6511 | pUnused->fd = fd; |
| 6512 | pUnused->flags = openFlags; |
| 6513 | pNew->pUnused = pUnused; |
| 6514 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6515 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6516 | if( rc==SQLITE_OK ){ |
| 6517 | *ppFile = pNew; |
| 6518 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6519 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6520 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6521 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6522 | sqlite3_free(pNew); |
| 6523 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6524 | return rc; |
| 6525 | } |
| 6526 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6527 | #ifdef SQLITE_TEST |
| 6528 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6529 | int sqlite3_hostid_num = 0; |
| 6530 | #endif |
| 6531 | |
| 6532 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 6533 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6534 | /* Not always defined in the headers as it ought to be */ |
| 6535 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 6536 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6537 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 6538 | ** bytes of writable memory. |
| 6539 | */ |
| 6540 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6541 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 6542 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6543 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 6544 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6545 | { |
| 6546 | static const struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 6547 | if( gethostuuid(pHostID, &timeout) ){ |
| 6548 | int err = errno; |
| 6549 | if( pError ){ |
| 6550 | *pError = err; |
| 6551 | } |
| 6552 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6553 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6554 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6555 | #else |
| 6556 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6557 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6558 | #ifdef SQLITE_TEST |
| 6559 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6560 | if( sqlite3_hostid_num != 0){ |
| 6561 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 6562 | } |
| 6563 | #endif |
| 6564 | |
| 6565 | return SQLITE_OK; |
| 6566 | } |
| 6567 | |
| 6568 | /* The conch file contains the header, host id and lock file path |
| 6569 | */ |
| 6570 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 6571 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 6572 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 6573 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 6574 | |
| 6575 | /* |
| 6576 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 6577 | ** it back. The newly created file's file descriptor is assigned to the |
| 6578 | ** conch file structure and finally the original conch file descriptor is |
| 6579 | ** closed. Returns zero if successful. |
| 6580 | */ |
| 6581 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 6582 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6583 | unixFile *conchFile = pCtx->conchFile; |
| 6584 | char tPath[MAXPATHLEN]; |
| 6585 | char buf[PROXY_MAXCONCHLEN]; |
| 6586 | char *cPath = pCtx->conchFilePath; |
| 6587 | size_t readLen = 0; |
| 6588 | size_t pathLen = 0; |
| 6589 | char errmsg[64] = ""; |
| 6590 | int fd = -1; |
| 6591 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6592 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6593 | |
| 6594 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 6595 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 6596 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 6597 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6598 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6599 | goto end_breaklock; |
| 6600 | } |
| 6601 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6602 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6603 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6604 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6605 | goto end_breaklock; |
| 6606 | } |
| 6607 | /* write it out to the temporary break file */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6608 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6609 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6610 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6611 | goto end_breaklock; |
| 6612 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6613 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6614 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6615 | goto end_breaklock; |
| 6616 | } |
| 6617 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6618 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6619 | goto end_breaklock; |
| 6620 | } |
| 6621 | rc = 0; |
| 6622 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6623 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6624 | conchFile->h = fd; |
| 6625 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 6626 | |
| 6627 | end_breaklock: |
| 6628 | if( rc ){ |
| 6629 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6630 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6631 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6632 | } |
| 6633 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 6634 | } |
| 6635 | return rc; |
| 6636 | } |
| 6637 | |
| 6638 | /* Take the requested lock on the conch file and break a stale lock if the |
| 6639 | ** host id matches. |
| 6640 | */ |
| 6641 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 6642 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6643 | unixFile *conchFile = pCtx->conchFile; |
| 6644 | int rc = SQLITE_OK; |
| 6645 | int nTries = 0; |
| 6646 | struct timespec conchModTime; |
| 6647 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6648 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6649 | do { |
| 6650 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6651 | nTries ++; |
| 6652 | if( rc==SQLITE_BUSY ){ |
| 6653 | /* If the lock failed (busy): |
| 6654 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 6655 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 6656 | * 10 sec and try again |
| 6657 | * 3rd try: break the lock unless the mod time has changed. |
| 6658 | */ |
| 6659 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6660 | if( osFstat(conchFile->h, &buf) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6661 | pFile->lastErrno = errno; |
| 6662 | return SQLITE_IOERR_LOCK; |
| 6663 | } |
| 6664 | |
| 6665 | if( nTries==1 ){ |
| 6666 | conchModTime = buf.st_mtimespec; |
| 6667 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 6668 | continue; |
| 6669 | } |
| 6670 | |
| 6671 | assert( nTries>1 ); |
| 6672 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 6673 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 6674 | return SQLITE_BUSY; |
| 6675 | } |
| 6676 | |
| 6677 | if( nTries==2 ){ |
| 6678 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6679 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6680 | if( len<0 ){ |
| 6681 | pFile->lastErrno = errno; |
| 6682 | return SQLITE_IOERR_LOCK; |
| 6683 | } |
| 6684 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 6685 | /* don't break the lock if the host id doesn't match */ |
| 6686 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 6687 | return SQLITE_BUSY; |
| 6688 | } |
| 6689 | }else{ |
| 6690 | /* don't break the lock on short read or a version mismatch */ |
| 6691 | return SQLITE_BUSY; |
| 6692 | } |
| 6693 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 6694 | continue; |
| 6695 | } |
| 6696 | |
| 6697 | assert( nTries==3 ); |
| 6698 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 6699 | rc = SQLITE_OK; |
| 6700 | if( lockType==EXCLUSIVE_LOCK ){ |
| 6701 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6702 | } |
| 6703 | if( !rc ){ |
| 6704 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6705 | } |
| 6706 | } |
| 6707 | } |
| 6708 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6709 | |
| 6710 | return rc; |
| 6711 | } |
| 6712 | |
| 6713 | /* Takes the conch by taking a shared lock and read the contents conch, if |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6714 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6715 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6716 | ** host IDs match, or a new lock path will be generated automatically |
| 6717 | ** and written to the conch file. |
| 6718 | */ |
| 6719 | static int proxyTakeConch(unixFile *pFile){ |
| 6720 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6721 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6722 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6723 | return SQLITE_OK; |
| 6724 | }else{ |
| 6725 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6726 | uuid_t myHostID; |
| 6727 | int pError = 0; |
| 6728 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6729 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6730 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6731 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6732 | int createConch = 0; |
| 6733 | int hostIdMatch = 0; |
| 6734 | int readLen = 0; |
| 6735 | int tryOldLockPath = 0; |
| 6736 | int forceNewLockPath = 0; |
| 6737 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6738 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 6739 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6740 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6741 | rc = proxyGetHostID(myHostID, &pError); |
| 6742 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 6743 | pFile->lastErrno = pError; |
| 6744 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6745 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6746 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6747 | if( rc!=SQLITE_OK ){ |
| 6748 | goto end_takeconch; |
| 6749 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6750 | /* read the existing conch file */ |
| 6751 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 6752 | if( readLen<0 ){ |
| 6753 | /* I/O error: lastErrno set by seekAndRead */ |
| 6754 | pFile->lastErrno = conchFile->lastErrno; |
| 6755 | rc = SQLITE_IOERR_READ; |
| 6756 | goto end_takeconch; |
| 6757 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 6758 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 6759 | /* a short read or version format mismatch means we need to create a new |
| 6760 | ** conch file. |
| 6761 | */ |
| 6762 | createConch = 1; |
| 6763 | } |
| 6764 | /* if the host id matches and the lock path already exists in the conch |
| 6765 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6766 | ** retry with a new auto-generated path |
| 6767 | */ |
| 6768 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6769 | |
| 6770 | if( !createConch && !forceNewLockPath ){ |
| 6771 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6772 | PROXY_HOSTIDLEN); |
| 6773 | /* if the conch has data compare the contents */ |
| 6774 | if( !pCtx->lockProxyPath ){ |
| 6775 | /* for auto-named local lock file, just check the host ID and we'll |
| 6776 | ** use the local lock file path that's already in there |
| 6777 | */ |
| 6778 | if( hostIdMatch ){ |
| 6779 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6780 | |
| 6781 | if( pathLen>=MAXPATHLEN ){ |
| 6782 | pathLen=MAXPATHLEN-1; |
| 6783 | } |
| 6784 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6785 | lockPath[pathLen] = 0; |
| 6786 | tempLockPath = lockPath; |
| 6787 | tryOldLockPath = 1; |
| 6788 | /* create a copy of the lock path if the conch is taken */ |
| 6789 | goto end_takeconch; |
| 6790 | } |
| 6791 | }else if( hostIdMatch |
| 6792 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6793 | readLen-PROXY_PATHINDEX) |
| 6794 | ){ |
| 6795 | /* conch host and lock path match */ |
| 6796 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6797 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6801 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6802 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6803 | goto end_takeconch; |
| 6804 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6805 | |
| 6806 | /* either the conch didn't match or we need to create a new one */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6807 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6808 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6809 | tempLockPath = lockPath; |
| 6810 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6811 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6812 | |
| 6813 | /* update conch with host and path (this will fail if other process |
| 6814 | ** has a shared lock already), if the host id matches, use the big |
| 6815 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6816 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6817 | futimes(conchFile->h, NULL); |
| 6818 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 6819 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6820 | /* We are trying for an exclusive lock but another thread in this |
| 6821 | ** same process is still holding a shared lock. */ |
| 6822 | rc = SQLITE_BUSY; |
| 6823 | } else { |
| 6824 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6825 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6826 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6827 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6828 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6829 | if( rc==SQLITE_OK ){ |
| 6830 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 6831 | int writeSize = 0; |
| 6832 | |
| 6833 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 6834 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 6835 | if( pCtx->lockProxyPath!=NULL ){ |
| 6836 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 6837 | }else{ |
| 6838 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 6839 | } |
| 6840 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6841 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6842 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 6843 | fsync(conchFile->h); |
| 6844 | /* If we created a new conch file (not just updated the contents of a |
| 6845 | ** valid conch file), try to match the permissions of the database |
| 6846 | */ |
| 6847 | if( rc==SQLITE_OK && createConch ){ |
| 6848 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6849 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6850 | if( err==0 ){ |
| 6851 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 6852 | S_IROTH|S_IWOTH); |
| 6853 | /* try to match the database file R/W permissions, ignore failure */ |
| 6854 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6855 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6856 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6857 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6858 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6859 | }while( rc==(-1) && errno==EINTR ); |
| 6860 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6861 | int code = errno; |
| 6862 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6863 | cmode, code, strerror(code)); |
| 6864 | } else { |
| 6865 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6866 | } |
| 6867 | }else{ |
| 6868 | int code = errno; |
| 6869 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6870 | err, code, strerror(code)); |
| 6871 | #endif |
| 6872 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6873 | } |
| 6874 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6875 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6876 | |
| 6877 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6878 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6879 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6880 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6881 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 6882 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6883 | } |
| 6884 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6885 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6886 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6887 | if( fd>=0 ){ |
| 6888 | pFile->h = fd; |
| 6889 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6890 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6891 | during locking */ |
| 6892 | } |
| 6893 | } |
| 6894 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6895 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6896 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6897 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6898 | /* we couldn't create the proxy lock file with the old lock file path |
| 6899 | ** so try again via auto-naming |
| 6900 | */ |
| 6901 | forceNewLockPath = 1; |
| 6902 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6903 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6904 | } |
| 6905 | } |
| 6906 | if( rc==SQLITE_OK ){ |
| 6907 | /* Need to make a copy of path if we extracted the value |
| 6908 | ** from the conch file or the path was allocated on the stack |
| 6909 | */ |
| 6910 | if( tempLockPath ){ |
| 6911 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6912 | if( !pCtx->lockProxyPath ){ |
| 6913 | rc = SQLITE_NOMEM; |
| 6914 | } |
| 6915 | } |
| 6916 | } |
| 6917 | if( rc==SQLITE_OK ){ |
| 6918 | pCtx->conchHeld = 1; |
| 6919 | |
| 6920 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 6921 | afpLockingContext *afpCtx; |
| 6922 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 6923 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 6924 | } |
| 6925 | } else { |
| 6926 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6927 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6928 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 6929 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6930 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6931 | } while (1); /* in case we need to retry the :auto: lock file - |
| 6932 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6933 | } |
| 6934 | } |
| 6935 | |
| 6936 | /* |
| 6937 | ** If pFile holds a lock on a conch file, then release that lock. |
| 6938 | */ |
| 6939 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 6940 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6941 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 6942 | unixFile *conchFile; /* Name of the conch file */ |
| 6943 | |
| 6944 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6945 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6946 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6947 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6948 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6949 | if( pCtx->conchHeld>0 ){ |
| 6950 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6951 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6952 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6953 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 6954 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6955 | return rc; |
| 6956 | } |
| 6957 | |
| 6958 | /* |
| 6959 | ** Given the name of a database file, compute the name of its conch file. |
| 6960 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 6961 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 6962 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 6963 | ** |
| 6964 | ** The caller is responsible for ensuring that the allocated memory |
| 6965 | ** space is eventually freed. |
| 6966 | ** |
| 6967 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 6968 | */ |
| 6969 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 6970 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6971 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6972 | char *conchPath; /* buffer in which to construct conch name */ |
| 6973 | |
| 6974 | /* Allocate space for the conch filename and initialize the name to |
| 6975 | ** the name of the original database file. */ |
| 6976 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 6977 | if( conchPath==0 ){ |
| 6978 | return SQLITE_NOMEM; |
| 6979 | } |
| 6980 | memcpy(conchPath, dbPath, len+1); |
| 6981 | |
| 6982 | /* now insert a "." before the last / character */ |
| 6983 | for( i=(len-1); i>=0; i-- ){ |
| 6984 | if( conchPath[i]=='/' ){ |
| 6985 | i++; |
| 6986 | break; |
| 6987 | } |
| 6988 | } |
| 6989 | conchPath[i]='.'; |
| 6990 | while ( i<len ){ |
| 6991 | conchPath[i+1]=dbPath[i]; |
| 6992 | i++; |
| 6993 | } |
| 6994 | |
| 6995 | /* append the "-conch" suffix to the file */ |
| 6996 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6997 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6998 | |
| 6999 | return SQLITE_OK; |
| 7000 | } |
| 7001 | |
| 7002 | |
| 7003 | /* Takes a fully configured proxy locking-style unix file and switches |
| 7004 | ** the local lock file path |
| 7005 | */ |
| 7006 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 7007 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7008 | char *oldPath = pCtx->lockProxyPath; |
| 7009 | int rc = SQLITE_OK; |
| 7010 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7011 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7012 | return SQLITE_BUSY; |
| 7013 | } |
| 7014 | |
| 7015 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 7016 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 7017 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 7018 | return SQLITE_OK; |
| 7019 | }else{ |
| 7020 | unixFile *lockProxy = pCtx->lockProxy; |
| 7021 | pCtx->lockProxy=NULL; |
| 7022 | pCtx->conchHeld = 0; |
| 7023 | if( lockProxy!=NULL ){ |
| 7024 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 7025 | if( rc ) return rc; |
| 7026 | sqlite3_free(lockProxy); |
| 7027 | } |
| 7028 | sqlite3_free(oldPath); |
| 7029 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 7030 | } |
| 7031 | |
| 7032 | return rc; |
| 7033 | } |
| 7034 | |
| 7035 | /* |
| 7036 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 7037 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 7038 | ** |
| 7039 | ** This routine find the filename associated with pFile and writes it |
| 7040 | ** int dbPath. |
| 7041 | */ |
| 7042 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7043 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7044 | if( pFile->pMethod == &afpIoMethods ){ |
| 7045 | /* afp style keeps a reference to the db path in the filePath field |
| 7046 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7047 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7048 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 7049 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7050 | #endif |
| 7051 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 7052 | /* dot lock style uses the locking context to store the dot lock |
| 7053 | ** file path */ |
| 7054 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 7055 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 7056 | }else{ |
| 7057 | /* all other styles use the locking context to store the db file path */ |
| 7058 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7059 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7060 | } |
| 7061 | return SQLITE_OK; |
| 7062 | } |
| 7063 | |
| 7064 | /* |
| 7065 | ** Takes an already filled in unix file and alters it so all file locking |
| 7066 | ** will be performed on the local proxy lock file. The following fields |
| 7067 | ** are preserved in the locking context so that they can be restored and |
| 7068 | ** the unix structure properly cleaned up at close time: |
| 7069 | ** ->lockingContext |
| 7070 | ** ->pMethod |
| 7071 | */ |
| 7072 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 7073 | proxyLockingContext *pCtx; |
| 7074 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 7075 | char *lockPath=NULL; |
| 7076 | int rc = SQLITE_OK; |
| 7077 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7078 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7079 | return SQLITE_BUSY; |
| 7080 | } |
| 7081 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 7082 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 7083 | lockPath=NULL; |
| 7084 | }else{ |
| 7085 | lockPath=(char *)path; |
| 7086 | } |
| 7087 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7088 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 7089 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7090 | |
| 7091 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 7092 | if( pCtx==0 ){ |
| 7093 | return SQLITE_NOMEM; |
| 7094 | } |
| 7095 | memset(pCtx, 0, sizeof(*pCtx)); |
| 7096 | |
| 7097 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 7098 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7099 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 7100 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 7101 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 7102 | ** (c) the file system is read-only, then enable no-locking access. |
| 7103 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 7104 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 7105 | */ |
| 7106 | struct statfs fsInfo; |
| 7107 | struct stat conchInfo; |
| 7108 | int goLockless = 0; |
| 7109 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7110 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7111 | int err = errno; |
| 7112 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 7113 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 7114 | } |
| 7115 | } |
| 7116 | if( goLockless ){ |
| 7117 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 7118 | rc = SQLITE_OK; |
| 7119 | } |
| 7120 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7121 | } |
| 7122 | if( rc==SQLITE_OK && lockPath ){ |
| 7123 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 7124 | } |
| 7125 | |
| 7126 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7127 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 7128 | if( pCtx->dbPath==NULL ){ |
| 7129 | rc = SQLITE_NOMEM; |
| 7130 | } |
| 7131 | } |
| 7132 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7133 | /* all memory is allocated, proxys are created and assigned, |
| 7134 | ** switch the locking context and pMethod then return. |
| 7135 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7136 | pCtx->oldLockingContext = pFile->lockingContext; |
| 7137 | pFile->lockingContext = pCtx; |
| 7138 | pCtx->pOldMethod = pFile->pMethod; |
| 7139 | pFile->pMethod = &proxyIoMethods; |
| 7140 | }else{ |
| 7141 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7142 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7143 | sqlite3_free(pCtx->conchFile); |
| 7144 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7145 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7146 | sqlite3_free(pCtx->conchFilePath); |
| 7147 | sqlite3_free(pCtx); |
| 7148 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7149 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 7150 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7151 | return rc; |
| 7152 | } |
| 7153 | |
| 7154 | |
| 7155 | /* |
| 7156 | ** This routine handles sqlite3_file_control() calls that are specific |
| 7157 | ** to proxy locking. |
| 7158 | */ |
| 7159 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 7160 | switch( op ){ |
| 7161 | case SQLITE_GET_LOCKPROXYFILE: { |
| 7162 | unixFile *pFile = (unixFile*)id; |
| 7163 | if( pFile->pMethod == &proxyIoMethods ){ |
| 7164 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7165 | proxyTakeConch(pFile); |
| 7166 | if( pCtx->lockProxyPath ){ |
| 7167 | *(const char **)pArg = pCtx->lockProxyPath; |
| 7168 | }else{ |
| 7169 | *(const char **)pArg = ":auto: (not held)"; |
| 7170 | } |
| 7171 | } else { |
| 7172 | *(const char **)pArg = NULL; |
| 7173 | } |
| 7174 | return SQLITE_OK; |
| 7175 | } |
| 7176 | case SQLITE_SET_LOCKPROXYFILE: { |
| 7177 | unixFile *pFile = (unixFile*)id; |
| 7178 | int rc = SQLITE_OK; |
| 7179 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 7180 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 7181 | if( isProxyStyle ){ |
| 7182 | /* turn off proxy locking - not supported */ |
| 7183 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 7184 | }else{ |
| 7185 | /* turn off proxy locking - already off - NOOP */ |
| 7186 | rc = SQLITE_OK; |
| 7187 | } |
| 7188 | }else{ |
| 7189 | const char *proxyPath = (const char *)pArg; |
| 7190 | if( isProxyStyle ){ |
| 7191 | proxyLockingContext *pCtx = |
| 7192 | (proxyLockingContext*)pFile->lockingContext; |
| 7193 | if( !strcmp(pArg, ":auto:") |
| 7194 | || (pCtx->lockProxyPath && |
| 7195 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 7196 | ){ |
| 7197 | rc = SQLITE_OK; |
| 7198 | }else{ |
| 7199 | rc = switchLockProxyPath(pFile, proxyPath); |
| 7200 | } |
| 7201 | }else{ |
| 7202 | /* turn on proxy file locking */ |
| 7203 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 7204 | } |
| 7205 | } |
| 7206 | return rc; |
| 7207 | } |
| 7208 | default: { |
| 7209 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 7210 | } |
| 7211 | } |
| 7212 | /*NOTREACHED*/ |
| 7213 | return SQLITE_ERROR; |
| 7214 | } |
| 7215 | |
| 7216 | /* |
| 7217 | ** Within this division (the proxying locking implementation) the procedures |
| 7218 | ** above this point are all utilities. The lock-related methods of the |
| 7219 | ** proxy-locking sqlite3_io_method object follow. |
| 7220 | */ |
| 7221 | |
| 7222 | |
| 7223 | /* |
| 7224 | ** This routine checks if there is a RESERVED lock held on the specified |
| 7225 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 7226 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7227 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7228 | */ |
| 7229 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 7230 | unixFile *pFile = (unixFile*)id; |
| 7231 | int rc = proxyTakeConch(pFile); |
| 7232 | if( rc==SQLITE_OK ){ |
| 7233 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7234 | if( pCtx->conchHeld>0 ){ |
| 7235 | unixFile *proxy = pCtx->lockProxy; |
| 7236 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 7237 | }else{ /* conchHeld < 0 is lockless */ |
| 7238 | pResOut=0; |
| 7239 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7240 | } |
| 7241 | return rc; |
| 7242 | } |
| 7243 | |
| 7244 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7245 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7246 | ** of the following: |
| 7247 | ** |
| 7248 | ** (1) SHARED_LOCK |
| 7249 | ** (2) RESERVED_LOCK |
| 7250 | ** (3) PENDING_LOCK |
| 7251 | ** (4) EXCLUSIVE_LOCK |
| 7252 | ** |
| 7253 | ** Sometimes when requesting one lock state, additional lock states |
| 7254 | ** are inserted in between. The locking might fail on one of the later |
| 7255 | ** transitions leaving the lock state different from what it started but |
| 7256 | ** still short of its goal. The following chart shows the allowed |
| 7257 | ** transitions and the inserted intermediate states: |
| 7258 | ** |
| 7259 | ** UNLOCKED -> SHARED |
| 7260 | ** SHARED -> RESERVED |
| 7261 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7262 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7263 | ** PENDING -> EXCLUSIVE |
| 7264 | ** |
| 7265 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7266 | ** routine to lower a locking level. |
| 7267 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7268 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7269 | unixFile *pFile = (unixFile*)id; |
| 7270 | int rc = proxyTakeConch(pFile); |
| 7271 | if( rc==SQLITE_OK ){ |
| 7272 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7273 | if( pCtx->conchHeld>0 ){ |
| 7274 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7275 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7276 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7277 | }else{ |
| 7278 | /* conchHeld < 0 is lockless */ |
| 7279 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7280 | } |
| 7281 | return rc; |
| 7282 | } |
| 7283 | |
| 7284 | |
| 7285 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7286 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7287 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7288 | ** |
| 7289 | ** If the locking level of the file descriptor is already at or below |
| 7290 | ** the requested locking level, this routine is a no-op. |
| 7291 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7292 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7293 | unixFile *pFile = (unixFile*)id; |
| 7294 | int rc = proxyTakeConch(pFile); |
| 7295 | if( rc==SQLITE_OK ){ |
| 7296 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7297 | if( pCtx->conchHeld>0 ){ |
| 7298 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7299 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7300 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7301 | }else{ |
| 7302 | /* conchHeld < 0 is lockless */ |
| 7303 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7304 | } |
| 7305 | return rc; |
| 7306 | } |
| 7307 | |
| 7308 | /* |
| 7309 | ** Close a file that uses proxy locks. |
| 7310 | */ |
| 7311 | static int proxyClose(sqlite3_file *id) { |
| 7312 | if( id ){ |
| 7313 | unixFile *pFile = (unixFile*)id; |
| 7314 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7315 | unixFile *lockProxy = pCtx->lockProxy; |
| 7316 | unixFile *conchFile = pCtx->conchFile; |
| 7317 | int rc = SQLITE_OK; |
| 7318 | |
| 7319 | if( lockProxy ){ |
| 7320 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7321 | if( rc ) return rc; |
| 7322 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7323 | if( rc ) return rc; |
| 7324 | sqlite3_free(lockProxy); |
| 7325 | pCtx->lockProxy = 0; |
| 7326 | } |
| 7327 | if( conchFile ){ |
| 7328 | if( pCtx->conchHeld ){ |
| 7329 | rc = proxyReleaseConch(pFile); |
| 7330 | if( rc ) return rc; |
| 7331 | } |
| 7332 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7333 | if( rc ) return rc; |
| 7334 | sqlite3_free(conchFile); |
| 7335 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7336 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7337 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7338 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7339 | /* restore the original locking context and pMethod then close it */ |
| 7340 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7341 | pFile->pMethod = pCtx->pOldMethod; |
| 7342 | sqlite3_free(pCtx); |
| 7343 | return pFile->pMethod->xClose(id); |
| 7344 | } |
| 7345 | return SQLITE_OK; |
| 7346 | } |
| 7347 | |
| 7348 | |
| 7349 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7350 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7351 | /* |
| 7352 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7353 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7354 | ** restricted to MacOSX. |
| 7355 | ** |
| 7356 | ** |
| 7357 | ******************* End of the proxy lock implementation ********************** |
| 7358 | ******************************************************************************/ |
| 7359 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7360 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7361 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7362 | ** |
| 7363 | ** This routine registers all VFS implementations for unix-like operating |
| 7364 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7365 | ** should be the only routines in this file that are visible from other |
| 7366 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7367 | ** |
| 7368 | ** This routine is called once during SQLite initialization and by a |
| 7369 | ** single thread. The memory allocation and mutex subsystems have not |
| 7370 | ** necessarily been initialized when this routine is called, and so they |
| 7371 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7372 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7373 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7374 | /* |
| 7375 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7376 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7377 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7378 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7379 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7380 | ** when compiling with -pedantic-errors on GCC.) |
| 7381 | ** |
| 7382 | ** The FINDER parameter to this macro is the name of the pointer to the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7383 | ** finder-function. The finder-function returns a pointer to the |
| 7384 | ** sqlite_io_methods object that implements the desired locking |
| 7385 | ** behaviors. See the division above that contains the IOMETHODS |
| 7386 | ** macro for addition information on finder-functions. |
| 7387 | ** |
| 7388 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7389 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7390 | ** more than that; it looks at the filesystem type that hosts the |
| 7391 | ** database file and tries to choose an locking method appropriate for |
| 7392 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7393 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7394 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7395 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7396 | sizeof(unixFile), /* szOsFile */ \ |
| 7397 | MAX_PATHNAME, /* mxPathname */ \ |
| 7398 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7399 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7400 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7401 | unixOpen, /* xOpen */ \ |
| 7402 | unixDelete, /* xDelete */ \ |
| 7403 | unixAccess, /* xAccess */ \ |
| 7404 | unixFullPathname, /* xFullPathname */ \ |
| 7405 | unixDlOpen, /* xDlOpen */ \ |
| 7406 | unixDlError, /* xDlError */ \ |
| 7407 | unixDlSym, /* xDlSym */ \ |
| 7408 | unixDlClose, /* xDlClose */ \ |
| 7409 | unixRandomness, /* xRandomness */ \ |
| 7410 | unixSleep, /* xSleep */ \ |
| 7411 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7412 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7413 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7414 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7415 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7416 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7417 | } |
| 7418 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7419 | /* |
| 7420 | ** All default VFSes for unix are contained in the following array. |
| 7421 | ** |
| 7422 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7423 | ** by the SQLite core when the VFS is registered. So the following |
| 7424 | ** array cannot be const. |
| 7425 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7426 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7427 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7428 | UNIXVFS("unix", autolockIoFinder ), |
| 7429 | #else |
| 7430 | UNIXVFS("unix", posixIoFinder ), |
| 7431 | #endif |
| 7432 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7433 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7434 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7435 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7436 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7437 | #endif |
| 7438 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7439 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7440 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7441 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7442 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7443 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7444 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7445 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7446 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7447 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7448 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7449 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7450 | unsigned int i; /* Loop counter */ |
| 7451 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7452 | /* Double-check that the aSyscall[] array has been constructed |
| 7453 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 7454 | assert( ArraySize(aSyscall)==24 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7455 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7456 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7457 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7458 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7459 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7460 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7461 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7462 | |
| 7463 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7464 | ** Shutdown the operating system interface. |
| 7465 | ** |
| 7466 | ** Some operating systems might need to do some cleanup in this routine, |
| 7467 | ** to release dynamically allocated objects. But not on unix. |
| 7468 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7469 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7470 | int sqlite3_os_end(void){ |
| 7471 | return SQLITE_OK; |
| 7472 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 7473 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 7474 | #endif /* SQLITE_OS_UNIX */ |